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 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +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 | 1c5d21d | 2009-01-31 22:33:02 +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 | |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +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" |
| 44 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 45 | #include "unicodeobject.h" |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 46 | #include "ucnhash.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 47 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 48 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 49 | #include <windows.h> |
| 50 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 52 | /* Limit for the Unicode object free list */ |
| 53 | |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 54 | #define PyUnicode_MAXFREELIST 1024 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 55 | |
| 56 | /* Limit for the Unicode object free list stay alive optimization. |
| 57 | |
| 58 | The implementation will keep allocated Unicode memory intact for |
| 59 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 60 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 61 | |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 62 | At worst this will result in PyUnicode_MAXFREELIST * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 63 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 64 | malloc()-overhead) bytes of unused garbage. |
| 65 | |
| 66 | Setting the limit to 0 effectively turns the feature off. |
| 67 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 68 | Note: This is an experimental feature ! If you get core dumps when |
| 69 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 70 | |
| 71 | */ |
| 72 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 73 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 74 | |
| 75 | /* Endianness switches; defaults to little endian */ |
| 76 | |
| 77 | #ifdef WORDS_BIGENDIAN |
| 78 | # define BYTEORDER_IS_BIG_ENDIAN |
| 79 | #else |
| 80 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 81 | #endif |
| 82 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 83 | /* --- Globals ------------------------------------------------------------ |
| 84 | |
| 85 | The globals are initialized by the _PyUnicode_Init() API and should |
| 86 | not be used before calling that API. |
| 87 | |
| 88 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 89 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 90 | |
| 91 | #ifdef __cplusplus |
| 92 | extern "C" { |
| 93 | #endif |
| 94 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 95 | /* Free list for Unicode objects */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 96 | static PyUnicodeObject *free_list; |
| 97 | static int numfree; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 98 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 99 | /* The empty Unicode object is shared to improve performance. */ |
| 100 | static PyUnicodeObject *unicode_empty; |
| 101 | |
| 102 | /* Single character Unicode strings in the Latin-1 range are being |
| 103 | shared as well. */ |
| 104 | static PyUnicodeObject *unicode_latin1[256]; |
| 105 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 106 | /* Default encoding to use and assume when NULL is passed as encoding |
| 107 | parameter; it is initialized by _PyUnicode_Init(). |
| 108 | |
| 109 | Always use the PyUnicode_SetDefaultEncoding() and |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 110 | PyUnicode_GetDefaultEncoding() APIs to access this global. |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 111 | |
| 112 | */ |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 113 | static char unicode_default_encoding[100]; |
| 114 | |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 115 | /* Fast detection of the most frequent whitespace characters */ |
| 116 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 117 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 32a66a0 | 2008-10-02 19:47:50 +0000 | [diff] [blame] | 118 | /* case 0x0009: * HORIZONTAL TABULATION */ |
| 119 | /* case 0x000A: * LINE FEED */ |
| 120 | /* case 0x000B: * VERTICAL TABULATION */ |
| 121 | /* case 0x000C: * FORM FEED */ |
| 122 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 123 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 124 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 32a66a0 | 2008-10-02 19:47:50 +0000 | [diff] [blame] | 125 | /* case 0x001C: * FILE SEPARATOR */ |
| 126 | /* case 0x001D: * GROUP SEPARATOR */ |
| 127 | /* case 0x001E: * RECORD SEPARATOR */ |
| 128 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 129 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 32a66a0 | 2008-10-02 19:47:50 +0000 | [diff] [blame] | 130 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 131 | 1, 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, |
| 134 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 135 | |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 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, |
| 143 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | /* Same for linebreaks */ |
| 147 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 148 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 32a66a0 | 2008-10-02 19:47:50 +0000 | [diff] [blame] | 149 | /* 0x000A, * LINE FEED */ |
| 150 | /* 0x000D, * CARRIAGE RETURN */ |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 151 | 0, 0, 1, 0, 0, 1, 0, 0, |
| 152 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 32a66a0 | 2008-10-02 19:47:50 +0000 | [diff] [blame] | 153 | /* 0x001C, * FILE SEPARATOR */ |
| 154 | /* 0x001D, * GROUP SEPARATOR */ |
| 155 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 156 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 157 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 158 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 159 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 160 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 161 | |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 162 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 163 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 164 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 165 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 166 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 167 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 168 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 169 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 173 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 174 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 175 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 176 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 177 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 178 | #else |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 179 | /* This is actually an illegal character, so it should |
| 180 | not be passed to unichr. */ |
| 181 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 182 | #endif |
| 183 | } |
| 184 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 185 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 186 | |
| 187 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 188 | to keep things simple, we use a single bitmask, using the least 5 |
| 189 | bits from each unicode characters as the bit index. */ |
| 190 | |
| 191 | /* the linebreak mask is set up by Unicode_Init below */ |
| 192 | |
| 193 | #define BLOOM_MASK unsigned long |
| 194 | |
| 195 | static BLOOM_MASK bloom_linebreak; |
| 196 | |
| 197 | #define BLOOM(mask, ch) ((mask & (1 << ((ch) & 0x1F)))) |
| 198 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 199 | #define BLOOM_LINEBREAK(ch) \ |
| 200 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 201 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 202 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 203 | Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len) |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 204 | { |
| 205 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 206 | |
| 207 | long mask; |
| 208 | Py_ssize_t i; |
| 209 | |
| 210 | mask = 0; |
| 211 | for (i = 0; i < len; i++) |
| 212 | mask |= (1 << (ptr[i] & 0x1F)); |
| 213 | |
| 214 | return mask; |
| 215 | } |
| 216 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 217 | Py_LOCAL_INLINE(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen) |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 218 | { |
| 219 | Py_ssize_t i; |
| 220 | |
| 221 | for (i = 0; i < setlen; i++) |
| 222 | if (set[i] == chr) |
| 223 | return 1; |
| 224 | |
Fredrik Lundh | 7763351 | 2006-05-23 19:47:35 +0000 | [diff] [blame] | 225 | return 0; |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 228 | #define BLOOM_MEMBER(mask, chr, set, setlen) \ |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 229 | BLOOM(mask, chr) && unicode_member(chr, set, setlen) |
| 230 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 231 | /* --- Unicode Object ----------------------------------------------------- */ |
| 232 | |
| 233 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 234 | int unicode_resize(register PyUnicodeObject *unicode, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 235 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 236 | { |
| 237 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 238 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 239 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 240 | if (unicode->length == length) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 241 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 242 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 243 | /* Resizing shared object (unicode_empty or single character |
| 244 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 245 | instead ! */ |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 246 | |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 247 | if (unicode == unicode_empty || |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 248 | (unicode->length == 1 && |
| 249 | unicode->str[0] < 256U && |
| 250 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 251 | PyErr_SetString(PyExc_SystemError, |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 252 | "can't resize shared unicode objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 253 | return -1; |
| 254 | } |
| 255 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 256 | /* We allocate one more byte to make sure the string is Ux0000 terminated. |
| 257 | The overallocation is also used by fastsearch, which assumes that it's |
Andrew M. Kuchling | 07bbfc6 | 2006-05-26 19:51:10 +0000 | [diff] [blame] | 258 | safe to look at str[length] (without making any assumptions about what |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 259 | it contains). */ |
| 260 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 261 | oldstr = unicode->str; |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 262 | unicode->str = PyObject_REALLOC(unicode->str, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 263 | sizeof(Py_UNICODE) * (length + 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 264 | if (!unicode->str) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 265 | unicode->str = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 266 | PyErr_NoMemory(); |
| 267 | return -1; |
| 268 | } |
| 269 | unicode->str[length] = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 270 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 271 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 272 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 273 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 274 | if (unicode->defenc) { |
| 275 | Py_DECREF(unicode->defenc); |
| 276 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 277 | } |
| 278 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 279 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | /* We allocate one more byte to make sure the string is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 284 | Ux0000 terminated -- XXX is this needed ? |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 285 | |
| 286 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 287 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 288 | |
| 289 | */ |
| 290 | |
| 291 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 292 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 293 | { |
| 294 | register PyUnicodeObject *unicode; |
| 295 | |
Andrew Dalke | e0df762 | 2006-05-27 11:04:36 +0000 | [diff] [blame] | 296 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 297 | if (length == 0 && unicode_empty != NULL) { |
| 298 | Py_INCREF(unicode_empty); |
| 299 | return unicode_empty; |
| 300 | } |
| 301 | |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 302 | /* Ensure we won't overflow the size. */ |
| 303 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 304 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 305 | } |
| 306 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 307 | /* Unicode freelist & memory allocation */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 308 | if (free_list) { |
| 309 | unicode = free_list; |
| 310 | free_list = *(PyUnicodeObject **)unicode; |
| 311 | numfree--; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 312 | if (unicode->str) { |
| 313 | /* Keep-Alive optimization: we only upsize the buffer, |
| 314 | never downsize it. */ |
| 315 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 316 | unicode_resize(unicode, length) < 0) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 317 | PyObject_DEL(unicode->str); |
| 318 | unicode->str = NULL; |
| 319 | } |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 320 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 321 | else { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 322 | size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 323 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 324 | } |
| 325 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 326 | } |
| 327 | else { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 328 | size_t new_size; |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 329 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 330 | if (unicode == NULL) |
| 331 | return NULL; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 332 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 333 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 336 | if (!unicode->str) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 337 | PyErr_NoMemory(); |
| 338 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 339 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 340 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 341 | * the caller fails before initializing str -- unicode_resize() |
| 342 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 343 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 344 | * We don't want unicode_resize to read uninitialized memory in |
| 345 | * that case. |
| 346 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 347 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 348 | unicode->str[length] = 0; |
Martin v. Löwis | f15da69 | 2006-04-13 07:24:50 +0000 | [diff] [blame] | 349 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 350 | unicode->hash = -1; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 351 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 352 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 353 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 354 | onError: |
Amaury Forgeot d'Arc | 06847b1 | 2008-07-31 23:39:05 +0000 | [diff] [blame] | 355 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 356 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 357 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 358 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 359 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 363 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 364 | { |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 365 | if (PyUnicode_CheckExact(unicode) && |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 366 | numfree < PyUnicode_MAXFREELIST) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 367 | /* Keep-Alive optimization */ |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 368 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
| 369 | PyObject_DEL(unicode->str); |
| 370 | unicode->str = NULL; |
| 371 | unicode->length = 0; |
| 372 | } |
| 373 | if (unicode->defenc) { |
| 374 | Py_DECREF(unicode->defenc); |
| 375 | unicode->defenc = NULL; |
| 376 | } |
| 377 | /* Add to free list */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 378 | *(PyUnicodeObject **)unicode = free_list; |
| 379 | free_list = unicode; |
| 380 | numfree++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 381 | } |
| 382 | else { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 383 | PyObject_DEL(unicode->str); |
| 384 | Py_XDECREF(unicode->defenc); |
| 385 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 389 | static |
| 390 | int _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 391 | { |
| 392 | register PyUnicodeObject *v; |
| 393 | |
| 394 | /* Argument checks */ |
| 395 | if (unicode == NULL) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 396 | PyErr_BadInternalCall(); |
| 397 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 398 | } |
Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 399 | v = *unicode; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 400 | if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 401 | PyErr_BadInternalCall(); |
| 402 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /* Resizing unicode_empty and single character objects is not |
| 406 | possible since these are being shared. We simply return a fresh |
| 407 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 408 | if (v->length != length && |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 409 | (v == unicode_empty || v->length == 1)) { |
| 410 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 411 | if (w == NULL) |
| 412 | return -1; |
| 413 | Py_UNICODE_COPY(w->str, v->str, |
| 414 | length < v->length ? length : v->length); |
| 415 | Py_DECREF(*unicode); |
| 416 | *unicode = w; |
| 417 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 421 | objects, since we can modify them in-place. */ |
| 422 | return unicode_resize(v, length); |
| 423 | } |
| 424 | |
Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 425 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
| 426 | { |
| 427 | return _PyUnicode_Resize((PyUnicodeObject **)unicode, length); |
| 428 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 429 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 430 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 431 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 432 | { |
| 433 | PyUnicodeObject *unicode; |
| 434 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 435 | /* If the Unicode data is known at construction time, we can apply |
| 436 | some optimizations which share commonly used objects. */ |
| 437 | if (u != NULL) { |
| 438 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 439 | /* Optimization for empty strings */ |
| 440 | if (size == 0 && unicode_empty != NULL) { |
| 441 | Py_INCREF(unicode_empty); |
| 442 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 443 | } |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 444 | |
| 445 | /* Single character Unicode objects in the Latin-1 range are |
| 446 | shared when using this constructor */ |
| 447 | if (size == 1 && *u < 256) { |
| 448 | unicode = unicode_latin1[*u]; |
| 449 | if (!unicode) { |
| 450 | unicode = _PyUnicode_New(1); |
| 451 | if (!unicode) |
| 452 | return NULL; |
| 453 | unicode->str[0] = *u; |
| 454 | unicode_latin1[*u] = unicode; |
| 455 | } |
| 456 | Py_INCREF(unicode); |
| 457 | return (PyObject *)unicode; |
| 458 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 459 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 460 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 461 | unicode = _PyUnicode_New(size); |
| 462 | if (!unicode) |
| 463 | return NULL; |
| 464 | |
| 465 | /* Copy the Unicode data into the new object */ |
| 466 | if (u != NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 467 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 468 | |
| 469 | return (PyObject *)unicode; |
| 470 | } |
| 471 | |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 472 | PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
| 473 | { |
| 474 | PyUnicodeObject *unicode; |
Gregory P. Smith | c00eb73 | 2008-04-09 23:16:37 +0000 | [diff] [blame] | 475 | |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 476 | if (size < 0) { |
| 477 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 478 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 479 | return NULL; |
| 480 | } |
Gregory P. Smith | c00eb73 | 2008-04-09 23:16:37 +0000 | [diff] [blame] | 481 | |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +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 | Also, this means the input must be UTF-8, so fall back to the |
| 485 | UTF-8 decoder at the end. */ |
| 486 | if (u != NULL) { |
| 487 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 488 | /* Optimization for empty strings */ |
| 489 | if (size == 0 && unicode_empty != NULL) { |
| 490 | Py_INCREF(unicode_empty); |
| 491 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 492 | } |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 493 | |
| 494 | /* Single characters are shared when using this constructor. |
| 495 | Restrict to ASCII, since the input must be UTF-8. */ |
| 496 | if (size == 1 && Py_CHARMASK(*u) < 128) { |
| 497 | unicode = unicode_latin1[Py_CHARMASK(*u)]; |
| 498 | if (!unicode) { |
| 499 | unicode = _PyUnicode_New(1); |
| 500 | if (!unicode) |
| 501 | return NULL; |
| 502 | unicode->str[0] = Py_CHARMASK(*u); |
| 503 | unicode_latin1[Py_CHARMASK(*u)] = unicode; |
| 504 | } |
| 505 | Py_INCREF(unicode); |
| 506 | return (PyObject *)unicode; |
| 507 | } |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 508 | |
| 509 | return PyUnicode_DecodeUTF8(u, size, NULL); |
| 510 | } |
| 511 | |
| 512 | unicode = _PyUnicode_New(size); |
| 513 | if (!unicode) |
| 514 | return NULL; |
| 515 | |
| 516 | return (PyObject *)unicode; |
| 517 | } |
| 518 | |
| 519 | PyObject *PyUnicode_FromString(const char *u) |
| 520 | { |
| 521 | size_t size = strlen(u); |
| 522 | if (size > PY_SSIZE_T_MAX) { |
| 523 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 524 | return NULL; |
| 525 | } |
| 526 | |
| 527 | return PyUnicode_FromStringAndSize(u, size); |
| 528 | } |
| 529 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 530 | #ifdef HAVE_WCHAR_H |
| 531 | |
Mark Dickinson | 6b265f1 | 2009-03-18 16:07:26 +0000 | [diff] [blame] | 532 | #if (Py_UNICODE_SIZE == 2) && defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
| 533 | # define CONVERT_WCHAR_TO_SURROGATES |
| 534 | #endif |
| 535 | |
| 536 | #ifdef CONVERT_WCHAR_TO_SURROGATES |
| 537 | |
| 538 | /* Here sizeof(wchar_t) is 4 but Py_UNICODE_SIZE == 2, so we need |
| 539 | to convert from UTF32 to UTF16. */ |
| 540 | |
| 541 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
| 542 | Py_ssize_t size) |
| 543 | { |
| 544 | PyUnicodeObject *unicode; |
| 545 | register Py_ssize_t i; |
| 546 | Py_ssize_t alloc; |
| 547 | const wchar_t *orig_w; |
| 548 | |
| 549 | if (w == NULL) { |
| 550 | PyErr_BadInternalCall(); |
| 551 | return NULL; |
| 552 | } |
| 553 | |
| 554 | alloc = size; |
| 555 | orig_w = w; |
| 556 | for (i = size; i > 0; i--) { |
| 557 | if (*w > 0xFFFF) |
| 558 | alloc++; |
| 559 | w++; |
| 560 | } |
| 561 | w = orig_w; |
| 562 | unicode = _PyUnicode_New(alloc); |
| 563 | if (!unicode) |
| 564 | return NULL; |
| 565 | |
| 566 | /* Copy the wchar_t data into the new object */ |
| 567 | { |
| 568 | register Py_UNICODE *u; |
| 569 | u = PyUnicode_AS_UNICODE(unicode); |
| 570 | for (i = size; i > 0; i--) { |
| 571 | if (*w > 0xFFFF) { |
| 572 | wchar_t ordinal = *w++; |
| 573 | ordinal -= 0x10000; |
| 574 | *u++ = 0xD800 | (ordinal >> 10); |
| 575 | *u++ = 0xDC00 | (ordinal & 0x3FF); |
| 576 | } |
| 577 | else |
| 578 | *u++ = *w++; |
| 579 | } |
| 580 | } |
| 581 | return (PyObject *)unicode; |
| 582 | } |
| 583 | |
| 584 | #else |
| 585 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 586 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 587 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 588 | { |
| 589 | PyUnicodeObject *unicode; |
| 590 | |
| 591 | if (w == NULL) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 592 | PyErr_BadInternalCall(); |
| 593 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | unicode = _PyUnicode_New(size); |
| 597 | if (!unicode) |
| 598 | return NULL; |
| 599 | |
| 600 | /* Copy the wchar_t data into the new object */ |
| 601 | #ifdef HAVE_USABLE_WCHAR_T |
| 602 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 603 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 604 | { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 605 | register Py_UNICODE *u; |
| 606 | register Py_ssize_t i; |
| 607 | u = PyUnicode_AS_UNICODE(unicode); |
| 608 | for (i = size; i > 0; i--) |
| 609 | *u++ = *w++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 610 | } |
| 611 | #endif |
| 612 | |
| 613 | return (PyObject *)unicode; |
| 614 | } |
| 615 | |
Mark Dickinson | 6b265f1 | 2009-03-18 16:07:26 +0000 | [diff] [blame] | 616 | #endif /* CONVERT_WCHAR_TO_SURROGATES */ |
| 617 | |
| 618 | #undef CONVERT_WCHAR_TO_SURROGATES |
| 619 | |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 620 | static void |
| 621 | makefmt(char *fmt, int longflag, int size_tflag, int zeropad, int width, int precision, char c) |
| 622 | { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 623 | *fmt++ = '%'; |
| 624 | if (width) { |
| 625 | if (zeropad) |
| 626 | *fmt++ = '0'; |
| 627 | fmt += sprintf(fmt, "%d", width); |
| 628 | } |
| 629 | if (precision) |
| 630 | fmt += sprintf(fmt, ".%d", precision); |
| 631 | if (longflag) |
| 632 | *fmt++ = 'l'; |
| 633 | else if (size_tflag) { |
| 634 | char *f = PY_FORMAT_SIZE_T; |
| 635 | while (*f) |
| 636 | *fmt++ = *f++; |
| 637 | } |
| 638 | *fmt++ = c; |
| 639 | *fmt = '\0'; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} |
| 643 | |
| 644 | PyObject * |
| 645 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 646 | { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 647 | va_list count; |
| 648 | Py_ssize_t callcount = 0; |
| 649 | PyObject **callresults = NULL; |
| 650 | PyObject **callresult = NULL; |
| 651 | Py_ssize_t n = 0; |
| 652 | int width = 0; |
| 653 | int precision = 0; |
| 654 | int zeropad; |
| 655 | const char* f; |
| 656 | Py_UNICODE *s; |
| 657 | PyObject *string; |
| 658 | /* used by sprintf */ |
| 659 | char buffer[21]; |
| 660 | /* use abuffer instead of buffer, if we need more space |
| 661 | * (which can happen if there's a format specifier with width). */ |
| 662 | char *abuffer = NULL; |
| 663 | char *realbuffer; |
| 664 | Py_ssize_t abuffersize = 0; |
| 665 | char fmt[60]; /* should be enough for %0width.precisionld */ |
| 666 | const char *copy; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 667 | |
| 668 | #ifdef VA_LIST_IS_ARRAY |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 669 | Py_MEMCPY(count, vargs, sizeof(va_list)); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 670 | #else |
| 671 | #ifdef __va_copy |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 672 | __va_copy(count, vargs); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 673 | #else |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 674 | count = vargs; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 675 | #endif |
| 676 | #endif |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 677 | /* step 1: count the number of %S/%R format specifications |
| 678 | * (we call PyObject_Str()/PyObject_Repr() for these objects |
| 679 | * once during step 3 and put the result in an array) */ |
| 680 | for (f = format; *f; f++) { |
| 681 | if (*f == '%' && (*(f+1)=='S' || *(f+1)=='R')) |
| 682 | ++callcount; |
| 683 | } |
| 684 | /* step 2: allocate memory for the results of |
| 685 | * PyObject_Str()/PyObject_Repr() calls */ |
| 686 | if (callcount) { |
| 687 | callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); |
| 688 | if (!callresults) { |
| 689 | PyErr_NoMemory(); |
| 690 | return NULL; |
| 691 | } |
| 692 | callresult = callresults; |
| 693 | } |
| 694 | /* step 3: figure out how large a buffer we need */ |
| 695 | for (f = format; *f; f++) { |
| 696 | if (*f == '%') { |
| 697 | const char* p = f; |
| 698 | width = 0; |
| 699 | while (isdigit((unsigned)*f)) |
| 700 | width = (width*10) + *f++ - '0'; |
| 701 | while (*++f && *f != '%' && !isalpha((unsigned)*f)) |
| 702 | ; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 703 | |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 704 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 705 | * they don't affect the amount of space we reserve. |
| 706 | */ |
| 707 | if ((*f == 'l' || *f == 'z') && |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 708 | (f[1] == 'd' || f[1] == 'u')) |
| 709 | ++f; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 710 | |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 711 | switch (*f) { |
| 712 | case 'c': |
| 713 | (void)va_arg(count, int); |
| 714 | /* fall through... */ |
| 715 | case '%': |
| 716 | n++; |
| 717 | break; |
| 718 | case 'd': case 'u': case 'i': case 'x': |
| 719 | (void) va_arg(count, int); |
| 720 | /* 20 bytes is enough to hold a 64-bit |
| 721 | integer. Decimal takes the most space. |
| 722 | This isn't enough for octal. |
| 723 | If a width is specified we need more |
| 724 | (which we allocate later). */ |
| 725 | if (width < 20) |
| 726 | width = 20; |
| 727 | n += width; |
| 728 | if (abuffersize < width) |
| 729 | abuffersize = width; |
| 730 | break; |
| 731 | case 's': |
| 732 | { |
| 733 | /* UTF-8 */ |
| 734 | unsigned char*s; |
| 735 | s = va_arg(count, unsigned char*); |
| 736 | while (*s) { |
| 737 | if (*s < 128) { |
| 738 | n++; s++; |
| 739 | } else if (*s < 0xc0) { |
| 740 | /* invalid UTF-8 */ |
| 741 | n++; s++; |
| 742 | } else if (*s < 0xc0) { |
| 743 | n++; |
| 744 | s++; if(!*s)break; |
| 745 | s++; |
| 746 | } else if (*s < 0xe0) { |
| 747 | n++; |
| 748 | s++; if(!*s)break; |
| 749 | s++; if(!*s)break; |
| 750 | s++; |
| 751 | } else { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 752 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 753 | n++; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 754 | #else |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 755 | n+=2; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 756 | #endif |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 757 | s++; if(!*s)break; |
| 758 | s++; if(!*s)break; |
| 759 | s++; if(!*s)break; |
| 760 | s++; |
| 761 | } |
| 762 | } |
| 763 | break; |
| 764 | } |
| 765 | case 'U': |
| 766 | { |
| 767 | PyObject *obj = va_arg(count, PyObject *); |
| 768 | assert(obj && PyUnicode_Check(obj)); |
| 769 | n += PyUnicode_GET_SIZE(obj); |
| 770 | break; |
| 771 | } |
| 772 | case 'V': |
| 773 | { |
| 774 | PyObject *obj = va_arg(count, PyObject *); |
| 775 | const char *str = va_arg(count, const char *); |
| 776 | assert(obj || str); |
| 777 | assert(!obj || PyUnicode_Check(obj)); |
| 778 | if (obj) |
| 779 | n += PyUnicode_GET_SIZE(obj); |
| 780 | else |
| 781 | n += strlen(str); |
| 782 | break; |
| 783 | } |
| 784 | case 'S': |
| 785 | { |
| 786 | PyObject *obj = va_arg(count, PyObject *); |
| 787 | PyObject *str; |
| 788 | assert(obj); |
| 789 | str = PyObject_Str(obj); |
| 790 | if (!str) |
| 791 | goto fail; |
| 792 | n += PyUnicode_GET_SIZE(str); |
| 793 | /* Remember the str and switch to the next slot */ |
| 794 | *callresult++ = str; |
| 795 | break; |
| 796 | } |
| 797 | case 'R': |
| 798 | { |
| 799 | PyObject *obj = va_arg(count, PyObject *); |
| 800 | PyObject *repr; |
| 801 | assert(obj); |
| 802 | repr = PyObject_Repr(obj); |
| 803 | if (!repr) |
| 804 | goto fail; |
| 805 | n += PyUnicode_GET_SIZE(repr); |
| 806 | /* Remember the repr and switch to the next slot */ |
| 807 | *callresult++ = repr; |
| 808 | break; |
| 809 | } |
| 810 | case 'p': |
| 811 | (void) va_arg(count, int); |
| 812 | /* maximum 64-bit pointer representation: |
| 813 | * 0xffffffffffffffff |
| 814 | * so 19 characters is enough. |
| 815 | * XXX I count 18 -- what's the extra for? |
| 816 | */ |
| 817 | n += 19; |
| 818 | break; |
| 819 | default: |
| 820 | /* if we stumble upon an unknown |
| 821 | formatting code, copy the rest of |
| 822 | the format string to the output |
| 823 | string. (we cannot just skip the |
| 824 | code, since there's no way to know |
| 825 | what's in the argument list) */ |
| 826 | n += strlen(p); |
| 827 | goto expand; |
| 828 | } |
| 829 | } else |
| 830 | n++; |
| 831 | } |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 832 | expand: |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 833 | if (abuffersize > 20) { |
| 834 | abuffer = PyObject_Malloc(abuffersize); |
| 835 | if (!abuffer) { |
| 836 | PyErr_NoMemory(); |
| 837 | goto fail; |
| 838 | } |
| 839 | realbuffer = abuffer; |
| 840 | } |
| 841 | else |
| 842 | realbuffer = buffer; |
| 843 | /* step 4: fill the buffer */ |
| 844 | /* Since we've analyzed how much space we need for the worst case, |
| 845 | we don't have to resize the string. |
| 846 | There can be no errors beyond this point. */ |
| 847 | string = PyUnicode_FromUnicode(NULL, n); |
| 848 | if (!string) |
| 849 | goto fail; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 850 | |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 851 | s = PyUnicode_AS_UNICODE(string); |
| 852 | callresult = callresults; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 853 | |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 854 | for (f = format; *f; f++) { |
| 855 | if (*f == '%') { |
| 856 | const char* p = f++; |
| 857 | int longflag = 0; |
| 858 | int size_tflag = 0; |
| 859 | zeropad = (*f == '0'); |
| 860 | /* parse the width.precision part */ |
| 861 | width = 0; |
| 862 | while (isdigit((unsigned)*f)) |
| 863 | width = (width*10) + *f++ - '0'; |
| 864 | precision = 0; |
| 865 | if (*f == '.') { |
| 866 | f++; |
| 867 | while (isdigit((unsigned)*f)) |
| 868 | precision = (precision*10) + *f++ - '0'; |
| 869 | } |
| 870 | /* handle the long flag, but only for %ld and %lu. |
| 871 | others can be added when necessary. */ |
| 872 | if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
| 873 | longflag = 1; |
| 874 | ++f; |
| 875 | } |
| 876 | /* handle the size_t flag. */ |
| 877 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 878 | size_tflag = 1; |
| 879 | ++f; |
| 880 | } |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 881 | |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 882 | switch (*f) { |
| 883 | case 'c': |
| 884 | *s++ = va_arg(vargs, int); |
| 885 | break; |
| 886 | case 'd': |
| 887 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'd'); |
| 888 | if (longflag) |
| 889 | sprintf(realbuffer, fmt, va_arg(vargs, long)); |
| 890 | else if (size_tflag) |
| 891 | sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
| 892 | else |
| 893 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 894 | appendstring(realbuffer); |
| 895 | break; |
| 896 | case 'u': |
| 897 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'u'); |
| 898 | if (longflag) |
| 899 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
| 900 | else if (size_tflag) |
| 901 | sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
| 902 | else |
| 903 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
| 904 | appendstring(realbuffer); |
| 905 | break; |
| 906 | case 'i': |
| 907 | makefmt(fmt, 0, 0, zeropad, width, precision, 'i'); |
| 908 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 909 | appendstring(realbuffer); |
| 910 | break; |
| 911 | case 'x': |
| 912 | makefmt(fmt, 0, 0, zeropad, width, precision, 'x'); |
| 913 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 914 | appendstring(realbuffer); |
| 915 | break; |
| 916 | case 's': |
| 917 | { |
| 918 | /* Parameter must be UTF-8 encoded. |
| 919 | In case of encoding errors, use |
| 920 | the replacement character. */ |
| 921 | PyObject *u; |
| 922 | p = va_arg(vargs, char*); |
| 923 | u = PyUnicode_DecodeUTF8(p, strlen(p), |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 924 | "replace"); |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 925 | if (!u) |
| 926 | goto fail; |
| 927 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(u), |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 928 | PyUnicode_GET_SIZE(u)); |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 929 | s += PyUnicode_GET_SIZE(u); |
| 930 | Py_DECREF(u); |
| 931 | break; |
| 932 | } |
| 933 | case 'U': |
| 934 | { |
| 935 | PyObject *obj = va_arg(vargs, PyObject *); |
| 936 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 937 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 938 | s += size; |
| 939 | break; |
| 940 | } |
| 941 | case 'V': |
| 942 | { |
| 943 | PyObject *obj = va_arg(vargs, PyObject *); |
| 944 | const char *str = va_arg(vargs, const char *); |
| 945 | if (obj) { |
| 946 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 947 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 948 | s += size; |
| 949 | } else { |
| 950 | appendstring(str); |
| 951 | } |
| 952 | break; |
| 953 | } |
| 954 | case 'S': |
| 955 | case 'R': |
| 956 | { |
| 957 | Py_UNICODE *ucopy; |
| 958 | Py_ssize_t usize; |
| 959 | Py_ssize_t upos; |
| 960 | /* unused, since we already have the result */ |
| 961 | (void) va_arg(vargs, PyObject *); |
| 962 | ucopy = PyUnicode_AS_UNICODE(*callresult); |
| 963 | usize = PyUnicode_GET_SIZE(*callresult); |
| 964 | for (upos = 0; upos<usize;) |
| 965 | *s++ = ucopy[upos++]; |
| 966 | /* We're done with the unicode()/repr() => forget it */ |
| 967 | Py_DECREF(*callresult); |
| 968 | /* switch to next unicode()/repr() result */ |
| 969 | ++callresult; |
| 970 | break; |
| 971 | } |
| 972 | case 'p': |
| 973 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 974 | /* %p is ill-defined: ensure leading 0x. */ |
| 975 | if (buffer[1] == 'X') |
| 976 | buffer[1] = 'x'; |
| 977 | else if (buffer[1] != 'x') { |
| 978 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 979 | buffer[0] = '0'; |
| 980 | buffer[1] = 'x'; |
| 981 | } |
| 982 | appendstring(buffer); |
| 983 | break; |
| 984 | case '%': |
| 985 | *s++ = '%'; |
| 986 | break; |
| 987 | default: |
| 988 | appendstring(p); |
| 989 | goto end; |
| 990 | } |
| 991 | } else |
| 992 | *s++ = *f; |
| 993 | } |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 994 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 995 | end: |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 996 | if (callresults) |
| 997 | PyObject_Free(callresults); |
| 998 | if (abuffer) |
| 999 | PyObject_Free(abuffer); |
| 1000 | PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
| 1001 | return string; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1002 | fail: |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 1003 | if (callresults) { |
| 1004 | PyObject **callresult2 = callresults; |
| 1005 | while (callresult2 < callresult) { |
| 1006 | Py_DECREF(*callresult2); |
| 1007 | ++callresult2; |
| 1008 | } |
| 1009 | PyObject_Free(callresults); |
| 1010 | } |
| 1011 | if (abuffer) |
| 1012 | PyObject_Free(abuffer); |
| 1013 | return NULL; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | #undef appendstring |
| 1017 | |
| 1018 | PyObject * |
| 1019 | PyUnicode_FromFormat(const char *format, ...) |
| 1020 | { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 1021 | PyObject* ret; |
| 1022 | va_list vargs; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 1023 | |
| 1024 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 1025 | va_start(vargs, format); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 1026 | #else |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 1027 | va_start(vargs); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 1028 | #endif |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 1029 | ret = PyUnicode_FromFormatV(format, vargs); |
| 1030 | va_end(vargs); |
| 1031 | return ret; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1034 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1035 | wchar_t *w, |
| 1036 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1037 | { |
| 1038 | if (unicode == NULL) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1039 | PyErr_BadInternalCall(); |
| 1040 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1041 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1042 | |
| 1043 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1044 | if (size > PyUnicode_GET_SIZE(unicode)) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1045 | size = PyUnicode_GET_SIZE(unicode) + 1; |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1046 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1047 | #ifdef HAVE_USABLE_WCHAR_T |
| 1048 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 1049 | #else |
| 1050 | { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1051 | register Py_UNICODE *u; |
| 1052 | register Py_ssize_t i; |
| 1053 | u = PyUnicode_AS_UNICODE(unicode); |
| 1054 | for (i = size; i > 0; i--) |
| 1055 | *w++ = *u++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1056 | } |
| 1057 | #endif |
| 1058 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1059 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 1060 | return PyUnicode_GET_SIZE(unicode); |
| 1061 | else |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1062 | return size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | #endif |
| 1066 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1067 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 1068 | { |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1069 | Py_UNICODE s[1]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1070 | |
| 1071 | #ifdef Py_UNICODE_WIDE |
| 1072 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1073 | PyErr_SetString(PyExc_ValueError, |
| 1074 | "unichr() arg not in range(0x110000) " |
| 1075 | "(wide Python build)"); |
| 1076 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1077 | } |
| 1078 | #else |
| 1079 | if (ordinal < 0 || ordinal > 0xffff) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1080 | PyErr_SetString(PyExc_ValueError, |
| 1081 | "unichr() arg not in range(0x10000) " |
| 1082 | "(narrow Python build)"); |
| 1083 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1084 | } |
| 1085 | #endif |
| 1086 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1087 | s[0] = (Py_UNICODE)ordinal; |
| 1088 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1091 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 1092 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1093 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1094 | PyObject_Unicode() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1095 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1096 | Py_INCREF(obj); |
| 1097 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1098 | } |
| 1099 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1100 | /* For a Unicode subtype that's not a Unicode object, |
| 1101 | return a true Unicode object with the same data. */ |
| 1102 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 1103 | PyUnicode_GET_SIZE(obj)); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1104 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1105 | return PyUnicode_FromEncodedObject(obj, NULL, "strict"); |
| 1106 | } |
| 1107 | |
| 1108 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1109 | const char *encoding, |
| 1110 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1111 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1112 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1113 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1114 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1115 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1116 | if (obj == NULL) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1117 | PyErr_BadInternalCall(); |
| 1118 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1119 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1120 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1121 | #if 0 |
| 1122 | /* For b/w compatibility we also accept Unicode objects provided |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 1123 | that no encodings is given and then redirect to |
| 1124 | PyObject_Unicode() which then applies the additional logic for |
| 1125 | Unicode subclasses. |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1126 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1127 | NOTE: This API should really only be used for object which |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1128 | represent *encoded* Unicode ! |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1129 | |
| 1130 | */ |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 1131 | if (PyUnicode_Check(obj)) { |
| 1132 | if (encoding) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1133 | PyErr_SetString(PyExc_TypeError, |
| 1134 | "decoding Unicode is not supported"); |
| 1135 | return NULL; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 1136 | } |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1137 | return PyObject_Unicode(obj); |
| 1138 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1139 | #else |
| 1140 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1141 | PyErr_SetString(PyExc_TypeError, |
| 1142 | "decoding Unicode is not supported"); |
| 1143 | return NULL; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 1144 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1145 | #endif |
| 1146 | |
| 1147 | /* Coerce object */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1148 | if (PyString_Check(obj)) { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 1149 | s = PyString_AS_STRING(obj); |
| 1150 | len = PyString_GET_SIZE(obj); |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 1151 | } |
Christian Heimes | 3497f94 | 2008-05-26 12:29:14 +0000 | [diff] [blame] | 1152 | else if (PyByteArray_Check(obj)) { |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 1153 | /* Python 2.x specific */ |
| 1154 | PyErr_Format(PyExc_TypeError, |
| 1155 | "decoding bytearray is not supported"); |
| 1156 | return NULL; |
| 1157 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1158 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1159 | /* Overwrite the error message with something more useful in |
| 1160 | case of a TypeError. */ |
| 1161 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1162 | PyErr_Format(PyExc_TypeError, |
| 1163 | "coercing to Unicode: need string or buffer, " |
| 1164 | "%.80s found", |
| 1165 | Py_TYPE(obj)->tp_name); |
| 1166 | goto onError; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1167 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1168 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1169 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1170 | if (len == 0) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1171 | Py_INCREF(unicode_empty); |
| 1172 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1173 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1174 | else |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1175 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1176 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1177 | return v; |
| 1178 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1179 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1180 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | PyObject *PyUnicode_Decode(const char *s, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1184 | Py_ssize_t size, |
| 1185 | const char *encoding, |
| 1186 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1187 | { |
| 1188 | PyObject *buffer = NULL, *unicode; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1189 | |
| 1190 | if (encoding == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1191 | encoding = PyUnicode_GetDefaultEncoding(); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1192 | |
| 1193 | /* Shortcuts for common default encodings */ |
| 1194 | if (strcmp(encoding, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1195 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1196 | else if (strcmp(encoding, "latin-1") == 0) |
| 1197 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1198 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1199 | else if (strcmp(encoding, "mbcs") == 0) |
| 1200 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 1201 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1202 | else if (strcmp(encoding, "ascii") == 0) |
| 1203 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1204 | |
| 1205 | /* Decode via the codec registry */ |
| 1206 | buffer = PyBuffer_FromMemory((void *)s, size); |
| 1207 | if (buffer == NULL) |
| 1208 | goto onError; |
| 1209 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1210 | if (unicode == NULL) |
| 1211 | goto onError; |
| 1212 | if (!PyUnicode_Check(unicode)) { |
| 1213 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 1214 | "decoder did not return an unicode object (type=%.400s)", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1215 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1216 | Py_DECREF(unicode); |
| 1217 | goto onError; |
| 1218 | } |
| 1219 | Py_DECREF(buffer); |
| 1220 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1221 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1222 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1223 | Py_XDECREF(buffer); |
| 1224 | return NULL; |
| 1225 | } |
| 1226 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1227 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1228 | const char *encoding, |
| 1229 | const char *errors) |
| 1230 | { |
| 1231 | PyObject *v; |
| 1232 | |
| 1233 | if (!PyUnicode_Check(unicode)) { |
| 1234 | PyErr_BadArgument(); |
| 1235 | goto onError; |
| 1236 | } |
| 1237 | |
| 1238 | if (encoding == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1239 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1240 | |
| 1241 | /* Decode via the codec registry */ |
| 1242 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1243 | if (v == NULL) |
| 1244 | goto onError; |
| 1245 | return v; |
| 1246 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1247 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1248 | return NULL; |
| 1249 | } |
| 1250 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1251 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1252 | Py_ssize_t size, |
| 1253 | const char *encoding, |
| 1254 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1255 | { |
| 1256 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1257 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1258 | unicode = PyUnicode_FromUnicode(s, size); |
| 1259 | if (unicode == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1260 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1261 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1262 | Py_DECREF(unicode); |
| 1263 | return v; |
| 1264 | } |
| 1265 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1266 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1267 | const char *encoding, |
| 1268 | const char *errors) |
| 1269 | { |
| 1270 | PyObject *v; |
| 1271 | |
| 1272 | if (!PyUnicode_Check(unicode)) { |
| 1273 | PyErr_BadArgument(); |
| 1274 | goto onError; |
| 1275 | } |
| 1276 | |
| 1277 | if (encoding == NULL) |
Benjamin Peterson | d17fec7 | 2009-01-31 21:47:42 +0000 | [diff] [blame] | 1278 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1279 | |
| 1280 | /* Encode via the codec registry */ |
| 1281 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1282 | if (v == NULL) |
| 1283 | goto onError; |
| 1284 | return v; |
| 1285 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1286 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1287 | return NULL; |
| 1288 | } |
| 1289 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1290 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1291 | const char *encoding, |
| 1292 | const char *errors) |
| 1293 | { |
| 1294 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1295 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1296 | if (!PyUnicode_Check(unicode)) { |
| 1297 | PyErr_BadArgument(); |
| 1298 | goto onError; |
| 1299 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1300 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1301 | if (encoding == NULL) |
Benjamin Peterson | d17fec7 | 2009-01-31 21:47:42 +0000 | [diff] [blame] | 1302 | encoding = PyUnicode_GetDefaultEncoding(); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1303 | |
| 1304 | /* Shortcuts for common default encodings */ |
| 1305 | if (errors == NULL) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1306 | if (strcmp(encoding, "utf-8") == 0) |
| 1307 | return PyUnicode_AsUTF8String(unicode); |
| 1308 | else if (strcmp(encoding, "latin-1") == 0) |
| 1309 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1310 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1311 | else if (strcmp(encoding, "mbcs") == 0) |
| 1312 | return PyUnicode_AsMBCSString(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1313 | #endif |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1314 | else if (strcmp(encoding, "ascii") == 0) |
| 1315 | return PyUnicode_AsASCIIString(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1316 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1317 | |
| 1318 | /* Encode via the codec registry */ |
| 1319 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1320 | if (v == NULL) |
| 1321 | goto onError; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1322 | if (!PyString_Check(v)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1323 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 1324 | "encoder did not return a string object (type=%.400s)", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1325 | Py_TYPE(v)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1326 | Py_DECREF(v); |
| 1327 | goto onError; |
| 1328 | } |
| 1329 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1330 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1331 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1332 | return NULL; |
| 1333 | } |
| 1334 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1335 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1336 | const char *errors) |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1337 | { |
| 1338 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
| 1339 | |
| 1340 | if (v) |
| 1341 | return v; |
| 1342 | v = PyUnicode_AsEncodedString(unicode, NULL, errors); |
| 1343 | if (v && errors == NULL) |
| 1344 | ((PyUnicodeObject *)unicode)->defenc = v; |
| 1345 | return v; |
| 1346 | } |
| 1347 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1348 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1349 | { |
| 1350 | if (!PyUnicode_Check(unicode)) { |
| 1351 | PyErr_BadArgument(); |
| 1352 | goto onError; |
| 1353 | } |
| 1354 | return PyUnicode_AS_UNICODE(unicode); |
| 1355 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1356 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1357 | return NULL; |
| 1358 | } |
| 1359 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1360 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1361 | { |
| 1362 | if (!PyUnicode_Check(unicode)) { |
| 1363 | PyErr_BadArgument(); |
| 1364 | goto onError; |
| 1365 | } |
| 1366 | return PyUnicode_GET_SIZE(unicode); |
| 1367 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1368 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1369 | return -1; |
| 1370 | } |
| 1371 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1372 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1373 | { |
| 1374 | return unicode_default_encoding; |
| 1375 | } |
| 1376 | |
| 1377 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 1378 | { |
| 1379 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1380 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1381 | /* Make sure the encoding is valid. As side effect, this also |
| 1382 | loads the encoding into the codec registry cache. */ |
| 1383 | v = _PyCodec_Lookup(encoding); |
| 1384 | if (v == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1385 | goto onError; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1386 | Py_DECREF(v); |
| 1387 | strncpy(unicode_default_encoding, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1388 | encoding, |
| 1389 | sizeof(unicode_default_encoding)); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1390 | return 0; |
| 1391 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1392 | onError: |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1393 | return -1; |
| 1394 | } |
| 1395 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1396 | /* error handling callback helper: |
| 1397 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1398 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1399 | and adjust various state variables. |
| 1400 | return 0 on success, -1 on error |
| 1401 | */ |
| 1402 | |
| 1403 | static |
| 1404 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1405 | const char *encoding, const char *reason, |
| 1406 | const char *input, Py_ssize_t insize, Py_ssize_t *startinpos, |
| 1407 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 1408 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1409 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1410 | static char *argparse = "O!n;decoding error handler must return (unicode, int) tuple"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1411 | |
| 1412 | PyObject *restuple = NULL; |
| 1413 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1414 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
| 1415 | Py_ssize_t requiredsize; |
| 1416 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1417 | Py_UNICODE *repptr; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1418 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1419 | int res = -1; |
| 1420 | |
| 1421 | if (*errorHandler == NULL) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1422 | *errorHandler = PyCodec_LookupError(errors); |
| 1423 | if (*errorHandler == NULL) |
| 1424 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 1428 | *exceptionObject = PyUnicodeDecodeError_Create( |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1429 | encoding, input, insize, *startinpos, *endinpos, reason); |
| 1430 | if (*exceptionObject == NULL) |
| 1431 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1432 | } |
| 1433 | else { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1434 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 1435 | goto onError; |
| 1436 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 1437 | goto onError; |
| 1438 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1439 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1443 | if (restuple == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1444 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1445 | if (!PyTuple_Check(restuple)) { |
Georg Brandl | cbb4958 | 2009-02-13 11:06:59 +0000 | [diff] [blame] | 1446 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1447 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1448 | } |
| 1449 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1450 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1451 | if (newpos<0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1452 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1453 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1454 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 1455 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1456 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1457 | |
| 1458 | /* need more space? (at least enough for what we |
| 1459 | have+the replacement+the rest of the string (starting |
| 1460 | at the new input position), so we won't have to check space |
| 1461 | when there are no errors in the rest of the string) */ |
| 1462 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 1463 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 1464 | requiredsize = *outpos + repsize + insize-newpos; |
| 1465 | if (requiredsize > outsize) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1466 | if (requiredsize<2*outsize) |
| 1467 | requiredsize = 2*outsize; |
| 1468 | if (_PyUnicode_Resize(output, requiredsize) < 0) |
| 1469 | goto onError; |
| 1470 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1471 | } |
| 1472 | *endinpos = newpos; |
| 1473 | *inptr = input + newpos; |
| 1474 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 1475 | *outptr += repsize; |
| 1476 | *outpos += repsize; |
| 1477 | /* we made it! */ |
| 1478 | res = 0; |
| 1479 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1480 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1481 | Py_XDECREF(restuple); |
| 1482 | return res; |
| 1483 | } |
| 1484 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1485 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 1486 | |
| 1487 | /* see RFC2152 for details */ |
| 1488 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1489 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1490 | char utf7_special[128] = { |
| 1491 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 1492 | encoded: |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 1493 | 0 - not special |
| 1494 | 1 - special |
| 1495 | 2 - whitespace (optional) |
| 1496 | 3 - RFC2152 Set O (optional) */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1497 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 1498 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1499 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 1500 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 1501 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1502 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 1503 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1504 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 1505 | |
| 1506 | }; |
| 1507 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1508 | /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
| 1509 | warnings about the comparison always being false; since |
| 1510 | utf7_special[0] is 1, we can safely make that one comparison |
| 1511 | true */ |
| 1512 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1513 | #define SPECIAL(c, encodeO, encodeWS) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1514 | ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1515 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1516 | (encodeO && (utf7_special[(c)] == 3))) |
| 1517 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1518 | #define B64(n) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1519 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1520 | #define B64CHAR(c) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1521 | (isalnum(c) || (c) == '+' || (c) == '/') |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1522 | #define UB64(c) \ |
| 1523 | ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1524 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1525 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1526 | #define ENCODE(out, ch, bits) \ |
| 1527 | while (bits >= 6) { \ |
| 1528 | *out++ = B64(ch >> (bits-6)); \ |
| 1529 | bits -= 6; \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1532 | #define DECODE(out, ch, bits, surrogate) \ |
| 1533 | while (bits >= 16) { \ |
| 1534 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 1535 | bits -= 16; \ |
| 1536 | if (surrogate) { \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1537 | /* We have already generated an error for the high surrogate \ |
| 1538 | so let's not bother seeing if the low surrogate is correct or not */ \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1539 | surrogate = 0; \ |
| 1540 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1541 | /* This is a surrogate pair. Unfortunately we can't represent \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1542 | it in a 16-bit character */ \ |
| 1543 | surrogate = 1; \ |
| 1544 | errmsg = "code pairs are not supported"; \ |
| 1545 | goto utf7Error; \ |
| 1546 | } else { \ |
| 1547 | *out++ = outCh; \ |
| 1548 | } \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1549 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1550 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1551 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1552 | Py_ssize_t size, |
| 1553 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1554 | { |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1555 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 1556 | } |
| 1557 | |
| 1558 | PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1559 | Py_ssize_t size, |
| 1560 | const char *errors, |
| 1561 | Py_ssize_t *consumed) |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1562 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1563 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1564 | Py_ssize_t startinpos; |
| 1565 | Py_ssize_t endinpos; |
| 1566 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1567 | const char *e; |
| 1568 | PyUnicodeObject *unicode; |
| 1569 | Py_UNICODE *p; |
| 1570 | const char *errmsg = ""; |
| 1571 | int inShift = 0; |
| 1572 | unsigned int bitsleft = 0; |
| 1573 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1574 | int surrogate = 0; |
| 1575 | PyObject *errorHandler = NULL; |
| 1576 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1577 | |
| 1578 | unicode = _PyUnicode_New(size); |
| 1579 | if (!unicode) |
| 1580 | return NULL; |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1581 | if (size == 0) { |
| 1582 | if (consumed) |
| 1583 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1584 | return (PyObject *)unicode; |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1585 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1586 | |
| 1587 | p = unicode->str; |
| 1588 | e = s + size; |
| 1589 | |
| 1590 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1591 | Py_UNICODE ch; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1592 | restart: |
Antoine Pitrou | 4982d5d | 2008-07-25 17:45:59 +0000 | [diff] [blame] | 1593 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1594 | |
| 1595 | if (inShift) { |
| 1596 | if ((ch == '-') || !B64CHAR(ch)) { |
| 1597 | inShift = 0; |
| 1598 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1599 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1600 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1601 | if (bitsleft >= 6) { |
| 1602 | /* The shift sequence has a partial character in it. If |
| 1603 | bitsleft < 6 then we could just classify it as padding |
| 1604 | but that is not the case here */ |
| 1605 | |
| 1606 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1607 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1608 | } |
| 1609 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1610 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1611 | here so indicate the potential of a misencoded character. */ |
| 1612 | |
| 1613 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 1614 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 1615 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1616 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
| 1619 | if (ch == '-') { |
| 1620 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1621 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1622 | inShift = 1; |
| 1623 | } |
| 1624 | } else if (SPECIAL(ch,0,0)) { |
| 1625 | errmsg = "unexpected special character"; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 1626 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1627 | } else { |
| 1628 | *p++ = ch; |
| 1629 | } |
| 1630 | } else { |
| 1631 | charsleft = (charsleft << 6) | UB64(ch); |
| 1632 | bitsleft += 6; |
| 1633 | s++; |
| 1634 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1635 | } |
| 1636 | } |
| 1637 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1638 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1639 | s++; |
| 1640 | if (s < e && *s == '-') { |
| 1641 | s++; |
| 1642 | *p++ = '+'; |
| 1643 | } else |
| 1644 | { |
| 1645 | inShift = 1; |
| 1646 | bitsleft = 0; |
| 1647 | } |
| 1648 | } |
| 1649 | else if (SPECIAL(ch,0,0)) { |
Walter Dörwald | 9d04542 | 2007-08-30 15:34:55 +0000 | [diff] [blame] | 1650 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1651 | errmsg = "unexpected special character"; |
| 1652 | s++; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 1653 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1654 | } |
| 1655 | else { |
| 1656 | *p++ = ch; |
| 1657 | s++; |
| 1658 | } |
| 1659 | continue; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1660 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1661 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1662 | endinpos = s-starts; |
| 1663 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1664 | errors, &errorHandler, |
| 1665 | "utf7", errmsg, |
| 1666 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1667 | &unicode, &outpos, &p)) |
| 1668 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1671 | if (inShift && !consumed) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1672 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1673 | endinpos = size; |
| 1674 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1675 | errors, &errorHandler, |
| 1676 | "utf7", "unterminated shift sequence", |
| 1677 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1678 | &unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1679 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1680 | if (s < e) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1681 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1682 | } |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1683 | if (consumed) { |
| 1684 | if(inShift) |
| 1685 | *consumed = startinpos; |
| 1686 | else |
| 1687 | *consumed = s-starts; |
| 1688 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1689 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1690 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1691 | goto onError; |
| 1692 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1693 | Py_XDECREF(errorHandler); |
| 1694 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1695 | return (PyObject *)unicode; |
| 1696 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1697 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1698 | Py_XDECREF(errorHandler); |
| 1699 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1700 | Py_DECREF(unicode); |
| 1701 | return NULL; |
| 1702 | } |
| 1703 | |
| 1704 | |
| 1705 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1706 | Py_ssize_t size, |
| 1707 | int encodeSetO, |
| 1708 | int encodeWhiteSpace, |
| 1709 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1710 | { |
| 1711 | PyObject *v; |
| 1712 | /* It might be possible to tighten this worst case */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1713 | Py_ssize_t cbAllocated = 5 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1714 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1715 | Py_ssize_t i = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1716 | unsigned int bitsleft = 0; |
| 1717 | unsigned long charsleft = 0; |
| 1718 | char * out; |
| 1719 | char * start; |
| 1720 | |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 1721 | if (cbAllocated / 5 != size) |
| 1722 | return PyErr_NoMemory(); |
| 1723 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1724 | if (size == 0) |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 1725 | return PyString_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1726 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1727 | v = PyString_FromStringAndSize(NULL, cbAllocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1728 | if (v == NULL) |
| 1729 | return NULL; |
| 1730 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1731 | start = out = PyString_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1732 | for (;i < size; ++i) { |
| 1733 | Py_UNICODE ch = s[i]; |
| 1734 | |
| 1735 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1736 | if (ch == '+') { |
| 1737 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1738 | *out++ = '-'; |
| 1739 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1740 | charsleft = ch; |
| 1741 | bitsleft = 16; |
| 1742 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1743 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1744 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1745 | } else { |
| 1746 | *out++ = (char) ch; |
| 1747 | } |
| 1748 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1749 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1750 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1751 | charsleft = 0; |
| 1752 | bitsleft = 0; |
| 1753 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1754 | so no '-' is required, except if the character is itself a '-' */ |
| 1755 | if (B64CHAR(ch) || ch == '-') { |
| 1756 | *out++ = '-'; |
| 1757 | } |
| 1758 | inShift = 0; |
| 1759 | *out++ = (char) ch; |
| 1760 | } else { |
| 1761 | bitsleft += 16; |
| 1762 | charsleft = (charsleft << 16) | ch; |
| 1763 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1764 | |
Mark Dickinson | 3e4caeb | 2009-02-21 20:27:01 +0000 | [diff] [blame] | 1765 | /* If the next character is special then we don't need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1766 | the shift sequence. If the next character is not a BASE64 character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1767 | or '-' then the shift sequence will be terminated implicitly and we |
| 1768 | don't have to insert a '-'. */ |
| 1769 | |
| 1770 | if (bitsleft == 0) { |
| 1771 | if (i + 1 < size) { |
| 1772 | Py_UNICODE ch2 = s[i+1]; |
| 1773 | |
| 1774 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1775 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1776 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1777 | *out++ = '-'; |
| 1778 | inShift = 0; |
| 1779 | } else { |
| 1780 | inShift = 0; |
| 1781 | } |
| 1782 | |
| 1783 | } |
| 1784 | else { |
| 1785 | *out++ = '-'; |
| 1786 | inShift = 0; |
| 1787 | } |
| 1788 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1789 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1790 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1791 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1792 | if (bitsleft) { |
| 1793 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1794 | *out++ = '-'; |
| 1795 | } |
| 1796 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1797 | _PyString_Resize(&v, out - start); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1798 | return v; |
| 1799 | } |
| 1800 | |
| 1801 | #undef SPECIAL |
| 1802 | #undef B64 |
| 1803 | #undef B64CHAR |
| 1804 | #undef UB64 |
| 1805 | #undef ENCODE |
| 1806 | #undef DECODE |
| 1807 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1808 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1809 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1810 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1811 | char utf8_code_length[256] = { |
| 1812 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1813 | illegal prefix. see RFC 2279 for details */ |
| 1814 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1815 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1816 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1817 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1818 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1819 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1820 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1821 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1822 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1823 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1824 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1825 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1826 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1827 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1828 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1829 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1830 | }; |
| 1831 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1832 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1833 | Py_ssize_t size, |
| 1834 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1835 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1836 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 1837 | } |
| 1838 | |
| 1839 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1840 | Py_ssize_t size, |
| 1841 | const char *errors, |
| 1842 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1843 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1844 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1845 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1846 | Py_ssize_t startinpos; |
| 1847 | Py_ssize_t endinpos; |
| 1848 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1849 | const char *e; |
| 1850 | PyUnicodeObject *unicode; |
| 1851 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1852 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1853 | PyObject *errorHandler = NULL; |
| 1854 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1855 | |
| 1856 | /* Note: size will always be longer than the resulting Unicode |
| 1857 | character count */ |
| 1858 | unicode = _PyUnicode_New(size); |
| 1859 | if (!unicode) |
| 1860 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1861 | if (size == 0) { |
| 1862 | if (consumed) |
| 1863 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1864 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1865 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1866 | |
| 1867 | /* Unpack UTF-8 encoded data */ |
| 1868 | p = unicode->str; |
| 1869 | e = s + size; |
| 1870 | |
| 1871 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1872 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1873 | |
| 1874 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1875 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1876 | s++; |
| 1877 | continue; |
| 1878 | } |
| 1879 | |
| 1880 | n = utf8_code_length[ch]; |
| 1881 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1882 | if (s + n > e) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1883 | if (consumed) |
| 1884 | break; |
| 1885 | else { |
| 1886 | errmsg = "unexpected end of data"; |
| 1887 | startinpos = s-starts; |
| 1888 | endinpos = size; |
| 1889 | goto utf8Error; |
| 1890 | } |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 1891 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1892 | |
| 1893 | switch (n) { |
| 1894 | |
| 1895 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1896 | errmsg = "unexpected code byte"; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1897 | startinpos = s-starts; |
| 1898 | endinpos = startinpos+1; |
| 1899 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1900 | |
| 1901 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1902 | errmsg = "internal error"; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1903 | startinpos = s-starts; |
| 1904 | endinpos = startinpos+1; |
| 1905 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1906 | |
| 1907 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1908 | if ((s[1] & 0xc0) != 0x80) { |
| 1909 | errmsg = "invalid data"; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1910 | startinpos = s-starts; |
| 1911 | endinpos = startinpos+2; |
| 1912 | goto utf8Error; |
| 1913 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1914 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1915 | if (ch < 0x80) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1916 | startinpos = s-starts; |
| 1917 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1918 | errmsg = "illegal encoding"; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1919 | goto utf8Error; |
| 1920 | } |
| 1921 | else |
| 1922 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1923 | break; |
| 1924 | |
| 1925 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1926 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1927 | (s[2] & 0xc0) != 0x80) { |
| 1928 | errmsg = "invalid data"; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1929 | startinpos = s-starts; |
| 1930 | endinpos = startinpos+3; |
| 1931 | goto utf8Error; |
| 1932 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1933 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1934 | if (ch < 0x0800) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1935 | /* Note: UTF-8 encodings of surrogates are considered |
| 1936 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1937 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1938 | XXX For wide builds (UCS-4) we should probably try |
| 1939 | to recombine the surrogates into a single code |
| 1940 | unit. |
| 1941 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1942 | errmsg = "illegal encoding"; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1943 | startinpos = s-starts; |
| 1944 | endinpos = startinpos+3; |
| 1945 | goto utf8Error; |
| 1946 | } |
| 1947 | else |
| 1948 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1949 | break; |
| 1950 | |
| 1951 | case 4: |
| 1952 | if ((s[1] & 0xc0) != 0x80 || |
| 1953 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1954 | (s[3] & 0xc0) != 0x80) { |
| 1955 | errmsg = "invalid data"; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1956 | startinpos = s-starts; |
| 1957 | endinpos = startinpos+4; |
| 1958 | goto utf8Error; |
| 1959 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1960 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1961 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1962 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1963 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1964 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1965 | || (ch > 0x10ffff)) /* maximum value allowed for |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1966 | UTF-16 */ |
| 1967 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1968 | errmsg = "illegal encoding"; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1969 | startinpos = s-starts; |
| 1970 | endinpos = startinpos+4; |
| 1971 | goto utf8Error; |
| 1972 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1973 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1974 | *p++ = (Py_UNICODE)ch; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1975 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1976 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1977 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1978 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 1979 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1980 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1981 | /* high surrogate = top 10 bits added to D800 */ |
| 1982 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1983 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1984 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1985 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1986 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1987 | break; |
| 1988 | |
| 1989 | default: |
| 1990 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1991 | errmsg = "unsupported Unicode code range"; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1992 | startinpos = s-starts; |
| 1993 | endinpos = startinpos+n; |
| 1994 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1995 | } |
| 1996 | s += n; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1997 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1998 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1999 | utf8Error: |
| 2000 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2001 | if (unicode_decode_call_errorhandler( |
| 2002 | errors, &errorHandler, |
| 2003 | "utf8", errmsg, |
| 2004 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2005 | &unicode, &outpos, &p)) |
| 2006 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2007 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2008 | if (consumed) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2009 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2010 | |
| 2011 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2012 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2013 | goto onError; |
| 2014 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2015 | Py_XDECREF(errorHandler); |
| 2016 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2017 | return (PyObject *)unicode; |
| 2018 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2019 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2020 | Py_XDECREF(errorHandler); |
| 2021 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2022 | Py_DECREF(unicode); |
| 2023 | return NULL; |
| 2024 | } |
| 2025 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2026 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 2027 | and allocate exactly as much space needed at the end. Else allocate the |
| 2028 | maximum possible needed (4 result bytes per Unicode character), and return |
| 2029 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2030 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 2031 | PyObject * |
| 2032 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2033 | Py_ssize_t size, |
| 2034 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2035 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2036 | #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] | 2037 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2038 | Py_ssize_t i; /* index into s of next input byte */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2039 | PyObject *v; /* result string object */ |
| 2040 | char *p; /* next free byte in output buffer */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2041 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2042 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2043 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2044 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2045 | assert(s != NULL); |
| 2046 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2047 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2048 | if (size <= MAX_SHORT_UNICHARS) { |
| 2049 | /* Write into the stack buffer; nallocated can't overflow. |
| 2050 | * At the end, we'll allocate exactly as much heap space as it |
| 2051 | * turns out we need. |
| 2052 | */ |
| 2053 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
| 2054 | v = NULL; /* will allocate after we're done */ |
| 2055 | p = stackbuf; |
| 2056 | } |
| 2057 | else { |
| 2058 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 2059 | nallocated = size * 4; |
| 2060 | if (nallocated / 4 != size) /* overflow! */ |
| 2061 | return PyErr_NoMemory(); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2062 | v = PyString_FromStringAndSize(NULL, nallocated); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2063 | if (v == NULL) |
| 2064 | return NULL; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2065 | p = PyString_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2066 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2067 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2068 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2069 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2070 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2071 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2072 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2073 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2074 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2075 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2076 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 2077 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 2078 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2079 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2080 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2081 | /* Encode UCS2 Unicode ordinals */ |
| 2082 | if (ch < 0x10000) { |
| 2083 | /* Special case: check for high surrogate */ |
| 2084 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 2085 | Py_UCS4 ch2 = s[i]; |
| 2086 | /* Check for low surrogate and combine the two to |
| 2087 | form a UCS4 value */ |
| 2088 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2089 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2090 | i++; |
| 2091 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2092 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2093 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2094 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2095 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2096 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2097 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2098 | continue; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 2099 | } |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2100 | encodeUCS4: |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2101 | /* Encode UCS4 Unicode ordinals */ |
| 2102 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2103 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 2104 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2105 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2106 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2107 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2108 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2109 | if (v == NULL) { |
| 2110 | /* This was stack allocated. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2111 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2112 | assert(nneeded <= nallocated); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2113 | v = PyString_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2114 | } |
| 2115 | else { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 2116 | /* Cut back to size actually needed. */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2117 | nneeded = p - PyString_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2118 | assert(nneeded <= nallocated); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2119 | _PyString_Resize(&v, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2120 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2121 | return v; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2122 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2123 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2124 | } |
| 2125 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2126 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 2127 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2128 | if (!PyUnicode_Check(unicode)) { |
| 2129 | PyErr_BadArgument(); |
| 2130 | return NULL; |
| 2131 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 2132 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2133 | PyUnicode_GET_SIZE(unicode), |
| 2134 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2135 | } |
| 2136 | |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2137 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 2138 | |
| 2139 | PyObject * |
| 2140 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2141 | Py_ssize_t size, |
| 2142 | const char *errors, |
| 2143 | int *byteorder) |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2144 | { |
| 2145 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 2146 | } |
| 2147 | |
| 2148 | PyObject * |
| 2149 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2150 | Py_ssize_t size, |
| 2151 | const char *errors, |
| 2152 | int *byteorder, |
| 2153 | Py_ssize_t *consumed) |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2154 | { |
| 2155 | const char *starts = s; |
| 2156 | Py_ssize_t startinpos; |
| 2157 | Py_ssize_t endinpos; |
| 2158 | Py_ssize_t outpos; |
| 2159 | PyUnicodeObject *unicode; |
| 2160 | Py_UNICODE *p; |
| 2161 | #ifndef Py_UNICODE_WIDE |
| 2162 | int i, pairs; |
| 2163 | #else |
| 2164 | const int pairs = 0; |
| 2165 | #endif |
| 2166 | const unsigned char *q, *e; |
| 2167 | int bo = 0; /* assume native ordering by default */ |
| 2168 | const char *errmsg = ""; |
Walter Dörwald | 20b40d3 | 2007-08-17 16:52:50 +0000 | [diff] [blame] | 2169 | /* Offsets from q for retrieving bytes in the right order. */ |
| 2170 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2171 | int iorder[] = {0, 1, 2, 3}; |
| 2172 | #else |
| 2173 | int iorder[] = {3, 2, 1, 0}; |
| 2174 | #endif |
Walter Dörwald | 9ab80a9 | 2007-08-17 16:58:43 +0000 | [diff] [blame] | 2175 | PyObject *errorHandler = NULL; |
| 2176 | PyObject *exc = NULL; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2177 | /* On narrow builds we split characters outside the BMP into two |
| 2178 | codepoints => count how much extra space we need. */ |
| 2179 | #ifndef Py_UNICODE_WIDE |
| 2180 | for (i = pairs = 0; i < size/4; i++) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2181 | if (((Py_UCS4 *)s)[i] >= 0x10000) |
| 2182 | pairs++; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2183 | #endif |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2184 | |
| 2185 | /* This might be one to much, because of a BOM */ |
| 2186 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 2187 | if (!unicode) |
| 2188 | return NULL; |
| 2189 | if (size == 0) |
| 2190 | return (PyObject *)unicode; |
| 2191 | |
| 2192 | /* Unpack UTF-32 encoded data */ |
| 2193 | p = unicode->str; |
| 2194 | q = (unsigned char *)s; |
| 2195 | e = q + size; |
| 2196 | |
| 2197 | if (byteorder) |
| 2198 | bo = *byteorder; |
| 2199 | |
| 2200 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2201 | byte order setting accordingly. In native mode, the leading BOM |
| 2202 | mark is skipped, in all other modes, it is copied to the output |
| 2203 | stream as-is (giving a ZWNBSP character). */ |
| 2204 | if (bo == 0) { |
| 2205 | if (size >= 4) { |
| 2206 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2207 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2208 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2209 | if (bom == 0x0000FEFF) { |
| 2210 | q += 4; |
| 2211 | bo = -1; |
| 2212 | } |
| 2213 | else if (bom == 0xFFFE0000) { |
| 2214 | q += 4; |
| 2215 | bo = 1; |
| 2216 | } |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2217 | #else |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2218 | if (bom == 0x0000FEFF) { |
| 2219 | q += 4; |
| 2220 | bo = 1; |
| 2221 | } |
| 2222 | else if (bom == 0xFFFE0000) { |
| 2223 | q += 4; |
| 2224 | bo = -1; |
| 2225 | } |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2226 | #endif |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2227 | } |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
| 2230 | if (bo == -1) { |
| 2231 | /* force LE */ |
| 2232 | iorder[0] = 0; |
| 2233 | iorder[1] = 1; |
| 2234 | iorder[2] = 2; |
| 2235 | iorder[3] = 3; |
| 2236 | } |
| 2237 | else if (bo == 1) { |
| 2238 | /* force BE */ |
| 2239 | iorder[0] = 3; |
| 2240 | iorder[1] = 2; |
| 2241 | iorder[2] = 1; |
| 2242 | iorder[3] = 0; |
| 2243 | } |
| 2244 | |
| 2245 | while (q < e) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2246 | Py_UCS4 ch; |
| 2247 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 2248 | if (e-q<4) { |
| 2249 | if (consumed) |
| 2250 | break; |
| 2251 | errmsg = "truncated data"; |
| 2252 | startinpos = ((const char *)q)-starts; |
| 2253 | endinpos = ((const char *)e)-starts; |
| 2254 | goto utf32Error; |
| 2255 | /* The remaining input chars are ignored if the callback |
| 2256 | chooses to skip the input */ |
| 2257 | } |
| 2258 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2259 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2260 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2261 | if (ch >= 0x110000) |
| 2262 | { |
| 2263 | errmsg = "codepoint not in range(0x110000)"; |
| 2264 | startinpos = ((const char *)q)-starts; |
| 2265 | endinpos = startinpos+4; |
| 2266 | goto utf32Error; |
| 2267 | } |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2268 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2269 | if (ch >= 0x10000) |
| 2270 | { |
| 2271 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 2272 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2273 | } |
| 2274 | else |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2275 | #endif |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2276 | *p++ = ch; |
| 2277 | q += 4; |
| 2278 | continue; |
| 2279 | utf32Error: |
| 2280 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2281 | if (unicode_decode_call_errorhandler( |
| 2282 | errors, &errorHandler, |
| 2283 | "utf32", errmsg, |
| 2284 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2285 | &unicode, &outpos, &p)) |
| 2286 | goto onError; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2287 | } |
| 2288 | |
| 2289 | if (byteorder) |
| 2290 | *byteorder = bo; |
| 2291 | |
| 2292 | if (consumed) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2293 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2294 | |
| 2295 | /* Adjust length */ |
| 2296 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 2297 | goto onError; |
| 2298 | |
| 2299 | Py_XDECREF(errorHandler); |
| 2300 | Py_XDECREF(exc); |
| 2301 | return (PyObject *)unicode; |
| 2302 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2303 | onError: |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2304 | Py_DECREF(unicode); |
| 2305 | Py_XDECREF(errorHandler); |
| 2306 | Py_XDECREF(exc); |
| 2307 | return NULL; |
| 2308 | } |
| 2309 | |
| 2310 | PyObject * |
| 2311 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2312 | Py_ssize_t size, |
| 2313 | const char *errors, |
| 2314 | int byteorder) |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2315 | { |
| 2316 | PyObject *v; |
| 2317 | unsigned char *p; |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2318 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2319 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2320 | Py_ssize_t i, pairs; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2321 | #else |
| 2322 | const int pairs = 0; |
| 2323 | #endif |
| 2324 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2325 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2326 | int iorder[] = {0, 1, 2, 3}; |
| 2327 | #else |
| 2328 | int iorder[] = {3, 2, 1, 0}; |
| 2329 | #endif |
| 2330 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2331 | #define STORECHAR(CH) \ |
| 2332 | do { \ |
| 2333 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 2334 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 2335 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 2336 | p[iorder[0]] = (CH) & 0xff; \ |
| 2337 | p += 4; \ |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2338 | } while(0) |
| 2339 | |
| 2340 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 2341 | so we need less space. */ |
| 2342 | #ifndef Py_UNICODE_WIDE |
| 2343 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2344 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2345 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 2346 | pairs++; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2347 | #endif |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2348 | nsize = (size - pairs + (byteorder == 0)); |
| 2349 | bytesize = nsize * 4; |
| 2350 | if (bytesize / 4 != nsize) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2351 | return PyErr_NoMemory(); |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2352 | v = PyString_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2353 | if (v == NULL) |
| 2354 | return NULL; |
| 2355 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2356 | p = (unsigned char *)PyString_AS_STRING(v); |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2357 | if (byteorder == 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2358 | STORECHAR(0xFEFF); |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2359 | if (size == 0) |
| 2360 | return v; |
| 2361 | |
| 2362 | if (byteorder == -1) { |
| 2363 | /* force LE */ |
| 2364 | iorder[0] = 0; |
| 2365 | iorder[1] = 1; |
| 2366 | iorder[2] = 2; |
| 2367 | iorder[3] = 3; |
| 2368 | } |
| 2369 | else if (byteorder == 1) { |
| 2370 | /* force BE */ |
| 2371 | iorder[0] = 3; |
| 2372 | iorder[1] = 2; |
| 2373 | iorder[2] = 1; |
| 2374 | iorder[3] = 0; |
| 2375 | } |
| 2376 | |
| 2377 | while (size-- > 0) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2378 | Py_UCS4 ch = *s++; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2379 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2380 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 2381 | Py_UCS4 ch2 = *s; |
| 2382 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 2383 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 2384 | s++; |
| 2385 | size--; |
| 2386 | } |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 2387 | } |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2388 | #endif |
| 2389 | STORECHAR(ch); |
| 2390 | } |
| 2391 | return v; |
| 2392 | #undef STORECHAR |
| 2393 | } |
| 2394 | |
| 2395 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 2396 | { |
| 2397 | if (!PyUnicode_Check(unicode)) { |
| 2398 | PyErr_BadArgument(); |
| 2399 | return NULL; |
| 2400 | } |
| 2401 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2402 | PyUnicode_GET_SIZE(unicode), |
| 2403 | NULL, |
| 2404 | 0); |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2405 | } |
| 2406 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2407 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 2408 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2409 | PyObject * |
| 2410 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2411 | Py_ssize_t size, |
| 2412 | const char *errors, |
| 2413 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2414 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2415 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 2416 | } |
| 2417 | |
| 2418 | PyObject * |
| 2419 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2420 | Py_ssize_t size, |
| 2421 | const char *errors, |
| 2422 | int *byteorder, |
| 2423 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2424 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2425 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2426 | Py_ssize_t startinpos; |
| 2427 | Py_ssize_t endinpos; |
| 2428 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2429 | PyUnicodeObject *unicode; |
| 2430 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2431 | const unsigned char *q, *e; |
| 2432 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2433 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2434 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 2435 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2436 | int ihi = 1, ilo = 0; |
| 2437 | #else |
| 2438 | int ihi = 0, ilo = 1; |
| 2439 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2440 | PyObject *errorHandler = NULL; |
| 2441 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2442 | |
| 2443 | /* Note: size will always be longer than the resulting Unicode |
| 2444 | character count */ |
| 2445 | unicode = _PyUnicode_New(size); |
| 2446 | if (!unicode) |
| 2447 | return NULL; |
| 2448 | if (size == 0) |
| 2449 | return (PyObject *)unicode; |
| 2450 | |
| 2451 | /* Unpack UTF-16 encoded data */ |
| 2452 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2453 | q = (unsigned char *)s; |
| 2454 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2455 | |
| 2456 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2457 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2458 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2459 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2460 | byte order setting accordingly. In native mode, the leading BOM |
| 2461 | mark is skipped, in all other modes, it is copied to the output |
| 2462 | stream as-is (giving a ZWNBSP character). */ |
| 2463 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2464 | if (size >= 2) { |
| 2465 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2466 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2467 | if (bom == 0xFEFF) { |
| 2468 | q += 2; |
| 2469 | bo = -1; |
| 2470 | } |
| 2471 | else if (bom == 0xFFFE) { |
| 2472 | q += 2; |
| 2473 | bo = 1; |
| 2474 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2475 | #else |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2476 | if (bom == 0xFEFF) { |
| 2477 | q += 2; |
| 2478 | bo = 1; |
| 2479 | } |
| 2480 | else if (bom == 0xFFFE) { |
| 2481 | q += 2; |
| 2482 | bo = -1; |
| 2483 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2484 | #endif |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2485 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2486 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2487 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2488 | if (bo == -1) { |
| 2489 | /* force LE */ |
| 2490 | ihi = 1; |
| 2491 | ilo = 0; |
| 2492 | } |
| 2493 | else if (bo == 1) { |
| 2494 | /* force BE */ |
| 2495 | ihi = 0; |
| 2496 | ilo = 1; |
| 2497 | } |
| 2498 | |
| 2499 | while (q < e) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2500 | Py_UNICODE ch; |
| 2501 | /* remaining bytes at the end? (size should be even) */ |
| 2502 | if (e-q<2) { |
| 2503 | if (consumed) |
| 2504 | break; |
| 2505 | errmsg = "truncated data"; |
| 2506 | startinpos = ((const char *)q)-starts; |
| 2507 | endinpos = ((const char *)e)-starts; |
| 2508 | goto utf16Error; |
| 2509 | /* The remaining input chars are ignored if the callback |
| 2510 | chooses to skip the input */ |
| 2511 | } |
| 2512 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2513 | |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 2514 | q += 2; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2515 | |
| 2516 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 2517 | *p++ = ch; |
| 2518 | continue; |
| 2519 | } |
| 2520 | |
| 2521 | /* UTF-16 code pair: */ |
| 2522 | if (q >= e) { |
| 2523 | errmsg = "unexpected end of data"; |
| 2524 | startinpos = (((const char *)q)-2)-starts; |
| 2525 | endinpos = ((const char *)e)-starts; |
| 2526 | goto utf16Error; |
| 2527 | } |
| 2528 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 2529 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 2530 | q += 2; |
| 2531 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2532 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2533 | *p++ = ch; |
| 2534 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2535 | #else |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2536 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2537 | #endif |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2538 | continue; |
| 2539 | } |
| 2540 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2541 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2542 | startinpos = (((const char *)q)-4)-starts; |
| 2543 | endinpos = startinpos+2; |
| 2544 | goto utf16Error; |
| 2545 | } |
| 2546 | |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 2547 | } |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2548 | errmsg = "illegal encoding"; |
| 2549 | startinpos = (((const char *)q)-2)-starts; |
| 2550 | endinpos = startinpos+2; |
| 2551 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2552 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2553 | utf16Error: |
| 2554 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2555 | if (unicode_decode_call_errorhandler( |
| 2556 | errors, &errorHandler, |
| 2557 | "utf16", errmsg, |
| 2558 | starts, size, &startinpos, &endinpos, &exc, (const char **)&q, |
| 2559 | &unicode, &outpos, &p)) |
| 2560 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2561 | } |
| 2562 | |
| 2563 | if (byteorder) |
| 2564 | *byteorder = bo; |
| 2565 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2566 | if (consumed) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2567 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2568 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2569 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2570 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2571 | goto onError; |
| 2572 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2573 | Py_XDECREF(errorHandler); |
| 2574 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2575 | return (PyObject *)unicode; |
| 2576 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2577 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2578 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2579 | Py_XDECREF(errorHandler); |
| 2580 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2581 | return NULL; |
| 2582 | } |
| 2583 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2584 | PyObject * |
| 2585 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2586 | Py_ssize_t size, |
| 2587 | const char *errors, |
| 2588 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2589 | { |
| 2590 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2591 | unsigned char *p; |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2592 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2593 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2594 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2595 | #else |
| 2596 | const int pairs = 0; |
| 2597 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2598 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2599 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2600 | int ihi = 1, ilo = 0; |
| 2601 | #else |
| 2602 | int ihi = 0, ilo = 1; |
| 2603 | #endif |
| 2604 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2605 | #define STORECHAR(CH) \ |
| 2606 | do { \ |
| 2607 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 2608 | p[ilo] = (CH) & 0xff; \ |
| 2609 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2610 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2611 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2612 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2613 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2614 | if (s[i] >= 0x10000) |
| 2615 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2616 | #endif |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2617 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 2618 | if (size > PY_SSIZE_T_MAX || |
| 2619 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2620 | return PyErr_NoMemory(); |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2621 | nsize = size + pairs + (byteorder == 0); |
| 2622 | bytesize = nsize * 2; |
| 2623 | if (bytesize / 2 != nsize) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2624 | return PyErr_NoMemory(); |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2625 | v = PyString_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2626 | if (v == NULL) |
| 2627 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2628 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2629 | p = (unsigned char *)PyString_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2630 | if (byteorder == 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2631 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2632 | if (size == 0) |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2633 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2634 | |
| 2635 | if (byteorder == -1) { |
| 2636 | /* force LE */ |
| 2637 | ihi = 1; |
| 2638 | ilo = 0; |
| 2639 | } |
| 2640 | else if (byteorder == 1) { |
| 2641 | /* force BE */ |
| 2642 | ihi = 0; |
| 2643 | ilo = 1; |
| 2644 | } |
| 2645 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2646 | while (size-- > 0) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2647 | Py_UNICODE ch = *s++; |
| 2648 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2649 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2650 | if (ch >= 0x10000) { |
| 2651 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2652 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 2653 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2654 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2655 | STORECHAR(ch); |
| 2656 | if (ch2) |
| 2657 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2658 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2659 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2660 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2661 | } |
| 2662 | |
| 2663 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 2664 | { |
| 2665 | if (!PyUnicode_Check(unicode)) { |
| 2666 | PyErr_BadArgument(); |
| 2667 | return NULL; |
| 2668 | } |
| 2669 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2670 | PyUnicode_GET_SIZE(unicode), |
| 2671 | NULL, |
| 2672 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2673 | } |
| 2674 | |
| 2675 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 2676 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2677 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 2678 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2679 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2680 | Py_ssize_t size, |
| 2681 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2682 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2683 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2684 | Py_ssize_t startinpos; |
| 2685 | Py_ssize_t endinpos; |
| 2686 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2687 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2688 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2689 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2690 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2691 | char* message; |
| 2692 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2693 | PyObject *errorHandler = NULL; |
| 2694 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2695 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2696 | /* Escaped strings will always be longer than the resulting |
| 2697 | 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] | 2698 | length after conversion to the true value. |
| 2699 | (but if the error callback returns a long replacement string |
| 2700 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2701 | v = _PyUnicode_New(size); |
| 2702 | if (v == NULL) |
| 2703 | goto onError; |
| 2704 | if (size == 0) |
| 2705 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2706 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2707 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2708 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2709 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2710 | while (s < end) { |
| 2711 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2712 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2713 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2714 | |
| 2715 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2716 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2717 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2718 | continue; |
| 2719 | } |
| 2720 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2721 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2722 | /* \ - Escapes */ |
| 2723 | s++; |
Guido van Rossum | 1c1ac38 | 2007-10-29 22:15:05 +0000 | [diff] [blame] | 2724 | c = *s++; |
| 2725 | if (s > end) |
| 2726 | c = '\0'; /* Invalid after \ */ |
| 2727 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2728 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2729 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2730 | case '\n': break; |
| 2731 | case '\\': *p++ = '\\'; break; |
| 2732 | case '\'': *p++ = '\''; break; |
| 2733 | case '\"': *p++ = '\"'; break; |
| 2734 | case 'b': *p++ = '\b'; break; |
| 2735 | case 'f': *p++ = '\014'; break; /* FF */ |
| 2736 | case 't': *p++ = '\t'; break; |
| 2737 | case 'n': *p++ = '\n'; break; |
| 2738 | case 'r': *p++ = '\r'; break; |
| 2739 | case 'v': *p++ = '\013'; break; /* VT */ |
| 2740 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 2741 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2742 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2743 | case '0': case '1': case '2': case '3': |
| 2744 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2745 | x = s[-1] - '0'; |
Guido van Rossum | 1c1ac38 | 2007-10-29 22:15:05 +0000 | [diff] [blame] | 2746 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2747 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 1c1ac38 | 2007-10-29 22:15:05 +0000 | [diff] [blame] | 2748 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2749 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2750 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2751 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2752 | break; |
| 2753 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2754 | /* hex escapes */ |
| 2755 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2756 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2757 | digits = 2; |
| 2758 | message = "truncated \\xXX escape"; |
| 2759 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2760 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2761 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2762 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2763 | digits = 4; |
| 2764 | message = "truncated \\uXXXX escape"; |
| 2765 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2766 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2767 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2768 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2769 | digits = 8; |
| 2770 | message = "truncated \\UXXXXXXXX escape"; |
| 2771 | hexescape: |
| 2772 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2773 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2774 | if (s+digits>end) { |
| 2775 | endinpos = size; |
| 2776 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2777 | errors, &errorHandler, |
| 2778 | "unicodeescape", "end of string in escape sequence", |
| 2779 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2780 | &v, &outpos, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2781 | goto onError; |
| 2782 | goto nextByte; |
| 2783 | } |
| 2784 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2785 | c = (unsigned char) s[i]; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2786 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2787 | endinpos = (s+i+1)-starts; |
| 2788 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2789 | errors, &errorHandler, |
| 2790 | "unicodeescape", message, |
| 2791 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2792 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2793 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2794 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2795 | } |
| 2796 | chr = (chr<<4) & ~0xF; |
| 2797 | if (c >= '0' && c <= '9') |
| 2798 | chr += c - '0'; |
| 2799 | else if (c >= 'a' && c <= 'f') |
| 2800 | chr += 10 + c - 'a'; |
| 2801 | else |
| 2802 | chr += 10 + c - 'A'; |
| 2803 | } |
| 2804 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 2805 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2806 | /* _decoding_error will have already written into the |
| 2807 | target buffer. */ |
| 2808 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2809 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2810 | /* when we get here, chr is a 32-bit unicode character */ |
| 2811 | if (chr <= 0xffff) |
| 2812 | /* UCS-2 character */ |
| 2813 | *p++ = (Py_UNICODE) chr; |
| 2814 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2815 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2816 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2817 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2818 | *p++ = chr; |
| 2819 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2820 | chr -= 0x10000L; |
| 2821 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2822 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2823 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2824 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2825 | endinpos = s-starts; |
| 2826 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2827 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2828 | errors, &errorHandler, |
| 2829 | "unicodeescape", "illegal Unicode character", |
| 2830 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2831 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2832 | goto onError; |
| 2833 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2834 | break; |
| 2835 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2836 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2837 | case 'N': |
| 2838 | message = "malformed \\N character escape"; |
| 2839 | if (ucnhash_CAPI == NULL) { |
| 2840 | /* load the unicode data module */ |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2841 | PyObject *m, *api; |
Christian Heimes | 000a074 | 2008-01-03 22:16:32 +0000 | [diff] [blame] | 2842 | m = PyImport_ImportModuleNoBlock("unicodedata"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2843 | if (m == NULL) |
| 2844 | goto ucnhashError; |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2845 | api = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2846 | Py_DECREF(m); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2847 | if (api == NULL) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2848 | goto ucnhashError; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2849 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2850 | Py_DECREF(api); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2851 | if (ucnhash_CAPI == NULL) |
| 2852 | goto ucnhashError; |
| 2853 | } |
| 2854 | if (*s == '{') { |
| 2855 | const char *start = s+1; |
| 2856 | /* look for the closing brace */ |
| 2857 | while (*s != '}' && s < end) |
| 2858 | s++; |
| 2859 | if (s > start && s < end && *s == '}') { |
| 2860 | /* found a name. look it up in the unicode database */ |
| 2861 | message = "unknown Unicode character name"; |
| 2862 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 2863 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2864 | goto store; |
| 2865 | } |
| 2866 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2867 | endinpos = s-starts; |
| 2868 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2869 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2870 | errors, &errorHandler, |
| 2871 | "unicodeescape", message, |
| 2872 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2873 | &v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2874 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2875 | break; |
| 2876 | |
| 2877 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2878 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2879 | message = "\\ at end of string"; |
| 2880 | s--; |
| 2881 | endinpos = s-starts; |
| 2882 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2883 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2884 | errors, &errorHandler, |
| 2885 | "unicodeescape", message, |
| 2886 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2887 | &v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2888 | goto onError; |
| 2889 | } |
| 2890 | else { |
| 2891 | *p++ = '\\'; |
| 2892 | *p++ = (unsigned char)s[-1]; |
| 2893 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2894 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2895 | } |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2896 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2897 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2898 | } |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2899 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2900 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 2901 | Py_XDECREF(errorHandler); |
| 2902 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2903 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2904 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2905 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2906 | PyErr_SetString( |
| 2907 | PyExc_UnicodeError, |
| 2908 | "\\N escapes not supported (can't load unicodedata module)" |
| 2909 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2910 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2911 | Py_XDECREF(errorHandler); |
| 2912 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 2913 | return NULL; |
| 2914 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2915 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2916 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2917 | Py_XDECREF(errorHandler); |
| 2918 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2919 | return NULL; |
| 2920 | } |
| 2921 | |
| 2922 | /* Return a Unicode-Escape string version of the Unicode object. |
| 2923 | |
| 2924 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 2925 | appropriate. |
| 2926 | |
| 2927 | */ |
| 2928 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 2929 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2930 | Py_ssize_t size, |
| 2931 | Py_UNICODE ch) |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 2932 | { |
| 2933 | /* like wcschr, but doesn't stop at NULL characters */ |
| 2934 | |
| 2935 | while (size-- > 0) { |
| 2936 | if (*s == ch) |
| 2937 | return s; |
| 2938 | s++; |
| 2939 | } |
| 2940 | |
| 2941 | return NULL; |
| 2942 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 2943 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2944 | static |
| 2945 | PyObject *unicodeescape_string(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2946 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2947 | int quotes) |
| 2948 | { |
| 2949 | PyObject *repr; |
| 2950 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2951 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2952 | static const char *hexdigit = "0123456789abcdef"; |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2953 | #ifdef Py_UNICODE_WIDE |
| 2954 | const Py_ssize_t expandsize = 10; |
| 2955 | #else |
| 2956 | const Py_ssize_t expandsize = 6; |
| 2957 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2958 | |
Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 2959 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 2960 | better to choose a different scheme. Perhaps scan the |
| 2961 | first N-chars of the string and allocate based on that size. |
| 2962 | */ |
| 2963 | /* Initial allocation is based on the longest-possible unichr |
| 2964 | escape. |
| 2965 | |
| 2966 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 2967 | unichr, so in this case it's the longest unichr escape. In |
| 2968 | narrow (UTF-16) builds this is five chars per source unichr |
| 2969 | since there are two unichrs in the surrogate pair, so in narrow |
| 2970 | (UTF-16) builds it's not the longest unichr escape. |
| 2971 | |
| 2972 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 2973 | so in the narrow (UTF-16) build case it's the longest unichr |
| 2974 | escape. |
| 2975 | */ |
| 2976 | |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2977 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2978 | return PyErr_NoMemory(); |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2979 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2980 | repr = PyString_FromStringAndSize(NULL, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2981 | 2 |
| 2982 | + expandsize*size |
| 2983 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2984 | if (repr == NULL) |
| 2985 | return NULL; |
| 2986 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2987 | p = PyString_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2988 | |
| 2989 | if (quotes) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2990 | *p++ = 'u'; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2991 | *p++ = (findchar(s, size, '\'') && |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2992 | !findchar(s, size, '"')) ? '"' : '\''; |
| 2993 | } |
| 2994 | while (size-- > 0) { |
| 2995 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2996 | |
Hye-Shik Chang | 835b243 | 2005-12-17 04:38:31 +0000 | [diff] [blame] | 2997 | /* Escape quotes and backslashes */ |
| 2998 | if ((quotes && |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2999 | ch == (Py_UNICODE) PyString_AS_STRING(repr)[1]) || ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3000 | *p++ = '\\'; |
| 3001 | *p++ = (char) ch; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3002 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3003 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3004 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 3005 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3006 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 3007 | else if (ch >= 0x10000) { |
| 3008 | *p++ = '\\'; |
| 3009 | *p++ = 'U'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3010 | *p++ = hexdigit[(ch >> 28) & 0x0000000F]; |
| 3011 | *p++ = hexdigit[(ch >> 24) & 0x0000000F]; |
| 3012 | *p++ = hexdigit[(ch >> 20) & 0x0000000F]; |
| 3013 | *p++ = hexdigit[(ch >> 16) & 0x0000000F]; |
| 3014 | *p++ = hexdigit[(ch >> 12) & 0x0000000F]; |
| 3015 | *p++ = hexdigit[(ch >> 8) & 0x0000000F]; |
| 3016 | *p++ = hexdigit[(ch >> 4) & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3017 | *p++ = hexdigit[ch & 0x0000000F]; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3018 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3019 | } |
Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 3020 | #else |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3021 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3022 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 3023 | Py_UNICODE ch2; |
| 3024 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3025 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3026 | ch2 = *s++; |
| 3027 | size--; |
| 3028 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3029 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3030 | *p++ = '\\'; |
| 3031 | *p++ = 'U'; |
| 3032 | *p++ = hexdigit[(ucs >> 28) & 0x0000000F]; |
| 3033 | *p++ = hexdigit[(ucs >> 24) & 0x0000000F]; |
| 3034 | *p++ = hexdigit[(ucs >> 20) & 0x0000000F]; |
| 3035 | *p++ = hexdigit[(ucs >> 16) & 0x0000000F]; |
| 3036 | *p++ = hexdigit[(ucs >> 12) & 0x0000000F]; |
| 3037 | *p++ = hexdigit[(ucs >> 8) & 0x0000000F]; |
| 3038 | *p++ = hexdigit[(ucs >> 4) & 0x0000000F]; |
| 3039 | *p++ = hexdigit[ucs & 0x0000000F]; |
| 3040 | continue; |
| 3041 | } |
| 3042 | /* Fall through: isolated surrogates are copied as-is */ |
| 3043 | s--; |
| 3044 | size++; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 3045 | } |
Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 3046 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3047 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3048 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3049 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3050 | *p++ = '\\'; |
| 3051 | *p++ = 'u'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3052 | *p++ = hexdigit[(ch >> 12) & 0x000F]; |
| 3053 | *p++ = hexdigit[(ch >> 8) & 0x000F]; |
| 3054 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 3055 | *p++ = hexdigit[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3056 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3057 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3058 | /* Map special whitespace to '\t', \n', '\r' */ |
| 3059 | else if (ch == '\t') { |
| 3060 | *p++ = '\\'; |
| 3061 | *p++ = 't'; |
| 3062 | } |
| 3063 | else if (ch == '\n') { |
| 3064 | *p++ = '\\'; |
| 3065 | *p++ = 'n'; |
| 3066 | } |
| 3067 | else if (ch == '\r') { |
| 3068 | *p++ = '\\'; |
| 3069 | *p++ = 'r'; |
| 3070 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3071 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3072 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 3073 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3074 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3075 | *p++ = 'x'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3076 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 3077 | *p++ = hexdigit[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3078 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3079 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3080 | /* Copy everything else as-is */ |
| 3081 | else |
| 3082 | *p++ = (char) ch; |
| 3083 | } |
| 3084 | if (quotes) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3085 | *p++ = PyString_AS_STRING(repr)[1]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3086 | |
| 3087 | *p = '\0'; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3088 | _PyString_Resize(&repr, p - PyString_AS_STRING(repr)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3089 | return repr; |
| 3090 | } |
| 3091 | |
| 3092 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3093 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3094 | { |
| 3095 | return unicodeescape_string(s, size, 0); |
| 3096 | } |
| 3097 | |
| 3098 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 3099 | { |
| 3100 | if (!PyUnicode_Check(unicode)) { |
| 3101 | PyErr_BadArgument(); |
| 3102 | return NULL; |
| 3103 | } |
| 3104 | return PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3105 | PyUnicode_GET_SIZE(unicode)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3106 | } |
| 3107 | |
| 3108 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 3109 | |
| 3110 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3111 | Py_ssize_t size, |
| 3112 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3113 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3114 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3115 | Py_ssize_t startinpos; |
| 3116 | Py_ssize_t endinpos; |
| 3117 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3118 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3119 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3120 | const char *end; |
| 3121 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3122 | PyObject *errorHandler = NULL; |
| 3123 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3124 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3125 | /* Escaped strings will always be longer than the resulting |
| 3126 | 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] | 3127 | length after conversion to the true value. (But decoding error |
| 3128 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3129 | v = _PyUnicode_New(size); |
| 3130 | if (v == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3131 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3132 | if (size == 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3133 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3134 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3135 | end = s + size; |
| 3136 | while (s < end) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3137 | unsigned char c; |
| 3138 | Py_UCS4 x; |
| 3139 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3140 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3141 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3142 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3143 | if (*s != '\\') { |
| 3144 | *p++ = (unsigned char)*s++; |
| 3145 | continue; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 3146 | } |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3147 | startinpos = s-starts; |
| 3148 | |
| 3149 | /* \u-escapes are only interpreted iff the number of leading |
| 3150 | backslashes if odd */ |
| 3151 | bs = s; |
| 3152 | for (;s < end;) { |
| 3153 | if (*s != '\\') |
| 3154 | break; |
| 3155 | *p++ = (unsigned char)*s++; |
| 3156 | } |
| 3157 | if (((s - bs) & 1) == 0 || |
| 3158 | s >= end || |
| 3159 | (*s != 'u' && *s != 'U')) { |
| 3160 | continue; |
| 3161 | } |
| 3162 | p--; |
| 3163 | count = *s=='u' ? 4 : 8; |
| 3164 | s++; |
| 3165 | |
| 3166 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 3167 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3168 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 3169 | c = (unsigned char)*s; |
| 3170 | if (!isxdigit(c)) { |
| 3171 | endinpos = s-starts; |
| 3172 | if (unicode_decode_call_errorhandler( |
| 3173 | errors, &errorHandler, |
| 3174 | "rawunicodeescape", "truncated \\uXXXX", |
| 3175 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3176 | &v, &outpos, &p)) |
| 3177 | goto onError; |
| 3178 | goto nextByte; |
| 3179 | } |
| 3180 | x = (x<<4) & ~0xF; |
| 3181 | if (c >= '0' && c <= '9') |
| 3182 | x += c - '0'; |
| 3183 | else if (c >= 'a' && c <= 'f') |
| 3184 | x += 10 + c - 'a'; |
| 3185 | else |
| 3186 | x += 10 + c - 'A'; |
| 3187 | } |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3188 | if (x <= 0xffff) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3189 | /* UCS-2 character */ |
| 3190 | *p++ = (Py_UNICODE) x; |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3191 | else if (x <= 0x10ffff) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3192 | /* UCS-4 character. Either store directly, or as |
| 3193 | surrogate pair. */ |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3194 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3195 | *p++ = (Py_UNICODE) x; |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3196 | #else |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3197 | x -= 0x10000L; |
| 3198 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 3199 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3200 | #endif |
| 3201 | } else { |
| 3202 | endinpos = s-starts; |
| 3203 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3204 | if (unicode_decode_call_errorhandler( |
| 3205 | errors, &errorHandler, |
| 3206 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3207 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3208 | &v, &outpos, &p)) |
| 3209 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3210 | } |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3211 | nextByte: |
| 3212 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3213 | } |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3214 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3215 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3216 | Py_XDECREF(errorHandler); |
| 3217 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3218 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3219 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3220 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3221 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3222 | Py_XDECREF(errorHandler); |
| 3223 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3224 | return NULL; |
| 3225 | } |
| 3226 | |
| 3227 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3228 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3229 | { |
| 3230 | PyObject *repr; |
| 3231 | char *p; |
| 3232 | char *q; |
| 3233 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3234 | static const char *hexdigit = "0123456789abcdef"; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3235 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 3236 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3237 | #else |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 3238 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3239 | #endif |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 3240 | |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 3241 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3242 | return PyErr_NoMemory(); |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 3243 | |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 3244 | repr = PyString_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3245 | if (repr == NULL) |
| 3246 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3247 | if (size == 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3248 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3249 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3250 | p = q = PyString_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3251 | while (size-- > 0) { |
| 3252 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3253 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3254 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 3255 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3256 | *p++ = '\\'; |
| 3257 | *p++ = 'U'; |
| 3258 | *p++ = hexdigit[(ch >> 28) & 0xf]; |
| 3259 | *p++ = hexdigit[(ch >> 24) & 0xf]; |
| 3260 | *p++ = hexdigit[(ch >> 20) & 0xf]; |
| 3261 | *p++ = hexdigit[(ch >> 16) & 0xf]; |
| 3262 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 3263 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 3264 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 3265 | *p++ = hexdigit[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3266 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3267 | else |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3268 | #else |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3269 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3270 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 3271 | Py_UNICODE ch2; |
| 3272 | Py_UCS4 ucs; |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3273 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3274 | ch2 = *s++; |
| 3275 | size--; |
| 3276 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3277 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3278 | *p++ = '\\'; |
| 3279 | *p++ = 'U'; |
| 3280 | *p++ = hexdigit[(ucs >> 28) & 0xf]; |
| 3281 | *p++ = hexdigit[(ucs >> 24) & 0xf]; |
| 3282 | *p++ = hexdigit[(ucs >> 20) & 0xf]; |
| 3283 | *p++ = hexdigit[(ucs >> 16) & 0xf]; |
| 3284 | *p++ = hexdigit[(ucs >> 12) & 0xf]; |
| 3285 | *p++ = hexdigit[(ucs >> 8) & 0xf]; |
| 3286 | *p++ = hexdigit[(ucs >> 4) & 0xf]; |
| 3287 | *p++ = hexdigit[ucs & 0xf]; |
| 3288 | continue; |
| 3289 | } |
| 3290 | /* Fall through: isolated surrogates are copied as-is */ |
| 3291 | s--; |
| 3292 | size++; |
| 3293 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3294 | #endif |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3295 | /* Map 16-bit characters to '\uxxxx' */ |
| 3296 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3297 | *p++ = '\\'; |
| 3298 | *p++ = 'u'; |
| 3299 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 3300 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 3301 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 3302 | *p++ = hexdigit[ch & 15]; |
| 3303 | } |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3304 | /* Copy everything else as-is */ |
| 3305 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3306 | *p++ = (char) ch; |
| 3307 | } |
| 3308 | *p = '\0'; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3309 | _PyString_Resize(&repr, p - q); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3310 | return repr; |
| 3311 | } |
| 3312 | |
| 3313 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 3314 | { |
| 3315 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3316 | PyErr_BadArgument(); |
| 3317 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3318 | } |
| 3319 | return PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3320 | PyUnicode_GET_SIZE(unicode)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3321 | } |
| 3322 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3323 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 3324 | |
| 3325 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3326 | Py_ssize_t size, |
| 3327 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3328 | { |
| 3329 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3330 | Py_ssize_t startinpos; |
| 3331 | Py_ssize_t endinpos; |
| 3332 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3333 | PyUnicodeObject *v; |
| 3334 | Py_UNICODE *p; |
| 3335 | const char *end; |
| 3336 | const char *reason; |
| 3337 | PyObject *errorHandler = NULL; |
| 3338 | PyObject *exc = NULL; |
| 3339 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 3340 | #ifdef Py_UNICODE_WIDE |
| 3341 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 3342 | #endif |
| 3343 | |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 3344 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3345 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 3346 | if (v == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3347 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3348 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3349 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3350 | p = PyUnicode_AS_UNICODE(v); |
| 3351 | end = s + size; |
| 3352 | |
| 3353 | while (s < end) { |
Neal Norwitz | 1004a53 | 2006-05-15 07:17:23 +0000 | [diff] [blame] | 3354 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3355 | /* We have to sanity check the raw data, otherwise doom looms for |
| 3356 | some malformed UCS-4 data. */ |
| 3357 | if ( |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3358 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3359 | *p > unimax || *p < 0 || |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3360 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3361 | end-s < Py_UNICODE_SIZE |
| 3362 | ) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3363 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3364 | startinpos = s - starts; |
| 3365 | if (end-s < Py_UNICODE_SIZE) { |
| 3366 | endinpos = end-starts; |
| 3367 | reason = "truncated input"; |
| 3368 | } |
| 3369 | else { |
| 3370 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 3371 | reason = "illegal code point (> 0x10FFFF)"; |
| 3372 | } |
| 3373 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 3374 | if (unicode_decode_call_errorhandler( |
| 3375 | errors, &errorHandler, |
| 3376 | "unicode_internal", reason, |
| 3377 | starts, size, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 3378 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3379 | goto onError; |
| 3380 | } |
| 3381 | } |
| 3382 | else { |
| 3383 | p++; |
| 3384 | s += Py_UNICODE_SIZE; |
| 3385 | } |
| 3386 | } |
| 3387 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3388 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3389 | goto onError; |
| 3390 | Py_XDECREF(errorHandler); |
| 3391 | Py_XDECREF(exc); |
| 3392 | return (PyObject *)v; |
| 3393 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3394 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3395 | Py_XDECREF(v); |
| 3396 | Py_XDECREF(errorHandler); |
| 3397 | Py_XDECREF(exc); |
| 3398 | return NULL; |
| 3399 | } |
| 3400 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3401 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 3402 | |
| 3403 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3404 | Py_ssize_t size, |
| 3405 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3406 | { |
| 3407 | PyUnicodeObject *v; |
| 3408 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3409 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3410 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3411 | if (size == 1) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3412 | Py_UNICODE r = *(unsigned char*)s; |
| 3413 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3414 | } |
| 3415 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3416 | v = _PyUnicode_New(size); |
| 3417 | if (v == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3418 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3419 | if (size == 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3420 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3421 | p = PyUnicode_AS_UNICODE(v); |
| 3422 | while (size-- > 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3423 | *p++ = (unsigned char)*s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3424 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3425 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3426 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3427 | Py_XDECREF(v); |
| 3428 | return NULL; |
| 3429 | } |
| 3430 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3431 | /* create or adjust a UnicodeEncodeError */ |
| 3432 | static void make_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3433 | const char *encoding, |
| 3434 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3435 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3436 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3437 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3438 | if (*exceptionObject == NULL) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3439 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 3440 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3441 | } |
| 3442 | else { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3443 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 3444 | goto onError; |
| 3445 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 3446 | goto onError; |
| 3447 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 3448 | goto onError; |
| 3449 | return; |
| 3450 | onError: |
| 3451 | Py_DECREF(*exceptionObject); |
| 3452 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3453 | } |
| 3454 | } |
| 3455 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3456 | /* raises a UnicodeEncodeError */ |
| 3457 | static void raise_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3458 | const char *encoding, |
| 3459 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3460 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3461 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3462 | { |
| 3463 | make_encode_exception(exceptionObject, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3464 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3465 | if (*exceptionObject != NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3466 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3467 | } |
| 3468 | |
| 3469 | /* error handling callback helper: |
| 3470 | build arguments, call the callback and check the arguments, |
| 3471 | put the result into newpos and return the replacement string, which |
| 3472 | has to be freed by the caller */ |
| 3473 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3474 | PyObject **errorHandler, |
| 3475 | const char *encoding, const char *reason, |
| 3476 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 3477 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3478 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3479 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3480 | static char *argparse = "O!n;encoding error handler must return (unicode, int) tuple"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3481 | |
| 3482 | PyObject *restuple; |
| 3483 | PyObject *resunicode; |
| 3484 | |
| 3485 | if (*errorHandler == NULL) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3486 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3487 | if (*errorHandler == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3488 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3489 | } |
| 3490 | |
| 3491 | make_encode_exception(exceptionObject, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3492 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3493 | if (*exceptionObject == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3494 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3495 | |
| 3496 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3497 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3498 | if (restuple == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3499 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3500 | if (!PyTuple_Check(restuple)) { |
Georg Brandl | cbb4958 | 2009-02-13 11:06:59 +0000 | [diff] [blame] | 3501 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3502 | Py_DECREF(restuple); |
| 3503 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3504 | } |
| 3505 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3506 | &resunicode, newpos)) { |
| 3507 | Py_DECREF(restuple); |
| 3508 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3509 | } |
| 3510 | if (*newpos<0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3511 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3512 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3513 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 3514 | Py_DECREF(restuple); |
| 3515 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3516 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3517 | Py_INCREF(resunicode); |
| 3518 | Py_DECREF(restuple); |
| 3519 | return resunicode; |
| 3520 | } |
| 3521 | |
| 3522 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3523 | Py_ssize_t size, |
| 3524 | const char *errors, |
| 3525 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3526 | { |
| 3527 | /* output object */ |
| 3528 | PyObject *res; |
| 3529 | /* pointers to the beginning and end+1 of input */ |
| 3530 | const Py_UNICODE *startp = p; |
| 3531 | const Py_UNICODE *endp = p + size; |
| 3532 | /* pointer to the beginning of the unencodable characters */ |
| 3533 | /* const Py_UNICODE *badp = NULL; */ |
| 3534 | /* pointer into the output */ |
| 3535 | char *str; |
| 3536 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3537 | Py_ssize_t respos = 0; |
| 3538 | Py_ssize_t ressize; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3539 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 3540 | 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] | 3541 | PyObject *errorHandler = NULL; |
| 3542 | PyObject *exc = NULL; |
| 3543 | /* the following variable is used for caching string comparisons |
| 3544 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 3545 | int known_errorHandler = -1; |
| 3546 | |
| 3547 | /* allocate enough for a simple encoding without |
| 3548 | replacements, if we need more, we'll resize */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3549 | res = PyString_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3550 | if (res == NULL) |
| 3551 | goto onError; |
| 3552 | if (size == 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3553 | return res; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3554 | str = PyString_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3555 | ressize = size; |
| 3556 | |
| 3557 | while (p<endp) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3558 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3559 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3560 | /* can we encode this? */ |
| 3561 | if (c<limit) { |
| 3562 | /* no overflow check, because we know that the space is enough */ |
| 3563 | *str++ = (char)c; |
| 3564 | ++p; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 3565 | } |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3566 | else { |
| 3567 | Py_ssize_t unicodepos = p-startp; |
| 3568 | Py_ssize_t requiredsize; |
| 3569 | PyObject *repunicode; |
| 3570 | Py_ssize_t repsize; |
| 3571 | Py_ssize_t newpos; |
| 3572 | Py_ssize_t respos; |
| 3573 | Py_UNICODE *uni2; |
| 3574 | /* startpos for collecting unencodable chars */ |
| 3575 | const Py_UNICODE *collstart = p; |
| 3576 | const Py_UNICODE *collend = p; |
| 3577 | /* find all unecodable characters */ |
| 3578 | while ((collend < endp) && ((*collend)>=limit)) |
| 3579 | ++collend; |
| 3580 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 3581 | if (known_errorHandler==-1) { |
| 3582 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3583 | known_errorHandler = 1; |
| 3584 | else if (!strcmp(errors, "replace")) |
| 3585 | known_errorHandler = 2; |
| 3586 | else if (!strcmp(errors, "ignore")) |
| 3587 | known_errorHandler = 3; |
| 3588 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3589 | known_errorHandler = 4; |
| 3590 | else |
| 3591 | known_errorHandler = 0; |
| 3592 | } |
| 3593 | switch (known_errorHandler) { |
| 3594 | case 1: /* strict */ |
| 3595 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 3596 | goto onError; |
| 3597 | case 2: /* replace */ |
| 3598 | while (collstart++<collend) |
| 3599 | *str++ = '?'; /* fall through */ |
| 3600 | case 3: /* ignore */ |
| 3601 | p = collend; |
| 3602 | break; |
| 3603 | case 4: /* xmlcharrefreplace */ |
| 3604 | respos = str-PyString_AS_STRING(res); |
| 3605 | /* determine replacement size (temporarily (mis)uses p) */ |
| 3606 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 3607 | if (*p<10) |
| 3608 | repsize += 2+1+1; |
| 3609 | else if (*p<100) |
| 3610 | repsize += 2+2+1; |
| 3611 | else if (*p<1000) |
| 3612 | repsize += 2+3+1; |
| 3613 | else if (*p<10000) |
| 3614 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 3615 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3616 | else |
| 3617 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 3618 | #else |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3619 | else if (*p<100000) |
| 3620 | repsize += 2+5+1; |
| 3621 | else if (*p<1000000) |
| 3622 | repsize += 2+6+1; |
| 3623 | else |
| 3624 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3625 | #endif |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3626 | } |
| 3627 | requiredsize = respos+repsize+(endp-collend); |
| 3628 | if (requiredsize > ressize) { |
| 3629 | if (requiredsize<2*ressize) |
| 3630 | requiredsize = 2*ressize; |
| 3631 | if (_PyString_Resize(&res, requiredsize)) |
| 3632 | goto onError; |
| 3633 | str = PyString_AS_STRING(res) + respos; |
| 3634 | ressize = requiredsize; |
| 3635 | } |
| 3636 | /* generate replacement (temporarily (mis)uses p) */ |
| 3637 | for (p = collstart; p < collend; ++p) { |
| 3638 | str += sprintf(str, "&#%d;", (int)*p); |
| 3639 | } |
| 3640 | p = collend; |
| 3641 | break; |
| 3642 | default: |
| 3643 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 3644 | encoding, reason, startp, size, &exc, |
| 3645 | collstart-startp, collend-startp, &newpos); |
| 3646 | if (repunicode == NULL) |
| 3647 | goto onError; |
| 3648 | /* need more space? (at least enough for what we have+the |
| 3649 | replacement+the rest of the string, so we won't have to |
| 3650 | check space for encodable characters) */ |
| 3651 | respos = str-PyString_AS_STRING(res); |
| 3652 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3653 | requiredsize = respos+repsize+(endp-collend); |
| 3654 | if (requiredsize > ressize) { |
| 3655 | if (requiredsize<2*ressize) |
| 3656 | requiredsize = 2*ressize; |
| 3657 | if (_PyString_Resize(&res, requiredsize)) { |
| 3658 | Py_DECREF(repunicode); |
| 3659 | goto onError; |
| 3660 | } |
| 3661 | str = PyString_AS_STRING(res) + respos; |
| 3662 | ressize = requiredsize; |
| 3663 | } |
| 3664 | /* check if there is anything unencodable in the replacement |
| 3665 | and copy it to the output */ |
| 3666 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 3667 | c = *uni2; |
| 3668 | if (c >= limit) { |
| 3669 | raise_encode_exception(&exc, encoding, startp, size, |
| 3670 | unicodepos, unicodepos+1, reason); |
| 3671 | Py_DECREF(repunicode); |
| 3672 | goto onError; |
| 3673 | } |
| 3674 | *str = (char)c; |
| 3675 | } |
| 3676 | p = startp + newpos; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 3677 | Py_DECREF(repunicode); |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 3678 | } |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 3679 | } |
| 3680 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3681 | /* Resize if we allocated to much */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3682 | respos = str-PyString_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3683 | if (respos<ressize) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3684 | /* If this falls res will be NULL */ |
| 3685 | _PyString_Resize(&res, respos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3686 | Py_XDECREF(errorHandler); |
| 3687 | Py_XDECREF(exc); |
| 3688 | return res; |
| 3689 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3690 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3691 | Py_XDECREF(res); |
| 3692 | Py_XDECREF(errorHandler); |
| 3693 | Py_XDECREF(exc); |
| 3694 | return NULL; |
| 3695 | } |
| 3696 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3697 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3698 | Py_ssize_t size, |
| 3699 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3700 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3701 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3702 | } |
| 3703 | |
| 3704 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 3705 | { |
| 3706 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3707 | PyErr_BadArgument(); |
| 3708 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3709 | } |
| 3710 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3711 | PyUnicode_GET_SIZE(unicode), |
| 3712 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3713 | } |
| 3714 | |
| 3715 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 3716 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3717 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3718 | Py_ssize_t size, |
| 3719 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3720 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3721 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3722 | PyUnicodeObject *v; |
| 3723 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3724 | Py_ssize_t startinpos; |
| 3725 | Py_ssize_t endinpos; |
| 3726 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3727 | const char *e; |
| 3728 | PyObject *errorHandler = NULL; |
| 3729 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3730 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3731 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3732 | if (size == 1 && *(unsigned char*)s < 128) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3733 | Py_UNICODE r = *(unsigned char*)s; |
| 3734 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3735 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3736 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3737 | v = _PyUnicode_New(size); |
| 3738 | if (v == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3739 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3740 | if (size == 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3741 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3742 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3743 | e = s + size; |
| 3744 | while (s < e) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3745 | register unsigned char c = (unsigned char)*s; |
| 3746 | if (c < 128) { |
| 3747 | *p++ = c; |
| 3748 | ++s; |
| 3749 | } |
| 3750 | else { |
| 3751 | startinpos = s-starts; |
| 3752 | endinpos = startinpos + 1; |
| 3753 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
| 3754 | if (unicode_decode_call_errorhandler( |
| 3755 | errors, &errorHandler, |
| 3756 | "ascii", "ordinal not in range(128)", |
| 3757 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3758 | &v, &outpos, &p)) |
| 3759 | goto onError; |
| 3760 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3761 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3762 | if (p - PyUnicode_AS_UNICODE(v) < PyString_GET_SIZE(v)) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3763 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 3764 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3765 | Py_XDECREF(errorHandler); |
| 3766 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3767 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3768 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3769 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3770 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3771 | Py_XDECREF(errorHandler); |
| 3772 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3773 | return NULL; |
| 3774 | } |
| 3775 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3776 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3777 | Py_ssize_t size, |
| 3778 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3779 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3780 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3781 | } |
| 3782 | |
| 3783 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 3784 | { |
| 3785 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3786 | PyErr_BadArgument(); |
| 3787 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3788 | } |
| 3789 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3790 | PyUnicode_GET_SIZE(unicode), |
| 3791 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3792 | } |
| 3793 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3794 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3795 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3796 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3797 | |
Hirokazu Yamamoto | 52a3492 | 2009-03-21 10:32:52 +0000 | [diff] [blame] | 3798 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3799 | #define NEED_RETRY |
| 3800 | #endif |
| 3801 | |
| 3802 | /* XXX This code is limited to "true" double-byte encodings, as |
| 3803 | a) it assumes an incomplete character consists of a single byte, and |
| 3804 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3805 | encodings, see IsDBCSLeadByteEx documentation. */ |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3806 | |
| 3807 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 3808 | { |
| 3809 | const char *curr = s + offset; |
| 3810 | |
| 3811 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3812 | const char *prev = CharPrev(s, curr); |
| 3813 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3814 | } |
| 3815 | return 0; |
| 3816 | } |
| 3817 | |
| 3818 | /* |
| 3819 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 3820 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 3821 | */ |
| 3822 | static int decode_mbcs(PyUnicodeObject **v, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3823 | const char *s, /* MBCS string */ |
| 3824 | int size, /* sizeof MBCS string */ |
| 3825 | int final) |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3826 | { |
| 3827 | Py_UNICODE *p; |
| 3828 | Py_ssize_t n = 0; |
| 3829 | int usize = 0; |
| 3830 | |
| 3831 | assert(size >= 0); |
| 3832 | |
| 3833 | /* Skip trailing lead-byte unless 'final' is set */ |
| 3834 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3835 | --size; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3836 | |
| 3837 | /* First get the size of the result */ |
| 3838 | if (size > 0) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3839 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 3840 | if (usize == 0) { |
| 3841 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3842 | return -1; |
| 3843 | } |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3844 | } |
| 3845 | |
| 3846 | if (*v == NULL) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3847 | /* Create unicode object */ |
| 3848 | *v = _PyUnicode_New(usize); |
| 3849 | if (*v == NULL) |
| 3850 | return -1; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3851 | } |
| 3852 | else { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3853 | /* Extend unicode object */ |
| 3854 | n = PyUnicode_GET_SIZE(*v); |
| 3855 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 3856 | return -1; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3857 | } |
| 3858 | |
| 3859 | /* Do the conversion */ |
| 3860 | if (size > 0) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3861 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 3862 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 3863 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3864 | return -1; |
| 3865 | } |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3866 | } |
| 3867 | |
| 3868 | return size; |
| 3869 | } |
| 3870 | |
| 3871 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3872 | Py_ssize_t size, |
| 3873 | const char *errors, |
| 3874 | Py_ssize_t *consumed) |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3875 | { |
| 3876 | PyUnicodeObject *v = NULL; |
| 3877 | int done; |
| 3878 | |
| 3879 | if (consumed) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3880 | *consumed = 0; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3881 | |
| 3882 | #ifdef NEED_RETRY |
| 3883 | retry: |
| 3884 | if (size > INT_MAX) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3885 | done = decode_mbcs(&v, s, INT_MAX, 0); |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3886 | else |
| 3887 | #endif |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3888 | done = decode_mbcs(&v, s, (int)size, !consumed); |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3889 | |
| 3890 | if (done < 0) { |
| 3891 | Py_XDECREF(v); |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3892 | return NULL; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3893 | } |
| 3894 | |
| 3895 | if (consumed) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3896 | *consumed += done; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3897 | |
| 3898 | #ifdef NEED_RETRY |
| 3899 | if (size > INT_MAX) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3900 | s += done; |
| 3901 | size -= done; |
| 3902 | goto retry; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3903 | } |
| 3904 | #endif |
| 3905 | |
| 3906 | return (PyObject *)v; |
| 3907 | } |
| 3908 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3909 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3910 | Py_ssize_t size, |
| 3911 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3912 | { |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3913 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 3914 | } |
| 3915 | |
| 3916 | /* |
| 3917 | * Convert unicode into string object (MBCS). |
| 3918 | * Returns 0 if succeed, -1 otherwise. |
| 3919 | */ |
| 3920 | static int encode_mbcs(PyObject **repr, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3921 | const Py_UNICODE *p, /* unicode */ |
| 3922 | int size) /* size of unicode */ |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3923 | { |
| 3924 | int mbcssize = 0; |
| 3925 | Py_ssize_t n = 0; |
| 3926 | |
| 3927 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3928 | |
| 3929 | /* First get the size of the result */ |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3930 | if (size > 0) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3931 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 3932 | if (mbcssize == 0) { |
| 3933 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3934 | return -1; |
| 3935 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3936 | } |
| 3937 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3938 | if (*repr == NULL) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3939 | /* Create string object */ |
| 3940 | *repr = PyString_FromStringAndSize(NULL, mbcssize); |
| 3941 | if (*repr == NULL) |
| 3942 | return -1; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3943 | } |
| 3944 | else { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3945 | /* Extend string object */ |
| 3946 | n = PyString_Size(*repr); |
| 3947 | if (_PyString_Resize(repr, n + mbcssize) < 0) |
| 3948 | return -1; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3949 | } |
| 3950 | |
| 3951 | /* Do the conversion */ |
| 3952 | if (size > 0) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3953 | char *s = PyString_AS_STRING(*repr) + n; |
| 3954 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 3955 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3956 | return -1; |
| 3957 | } |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3958 | } |
| 3959 | |
| 3960 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3961 | } |
| 3962 | |
| 3963 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3964 | Py_ssize_t size, |
| 3965 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3966 | { |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3967 | PyObject *repr = NULL; |
| 3968 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 3969 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3970 | #ifdef NEED_RETRY |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3971 | retry: |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3972 | if (size > INT_MAX) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3973 | ret = encode_mbcs(&repr, p, INT_MAX); |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3974 | else |
| 3975 | #endif |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3976 | ret = encode_mbcs(&repr, p, (int)size); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3977 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3978 | if (ret < 0) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3979 | Py_XDECREF(repr); |
| 3980 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3981 | } |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3982 | |
| 3983 | #ifdef NEED_RETRY |
| 3984 | if (size > INT_MAX) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3985 | p += INT_MAX; |
| 3986 | size -= INT_MAX; |
| 3987 | goto retry; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3988 | } |
| 3989 | #endif |
| 3990 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3991 | return repr; |
| 3992 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3993 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3994 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 3995 | { |
| 3996 | if (!PyUnicode_Check(unicode)) { |
| 3997 | PyErr_BadArgument(); |
| 3998 | return NULL; |
| 3999 | } |
| 4000 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4001 | PyUnicode_GET_SIZE(unicode), |
| 4002 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4003 | } |
| 4004 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 4005 | #undef NEED_RETRY |
| 4006 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4007 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4008 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4009 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 4010 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4011 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4012 | Py_ssize_t size, |
| 4013 | PyObject *mapping, |
| 4014 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4015 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4016 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4017 | Py_ssize_t startinpos; |
| 4018 | Py_ssize_t endinpos; |
| 4019 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4020 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4021 | PyUnicodeObject *v; |
| 4022 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4023 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4024 | PyObject *errorHandler = NULL; |
| 4025 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4026 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4027 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4028 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4029 | /* Default to Latin-1 */ |
| 4030 | if (mapping == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4031 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4032 | |
| 4033 | v = _PyUnicode_New(size); |
| 4034 | if (v == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4035 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4036 | if (size == 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4037 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4038 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4039 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4040 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4041 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 4042 | maplen = PyUnicode_GET_SIZE(mapping); |
| 4043 | while (s < e) { |
| 4044 | unsigned char ch = *s; |
| 4045 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4046 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4047 | if (ch < maplen) |
| 4048 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4049 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4050 | if (x == 0xfffe) { |
| 4051 | /* undefined mapping */ |
| 4052 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4053 | startinpos = s-starts; |
| 4054 | endinpos = startinpos+1; |
| 4055 | if (unicode_decode_call_errorhandler( |
| 4056 | errors, &errorHandler, |
| 4057 | "charmap", "character maps to <undefined>", |
| 4058 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 4059 | &v, &outpos, &p)) { |
| 4060 | goto onError; |
| 4061 | } |
| 4062 | continue; |
| 4063 | } |
| 4064 | *p++ = x; |
| 4065 | ++s; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4066 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4067 | } |
| 4068 | else { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4069 | while (s < e) { |
| 4070 | unsigned char ch = *s; |
| 4071 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4072 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4073 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 4074 | w = PyInt_FromLong((long)ch); |
| 4075 | if (w == NULL) |
| 4076 | goto onError; |
| 4077 | x = PyObject_GetItem(mapping, w); |
| 4078 | Py_DECREF(w); |
| 4079 | if (x == NULL) { |
| 4080 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4081 | /* No mapping found means: mapping is undefined. */ |
| 4082 | PyErr_Clear(); |
| 4083 | x = Py_None; |
| 4084 | Py_INCREF(x); |
| 4085 | } else |
| 4086 | goto onError; |
| 4087 | } |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4088 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4089 | /* Apply mapping */ |
| 4090 | if (PyInt_Check(x)) { |
| 4091 | long value = PyInt_AS_LONG(x); |
| 4092 | if (value < 0 || value > 65535) { |
| 4093 | PyErr_SetString(PyExc_TypeError, |
| 4094 | "character mapping must be in range(65536)"); |
| 4095 | Py_DECREF(x); |
| 4096 | goto onError; |
| 4097 | } |
| 4098 | *p++ = (Py_UNICODE)value; |
| 4099 | } |
| 4100 | else if (x == Py_None) { |
| 4101 | /* undefined mapping */ |
| 4102 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4103 | startinpos = s-starts; |
| 4104 | endinpos = startinpos+1; |
| 4105 | if (unicode_decode_call_errorhandler( |
| 4106 | errors, &errorHandler, |
| 4107 | "charmap", "character maps to <undefined>", |
| 4108 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 4109 | &v, &outpos, &p)) { |
| 4110 | Py_DECREF(x); |
| 4111 | goto onError; |
| 4112 | } |
| 4113 | Py_DECREF(x); |
| 4114 | continue; |
| 4115 | } |
| 4116 | else if (PyUnicode_Check(x)) { |
| 4117 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4118 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4119 | if (targetsize == 1) |
| 4120 | /* 1-1 mapping */ |
| 4121 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4122 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4123 | else if (targetsize > 1) { |
| 4124 | /* 1-n mapping */ |
| 4125 | if (targetsize > extrachars) { |
| 4126 | /* resize first */ |
| 4127 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 4128 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 4129 | (targetsize << 2); |
| 4130 | extrachars += needed; |
| 4131 | /* XXX overflow detection missing */ |
| 4132 | if (_PyUnicode_Resize(&v, |
| 4133 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 4134 | Py_DECREF(x); |
| 4135 | goto onError; |
| 4136 | } |
| 4137 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 4138 | } |
| 4139 | Py_UNICODE_COPY(p, |
| 4140 | PyUnicode_AS_UNICODE(x), |
| 4141 | targetsize); |
| 4142 | p += targetsize; |
| 4143 | extrachars -= targetsize; |
| 4144 | } |
| 4145 | /* 1-0 mapping: skip the character */ |
| 4146 | } |
| 4147 | else { |
| 4148 | /* wrong return value */ |
| 4149 | PyErr_SetString(PyExc_TypeError, |
| 4150 | "character mapping must return integer, None or unicode"); |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4151 | Py_DECREF(x); |
| 4152 | goto onError; |
| 4153 | } |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4154 | Py_DECREF(x); |
| 4155 | ++s; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4156 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4157 | } |
| 4158 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4159 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4160 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4161 | Py_XDECREF(errorHandler); |
| 4162 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4163 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4164 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4165 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4166 | Py_XDECREF(errorHandler); |
| 4167 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4168 | Py_XDECREF(v); |
| 4169 | return NULL; |
| 4170 | } |
| 4171 | |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4172 | /* Charmap encoding: the lookup table */ |
| 4173 | |
| 4174 | struct encoding_map{ |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4175 | PyObject_HEAD |
| 4176 | unsigned char level1[32]; |
| 4177 | int count2, count3; |
| 4178 | unsigned char level23[1]; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4179 | }; |
| 4180 | |
| 4181 | static PyObject* |
| 4182 | encoding_map_size(PyObject *obj, PyObject* args) |
| 4183 | { |
| 4184 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4185 | return PyInt_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4186 | 128*map->count3); |
| 4187 | } |
| 4188 | |
| 4189 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4190 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4191 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 4192 | { 0 } |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4193 | }; |
| 4194 | |
| 4195 | static void |
| 4196 | encoding_map_dealloc(PyObject* o) |
| 4197 | { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4198 | PyObject_FREE(o); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4199 | } |
| 4200 | |
| 4201 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4202 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4203 | "EncodingMap", /*tp_name*/ |
| 4204 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 4205 | 0, /*tp_itemsize*/ |
| 4206 | /* methods */ |
| 4207 | encoding_map_dealloc, /*tp_dealloc*/ |
| 4208 | 0, /*tp_print*/ |
| 4209 | 0, /*tp_getattr*/ |
| 4210 | 0, /*tp_setattr*/ |
| 4211 | 0, /*tp_compare*/ |
| 4212 | 0, /*tp_repr*/ |
| 4213 | 0, /*tp_as_number*/ |
| 4214 | 0, /*tp_as_sequence*/ |
| 4215 | 0, /*tp_as_mapping*/ |
| 4216 | 0, /*tp_hash*/ |
| 4217 | 0, /*tp_call*/ |
| 4218 | 0, /*tp_str*/ |
| 4219 | 0, /*tp_getattro*/ |
| 4220 | 0, /*tp_setattro*/ |
| 4221 | 0, /*tp_as_buffer*/ |
| 4222 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 4223 | 0, /*tp_doc*/ |
| 4224 | 0, /*tp_traverse*/ |
| 4225 | 0, /*tp_clear*/ |
| 4226 | 0, /*tp_richcompare*/ |
| 4227 | 0, /*tp_weaklistoffset*/ |
| 4228 | 0, /*tp_iter*/ |
| 4229 | 0, /*tp_iternext*/ |
| 4230 | encoding_map_methods, /*tp_methods*/ |
| 4231 | 0, /*tp_members*/ |
| 4232 | 0, /*tp_getset*/ |
| 4233 | 0, /*tp_base*/ |
| 4234 | 0, /*tp_dict*/ |
| 4235 | 0, /*tp_descr_get*/ |
| 4236 | 0, /*tp_descr_set*/ |
| 4237 | 0, /*tp_dictoffset*/ |
| 4238 | 0, /*tp_init*/ |
| 4239 | 0, /*tp_alloc*/ |
| 4240 | 0, /*tp_new*/ |
| 4241 | 0, /*tp_free*/ |
| 4242 | 0, /*tp_is_gc*/ |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4243 | }; |
| 4244 | |
| 4245 | PyObject* |
| 4246 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 4247 | { |
| 4248 | Py_UNICODE *decode; |
| 4249 | PyObject *result; |
| 4250 | struct encoding_map *mresult; |
| 4251 | int i; |
| 4252 | int need_dict = 0; |
| 4253 | unsigned char level1[32]; |
| 4254 | unsigned char level2[512]; |
| 4255 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 4256 | int count2 = 0, count3 = 0; |
| 4257 | |
| 4258 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 4259 | PyErr_BadArgument(); |
| 4260 | return NULL; |
| 4261 | } |
| 4262 | decode = PyUnicode_AS_UNICODE(string); |
| 4263 | memset(level1, 0xFF, sizeof level1); |
| 4264 | memset(level2, 0xFF, sizeof level2); |
| 4265 | |
| 4266 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 4267 | or if there are non-BMP characters, we need to use |
| 4268 | a mapping dictionary. */ |
| 4269 | if (decode[0] != 0) |
| 4270 | need_dict = 1; |
| 4271 | for (i = 1; i < 256; i++) { |
| 4272 | int l1, l2; |
| 4273 | if (decode[i] == 0 |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4274 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4275 | || decode[i] > 0xFFFF |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4276 | #endif |
| 4277 | ) { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4278 | need_dict = 1; |
| 4279 | break; |
| 4280 | } |
| 4281 | if (decode[i] == 0xFFFE) |
| 4282 | /* unmapped character */ |
| 4283 | continue; |
| 4284 | l1 = decode[i] >> 11; |
| 4285 | l2 = decode[i] >> 7; |
| 4286 | if (level1[l1] == 0xFF) |
| 4287 | level1[l1] = count2++; |
| 4288 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4289 | level2[l2] = count3++; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4290 | } |
| 4291 | |
| 4292 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 4293 | need_dict = 1; |
| 4294 | |
| 4295 | if (need_dict) { |
| 4296 | PyObject *result = PyDict_New(); |
| 4297 | PyObject *key, *value; |
| 4298 | if (!result) |
| 4299 | return NULL; |
| 4300 | for (i = 0; i < 256; i++) { |
| 4301 | key = value = NULL; |
| 4302 | key = PyInt_FromLong(decode[i]); |
| 4303 | value = PyInt_FromLong(i); |
| 4304 | if (!key || !value) |
| 4305 | goto failed1; |
| 4306 | if (PyDict_SetItem(result, key, value) == -1) |
| 4307 | goto failed1; |
Georg Brandl | 9f16760 | 2006-06-04 21:46:16 +0000 | [diff] [blame] | 4308 | Py_DECREF(key); |
| 4309 | Py_DECREF(value); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4310 | } |
| 4311 | return result; |
| 4312 | failed1: |
| 4313 | Py_XDECREF(key); |
| 4314 | Py_XDECREF(value); |
| 4315 | Py_DECREF(result); |
| 4316 | return NULL; |
| 4317 | } |
| 4318 | |
| 4319 | /* Create a three-level trie */ |
| 4320 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 4321 | 16*count2 + 128*count3 - 1); |
| 4322 | if (!result) |
| 4323 | return PyErr_NoMemory(); |
| 4324 | PyObject_Init(result, &EncodingMapType); |
| 4325 | mresult = (struct encoding_map*)result; |
| 4326 | mresult->count2 = count2; |
| 4327 | mresult->count3 = count3; |
| 4328 | mlevel1 = mresult->level1; |
| 4329 | mlevel2 = mresult->level23; |
| 4330 | mlevel3 = mresult->level23 + 16*count2; |
| 4331 | memcpy(mlevel1, level1, 32); |
| 4332 | memset(mlevel2, 0xFF, 16*count2); |
| 4333 | memset(mlevel3, 0, 128*count3); |
| 4334 | count3 = 0; |
| 4335 | for (i = 1; i < 256; i++) { |
| 4336 | int o1, o2, o3, i2, i3; |
| 4337 | if (decode[i] == 0xFFFE) |
| 4338 | /* unmapped character */ |
| 4339 | continue; |
| 4340 | o1 = decode[i]>>11; |
| 4341 | o2 = (decode[i]>>7) & 0xF; |
| 4342 | i2 = 16*mlevel1[o1] + o2; |
| 4343 | if (mlevel2[i2] == 0xFF) |
| 4344 | mlevel2[i2] = count3++; |
| 4345 | o3 = decode[i] & 0x7F; |
| 4346 | i3 = 128*mlevel2[i2] + o3; |
| 4347 | mlevel3[i3] = i; |
| 4348 | } |
| 4349 | return result; |
| 4350 | } |
| 4351 | |
| 4352 | static int |
| 4353 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 4354 | { |
| 4355 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 4356 | int l1 = c>>11; |
| 4357 | int l2 = (c>>7) & 0xF; |
| 4358 | int l3 = c & 0x7F; |
| 4359 | int i; |
| 4360 | |
| 4361 | #ifdef Py_UNICODE_WIDE |
| 4362 | if (c > 0xFFFF) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4363 | return -1; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4364 | } |
| 4365 | #endif |
| 4366 | if (c == 0) |
| 4367 | return 0; |
| 4368 | /* level 1*/ |
| 4369 | i = map->level1[l1]; |
| 4370 | if (i == 0xFF) { |
| 4371 | return -1; |
| 4372 | } |
| 4373 | /* level 2*/ |
| 4374 | i = map->level23[16*i+l2]; |
| 4375 | if (i == 0xFF) { |
| 4376 | return -1; |
| 4377 | } |
| 4378 | /* level 3 */ |
| 4379 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 4380 | if (i == 0) { |
| 4381 | return -1; |
| 4382 | } |
| 4383 | return i; |
| 4384 | } |
| 4385 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4386 | /* Lookup the character ch in the mapping. If the character |
| 4387 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4388 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4389 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4390 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4391 | PyObject *w = PyInt_FromLong((long)c); |
| 4392 | PyObject *x; |
| 4393 | |
| 4394 | if (w == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4395 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4396 | x = PyObject_GetItem(mapping, w); |
| 4397 | Py_DECREF(w); |
| 4398 | if (x == NULL) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4399 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4400 | /* No mapping found means: mapping is undefined. */ |
| 4401 | PyErr_Clear(); |
| 4402 | x = Py_None; |
| 4403 | Py_INCREF(x); |
| 4404 | return x; |
| 4405 | } else |
| 4406 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4407 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 4408 | else if (x == Py_None) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4409 | return x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4410 | else if (PyInt_Check(x)) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4411 | long value = PyInt_AS_LONG(x); |
| 4412 | if (value < 0 || value > 255) { |
| 4413 | PyErr_SetString(PyExc_TypeError, |
| 4414 | "character mapping must be in range(256)"); |
| 4415 | Py_DECREF(x); |
| 4416 | return NULL; |
| 4417 | } |
| 4418 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4419 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4420 | else if (PyString_Check(x)) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4421 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4422 | else { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4423 | /* wrong return value */ |
| 4424 | PyErr_SetString(PyExc_TypeError, |
| 4425 | "character mapping must return integer, None or str"); |
| 4426 | Py_DECREF(x); |
| 4427 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4428 | } |
| 4429 | } |
| 4430 | |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4431 | static int |
| 4432 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
| 4433 | { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4434 | Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
| 4435 | /* exponentially overallocate to minimize reallocations */ |
| 4436 | if (requiredsize < 2*outsize) |
| 4437 | requiredsize = 2*outsize; |
| 4438 | if (_PyString_Resize(outobj, requiredsize)) { |
| 4439 | return 0; |
| 4440 | } |
| 4441 | return 1; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4442 | } |
| 4443 | |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4444 | typedef enum charmapencode_result { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4445 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4446 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4447 | /* lookup the character, put the result in the output string and adjust |
| 4448 | various state variables. Reallocate the output string if not enough |
| 4449 | space is available. Return a new reference to the object that |
| 4450 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 4451 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 4452 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4453 | static |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4454 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4455 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4456 | { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4457 | PyObject *rep; |
| 4458 | char *outstart; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4459 | Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4460 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4461 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4462 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4463 | Py_ssize_t requiredsize = *outpos+1; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4464 | if (res == -1) |
| 4465 | return enc_FAILED; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4466 | if (outsize<requiredsize) |
| 4467 | if (!charmapencode_resize(outobj, outpos, requiredsize)) |
| 4468 | return enc_EXCEPTION; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4469 | outstart = PyString_AS_STRING(*outobj); |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4470 | outstart[(*outpos)++] = (char)res; |
| 4471 | return enc_SUCCESS; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4472 | } |
| 4473 | |
| 4474 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4475 | if (rep==NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4476 | return enc_EXCEPTION; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4477 | else if (rep==Py_None) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4478 | Py_DECREF(rep); |
| 4479 | return enc_FAILED; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4480 | } else { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4481 | if (PyInt_Check(rep)) { |
| 4482 | Py_ssize_t requiredsize = *outpos+1; |
| 4483 | if (outsize<requiredsize) |
| 4484 | if (!charmapencode_resize(outobj, outpos, requiredsize)) { |
| 4485 | Py_DECREF(rep); |
| 4486 | return enc_EXCEPTION; |
| 4487 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4488 | outstart = PyString_AS_STRING(*outobj); |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4489 | outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4490 | } |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4491 | else { |
| 4492 | const char *repchars = PyString_AS_STRING(rep); |
| 4493 | Py_ssize_t repsize = PyString_GET_SIZE(rep); |
| 4494 | Py_ssize_t requiredsize = *outpos+repsize; |
| 4495 | if (outsize<requiredsize) |
| 4496 | if (!charmapencode_resize(outobj, outpos, requiredsize)) { |
| 4497 | Py_DECREF(rep); |
| 4498 | return enc_EXCEPTION; |
| 4499 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4500 | outstart = PyString_AS_STRING(*outobj); |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4501 | memcpy(outstart + *outpos, repchars, repsize); |
| 4502 | *outpos += repsize; |
| 4503 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4504 | } |
Georg Brandl | 9f16760 | 2006-06-04 21:46:16 +0000 | [diff] [blame] | 4505 | Py_DECREF(rep); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4506 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4507 | } |
| 4508 | |
| 4509 | /* handle an error in PyUnicode_EncodeCharmap |
| 4510 | Return 0 on success, -1 on error */ |
| 4511 | static |
| 4512 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4513 | 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] | 4514 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4515 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4516 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4517 | { |
| 4518 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4519 | Py_ssize_t repsize; |
| 4520 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4521 | Py_UNICODE *uni2; |
| 4522 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4523 | Py_ssize_t collstartpos = *inpos; |
| 4524 | Py_ssize_t collendpos = *inpos+1; |
| 4525 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4526 | char *encoding = "charmap"; |
| 4527 | char *reason = "character maps to <undefined>"; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4528 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4529 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4530 | /* find all unencodable characters */ |
| 4531 | while (collendpos < size) { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4532 | PyObject *rep; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4533 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4534 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 4535 | if (res != -1) |
| 4536 | break; |
| 4537 | ++collendpos; |
| 4538 | continue; |
| 4539 | } |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4540 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4541 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 4542 | if (rep==NULL) |
| 4543 | return -1; |
| 4544 | else if (rep!=Py_None) { |
| 4545 | Py_DECREF(rep); |
| 4546 | break; |
| 4547 | } |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4548 | Py_DECREF(rep); |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4549 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4550 | } |
| 4551 | /* cache callback name lookup |
| 4552 | * (if not done yet, i.e. it's the first error) */ |
| 4553 | if (*known_errorHandler==-1) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4554 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4555 | *known_errorHandler = 1; |
| 4556 | else if (!strcmp(errors, "replace")) |
| 4557 | *known_errorHandler = 2; |
| 4558 | else if (!strcmp(errors, "ignore")) |
| 4559 | *known_errorHandler = 3; |
| 4560 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4561 | *known_errorHandler = 4; |
| 4562 | else |
| 4563 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4564 | } |
| 4565 | switch (*known_errorHandler) { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4566 | case 1: /* strict */ |
| 4567 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4568 | return -1; |
| 4569 | case 2: /* replace */ |
| 4570 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4571 | x = charmapencode_output('?', mapping, res, respos); |
| 4572 | if (x==enc_EXCEPTION) { |
| 4573 | return -1; |
| 4574 | } |
| 4575 | else if (x==enc_FAILED) { |
| 4576 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4577 | return -1; |
| 4578 | } |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4579 | } |
| 4580 | /* fall through */ |
| 4581 | case 3: /* ignore */ |
| 4582 | *inpos = collendpos; |
| 4583 | break; |
| 4584 | case 4: /* xmlcharrefreplace */ |
| 4585 | /* generate replacement (temporarily (mis)uses p) */ |
| 4586 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4587 | char buffer[2+29+1+1]; |
| 4588 | char *cp; |
| 4589 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 4590 | for (cp = buffer; *cp; ++cp) { |
| 4591 | x = charmapencode_output(*cp, mapping, res, respos); |
| 4592 | if (x==enc_EXCEPTION) |
| 4593 | return -1; |
| 4594 | else if (x==enc_FAILED) { |
| 4595 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4596 | return -1; |
| 4597 | } |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4598 | } |
| 4599 | } |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4600 | *inpos = collendpos; |
| 4601 | break; |
| 4602 | default: |
| 4603 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4604 | encoding, reason, p, size, exceptionObject, |
| 4605 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4606 | if (repunicode == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4607 | return -1; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4608 | /* generate replacement */ |
| 4609 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4610 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4611 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 4612 | if (x==enc_EXCEPTION) { |
| 4613 | return -1; |
| 4614 | } |
| 4615 | else if (x==enc_FAILED) { |
| 4616 | Py_DECREF(repunicode); |
| 4617 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4618 | return -1; |
| 4619 | } |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4620 | } |
| 4621 | *inpos = newpos; |
| 4622 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4623 | } |
| 4624 | return 0; |
| 4625 | } |
| 4626 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4627 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4628 | Py_ssize_t size, |
| 4629 | PyObject *mapping, |
| 4630 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4631 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4632 | /* output object */ |
| 4633 | PyObject *res = NULL; |
| 4634 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4635 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4636 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4637 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4638 | PyObject *errorHandler = NULL; |
| 4639 | PyObject *exc = NULL; |
| 4640 | /* the following variable is used for caching string comparisons |
| 4641 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4642 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4643 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4644 | |
| 4645 | /* Default to Latin-1 */ |
| 4646 | if (mapping == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4647 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4648 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4649 | /* allocate enough for a simple encoding without |
| 4650 | replacements, if we need more, we'll resize */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4651 | res = PyString_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4652 | if (res == NULL) |
| 4653 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 4654 | if (size == 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4655 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4656 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4657 | while (inpos<size) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4658 | /* try to encode it */ |
| 4659 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 4660 | if (x==enc_EXCEPTION) /* error */ |
| 4661 | goto onError; |
| 4662 | if (x==enc_FAILED) { /* unencodable character */ |
| 4663 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 4664 | &exc, |
| 4665 | &known_errorHandler, &errorHandler, errors, |
| 4666 | &res, &respos)) { |
| 4667 | goto onError; |
| 4668 | } |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4669 | } |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4670 | else |
| 4671 | /* done with this character => adjust input position */ |
| 4672 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4673 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4674 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4675 | /* Resize if we allocated to much */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4676 | if (respos<PyString_GET_SIZE(res)) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4677 | if (_PyString_Resize(&res, respos)) |
| 4678 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4679 | } |
| 4680 | Py_XDECREF(exc); |
| 4681 | Py_XDECREF(errorHandler); |
| 4682 | return res; |
| 4683 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4684 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4685 | Py_XDECREF(res); |
| 4686 | Py_XDECREF(exc); |
| 4687 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4688 | return NULL; |
| 4689 | } |
| 4690 | |
| 4691 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4692 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4693 | { |
| 4694 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4695 | PyErr_BadArgument(); |
| 4696 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4697 | } |
| 4698 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4699 | PyUnicode_GET_SIZE(unicode), |
| 4700 | mapping, |
| 4701 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4702 | } |
| 4703 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4704 | /* create or adjust a UnicodeTranslateError */ |
| 4705 | static void make_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4706 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4707 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4708 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4709 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4710 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4711 | *exceptionObject = PyUnicodeTranslateError_Create( |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4712 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4713 | } |
| 4714 | else { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4715 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 4716 | goto onError; |
| 4717 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 4718 | goto onError; |
| 4719 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 4720 | goto onError; |
| 4721 | return; |
| 4722 | onError: |
| 4723 | Py_DECREF(*exceptionObject); |
| 4724 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4725 | } |
| 4726 | } |
| 4727 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4728 | /* raises a UnicodeTranslateError */ |
| 4729 | static void raise_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4730 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4731 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4732 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4733 | { |
| 4734 | make_translate_exception(exceptionObject, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4735 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4736 | if (*exceptionObject != NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4737 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4738 | } |
| 4739 | |
| 4740 | /* error handling callback helper: |
| 4741 | build arguments, call the callback and check the arguments, |
| 4742 | put the result into newpos and return the replacement string, which |
| 4743 | has to be freed by the caller */ |
| 4744 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4745 | PyObject **errorHandler, |
| 4746 | const char *reason, |
| 4747 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4748 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4749 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4750 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4751 | static char *argparse = "O!n;translating error handler must return (unicode, int) tuple"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4752 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4753 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4754 | PyObject *restuple; |
| 4755 | PyObject *resunicode; |
| 4756 | |
| 4757 | if (*errorHandler == NULL) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4758 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4759 | if (*errorHandler == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4760 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4761 | } |
| 4762 | |
| 4763 | make_translate_exception(exceptionObject, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4764 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4765 | if (*exceptionObject == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4766 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4767 | |
| 4768 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4769 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4770 | if (restuple == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4771 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4772 | if (!PyTuple_Check(restuple)) { |
Georg Brandl | cbb4958 | 2009-02-13 11:06:59 +0000 | [diff] [blame] | 4773 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4774 | Py_DECREF(restuple); |
| 4775 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4776 | } |
| 4777 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4778 | &resunicode, &i_newpos)) { |
| 4779 | Py_DECREF(restuple); |
| 4780 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4781 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4782 | if (i_newpos<0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4783 | *newpos = size+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4784 | else |
| 4785 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4786 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4787 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 4788 | Py_DECREF(restuple); |
| 4789 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4790 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4791 | Py_INCREF(resunicode); |
| 4792 | Py_DECREF(restuple); |
| 4793 | return resunicode; |
| 4794 | } |
| 4795 | |
| 4796 | /* Lookup the character ch in the mapping and put the result in result, |
| 4797 | which must be decrefed by the caller. |
| 4798 | Return 0 on success, -1 on error */ |
| 4799 | static |
| 4800 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 4801 | { |
| 4802 | PyObject *w = PyInt_FromLong((long)c); |
| 4803 | PyObject *x; |
| 4804 | |
| 4805 | if (w == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4806 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4807 | x = PyObject_GetItem(mapping, w); |
| 4808 | Py_DECREF(w); |
| 4809 | if (x == NULL) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4810 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4811 | /* No mapping found means: use 1:1 mapping. */ |
| 4812 | PyErr_Clear(); |
| 4813 | *result = NULL; |
| 4814 | return 0; |
| 4815 | } else |
| 4816 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4817 | } |
| 4818 | else if (x == Py_None) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4819 | *result = x; |
| 4820 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4821 | } |
| 4822 | else if (PyInt_Check(x)) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4823 | long value = PyInt_AS_LONG(x); |
| 4824 | long max = PyUnicode_GetMax(); |
| 4825 | if (value < 0 || value > max) { |
| 4826 | PyErr_Format(PyExc_TypeError, |
| 4827 | "character mapping must be in range(0x%lx)", max+1); |
| 4828 | Py_DECREF(x); |
| 4829 | return -1; |
| 4830 | } |
| 4831 | *result = x; |
| 4832 | return 0; |
| 4833 | } |
| 4834 | else if (PyUnicode_Check(x)) { |
| 4835 | *result = x; |
| 4836 | return 0; |
| 4837 | } |
| 4838 | else { |
| 4839 | /* wrong return value */ |
| 4840 | PyErr_SetString(PyExc_TypeError, |
| 4841 | "character mapping must return integer, None or unicode"); |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4842 | Py_DECREF(x); |
| 4843 | return -1; |
| 4844 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4845 | } |
| 4846 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4847 | if not reallocate and adjust various state variables. |
| 4848 | Return 0 on success, -1 on error */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4849 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4850 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4851 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4852 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4853 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4854 | if (requiredsize > oldsize) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4855 | /* remember old output position */ |
| 4856 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
| 4857 | /* exponentially overallocate to minimize reallocations */ |
| 4858 | if (requiredsize < 2 * oldsize) |
| 4859 | requiredsize = 2 * oldsize; |
| 4860 | if (PyUnicode_Resize(outobj, requiredsize) < 0) |
| 4861 | return -1; |
| 4862 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4863 | } |
| 4864 | return 0; |
| 4865 | } |
| 4866 | /* lookup the character, put the result in the output string and adjust |
| 4867 | various state variables. Return a new reference to the object that |
| 4868 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 4869 | undefined (in which case no character was written). |
| 4870 | The called must decref result. |
| 4871 | Return 0 on success, -1 on error. */ |
| 4872 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4873 | int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4874 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
| 4875 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4876 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4877 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4878 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4879 | if (*res==NULL) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4880 | /* not found => default to 1:1 mapping */ |
| 4881 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4882 | } |
| 4883 | else if (*res==Py_None) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4884 | ; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4885 | else if (PyInt_Check(*res)) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4886 | /* no overflow check, because we know that the space is enough */ |
| 4887 | *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4888 | } |
| 4889 | else if (PyUnicode_Check(*res)) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4890 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
| 4891 | if (repsize==1) { |
| 4892 | /* no overflow check, because we know that the space is enough */ |
| 4893 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 4894 | } |
| 4895 | else if (repsize!=0) { |
| 4896 | /* more than one character */ |
| 4897 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
| 4898 | (insize - (curinp-startinp)) + |
| 4899 | repsize - 1; |
| 4900 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
| 4901 | return -1; |
| 4902 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 4903 | *outp += repsize; |
| 4904 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4905 | } |
| 4906 | else |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4907 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4908 | return 0; |
| 4909 | } |
| 4910 | |
| 4911 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4912 | Py_ssize_t size, |
| 4913 | PyObject *mapping, |
| 4914 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4915 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4916 | /* output object */ |
| 4917 | PyObject *res = NULL; |
| 4918 | /* pointers to the beginning and end+1 of input */ |
| 4919 | const Py_UNICODE *startp = p; |
| 4920 | const Py_UNICODE *endp = p + size; |
| 4921 | /* pointer into the output */ |
| 4922 | Py_UNICODE *str; |
| 4923 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4924 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4925 | char *reason = "character maps to <undefined>"; |
| 4926 | PyObject *errorHandler = NULL; |
| 4927 | PyObject *exc = NULL; |
| 4928 | /* the following variable is used for caching string comparisons |
| 4929 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4930 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4931 | int known_errorHandler = -1; |
| 4932 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4933 | if (mapping == NULL) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4934 | PyErr_BadArgument(); |
| 4935 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4936 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4937 | |
| 4938 | /* allocate enough for a simple 1:1 translation without |
| 4939 | replacements, if we need more, we'll resize */ |
| 4940 | res = PyUnicode_FromUnicode(NULL, size); |
| 4941 | if (res == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4942 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4943 | if (size == 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4944 | return res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4945 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4946 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4947 | while (p<endp) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4948 | /* try to encode it */ |
| 4949 | PyObject *x = NULL; |
| 4950 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
| 4951 | Py_XDECREF(x); |
| 4952 | goto onError; |
| 4953 | } |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4954 | Py_XDECREF(x); |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4955 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 4956 | ++p; |
| 4957 | else { /* untranslatable character */ |
| 4958 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 4959 | Py_ssize_t repsize; |
| 4960 | Py_ssize_t newpos; |
| 4961 | Py_UNICODE *uni2; |
| 4962 | /* startpos for collecting untranslatable chars */ |
| 4963 | const Py_UNICODE *collstart = p; |
| 4964 | const Py_UNICODE *collend = p+1; |
| 4965 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4966 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4967 | /* find all untranslatable characters */ |
| 4968 | while (collend < endp) { |
| 4969 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
| 4970 | goto onError; |
| 4971 | Py_XDECREF(x); |
| 4972 | if (x!=Py_None) |
| 4973 | break; |
| 4974 | ++collend; |
| 4975 | } |
| 4976 | /* cache callback name lookup |
| 4977 | * (if not done yet, i.e. it's the first error) */ |
| 4978 | if (known_errorHandler==-1) { |
| 4979 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4980 | known_errorHandler = 1; |
| 4981 | else if (!strcmp(errors, "replace")) |
| 4982 | known_errorHandler = 2; |
| 4983 | else if (!strcmp(errors, "ignore")) |
| 4984 | known_errorHandler = 3; |
| 4985 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4986 | known_errorHandler = 4; |
| 4987 | else |
| 4988 | known_errorHandler = 0; |
| 4989 | } |
| 4990 | switch (known_errorHandler) { |
| 4991 | case 1: /* strict */ |
| 4992 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4993 | goto onError; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4994 | case 2: /* replace */ |
| 4995 | /* No need to check for space, this is a 1:1 replacement */ |
| 4996 | for (coll = collstart; coll<collend; ++coll) |
| 4997 | *str++ = '?'; |
| 4998 | /* fall through */ |
| 4999 | case 3: /* ignore */ |
| 5000 | p = collend; |
| 5001 | break; |
| 5002 | case 4: /* xmlcharrefreplace */ |
| 5003 | /* generate replacement (temporarily (mis)uses p) */ |
| 5004 | for (p = collstart; p < collend; ++p) { |
| 5005 | char buffer[2+29+1+1]; |
| 5006 | char *cp; |
| 5007 | sprintf(buffer, "&#%d;", (int)*p); |
| 5008 | if (charmaptranslate_makespace(&res, &str, |
| 5009 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 5010 | goto onError; |
| 5011 | for (cp = buffer; *cp; ++cp) |
| 5012 | *str++ = *cp; |
| 5013 | } |
| 5014 | p = collend; |
| 5015 | break; |
| 5016 | default: |
| 5017 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 5018 | reason, startp, size, &exc, |
| 5019 | collstart-startp, collend-startp, &newpos); |
| 5020 | if (repunicode == NULL) |
| 5021 | goto onError; |
| 5022 | /* generate replacement */ |
| 5023 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5024 | if (charmaptranslate_makespace(&res, &str, |
| 5025 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 5026 | Py_DECREF(repunicode); |
| 5027 | goto onError; |
| 5028 | } |
| 5029 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 5030 | *str++ = *uni2; |
| 5031 | p = startp + newpos; |
| 5032 | Py_DECREF(repunicode); |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5033 | } |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5034 | } |
| 5035 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5036 | /* Resize if we allocated to much */ |
| 5037 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5038 | if (respos<PyUnicode_GET_SIZE(res)) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5039 | if (PyUnicode_Resize(&res, respos) < 0) |
| 5040 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5041 | } |
| 5042 | Py_XDECREF(exc); |
| 5043 | Py_XDECREF(errorHandler); |
| 5044 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5045 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5046 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5047 | Py_XDECREF(res); |
| 5048 | Py_XDECREF(exc); |
| 5049 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5050 | return NULL; |
| 5051 | } |
| 5052 | |
| 5053 | PyObject *PyUnicode_Translate(PyObject *str, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5054 | PyObject *mapping, |
| 5055 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5056 | { |
| 5057 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5058 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5059 | str = PyUnicode_FromObject(str); |
| 5060 | if (str == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5061 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5062 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5063 | PyUnicode_GET_SIZE(str), |
| 5064 | mapping, |
| 5065 | errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5066 | Py_DECREF(str); |
| 5067 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5068 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5069 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5070 | Py_XDECREF(str); |
| 5071 | return NULL; |
| 5072 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5073 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5074 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 5075 | |
| 5076 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5077 | Py_ssize_t length, |
| 5078 | char *output, |
| 5079 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5080 | { |
| 5081 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5082 | PyObject *errorHandler = NULL; |
| 5083 | PyObject *exc = NULL; |
| 5084 | const char *encoding = "decimal"; |
| 5085 | const char *reason = "invalid decimal Unicode string"; |
| 5086 | /* the following variable is used for caching string comparisons |
| 5087 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 5088 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5089 | |
| 5090 | if (output == NULL) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5091 | PyErr_BadArgument(); |
| 5092 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5093 | } |
| 5094 | |
| 5095 | p = s; |
| 5096 | end = s + length; |
| 5097 | while (p < end) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5098 | register Py_UNICODE ch = *p; |
| 5099 | int decimal; |
| 5100 | PyObject *repunicode; |
| 5101 | Py_ssize_t repsize; |
| 5102 | Py_ssize_t newpos; |
| 5103 | Py_UNICODE *uni2; |
| 5104 | Py_UNICODE *collstart; |
| 5105 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5106 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5107 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5108 | *output++ = ' '; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5109 | ++p; |
| 5110 | continue; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5111 | } |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5112 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5113 | if (decimal >= 0) { |
| 5114 | *output++ = '0' + decimal; |
| 5115 | ++p; |
| 5116 | continue; |
| 5117 | } |
| 5118 | if (0 < ch && ch < 256) { |
| 5119 | *output++ = (char)ch; |
| 5120 | ++p; |
| 5121 | continue; |
| 5122 | } |
| 5123 | /* All other characters are considered unencodable */ |
| 5124 | collstart = p; |
| 5125 | collend = p+1; |
| 5126 | while (collend < end) { |
| 5127 | if ((0 < *collend && *collend < 256) || |
| 5128 | !Py_UNICODE_ISSPACE(*collend) || |
| 5129 | Py_UNICODE_TODECIMAL(*collend)) |
| 5130 | break; |
| 5131 | } |
| 5132 | /* cache callback name lookup |
| 5133 | * (if not done yet, i.e. it's the first error) */ |
| 5134 | if (known_errorHandler==-1) { |
| 5135 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5136 | known_errorHandler = 1; |
| 5137 | else if (!strcmp(errors, "replace")) |
| 5138 | known_errorHandler = 2; |
| 5139 | else if (!strcmp(errors, "ignore")) |
| 5140 | known_errorHandler = 3; |
| 5141 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5142 | known_errorHandler = 4; |
| 5143 | else |
| 5144 | known_errorHandler = 0; |
| 5145 | } |
| 5146 | switch (known_errorHandler) { |
| 5147 | case 1: /* strict */ |
| 5148 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 5149 | goto onError; |
| 5150 | case 2: /* replace */ |
| 5151 | for (p = collstart; p < collend; ++p) |
| 5152 | *output++ = '?'; |
| 5153 | /* fall through */ |
| 5154 | case 3: /* ignore */ |
| 5155 | p = collend; |
| 5156 | break; |
| 5157 | case 4: /* xmlcharrefreplace */ |
| 5158 | /* generate replacement (temporarily (mis)uses p) */ |
| 5159 | for (p = collstart; p < collend; ++p) |
| 5160 | output += sprintf(output, "&#%d;", (int)*p); |
| 5161 | p = collend; |
| 5162 | break; |
| 5163 | default: |
| 5164 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 5165 | encoding, reason, s, length, &exc, |
| 5166 | collstart-s, collend-s, &newpos); |
| 5167 | if (repunicode == NULL) |
| 5168 | goto onError; |
| 5169 | /* generate replacement */ |
| 5170 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5171 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 5172 | Py_UNICODE ch = *uni2; |
| 5173 | if (Py_UNICODE_ISSPACE(ch)) |
| 5174 | *output++ = ' '; |
| 5175 | else { |
| 5176 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5177 | if (decimal >= 0) |
| 5178 | *output++ = '0' + decimal; |
| 5179 | else if (0 < ch && ch < 256) |
| 5180 | *output++ = (char)ch; |
| 5181 | else { |
| 5182 | Py_DECREF(repunicode); |
| 5183 | raise_encode_exception(&exc, encoding, |
| 5184 | s, length, collstart-s, collend-s, reason); |
| 5185 | goto onError; |
| 5186 | } |
| 5187 | } |
| 5188 | } |
| 5189 | p = s + newpos; |
| 5190 | Py_DECREF(repunicode); |
| 5191 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5192 | } |
| 5193 | /* 0-terminate the output string */ |
| 5194 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5195 | Py_XDECREF(exc); |
| 5196 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5197 | return 0; |
| 5198 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5199 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5200 | Py_XDECREF(exc); |
| 5201 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5202 | return -1; |
| 5203 | } |
| 5204 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5205 | /* --- Helpers ------------------------------------------------------------ */ |
| 5206 | |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 5207 | #include "stringlib/unicodedefs.h" |
Fredrik Lundh | 6471ee4 | 2006-05-24 14:28:11 +0000 | [diff] [blame] | 5208 | |
Facundo Batista | 6f7e6fb | 2007-11-16 19:16:15 +0000 | [diff] [blame] | 5209 | #define FROM_UNICODE |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 5210 | |
Fredrik Lundh | a50d201 | 2006-05-26 17:04:58 +0000 | [diff] [blame] | 5211 | #include "stringlib/fastsearch.h" |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5212 | |
| 5213 | #include "stringlib/count.h" |
| 5214 | #include "stringlib/find.h" |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 5215 | #include "stringlib/partition.h" |
| 5216 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 5217 | /* helper macro to fixup start/end slice values */ |
| 5218 | #define FIX_START_END(obj) \ |
| 5219 | if (start < 0) \ |
| 5220 | start += (obj)->length; \ |
| 5221 | if (start < 0) \ |
| 5222 | start = 0; \ |
| 5223 | if (end > (obj)->length) \ |
| 5224 | end = (obj)->length; \ |
| 5225 | if (end < 0) \ |
| 5226 | end += (obj)->length; \ |
| 5227 | if (end < 0) \ |
| 5228 | end = 0; |
| 5229 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5230 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5231 | PyObject *substr, |
| 5232 | Py_ssize_t start, |
| 5233 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5234 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5235 | Py_ssize_t result; |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5236 | PyUnicodeObject* str_obj; |
| 5237 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5238 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5239 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 5240 | if (!str_obj) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5241 | return -1; |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5242 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 5243 | if (!sub_obj) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5244 | Py_DECREF(str_obj); |
| 5245 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5246 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5247 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 5248 | FIX_START_END(str_obj); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5249 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5250 | result = stringlib_count( |
| 5251 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
| 5252 | ); |
| 5253 | |
| 5254 | Py_DECREF(sub_obj); |
| 5255 | Py_DECREF(str_obj); |
| 5256 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5257 | return result; |
| 5258 | } |
| 5259 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5260 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5261 | PyObject *sub, |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 5262 | Py_ssize_t start, |
| 5263 | Py_ssize_t end, |
| 5264 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5265 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5266 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5267 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5268 | str = PyUnicode_FromObject(str); |
| 5269 | if (!str) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5270 | return -2; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5271 | sub = PyUnicode_FromObject(sub); |
| 5272 | if (!sub) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5273 | Py_DECREF(str); |
| 5274 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5275 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5276 | |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 5277 | if (direction > 0) |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 5278 | result = stringlib_find_slice( |
| 5279 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5280 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5281 | start, end |
| 5282 | ); |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 5283 | else |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 5284 | result = stringlib_rfind_slice( |
| 5285 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5286 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5287 | start, end |
| 5288 | ); |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 5289 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5290 | Py_DECREF(str); |
| 5291 | Py_DECREF(sub); |
| 5292 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5293 | return result; |
| 5294 | } |
| 5295 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5296 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5297 | int tailmatch(PyUnicodeObject *self, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5298 | PyUnicodeObject *substring, |
| 5299 | Py_ssize_t start, |
| 5300 | Py_ssize_t end, |
| 5301 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5302 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5303 | if (substring->length == 0) |
| 5304 | return 1; |
| 5305 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 5306 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5307 | |
| 5308 | end -= substring->length; |
| 5309 | if (end < start) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5310 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5311 | |
| 5312 | if (direction > 0) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5313 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 5314 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5315 | } else { |
| 5316 | if (Py_UNICODE_MATCH(self, start, substring)) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5317 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5318 | } |
| 5319 | |
| 5320 | return 0; |
| 5321 | } |
| 5322 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5323 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5324 | PyObject *substr, |
| 5325 | Py_ssize_t start, |
| 5326 | Py_ssize_t end, |
| 5327 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5328 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5329 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5330 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5331 | str = PyUnicode_FromObject(str); |
| 5332 | if (str == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5333 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5334 | substr = PyUnicode_FromObject(substr); |
| 5335 | if (substr == NULL) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5336 | Py_DECREF(str); |
| 5337 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5338 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5339 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5340 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5341 | (PyUnicodeObject *)substr, |
| 5342 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5343 | Py_DECREF(str); |
| 5344 | Py_DECREF(substr); |
| 5345 | return result; |
| 5346 | } |
| 5347 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5348 | /* Apply fixfct filter to the Unicode object self and return a |
| 5349 | reference to the modified object */ |
| 5350 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5351 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5352 | PyObject *fixup(PyUnicodeObject *self, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5353 | int (*fixfct)(PyUnicodeObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5354 | { |
| 5355 | |
| 5356 | PyUnicodeObject *u; |
| 5357 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5358 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5359 | if (u == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5360 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5361 | |
| 5362 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5363 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5364 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5365 | /* fixfct should return TRUE if it modified the buffer. If |
| 5366 | FALSE, return a reference to the original buffer instead |
| 5367 | (to save space, not time) */ |
| 5368 | Py_INCREF(self); |
| 5369 | Py_DECREF(u); |
| 5370 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5371 | } |
| 5372 | return (PyObject*) u; |
| 5373 | } |
| 5374 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5375 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5376 | int fixupper(PyUnicodeObject *self) |
| 5377 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5378 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5379 | Py_UNICODE *s = self->str; |
| 5380 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5381 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5382 | while (len-- > 0) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5383 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5384 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5385 | ch = Py_UNICODE_TOUPPER(*s); |
| 5386 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5387 | status = 1; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5388 | *s = ch; |
| 5389 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5390 | s++; |
| 5391 | } |
| 5392 | |
| 5393 | return status; |
| 5394 | } |
| 5395 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5396 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5397 | int fixlower(PyUnicodeObject *self) |
| 5398 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5399 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5400 | Py_UNICODE *s = self->str; |
| 5401 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5402 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5403 | while (len-- > 0) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5404 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5405 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5406 | ch = Py_UNICODE_TOLOWER(*s); |
| 5407 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5408 | status = 1; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5409 | *s = ch; |
| 5410 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5411 | s++; |
| 5412 | } |
| 5413 | |
| 5414 | return status; |
| 5415 | } |
| 5416 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5417 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5418 | int fixswapcase(PyUnicodeObject *self) |
| 5419 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5420 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5421 | Py_UNICODE *s = self->str; |
| 5422 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5423 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5424 | while (len-- > 0) { |
| 5425 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5426 | *s = Py_UNICODE_TOLOWER(*s); |
| 5427 | status = 1; |
| 5428 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 5429 | *s = Py_UNICODE_TOUPPER(*s); |
| 5430 | status = 1; |
| 5431 | } |
| 5432 | s++; |
| 5433 | } |
| 5434 | |
| 5435 | return status; |
| 5436 | } |
| 5437 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5438 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5439 | int fixcapitalize(PyUnicodeObject *self) |
| 5440 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5441 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5442 | Py_UNICODE *s = self->str; |
| 5443 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5444 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5445 | if (len == 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5446 | return 0; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5447 | if (Py_UNICODE_ISLOWER(*s)) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5448 | *s = Py_UNICODE_TOUPPER(*s); |
| 5449 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5450 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5451 | s++; |
| 5452 | while (--len > 0) { |
| 5453 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5454 | *s = Py_UNICODE_TOLOWER(*s); |
| 5455 | status = 1; |
| 5456 | } |
| 5457 | s++; |
| 5458 | } |
| 5459 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5460 | } |
| 5461 | |
| 5462 | static |
| 5463 | int fixtitle(PyUnicodeObject *self) |
| 5464 | { |
| 5465 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5466 | register Py_UNICODE *e; |
| 5467 | int previous_is_cased; |
| 5468 | |
| 5469 | /* Shortcut for single character strings */ |
| 5470 | if (PyUnicode_GET_SIZE(self) == 1) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5471 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 5472 | if (*p != ch) { |
| 5473 | *p = ch; |
| 5474 | return 1; |
| 5475 | } |
| 5476 | else |
| 5477 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5478 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5479 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5480 | e = p + PyUnicode_GET_SIZE(self); |
| 5481 | previous_is_cased = 0; |
| 5482 | for (; p < e; p++) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5483 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5484 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5485 | if (previous_is_cased) |
| 5486 | *p = Py_UNICODE_TOLOWER(ch); |
| 5487 | else |
| 5488 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5489 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5490 | if (Py_UNICODE_ISLOWER(ch) || |
| 5491 | Py_UNICODE_ISUPPER(ch) || |
| 5492 | Py_UNICODE_ISTITLE(ch)) |
| 5493 | previous_is_cased = 1; |
| 5494 | else |
| 5495 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5496 | } |
| 5497 | return 1; |
| 5498 | } |
| 5499 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5500 | PyObject * |
| 5501 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5502 | { |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5503 | PyObject *internal_separator = NULL; |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 5504 | const Py_UNICODE blank = ' '; |
| 5505 | const Py_UNICODE *sep = ␣ |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 5506 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5507 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 5508 | Py_ssize_t res_alloc = 100; /* # allocated bytes for string in res */ |
| 5509 | Py_ssize_t res_used; /* # used bytes */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5510 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 5511 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5512 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5513 | PyObject *item; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5514 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5515 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5516 | fseq = PySequence_Fast(seq, ""); |
| 5517 | if (fseq == NULL) { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5518 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5519 | } |
| 5520 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5521 | /* Grrrr. A codec may be invoked to convert str objects to |
| 5522 | * Unicode, and so it's possible to call back into Python code |
| 5523 | * during PyUnicode_FromObject(), and so it's possible for a sick |
| 5524 | * codec to change the size of fseq (if seq is a list). Therefore |
| 5525 | * we have to keep refetching the size -- can't assume seqlen |
| 5526 | * is invariant. |
| 5527 | */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5528 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5529 | /* If empty sequence, return u"". */ |
| 5530 | if (seqlen == 0) { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5531 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 5532 | goto Done; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5533 | } |
| 5534 | /* If singleton sequence with an exact Unicode, return that. */ |
| 5535 | if (seqlen == 1) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5536 | item = PySequence_Fast_GET_ITEM(fseq, 0); |
| 5537 | if (PyUnicode_CheckExact(item)) { |
| 5538 | Py_INCREF(item); |
| 5539 | res = (PyUnicodeObject *)item; |
| 5540 | goto Done; |
| 5541 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5542 | } |
| 5543 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5544 | /* At least two items to join, or one that isn't exact Unicode. */ |
| 5545 | if (seqlen > 1) { |
| 5546 | /* Set up sep and seplen -- they're needed. */ |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5547 | if (separator == NULL) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5548 | sep = ␣ |
| 5549 | seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5550 | } |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5551 | else { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5552 | internal_separator = PyUnicode_FromObject(separator); |
| 5553 | if (internal_separator == NULL) |
| 5554 | goto onError; |
| 5555 | sep = PyUnicode_AS_UNICODE(internal_separator); |
| 5556 | seplen = PyUnicode_GET_SIZE(internal_separator); |
| 5557 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 5558 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5559 | } |
| 5560 | } |
| 5561 | |
| 5562 | /* Get space. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5563 | res = _PyUnicode_New(res_alloc); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5564 | if (res == NULL) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5565 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5566 | res_p = PyUnicode_AS_UNICODE(res); |
| 5567 | res_used = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5568 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5569 | for (i = 0; i < seqlen; ++i) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5570 | Py_ssize_t itemlen; |
| 5571 | Py_ssize_t new_res_used; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5572 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5573 | item = PySequence_Fast_GET_ITEM(fseq, i); |
| 5574 | /* Convert item to Unicode. */ |
| 5575 | if (! PyUnicode_Check(item) && ! PyString_Check(item)) { |
| 5576 | PyErr_Format(PyExc_TypeError, |
| 5577 | "sequence item %zd: expected string or Unicode," |
| 5578 | " %.80s found", |
| 5579 | i, Py_TYPE(item)->tp_name); |
| 5580 | goto onError; |
| 5581 | } |
| 5582 | item = PyUnicode_FromObject(item); |
| 5583 | if (item == NULL) |
| 5584 | goto onError; |
| 5585 | /* We own a reference to item from here on. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5586 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5587 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 5588 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5589 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5590 | /* Make sure we have enough space for the separator and the item. */ |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5591 | itemlen = PyUnicode_GET_SIZE(item); |
| 5592 | new_res_used = res_used + itemlen; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5593 | if (new_res_used < 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5594 | goto Overflow; |
| 5595 | if (i < seqlen - 1) { |
| 5596 | new_res_used += seplen; |
| 5597 | if (new_res_used < 0) |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5598 | goto Overflow; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5599 | } |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5600 | if (new_res_used > res_alloc) { |
| 5601 | /* double allocated size until it's big enough */ |
| 5602 | do { |
| 5603 | res_alloc += res_alloc; |
| 5604 | if (res_alloc <= 0) |
| 5605 | goto Overflow; |
| 5606 | } while (new_res_used > res_alloc); |
| 5607 | if (_PyUnicode_Resize(&res, res_alloc) < 0) { |
| 5608 | Py_DECREF(item); |
| 5609 | goto onError; |
| 5610 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5611 | res_p = PyUnicode_AS_UNICODE(res) + res_used; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5612 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5613 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5614 | /* Copy item, and maybe the separator. */ |
| 5615 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
| 5616 | res_p += itemlen; |
| 5617 | if (i < seqlen - 1) { |
| 5618 | Py_UNICODE_COPY(res_p, sep, seplen); |
| 5619 | res_p += seplen; |
| 5620 | } |
| 5621 | Py_DECREF(item); |
| 5622 | res_used = new_res_used; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5623 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5624 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5625 | /* Shrink res to match the used area; this probably can't fail, |
| 5626 | * but it's cheap to check. |
| 5627 | */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5628 | if (_PyUnicode_Resize(&res, res_used) < 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5629 | goto onError; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5630 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5631 | Done: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5632 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5633 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5634 | return (PyObject *)res; |
| 5635 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5636 | Overflow: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5637 | PyErr_SetString(PyExc_OverflowError, |
Georg Brandl | 90e27d3 | 2006-06-10 06:40:50 +0000 | [diff] [blame] | 5638 | "join() result is too long for a Python string"); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5639 | Py_DECREF(item); |
| 5640 | /* fall through */ |
| 5641 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5642 | onError: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5643 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5644 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5645 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5646 | return NULL; |
| 5647 | } |
| 5648 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5649 | static |
| 5650 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5651 | Py_ssize_t left, |
| 5652 | Py_ssize_t right, |
| 5653 | Py_UNICODE fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5654 | { |
| 5655 | PyUnicodeObject *u; |
| 5656 | |
| 5657 | if (left < 0) |
| 5658 | left = 0; |
| 5659 | if (right < 0) |
| 5660 | right = 0; |
| 5661 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5662 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5663 | Py_INCREF(self); |
| 5664 | return self; |
| 5665 | } |
| 5666 | |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 5667 | if (left > PY_SSIZE_T_MAX - self->length || |
| 5668 | right > PY_SSIZE_T_MAX - (left + self->length)) { |
| 5669 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 5670 | return NULL; |
| 5671 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5672 | u = _PyUnicode_New(left + self->length + right); |
| 5673 | if (u) { |
| 5674 | if (left) |
| 5675 | Py_UNICODE_FILL(u->str, fill, left); |
| 5676 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 5677 | if (right) |
| 5678 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 5679 | } |
| 5680 | |
| 5681 | return u; |
| 5682 | } |
| 5683 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5684 | #define SPLIT_APPEND(data, left, right) \ |
| 5685 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
| 5686 | if (!str) \ |
| 5687 | goto onError; \ |
| 5688 | if (PyList_Append(list, str)) { \ |
| 5689 | Py_DECREF(str); \ |
| 5690 | goto onError; \ |
| 5691 | } \ |
| 5692 | else \ |
| 5693 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5694 | |
| 5695 | static |
| 5696 | PyObject *split_whitespace(PyUnicodeObject *self, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5697 | PyObject *list, |
| 5698 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5699 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5700 | register Py_ssize_t i; |
| 5701 | register Py_ssize_t j; |
| 5702 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5703 | PyObject *str; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5704 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5705 | |
| 5706 | for (i = j = 0; i < len; ) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5707 | /* find a token */ |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5708 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5709 | i++; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5710 | j = i; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5711 | while (i < len && !Py_UNICODE_ISSPACE(buf[i])) |
| 5712 | i++; |
| 5713 | if (j < i) { |
| 5714 | if (maxcount-- <= 0) |
| 5715 | break; |
| 5716 | SPLIT_APPEND(buf, j, i); |
| 5717 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
| 5718 | i++; |
| 5719 | j = i; |
| 5720 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5721 | } |
| 5722 | if (j < len) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5723 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5724 | } |
| 5725 | return list; |
| 5726 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5727 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5728 | Py_DECREF(list); |
| 5729 | return NULL; |
| 5730 | } |
| 5731 | |
| 5732 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5733 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5734 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5735 | register Py_ssize_t i; |
| 5736 | register Py_ssize_t j; |
| 5737 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5738 | PyObject *list; |
| 5739 | PyObject *str; |
| 5740 | Py_UNICODE *data; |
| 5741 | |
| 5742 | string = PyUnicode_FromObject(string); |
| 5743 | if (string == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5744 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5745 | data = PyUnicode_AS_UNICODE(string); |
| 5746 | len = PyUnicode_GET_SIZE(string); |
| 5747 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5748 | list = PyList_New(0); |
| 5749 | if (!list) |
| 5750 | goto onError; |
| 5751 | |
| 5752 | for (i = j = 0; i < len; ) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5753 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5754 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5755 | /* Find a line and append it */ |
| 5756 | while (i < len && !BLOOM_LINEBREAK(data[i])) |
| 5757 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5758 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5759 | /* Skip the line break reading CRLF as one line break */ |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5760 | eol = i; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5761 | if (i < len) { |
| 5762 | if (data[i] == '\r' && i + 1 < len && |
| 5763 | data[i+1] == '\n') |
| 5764 | i += 2; |
| 5765 | else |
| 5766 | i++; |
| 5767 | if (keepends) |
| 5768 | eol = i; |
| 5769 | } |
| 5770 | SPLIT_APPEND(data, j, eol); |
| 5771 | j = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5772 | } |
| 5773 | if (j < len) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5774 | SPLIT_APPEND(data, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5775 | } |
| 5776 | |
| 5777 | Py_DECREF(string); |
| 5778 | return list; |
| 5779 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5780 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5781 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5782 | Py_DECREF(string); |
| 5783 | return NULL; |
| 5784 | } |
| 5785 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5786 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5787 | PyObject *split_char(PyUnicodeObject *self, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5788 | PyObject *list, |
| 5789 | Py_UNICODE ch, |
| 5790 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5791 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5792 | register Py_ssize_t i; |
| 5793 | register Py_ssize_t j; |
| 5794 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5795 | PyObject *str; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5796 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5797 | |
| 5798 | for (i = j = 0; i < len; ) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5799 | if (buf[i] == ch) { |
| 5800 | if (maxcount-- <= 0) |
| 5801 | break; |
| 5802 | SPLIT_APPEND(buf, j, i); |
| 5803 | i = j = i + 1; |
| 5804 | } else |
| 5805 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5806 | } |
| 5807 | if (j <= len) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5808 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5809 | } |
| 5810 | return list; |
| 5811 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5812 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5813 | Py_DECREF(list); |
| 5814 | return NULL; |
| 5815 | } |
| 5816 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5817 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5818 | PyObject *split_substring(PyUnicodeObject *self, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5819 | PyObject *list, |
| 5820 | PyUnicodeObject *substring, |
| 5821 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5822 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5823 | register Py_ssize_t i; |
| 5824 | register Py_ssize_t j; |
| 5825 | Py_ssize_t len = self->length; |
| 5826 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5827 | PyObject *str; |
| 5828 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 5829 | for (i = j = 0; i <= len - sublen; ) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5830 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5831 | if (maxcount-- <= 0) |
| 5832 | break; |
| 5833 | SPLIT_APPEND(self->str, j, i); |
| 5834 | i = j = i + sublen; |
| 5835 | } else |
| 5836 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5837 | } |
| 5838 | if (j <= len) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5839 | SPLIT_APPEND(self->str, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5840 | } |
| 5841 | return list; |
| 5842 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5843 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5844 | Py_DECREF(list); |
| 5845 | return NULL; |
| 5846 | } |
| 5847 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5848 | static |
| 5849 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5850 | PyObject *list, |
| 5851 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5852 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5853 | register Py_ssize_t i; |
| 5854 | register Py_ssize_t j; |
| 5855 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5856 | PyObject *str; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5857 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5858 | |
| 5859 | for (i = j = len - 1; i >= 0; ) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5860 | /* find a token */ |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5861 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5862 | i--; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5863 | j = i; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5864 | while (i >= 0 && !Py_UNICODE_ISSPACE(buf[i])) |
| 5865 | i--; |
| 5866 | if (j > i) { |
| 5867 | if (maxcount-- <= 0) |
| 5868 | break; |
| 5869 | SPLIT_APPEND(buf, i + 1, j + 1); |
| 5870 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
| 5871 | i--; |
| 5872 | j = i; |
| 5873 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5874 | } |
| 5875 | if (j >= 0) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5876 | SPLIT_APPEND(buf, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5877 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5878 | if (PyList_Reverse(list) < 0) |
| 5879 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5880 | return list; |
| 5881 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5882 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5883 | Py_DECREF(list); |
| 5884 | return NULL; |
| 5885 | } |
| 5886 | |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5887 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5888 | PyObject *rsplit_char(PyUnicodeObject *self, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5889 | PyObject *list, |
| 5890 | Py_UNICODE ch, |
| 5891 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5892 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5893 | register Py_ssize_t i; |
| 5894 | register Py_ssize_t j; |
| 5895 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5896 | PyObject *str; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5897 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5898 | |
| 5899 | for (i = j = len - 1; i >= 0; ) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5900 | if (buf[i] == ch) { |
| 5901 | if (maxcount-- <= 0) |
| 5902 | break; |
| 5903 | SPLIT_APPEND(buf, i + 1, j + 1); |
| 5904 | j = i = i - 1; |
| 5905 | } else |
| 5906 | i--; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5907 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 5908 | if (j >= -1) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5909 | SPLIT_APPEND(buf, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5910 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5911 | if (PyList_Reverse(list) < 0) |
| 5912 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5913 | return list; |
| 5914 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5915 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5916 | Py_DECREF(list); |
| 5917 | return NULL; |
| 5918 | } |
| 5919 | |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5920 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5921 | PyObject *rsplit_substring(PyUnicodeObject *self, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5922 | PyObject *list, |
| 5923 | PyUnicodeObject *substring, |
| 5924 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5925 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5926 | register Py_ssize_t i; |
| 5927 | register Py_ssize_t j; |
| 5928 | Py_ssize_t len = self->length; |
| 5929 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5930 | PyObject *str; |
| 5931 | |
| 5932 | for (i = len - sublen, j = len; i >= 0; ) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5933 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5934 | if (maxcount-- <= 0) |
| 5935 | break; |
| 5936 | SPLIT_APPEND(self->str, i + sublen, j); |
| 5937 | j = i; |
| 5938 | i -= sublen; |
| 5939 | } else |
| 5940 | i--; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5941 | } |
| 5942 | if (j >= 0) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5943 | SPLIT_APPEND(self->str, 0, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5944 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5945 | if (PyList_Reverse(list) < 0) |
| 5946 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5947 | return list; |
| 5948 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5949 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5950 | Py_DECREF(list); |
| 5951 | return NULL; |
| 5952 | } |
| 5953 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5954 | #undef SPLIT_APPEND |
| 5955 | |
| 5956 | static |
| 5957 | PyObject *split(PyUnicodeObject *self, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5958 | PyUnicodeObject *substring, |
| 5959 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5960 | { |
| 5961 | PyObject *list; |
| 5962 | |
| 5963 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5964 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5965 | |
| 5966 | list = PyList_New(0); |
| 5967 | if (!list) |
| 5968 | return NULL; |
| 5969 | |
| 5970 | if (substring == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5971 | return split_whitespace(self,list,maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5972 | |
| 5973 | else if (substring->length == 1) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5974 | return split_char(self,list,substring->str[0],maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5975 | |
| 5976 | else if (substring->length == 0) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5977 | Py_DECREF(list); |
| 5978 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5979 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5980 | } |
| 5981 | else |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5982 | return split_substring(self,list,substring,maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5983 | } |
| 5984 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5985 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5986 | PyObject *rsplit(PyUnicodeObject *self, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5987 | PyUnicodeObject *substring, |
| 5988 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5989 | { |
| 5990 | PyObject *list; |
| 5991 | |
| 5992 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5993 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5994 | |
| 5995 | list = PyList_New(0); |
| 5996 | if (!list) |
| 5997 | return NULL; |
| 5998 | |
| 5999 | if (substring == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6000 | return rsplit_whitespace(self,list,maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6001 | |
| 6002 | else if (substring->length == 1) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6003 | return rsplit_char(self,list,substring->str[0],maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6004 | |
| 6005 | else if (substring->length == 0) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6006 | Py_DECREF(list); |
| 6007 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 6008 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6009 | } |
| 6010 | else |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6011 | return rsplit_substring(self,list,substring,maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6012 | } |
| 6013 | |
| 6014 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6015 | PyObject *replace(PyUnicodeObject *self, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6016 | PyUnicodeObject *str1, |
| 6017 | PyUnicodeObject *str2, |
| 6018 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6019 | { |
| 6020 | PyUnicodeObject *u; |
| 6021 | |
| 6022 | if (maxcount < 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6023 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6024 | |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6025 | if (str1->length == str2->length) { |
| 6026 | /* same length */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6027 | Py_ssize_t i; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6028 | if (str1->length == 1) { |
| 6029 | /* replace characters */ |
| 6030 | Py_UNICODE u1, u2; |
| 6031 | if (!findchar(self->str, self->length, str1->str[0])) |
| 6032 | goto nothing; |
| 6033 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6034 | if (!u) |
| 6035 | return NULL; |
| 6036 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6037 | u1 = str1->str[0]; |
| 6038 | u2 = str2->str[0]; |
| 6039 | for (i = 0; i < u->length; i++) |
| 6040 | if (u->str[i] == u1) { |
| 6041 | if (--maxcount < 0) |
| 6042 | break; |
| 6043 | u->str[i] = u2; |
| 6044 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6045 | } else { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6046 | i = fastsearch( |
| 6047 | self->str, self->length, str1->str, str1->length, FAST_SEARCH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6048 | ); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6049 | if (i < 0) |
| 6050 | goto nothing; |
| 6051 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6052 | if (!u) |
| 6053 | return NULL; |
| 6054 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6055 | while (i <= self->length - str1->length) |
| 6056 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 6057 | if (--maxcount < 0) |
| 6058 | break; |
| 6059 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6060 | i += str1->length; |
| 6061 | } else |
| 6062 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6063 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6064 | } else { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6065 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 6066 | Py_ssize_t n, i, j, e; |
Fredrik Lundh | 0c71f88 | 2006-05-25 16:46:54 +0000 | [diff] [blame] | 6067 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6068 | Py_UNICODE *p; |
| 6069 | |
| 6070 | /* replace strings */ |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6071 | n = stringlib_count(self->str, self->length, str1->str, str1->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6072 | if (n > maxcount) |
| 6073 | n = maxcount; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6074 | if (n == 0) |
| 6075 | goto nothing; |
Fredrik Lundh | 0c71f88 | 2006-05-25 16:46:54 +0000 | [diff] [blame] | 6076 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 6077 | delta = (str2->length - str1->length); |
| 6078 | if (delta == 0) { |
| 6079 | new_size = self->length; |
| 6080 | } else { |
| 6081 | product = n * (str2->length - str1->length); |
| 6082 | if ((product / (str2->length - str1->length)) != n) { |
| 6083 | PyErr_SetString(PyExc_OverflowError, |
| 6084 | "replace string is too long"); |
| 6085 | return NULL; |
| 6086 | } |
| 6087 | new_size = self->length + product; |
| 6088 | if (new_size < 0) { |
| 6089 | PyErr_SetString(PyExc_OverflowError, |
| 6090 | "replace string is too long"); |
| 6091 | return NULL; |
| 6092 | } |
| 6093 | } |
| 6094 | u = _PyUnicode_New(new_size); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6095 | if (!u) |
| 6096 | return NULL; |
| 6097 | i = 0; |
| 6098 | p = u->str; |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 6099 | e = self->length - str1->length; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6100 | if (str1->length > 0) { |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 6101 | while (n-- > 0) { |
| 6102 | /* look for next match */ |
| 6103 | j = i; |
| 6104 | while (j <= e) { |
| 6105 | if (Py_UNICODE_MATCH(self, j, str1)) |
| 6106 | break; |
| 6107 | j++; |
| 6108 | } |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6109 | if (j > i) { |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 6110 | if (j > e) |
| 6111 | break; |
| 6112 | /* copy unchanged part [i:j] */ |
| 6113 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 6114 | p += j - i; |
| 6115 | } |
| 6116 | /* copy substitution string */ |
| 6117 | if (str2->length > 0) { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6118 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6119 | p += str2->length; |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 6120 | } |
| 6121 | i = j + str1->length; |
| 6122 | } |
| 6123 | if (i < self->length) |
| 6124 | /* copy tail [i:] */ |
| 6125 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6126 | } else { |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 6127 | /* interleave */ |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6128 | while (n > 0) { |
| 6129 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6130 | p += str2->length; |
| 6131 | if (--n <= 0) |
| 6132 | break; |
| 6133 | *p++ = self->str[i++]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6134 | } |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6135 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6136 | } |
| 6137 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6138 | return (PyObject *) u; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6139 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6140 | nothing: |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6141 | /* nothing to replace; return original string (when possible) */ |
| 6142 | if (PyUnicode_CheckExact(self)) { |
| 6143 | Py_INCREF(self); |
| 6144 | return (PyObject *) self; |
| 6145 | } |
| 6146 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6147 | } |
| 6148 | |
| 6149 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 6150 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6151 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6152 | "S.title() -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6153 | \n\ |
| 6154 | 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] | 6155 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6156 | |
| 6157 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6158 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6159 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6160 | return fixup(self, fixtitle); |
| 6161 | } |
| 6162 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6163 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6164 | "S.capitalize() -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6165 | \n\ |
| 6166 | Return a capitalized version of S, i.e. make the first character\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6167 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6168 | |
| 6169 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6170 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6171 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6172 | return fixup(self, fixcapitalize); |
| 6173 | } |
| 6174 | |
| 6175 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6176 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6177 | "S.capwords() -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6178 | \n\ |
| 6179 | 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] | 6180 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6181 | |
| 6182 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6183 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6184 | { |
| 6185 | PyObject *list; |
| 6186 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6187 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6188 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6189 | /* Split into words */ |
| 6190 | list = split(self, NULL, -1); |
| 6191 | if (!list) |
| 6192 | return NULL; |
| 6193 | |
| 6194 | /* Capitalize each word */ |
| 6195 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 6196 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6197 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6198 | if (item == NULL) |
| 6199 | goto onError; |
| 6200 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 6201 | PyList_SET_ITEM(list, i, item); |
| 6202 | } |
| 6203 | |
| 6204 | /* Join the words to form a new string */ |
| 6205 | item = PyUnicode_Join(NULL, list); |
| 6206 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6207 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6208 | Py_DECREF(list); |
| 6209 | return (PyObject *)item; |
| 6210 | } |
| 6211 | #endif |
| 6212 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6213 | /* Argument converter. Coerces to a single unicode character */ |
| 6214 | |
| 6215 | static int |
| 6216 | convert_uc(PyObject *obj, void *addr) |
| 6217 | { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 6218 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 6219 | PyObject *uniobj; |
| 6220 | Py_UNICODE *unistr; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6221 | |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 6222 | uniobj = PyUnicode_FromObject(obj); |
| 6223 | if (uniobj == NULL) { |
| 6224 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6225 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 6226 | return 0; |
| 6227 | } |
| 6228 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 6229 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6230 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 6231 | Py_DECREF(uniobj); |
| 6232 | return 0; |
| 6233 | } |
| 6234 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 6235 | *fillcharloc = unistr[0]; |
| 6236 | Py_DECREF(uniobj); |
| 6237 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6238 | } |
| 6239 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6240 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6241 | "S.center(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6242 | \n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6243 | Return S centered in a Unicode string of length width. Padding is\n\ |
| 6244 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6245 | |
| 6246 | static PyObject * |
| 6247 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 6248 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6249 | Py_ssize_t marg, left; |
| 6250 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6251 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6252 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6253 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6254 | return NULL; |
| 6255 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6256 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6257 | Py_INCREF(self); |
| 6258 | return (PyObject*) self; |
| 6259 | } |
| 6260 | |
| 6261 | marg = width - self->length; |
| 6262 | left = marg / 2 + (marg & width & 1); |
| 6263 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6264 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6265 | } |
| 6266 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6267 | #if 0 |
| 6268 | |
| 6269 | /* This code should go into some future Unicode collation support |
| 6270 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 6271 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6272 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6273 | /* speedy UTF-16 code point order comparison */ |
| 6274 | /* gleaned from: */ |
| 6275 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 6276 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6277 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6278 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6279 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6280 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 6281 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6282 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6283 | }; |
| 6284 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6285 | static int |
| 6286 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6287 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6288 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6289 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6290 | Py_UNICODE *s1 = str1->str; |
| 6291 | Py_UNICODE *s2 = str2->str; |
| 6292 | |
| 6293 | len1 = str1->length; |
| 6294 | len2 = str2->length; |
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 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6297 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6298 | |
| 6299 | c1 = *s1++; |
| 6300 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6301 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6302 | if (c1 > (1<<11) * 26) |
| 6303 | c1 += utf16Fixup[c1>>11]; |
| 6304 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6305 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6306 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6307 | |
| 6308 | if (c1 != c2) |
| 6309 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6310 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6311 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6312 | } |
| 6313 | |
| 6314 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6315 | } |
| 6316 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6317 | #else |
| 6318 | |
| 6319 | static int |
| 6320 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6321 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6322 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6323 | |
| 6324 | Py_UNICODE *s1 = str1->str; |
| 6325 | Py_UNICODE *s2 = str2->str; |
| 6326 | |
| 6327 | len1 = str1->length; |
| 6328 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6329 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6330 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6331 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6332 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6333 | c1 = *s1++; |
| 6334 | c2 = *s2++; |
| 6335 | |
| 6336 | if (c1 != c2) |
| 6337 | return (c1 < c2) ? -1 : 1; |
| 6338 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6339 | len1--; len2--; |
| 6340 | } |
| 6341 | |
| 6342 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6343 | } |
| 6344 | |
| 6345 | #endif |
| 6346 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6347 | int PyUnicode_Compare(PyObject *left, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6348 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6349 | { |
| 6350 | PyUnicodeObject *u = NULL, *v = NULL; |
| 6351 | int result; |
| 6352 | |
| 6353 | /* Coerce the two arguments */ |
| 6354 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 6355 | if (u == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6356 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6357 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 6358 | if (v == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6359 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6360 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6361 | /* Shortcut for empty or interned objects */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6362 | if (v == u) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6363 | Py_DECREF(u); |
| 6364 | Py_DECREF(v); |
| 6365 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6366 | } |
| 6367 | |
| 6368 | result = unicode_compare(u, v); |
| 6369 | |
| 6370 | Py_DECREF(u); |
| 6371 | Py_DECREF(v); |
| 6372 | return result; |
| 6373 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6374 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6375 | Py_XDECREF(u); |
| 6376 | Py_XDECREF(v); |
| 6377 | return -1; |
| 6378 | } |
| 6379 | |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 6380 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 6381 | PyObject *right, |
| 6382 | int op) |
| 6383 | { |
| 6384 | int result; |
| 6385 | |
| 6386 | result = PyUnicode_Compare(left, right); |
| 6387 | if (result == -1 && PyErr_Occurred()) |
| 6388 | goto onError; |
| 6389 | |
| 6390 | /* Convert the return value to a Boolean */ |
| 6391 | switch (op) { |
| 6392 | case Py_EQ: |
| 6393 | result = (result == 0); |
| 6394 | break; |
| 6395 | case Py_NE: |
| 6396 | result = (result != 0); |
| 6397 | break; |
| 6398 | case Py_LE: |
| 6399 | result = (result <= 0); |
| 6400 | break; |
| 6401 | case Py_GE: |
| 6402 | result = (result >= 0); |
| 6403 | break; |
| 6404 | case Py_LT: |
| 6405 | result = (result == -1); |
| 6406 | break; |
| 6407 | case Py_GT: |
| 6408 | result = (result == 1); |
| 6409 | break; |
| 6410 | } |
| 6411 | return PyBool_FromLong(result); |
| 6412 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6413 | onError: |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 6414 | |
| 6415 | /* Standard case |
| 6416 | |
| 6417 | Type errors mean that PyUnicode_FromObject() could not convert |
| 6418 | one of the arguments (usually the right hand side) to Unicode, |
| 6419 | ie. we can't handle the comparison request. However, it is |
| 6420 | possible that the other object knows a comparison method, which |
| 6421 | is why we return Py_NotImplemented to give the other object a |
| 6422 | chance. |
| 6423 | |
| 6424 | */ |
| 6425 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 6426 | PyErr_Clear(); |
| 6427 | Py_INCREF(Py_NotImplemented); |
| 6428 | return Py_NotImplemented; |
| 6429 | } |
| 6430 | if (op != Py_EQ && op != Py_NE) |
| 6431 | return NULL; |
| 6432 | |
| 6433 | /* Equality comparison. |
| 6434 | |
| 6435 | This is a special case: we silence any PyExc_UnicodeDecodeError |
| 6436 | and instead turn it into a PyErr_UnicodeWarning. |
| 6437 | |
| 6438 | */ |
| 6439 | if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) |
| 6440 | return NULL; |
| 6441 | PyErr_Clear(); |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 6442 | if (PyErr_Warn(PyExc_UnicodeWarning, |
| 6443 | (op == Py_EQ) ? |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 6444 | "Unicode equal comparison " |
| 6445 | "failed to convert both arguments to Unicode - " |
| 6446 | "interpreting them as being unequal" : |
| 6447 | "Unicode unequal comparison " |
| 6448 | "failed to convert both arguments to Unicode - " |
| 6449 | "interpreting them as being unequal" |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6450 | ) < 0) |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 6451 | return NULL; |
| 6452 | result = (op == Py_NE); |
| 6453 | return PyBool_FromLong(result); |
| 6454 | } |
| 6455 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6456 | int PyUnicode_Contains(PyObject *container, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6457 | PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6458 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6459 | PyObject *str, *sub; |
| 6460 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6461 | |
| 6462 | /* Coerce the two arguments */ |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6463 | sub = PyUnicode_FromObject(element); |
| 6464 | if (!sub) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6465 | PyErr_SetString(PyExc_TypeError, |
| 6466 | "'in <string>' requires string as left operand"); |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 6467 | return -1; |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 6468 | } |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 6469 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6470 | str = PyUnicode_FromObject(container); |
| 6471 | if (!str) { |
| 6472 | Py_DECREF(sub); |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 6473 | return -1; |
| 6474 | } |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6475 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6476 | result = stringlib_contains_obj(str, sub); |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 6477 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6478 | Py_DECREF(str); |
| 6479 | Py_DECREF(sub); |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6480 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6481 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6482 | } |
| 6483 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6484 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 6485 | |
| 6486 | PyObject *PyUnicode_Concat(PyObject *left, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6487 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6488 | { |
| 6489 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 6490 | |
| 6491 | /* Coerce the two arguments */ |
| 6492 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 6493 | if (u == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6494 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6495 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 6496 | if (v == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6497 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6498 | |
| 6499 | /* Shortcuts */ |
| 6500 | if (v == unicode_empty) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6501 | Py_DECREF(v); |
| 6502 | return (PyObject *)u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6503 | } |
| 6504 | if (u == unicode_empty) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6505 | Py_DECREF(u); |
| 6506 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6507 | } |
| 6508 | |
| 6509 | /* Concat the two Unicode strings */ |
| 6510 | w = _PyUnicode_New(u->length + v->length); |
| 6511 | if (w == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6512 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6513 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 6514 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 6515 | |
| 6516 | Py_DECREF(u); |
| 6517 | Py_DECREF(v); |
| 6518 | return (PyObject *)w; |
| 6519 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6520 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6521 | Py_XDECREF(u); |
| 6522 | Py_XDECREF(v); |
| 6523 | return NULL; |
| 6524 | } |
| 6525 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6526 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6527 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6528 | \n\ |
Fredrik Lundh | 763b50f | 2006-05-22 15:35:12 +0000 | [diff] [blame] | 6529 | Return the number of non-overlapping occurrences of substring sub in\n\ |
| 6530 | Unicode 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] | 6531 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6532 | |
| 6533 | static PyObject * |
| 6534 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 6535 | { |
| 6536 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6537 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6538 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6539 | PyObject *result; |
| 6540 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6541 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6542 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6543 | return NULL; |
| 6544 | |
| 6545 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6546 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6547 | if (substring == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6548 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6549 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 6550 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6551 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6552 | result = PyInt_FromSsize_t( |
| 6553 | stringlib_count(self->str + start, end - start, |
| 6554 | substring->str, substring->length) |
| 6555 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6556 | |
| 6557 | Py_DECREF(substring); |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6558 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6559 | return result; |
| 6560 | } |
| 6561 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6562 | PyDoc_STRVAR(encode__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6563 | "S.encode([encoding[,errors]]) -> string or unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6564 | \n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6565 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 6566 | to the default encoding. errors may be given to set a different error\n\ |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6567 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6568 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 6569 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 6570 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6571 | |
| 6572 | static PyObject * |
| 6573 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 6574 | { |
| 6575 | char *encoding = NULL; |
| 6576 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6577 | PyObject *v; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 6578 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6579 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 6580 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6581 | v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6582 | if (v == NULL) |
| 6583 | goto onError; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 6584 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6585 | PyErr_Format(PyExc_TypeError, |
| 6586 | "encoder did not return a string/unicode object " |
| 6587 | "(type=%.400s)", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6588 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6589 | Py_DECREF(v); |
| 6590 | return NULL; |
| 6591 | } |
| 6592 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6593 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6594 | onError: |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6595 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6596 | } |
| 6597 | |
| 6598 | PyDoc_STRVAR(decode__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6599 | "S.decode([encoding[,errors]]) -> string or unicode\n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6600 | \n\ |
| 6601 | Decodes S using the codec registered for encoding. encoding defaults\n\ |
| 6602 | to the default encoding. errors may be given to set a different error\n\ |
| 6603 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 6604 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 6605 | as well as any other name registerd with codecs.register_error that is\n\ |
| 6606 | able to handle UnicodeDecodeErrors."); |
| 6607 | |
| 6608 | static PyObject * |
Marc-André Lemburg | 126b44c | 2004-07-10 12:04:20 +0000 | [diff] [blame] | 6609 | unicode_decode(PyUnicodeObject *self, PyObject *args) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6610 | { |
| 6611 | char *encoding = NULL; |
| 6612 | char *errors = NULL; |
| 6613 | PyObject *v; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 6614 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6615 | if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
| 6616 | return NULL; |
| 6617 | v = PyUnicode_AsDecodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6618 | if (v == NULL) |
| 6619 | goto onError; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 6620 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6621 | PyErr_Format(PyExc_TypeError, |
| 6622 | "decoder did not return a string/unicode object " |
| 6623 | "(type=%.400s)", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6624 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6625 | Py_DECREF(v); |
| 6626 | return NULL; |
| 6627 | } |
| 6628 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6629 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6630 | onError: |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6631 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6632 | } |
| 6633 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6634 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6635 | "S.expandtabs([tabsize]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6636 | \n\ |
| 6637 | 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] | 6638 | 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] | 6639 | |
| 6640 | static PyObject* |
| 6641 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 6642 | { |
| 6643 | Py_UNICODE *e; |
| 6644 | Py_UNICODE *p; |
| 6645 | Py_UNICODE *q; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6646 | Py_UNICODE *qe; |
| 6647 | Py_ssize_t i, j, incr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6648 | PyUnicodeObject *u; |
| 6649 | int tabsize = 8; |
| 6650 | |
| 6651 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6652 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6653 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6654 | /* First pass: determine size of output string */ |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6655 | i = 0; /* chars up to and including most recent \n or \r */ |
| 6656 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
| 6657 | e = self->str + self->length; /* end of input */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6658 | for (p = self->str; p < e; p++) |
| 6659 | if (*p == '\t') { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6660 | if (tabsize > 0) { |
| 6661 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 6662 | if (j > PY_SSIZE_T_MAX - incr) |
| 6663 | goto overflow1; |
| 6664 | j += incr; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6665 | } |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6666 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6667 | else { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6668 | if (j > PY_SSIZE_T_MAX - 1) |
| 6669 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6670 | j++; |
| 6671 | if (*p == '\n' || *p == '\r') { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6672 | if (i > PY_SSIZE_T_MAX - j) |
| 6673 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6674 | i += j; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6675 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6676 | } |
| 6677 | } |
| 6678 | |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6679 | if (i > PY_SSIZE_T_MAX - j) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6680 | goto overflow1; |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 6681 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6682 | /* Second pass: create output string and fill it */ |
| 6683 | u = _PyUnicode_New(i + j); |
| 6684 | if (!u) |
| 6685 | return NULL; |
| 6686 | |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6687 | j = 0; /* same as in first pass */ |
| 6688 | q = u->str; /* next output char */ |
| 6689 | qe = u->str + u->length; /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6690 | |
| 6691 | for (p = self->str; p < e; p++) |
| 6692 | if (*p == '\t') { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6693 | if (tabsize > 0) { |
| 6694 | i = tabsize - (j % tabsize); |
| 6695 | j += i; |
| 6696 | while (i--) { |
| 6697 | if (q >= qe) |
| 6698 | goto overflow2; |
| 6699 | *q++ = ' '; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6700 | } |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6701 | } |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 6702 | } |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6703 | else { |
| 6704 | if (q >= qe) |
| 6705 | goto overflow2; |
| 6706 | *q++ = *p; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6707 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6708 | if (*p == '\n' || *p == '\r') |
| 6709 | j = 0; |
| 6710 | } |
| 6711 | |
| 6712 | return (PyObject*) u; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6713 | |
| 6714 | overflow2: |
| 6715 | Py_DECREF(u); |
| 6716 | overflow1: |
| 6717 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 6718 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6719 | } |
| 6720 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6721 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6722 | "S.find(sub [,start [,end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6723 | \n\ |
| 6724 | Return the lowest index in S where substring sub is found,\n\ |
Georg Brandl | 9efd9b6 | 2007-07-29 17:38:35 +0000 | [diff] [blame] | 6725 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6726 | arguments start and end are interpreted as in slice notation.\n\ |
| 6727 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6728 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6729 | |
| 6730 | static PyObject * |
| 6731 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 6732 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6733 | PyObject *substring; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 6734 | Py_ssize_t start; |
| 6735 | Py_ssize_t end; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6736 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6737 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 6738 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6739 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6740 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 6741 | result = stringlib_find_slice( |
| 6742 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6743 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6744 | start, end |
| 6745 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6746 | |
| 6747 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6748 | |
| 6749 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6750 | } |
| 6751 | |
| 6752 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6753 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6754 | { |
| 6755 | if (index < 0 || index >= self->length) { |
| 6756 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 6757 | return NULL; |
| 6758 | } |
| 6759 | |
| 6760 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 6761 | } |
| 6762 | |
| 6763 | static long |
| 6764 | unicode_hash(PyUnicodeObject *self) |
| 6765 | { |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6766 | /* Since Unicode objects compare equal to their ASCII string |
| 6767 | counterparts, they should use the individual character values |
| 6768 | as basis for their hash value. This is needed to assure that |
| 6769 | strings and Unicode objects behave in the same way as |
| 6770 | dictionary keys. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6771 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6772 | register Py_ssize_t len; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6773 | register Py_UNICODE *p; |
| 6774 | register long x; |
| 6775 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6776 | if (self->hash != -1) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6777 | return self->hash; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6778 | len = PyUnicode_GET_SIZE(self); |
| 6779 | p = PyUnicode_AS_UNICODE(self); |
| 6780 | x = *p << 7; |
| 6781 | while (--len >= 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6782 | x = (1000003*x) ^ *p++; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6783 | x ^= PyUnicode_GET_SIZE(self); |
| 6784 | if (x == -1) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6785 | x = -2; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6786 | self->hash = x; |
| 6787 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6788 | } |
| 6789 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6790 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6791 | "S.index(sub [,start [,end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6792 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6793 | 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] | 6794 | |
| 6795 | static PyObject * |
| 6796 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 6797 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6798 | Py_ssize_t result; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6799 | PyObject *substring; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 6800 | Py_ssize_t start; |
| 6801 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6802 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 6803 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6804 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6805 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 6806 | result = stringlib_find_slice( |
| 6807 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6808 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6809 | start, end |
| 6810 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6811 | |
| 6812 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6813 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6814 | if (result < 0) { |
| 6815 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 6816 | return NULL; |
| 6817 | } |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6818 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6819 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6820 | } |
| 6821 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6822 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6823 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6824 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6825 | 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] | 6826 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6827 | |
| 6828 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6829 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6830 | { |
| 6831 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6832 | register const Py_UNICODE *e; |
| 6833 | int cased; |
| 6834 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6835 | /* Shortcut for single character strings */ |
| 6836 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6837 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6838 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6839 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6840 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6841 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6842 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6843 | e = p + PyUnicode_GET_SIZE(self); |
| 6844 | cased = 0; |
| 6845 | for (; p < e; p++) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6846 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6847 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6848 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 6849 | return PyBool_FromLong(0); |
| 6850 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 6851 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6852 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6853 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6854 | } |
| 6855 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6856 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6857 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6858 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6859 | 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] | 6860 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6861 | |
| 6862 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6863 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6864 | { |
| 6865 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6866 | register const Py_UNICODE *e; |
| 6867 | int cased; |
| 6868 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6869 | /* Shortcut for single character strings */ |
| 6870 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6871 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6872 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6873 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6874 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6875 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6876 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6877 | e = p + PyUnicode_GET_SIZE(self); |
| 6878 | cased = 0; |
| 6879 | for (; p < e; p++) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6880 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6881 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6882 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 6883 | return PyBool_FromLong(0); |
| 6884 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 6885 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6886 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6887 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6888 | } |
| 6889 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6890 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6891 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6892 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6893 | Return True if S is a titlecased string and there is at least one\n\ |
| 6894 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 6895 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 6896 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6897 | |
| 6898 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6899 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6900 | { |
| 6901 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6902 | register const Py_UNICODE *e; |
| 6903 | int cased, previous_is_cased; |
| 6904 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6905 | /* Shortcut for single character strings */ |
| 6906 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6907 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 6908 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6909 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6910 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6911 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6912 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6913 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6914 | e = p + PyUnicode_GET_SIZE(self); |
| 6915 | cased = 0; |
| 6916 | previous_is_cased = 0; |
| 6917 | for (; p < e; p++) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6918 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6919 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6920 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 6921 | if (previous_is_cased) |
| 6922 | return PyBool_FromLong(0); |
| 6923 | previous_is_cased = 1; |
| 6924 | cased = 1; |
| 6925 | } |
| 6926 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 6927 | if (!previous_is_cased) |
| 6928 | return PyBool_FromLong(0); |
| 6929 | previous_is_cased = 1; |
| 6930 | cased = 1; |
| 6931 | } |
| 6932 | else |
| 6933 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6934 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6935 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6936 | } |
| 6937 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6938 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6939 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6940 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6941 | Return True if all characters in S are whitespace\n\ |
| 6942 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6943 | |
| 6944 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6945 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6946 | { |
| 6947 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6948 | register const Py_UNICODE *e; |
| 6949 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6950 | /* Shortcut for single character strings */ |
| 6951 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6952 | Py_UNICODE_ISSPACE(*p)) |
| 6953 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6954 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6955 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6956 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6957 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6958 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6959 | e = p + PyUnicode_GET_SIZE(self); |
| 6960 | for (; p < e; p++) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6961 | if (!Py_UNICODE_ISSPACE(*p)) |
| 6962 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6963 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6964 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6965 | } |
| 6966 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6967 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6968 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6969 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6970 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6971 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6972 | |
| 6973 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6974 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6975 | { |
| 6976 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6977 | register const Py_UNICODE *e; |
| 6978 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6979 | /* Shortcut for single character strings */ |
| 6980 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6981 | Py_UNICODE_ISALPHA(*p)) |
| 6982 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6983 | |
| 6984 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6985 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6986 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6987 | |
| 6988 | e = p + PyUnicode_GET_SIZE(self); |
| 6989 | for (; p < e; p++) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6990 | if (!Py_UNICODE_ISALPHA(*p)) |
| 6991 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6992 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6993 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6994 | } |
| 6995 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6996 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6997 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6998 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6999 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7000 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7001 | |
| 7002 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7003 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7004 | { |
| 7005 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7006 | register const Py_UNICODE *e; |
| 7007 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7008 | /* Shortcut for single character strings */ |
| 7009 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7010 | Py_UNICODE_ISALNUM(*p)) |
| 7011 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7012 | |
| 7013 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7014 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7015 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7016 | |
| 7017 | e = p + PyUnicode_GET_SIZE(self); |
| 7018 | for (; p < e; p++) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7019 | if (!Py_UNICODE_ISALNUM(*p)) |
| 7020 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7021 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7022 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7023 | } |
| 7024 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7025 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7026 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7027 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7028 | 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] | 7029 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7030 | |
| 7031 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7032 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7033 | { |
| 7034 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7035 | register const Py_UNICODE *e; |
| 7036 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7037 | /* Shortcut for single character strings */ |
| 7038 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7039 | Py_UNICODE_ISDECIMAL(*p)) |
| 7040 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7041 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7042 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7043 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7044 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7045 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7046 | e = p + PyUnicode_GET_SIZE(self); |
| 7047 | for (; p < e; p++) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7048 | if (!Py_UNICODE_ISDECIMAL(*p)) |
| 7049 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7050 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7051 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7052 | } |
| 7053 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7054 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7055 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7056 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7057 | Return True if all characters in S are digits\n\ |
| 7058 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7059 | |
| 7060 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7061 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7062 | { |
| 7063 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7064 | register const Py_UNICODE *e; |
| 7065 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7066 | /* Shortcut for single character strings */ |
| 7067 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7068 | Py_UNICODE_ISDIGIT(*p)) |
| 7069 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7070 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7071 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7072 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7073 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7074 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7075 | e = p + PyUnicode_GET_SIZE(self); |
| 7076 | for (; p < e; p++) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7077 | if (!Py_UNICODE_ISDIGIT(*p)) |
| 7078 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7079 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7080 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7081 | } |
| 7082 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7083 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7084 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7085 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7086 | 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] | 7087 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7088 | |
| 7089 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7090 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7091 | { |
| 7092 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7093 | register const Py_UNICODE *e; |
| 7094 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7095 | /* Shortcut for single character strings */ |
| 7096 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7097 | Py_UNICODE_ISNUMERIC(*p)) |
| 7098 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7099 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7100 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7101 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7102 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7103 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7104 | e = p + PyUnicode_GET_SIZE(self); |
| 7105 | for (; p < e; p++) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7106 | if (!Py_UNICODE_ISNUMERIC(*p)) |
| 7107 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7108 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7109 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7110 | } |
| 7111 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7112 | PyDoc_STRVAR(join__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7113 | "S.join(sequence) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7114 | \n\ |
| 7115 | Return a string which is the concatenation of the strings in the\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7116 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7117 | |
| 7118 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7119 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7120 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7121 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7122 | } |
| 7123 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7124 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7125 | unicode_length(PyUnicodeObject *self) |
| 7126 | { |
| 7127 | return self->length; |
| 7128 | } |
| 7129 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7130 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7131 | "S.ljust(width[, fillchar]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7132 | \n\ |
Andrew M. Kuchling | efeb43e | 2008-10-04 01:05:56 +0000 | [diff] [blame] | 7133 | 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] | 7134 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7135 | |
| 7136 | static PyObject * |
| 7137 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 7138 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7139 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7140 | Py_UNICODE fillchar = ' '; |
| 7141 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7142 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7143 | return NULL; |
| 7144 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7145 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7146 | Py_INCREF(self); |
| 7147 | return (PyObject*) self; |
| 7148 | } |
| 7149 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7150 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7151 | } |
| 7152 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7153 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7154 | "S.lower() -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7155 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7156 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7157 | |
| 7158 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7159 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7160 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7161 | return fixup(self, fixlower); |
| 7162 | } |
| 7163 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7164 | #define LEFTSTRIP 0 |
| 7165 | #define RIGHTSTRIP 1 |
| 7166 | #define BOTHSTRIP 2 |
| 7167 | |
| 7168 | /* Arrays indexed by above */ |
| 7169 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 7170 | |
| 7171 | #define STRIPNAME(i) (stripformat[i]+3) |
| 7172 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7173 | /* externally visible for str.strip(unicode) */ |
| 7174 | PyObject * |
| 7175 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 7176 | { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7177 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7178 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
| 7179 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
| 7180 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 7181 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7182 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7183 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 7184 | |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7185 | i = 0; |
| 7186 | if (striptype != RIGHTSTRIP) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7187 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7188 | i++; |
| 7189 | } |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7190 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7191 | |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7192 | j = len; |
| 7193 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7194 | do { |
| 7195 | j--; |
| 7196 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 7197 | j++; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7198 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7199 | |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7200 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7201 | Py_INCREF(self); |
| 7202 | return (PyObject*)self; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7203 | } |
| 7204 | else |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7205 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7206 | } |
| 7207 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7208 | |
| 7209 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7210 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7211 | { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7212 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7213 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7214 | |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7215 | i = 0; |
| 7216 | if (striptype != RIGHTSTRIP) { |
| 7217 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 7218 | i++; |
| 7219 | } |
| 7220 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7221 | |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7222 | j = len; |
| 7223 | if (striptype != LEFTSTRIP) { |
| 7224 | do { |
| 7225 | j--; |
| 7226 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 7227 | j++; |
| 7228 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7229 | |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7230 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 7231 | Py_INCREF(self); |
| 7232 | return (PyObject*)self; |
| 7233 | } |
| 7234 | else |
| 7235 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7236 | } |
| 7237 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7238 | |
| 7239 | static PyObject * |
| 7240 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 7241 | { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7242 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7243 | |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7244 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 7245 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7246 | |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7247 | if (sep != NULL && sep != Py_None) { |
| 7248 | if (PyUnicode_Check(sep)) |
| 7249 | return _PyUnicode_XStrip(self, striptype, sep); |
| 7250 | else if (PyString_Check(sep)) { |
| 7251 | PyObject *res; |
| 7252 | sep = PyUnicode_FromObject(sep); |
| 7253 | if (sep==NULL) |
| 7254 | return NULL; |
| 7255 | res = _PyUnicode_XStrip(self, striptype, sep); |
| 7256 | Py_DECREF(sep); |
| 7257 | return res; |
| 7258 | } |
| 7259 | else { |
| 7260 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7261 | "%s arg must be None, unicode or str", |
| 7262 | STRIPNAME(striptype)); |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7263 | return NULL; |
| 7264 | } |
| 7265 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7266 | |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7267 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7268 | } |
| 7269 | |
| 7270 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7271 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7272 | "S.strip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7273 | \n\ |
| 7274 | Return a copy of the string S with leading and trailing\n\ |
| 7275 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7276 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7277 | If chars is a str, it will be converted to unicode before stripping"); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7278 | |
| 7279 | static PyObject * |
| 7280 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 7281 | { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7282 | if (PyTuple_GET_SIZE(args) == 0) |
| 7283 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 7284 | else |
| 7285 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7286 | } |
| 7287 | |
| 7288 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7289 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7290 | "S.lstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7291 | \n\ |
| 7292 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7293 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7294 | If chars is a str, it will be converted to unicode before stripping"); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7295 | |
| 7296 | static PyObject * |
| 7297 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 7298 | { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7299 | if (PyTuple_GET_SIZE(args) == 0) |
| 7300 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 7301 | else |
| 7302 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7303 | } |
| 7304 | |
| 7305 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7306 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7307 | "S.rstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7308 | \n\ |
| 7309 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7310 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7311 | If chars is a str, it will be converted to unicode before stripping"); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7312 | |
| 7313 | static PyObject * |
| 7314 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 7315 | { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7316 | if (PyTuple_GET_SIZE(args) == 0) |
| 7317 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 7318 | else |
| 7319 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7320 | } |
| 7321 | |
| 7322 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7323 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7324 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7325 | { |
| 7326 | PyUnicodeObject *u; |
| 7327 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7328 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7329 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7330 | |
| 7331 | if (len < 0) |
| 7332 | len = 0; |
| 7333 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7334 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7335 | /* no repeat, return original string */ |
| 7336 | Py_INCREF(str); |
| 7337 | return (PyObject*) str; |
| 7338 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7339 | |
| 7340 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 7341 | * needed doesn't overflow size_t |
| 7342 | */ |
| 7343 | nchars = len * str->length; |
| 7344 | if (len && nchars / len != str->length) { |
| 7345 | PyErr_SetString(PyExc_OverflowError, |
| 7346 | "repeated string is too long"); |
| 7347 | return NULL; |
| 7348 | } |
| 7349 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 7350 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 7351 | PyErr_SetString(PyExc_OverflowError, |
| 7352 | "repeated string is too long"); |
| 7353 | return NULL; |
| 7354 | } |
| 7355 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7356 | if (!u) |
| 7357 | return NULL; |
| 7358 | |
| 7359 | p = u->str; |
| 7360 | |
Fredrik Lundh | f1d60a5 | 2006-05-22 16:29:30 +0000 | [diff] [blame] | 7361 | if (str->length == 1 && len > 0) { |
| 7362 | Py_UNICODE_FILL(p, str->str[0], len); |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 7363 | } else { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7364 | Py_ssize_t done = 0; /* number of characters copied this far */ |
| 7365 | if (done < nchars) { |
Fredrik Lundh | f1d60a5 | 2006-05-22 16:29:30 +0000 | [diff] [blame] | 7366 | Py_UNICODE_COPY(p, str->str, str->length); |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 7367 | done = str->length; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7368 | } |
| 7369 | while (done < nchars) { |
Neal Norwitz | 4677fbf7 | 2008-03-25 04:18:18 +0000 | [diff] [blame] | 7370 | Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 7371 | Py_UNICODE_COPY(p+done, p, n); |
| 7372 | done += n; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7373 | } |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 7374 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7375 | |
| 7376 | return (PyObject*) u; |
| 7377 | } |
| 7378 | |
| 7379 | PyObject *PyUnicode_Replace(PyObject *obj, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7380 | PyObject *subobj, |
| 7381 | PyObject *replobj, |
| 7382 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7383 | { |
| 7384 | PyObject *self; |
| 7385 | PyObject *str1; |
| 7386 | PyObject *str2; |
| 7387 | PyObject *result; |
| 7388 | |
| 7389 | self = PyUnicode_FromObject(obj); |
| 7390 | if (self == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7391 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7392 | str1 = PyUnicode_FromObject(subobj); |
| 7393 | if (str1 == NULL) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7394 | Py_DECREF(self); |
| 7395 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7396 | } |
| 7397 | str2 = PyUnicode_FromObject(replobj); |
| 7398 | if (str2 == NULL) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7399 | Py_DECREF(self); |
| 7400 | Py_DECREF(str1); |
| 7401 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7402 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7403 | result = replace((PyUnicodeObject *)self, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7404 | (PyUnicodeObject *)str1, |
| 7405 | (PyUnicodeObject *)str2, |
| 7406 | maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7407 | Py_DECREF(self); |
| 7408 | Py_DECREF(str1); |
| 7409 | Py_DECREF(str2); |
| 7410 | return result; |
| 7411 | } |
| 7412 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7413 | PyDoc_STRVAR(replace__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7414 | "S.replace (old, new[, count]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7415 | \n\ |
| 7416 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | 30fadc1 | 2008-05-30 07:54:16 +0000 | [diff] [blame] | 7417 | old replaced by new. If the optional argument count is\n\ |
| 7418 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7419 | |
| 7420 | static PyObject* |
| 7421 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 7422 | { |
| 7423 | PyUnicodeObject *str1; |
| 7424 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7425 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7426 | PyObject *result; |
| 7427 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7428 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7429 | return NULL; |
| 7430 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 7431 | if (str1 == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7432 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7433 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7434 | if (str2 == NULL) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7435 | Py_DECREF(str1); |
| 7436 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7437 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7438 | |
| 7439 | result = replace(self, str1, str2, maxcount); |
| 7440 | |
| 7441 | Py_DECREF(str1); |
| 7442 | Py_DECREF(str2); |
| 7443 | return result; |
| 7444 | } |
| 7445 | |
| 7446 | static |
| 7447 | PyObject *unicode_repr(PyObject *unicode) |
| 7448 | { |
| 7449 | return unicodeescape_string(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7450 | PyUnicode_GET_SIZE(unicode), |
| 7451 | 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7452 | } |
| 7453 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7454 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7455 | "S.rfind(sub [,start [,end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7456 | \n\ |
| 7457 | Return the highest index in S where substring sub is found,\n\ |
Georg Brandl | 9efd9b6 | 2007-07-29 17:38:35 +0000 | [diff] [blame] | 7458 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7459 | arguments start and end are interpreted as in slice notation.\n\ |
| 7460 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7461 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7462 | |
| 7463 | static PyObject * |
| 7464 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 7465 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7466 | PyObject *substring; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 7467 | Py_ssize_t start; |
| 7468 | Py_ssize_t end; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7469 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7470 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 7471 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7472 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7473 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 7474 | result = stringlib_rfind_slice( |
| 7475 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7476 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7477 | start, end |
| 7478 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7479 | |
| 7480 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7481 | |
| 7482 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7483 | } |
| 7484 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7485 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7486 | "S.rindex(sub [,start [,end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7487 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7488 | 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] | 7489 | |
| 7490 | static PyObject * |
| 7491 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 7492 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7493 | PyObject *substring; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 7494 | Py_ssize_t start; |
| 7495 | Py_ssize_t end; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7496 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7497 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 7498 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7499 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7500 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 7501 | result = stringlib_rfind_slice( |
| 7502 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7503 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7504 | start, end |
| 7505 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7506 | |
| 7507 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7508 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7509 | if (result < 0) { |
| 7510 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7511 | return NULL; |
| 7512 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7513 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7514 | } |
| 7515 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7516 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7517 | "S.rjust(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7518 | \n\ |
Andrew M. Kuchling | efeb43e | 2008-10-04 01:05:56 +0000 | [diff] [blame] | 7519 | Return S right-justified in a Unicode string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7520 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7521 | |
| 7522 | static PyObject * |
| 7523 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 7524 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7525 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7526 | Py_UNICODE fillchar = ' '; |
| 7527 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7528 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7529 | return NULL; |
| 7530 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7531 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7532 | Py_INCREF(self); |
| 7533 | return (PyObject*) self; |
| 7534 | } |
| 7535 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7536 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7537 | } |
| 7538 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7539 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7540 | unicode_slice(PyUnicodeObject *self, Py_ssize_t start, Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7541 | { |
| 7542 | /* standard clamping */ |
| 7543 | if (start < 0) |
| 7544 | start = 0; |
| 7545 | if (end < 0) |
| 7546 | end = 0; |
| 7547 | if (end > self->length) |
| 7548 | end = self->length; |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7549 | if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7550 | /* full slice, return original string */ |
| 7551 | Py_INCREF(self); |
| 7552 | return (PyObject*) self; |
| 7553 | } |
| 7554 | if (start > end) |
| 7555 | start = end; |
| 7556 | /* copy slice */ |
| 7557 | return (PyObject*) PyUnicode_FromUnicode(self->str + start, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7558 | end - start); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7559 | } |
| 7560 | |
| 7561 | PyObject *PyUnicode_Split(PyObject *s, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7562 | PyObject *sep, |
| 7563 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7564 | { |
| 7565 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7566 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7567 | s = PyUnicode_FromObject(s); |
| 7568 | if (s == NULL) |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7569 | return NULL; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7570 | if (sep != NULL) { |
| 7571 | sep = PyUnicode_FromObject(sep); |
| 7572 | if (sep == NULL) { |
| 7573 | Py_DECREF(s); |
| 7574 | return NULL; |
| 7575 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7576 | } |
| 7577 | |
| 7578 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7579 | |
| 7580 | Py_DECREF(s); |
| 7581 | Py_XDECREF(sep); |
| 7582 | return result; |
| 7583 | } |
| 7584 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7585 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7586 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7587 | \n\ |
| 7588 | Return a list of the words in S, using sep as the\n\ |
| 7589 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Georg Brandl | dfb77db | 2008-05-11 09:11:40 +0000 | [diff] [blame] | 7590 | splits are done. If sep is not specified or is None, any\n\ |
Georg Brandl | ecbbd94 | 2008-05-11 20:53:55 +0000 | [diff] [blame] | 7591 | whitespace string is a separator and empty strings are\n\ |
| 7592 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7593 | |
| 7594 | static PyObject* |
| 7595 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 7596 | { |
| 7597 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7598 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7599 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7600 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7601 | return NULL; |
| 7602 | |
| 7603 | if (substring == Py_None) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7604 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7605 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7606 | return split(self, (PyUnicodeObject *)substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7607 | else |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7608 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7609 | } |
| 7610 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7611 | PyObject * |
| 7612 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 7613 | { |
| 7614 | PyObject* str_obj; |
| 7615 | PyObject* sep_obj; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7616 | PyObject* out; |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 7617 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7618 | str_obj = PyUnicode_FromObject(str_in); |
| 7619 | if (!str_obj) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7620 | return NULL; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7621 | sep_obj = PyUnicode_FromObject(sep_in); |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 7622 | if (!sep_obj) { |
| 7623 | Py_DECREF(str_obj); |
| 7624 | return NULL; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7625 | } |
| 7626 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 7627 | out = stringlib_partition( |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 7628 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7629 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7630 | ); |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7631 | |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 7632 | Py_DECREF(sep_obj); |
| 7633 | Py_DECREF(str_obj); |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7634 | |
| 7635 | return out; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7636 | } |
| 7637 | |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7638 | |
| 7639 | PyObject * |
| 7640 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 7641 | { |
| 7642 | PyObject* str_obj; |
| 7643 | PyObject* sep_obj; |
| 7644 | PyObject* out; |
| 7645 | |
| 7646 | str_obj = PyUnicode_FromObject(str_in); |
| 7647 | if (!str_obj) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7648 | return NULL; |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7649 | sep_obj = PyUnicode_FromObject(sep_in); |
| 7650 | if (!sep_obj) { |
| 7651 | Py_DECREF(str_obj); |
| 7652 | return NULL; |
| 7653 | } |
| 7654 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 7655 | out = stringlib_rpartition( |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7656 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7657 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7658 | ); |
| 7659 | |
| 7660 | Py_DECREF(sep_obj); |
| 7661 | Py_DECREF(str_obj); |
| 7662 | |
| 7663 | return out; |
| 7664 | } |
| 7665 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7666 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7667 | "S.partition(sep) -> (head, sep, tail)\n\ |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7668 | \n\ |
Andrew M. Kuchling | efeb43e | 2008-10-04 01:05:56 +0000 | [diff] [blame] | 7669 | Search for the separator sep in S, and return the part before it,\n\ |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7670 | the separator itself, and the part after it. If the separator is not\n\ |
Andrew M. Kuchling | efeb43e | 2008-10-04 01:05:56 +0000 | [diff] [blame] | 7671 | found, return S and two empty strings."); |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7672 | |
| 7673 | static PyObject* |
Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 7674 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7675 | { |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7676 | return PyUnicode_Partition((PyObject *)self, separator); |
| 7677 | } |
| 7678 | |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7679 | PyDoc_STRVAR(rpartition__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7680 | "S.rpartition(sep) -> (tail, sep, head)\n\ |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7681 | \n\ |
Andrew M. Kuchling | efeb43e | 2008-10-04 01:05:56 +0000 | [diff] [blame] | 7682 | Search for the separator sep in S, starting at the end of S, and return\n\ |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7683 | the part before it, the separator itself, and the part after it. If the\n\ |
Andrew M. Kuchling | efeb43e | 2008-10-04 01:05:56 +0000 | [diff] [blame] | 7684 | separator is not found, return two empty strings and S."); |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7685 | |
| 7686 | static PyObject* |
| 7687 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 7688 | { |
| 7689 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 7690 | } |
| 7691 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7692 | PyObject *PyUnicode_RSplit(PyObject *s, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7693 | PyObject *sep, |
| 7694 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7695 | { |
| 7696 | PyObject *result; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7697 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7698 | s = PyUnicode_FromObject(s); |
| 7699 | if (s == NULL) |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7700 | return NULL; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7701 | if (sep != NULL) { |
| 7702 | sep = PyUnicode_FromObject(sep); |
| 7703 | if (sep == NULL) { |
| 7704 | Py_DECREF(s); |
| 7705 | return NULL; |
| 7706 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7707 | } |
| 7708 | |
| 7709 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7710 | |
| 7711 | Py_DECREF(s); |
| 7712 | Py_XDECREF(sep); |
| 7713 | return result; |
| 7714 | } |
| 7715 | |
| 7716 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7717 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7718 | \n\ |
| 7719 | Return a list of the words in S, using sep as the\n\ |
| 7720 | delimiter string, starting at the end of the string and\n\ |
| 7721 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 7722 | splits are done. If sep is not specified, any whitespace string\n\ |
| 7723 | is a separator."); |
| 7724 | |
| 7725 | static PyObject* |
| 7726 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 7727 | { |
| 7728 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7729 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7730 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7731 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7732 | return NULL; |
| 7733 | |
| 7734 | if (substring == Py_None) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7735 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7736 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7737 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7738 | else |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7739 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7740 | } |
| 7741 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7742 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7743 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7744 | \n\ |
| 7745 | 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] | 7746 | 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] | 7747 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7748 | |
| 7749 | static PyObject* |
| 7750 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 7751 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7752 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7753 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7754 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7755 | return NULL; |
| 7756 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7757 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7758 | } |
| 7759 | |
| 7760 | static |
| 7761 | PyObject *unicode_str(PyUnicodeObject *self) |
| 7762 | { |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7763 | return PyUnicode_AsEncodedString((PyObject *)self, NULL, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7764 | } |
| 7765 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7766 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7767 | "S.swapcase() -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7768 | \n\ |
| 7769 | 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] | 7770 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7771 | |
| 7772 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7773 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7774 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7775 | return fixup(self, fixswapcase); |
| 7776 | } |
| 7777 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7778 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7779 | "S.translate(table) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7780 | \n\ |
| 7781 | Return a copy of the string S, where all characters have been mapped\n\ |
| 7782 | through the given translation table, which must be a mapping of\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 7783 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ |
| 7784 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 7785 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7786 | |
| 7787 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7788 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7789 | { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7790 | return PyUnicode_TranslateCharmap(self->str, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7791 | self->length, |
| 7792 | table, |
| 7793 | "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7794 | } |
| 7795 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7796 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7797 | "S.upper() -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7798 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7799 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7800 | |
| 7801 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7802 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7803 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7804 | return fixup(self, fixupper); |
| 7805 | } |
| 7806 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7807 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7808 | "S.zfill(width) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7809 | \n\ |
Georg Brandl | 9806407 | 2008-09-09 19:26:00 +0000 | [diff] [blame] | 7810 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 7811 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7812 | |
| 7813 | static PyObject * |
| 7814 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 7815 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7816 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7817 | PyUnicodeObject *u; |
| 7818 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7819 | Py_ssize_t width; |
| 7820 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7821 | return NULL; |
| 7822 | |
| 7823 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 7824 | if (PyUnicode_CheckExact(self)) { |
| 7825 | Py_INCREF(self); |
| 7826 | return (PyObject*) self; |
| 7827 | } |
| 7828 | else |
| 7829 | return PyUnicode_FromUnicode( |
| 7830 | PyUnicode_AS_UNICODE(self), |
| 7831 | PyUnicode_GET_SIZE(self) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7832 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7833 | } |
| 7834 | |
| 7835 | fill = width - self->length; |
| 7836 | |
| 7837 | u = pad(self, fill, 0, '0'); |
| 7838 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 7839 | if (u == NULL) |
| 7840 | return NULL; |
| 7841 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7842 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 7843 | /* move sign to beginning of string */ |
| 7844 | u->str[0] = u->str[fill]; |
| 7845 | u->str[fill] = '0'; |
| 7846 | } |
| 7847 | |
| 7848 | return (PyObject*) u; |
| 7849 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7850 | |
| 7851 | #if 0 |
| 7852 | static PyObject* |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 7853 | free_listsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7854 | { |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 7855 | return PyInt_FromLong(numfree); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7856 | } |
| 7857 | #endif |
| 7858 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7859 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7860 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7861 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7862 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 7863 | With optional start, test S beginning at that position.\n\ |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7864 | With optional end, stop comparing S at that position.\n\ |
| 7865 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7866 | |
| 7867 | static PyObject * |
| 7868 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7869 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7870 | { |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7871 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7872 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7873 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7874 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7875 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7876 | |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7877 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7878 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 7879 | return NULL; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7880 | if (PyTuple_Check(subobj)) { |
| 7881 | Py_ssize_t i; |
| 7882 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 7883 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7884 | PyTuple_GET_ITEM(subobj, i)); |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7885 | if (substring == NULL) |
| 7886 | return NULL; |
| 7887 | result = tailmatch(self, substring, start, end, -1); |
| 7888 | Py_DECREF(substring); |
| 7889 | if (result) { |
| 7890 | Py_RETURN_TRUE; |
| 7891 | } |
| 7892 | } |
| 7893 | /* nothing matched */ |
| 7894 | Py_RETURN_FALSE; |
| 7895 | } |
| 7896 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7897 | if (substring == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7898 | return NULL; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7899 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7900 | Py_DECREF(substring); |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7901 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7902 | } |
| 7903 | |
| 7904 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7905 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7906 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7907 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7908 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 7909 | With optional start, test S beginning at that position.\n\ |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7910 | With optional end, stop comparing S at that position.\n\ |
| 7911 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7912 | |
| 7913 | static PyObject * |
| 7914 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7915 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7916 | { |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7917 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7918 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7919 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7920 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7921 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7922 | |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7923 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7924 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 7925 | return NULL; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7926 | if (PyTuple_Check(subobj)) { |
| 7927 | Py_ssize_t i; |
| 7928 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 7929 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7930 | PyTuple_GET_ITEM(subobj, i)); |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7931 | if (substring == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7932 | return NULL; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7933 | result = tailmatch(self, substring, start, end, +1); |
| 7934 | Py_DECREF(substring); |
| 7935 | if (result) { |
| 7936 | Py_RETURN_TRUE; |
| 7937 | } |
| 7938 | } |
| 7939 | Py_RETURN_FALSE; |
| 7940 | } |
| 7941 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7942 | if (substring == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7943 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7944 | |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7945 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7946 | Py_DECREF(substring); |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7947 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7948 | } |
| 7949 | |
| 7950 | |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 7951 | /* Implements do_string_format, which is unicode because of stringlib */ |
| 7952 | #include "stringlib/string_format.h" |
| 7953 | |
| 7954 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7955 | "S.format(*args, **kwargs) -> unicode\n\ |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 7956 | \n\ |
| 7957 | "); |
| 7958 | |
Eric Smith | dc13b79 | 2008-05-30 18:10:04 +0000 | [diff] [blame] | 7959 | static PyObject * |
| 7960 | unicode__format__(PyObject *self, PyObject *args) |
| 7961 | { |
| 7962 | PyObject *format_spec; |
| 7963 | PyObject *result = NULL; |
| 7964 | PyObject *tmp = NULL; |
| 7965 | |
| 7966 | /* If 2.x, convert format_spec to the same type as value */ |
| 7967 | /* This is to allow things like u''.format('') */ |
| 7968 | if (!PyArg_ParseTuple(args, "O:__format__", &format_spec)) |
| 7969 | goto done; |
| 7970 | if (!(PyBytes_Check(format_spec) || PyUnicode_Check(format_spec))) { |
| 7971 | PyErr_Format(PyExc_TypeError, "__format__ arg must be str " |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7972 | "or unicode, not %s", Py_TYPE(format_spec)->tp_name); |
Eric Smith | dc13b79 | 2008-05-30 18:10:04 +0000 | [diff] [blame] | 7973 | goto done; |
| 7974 | } |
| 7975 | tmp = PyObject_Unicode(format_spec); |
| 7976 | if (tmp == NULL) |
| 7977 | goto done; |
| 7978 | format_spec = tmp; |
| 7979 | |
| 7980 | result = _PyUnicode_FormatAdvanced(self, |
| 7981 | PyUnicode_AS_UNICODE(format_spec), |
| 7982 | PyUnicode_GET_SIZE(format_spec)); |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7983 | done: |
Eric Smith | dc13b79 | 2008-05-30 18:10:04 +0000 | [diff] [blame] | 7984 | Py_XDECREF(tmp); |
| 7985 | return result; |
| 7986 | } |
| 7987 | |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 7988 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7989 | "S.__format__(format_spec) -> unicode\n\ |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 7990 | \n\ |
| 7991 | "); |
| 7992 | |
Robert Schuppenies | 901c997 | 2008-06-10 10:10:31 +0000 | [diff] [blame] | 7993 | static PyObject * |
| 7994 | unicode__sizeof__(PyUnicodeObject *v) |
| 7995 | { |
Robert Schuppenies | 9be2ec1 | 2008-07-10 15:24:04 +0000 | [diff] [blame] | 7996 | return PyInt_FromSsize_t(sizeof(PyUnicodeObject) + |
| 7997 | sizeof(Py_UNICODE) * (v->length + 1)); |
Robert Schuppenies | 901c997 | 2008-06-10 10:10:31 +0000 | [diff] [blame] | 7998 | } |
| 7999 | |
| 8000 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8001 | "S.__sizeof__() -> size of S in memory, in bytes\n\ |
Robert Schuppenies | 901c997 | 2008-06-10 10:10:31 +0000 | [diff] [blame] | 8002 | \n\ |
| 8003 | "); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8004 | |
| 8005 | static PyObject * |
| 8006 | unicode_getnewargs(PyUnicodeObject *v) |
| 8007 | { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8008 | return Py_BuildValue("(u#)", v->str, v->length); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8009 | } |
| 8010 | |
| 8011 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8012 | static PyMethodDef unicode_methods[] = { |
| 8013 | |
| 8014 | /* Order is according to common usage: often used methods should |
| 8015 | appear first, since lookup is done sequentially. */ |
| 8016 | |
Georg Brandl | ecdc0a9 | 2006-03-30 12:19:07 +0000 | [diff] [blame] | 8017 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8018 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 8019 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8020 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8021 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 8022 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 8023 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 8024 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 8025 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 8026 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 8027 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 8028 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8029 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 8030 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 8031 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8032 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 8033 | {"decode", (PyCFunction) unicode_decode, METH_VARARGS, decode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8034 | /* {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */ |
| 8035 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 8036 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 8037 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8038 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 8039 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8040 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8041 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8042 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 8043 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 8044 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 8045 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 8046 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 8047 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 8048 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 8049 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 8050 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 8051 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 8052 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 8053 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 8054 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 8055 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8056 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 8057 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
| 8058 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
| 8059 | {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
| 8060 | {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
Robert Schuppenies | 901c997 | 2008-06-10 10:10:31 +0000 | [diff] [blame] | 8061 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8062 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8063 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8064 | #endif |
| 8065 | |
| 8066 | #if 0 |
| 8067 | /* This one is just used for debugging the implementation. */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 8068 | {"freelistsize", (PyCFunction) free_listsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8069 | #endif |
| 8070 | |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8071 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8072 | {NULL, NULL} |
| 8073 | }; |
| 8074 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8075 | static PyObject * |
| 8076 | unicode_mod(PyObject *v, PyObject *w) |
| 8077 | { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8078 | if (!PyUnicode_Check(v)) { |
| 8079 | Py_INCREF(Py_NotImplemented); |
| 8080 | return Py_NotImplemented; |
| 8081 | } |
| 8082 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8083 | } |
| 8084 | |
| 8085 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8086 | 0, /*nb_add*/ |
| 8087 | 0, /*nb_subtract*/ |
| 8088 | 0, /*nb_multiply*/ |
| 8089 | 0, /*nb_divide*/ |
| 8090 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8091 | }; |
| 8092 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8093 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8094 | (lenfunc) unicode_length, /* sq_length */ |
| 8095 | PyUnicode_Concat, /* sq_concat */ |
| 8096 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 8097 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 8098 | (ssizessizeargfunc) unicode_slice, /* sq_slice */ |
| 8099 | 0, /* sq_ass_item */ |
| 8100 | 0, /* sq_ass_slice */ |
| 8101 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8102 | }; |
| 8103 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8104 | static PyObject* |
| 8105 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 8106 | { |
Marc-André Lemburg | 3a45779 | 2006-08-14 12:57:27 +0000 | [diff] [blame] | 8107 | if (PyIndex_Check(item)) { |
| 8108 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8109 | if (i == -1 && PyErr_Occurred()) |
| 8110 | return NULL; |
| 8111 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8112 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8113 | return unicode_getitem(self, i); |
| 8114 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8115 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8116 | Py_UNICODE* source_buf; |
| 8117 | Py_UNICODE* result_buf; |
| 8118 | PyObject* result; |
| 8119 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8120 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8121 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8122 | return NULL; |
| 8123 | } |
| 8124 | |
| 8125 | if (slicelength <= 0) { |
| 8126 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 8127 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 8128 | PyUnicode_CheckExact(self)) { |
| 8129 | Py_INCREF(self); |
| 8130 | return (PyObject *)self; |
| 8131 | } else if (step == 1) { |
| 8132 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8133 | } else { |
| 8134 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 8135 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 8136 | sizeof(Py_UNICODE)); |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8137 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8138 | if (result_buf == NULL) |
| 8139 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8140 | |
| 8141 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 8142 | result_buf[i] = source_buf[cur]; |
| 8143 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8144 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8145 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 8146 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8147 | return result; |
| 8148 | } |
| 8149 | } else { |
| 8150 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 8151 | return NULL; |
| 8152 | } |
| 8153 | } |
| 8154 | |
| 8155 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8156 | (lenfunc)unicode_length, /* mp_length */ |
| 8157 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 8158 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8159 | }; |
| 8160 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8161 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8162 | unicode_buffer_getreadbuf(PyUnicodeObject *self, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8163 | Py_ssize_t index, |
| 8164 | const void **ptr) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8165 | { |
| 8166 | if (index != 0) { |
| 8167 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8168 | "accessing non-existent unicode segment"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8169 | return -1; |
| 8170 | } |
| 8171 | *ptr = (void *) self->str; |
| 8172 | return PyUnicode_GET_DATA_SIZE(self); |
| 8173 | } |
| 8174 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8175 | static Py_ssize_t |
| 8176 | unicode_buffer_getwritebuf(PyUnicodeObject *self, Py_ssize_t index, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8177 | const void **ptr) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8178 | { |
| 8179 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8180 | "cannot use unicode as modifiable buffer"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8181 | return -1; |
| 8182 | } |
| 8183 | |
| 8184 | static int |
| 8185 | unicode_buffer_getsegcount(PyUnicodeObject *self, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8186 | Py_ssize_t *lenp) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8187 | { |
| 8188 | if (lenp) |
| 8189 | *lenp = PyUnicode_GET_DATA_SIZE(self); |
| 8190 | return 1; |
| 8191 | } |
| 8192 | |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 8193 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8194 | unicode_buffer_getcharbuf(PyUnicodeObject *self, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8195 | Py_ssize_t index, |
| 8196 | const void **ptr) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8197 | { |
| 8198 | PyObject *str; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8199 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8200 | if (index != 0) { |
| 8201 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8202 | "accessing non-existent unicode segment"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8203 | return -1; |
| 8204 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 8205 | str = _PyUnicode_AsDefaultEncodedString((PyObject *)self, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8206 | if (str == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8207 | return -1; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 8208 | *ptr = (void *) PyString_AS_STRING(str); |
| 8209 | return PyString_GET_SIZE(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8210 | } |
| 8211 | |
| 8212 | /* Helpers for PyUnicode_Format() */ |
| 8213 | |
| 8214 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8215 | 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] | 8216 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8217 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8218 | if (argidx < arglen) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8219 | (*p_argidx)++; |
| 8220 | if (arglen < 0) |
| 8221 | return args; |
| 8222 | else |
| 8223 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8224 | } |
| 8225 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8226 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8227 | return NULL; |
| 8228 | } |
| 8229 | |
| 8230 | #define F_LJUST (1<<0) |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8231 | #define F_SIGN (1<<1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8232 | #define F_BLANK (1<<2) |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8233 | #define F_ALT (1<<3) |
| 8234 | #define F_ZERO (1<<4) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8235 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8236 | static Py_ssize_t |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8237 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8238 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8239 | register Py_ssize_t i; |
| 8240 | Py_ssize_t len = strlen(charbuffer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8241 | for (i = len - 1; i >= 0; i--) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8242 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8243 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8244 | return len; |
| 8245 | } |
| 8246 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8247 | static int |
| 8248 | doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) |
| 8249 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8250 | Py_ssize_t result; |
| 8251 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8252 | PyOS_ascii_formatd((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8253 | result = strtounicode(buffer, (char *)buffer); |
| 8254 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8255 | } |
| 8256 | |
| 8257 | static int |
| 8258 | longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) |
| 8259 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8260 | Py_ssize_t result; |
| 8261 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8262 | PyOS_snprintf((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8263 | result = strtounicode(buffer, (char *)buffer); |
| 8264 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8265 | } |
| 8266 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8267 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 8268 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 8269 | formatting is done. */ |
| 8270 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8271 | static int |
| 8272 | formatfloat(Py_UNICODE *buf, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8273 | size_t buflen, |
| 8274 | int flags, |
| 8275 | int prec, |
| 8276 | int type, |
| 8277 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8278 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8279 | /* fmt = '%#.' + `prec` + `type` |
| 8280 | worst case length = 3 + 10 (len of INT_MAX) + 1 = 14 (use 20)*/ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8281 | char fmt[20]; |
| 8282 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8283 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8284 | x = PyFloat_AsDouble(v); |
| 8285 | if (x == -1.0 && PyErr_Occurred()) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8286 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8287 | if (prec < 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8288 | prec = 6; |
Eric Smith | d6c393a | 2008-07-17 19:49:47 +0000 | [diff] [blame] | 8289 | if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8290 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8291 | /* Worst case length calc to ensure no buffer overrun: |
| 8292 | |
| 8293 | 'g' formats: |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8294 | fmt = %#.<prec>g |
| 8295 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 8296 | for any double rep.) |
| 8297 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8298 | |
| 8299 | 'f' formats: |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8300 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 8301 | len = 1 + 50 + 1 + prec = 52 + prec |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8302 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8303 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8304 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8305 | |
| 8306 | */ |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8307 | if (((type == 'g' || type == 'G') && |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8308 | buflen <= (size_t)10 + (size_t)prec) || |
| 8309 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
| 8310 | PyErr_SetString(PyExc_OverflowError, |
| 8311 | "formatted float is too long (precision too large?)"); |
| 8312 | return -1; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8313 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8314 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8315 | (flags&F_ALT) ? "#" : "", |
| 8316 | prec, type); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8317 | return doubletounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8318 | } |
| 8319 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8320 | static PyObject* |
| 8321 | formatlong(PyObject *val, int flags, int prec, int type) |
| 8322 | { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8323 | char *buf; |
| 8324 | int i, len; |
| 8325 | PyObject *str; /* temporary string object. */ |
| 8326 | PyUnicodeObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8327 | |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8328 | str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
| 8329 | if (!str) |
| 8330 | return NULL; |
| 8331 | result = _PyUnicode_New(len); |
| 8332 | if (!result) { |
| 8333 | Py_DECREF(str); |
| 8334 | return NULL; |
| 8335 | } |
| 8336 | for (i = 0; i < len; i++) |
| 8337 | result->str[i] = buf[i]; |
| 8338 | result->str[len] = 0; |
| 8339 | Py_DECREF(str); |
| 8340 | return (PyObject*)result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8341 | } |
| 8342 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8343 | static int |
| 8344 | formatint(Py_UNICODE *buf, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8345 | size_t buflen, |
| 8346 | int flags, |
| 8347 | int prec, |
| 8348 | int type, |
| 8349 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8350 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8351 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8352 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 8353 | * + 1 + 1 |
| 8354 | * = 24 |
| 8355 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8356 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8357 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8358 | long x; |
| 8359 | |
| 8360 | x = PyInt_AsLong(v); |
| 8361 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8362 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8363 | if (x < 0 && type == 'u') { |
| 8364 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8365 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8366 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 8367 | sign = "-"; |
| 8368 | else |
| 8369 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8370 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8371 | prec = 1; |
| 8372 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8373 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 8374 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8375 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8376 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8377 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8378 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8379 | return -1; |
| 8380 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8381 | |
| 8382 | if ((flags & F_ALT) && |
| 8383 | (type == 'x' || type == 'X')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8384 | /* When converting under %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8385 | * of issues that cause pain: |
| 8386 | * - when 0 is being converted, the C standard leaves off |
| 8387 | * the '0x' or '0X', which is inconsistent with other |
| 8388 | * %#x/%#X conversions and inconsistent with Python's |
| 8389 | * hex() function |
| 8390 | * - there are platforms that violate the standard and |
| 8391 | * convert 0 with the '0x' or '0X' |
| 8392 | * (Metrowerks, Compaq Tru64) |
| 8393 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8394 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8395 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8396 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8397 | * We can achieve the desired consistency by inserting our |
| 8398 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 8399 | * of %#x/%#X. |
| 8400 | * |
| 8401 | * Note that this is the same approach as used in |
| 8402 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8403 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8404 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 8405 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8406 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8407 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8408 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 8409 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8410 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 8411 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8412 | if (sign[0]) |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8413 | return longtounicode(buf, buflen, fmt, -x); |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8414 | else |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8415 | return longtounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8416 | } |
| 8417 | |
| 8418 | static int |
| 8419 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8420 | size_t buflen, |
| 8421 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8422 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8423 | /* presume that the buffer is at least 2 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8424 | if (PyUnicode_Check(v)) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8425 | if (PyUnicode_GET_SIZE(v) != 1) |
| 8426 | goto onError; |
| 8427 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8428 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8429 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 8430 | else if (PyString_Check(v)) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8431 | if (PyString_GET_SIZE(v) != 1) |
| 8432 | goto onError; |
| 8433 | buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8434 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8435 | |
| 8436 | else { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8437 | /* Integer input truncated to a character */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8438 | long x; |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8439 | x = PyInt_AsLong(v); |
| 8440 | if (x == -1 && PyErr_Occurred()) |
| 8441 | goto onError; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8442 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8443 | if (x < 0 || x > 0x10ffff) { |
| 8444 | PyErr_SetString(PyExc_OverflowError, |
| 8445 | "%c arg not in range(0x110000) " |
| 8446 | "(wide Python build)"); |
| 8447 | return -1; |
| 8448 | } |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8449 | #else |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8450 | if (x < 0 || x > 0xffff) { |
| 8451 | PyErr_SetString(PyExc_OverflowError, |
| 8452 | "%c arg not in range(0x10000) " |
| 8453 | "(narrow Python build)"); |
| 8454 | return -1; |
| 8455 | } |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8456 | #endif |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8457 | buf[0] = (Py_UNICODE) x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8458 | } |
| 8459 | buf[1] = '\0'; |
| 8460 | return 1; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8461 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8462 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8463 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8464 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8465 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8466 | } |
| 8467 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8468 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 8469 | |
| 8470 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 8471 | chars are formatted. XXX This is a magic number. Each formatting |
| 8472 | routine does bounds checking to ensure no overflow, but a better |
| 8473 | solution may be to malloc a buffer of appropriate size for each |
| 8474 | format. For now, the current solution is sufficient. |
| 8475 | */ |
| 8476 | #define FORMATBUFLEN (size_t)120 |
| 8477 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8478 | PyObject *PyUnicode_Format(PyObject *format, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8479 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8480 | { |
| 8481 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8482 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8483 | int args_owned = 0; |
| 8484 | PyUnicodeObject *result = NULL; |
| 8485 | PyObject *dict = NULL; |
| 8486 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8487 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8488 | if (format == NULL || args == NULL) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8489 | PyErr_BadInternalCall(); |
| 8490 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8491 | } |
| 8492 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8493 | if (uformat == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8494 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8495 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 8496 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 8497 | |
| 8498 | reslen = rescnt = fmtcnt + 100; |
| 8499 | result = _PyUnicode_New(reslen); |
| 8500 | if (result == NULL) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8501 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8502 | res = PyUnicode_AS_UNICODE(result); |
| 8503 | |
| 8504 | if (PyTuple_Check(args)) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8505 | arglen = PyTuple_Size(args); |
| 8506 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8507 | } |
| 8508 | else { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8509 | arglen = -1; |
| 8510 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8511 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 8512 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 8513 | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8514 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8515 | |
| 8516 | while (--fmtcnt >= 0) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8517 | if (*fmt != '%') { |
| 8518 | if (--rescnt < 0) { |
| 8519 | rescnt = fmtcnt + 100; |
| 8520 | reslen += rescnt; |
| 8521 | if (_PyUnicode_Resize(&result, reslen) < 0) |
| 8522 | goto onError; |
| 8523 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 8524 | --rescnt; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8525 | } |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8526 | *res++ = *fmt++; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8527 | } |
| 8528 | else { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8529 | /* Got a format specifier */ |
| 8530 | int flags = 0; |
| 8531 | Py_ssize_t width = -1; |
| 8532 | int prec = -1; |
| 8533 | Py_UNICODE c = '\0'; |
| 8534 | Py_UNICODE fill; |
| 8535 | int isnumok; |
| 8536 | PyObject *v = NULL; |
| 8537 | PyObject *temp = NULL; |
| 8538 | Py_UNICODE *pbuf; |
| 8539 | Py_UNICODE sign; |
| 8540 | Py_ssize_t len; |
| 8541 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
| 8542 | |
| 8543 | fmt++; |
| 8544 | if (*fmt == '(') { |
| 8545 | Py_UNICODE *keystart; |
| 8546 | Py_ssize_t keylen; |
| 8547 | PyObject *key; |
| 8548 | int pcount = 1; |
| 8549 | |
| 8550 | if (dict == NULL) { |
| 8551 | PyErr_SetString(PyExc_TypeError, |
| 8552 | "format requires a mapping"); |
| 8553 | goto onError; |
| 8554 | } |
| 8555 | ++fmt; |
| 8556 | --fmtcnt; |
| 8557 | keystart = fmt; |
| 8558 | /* Skip over balanced parentheses */ |
| 8559 | while (pcount > 0 && --fmtcnt >= 0) { |
| 8560 | if (*fmt == ')') |
| 8561 | --pcount; |
| 8562 | else if (*fmt == '(') |
| 8563 | ++pcount; |
| 8564 | fmt++; |
| 8565 | } |
| 8566 | keylen = fmt - keystart - 1; |
| 8567 | if (fmtcnt < 0 || pcount > 0) { |
| 8568 | PyErr_SetString(PyExc_ValueError, |
| 8569 | "incomplete format key"); |
| 8570 | goto onError; |
| 8571 | } |
| 8572 | #if 0 |
| 8573 | /* keys are converted to strings using UTF-8 and |
| 8574 | then looked up since Python uses strings to hold |
| 8575 | variables names etc. in its namespaces and we |
| 8576 | wouldn't want to break common idioms. */ |
| 8577 | key = PyUnicode_EncodeUTF8(keystart, |
| 8578 | keylen, |
| 8579 | NULL); |
| 8580 | #else |
| 8581 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 8582 | #endif |
| 8583 | if (key == NULL) |
| 8584 | goto onError; |
| 8585 | if (args_owned) { |
| 8586 | Py_DECREF(args); |
| 8587 | args_owned = 0; |
| 8588 | } |
| 8589 | args = PyObject_GetItem(dict, key); |
| 8590 | Py_DECREF(key); |
| 8591 | if (args == NULL) { |
| 8592 | goto onError; |
| 8593 | } |
| 8594 | args_owned = 1; |
| 8595 | arglen = -1; |
| 8596 | argidx = -2; |
| 8597 | } |
| 8598 | while (--fmtcnt >= 0) { |
| 8599 | switch (c = *fmt++) { |
| 8600 | case '-': flags |= F_LJUST; continue; |
| 8601 | case '+': flags |= F_SIGN; continue; |
| 8602 | case ' ': flags |= F_BLANK; continue; |
| 8603 | case '#': flags |= F_ALT; continue; |
| 8604 | case '0': flags |= F_ZERO; continue; |
| 8605 | } |
| 8606 | break; |
| 8607 | } |
| 8608 | if (c == '*') { |
| 8609 | v = getnextarg(args, arglen, &argidx); |
| 8610 | if (v == NULL) |
| 8611 | goto onError; |
| 8612 | if (!PyInt_Check(v)) { |
| 8613 | PyErr_SetString(PyExc_TypeError, |
| 8614 | "* wants int"); |
| 8615 | goto onError; |
| 8616 | } |
| 8617 | width = PyInt_AsLong(v); |
| 8618 | if (width < 0) { |
| 8619 | flags |= F_LJUST; |
| 8620 | width = -width; |
| 8621 | } |
| 8622 | if (--fmtcnt >= 0) |
| 8623 | c = *fmt++; |
| 8624 | } |
| 8625 | else if (c >= '0' && c <= '9') { |
| 8626 | width = c - '0'; |
| 8627 | while (--fmtcnt >= 0) { |
| 8628 | c = *fmt++; |
| 8629 | if (c < '0' || c > '9') |
| 8630 | break; |
| 8631 | if ((width*10) / 10 != width) { |
| 8632 | PyErr_SetString(PyExc_ValueError, |
| 8633 | "width too big"); |
| 8634 | goto onError; |
| 8635 | } |
| 8636 | width = width*10 + (c - '0'); |
| 8637 | } |
| 8638 | } |
| 8639 | if (c == '.') { |
| 8640 | prec = 0; |
| 8641 | if (--fmtcnt >= 0) |
| 8642 | c = *fmt++; |
| 8643 | if (c == '*') { |
| 8644 | v = getnextarg(args, arglen, &argidx); |
| 8645 | if (v == NULL) |
| 8646 | goto onError; |
| 8647 | if (!PyInt_Check(v)) { |
| 8648 | PyErr_SetString(PyExc_TypeError, |
| 8649 | "* wants int"); |
| 8650 | goto onError; |
| 8651 | } |
| 8652 | prec = PyInt_AsLong(v); |
| 8653 | if (prec < 0) |
| 8654 | prec = 0; |
| 8655 | if (--fmtcnt >= 0) |
| 8656 | c = *fmt++; |
| 8657 | } |
| 8658 | else if (c >= '0' && c <= '9') { |
| 8659 | prec = c - '0'; |
| 8660 | while (--fmtcnt >= 0) { |
| 8661 | c = Py_CHARMASK(*fmt++); |
| 8662 | if (c < '0' || c > '9') |
| 8663 | break; |
| 8664 | if ((prec*10) / 10 != prec) { |
| 8665 | PyErr_SetString(PyExc_ValueError, |
| 8666 | "prec too big"); |
| 8667 | goto onError; |
| 8668 | } |
| 8669 | prec = prec*10 + (c - '0'); |
| 8670 | } |
| 8671 | } |
| 8672 | } /* prec */ |
| 8673 | if (fmtcnt >= 0) { |
| 8674 | if (c == 'h' || c == 'l' || c == 'L') { |
| 8675 | if (--fmtcnt >= 0) |
| 8676 | c = *fmt++; |
| 8677 | } |
| 8678 | } |
| 8679 | if (fmtcnt < 0) { |
| 8680 | PyErr_SetString(PyExc_ValueError, |
| 8681 | "incomplete format"); |
| 8682 | goto onError; |
| 8683 | } |
| 8684 | if (c != '%') { |
| 8685 | v = getnextarg(args, arglen, &argidx); |
| 8686 | if (v == NULL) |
| 8687 | goto onError; |
| 8688 | } |
| 8689 | sign = 0; |
| 8690 | fill = ' '; |
| 8691 | switch (c) { |
| 8692 | |
| 8693 | case '%': |
| 8694 | pbuf = formatbuf; |
| 8695 | /* presume that buffer length is at least 1 */ |
| 8696 | pbuf[0] = '%'; |
| 8697 | len = 1; |
| 8698 | break; |
| 8699 | |
| 8700 | case 's': |
| 8701 | case 'r': |
| 8702 | if (PyUnicode_Check(v) && c == 's') { |
| 8703 | temp = v; |
| 8704 | Py_INCREF(temp); |
| 8705 | } |
| 8706 | else { |
| 8707 | PyObject *unicode; |
| 8708 | if (c == 's') |
| 8709 | temp = PyObject_Unicode(v); |
| 8710 | else |
| 8711 | temp = PyObject_Repr(v); |
| 8712 | if (temp == NULL) |
| 8713 | goto onError; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8714 | if (PyUnicode_Check(temp)) |
| 8715 | /* nothing to do */; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 8716 | else if (PyString_Check(temp)) { |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8717 | /* convert to string to Unicode */ |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8718 | unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
| 8719 | PyString_GET_SIZE(temp), |
| 8720 | NULL, |
| 8721 | "strict"); |
| 8722 | Py_DECREF(temp); |
| 8723 | temp = unicode; |
| 8724 | if (temp == NULL) |
| 8725 | goto onError; |
| 8726 | } |
| 8727 | else { |
| 8728 | Py_DECREF(temp); |
| 8729 | PyErr_SetString(PyExc_TypeError, |
| 8730 | "%s argument has non-string str()"); |
| 8731 | goto onError; |
| 8732 | } |
| 8733 | } |
| 8734 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 8735 | len = PyUnicode_GET_SIZE(temp); |
| 8736 | if (prec >= 0 && len > prec) |
| 8737 | len = prec; |
| 8738 | break; |
| 8739 | |
| 8740 | case 'i': |
| 8741 | case 'd': |
| 8742 | case 'u': |
| 8743 | case 'o': |
| 8744 | case 'x': |
| 8745 | case 'X': |
| 8746 | if (c == 'i') |
| 8747 | c = 'd'; |
| 8748 | isnumok = 0; |
| 8749 | if (PyNumber_Check(v)) { |
| 8750 | PyObject *iobj=NULL; |
| 8751 | |
| 8752 | if (PyInt_Check(v) || (PyLong_Check(v))) { |
| 8753 | iobj = v; |
| 8754 | Py_INCREF(iobj); |
| 8755 | } |
| 8756 | else { |
| 8757 | iobj = PyNumber_Int(v); |
| 8758 | if (iobj==NULL) iobj = PyNumber_Long(v); |
| 8759 | } |
| 8760 | if (iobj!=NULL) { |
| 8761 | if (PyInt_Check(iobj)) { |
| 8762 | isnumok = 1; |
| 8763 | pbuf = formatbuf; |
| 8764 | len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8765 | flags, prec, c, iobj); |
| 8766 | Py_DECREF(iobj); |
| 8767 | if (len < 0) |
| 8768 | goto onError; |
| 8769 | sign = 1; |
| 8770 | } |
| 8771 | else if (PyLong_Check(iobj)) { |
| 8772 | isnumok = 1; |
| 8773 | temp = formatlong(iobj, flags, prec, c); |
| 8774 | Py_DECREF(iobj); |
| 8775 | if (!temp) |
| 8776 | goto onError; |
| 8777 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 8778 | len = PyUnicode_GET_SIZE(temp); |
| 8779 | sign = 1; |
| 8780 | } |
| 8781 | else { |
| 8782 | Py_DECREF(iobj); |
| 8783 | } |
| 8784 | } |
| 8785 | } |
| 8786 | if (!isnumok) { |
| 8787 | PyErr_Format(PyExc_TypeError, |
| 8788 | "%%%c format: a number is required, " |
| 8789 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 8790 | goto onError; |
| 8791 | } |
| 8792 | if (flags & F_ZERO) |
| 8793 | fill = '0'; |
| 8794 | break; |
| 8795 | |
| 8796 | case 'e': |
| 8797 | case 'E': |
| 8798 | case 'f': |
| 8799 | case 'F': |
| 8800 | case 'g': |
| 8801 | case 'G': |
| 8802 | if (c == 'F') |
| 8803 | c = 'f'; |
| 8804 | pbuf = formatbuf; |
| 8805 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8806 | flags, prec, c, v); |
| 8807 | if (len < 0) |
| 8808 | goto onError; |
| 8809 | sign = 1; |
| 8810 | if (flags & F_ZERO) |
| 8811 | fill = '0'; |
| 8812 | break; |
| 8813 | |
| 8814 | case 'c': |
| 8815 | pbuf = formatbuf; |
| 8816 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
| 8817 | if (len < 0) |
| 8818 | goto onError; |
| 8819 | break; |
| 8820 | |
| 8821 | default: |
| 8822 | PyErr_Format(PyExc_ValueError, |
| 8823 | "unsupported format character '%c' (0x%x) " |
| 8824 | "at index %zd", |
| 8825 | (31<=c && c<=126) ? (char)c : '?', |
| 8826 | (int)c, |
| 8827 | (Py_ssize_t)(fmt - 1 - |
| 8828 | PyUnicode_AS_UNICODE(uformat))); |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8829 | goto onError; |
| 8830 | } |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8831 | if (sign) { |
| 8832 | if (*pbuf == '-' || *pbuf == '+') { |
| 8833 | sign = *pbuf++; |
| 8834 | len--; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8835 | } |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8836 | else if (flags & F_SIGN) |
| 8837 | sign = '+'; |
| 8838 | else if (flags & F_BLANK) |
| 8839 | sign = ' '; |
| 8840 | else |
| 8841 | sign = 0; |
| 8842 | } |
| 8843 | if (width < len) |
| 8844 | width = len; |
| 8845 | if (rescnt - (sign != 0) < width) { |
| 8846 | reslen -= rescnt; |
| 8847 | rescnt = width + fmtcnt + 100; |
| 8848 | reslen += rescnt; |
| 8849 | if (reslen < 0) { |
| 8850 | Py_XDECREF(temp); |
| 8851 | PyErr_NoMemory(); |
| 8852 | goto onError; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8853 | } |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8854 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 8855 | Py_XDECREF(temp); |
| 8856 | goto onError; |
| 8857 | } |
| 8858 | res = PyUnicode_AS_UNICODE(result) |
| 8859 | + reslen - rescnt; |
| 8860 | } |
| 8861 | if (sign) { |
| 8862 | if (fill != ' ') |
| 8863 | *res++ = sign; |
| 8864 | rescnt--; |
| 8865 | if (width > len) |
| 8866 | width--; |
| 8867 | } |
| 8868 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 8869 | assert(pbuf[0] == '0'); |
| 8870 | assert(pbuf[1] == c); |
| 8871 | if (fill != ' ') { |
| 8872 | *res++ = *pbuf++; |
| 8873 | *res++ = *pbuf++; |
| 8874 | } |
| 8875 | rescnt -= 2; |
| 8876 | width -= 2; |
| 8877 | if (width < 0) |
| 8878 | width = 0; |
| 8879 | len -= 2; |
| 8880 | } |
| 8881 | if (width > len && !(flags & F_LJUST)) { |
| 8882 | do { |
| 8883 | --rescnt; |
| 8884 | *res++ = fill; |
| 8885 | } while (--width > len); |
| 8886 | } |
| 8887 | if (fill == ' ') { |
| 8888 | if (sign) |
| 8889 | *res++ = sign; |
| 8890 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 8891 | assert(pbuf[0] == '0'); |
| 8892 | assert(pbuf[1] == c); |
| 8893 | *res++ = *pbuf++; |
| 8894 | *res++ = *pbuf++; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8895 | } |
| 8896 | } |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8897 | Py_UNICODE_COPY(res, pbuf, len); |
| 8898 | res += len; |
| 8899 | rescnt -= len; |
| 8900 | while (--width >= len) { |
| 8901 | --rescnt; |
| 8902 | *res++ = ' '; |
| 8903 | } |
| 8904 | if (dict && (argidx < arglen) && c != '%') { |
| 8905 | PyErr_SetString(PyExc_TypeError, |
| 8906 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8907 | Py_XDECREF(temp); |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8908 | goto onError; |
| 8909 | } |
| 8910 | Py_XDECREF(temp); |
| 8911 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8912 | } /* until end */ |
| 8913 | if (argidx < arglen && !dict) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8914 | PyErr_SetString(PyExc_TypeError, |
| 8915 | "not all arguments converted during string formatting"); |
| 8916 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8917 | } |
| 8918 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8919 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8920 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8921 | if (args_owned) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8922 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8923 | } |
| 8924 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8925 | return (PyObject *)result; |
| 8926 | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8927 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8928 | Py_XDECREF(result); |
| 8929 | Py_DECREF(uformat); |
| 8930 | if (args_owned) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8931 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8932 | } |
| 8933 | return NULL; |
| 8934 | } |
| 8935 | |
| 8936 | static PyBufferProcs unicode_as_buffer = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8937 | (readbufferproc) unicode_buffer_getreadbuf, |
| 8938 | (writebufferproc) unicode_buffer_getwritebuf, |
| 8939 | (segcountproc) unicode_buffer_getsegcount, |
| 8940 | (charbufferproc) unicode_buffer_getcharbuf, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8941 | }; |
| 8942 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 8943 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8944 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 8945 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8946 | static PyObject * |
| 8947 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8948 | { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8949 | PyObject *x = NULL; |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8950 | static char *kwlist[] = {"string", "encoding", "errors", 0}; |
| 8951 | char *encoding = NULL; |
| 8952 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8953 | |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8954 | if (type != &PyUnicode_Type) |
| 8955 | return unicode_subtype_new(type, args, kwds); |
| 8956 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8957 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8958 | return NULL; |
| 8959 | if (x == NULL) |
| 8960 | return (PyObject *)_PyUnicode_New(0); |
| 8961 | if (encoding == NULL && errors == NULL) |
| 8962 | return PyObject_Unicode(x); |
| 8963 | else |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8964 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8965 | } |
| 8966 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8967 | static PyObject * |
| 8968 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8969 | { |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8970 | PyUnicodeObject *tmp, *pnew; |
| 8971 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8972 | |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8973 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 8974 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 8975 | if (tmp == NULL) |
| 8976 | return NULL; |
| 8977 | assert(PyUnicode_Check(tmp)); |
| 8978 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
| 8979 | if (pnew == NULL) { |
| 8980 | Py_DECREF(tmp); |
| 8981 | return NULL; |
| 8982 | } |
| 8983 | pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
| 8984 | if (pnew->str == NULL) { |
| 8985 | _Py_ForgetReference((PyObject *)pnew); |
| 8986 | PyObject_Del(pnew); |
| 8987 | Py_DECREF(tmp); |
| 8988 | return PyErr_NoMemory(); |
| 8989 | } |
| 8990 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 8991 | pnew->length = n; |
| 8992 | pnew->hash = tmp->hash; |
| 8993 | Py_DECREF(tmp); |
| 8994 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8995 | } |
| 8996 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8997 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8998 | "unicode(string [, encoding[, errors]]) -> object\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8999 | \n\ |
| 9000 | Create a new Unicode object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 9001 | encoding defaults to the current default string encoding.\n\ |
| 9002 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9003 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9004 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 9005 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 9006 | "unicode", /* tp_name */ |
| 9007 | sizeof(PyUnicodeObject), /* tp_size */ |
| 9008 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9009 | /* Slots */ |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 9010 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 9011 | 0, /* tp_print */ |
| 9012 | 0, /* tp_getattr */ |
| 9013 | 0, /* tp_setattr */ |
| 9014 | 0, /* tp_compare */ |
| 9015 | unicode_repr, /* tp_repr */ |
| 9016 | &unicode_as_number, /* tp_as_number */ |
| 9017 | &unicode_as_sequence, /* tp_as_sequence */ |
| 9018 | &unicode_as_mapping, /* tp_as_mapping */ |
| 9019 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 9020 | 0, /* tp_call*/ |
| 9021 | (reprfunc) unicode_str, /* tp_str */ |
| 9022 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9023 | 0, /* tp_setattro */ |
| 9024 | &unicode_as_buffer, /* tp_as_buffer */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 9025 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 9026 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 9027 | unicode_doc, /* tp_doc */ |
| 9028 | 0, /* tp_traverse */ |
| 9029 | 0, /* tp_clear */ |
| 9030 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 9031 | 0, /* tp_weaklistoffset */ |
| 9032 | 0, /* tp_iter */ |
| 9033 | 0, /* tp_iternext */ |
| 9034 | unicode_methods, /* tp_methods */ |
| 9035 | 0, /* tp_members */ |
| 9036 | 0, /* tp_getset */ |
| 9037 | &PyBaseString_Type, /* tp_base */ |
| 9038 | 0, /* tp_dict */ |
| 9039 | 0, /* tp_descr_get */ |
| 9040 | 0, /* tp_descr_set */ |
| 9041 | 0, /* tp_dictoffset */ |
| 9042 | 0, /* tp_init */ |
| 9043 | 0, /* tp_alloc */ |
| 9044 | unicode_new, /* tp_new */ |
| 9045 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9046 | }; |
| 9047 | |
| 9048 | /* Initialize the Unicode implementation */ |
| 9049 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9050 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9051 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9052 | int i; |
| 9053 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 9054 | /* XXX - move this array to unicodectype.c ? */ |
| 9055 | Py_UNICODE linebreak[] = { |
| 9056 | 0x000A, /* LINE FEED */ |
| 9057 | 0x000D, /* CARRIAGE RETURN */ |
| 9058 | 0x001C, /* FILE SEPARATOR */ |
| 9059 | 0x001D, /* GROUP SEPARATOR */ |
| 9060 | 0x001E, /* RECORD SEPARATOR */ |
| 9061 | 0x0085, /* NEXT LINE */ |
| 9062 | 0x2028, /* LINE SEPARATOR */ |
| 9063 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 9064 | }; |
| 9065 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9066 | /* Init the implementation */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 9067 | free_list = NULL; |
| 9068 | numfree = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9069 | unicode_empty = _PyUnicode_New(0); |
Neal Norwitz | e1fdb32 | 2006-07-21 05:32:28 +0000 | [diff] [blame] | 9070 | if (!unicode_empty) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 9071 | return; |
Neal Norwitz | e1fdb32 | 2006-07-21 05:32:28 +0000 | [diff] [blame] | 9072 | |
Marc-André Lemburg | 90e8147 | 2000-06-07 09:13:21 +0000 | [diff] [blame] | 9073 | strcpy(unicode_default_encoding, "ascii"); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9074 | for (i = 0; i < 256; i++) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 9075 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 9076 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 9077 | Py_FatalError("Can't initialize 'unicode'"); |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 9078 | |
| 9079 | /* initialize the linebreak bloom filter */ |
| 9080 | bloom_linebreak = make_bloom_mask( |
| 9081 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 9082 | ); |
Neal Norwitz | de4c78a | 2006-06-13 08:28:19 +0000 | [diff] [blame] | 9083 | |
| 9084 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9085 | } |
| 9086 | |
| 9087 | /* Finalize the Unicode implementation */ |
| 9088 | |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 9089 | int |
| 9090 | PyUnicode_ClearFreeList(void) |
| 9091 | { |
| 9092 | int freelist_size = numfree; |
| 9093 | PyUnicodeObject *u; |
| 9094 | |
| 9095 | for (u = free_list; u != NULL;) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 9096 | PyUnicodeObject *v = u; |
| 9097 | u = *(PyUnicodeObject **)u; |
| 9098 | if (v->str) |
| 9099 | PyObject_DEL(v->str); |
| 9100 | Py_XDECREF(v->defenc); |
| 9101 | PyObject_Del(v); |
| 9102 | numfree--; |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 9103 | } |
| 9104 | free_list = NULL; |
| 9105 | assert(numfree == 0); |
| 9106 | return freelist_size; |
| 9107 | } |
| 9108 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9109 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9110 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9111 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9112 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9113 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 9114 | Py_XDECREF(unicode_empty); |
| 9115 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9116 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9117 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 9118 | if (unicode_latin1[i]) { |
| 9119 | Py_DECREF(unicode_latin1[i]); |
| 9120 | unicode_latin1[i] = NULL; |
| 9121 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9122 | } |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 9123 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9124 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9125 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 9126 | #ifdef __cplusplus |
| 9127 | } |
| 9128 | #endif |
| 9129 | |
| 9130 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9131 | /* |
Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 9132 | Local variables: |
| 9133 | c-basic-offset: 4 |
| 9134 | indent-tabs-mode: nil |
| 9135 | End: |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9136 | */ |