Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1 | /* |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2 | |
| 3 | Unicode implementation based on original code by Fredrik Lundh, |
Fred Drake | 785d14f | 2000-05-09 19:54:43 +0000 | [diff] [blame] | 4 | modified by Marc-Andre Lemburg <mal@lemburg.com> according to the |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5 | Unicode Integration Proposal (see file Misc/unicode.txt). |
| 6 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7 | Major speed upgrades to the method implementations at the Reykjavik |
| 8 | NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke. |
| 9 | |
Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 10 | Copyright (c) Corporation for National Research Initiatives. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 12 | -------------------------------------------------------------------- |
| 13 | The original string type implementation is: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 15 | Copyright (c) 1999 by Secret Labs AB |
| 16 | Copyright (c) 1999 by Fredrik Lundh |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 17 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 18 | By obtaining, using, and/or copying this software and/or its |
| 19 | associated documentation, you agree that you have read, understood, |
| 20 | and will comply with the following terms and conditions: |
| 21 | |
| 22 | Permission to use, copy, modify, and distribute this software and its |
| 23 | associated documentation for any purpose and without fee is hereby |
| 24 | granted, provided that the above copyright notice appears in all |
| 25 | copies, and that both that copyright notice and this permission notice |
| 26 | appear in supporting documentation, and that the name of Secret Labs |
| 27 | AB or the author not be used in advertising or publicity pertaining to |
| 28 | distribution of the software without specific, written prior |
| 29 | permission. |
| 30 | |
| 31 | SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 32 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 33 | FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR |
| 34 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 35 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 36 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 37 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 38 | -------------------------------------------------------------------- |
| 39 | |
| 40 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 41 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 42 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 43 | #include "Python.h" |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 44 | #include "ucnhash.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 45 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 46 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 47 | #include <windows.h> |
| 48 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 49 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 50 | /* Limit for the Unicode object free list */ |
| 51 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 52 | #define PyUnicode_MAXFREELIST 1024 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 53 | |
| 54 | /* Limit for the Unicode object free list stay alive optimization. |
| 55 | |
| 56 | The implementation will keep allocated Unicode memory intact for |
| 57 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 58 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 59 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 60 | At worst this will result in PyUnicode_MAXFREELIST * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 61 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 62 | malloc()-overhead) bytes of unused garbage. |
| 63 | |
| 64 | Setting the limit to 0 effectively turns the feature off. |
| 65 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 66 | Note: This is an experimental feature ! If you get core dumps when |
| 67 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 68 | |
| 69 | */ |
| 70 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 71 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 72 | |
| 73 | /* Endianness switches; defaults to little endian */ |
| 74 | |
| 75 | #ifdef WORDS_BIGENDIAN |
| 76 | # define BYTEORDER_IS_BIG_ENDIAN |
| 77 | #else |
| 78 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 79 | #endif |
| 80 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 81 | /* --- Globals ------------------------------------------------------------ |
| 82 | |
| 83 | The globals are initialized by the _PyUnicode_Init() API and should |
| 84 | not be used before calling that API. |
| 85 | |
| 86 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 87 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 88 | |
| 89 | #ifdef __cplusplus |
| 90 | extern "C" { |
| 91 | #endif |
| 92 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 93 | /* This dictionary holds all interned unicode strings. Note that references |
| 94 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 95 | When the interned string reaches a refcnt of 0 the string deallocation |
| 96 | function will delete the reference from this dictionary. |
| 97 | |
| 98 | Another way to look at this is that to say that the actual reference |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 99 | count of a string is: s->ob_refcnt + (s->state ? 2 : 0) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 100 | */ |
| 101 | static PyObject *interned; |
| 102 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 103 | /* Free list for Unicode objects */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 104 | static PyUnicodeObject *free_list; |
| 105 | static int numfree; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 106 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 107 | /* The empty Unicode object is shared to improve performance. */ |
| 108 | static PyUnicodeObject *unicode_empty; |
| 109 | |
| 110 | /* Single character Unicode strings in the Latin-1 range are being |
| 111 | shared as well. */ |
| 112 | static PyUnicodeObject *unicode_latin1[256]; |
| 113 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 114 | /* Fast detection of the most frequent whitespace characters */ |
| 115 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 116 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 117 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 118 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 119 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 120 | /* case 0x000C: * FORM FEED */ |
| 121 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 122 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 123 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 124 | /* case 0x001C: * FILE SEPARATOR */ |
| 125 | /* case 0x001D: * GROUP SEPARATOR */ |
| 126 | /* case 0x001E: * RECORD SEPARATOR */ |
| 127 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 128 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 129 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 130 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 131 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 132 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 133 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 134 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 135 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 136 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 137 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 138 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 139 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 140 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 141 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 142 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 143 | }; |
| 144 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 145 | static PyObject * |
| 146 | unicode_encode_call_errorhandler(const char *errors, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 147 | PyObject **errorHandler,const char *encoding, const char *reason, |
| 148 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 149 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 150 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 151 | static void |
| 152 | raise_encode_exception(PyObject **exceptionObject, |
| 153 | const char *encoding, |
| 154 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 155 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 156 | const char *reason); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 157 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 158 | /* Same for linebreaks */ |
| 159 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 160 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 161 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 162 | /* 0x000B, * LINE TABULATION */ |
| 163 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 164 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 165 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 166 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 167 | /* 0x001C, * FILE SEPARATOR */ |
| 168 | /* 0x001D, * GROUP SEPARATOR */ |
| 169 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 170 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 171 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 172 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 173 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 174 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 175 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 176 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 177 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 178 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 179 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 180 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 181 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 182 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 183 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 187 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 188 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 189 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 190 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 191 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 192 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 193 | /* This is actually an illegal character, so it should |
| 194 | not be passed to unichr. */ |
| 195 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 196 | #endif |
| 197 | } |
| 198 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 199 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 200 | |
| 201 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 202 | to keep things simple, we use a single bitmask, using the least 5 |
| 203 | bits from each unicode characters as the bit index. */ |
| 204 | |
| 205 | /* the linebreak mask is set up by Unicode_Init below */ |
| 206 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 207 | #if LONG_BIT >= 128 |
| 208 | #define BLOOM_WIDTH 128 |
| 209 | #elif LONG_BIT >= 64 |
| 210 | #define BLOOM_WIDTH 64 |
| 211 | #elif LONG_BIT >= 32 |
| 212 | #define BLOOM_WIDTH 32 |
| 213 | #else |
| 214 | #error "LONG_BIT is smaller than 32" |
| 215 | #endif |
| 216 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 217 | #define BLOOM_MASK unsigned long |
| 218 | |
| 219 | static BLOOM_MASK bloom_linebreak; |
| 220 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 221 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 222 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 223 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 224 | #define BLOOM_LINEBREAK(ch) \ |
| 225 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 226 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 227 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 228 | Py_LOCAL_INLINE(BLOOM_MASK) |
| 229 | make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 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 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 243 | Py_LOCAL_INLINE(int) |
| 244 | unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 245 | { |
| 246 | Py_ssize_t i; |
| 247 | |
| 248 | for (i = 0; i < setlen; i++) |
| 249 | if (set[i] == chr) |
| 250 | return 1; |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 255 | #define BLOOM_MEMBER(mask, chr, set, setlen) \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 256 | BLOOM(mask, chr) && unicode_member(chr, set, setlen) |
| 257 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 258 | /* --- Unicode Object ----------------------------------------------------- */ |
| 259 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 260 | static int |
| 261 | unicode_resize(register PyUnicodeObject *unicode, |
| 262 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 263 | { |
| 264 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 265 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 266 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 267 | if (unicode->length == length) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 268 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 269 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 270 | /* Resizing shared object (unicode_empty or single character |
| 271 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 272 | instead ! */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 273 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 274 | if (unicode == unicode_empty || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 275 | (unicode->length == 1 && |
| 276 | unicode->str[0] < 256U && |
| 277 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 278 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 279 | "can't resize shared str objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 280 | return -1; |
| 281 | } |
| 282 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 283 | /* We allocate one more byte to make sure the string is Ux0000 terminated. |
| 284 | The overallocation is also used by fastsearch, which assumes that it's |
| 285 | safe to look at str[length] (without making any assumptions about what |
| 286 | it contains). */ |
| 287 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 288 | oldstr = unicode->str; |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 289 | unicode->str = PyObject_REALLOC(unicode->str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 290 | sizeof(Py_UNICODE) * (length + 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 291 | if (!unicode->str) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 292 | unicode->str = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 293 | PyErr_NoMemory(); |
| 294 | return -1; |
| 295 | } |
| 296 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 297 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 298 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 299 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 300 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 301 | if (unicode->defenc) { |
Georg Brandl | 8ee604b | 2010-07-29 14:23:06 +0000 | [diff] [blame] | 302 | Py_CLEAR(unicode->defenc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 303 | } |
| 304 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 305 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | /* 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] | 310 | Ux0000 terminated; some code (e.g. new_identifier) |
| 311 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 312 | |
| 313 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 314 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 315 | |
| 316 | */ |
| 317 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 318 | static PyUnicodeObject * |
| 319 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 320 | { |
| 321 | register PyUnicodeObject *unicode; |
| 322 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 323 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 324 | if (length == 0 && unicode_empty != NULL) { |
| 325 | Py_INCREF(unicode_empty); |
| 326 | return unicode_empty; |
| 327 | } |
| 328 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 329 | /* Ensure we won't overflow the size. */ |
| 330 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 331 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 332 | } |
| 333 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 334 | /* Unicode freelist & memory allocation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 335 | if (free_list) { |
| 336 | unicode = free_list; |
| 337 | free_list = *(PyUnicodeObject **)unicode; |
| 338 | numfree--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 339 | if (unicode->str) { |
| 340 | /* Keep-Alive optimization: we only upsize the buffer, |
| 341 | never downsize it. */ |
| 342 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 343 | unicode_resize(unicode, length) < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 344 | PyObject_DEL(unicode->str); |
| 345 | unicode->str = NULL; |
| 346 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 347 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 348 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 349 | size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 350 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 351 | } |
| 352 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 353 | } |
| 354 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 355 | size_t new_size; |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 356 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 357 | if (unicode == NULL) |
| 358 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 359 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 360 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 363 | if (!unicode->str) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 364 | PyErr_NoMemory(); |
| 365 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 366 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 367 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 368 | * the caller fails before initializing str -- unicode_resize() |
| 369 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 370 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 371 | * We don't want unicode_resize to read uninitialized memory in |
| 372 | * that case. |
| 373 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 374 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 375 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 376 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 377 | unicode->hash = -1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 378 | unicode->state = 0; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 379 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 380 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 381 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 382 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 383 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 384 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 385 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 386 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 387 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 390 | static void |
| 391 | unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 392 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 393 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 394 | case SSTATE_NOT_INTERNED: |
| 395 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 396 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 397 | case SSTATE_INTERNED_MORTAL: |
| 398 | /* revive dead object temporarily for DelItem */ |
| 399 | Py_REFCNT(unicode) = 3; |
| 400 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 401 | Py_FatalError( |
| 402 | "deletion of interned string failed"); |
| 403 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 404 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 405 | case SSTATE_INTERNED_IMMORTAL: |
| 406 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 407 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 408 | default: |
| 409 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 412 | if (PyUnicode_CheckExact(unicode) && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 413 | numfree < PyUnicode_MAXFREELIST) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 414 | /* Keep-Alive optimization */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 415 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
| 416 | PyObject_DEL(unicode->str); |
| 417 | unicode->str = NULL; |
| 418 | unicode->length = 0; |
| 419 | } |
| 420 | if (unicode->defenc) { |
Georg Brandl | 8ee604b | 2010-07-29 14:23:06 +0000 | [diff] [blame] | 421 | Py_CLEAR(unicode->defenc); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 422 | } |
| 423 | /* Add to free list */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 424 | *(PyUnicodeObject **)unicode = free_list; |
| 425 | free_list = unicode; |
| 426 | numfree++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 427 | } |
| 428 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 429 | PyObject_DEL(unicode->str); |
| 430 | Py_XDECREF(unicode->defenc); |
| 431 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 435 | static int |
| 436 | _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 437 | { |
| 438 | register PyUnicodeObject *v; |
| 439 | |
| 440 | /* Argument checks */ |
| 441 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 442 | PyErr_BadInternalCall(); |
| 443 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 444 | } |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 445 | v = *unicode; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 446 | if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 447 | PyErr_BadInternalCall(); |
| 448 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | /* Resizing unicode_empty and single character objects is not |
| 452 | possible since these are being shared. We simply return a fresh |
| 453 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 454 | if (v->length != length && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 455 | (v == unicode_empty || v->length == 1)) { |
| 456 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 457 | if (w == NULL) |
| 458 | return -1; |
| 459 | Py_UNICODE_COPY(w->str, v->str, |
| 460 | length < v->length ? length : v->length); |
| 461 | Py_DECREF(*unicode); |
| 462 | *unicode = w; |
| 463 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 467 | objects, since we can modify them in-place. */ |
| 468 | return unicode_resize(v, length); |
| 469 | } |
| 470 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 471 | int |
| 472 | PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 473 | { |
| 474 | return _PyUnicode_Resize((PyUnicodeObject **)unicode, length); |
| 475 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 476 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 477 | PyObject * |
| 478 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 479 | { |
| 480 | PyUnicodeObject *unicode; |
| 481 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 482 | /* If the Unicode data is known at construction time, we can apply |
| 483 | some optimizations which share commonly used objects. */ |
| 484 | if (u != NULL) { |
| 485 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 486 | /* Optimization for empty strings */ |
| 487 | if (size == 0 && unicode_empty != NULL) { |
| 488 | Py_INCREF(unicode_empty); |
| 489 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 490 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 491 | |
| 492 | /* Single character Unicode objects in the Latin-1 range are |
| 493 | shared when using this constructor */ |
| 494 | if (size == 1 && *u < 256) { |
| 495 | unicode = unicode_latin1[*u]; |
| 496 | if (!unicode) { |
| 497 | unicode = _PyUnicode_New(1); |
| 498 | if (!unicode) |
| 499 | return NULL; |
| 500 | unicode->str[0] = *u; |
| 501 | unicode_latin1[*u] = unicode; |
| 502 | } |
| 503 | Py_INCREF(unicode); |
| 504 | return (PyObject *)unicode; |
| 505 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 506 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 507 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 508 | unicode = _PyUnicode_New(size); |
| 509 | if (!unicode) |
| 510 | return NULL; |
| 511 | |
| 512 | /* Copy the Unicode data into the new object */ |
| 513 | if (u != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 514 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 515 | |
| 516 | return (PyObject *)unicode; |
| 517 | } |
| 518 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 519 | PyObject * |
| 520 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 521 | { |
| 522 | PyUnicodeObject *unicode; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 523 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 524 | if (size < 0) { |
| 525 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 526 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 527 | return NULL; |
| 528 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 529 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 530 | /* 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] | 531 | some optimizations which share commonly used objects. |
| 532 | Also, this means the input must be UTF-8, so fall back to the |
| 533 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 534 | if (u != NULL) { |
| 535 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 536 | /* Optimization for empty strings */ |
| 537 | if (size == 0 && unicode_empty != NULL) { |
| 538 | Py_INCREF(unicode_empty); |
| 539 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 540 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 541 | |
| 542 | /* Single characters are shared when using this constructor. |
| 543 | Restrict to ASCII, since the input must be UTF-8. */ |
| 544 | if (size == 1 && Py_CHARMASK(*u) < 128) { |
| 545 | unicode = unicode_latin1[Py_CHARMASK(*u)]; |
| 546 | if (!unicode) { |
| 547 | unicode = _PyUnicode_New(1); |
| 548 | if (!unicode) |
| 549 | return NULL; |
| 550 | unicode->str[0] = Py_CHARMASK(*u); |
| 551 | unicode_latin1[Py_CHARMASK(*u)] = unicode; |
| 552 | } |
| 553 | Py_INCREF(unicode); |
| 554 | return (PyObject *)unicode; |
| 555 | } |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 556 | |
| 557 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 560 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 561 | if (!unicode) |
| 562 | return NULL; |
| 563 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 564 | return (PyObject *)unicode; |
| 565 | } |
| 566 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 567 | PyObject * |
| 568 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 569 | { |
| 570 | size_t size = strlen(u); |
| 571 | if (size > PY_SSIZE_T_MAX) { |
| 572 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 573 | return NULL; |
| 574 | } |
| 575 | |
| 576 | return PyUnicode_FromStringAndSize(u, size); |
| 577 | } |
| 578 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 579 | #ifdef HAVE_WCHAR_H |
| 580 | |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 581 | #if (Py_UNICODE_SIZE == 2) && defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
| 582 | # define CONVERT_WCHAR_TO_SURROGATES |
| 583 | #endif |
| 584 | |
| 585 | #ifdef CONVERT_WCHAR_TO_SURROGATES |
| 586 | |
| 587 | /* Here sizeof(wchar_t) is 4 but Py_UNICODE_SIZE == 2, so we need |
| 588 | to convert from UTF32 to UTF16. */ |
| 589 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 590 | PyObject * |
| 591 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 592 | { |
| 593 | PyUnicodeObject *unicode; |
| 594 | register Py_ssize_t i; |
| 595 | Py_ssize_t alloc; |
| 596 | const wchar_t *orig_w; |
| 597 | |
| 598 | if (w == NULL) { |
| 599 | if (size == 0) |
| 600 | return PyUnicode_FromStringAndSize(NULL, 0); |
| 601 | PyErr_BadInternalCall(); |
| 602 | return NULL; |
| 603 | } |
| 604 | |
| 605 | if (size == -1) { |
| 606 | size = wcslen(w); |
| 607 | } |
| 608 | |
| 609 | alloc = size; |
| 610 | orig_w = w; |
| 611 | for (i = size; i > 0; i--) { |
| 612 | if (*w > 0xFFFF) |
| 613 | alloc++; |
| 614 | w++; |
| 615 | } |
| 616 | w = orig_w; |
| 617 | unicode = _PyUnicode_New(alloc); |
| 618 | if (!unicode) |
| 619 | return NULL; |
| 620 | |
| 621 | /* Copy the wchar_t data into the new object */ |
| 622 | { |
| 623 | register Py_UNICODE *u; |
| 624 | u = PyUnicode_AS_UNICODE(unicode); |
| 625 | for (i = size; i > 0; i--) { |
| 626 | if (*w > 0xFFFF) { |
| 627 | wchar_t ordinal = *w++; |
| 628 | ordinal -= 0x10000; |
| 629 | *u++ = 0xD800 | (ordinal >> 10); |
| 630 | *u++ = 0xDC00 | (ordinal & 0x3FF); |
| 631 | } |
| 632 | else |
| 633 | *u++ = *w++; |
| 634 | } |
| 635 | } |
| 636 | return (PyObject *)unicode; |
| 637 | } |
| 638 | |
| 639 | #else |
| 640 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 641 | PyObject * |
| 642 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 643 | { |
| 644 | PyUnicodeObject *unicode; |
| 645 | |
| 646 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 647 | if (size == 0) |
| 648 | return PyUnicode_FromStringAndSize(NULL, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 649 | PyErr_BadInternalCall(); |
| 650 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 653 | if (size == -1) { |
| 654 | size = wcslen(w); |
| 655 | } |
| 656 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 657 | unicode = _PyUnicode_New(size); |
| 658 | if (!unicode) |
| 659 | return NULL; |
| 660 | |
| 661 | /* Copy the wchar_t data into the new object */ |
Daniel Stutzbach | 8515eae | 2010-08-24 21:57:33 +0000 | [diff] [blame] | 662 | #if Py_UNICODE_SIZE == SIZEOF_WCHAR_T |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 663 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 664 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 665 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 666 | register Py_UNICODE *u; |
| 667 | register Py_ssize_t i; |
| 668 | u = PyUnicode_AS_UNICODE(unicode); |
| 669 | for (i = size; i > 0; i--) |
| 670 | *u++ = *w++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 671 | } |
| 672 | #endif |
| 673 | |
| 674 | return (PyObject *)unicode; |
| 675 | } |
| 676 | |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 677 | #endif /* CONVERT_WCHAR_TO_SURROGATES */ |
| 678 | |
| 679 | #undef CONVERT_WCHAR_TO_SURROGATES |
| 680 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 681 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 682 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 683 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 684 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 685 | *fmt++ = '%'; |
| 686 | if (width) { |
| 687 | if (zeropad) |
| 688 | *fmt++ = '0'; |
| 689 | fmt += sprintf(fmt, "%d", width); |
| 690 | } |
| 691 | if (precision) |
| 692 | fmt += sprintf(fmt, ".%d", precision); |
| 693 | if (longflag) |
| 694 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 695 | else if (longlongflag) { |
| 696 | /* longlongflag should only ever be nonzero on machines with |
| 697 | HAVE_LONG_LONG defined */ |
| 698 | #ifdef HAVE_LONG_LONG |
| 699 | char *f = PY_FORMAT_LONG_LONG; |
| 700 | while (*f) |
| 701 | *fmt++ = *f++; |
| 702 | #else |
| 703 | /* we shouldn't ever get here */ |
| 704 | assert(0); |
| 705 | *fmt++ = 'l'; |
| 706 | #endif |
| 707 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 708 | else if (size_tflag) { |
| 709 | char *f = PY_FORMAT_SIZE_T; |
| 710 | while (*f) |
| 711 | *fmt++ = *f++; |
| 712 | } |
| 713 | *fmt++ = c; |
| 714 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 717 | /* helper for PyUnicode_FromFormatV() */ |
| 718 | |
| 719 | static const char* |
| 720 | parse_format_flags(const char *f, |
| 721 | int *p_width, int *p_precision, |
| 722 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 723 | { |
| 724 | int width, precision, longflag, longlongflag, size_tflag; |
| 725 | |
| 726 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 727 | f++; |
| 728 | width = 0; |
| 729 | while (Py_ISDIGIT((unsigned)*f)) |
| 730 | width = (width*10) + *f++ - '0'; |
| 731 | precision = 0; |
| 732 | if (*f == '.') { |
| 733 | f++; |
| 734 | while (Py_ISDIGIT((unsigned)*f)) |
| 735 | precision = (precision*10) + *f++ - '0'; |
| 736 | if (*f == '%') { |
| 737 | /* "%.3%s" => f points to "3" */ |
| 738 | f--; |
| 739 | } |
| 740 | } |
| 741 | if (*f == '\0') { |
| 742 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 743 | f--; |
| 744 | } |
| 745 | if (p_width != NULL) |
| 746 | *p_width = width; |
| 747 | if (p_precision != NULL) |
| 748 | *p_precision = precision; |
| 749 | |
| 750 | /* Handle %ld, %lu, %lld and %llu. */ |
| 751 | longflag = 0; |
| 752 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 753 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 754 | |
| 755 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 756 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 757 | longflag = 1; |
| 758 | ++f; |
| 759 | } |
| 760 | #ifdef HAVE_LONG_LONG |
| 761 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 762 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 763 | longlongflag = 1; |
| 764 | f += 2; |
| 765 | } |
| 766 | #endif |
| 767 | } |
| 768 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 769 | else if (*f == 'z' && (f[1] == 'd' || f[1] == 'u' || f[1] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 770 | size_tflag = 1; |
| 771 | ++f; |
| 772 | } |
| 773 | if (p_longflag != NULL) |
| 774 | *p_longflag = longflag; |
| 775 | if (p_longlongflag != NULL) |
| 776 | *p_longlongflag = longlongflag; |
| 777 | if (p_size_tflag != NULL) |
| 778 | *p_size_tflag = size_tflag; |
| 779 | return f; |
| 780 | } |
| 781 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 782 | #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} |
| 783 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 784 | /* size of fixed-size buffer for formatting single arguments */ |
| 785 | #define ITEM_BUFFER_LEN 21 |
| 786 | /* maximum number of characters required for output of %ld. 21 characters |
| 787 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 788 | #define MAX_LONG_CHARS 21 |
| 789 | /* maximum number of characters required for output of %lld. |
| 790 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 791 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 792 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 793 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 794 | PyObject * |
| 795 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 796 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 797 | va_list count; |
| 798 | Py_ssize_t callcount = 0; |
| 799 | PyObject **callresults = NULL; |
| 800 | PyObject **callresult = NULL; |
| 801 | Py_ssize_t n = 0; |
| 802 | int width = 0; |
| 803 | int precision = 0; |
| 804 | int zeropad; |
| 805 | const char* f; |
| 806 | Py_UNICODE *s; |
| 807 | PyObject *string; |
| 808 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 809 | char buffer[ITEM_BUFFER_LEN+1]; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 810 | /* use abuffer instead of buffer, if we need more space |
| 811 | * (which can happen if there's a format specifier with width). */ |
| 812 | char *abuffer = NULL; |
| 813 | char *realbuffer; |
| 814 | Py_ssize_t abuffersize = 0; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 815 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 816 | const char *copy; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 817 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 818 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 819 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 820 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 821 | * PyUnicode_DecodeUTF8() for these objects once during step 3 and put the |
| 822 | * result in an array) */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 823 | for (f = format; *f; f++) { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 824 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 825 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 826 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 827 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 828 | ++callcount; |
| 829 | } |
Benjamin Peterson | 9be0b2e | 2010-09-12 03:40:54 +0000 | [diff] [blame] | 830 | else if (128 <= (unsigned char)*f) { |
| 831 | PyErr_Format(PyExc_ValueError, |
| 832 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
Victor Stinner | 4c7db31 | 2010-09-12 07:51:18 +0000 | [diff] [blame] | 833 | "string, got a non-ASCII byte: 0x%02x", |
Benjamin Peterson | 9be0b2e | 2010-09-12 03:40:54 +0000 | [diff] [blame] | 834 | (unsigned char)*f); |
Benjamin Peterson | d4ac96a | 2010-09-12 16:40:53 +0000 | [diff] [blame] | 835 | return NULL; |
Benjamin Peterson | 9be0b2e | 2010-09-12 03:40:54 +0000 | [diff] [blame] | 836 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 837 | } |
| 838 | /* step 2: allocate memory for the results of |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 839 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 840 | if (callcount) { |
| 841 | callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); |
| 842 | if (!callresults) { |
| 843 | PyErr_NoMemory(); |
| 844 | return NULL; |
| 845 | } |
| 846 | callresult = callresults; |
| 847 | } |
| 848 | /* step 3: figure out how large a buffer we need */ |
| 849 | for (f = format; *f; f++) { |
| 850 | if (*f == '%') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 851 | #ifdef HAVE_LONG_LONG |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 852 | int longlongflag; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 853 | #endif |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 854 | const char* p; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 855 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 856 | p = f; |
| 857 | f = parse_format_flags(f, &width, NULL, |
| 858 | NULL, &longlongflag, NULL); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 859 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 860 | switch (*f) { |
| 861 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 862 | { |
| 863 | #ifndef Py_UNICODE_WIDE |
| 864 | int ordinal = va_arg(count, int); |
| 865 | if (ordinal > 0xffff) |
| 866 | n += 2; |
| 867 | else |
| 868 | n++; |
| 869 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 870 | (void)va_arg(count, int); |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 871 | n++; |
| 872 | #endif |
| 873 | break; |
| 874 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 875 | case '%': |
| 876 | n++; |
| 877 | break; |
| 878 | case 'd': case 'u': case 'i': case 'x': |
| 879 | (void) va_arg(count, int); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 880 | #ifdef HAVE_LONG_LONG |
| 881 | if (longlongflag) { |
| 882 | if (width < MAX_LONG_LONG_CHARS) |
| 883 | width = MAX_LONG_LONG_CHARS; |
| 884 | } |
| 885 | else |
| 886 | #endif |
| 887 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 888 | including sign. Decimal takes the most space. This |
| 889 | isn't enough for octal. If a width is specified we |
| 890 | need more (which we allocate later). */ |
| 891 | if (width < MAX_LONG_CHARS) |
| 892 | width = MAX_LONG_CHARS; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 893 | n += width; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 894 | /* XXX should allow for large precision here too. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 895 | if (abuffersize < width) |
| 896 | abuffersize = width; |
| 897 | break; |
| 898 | case 's': |
| 899 | { |
| 900 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 901 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 902 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 903 | if (!str) |
| 904 | goto fail; |
| 905 | n += PyUnicode_GET_SIZE(str); |
| 906 | /* Remember the str and switch to the next slot */ |
| 907 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 908 | break; |
| 909 | } |
| 910 | case 'U': |
| 911 | { |
| 912 | PyObject *obj = va_arg(count, PyObject *); |
| 913 | assert(obj && PyUnicode_Check(obj)); |
| 914 | n += PyUnicode_GET_SIZE(obj); |
| 915 | break; |
| 916 | } |
| 917 | case 'V': |
| 918 | { |
| 919 | PyObject *obj = va_arg(count, PyObject *); |
| 920 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 921 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 922 | assert(obj || str); |
| 923 | assert(!obj || PyUnicode_Check(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 924 | if (obj) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 925 | n += PyUnicode_GET_SIZE(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 926 | *callresult++ = NULL; |
| 927 | } |
| 928 | else { |
| 929 | str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace"); |
| 930 | if (!str_obj) |
| 931 | goto fail; |
| 932 | n += PyUnicode_GET_SIZE(str_obj); |
| 933 | *callresult++ = str_obj; |
| 934 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 935 | break; |
| 936 | } |
| 937 | case 'S': |
| 938 | { |
| 939 | PyObject *obj = va_arg(count, PyObject *); |
| 940 | PyObject *str; |
| 941 | assert(obj); |
| 942 | str = PyObject_Str(obj); |
| 943 | if (!str) |
| 944 | goto fail; |
| 945 | n += PyUnicode_GET_SIZE(str); |
| 946 | /* Remember the str and switch to the next slot */ |
| 947 | *callresult++ = str; |
| 948 | break; |
| 949 | } |
| 950 | case 'R': |
| 951 | { |
| 952 | PyObject *obj = va_arg(count, PyObject *); |
| 953 | PyObject *repr; |
| 954 | assert(obj); |
| 955 | repr = PyObject_Repr(obj); |
| 956 | if (!repr) |
| 957 | goto fail; |
| 958 | n += PyUnicode_GET_SIZE(repr); |
| 959 | /* Remember the repr and switch to the next slot */ |
| 960 | *callresult++ = repr; |
| 961 | break; |
| 962 | } |
| 963 | case 'A': |
| 964 | { |
| 965 | PyObject *obj = va_arg(count, PyObject *); |
| 966 | PyObject *ascii; |
| 967 | assert(obj); |
| 968 | ascii = PyObject_ASCII(obj); |
| 969 | if (!ascii) |
| 970 | goto fail; |
| 971 | n += PyUnicode_GET_SIZE(ascii); |
| 972 | /* Remember the repr and switch to the next slot */ |
| 973 | *callresult++ = ascii; |
| 974 | break; |
| 975 | } |
| 976 | case 'p': |
| 977 | (void) va_arg(count, int); |
| 978 | /* maximum 64-bit pointer representation: |
| 979 | * 0xffffffffffffffff |
| 980 | * so 19 characters is enough. |
| 981 | * XXX I count 18 -- what's the extra for? |
| 982 | */ |
| 983 | n += 19; |
| 984 | break; |
| 985 | default: |
| 986 | /* if we stumble upon an unknown |
| 987 | formatting code, copy the rest of |
| 988 | the format string to the output |
| 989 | string. (we cannot just skip the |
| 990 | code, since there's no way to know |
| 991 | what's in the argument list) */ |
| 992 | n += strlen(p); |
| 993 | goto expand; |
| 994 | } |
| 995 | } else |
| 996 | n++; |
| 997 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 998 | expand: |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 999 | if (abuffersize > ITEM_BUFFER_LEN) { |
| 1000 | /* add 1 for sprintf's trailing null byte */ |
| 1001 | abuffer = PyObject_Malloc(abuffersize + 1); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1002 | if (!abuffer) { |
| 1003 | PyErr_NoMemory(); |
| 1004 | goto fail; |
| 1005 | } |
| 1006 | realbuffer = abuffer; |
| 1007 | } |
| 1008 | else |
| 1009 | realbuffer = buffer; |
| 1010 | /* step 4: fill the buffer */ |
| 1011 | /* Since we've analyzed how much space we need for the worst case, |
| 1012 | we don't have to resize the string. |
| 1013 | There can be no errors beyond this point. */ |
| 1014 | string = PyUnicode_FromUnicode(NULL, n); |
| 1015 | if (!string) |
| 1016 | goto fail; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1017 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1018 | s = PyUnicode_AS_UNICODE(string); |
| 1019 | callresult = callresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1020 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1021 | for (f = format; *f; f++) { |
| 1022 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1023 | const char* p; |
| 1024 | int longflag; |
| 1025 | int longlongflag; |
| 1026 | int size_tflag; |
| 1027 | |
| 1028 | p = f; |
| 1029 | zeropad = (f[1] == '0'); |
| 1030 | f = parse_format_flags(f, &width, &precision, |
| 1031 | &longflag, &longlongflag, &size_tflag); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1032 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1033 | switch (*f) { |
| 1034 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 1035 | { |
| 1036 | int ordinal = va_arg(vargs, int); |
| 1037 | #ifndef Py_UNICODE_WIDE |
| 1038 | if (ordinal > 0xffff) { |
| 1039 | ordinal -= 0x10000; |
| 1040 | *s++ = 0xD800 | (ordinal >> 10); |
| 1041 | *s++ = 0xDC00 | (ordinal & 0x3FF); |
| 1042 | } else |
| 1043 | #endif |
| 1044 | *s++ = ordinal; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1045 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 1046 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1047 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1048 | case 'd': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1049 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1050 | width, precision, *f); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1051 | if (longflag) |
| 1052 | sprintf(realbuffer, fmt, va_arg(vargs, long)); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1053 | #ifdef HAVE_LONG_LONG |
| 1054 | else if (longlongflag) |
| 1055 | sprintf(realbuffer, fmt, va_arg(vargs, PY_LONG_LONG)); |
| 1056 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1057 | else if (size_tflag) |
| 1058 | sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
| 1059 | else |
| 1060 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 1061 | appendstring(realbuffer); |
| 1062 | break; |
| 1063 | case 'u': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1064 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1065 | width, precision, 'u'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1066 | if (longflag) |
| 1067 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1068 | #ifdef HAVE_LONG_LONG |
| 1069 | else if (longlongflag) |
| 1070 | sprintf(realbuffer, fmt, va_arg(vargs, |
| 1071 | unsigned PY_LONG_LONG)); |
| 1072 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1073 | else if (size_tflag) |
| 1074 | sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
| 1075 | else |
| 1076 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
| 1077 | appendstring(realbuffer); |
| 1078 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1079 | case 'x': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1080 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1081 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 1082 | appendstring(realbuffer); |
| 1083 | break; |
| 1084 | case 's': |
| 1085 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1086 | /* unused, since we already have the result */ |
| 1087 | (void) va_arg(vargs, char *); |
| 1088 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(*callresult), |
| 1089 | PyUnicode_GET_SIZE(*callresult)); |
| 1090 | s += PyUnicode_GET_SIZE(*callresult); |
| 1091 | /* We're done with the unicode()/repr() => forget it */ |
| 1092 | Py_DECREF(*callresult); |
| 1093 | /* switch to next unicode()/repr() result */ |
| 1094 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1095 | break; |
| 1096 | } |
| 1097 | case 'U': |
| 1098 | { |
| 1099 | PyObject *obj = va_arg(vargs, PyObject *); |
| 1100 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 1101 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 1102 | s += size; |
| 1103 | break; |
| 1104 | } |
| 1105 | case 'V': |
| 1106 | { |
| 1107 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1108 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1109 | if (obj) { |
| 1110 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 1111 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 1112 | s += size; |
| 1113 | } else { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1114 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(*callresult), |
| 1115 | PyUnicode_GET_SIZE(*callresult)); |
| 1116 | s += PyUnicode_GET_SIZE(*callresult); |
| 1117 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1118 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1119 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1120 | break; |
| 1121 | } |
| 1122 | case 'S': |
| 1123 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 1124 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1125 | { |
| 1126 | Py_UNICODE *ucopy; |
| 1127 | Py_ssize_t usize; |
| 1128 | Py_ssize_t upos; |
| 1129 | /* unused, since we already have the result */ |
| 1130 | (void) va_arg(vargs, PyObject *); |
| 1131 | ucopy = PyUnicode_AS_UNICODE(*callresult); |
| 1132 | usize = PyUnicode_GET_SIZE(*callresult); |
| 1133 | for (upos = 0; upos<usize;) |
| 1134 | *s++ = ucopy[upos++]; |
| 1135 | /* We're done with the unicode()/repr() => forget it */ |
| 1136 | Py_DECREF(*callresult); |
| 1137 | /* switch to next unicode()/repr() result */ |
| 1138 | ++callresult; |
| 1139 | break; |
| 1140 | } |
| 1141 | case 'p': |
| 1142 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 1143 | /* %p is ill-defined: ensure leading 0x. */ |
| 1144 | if (buffer[1] == 'X') |
| 1145 | buffer[1] = 'x'; |
| 1146 | else if (buffer[1] != 'x') { |
| 1147 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 1148 | buffer[0] = '0'; |
| 1149 | buffer[1] = 'x'; |
| 1150 | } |
| 1151 | appendstring(buffer); |
| 1152 | break; |
| 1153 | case '%': |
| 1154 | *s++ = '%'; |
| 1155 | break; |
| 1156 | default: |
| 1157 | appendstring(p); |
| 1158 | goto end; |
| 1159 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 1160 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 1161 | else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1162 | *s++ = *f; |
| 1163 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1164 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1165 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1166 | if (callresults) |
| 1167 | PyObject_Free(callresults); |
| 1168 | if (abuffer) |
| 1169 | PyObject_Free(abuffer); |
| 1170 | PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
| 1171 | return string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1172 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1173 | if (callresults) { |
| 1174 | PyObject **callresult2 = callresults; |
| 1175 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1176 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1177 | ++callresult2; |
| 1178 | } |
| 1179 | PyObject_Free(callresults); |
| 1180 | } |
| 1181 | if (abuffer) |
| 1182 | PyObject_Free(abuffer); |
| 1183 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | #undef appendstring |
| 1187 | |
| 1188 | PyObject * |
| 1189 | PyUnicode_FromFormat(const char *format, ...) |
| 1190 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1191 | PyObject* ret; |
| 1192 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1193 | |
| 1194 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1195 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1196 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1197 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1198 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1199 | ret = PyUnicode_FromFormatV(format, vargs); |
| 1200 | va_end(vargs); |
| 1201 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1204 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 1205 | convert a Unicode object to a wide character string. |
| 1206 | |
| 1207 | - If w is NULL: return the number of wide characters (including the nul |
| 1208 | character) required to convert the unicode object. Ignore size argument. |
| 1209 | |
| 1210 | - Otherwise: return the number of wide characters (excluding the nul |
| 1211 | character) written into w. Write at most size wide characters (including |
| 1212 | the nul character). */ |
| 1213 | static Py_ssize_t |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1214 | unicode_aswidechar(PyUnicodeObject *unicode, |
| 1215 | wchar_t *w, |
| 1216 | Py_ssize_t size) |
| 1217 | { |
| 1218 | #if Py_UNICODE_SIZE == SIZEOF_WCHAR_T |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1219 | Py_ssize_t res; |
| 1220 | if (w != NULL) { |
| 1221 | res = PyUnicode_GET_SIZE(unicode); |
| 1222 | if (size > res) |
| 1223 | size = res + 1; |
| 1224 | else |
| 1225 | res = size; |
| 1226 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 1227 | return res; |
| 1228 | } |
| 1229 | else |
| 1230 | return PyUnicode_GET_SIZE(unicode) + 1; |
| 1231 | #elif Py_UNICODE_SIZE == 2 && SIZEOF_WCHAR_T == 4 |
| 1232 | register const Py_UNICODE *u; |
| 1233 | const Py_UNICODE *uend; |
| 1234 | const wchar_t *worig, *wend; |
| 1235 | Py_ssize_t nchar; |
| 1236 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1237 | u = PyUnicode_AS_UNICODE(unicode); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1238 | uend = u + PyUnicode_GET_SIZE(unicode); |
| 1239 | if (w != NULL) { |
| 1240 | worig = w; |
| 1241 | wend = w + size; |
| 1242 | while (u != uend && w != wend) { |
| 1243 | if (0xD800 <= u[0] && u[0] <= 0xDBFF |
| 1244 | && 0xDC00 <= u[1] && u[1] <= 0xDFFF) |
| 1245 | { |
| 1246 | *w = (((u[0] & 0x3FF) << 10) | (u[1] & 0x3FF)) + 0x10000; |
| 1247 | u += 2; |
| 1248 | } |
| 1249 | else { |
| 1250 | *w = *u; |
| 1251 | u++; |
| 1252 | } |
| 1253 | w++; |
| 1254 | } |
| 1255 | if (w != wend) |
| 1256 | *w = L'\0'; |
| 1257 | return w - worig; |
| 1258 | } |
| 1259 | else { |
| 1260 | nchar = 1; /* nul character at the end */ |
| 1261 | while (u != uend) { |
| 1262 | if (0xD800 <= u[0] && u[0] <= 0xDBFF |
| 1263 | && 0xDC00 <= u[1] && u[1] <= 0xDFFF) |
| 1264 | u += 2; |
| 1265 | else |
| 1266 | u++; |
| 1267 | nchar++; |
| 1268 | } |
| 1269 | } |
| 1270 | return nchar; |
| 1271 | #elif Py_UNICODE_SIZE == 4 && SIZEOF_WCHAR_T == 2 |
| 1272 | register Py_UNICODE *u, *uend, ordinal; |
| 1273 | register Py_ssize_t i; |
| 1274 | wchar_t *worig, *wend; |
| 1275 | Py_ssize_t nchar; |
| 1276 | |
| 1277 | u = PyUnicode_AS_UNICODE(unicode); |
| 1278 | uend = u + PyUnicode_GET_SIZE(u); |
| 1279 | if (w != NULL) { |
| 1280 | worig = w; |
| 1281 | wend = w + size; |
| 1282 | while (u != uend && w != wend) { |
| 1283 | ordinal = *u; |
| 1284 | if (ordinal > 0xffff) { |
| 1285 | ordinal -= 0x10000; |
| 1286 | *w++ = 0xD800 | (ordinal >> 10); |
| 1287 | *w++ = 0xDC00 | (ordinal & 0x3FF); |
| 1288 | } |
| 1289 | else |
| 1290 | *w++ = ordinal; |
| 1291 | u++; |
| 1292 | } |
| 1293 | if (w != wend) |
| 1294 | *w = 0; |
| 1295 | return w - worig; |
| 1296 | } |
| 1297 | else { |
| 1298 | nchar = 1; /* nul character */ |
| 1299 | while (u != uend) { |
| 1300 | if (*u > 0xffff) |
| 1301 | nchar += 2; |
| 1302 | else |
| 1303 | nchar++; |
| 1304 | u++; |
| 1305 | } |
| 1306 | return nchar; |
| 1307 | } |
| 1308 | #else |
| 1309 | # error "unsupported wchar_t and Py_UNICODE sizes, see issue #8670" |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1310 | #endif |
| 1311 | } |
| 1312 | |
| 1313 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1314 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1315 | wchar_t *w, |
| 1316 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1317 | { |
| 1318 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1319 | PyErr_BadInternalCall(); |
| 1320 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1321 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1322 | return unicode_aswidechar((PyUnicodeObject*)unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1325 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 1326 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1327 | Py_ssize_t *size) |
| 1328 | { |
| 1329 | wchar_t* buffer; |
| 1330 | Py_ssize_t buflen; |
| 1331 | |
| 1332 | if (unicode == NULL) { |
| 1333 | PyErr_BadInternalCall(); |
| 1334 | return NULL; |
| 1335 | } |
| 1336 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 1337 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, NULL, 0); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1338 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1339 | PyErr_NoMemory(); |
| 1340 | return NULL; |
| 1341 | } |
| 1342 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1343 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 1344 | if (buffer == NULL) { |
| 1345 | PyErr_NoMemory(); |
| 1346 | return NULL; |
| 1347 | } |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 1348 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, buffer, buflen); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1349 | if (size != NULL) |
| 1350 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 1351 | return buffer; |
| 1352 | } |
| 1353 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1354 | #endif |
| 1355 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1356 | PyObject * |
| 1357 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1358 | { |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1359 | Py_UNICODE s[2]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1360 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1361 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1362 | PyErr_SetString(PyExc_ValueError, |
| 1363 | "chr() arg not in range(0x110000)"); |
| 1364 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1365 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1366 | |
| 1367 | #ifndef Py_UNICODE_WIDE |
| 1368 | if (ordinal > 0xffff) { |
| 1369 | ordinal -= 0x10000; |
| 1370 | s[0] = 0xD800 | (ordinal >> 10); |
| 1371 | s[1] = 0xDC00 | (ordinal & 0x3FF); |
| 1372 | return PyUnicode_FromUnicode(s, 2); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1373 | } |
| 1374 | #endif |
| 1375 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1376 | s[0] = (Py_UNICODE)ordinal; |
| 1377 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1380 | PyObject * |
| 1381 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1382 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1383 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1384 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1385 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1386 | Py_INCREF(obj); |
| 1387 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1388 | } |
| 1389 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1390 | /* For a Unicode subtype that's not a Unicode object, |
| 1391 | return a true Unicode object with the same data. */ |
| 1392 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 1393 | PyUnicode_GET_SIZE(obj)); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1394 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1395 | PyErr_Format(PyExc_TypeError, |
| 1396 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1397 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1398 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1401 | PyObject * |
| 1402 | PyUnicode_FromEncodedObject(register PyObject *obj, |
| 1403 | const char *encoding, |
| 1404 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1405 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1406 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1407 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1408 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1409 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1410 | PyErr_BadInternalCall(); |
| 1411 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1412 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1413 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1414 | /* Decoding bytes objects is the most common case and should be fast */ |
| 1415 | if (PyBytes_Check(obj)) { |
| 1416 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 1417 | Py_INCREF(unicode_empty); |
| 1418 | v = (PyObject *) unicode_empty; |
| 1419 | } |
| 1420 | else { |
| 1421 | v = PyUnicode_Decode( |
| 1422 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 1423 | encoding, errors); |
| 1424 | } |
| 1425 | return v; |
| 1426 | } |
| 1427 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1428 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1429 | PyErr_SetString(PyExc_TypeError, |
| 1430 | "decoding str is not supported"); |
| 1431 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1432 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1433 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1434 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 1435 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 1436 | PyErr_Format(PyExc_TypeError, |
| 1437 | "coercing to str: need bytes, bytearray " |
| 1438 | "or buffer-like object, %.80s found", |
| 1439 | Py_TYPE(obj)->tp_name); |
| 1440 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1441 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1442 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1443 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1444 | Py_INCREF(unicode_empty); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1445 | v = (PyObject *) unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1446 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1447 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1448 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1449 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 1450 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1451 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1454 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1455 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 1456 | 1 on success. */ |
| 1457 | static int |
| 1458 | normalize_encoding(const char *encoding, |
| 1459 | char *lower, |
| 1460 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1461 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1462 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1463 | char *l; |
| 1464 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1465 | |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1466 | e = encoding; |
| 1467 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1468 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1469 | while (*e) { |
| 1470 | if (l == l_end) |
| 1471 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 1472 | if (Py_ISUPPER(*e)) { |
| 1473 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1474 | } |
| 1475 | else if (*e == '_') { |
| 1476 | *l++ = '-'; |
| 1477 | e++; |
| 1478 | } |
| 1479 | else { |
| 1480 | *l++ = *e++; |
| 1481 | } |
| 1482 | } |
| 1483 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1484 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1487 | PyObject * |
| 1488 | PyUnicode_Decode(const char *s, |
| 1489 | Py_ssize_t size, |
| 1490 | const char *encoding, |
| 1491 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1492 | { |
| 1493 | PyObject *buffer = NULL, *unicode; |
| 1494 | Py_buffer info; |
| 1495 | char lower[11]; /* Enough for any encoding shortcut */ |
| 1496 | |
| 1497 | if (encoding == NULL) |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 1498 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1499 | |
| 1500 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1501 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 1502 | if ((strcmp(lower, "utf-8") == 0) || |
| 1503 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1504 | return PyUnicode_DecodeUTF8(s, size, errors); |
| 1505 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 1506 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1507 | (strcmp(lower, "iso-8859-1") == 0)) |
| 1508 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1509 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1510 | else if (strcmp(lower, "mbcs") == 0) |
| 1511 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1512 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1513 | else if (strcmp(lower, "ascii") == 0) |
| 1514 | return PyUnicode_DecodeASCII(s, size, errors); |
| 1515 | else if (strcmp(lower, "utf-16") == 0) |
| 1516 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 1517 | else if (strcmp(lower, "utf-32") == 0) |
| 1518 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 1519 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1520 | |
| 1521 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1522 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 1523 | 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] | 1524 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 1525 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1526 | if (buffer == NULL) |
| 1527 | goto onError; |
| 1528 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1529 | if (unicode == NULL) |
| 1530 | goto onError; |
| 1531 | if (!PyUnicode_Check(unicode)) { |
| 1532 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1533 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1534 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1535 | Py_DECREF(unicode); |
| 1536 | goto onError; |
| 1537 | } |
| 1538 | Py_DECREF(buffer); |
| 1539 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1540 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1541 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1542 | Py_XDECREF(buffer); |
| 1543 | return NULL; |
| 1544 | } |
| 1545 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1546 | PyObject * |
| 1547 | PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1548 | const char *encoding, |
| 1549 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1550 | { |
| 1551 | PyObject *v; |
| 1552 | |
| 1553 | if (!PyUnicode_Check(unicode)) { |
| 1554 | PyErr_BadArgument(); |
| 1555 | goto onError; |
| 1556 | } |
| 1557 | |
| 1558 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1559 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1560 | |
| 1561 | /* Decode via the codec registry */ |
| 1562 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1563 | if (v == NULL) |
| 1564 | goto onError; |
| 1565 | return v; |
| 1566 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1567 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1568 | return NULL; |
| 1569 | } |
| 1570 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1571 | PyObject * |
| 1572 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
| 1573 | const char *encoding, |
| 1574 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1575 | { |
| 1576 | PyObject *v; |
| 1577 | |
| 1578 | if (!PyUnicode_Check(unicode)) { |
| 1579 | PyErr_BadArgument(); |
| 1580 | goto onError; |
| 1581 | } |
| 1582 | |
| 1583 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1584 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1585 | |
| 1586 | /* Decode via the codec registry */ |
| 1587 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1588 | if (v == NULL) |
| 1589 | goto onError; |
| 1590 | if (!PyUnicode_Check(v)) { |
| 1591 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1592 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1593 | Py_TYPE(v)->tp_name); |
| 1594 | Py_DECREF(v); |
| 1595 | goto onError; |
| 1596 | } |
| 1597 | return v; |
| 1598 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1599 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1600 | return NULL; |
| 1601 | } |
| 1602 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1603 | PyObject * |
| 1604 | PyUnicode_Encode(const Py_UNICODE *s, |
| 1605 | Py_ssize_t size, |
| 1606 | const char *encoding, |
| 1607 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1608 | { |
| 1609 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1610 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1611 | unicode = PyUnicode_FromUnicode(s, size); |
| 1612 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1613 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1614 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1615 | Py_DECREF(unicode); |
| 1616 | return v; |
| 1617 | } |
| 1618 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1619 | PyObject * |
| 1620 | PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1621 | const char *encoding, |
| 1622 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1623 | { |
| 1624 | PyObject *v; |
| 1625 | |
| 1626 | if (!PyUnicode_Check(unicode)) { |
| 1627 | PyErr_BadArgument(); |
| 1628 | goto onError; |
| 1629 | } |
| 1630 | |
| 1631 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1632 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1633 | |
| 1634 | /* Encode via the codec registry */ |
| 1635 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1636 | if (v == NULL) |
| 1637 | goto onError; |
| 1638 | return v; |
| 1639 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1640 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1641 | return NULL; |
| 1642 | } |
| 1643 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 1644 | PyObject * |
| 1645 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1646 | { |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 1647 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 1648 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 1649 | PyUnicode_GET_SIZE(unicode), |
| 1650 | NULL); |
| 1651 | #elif defined(__APPLE__) |
| 1652 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1653 | PyUnicode_GET_SIZE(unicode), |
| 1654 | "surrogateescape"); |
| 1655 | #else |
| 1656 | if (Py_FileSystemDefaultEncoding) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1657 | return PyUnicode_AsEncodedString(unicode, |
| 1658 | Py_FileSystemDefaultEncoding, |
| 1659 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 1660 | } |
| 1661 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1662 | /* locale encoding with surrogateescape */ |
| 1663 | wchar_t *wchar; |
| 1664 | char *bytes; |
| 1665 | PyObject *bytes_obj; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 1666 | size_t error_pos; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1667 | |
| 1668 | wchar = PyUnicode_AsWideCharString(unicode, NULL); |
| 1669 | if (wchar == NULL) |
| 1670 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 1671 | bytes = _Py_wchar2char(wchar, &error_pos); |
| 1672 | if (bytes == NULL) { |
| 1673 | if (error_pos != (size_t)-1) { |
| 1674 | char *errmsg = strerror(errno); |
| 1675 | PyObject *exc = NULL; |
| 1676 | if (errmsg == NULL) |
| 1677 | errmsg = "Py_wchar2char() failed"; |
| 1678 | raise_encode_exception(&exc, |
| 1679 | "filesystemencoding", |
| 1680 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 1681 | error_pos, error_pos+1, |
| 1682 | errmsg); |
| 1683 | Py_XDECREF(exc); |
| 1684 | } |
| 1685 | else |
| 1686 | PyErr_NoMemory(); |
| 1687 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1688 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 1689 | } |
| 1690 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1691 | |
| 1692 | bytes_obj = PyBytes_FromString(bytes); |
| 1693 | PyMem_Free(bytes); |
| 1694 | return bytes_obj; |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 1695 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 1696 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1699 | PyObject * |
| 1700 | PyUnicode_AsEncodedString(PyObject *unicode, |
| 1701 | const char *encoding, |
| 1702 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1703 | { |
| 1704 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1705 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1706 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1707 | if (!PyUnicode_Check(unicode)) { |
| 1708 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1709 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1710 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1711 | |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 1712 | if (encoding == NULL) { |
| 1713 | if (errors == NULL || strcmp(errors, "strict") == 0) |
| 1714 | return PyUnicode_AsUTF8String(unicode); |
| 1715 | else |
| 1716 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1717 | PyUnicode_GET_SIZE(unicode), |
| 1718 | errors); |
| 1719 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1720 | |
| 1721 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1722 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 1723 | if ((strcmp(lower, "utf-8") == 0) || |
| 1724 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 1725 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 1726 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 1727 | return PyUnicode_AsUTF8String(unicode); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 1728 | else |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 1729 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1730 | PyUnicode_GET_SIZE(unicode), |
| 1731 | errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 1732 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1733 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 1734 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1735 | (strcmp(lower, "iso-8859-1") == 0)) |
| 1736 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 1737 | PyUnicode_GET_SIZE(unicode), |
| 1738 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1739 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1740 | else if (strcmp(lower, "mbcs") == 0) |
| 1741 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 1742 | PyUnicode_GET_SIZE(unicode), |
| 1743 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1744 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1745 | else if (strcmp(lower, "ascii") == 0) |
| 1746 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 1747 | PyUnicode_GET_SIZE(unicode), |
| 1748 | errors); |
| 1749 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1750 | |
| 1751 | /* Encode via the codec registry */ |
| 1752 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1753 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1754 | return NULL; |
| 1755 | |
| 1756 | /* The normal path */ |
| 1757 | if (PyBytes_Check(v)) |
| 1758 | return v; |
| 1759 | |
| 1760 | /* 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] | 1761 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1762 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1763 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1764 | |
| 1765 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 1766 | "encoder %s returned bytearray instead of bytes", |
| 1767 | encoding); |
| 1768 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1769 | Py_DECREF(v); |
| 1770 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1771 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1772 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1773 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 1774 | Py_DECREF(v); |
| 1775 | return b; |
| 1776 | } |
| 1777 | |
| 1778 | PyErr_Format(PyExc_TypeError, |
| 1779 | "encoder did not return a bytes object (type=%.400s)", |
| 1780 | Py_TYPE(v)->tp_name); |
| 1781 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1782 | return NULL; |
| 1783 | } |
| 1784 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1785 | PyObject * |
| 1786 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
| 1787 | const char *encoding, |
| 1788 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1789 | { |
| 1790 | PyObject *v; |
| 1791 | |
| 1792 | if (!PyUnicode_Check(unicode)) { |
| 1793 | PyErr_BadArgument(); |
| 1794 | goto onError; |
| 1795 | } |
| 1796 | |
| 1797 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1798 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1799 | |
| 1800 | /* Encode via the codec registry */ |
| 1801 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1802 | if (v == NULL) |
| 1803 | goto onError; |
| 1804 | if (!PyUnicode_Check(v)) { |
| 1805 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1806 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1807 | Py_TYPE(v)->tp_name); |
| 1808 | Py_DECREF(v); |
| 1809 | goto onError; |
| 1810 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1811 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1812 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1813 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1814 | return NULL; |
| 1815 | } |
| 1816 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1817 | PyObject * |
Victor Stinner | f3fd733 | 2011-03-02 01:03:11 +0000 | [diff] [blame] | 1818 | _PyUnicode_AsDefaultEncodedString(PyObject *unicode) |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1819 | { |
| 1820 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1821 | if (v) |
| 1822 | return v; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1823 | v = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Guido van Rossum | 0661009 | 2007-08-16 21:02:22 +0000 | [diff] [blame] | 1824 | PyUnicode_GET_SIZE(unicode), |
| 1825 | NULL); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1826 | if (!v) |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1827 | return NULL; |
Guido van Rossum | e7a0d39 | 2007-07-12 07:53:00 +0000 | [diff] [blame] | 1828 | ((PyUnicodeObject *)unicode)->defenc = v; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1829 | return v; |
| 1830 | } |
| 1831 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1832 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1833 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1834 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1835 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 1836 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1837 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1838 | PyObject* |
| 1839 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 1840 | { |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 1841 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1842 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 1843 | #elif defined(__APPLE__) |
| 1844 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
| 1845 | #else |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1846 | /* During the early bootstrapping process, Py_FileSystemDefaultEncoding |
| 1847 | can be undefined. If it is case, decode using UTF-8. The following assumes |
| 1848 | that Py_FileSystemDefaultEncoding is set to a built-in encoding during the |
| 1849 | bootstrapping process where the codecs aren't ready yet. |
| 1850 | */ |
| 1851 | if (Py_FileSystemDefaultEncoding) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1852 | return PyUnicode_Decode(s, size, |
| 1853 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 1854 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1855 | } |
| 1856 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1857 | /* locale encoding with surrogateescape */ |
| 1858 | wchar_t *wchar; |
| 1859 | PyObject *unicode; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 1860 | size_t len; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1861 | |
| 1862 | if (s[size] != '\0' || size != strlen(s)) { |
| 1863 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 1864 | return NULL; |
| 1865 | } |
| 1866 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 1867 | wchar = _Py_char2wchar(s, &len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1868 | if (wchar == NULL) |
Victor Stinner | d5af0a5 | 2010-11-08 23:34:29 +0000 | [diff] [blame] | 1869 | return PyErr_NoMemory(); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1870 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 1871 | unicode = PyUnicode_FromWideChar(wchar, len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 1872 | PyMem_Free(wchar); |
| 1873 | return unicode; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1874 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 1875 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1878 | |
| 1879 | int |
| 1880 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 1881 | { |
| 1882 | PyObject *output = NULL; |
| 1883 | Py_ssize_t size; |
| 1884 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1885 | if (arg == NULL) { |
| 1886 | Py_DECREF(*(PyObject**)addr); |
| 1887 | return 1; |
| 1888 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 1889 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1890 | output = arg; |
| 1891 | Py_INCREF(output); |
| 1892 | } |
| 1893 | else { |
| 1894 | arg = PyUnicode_FromObject(arg); |
| 1895 | if (!arg) |
| 1896 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1897 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1898 | Py_DECREF(arg); |
| 1899 | if (!output) |
| 1900 | return 0; |
| 1901 | if (!PyBytes_Check(output)) { |
| 1902 | Py_DECREF(output); |
| 1903 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 1904 | return 0; |
| 1905 | } |
| 1906 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 1907 | size = PyBytes_GET_SIZE(output); |
| 1908 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1909 | if (size != strlen(data)) { |
| 1910 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 1911 | Py_DECREF(output); |
| 1912 | return 0; |
| 1913 | } |
| 1914 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1915 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
| 1918 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 1919 | int |
| 1920 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 1921 | { |
| 1922 | PyObject *output = NULL; |
| 1923 | Py_ssize_t size; |
| 1924 | void *data; |
| 1925 | if (arg == NULL) { |
| 1926 | Py_DECREF(*(PyObject**)addr); |
| 1927 | return 1; |
| 1928 | } |
| 1929 | if (PyUnicode_Check(arg)) { |
| 1930 | output = arg; |
| 1931 | Py_INCREF(output); |
| 1932 | } |
| 1933 | else { |
| 1934 | arg = PyBytes_FromObject(arg); |
| 1935 | if (!arg) |
| 1936 | return 0; |
| 1937 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 1938 | PyBytes_GET_SIZE(arg)); |
| 1939 | Py_DECREF(arg); |
| 1940 | if (!output) |
| 1941 | return 0; |
| 1942 | if (!PyUnicode_Check(output)) { |
| 1943 | Py_DECREF(output); |
| 1944 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 1945 | return 0; |
| 1946 | } |
| 1947 | } |
| 1948 | size = PyUnicode_GET_SIZE(output); |
| 1949 | data = PyUnicode_AS_UNICODE(output); |
| 1950 | if (size != Py_UNICODE_strlen(data)) { |
| 1951 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 1952 | Py_DECREF(output); |
| 1953 | return 0; |
| 1954 | } |
| 1955 | *(PyObject**)addr = output; |
| 1956 | return Py_CLEANUP_SUPPORTED; |
| 1957 | } |
| 1958 | |
| 1959 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1960 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1961 | _PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1962 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1963 | PyObject *bytes; |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 1964 | if (!PyUnicode_Check(unicode)) { |
| 1965 | PyErr_BadArgument(); |
| 1966 | return NULL; |
| 1967 | } |
Victor Stinner | f3fd733 | 2011-03-02 01:03:11 +0000 | [diff] [blame] | 1968 | bytes = _PyUnicode_AsDefaultEncodedString(unicode); |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1969 | if (bytes == NULL) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1970 | return NULL; |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1971 | if (psize != NULL) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1972 | *psize = PyBytes_GET_SIZE(bytes); |
| 1973 | return PyBytes_AS_STRING(bytes); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1977 | _PyUnicode_AsString(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1978 | { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1979 | return _PyUnicode_AsStringAndSize(unicode, NULL); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1982 | Py_UNICODE * |
| 1983 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1984 | { |
| 1985 | if (!PyUnicode_Check(unicode)) { |
| 1986 | PyErr_BadArgument(); |
| 1987 | goto onError; |
| 1988 | } |
| 1989 | return PyUnicode_AS_UNICODE(unicode); |
| 1990 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1991 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1992 | return NULL; |
| 1993 | } |
| 1994 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1995 | Py_ssize_t |
| 1996 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1997 | { |
| 1998 | if (!PyUnicode_Check(unicode)) { |
| 1999 | PyErr_BadArgument(); |
| 2000 | goto onError; |
| 2001 | } |
| 2002 | return PyUnicode_GET_SIZE(unicode); |
| 2003 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2004 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2005 | return -1; |
| 2006 | } |
| 2007 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2008 | const char * |
| 2009 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2010 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 2011 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2012 | } |
| 2013 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 2014 | /* create or adjust a UnicodeDecodeError */ |
| 2015 | static void |
| 2016 | make_decode_exception(PyObject **exceptionObject, |
| 2017 | const char *encoding, |
| 2018 | const char *input, Py_ssize_t length, |
| 2019 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 2020 | const char *reason) |
| 2021 | { |
| 2022 | if (*exceptionObject == NULL) { |
| 2023 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 2024 | encoding, input, length, startpos, endpos, reason); |
| 2025 | } |
| 2026 | else { |
| 2027 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 2028 | goto onError; |
| 2029 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 2030 | goto onError; |
| 2031 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 2032 | goto onError; |
| 2033 | } |
| 2034 | return; |
| 2035 | |
| 2036 | onError: |
| 2037 | Py_DECREF(*exceptionObject); |
| 2038 | *exceptionObject = NULL; |
| 2039 | } |
| 2040 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2041 | /* error handling callback helper: |
| 2042 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 2043 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2044 | and adjust various state variables. |
| 2045 | return 0 on success, -1 on error |
| 2046 | */ |
| 2047 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2048 | static int |
| 2049 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 2050 | const char *encoding, const char *reason, |
| 2051 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 2052 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 2053 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2054 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2055 | 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] | 2056 | |
| 2057 | PyObject *restuple = NULL; |
| 2058 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2059 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2060 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2061 | Py_ssize_t requiredsize; |
| 2062 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2063 | Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2064 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2065 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2066 | int res = -1; |
| 2067 | |
| 2068 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2069 | *errorHandler = PyCodec_LookupError(errors); |
| 2070 | if (*errorHandler == NULL) |
| 2071 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 2074 | make_decode_exception(exceptionObject, |
| 2075 | encoding, |
| 2076 | *input, *inend - *input, |
| 2077 | *startinpos, *endinpos, |
| 2078 | reason); |
| 2079 | if (*exceptionObject == NULL) |
| 2080 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2081 | |
| 2082 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 2083 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2084 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2085 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 2086 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2087 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2088 | } |
| 2089 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2090 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2091 | |
| 2092 | /* Copy back the bytes variables, which might have been modified by the |
| 2093 | callback */ |
| 2094 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 2095 | if (!inputobj) |
| 2096 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2097 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2098 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2099 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2100 | *input = PyBytes_AS_STRING(inputobj); |
| 2101 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2102 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 2103 | /* we can DECREF safely, as the exception has another reference, |
| 2104 | so the object won't go away. */ |
| 2105 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2106 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2107 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2108 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2109 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2110 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 2111 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2112 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2113 | |
| 2114 | /* need more space? (at least enough for what we |
| 2115 | have+the replacement+the rest of the string (starting |
| 2116 | at the new input position), so we won't have to check space |
| 2117 | when there are no errors in the rest of the string) */ |
| 2118 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 2119 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 2120 | requiredsize = *outpos + repsize + insize-newpos; |
| 2121 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2122 | if (requiredsize<2*outsize) |
| 2123 | requiredsize = 2*outsize; |
| 2124 | if (_PyUnicode_Resize(output, requiredsize) < 0) |
| 2125 | goto onError; |
| 2126 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2127 | } |
| 2128 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2129 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2130 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 2131 | *outptr += repsize; |
| 2132 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2133 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2134 | /* we made it! */ |
| 2135 | res = 0; |
| 2136 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2137 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2138 | Py_XDECREF(restuple); |
| 2139 | return res; |
| 2140 | } |
| 2141 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2142 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 2143 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2144 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 2145 | |
| 2146 | /* Three simple macros defining base-64. */ |
| 2147 | |
| 2148 | /* Is c a base-64 character? */ |
| 2149 | |
| 2150 | #define IS_BASE64(c) \ |
| 2151 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 2152 | ((c) >= 'a' && (c) <= 'z') || \ |
| 2153 | ((c) >= '0' && (c) <= '9') || \ |
| 2154 | (c) == '+' || (c) == '/') |
| 2155 | |
| 2156 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 2157 | |
| 2158 | #define FROM_BASE64(c) \ |
| 2159 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 2160 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 2161 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 2162 | (c) == '+' ? 62 : 63) |
| 2163 | |
| 2164 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 2165 | |
| 2166 | #define TO_BASE64(n) \ |
| 2167 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 2168 | |
| 2169 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 2170 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 2171 | * byte not decoding to itself is the + which begins a base64 |
| 2172 | * string. */ |
| 2173 | |
| 2174 | #define DECODE_DIRECT(c) \ |
| 2175 | ((c) <= 127 && (c) != '+') |
| 2176 | |
| 2177 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 2178 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 2179 | * the above). See RFC2152. This array identifies these different |
| 2180 | * sets: |
| 2181 | * 0 : "Set D" |
| 2182 | * alphanumeric and '(),-./:? |
| 2183 | * 1 : "Set O" |
| 2184 | * !"#$%&*;<=>@[]^_`{|} |
| 2185 | * 2 : "whitespace" |
| 2186 | * ht nl cr sp |
| 2187 | * 3 : special (must be base64 encoded) |
| 2188 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 2189 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2190 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2191 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2192 | char utf7_category[128] = { |
| 2193 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 2194 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 2195 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 2196 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 2197 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 2198 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 2199 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 2200 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 2201 | /* @ A B C D E F G H I J K L M N O */ |
| 2202 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2203 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 2204 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 2205 | /* ` a b c d e f g h i j k l m n o */ |
| 2206 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2207 | /* p q r s t u v w x y z { | } ~ del */ |
| 2208 | 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] | 2209 | }; |
| 2210 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2211 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 2212 | * answer depends on whether we are encoding set O as itself, and also |
| 2213 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 2214 | * clear that the answers to these questions vary between |
| 2215 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 2216 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2217 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 2218 | ((c) < 128 && (c) > 0 && \ |
| 2219 | ((utf7_category[(c)] == 0) || \ |
| 2220 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 2221 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2222 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2223 | PyObject * |
| 2224 | PyUnicode_DecodeUTF7(const char *s, |
| 2225 | Py_ssize_t size, |
| 2226 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2227 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2228 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 2229 | } |
| 2230 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2231 | /* The decoder. The only state we preserve is our read position, |
| 2232 | * i.e. how many characters we have consumed. So if we end in the |
| 2233 | * middle of a shift sequence we have to back off the read position |
| 2234 | * and the output to the beginning of the sequence, otherwise we lose |
| 2235 | * all the shift state (seen bits, number of bits seen, high |
| 2236 | * surrogate). */ |
| 2237 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2238 | PyObject * |
| 2239 | PyUnicode_DecodeUTF7Stateful(const char *s, |
| 2240 | Py_ssize_t size, |
| 2241 | const char *errors, |
| 2242 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2243 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2244 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2245 | Py_ssize_t startinpos; |
| 2246 | Py_ssize_t endinpos; |
| 2247 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2248 | const char *e; |
| 2249 | PyUnicodeObject *unicode; |
| 2250 | Py_UNICODE *p; |
| 2251 | const char *errmsg = ""; |
| 2252 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2253 | Py_UNICODE *shiftOutStart; |
| 2254 | unsigned int base64bits = 0; |
| 2255 | unsigned long base64buffer = 0; |
| 2256 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2257 | PyObject *errorHandler = NULL; |
| 2258 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2259 | |
| 2260 | unicode = _PyUnicode_New(size); |
| 2261 | if (!unicode) |
| 2262 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2263 | if (size == 0) { |
| 2264 | if (consumed) |
| 2265 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2266 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2267 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2268 | |
| 2269 | p = unicode->str; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2270 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2271 | e = s + size; |
| 2272 | |
| 2273 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2274 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2275 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 2276 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2277 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2278 | if (inShift) { /* in a base-64 section */ |
| 2279 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 2280 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 2281 | base64bits += 6; |
| 2282 | s++; |
| 2283 | if (base64bits >= 16) { |
| 2284 | /* we have enough bits for a UTF-16 value */ |
| 2285 | Py_UNICODE outCh = (Py_UNICODE) |
| 2286 | (base64buffer >> (base64bits-16)); |
| 2287 | base64bits -= 16; |
| 2288 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 2289 | if (surrogate) { |
| 2290 | /* expecting a second surrogate */ |
| 2291 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 2292 | #ifdef Py_UNICODE_WIDE |
| 2293 | *p++ = (((surrogate & 0x3FF)<<10) |
| 2294 | | (outCh & 0x3FF)) + 0x10000; |
| 2295 | #else |
| 2296 | *p++ = surrogate; |
| 2297 | *p++ = outCh; |
| 2298 | #endif |
| 2299 | surrogate = 0; |
| 2300 | } |
| 2301 | else { |
| 2302 | surrogate = 0; |
| 2303 | errmsg = "second surrogate missing"; |
| 2304 | goto utf7Error; |
| 2305 | } |
| 2306 | } |
| 2307 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 2308 | /* first surrogate */ |
| 2309 | surrogate = outCh; |
| 2310 | } |
| 2311 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 2312 | errmsg = "unexpected second surrogate"; |
| 2313 | goto utf7Error; |
| 2314 | } |
| 2315 | else { |
| 2316 | *p++ = outCh; |
| 2317 | } |
| 2318 | } |
| 2319 | } |
| 2320 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2321 | inShift = 0; |
| 2322 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2323 | if (surrogate) { |
| 2324 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2325 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2326 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2327 | if (base64bits > 0) { /* left-over bits */ |
| 2328 | if (base64bits >= 6) { |
| 2329 | /* We've seen at least one base-64 character */ |
| 2330 | errmsg = "partial character in shift sequence"; |
| 2331 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2332 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2333 | else { |
| 2334 | /* Some bits remain; they should be zero */ |
| 2335 | if (base64buffer != 0) { |
| 2336 | errmsg = "non-zero padding bits in shift sequence"; |
| 2337 | goto utf7Error; |
| 2338 | } |
| 2339 | } |
| 2340 | } |
| 2341 | if (ch != '-') { |
| 2342 | /* '-' is absorbed; other terminating |
| 2343 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2344 | *p++ = ch; |
| 2345 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2346 | } |
| 2347 | } |
| 2348 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2349 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2350 | s++; /* consume '+' */ |
| 2351 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2352 | s++; |
| 2353 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2354 | } |
| 2355 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2356 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2357 | shiftOutStart = p; |
| 2358 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2359 | } |
| 2360 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2361 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2362 | *p++ = ch; |
| 2363 | s++; |
| 2364 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2365 | else { |
| 2366 | startinpos = s-starts; |
| 2367 | s++; |
| 2368 | errmsg = "unexpected special character"; |
| 2369 | goto utf7Error; |
| 2370 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2371 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2372 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2373 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2374 | endinpos = s-starts; |
| 2375 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2376 | errors, &errorHandler, |
| 2377 | "utf7", errmsg, |
| 2378 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2379 | &unicode, &outpos, &p)) |
| 2380 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2381 | } |
| 2382 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2383 | /* end of string */ |
| 2384 | |
| 2385 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 2386 | /* if we're in an inconsistent state, that's an error */ |
| 2387 | if (surrogate || |
| 2388 | (base64bits >= 6) || |
| 2389 | (base64bits > 0 && base64buffer != 0)) { |
| 2390 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2391 | endinpos = size; |
| 2392 | if (unicode_decode_call_errorhandler( |
| 2393 | errors, &errorHandler, |
| 2394 | "utf7", "unterminated shift sequence", |
| 2395 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2396 | &unicode, &outpos, &p)) |
| 2397 | goto onError; |
| 2398 | if (s < e) |
| 2399 | goto restart; |
| 2400 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2401 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2402 | |
| 2403 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2404 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2405 | if (inShift) { |
| 2406 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2407 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2408 | } |
| 2409 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2410 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2411 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2412 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2413 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2414 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2415 | goto onError; |
| 2416 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2417 | Py_XDECREF(errorHandler); |
| 2418 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2419 | return (PyObject *)unicode; |
| 2420 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2421 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2422 | Py_XDECREF(errorHandler); |
| 2423 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2424 | Py_DECREF(unicode); |
| 2425 | return NULL; |
| 2426 | } |
| 2427 | |
| 2428 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2429 | PyObject * |
| 2430 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 2431 | Py_ssize_t size, |
| 2432 | int base64SetO, |
| 2433 | int base64WhiteSpace, |
| 2434 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2435 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2436 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2437 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 2438 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2439 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2440 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2441 | unsigned int base64bits = 0; |
| 2442 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2443 | char * out; |
| 2444 | char * start; |
| 2445 | |
| 2446 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2447 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2448 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 2449 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2450 | return PyErr_NoMemory(); |
| 2451 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2452 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2453 | if (v == NULL) |
| 2454 | return NULL; |
| 2455 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2456 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2457 | for (;i < size; ++i) { |
| 2458 | Py_UNICODE ch = s[i]; |
| 2459 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2460 | if (inShift) { |
| 2461 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2462 | /* shifting out */ |
| 2463 | if (base64bits) { /* output remaining bits */ |
| 2464 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 2465 | base64buffer = 0; |
| 2466 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2467 | } |
| 2468 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2469 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 2470 | so no '-' is required, except if the character is itself a '-' */ |
| 2471 | if (IS_BASE64(ch) || ch == '-') { |
| 2472 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2473 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2474 | *out++ = (char) ch; |
| 2475 | } |
| 2476 | else { |
| 2477 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2478 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2479 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2480 | else { /* not in a shift sequence */ |
| 2481 | if (ch == '+') { |
| 2482 | *out++ = '+'; |
| 2483 | *out++ = '-'; |
| 2484 | } |
| 2485 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2486 | *out++ = (char) ch; |
| 2487 | } |
| 2488 | else { |
| 2489 | *out++ = '+'; |
| 2490 | inShift = 1; |
| 2491 | goto encode_char; |
| 2492 | } |
| 2493 | } |
| 2494 | continue; |
| 2495 | encode_char: |
| 2496 | #ifdef Py_UNICODE_WIDE |
| 2497 | if (ch >= 0x10000) { |
| 2498 | /* code first surrogate */ |
| 2499 | base64bits += 16; |
| 2500 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 2501 | while (base64bits >= 6) { |
| 2502 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2503 | base64bits -= 6; |
| 2504 | } |
| 2505 | /* prepare second surrogate */ |
| 2506 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2507 | } |
| 2508 | #endif |
| 2509 | base64bits += 16; |
| 2510 | base64buffer = (base64buffer << 16) | ch; |
| 2511 | while (base64bits >= 6) { |
| 2512 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2513 | base64bits -= 6; |
| 2514 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 2515 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2516 | if (base64bits) |
| 2517 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 2518 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2519 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2520 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 2521 | return NULL; |
| 2522 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2523 | } |
| 2524 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2525 | #undef IS_BASE64 |
| 2526 | #undef FROM_BASE64 |
| 2527 | #undef TO_BASE64 |
| 2528 | #undef DECODE_DIRECT |
| 2529 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2530 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2531 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 2532 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2533 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2534 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2535 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 2536 | illegal prefix. See RFC 3629 for details */ |
| 2537 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 2538 | 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] | 2539 | 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] | 2540 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2541 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2542 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2543 | 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] | 2544 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 2545 | 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] | 2546 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2547 | 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] | 2548 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 2549 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 2550 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 2551 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 2552 | 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] | 2553 | }; |
| 2554 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2555 | PyObject * |
| 2556 | PyUnicode_DecodeUTF8(const char *s, |
| 2557 | Py_ssize_t size, |
| 2558 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2559 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2560 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 2561 | } |
| 2562 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2563 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 2564 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 2565 | |
| 2566 | /* Mask to quickly check whether a C 'long' contains a |
| 2567 | non-ASCII, UTF8-encoded char. */ |
| 2568 | #if (SIZEOF_LONG == 8) |
| 2569 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 2570 | #elif (SIZEOF_LONG == 4) |
| 2571 | # define ASCII_CHAR_MASK 0x80808080L |
| 2572 | #else |
| 2573 | # error C 'long' size should be either 4 or 8! |
| 2574 | #endif |
| 2575 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2576 | PyObject * |
| 2577 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 2578 | Py_ssize_t size, |
| 2579 | const char *errors, |
| 2580 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2581 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2582 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2583 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2584 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2585 | Py_ssize_t startinpos; |
| 2586 | Py_ssize_t endinpos; |
| 2587 | Py_ssize_t outpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2588 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2589 | PyUnicodeObject *unicode; |
| 2590 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2591 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2592 | PyObject *errorHandler = NULL; |
| 2593 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2594 | |
| 2595 | /* Note: size will always be longer than the resulting Unicode |
| 2596 | character count */ |
| 2597 | unicode = _PyUnicode_New(size); |
| 2598 | if (!unicode) |
| 2599 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2600 | if (size == 0) { |
| 2601 | if (consumed) |
| 2602 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2603 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2604 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2605 | |
| 2606 | /* Unpack UTF-8 encoded data */ |
| 2607 | p = unicode->str; |
| 2608 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2609 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2610 | |
| 2611 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2612 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2613 | |
| 2614 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2615 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 2616 | input will consist of an overwhelming majority of ASCII |
| 2617 | characters, we try to optimize for this case by checking |
| 2618 | as many characters as a C 'long' can contain. |
| 2619 | First, check if we can do an aligned read, as most CPUs have |
| 2620 | a penalty for unaligned reads. |
| 2621 | */ |
| 2622 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 2623 | /* Help register allocation */ |
| 2624 | register const char *_s = s; |
| 2625 | register Py_UNICODE *_p = p; |
| 2626 | while (_s < aligned_end) { |
| 2627 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 2628 | and do a fast unrolled copy if it only contains ASCII |
| 2629 | characters. */ |
| 2630 | unsigned long data = *(unsigned long *) _s; |
| 2631 | if (data & ASCII_CHAR_MASK) |
| 2632 | break; |
| 2633 | _p[0] = (unsigned char) _s[0]; |
| 2634 | _p[1] = (unsigned char) _s[1]; |
| 2635 | _p[2] = (unsigned char) _s[2]; |
| 2636 | _p[3] = (unsigned char) _s[3]; |
| 2637 | #if (SIZEOF_LONG == 8) |
| 2638 | _p[4] = (unsigned char) _s[4]; |
| 2639 | _p[5] = (unsigned char) _s[5]; |
| 2640 | _p[6] = (unsigned char) _s[6]; |
| 2641 | _p[7] = (unsigned char) _s[7]; |
| 2642 | #endif |
| 2643 | _s += SIZEOF_LONG; |
| 2644 | _p += SIZEOF_LONG; |
| 2645 | } |
| 2646 | s = _s; |
| 2647 | p = _p; |
| 2648 | if (s == e) |
| 2649 | break; |
| 2650 | ch = (unsigned char)*s; |
| 2651 | } |
| 2652 | } |
| 2653 | |
| 2654 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2655 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2656 | s++; |
| 2657 | continue; |
| 2658 | } |
| 2659 | |
| 2660 | n = utf8_code_length[ch]; |
| 2661 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2662 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2663 | if (consumed) |
| 2664 | break; |
| 2665 | else { |
| 2666 | errmsg = "unexpected end of data"; |
| 2667 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2668 | endinpos = startinpos+1; |
| 2669 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 2670 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2671 | goto utf8Error; |
| 2672 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2673 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2674 | |
| 2675 | switch (n) { |
| 2676 | |
| 2677 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2678 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2679 | startinpos = s-starts; |
| 2680 | endinpos = startinpos+1; |
| 2681 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2682 | |
| 2683 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2684 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2685 | startinpos = s-starts; |
| 2686 | endinpos = startinpos+1; |
| 2687 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2688 | |
| 2689 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2690 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2691 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2692 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2693 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2694 | goto utf8Error; |
| 2695 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2696 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2697 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 2698 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2699 | break; |
| 2700 | |
| 2701 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 2702 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 2703 | will result in surrogates in range d800-dfff. Surrogates are |
| 2704 | not valid UTF-8 so they are rejected. |
| 2705 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 2706 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2707 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2708 | (s[2] & 0xc0) != 0x80 || |
| 2709 | ((unsigned char)s[0] == 0xE0 && |
| 2710 | (unsigned char)s[1] < 0xA0) || |
| 2711 | ((unsigned char)s[0] == 0xED && |
| 2712 | (unsigned char)s[1] > 0x9F)) { |
| 2713 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2714 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2715 | endinpos = startinpos + 1; |
| 2716 | |
| 2717 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 2718 | continuation byte is s[2], so increment endinpos by 1, |
| 2719 | if not, s[1] is invalid and endinpos doesn't need to |
| 2720 | be incremented. */ |
| 2721 | if ((s[1] & 0xC0) == 0x80) |
| 2722 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2723 | goto utf8Error; |
| 2724 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2725 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2726 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
| 2727 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2728 | break; |
| 2729 | |
| 2730 | case 4: |
| 2731 | if ((s[1] & 0xc0) != 0x80 || |
| 2732 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2733 | (s[3] & 0xc0) != 0x80 || |
| 2734 | ((unsigned char)s[0] == 0xF0 && |
| 2735 | (unsigned char)s[1] < 0x90) || |
| 2736 | ((unsigned char)s[0] == 0xF4 && |
| 2737 | (unsigned char)s[1] > 0x8F)) { |
| 2738 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2739 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2740 | endinpos = startinpos + 1; |
| 2741 | if ((s[1] & 0xC0) == 0x80) { |
| 2742 | endinpos++; |
| 2743 | if ((s[2] & 0xC0) == 0x80) |
| 2744 | endinpos++; |
| 2745 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2746 | goto utf8Error; |
| 2747 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2748 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2749 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 2750 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 2751 | |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2752 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2753 | *p++ = (Py_UNICODE)ch; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2754 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2755 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2756 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2757 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 2758 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2759 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2760 | /* high surrogate = top 10 bits added to D800 */ |
| 2761 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2762 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2763 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2764 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2765 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2766 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2767 | } |
| 2768 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2769 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2770 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2771 | utf8Error: |
| 2772 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2773 | if (unicode_decode_call_errorhandler( |
| 2774 | errors, &errorHandler, |
| 2775 | "utf8", errmsg, |
| 2776 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2777 | &unicode, &outpos, &p)) |
| 2778 | goto onError; |
| 2779 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2780 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2781 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2782 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2783 | |
| 2784 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2785 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2786 | goto onError; |
| 2787 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2788 | Py_XDECREF(errorHandler); |
| 2789 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2790 | return (PyObject *)unicode; |
| 2791 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2792 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2793 | Py_XDECREF(errorHandler); |
| 2794 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2795 | Py_DECREF(unicode); |
| 2796 | return NULL; |
| 2797 | } |
| 2798 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2799 | #undef ASCII_CHAR_MASK |
| 2800 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 2801 | #ifdef __APPLE__ |
| 2802 | |
| 2803 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 2804 | used to decode the command line arguments on Mac OS X. */ |
| 2805 | |
| 2806 | wchar_t* |
| 2807 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 2808 | { |
| 2809 | int n; |
| 2810 | const char *e; |
| 2811 | wchar_t *unicode, *p; |
| 2812 | |
| 2813 | /* Note: size will always be longer than the resulting Unicode |
| 2814 | character count */ |
| 2815 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 2816 | PyErr_NoMemory(); |
| 2817 | return NULL; |
| 2818 | } |
| 2819 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 2820 | if (!unicode) |
| 2821 | return NULL; |
| 2822 | |
| 2823 | /* Unpack UTF-8 encoded data */ |
| 2824 | p = unicode; |
| 2825 | e = s + size; |
| 2826 | while (s < e) { |
| 2827 | Py_UCS4 ch = (unsigned char)*s; |
| 2828 | |
| 2829 | if (ch < 0x80) { |
| 2830 | *p++ = (wchar_t)ch; |
| 2831 | s++; |
| 2832 | continue; |
| 2833 | } |
| 2834 | |
| 2835 | n = utf8_code_length[ch]; |
| 2836 | if (s + n > e) { |
| 2837 | goto surrogateescape; |
| 2838 | } |
| 2839 | |
| 2840 | switch (n) { |
| 2841 | case 0: |
| 2842 | case 1: |
| 2843 | goto surrogateescape; |
| 2844 | |
| 2845 | case 2: |
| 2846 | if ((s[1] & 0xc0) != 0x80) |
| 2847 | goto surrogateescape; |
| 2848 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 2849 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 2850 | *p++ = (wchar_t)ch; |
| 2851 | break; |
| 2852 | |
| 2853 | case 3: |
| 2854 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 2855 | will result in surrogates in range d800-dfff. Surrogates are |
| 2856 | not valid UTF-8 so they are rejected. |
| 2857 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 2858 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 2859 | if ((s[1] & 0xc0) != 0x80 || |
| 2860 | (s[2] & 0xc0) != 0x80 || |
| 2861 | ((unsigned char)s[0] == 0xE0 && |
| 2862 | (unsigned char)s[1] < 0xA0) || |
| 2863 | ((unsigned char)s[0] == 0xED && |
| 2864 | (unsigned char)s[1] > 0x9F)) { |
| 2865 | |
| 2866 | goto surrogateescape; |
| 2867 | } |
| 2868 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 2869 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
| 2870 | *p++ = (Py_UNICODE)ch; |
| 2871 | break; |
| 2872 | |
| 2873 | case 4: |
| 2874 | if ((s[1] & 0xc0) != 0x80 || |
| 2875 | (s[2] & 0xc0) != 0x80 || |
| 2876 | (s[3] & 0xc0) != 0x80 || |
| 2877 | ((unsigned char)s[0] == 0xF0 && |
| 2878 | (unsigned char)s[1] < 0x90) || |
| 2879 | ((unsigned char)s[0] == 0xF4 && |
| 2880 | (unsigned char)s[1] > 0x8F)) { |
| 2881 | goto surrogateescape; |
| 2882 | } |
| 2883 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 2884 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 2885 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 2886 | |
| 2887 | #if SIZEOF_WCHAR_T == 4 |
| 2888 | *p++ = (wchar_t)ch; |
| 2889 | #else |
| 2890 | /* compute and append the two surrogates: */ |
| 2891 | |
| 2892 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 2893 | ch -= 0x10000; |
| 2894 | |
| 2895 | /* high surrogate = top 10 bits added to D800 */ |
| 2896 | *p++ = (wchar_t)(0xD800 + (ch >> 10)); |
| 2897 | |
| 2898 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 2899 | *p++ = (wchar_t)(0xDC00 + (ch & 0x03FF)); |
| 2900 | #endif |
| 2901 | break; |
| 2902 | } |
| 2903 | s += n; |
| 2904 | continue; |
| 2905 | |
| 2906 | surrogateescape: |
| 2907 | *p++ = 0xDC00 + ch; |
| 2908 | s++; |
| 2909 | } |
| 2910 | *p = L'\0'; |
| 2911 | return unicode; |
| 2912 | } |
| 2913 | |
| 2914 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2915 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2916 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 2917 | and allocate exactly as much space needed at the end. Else allocate the |
| 2918 | maximum possible needed (4 result bytes per Unicode character), and return |
| 2919 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2920 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 2921 | PyObject * |
| 2922 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2923 | Py_ssize_t size, |
| 2924 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2925 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2926 | #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] | 2927 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2928 | Py_ssize_t i; /* index into s of next input byte */ |
| 2929 | PyObject *result; /* result string object */ |
| 2930 | char *p; /* next free byte in output buffer */ |
| 2931 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 2932 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2933 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2934 | PyObject *errorHandler = NULL; |
| 2935 | PyObject *exc = NULL; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2936 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2937 | assert(s != NULL); |
| 2938 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2939 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2940 | if (size <= MAX_SHORT_UNICHARS) { |
| 2941 | /* Write into the stack buffer; nallocated can't overflow. |
| 2942 | * At the end, we'll allocate exactly as much heap space as it |
| 2943 | * turns out we need. |
| 2944 | */ |
| 2945 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2946 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2947 | p = stackbuf; |
| 2948 | } |
| 2949 | else { |
| 2950 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 2951 | nallocated = size * 4; |
| 2952 | if (nallocated / 4 != size) /* overflow! */ |
| 2953 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2954 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2955 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2956 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2957 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2958 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2959 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2960 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2961 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2962 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2963 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2964 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2965 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2966 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2967 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2968 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 2969 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 2970 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2971 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2972 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2973 | /* Special case: check for high and low surrogate */ |
| 2974 | if (ch <= 0xDBFF && i != size && 0xDC00 <= s[i] && s[i] <= 0xDFFF) { |
| 2975 | Py_UCS4 ch2 = s[i]; |
| 2976 | /* Combine the two surrogates to form a UCS4 value */ |
| 2977 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
| 2978 | i++; |
| 2979 | |
| 2980 | /* Encode UCS4 Unicode ordinals */ |
| 2981 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2982 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2983 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2984 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2985 | } else { |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 2986 | #endif |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2987 | Py_ssize_t newpos; |
| 2988 | PyObject *rep; |
| 2989 | Py_ssize_t repsize, k; |
| 2990 | rep = unicode_encode_call_errorhandler |
| 2991 | (errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 2992 | s, size, &exc, i-1, i, &newpos); |
| 2993 | if (!rep) |
| 2994 | goto error; |
| 2995 | |
| 2996 | if (PyBytes_Check(rep)) |
| 2997 | repsize = PyBytes_GET_SIZE(rep); |
| 2998 | else |
| 2999 | repsize = PyUnicode_GET_SIZE(rep); |
| 3000 | |
| 3001 | if (repsize > 4) { |
| 3002 | Py_ssize_t offset; |
| 3003 | |
| 3004 | if (result == NULL) |
| 3005 | offset = p - stackbuf; |
| 3006 | else |
| 3007 | offset = p - PyBytes_AS_STRING(result); |
| 3008 | |
| 3009 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 3010 | /* integer overflow */ |
| 3011 | PyErr_NoMemory(); |
| 3012 | goto error; |
| 3013 | } |
| 3014 | nallocated += repsize - 4; |
| 3015 | if (result != NULL) { |
| 3016 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 3017 | goto error; |
| 3018 | } else { |
| 3019 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
| 3020 | if (result == NULL) |
| 3021 | goto error; |
| 3022 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 3023 | } |
| 3024 | p = PyBytes_AS_STRING(result) + offset; |
| 3025 | } |
| 3026 | |
| 3027 | if (PyBytes_Check(rep)) { |
| 3028 | char *prep = PyBytes_AS_STRING(rep); |
| 3029 | for(k = repsize; k > 0; k--) |
| 3030 | *p++ = *prep++; |
| 3031 | } else /* rep is unicode */ { |
| 3032 | Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); |
| 3033 | Py_UNICODE c; |
| 3034 | |
| 3035 | for(k=0; k<repsize; k++) { |
| 3036 | c = prep[k]; |
| 3037 | if (0x80 <= c) { |
| 3038 | raise_encode_exception(&exc, "utf-8", s, size, |
| 3039 | i-1, i, "surrogates not allowed"); |
| 3040 | goto error; |
| 3041 | } |
| 3042 | *p++ = (char)prep[k]; |
| 3043 | } |
| 3044 | } |
| 3045 | Py_DECREF(rep); |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 3046 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 3047 | } |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 3048 | #endif |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 3049 | } else if (ch < 0x10000) { |
| 3050 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 3051 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 3052 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 3053 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3054 | /* Encode UCS4 Unicode ordinals */ |
| 3055 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 3056 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 3057 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 3058 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 3059 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3060 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 3061 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3062 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3063 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3064 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3065 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3066 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3067 | } |
| 3068 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3069 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3070 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3071 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3072 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3073 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 3074 | Py_XDECREF(errorHandler); |
| 3075 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3076 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 3077 | error: |
| 3078 | Py_XDECREF(errorHandler); |
| 3079 | Py_XDECREF(exc); |
| 3080 | Py_XDECREF(result); |
| 3081 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 3082 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 3083 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3084 | } |
| 3085 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3086 | PyObject * |
| 3087 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3088 | { |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3089 | PyObject *utf8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3090 | if (!PyUnicode_Check(unicode)) { |
| 3091 | PyErr_BadArgument(); |
| 3092 | return NULL; |
| 3093 | } |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3094 | utf8 = _PyUnicode_AsDefaultEncodedString(unicode); |
| 3095 | if (utf8 == NULL) |
| 3096 | return NULL; |
| 3097 | Py_INCREF(utf8); |
| 3098 | return utf8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3099 | } |
| 3100 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3101 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 3102 | |
| 3103 | PyObject * |
| 3104 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3105 | Py_ssize_t size, |
| 3106 | const char *errors, |
| 3107 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3108 | { |
| 3109 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 3110 | } |
| 3111 | |
| 3112 | PyObject * |
| 3113 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3114 | Py_ssize_t size, |
| 3115 | const char *errors, |
| 3116 | int *byteorder, |
| 3117 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3118 | { |
| 3119 | const char *starts = s; |
| 3120 | Py_ssize_t startinpos; |
| 3121 | Py_ssize_t endinpos; |
| 3122 | Py_ssize_t outpos; |
| 3123 | PyUnicodeObject *unicode; |
| 3124 | Py_UNICODE *p; |
| 3125 | #ifndef Py_UNICODE_WIDE |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 3126 | int pairs = 0; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 3127 | const unsigned char *qq; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3128 | #else |
| 3129 | const int pairs = 0; |
| 3130 | #endif |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 3131 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3132 | int bo = 0; /* assume native ordering by default */ |
| 3133 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3134 | /* Offsets from q for retrieving bytes in the right order. */ |
| 3135 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3136 | int iorder[] = {0, 1, 2, 3}; |
| 3137 | #else |
| 3138 | int iorder[] = {3, 2, 1, 0}; |
| 3139 | #endif |
| 3140 | PyObject *errorHandler = NULL; |
| 3141 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 3142 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3143 | q = (unsigned char *)s; |
| 3144 | e = q + size; |
| 3145 | |
| 3146 | if (byteorder) |
| 3147 | bo = *byteorder; |
| 3148 | |
| 3149 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 3150 | byte order setting accordingly. In native mode, the leading BOM |
| 3151 | mark is skipped, in all other modes, it is copied to the output |
| 3152 | stream as-is (giving a ZWNBSP character). */ |
| 3153 | if (bo == 0) { |
| 3154 | if (size >= 4) { |
| 3155 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3156 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3157 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3158 | if (bom == 0x0000FEFF) { |
| 3159 | q += 4; |
| 3160 | bo = -1; |
| 3161 | } |
| 3162 | else if (bom == 0xFFFE0000) { |
| 3163 | q += 4; |
| 3164 | bo = 1; |
| 3165 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3166 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3167 | if (bom == 0x0000FEFF) { |
| 3168 | q += 4; |
| 3169 | bo = 1; |
| 3170 | } |
| 3171 | else if (bom == 0xFFFE0000) { |
| 3172 | q += 4; |
| 3173 | bo = -1; |
| 3174 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3175 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3176 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3177 | } |
| 3178 | |
| 3179 | if (bo == -1) { |
| 3180 | /* force LE */ |
| 3181 | iorder[0] = 0; |
| 3182 | iorder[1] = 1; |
| 3183 | iorder[2] = 2; |
| 3184 | iorder[3] = 3; |
| 3185 | } |
| 3186 | else if (bo == 1) { |
| 3187 | /* force BE */ |
| 3188 | iorder[0] = 3; |
| 3189 | iorder[1] = 2; |
| 3190 | iorder[2] = 1; |
| 3191 | iorder[3] = 0; |
| 3192 | } |
| 3193 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 3194 | /* On narrow builds we split characters outside the BMP into two |
| 3195 | codepoints => count how much extra space we need. */ |
| 3196 | #ifndef Py_UNICODE_WIDE |
| 3197 | for (qq = q; qq < e; qq += 4) |
| 3198 | if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) |
| 3199 | pairs++; |
| 3200 | #endif |
| 3201 | |
| 3202 | /* This might be one to much, because of a BOM */ |
| 3203 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 3204 | if (!unicode) |
| 3205 | return NULL; |
| 3206 | if (size == 0) |
| 3207 | return (PyObject *)unicode; |
| 3208 | |
| 3209 | /* Unpack UTF-32 encoded data */ |
| 3210 | p = unicode->str; |
| 3211 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3212 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3213 | Py_UCS4 ch; |
| 3214 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 3215 | if (e-q<4) { |
| 3216 | if (consumed) |
| 3217 | break; |
| 3218 | errmsg = "truncated data"; |
| 3219 | startinpos = ((const char *)q)-starts; |
| 3220 | endinpos = ((const char *)e)-starts; |
| 3221 | goto utf32Error; |
| 3222 | /* The remaining input chars are ignored if the callback |
| 3223 | chooses to skip the input */ |
| 3224 | } |
| 3225 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 3226 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3227 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3228 | if (ch >= 0x110000) |
| 3229 | { |
| 3230 | errmsg = "codepoint not in range(0x110000)"; |
| 3231 | startinpos = ((const char *)q)-starts; |
| 3232 | endinpos = startinpos+4; |
| 3233 | goto utf32Error; |
| 3234 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3235 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3236 | if (ch >= 0x10000) |
| 3237 | { |
| 3238 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 3239 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3240 | } |
| 3241 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3242 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3243 | *p++ = ch; |
| 3244 | q += 4; |
| 3245 | continue; |
| 3246 | utf32Error: |
| 3247 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3248 | if (unicode_decode_call_errorhandler( |
| 3249 | errors, &errorHandler, |
| 3250 | "utf32", errmsg, |
| 3251 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 3252 | &unicode, &outpos, &p)) |
| 3253 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3254 | } |
| 3255 | |
| 3256 | if (byteorder) |
| 3257 | *byteorder = bo; |
| 3258 | |
| 3259 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3260 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3261 | |
| 3262 | /* Adjust length */ |
| 3263 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 3264 | goto onError; |
| 3265 | |
| 3266 | Py_XDECREF(errorHandler); |
| 3267 | Py_XDECREF(exc); |
| 3268 | return (PyObject *)unicode; |
| 3269 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3270 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3271 | Py_DECREF(unicode); |
| 3272 | Py_XDECREF(errorHandler); |
| 3273 | Py_XDECREF(exc); |
| 3274 | return NULL; |
| 3275 | } |
| 3276 | |
| 3277 | PyObject * |
| 3278 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3279 | Py_ssize_t size, |
| 3280 | const char *errors, |
| 3281 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3282 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3283 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3284 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3285 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3286 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3287 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3288 | #else |
| 3289 | const int pairs = 0; |
| 3290 | #endif |
| 3291 | /* Offsets from p for storing byte pairs in the right order. */ |
| 3292 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3293 | int iorder[] = {0, 1, 2, 3}; |
| 3294 | #else |
| 3295 | int iorder[] = {3, 2, 1, 0}; |
| 3296 | #endif |
| 3297 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3298 | #define STORECHAR(CH) \ |
| 3299 | do { \ |
| 3300 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 3301 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 3302 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 3303 | p[iorder[0]] = (CH) & 0xff; \ |
| 3304 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3305 | } while(0) |
| 3306 | |
| 3307 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 3308 | so we need less space. */ |
| 3309 | #ifndef Py_UNICODE_WIDE |
| 3310 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3311 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 3312 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 3313 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3314 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3315 | nsize = (size - pairs + (byteorder == 0)); |
| 3316 | bytesize = nsize * 4; |
| 3317 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3318 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3319 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3320 | if (v == NULL) |
| 3321 | return NULL; |
| 3322 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3323 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3324 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3325 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3326 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3327 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3328 | |
| 3329 | if (byteorder == -1) { |
| 3330 | /* force LE */ |
| 3331 | iorder[0] = 0; |
| 3332 | iorder[1] = 1; |
| 3333 | iorder[2] = 2; |
| 3334 | iorder[3] = 3; |
| 3335 | } |
| 3336 | else if (byteorder == 1) { |
| 3337 | /* force BE */ |
| 3338 | iorder[0] = 3; |
| 3339 | iorder[1] = 2; |
| 3340 | iorder[2] = 1; |
| 3341 | iorder[3] = 0; |
| 3342 | } |
| 3343 | |
| 3344 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3345 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3346 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3347 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 3348 | Py_UCS4 ch2 = *s; |
| 3349 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 3350 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 3351 | s++; |
| 3352 | size--; |
| 3353 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3354 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3355 | #endif |
| 3356 | STORECHAR(ch); |
| 3357 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3358 | |
| 3359 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3360 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3361 | #undef STORECHAR |
| 3362 | } |
| 3363 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3364 | PyObject * |
| 3365 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3366 | { |
| 3367 | if (!PyUnicode_Check(unicode)) { |
| 3368 | PyErr_BadArgument(); |
| 3369 | return NULL; |
| 3370 | } |
| 3371 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3372 | PyUnicode_GET_SIZE(unicode), |
| 3373 | NULL, |
| 3374 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3375 | } |
| 3376 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3377 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 3378 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3379 | PyObject * |
| 3380 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3381 | Py_ssize_t size, |
| 3382 | const char *errors, |
| 3383 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3384 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3385 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 3386 | } |
| 3387 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3388 | /* Two masks for fast checking of whether a C 'long' may contain |
| 3389 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 3390 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 3391 | rare in most input. |
| 3392 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 3393 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3394 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3395 | #if (SIZEOF_LONG == 8) |
| 3396 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 3397 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 3398 | #elif (SIZEOF_LONG == 4) |
| 3399 | # define FAST_CHAR_MASK 0x80008000L |
| 3400 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 3401 | #else |
| 3402 | # error C 'long' size should be either 4 or 8! |
| 3403 | #endif |
| 3404 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3405 | PyObject * |
| 3406 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3407 | Py_ssize_t size, |
| 3408 | const char *errors, |
| 3409 | int *byteorder, |
| 3410 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3411 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3412 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3413 | Py_ssize_t startinpos; |
| 3414 | Py_ssize_t endinpos; |
| 3415 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3416 | PyUnicodeObject *unicode; |
| 3417 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3418 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3419 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3420 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3421 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3422 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 3423 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3424 | int ihi = 1, ilo = 0; |
| 3425 | #else |
| 3426 | int ihi = 0, ilo = 1; |
| 3427 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3428 | PyObject *errorHandler = NULL; |
| 3429 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3430 | |
| 3431 | /* Note: size will always be longer than the resulting Unicode |
| 3432 | character count */ |
| 3433 | unicode = _PyUnicode_New(size); |
| 3434 | if (!unicode) |
| 3435 | return NULL; |
| 3436 | if (size == 0) |
| 3437 | return (PyObject *)unicode; |
| 3438 | |
| 3439 | /* Unpack UTF-16 encoded data */ |
| 3440 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3441 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3442 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3443 | |
| 3444 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3445 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3446 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3447 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 3448 | byte order setting accordingly. In native mode, the leading BOM |
| 3449 | mark is skipped, in all other modes, it is copied to the output |
| 3450 | stream as-is (giving a ZWNBSP character). */ |
| 3451 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3452 | if (size >= 2) { |
| 3453 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3454 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3455 | if (bom == 0xFEFF) { |
| 3456 | q += 2; |
| 3457 | bo = -1; |
| 3458 | } |
| 3459 | else if (bom == 0xFFFE) { |
| 3460 | q += 2; |
| 3461 | bo = 1; |
| 3462 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3463 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3464 | if (bom == 0xFEFF) { |
| 3465 | q += 2; |
| 3466 | bo = 1; |
| 3467 | } |
| 3468 | else if (bom == 0xFFFE) { |
| 3469 | q += 2; |
| 3470 | bo = -1; |
| 3471 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3472 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3473 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3474 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3475 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3476 | if (bo == -1) { |
| 3477 | /* force LE */ |
| 3478 | ihi = 1; |
| 3479 | ilo = 0; |
| 3480 | } |
| 3481 | else if (bo == 1) { |
| 3482 | /* force BE */ |
| 3483 | ihi = 0; |
| 3484 | ilo = 1; |
| 3485 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3486 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3487 | native_ordering = ilo < ihi; |
| 3488 | #else |
| 3489 | native_ordering = ilo > ihi; |
| 3490 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3491 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3492 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3493 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3494 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3495 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 3496 | reads are more expensive, better to defer to another iteration. */ |
| 3497 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 3498 | /* Fast path for runs of non-surrogate chars. */ |
| 3499 | register const unsigned char *_q = q; |
| 3500 | Py_UNICODE *_p = p; |
| 3501 | if (native_ordering) { |
| 3502 | /* Native ordering is simple: as long as the input cannot |
| 3503 | possibly contain a surrogate char, do an unrolled copy |
| 3504 | of several 16-bit code points to the target object. |
| 3505 | The non-surrogate check is done on several input bytes |
| 3506 | at a time (as many as a C 'long' can contain). */ |
| 3507 | while (_q < aligned_end) { |
| 3508 | unsigned long data = * (unsigned long *) _q; |
| 3509 | if (data & FAST_CHAR_MASK) |
| 3510 | break; |
| 3511 | _p[0] = ((unsigned short *) _q)[0]; |
| 3512 | _p[1] = ((unsigned short *) _q)[1]; |
| 3513 | #if (SIZEOF_LONG == 8) |
| 3514 | _p[2] = ((unsigned short *) _q)[2]; |
| 3515 | _p[3] = ((unsigned short *) _q)[3]; |
| 3516 | #endif |
| 3517 | _q += SIZEOF_LONG; |
| 3518 | _p += SIZEOF_LONG / 2; |
| 3519 | } |
| 3520 | } |
| 3521 | else { |
| 3522 | /* Byteswapped ordering is similar, but we must decompose |
| 3523 | the copy bytewise, and take care of zero'ing out the |
| 3524 | upper bytes if the target object is in 32-bit units |
| 3525 | (that is, in UCS-4 builds). */ |
| 3526 | while (_q < aligned_end) { |
| 3527 | unsigned long data = * (unsigned long *) _q; |
| 3528 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 3529 | break; |
| 3530 | /* Zero upper bytes in UCS-4 builds */ |
| 3531 | #if (Py_UNICODE_SIZE > 2) |
| 3532 | _p[0] = 0; |
| 3533 | _p[1] = 0; |
| 3534 | #if (SIZEOF_LONG == 8) |
| 3535 | _p[2] = 0; |
| 3536 | _p[3] = 0; |
| 3537 | #endif |
| 3538 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 3539 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 3540 | fill the two last bytes of each 4-byte unit. */ |
| 3541 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 3542 | # define OFF 2 |
| 3543 | #else |
| 3544 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3545 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 3546 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 3547 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 3548 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 3549 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 3550 | #if (SIZEOF_LONG == 8) |
| 3551 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 3552 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 3553 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 3554 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 3555 | #endif |
| 3556 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3557 | _q += SIZEOF_LONG; |
| 3558 | _p += SIZEOF_LONG / 2; |
| 3559 | } |
| 3560 | } |
| 3561 | p = _p; |
| 3562 | q = _q; |
| 3563 | if (q >= e) |
| 3564 | break; |
| 3565 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3566 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3567 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3568 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3569 | |
| 3570 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 3571 | *p++ = ch; |
| 3572 | continue; |
| 3573 | } |
| 3574 | |
| 3575 | /* UTF-16 code pair: */ |
| 3576 | if (q > e) { |
| 3577 | errmsg = "unexpected end of data"; |
| 3578 | startinpos = (((const char *)q) - 2) - starts; |
| 3579 | endinpos = ((const char *)e) + 1 - starts; |
| 3580 | goto utf16Error; |
| 3581 | } |
| 3582 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 3583 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 3584 | q += 2; |
| 3585 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3586 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3587 | *p++ = ch; |
| 3588 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3589 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3590 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3591 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3592 | continue; |
| 3593 | } |
| 3594 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3595 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3596 | startinpos = (((const char *)q)-4)-starts; |
| 3597 | endinpos = startinpos+2; |
| 3598 | goto utf16Error; |
| 3599 | } |
| 3600 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3601 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3602 | errmsg = "illegal encoding"; |
| 3603 | startinpos = (((const char *)q)-2)-starts; |
| 3604 | endinpos = startinpos+2; |
| 3605 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3606 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3607 | utf16Error: |
| 3608 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3609 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3610 | errors, |
| 3611 | &errorHandler, |
| 3612 | "utf16", errmsg, |
| 3613 | &starts, |
| 3614 | (const char **)&e, |
| 3615 | &startinpos, |
| 3616 | &endinpos, |
| 3617 | &exc, |
| 3618 | (const char **)&q, |
| 3619 | &unicode, |
| 3620 | &outpos, |
| 3621 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3622 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3623 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3624 | /* remaining byte at the end? (size should be even) */ |
| 3625 | if (e == q) { |
| 3626 | if (!consumed) { |
| 3627 | errmsg = "truncated data"; |
| 3628 | startinpos = ((const char *)q) - starts; |
| 3629 | endinpos = ((const char *)e) + 1 - starts; |
| 3630 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3631 | if (unicode_decode_call_errorhandler( |
| 3632 | errors, |
| 3633 | &errorHandler, |
| 3634 | "utf16", errmsg, |
| 3635 | &starts, |
| 3636 | (const char **)&e, |
| 3637 | &startinpos, |
| 3638 | &endinpos, |
| 3639 | &exc, |
| 3640 | (const char **)&q, |
| 3641 | &unicode, |
| 3642 | &outpos, |
| 3643 | &p)) |
| 3644 | goto onError; |
| 3645 | /* The remaining input chars are ignored if the callback |
| 3646 | chooses to skip the input */ |
| 3647 | } |
| 3648 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3649 | |
| 3650 | if (byteorder) |
| 3651 | *byteorder = bo; |
| 3652 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3653 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3654 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3655 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3656 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3657 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3658 | goto onError; |
| 3659 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3660 | Py_XDECREF(errorHandler); |
| 3661 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3662 | return (PyObject *)unicode; |
| 3663 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3664 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3665 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3666 | Py_XDECREF(errorHandler); |
| 3667 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3668 | return NULL; |
| 3669 | } |
| 3670 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3671 | #undef FAST_CHAR_MASK |
| 3672 | #undef SWAPPED_FAST_CHAR_MASK |
| 3673 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3674 | PyObject * |
| 3675 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3676 | Py_ssize_t size, |
| 3677 | const char *errors, |
| 3678 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3679 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3680 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3681 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3682 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3683 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3684 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3685 | #else |
| 3686 | const int pairs = 0; |
| 3687 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3688 | /* Offsets from p for storing byte pairs in the right order. */ |
| 3689 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3690 | int ihi = 1, ilo = 0; |
| 3691 | #else |
| 3692 | int ihi = 0, ilo = 1; |
| 3693 | #endif |
| 3694 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3695 | #define STORECHAR(CH) \ |
| 3696 | do { \ |
| 3697 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 3698 | p[ilo] = (CH) & 0xff; \ |
| 3699 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3700 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3701 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3702 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3703 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3704 | if (s[i] >= 0x10000) |
| 3705 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3706 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3707 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 3708 | if (size > PY_SSIZE_T_MAX || |
| 3709 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3710 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3711 | nsize = size + pairs + (byteorder == 0); |
| 3712 | bytesize = nsize * 2; |
| 3713 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3714 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3715 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3716 | if (v == NULL) |
| 3717 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3718 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3719 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3720 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3721 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3722 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3723 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3724 | |
| 3725 | if (byteorder == -1) { |
| 3726 | /* force LE */ |
| 3727 | ihi = 1; |
| 3728 | ilo = 0; |
| 3729 | } |
| 3730 | else if (byteorder == 1) { |
| 3731 | /* force BE */ |
| 3732 | ihi = 0; |
| 3733 | ilo = 1; |
| 3734 | } |
| 3735 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3736 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3737 | Py_UNICODE ch = *s++; |
| 3738 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3739 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3740 | if (ch >= 0x10000) { |
| 3741 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3742 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 3743 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3744 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3745 | STORECHAR(ch); |
| 3746 | if (ch2) |
| 3747 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3748 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3749 | |
| 3750 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3751 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3752 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3753 | } |
| 3754 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3755 | PyObject * |
| 3756 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3757 | { |
| 3758 | if (!PyUnicode_Check(unicode)) { |
| 3759 | PyErr_BadArgument(); |
| 3760 | return NULL; |
| 3761 | } |
| 3762 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3763 | PyUnicode_GET_SIZE(unicode), |
| 3764 | NULL, |
| 3765 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3766 | } |
| 3767 | |
| 3768 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 3769 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3770 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 3771 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3772 | PyObject * |
| 3773 | PyUnicode_DecodeUnicodeEscape(const char *s, |
| 3774 | Py_ssize_t size, |
| 3775 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3776 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3777 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3778 | Py_ssize_t startinpos; |
| 3779 | Py_ssize_t endinpos; |
| 3780 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3781 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3782 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3783 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3784 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3785 | char* message; |
| 3786 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3787 | PyObject *errorHandler = NULL; |
| 3788 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3789 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3790 | /* Escaped strings will always be longer than the resulting |
| 3791 | 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] | 3792 | length after conversion to the true value. |
| 3793 | (but if the error callback returns a long replacement string |
| 3794 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3795 | v = _PyUnicode_New(size); |
| 3796 | if (v == NULL) |
| 3797 | goto onError; |
| 3798 | if (size == 0) |
| 3799 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3800 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3801 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3802 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3803 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3804 | while (s < end) { |
| 3805 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3806 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3807 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3808 | |
| 3809 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3810 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3811 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3812 | continue; |
| 3813 | } |
| 3814 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3815 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3816 | /* \ - Escapes */ |
| 3817 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3818 | c = *s++; |
| 3819 | if (s > end) |
| 3820 | c = '\0'; /* Invalid after \ */ |
| 3821 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3822 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3823 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3824 | case '\n': break; |
| 3825 | case '\\': *p++ = '\\'; break; |
| 3826 | case '\'': *p++ = '\''; break; |
| 3827 | case '\"': *p++ = '\"'; break; |
| 3828 | case 'b': *p++ = '\b'; break; |
| 3829 | case 'f': *p++ = '\014'; break; /* FF */ |
| 3830 | case 't': *p++ = '\t'; break; |
| 3831 | case 'n': *p++ = '\n'; break; |
| 3832 | case 'r': *p++ = '\r'; break; |
| 3833 | case 'v': *p++ = '\013'; break; /* VT */ |
| 3834 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 3835 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3836 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3837 | case '0': case '1': case '2': case '3': |
| 3838 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3839 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3840 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3841 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3842 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3843 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3844 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3845 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3846 | break; |
| 3847 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3848 | /* hex escapes */ |
| 3849 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3850 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3851 | digits = 2; |
| 3852 | message = "truncated \\xXX escape"; |
| 3853 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3854 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3855 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3856 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3857 | digits = 4; |
| 3858 | message = "truncated \\uXXXX escape"; |
| 3859 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3860 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3861 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3862 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3863 | digits = 8; |
| 3864 | message = "truncated \\UXXXXXXXX escape"; |
| 3865 | hexescape: |
| 3866 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3867 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3868 | if (s+digits>end) { |
| 3869 | endinpos = size; |
| 3870 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3871 | errors, &errorHandler, |
| 3872 | "unicodeescape", "end of string in escape sequence", |
| 3873 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3874 | &v, &outpos, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3875 | goto onError; |
| 3876 | goto nextByte; |
| 3877 | } |
| 3878 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3879 | c = (unsigned char) s[i]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 3880 | if (!Py_ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3881 | endinpos = (s+i+1)-starts; |
| 3882 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3883 | errors, &errorHandler, |
| 3884 | "unicodeescape", message, |
| 3885 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3886 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3887 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3888 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3889 | } |
| 3890 | chr = (chr<<4) & ~0xF; |
| 3891 | if (c >= '0' && c <= '9') |
| 3892 | chr += c - '0'; |
| 3893 | else if (c >= 'a' && c <= 'f') |
| 3894 | chr += 10 + c - 'a'; |
| 3895 | else |
| 3896 | chr += 10 + c - 'A'; |
| 3897 | } |
| 3898 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 3899 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3900 | /* _decoding_error will have already written into the |
| 3901 | target buffer. */ |
| 3902 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3903 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3904 | /* when we get here, chr is a 32-bit unicode character */ |
| 3905 | if (chr <= 0xffff) |
| 3906 | /* UCS-2 character */ |
| 3907 | *p++ = (Py_UNICODE) chr; |
| 3908 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3909 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3910 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3911 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3912 | *p++ = chr; |
| 3913 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3914 | chr -= 0x10000L; |
| 3915 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 3916 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3917 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3918 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3919 | endinpos = s-starts; |
| 3920 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3921 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3922 | errors, &errorHandler, |
| 3923 | "unicodeescape", "illegal Unicode character", |
| 3924 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3925 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3926 | goto onError; |
| 3927 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3928 | break; |
| 3929 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3930 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3931 | case 'N': |
| 3932 | message = "malformed \\N character escape"; |
| 3933 | if (ucnhash_CAPI == NULL) { |
| 3934 | /* load the unicode data module */ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3935 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import(PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3936 | if (ucnhash_CAPI == NULL) |
| 3937 | goto ucnhashError; |
| 3938 | } |
| 3939 | if (*s == '{') { |
| 3940 | const char *start = s+1; |
| 3941 | /* look for the closing brace */ |
| 3942 | while (*s != '}' && s < end) |
| 3943 | s++; |
| 3944 | if (s > start && s < end && *s == '}') { |
| 3945 | /* found a name. look it up in the unicode database */ |
| 3946 | message = "unknown Unicode character name"; |
| 3947 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 3948 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3949 | goto store; |
| 3950 | } |
| 3951 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3952 | endinpos = s-starts; |
| 3953 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3954 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3955 | errors, &errorHandler, |
| 3956 | "unicodeescape", message, |
| 3957 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3958 | &v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3959 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3960 | break; |
| 3961 | |
| 3962 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3963 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3964 | message = "\\ at end of string"; |
| 3965 | s--; |
| 3966 | endinpos = s-starts; |
| 3967 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3968 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3969 | errors, &errorHandler, |
| 3970 | "unicodeescape", message, |
| 3971 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3972 | &v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3973 | goto onError; |
| 3974 | } |
| 3975 | else { |
| 3976 | *p++ = '\\'; |
| 3977 | *p++ = (unsigned char)s[-1]; |
| 3978 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3979 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3980 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3981 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3982 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3983 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3984 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3985 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 3986 | Py_XDECREF(errorHandler); |
| 3987 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3988 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3989 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3990 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3991 | PyErr_SetString( |
| 3992 | PyExc_UnicodeError, |
| 3993 | "\\N escapes not supported (can't load unicodedata module)" |
| 3994 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3995 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3996 | Py_XDECREF(errorHandler); |
| 3997 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 3998 | return NULL; |
| 3999 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4000 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4001 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4002 | Py_XDECREF(errorHandler); |
| 4003 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4004 | return NULL; |
| 4005 | } |
| 4006 | |
| 4007 | /* Return a Unicode-Escape string version of the Unicode object. |
| 4008 | |
| 4009 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 4010 | appropriate. |
| 4011 | |
| 4012 | */ |
| 4013 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4014 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4015 | Py_ssize_t size, |
| 4016 | Py_UNICODE ch) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4017 | { |
| 4018 | /* like wcschr, but doesn't stop at NULL characters */ |
| 4019 | |
| 4020 | while (size-- > 0) { |
| 4021 | if (*s == ch) |
| 4022 | return s; |
| 4023 | s++; |
| 4024 | } |
| 4025 | |
| 4026 | return NULL; |
| 4027 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 4028 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 4029 | static const char *hexdigits = "0123456789abcdef"; |
| 4030 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4031 | PyObject * |
| 4032 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 4033 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4034 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4035 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4036 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4037 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4038 | #ifdef Py_UNICODE_WIDE |
| 4039 | const Py_ssize_t expandsize = 10; |
| 4040 | #else |
| 4041 | const Py_ssize_t expandsize = 6; |
| 4042 | #endif |
| 4043 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4044 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 4045 | better to choose a different scheme. Perhaps scan the |
| 4046 | first N-chars of the string and allocate based on that size. |
| 4047 | */ |
| 4048 | /* Initial allocation is based on the longest-possible unichr |
| 4049 | escape. |
| 4050 | |
| 4051 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 4052 | unichr, so in this case it's the longest unichr escape. In |
| 4053 | narrow (UTF-16) builds this is five chars per source unichr |
| 4054 | since there are two unichrs in the surrogate pair, so in narrow |
| 4055 | (UTF-16) builds it's not the longest unichr escape. |
| 4056 | |
| 4057 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 4058 | so in the narrow (UTF-16) build case it's the longest unichr |
| 4059 | escape. |
| 4060 | */ |
| 4061 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4062 | if (size == 0) |
| 4063 | return PyBytes_FromStringAndSize(NULL, 0); |
| 4064 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4065 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4066 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4067 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4068 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4069 | 2 |
| 4070 | + expandsize*size |
| 4071 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4072 | if (repr == NULL) |
| 4073 | return NULL; |
| 4074 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4075 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4076 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4077 | while (size-- > 0) { |
| 4078 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 4079 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 4080 | /* Escape backslashes */ |
| 4081 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4082 | *p++ = '\\'; |
| 4083 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 4084 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4085 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 4086 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 4087 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4088 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 4089 | else if (ch >= 0x10000) { |
| 4090 | *p++ = '\\'; |
| 4091 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 4092 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 4093 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 4094 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 4095 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 4096 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 4097 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 4098 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 4099 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4100 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4101 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4102 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4103 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 4104 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 4105 | Py_UNICODE ch2; |
| 4106 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4107 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4108 | ch2 = *s++; |
| 4109 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 4110 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4111 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 4112 | *p++ = '\\'; |
| 4113 | *p++ = 'U'; |
| 4114 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 4115 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 4116 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 4117 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 4118 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 4119 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 4120 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 4121 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 4122 | continue; |
| 4123 | } |
| 4124 | /* Fall through: isolated surrogates are copied as-is */ |
| 4125 | s--; |
| 4126 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4127 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4128 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 4129 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4130 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 4131 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4132 | *p++ = '\\'; |
| 4133 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 4134 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 4135 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 4136 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 4137 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4138 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 4139 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 4140 | /* Map special whitespace to '\t', \n', '\r' */ |
| 4141 | else if (ch == '\t') { |
| 4142 | *p++ = '\\'; |
| 4143 | *p++ = 't'; |
| 4144 | } |
| 4145 | else if (ch == '\n') { |
| 4146 | *p++ = '\\'; |
| 4147 | *p++ = 'n'; |
| 4148 | } |
| 4149 | else if (ch == '\r') { |
| 4150 | *p++ = '\\'; |
| 4151 | *p++ = 'r'; |
| 4152 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 4153 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 4154 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 4155 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4156 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 4157 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 4158 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 4159 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4160 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 4161 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4162 | /* Copy everything else as-is */ |
| 4163 | else |
| 4164 | *p++ = (char) ch; |
| 4165 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4166 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4167 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 4168 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 4169 | return NULL; |
| 4170 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4171 | } |
| 4172 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4173 | PyObject * |
| 4174 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4175 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 4176 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4177 | if (!PyUnicode_Check(unicode)) { |
| 4178 | PyErr_BadArgument(); |
| 4179 | return NULL; |
| 4180 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 4181 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 4182 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 4183 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4184 | } |
| 4185 | |
| 4186 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 4187 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4188 | PyObject * |
| 4189 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
| 4190 | Py_ssize_t size, |
| 4191 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4192 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4193 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4194 | Py_ssize_t startinpos; |
| 4195 | Py_ssize_t endinpos; |
| 4196 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4197 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4198 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4199 | const char *end; |
| 4200 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4201 | PyObject *errorHandler = NULL; |
| 4202 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4203 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4204 | /* Escaped strings will always be longer than the resulting |
| 4205 | 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] | 4206 | length after conversion to the true value. (But decoding error |
| 4207 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4208 | v = _PyUnicode_New(size); |
| 4209 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4210 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4211 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4212 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4213 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4214 | end = s + size; |
| 4215 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4216 | unsigned char c; |
| 4217 | Py_UCS4 x; |
| 4218 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4219 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4220 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4221 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 4222 | if (*s != '\\') { |
| 4223 | *p++ = (unsigned char)*s++; |
| 4224 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4225 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4226 | startinpos = s-starts; |
| 4227 | |
| 4228 | /* \u-escapes are only interpreted iff the number of leading |
| 4229 | backslashes if odd */ |
| 4230 | bs = s; |
| 4231 | for (;s < end;) { |
| 4232 | if (*s != '\\') |
| 4233 | break; |
| 4234 | *p++ = (unsigned char)*s++; |
| 4235 | } |
| 4236 | if (((s - bs) & 1) == 0 || |
| 4237 | s >= end || |
| 4238 | (*s != 'u' && *s != 'U')) { |
| 4239 | continue; |
| 4240 | } |
| 4241 | p--; |
| 4242 | count = *s=='u' ? 4 : 8; |
| 4243 | s++; |
| 4244 | |
| 4245 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 4246 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4247 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 4248 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 4249 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4250 | endinpos = s-starts; |
| 4251 | if (unicode_decode_call_errorhandler( |
| 4252 | errors, &errorHandler, |
| 4253 | "rawunicodeescape", "truncated \\uXXXX", |
| 4254 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 4255 | &v, &outpos, &p)) |
| 4256 | goto onError; |
| 4257 | goto nextByte; |
| 4258 | } |
| 4259 | x = (x<<4) & ~0xF; |
| 4260 | if (c >= '0' && c <= '9') |
| 4261 | x += c - '0'; |
| 4262 | else if (c >= 'a' && c <= 'f') |
| 4263 | x += 10 + c - 'a'; |
| 4264 | else |
| 4265 | x += 10 + c - 'A'; |
| 4266 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4267 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4268 | /* UCS-2 character */ |
| 4269 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4270 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4271 | /* UCS-4 character. Either store directly, or as |
| 4272 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4273 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4274 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4275 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4276 | x -= 0x10000L; |
| 4277 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 4278 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4279 | #endif |
| 4280 | } else { |
| 4281 | endinpos = s-starts; |
| 4282 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4283 | if (unicode_decode_call_errorhandler( |
| 4284 | errors, &errorHandler, |
| 4285 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4286 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 4287 | &v, &outpos, &p)) |
| 4288 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4289 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4290 | nextByte: |
| 4291 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4292 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4293 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4294 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4295 | Py_XDECREF(errorHandler); |
| 4296 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4297 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4298 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4299 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4300 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4301 | Py_XDECREF(errorHandler); |
| 4302 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4303 | return NULL; |
| 4304 | } |
| 4305 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4306 | PyObject * |
| 4307 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 4308 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4309 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4310 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4311 | char *p; |
| 4312 | char *q; |
| 4313 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4314 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4315 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4316 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4317 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4318 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4319 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4320 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4321 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4322 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4323 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4324 | if (repr == NULL) |
| 4325 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 4326 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4327 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4328 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4329 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4330 | while (size-- > 0) { |
| 4331 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4332 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4333 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 4334 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4335 | *p++ = '\\'; |
| 4336 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 4337 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 4338 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 4339 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 4340 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 4341 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 4342 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 4343 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 4344 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4345 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4346 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4347 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4348 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 4349 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 4350 | Py_UNICODE ch2; |
| 4351 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4352 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4353 | ch2 = *s++; |
| 4354 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 4355 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4356 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 4357 | *p++ = '\\'; |
| 4358 | *p++ = 'U'; |
| 4359 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 4360 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 4361 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 4362 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 4363 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 4364 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 4365 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 4366 | *p++ = hexdigits[ucs & 0xf]; |
| 4367 | continue; |
| 4368 | } |
| 4369 | /* Fall through: isolated surrogates are copied as-is */ |
| 4370 | s--; |
| 4371 | size++; |
| 4372 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4373 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4374 | /* Map 16-bit characters to '\uxxxx' */ |
| 4375 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4376 | *p++ = '\\'; |
| 4377 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 4378 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 4379 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 4380 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 4381 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4382 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4383 | /* Copy everything else as-is */ |
| 4384 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4385 | *p++ = (char) ch; |
| 4386 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4387 | size = p - q; |
| 4388 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4389 | assert(size > 0); |
| 4390 | if (_PyBytes_Resize(&repr, size) < 0) |
| 4391 | return NULL; |
| 4392 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4393 | } |
| 4394 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4395 | PyObject * |
| 4396 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4397 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 4398 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4399 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 4400 | PyErr_BadArgument(); |
| 4401 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4402 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 4403 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 4404 | PyUnicode_GET_SIZE(unicode)); |
| 4405 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 4406 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4407 | } |
| 4408 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4409 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 4410 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4411 | PyObject * |
| 4412 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
| 4413 | Py_ssize_t size, |
| 4414 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4415 | { |
| 4416 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4417 | Py_ssize_t startinpos; |
| 4418 | Py_ssize_t endinpos; |
| 4419 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4420 | PyUnicodeObject *v; |
| 4421 | Py_UNICODE *p; |
| 4422 | const char *end; |
| 4423 | const char *reason; |
| 4424 | PyObject *errorHandler = NULL; |
| 4425 | PyObject *exc = NULL; |
| 4426 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 4427 | #ifdef Py_UNICODE_WIDE |
| 4428 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 4429 | #endif |
| 4430 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4431 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4432 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 4433 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4434 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4435 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4436 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4437 | p = PyUnicode_AS_UNICODE(v); |
| 4438 | end = s + size; |
| 4439 | |
| 4440 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4441 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4442 | /* We have to sanity check the raw data, otherwise doom looms for |
| 4443 | some malformed UCS-4 data. */ |
| 4444 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4445 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4446 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4447 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4448 | end-s < Py_UNICODE_SIZE |
| 4449 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4450 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4451 | startinpos = s - starts; |
| 4452 | if (end-s < Py_UNICODE_SIZE) { |
| 4453 | endinpos = end-starts; |
| 4454 | reason = "truncated input"; |
| 4455 | } |
| 4456 | else { |
| 4457 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 4458 | reason = "illegal code point (> 0x10FFFF)"; |
| 4459 | } |
| 4460 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 4461 | if (unicode_decode_call_errorhandler( |
| 4462 | errors, &errorHandler, |
| 4463 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4464 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 4465 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4466 | goto onError; |
| 4467 | } |
| 4468 | } |
| 4469 | else { |
| 4470 | p++; |
| 4471 | s += Py_UNICODE_SIZE; |
| 4472 | } |
| 4473 | } |
| 4474 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4475 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4476 | goto onError; |
| 4477 | Py_XDECREF(errorHandler); |
| 4478 | Py_XDECREF(exc); |
| 4479 | return (PyObject *)v; |
| 4480 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4481 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4482 | Py_XDECREF(v); |
| 4483 | Py_XDECREF(errorHandler); |
| 4484 | Py_XDECREF(exc); |
| 4485 | return NULL; |
| 4486 | } |
| 4487 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4488 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 4489 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4490 | PyObject * |
| 4491 | PyUnicode_DecodeLatin1(const char *s, |
| 4492 | Py_ssize_t size, |
| 4493 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4494 | { |
| 4495 | PyUnicodeObject *v; |
| 4496 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4497 | const char *e, *unrolled_end; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4498 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4499 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4500 | if (size == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4501 | Py_UNICODE r = *(unsigned char*)s; |
| 4502 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4503 | } |
| 4504 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4505 | v = _PyUnicode_New(size); |
| 4506 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4507 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4508 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4509 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4510 | p = PyUnicode_AS_UNICODE(v); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4511 | e = s + size; |
| 4512 | /* Unrolling the copy makes it much faster by reducing the looping |
| 4513 | overhead. This is similar to what many memcpy() implementations do. */ |
| 4514 | unrolled_end = e - 4; |
| 4515 | while (s < unrolled_end) { |
| 4516 | p[0] = (unsigned char) s[0]; |
| 4517 | p[1] = (unsigned char) s[1]; |
| 4518 | p[2] = (unsigned char) s[2]; |
| 4519 | p[3] = (unsigned char) s[3]; |
| 4520 | s += 4; |
| 4521 | p += 4; |
| 4522 | } |
| 4523 | while (s < e) |
| 4524 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4525 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4526 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4527 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4528 | Py_XDECREF(v); |
| 4529 | return NULL; |
| 4530 | } |
| 4531 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4532 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4533 | static void |
| 4534 | make_encode_exception(PyObject **exceptionObject, |
| 4535 | const char *encoding, |
| 4536 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4537 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4538 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4539 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4540 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4541 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 4542 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4543 | } |
| 4544 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4545 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 4546 | goto onError; |
| 4547 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 4548 | goto onError; |
| 4549 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 4550 | goto onError; |
| 4551 | return; |
| 4552 | onError: |
| 4553 | Py_DECREF(*exceptionObject); |
| 4554 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4555 | } |
| 4556 | } |
| 4557 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4558 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4559 | static void |
| 4560 | raise_encode_exception(PyObject **exceptionObject, |
| 4561 | const char *encoding, |
| 4562 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4563 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4564 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4565 | { |
| 4566 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4567 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4568 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4569 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4570 | } |
| 4571 | |
| 4572 | /* error handling callback helper: |
| 4573 | build arguments, call the callback and check the arguments, |
| 4574 | put the result into newpos and return the replacement string, which |
| 4575 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4576 | static PyObject * |
| 4577 | unicode_encode_call_errorhandler(const char *errors, |
| 4578 | PyObject **errorHandler, |
| 4579 | const char *encoding, const char *reason, |
| 4580 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4581 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4582 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4583 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4584 | 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] | 4585 | |
| 4586 | PyObject *restuple; |
| 4587 | PyObject *resunicode; |
| 4588 | |
| 4589 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4590 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4591 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4592 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4593 | } |
| 4594 | |
| 4595 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4596 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4597 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4598 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4599 | |
| 4600 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4601 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4602 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4603 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4604 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4605 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4606 | Py_DECREF(restuple); |
| 4607 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4608 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4609 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4610 | &resunicode, newpos)) { |
| 4611 | Py_DECREF(restuple); |
| 4612 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4613 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4614 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 4615 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 4616 | Py_DECREF(restuple); |
| 4617 | return NULL; |
| 4618 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4619 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4620 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4621 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4622 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 4623 | Py_DECREF(restuple); |
| 4624 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4625 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4626 | Py_INCREF(resunicode); |
| 4627 | Py_DECREF(restuple); |
| 4628 | return resunicode; |
| 4629 | } |
| 4630 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4631 | static PyObject * |
| 4632 | unicode_encode_ucs1(const Py_UNICODE *p, |
| 4633 | Py_ssize_t size, |
| 4634 | const char *errors, |
| 4635 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4636 | { |
| 4637 | /* output object */ |
| 4638 | PyObject *res; |
| 4639 | /* pointers to the beginning and end+1 of input */ |
| 4640 | const Py_UNICODE *startp = p; |
| 4641 | const Py_UNICODE *endp = p + size; |
| 4642 | /* pointer to the beginning of the unencodable characters */ |
| 4643 | /* const Py_UNICODE *badp = NULL; */ |
| 4644 | /* pointer into the output */ |
| 4645 | char *str; |
| 4646 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4647 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4648 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 4649 | 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] | 4650 | PyObject *errorHandler = NULL; |
| 4651 | PyObject *exc = NULL; |
| 4652 | /* the following variable is used for caching string comparisons |
| 4653 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 4654 | int known_errorHandler = -1; |
| 4655 | |
| 4656 | /* allocate enough for a simple encoding without |
| 4657 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4658 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4659 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4660 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4661 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4662 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4663 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4664 | ressize = size; |
| 4665 | |
| 4666 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4667 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4668 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4669 | /* can we encode this? */ |
| 4670 | if (c<limit) { |
| 4671 | /* no overflow check, because we know that the space is enough */ |
| 4672 | *str++ = (char)c; |
| 4673 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4674 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4675 | else { |
| 4676 | Py_ssize_t unicodepos = p-startp; |
| 4677 | Py_ssize_t requiredsize; |
| 4678 | PyObject *repunicode; |
| 4679 | Py_ssize_t repsize; |
| 4680 | Py_ssize_t newpos; |
| 4681 | Py_ssize_t respos; |
| 4682 | Py_UNICODE *uni2; |
| 4683 | /* startpos for collecting unencodable chars */ |
| 4684 | const Py_UNICODE *collstart = p; |
| 4685 | const Py_UNICODE *collend = p; |
| 4686 | /* find all unecodable characters */ |
| 4687 | while ((collend < endp) && ((*collend)>=limit)) |
| 4688 | ++collend; |
| 4689 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 4690 | if (known_errorHandler==-1) { |
| 4691 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4692 | known_errorHandler = 1; |
| 4693 | else if (!strcmp(errors, "replace")) |
| 4694 | known_errorHandler = 2; |
| 4695 | else if (!strcmp(errors, "ignore")) |
| 4696 | known_errorHandler = 3; |
| 4697 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4698 | known_errorHandler = 4; |
| 4699 | else |
| 4700 | known_errorHandler = 0; |
| 4701 | } |
| 4702 | switch (known_errorHandler) { |
| 4703 | case 1: /* strict */ |
| 4704 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 4705 | goto onError; |
| 4706 | case 2: /* replace */ |
| 4707 | while (collstart++<collend) |
| 4708 | *str++ = '?'; /* fall through */ |
| 4709 | case 3: /* ignore */ |
| 4710 | p = collend; |
| 4711 | break; |
| 4712 | case 4: /* xmlcharrefreplace */ |
| 4713 | respos = str - PyBytes_AS_STRING(res); |
| 4714 | /* determine replacement size (temporarily (mis)uses p) */ |
| 4715 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 4716 | if (*p<10) |
| 4717 | repsize += 2+1+1; |
| 4718 | else if (*p<100) |
| 4719 | repsize += 2+2+1; |
| 4720 | else if (*p<1000) |
| 4721 | repsize += 2+3+1; |
| 4722 | else if (*p<10000) |
| 4723 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4724 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4725 | else |
| 4726 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4727 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4728 | else if (*p<100000) |
| 4729 | repsize += 2+5+1; |
| 4730 | else if (*p<1000000) |
| 4731 | repsize += 2+6+1; |
| 4732 | else |
| 4733 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4734 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4735 | } |
| 4736 | requiredsize = respos+repsize+(endp-collend); |
| 4737 | if (requiredsize > ressize) { |
| 4738 | if (requiredsize<2*ressize) |
| 4739 | requiredsize = 2*ressize; |
| 4740 | if (_PyBytes_Resize(&res, requiredsize)) |
| 4741 | goto onError; |
| 4742 | str = PyBytes_AS_STRING(res) + respos; |
| 4743 | ressize = requiredsize; |
| 4744 | } |
| 4745 | /* generate replacement (temporarily (mis)uses p) */ |
| 4746 | for (p = collstart; p < collend; ++p) { |
| 4747 | str += sprintf(str, "&#%d;", (int)*p); |
| 4748 | } |
| 4749 | p = collend; |
| 4750 | break; |
| 4751 | default: |
| 4752 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 4753 | encoding, reason, startp, size, &exc, |
| 4754 | collstart-startp, collend-startp, &newpos); |
| 4755 | if (repunicode == NULL) |
| 4756 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4757 | if (PyBytes_Check(repunicode)) { |
| 4758 | /* Directly copy bytes result to output. */ |
| 4759 | repsize = PyBytes_Size(repunicode); |
| 4760 | if (repsize > 1) { |
| 4761 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 4762 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4763 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 4764 | Py_DECREF(repunicode); |
| 4765 | goto onError; |
| 4766 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 4767 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4768 | ressize += repsize-1; |
| 4769 | } |
| 4770 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 4771 | str += repsize; |
| 4772 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4773 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4774 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4775 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4776 | /* need more space? (at least enough for what we |
| 4777 | have+the replacement+the rest of the string, so |
| 4778 | we won't have to check space for encodable characters) */ |
| 4779 | respos = str - PyBytes_AS_STRING(res); |
| 4780 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4781 | requiredsize = respos+repsize+(endp-collend); |
| 4782 | if (requiredsize > ressize) { |
| 4783 | if (requiredsize<2*ressize) |
| 4784 | requiredsize = 2*ressize; |
| 4785 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 4786 | Py_DECREF(repunicode); |
| 4787 | goto onError; |
| 4788 | } |
| 4789 | str = PyBytes_AS_STRING(res) + respos; |
| 4790 | ressize = requiredsize; |
| 4791 | } |
| 4792 | /* check if there is anything unencodable in the replacement |
| 4793 | and copy it to the output */ |
| 4794 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 4795 | c = *uni2; |
| 4796 | if (c >= limit) { |
| 4797 | raise_encode_exception(&exc, encoding, startp, size, |
| 4798 | unicodepos, unicodepos+1, reason); |
| 4799 | Py_DECREF(repunicode); |
| 4800 | goto onError; |
| 4801 | } |
| 4802 | *str = (char)c; |
| 4803 | } |
| 4804 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4805 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4806 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4807 | } |
| 4808 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4809 | /* Resize if we allocated to much */ |
| 4810 | size = str - PyBytes_AS_STRING(res); |
| 4811 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 4812 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4813 | if (_PyBytes_Resize(&res, size) < 0) |
| 4814 | goto onError; |
| 4815 | } |
| 4816 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4817 | Py_XDECREF(errorHandler); |
| 4818 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4819 | return res; |
| 4820 | |
| 4821 | onError: |
| 4822 | Py_XDECREF(res); |
| 4823 | Py_XDECREF(errorHandler); |
| 4824 | Py_XDECREF(exc); |
| 4825 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4826 | } |
| 4827 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4828 | PyObject * |
| 4829 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
| 4830 | Py_ssize_t size, |
| 4831 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4832 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4833 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4834 | } |
| 4835 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4836 | PyObject * |
| 4837 | PyUnicode_AsLatin1String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4838 | { |
| 4839 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4840 | PyErr_BadArgument(); |
| 4841 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4842 | } |
| 4843 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4844 | PyUnicode_GET_SIZE(unicode), |
| 4845 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4846 | } |
| 4847 | |
| 4848 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 4849 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4850 | PyObject * |
| 4851 | PyUnicode_DecodeASCII(const char *s, |
| 4852 | Py_ssize_t size, |
| 4853 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4854 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4855 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4856 | PyUnicodeObject *v; |
| 4857 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4858 | Py_ssize_t startinpos; |
| 4859 | Py_ssize_t endinpos; |
| 4860 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4861 | const char *e; |
| 4862 | PyObject *errorHandler = NULL; |
| 4863 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4864 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4865 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4866 | if (size == 1 && *(unsigned char*)s < 128) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4867 | Py_UNICODE r = *(unsigned char*)s; |
| 4868 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4869 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4870 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4871 | v = _PyUnicode_New(size); |
| 4872 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4873 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4874 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4875 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4876 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4877 | e = s + size; |
| 4878 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4879 | register unsigned char c = (unsigned char)*s; |
| 4880 | if (c < 128) { |
| 4881 | *p++ = c; |
| 4882 | ++s; |
| 4883 | } |
| 4884 | else { |
| 4885 | startinpos = s-starts; |
| 4886 | endinpos = startinpos + 1; |
| 4887 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
| 4888 | if (unicode_decode_call_errorhandler( |
| 4889 | errors, &errorHandler, |
| 4890 | "ascii", "ordinal not in range(128)", |
| 4891 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4892 | &v, &outpos, &p)) |
| 4893 | goto onError; |
| 4894 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4895 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 4896 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4897 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4898 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4899 | Py_XDECREF(errorHandler); |
| 4900 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4901 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4902 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4903 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4904 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4905 | Py_XDECREF(errorHandler); |
| 4906 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4907 | return NULL; |
| 4908 | } |
| 4909 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4910 | PyObject * |
| 4911 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 4912 | Py_ssize_t size, |
| 4913 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4914 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4915 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4916 | } |
| 4917 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4918 | PyObject * |
| 4919 | PyUnicode_AsASCIIString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4920 | { |
| 4921 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4922 | PyErr_BadArgument(); |
| 4923 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4924 | } |
| 4925 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4926 | PyUnicode_GET_SIZE(unicode), |
| 4927 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4928 | } |
| 4929 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4930 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4931 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4932 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4933 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 4934 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4935 | #define NEED_RETRY |
| 4936 | #endif |
| 4937 | |
| 4938 | /* XXX This code is limited to "true" double-byte encodings, as |
| 4939 | a) it assumes an incomplete character consists of a single byte, and |
| 4940 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4941 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4942 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4943 | static int |
| 4944 | is_dbcs_lead_byte(const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4945 | { |
| 4946 | const char *curr = s + offset; |
| 4947 | |
| 4948 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4949 | const char *prev = CharPrev(s, curr); |
| 4950 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4951 | } |
| 4952 | return 0; |
| 4953 | } |
| 4954 | |
| 4955 | /* |
| 4956 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 4957 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 4958 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4959 | static int |
| 4960 | decode_mbcs(PyUnicodeObject **v, |
| 4961 | const char *s, /* MBCS string */ |
| 4962 | int size, /* sizeof MBCS string */ |
| 4963 | int final, |
| 4964 | const char *errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4965 | { |
| 4966 | Py_UNICODE *p; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4967 | Py_ssize_t n; |
| 4968 | DWORD usize; |
| 4969 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4970 | |
| 4971 | assert(size >= 0); |
| 4972 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4973 | /* check and handle 'errors' arg */ |
| 4974 | if (errors==NULL || strcmp(errors, "strict")==0) |
| 4975 | flags = MB_ERR_INVALID_CHARS; |
| 4976 | else if (strcmp(errors, "ignore")==0) |
| 4977 | flags = 0; |
| 4978 | else { |
| 4979 | PyErr_Format(PyExc_ValueError, |
| 4980 | "mbcs encoding does not support errors='%s'", |
| 4981 | errors); |
| 4982 | return -1; |
| 4983 | } |
| 4984 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4985 | /* Skip trailing lead-byte unless 'final' is set */ |
| 4986 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4987 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4988 | |
| 4989 | /* First get the size of the result */ |
| 4990 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4991 | usize = MultiByteToWideChar(CP_ACP, flags, s, size, NULL, 0); |
| 4992 | if (usize==0) |
| 4993 | goto mbcs_decode_error; |
| 4994 | } else |
| 4995 | usize = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4996 | |
| 4997 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4998 | /* Create unicode object */ |
| 4999 | *v = _PyUnicode_New(usize); |
| 5000 | if (*v == NULL) |
| 5001 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5002 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5003 | } |
| 5004 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5005 | /* Extend unicode object */ |
| 5006 | n = PyUnicode_GET_SIZE(*v); |
| 5007 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 5008 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5009 | } |
| 5010 | |
| 5011 | /* Do the conversion */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5012 | if (usize > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5013 | p = PyUnicode_AS_UNICODE(*v) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5014 | if (0 == MultiByteToWideChar(CP_ACP, flags, s, size, p, usize)) { |
| 5015 | goto mbcs_decode_error; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5016 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5017 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5018 | return size; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5019 | |
| 5020 | mbcs_decode_error: |
| 5021 | /* If the last error was ERROR_NO_UNICODE_TRANSLATION, then |
| 5022 | we raise a UnicodeDecodeError - else it is a 'generic' |
| 5023 | windows error |
| 5024 | */ |
| 5025 | if (GetLastError()==ERROR_NO_UNICODE_TRANSLATION) { |
| 5026 | /* Ideally, we should get reason from FormatMessage - this |
| 5027 | is the Windows 2000 English version of the message |
| 5028 | */ |
| 5029 | PyObject *exc = NULL; |
| 5030 | const char *reason = "No mapping for the Unicode character exists " |
| 5031 | "in the target multi-byte code page."; |
| 5032 | make_decode_exception(&exc, "mbcs", s, size, 0, 0, reason); |
| 5033 | if (exc != NULL) { |
| 5034 | PyCodec_StrictErrors(exc); |
| 5035 | Py_DECREF(exc); |
| 5036 | } |
| 5037 | } else { |
| 5038 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 5039 | } |
| 5040 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5041 | } |
| 5042 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5043 | PyObject * |
| 5044 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 5045 | Py_ssize_t size, |
| 5046 | const char *errors, |
| 5047 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5048 | { |
| 5049 | PyUnicodeObject *v = NULL; |
| 5050 | int done; |
| 5051 | |
| 5052 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5053 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5054 | |
| 5055 | #ifdef NEED_RETRY |
| 5056 | retry: |
| 5057 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5058 | done = decode_mbcs(&v, s, INT_MAX, 0, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5059 | else |
| 5060 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5061 | done = decode_mbcs(&v, s, (int)size, !consumed, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5062 | |
| 5063 | if (done < 0) { |
| 5064 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5065 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5066 | } |
| 5067 | |
| 5068 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5069 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5070 | |
| 5071 | #ifdef NEED_RETRY |
| 5072 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5073 | s += done; |
| 5074 | size -= done; |
| 5075 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5076 | } |
| 5077 | #endif |
| 5078 | |
| 5079 | return (PyObject *)v; |
| 5080 | } |
| 5081 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5082 | PyObject * |
| 5083 | PyUnicode_DecodeMBCS(const char *s, |
| 5084 | Py_ssize_t size, |
| 5085 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5086 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5087 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 5088 | } |
| 5089 | |
| 5090 | /* |
| 5091 | * Convert unicode into string object (MBCS). |
| 5092 | * Returns 0 if succeed, -1 otherwise. |
| 5093 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5094 | static int |
| 5095 | encode_mbcs(PyObject **repr, |
| 5096 | const Py_UNICODE *p, /* unicode */ |
| 5097 | int size, /* size of unicode */ |
| 5098 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5099 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5100 | BOOL usedDefaultChar = FALSE; |
| 5101 | BOOL *pusedDefaultChar; |
| 5102 | int mbcssize; |
| 5103 | Py_ssize_t n; |
| 5104 | PyObject *exc = NULL; |
| 5105 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5106 | |
| 5107 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5108 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5109 | /* check and handle 'errors' arg */ |
| 5110 | if (errors==NULL || strcmp(errors, "strict")==0) { |
| 5111 | flags = WC_NO_BEST_FIT_CHARS; |
| 5112 | pusedDefaultChar = &usedDefaultChar; |
| 5113 | } else if (strcmp(errors, "replace")==0) { |
| 5114 | flags = 0; |
| 5115 | pusedDefaultChar = NULL; |
| 5116 | } else { |
| 5117 | PyErr_Format(PyExc_ValueError, |
| 5118 | "mbcs encoding does not support errors='%s'", |
| 5119 | errors); |
| 5120 | return -1; |
| 5121 | } |
| 5122 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5123 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5124 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5125 | mbcssize = WideCharToMultiByte(CP_ACP, flags, p, size, NULL, 0, |
| 5126 | NULL, pusedDefaultChar); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5127 | if (mbcssize == 0) { |
| 5128 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 5129 | return -1; |
| 5130 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5131 | /* If we used a default char, then we failed! */ |
| 5132 | if (pusedDefaultChar && *pusedDefaultChar) |
| 5133 | goto mbcs_encode_error; |
| 5134 | } else { |
| 5135 | mbcssize = 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5136 | } |
| 5137 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5138 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5139 | /* Create string object */ |
| 5140 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 5141 | if (*repr == NULL) |
| 5142 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5143 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5144 | } |
| 5145 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5146 | /* Extend string object */ |
| 5147 | n = PyBytes_Size(*repr); |
| 5148 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 5149 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5150 | } |
| 5151 | |
| 5152 | /* Do the conversion */ |
| 5153 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5154 | char *s = PyBytes_AS_STRING(*repr) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5155 | if (0 == WideCharToMultiByte(CP_ACP, flags, p, size, s, mbcssize, |
| 5156 | NULL, pusedDefaultChar)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5157 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 5158 | return -1; |
| 5159 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5160 | if (pusedDefaultChar && *pusedDefaultChar) |
| 5161 | goto mbcs_encode_error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5162 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5163 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5164 | |
| 5165 | mbcs_encode_error: |
| 5166 | raise_encode_exception(&exc, "mbcs", p, size, 0, 0, "invalid character"); |
| 5167 | Py_XDECREF(exc); |
| 5168 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5169 | } |
| 5170 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5171 | PyObject * |
| 5172 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 5173 | Py_ssize_t size, |
| 5174 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5175 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5176 | PyObject *repr = NULL; |
| 5177 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 5178 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5179 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5180 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5181 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5182 | ret = encode_mbcs(&repr, p, INT_MAX, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5183 | else |
| 5184 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 5185 | ret = encode_mbcs(&repr, p, (int)size, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5186 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5187 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5188 | Py_XDECREF(repr); |
| 5189 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5190 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5191 | |
| 5192 | #ifdef NEED_RETRY |
| 5193 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5194 | p += INT_MAX; |
| 5195 | size -= INT_MAX; |
| 5196 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5197 | } |
| 5198 | #endif |
| 5199 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5200 | return repr; |
| 5201 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 5202 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5203 | PyObject * |
| 5204 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 5205 | { |
| 5206 | if (!PyUnicode_Check(unicode)) { |
| 5207 | PyErr_BadArgument(); |
| 5208 | return NULL; |
| 5209 | } |
| 5210 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5211 | PyUnicode_GET_SIZE(unicode), |
| 5212 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 5213 | } |
| 5214 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5215 | #undef NEED_RETRY |
| 5216 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5217 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 5218 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5219 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 5220 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5221 | PyObject * |
| 5222 | PyUnicode_DecodeCharmap(const char *s, |
| 5223 | Py_ssize_t size, |
| 5224 | PyObject *mapping, |
| 5225 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5226 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5227 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5228 | Py_ssize_t startinpos; |
| 5229 | Py_ssize_t endinpos; |
| 5230 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5231 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5232 | PyUnicodeObject *v; |
| 5233 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5234 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5235 | PyObject *errorHandler = NULL; |
| 5236 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 5237 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5238 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5239 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5240 | /* Default to Latin-1 */ |
| 5241 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5242 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5243 | |
| 5244 | v = _PyUnicode_New(size); |
| 5245 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5246 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5247 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5248 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5249 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5250 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 5251 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5252 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 5253 | maplen = PyUnicode_GET_SIZE(mapping); |
| 5254 | while (s < e) { |
| 5255 | unsigned char ch = *s; |
| 5256 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5257 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5258 | if (ch < maplen) |
| 5259 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5260 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5261 | if (x == 0xfffe) { |
| 5262 | /* undefined mapping */ |
| 5263 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 5264 | startinpos = s-starts; |
| 5265 | endinpos = startinpos+1; |
| 5266 | if (unicode_decode_call_errorhandler( |
| 5267 | errors, &errorHandler, |
| 5268 | "charmap", "character maps to <undefined>", |
| 5269 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 5270 | &v, &outpos, &p)) { |
| 5271 | goto onError; |
| 5272 | } |
| 5273 | continue; |
| 5274 | } |
| 5275 | *p++ = x; |
| 5276 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5277 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 5278 | } |
| 5279 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5280 | while (s < e) { |
| 5281 | unsigned char ch = *s; |
| 5282 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 5283 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5284 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 5285 | w = PyLong_FromLong((long)ch); |
| 5286 | if (w == NULL) |
| 5287 | goto onError; |
| 5288 | x = PyObject_GetItem(mapping, w); |
| 5289 | Py_DECREF(w); |
| 5290 | if (x == NULL) { |
| 5291 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5292 | /* No mapping found means: mapping is undefined. */ |
| 5293 | PyErr_Clear(); |
| 5294 | x = Py_None; |
| 5295 | Py_INCREF(x); |
| 5296 | } else |
| 5297 | goto onError; |
| 5298 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5299 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5300 | /* Apply mapping */ |
| 5301 | if (PyLong_Check(x)) { |
| 5302 | long value = PyLong_AS_LONG(x); |
| 5303 | if (value < 0 || value > 65535) { |
| 5304 | PyErr_SetString(PyExc_TypeError, |
| 5305 | "character mapping must be in range(65536)"); |
| 5306 | Py_DECREF(x); |
| 5307 | goto onError; |
| 5308 | } |
| 5309 | *p++ = (Py_UNICODE)value; |
| 5310 | } |
| 5311 | else if (x == Py_None) { |
| 5312 | /* undefined mapping */ |
| 5313 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 5314 | startinpos = s-starts; |
| 5315 | endinpos = startinpos+1; |
| 5316 | if (unicode_decode_call_errorhandler( |
| 5317 | errors, &errorHandler, |
| 5318 | "charmap", "character maps to <undefined>", |
| 5319 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 5320 | &v, &outpos, &p)) { |
| 5321 | Py_DECREF(x); |
| 5322 | goto onError; |
| 5323 | } |
| 5324 | Py_DECREF(x); |
| 5325 | continue; |
| 5326 | } |
| 5327 | else if (PyUnicode_Check(x)) { |
| 5328 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5329 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5330 | if (targetsize == 1) |
| 5331 | /* 1-1 mapping */ |
| 5332 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5333 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5334 | else if (targetsize > 1) { |
| 5335 | /* 1-n mapping */ |
| 5336 | if (targetsize > extrachars) { |
| 5337 | /* resize first */ |
| 5338 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 5339 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 5340 | (targetsize << 2); |
| 5341 | extrachars += needed; |
| 5342 | /* XXX overflow detection missing */ |
| 5343 | if (_PyUnicode_Resize(&v, |
| 5344 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 5345 | Py_DECREF(x); |
| 5346 | goto onError; |
| 5347 | } |
| 5348 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 5349 | } |
| 5350 | Py_UNICODE_COPY(p, |
| 5351 | PyUnicode_AS_UNICODE(x), |
| 5352 | targetsize); |
| 5353 | p += targetsize; |
| 5354 | extrachars -= targetsize; |
| 5355 | } |
| 5356 | /* 1-0 mapping: skip the character */ |
| 5357 | } |
| 5358 | else { |
| 5359 | /* wrong return value */ |
| 5360 | PyErr_SetString(PyExc_TypeError, |
| 5361 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5362 | Py_DECREF(x); |
| 5363 | goto onError; |
| 5364 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5365 | Py_DECREF(x); |
| 5366 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5367 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5368 | } |
| 5369 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5370 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 5371 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5372 | Py_XDECREF(errorHandler); |
| 5373 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5374 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5375 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5376 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5377 | Py_XDECREF(errorHandler); |
| 5378 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5379 | Py_XDECREF(v); |
| 5380 | return NULL; |
| 5381 | } |
| 5382 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5383 | /* Charmap encoding: the lookup table */ |
| 5384 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5385 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5386 | PyObject_HEAD |
| 5387 | unsigned char level1[32]; |
| 5388 | int count2, count3; |
| 5389 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5390 | }; |
| 5391 | |
| 5392 | static PyObject* |
| 5393 | encoding_map_size(PyObject *obj, PyObject* args) |
| 5394 | { |
| 5395 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5396 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5397 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5398 | } |
| 5399 | |
| 5400 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5401 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5402 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 5403 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5404 | }; |
| 5405 | |
| 5406 | static void |
| 5407 | encoding_map_dealloc(PyObject* o) |
| 5408 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5409 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5410 | } |
| 5411 | |
| 5412 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5413 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5414 | "EncodingMap", /*tp_name*/ |
| 5415 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 5416 | 0, /*tp_itemsize*/ |
| 5417 | /* methods */ |
| 5418 | encoding_map_dealloc, /*tp_dealloc*/ |
| 5419 | 0, /*tp_print*/ |
| 5420 | 0, /*tp_getattr*/ |
| 5421 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 5422 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5423 | 0, /*tp_repr*/ |
| 5424 | 0, /*tp_as_number*/ |
| 5425 | 0, /*tp_as_sequence*/ |
| 5426 | 0, /*tp_as_mapping*/ |
| 5427 | 0, /*tp_hash*/ |
| 5428 | 0, /*tp_call*/ |
| 5429 | 0, /*tp_str*/ |
| 5430 | 0, /*tp_getattro*/ |
| 5431 | 0, /*tp_setattro*/ |
| 5432 | 0, /*tp_as_buffer*/ |
| 5433 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 5434 | 0, /*tp_doc*/ |
| 5435 | 0, /*tp_traverse*/ |
| 5436 | 0, /*tp_clear*/ |
| 5437 | 0, /*tp_richcompare*/ |
| 5438 | 0, /*tp_weaklistoffset*/ |
| 5439 | 0, /*tp_iter*/ |
| 5440 | 0, /*tp_iternext*/ |
| 5441 | encoding_map_methods, /*tp_methods*/ |
| 5442 | 0, /*tp_members*/ |
| 5443 | 0, /*tp_getset*/ |
| 5444 | 0, /*tp_base*/ |
| 5445 | 0, /*tp_dict*/ |
| 5446 | 0, /*tp_descr_get*/ |
| 5447 | 0, /*tp_descr_set*/ |
| 5448 | 0, /*tp_dictoffset*/ |
| 5449 | 0, /*tp_init*/ |
| 5450 | 0, /*tp_alloc*/ |
| 5451 | 0, /*tp_new*/ |
| 5452 | 0, /*tp_free*/ |
| 5453 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5454 | }; |
| 5455 | |
| 5456 | PyObject* |
| 5457 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 5458 | { |
| 5459 | Py_UNICODE *decode; |
| 5460 | PyObject *result; |
| 5461 | struct encoding_map *mresult; |
| 5462 | int i; |
| 5463 | int need_dict = 0; |
| 5464 | unsigned char level1[32]; |
| 5465 | unsigned char level2[512]; |
| 5466 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 5467 | int count2 = 0, count3 = 0; |
| 5468 | |
| 5469 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 5470 | PyErr_BadArgument(); |
| 5471 | return NULL; |
| 5472 | } |
| 5473 | decode = PyUnicode_AS_UNICODE(string); |
| 5474 | memset(level1, 0xFF, sizeof level1); |
| 5475 | memset(level2, 0xFF, sizeof level2); |
| 5476 | |
| 5477 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 5478 | or if there are non-BMP characters, we need to use |
| 5479 | a mapping dictionary. */ |
| 5480 | if (decode[0] != 0) |
| 5481 | need_dict = 1; |
| 5482 | for (i = 1; i < 256; i++) { |
| 5483 | int l1, l2; |
| 5484 | if (decode[i] == 0 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5485 | #ifdef Py_UNICODE_WIDE |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5486 | || decode[i] > 0xFFFF |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5487 | #endif |
| 5488 | ) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5489 | need_dict = 1; |
| 5490 | break; |
| 5491 | } |
| 5492 | if (decode[i] == 0xFFFE) |
| 5493 | /* unmapped character */ |
| 5494 | continue; |
| 5495 | l1 = decode[i] >> 11; |
| 5496 | l2 = decode[i] >> 7; |
| 5497 | if (level1[l1] == 0xFF) |
| 5498 | level1[l1] = count2++; |
| 5499 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5500 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5501 | } |
| 5502 | |
| 5503 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 5504 | need_dict = 1; |
| 5505 | |
| 5506 | if (need_dict) { |
| 5507 | PyObject *result = PyDict_New(); |
| 5508 | PyObject *key, *value; |
| 5509 | if (!result) |
| 5510 | return NULL; |
| 5511 | for (i = 0; i < 256; i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5512 | key = PyLong_FromLong(decode[i]); |
| 5513 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5514 | if (!key || !value) |
| 5515 | goto failed1; |
| 5516 | if (PyDict_SetItem(result, key, value) == -1) |
| 5517 | goto failed1; |
| 5518 | Py_DECREF(key); |
| 5519 | Py_DECREF(value); |
| 5520 | } |
| 5521 | return result; |
| 5522 | failed1: |
| 5523 | Py_XDECREF(key); |
| 5524 | Py_XDECREF(value); |
| 5525 | Py_DECREF(result); |
| 5526 | return NULL; |
| 5527 | } |
| 5528 | |
| 5529 | /* Create a three-level trie */ |
| 5530 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 5531 | 16*count2 + 128*count3 - 1); |
| 5532 | if (!result) |
| 5533 | return PyErr_NoMemory(); |
| 5534 | PyObject_Init(result, &EncodingMapType); |
| 5535 | mresult = (struct encoding_map*)result; |
| 5536 | mresult->count2 = count2; |
| 5537 | mresult->count3 = count3; |
| 5538 | mlevel1 = mresult->level1; |
| 5539 | mlevel2 = mresult->level23; |
| 5540 | mlevel3 = mresult->level23 + 16*count2; |
| 5541 | memcpy(mlevel1, level1, 32); |
| 5542 | memset(mlevel2, 0xFF, 16*count2); |
| 5543 | memset(mlevel3, 0, 128*count3); |
| 5544 | count3 = 0; |
| 5545 | for (i = 1; i < 256; i++) { |
| 5546 | int o1, o2, o3, i2, i3; |
| 5547 | if (decode[i] == 0xFFFE) |
| 5548 | /* unmapped character */ |
| 5549 | continue; |
| 5550 | o1 = decode[i]>>11; |
| 5551 | o2 = (decode[i]>>7) & 0xF; |
| 5552 | i2 = 16*mlevel1[o1] + o2; |
| 5553 | if (mlevel2[i2] == 0xFF) |
| 5554 | mlevel2[i2] = count3++; |
| 5555 | o3 = decode[i] & 0x7F; |
| 5556 | i3 = 128*mlevel2[i2] + o3; |
| 5557 | mlevel3[i3] = i; |
| 5558 | } |
| 5559 | return result; |
| 5560 | } |
| 5561 | |
| 5562 | static int |
| 5563 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 5564 | { |
| 5565 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 5566 | int l1 = c>>11; |
| 5567 | int l2 = (c>>7) & 0xF; |
| 5568 | int l3 = c & 0x7F; |
| 5569 | int i; |
| 5570 | |
| 5571 | #ifdef Py_UNICODE_WIDE |
| 5572 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5573 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5574 | } |
| 5575 | #endif |
| 5576 | if (c == 0) |
| 5577 | return 0; |
| 5578 | /* level 1*/ |
| 5579 | i = map->level1[l1]; |
| 5580 | if (i == 0xFF) { |
| 5581 | return -1; |
| 5582 | } |
| 5583 | /* level 2*/ |
| 5584 | i = map->level23[16*i+l2]; |
| 5585 | if (i == 0xFF) { |
| 5586 | return -1; |
| 5587 | } |
| 5588 | /* level 3 */ |
| 5589 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 5590 | if (i == 0) { |
| 5591 | return -1; |
| 5592 | } |
| 5593 | return i; |
| 5594 | } |
| 5595 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5596 | /* Lookup the character ch in the mapping. If the character |
| 5597 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 5598 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5599 | static PyObject * |
| 5600 | charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5601 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5602 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5603 | PyObject *x; |
| 5604 | |
| 5605 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5606 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5607 | x = PyObject_GetItem(mapping, w); |
| 5608 | Py_DECREF(w); |
| 5609 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5610 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5611 | /* No mapping found means: mapping is undefined. */ |
| 5612 | PyErr_Clear(); |
| 5613 | x = Py_None; |
| 5614 | Py_INCREF(x); |
| 5615 | return x; |
| 5616 | } else |
| 5617 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5618 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 5619 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5620 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5621 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5622 | long value = PyLong_AS_LONG(x); |
| 5623 | if (value < 0 || value > 255) { |
| 5624 | PyErr_SetString(PyExc_TypeError, |
| 5625 | "character mapping must be in range(256)"); |
| 5626 | Py_DECREF(x); |
| 5627 | return NULL; |
| 5628 | } |
| 5629 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5630 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5631 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5632 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5633 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5634 | /* wrong return value */ |
| 5635 | PyErr_Format(PyExc_TypeError, |
| 5636 | "character mapping must return integer, bytes or None, not %.400s", |
| 5637 | x->ob_type->tp_name); |
| 5638 | Py_DECREF(x); |
| 5639 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5640 | } |
| 5641 | } |
| 5642 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5643 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5644 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5645 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5646 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 5647 | /* exponentially overallocate to minimize reallocations */ |
| 5648 | if (requiredsize < 2*outsize) |
| 5649 | requiredsize = 2*outsize; |
| 5650 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 5651 | return -1; |
| 5652 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5653 | } |
| 5654 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5655 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5656 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5657 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5658 | /* 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] | 5659 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5660 | space is available. Return a new reference to the object that |
| 5661 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 5662 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 5663 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5664 | static charmapencode_result |
| 5665 | charmapencode_output(Py_UNICODE c, PyObject *mapping, |
| 5666 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5667 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5668 | PyObject *rep; |
| 5669 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5670 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5671 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5672 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5673 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5674 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5675 | if (res == -1) |
| 5676 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5677 | if (outsize<requiredsize) |
| 5678 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 5679 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5680 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5681 | outstart[(*outpos)++] = (char)res; |
| 5682 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5683 | } |
| 5684 | |
| 5685 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5686 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5687 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5688 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5689 | Py_DECREF(rep); |
| 5690 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5691 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5692 | if (PyLong_Check(rep)) { |
| 5693 | Py_ssize_t requiredsize = *outpos+1; |
| 5694 | if (outsize<requiredsize) |
| 5695 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5696 | Py_DECREF(rep); |
| 5697 | return enc_EXCEPTION; |
| 5698 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5699 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5700 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5701 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5702 | else { |
| 5703 | const char *repchars = PyBytes_AS_STRING(rep); |
| 5704 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 5705 | Py_ssize_t requiredsize = *outpos+repsize; |
| 5706 | if (outsize<requiredsize) |
| 5707 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5708 | Py_DECREF(rep); |
| 5709 | return enc_EXCEPTION; |
| 5710 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5711 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5712 | memcpy(outstart + *outpos, repchars, repsize); |
| 5713 | *outpos += repsize; |
| 5714 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5715 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5716 | Py_DECREF(rep); |
| 5717 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5718 | } |
| 5719 | |
| 5720 | /* handle an error in PyUnicode_EncodeCharmap |
| 5721 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5722 | static int |
| 5723 | charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5724 | 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] | 5725 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 5726 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5727 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5728 | { |
| 5729 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5730 | Py_ssize_t repsize; |
| 5731 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5732 | Py_UNICODE *uni2; |
| 5733 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5734 | Py_ssize_t collstartpos = *inpos; |
| 5735 | Py_ssize_t collendpos = *inpos+1; |
| 5736 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5737 | char *encoding = "charmap"; |
| 5738 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5739 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5740 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5741 | /* find all unencodable characters */ |
| 5742 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5743 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5744 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5745 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 5746 | if (res != -1) |
| 5747 | break; |
| 5748 | ++collendpos; |
| 5749 | continue; |
| 5750 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5751 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5752 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 5753 | if (rep==NULL) |
| 5754 | return -1; |
| 5755 | else if (rep!=Py_None) { |
| 5756 | Py_DECREF(rep); |
| 5757 | break; |
| 5758 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5759 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5760 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5761 | } |
| 5762 | /* cache callback name lookup |
| 5763 | * (if not done yet, i.e. it's the first error) */ |
| 5764 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5765 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5766 | *known_errorHandler = 1; |
| 5767 | else if (!strcmp(errors, "replace")) |
| 5768 | *known_errorHandler = 2; |
| 5769 | else if (!strcmp(errors, "ignore")) |
| 5770 | *known_errorHandler = 3; |
| 5771 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5772 | *known_errorHandler = 4; |
| 5773 | else |
| 5774 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5775 | } |
| 5776 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5777 | case 1: /* strict */ |
| 5778 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5779 | return -1; |
| 5780 | case 2: /* replace */ |
| 5781 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5782 | x = charmapencode_output('?', mapping, res, respos); |
| 5783 | if (x==enc_EXCEPTION) { |
| 5784 | return -1; |
| 5785 | } |
| 5786 | else if (x==enc_FAILED) { |
| 5787 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5788 | return -1; |
| 5789 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5790 | } |
| 5791 | /* fall through */ |
| 5792 | case 3: /* ignore */ |
| 5793 | *inpos = collendpos; |
| 5794 | break; |
| 5795 | case 4: /* xmlcharrefreplace */ |
| 5796 | /* generate replacement (temporarily (mis)uses p) */ |
| 5797 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5798 | char buffer[2+29+1+1]; |
| 5799 | char *cp; |
| 5800 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 5801 | for (cp = buffer; *cp; ++cp) { |
| 5802 | x = charmapencode_output(*cp, mapping, res, respos); |
| 5803 | if (x==enc_EXCEPTION) |
| 5804 | return -1; |
| 5805 | else if (x==enc_FAILED) { |
| 5806 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5807 | return -1; |
| 5808 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5809 | } |
| 5810 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5811 | *inpos = collendpos; |
| 5812 | break; |
| 5813 | default: |
| 5814 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5815 | encoding, reason, p, size, exceptionObject, |
| 5816 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5817 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5818 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5819 | if (PyBytes_Check(repunicode)) { |
| 5820 | /* Directly copy bytes result to output. */ |
| 5821 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 5822 | Py_ssize_t requiredsize; |
| 5823 | repsize = PyBytes_Size(repunicode); |
| 5824 | requiredsize = *respos + repsize; |
| 5825 | if (requiredsize > outsize) |
| 5826 | /* Make room for all additional bytes. */ |
| 5827 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 5828 | Py_DECREF(repunicode); |
| 5829 | return -1; |
| 5830 | } |
| 5831 | memcpy(PyBytes_AsString(*res) + *respos, |
| 5832 | PyBytes_AsString(repunicode), repsize); |
| 5833 | *respos += repsize; |
| 5834 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5835 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5836 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5837 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5838 | /* generate replacement */ |
| 5839 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5840 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5841 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 5842 | if (x==enc_EXCEPTION) { |
| 5843 | return -1; |
| 5844 | } |
| 5845 | else if (x==enc_FAILED) { |
| 5846 | Py_DECREF(repunicode); |
| 5847 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5848 | return -1; |
| 5849 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5850 | } |
| 5851 | *inpos = newpos; |
| 5852 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5853 | } |
| 5854 | return 0; |
| 5855 | } |
| 5856 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5857 | PyObject * |
| 5858 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 5859 | Py_ssize_t size, |
| 5860 | PyObject *mapping, |
| 5861 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5862 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5863 | /* output object */ |
| 5864 | PyObject *res = NULL; |
| 5865 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5866 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5867 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5868 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5869 | PyObject *errorHandler = NULL; |
| 5870 | PyObject *exc = NULL; |
| 5871 | /* the following variable is used for caching string comparisons |
| 5872 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5873 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5874 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5875 | |
| 5876 | /* Default to Latin-1 */ |
| 5877 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5878 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5879 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5880 | /* allocate enough for a simple encoding without |
| 5881 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5882 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5883 | if (res == NULL) |
| 5884 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 5885 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5886 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5887 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5888 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5889 | /* try to encode it */ |
| 5890 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 5891 | if (x==enc_EXCEPTION) /* error */ |
| 5892 | goto onError; |
| 5893 | if (x==enc_FAILED) { /* unencodable character */ |
| 5894 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 5895 | &exc, |
| 5896 | &known_errorHandler, &errorHandler, errors, |
| 5897 | &res, &respos)) { |
| 5898 | goto onError; |
| 5899 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5900 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5901 | else |
| 5902 | /* done with this character => adjust input position */ |
| 5903 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5904 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5905 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5906 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5907 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5908 | if (_PyBytes_Resize(&res, respos) < 0) |
| 5909 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5910 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5911 | Py_XDECREF(exc); |
| 5912 | Py_XDECREF(errorHandler); |
| 5913 | return res; |
| 5914 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5915 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5916 | Py_XDECREF(res); |
| 5917 | Py_XDECREF(exc); |
| 5918 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5919 | return NULL; |
| 5920 | } |
| 5921 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5922 | PyObject * |
| 5923 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 5924 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5925 | { |
| 5926 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5927 | PyErr_BadArgument(); |
| 5928 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5929 | } |
| 5930 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5931 | PyUnicode_GET_SIZE(unicode), |
| 5932 | mapping, |
| 5933 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5934 | } |
| 5935 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5936 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5937 | static void |
| 5938 | make_translate_exception(PyObject **exceptionObject, |
| 5939 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5940 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5941 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5942 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5943 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5944 | *exceptionObject = PyUnicodeTranslateError_Create( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5945 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5946 | } |
| 5947 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5948 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 5949 | goto onError; |
| 5950 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 5951 | goto onError; |
| 5952 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 5953 | goto onError; |
| 5954 | return; |
| 5955 | onError: |
| 5956 | Py_DECREF(*exceptionObject); |
| 5957 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5958 | } |
| 5959 | } |
| 5960 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5961 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5962 | static void |
| 5963 | raise_translate_exception(PyObject **exceptionObject, |
| 5964 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5965 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5966 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5967 | { |
| 5968 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5969 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5970 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5971 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5972 | } |
| 5973 | |
| 5974 | /* error handling callback helper: |
| 5975 | build arguments, call the callback and check the arguments, |
| 5976 | put the result into newpos and return the replacement string, which |
| 5977 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5978 | static PyObject * |
| 5979 | unicode_translate_call_errorhandler(const char *errors, |
| 5980 | PyObject **errorHandler, |
| 5981 | const char *reason, |
| 5982 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 5983 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5984 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5985 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 5986 | 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] | 5987 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5988 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5989 | PyObject *restuple; |
| 5990 | PyObject *resunicode; |
| 5991 | |
| 5992 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5993 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5994 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5995 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5996 | } |
| 5997 | |
| 5998 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5999 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6000 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6001 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6002 | |
| 6003 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6004 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6005 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6006 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6007 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 6008 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6009 | Py_DECREF(restuple); |
| 6010 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6011 | } |
| 6012 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6013 | &resunicode, &i_newpos)) { |
| 6014 | Py_DECREF(restuple); |
| 6015 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6016 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6017 | if (i_newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6018 | *newpos = size+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6019 | else |
| 6020 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6021 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6022 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6023 | Py_DECREF(restuple); |
| 6024 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6025 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6026 | Py_INCREF(resunicode); |
| 6027 | Py_DECREF(restuple); |
| 6028 | return resunicode; |
| 6029 | } |
| 6030 | |
| 6031 | /* Lookup the character ch in the mapping and put the result in result, |
| 6032 | which must be decrefed by the caller. |
| 6033 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6034 | static int |
| 6035 | charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6036 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6037 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6038 | PyObject *x; |
| 6039 | |
| 6040 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6041 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6042 | x = PyObject_GetItem(mapping, w); |
| 6043 | Py_DECREF(w); |
| 6044 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6045 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 6046 | /* No mapping found means: use 1:1 mapping. */ |
| 6047 | PyErr_Clear(); |
| 6048 | *result = NULL; |
| 6049 | return 0; |
| 6050 | } else |
| 6051 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6052 | } |
| 6053 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6054 | *result = x; |
| 6055 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6056 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6057 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6058 | long value = PyLong_AS_LONG(x); |
| 6059 | long max = PyUnicode_GetMax(); |
| 6060 | if (value < 0 || value > max) { |
| 6061 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 6062 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6063 | Py_DECREF(x); |
| 6064 | return -1; |
| 6065 | } |
| 6066 | *result = x; |
| 6067 | return 0; |
| 6068 | } |
| 6069 | else if (PyUnicode_Check(x)) { |
| 6070 | *result = x; |
| 6071 | return 0; |
| 6072 | } |
| 6073 | else { |
| 6074 | /* wrong return value */ |
| 6075 | PyErr_SetString(PyExc_TypeError, |
| 6076 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6077 | Py_DECREF(x); |
| 6078 | return -1; |
| 6079 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6080 | } |
| 6081 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6082 | if not reallocate and adjust various state variables. |
| 6083 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6084 | static int |
| 6085 | charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6086 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6087 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6088 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 6089 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6090 | /* remember old output position */ |
| 6091 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
| 6092 | /* exponentially overallocate to minimize reallocations */ |
| 6093 | if (requiredsize < 2 * oldsize) |
| 6094 | requiredsize = 2 * oldsize; |
| 6095 | if (PyUnicode_Resize(outobj, requiredsize) < 0) |
| 6096 | return -1; |
| 6097 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6098 | } |
| 6099 | return 0; |
| 6100 | } |
| 6101 | /* lookup the character, put the result in the output string and adjust |
| 6102 | various state variables. Return a new reference to the object that |
| 6103 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 6104 | undefined (in which case no character was written). |
| 6105 | The called must decref result. |
| 6106 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6107 | static int |
| 6108 | charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
| 6109 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
| 6110 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6111 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 6112 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6113 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6114 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6115 | /* not found => default to 1:1 mapping */ |
| 6116 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6117 | } |
| 6118 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6119 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6120 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6121 | /* no overflow check, because we know that the space is enough */ |
| 6122 | *(*outp)++ = (Py_UNICODE)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6123 | } |
| 6124 | else if (PyUnicode_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6125 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
| 6126 | if (repsize==1) { |
| 6127 | /* no overflow check, because we know that the space is enough */ |
| 6128 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 6129 | } |
| 6130 | else if (repsize!=0) { |
| 6131 | /* more than one character */ |
| 6132 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
| 6133 | (insize - (curinp-startinp)) + |
| 6134 | repsize - 1; |
| 6135 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
| 6136 | return -1; |
| 6137 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 6138 | *outp += repsize; |
| 6139 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6140 | } |
| 6141 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6142 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6143 | return 0; |
| 6144 | } |
| 6145 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6146 | PyObject * |
| 6147 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 6148 | Py_ssize_t size, |
| 6149 | PyObject *mapping, |
| 6150 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6151 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6152 | /* output object */ |
| 6153 | PyObject *res = NULL; |
| 6154 | /* pointers to the beginning and end+1 of input */ |
| 6155 | const Py_UNICODE *startp = p; |
| 6156 | const Py_UNICODE *endp = p + size; |
| 6157 | /* pointer into the output */ |
| 6158 | Py_UNICODE *str; |
| 6159 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6160 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6161 | char *reason = "character maps to <undefined>"; |
| 6162 | PyObject *errorHandler = NULL; |
| 6163 | PyObject *exc = NULL; |
| 6164 | /* the following variable is used for caching string comparisons |
| 6165 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 6166 | * 3=ignore, 4=xmlcharrefreplace */ |
| 6167 | int known_errorHandler = -1; |
| 6168 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6169 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6170 | PyErr_BadArgument(); |
| 6171 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6172 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6173 | |
| 6174 | /* allocate enough for a simple 1:1 translation without |
| 6175 | replacements, if we need more, we'll resize */ |
| 6176 | res = PyUnicode_FromUnicode(NULL, size); |
| 6177 | if (res == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6178 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6179 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6180 | return res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6181 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6182 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6183 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6184 | /* try to encode it */ |
| 6185 | PyObject *x = NULL; |
| 6186 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
| 6187 | Py_XDECREF(x); |
| 6188 | goto onError; |
| 6189 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6190 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6191 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 6192 | ++p; |
| 6193 | else { /* untranslatable character */ |
| 6194 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 6195 | Py_ssize_t repsize; |
| 6196 | Py_ssize_t newpos; |
| 6197 | Py_UNICODE *uni2; |
| 6198 | /* startpos for collecting untranslatable chars */ |
| 6199 | const Py_UNICODE *collstart = p; |
| 6200 | const Py_UNICODE *collend = p+1; |
| 6201 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6202 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6203 | /* find all untranslatable characters */ |
| 6204 | while (collend < endp) { |
| 6205 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
| 6206 | goto onError; |
| 6207 | Py_XDECREF(x); |
| 6208 | if (x!=Py_None) |
| 6209 | break; |
| 6210 | ++collend; |
| 6211 | } |
| 6212 | /* cache callback name lookup |
| 6213 | * (if not done yet, i.e. it's the first error) */ |
| 6214 | if (known_errorHandler==-1) { |
| 6215 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6216 | known_errorHandler = 1; |
| 6217 | else if (!strcmp(errors, "replace")) |
| 6218 | known_errorHandler = 2; |
| 6219 | else if (!strcmp(errors, "ignore")) |
| 6220 | known_errorHandler = 3; |
| 6221 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6222 | known_errorHandler = 4; |
| 6223 | else |
| 6224 | known_errorHandler = 0; |
| 6225 | } |
| 6226 | switch (known_errorHandler) { |
| 6227 | case 1: /* strict */ |
| 6228 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6229 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6230 | case 2: /* replace */ |
| 6231 | /* No need to check for space, this is a 1:1 replacement */ |
| 6232 | for (coll = collstart; coll<collend; ++coll) |
| 6233 | *str++ = '?'; |
| 6234 | /* fall through */ |
| 6235 | case 3: /* ignore */ |
| 6236 | p = collend; |
| 6237 | break; |
| 6238 | case 4: /* xmlcharrefreplace */ |
| 6239 | /* generate replacement (temporarily (mis)uses p) */ |
| 6240 | for (p = collstart; p < collend; ++p) { |
| 6241 | char buffer[2+29+1+1]; |
| 6242 | char *cp; |
| 6243 | sprintf(buffer, "&#%d;", (int)*p); |
| 6244 | if (charmaptranslate_makespace(&res, &str, |
| 6245 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 6246 | goto onError; |
| 6247 | for (cp = buffer; *cp; ++cp) |
| 6248 | *str++ = *cp; |
| 6249 | } |
| 6250 | p = collend; |
| 6251 | break; |
| 6252 | default: |
| 6253 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 6254 | reason, startp, size, &exc, |
| 6255 | collstart-startp, collend-startp, &newpos); |
| 6256 | if (repunicode == NULL) |
| 6257 | goto onError; |
| 6258 | /* generate replacement */ |
| 6259 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 6260 | if (charmaptranslate_makespace(&res, &str, |
| 6261 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 6262 | Py_DECREF(repunicode); |
| 6263 | goto onError; |
| 6264 | } |
| 6265 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 6266 | *str++ = *uni2; |
| 6267 | p = startp + newpos; |
| 6268 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6269 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6270 | } |
| 6271 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6272 | /* Resize if we allocated to much */ |
| 6273 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 6274 | if (respos<PyUnicode_GET_SIZE(res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6275 | if (PyUnicode_Resize(&res, respos) < 0) |
| 6276 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6277 | } |
| 6278 | Py_XDECREF(exc); |
| 6279 | Py_XDECREF(errorHandler); |
| 6280 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6281 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6282 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6283 | Py_XDECREF(res); |
| 6284 | Py_XDECREF(exc); |
| 6285 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6286 | return NULL; |
| 6287 | } |
| 6288 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6289 | PyObject * |
| 6290 | PyUnicode_Translate(PyObject *str, |
| 6291 | PyObject *mapping, |
| 6292 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6293 | { |
| 6294 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6295 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6296 | str = PyUnicode_FromObject(str); |
| 6297 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6298 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6299 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6300 | PyUnicode_GET_SIZE(str), |
| 6301 | mapping, |
| 6302 | errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6303 | Py_DECREF(str); |
| 6304 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6305 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6306 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6307 | Py_XDECREF(str); |
| 6308 | return NULL; |
| 6309 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6310 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 6311 | PyObject * |
| 6312 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 6313 | Py_ssize_t length) |
| 6314 | { |
| 6315 | PyObject *result; |
| 6316 | Py_UNICODE *p; /* write pointer into result */ |
| 6317 | Py_ssize_t i; |
| 6318 | /* Copy to a new string */ |
| 6319 | result = (PyObject *)_PyUnicode_New(length); |
| 6320 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(result), s, length); |
| 6321 | if (result == NULL) |
| 6322 | return result; |
| 6323 | p = PyUnicode_AS_UNICODE(result); |
| 6324 | /* Iterate over code points */ |
| 6325 | for (i = 0; i < length; i++) { |
| 6326 | Py_UNICODE ch =s[i]; |
| 6327 | if (ch > 127) { |
| 6328 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 6329 | if (decimal >= 0) |
| 6330 | p[i] = '0' + decimal; |
| 6331 | } |
| 6332 | } |
| 6333 | return result; |
| 6334 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6335 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 6336 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6337 | int |
| 6338 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 6339 | Py_ssize_t length, |
| 6340 | char *output, |
| 6341 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6342 | { |
| 6343 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6344 | PyObject *errorHandler = NULL; |
| 6345 | PyObject *exc = NULL; |
| 6346 | const char *encoding = "decimal"; |
| 6347 | const char *reason = "invalid decimal Unicode string"; |
| 6348 | /* the following variable is used for caching string comparisons |
| 6349 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6350 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6351 | |
| 6352 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6353 | PyErr_BadArgument(); |
| 6354 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6355 | } |
| 6356 | |
| 6357 | p = s; |
| 6358 | end = s + length; |
| 6359 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6360 | register Py_UNICODE ch = *p; |
| 6361 | int decimal; |
| 6362 | PyObject *repunicode; |
| 6363 | Py_ssize_t repsize; |
| 6364 | Py_ssize_t newpos; |
| 6365 | Py_UNICODE *uni2; |
| 6366 | Py_UNICODE *collstart; |
| 6367 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6368 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6369 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6370 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6371 | ++p; |
| 6372 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6373 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6374 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 6375 | if (decimal >= 0) { |
| 6376 | *output++ = '0' + decimal; |
| 6377 | ++p; |
| 6378 | continue; |
| 6379 | } |
| 6380 | if (0 < ch && ch < 256) { |
| 6381 | *output++ = (char)ch; |
| 6382 | ++p; |
| 6383 | continue; |
| 6384 | } |
| 6385 | /* All other characters are considered unencodable */ |
| 6386 | collstart = p; |
| 6387 | collend = p+1; |
| 6388 | while (collend < end) { |
| 6389 | if ((0 < *collend && *collend < 256) || |
| 6390 | !Py_UNICODE_ISSPACE(*collend) || |
| 6391 | Py_UNICODE_TODECIMAL(*collend)) |
| 6392 | break; |
| 6393 | } |
| 6394 | /* cache callback name lookup |
| 6395 | * (if not done yet, i.e. it's the first error) */ |
| 6396 | if (known_errorHandler==-1) { |
| 6397 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6398 | known_errorHandler = 1; |
| 6399 | else if (!strcmp(errors, "replace")) |
| 6400 | known_errorHandler = 2; |
| 6401 | else if (!strcmp(errors, "ignore")) |
| 6402 | known_errorHandler = 3; |
| 6403 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6404 | known_errorHandler = 4; |
| 6405 | else |
| 6406 | known_errorHandler = 0; |
| 6407 | } |
| 6408 | switch (known_errorHandler) { |
| 6409 | case 1: /* strict */ |
| 6410 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 6411 | goto onError; |
| 6412 | case 2: /* replace */ |
| 6413 | for (p = collstart; p < collend; ++p) |
| 6414 | *output++ = '?'; |
| 6415 | /* fall through */ |
| 6416 | case 3: /* ignore */ |
| 6417 | p = collend; |
| 6418 | break; |
| 6419 | case 4: /* xmlcharrefreplace */ |
| 6420 | /* generate replacement (temporarily (mis)uses p) */ |
| 6421 | for (p = collstart; p < collend; ++p) |
| 6422 | output += sprintf(output, "&#%d;", (int)*p); |
| 6423 | p = collend; |
| 6424 | break; |
| 6425 | default: |
| 6426 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 6427 | encoding, reason, s, length, &exc, |
| 6428 | collstart-s, collend-s, &newpos); |
| 6429 | if (repunicode == NULL) |
| 6430 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6431 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6432 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6433 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 6434 | Py_DECREF(repunicode); |
| 6435 | goto onError; |
| 6436 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6437 | /* generate replacement */ |
| 6438 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 6439 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 6440 | Py_UNICODE ch = *uni2; |
| 6441 | if (Py_UNICODE_ISSPACE(ch)) |
| 6442 | *output++ = ' '; |
| 6443 | else { |
| 6444 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 6445 | if (decimal >= 0) |
| 6446 | *output++ = '0' + decimal; |
| 6447 | else if (0 < ch && ch < 256) |
| 6448 | *output++ = (char)ch; |
| 6449 | else { |
| 6450 | Py_DECREF(repunicode); |
| 6451 | raise_encode_exception(&exc, encoding, |
| 6452 | s, length, collstart-s, collend-s, reason); |
| 6453 | goto onError; |
| 6454 | } |
| 6455 | } |
| 6456 | } |
| 6457 | p = s + newpos; |
| 6458 | Py_DECREF(repunicode); |
| 6459 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6460 | } |
| 6461 | /* 0-terminate the output string */ |
| 6462 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6463 | Py_XDECREF(exc); |
| 6464 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6465 | return 0; |
| 6466 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6467 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6468 | Py_XDECREF(exc); |
| 6469 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6470 | return -1; |
| 6471 | } |
| 6472 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6473 | /* --- Helpers ------------------------------------------------------------ */ |
| 6474 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 6475 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6476 | #include "stringlib/fastsearch.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6477 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6478 | #include "stringlib/count.h" |
| 6479 | #include "stringlib/find.h" |
| 6480 | #include "stringlib/partition.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6481 | #include "stringlib/split.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6482 | |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 6483 | #define _Py_InsertThousandsGrouping _PyUnicode_InsertThousandsGrouping |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 6484 | #define _Py_InsertThousandsGroupingLocale _PyUnicode_InsertThousandsGroupingLocale |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 6485 | #include "stringlib/localeutil.h" |
| 6486 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6487 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6488 | #define ADJUST_INDICES(start, end, len) \ |
| 6489 | if (end > len) \ |
| 6490 | end = len; \ |
| 6491 | else if (end < 0) { \ |
| 6492 | end += len; \ |
| 6493 | if (end < 0) \ |
| 6494 | end = 0; \ |
| 6495 | } \ |
| 6496 | if (start < 0) { \ |
| 6497 | start += len; \ |
| 6498 | if (start < 0) \ |
| 6499 | start = 0; \ |
| 6500 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6501 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6502 | Py_ssize_t |
| 6503 | PyUnicode_Count(PyObject *str, |
| 6504 | PyObject *substr, |
| 6505 | Py_ssize_t start, |
| 6506 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6507 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6508 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6509 | PyUnicodeObject* str_obj; |
| 6510 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6511 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6512 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 6513 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6514 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6515 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 6516 | if (!sub_obj) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6517 | Py_DECREF(str_obj); |
| 6518 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6519 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6520 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6521 | ADJUST_INDICES(start, end, str_obj->length); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6522 | result = stringlib_count( |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6523 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length, |
| 6524 | PY_SSIZE_T_MAX |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6525 | ); |
| 6526 | |
| 6527 | Py_DECREF(sub_obj); |
| 6528 | Py_DECREF(str_obj); |
| 6529 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6530 | return result; |
| 6531 | } |
| 6532 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6533 | Py_ssize_t |
| 6534 | PyUnicode_Find(PyObject *str, |
| 6535 | PyObject *sub, |
| 6536 | Py_ssize_t start, |
| 6537 | Py_ssize_t end, |
| 6538 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6539 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6540 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6541 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6542 | str = PyUnicode_FromObject(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6543 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6544 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6545 | sub = PyUnicode_FromObject(sub); |
| 6546 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6547 | Py_DECREF(str); |
| 6548 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6549 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6550 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6551 | if (direction > 0) |
| 6552 | result = stringlib_find_slice( |
| 6553 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 6554 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 6555 | start, end |
| 6556 | ); |
| 6557 | else |
| 6558 | result = stringlib_rfind_slice( |
| 6559 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 6560 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 6561 | start, end |
| 6562 | ); |
| 6563 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6564 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6565 | Py_DECREF(sub); |
| 6566 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6567 | return result; |
| 6568 | } |
| 6569 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6570 | static int |
| 6571 | tailmatch(PyUnicodeObject *self, |
| 6572 | PyUnicodeObject *substring, |
| 6573 | Py_ssize_t start, |
| 6574 | Py_ssize_t end, |
| 6575 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6576 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6577 | if (substring->length == 0) |
| 6578 | return 1; |
| 6579 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6580 | ADJUST_INDICES(start, end, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6581 | end -= substring->length; |
| 6582 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6583 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6584 | |
| 6585 | if (direction > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6586 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 6587 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6588 | } else { |
| 6589 | if (Py_UNICODE_MATCH(self, start, substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6590 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6591 | } |
| 6592 | |
| 6593 | return 0; |
| 6594 | } |
| 6595 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6596 | Py_ssize_t |
| 6597 | PyUnicode_Tailmatch(PyObject *str, |
| 6598 | PyObject *substr, |
| 6599 | Py_ssize_t start, |
| 6600 | Py_ssize_t end, |
| 6601 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6602 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6603 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6604 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6605 | str = PyUnicode_FromObject(str); |
| 6606 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6607 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6608 | substr = PyUnicode_FromObject(substr); |
| 6609 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6610 | Py_DECREF(str); |
| 6611 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6612 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6613 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6614 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6615 | (PyUnicodeObject *)substr, |
| 6616 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6617 | Py_DECREF(str); |
| 6618 | Py_DECREF(substr); |
| 6619 | return result; |
| 6620 | } |
| 6621 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6622 | /* Apply fixfct filter to the Unicode object self and return a |
| 6623 | reference to the modified object */ |
| 6624 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6625 | static PyObject * |
| 6626 | fixup(PyUnicodeObject *self, |
| 6627 | int (*fixfct)(PyUnicodeObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6628 | { |
| 6629 | |
| 6630 | PyUnicodeObject *u; |
| 6631 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6632 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6633 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6634 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6635 | |
| 6636 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6637 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6638 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6639 | /* fixfct should return TRUE if it modified the buffer. If |
| 6640 | FALSE, return a reference to the original buffer instead |
| 6641 | (to save space, not time) */ |
| 6642 | Py_INCREF(self); |
| 6643 | Py_DECREF(u); |
| 6644 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6645 | } |
| 6646 | return (PyObject*) u; |
| 6647 | } |
| 6648 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6649 | static int |
| 6650 | fixupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6651 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6652 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6653 | Py_UNICODE *s = self->str; |
| 6654 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6655 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6656 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6657 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6658 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6659 | ch = Py_UNICODE_TOUPPER(*s); |
| 6660 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6661 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6662 | *s = ch; |
| 6663 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6664 | s++; |
| 6665 | } |
| 6666 | |
| 6667 | return status; |
| 6668 | } |
| 6669 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6670 | static int |
| 6671 | fixlower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6672 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6673 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6674 | Py_UNICODE *s = self->str; |
| 6675 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6676 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6677 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6678 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6679 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6680 | ch = Py_UNICODE_TOLOWER(*s); |
| 6681 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6682 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6683 | *s = ch; |
| 6684 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6685 | s++; |
| 6686 | } |
| 6687 | |
| 6688 | return status; |
| 6689 | } |
| 6690 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6691 | static int |
| 6692 | fixswapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6693 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6694 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6695 | Py_UNICODE *s = self->str; |
| 6696 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6697 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6698 | while (len-- > 0) { |
| 6699 | if (Py_UNICODE_ISUPPER(*s)) { |
| 6700 | *s = Py_UNICODE_TOLOWER(*s); |
| 6701 | status = 1; |
| 6702 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 6703 | *s = Py_UNICODE_TOUPPER(*s); |
| 6704 | status = 1; |
| 6705 | } |
| 6706 | s++; |
| 6707 | } |
| 6708 | |
| 6709 | return status; |
| 6710 | } |
| 6711 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6712 | static int |
| 6713 | fixcapitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6714 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6715 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6716 | Py_UNICODE *s = self->str; |
| 6717 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6718 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6719 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6720 | return 0; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6721 | if (Py_UNICODE_ISLOWER(*s)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6722 | *s = Py_UNICODE_TOUPPER(*s); |
| 6723 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6724 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6725 | s++; |
| 6726 | while (--len > 0) { |
| 6727 | if (Py_UNICODE_ISUPPER(*s)) { |
| 6728 | *s = Py_UNICODE_TOLOWER(*s); |
| 6729 | status = 1; |
| 6730 | } |
| 6731 | s++; |
| 6732 | } |
| 6733 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6734 | } |
| 6735 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6736 | static int |
| 6737 | fixtitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6738 | { |
| 6739 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6740 | register Py_UNICODE *e; |
| 6741 | int previous_is_cased; |
| 6742 | |
| 6743 | /* Shortcut for single character strings */ |
| 6744 | if (PyUnicode_GET_SIZE(self) == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6745 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 6746 | if (*p != ch) { |
| 6747 | *p = ch; |
| 6748 | return 1; |
| 6749 | } |
| 6750 | else |
| 6751 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6752 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6753 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6754 | e = p + PyUnicode_GET_SIZE(self); |
| 6755 | previous_is_cased = 0; |
| 6756 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6757 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6758 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6759 | if (previous_is_cased) |
| 6760 | *p = Py_UNICODE_TOLOWER(ch); |
| 6761 | else |
| 6762 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6763 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6764 | if (Py_UNICODE_ISLOWER(ch) || |
| 6765 | Py_UNICODE_ISUPPER(ch) || |
| 6766 | Py_UNICODE_ISTITLE(ch)) |
| 6767 | previous_is_cased = 1; |
| 6768 | else |
| 6769 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6770 | } |
| 6771 | return 1; |
| 6772 | } |
| 6773 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6774 | PyObject * |
| 6775 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6776 | { |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 6777 | const Py_UNICODE blank = ' '; |
| 6778 | const Py_UNICODE *sep = ␣ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6779 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6780 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6781 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 6782 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6783 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 6784 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6785 | PyObject *item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6786 | Py_ssize_t sz, i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6787 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6788 | fseq = PySequence_Fast(seq, ""); |
| 6789 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6790 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6791 | } |
| 6792 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6793 | /* NOTE: the following code can't call back into Python code, |
| 6794 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6795 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6796 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6797 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 6798 | /* If empty sequence, return u"". */ |
| 6799 | if (seqlen == 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6800 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 6801 | goto Done; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6802 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6803 | items = PySequence_Fast_ITEMS(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6804 | /* If singleton sequence with an exact Unicode, return that. */ |
| 6805 | if (seqlen == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6806 | item = items[0]; |
| 6807 | if (PyUnicode_CheckExact(item)) { |
| 6808 | Py_INCREF(item); |
| 6809 | res = (PyUnicodeObject *)item; |
| 6810 | goto Done; |
| 6811 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6812 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6813 | else { |
| 6814 | /* Set up sep and seplen */ |
| 6815 | if (separator == NULL) { |
| 6816 | sep = ␣ |
| 6817 | seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6818 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6819 | else { |
| 6820 | if (!PyUnicode_Check(separator)) { |
| 6821 | PyErr_Format(PyExc_TypeError, |
| 6822 | "separator: expected str instance," |
| 6823 | " %.80s found", |
| 6824 | Py_TYPE(separator)->tp_name); |
| 6825 | goto onError; |
| 6826 | } |
| 6827 | sep = PyUnicode_AS_UNICODE(separator); |
| 6828 | seplen = PyUnicode_GET_SIZE(separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6829 | } |
| 6830 | } |
| 6831 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6832 | /* There are at least two things to join, or else we have a subclass |
| 6833 | * of str in the sequence. |
| 6834 | * Do a pre-pass to figure out the total amount of space we'll |
| 6835 | * need (sz), and see whether all argument are strings. |
| 6836 | */ |
| 6837 | sz = 0; |
| 6838 | for (i = 0; i < seqlen; i++) { |
| 6839 | const Py_ssize_t old_sz = sz; |
| 6840 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6841 | if (!PyUnicode_Check(item)) { |
| 6842 | PyErr_Format(PyExc_TypeError, |
| 6843 | "sequence item %zd: expected str instance," |
| 6844 | " %.80s found", |
| 6845 | i, Py_TYPE(item)->tp_name); |
| 6846 | goto onError; |
| 6847 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6848 | sz += PyUnicode_GET_SIZE(item); |
| 6849 | if (i != 0) |
| 6850 | sz += seplen; |
| 6851 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 6852 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6853 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6854 | goto onError; |
| 6855 | } |
| 6856 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6857 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6858 | res = _PyUnicode_New(sz); |
| 6859 | if (res == NULL) |
| 6860 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6861 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6862 | /* Catenate everything. */ |
| 6863 | res_p = PyUnicode_AS_UNICODE(res); |
| 6864 | for (i = 0; i < seqlen; ++i) { |
| 6865 | Py_ssize_t itemlen; |
| 6866 | item = items[i]; |
| 6867 | itemlen = PyUnicode_GET_SIZE(item); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6868 | /* Copy item, and maybe the separator. */ |
| 6869 | if (i) { |
| 6870 | Py_UNICODE_COPY(res_p, sep, seplen); |
| 6871 | res_p += seplen; |
| 6872 | } |
| 6873 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
| 6874 | res_p += itemlen; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6875 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6876 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6877 | Done: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6878 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6879 | return (PyObject *)res; |
| 6880 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6881 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6882 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6883 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6884 | return NULL; |
| 6885 | } |
| 6886 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6887 | static PyUnicodeObject * |
| 6888 | pad(PyUnicodeObject *self, |
| 6889 | Py_ssize_t left, |
| 6890 | Py_ssize_t right, |
| 6891 | Py_UNICODE fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6892 | { |
| 6893 | PyUnicodeObject *u; |
| 6894 | |
| 6895 | if (left < 0) |
| 6896 | left = 0; |
| 6897 | if (right < 0) |
| 6898 | right = 0; |
| 6899 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6900 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6901 | Py_INCREF(self); |
| 6902 | return self; |
| 6903 | } |
| 6904 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6905 | if (left > PY_SSIZE_T_MAX - self->length || |
| 6906 | right > PY_SSIZE_T_MAX - (left + self->length)) { |
| 6907 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 6908 | return NULL; |
| 6909 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6910 | u = _PyUnicode_New(left + self->length + right); |
| 6911 | if (u) { |
| 6912 | if (left) |
| 6913 | Py_UNICODE_FILL(u->str, fill, left); |
| 6914 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 6915 | if (right) |
| 6916 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 6917 | } |
| 6918 | |
| 6919 | return u; |
| 6920 | } |
| 6921 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6922 | PyObject * |
| 6923 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6924 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6925 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6926 | |
| 6927 | string = PyUnicode_FromObject(string); |
| 6928 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6929 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6930 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6931 | list = stringlib_splitlines( |
| 6932 | (PyObject*) string, PyUnicode_AS_UNICODE(string), |
| 6933 | PyUnicode_GET_SIZE(string), keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6934 | |
| 6935 | Py_DECREF(string); |
| 6936 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6937 | } |
| 6938 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6939 | static PyObject * |
| 6940 | split(PyUnicodeObject *self, |
| 6941 | PyUnicodeObject *substring, |
| 6942 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6943 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6944 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6945 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6946 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6947 | if (substring == NULL) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6948 | return stringlib_split_whitespace( |
| 6949 | (PyObject*) self, self->str, self->length, maxcount |
| 6950 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6951 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6952 | return stringlib_split( |
| 6953 | (PyObject*) self, self->str, self->length, |
| 6954 | substring->str, substring->length, |
| 6955 | maxcount |
| 6956 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6957 | } |
| 6958 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6959 | static PyObject * |
| 6960 | rsplit(PyUnicodeObject *self, |
| 6961 | PyUnicodeObject *substring, |
| 6962 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6963 | { |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6964 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6965 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6966 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6967 | if (substring == NULL) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6968 | return stringlib_rsplit_whitespace( |
| 6969 | (PyObject*) self, self->str, self->length, maxcount |
| 6970 | ); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6971 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6972 | return stringlib_rsplit( |
| 6973 | (PyObject*) self, self->str, self->length, |
| 6974 | substring->str, substring->length, |
| 6975 | maxcount |
| 6976 | ); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6977 | } |
| 6978 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6979 | static PyObject * |
| 6980 | replace(PyUnicodeObject *self, |
| 6981 | PyUnicodeObject *str1, |
| 6982 | PyUnicodeObject *str2, |
| 6983 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6984 | { |
| 6985 | PyUnicodeObject *u; |
| 6986 | |
| 6987 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6988 | maxcount = PY_SSIZE_T_MAX; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6989 | else if (maxcount == 0 || self->length == 0) |
| 6990 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6991 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6992 | if (str1->length == str2->length) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 6993 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6994 | /* same length */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6995 | if (str1->length == 0) |
| 6996 | goto nothing; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6997 | if (str1->length == 1) { |
| 6998 | /* replace characters */ |
| 6999 | Py_UNICODE u1, u2; |
| 7000 | if (!findchar(self->str, self->length, str1->str[0])) |
| 7001 | goto nothing; |
| 7002 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 7003 | if (!u) |
| 7004 | return NULL; |
| 7005 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 7006 | u1 = str1->str[0]; |
| 7007 | u2 = str2->str[0]; |
| 7008 | for (i = 0; i < u->length; i++) |
| 7009 | if (u->str[i] == u1) { |
| 7010 | if (--maxcount < 0) |
| 7011 | break; |
| 7012 | u->str[i] = u2; |
| 7013 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7014 | } else { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7015 | i = stringlib_find( |
| 7016 | self->str, self->length, str1->str, str1->length, 0 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7017 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7018 | if (i < 0) |
| 7019 | goto nothing; |
| 7020 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 7021 | if (!u) |
| 7022 | return NULL; |
| 7023 | Py_UNICODE_COPY(u->str, self->str, self->length); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7024 | |
| 7025 | /* change everything in-place, starting with this one */ |
| 7026 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 7027 | i += str1->length; |
| 7028 | |
| 7029 | while ( --maxcount > 0) { |
| 7030 | i = stringlib_find(self->str+i, self->length-i, |
| 7031 | str1->str, str1->length, |
| 7032 | i); |
| 7033 | if (i == -1) |
| 7034 | break; |
| 7035 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 7036 | i += str1->length; |
| 7037 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7038 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7039 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7040 | |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 7041 | Py_ssize_t n, i, j; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7042 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7043 | Py_UNICODE *p; |
| 7044 | |
| 7045 | /* replace strings */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7046 | n = stringlib_count(self->str, self->length, str1->str, str1->length, |
| 7047 | maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7048 | if (n == 0) |
| 7049 | goto nothing; |
| 7050 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 7051 | delta = (str2->length - str1->length); |
| 7052 | if (delta == 0) { |
| 7053 | new_size = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7054 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7055 | product = n * (str2->length - str1->length); |
| 7056 | if ((product / (str2->length - str1->length)) != n) { |
| 7057 | PyErr_SetString(PyExc_OverflowError, |
| 7058 | "replace string is too long"); |
| 7059 | return NULL; |
| 7060 | } |
| 7061 | new_size = self->length + product; |
| 7062 | if (new_size < 0) { |
| 7063 | PyErr_SetString(PyExc_OverflowError, |
| 7064 | "replace string is too long"); |
| 7065 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7066 | } |
| 7067 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7068 | u = _PyUnicode_New(new_size); |
| 7069 | if (!u) |
| 7070 | return NULL; |
| 7071 | i = 0; |
| 7072 | p = u->str; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7073 | if (str1->length > 0) { |
| 7074 | while (n-- > 0) { |
| 7075 | /* look for next match */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7076 | j = stringlib_find(self->str+i, self->length-i, |
| 7077 | str1->str, str1->length, |
| 7078 | i); |
| 7079 | if (j == -1) |
| 7080 | break; |
| 7081 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7082 | /* copy unchanged part [i:j] */ |
| 7083 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 7084 | p += j - i; |
| 7085 | } |
| 7086 | /* copy substitution string */ |
| 7087 | if (str2->length > 0) { |
| 7088 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 7089 | p += str2->length; |
| 7090 | } |
| 7091 | i = j + str1->length; |
| 7092 | } |
| 7093 | if (i < self->length) |
| 7094 | /* copy tail [i:] */ |
| 7095 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 7096 | } else { |
| 7097 | /* interleave */ |
| 7098 | while (n > 0) { |
| 7099 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 7100 | p += str2->length; |
| 7101 | if (--n <= 0) |
| 7102 | break; |
| 7103 | *p++ = self->str[i++]; |
| 7104 | } |
| 7105 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 7106 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7107 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7108 | return (PyObject *) u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7109 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7110 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7111 | /* nothing to replace; return original string (when possible) */ |
| 7112 | if (PyUnicode_CheckExact(self)) { |
| 7113 | Py_INCREF(self); |
| 7114 | return (PyObject *) self; |
| 7115 | } |
| 7116 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7117 | } |
| 7118 | |
| 7119 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 7120 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7121 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7122 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7123 | \n\ |
| 7124 | 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] | 7125 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7126 | |
| 7127 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7128 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7129 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7130 | return fixup(self, fixtitle); |
| 7131 | } |
| 7132 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7133 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7134 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7135 | \n\ |
| 7136 | 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] | 7137 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7138 | |
| 7139 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7140 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7141 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7142 | return fixup(self, fixcapitalize); |
| 7143 | } |
| 7144 | |
| 7145 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7146 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7147 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7148 | \n\ |
| 7149 | 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] | 7150 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7151 | |
| 7152 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7153 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7154 | { |
| 7155 | PyObject *list; |
| 7156 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7157 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7158 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7159 | /* Split into words */ |
| 7160 | list = split(self, NULL, -1); |
| 7161 | if (!list) |
| 7162 | return NULL; |
| 7163 | |
| 7164 | /* Capitalize each word */ |
| 7165 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 7166 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7167 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7168 | if (item == NULL) |
| 7169 | goto onError; |
| 7170 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 7171 | PyList_SET_ITEM(list, i, item); |
| 7172 | } |
| 7173 | |
| 7174 | /* Join the words to form a new string */ |
| 7175 | item = PyUnicode_Join(NULL, list); |
| 7176 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7177 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7178 | Py_DECREF(list); |
| 7179 | return (PyObject *)item; |
| 7180 | } |
| 7181 | #endif |
| 7182 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7183 | /* Argument converter. Coerces to a single unicode character */ |
| 7184 | |
| 7185 | static int |
| 7186 | convert_uc(PyObject *obj, void *addr) |
| 7187 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7188 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 7189 | PyObject *uniobj; |
| 7190 | Py_UNICODE *unistr; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7191 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7192 | uniobj = PyUnicode_FromObject(obj); |
| 7193 | if (uniobj == NULL) { |
| 7194 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7195 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7196 | return 0; |
| 7197 | } |
| 7198 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 7199 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7200 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7201 | Py_DECREF(uniobj); |
| 7202 | return 0; |
| 7203 | } |
| 7204 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 7205 | *fillcharloc = unistr[0]; |
| 7206 | Py_DECREF(uniobj); |
| 7207 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7208 | } |
| 7209 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7210 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7211 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7212 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7213 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7214 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7215 | |
| 7216 | static PyObject * |
| 7217 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 7218 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7219 | Py_ssize_t marg, left; |
| 7220 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7221 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7222 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 7223 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7224 | return NULL; |
| 7225 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7226 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7227 | Py_INCREF(self); |
| 7228 | return (PyObject*) self; |
| 7229 | } |
| 7230 | |
| 7231 | marg = width - self->length; |
| 7232 | left = marg / 2 + (marg & width & 1); |
| 7233 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7234 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7235 | } |
| 7236 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7237 | #if 0 |
| 7238 | |
| 7239 | /* This code should go into some future Unicode collation support |
| 7240 | module. The basic comparison should compare ordinals on a naive |
Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 7241 | basis (this is what Java does and thus Jython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7242 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7243 | /* speedy UTF-16 code point order comparison */ |
| 7244 | /* gleaned from: */ |
| 7245 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 7246 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 7247 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7248 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7249 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7250 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 7251 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 7252 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7253 | }; |
| 7254 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7255 | static int |
| 7256 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 7257 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7258 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7259 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7260 | Py_UNICODE *s1 = str1->str; |
| 7261 | Py_UNICODE *s2 = str2->str; |
| 7262 | |
| 7263 | len1 = str1->length; |
| 7264 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7265 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7266 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7267 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7268 | |
| 7269 | c1 = *s1++; |
| 7270 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 7271 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7272 | if (c1 > (1<<11) * 26) |
| 7273 | c1 += utf16Fixup[c1>>11]; |
| 7274 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7275 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7276 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 7277 | |
| 7278 | if (c1 != c2) |
| 7279 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7280 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 7281 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7282 | } |
| 7283 | |
| 7284 | return (len1 < len2) ? -1 : (len1 != len2); |
| 7285 | } |
| 7286 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7287 | #else |
| 7288 | |
| 7289 | static int |
| 7290 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 7291 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7292 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7293 | |
| 7294 | Py_UNICODE *s1 = str1->str; |
| 7295 | Py_UNICODE *s2 = str2->str; |
| 7296 | |
| 7297 | len1 = str1->length; |
| 7298 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7299 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7300 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7301 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7302 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 7303 | c1 = *s1++; |
| 7304 | c2 = *s2++; |
| 7305 | |
| 7306 | if (c1 != c2) |
| 7307 | return (c1 < c2) ? -1 : 1; |
| 7308 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 7309 | len1--; len2--; |
| 7310 | } |
| 7311 | |
| 7312 | return (len1 < len2) ? -1 : (len1 != len2); |
| 7313 | } |
| 7314 | |
| 7315 | #endif |
| 7316 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7317 | int |
| 7318 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7319 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 7320 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) |
| 7321 | return unicode_compare((PyUnicodeObject *)left, |
| 7322 | (PyUnicodeObject *)right); |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 7323 | PyErr_Format(PyExc_TypeError, |
| 7324 | "Can't compare %.100s and %.100s", |
| 7325 | left->ob_type->tp_name, |
| 7326 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7327 | return -1; |
| 7328 | } |
| 7329 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 7330 | int |
| 7331 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 7332 | { |
| 7333 | int i; |
| 7334 | Py_UNICODE *id; |
| 7335 | assert(PyUnicode_Check(uni)); |
| 7336 | id = PyUnicode_AS_UNICODE(uni); |
| 7337 | /* Compare Unicode string and source character set string */ |
| 7338 | for (i = 0; id[i] && str[i]; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7339 | if (id[i] != str[i]) |
| 7340 | return ((int)id[i] < (int)str[i]) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 7341 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 7342 | to C strings identical up to that point. */ |
Benjamin Peterson | a23831f | 2010-04-25 21:54:00 +0000 | [diff] [blame] | 7343 | if (PyUnicode_GET_SIZE(uni) != i || id[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7344 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 7345 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7346 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 7347 | return 0; |
| 7348 | } |
| 7349 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7350 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7351 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7352 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7353 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7354 | PyObject * |
| 7355 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7356 | { |
| 7357 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7358 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7359 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 7360 | PyObject *v; |
Benjamin Peterson | 5fd4bd3 | 2011-03-06 09:06:34 -0600 | [diff] [blame] | 7361 | if (PyUnicode_GET_SIZE(left) != PyUnicode_GET_SIZE(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7362 | if (op == Py_EQ) { |
| 7363 | Py_INCREF(Py_False); |
| 7364 | return Py_False; |
| 7365 | } |
| 7366 | if (op == Py_NE) { |
| 7367 | Py_INCREF(Py_True); |
| 7368 | return Py_True; |
| 7369 | } |
| 7370 | } |
| 7371 | if (left == right) |
| 7372 | result = 0; |
| 7373 | else |
| 7374 | result = unicode_compare((PyUnicodeObject *)left, |
| 7375 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7376 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7377 | /* Convert the return value to a Boolean */ |
| 7378 | switch (op) { |
| 7379 | case Py_EQ: |
| 7380 | v = TEST_COND(result == 0); |
| 7381 | break; |
| 7382 | case Py_NE: |
| 7383 | v = TEST_COND(result != 0); |
| 7384 | break; |
| 7385 | case Py_LE: |
| 7386 | v = TEST_COND(result <= 0); |
| 7387 | break; |
| 7388 | case Py_GE: |
| 7389 | v = TEST_COND(result >= 0); |
| 7390 | break; |
| 7391 | case Py_LT: |
| 7392 | v = TEST_COND(result == -1); |
| 7393 | break; |
| 7394 | case Py_GT: |
| 7395 | v = TEST_COND(result == 1); |
| 7396 | break; |
| 7397 | default: |
| 7398 | PyErr_BadArgument(); |
| 7399 | return NULL; |
| 7400 | } |
| 7401 | Py_INCREF(v); |
| 7402 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7403 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7404 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7405 | Py_INCREF(Py_NotImplemented); |
| 7406 | return Py_NotImplemented; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7407 | } |
| 7408 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7409 | int |
| 7410 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7411 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7412 | PyObject *str, *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7413 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7414 | |
| 7415 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7416 | sub = PyUnicode_FromObject(element); |
| 7417 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7418 | PyErr_Format(PyExc_TypeError, |
| 7419 | "'in <string>' requires string as left operand, not %s", |
| 7420 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7421 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7422 | } |
| 7423 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7424 | str = PyUnicode_FromObject(container); |
| 7425 | if (!str) { |
| 7426 | Py_DECREF(sub); |
| 7427 | return -1; |
| 7428 | } |
| 7429 | |
| 7430 | result = stringlib_contains_obj(str, sub); |
| 7431 | |
| 7432 | Py_DECREF(str); |
| 7433 | Py_DECREF(sub); |
| 7434 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7435 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7436 | } |
| 7437 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7438 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 7439 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7440 | PyObject * |
| 7441 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7442 | { |
| 7443 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 7444 | |
| 7445 | /* Coerce the two arguments */ |
| 7446 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 7447 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7448 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7449 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 7450 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7451 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7452 | |
| 7453 | /* Shortcuts */ |
| 7454 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7455 | Py_DECREF(v); |
| 7456 | return (PyObject *)u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7457 | } |
| 7458 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7459 | Py_DECREF(u); |
| 7460 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7461 | } |
| 7462 | |
| 7463 | /* Concat the two Unicode strings */ |
| 7464 | w = _PyUnicode_New(u->length + v->length); |
| 7465 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7466 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7467 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 7468 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 7469 | |
| 7470 | Py_DECREF(u); |
| 7471 | Py_DECREF(v); |
| 7472 | return (PyObject *)w; |
| 7473 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7474 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7475 | Py_XDECREF(u); |
| 7476 | Py_XDECREF(v); |
| 7477 | return NULL; |
| 7478 | } |
| 7479 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7480 | void |
| 7481 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 7482 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7483 | PyObject *new; |
| 7484 | if (*pleft == NULL) |
| 7485 | return; |
| 7486 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 7487 | Py_DECREF(*pleft); |
| 7488 | *pleft = NULL; |
| 7489 | return; |
| 7490 | } |
| 7491 | new = PyUnicode_Concat(*pleft, right); |
| 7492 | Py_DECREF(*pleft); |
| 7493 | *pleft = new; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7494 | } |
| 7495 | |
| 7496 | void |
| 7497 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 7498 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7499 | PyUnicode_Append(pleft, right); |
| 7500 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7501 | } |
| 7502 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7503 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7504 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7505 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7506 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7507 | 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] | 7508 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7509 | |
| 7510 | static PyObject * |
| 7511 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 7512 | { |
| 7513 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7514 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7515 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7516 | PyObject *result; |
| 7517 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7518 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7519 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7520 | return NULL; |
| 7521 | |
| 7522 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7523 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7524 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7525 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7526 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7527 | ADJUST_INDICES(start, end, self->length); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7528 | result = PyLong_FromSsize_t( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7529 | stringlib_count(self->str + start, end - start, |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7530 | substring->str, substring->length, |
| 7531 | PY_SSIZE_T_MAX) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7532 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7533 | |
| 7534 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7535 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7536 | return result; |
| 7537 | } |
| 7538 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7539 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 7540 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7541 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 7542 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 7543 | is 'utf-8'. errors may be given to set a different error\n\ |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7544 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7545 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 7546 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 7547 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7548 | |
| 7549 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7550 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7551 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7552 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7553 | char *encoding = NULL; |
| 7554 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 7555 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7556 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 7557 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7558 | return NULL; |
Georg Brandl | 3b9406b | 2010-12-03 07:54:09 +0000 | [diff] [blame] | 7559 | return PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7560 | } |
| 7561 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7562 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7563 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7564 | \n\ |
| 7565 | 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] | 7566 | 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] | 7567 | |
| 7568 | static PyObject* |
| 7569 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 7570 | { |
| 7571 | Py_UNICODE *e; |
| 7572 | Py_UNICODE *p; |
| 7573 | Py_UNICODE *q; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7574 | Py_UNICODE *qe; |
| 7575 | Py_ssize_t i, j, incr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7576 | PyUnicodeObject *u; |
| 7577 | int tabsize = 8; |
| 7578 | |
| 7579 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7580 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7581 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 7582 | /* First pass: determine size of output string */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7583 | i = 0; /* chars up to and including most recent \n or \r */ |
| 7584 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
| 7585 | e = self->str + self->length; /* end of input */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7586 | for (p = self->str; p < e; p++) |
| 7587 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7588 | if (tabsize > 0) { |
| 7589 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 7590 | if (j > PY_SSIZE_T_MAX - incr) |
| 7591 | goto overflow1; |
| 7592 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7593 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7594 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7595 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7596 | if (j > PY_SSIZE_T_MAX - 1) |
| 7597 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7598 | j++; |
| 7599 | if (*p == '\n' || *p == '\r') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7600 | if (i > PY_SSIZE_T_MAX - j) |
| 7601 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7602 | i += j; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7603 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7604 | } |
| 7605 | } |
| 7606 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7607 | if (i > PY_SSIZE_T_MAX - j) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7608 | goto overflow1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 7609 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7610 | /* Second pass: create output string and fill it */ |
| 7611 | u = _PyUnicode_New(i + j); |
| 7612 | if (!u) |
| 7613 | return NULL; |
| 7614 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7615 | j = 0; /* same as in first pass */ |
| 7616 | q = u->str; /* next output char */ |
| 7617 | qe = u->str + u->length; /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7618 | |
| 7619 | for (p = self->str; p < e; p++) |
| 7620 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7621 | if (tabsize > 0) { |
| 7622 | i = tabsize - (j % tabsize); |
| 7623 | j += i; |
| 7624 | while (i--) { |
| 7625 | if (q >= qe) |
| 7626 | goto overflow2; |
| 7627 | *q++ = ' '; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7628 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7629 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7630 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7631 | else { |
| 7632 | if (q >= qe) |
| 7633 | goto overflow2; |
| 7634 | *q++ = *p; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7635 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7636 | if (*p == '\n' || *p == '\r') |
| 7637 | j = 0; |
| 7638 | } |
| 7639 | |
| 7640 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7641 | |
| 7642 | overflow2: |
| 7643 | Py_DECREF(u); |
| 7644 | overflow1: |
| 7645 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 7646 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7647 | } |
| 7648 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7649 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7650 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7651 | \n\ |
| 7652 | 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] | 7653 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7654 | arguments start and end are interpreted as in slice notation.\n\ |
| 7655 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7656 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7657 | |
| 7658 | static PyObject * |
| 7659 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 7660 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7661 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7662 | Py_ssize_t start; |
| 7663 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7664 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7665 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7666 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7667 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7668 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7669 | result = stringlib_find_slice( |
| 7670 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7671 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7672 | start, end |
| 7673 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7674 | |
| 7675 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7676 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7677 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7678 | } |
| 7679 | |
| 7680 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7681 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7682 | { |
| 7683 | if (index < 0 || index >= self->length) { |
| 7684 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 7685 | return NULL; |
| 7686 | } |
| 7687 | |
| 7688 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 7689 | } |
| 7690 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7691 | /* Believe it or not, this produces the same value for ASCII strings |
| 7692 | as string_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 7693 | static Py_hash_t |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 7694 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7695 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7696 | Py_ssize_t len; |
| 7697 | Py_UNICODE *p; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 7698 | Py_hash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7699 | |
| 7700 | if (self->hash != -1) |
| 7701 | return self->hash; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7702 | len = Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7703 | p = self->str; |
| 7704 | x = *p << 7; |
| 7705 | while (--len >= 0) |
| 7706 | x = (1000003*x) ^ *p++; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7707 | x ^= Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7708 | if (x == -1) |
| 7709 | x = -2; |
| 7710 | self->hash = x; |
| 7711 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7712 | } |
| 7713 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7714 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7715 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7716 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7717 | 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] | 7718 | |
| 7719 | static PyObject * |
| 7720 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 7721 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7722 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7723 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7724 | Py_ssize_t start; |
| 7725 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7726 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7727 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7728 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7729 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7730 | result = stringlib_find_slice( |
| 7731 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7732 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7733 | start, end |
| 7734 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7735 | |
| 7736 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7737 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7738 | if (result < 0) { |
| 7739 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7740 | return NULL; |
| 7741 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7742 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7743 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7744 | } |
| 7745 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7746 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7747 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7748 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7749 | 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] | 7750 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7751 | |
| 7752 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7753 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7754 | { |
| 7755 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7756 | register const Py_UNICODE *e; |
| 7757 | int cased; |
| 7758 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7759 | /* Shortcut for single character strings */ |
| 7760 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7761 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7762 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7763 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7764 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7765 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7766 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7767 | e = p + PyUnicode_GET_SIZE(self); |
| 7768 | cased = 0; |
| 7769 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7770 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7771 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7772 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7773 | return PyBool_FromLong(0); |
| 7774 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 7775 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7776 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7777 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7778 | } |
| 7779 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7780 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7781 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7782 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7783 | 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] | 7784 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7785 | |
| 7786 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7787 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7788 | { |
| 7789 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7790 | register const Py_UNICODE *e; |
| 7791 | int cased; |
| 7792 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7793 | /* Shortcut for single character strings */ |
| 7794 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7795 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7796 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7797 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7798 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7799 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7800 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7801 | e = p + PyUnicode_GET_SIZE(self); |
| 7802 | cased = 0; |
| 7803 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7804 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7805 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7806 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7807 | return PyBool_FromLong(0); |
| 7808 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 7809 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7810 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7811 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7812 | } |
| 7813 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7814 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7815 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7816 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7817 | Return True if S is a titlecased string and there is at least one\n\ |
| 7818 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 7819 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 7820 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7821 | |
| 7822 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7823 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7824 | { |
| 7825 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7826 | register const Py_UNICODE *e; |
| 7827 | int cased, previous_is_cased; |
| 7828 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7829 | /* Shortcut for single character strings */ |
| 7830 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7831 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 7832 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7833 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7834 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7835 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7836 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7837 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7838 | e = p + PyUnicode_GET_SIZE(self); |
| 7839 | cased = 0; |
| 7840 | previous_is_cased = 0; |
| 7841 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7842 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7843 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7844 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 7845 | if (previous_is_cased) |
| 7846 | return PyBool_FromLong(0); |
| 7847 | previous_is_cased = 1; |
| 7848 | cased = 1; |
| 7849 | } |
| 7850 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 7851 | if (!previous_is_cased) |
| 7852 | return PyBool_FromLong(0); |
| 7853 | previous_is_cased = 1; |
| 7854 | cased = 1; |
| 7855 | } |
| 7856 | else |
| 7857 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7858 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7859 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7860 | } |
| 7861 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7862 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7863 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7864 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7865 | Return True if all characters in S are whitespace\n\ |
| 7866 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7867 | |
| 7868 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7869 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7870 | { |
| 7871 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7872 | register const Py_UNICODE *e; |
| 7873 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7874 | /* Shortcut for single character strings */ |
| 7875 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7876 | Py_UNICODE_ISSPACE(*p)) |
| 7877 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7878 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7879 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7880 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7881 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7882 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7883 | e = p + PyUnicode_GET_SIZE(self); |
| 7884 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7885 | if (!Py_UNICODE_ISSPACE(*p)) |
| 7886 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7887 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7888 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7889 | } |
| 7890 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7891 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7892 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7893 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7894 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7895 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7896 | |
| 7897 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7898 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7899 | { |
| 7900 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7901 | register const Py_UNICODE *e; |
| 7902 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7903 | /* Shortcut for single character strings */ |
| 7904 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7905 | Py_UNICODE_ISALPHA(*p)) |
| 7906 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7907 | |
| 7908 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7909 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7910 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7911 | |
| 7912 | e = p + PyUnicode_GET_SIZE(self); |
| 7913 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7914 | if (!Py_UNICODE_ISALPHA(*p)) |
| 7915 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7916 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7917 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7918 | } |
| 7919 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7920 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7921 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7922 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7923 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7924 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7925 | |
| 7926 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7927 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7928 | { |
| 7929 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7930 | register const Py_UNICODE *e; |
| 7931 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7932 | /* Shortcut for single character strings */ |
| 7933 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7934 | Py_UNICODE_ISALNUM(*p)) |
| 7935 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7936 | |
| 7937 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7938 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7939 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7940 | |
| 7941 | e = p + PyUnicode_GET_SIZE(self); |
| 7942 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7943 | if (!Py_UNICODE_ISALNUM(*p)) |
| 7944 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7945 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7946 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7947 | } |
| 7948 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7949 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7950 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7951 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7952 | 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] | 7953 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7954 | |
| 7955 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7956 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7957 | { |
| 7958 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7959 | register const Py_UNICODE *e; |
| 7960 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7961 | /* Shortcut for single character strings */ |
| 7962 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7963 | Py_UNICODE_ISDECIMAL(*p)) |
| 7964 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7965 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7966 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7967 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7968 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7969 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7970 | e = p + PyUnicode_GET_SIZE(self); |
| 7971 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7972 | if (!Py_UNICODE_ISDECIMAL(*p)) |
| 7973 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7974 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7975 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7976 | } |
| 7977 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7978 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7979 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7980 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7981 | Return True if all characters in S are digits\n\ |
| 7982 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7983 | |
| 7984 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7985 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7986 | { |
| 7987 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7988 | register const Py_UNICODE *e; |
| 7989 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7990 | /* Shortcut for single character strings */ |
| 7991 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7992 | Py_UNICODE_ISDIGIT(*p)) |
| 7993 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7994 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7995 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7996 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7997 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7998 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7999 | e = p + PyUnicode_GET_SIZE(self); |
| 8000 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8001 | if (!Py_UNICODE_ISDIGIT(*p)) |
| 8002 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8003 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 8004 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8005 | } |
| 8006 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8007 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8008 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8009 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 8010 | 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] | 8011 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8012 | |
| 8013 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8014 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8015 | { |
| 8016 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 8017 | register const Py_UNICODE *e; |
| 8018 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8019 | /* Shortcut for single character strings */ |
| 8020 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8021 | Py_UNICODE_ISNUMERIC(*p)) |
| 8022 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8023 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 8024 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8025 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8026 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 8027 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8028 | e = p + PyUnicode_GET_SIZE(self); |
| 8029 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8030 | if (!Py_UNICODE_ISNUMERIC(*p)) |
| 8031 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8032 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 8033 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8034 | } |
| 8035 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8036 | int |
| 8037 | PyUnicode_IsIdentifier(PyObject *self) |
| 8038 | { |
| 8039 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE((PyUnicodeObject*)self); |
| 8040 | register const Py_UNICODE *e; |
| 8041 | |
| 8042 | /* Special case for empty strings */ |
| 8043 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8044 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8045 | |
| 8046 | /* PEP 3131 says that the first character must be in |
| 8047 | XID_Start and subsequent characters in XID_Continue, |
| 8048 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8049 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8050 | letters, digits, underscore). However, given the current |
| 8051 | definition of XID_Start and XID_Continue, it is sufficient |
| 8052 | to check just for these, except that _ must be allowed |
| 8053 | as starting an identifier. */ |
| 8054 | if (!_PyUnicode_IsXidStart(*p) && *p != 0x5F /* LOW LINE */) |
| 8055 | return 0; |
| 8056 | |
| 8057 | e = p + PyUnicode_GET_SIZE(self); |
| 8058 | for (p++; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8059 | if (!_PyUnicode_IsXidContinue(*p)) |
| 8060 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8061 | } |
| 8062 | return 1; |
| 8063 | } |
| 8064 | |
| 8065 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8066 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8067 | \n\ |
| 8068 | Return True if S is a valid identifier according\n\ |
| 8069 | to the language definition."); |
| 8070 | |
| 8071 | static PyObject* |
| 8072 | unicode_isidentifier(PyObject *self) |
| 8073 | { |
| 8074 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 8075 | } |
| 8076 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8077 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8078 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8079 | \n\ |
| 8080 | Return True if all characters in S are considered\n\ |
| 8081 | printable in repr() or S is empty, False otherwise."); |
| 8082 | |
| 8083 | static PyObject* |
| 8084 | unicode_isprintable(PyObject *self) |
| 8085 | { |
| 8086 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 8087 | register const Py_UNICODE *e; |
| 8088 | |
| 8089 | /* Shortcut for single character strings */ |
| 8090 | if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISPRINTABLE(*p)) { |
| 8091 | Py_RETURN_TRUE; |
| 8092 | } |
| 8093 | |
| 8094 | e = p + PyUnicode_GET_SIZE(self); |
| 8095 | for (; p < e; p++) { |
| 8096 | if (!Py_UNICODE_ISPRINTABLE(*p)) { |
| 8097 | Py_RETURN_FALSE; |
| 8098 | } |
| 8099 | } |
| 8100 | Py_RETURN_TRUE; |
| 8101 | } |
| 8102 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8103 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 8104 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8105 | \n\ |
| 8106 | 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] | 8107 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8108 | |
| 8109 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8110 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8111 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8112 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8113 | } |
| 8114 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8115 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8116 | unicode_length(PyUnicodeObject *self) |
| 8117 | { |
| 8118 | return self->length; |
| 8119 | } |
| 8120 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8121 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8122 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8123 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 8124 | 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] | 8125 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8126 | |
| 8127 | static PyObject * |
| 8128 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 8129 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8130 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8131 | Py_UNICODE fillchar = ' '; |
| 8132 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8133 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8134 | return NULL; |
| 8135 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8136 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8137 | Py_INCREF(self); |
| 8138 | return (PyObject*) self; |
| 8139 | } |
| 8140 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8141 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8142 | } |
| 8143 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8144 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8145 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8146 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8147 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8148 | |
| 8149 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8150 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8151 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8152 | return fixup(self, fixlower); |
| 8153 | } |
| 8154 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8155 | #define LEFTSTRIP 0 |
| 8156 | #define RIGHTSTRIP 1 |
| 8157 | #define BOTHSTRIP 2 |
| 8158 | |
| 8159 | /* Arrays indexed by above */ |
| 8160 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 8161 | |
| 8162 | #define STRIPNAME(i) (stripformat[i]+3) |
| 8163 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8164 | /* externally visible for str.strip(unicode) */ |
| 8165 | PyObject * |
| 8166 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 8167 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8168 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 8169 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
| 8170 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
| 8171 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 8172 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8173 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8174 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8175 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8176 | i = 0; |
| 8177 | if (striptype != RIGHTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8178 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 8179 | i++; |
| 8180 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8181 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8182 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8183 | j = len; |
| 8184 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8185 | do { |
| 8186 | j--; |
| 8187 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 8188 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8189 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8190 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8191 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8192 | Py_INCREF(self); |
| 8193 | return (PyObject*)self; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8194 | } |
| 8195 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8196 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8197 | } |
| 8198 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8199 | |
| 8200 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8201 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8202 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8203 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 8204 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8205 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8206 | i = 0; |
| 8207 | if (striptype != RIGHTSTRIP) { |
| 8208 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 8209 | i++; |
| 8210 | } |
| 8211 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8212 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8213 | j = len; |
| 8214 | if (striptype != LEFTSTRIP) { |
| 8215 | do { |
| 8216 | j--; |
| 8217 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 8218 | j++; |
| 8219 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8220 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8221 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 8222 | Py_INCREF(self); |
| 8223 | return (PyObject*)self; |
| 8224 | } |
| 8225 | else |
| 8226 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8227 | } |
| 8228 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8229 | |
| 8230 | static PyObject * |
| 8231 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 8232 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8233 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8234 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8235 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 8236 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8237 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8238 | if (sep != NULL && sep != Py_None) { |
| 8239 | if (PyUnicode_Check(sep)) |
| 8240 | return _PyUnicode_XStrip(self, striptype, sep); |
| 8241 | else { |
| 8242 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8243 | "%s arg must be None or str", |
| 8244 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8245 | return NULL; |
| 8246 | } |
| 8247 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8248 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8249 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8250 | } |
| 8251 | |
| 8252 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8253 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8254 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8255 | \n\ |
| 8256 | Return a copy of the string S with leading and trailing\n\ |
| 8257 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8258 | 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] | 8259 | |
| 8260 | static PyObject * |
| 8261 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 8262 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8263 | if (PyTuple_GET_SIZE(args) == 0) |
| 8264 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 8265 | else |
| 8266 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8267 | } |
| 8268 | |
| 8269 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8270 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8271 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8272 | \n\ |
| 8273 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8274 | 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] | 8275 | |
| 8276 | static PyObject * |
| 8277 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 8278 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8279 | if (PyTuple_GET_SIZE(args) == 0) |
| 8280 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 8281 | else |
| 8282 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8283 | } |
| 8284 | |
| 8285 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8286 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8287 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8288 | \n\ |
| 8289 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8290 | 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] | 8291 | |
| 8292 | static PyObject * |
| 8293 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 8294 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8295 | if (PyTuple_GET_SIZE(args) == 0) |
| 8296 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 8297 | else |
| 8298 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8299 | } |
| 8300 | |
| 8301 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8302 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8303 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8304 | { |
| 8305 | PyUnicodeObject *u; |
| 8306 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8307 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 8308 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8309 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 8310 | if (len < 1) { |
| 8311 | Py_INCREF(unicode_empty); |
| 8312 | return (PyObject *)unicode_empty; |
| 8313 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8314 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8315 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8316 | /* no repeat, return original string */ |
| 8317 | Py_INCREF(str); |
| 8318 | return (PyObject*) str; |
| 8319 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 8320 | |
| 8321 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 8322 | * needed doesn't overflow size_t |
| 8323 | */ |
| 8324 | nchars = len * str->length; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 8325 | if (nchars / len != str->length) { |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 8326 | PyErr_SetString(PyExc_OverflowError, |
| 8327 | "repeated string is too long"); |
| 8328 | return NULL; |
| 8329 | } |
| 8330 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 8331 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 8332 | PyErr_SetString(PyExc_OverflowError, |
| 8333 | "repeated string is too long"); |
| 8334 | return NULL; |
| 8335 | } |
| 8336 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8337 | if (!u) |
| 8338 | return NULL; |
| 8339 | |
| 8340 | p = u->str; |
| 8341 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 8342 | if (str->length == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8343 | Py_UNICODE_FILL(p, str->str[0], len); |
| 8344 | } else { |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 8345 | Py_ssize_t done = str->length; /* number of characters copied this far */ |
| 8346 | Py_UNICODE_COPY(p, str->str, str->length); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8347 | while (done < nchars) { |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 8348 | Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8349 | Py_UNICODE_COPY(p+done, p, n); |
| 8350 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8351 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8352 | } |
| 8353 | |
| 8354 | return (PyObject*) u; |
| 8355 | } |
| 8356 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8357 | PyObject * |
| 8358 | PyUnicode_Replace(PyObject *obj, |
| 8359 | PyObject *subobj, |
| 8360 | PyObject *replobj, |
| 8361 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8362 | { |
| 8363 | PyObject *self; |
| 8364 | PyObject *str1; |
| 8365 | PyObject *str2; |
| 8366 | PyObject *result; |
| 8367 | |
| 8368 | self = PyUnicode_FromObject(obj); |
| 8369 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8370 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8371 | str1 = PyUnicode_FromObject(subobj); |
| 8372 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8373 | Py_DECREF(self); |
| 8374 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8375 | } |
| 8376 | str2 = PyUnicode_FromObject(replobj); |
| 8377 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8378 | Py_DECREF(self); |
| 8379 | Py_DECREF(str1); |
| 8380 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8381 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8382 | result = replace((PyUnicodeObject *)self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8383 | (PyUnicodeObject *)str1, |
| 8384 | (PyUnicodeObject *)str2, |
| 8385 | maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8386 | Py_DECREF(self); |
| 8387 | Py_DECREF(str1); |
| 8388 | Py_DECREF(str2); |
| 8389 | return result; |
| 8390 | } |
| 8391 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8392 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 8393 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8394 | \n\ |
| 8395 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 8396 | old replaced by new. If the optional argument count is\n\ |
| 8397 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8398 | |
| 8399 | static PyObject* |
| 8400 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 8401 | { |
| 8402 | PyUnicodeObject *str1; |
| 8403 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8404 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8405 | PyObject *result; |
| 8406 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8407 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8408 | return NULL; |
| 8409 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 8410 | if (str1 == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8411 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8412 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 8413 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8414 | Py_DECREF(str1); |
| 8415 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 8416 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8417 | |
| 8418 | result = replace(self, str1, str2, maxcount); |
| 8419 | |
| 8420 | Py_DECREF(str1); |
| 8421 | Py_DECREF(str2); |
| 8422 | return result; |
| 8423 | } |
| 8424 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8425 | static PyObject * |
| 8426 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8427 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8428 | PyObject *repr; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8429 | Py_UNICODE *p; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8430 | Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); |
| 8431 | Py_ssize_t size = PyUnicode_GET_SIZE(unicode); |
| 8432 | |
| 8433 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 8434 | better to choose a different scheme. Perhaps scan the |
| 8435 | first N-chars of the string and allocate based on that size. |
| 8436 | */ |
| 8437 | /* Initial allocation is based on the longest-possible unichr |
| 8438 | escape. |
| 8439 | |
| 8440 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 8441 | unichr, so in this case it's the longest unichr escape. In |
| 8442 | narrow (UTF-16) builds this is five chars per source unichr |
| 8443 | since there are two unichrs in the surrogate pair, so in narrow |
| 8444 | (UTF-16) builds it's not the longest unichr escape. |
| 8445 | |
| 8446 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 8447 | so in the narrow (UTF-16) build case it's the longest unichr |
| 8448 | escape. |
| 8449 | */ |
| 8450 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8451 | repr = PyUnicode_FromUnicode(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8452 | 2 /* quotes */ |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8453 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8454 | + 10*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8455 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8456 | + 6*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8457 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8458 | + 1); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8459 | if (repr == NULL) |
| 8460 | return NULL; |
| 8461 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8462 | p = PyUnicode_AS_UNICODE(repr); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8463 | |
| 8464 | /* Add quote */ |
| 8465 | *p++ = (findchar(s, size, '\'') && |
| 8466 | !findchar(s, size, '"')) ? '"' : '\''; |
| 8467 | while (size-- > 0) { |
| 8468 | Py_UNICODE ch = *s++; |
| 8469 | |
| 8470 | /* Escape quotes and backslashes */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8471 | if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8472 | *p++ = '\\'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8473 | *p++ = ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8474 | continue; |
| 8475 | } |
| 8476 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8477 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8478 | if (ch == '\t') { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8479 | *p++ = '\\'; |
| 8480 | *p++ = 't'; |
| 8481 | } |
| 8482 | else if (ch == '\n') { |
| 8483 | *p++ = '\\'; |
| 8484 | *p++ = 'n'; |
| 8485 | } |
| 8486 | else if (ch == '\r') { |
| 8487 | *p++ = '\\'; |
| 8488 | *p++ = 'r'; |
| 8489 | } |
| 8490 | |
| 8491 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8492 | else if (ch < ' ' || ch == 0x7F) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8493 | *p++ = '\\'; |
| 8494 | *p++ = 'x'; |
| 8495 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 8496 | *p++ = hexdigits[ch & 0x000F]; |
| 8497 | } |
| 8498 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8499 | /* Copy ASCII characters as-is */ |
| 8500 | else if (ch < 0x7F) { |
| 8501 | *p++ = ch; |
| 8502 | } |
| 8503 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8504 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8505 | else { |
| 8506 | Py_UCS4 ucs = ch; |
| 8507 | |
| 8508 | #ifndef Py_UNICODE_WIDE |
| 8509 | Py_UNICODE ch2 = 0; |
| 8510 | /* Get code point from surrogate pair */ |
| 8511 | if (size > 0) { |
| 8512 | ch2 = *s; |
| 8513 | if (ch >= 0xD800 && ch < 0xDC00 && ch2 >= 0xDC00 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8514 | && ch2 <= 0xDFFF) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8515 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8516 | + 0x00010000; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8517 | s++; |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8518 | size--; |
| 8519 | } |
| 8520 | } |
| 8521 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8522 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8523 | (categories Z* and C* except ASCII space) |
| 8524 | */ |
| 8525 | if (!Py_UNICODE_ISPRINTABLE(ucs)) { |
| 8526 | /* Map 8-bit characters to '\xhh' */ |
| 8527 | if (ucs <= 0xff) { |
| 8528 | *p++ = '\\'; |
| 8529 | *p++ = 'x'; |
| 8530 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 8531 | *p++ = hexdigits[ch & 0x000F]; |
| 8532 | } |
| 8533 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 8534 | else if (ucs >= 0x10000) { |
| 8535 | *p++ = '\\'; |
| 8536 | *p++ = 'U'; |
| 8537 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 8538 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 8539 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 8540 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 8541 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 8542 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 8543 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 8544 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 8545 | } |
| 8546 | /* Map 16-bit characters to '\uxxxx' */ |
| 8547 | else { |
| 8548 | *p++ = '\\'; |
| 8549 | *p++ = 'u'; |
| 8550 | *p++ = hexdigits[(ucs >> 12) & 0x000F]; |
| 8551 | *p++ = hexdigits[(ucs >> 8) & 0x000F]; |
| 8552 | *p++ = hexdigits[(ucs >> 4) & 0x000F]; |
| 8553 | *p++ = hexdigits[ucs & 0x000F]; |
| 8554 | } |
| 8555 | } |
| 8556 | /* Copy characters as-is */ |
| 8557 | else { |
| 8558 | *p++ = ch; |
| 8559 | #ifndef Py_UNICODE_WIDE |
| 8560 | if (ucs >= 0x10000) |
| 8561 | *p++ = ch2; |
| 8562 | #endif |
| 8563 | } |
| 8564 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8565 | } |
| 8566 | /* Add quote */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8567 | *p++ = PyUnicode_AS_UNICODE(repr)[0]; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8568 | |
| 8569 | *p = '\0'; |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 8570 | PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8571 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8572 | } |
| 8573 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8574 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8575 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8576 | \n\ |
| 8577 | 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] | 8578 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8579 | arguments start and end are interpreted as in slice notation.\n\ |
| 8580 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8581 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8582 | |
| 8583 | static PyObject * |
| 8584 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 8585 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8586 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8587 | Py_ssize_t start; |
| 8588 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8589 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8590 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8591 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8592 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8593 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8594 | result = stringlib_rfind_slice( |
| 8595 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8596 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8597 | start, end |
| 8598 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8599 | |
| 8600 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8601 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8602 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8603 | } |
| 8604 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8605 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8606 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8607 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8608 | 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] | 8609 | |
| 8610 | static PyObject * |
| 8611 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 8612 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8613 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8614 | Py_ssize_t start; |
| 8615 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8616 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8617 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8618 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8619 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8620 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8621 | result = stringlib_rfind_slice( |
| 8622 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8623 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8624 | start, end |
| 8625 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8626 | |
| 8627 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8628 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8629 | if (result < 0) { |
| 8630 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 8631 | return NULL; |
| 8632 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8633 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8634 | } |
| 8635 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8636 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8637 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8638 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 8639 | 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] | 8640 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8641 | |
| 8642 | static PyObject * |
| 8643 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 8644 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8645 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8646 | Py_UNICODE fillchar = ' '; |
| 8647 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8648 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8649 | return NULL; |
| 8650 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8651 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8652 | Py_INCREF(self); |
| 8653 | return (PyObject*) self; |
| 8654 | } |
| 8655 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8656 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8657 | } |
| 8658 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8659 | PyObject * |
| 8660 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8661 | { |
| 8662 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8663 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8664 | s = PyUnicode_FromObject(s); |
| 8665 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8666 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8667 | if (sep != NULL) { |
| 8668 | sep = PyUnicode_FromObject(sep); |
| 8669 | if (sep == NULL) { |
| 8670 | Py_DECREF(s); |
| 8671 | return NULL; |
| 8672 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8673 | } |
| 8674 | |
| 8675 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8676 | |
| 8677 | Py_DECREF(s); |
| 8678 | Py_XDECREF(sep); |
| 8679 | return result; |
| 8680 | } |
| 8681 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8682 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8683 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8684 | \n\ |
| 8685 | Return a list of the words in S, using sep as the\n\ |
| 8686 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 8687 | 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] | 8688 | whitespace string is a separator and empty strings are\n\ |
| 8689 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8690 | |
| 8691 | static PyObject* |
| 8692 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 8693 | { |
| 8694 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8695 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8696 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8697 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8698 | return NULL; |
| 8699 | |
| 8700 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8701 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8702 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8703 | return split(self, (PyUnicodeObject *)substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8704 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8705 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8706 | } |
| 8707 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8708 | PyObject * |
| 8709 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 8710 | { |
| 8711 | PyObject* str_obj; |
| 8712 | PyObject* sep_obj; |
| 8713 | PyObject* out; |
| 8714 | |
| 8715 | str_obj = PyUnicode_FromObject(str_in); |
| 8716 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8717 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8718 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8719 | if (!sep_obj) { |
| 8720 | Py_DECREF(str_obj); |
| 8721 | return NULL; |
| 8722 | } |
| 8723 | |
| 8724 | out = stringlib_partition( |
| 8725 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8726 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8727 | ); |
| 8728 | |
| 8729 | Py_DECREF(sep_obj); |
| 8730 | Py_DECREF(str_obj); |
| 8731 | |
| 8732 | return out; |
| 8733 | } |
| 8734 | |
| 8735 | |
| 8736 | PyObject * |
| 8737 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 8738 | { |
| 8739 | PyObject* str_obj; |
| 8740 | PyObject* sep_obj; |
| 8741 | PyObject* out; |
| 8742 | |
| 8743 | str_obj = PyUnicode_FromObject(str_in); |
| 8744 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8745 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8746 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8747 | if (!sep_obj) { |
| 8748 | Py_DECREF(str_obj); |
| 8749 | return NULL; |
| 8750 | } |
| 8751 | |
| 8752 | out = stringlib_rpartition( |
| 8753 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8754 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8755 | ); |
| 8756 | |
| 8757 | Py_DECREF(sep_obj); |
| 8758 | Py_DECREF(str_obj); |
| 8759 | |
| 8760 | return out; |
| 8761 | } |
| 8762 | |
| 8763 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8764 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8765 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8766 | 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] | 8767 | 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] | 8768 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8769 | |
| 8770 | static PyObject* |
| 8771 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 8772 | { |
| 8773 | return PyUnicode_Partition((PyObject *)self, separator); |
| 8774 | } |
| 8775 | |
| 8776 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 8777 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8778 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8779 | 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] | 8780 | 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] | 8781 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8782 | |
| 8783 | static PyObject* |
| 8784 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 8785 | { |
| 8786 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 8787 | } |
| 8788 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8789 | PyObject * |
| 8790 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8791 | { |
| 8792 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8793 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8794 | s = PyUnicode_FromObject(s); |
| 8795 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8796 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8797 | if (sep != NULL) { |
| 8798 | sep = PyUnicode_FromObject(sep); |
| 8799 | if (sep == NULL) { |
| 8800 | Py_DECREF(s); |
| 8801 | return NULL; |
| 8802 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8803 | } |
| 8804 | |
| 8805 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8806 | |
| 8807 | Py_DECREF(s); |
| 8808 | Py_XDECREF(sep); |
| 8809 | return result; |
| 8810 | } |
| 8811 | |
| 8812 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8813 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8814 | \n\ |
| 8815 | Return a list of the words in S, using sep as the\n\ |
| 8816 | delimiter string, starting at the end of the string and\n\ |
| 8817 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 8818 | splits are done. If sep is not specified, any whitespace string\n\ |
| 8819 | is a separator."); |
| 8820 | |
| 8821 | static PyObject* |
| 8822 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 8823 | { |
| 8824 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8825 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8826 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8827 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8828 | return NULL; |
| 8829 | |
| 8830 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8831 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8832 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8833 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8834 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8835 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8836 | } |
| 8837 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8838 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8839 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8840 | \n\ |
| 8841 | 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] | 8842 | 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] | 8843 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8844 | |
| 8845 | static PyObject* |
| 8846 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 8847 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8848 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8849 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8850 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8851 | return NULL; |
| 8852 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8853 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8854 | } |
| 8855 | |
| 8856 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 8857 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8858 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 8859 | if (PyUnicode_CheckExact(self)) { |
| 8860 | Py_INCREF(self); |
| 8861 | return self; |
| 8862 | } else |
| 8863 | /* Subtype -- return genuine unicode string with the same value. */ |
| 8864 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), |
| 8865 | PyUnicode_GET_SIZE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8866 | } |
| 8867 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8868 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8869 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8870 | \n\ |
| 8871 | 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] | 8872 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8873 | |
| 8874 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8875 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8876 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8877 | return fixup(self, fixswapcase); |
| 8878 | } |
| 8879 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8880 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8881 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8882 | \n\ |
| 8883 | Return a translation table usable for str.translate().\n\ |
| 8884 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 8885 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8886 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8887 | If there are two arguments, they must be strings of equal length, and\n\ |
| 8888 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 8889 | character at the same position in y. If there is a third argument, it\n\ |
| 8890 | must be a string, whose characters will be mapped to None in the result."); |
| 8891 | |
| 8892 | static PyObject* |
| 8893 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 8894 | { |
| 8895 | PyObject *x, *y = NULL, *z = NULL; |
| 8896 | PyObject *new = NULL, *key, *value; |
| 8897 | Py_ssize_t i = 0; |
| 8898 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8899 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8900 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 8901 | return NULL; |
| 8902 | new = PyDict_New(); |
| 8903 | if (!new) |
| 8904 | return NULL; |
| 8905 | if (y != NULL) { |
| 8906 | /* x must be a string too, of equal length */ |
| 8907 | Py_ssize_t ylen = PyUnicode_GET_SIZE(y); |
| 8908 | if (!PyUnicode_Check(x)) { |
| 8909 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 8910 | "be a string if there is a second argument"); |
| 8911 | goto err; |
| 8912 | } |
| 8913 | if (PyUnicode_GET_SIZE(x) != ylen) { |
| 8914 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 8915 | "arguments must have equal length"); |
| 8916 | goto err; |
| 8917 | } |
| 8918 | /* create entries for translating chars in x to those in y */ |
| 8919 | for (i = 0; i < PyUnicode_GET_SIZE(x); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8920 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(x)[i]); |
| 8921 | value = PyLong_FromLong(PyUnicode_AS_UNICODE(y)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8922 | if (!key || !value) |
| 8923 | goto err; |
| 8924 | res = PyDict_SetItem(new, key, value); |
| 8925 | Py_DECREF(key); |
| 8926 | Py_DECREF(value); |
| 8927 | if (res < 0) |
| 8928 | goto err; |
| 8929 | } |
| 8930 | /* create entries for deleting chars in z */ |
| 8931 | if (z != NULL) { |
| 8932 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8933 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(z)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8934 | if (!key) |
| 8935 | goto err; |
| 8936 | res = PyDict_SetItem(new, key, Py_None); |
| 8937 | Py_DECREF(key); |
| 8938 | if (res < 0) |
| 8939 | goto err; |
| 8940 | } |
| 8941 | } |
| 8942 | } else { |
| 8943 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 8944 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8945 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 8946 | "to maketrans it must be a dict"); |
| 8947 | goto err; |
| 8948 | } |
| 8949 | /* copy entries into the new dict, converting string keys to int keys */ |
| 8950 | while (PyDict_Next(x, &i, &key, &value)) { |
| 8951 | if (PyUnicode_Check(key)) { |
| 8952 | /* convert string keys to integer keys */ |
| 8953 | PyObject *newkey; |
| 8954 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 8955 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 8956 | "table must be of length 1"); |
| 8957 | goto err; |
| 8958 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8959 | newkey = PyLong_FromLong(PyUnicode_AS_UNICODE(key)[0]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8960 | if (!newkey) |
| 8961 | goto err; |
| 8962 | res = PyDict_SetItem(new, newkey, value); |
| 8963 | Py_DECREF(newkey); |
| 8964 | if (res < 0) |
| 8965 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8966 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8967 | /* just keep integer keys */ |
| 8968 | if (PyDict_SetItem(new, key, value) < 0) |
| 8969 | goto err; |
| 8970 | } else { |
| 8971 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 8972 | "be strings or integers"); |
| 8973 | goto err; |
| 8974 | } |
| 8975 | } |
| 8976 | } |
| 8977 | return new; |
| 8978 | err: |
| 8979 | Py_DECREF(new); |
| 8980 | return NULL; |
| 8981 | } |
| 8982 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8983 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8984 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8985 | \n\ |
| 8986 | Return a copy of the string S, where all characters have been mapped\n\ |
| 8987 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8988 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 8989 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 8990 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8991 | |
| 8992 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8993 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8994 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8995 | return PyUnicode_TranslateCharmap(self->str, self->length, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8996 | } |
| 8997 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8998 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8999 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9000 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9001 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9002 | |
| 9003 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9004 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9005 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9006 | return fixup(self, fixupper); |
| 9007 | } |
| 9008 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9009 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9010 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9011 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 9012 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 9013 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9014 | |
| 9015 | static PyObject * |
| 9016 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 9017 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9018 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9019 | PyUnicodeObject *u; |
| 9020 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9021 | Py_ssize_t width; |
| 9022 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9023 | return NULL; |
| 9024 | |
| 9025 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 9026 | if (PyUnicode_CheckExact(self)) { |
| 9027 | Py_INCREF(self); |
| 9028 | return (PyObject*) self; |
| 9029 | } |
| 9030 | else |
| 9031 | return PyUnicode_FromUnicode( |
| 9032 | PyUnicode_AS_UNICODE(self), |
| 9033 | PyUnicode_GET_SIZE(self) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9034 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9035 | } |
| 9036 | |
| 9037 | fill = width - self->length; |
| 9038 | |
| 9039 | u = pad(self, fill, 0, '0'); |
| 9040 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 9041 | if (u == NULL) |
| 9042 | return NULL; |
| 9043 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9044 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 9045 | /* move sign to beginning of string */ |
| 9046 | u->str[0] = u->str[fill]; |
| 9047 | u->str[fill] = '0'; |
| 9048 | } |
| 9049 | |
| 9050 | return (PyObject*) u; |
| 9051 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9052 | |
| 9053 | #if 0 |
| 9054 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9055 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9056 | { |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 9057 | return PyLong_FromLong(numfree); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9058 | } |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9059 | |
| 9060 | static PyObject * |
| 9061 | unicode__decimal2ascii(PyObject *self) |
| 9062 | { |
| 9063 | return PyUnicode_TransformDecimalToASCII(PyUnicode_AS_UNICODE(self), |
| 9064 | PyUnicode_GET_SIZE(self)); |
| 9065 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9066 | #endif |
| 9067 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9068 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9069 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9070 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 9071 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 9072 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9073 | With optional end, stop comparing S at that position.\n\ |
| 9074 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9075 | |
| 9076 | static PyObject * |
| 9077 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9078 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9079 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9080 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9081 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9082 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9083 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9084 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9085 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9086 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9087 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 9088 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9089 | if (PyTuple_Check(subobj)) { |
| 9090 | Py_ssize_t i; |
| 9091 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 9092 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9093 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9094 | if (substring == NULL) |
| 9095 | return NULL; |
| 9096 | result = tailmatch(self, substring, start, end, -1); |
| 9097 | Py_DECREF(substring); |
| 9098 | if (result) { |
| 9099 | Py_RETURN_TRUE; |
| 9100 | } |
| 9101 | } |
| 9102 | /* nothing matched */ |
| 9103 | Py_RETURN_FALSE; |
| 9104 | } |
| 9105 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9106 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9107 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9108 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9109 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9110 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9111 | } |
| 9112 | |
| 9113 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9114 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9115 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9116 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 9117 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 9118 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9119 | With optional end, stop comparing S at that position.\n\ |
| 9120 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9121 | |
| 9122 | static PyObject * |
| 9123 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9124 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9125 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9126 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9127 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9128 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9129 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9130 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9131 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9132 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9133 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 9134 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9135 | if (PyTuple_Check(subobj)) { |
| 9136 | Py_ssize_t i; |
| 9137 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 9138 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9139 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9140 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9141 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9142 | result = tailmatch(self, substring, start, end, +1); |
| 9143 | Py_DECREF(substring); |
| 9144 | if (result) { |
| 9145 | Py_RETURN_TRUE; |
| 9146 | } |
| 9147 | } |
| 9148 | Py_RETURN_FALSE; |
| 9149 | } |
| 9150 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9151 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9152 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9153 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9154 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9155 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9156 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9157 | } |
| 9158 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 9159 | #include "stringlib/string_format.h" |
| 9160 | |
| 9161 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9162 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 9163 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 9164 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 9165 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 9166 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 9167 | PyDoc_STRVAR(format_map__doc__, |
| 9168 | "S.format_map(mapping) -> str\n\ |
| 9169 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 9170 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 9171 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 9172 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 9173 | static PyObject * |
| 9174 | unicode__format__(PyObject* self, PyObject* args) |
| 9175 | { |
| 9176 | PyObject *format_spec; |
| 9177 | |
| 9178 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 9179 | return NULL; |
| 9180 | |
| 9181 | return _PyUnicode_FormatAdvanced(self, |
| 9182 | PyUnicode_AS_UNICODE(format_spec), |
| 9183 | PyUnicode_GET_SIZE(format_spec)); |
| 9184 | } |
| 9185 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 9186 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9187 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 9188 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 9189 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 9190 | |
| 9191 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 9192 | unicode__sizeof__(PyUnicodeObject *v) |
| 9193 | { |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 9194 | return PyLong_FromSsize_t(sizeof(PyUnicodeObject) + |
| 9195 | sizeof(Py_UNICODE) * (v->length + 1)); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 9196 | } |
| 9197 | |
| 9198 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9199 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 9200 | |
| 9201 | static PyObject * |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 9202 | unicode_getnewargs(PyUnicodeObject *v) |
| 9203 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9204 | return Py_BuildValue("(u#)", v->str, v->length); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 9205 | } |
| 9206 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9207 | static PyMethodDef unicode_methods[] = { |
| 9208 | |
| 9209 | /* Order is according to common usage: often used methods should |
| 9210 | appear first, since lookup is done sequentially. */ |
| 9211 | |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 9212 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9213 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 9214 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9215 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9216 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 9217 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 9218 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 9219 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 9220 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 9221 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 9222 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9223 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9224 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 9225 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 9226 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 9227 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9228 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 9229 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 9230 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 9231 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9232 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9233 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 9234 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9235 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 9236 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 9237 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 9238 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 9239 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 9240 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 9241 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 9242 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 9243 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 9244 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 9245 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 9246 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 9247 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 9248 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 9249 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 9250 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9251 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 9252 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 9253 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 9254 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 9255 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 9256 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 9257 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 9258 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9259 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9260 | #endif |
| 9261 | |
| 9262 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9263 | /* These methods are just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9264 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9265 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9266 | #endif |
| 9267 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9268 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9269 | {NULL, NULL} |
| 9270 | }; |
| 9271 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 9272 | static PyObject * |
| 9273 | unicode_mod(PyObject *v, PyObject *w) |
| 9274 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9275 | if (!PyUnicode_Check(v)) { |
| 9276 | Py_INCREF(Py_NotImplemented); |
| 9277 | return Py_NotImplemented; |
| 9278 | } |
| 9279 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 9280 | } |
| 9281 | |
| 9282 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9283 | 0, /*nb_add*/ |
| 9284 | 0, /*nb_subtract*/ |
| 9285 | 0, /*nb_multiply*/ |
| 9286 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 9287 | }; |
| 9288 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9289 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9290 | (lenfunc) unicode_length, /* sq_length */ |
| 9291 | PyUnicode_Concat, /* sq_concat */ |
| 9292 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 9293 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 9294 | 0, /* sq_slice */ |
| 9295 | 0, /* sq_ass_item */ |
| 9296 | 0, /* sq_ass_slice */ |
| 9297 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9298 | }; |
| 9299 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9300 | static PyObject* |
| 9301 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 9302 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9303 | if (PyIndex_Check(item)) { |
| 9304 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9305 | if (i == -1 && PyErr_Occurred()) |
| 9306 | return NULL; |
| 9307 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 9308 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9309 | return unicode_getitem(self, i); |
| 9310 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9311 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9312 | Py_UNICODE* source_buf; |
| 9313 | Py_UNICODE* result_buf; |
| 9314 | PyObject* result; |
| 9315 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 9316 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_SIZE(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9317 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9318 | return NULL; |
| 9319 | } |
| 9320 | |
| 9321 | if (slicelength <= 0) { |
| 9322 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 9323 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 9324 | PyUnicode_CheckExact(self)) { |
| 9325 | Py_INCREF(self); |
| 9326 | return (PyObject *)self; |
| 9327 | } else if (step == 1) { |
| 9328 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9329 | } else { |
| 9330 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 9331 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 9332 | sizeof(Py_UNICODE)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9333 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9334 | if (result_buf == NULL) |
| 9335 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9336 | |
| 9337 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 9338 | result_buf[i] = source_buf[cur]; |
| 9339 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9340 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9341 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 9342 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9343 | return result; |
| 9344 | } |
| 9345 | } else { |
| 9346 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 9347 | return NULL; |
| 9348 | } |
| 9349 | } |
| 9350 | |
| 9351 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9352 | (lenfunc)unicode_length, /* mp_length */ |
| 9353 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 9354 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9355 | }; |
| 9356 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9357 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9358 | /* Helpers for PyUnicode_Format() */ |
| 9359 | |
| 9360 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9361 | 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] | 9362 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9363 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9364 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9365 | (*p_argidx)++; |
| 9366 | if (arglen < 0) |
| 9367 | return args; |
| 9368 | else |
| 9369 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9370 | } |
| 9371 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9372 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9373 | return NULL; |
| 9374 | } |
| 9375 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9376 | /* 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] | 9377 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9378 | static PyObject * |
| 9379 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9380 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9381 | char *p; |
| 9382 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9383 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9384 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9385 | x = PyFloat_AsDouble(v); |
| 9386 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9387 | return NULL; |
| 9388 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9389 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9390 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 9391 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 9392 | p = PyOS_double_to_string(x, type, prec, |
| 9393 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9394 | if (p == NULL) |
| 9395 | return NULL; |
| 9396 | result = PyUnicode_FromStringAndSize(p, strlen(p)); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 9397 | PyMem_Free(p); |
| 9398 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9399 | } |
| 9400 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9401 | static PyObject* |
| 9402 | formatlong(PyObject *val, int flags, int prec, int type) |
| 9403 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9404 | char *buf; |
| 9405 | int len; |
| 9406 | PyObject *str; /* temporary string object. */ |
| 9407 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9408 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9409 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 9410 | if (!str) |
| 9411 | return NULL; |
| 9412 | result = PyUnicode_FromStringAndSize(buf, len); |
| 9413 | Py_DECREF(str); |
| 9414 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9415 | } |
| 9416 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9417 | static int |
| 9418 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9419 | size_t buflen, |
| 9420 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9421 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 9422 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9423 | if (PyUnicode_Check(v)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9424 | if (PyUnicode_GET_SIZE(v) == 1) { |
| 9425 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
| 9426 | buf[1] = '\0'; |
| 9427 | return 1; |
| 9428 | } |
| 9429 | #ifndef Py_UNICODE_WIDE |
| 9430 | if (PyUnicode_GET_SIZE(v) == 2) { |
| 9431 | /* Decode a valid surrogate pair */ |
| 9432 | int c0 = PyUnicode_AS_UNICODE(v)[0]; |
| 9433 | int c1 = PyUnicode_AS_UNICODE(v)[1]; |
| 9434 | if (0xD800 <= c0 && c0 <= 0xDBFF && |
| 9435 | 0xDC00 <= c1 && c1 <= 0xDFFF) { |
| 9436 | buf[0] = c0; |
| 9437 | buf[1] = c1; |
| 9438 | buf[2] = '\0'; |
| 9439 | return 2; |
| 9440 | } |
| 9441 | } |
| 9442 | #endif |
| 9443 | goto onError; |
| 9444 | } |
| 9445 | else { |
| 9446 | /* Integer input truncated to a character */ |
| 9447 | long x; |
| 9448 | x = PyLong_AsLong(v); |
| 9449 | if (x == -1 && PyErr_Occurred()) |
| 9450 | goto onError; |
| 9451 | |
| 9452 | if (x < 0 || x > 0x10ffff) { |
| 9453 | PyErr_SetString(PyExc_OverflowError, |
| 9454 | "%c arg not in range(0x110000)"); |
| 9455 | return -1; |
| 9456 | } |
| 9457 | |
| 9458 | #ifndef Py_UNICODE_WIDE |
| 9459 | if (x > 0xffff) { |
| 9460 | x -= 0x10000; |
| 9461 | buf[0] = (Py_UNICODE)(0xD800 | (x >> 10)); |
| 9462 | buf[1] = (Py_UNICODE)(0xDC00 | (x & 0x3FF)); |
| 9463 | return 2; |
| 9464 | } |
| 9465 | #endif |
| 9466 | buf[0] = (Py_UNICODE) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9467 | buf[1] = '\0'; |
| 9468 | return 1; |
| 9469 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 9470 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9471 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9472 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9473 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9474 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9475 | } |
| 9476 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9477 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9478 | 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] | 9479 | */ |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9480 | #define FORMATBUFLEN (size_t)10 |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9481 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9482 | PyObject * |
| 9483 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9484 | { |
| 9485 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9486 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9487 | int args_owned = 0; |
| 9488 | PyUnicodeObject *result = NULL; |
| 9489 | PyObject *dict = NULL; |
| 9490 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9491 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9492 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9493 | PyErr_BadInternalCall(); |
| 9494 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9495 | } |
| 9496 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9497 | if (uformat == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9498 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9499 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 9500 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 9501 | |
| 9502 | reslen = rescnt = fmtcnt + 100; |
| 9503 | result = _PyUnicode_New(reslen); |
| 9504 | if (result == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9505 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9506 | res = PyUnicode_AS_UNICODE(result); |
| 9507 | |
| 9508 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9509 | arglen = PyTuple_Size(args); |
| 9510 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9511 | } |
| 9512 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9513 | arglen = -1; |
| 9514 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9515 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 9516 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 9517 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9518 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9519 | |
| 9520 | while (--fmtcnt >= 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9521 | if (*fmt != '%') { |
| 9522 | if (--rescnt < 0) { |
| 9523 | rescnt = fmtcnt + 100; |
| 9524 | reslen += rescnt; |
| 9525 | if (_PyUnicode_Resize(&result, reslen) < 0) |
| 9526 | goto onError; |
| 9527 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 9528 | --rescnt; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9529 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9530 | *res++ = *fmt++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9531 | } |
| 9532 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9533 | /* Got a format specifier */ |
| 9534 | int flags = 0; |
| 9535 | Py_ssize_t width = -1; |
| 9536 | int prec = -1; |
| 9537 | Py_UNICODE c = '\0'; |
| 9538 | Py_UNICODE fill; |
| 9539 | int isnumok; |
| 9540 | PyObject *v = NULL; |
| 9541 | PyObject *temp = NULL; |
| 9542 | Py_UNICODE *pbuf; |
| 9543 | Py_UNICODE sign; |
| 9544 | Py_ssize_t len; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9545 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For formatchar() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9546 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9547 | fmt++; |
| 9548 | if (*fmt == '(') { |
| 9549 | Py_UNICODE *keystart; |
| 9550 | Py_ssize_t keylen; |
| 9551 | PyObject *key; |
| 9552 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 9553 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9554 | if (dict == NULL) { |
| 9555 | PyErr_SetString(PyExc_TypeError, |
| 9556 | "format requires a mapping"); |
| 9557 | goto onError; |
| 9558 | } |
| 9559 | ++fmt; |
| 9560 | --fmtcnt; |
| 9561 | keystart = fmt; |
| 9562 | /* Skip over balanced parentheses */ |
| 9563 | while (pcount > 0 && --fmtcnt >= 0) { |
| 9564 | if (*fmt == ')') |
| 9565 | --pcount; |
| 9566 | else if (*fmt == '(') |
| 9567 | ++pcount; |
| 9568 | fmt++; |
| 9569 | } |
| 9570 | keylen = fmt - keystart - 1; |
| 9571 | if (fmtcnt < 0 || pcount > 0) { |
| 9572 | PyErr_SetString(PyExc_ValueError, |
| 9573 | "incomplete format key"); |
| 9574 | goto onError; |
| 9575 | } |
| 9576 | #if 0 |
| 9577 | /* keys are converted to strings using UTF-8 and |
| 9578 | then looked up since Python uses strings to hold |
| 9579 | variables names etc. in its namespaces and we |
| 9580 | wouldn't want to break common idioms. */ |
| 9581 | key = PyUnicode_EncodeUTF8(keystart, |
| 9582 | keylen, |
| 9583 | NULL); |
| 9584 | #else |
| 9585 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 9586 | #endif |
| 9587 | if (key == NULL) |
| 9588 | goto onError; |
| 9589 | if (args_owned) { |
| 9590 | Py_DECREF(args); |
| 9591 | args_owned = 0; |
| 9592 | } |
| 9593 | args = PyObject_GetItem(dict, key); |
| 9594 | Py_DECREF(key); |
| 9595 | if (args == NULL) { |
| 9596 | goto onError; |
| 9597 | } |
| 9598 | args_owned = 1; |
| 9599 | arglen = -1; |
| 9600 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9601 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9602 | while (--fmtcnt >= 0) { |
| 9603 | switch (c = *fmt++) { |
| 9604 | case '-': flags |= F_LJUST; continue; |
| 9605 | case '+': flags |= F_SIGN; continue; |
| 9606 | case ' ': flags |= F_BLANK; continue; |
| 9607 | case '#': flags |= F_ALT; continue; |
| 9608 | case '0': flags |= F_ZERO; continue; |
| 9609 | } |
| 9610 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9611 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9612 | if (c == '*') { |
| 9613 | v = getnextarg(args, arglen, &argidx); |
| 9614 | if (v == NULL) |
| 9615 | goto onError; |
| 9616 | if (!PyLong_Check(v)) { |
| 9617 | PyErr_SetString(PyExc_TypeError, |
| 9618 | "* wants int"); |
| 9619 | goto onError; |
| 9620 | } |
| 9621 | width = PyLong_AsLong(v); |
| 9622 | if (width == -1 && PyErr_Occurred()) |
| 9623 | goto onError; |
| 9624 | if (width < 0) { |
| 9625 | flags |= F_LJUST; |
| 9626 | width = -width; |
| 9627 | } |
| 9628 | if (--fmtcnt >= 0) |
| 9629 | c = *fmt++; |
| 9630 | } |
| 9631 | else if (c >= '0' && c <= '9') { |
| 9632 | width = c - '0'; |
| 9633 | while (--fmtcnt >= 0) { |
| 9634 | c = *fmt++; |
| 9635 | if (c < '0' || c > '9') |
| 9636 | break; |
| 9637 | if ((width*10) / 10 != width) { |
| 9638 | PyErr_SetString(PyExc_ValueError, |
| 9639 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9640 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9641 | } |
| 9642 | width = width*10 + (c - '0'); |
| 9643 | } |
| 9644 | } |
| 9645 | if (c == '.') { |
| 9646 | prec = 0; |
| 9647 | if (--fmtcnt >= 0) |
| 9648 | c = *fmt++; |
| 9649 | if (c == '*') { |
| 9650 | v = getnextarg(args, arglen, &argidx); |
| 9651 | if (v == NULL) |
| 9652 | goto onError; |
| 9653 | if (!PyLong_Check(v)) { |
| 9654 | PyErr_SetString(PyExc_TypeError, |
| 9655 | "* wants int"); |
| 9656 | goto onError; |
| 9657 | } |
| 9658 | prec = PyLong_AsLong(v); |
| 9659 | if (prec == -1 && PyErr_Occurred()) |
| 9660 | goto onError; |
| 9661 | if (prec < 0) |
| 9662 | prec = 0; |
| 9663 | if (--fmtcnt >= 0) |
| 9664 | c = *fmt++; |
| 9665 | } |
| 9666 | else if (c >= '0' && c <= '9') { |
| 9667 | prec = c - '0'; |
| 9668 | while (--fmtcnt >= 0) { |
Stefan Krah | 99212f6 | 2010-07-19 17:58:26 +0000 | [diff] [blame] | 9669 | c = *fmt++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9670 | if (c < '0' || c > '9') |
| 9671 | break; |
| 9672 | if ((prec*10) / 10 != prec) { |
| 9673 | PyErr_SetString(PyExc_ValueError, |
| 9674 | "prec too big"); |
| 9675 | goto onError; |
| 9676 | } |
| 9677 | prec = prec*10 + (c - '0'); |
| 9678 | } |
| 9679 | } |
| 9680 | } /* prec */ |
| 9681 | if (fmtcnt >= 0) { |
| 9682 | if (c == 'h' || c == 'l' || c == 'L') { |
| 9683 | if (--fmtcnt >= 0) |
| 9684 | c = *fmt++; |
| 9685 | } |
| 9686 | } |
| 9687 | if (fmtcnt < 0) { |
| 9688 | PyErr_SetString(PyExc_ValueError, |
| 9689 | "incomplete format"); |
| 9690 | goto onError; |
| 9691 | } |
| 9692 | if (c != '%') { |
| 9693 | v = getnextarg(args, arglen, &argidx); |
| 9694 | if (v == NULL) |
| 9695 | goto onError; |
| 9696 | } |
| 9697 | sign = 0; |
| 9698 | fill = ' '; |
| 9699 | switch (c) { |
| 9700 | |
| 9701 | case '%': |
| 9702 | pbuf = formatbuf; |
| 9703 | /* presume that buffer length is at least 1 */ |
| 9704 | pbuf[0] = '%'; |
| 9705 | len = 1; |
| 9706 | break; |
| 9707 | |
| 9708 | case 's': |
| 9709 | case 'r': |
| 9710 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 9711 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9712 | temp = v; |
| 9713 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9714 | } |
| 9715 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9716 | if (c == 's') |
| 9717 | temp = PyObject_Str(v); |
| 9718 | else if (c == 'r') |
| 9719 | temp = PyObject_Repr(v); |
| 9720 | else |
| 9721 | temp = PyObject_ASCII(v); |
| 9722 | if (temp == NULL) |
| 9723 | goto onError; |
| 9724 | if (PyUnicode_Check(temp)) |
| 9725 | /* nothing to do */; |
| 9726 | else { |
| 9727 | Py_DECREF(temp); |
| 9728 | PyErr_SetString(PyExc_TypeError, |
| 9729 | "%s argument has non-string str()"); |
| 9730 | goto onError; |
| 9731 | } |
| 9732 | } |
| 9733 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9734 | len = PyUnicode_GET_SIZE(temp); |
| 9735 | if (prec >= 0 && len > prec) |
| 9736 | len = prec; |
| 9737 | break; |
| 9738 | |
| 9739 | case 'i': |
| 9740 | case 'd': |
| 9741 | case 'u': |
| 9742 | case 'o': |
| 9743 | case 'x': |
| 9744 | case 'X': |
| 9745 | if (c == 'i') |
| 9746 | c = 'd'; |
| 9747 | isnumok = 0; |
| 9748 | if (PyNumber_Check(v)) { |
| 9749 | PyObject *iobj=NULL; |
| 9750 | |
| 9751 | if (PyLong_Check(v)) { |
| 9752 | iobj = v; |
| 9753 | Py_INCREF(iobj); |
| 9754 | } |
| 9755 | else { |
| 9756 | iobj = PyNumber_Long(v); |
| 9757 | } |
| 9758 | if (iobj!=NULL) { |
| 9759 | if (PyLong_Check(iobj)) { |
| 9760 | isnumok = 1; |
| 9761 | temp = formatlong(iobj, flags, prec, c); |
| 9762 | Py_DECREF(iobj); |
| 9763 | if (!temp) |
| 9764 | goto onError; |
| 9765 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9766 | len = PyUnicode_GET_SIZE(temp); |
| 9767 | sign = 1; |
| 9768 | } |
| 9769 | else { |
| 9770 | Py_DECREF(iobj); |
| 9771 | } |
| 9772 | } |
| 9773 | } |
| 9774 | if (!isnumok) { |
| 9775 | PyErr_Format(PyExc_TypeError, |
| 9776 | "%%%c format: a number is required, " |
| 9777 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 9778 | goto onError; |
| 9779 | } |
| 9780 | if (flags & F_ZERO) |
| 9781 | fill = '0'; |
| 9782 | break; |
| 9783 | |
| 9784 | case 'e': |
| 9785 | case 'E': |
| 9786 | case 'f': |
| 9787 | case 'F': |
| 9788 | case 'g': |
| 9789 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9790 | temp = formatfloat(v, flags, prec, c); |
| 9791 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9792 | goto onError; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9793 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9794 | len = PyUnicode_GET_SIZE(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9795 | sign = 1; |
| 9796 | if (flags & F_ZERO) |
| 9797 | fill = '0'; |
| 9798 | break; |
| 9799 | |
| 9800 | case 'c': |
| 9801 | pbuf = formatbuf; |
| 9802 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
| 9803 | if (len < 0) |
| 9804 | goto onError; |
| 9805 | break; |
| 9806 | |
| 9807 | default: |
| 9808 | PyErr_Format(PyExc_ValueError, |
| 9809 | "unsupported format character '%c' (0x%x) " |
| 9810 | "at index %zd", |
| 9811 | (31<=c && c<=126) ? (char)c : '?', |
| 9812 | (int)c, |
| 9813 | (Py_ssize_t)(fmt - 1 - |
| 9814 | PyUnicode_AS_UNICODE(uformat))); |
| 9815 | goto onError; |
| 9816 | } |
| 9817 | if (sign) { |
| 9818 | if (*pbuf == '-' || *pbuf == '+') { |
| 9819 | sign = *pbuf++; |
| 9820 | len--; |
| 9821 | } |
| 9822 | else if (flags & F_SIGN) |
| 9823 | sign = '+'; |
| 9824 | else if (flags & F_BLANK) |
| 9825 | sign = ' '; |
| 9826 | else |
| 9827 | sign = 0; |
| 9828 | } |
| 9829 | if (width < len) |
| 9830 | width = len; |
| 9831 | if (rescnt - (sign != 0) < width) { |
| 9832 | reslen -= rescnt; |
| 9833 | rescnt = width + fmtcnt + 100; |
| 9834 | reslen += rescnt; |
| 9835 | if (reslen < 0) { |
| 9836 | Py_XDECREF(temp); |
| 9837 | PyErr_NoMemory(); |
| 9838 | goto onError; |
| 9839 | } |
| 9840 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 9841 | Py_XDECREF(temp); |
| 9842 | goto onError; |
| 9843 | } |
| 9844 | res = PyUnicode_AS_UNICODE(result) |
| 9845 | + reslen - rescnt; |
| 9846 | } |
| 9847 | if (sign) { |
| 9848 | if (fill != ' ') |
| 9849 | *res++ = sign; |
| 9850 | rescnt--; |
| 9851 | if (width > len) |
| 9852 | width--; |
| 9853 | } |
| 9854 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9855 | assert(pbuf[0] == '0'); |
| 9856 | assert(pbuf[1] == c); |
| 9857 | if (fill != ' ') { |
| 9858 | *res++ = *pbuf++; |
| 9859 | *res++ = *pbuf++; |
| 9860 | } |
| 9861 | rescnt -= 2; |
| 9862 | width -= 2; |
| 9863 | if (width < 0) |
| 9864 | width = 0; |
| 9865 | len -= 2; |
| 9866 | } |
| 9867 | if (width > len && !(flags & F_LJUST)) { |
| 9868 | do { |
| 9869 | --rescnt; |
| 9870 | *res++ = fill; |
| 9871 | } while (--width > len); |
| 9872 | } |
| 9873 | if (fill == ' ') { |
| 9874 | if (sign) |
| 9875 | *res++ = sign; |
| 9876 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9877 | assert(pbuf[0] == '0'); |
| 9878 | assert(pbuf[1] == c); |
| 9879 | *res++ = *pbuf++; |
| 9880 | *res++ = *pbuf++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9881 | } |
| 9882 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9883 | Py_UNICODE_COPY(res, pbuf, len); |
| 9884 | res += len; |
| 9885 | rescnt -= len; |
| 9886 | while (--width >= len) { |
| 9887 | --rescnt; |
| 9888 | *res++ = ' '; |
| 9889 | } |
| 9890 | if (dict && (argidx < arglen) && c != '%') { |
| 9891 | PyErr_SetString(PyExc_TypeError, |
| 9892 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9893 | Py_XDECREF(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9894 | goto onError; |
| 9895 | } |
| 9896 | Py_XDECREF(temp); |
| 9897 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9898 | } /* until end */ |
| 9899 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9900 | PyErr_SetString(PyExc_TypeError, |
| 9901 | "not all arguments converted during string formatting"); |
| 9902 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9903 | } |
| 9904 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9905 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9906 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9907 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9908 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9909 | } |
| 9910 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9911 | return (PyObject *)result; |
| 9912 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9913 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9914 | Py_XDECREF(result); |
| 9915 | Py_DECREF(uformat); |
| 9916 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9917 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9918 | } |
| 9919 | return NULL; |
| 9920 | } |
| 9921 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 9922 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9923 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 9924 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9925 | static PyObject * |
| 9926 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9927 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9928 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9929 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 9930 | char *encoding = NULL; |
| 9931 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9932 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9933 | if (type != &PyUnicode_Type) |
| 9934 | return unicode_subtype_new(type, args, kwds); |
| 9935 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9936 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9937 | return NULL; |
| 9938 | if (x == NULL) |
| 9939 | return (PyObject *)_PyUnicode_New(0); |
| 9940 | if (encoding == NULL && errors == NULL) |
| 9941 | return PyObject_Str(x); |
| 9942 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9943 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9944 | } |
| 9945 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9946 | static PyObject * |
| 9947 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9948 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9949 | PyUnicodeObject *tmp, *pnew; |
| 9950 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9951 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9952 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 9953 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 9954 | if (tmp == NULL) |
| 9955 | return NULL; |
| 9956 | assert(PyUnicode_Check(tmp)); |
| 9957 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
| 9958 | if (pnew == NULL) { |
| 9959 | Py_DECREF(tmp); |
| 9960 | return NULL; |
| 9961 | } |
| 9962 | pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
| 9963 | if (pnew->str == NULL) { |
| 9964 | _Py_ForgetReference((PyObject *)pnew); |
| 9965 | PyObject_Del(pnew); |
| 9966 | Py_DECREF(tmp); |
| 9967 | return PyErr_NoMemory(); |
| 9968 | } |
| 9969 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 9970 | pnew->length = n; |
| 9971 | pnew->hash = tmp->hash; |
| 9972 | Py_DECREF(tmp); |
| 9973 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9974 | } |
| 9975 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9976 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9977 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9978 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 9979 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 9980 | encoding defaults to the current default string encoding.\n\ |
| 9981 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9982 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9983 | static PyObject *unicode_iter(PyObject *seq); |
| 9984 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9985 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9986 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9987 | "str", /* tp_name */ |
| 9988 | sizeof(PyUnicodeObject), /* tp_size */ |
| 9989 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9990 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9991 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 9992 | 0, /* tp_print */ |
| 9993 | 0, /* tp_getattr */ |
| 9994 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 9995 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9996 | unicode_repr, /* tp_repr */ |
| 9997 | &unicode_as_number, /* tp_as_number */ |
| 9998 | &unicode_as_sequence, /* tp_as_sequence */ |
| 9999 | &unicode_as_mapping, /* tp_as_mapping */ |
| 10000 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 10001 | 0, /* tp_call*/ |
| 10002 | (reprfunc) unicode_str, /* tp_str */ |
| 10003 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 10004 | 0, /* tp_setattro */ |
| 10005 | 0, /* tp_as_buffer */ |
| 10006 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10007 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10008 | unicode_doc, /* tp_doc */ |
| 10009 | 0, /* tp_traverse */ |
| 10010 | 0, /* tp_clear */ |
| 10011 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 10012 | 0, /* tp_weaklistoffset */ |
| 10013 | unicode_iter, /* tp_iter */ |
| 10014 | 0, /* tp_iternext */ |
| 10015 | unicode_methods, /* tp_methods */ |
| 10016 | 0, /* tp_members */ |
| 10017 | 0, /* tp_getset */ |
| 10018 | &PyBaseObject_Type, /* tp_base */ |
| 10019 | 0, /* tp_dict */ |
| 10020 | 0, /* tp_descr_get */ |
| 10021 | 0, /* tp_descr_set */ |
| 10022 | 0, /* tp_dictoffset */ |
| 10023 | 0, /* tp_init */ |
| 10024 | 0, /* tp_alloc */ |
| 10025 | unicode_new, /* tp_new */ |
| 10026 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10027 | }; |
| 10028 | |
| 10029 | /* Initialize the Unicode implementation */ |
| 10030 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 10031 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10032 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 10033 | int i; |
| 10034 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10035 | /* XXX - move this array to unicodectype.c ? */ |
| 10036 | Py_UNICODE linebreak[] = { |
| 10037 | 0x000A, /* LINE FEED */ |
| 10038 | 0x000D, /* CARRIAGE RETURN */ |
| 10039 | 0x001C, /* FILE SEPARATOR */ |
| 10040 | 0x001D, /* GROUP SEPARATOR */ |
| 10041 | 0x001E, /* RECORD SEPARATOR */ |
| 10042 | 0x0085, /* NEXT LINE */ |
| 10043 | 0x2028, /* LINE SEPARATOR */ |
| 10044 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 10045 | }; |
| 10046 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 10047 | /* Init the implementation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 10048 | free_list = NULL; |
| 10049 | numfree = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10050 | unicode_empty = _PyUnicode_New(0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 10051 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10052 | return; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 10053 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 10054 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10055 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 10056 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10057 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10058 | |
| 10059 | /* initialize the linebreak bloom filter */ |
| 10060 | bloom_linebreak = make_bloom_mask( |
| 10061 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 10062 | ); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 10063 | |
| 10064 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10065 | } |
| 10066 | |
| 10067 | /* Finalize the Unicode implementation */ |
| 10068 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 10069 | int |
| 10070 | PyUnicode_ClearFreeList(void) |
| 10071 | { |
| 10072 | int freelist_size = numfree; |
| 10073 | PyUnicodeObject *u; |
| 10074 | |
| 10075 | for (u = free_list; u != NULL;) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10076 | PyUnicodeObject *v = u; |
| 10077 | u = *(PyUnicodeObject **)u; |
| 10078 | if (v->str) |
| 10079 | PyObject_DEL(v->str); |
| 10080 | Py_XDECREF(v->defenc); |
| 10081 | PyObject_Del(v); |
| 10082 | numfree--; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 10083 | } |
| 10084 | free_list = NULL; |
| 10085 | assert(numfree == 0); |
| 10086 | return freelist_size; |
| 10087 | } |
| 10088 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10089 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 10090 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10091 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 10092 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10093 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 10094 | Py_XDECREF(unicode_empty); |
| 10095 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 10096 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 10097 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10098 | if (unicode_latin1[i]) { |
| 10099 | Py_DECREF(unicode_latin1[i]); |
| 10100 | unicode_latin1[i] = NULL; |
| 10101 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 10102 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 10103 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10104 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 10105 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 10106 | void |
| 10107 | PyUnicode_InternInPlace(PyObject **p) |
| 10108 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10109 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 10110 | PyObject *t; |
| 10111 | if (s == NULL || !PyUnicode_Check(s)) |
| 10112 | Py_FatalError( |
| 10113 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 10114 | /* If it's a subclass, we don't really know what putting |
| 10115 | it in the interned dict might do. */ |
| 10116 | if (!PyUnicode_CheckExact(s)) |
| 10117 | return; |
| 10118 | if (PyUnicode_CHECK_INTERNED(s)) |
| 10119 | return; |
| 10120 | if (interned == NULL) { |
| 10121 | interned = PyDict_New(); |
| 10122 | if (interned == NULL) { |
| 10123 | PyErr_Clear(); /* Don't leave an exception */ |
| 10124 | return; |
| 10125 | } |
| 10126 | } |
| 10127 | /* It might be that the GetItem call fails even |
| 10128 | though the key is present in the dictionary, |
| 10129 | namely when this happens during a stack overflow. */ |
| 10130 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10131 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10132 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10133 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10134 | if (t) { |
| 10135 | Py_INCREF(t); |
| 10136 | Py_DECREF(*p); |
| 10137 | *p = t; |
| 10138 | return; |
| 10139 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 10140 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10141 | PyThreadState_GET()->recursion_critical = 1; |
| 10142 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 10143 | PyErr_Clear(); |
| 10144 | PyThreadState_GET()->recursion_critical = 0; |
| 10145 | return; |
| 10146 | } |
| 10147 | PyThreadState_GET()->recursion_critical = 0; |
| 10148 | /* The two references in interned are not counted by refcnt. |
| 10149 | The deallocator will take care of this */ |
| 10150 | Py_REFCNT(s) -= 2; |
| 10151 | PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 10152 | } |
| 10153 | |
| 10154 | void |
| 10155 | PyUnicode_InternImmortal(PyObject **p) |
| 10156 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10157 | PyUnicode_InternInPlace(p); |
| 10158 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 10159 | PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 10160 | Py_INCREF(*p); |
| 10161 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 10162 | } |
| 10163 | |
| 10164 | PyObject * |
| 10165 | PyUnicode_InternFromString(const char *cp) |
| 10166 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10167 | PyObject *s = PyUnicode_FromString(cp); |
| 10168 | if (s == NULL) |
| 10169 | return NULL; |
| 10170 | PyUnicode_InternInPlace(&s); |
| 10171 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 10172 | } |
| 10173 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10174 | void |
| 10175 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 10176 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10177 | PyObject *keys; |
| 10178 | PyUnicodeObject *s; |
| 10179 | Py_ssize_t i, n; |
| 10180 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 10181 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10182 | if (interned == NULL || !PyDict_Check(interned)) |
| 10183 | return; |
| 10184 | keys = PyDict_Keys(interned); |
| 10185 | if (keys == NULL || !PyList_Check(keys)) { |
| 10186 | PyErr_Clear(); |
| 10187 | return; |
| 10188 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 10189 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10190 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 10191 | detector, interned unicode strings are not forcibly deallocated; |
| 10192 | rather, we give them their stolen references back, and then clear |
| 10193 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 10194 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10195 | n = PyList_GET_SIZE(keys); |
| 10196 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10197 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10198 | for (i = 0; i < n; i++) { |
| 10199 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
| 10200 | switch (s->state) { |
| 10201 | case SSTATE_NOT_INTERNED: |
| 10202 | /* XXX Shouldn't happen */ |
| 10203 | break; |
| 10204 | case SSTATE_INTERNED_IMMORTAL: |
| 10205 | Py_REFCNT(s) += 1; |
| 10206 | immortal_size += s->length; |
| 10207 | break; |
| 10208 | case SSTATE_INTERNED_MORTAL: |
| 10209 | Py_REFCNT(s) += 2; |
| 10210 | mortal_size += s->length; |
| 10211 | break; |
| 10212 | default: |
| 10213 | Py_FatalError("Inconsistent interned string state."); |
| 10214 | } |
| 10215 | s->state = SSTATE_NOT_INTERNED; |
| 10216 | } |
| 10217 | fprintf(stderr, "total size of all interned strings: " |
| 10218 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 10219 | "mortal/immortal\n", mortal_size, immortal_size); |
| 10220 | Py_DECREF(keys); |
| 10221 | PyDict_Clear(interned); |
| 10222 | Py_DECREF(interned); |
| 10223 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 10224 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10225 | |
| 10226 | |
| 10227 | /********************* Unicode Iterator **************************/ |
| 10228 | |
| 10229 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10230 | PyObject_HEAD |
| 10231 | Py_ssize_t it_index; |
| 10232 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10233 | } unicodeiterobject; |
| 10234 | |
| 10235 | static void |
| 10236 | unicodeiter_dealloc(unicodeiterobject *it) |
| 10237 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10238 | _PyObject_GC_UNTRACK(it); |
| 10239 | Py_XDECREF(it->it_seq); |
| 10240 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10241 | } |
| 10242 | |
| 10243 | static int |
| 10244 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 10245 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10246 | Py_VISIT(it->it_seq); |
| 10247 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10248 | } |
| 10249 | |
| 10250 | static PyObject * |
| 10251 | unicodeiter_next(unicodeiterobject *it) |
| 10252 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10253 | PyUnicodeObject *seq; |
| 10254 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10255 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10256 | assert(it != NULL); |
| 10257 | seq = it->it_seq; |
| 10258 | if (seq == NULL) |
| 10259 | return NULL; |
| 10260 | assert(PyUnicode_Check(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10261 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10262 | if (it->it_index < PyUnicode_GET_SIZE(seq)) { |
| 10263 | item = PyUnicode_FromUnicode( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10264 | PyUnicode_AS_UNICODE(seq)+it->it_index, 1); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10265 | if (item != NULL) |
| 10266 | ++it->it_index; |
| 10267 | return item; |
| 10268 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10269 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10270 | Py_DECREF(seq); |
| 10271 | it->it_seq = NULL; |
| 10272 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10273 | } |
| 10274 | |
| 10275 | static PyObject * |
| 10276 | unicodeiter_len(unicodeiterobject *it) |
| 10277 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10278 | Py_ssize_t len = 0; |
| 10279 | if (it->it_seq) |
| 10280 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 10281 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10282 | } |
| 10283 | |
| 10284 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 10285 | |
| 10286 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10287 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10288 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10289 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10290 | }; |
| 10291 | |
| 10292 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10293 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 10294 | "str_iterator", /* tp_name */ |
| 10295 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 10296 | 0, /* tp_itemsize */ |
| 10297 | /* methods */ |
| 10298 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 10299 | 0, /* tp_print */ |
| 10300 | 0, /* tp_getattr */ |
| 10301 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 10302 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10303 | 0, /* tp_repr */ |
| 10304 | 0, /* tp_as_number */ |
| 10305 | 0, /* tp_as_sequence */ |
| 10306 | 0, /* tp_as_mapping */ |
| 10307 | 0, /* tp_hash */ |
| 10308 | 0, /* tp_call */ |
| 10309 | 0, /* tp_str */ |
| 10310 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 10311 | 0, /* tp_setattro */ |
| 10312 | 0, /* tp_as_buffer */ |
| 10313 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 10314 | 0, /* tp_doc */ |
| 10315 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 10316 | 0, /* tp_clear */ |
| 10317 | 0, /* tp_richcompare */ |
| 10318 | 0, /* tp_weaklistoffset */ |
| 10319 | PyObject_SelfIter, /* tp_iter */ |
| 10320 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 10321 | unicodeiter_methods, /* tp_methods */ |
| 10322 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10323 | }; |
| 10324 | |
| 10325 | static PyObject * |
| 10326 | unicode_iter(PyObject *seq) |
| 10327 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10328 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10329 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10330 | if (!PyUnicode_Check(seq)) { |
| 10331 | PyErr_BadInternalCall(); |
| 10332 | return NULL; |
| 10333 | } |
| 10334 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 10335 | if (it == NULL) |
| 10336 | return NULL; |
| 10337 | it->it_index = 0; |
| 10338 | Py_INCREF(seq); |
| 10339 | it->it_seq = (PyUnicodeObject *)seq; |
| 10340 | _PyObject_GC_TRACK(it); |
| 10341 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10342 | } |
| 10343 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10344 | size_t |
| 10345 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 10346 | { |
| 10347 | int res = 0; |
| 10348 | while(*u++) |
| 10349 | res++; |
| 10350 | return res; |
| 10351 | } |
| 10352 | |
| 10353 | Py_UNICODE* |
| 10354 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 10355 | { |
| 10356 | Py_UNICODE *u = s1; |
| 10357 | while ((*u++ = *s2++)); |
| 10358 | return s1; |
| 10359 | } |
| 10360 | |
| 10361 | Py_UNICODE* |
| 10362 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 10363 | { |
| 10364 | Py_UNICODE *u = s1; |
| 10365 | while ((*u++ = *s2++)) |
| 10366 | if (n-- == 0) |
| 10367 | break; |
| 10368 | return s1; |
| 10369 | } |
| 10370 | |
Victor Stinner | c4eb765 | 2010-09-01 23:43:50 +0000 | [diff] [blame] | 10371 | Py_UNICODE* |
| 10372 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 10373 | { |
| 10374 | Py_UNICODE *u1 = s1; |
| 10375 | u1 += Py_UNICODE_strlen(u1); |
| 10376 | Py_UNICODE_strcpy(u1, s2); |
| 10377 | return s1; |
| 10378 | } |
| 10379 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10380 | int |
| 10381 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 10382 | { |
| 10383 | while (*s1 && *s2 && *s1 == *s2) |
| 10384 | s1++, s2++; |
| 10385 | if (*s1 && *s2) |
| 10386 | return (*s1 < *s2) ? -1 : +1; |
| 10387 | if (*s1) |
| 10388 | return 1; |
| 10389 | if (*s2) |
| 10390 | return -1; |
| 10391 | return 0; |
| 10392 | } |
| 10393 | |
Victor Stinner | ef8d95c | 2010-08-16 22:03:11 +0000 | [diff] [blame] | 10394 | int |
| 10395 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 10396 | { |
| 10397 | register Py_UNICODE u1, u2; |
| 10398 | for (; n != 0; n--) { |
| 10399 | u1 = *s1; |
| 10400 | u2 = *s2; |
| 10401 | if (u1 != u2) |
| 10402 | return (u1 < u2) ? -1 : +1; |
| 10403 | if (u1 == '\0') |
| 10404 | return 0; |
| 10405 | s1++; |
| 10406 | s2++; |
| 10407 | } |
| 10408 | return 0; |
| 10409 | } |
| 10410 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10411 | Py_UNICODE* |
| 10412 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 10413 | { |
| 10414 | const Py_UNICODE *p; |
| 10415 | for (p = s; *p; p++) |
| 10416 | if (*p == c) |
| 10417 | return (Py_UNICODE*)p; |
| 10418 | return NULL; |
| 10419 | } |
| 10420 | |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 10421 | Py_UNICODE* |
| 10422 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 10423 | { |
| 10424 | const Py_UNICODE *p; |
| 10425 | p = s + Py_UNICODE_strlen(s); |
| 10426 | while (p != s) { |
| 10427 | p--; |
| 10428 | if (*p == c) |
| 10429 | return (Py_UNICODE*)p; |
| 10430 | } |
| 10431 | return NULL; |
| 10432 | } |
| 10433 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 10434 | Py_UNICODE* |
Victor Stinner | 4640860 | 2010-09-03 16:18:00 +0000 | [diff] [blame] | 10435 | PyUnicode_AsUnicodeCopy(PyObject *object) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 10436 | { |
| 10437 | PyUnicodeObject *unicode = (PyUnicodeObject *)object; |
| 10438 | Py_UNICODE *copy; |
| 10439 | Py_ssize_t size; |
| 10440 | |
| 10441 | /* Ensure we won't overflow the size. */ |
| 10442 | if (PyUnicode_GET_SIZE(unicode) > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 10443 | PyErr_NoMemory(); |
| 10444 | return NULL; |
| 10445 | } |
| 10446 | size = PyUnicode_GET_SIZE(unicode) + 1; /* copy the nul character */ |
| 10447 | size *= sizeof(Py_UNICODE); |
| 10448 | copy = PyMem_Malloc(size); |
| 10449 | if (copy == NULL) { |
| 10450 | PyErr_NoMemory(); |
| 10451 | return NULL; |
| 10452 | } |
| 10453 | memcpy(copy, PyUnicode_AS_UNICODE(unicode), size); |
| 10454 | return copy; |
| 10455 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10456 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 10457 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 10458 | to the string.Formatter class implemented in Python. */ |
| 10459 | |
| 10460 | static PyMethodDef _string_methods[] = { |
| 10461 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 10462 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 10463 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 10464 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 10465 | {NULL, NULL} |
| 10466 | }; |
| 10467 | |
| 10468 | static struct PyModuleDef _string_module = { |
| 10469 | PyModuleDef_HEAD_INIT, |
| 10470 | "_string", |
| 10471 | PyDoc_STR("string helper module"), |
| 10472 | 0, |
| 10473 | _string_methods, |
| 10474 | NULL, |
| 10475 | NULL, |
| 10476 | NULL, |
| 10477 | NULL |
| 10478 | }; |
| 10479 | |
| 10480 | PyMODINIT_FUNC |
| 10481 | PyInit__string(void) |
| 10482 | { |
| 10483 | return PyModule_Create(&_string_module); |
| 10484 | } |
| 10485 | |
| 10486 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10487 | #ifdef __cplusplus |
| 10488 | } |
| 10489 | #endif |