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 | 339f8c6 | 2009-01-31 22:25:08 +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 | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 117 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 4d8a6f4 | 2008-10-02 19:49:47 +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 | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 123 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 124 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 4d8a6f4 | 2008-10-02 19:49:47 +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 | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 129 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 4d8a6f4 | 2008-10-02 19:49:47 +0000 | [diff] [blame] | 130 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +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 | 186d9b3 | 2009-01-31 16:34:44 +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 | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 148 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 4d8a6f4 | 2008-10-02 19:49:47 +0000 | [diff] [blame] | 149 | /* 0x000A, * LINE FEED */ |
| 150 | /* 0x000D, * CARRIAGE RETURN */ |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 151 | 0, 0, 1, 0, 0, 1, 0, 0, |
| 152 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 4d8a6f4 | 2008-10-02 19:49:47 +0000 | [diff] [blame] | 153 | /* 0x001C, * FILE SEPARATOR */ |
| 154 | /* 0x001D, * GROUP SEPARATOR */ |
| 155 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +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 | 186d9b3 | 2009-01-31 16:34:44 +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 | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 177 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 178 | #else |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +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 | 339f8c6 | 2009-01-31 22:25:08 +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 | 339f8c6 | 2009-01-31 22:25:08 +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 | 339f8c6 | 2009-01-31 22:25:08 +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 | 339f8c6 | 2009-01-31 22:25:08 +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 | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 247 | if (unicode == unicode_empty || |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +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 | 339f8c6 | 2009-01-31 22:25:08 +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 | 339f8c6 | 2009-01-31 22:25:08 +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 | 339f8c6 | 2009-01-31 22:25:08 +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) { |
Georg Brandl | 6290bcf | 2010-08-01 21:48:47 +0000 | [diff] [blame] | 275 | Py_CLEAR(unicode->defenc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 276 | } |
| 277 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 278 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | /* We allocate one more byte to make sure the string is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 283 | Ux0000 terminated -- XXX is this needed ? |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 284 | |
| 285 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 286 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 287 | |
| 288 | */ |
| 289 | |
| 290 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 291 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 292 | { |
| 293 | register PyUnicodeObject *unicode; |
| 294 | |
Andrew Dalke | e0df762 | 2006-05-27 11:04:36 +0000 | [diff] [blame] | 295 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 296 | if (length == 0 && unicode_empty != NULL) { |
| 297 | Py_INCREF(unicode_empty); |
| 298 | return unicode_empty; |
| 299 | } |
| 300 | |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 301 | /* Ensure we won't overflow the size. */ |
| 302 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 303 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 304 | } |
| 305 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 306 | /* Unicode freelist & memory allocation */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 307 | if (free_list) { |
| 308 | unicode = free_list; |
| 309 | free_list = *(PyUnicodeObject **)unicode; |
| 310 | numfree--; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 311 | if (unicode->str) { |
| 312 | /* Keep-Alive optimization: we only upsize the buffer, |
| 313 | never downsize it. */ |
| 314 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 315 | unicode_resize(unicode, length) < 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 316 | PyObject_DEL(unicode->str); |
| 317 | unicode->str = NULL; |
| 318 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 319 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 320 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 321 | size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 322 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 323 | } |
| 324 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 325 | } |
| 326 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 327 | size_t new_size; |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 328 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 329 | if (unicode == NULL) |
| 330 | return NULL; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 331 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 332 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 335 | if (!unicode->str) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 336 | PyErr_NoMemory(); |
| 337 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 338 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 339 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 340 | * the caller fails before initializing str -- unicode_resize() |
| 341 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 342 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 343 | * We don't want unicode_resize to read uninitialized memory in |
| 344 | * that case. |
| 345 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 346 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 347 | unicode->str[length] = 0; |
Martin v. Löwis | f15da69 | 2006-04-13 07:24:50 +0000 | [diff] [blame] | 348 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 349 | unicode->hash = -1; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 350 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 351 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 352 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 353 | onError: |
Amaury Forgeot d'Arc | 06847b1 | 2008-07-31 23:39:05 +0000 | [diff] [blame] | 354 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 355 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 356 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 357 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 358 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 362 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 363 | { |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 364 | if (PyUnicode_CheckExact(unicode) && |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 365 | numfree < PyUnicode_MAXFREELIST) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 366 | /* Keep-Alive optimization */ |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 367 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
| 368 | PyObject_DEL(unicode->str); |
| 369 | unicode->str = NULL; |
| 370 | unicode->length = 0; |
| 371 | } |
| 372 | if (unicode->defenc) { |
Georg Brandl | 6290bcf | 2010-08-01 21:48:47 +0000 | [diff] [blame] | 373 | Py_CLEAR(unicode->defenc); |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 374 | } |
| 375 | /* Add to free list */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 376 | *(PyUnicodeObject **)unicode = free_list; |
| 377 | free_list = unicode; |
| 378 | numfree++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 379 | } |
| 380 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 381 | PyObject_DEL(unicode->str); |
| 382 | Py_XDECREF(unicode->defenc); |
| 383 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
Benjamin Peterson | 828a706 | 2008-12-27 17:05:29 +0000 | [diff] [blame] | 387 | static |
| 388 | int _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 389 | { |
| 390 | register PyUnicodeObject *v; |
| 391 | |
| 392 | /* Argument checks */ |
| 393 | if (unicode == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 394 | PyErr_BadInternalCall(); |
| 395 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 396 | } |
Benjamin Peterson | 828a706 | 2008-12-27 17:05:29 +0000 | [diff] [blame] | 397 | v = *unicode; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 398 | if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 399 | PyErr_BadInternalCall(); |
| 400 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | /* Resizing unicode_empty and single character objects is not |
| 404 | possible since these are being shared. We simply return a fresh |
| 405 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 406 | if (v->length != length && |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 407 | (v == unicode_empty || v->length == 1)) { |
| 408 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 409 | if (w == NULL) |
| 410 | return -1; |
| 411 | Py_UNICODE_COPY(w->str, v->str, |
| 412 | length < v->length ? length : v->length); |
| 413 | Py_DECREF(*unicode); |
| 414 | *unicode = w; |
| 415 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 419 | objects, since we can modify them in-place. */ |
| 420 | return unicode_resize(v, length); |
| 421 | } |
| 422 | |
Benjamin Peterson | 828a706 | 2008-12-27 17:05:29 +0000 | [diff] [blame] | 423 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
| 424 | { |
| 425 | return _PyUnicode_Resize((PyUnicodeObject **)unicode, length); |
| 426 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 427 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 428 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 429 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 430 | { |
| 431 | PyUnicodeObject *unicode; |
| 432 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 433 | /* If the Unicode data is known at construction time, we can apply |
| 434 | some optimizations which share commonly used objects. */ |
| 435 | if (u != NULL) { |
| 436 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 437 | /* Optimization for empty strings */ |
| 438 | if (size == 0 && unicode_empty != NULL) { |
| 439 | Py_INCREF(unicode_empty); |
| 440 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 441 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 442 | |
| 443 | /* Single character Unicode objects in the Latin-1 range are |
| 444 | shared when using this constructor */ |
| 445 | if (size == 1 && *u < 256) { |
| 446 | unicode = unicode_latin1[*u]; |
| 447 | if (!unicode) { |
| 448 | unicode = _PyUnicode_New(1); |
| 449 | if (!unicode) |
| 450 | return NULL; |
| 451 | unicode->str[0] = *u; |
| 452 | unicode_latin1[*u] = unicode; |
| 453 | } |
| 454 | Py_INCREF(unicode); |
| 455 | return (PyObject *)unicode; |
| 456 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 457 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 458 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 459 | unicode = _PyUnicode_New(size); |
| 460 | if (!unicode) |
| 461 | return NULL; |
| 462 | |
| 463 | /* Copy the Unicode data into the new object */ |
| 464 | if (u != NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 465 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 466 | |
| 467 | return (PyObject *)unicode; |
| 468 | } |
| 469 | |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 470 | PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
| 471 | { |
| 472 | PyUnicodeObject *unicode; |
Gregory P. Smith | c00eb73 | 2008-04-09 23:16:37 +0000 | [diff] [blame] | 473 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 474 | if (size < 0) { |
| 475 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 476 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 477 | return NULL; |
| 478 | } |
Gregory P. Smith | c00eb73 | 2008-04-09 23:16:37 +0000 | [diff] [blame] | 479 | |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 480 | /* If the Unicode data is known at construction time, we can apply |
| 481 | some optimizations which share commonly used objects. |
| 482 | Also, this means the input must be UTF-8, so fall back to the |
| 483 | UTF-8 decoder at the end. */ |
| 484 | if (u != NULL) { |
| 485 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 486 | /* Optimization for empty strings */ |
| 487 | if (size == 0 && unicode_empty != NULL) { |
| 488 | Py_INCREF(unicode_empty); |
| 489 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 490 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 491 | |
| 492 | /* Single characters are shared when using this constructor. |
| 493 | Restrict to ASCII, since the input must be UTF-8. */ |
| 494 | if (size == 1 && Py_CHARMASK(*u) < 128) { |
| 495 | unicode = unicode_latin1[Py_CHARMASK(*u)]; |
| 496 | if (!unicode) { |
| 497 | unicode = _PyUnicode_New(1); |
| 498 | if (!unicode) |
| 499 | return NULL; |
| 500 | unicode->str[0] = Py_CHARMASK(*u); |
| 501 | unicode_latin1[Py_CHARMASK(*u)] = unicode; |
| 502 | } |
| 503 | Py_INCREF(unicode); |
| 504 | return (PyObject *)unicode; |
| 505 | } |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 506 | |
| 507 | return PyUnicode_DecodeUTF8(u, size, NULL); |
| 508 | } |
| 509 | |
| 510 | unicode = _PyUnicode_New(size); |
| 511 | if (!unicode) |
| 512 | return NULL; |
| 513 | |
| 514 | return (PyObject *)unicode; |
| 515 | } |
| 516 | |
| 517 | PyObject *PyUnicode_FromString(const char *u) |
| 518 | { |
| 519 | size_t size = strlen(u); |
| 520 | if (size > PY_SSIZE_T_MAX) { |
| 521 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 522 | return NULL; |
| 523 | } |
| 524 | |
| 525 | return PyUnicode_FromStringAndSize(u, size); |
| 526 | } |
| 527 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 528 | #ifdef HAVE_WCHAR_H |
| 529 | |
| 530 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 531 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 532 | { |
| 533 | PyUnicodeObject *unicode; |
| 534 | |
| 535 | if (w == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 536 | PyErr_BadInternalCall(); |
| 537 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | unicode = _PyUnicode_New(size); |
| 541 | if (!unicode) |
| 542 | return NULL; |
| 543 | |
| 544 | /* Copy the wchar_t data into the new object */ |
| 545 | #ifdef HAVE_USABLE_WCHAR_T |
| 546 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 547 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 548 | { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 549 | register Py_UNICODE *u; |
| 550 | register Py_ssize_t i; |
| 551 | u = PyUnicode_AS_UNICODE(unicode); |
| 552 | for (i = size; i > 0; i--) |
| 553 | *u++ = *w++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 554 | } |
| 555 | #endif |
| 556 | |
| 557 | return (PyObject *)unicode; |
| 558 | } |
| 559 | |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 560 | static void |
| 561 | makefmt(char *fmt, int longflag, int size_tflag, int zeropad, int width, int precision, char c) |
| 562 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 563 | *fmt++ = '%'; |
| 564 | if (width) { |
| 565 | if (zeropad) |
| 566 | *fmt++ = '0'; |
| 567 | fmt += sprintf(fmt, "%d", width); |
| 568 | } |
| 569 | if (precision) |
| 570 | fmt += sprintf(fmt, ".%d", precision); |
| 571 | if (longflag) |
| 572 | *fmt++ = 'l'; |
| 573 | else if (size_tflag) { |
| 574 | char *f = PY_FORMAT_SIZE_T; |
| 575 | while (*f) |
| 576 | *fmt++ = *f++; |
| 577 | } |
| 578 | *fmt++ = c; |
| 579 | *fmt = '\0'; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} |
| 583 | |
| 584 | PyObject * |
| 585 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 586 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 587 | va_list count; |
| 588 | Py_ssize_t callcount = 0; |
| 589 | PyObject **callresults = NULL; |
| 590 | PyObject **callresult = NULL; |
| 591 | Py_ssize_t n = 0; |
| 592 | int width = 0; |
| 593 | int precision = 0; |
| 594 | int zeropad; |
| 595 | const char* f; |
| 596 | Py_UNICODE *s; |
| 597 | PyObject *string; |
| 598 | /* used by sprintf */ |
| 599 | char buffer[21]; |
| 600 | /* use abuffer instead of buffer, if we need more space |
| 601 | * (which can happen if there's a format specifier with width). */ |
| 602 | char *abuffer = NULL; |
| 603 | char *realbuffer; |
| 604 | Py_ssize_t abuffersize = 0; |
| 605 | char fmt[60]; /* should be enough for %0width.precisionld */ |
| 606 | const char *copy; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 607 | |
| 608 | #ifdef VA_LIST_IS_ARRAY |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 609 | Py_MEMCPY(count, vargs, sizeof(va_list)); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 610 | #else |
| 611 | #ifdef __va_copy |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 612 | __va_copy(count, vargs); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 613 | #else |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 614 | count = vargs; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 615 | #endif |
| 616 | #endif |
Walter Dörwald | f11232e | 2009-05-03 22:38:54 +0000 | [diff] [blame] | 617 | /* step 1: count the number of %S/%R/%s format specifications |
| 618 | * (we call PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() for these |
| 619 | * objects once during step 3 and put the result in an array) */ |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 620 | for (f = format; *f; f++) { |
Walter Dörwald | f11232e | 2009-05-03 22:38:54 +0000 | [diff] [blame] | 621 | if (*f == '%') { |
| 622 | if (*(f+1)=='%') |
| 623 | continue; |
Walter Dörwald | 6703225 | 2009-05-03 22:46:50 +0000 | [diff] [blame] | 624 | if (*(f+1)=='S' || *(f+1)=='R') |
Walter Dörwald | f11232e | 2009-05-03 22:38:54 +0000 | [diff] [blame] | 625 | ++callcount; |
| 626 | while (isdigit((unsigned)*f)) |
| 627 | width = (width*10) + *f++ - '0'; |
| 628 | while (*++f && *f != '%' && !isalpha((unsigned)*f)) |
| 629 | ; |
| 630 | if (*f == 's') |
| 631 | ++callcount; |
| 632 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 633 | } |
| 634 | /* step 2: allocate memory for the results of |
Walter Dörwald | f11232e | 2009-05-03 22:38:54 +0000 | [diff] [blame] | 635 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 636 | if (callcount) { |
| 637 | callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); |
| 638 | if (!callresults) { |
| 639 | PyErr_NoMemory(); |
| 640 | return NULL; |
| 641 | } |
| 642 | callresult = callresults; |
| 643 | } |
| 644 | /* step 3: figure out how large a buffer we need */ |
| 645 | for (f = format; *f; f++) { |
| 646 | if (*f == '%') { |
| 647 | const char* p = f; |
| 648 | width = 0; |
| 649 | while (isdigit((unsigned)*f)) |
| 650 | width = (width*10) + *f++ - '0'; |
| 651 | while (*++f && *f != '%' && !isalpha((unsigned)*f)) |
| 652 | ; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 653 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 654 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 655 | * they don't affect the amount of space we reserve. |
| 656 | */ |
| 657 | if ((*f == 'l' || *f == 'z') && |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 658 | (f[1] == 'd' || f[1] == 'u')) |
| 659 | ++f; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 660 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 661 | switch (*f) { |
| 662 | case 'c': |
| 663 | (void)va_arg(count, int); |
| 664 | /* fall through... */ |
| 665 | case '%': |
| 666 | n++; |
| 667 | break; |
| 668 | case 'd': case 'u': case 'i': case 'x': |
| 669 | (void) va_arg(count, int); |
| 670 | /* 20 bytes is enough to hold a 64-bit |
| 671 | integer. Decimal takes the most space. |
| 672 | This isn't enough for octal. |
| 673 | If a width is specified we need more |
| 674 | (which we allocate later). */ |
| 675 | if (width < 20) |
| 676 | width = 20; |
| 677 | n += width; |
| 678 | if (abuffersize < width) |
| 679 | abuffersize = width; |
| 680 | break; |
| 681 | case 's': |
| 682 | { |
| 683 | /* UTF-8 */ |
Walter Dörwald | f11232e | 2009-05-03 22:38:54 +0000 | [diff] [blame] | 684 | unsigned char *s = va_arg(count, unsigned char*); |
| 685 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 686 | if (!str) |
| 687 | goto fail; |
| 688 | n += PyUnicode_GET_SIZE(str); |
| 689 | /* Remember the str and switch to the next slot */ |
| 690 | *callresult++ = str; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 691 | break; |
| 692 | } |
| 693 | case 'U': |
| 694 | { |
| 695 | PyObject *obj = va_arg(count, PyObject *); |
| 696 | assert(obj && PyUnicode_Check(obj)); |
| 697 | n += PyUnicode_GET_SIZE(obj); |
| 698 | break; |
| 699 | } |
| 700 | case 'V': |
| 701 | { |
| 702 | PyObject *obj = va_arg(count, PyObject *); |
| 703 | const char *str = va_arg(count, const char *); |
| 704 | assert(obj || str); |
| 705 | assert(!obj || PyUnicode_Check(obj)); |
| 706 | if (obj) |
| 707 | n += PyUnicode_GET_SIZE(obj); |
| 708 | else |
| 709 | n += strlen(str); |
| 710 | break; |
| 711 | } |
| 712 | case 'S': |
| 713 | { |
| 714 | PyObject *obj = va_arg(count, PyObject *); |
| 715 | PyObject *str; |
| 716 | assert(obj); |
| 717 | str = PyObject_Str(obj); |
| 718 | if (!str) |
| 719 | goto fail; |
| 720 | n += PyUnicode_GET_SIZE(str); |
| 721 | /* Remember the str and switch to the next slot */ |
| 722 | *callresult++ = str; |
| 723 | break; |
| 724 | } |
| 725 | case 'R': |
| 726 | { |
| 727 | PyObject *obj = va_arg(count, PyObject *); |
| 728 | PyObject *repr; |
| 729 | assert(obj); |
| 730 | repr = PyObject_Repr(obj); |
| 731 | if (!repr) |
| 732 | goto fail; |
| 733 | n += PyUnicode_GET_SIZE(repr); |
| 734 | /* Remember the repr and switch to the next slot */ |
| 735 | *callresult++ = repr; |
| 736 | break; |
| 737 | } |
| 738 | case 'p': |
| 739 | (void) va_arg(count, int); |
| 740 | /* maximum 64-bit pointer representation: |
| 741 | * 0xffffffffffffffff |
| 742 | * so 19 characters is enough. |
| 743 | * XXX I count 18 -- what's the extra for? |
| 744 | */ |
| 745 | n += 19; |
| 746 | break; |
| 747 | default: |
| 748 | /* if we stumble upon an unknown |
| 749 | formatting code, copy the rest of |
| 750 | the format string to the output |
| 751 | string. (we cannot just skip the |
| 752 | code, since there's no way to know |
| 753 | what's in the argument list) */ |
| 754 | n += strlen(p); |
| 755 | goto expand; |
| 756 | } |
| 757 | } else |
| 758 | n++; |
| 759 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 760 | expand: |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 761 | if (abuffersize > 20) { |
| 762 | abuffer = PyObject_Malloc(abuffersize); |
| 763 | if (!abuffer) { |
| 764 | PyErr_NoMemory(); |
| 765 | goto fail; |
| 766 | } |
| 767 | realbuffer = abuffer; |
| 768 | } |
| 769 | else |
| 770 | realbuffer = buffer; |
| 771 | /* step 4: fill the buffer */ |
| 772 | /* Since we've analyzed how much space we need for the worst case, |
| 773 | we don't have to resize the string. |
| 774 | There can be no errors beyond this point. */ |
| 775 | string = PyUnicode_FromUnicode(NULL, n); |
| 776 | if (!string) |
| 777 | goto fail; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 778 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 779 | s = PyUnicode_AS_UNICODE(string); |
| 780 | callresult = callresults; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 781 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 782 | for (f = format; *f; f++) { |
| 783 | if (*f == '%') { |
| 784 | const char* p = f++; |
| 785 | int longflag = 0; |
| 786 | int size_tflag = 0; |
| 787 | zeropad = (*f == '0'); |
| 788 | /* parse the width.precision part */ |
| 789 | width = 0; |
| 790 | while (isdigit((unsigned)*f)) |
| 791 | width = (width*10) + *f++ - '0'; |
| 792 | precision = 0; |
| 793 | if (*f == '.') { |
| 794 | f++; |
| 795 | while (isdigit((unsigned)*f)) |
| 796 | precision = (precision*10) + *f++ - '0'; |
| 797 | } |
| 798 | /* handle the long flag, but only for %ld and %lu. |
| 799 | others can be added when necessary. */ |
| 800 | if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
| 801 | longflag = 1; |
| 802 | ++f; |
| 803 | } |
| 804 | /* handle the size_t flag. */ |
| 805 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 806 | size_tflag = 1; |
| 807 | ++f; |
| 808 | } |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 809 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 810 | switch (*f) { |
| 811 | case 'c': |
| 812 | *s++ = va_arg(vargs, int); |
| 813 | break; |
| 814 | case 'd': |
| 815 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'd'); |
| 816 | if (longflag) |
| 817 | sprintf(realbuffer, fmt, va_arg(vargs, long)); |
| 818 | else if (size_tflag) |
| 819 | sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
| 820 | else |
| 821 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 822 | appendstring(realbuffer); |
| 823 | break; |
| 824 | case 'u': |
| 825 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'u'); |
| 826 | if (longflag) |
| 827 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
| 828 | else if (size_tflag) |
| 829 | sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
| 830 | else |
| 831 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
| 832 | appendstring(realbuffer); |
| 833 | break; |
| 834 | case 'i': |
| 835 | makefmt(fmt, 0, 0, zeropad, width, precision, 'i'); |
| 836 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 837 | appendstring(realbuffer); |
| 838 | break; |
| 839 | case 'x': |
| 840 | makefmt(fmt, 0, 0, zeropad, width, precision, 'x'); |
| 841 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 842 | appendstring(realbuffer); |
| 843 | break; |
| 844 | case 's': |
| 845 | { |
Walter Dörwald | f11232e | 2009-05-03 22:38:54 +0000 | [diff] [blame] | 846 | /* unused, since we already have the result */ |
| 847 | (void) va_arg(vargs, char *); |
| 848 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(*callresult), |
| 849 | PyUnicode_GET_SIZE(*callresult)); |
| 850 | s += PyUnicode_GET_SIZE(*callresult); |
| 851 | /* We're done with the unicode()/repr() => forget it */ |
| 852 | Py_DECREF(*callresult); |
| 853 | /* switch to next unicode()/repr() result */ |
| 854 | ++callresult; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 855 | break; |
| 856 | } |
| 857 | case 'U': |
| 858 | { |
| 859 | PyObject *obj = va_arg(vargs, PyObject *); |
| 860 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 861 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 862 | s += size; |
| 863 | break; |
| 864 | } |
| 865 | case 'V': |
| 866 | { |
| 867 | PyObject *obj = va_arg(vargs, PyObject *); |
| 868 | const char *str = va_arg(vargs, const char *); |
| 869 | if (obj) { |
| 870 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 871 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 872 | s += size; |
| 873 | } else { |
| 874 | appendstring(str); |
| 875 | } |
| 876 | break; |
| 877 | } |
| 878 | case 'S': |
| 879 | case 'R': |
| 880 | { |
| 881 | Py_UNICODE *ucopy; |
| 882 | Py_ssize_t usize; |
| 883 | Py_ssize_t upos; |
| 884 | /* unused, since we already have the result */ |
| 885 | (void) va_arg(vargs, PyObject *); |
| 886 | ucopy = PyUnicode_AS_UNICODE(*callresult); |
| 887 | usize = PyUnicode_GET_SIZE(*callresult); |
| 888 | for (upos = 0; upos<usize;) |
| 889 | *s++ = ucopy[upos++]; |
| 890 | /* We're done with the unicode()/repr() => forget it */ |
| 891 | Py_DECREF(*callresult); |
| 892 | /* switch to next unicode()/repr() result */ |
| 893 | ++callresult; |
| 894 | break; |
| 895 | } |
| 896 | case 'p': |
| 897 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 898 | /* %p is ill-defined: ensure leading 0x. */ |
| 899 | if (buffer[1] == 'X') |
| 900 | buffer[1] = 'x'; |
| 901 | else if (buffer[1] != 'x') { |
| 902 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 903 | buffer[0] = '0'; |
| 904 | buffer[1] = 'x'; |
| 905 | } |
| 906 | appendstring(buffer); |
| 907 | break; |
| 908 | case '%': |
| 909 | *s++ = '%'; |
| 910 | break; |
| 911 | default: |
| 912 | appendstring(p); |
| 913 | goto end; |
| 914 | } |
| 915 | } else |
| 916 | *s++ = *f; |
| 917 | } |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 918 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 919 | end: |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 920 | if (callresults) |
| 921 | PyObject_Free(callresults); |
| 922 | if (abuffer) |
| 923 | PyObject_Free(abuffer); |
| 924 | PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
| 925 | return string; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 926 | fail: |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 927 | if (callresults) { |
| 928 | PyObject **callresult2 = callresults; |
| 929 | while (callresult2 < callresult) { |
| 930 | Py_DECREF(*callresult2); |
| 931 | ++callresult2; |
| 932 | } |
| 933 | PyObject_Free(callresults); |
| 934 | } |
| 935 | if (abuffer) |
| 936 | PyObject_Free(abuffer); |
| 937 | return NULL; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | #undef appendstring |
| 941 | |
| 942 | PyObject * |
| 943 | PyUnicode_FromFormat(const char *format, ...) |
| 944 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 945 | PyObject* ret; |
| 946 | va_list vargs; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 947 | |
| 948 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 949 | va_start(vargs, format); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 950 | #else |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 951 | va_start(vargs); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 952 | #endif |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 953 | ret = PyUnicode_FromFormatV(format, vargs); |
| 954 | va_end(vargs); |
| 955 | return ret; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 956 | } |
| 957 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 958 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 959 | wchar_t *w, |
| 960 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 961 | { |
| 962 | if (unicode == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 963 | PyErr_BadInternalCall(); |
| 964 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 965 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 966 | |
| 967 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 968 | if (size > PyUnicode_GET_SIZE(unicode)) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 969 | size = PyUnicode_GET_SIZE(unicode) + 1; |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 970 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 971 | #ifdef HAVE_USABLE_WCHAR_T |
| 972 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 973 | #else |
| 974 | { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 975 | register Py_UNICODE *u; |
| 976 | register Py_ssize_t i; |
| 977 | u = PyUnicode_AS_UNICODE(unicode); |
| 978 | for (i = size; i > 0; i--) |
| 979 | *w++ = *u++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 980 | } |
| 981 | #endif |
| 982 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 983 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 984 | return PyUnicode_GET_SIZE(unicode); |
| 985 | else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 986 | return size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | #endif |
| 990 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 991 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 992 | { |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 993 | Py_UNICODE s[1]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 994 | |
| 995 | #ifdef Py_UNICODE_WIDE |
| 996 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 997 | PyErr_SetString(PyExc_ValueError, |
| 998 | "unichr() arg not in range(0x110000) " |
| 999 | "(wide Python build)"); |
| 1000 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1001 | } |
| 1002 | #else |
| 1003 | if (ordinal < 0 || ordinal > 0xffff) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1004 | PyErr_SetString(PyExc_ValueError, |
| 1005 | "unichr() arg not in range(0x10000) " |
| 1006 | "(narrow Python build)"); |
| 1007 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1008 | } |
| 1009 | #endif |
| 1010 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1011 | s[0] = (Py_UNICODE)ordinal; |
| 1012 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1015 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 1016 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1017 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1018 | PyObject_Unicode() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1019 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1020 | Py_INCREF(obj); |
| 1021 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1022 | } |
| 1023 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1024 | /* For a Unicode subtype that's not a Unicode object, |
| 1025 | return a true Unicode object with the same data. */ |
| 1026 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 1027 | PyUnicode_GET_SIZE(obj)); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1028 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1029 | return PyUnicode_FromEncodedObject(obj, NULL, "strict"); |
| 1030 | } |
| 1031 | |
| 1032 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1033 | const char *encoding, |
| 1034 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1035 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1036 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1037 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1038 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1039 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1040 | if (obj == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1041 | PyErr_BadInternalCall(); |
| 1042 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1043 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1044 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1045 | #if 0 |
| 1046 | /* For b/w compatibility we also accept Unicode objects provided |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 1047 | that no encodings is given and then redirect to |
| 1048 | PyObject_Unicode() which then applies the additional logic for |
| 1049 | Unicode subclasses. |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1050 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1051 | NOTE: This API should really only be used for object which |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1052 | represent *encoded* Unicode ! |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1053 | |
| 1054 | */ |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 1055 | if (PyUnicode_Check(obj)) { |
| 1056 | if (encoding) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1057 | PyErr_SetString(PyExc_TypeError, |
| 1058 | "decoding Unicode is not supported"); |
| 1059 | return NULL; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 1060 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1061 | return PyObject_Unicode(obj); |
| 1062 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1063 | #else |
| 1064 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1065 | PyErr_SetString(PyExc_TypeError, |
| 1066 | "decoding Unicode is not supported"); |
| 1067 | return NULL; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 1068 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1069 | #endif |
| 1070 | |
| 1071 | /* Coerce object */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1072 | if (PyString_Check(obj)) { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 1073 | s = PyString_AS_STRING(obj); |
| 1074 | len = PyString_GET_SIZE(obj); |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 1075 | } |
Christian Heimes | 3497f94 | 2008-05-26 12:29:14 +0000 | [diff] [blame] | 1076 | else if (PyByteArray_Check(obj)) { |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 1077 | /* Python 2.x specific */ |
| 1078 | PyErr_Format(PyExc_TypeError, |
| 1079 | "decoding bytearray is not supported"); |
| 1080 | return NULL; |
| 1081 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1082 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1083 | /* Overwrite the error message with something more useful in |
| 1084 | case of a TypeError. */ |
| 1085 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1086 | PyErr_Format(PyExc_TypeError, |
| 1087 | "coercing to Unicode: need string or buffer, " |
| 1088 | "%.80s found", |
| 1089 | Py_TYPE(obj)->tp_name); |
| 1090 | goto onError; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1091 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1092 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1093 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1094 | if (len == 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1095 | Py_INCREF(unicode_empty); |
| 1096 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1097 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1098 | else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1099 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1100 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1101 | return v; |
| 1102 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1103 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1104 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | PyObject *PyUnicode_Decode(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1108 | Py_ssize_t size, |
| 1109 | const char *encoding, |
| 1110 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1111 | { |
| 1112 | PyObject *buffer = NULL, *unicode; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1113 | |
| 1114 | if (encoding == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1115 | encoding = PyUnicode_GetDefaultEncoding(); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1116 | |
| 1117 | /* Shortcuts for common default encodings */ |
| 1118 | if (strcmp(encoding, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1119 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1120 | else if (strcmp(encoding, "latin-1") == 0) |
| 1121 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1122 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1123 | else if (strcmp(encoding, "mbcs") == 0) |
| 1124 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 1125 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1126 | else if (strcmp(encoding, "ascii") == 0) |
| 1127 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1128 | |
| 1129 | /* Decode via the codec registry */ |
| 1130 | buffer = PyBuffer_FromMemory((void *)s, size); |
| 1131 | if (buffer == NULL) |
| 1132 | goto onError; |
| 1133 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1134 | if (unicode == NULL) |
| 1135 | goto onError; |
| 1136 | if (!PyUnicode_Check(unicode)) { |
| 1137 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 1138 | "decoder did not return an unicode object (type=%.400s)", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1139 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1140 | Py_DECREF(unicode); |
| 1141 | goto onError; |
| 1142 | } |
| 1143 | Py_DECREF(buffer); |
| 1144 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1145 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1146 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1147 | Py_XDECREF(buffer); |
| 1148 | return NULL; |
| 1149 | } |
| 1150 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1151 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1152 | const char *encoding, |
| 1153 | const char *errors) |
| 1154 | { |
| 1155 | PyObject *v; |
| 1156 | |
| 1157 | if (!PyUnicode_Check(unicode)) { |
| 1158 | PyErr_BadArgument(); |
| 1159 | goto onError; |
| 1160 | } |
| 1161 | |
| 1162 | if (encoding == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1163 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1164 | |
| 1165 | /* Decode via the codec registry */ |
| 1166 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1167 | if (v == NULL) |
| 1168 | goto onError; |
| 1169 | return v; |
| 1170 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1171 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1172 | return NULL; |
| 1173 | } |
| 1174 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1175 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1176 | Py_ssize_t size, |
| 1177 | const char *encoding, |
| 1178 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1179 | { |
| 1180 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1181 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1182 | unicode = PyUnicode_FromUnicode(s, size); |
| 1183 | if (unicode == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1184 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1185 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1186 | Py_DECREF(unicode); |
| 1187 | return v; |
| 1188 | } |
| 1189 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1190 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1191 | const char *encoding, |
| 1192 | const char *errors) |
| 1193 | { |
| 1194 | PyObject *v; |
| 1195 | |
| 1196 | if (!PyUnicode_Check(unicode)) { |
| 1197 | PyErr_BadArgument(); |
| 1198 | goto onError; |
| 1199 | } |
| 1200 | |
| 1201 | if (encoding == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1202 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1203 | |
| 1204 | /* Encode via the codec registry */ |
| 1205 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1206 | if (v == NULL) |
| 1207 | goto onError; |
| 1208 | return v; |
| 1209 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1210 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1211 | return NULL; |
| 1212 | } |
| 1213 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1214 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1215 | const char *encoding, |
| 1216 | const char *errors) |
| 1217 | { |
| 1218 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1219 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1220 | if (!PyUnicode_Check(unicode)) { |
| 1221 | PyErr_BadArgument(); |
| 1222 | goto onError; |
| 1223 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1224 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1225 | if (encoding == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1226 | encoding = PyUnicode_GetDefaultEncoding(); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1227 | |
| 1228 | /* Shortcuts for common default encodings */ |
| 1229 | if (errors == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1230 | if (strcmp(encoding, "utf-8") == 0) |
| 1231 | return PyUnicode_AsUTF8String(unicode); |
| 1232 | else if (strcmp(encoding, "latin-1") == 0) |
| 1233 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1234 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1235 | else if (strcmp(encoding, "mbcs") == 0) |
| 1236 | return PyUnicode_AsMBCSString(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1237 | #endif |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1238 | else if (strcmp(encoding, "ascii") == 0) |
| 1239 | return PyUnicode_AsASCIIString(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1240 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1241 | |
| 1242 | /* Encode via the codec registry */ |
| 1243 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1244 | if (v == NULL) |
| 1245 | goto onError; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1246 | if (!PyString_Check(v)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1247 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 1248 | "encoder did not return a string object (type=%.400s)", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1249 | Py_TYPE(v)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1250 | Py_DECREF(v); |
| 1251 | goto onError; |
| 1252 | } |
| 1253 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1254 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1255 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1256 | return NULL; |
| 1257 | } |
| 1258 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1259 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1260 | const char *errors) |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1261 | { |
| 1262 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
| 1263 | |
| 1264 | if (v) |
| 1265 | return v; |
| 1266 | v = PyUnicode_AsEncodedString(unicode, NULL, errors); |
| 1267 | if (v && errors == NULL) |
| 1268 | ((PyUnicodeObject *)unicode)->defenc = v; |
| 1269 | return v; |
| 1270 | } |
| 1271 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1272 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1273 | { |
| 1274 | if (!PyUnicode_Check(unicode)) { |
| 1275 | PyErr_BadArgument(); |
| 1276 | goto onError; |
| 1277 | } |
| 1278 | return PyUnicode_AS_UNICODE(unicode); |
| 1279 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1280 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1281 | return NULL; |
| 1282 | } |
| 1283 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1284 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1285 | { |
| 1286 | if (!PyUnicode_Check(unicode)) { |
| 1287 | PyErr_BadArgument(); |
| 1288 | goto onError; |
| 1289 | } |
| 1290 | return PyUnicode_GET_SIZE(unicode); |
| 1291 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1292 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1293 | return -1; |
| 1294 | } |
| 1295 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1296 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1297 | { |
| 1298 | return unicode_default_encoding; |
| 1299 | } |
| 1300 | |
| 1301 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 1302 | { |
| 1303 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1304 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1305 | /* Make sure the encoding is valid. As side effect, this also |
| 1306 | loads the encoding into the codec registry cache. */ |
| 1307 | v = _PyCodec_Lookup(encoding); |
| 1308 | if (v == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1309 | goto onError; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1310 | Py_DECREF(v); |
| 1311 | strncpy(unicode_default_encoding, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1312 | encoding, |
| 1313 | sizeof(unicode_default_encoding)); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1314 | return 0; |
| 1315 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1316 | onError: |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1317 | return -1; |
| 1318 | } |
| 1319 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1320 | /* error handling callback helper: |
| 1321 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1322 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1323 | and adjust various state variables. |
| 1324 | return 0 on success, -1 on error |
| 1325 | */ |
| 1326 | |
| 1327 | static |
| 1328 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1329 | const char *encoding, const char *reason, |
| 1330 | const char *input, Py_ssize_t insize, Py_ssize_t *startinpos, |
| 1331 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 1332 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1333 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1334 | 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] | 1335 | |
| 1336 | PyObject *restuple = NULL; |
| 1337 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1338 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
| 1339 | Py_ssize_t requiredsize; |
| 1340 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1341 | Py_UNICODE *repptr; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1342 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1343 | int res = -1; |
| 1344 | |
| 1345 | if (*errorHandler == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1346 | *errorHandler = PyCodec_LookupError(errors); |
| 1347 | if (*errorHandler == NULL) |
| 1348 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 1352 | *exceptionObject = PyUnicodeDecodeError_Create( |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1353 | encoding, input, insize, *startinpos, *endinpos, reason); |
| 1354 | if (*exceptionObject == NULL) |
| 1355 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1356 | } |
| 1357 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1358 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 1359 | goto onError; |
| 1360 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 1361 | goto onError; |
| 1362 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1363 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1367 | if (restuple == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1368 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1369 | if (!PyTuple_Check(restuple)) { |
Georg Brandl | 40e15ed | 2009-04-05 21:48:06 +0000 | [diff] [blame] | 1370 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1371 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1372 | } |
| 1373 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1374 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1375 | if (newpos<0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1376 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1377 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1378 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 1379 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1380 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1381 | |
| 1382 | /* need more space? (at least enough for what we |
| 1383 | have+the replacement+the rest of the string (starting |
| 1384 | at the new input position), so we won't have to check space |
| 1385 | when there are no errors in the rest of the string) */ |
| 1386 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 1387 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 1388 | requiredsize = *outpos + repsize + insize-newpos; |
| 1389 | if (requiredsize > outsize) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1390 | if (requiredsize<2*outsize) |
| 1391 | requiredsize = 2*outsize; |
| 1392 | if (_PyUnicode_Resize(output, requiredsize) < 0) |
| 1393 | goto onError; |
| 1394 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1395 | } |
| 1396 | *endinpos = newpos; |
| 1397 | *inptr = input + newpos; |
| 1398 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 1399 | *outptr += repsize; |
| 1400 | *outpos += repsize; |
| 1401 | /* we made it! */ |
| 1402 | res = 0; |
| 1403 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1404 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1405 | Py_XDECREF(restuple); |
| 1406 | return res; |
| 1407 | } |
| 1408 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1409 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 1410 | |
| 1411 | /* see RFC2152 for details */ |
| 1412 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1413 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1414 | char utf7_special[128] = { |
| 1415 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 1416 | encoded: |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 1417 | 0 - not special |
| 1418 | 1 - special |
| 1419 | 2 - whitespace (optional) |
| 1420 | 3 - RFC2152 Set O (optional) */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1421 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 1422 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1423 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 1424 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 1425 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1426 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 1427 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1428 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 1429 | |
| 1430 | }; |
| 1431 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1432 | /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
| 1433 | warnings about the comparison always being false; since |
| 1434 | utf7_special[0] is 1, we can safely make that one comparison |
| 1435 | true */ |
| 1436 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1437 | #define SPECIAL(c, encodeO, encodeWS) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1438 | ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1439 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1440 | (encodeO && (utf7_special[(c)] == 3))) |
| 1441 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1442 | #define B64(n) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1443 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1444 | #define B64CHAR(c) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1445 | (isalnum(c) || (c) == '+' || (c) == '/') |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1446 | #define UB64(c) \ |
| 1447 | ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1448 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1449 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1450 | #define ENCODE(out, ch, bits) \ |
| 1451 | while (bits >= 6) { \ |
| 1452 | *out++ = B64(ch >> (bits-6)); \ |
| 1453 | bits -= 6; \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1456 | #define DECODE(out, ch, bits, surrogate) \ |
| 1457 | while (bits >= 16) { \ |
| 1458 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 1459 | bits -= 16; \ |
| 1460 | if (surrogate) { \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1461 | /* We have already generated an error for the high surrogate \ |
| 1462 | 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] | 1463 | surrogate = 0; \ |
| 1464 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1465 | /* This is a surrogate pair. Unfortunately we can't represent \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1466 | it in a 16-bit character */ \ |
| 1467 | surrogate = 1; \ |
| 1468 | errmsg = "code pairs are not supported"; \ |
| 1469 | goto utf7Error; \ |
| 1470 | } else { \ |
| 1471 | *out++ = outCh; \ |
| 1472 | } \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1473 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1474 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1475 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1476 | Py_ssize_t size, |
| 1477 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1478 | { |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1479 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 1480 | } |
| 1481 | |
| 1482 | PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1483 | Py_ssize_t size, |
| 1484 | const char *errors, |
| 1485 | Py_ssize_t *consumed) |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1486 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1487 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1488 | Py_ssize_t startinpos; |
| 1489 | Py_ssize_t endinpos; |
| 1490 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1491 | const char *e; |
| 1492 | PyUnicodeObject *unicode; |
| 1493 | Py_UNICODE *p; |
| 1494 | const char *errmsg = ""; |
| 1495 | int inShift = 0; |
| 1496 | unsigned int bitsleft = 0; |
| 1497 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1498 | int surrogate = 0; |
| 1499 | PyObject *errorHandler = NULL; |
| 1500 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1501 | |
| 1502 | unicode = _PyUnicode_New(size); |
| 1503 | if (!unicode) |
| 1504 | return NULL; |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1505 | if (size == 0) { |
| 1506 | if (consumed) |
| 1507 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1508 | return (PyObject *)unicode; |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1509 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1510 | |
| 1511 | p = unicode->str; |
| 1512 | e = s + size; |
| 1513 | |
| 1514 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1515 | Py_UNICODE ch; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1516 | restart: |
Antoine Pitrou | 4982d5d | 2008-07-25 17:45:59 +0000 | [diff] [blame] | 1517 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1518 | |
| 1519 | if (inShift) { |
| 1520 | if ((ch == '-') || !B64CHAR(ch)) { |
| 1521 | inShift = 0; |
| 1522 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1523 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1524 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1525 | if (bitsleft >= 6) { |
| 1526 | /* The shift sequence has a partial character in it. If |
| 1527 | bitsleft < 6 then we could just classify it as padding |
| 1528 | but that is not the case here */ |
| 1529 | |
| 1530 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1531 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1532 | } |
| 1533 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1534 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1535 | here so indicate the potential of a misencoded character. */ |
| 1536 | |
| 1537 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 1538 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 1539 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1540 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | if (ch == '-') { |
| 1544 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1545 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1546 | inShift = 1; |
| 1547 | } |
| 1548 | } else if (SPECIAL(ch,0,0)) { |
| 1549 | errmsg = "unexpected special character"; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 1550 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1551 | } else { |
| 1552 | *p++ = ch; |
| 1553 | } |
| 1554 | } else { |
| 1555 | charsleft = (charsleft << 6) | UB64(ch); |
| 1556 | bitsleft += 6; |
| 1557 | s++; |
| 1558 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1559 | } |
| 1560 | } |
| 1561 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1562 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1563 | s++; |
| 1564 | if (s < e && *s == '-') { |
| 1565 | s++; |
| 1566 | *p++ = '+'; |
| 1567 | } else |
| 1568 | { |
| 1569 | inShift = 1; |
| 1570 | bitsleft = 0; |
| 1571 | } |
| 1572 | } |
| 1573 | else if (SPECIAL(ch,0,0)) { |
Walter Dörwald | 9d04542 | 2007-08-30 15:34:55 +0000 | [diff] [blame] | 1574 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1575 | errmsg = "unexpected special character"; |
| 1576 | s++; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 1577 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1578 | } |
| 1579 | else { |
| 1580 | *p++ = ch; |
| 1581 | s++; |
| 1582 | } |
| 1583 | continue; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1584 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1585 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1586 | endinpos = s-starts; |
| 1587 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1588 | errors, &errorHandler, |
| 1589 | "utf7", errmsg, |
| 1590 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1591 | &unicode, &outpos, &p)) |
| 1592 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1595 | if (inShift && !consumed) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1596 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1597 | endinpos = size; |
| 1598 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1599 | errors, &errorHandler, |
| 1600 | "utf7", "unterminated shift sequence", |
| 1601 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1602 | &unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1603 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1604 | if (s < e) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1605 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1606 | } |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1607 | if (consumed) { |
| 1608 | if(inShift) |
| 1609 | *consumed = startinpos; |
| 1610 | else |
| 1611 | *consumed = s-starts; |
| 1612 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1613 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1614 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1615 | goto onError; |
| 1616 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1617 | Py_XDECREF(errorHandler); |
| 1618 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1619 | return (PyObject *)unicode; |
| 1620 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1621 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1622 | Py_XDECREF(errorHandler); |
| 1623 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1624 | Py_DECREF(unicode); |
| 1625 | return NULL; |
| 1626 | } |
| 1627 | |
| 1628 | |
| 1629 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1630 | Py_ssize_t size, |
| 1631 | int encodeSetO, |
| 1632 | int encodeWhiteSpace, |
| 1633 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1634 | { |
| 1635 | PyObject *v; |
| 1636 | /* It might be possible to tighten this worst case */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1637 | Py_ssize_t cbAllocated = 5 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1638 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1639 | Py_ssize_t i = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1640 | unsigned int bitsleft = 0; |
| 1641 | unsigned long charsleft = 0; |
| 1642 | char * out; |
| 1643 | char * start; |
| 1644 | |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 1645 | if (cbAllocated / 5 != size) |
| 1646 | return PyErr_NoMemory(); |
| 1647 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1648 | if (size == 0) |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 1649 | return PyString_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1650 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1651 | v = PyString_FromStringAndSize(NULL, cbAllocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1652 | if (v == NULL) |
| 1653 | return NULL; |
| 1654 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1655 | start = out = PyString_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1656 | for (;i < size; ++i) { |
| 1657 | Py_UNICODE ch = s[i]; |
| 1658 | |
| 1659 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1660 | if (ch == '+') { |
| 1661 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1662 | *out++ = '-'; |
| 1663 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1664 | charsleft = ch; |
| 1665 | bitsleft = 16; |
| 1666 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1667 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1668 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1669 | } else { |
| 1670 | *out++ = (char) ch; |
| 1671 | } |
| 1672 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1673 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1674 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1675 | charsleft = 0; |
| 1676 | bitsleft = 0; |
| 1677 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1678 | so no '-' is required, except if the character is itself a '-' */ |
| 1679 | if (B64CHAR(ch) || ch == '-') { |
| 1680 | *out++ = '-'; |
| 1681 | } |
| 1682 | inShift = 0; |
| 1683 | *out++ = (char) ch; |
| 1684 | } else { |
| 1685 | bitsleft += 16; |
| 1686 | charsleft = (charsleft << 16) | ch; |
| 1687 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1688 | |
Jesus Cea | 585ad8a | 2009-07-02 15:37:21 +0000 | [diff] [blame] | 1689 | /* 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] | 1690 | 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] | 1691 | or '-' then the shift sequence will be terminated implicitly and we |
| 1692 | don't have to insert a '-'. */ |
| 1693 | |
| 1694 | if (bitsleft == 0) { |
| 1695 | if (i + 1 < size) { |
| 1696 | Py_UNICODE ch2 = s[i+1]; |
| 1697 | |
| 1698 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1699 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1700 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1701 | *out++ = '-'; |
| 1702 | inShift = 0; |
| 1703 | } else { |
| 1704 | inShift = 0; |
| 1705 | } |
| 1706 | |
| 1707 | } |
| 1708 | else { |
| 1709 | *out++ = '-'; |
| 1710 | inShift = 0; |
| 1711 | } |
| 1712 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1713 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1714 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1715 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1716 | if (bitsleft) { |
| 1717 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1718 | *out++ = '-'; |
| 1719 | } |
| 1720 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1721 | _PyString_Resize(&v, out - start); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1722 | return v; |
| 1723 | } |
| 1724 | |
| 1725 | #undef SPECIAL |
| 1726 | #undef B64 |
| 1727 | #undef B64CHAR |
| 1728 | #undef UB64 |
| 1729 | #undef ENCODE |
| 1730 | #undef DECODE |
| 1731 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1732 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1733 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1734 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1735 | char utf8_code_length[256] = { |
Ezio Melotti | 86e5e17 | 2010-07-03 05:34:39 +0000 | [diff] [blame] | 1736 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 1737 | illegal prefix. See RFC 3629 for details */ |
| 1738 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 1739 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1740 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1741 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1742 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1743 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1744 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
Ezio Melotti | 86e5e17 | 2010-07-03 05:34:39 +0000 | [diff] [blame] | 1745 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 1746 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80-8F */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1747 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1748 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
Ezio Melotti | 86e5e17 | 2010-07-03 05:34:39 +0000 | [diff] [blame] | 1749 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 1750 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 1751 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 1752 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 1753 | 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0-F4 + F5-FF */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1754 | }; |
| 1755 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1756 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1757 | Py_ssize_t size, |
| 1758 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1759 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1760 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 1761 | } |
| 1762 | |
| 1763 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1764 | Py_ssize_t size, |
| 1765 | const char *errors, |
| 1766 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1767 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1768 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1769 | int n; |
Ezio Melotti | 86e5e17 | 2010-07-03 05:34:39 +0000 | [diff] [blame] | 1770 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1771 | Py_ssize_t startinpos; |
| 1772 | Py_ssize_t endinpos; |
| 1773 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1774 | const char *e; |
| 1775 | PyUnicodeObject *unicode; |
| 1776 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1777 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1778 | PyObject *errorHandler = NULL; |
| 1779 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1780 | |
| 1781 | /* Note: size will always be longer than the resulting Unicode |
| 1782 | character count */ |
| 1783 | unicode = _PyUnicode_New(size); |
| 1784 | if (!unicode) |
| 1785 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1786 | if (size == 0) { |
| 1787 | if (consumed) |
| 1788 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1789 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1790 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1791 | |
| 1792 | /* Unpack UTF-8 encoded data */ |
| 1793 | p = unicode->str; |
| 1794 | e = s + size; |
| 1795 | |
| 1796 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1797 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1798 | |
| 1799 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1800 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1801 | s++; |
| 1802 | continue; |
| 1803 | } |
| 1804 | |
| 1805 | n = utf8_code_length[ch]; |
| 1806 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1807 | if (s + n > e) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1808 | if (consumed) |
| 1809 | break; |
| 1810 | else { |
| 1811 | errmsg = "unexpected end of data"; |
| 1812 | startinpos = s-starts; |
Ezio Melotti | 86e5e17 | 2010-07-03 05:34:39 +0000 | [diff] [blame] | 1813 | endinpos = startinpos+1; |
| 1814 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 1815 | endinpos++; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1816 | goto utf8Error; |
| 1817 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 1818 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1819 | |
| 1820 | switch (n) { |
| 1821 | |
| 1822 | case 0: |
Ezio Melotti | 86e5e17 | 2010-07-03 05:34:39 +0000 | [diff] [blame] | 1823 | errmsg = "invalid start byte"; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1824 | startinpos = s-starts; |
| 1825 | endinpos = startinpos+1; |
| 1826 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1827 | |
| 1828 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1829 | errmsg = "internal error"; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1830 | startinpos = s-starts; |
| 1831 | endinpos = startinpos+1; |
| 1832 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1833 | |
| 1834 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1835 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 86e5e17 | 2010-07-03 05:34:39 +0000 | [diff] [blame] | 1836 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1837 | startinpos = s-starts; |
Ezio Melotti | 86e5e17 | 2010-07-03 05:34:39 +0000 | [diff] [blame] | 1838 | endinpos = startinpos + 1; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1839 | goto utf8Error; |
| 1840 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1841 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 86e5e17 | 2010-07-03 05:34:39 +0000 | [diff] [blame] | 1842 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 1843 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1844 | break; |
| 1845 | |
| 1846 | case 3: |
Ezio Melotti | 86e5e17 | 2010-07-03 05:34:39 +0000 | [diff] [blame] | 1847 | /* XXX: surrogates shouldn't be valid UTF-8! |
| 1848 | see http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 1849 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt |
| 1850 | Uncomment the 2 lines below to make them invalid, |
| 1851 | codepoints: d800-dfff; UTF-8: \xed\xa0\x80-\xed\xbf\xbf. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1852 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 86e5e17 | 2010-07-03 05:34:39 +0000 | [diff] [blame] | 1853 | (s[2] & 0xc0) != 0x80 || |
| 1854 | ((unsigned char)s[0] == 0xE0 && |
| 1855 | (unsigned char)s[1] < 0xA0)/* || |
| 1856 | ((unsigned char)s[0] == 0xED && |
| 1857 | (unsigned char)s[1] > 0x9F)*/) { |
| 1858 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1859 | startinpos = s-starts; |
Ezio Melotti | 86e5e17 | 2010-07-03 05:34:39 +0000 | [diff] [blame] | 1860 | endinpos = startinpos + 1; |
| 1861 | |
| 1862 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 1863 | continuation byte is s[2], so increment endinpos by 1, |
| 1864 | if not, s[1] is invalid and endinpos doesn't need to |
| 1865 | be incremented. */ |
| 1866 | if ((s[1] & 0xC0) == 0x80) |
| 1867 | endinpos++; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1868 | goto utf8Error; |
| 1869 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1870 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 86e5e17 | 2010-07-03 05:34:39 +0000 | [diff] [blame] | 1871 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
| 1872 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1873 | break; |
| 1874 | |
| 1875 | case 4: |
| 1876 | if ((s[1] & 0xc0) != 0x80 || |
| 1877 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 86e5e17 | 2010-07-03 05:34:39 +0000 | [diff] [blame] | 1878 | (s[3] & 0xc0) != 0x80 || |
| 1879 | ((unsigned char)s[0] == 0xF0 && |
| 1880 | (unsigned char)s[1] < 0x90) || |
| 1881 | ((unsigned char)s[0] == 0xF4 && |
| 1882 | (unsigned char)s[1] > 0x8F)) { |
| 1883 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1884 | startinpos = s-starts; |
Ezio Melotti | 86e5e17 | 2010-07-03 05:34:39 +0000 | [diff] [blame] | 1885 | endinpos = startinpos + 1; |
| 1886 | if ((s[1] & 0xC0) == 0x80) { |
| 1887 | endinpos++; |
| 1888 | if ((s[2] & 0xC0) == 0x80) |
| 1889 | endinpos++; |
| 1890 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1891 | goto utf8Error; |
| 1892 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1893 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 86e5e17 | 2010-07-03 05:34:39 +0000 | [diff] [blame] | 1894 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 1895 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 1896 | |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1897 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1898 | *p++ = (Py_UNICODE)ch; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1899 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1900 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1901 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1902 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 1903 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1904 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1905 | /* high surrogate = top 10 bits added to D800 */ |
| 1906 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1907 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1908 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1909 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1910 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1911 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1912 | } |
| 1913 | s += n; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1914 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1915 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1916 | utf8Error: |
| 1917 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1918 | if (unicode_decode_call_errorhandler( |
| 1919 | errors, &errorHandler, |
| 1920 | "utf8", errmsg, |
| 1921 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1922 | &unicode, &outpos, &p)) |
| 1923 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1924 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1925 | if (consumed) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1926 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1927 | |
| 1928 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1929 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1930 | goto onError; |
| 1931 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1932 | Py_XDECREF(errorHandler); |
| 1933 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1934 | return (PyObject *)unicode; |
| 1935 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1936 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1937 | Py_XDECREF(errorHandler); |
| 1938 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1939 | Py_DECREF(unicode); |
| 1940 | return NULL; |
| 1941 | } |
| 1942 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1943 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 1944 | and allocate exactly as much space needed at the end. Else allocate the |
| 1945 | maximum possible needed (4 result bytes per Unicode character), and return |
| 1946 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1947 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1948 | PyObject * |
| 1949 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1950 | Py_ssize_t size, |
| 1951 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1952 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1953 | #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] | 1954 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1955 | Py_ssize_t i; /* index into s of next input byte */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1956 | PyObject *v; /* result string object */ |
| 1957 | char *p; /* next free byte in output buffer */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1958 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1959 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1960 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1961 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1962 | assert(s != NULL); |
| 1963 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1964 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1965 | if (size <= MAX_SHORT_UNICHARS) { |
| 1966 | /* Write into the stack buffer; nallocated can't overflow. |
| 1967 | * At the end, we'll allocate exactly as much heap space as it |
| 1968 | * turns out we need. |
| 1969 | */ |
| 1970 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
| 1971 | v = NULL; /* will allocate after we're done */ |
| 1972 | p = stackbuf; |
| 1973 | } |
| 1974 | else { |
| 1975 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 1976 | nallocated = size * 4; |
| 1977 | if (nallocated / 4 != size) /* overflow! */ |
| 1978 | return PyErr_NoMemory(); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1979 | v = PyString_FromStringAndSize(NULL, nallocated); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1980 | if (v == NULL) |
| 1981 | return NULL; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1982 | p = PyString_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1983 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1984 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1985 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1986 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1987 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1988 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1989 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1990 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1991 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1992 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1993 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 1994 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 1995 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1996 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1997 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1998 | /* Encode UCS2 Unicode ordinals */ |
| 1999 | if (ch < 0x10000) { |
| 2000 | /* Special case: check for high surrogate */ |
| 2001 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 2002 | Py_UCS4 ch2 = s[i]; |
| 2003 | /* Check for low surrogate and combine the two to |
| 2004 | form a UCS4 value */ |
| 2005 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2006 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2007 | i++; |
| 2008 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2009 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2010 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2011 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2012 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2013 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2014 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2015 | continue; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 2016 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2017 | encodeUCS4: |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2018 | /* Encode UCS4 Unicode ordinals */ |
| 2019 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2020 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 2021 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2022 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2023 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2024 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2025 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2026 | if (v == NULL) { |
| 2027 | /* This was stack allocated. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2028 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2029 | assert(nneeded <= nallocated); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2030 | v = PyString_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2031 | } |
| 2032 | else { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 2033 | /* Cut back to size actually needed. */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2034 | nneeded = p - PyString_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2035 | assert(nneeded <= nallocated); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2036 | _PyString_Resize(&v, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2037 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2038 | return v; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2039 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2040 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2041 | } |
| 2042 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2043 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 2044 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2045 | if (!PyUnicode_Check(unicode)) { |
| 2046 | PyErr_BadArgument(); |
| 2047 | return NULL; |
| 2048 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 2049 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2050 | PyUnicode_GET_SIZE(unicode), |
| 2051 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2054 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 2055 | |
| 2056 | PyObject * |
| 2057 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2058 | Py_ssize_t size, |
| 2059 | const char *errors, |
| 2060 | int *byteorder) |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2061 | { |
| 2062 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 2063 | } |
| 2064 | |
| 2065 | PyObject * |
| 2066 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2067 | Py_ssize_t size, |
| 2068 | const char *errors, |
| 2069 | int *byteorder, |
| 2070 | Py_ssize_t *consumed) |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2071 | { |
| 2072 | const char *starts = s; |
| 2073 | Py_ssize_t startinpos; |
| 2074 | Py_ssize_t endinpos; |
| 2075 | Py_ssize_t outpos; |
| 2076 | PyUnicodeObject *unicode; |
| 2077 | Py_UNICODE *p; |
| 2078 | #ifndef Py_UNICODE_WIDE |
Antoine Pitrou | 4595e51 | 2010-06-11 21:48:02 +0000 | [diff] [blame] | 2079 | int pairs = 0; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2080 | #else |
| 2081 | const int pairs = 0; |
| 2082 | #endif |
Antoine Pitrou | 4595e51 | 2010-06-11 21:48:02 +0000 | [diff] [blame] | 2083 | const unsigned char *q, *e, *qq; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2084 | int bo = 0; /* assume native ordering by default */ |
| 2085 | const char *errmsg = ""; |
Walter Dörwald | 20b40d3 | 2007-08-17 16:52:50 +0000 | [diff] [blame] | 2086 | /* Offsets from q for retrieving bytes in the right order. */ |
| 2087 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2088 | int iorder[] = {0, 1, 2, 3}; |
| 2089 | #else |
| 2090 | int iorder[] = {3, 2, 1, 0}; |
| 2091 | #endif |
Walter Dörwald | 9ab80a9 | 2007-08-17 16:58:43 +0000 | [diff] [blame] | 2092 | PyObject *errorHandler = NULL; |
| 2093 | PyObject *exc = NULL; |
Antoine Pitrou | 4595e51 | 2010-06-11 21:48:02 +0000 | [diff] [blame] | 2094 | |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2095 | q = (unsigned char *)s; |
| 2096 | e = q + size; |
| 2097 | |
| 2098 | if (byteorder) |
| 2099 | bo = *byteorder; |
| 2100 | |
| 2101 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2102 | byte order setting accordingly. In native mode, the leading BOM |
| 2103 | mark is skipped, in all other modes, it is copied to the output |
| 2104 | stream as-is (giving a ZWNBSP character). */ |
| 2105 | if (bo == 0) { |
| 2106 | if (size >= 4) { |
| 2107 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2108 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2109 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2110 | if (bom == 0x0000FEFF) { |
| 2111 | q += 4; |
| 2112 | bo = -1; |
| 2113 | } |
| 2114 | else if (bom == 0xFFFE0000) { |
| 2115 | q += 4; |
| 2116 | bo = 1; |
| 2117 | } |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2118 | #else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2119 | if (bom == 0x0000FEFF) { |
| 2120 | q += 4; |
| 2121 | bo = 1; |
| 2122 | } |
| 2123 | else if (bom == 0xFFFE0000) { |
| 2124 | q += 4; |
| 2125 | bo = -1; |
| 2126 | } |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2127 | #endif |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2128 | } |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2129 | } |
| 2130 | |
| 2131 | if (bo == -1) { |
| 2132 | /* force LE */ |
| 2133 | iorder[0] = 0; |
| 2134 | iorder[1] = 1; |
| 2135 | iorder[2] = 2; |
| 2136 | iorder[3] = 3; |
| 2137 | } |
| 2138 | else if (bo == 1) { |
| 2139 | /* force BE */ |
| 2140 | iorder[0] = 3; |
| 2141 | iorder[1] = 2; |
| 2142 | iorder[2] = 1; |
| 2143 | iorder[3] = 0; |
| 2144 | } |
| 2145 | |
Antoine Pitrou | 4595e51 | 2010-06-11 21:48:02 +0000 | [diff] [blame] | 2146 | /* On narrow builds we split characters outside the BMP into two |
| 2147 | codepoints => count how much extra space we need. */ |
| 2148 | #ifndef Py_UNICODE_WIDE |
| 2149 | for (qq = q; qq < e; qq += 4) |
| 2150 | if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) |
| 2151 | pairs++; |
| 2152 | #endif |
| 2153 | |
| 2154 | /* This might be one to much, because of a BOM */ |
| 2155 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 2156 | if (!unicode) |
| 2157 | return NULL; |
| 2158 | if (size == 0) |
| 2159 | return (PyObject *)unicode; |
| 2160 | |
| 2161 | /* Unpack UTF-32 encoded data */ |
| 2162 | p = unicode->str; |
| 2163 | |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2164 | while (q < e) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2165 | Py_UCS4 ch; |
| 2166 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 2167 | if (e-q<4) { |
| 2168 | if (consumed) |
| 2169 | break; |
| 2170 | errmsg = "truncated data"; |
| 2171 | startinpos = ((const char *)q)-starts; |
| 2172 | endinpos = ((const char *)e)-starts; |
| 2173 | goto utf32Error; |
| 2174 | /* The remaining input chars are ignored if the callback |
| 2175 | chooses to skip the input */ |
| 2176 | } |
| 2177 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2178 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2179 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2180 | if (ch >= 0x110000) |
| 2181 | { |
| 2182 | errmsg = "codepoint not in range(0x110000)"; |
| 2183 | startinpos = ((const char *)q)-starts; |
| 2184 | endinpos = startinpos+4; |
| 2185 | goto utf32Error; |
| 2186 | } |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2187 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2188 | if (ch >= 0x10000) |
| 2189 | { |
| 2190 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 2191 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2192 | } |
| 2193 | else |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2194 | #endif |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2195 | *p++ = ch; |
| 2196 | q += 4; |
| 2197 | continue; |
| 2198 | utf32Error: |
| 2199 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2200 | if (unicode_decode_call_errorhandler( |
| 2201 | errors, &errorHandler, |
| 2202 | "utf32", errmsg, |
Georg Brandl | f7a09be | 2009-09-17 11:33:31 +0000 | [diff] [blame] | 2203 | starts, size, &startinpos, &endinpos, &exc, (const char **)&q, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2204 | &unicode, &outpos, &p)) |
| 2205 | goto onError; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2206 | } |
| 2207 | |
| 2208 | if (byteorder) |
| 2209 | *byteorder = bo; |
| 2210 | |
| 2211 | if (consumed) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2212 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2213 | |
| 2214 | /* Adjust length */ |
| 2215 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 2216 | goto onError; |
| 2217 | |
| 2218 | Py_XDECREF(errorHandler); |
| 2219 | Py_XDECREF(exc); |
| 2220 | return (PyObject *)unicode; |
| 2221 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2222 | onError: |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2223 | Py_DECREF(unicode); |
| 2224 | Py_XDECREF(errorHandler); |
| 2225 | Py_XDECREF(exc); |
| 2226 | return NULL; |
| 2227 | } |
| 2228 | |
| 2229 | PyObject * |
| 2230 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2231 | Py_ssize_t size, |
| 2232 | const char *errors, |
| 2233 | int byteorder) |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2234 | { |
| 2235 | PyObject *v; |
| 2236 | unsigned char *p; |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2237 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2238 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2239 | Py_ssize_t i, pairs; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2240 | #else |
| 2241 | const int pairs = 0; |
| 2242 | #endif |
| 2243 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2244 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2245 | int iorder[] = {0, 1, 2, 3}; |
| 2246 | #else |
| 2247 | int iorder[] = {3, 2, 1, 0}; |
| 2248 | #endif |
| 2249 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2250 | #define STORECHAR(CH) \ |
| 2251 | do { \ |
| 2252 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 2253 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 2254 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 2255 | p[iorder[0]] = (CH) & 0xff; \ |
| 2256 | p += 4; \ |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2257 | } while(0) |
| 2258 | |
| 2259 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 2260 | so we need less space. */ |
| 2261 | #ifndef Py_UNICODE_WIDE |
| 2262 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2263 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2264 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 2265 | pairs++; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2266 | #endif |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2267 | nsize = (size - pairs + (byteorder == 0)); |
| 2268 | bytesize = nsize * 4; |
| 2269 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2270 | return PyErr_NoMemory(); |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2271 | v = PyString_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2272 | if (v == NULL) |
| 2273 | return NULL; |
| 2274 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2275 | p = (unsigned char *)PyString_AS_STRING(v); |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2276 | if (byteorder == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2277 | STORECHAR(0xFEFF); |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2278 | if (size == 0) |
| 2279 | return v; |
| 2280 | |
| 2281 | if (byteorder == -1) { |
| 2282 | /* force LE */ |
| 2283 | iorder[0] = 0; |
| 2284 | iorder[1] = 1; |
| 2285 | iorder[2] = 2; |
| 2286 | iorder[3] = 3; |
| 2287 | } |
| 2288 | else if (byteorder == 1) { |
| 2289 | /* force BE */ |
| 2290 | iorder[0] = 3; |
| 2291 | iorder[1] = 2; |
| 2292 | iorder[2] = 1; |
| 2293 | iorder[3] = 0; |
| 2294 | } |
| 2295 | |
| 2296 | while (size-- > 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2297 | Py_UCS4 ch = *s++; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2298 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2299 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 2300 | Py_UCS4 ch2 = *s; |
| 2301 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 2302 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 2303 | s++; |
| 2304 | size--; |
| 2305 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 2306 | } |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2307 | #endif |
| 2308 | STORECHAR(ch); |
| 2309 | } |
| 2310 | return v; |
| 2311 | #undef STORECHAR |
| 2312 | } |
| 2313 | |
| 2314 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 2315 | { |
| 2316 | if (!PyUnicode_Check(unicode)) { |
| 2317 | PyErr_BadArgument(); |
| 2318 | return NULL; |
| 2319 | } |
| 2320 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2321 | PyUnicode_GET_SIZE(unicode), |
| 2322 | NULL, |
| 2323 | 0); |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2324 | } |
| 2325 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2326 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 2327 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2328 | PyObject * |
| 2329 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2330 | Py_ssize_t size, |
| 2331 | const char *errors, |
| 2332 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2333 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2334 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 2335 | } |
| 2336 | |
| 2337 | PyObject * |
| 2338 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2339 | Py_ssize_t size, |
| 2340 | const char *errors, |
| 2341 | int *byteorder, |
| 2342 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2343 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2344 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2345 | Py_ssize_t startinpos; |
| 2346 | Py_ssize_t endinpos; |
| 2347 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2348 | PyUnicodeObject *unicode; |
| 2349 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2350 | const unsigned char *q, *e; |
| 2351 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2352 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2353 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 2354 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2355 | int ihi = 1, ilo = 0; |
| 2356 | #else |
| 2357 | int ihi = 0, ilo = 1; |
| 2358 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2359 | PyObject *errorHandler = NULL; |
| 2360 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2361 | |
| 2362 | /* Note: size will always be longer than the resulting Unicode |
| 2363 | character count */ |
| 2364 | unicode = _PyUnicode_New(size); |
| 2365 | if (!unicode) |
| 2366 | return NULL; |
| 2367 | if (size == 0) |
| 2368 | return (PyObject *)unicode; |
| 2369 | |
| 2370 | /* Unpack UTF-16 encoded data */ |
| 2371 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2372 | q = (unsigned char *)s; |
| 2373 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2374 | |
| 2375 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2376 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2377 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2378 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2379 | byte order setting accordingly. In native mode, the leading BOM |
| 2380 | mark is skipped, in all other modes, it is copied to the output |
| 2381 | stream as-is (giving a ZWNBSP character). */ |
| 2382 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2383 | if (size >= 2) { |
| 2384 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2385 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2386 | if (bom == 0xFEFF) { |
| 2387 | q += 2; |
| 2388 | bo = -1; |
| 2389 | } |
| 2390 | else if (bom == 0xFFFE) { |
| 2391 | q += 2; |
| 2392 | bo = 1; |
| 2393 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2394 | #else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2395 | if (bom == 0xFEFF) { |
| 2396 | q += 2; |
| 2397 | bo = 1; |
| 2398 | } |
| 2399 | else if (bom == 0xFFFE) { |
| 2400 | q += 2; |
| 2401 | bo = -1; |
| 2402 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2403 | #endif |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2404 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2405 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2406 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2407 | if (bo == -1) { |
| 2408 | /* force LE */ |
| 2409 | ihi = 1; |
| 2410 | ilo = 0; |
| 2411 | } |
| 2412 | else if (bo == 1) { |
| 2413 | /* force BE */ |
| 2414 | ihi = 0; |
| 2415 | ilo = 1; |
| 2416 | } |
| 2417 | |
| 2418 | while (q < e) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2419 | Py_UNICODE ch; |
| 2420 | /* remaining bytes at the end? (size should be even) */ |
| 2421 | if (e-q<2) { |
| 2422 | if (consumed) |
| 2423 | break; |
| 2424 | errmsg = "truncated data"; |
| 2425 | startinpos = ((const char *)q)-starts; |
| 2426 | endinpos = ((const char *)e)-starts; |
| 2427 | goto utf16Error; |
| 2428 | /* The remaining input chars are ignored if the callback |
| 2429 | chooses to skip the input */ |
| 2430 | } |
| 2431 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2432 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 2433 | q += 2; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2434 | |
| 2435 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 2436 | *p++ = ch; |
| 2437 | continue; |
| 2438 | } |
| 2439 | |
| 2440 | /* UTF-16 code pair: */ |
| 2441 | if (q >= e) { |
| 2442 | errmsg = "unexpected end of data"; |
| 2443 | startinpos = (((const char *)q)-2)-starts; |
| 2444 | endinpos = ((const char *)e)-starts; |
| 2445 | goto utf16Error; |
| 2446 | } |
| 2447 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 2448 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 2449 | q += 2; |
| 2450 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2451 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2452 | *p++ = ch; |
| 2453 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2454 | #else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2455 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2456 | #endif |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2457 | continue; |
| 2458 | } |
| 2459 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2460 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2461 | startinpos = (((const char *)q)-4)-starts; |
| 2462 | endinpos = startinpos+2; |
| 2463 | goto utf16Error; |
| 2464 | } |
| 2465 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 2466 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2467 | errmsg = "illegal encoding"; |
| 2468 | startinpos = (((const char *)q)-2)-starts; |
| 2469 | endinpos = startinpos+2; |
| 2470 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2471 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2472 | utf16Error: |
| 2473 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2474 | if (unicode_decode_call_errorhandler( |
| 2475 | errors, &errorHandler, |
| 2476 | "utf16", errmsg, |
| 2477 | starts, size, &startinpos, &endinpos, &exc, (const char **)&q, |
| 2478 | &unicode, &outpos, &p)) |
| 2479 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
| 2482 | if (byteorder) |
| 2483 | *byteorder = bo; |
| 2484 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2485 | if (consumed) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2486 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2487 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2488 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2489 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2490 | goto onError; |
| 2491 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2492 | Py_XDECREF(errorHandler); |
| 2493 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2494 | return (PyObject *)unicode; |
| 2495 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2496 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2497 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2498 | Py_XDECREF(errorHandler); |
| 2499 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2500 | return NULL; |
| 2501 | } |
| 2502 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2503 | PyObject * |
| 2504 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2505 | Py_ssize_t size, |
| 2506 | const char *errors, |
| 2507 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2508 | { |
| 2509 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2510 | unsigned char *p; |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2511 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2512 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2513 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2514 | #else |
| 2515 | const int pairs = 0; |
| 2516 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2517 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2518 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2519 | int ihi = 1, ilo = 0; |
| 2520 | #else |
| 2521 | int ihi = 0, ilo = 1; |
| 2522 | #endif |
| 2523 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2524 | #define STORECHAR(CH) \ |
| 2525 | do { \ |
| 2526 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 2527 | p[ilo] = (CH) & 0xff; \ |
| 2528 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2529 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2530 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2531 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2532 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2533 | if (s[i] >= 0x10000) |
| 2534 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2535 | #endif |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2536 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 2537 | if (size > PY_SSIZE_T_MAX || |
| 2538 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2539 | return PyErr_NoMemory(); |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2540 | nsize = size + pairs + (byteorder == 0); |
| 2541 | bytesize = nsize * 2; |
| 2542 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2543 | return PyErr_NoMemory(); |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2544 | v = PyString_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2545 | if (v == NULL) |
| 2546 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2547 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2548 | p = (unsigned char *)PyString_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2549 | if (byteorder == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2550 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2551 | if (size == 0) |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2552 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2553 | |
| 2554 | if (byteorder == -1) { |
| 2555 | /* force LE */ |
| 2556 | ihi = 1; |
| 2557 | ilo = 0; |
| 2558 | } |
| 2559 | else if (byteorder == 1) { |
| 2560 | /* force BE */ |
| 2561 | ihi = 0; |
| 2562 | ilo = 1; |
| 2563 | } |
| 2564 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2565 | while (size-- > 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2566 | Py_UNICODE ch = *s++; |
| 2567 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2568 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2569 | if (ch >= 0x10000) { |
| 2570 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2571 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 2572 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2573 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2574 | STORECHAR(ch); |
| 2575 | if (ch2) |
| 2576 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2577 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2578 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2579 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2580 | } |
| 2581 | |
| 2582 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 2583 | { |
| 2584 | if (!PyUnicode_Check(unicode)) { |
| 2585 | PyErr_BadArgument(); |
| 2586 | return NULL; |
| 2587 | } |
| 2588 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2589 | PyUnicode_GET_SIZE(unicode), |
| 2590 | NULL, |
| 2591 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2592 | } |
| 2593 | |
| 2594 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 2595 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2596 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 2597 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2598 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2599 | Py_ssize_t size, |
| 2600 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2601 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2602 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2603 | Py_ssize_t startinpos; |
| 2604 | Py_ssize_t endinpos; |
| 2605 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2606 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2607 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2608 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2609 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2610 | char* message; |
| 2611 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2612 | PyObject *errorHandler = NULL; |
| 2613 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2614 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2615 | /* Escaped strings will always be longer than the resulting |
| 2616 | 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] | 2617 | length after conversion to the true value. |
| 2618 | (but if the error callback returns a long replacement string |
| 2619 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2620 | v = _PyUnicode_New(size); |
| 2621 | if (v == NULL) |
| 2622 | goto onError; |
| 2623 | if (size == 0) |
| 2624 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2625 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2626 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2627 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2628 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2629 | while (s < end) { |
| 2630 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2631 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2632 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2633 | |
| 2634 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2635 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2636 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2637 | continue; |
| 2638 | } |
| 2639 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2640 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2641 | /* \ - Escapes */ |
| 2642 | s++; |
Guido van Rossum | 1c1ac38 | 2007-10-29 22:15:05 +0000 | [diff] [blame] | 2643 | c = *s++; |
| 2644 | if (s > end) |
| 2645 | c = '\0'; /* Invalid after \ */ |
| 2646 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2647 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2648 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2649 | case '\n': break; |
| 2650 | case '\\': *p++ = '\\'; break; |
| 2651 | case '\'': *p++ = '\''; break; |
| 2652 | case '\"': *p++ = '\"'; break; |
| 2653 | case 'b': *p++ = '\b'; break; |
| 2654 | case 'f': *p++ = '\014'; break; /* FF */ |
| 2655 | case 't': *p++ = '\t'; break; |
| 2656 | case 'n': *p++ = '\n'; break; |
| 2657 | case 'r': *p++ = '\r'; break; |
| 2658 | case 'v': *p++ = '\013'; break; /* VT */ |
| 2659 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 2660 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2661 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2662 | case '0': case '1': case '2': case '3': |
| 2663 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2664 | x = s[-1] - '0'; |
Guido van Rossum | 1c1ac38 | 2007-10-29 22:15:05 +0000 | [diff] [blame] | 2665 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2666 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 1c1ac38 | 2007-10-29 22:15:05 +0000 | [diff] [blame] | 2667 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2668 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2669 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2670 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2671 | break; |
| 2672 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2673 | /* hex escapes */ |
| 2674 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2675 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2676 | digits = 2; |
| 2677 | message = "truncated \\xXX escape"; |
| 2678 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2679 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2680 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2681 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2682 | digits = 4; |
| 2683 | message = "truncated \\uXXXX escape"; |
| 2684 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2685 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2686 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2687 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2688 | digits = 8; |
| 2689 | message = "truncated \\UXXXXXXXX escape"; |
| 2690 | hexescape: |
| 2691 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2692 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2693 | if (s+digits>end) { |
| 2694 | endinpos = size; |
| 2695 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2696 | errors, &errorHandler, |
| 2697 | "unicodeescape", "end of string in escape sequence", |
| 2698 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2699 | &v, &outpos, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2700 | goto onError; |
| 2701 | goto nextByte; |
| 2702 | } |
| 2703 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2704 | c = (unsigned char) s[i]; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2705 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2706 | endinpos = (s+i+1)-starts; |
| 2707 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2708 | errors, &errorHandler, |
| 2709 | "unicodeescape", message, |
| 2710 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2711 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2712 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2713 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2714 | } |
| 2715 | chr = (chr<<4) & ~0xF; |
| 2716 | if (c >= '0' && c <= '9') |
| 2717 | chr += c - '0'; |
| 2718 | else if (c >= 'a' && c <= 'f') |
| 2719 | chr += 10 + c - 'a'; |
| 2720 | else |
| 2721 | chr += 10 + c - 'A'; |
| 2722 | } |
| 2723 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 2724 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2725 | /* _decoding_error will have already written into the |
| 2726 | target buffer. */ |
| 2727 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2728 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2729 | /* when we get here, chr is a 32-bit unicode character */ |
| 2730 | if (chr <= 0xffff) |
| 2731 | /* UCS-2 character */ |
| 2732 | *p++ = (Py_UNICODE) chr; |
| 2733 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2734 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2735 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2736 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2737 | *p++ = chr; |
| 2738 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2739 | chr -= 0x10000L; |
| 2740 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2741 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2742 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2743 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2744 | endinpos = s-starts; |
| 2745 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2746 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2747 | errors, &errorHandler, |
| 2748 | "unicodeescape", "illegal Unicode character", |
| 2749 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2750 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2751 | goto onError; |
| 2752 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2753 | break; |
| 2754 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2755 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2756 | case 'N': |
| 2757 | message = "malformed \\N character escape"; |
| 2758 | if (ucnhash_CAPI == NULL) { |
| 2759 | /* load the unicode data module */ |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2760 | PyObject *m, *api; |
Christian Heimes | 000a074 | 2008-01-03 22:16:32 +0000 | [diff] [blame] | 2761 | m = PyImport_ImportModuleNoBlock("unicodedata"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2762 | if (m == NULL) |
| 2763 | goto ucnhashError; |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2764 | api = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2765 | Py_DECREF(m); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2766 | if (api == NULL) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2767 | goto ucnhashError; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2768 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2769 | Py_DECREF(api); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2770 | if (ucnhash_CAPI == NULL) |
| 2771 | goto ucnhashError; |
| 2772 | } |
| 2773 | if (*s == '{') { |
| 2774 | const char *start = s+1; |
| 2775 | /* look for the closing brace */ |
| 2776 | while (*s != '}' && s < end) |
| 2777 | s++; |
| 2778 | if (s > start && s < end && *s == '}') { |
| 2779 | /* found a name. look it up in the unicode database */ |
| 2780 | message = "unknown Unicode character name"; |
| 2781 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 2782 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2783 | goto store; |
| 2784 | } |
| 2785 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2786 | endinpos = s-starts; |
| 2787 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2788 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2789 | errors, &errorHandler, |
| 2790 | "unicodeescape", message, |
| 2791 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2792 | &v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2793 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2794 | break; |
| 2795 | |
| 2796 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2797 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2798 | message = "\\ at end of string"; |
| 2799 | s--; |
| 2800 | endinpos = s-starts; |
| 2801 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2802 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2803 | errors, &errorHandler, |
| 2804 | "unicodeescape", message, |
| 2805 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2806 | &v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2807 | goto onError; |
| 2808 | } |
| 2809 | else { |
| 2810 | *p++ = '\\'; |
| 2811 | *p++ = (unsigned char)s[-1]; |
| 2812 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2813 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2814 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2815 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2816 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2817 | } |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2818 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2819 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 2820 | Py_XDECREF(errorHandler); |
| 2821 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2822 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2823 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2824 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2825 | PyErr_SetString( |
| 2826 | PyExc_UnicodeError, |
| 2827 | "\\N escapes not supported (can't load unicodedata module)" |
| 2828 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2829 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2830 | Py_XDECREF(errorHandler); |
| 2831 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 2832 | return NULL; |
| 2833 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2834 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2835 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2836 | Py_XDECREF(errorHandler); |
| 2837 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2838 | return NULL; |
| 2839 | } |
| 2840 | |
| 2841 | /* Return a Unicode-Escape string version of the Unicode object. |
| 2842 | |
| 2843 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 2844 | appropriate. |
| 2845 | |
| 2846 | */ |
| 2847 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 2848 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2849 | Py_ssize_t size, |
| 2850 | Py_UNICODE ch) |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 2851 | { |
| 2852 | /* like wcschr, but doesn't stop at NULL characters */ |
| 2853 | |
| 2854 | while (size-- > 0) { |
| 2855 | if (*s == ch) |
| 2856 | return s; |
| 2857 | s++; |
| 2858 | } |
| 2859 | |
| 2860 | return NULL; |
| 2861 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 2862 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2863 | static |
| 2864 | PyObject *unicodeescape_string(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2865 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2866 | int quotes) |
| 2867 | { |
| 2868 | PyObject *repr; |
| 2869 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2870 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2871 | static const char *hexdigit = "0123456789abcdef"; |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2872 | #ifdef Py_UNICODE_WIDE |
| 2873 | const Py_ssize_t expandsize = 10; |
| 2874 | #else |
| 2875 | const Py_ssize_t expandsize = 6; |
| 2876 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2877 | |
Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 2878 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 2879 | better to choose a different scheme. Perhaps scan the |
| 2880 | first N-chars of the string and allocate based on that size. |
| 2881 | */ |
| 2882 | /* Initial allocation is based on the longest-possible unichr |
| 2883 | escape. |
| 2884 | |
| 2885 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 2886 | unichr, so in this case it's the longest unichr escape. In |
| 2887 | narrow (UTF-16) builds this is five chars per source unichr |
| 2888 | since there are two unichrs in the surrogate pair, so in narrow |
| 2889 | (UTF-16) builds it's not the longest unichr escape. |
| 2890 | |
| 2891 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 2892 | so in the narrow (UTF-16) build case it's the longest unichr |
| 2893 | escape. |
| 2894 | */ |
| 2895 | |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2896 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2897 | return PyErr_NoMemory(); |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2898 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2899 | repr = PyString_FromStringAndSize(NULL, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2900 | 2 |
| 2901 | + expandsize*size |
| 2902 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2903 | if (repr == NULL) |
| 2904 | return NULL; |
| 2905 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2906 | p = PyString_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2907 | |
| 2908 | if (quotes) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2909 | *p++ = 'u'; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2910 | *p++ = (findchar(s, size, '\'') && |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2911 | !findchar(s, size, '"')) ? '"' : '\''; |
| 2912 | } |
| 2913 | while (size-- > 0) { |
| 2914 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2915 | |
Hye-Shik Chang | 835b243 | 2005-12-17 04:38:31 +0000 | [diff] [blame] | 2916 | /* Escape quotes and backslashes */ |
| 2917 | if ((quotes && |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2918 | ch == (Py_UNICODE) PyString_AS_STRING(repr)[1]) || ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2919 | *p++ = '\\'; |
| 2920 | *p++ = (char) ch; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2921 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2922 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2923 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 2924 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2925 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 2926 | else if (ch >= 0x10000) { |
| 2927 | *p++ = '\\'; |
| 2928 | *p++ = 'U'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2929 | *p++ = hexdigit[(ch >> 28) & 0x0000000F]; |
| 2930 | *p++ = hexdigit[(ch >> 24) & 0x0000000F]; |
| 2931 | *p++ = hexdigit[(ch >> 20) & 0x0000000F]; |
| 2932 | *p++ = hexdigit[(ch >> 16) & 0x0000000F]; |
| 2933 | *p++ = hexdigit[(ch >> 12) & 0x0000000F]; |
| 2934 | *p++ = hexdigit[(ch >> 8) & 0x0000000F]; |
| 2935 | *p++ = hexdigit[(ch >> 4) & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2936 | *p++ = hexdigit[ch & 0x0000000F]; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2937 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2938 | } |
Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 2939 | #else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2940 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 2941 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 2942 | Py_UNICODE ch2; |
| 2943 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2944 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2945 | ch2 = *s++; |
| 2946 | size--; |
| 2947 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 2948 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 2949 | *p++ = '\\'; |
| 2950 | *p++ = 'U'; |
| 2951 | *p++ = hexdigit[(ucs >> 28) & 0x0000000F]; |
| 2952 | *p++ = hexdigit[(ucs >> 24) & 0x0000000F]; |
| 2953 | *p++ = hexdigit[(ucs >> 20) & 0x0000000F]; |
| 2954 | *p++ = hexdigit[(ucs >> 16) & 0x0000000F]; |
| 2955 | *p++ = hexdigit[(ucs >> 12) & 0x0000000F]; |
| 2956 | *p++ = hexdigit[(ucs >> 8) & 0x0000000F]; |
| 2957 | *p++ = hexdigit[(ucs >> 4) & 0x0000000F]; |
| 2958 | *p++ = hexdigit[ucs & 0x0000000F]; |
| 2959 | continue; |
| 2960 | } |
| 2961 | /* Fall through: isolated surrogates are copied as-is */ |
| 2962 | s--; |
| 2963 | size++; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 2964 | } |
Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 2965 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2966 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2967 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2968 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2969 | *p++ = '\\'; |
| 2970 | *p++ = 'u'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2971 | *p++ = hexdigit[(ch >> 12) & 0x000F]; |
| 2972 | *p++ = hexdigit[(ch >> 8) & 0x000F]; |
| 2973 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2974 | *p++ = hexdigit[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2975 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2976 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2977 | /* Map special whitespace to '\t', \n', '\r' */ |
| 2978 | else if (ch == '\t') { |
| 2979 | *p++ = '\\'; |
| 2980 | *p++ = 't'; |
| 2981 | } |
| 2982 | else if (ch == '\n') { |
| 2983 | *p++ = '\\'; |
| 2984 | *p++ = 'n'; |
| 2985 | } |
| 2986 | else if (ch == '\r') { |
| 2987 | *p++ = '\\'; |
| 2988 | *p++ = 'r'; |
| 2989 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2990 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2991 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 2992 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2993 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2994 | *p++ = 'x'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2995 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2996 | *p++ = hexdigit[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2997 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2998 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2999 | /* Copy everything else as-is */ |
| 3000 | else |
| 3001 | *p++ = (char) ch; |
| 3002 | } |
| 3003 | if (quotes) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3004 | *p++ = PyString_AS_STRING(repr)[1]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3005 | |
| 3006 | *p = '\0'; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3007 | _PyString_Resize(&repr, p - PyString_AS_STRING(repr)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3008 | return repr; |
| 3009 | } |
| 3010 | |
| 3011 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3012 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3013 | { |
| 3014 | return unicodeescape_string(s, size, 0); |
| 3015 | } |
| 3016 | |
| 3017 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 3018 | { |
| 3019 | if (!PyUnicode_Check(unicode)) { |
| 3020 | PyErr_BadArgument(); |
| 3021 | return NULL; |
| 3022 | } |
| 3023 | return PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3024 | PyUnicode_GET_SIZE(unicode)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3025 | } |
| 3026 | |
| 3027 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 3028 | |
| 3029 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3030 | Py_ssize_t size, |
| 3031 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3032 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3033 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3034 | Py_ssize_t startinpos; |
| 3035 | Py_ssize_t endinpos; |
| 3036 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3037 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3038 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3039 | const char *end; |
| 3040 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3041 | PyObject *errorHandler = NULL; |
| 3042 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3043 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3044 | /* Escaped strings will always be longer than the resulting |
| 3045 | 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] | 3046 | length after conversion to the true value. (But decoding error |
| 3047 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3048 | v = _PyUnicode_New(size); |
| 3049 | if (v == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3050 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3051 | if (size == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3052 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3053 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3054 | end = s + size; |
| 3055 | while (s < end) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3056 | unsigned char c; |
| 3057 | Py_UCS4 x; |
| 3058 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3059 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3060 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3061 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3062 | if (*s != '\\') { |
| 3063 | *p++ = (unsigned char)*s++; |
| 3064 | continue; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 3065 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3066 | startinpos = s-starts; |
| 3067 | |
| 3068 | /* \u-escapes are only interpreted iff the number of leading |
| 3069 | backslashes if odd */ |
| 3070 | bs = s; |
| 3071 | for (;s < end;) { |
| 3072 | if (*s != '\\') |
| 3073 | break; |
| 3074 | *p++ = (unsigned char)*s++; |
| 3075 | } |
| 3076 | if (((s - bs) & 1) == 0 || |
| 3077 | s >= end || |
| 3078 | (*s != 'u' && *s != 'U')) { |
| 3079 | continue; |
| 3080 | } |
| 3081 | p--; |
| 3082 | count = *s=='u' ? 4 : 8; |
| 3083 | s++; |
| 3084 | |
| 3085 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 3086 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3087 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 3088 | c = (unsigned char)*s; |
| 3089 | if (!isxdigit(c)) { |
| 3090 | endinpos = s-starts; |
| 3091 | if (unicode_decode_call_errorhandler( |
| 3092 | errors, &errorHandler, |
| 3093 | "rawunicodeescape", "truncated \\uXXXX", |
| 3094 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3095 | &v, &outpos, &p)) |
| 3096 | goto onError; |
| 3097 | goto nextByte; |
| 3098 | } |
| 3099 | x = (x<<4) & ~0xF; |
| 3100 | if (c >= '0' && c <= '9') |
| 3101 | x += c - '0'; |
| 3102 | else if (c >= 'a' && c <= 'f') |
| 3103 | x += 10 + c - 'a'; |
| 3104 | else |
| 3105 | x += 10 + c - 'A'; |
| 3106 | } |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3107 | if (x <= 0xffff) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3108 | /* UCS-2 character */ |
| 3109 | *p++ = (Py_UNICODE) x; |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3110 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3111 | /* UCS-4 character. Either store directly, or as |
| 3112 | surrogate pair. */ |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3113 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3114 | *p++ = (Py_UNICODE) x; |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3115 | #else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3116 | x -= 0x10000L; |
| 3117 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 3118 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3119 | #endif |
| 3120 | } else { |
| 3121 | endinpos = s-starts; |
| 3122 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3123 | if (unicode_decode_call_errorhandler( |
| 3124 | errors, &errorHandler, |
| 3125 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3126 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3127 | &v, &outpos, &p)) |
| 3128 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3129 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3130 | nextByte: |
| 3131 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3132 | } |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3133 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3134 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3135 | Py_XDECREF(errorHandler); |
| 3136 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3137 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3138 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3139 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3140 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3141 | Py_XDECREF(errorHandler); |
| 3142 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3143 | return NULL; |
| 3144 | } |
| 3145 | |
| 3146 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3147 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3148 | { |
| 3149 | PyObject *repr; |
| 3150 | char *p; |
| 3151 | char *q; |
| 3152 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3153 | static const char *hexdigit = "0123456789abcdef"; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3154 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 3155 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3156 | #else |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 3157 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3158 | #endif |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 3159 | |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 3160 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3161 | return PyErr_NoMemory(); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 3162 | |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 3163 | repr = PyString_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3164 | if (repr == NULL) |
| 3165 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3166 | if (size == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3167 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3168 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3169 | p = q = PyString_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3170 | while (size-- > 0) { |
| 3171 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3172 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3173 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 3174 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3175 | *p++ = '\\'; |
| 3176 | *p++ = 'U'; |
| 3177 | *p++ = hexdigit[(ch >> 28) & 0xf]; |
| 3178 | *p++ = hexdigit[(ch >> 24) & 0xf]; |
| 3179 | *p++ = hexdigit[(ch >> 20) & 0xf]; |
| 3180 | *p++ = hexdigit[(ch >> 16) & 0xf]; |
| 3181 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 3182 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 3183 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 3184 | *p++ = hexdigit[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3185 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3186 | else |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3187 | #else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3188 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3189 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 3190 | Py_UNICODE ch2; |
| 3191 | Py_UCS4 ucs; |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3192 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3193 | ch2 = *s++; |
| 3194 | size--; |
| 3195 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3196 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3197 | *p++ = '\\'; |
| 3198 | *p++ = 'U'; |
| 3199 | *p++ = hexdigit[(ucs >> 28) & 0xf]; |
| 3200 | *p++ = hexdigit[(ucs >> 24) & 0xf]; |
| 3201 | *p++ = hexdigit[(ucs >> 20) & 0xf]; |
| 3202 | *p++ = hexdigit[(ucs >> 16) & 0xf]; |
| 3203 | *p++ = hexdigit[(ucs >> 12) & 0xf]; |
| 3204 | *p++ = hexdigit[(ucs >> 8) & 0xf]; |
| 3205 | *p++ = hexdigit[(ucs >> 4) & 0xf]; |
| 3206 | *p++ = hexdigit[ucs & 0xf]; |
| 3207 | continue; |
| 3208 | } |
| 3209 | /* Fall through: isolated surrogates are copied as-is */ |
| 3210 | s--; |
| 3211 | size++; |
| 3212 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3213 | #endif |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3214 | /* Map 16-bit characters to '\uxxxx' */ |
| 3215 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3216 | *p++ = '\\'; |
| 3217 | *p++ = 'u'; |
| 3218 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 3219 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 3220 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 3221 | *p++ = hexdigit[ch & 15]; |
| 3222 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3223 | /* Copy everything else as-is */ |
| 3224 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3225 | *p++ = (char) ch; |
| 3226 | } |
| 3227 | *p = '\0'; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3228 | _PyString_Resize(&repr, p - q); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3229 | return repr; |
| 3230 | } |
| 3231 | |
| 3232 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 3233 | { |
| 3234 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3235 | PyErr_BadArgument(); |
| 3236 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3237 | } |
| 3238 | return PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3239 | PyUnicode_GET_SIZE(unicode)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3240 | } |
| 3241 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3242 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 3243 | |
| 3244 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3245 | Py_ssize_t size, |
| 3246 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3247 | { |
| 3248 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3249 | Py_ssize_t startinpos; |
| 3250 | Py_ssize_t endinpos; |
| 3251 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3252 | PyUnicodeObject *v; |
| 3253 | Py_UNICODE *p; |
| 3254 | const char *end; |
| 3255 | const char *reason; |
| 3256 | PyObject *errorHandler = NULL; |
| 3257 | PyObject *exc = NULL; |
| 3258 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 3259 | #ifdef Py_UNICODE_WIDE |
| 3260 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 3261 | #endif |
| 3262 | |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 3263 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3264 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 3265 | if (v == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3266 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3267 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3268 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3269 | p = PyUnicode_AS_UNICODE(v); |
| 3270 | end = s + size; |
| 3271 | |
| 3272 | while (s < end) { |
Neal Norwitz | 1004a53 | 2006-05-15 07:17:23 +0000 | [diff] [blame] | 3273 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3274 | /* We have to sanity check the raw data, otherwise doom looms for |
| 3275 | some malformed UCS-4 data. */ |
| 3276 | if ( |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3277 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3278 | *p > unimax || *p < 0 || |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3279 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3280 | end-s < Py_UNICODE_SIZE |
| 3281 | ) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3282 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3283 | startinpos = s - starts; |
| 3284 | if (end-s < Py_UNICODE_SIZE) { |
| 3285 | endinpos = end-starts; |
| 3286 | reason = "truncated input"; |
| 3287 | } |
| 3288 | else { |
| 3289 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 3290 | reason = "illegal code point (> 0x10FFFF)"; |
| 3291 | } |
| 3292 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 3293 | if (unicode_decode_call_errorhandler( |
| 3294 | errors, &errorHandler, |
| 3295 | "unicode_internal", reason, |
| 3296 | starts, size, &startinpos, &endinpos, &exc, &s, |
Benjamin Peterson | 828a706 | 2008-12-27 17:05:29 +0000 | [diff] [blame] | 3297 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3298 | goto onError; |
| 3299 | } |
| 3300 | } |
| 3301 | else { |
| 3302 | p++; |
| 3303 | s += Py_UNICODE_SIZE; |
| 3304 | } |
| 3305 | } |
| 3306 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3307 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3308 | goto onError; |
| 3309 | Py_XDECREF(errorHandler); |
| 3310 | Py_XDECREF(exc); |
| 3311 | return (PyObject *)v; |
| 3312 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3313 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3314 | Py_XDECREF(v); |
| 3315 | Py_XDECREF(errorHandler); |
| 3316 | Py_XDECREF(exc); |
| 3317 | return NULL; |
| 3318 | } |
| 3319 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3320 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 3321 | |
| 3322 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3323 | Py_ssize_t size, |
| 3324 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3325 | { |
| 3326 | PyUnicodeObject *v; |
| 3327 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3328 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3329 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3330 | if (size == 1) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3331 | Py_UNICODE r = *(unsigned char*)s; |
| 3332 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3333 | } |
| 3334 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3335 | v = _PyUnicode_New(size); |
| 3336 | if (v == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3337 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3338 | if (size == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3339 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3340 | p = PyUnicode_AS_UNICODE(v); |
| 3341 | while (size-- > 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3342 | *p++ = (unsigned char)*s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3343 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3344 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3345 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3346 | Py_XDECREF(v); |
| 3347 | return NULL; |
| 3348 | } |
| 3349 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3350 | /* create or adjust a UnicodeEncodeError */ |
| 3351 | static void make_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3352 | const char *encoding, |
| 3353 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3354 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3355 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3356 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3357 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3358 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 3359 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3360 | } |
| 3361 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3362 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 3363 | goto onError; |
| 3364 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 3365 | goto onError; |
| 3366 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 3367 | goto onError; |
| 3368 | return; |
| 3369 | onError: |
| 3370 | Py_DECREF(*exceptionObject); |
| 3371 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3372 | } |
| 3373 | } |
| 3374 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3375 | /* raises a UnicodeEncodeError */ |
| 3376 | static void raise_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3377 | const char *encoding, |
| 3378 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3379 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3380 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3381 | { |
| 3382 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3383 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3384 | if (*exceptionObject != NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3385 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3386 | } |
| 3387 | |
| 3388 | /* error handling callback helper: |
| 3389 | build arguments, call the callback and check the arguments, |
| 3390 | put the result into newpos and return the replacement string, which |
| 3391 | has to be freed by the caller */ |
| 3392 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3393 | PyObject **errorHandler, |
| 3394 | const char *encoding, const char *reason, |
| 3395 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 3396 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3397 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3398 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3399 | 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] | 3400 | |
| 3401 | PyObject *restuple; |
| 3402 | PyObject *resunicode; |
| 3403 | |
| 3404 | if (*errorHandler == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3405 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3406 | if (*errorHandler == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3407 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3408 | } |
| 3409 | |
| 3410 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3411 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3412 | if (*exceptionObject == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3413 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3414 | |
| 3415 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3416 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3417 | if (restuple == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3418 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3419 | if (!PyTuple_Check(restuple)) { |
Georg Brandl | 40e15ed | 2009-04-05 21:48:06 +0000 | [diff] [blame] | 3420 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3421 | Py_DECREF(restuple); |
| 3422 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3423 | } |
| 3424 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3425 | &resunicode, newpos)) { |
| 3426 | Py_DECREF(restuple); |
| 3427 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3428 | } |
| 3429 | if (*newpos<0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3430 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3431 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3432 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 3433 | Py_DECREF(restuple); |
| 3434 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3435 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3436 | Py_INCREF(resunicode); |
| 3437 | Py_DECREF(restuple); |
| 3438 | return resunicode; |
| 3439 | } |
| 3440 | |
| 3441 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3442 | Py_ssize_t size, |
| 3443 | const char *errors, |
| 3444 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3445 | { |
| 3446 | /* output object */ |
| 3447 | PyObject *res; |
| 3448 | /* pointers to the beginning and end+1 of input */ |
| 3449 | const Py_UNICODE *startp = p; |
| 3450 | const Py_UNICODE *endp = p + size; |
| 3451 | /* pointer to the beginning of the unencodable characters */ |
| 3452 | /* const Py_UNICODE *badp = NULL; */ |
| 3453 | /* pointer into the output */ |
| 3454 | char *str; |
| 3455 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3456 | Py_ssize_t respos = 0; |
| 3457 | Py_ssize_t ressize; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3458 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 3459 | 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] | 3460 | PyObject *errorHandler = NULL; |
| 3461 | PyObject *exc = NULL; |
| 3462 | /* the following variable is used for caching string comparisons |
| 3463 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 3464 | int known_errorHandler = -1; |
| 3465 | |
| 3466 | /* allocate enough for a simple encoding without |
| 3467 | replacements, if we need more, we'll resize */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3468 | res = PyString_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3469 | if (res == NULL) |
| 3470 | goto onError; |
| 3471 | if (size == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3472 | return res; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3473 | str = PyString_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3474 | ressize = size; |
| 3475 | |
| 3476 | while (p<endp) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3477 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3478 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3479 | /* can we encode this? */ |
| 3480 | if (c<limit) { |
| 3481 | /* no overflow check, because we know that the space is enough */ |
| 3482 | *str++ = (char)c; |
| 3483 | ++p; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 3484 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3485 | else { |
| 3486 | Py_ssize_t unicodepos = p-startp; |
| 3487 | Py_ssize_t requiredsize; |
| 3488 | PyObject *repunicode; |
| 3489 | Py_ssize_t repsize; |
| 3490 | Py_ssize_t newpos; |
| 3491 | Py_ssize_t respos; |
| 3492 | Py_UNICODE *uni2; |
| 3493 | /* startpos for collecting unencodable chars */ |
| 3494 | const Py_UNICODE *collstart = p; |
| 3495 | const Py_UNICODE *collend = p; |
| 3496 | /* find all unecodable characters */ |
| 3497 | while ((collend < endp) && ((*collend)>=limit)) |
| 3498 | ++collend; |
| 3499 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 3500 | if (known_errorHandler==-1) { |
| 3501 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3502 | known_errorHandler = 1; |
| 3503 | else if (!strcmp(errors, "replace")) |
| 3504 | known_errorHandler = 2; |
| 3505 | else if (!strcmp(errors, "ignore")) |
| 3506 | known_errorHandler = 3; |
| 3507 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3508 | known_errorHandler = 4; |
| 3509 | else |
| 3510 | known_errorHandler = 0; |
| 3511 | } |
| 3512 | switch (known_errorHandler) { |
| 3513 | case 1: /* strict */ |
| 3514 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 3515 | goto onError; |
| 3516 | case 2: /* replace */ |
| 3517 | while (collstart++<collend) |
| 3518 | *str++ = '?'; /* fall through */ |
| 3519 | case 3: /* ignore */ |
| 3520 | p = collend; |
| 3521 | break; |
| 3522 | case 4: /* xmlcharrefreplace */ |
| 3523 | respos = str-PyString_AS_STRING(res); |
| 3524 | /* determine replacement size (temporarily (mis)uses p) */ |
| 3525 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 3526 | if (*p<10) |
| 3527 | repsize += 2+1+1; |
| 3528 | else if (*p<100) |
| 3529 | repsize += 2+2+1; |
| 3530 | else if (*p<1000) |
| 3531 | repsize += 2+3+1; |
| 3532 | else if (*p<10000) |
| 3533 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 3534 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3535 | else |
| 3536 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 3537 | #else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3538 | else if (*p<100000) |
| 3539 | repsize += 2+5+1; |
| 3540 | else if (*p<1000000) |
| 3541 | repsize += 2+6+1; |
| 3542 | else |
| 3543 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3544 | #endif |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3545 | } |
| 3546 | requiredsize = respos+repsize+(endp-collend); |
| 3547 | if (requiredsize > ressize) { |
| 3548 | if (requiredsize<2*ressize) |
| 3549 | requiredsize = 2*ressize; |
| 3550 | if (_PyString_Resize(&res, requiredsize)) |
| 3551 | goto onError; |
| 3552 | str = PyString_AS_STRING(res) + respos; |
| 3553 | ressize = requiredsize; |
| 3554 | } |
| 3555 | /* generate replacement (temporarily (mis)uses p) */ |
| 3556 | for (p = collstart; p < collend; ++p) { |
| 3557 | str += sprintf(str, "&#%d;", (int)*p); |
| 3558 | } |
| 3559 | p = collend; |
| 3560 | break; |
| 3561 | default: |
| 3562 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 3563 | encoding, reason, startp, size, &exc, |
| 3564 | collstart-startp, collend-startp, &newpos); |
| 3565 | if (repunicode == NULL) |
| 3566 | goto onError; |
| 3567 | /* need more space? (at least enough for what we |
| 3568 | have+the replacement+the rest of the string, so |
| 3569 | we won't have to check space for encodable characters) */ |
| 3570 | respos = str-PyString_AS_STRING(res); |
| 3571 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3572 | requiredsize = respos+repsize+(endp-collend); |
| 3573 | if (requiredsize > ressize) { |
| 3574 | if (requiredsize<2*ressize) |
| 3575 | requiredsize = 2*ressize; |
| 3576 | if (_PyString_Resize(&res, requiredsize)) { |
| 3577 | Py_DECREF(repunicode); |
| 3578 | goto onError; |
| 3579 | } |
| 3580 | str = PyString_AS_STRING(res) + respos; |
| 3581 | ressize = requiredsize; |
| 3582 | } |
| 3583 | /* check if there is anything unencodable in the replacement |
| 3584 | and copy it to the output */ |
| 3585 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 3586 | c = *uni2; |
| 3587 | if (c >= limit) { |
| 3588 | raise_encode_exception(&exc, encoding, startp, size, |
| 3589 | unicodepos, unicodepos+1, reason); |
| 3590 | Py_DECREF(repunicode); |
| 3591 | goto onError; |
| 3592 | } |
| 3593 | *str = (char)c; |
| 3594 | } |
| 3595 | p = startp + newpos; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 3596 | Py_DECREF(repunicode); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 3597 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 3598 | } |
| 3599 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3600 | /* Resize if we allocated to much */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3601 | respos = str-PyString_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3602 | if (respos<ressize) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3603 | /* If this falls res will be NULL */ |
| 3604 | _PyString_Resize(&res, respos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3605 | Py_XDECREF(errorHandler); |
| 3606 | Py_XDECREF(exc); |
| 3607 | return res; |
| 3608 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3609 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3610 | Py_XDECREF(res); |
| 3611 | Py_XDECREF(errorHandler); |
| 3612 | Py_XDECREF(exc); |
| 3613 | return NULL; |
| 3614 | } |
| 3615 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3616 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3617 | Py_ssize_t size, |
| 3618 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3619 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3620 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3621 | } |
| 3622 | |
| 3623 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 3624 | { |
| 3625 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3626 | PyErr_BadArgument(); |
| 3627 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3628 | } |
| 3629 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3630 | PyUnicode_GET_SIZE(unicode), |
| 3631 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3632 | } |
| 3633 | |
| 3634 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 3635 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3636 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3637 | Py_ssize_t size, |
| 3638 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3639 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3640 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3641 | PyUnicodeObject *v; |
| 3642 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3643 | Py_ssize_t startinpos; |
| 3644 | Py_ssize_t endinpos; |
| 3645 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3646 | const char *e; |
| 3647 | PyObject *errorHandler = NULL; |
| 3648 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3649 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3650 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3651 | if (size == 1 && *(unsigned char*)s < 128) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3652 | Py_UNICODE r = *(unsigned char*)s; |
| 3653 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3654 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3655 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3656 | v = _PyUnicode_New(size); |
| 3657 | if (v == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3658 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3659 | if (size == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3660 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3661 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3662 | e = s + size; |
| 3663 | while (s < e) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3664 | register unsigned char c = (unsigned char)*s; |
| 3665 | if (c < 128) { |
| 3666 | *p++ = c; |
| 3667 | ++s; |
| 3668 | } |
| 3669 | else { |
| 3670 | startinpos = s-starts; |
| 3671 | endinpos = startinpos + 1; |
| 3672 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
| 3673 | if (unicode_decode_call_errorhandler( |
| 3674 | errors, &errorHandler, |
| 3675 | "ascii", "ordinal not in range(128)", |
| 3676 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3677 | &v, &outpos, &p)) |
| 3678 | goto onError; |
| 3679 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3680 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3681 | if (p - PyUnicode_AS_UNICODE(v) < PyString_GET_SIZE(v)) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3682 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 3683 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3684 | Py_XDECREF(errorHandler); |
| 3685 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3686 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3687 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3688 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3689 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3690 | Py_XDECREF(errorHandler); |
| 3691 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3692 | return NULL; |
| 3693 | } |
| 3694 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3695 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3696 | Py_ssize_t size, |
| 3697 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3698 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3699 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3700 | } |
| 3701 | |
| 3702 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 3703 | { |
| 3704 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3705 | PyErr_BadArgument(); |
| 3706 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3707 | } |
| 3708 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3709 | PyUnicode_GET_SIZE(unicode), |
| 3710 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3711 | } |
| 3712 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3713 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3714 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3715 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3716 | |
Hirokazu Yamamoto | 68e075e | 2009-03-21 13:04:41 +0000 | [diff] [blame] | 3717 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3718 | #define NEED_RETRY |
| 3719 | #endif |
| 3720 | |
| 3721 | /* XXX This code is limited to "true" double-byte encodings, as |
| 3722 | a) it assumes an incomplete character consists of a single byte, and |
| 3723 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3724 | encodings, see IsDBCSLeadByteEx documentation. */ |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3725 | |
| 3726 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 3727 | { |
| 3728 | const char *curr = s + offset; |
| 3729 | |
| 3730 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3731 | const char *prev = CharPrev(s, curr); |
| 3732 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3733 | } |
| 3734 | return 0; |
| 3735 | } |
| 3736 | |
| 3737 | /* |
| 3738 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 3739 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 3740 | */ |
| 3741 | static int decode_mbcs(PyUnicodeObject **v, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3742 | const char *s, /* MBCS string */ |
| 3743 | int size, /* sizeof MBCS string */ |
| 3744 | int final) |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3745 | { |
| 3746 | Py_UNICODE *p; |
| 3747 | Py_ssize_t n = 0; |
| 3748 | int usize = 0; |
| 3749 | |
| 3750 | assert(size >= 0); |
| 3751 | |
| 3752 | /* Skip trailing lead-byte unless 'final' is set */ |
| 3753 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3754 | --size; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3755 | |
| 3756 | /* First get the size of the result */ |
| 3757 | if (size > 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3758 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 3759 | if (usize == 0) { |
| 3760 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3761 | return -1; |
| 3762 | } |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3763 | } |
| 3764 | |
| 3765 | if (*v == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3766 | /* Create unicode object */ |
| 3767 | *v = _PyUnicode_New(usize); |
| 3768 | if (*v == NULL) |
| 3769 | return -1; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3770 | } |
| 3771 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3772 | /* Extend unicode object */ |
| 3773 | n = PyUnicode_GET_SIZE(*v); |
| 3774 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 3775 | return -1; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3776 | } |
| 3777 | |
| 3778 | /* Do the conversion */ |
| 3779 | if (size > 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3780 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 3781 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 3782 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3783 | return -1; |
| 3784 | } |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3785 | } |
| 3786 | |
| 3787 | return size; |
| 3788 | } |
| 3789 | |
| 3790 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3791 | Py_ssize_t size, |
| 3792 | const char *errors, |
| 3793 | Py_ssize_t *consumed) |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3794 | { |
| 3795 | PyUnicodeObject *v = NULL; |
| 3796 | int done; |
| 3797 | |
| 3798 | if (consumed) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3799 | *consumed = 0; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3800 | |
| 3801 | #ifdef NEED_RETRY |
| 3802 | retry: |
| 3803 | if (size > INT_MAX) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3804 | done = decode_mbcs(&v, s, INT_MAX, 0); |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3805 | else |
| 3806 | #endif |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3807 | done = decode_mbcs(&v, s, (int)size, !consumed); |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3808 | |
| 3809 | if (done < 0) { |
| 3810 | Py_XDECREF(v); |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3811 | return NULL; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3812 | } |
| 3813 | |
| 3814 | if (consumed) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3815 | *consumed += done; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3816 | |
| 3817 | #ifdef NEED_RETRY |
| 3818 | if (size > INT_MAX) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3819 | s += done; |
| 3820 | size -= done; |
| 3821 | goto retry; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3822 | } |
| 3823 | #endif |
| 3824 | |
| 3825 | return (PyObject *)v; |
| 3826 | } |
| 3827 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3828 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3829 | Py_ssize_t size, |
| 3830 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3831 | { |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3832 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 3833 | } |
| 3834 | |
| 3835 | /* |
| 3836 | * Convert unicode into string object (MBCS). |
| 3837 | * Returns 0 if succeed, -1 otherwise. |
| 3838 | */ |
| 3839 | static int encode_mbcs(PyObject **repr, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3840 | const Py_UNICODE *p, /* unicode */ |
| 3841 | int size) /* size of unicode */ |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3842 | { |
| 3843 | int mbcssize = 0; |
| 3844 | Py_ssize_t n = 0; |
| 3845 | |
| 3846 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3847 | |
| 3848 | /* First get the size of the result */ |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3849 | if (size > 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3850 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 3851 | if (mbcssize == 0) { |
| 3852 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3853 | return -1; |
| 3854 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3855 | } |
| 3856 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3857 | if (*repr == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3858 | /* Create string object */ |
| 3859 | *repr = PyString_FromStringAndSize(NULL, mbcssize); |
| 3860 | if (*repr == NULL) |
| 3861 | return -1; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3862 | } |
| 3863 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3864 | /* Extend string object */ |
| 3865 | n = PyString_Size(*repr); |
| 3866 | if (_PyString_Resize(repr, n + mbcssize) < 0) |
| 3867 | return -1; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3868 | } |
| 3869 | |
| 3870 | /* Do the conversion */ |
| 3871 | if (size > 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3872 | char *s = PyString_AS_STRING(*repr) + n; |
| 3873 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 3874 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3875 | return -1; |
| 3876 | } |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3877 | } |
| 3878 | |
| 3879 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3880 | } |
| 3881 | |
| 3882 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3883 | Py_ssize_t size, |
| 3884 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3885 | { |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3886 | PyObject *repr = NULL; |
| 3887 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 3888 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3889 | #ifdef NEED_RETRY |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3890 | retry: |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3891 | if (size > INT_MAX) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3892 | ret = encode_mbcs(&repr, p, INT_MAX); |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3893 | else |
| 3894 | #endif |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3895 | ret = encode_mbcs(&repr, p, (int)size); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3896 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3897 | if (ret < 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3898 | Py_XDECREF(repr); |
| 3899 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3900 | } |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3901 | |
| 3902 | #ifdef NEED_RETRY |
| 3903 | if (size > INT_MAX) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3904 | p += INT_MAX; |
| 3905 | size -= INT_MAX; |
| 3906 | goto retry; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3907 | } |
| 3908 | #endif |
| 3909 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3910 | return repr; |
| 3911 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3912 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3913 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 3914 | { |
| 3915 | if (!PyUnicode_Check(unicode)) { |
| 3916 | PyErr_BadArgument(); |
| 3917 | return NULL; |
| 3918 | } |
| 3919 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3920 | PyUnicode_GET_SIZE(unicode), |
| 3921 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3922 | } |
| 3923 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3924 | #undef NEED_RETRY |
| 3925 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3926 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3927 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3928 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 3929 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3930 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3931 | Py_ssize_t size, |
| 3932 | PyObject *mapping, |
| 3933 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3934 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3935 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3936 | Py_ssize_t startinpos; |
| 3937 | Py_ssize_t endinpos; |
| 3938 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3939 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3940 | PyUnicodeObject *v; |
| 3941 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3942 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3943 | PyObject *errorHandler = NULL; |
| 3944 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3945 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3946 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3947 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3948 | /* Default to Latin-1 */ |
| 3949 | if (mapping == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3950 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3951 | |
| 3952 | v = _PyUnicode_New(size); |
| 3953 | if (v == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3954 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3955 | if (size == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3956 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3957 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3958 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3959 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3960 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 3961 | maplen = PyUnicode_GET_SIZE(mapping); |
| 3962 | while (s < e) { |
| 3963 | unsigned char ch = *s; |
| 3964 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3965 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3966 | if (ch < maplen) |
| 3967 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3968 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3969 | if (x == 0xfffe) { |
| 3970 | /* undefined mapping */ |
| 3971 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3972 | startinpos = s-starts; |
| 3973 | endinpos = startinpos+1; |
| 3974 | if (unicode_decode_call_errorhandler( |
| 3975 | errors, &errorHandler, |
| 3976 | "charmap", "character maps to <undefined>", |
| 3977 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3978 | &v, &outpos, &p)) { |
| 3979 | goto onError; |
| 3980 | } |
| 3981 | continue; |
| 3982 | } |
| 3983 | *p++ = x; |
| 3984 | ++s; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 3985 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3986 | } |
| 3987 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3988 | while (s < e) { |
| 3989 | unsigned char ch = *s; |
| 3990 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3991 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3992 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 3993 | w = PyInt_FromLong((long)ch); |
| 3994 | if (w == NULL) |
| 3995 | goto onError; |
| 3996 | x = PyObject_GetItem(mapping, w); |
| 3997 | Py_DECREF(w); |
| 3998 | if (x == NULL) { |
| 3999 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4000 | /* No mapping found means: mapping is undefined. */ |
| 4001 | PyErr_Clear(); |
| 4002 | x = Py_None; |
| 4003 | Py_INCREF(x); |
| 4004 | } else |
| 4005 | goto onError; |
| 4006 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4007 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4008 | /* Apply mapping */ |
| 4009 | if (PyInt_Check(x)) { |
| 4010 | long value = PyInt_AS_LONG(x); |
| 4011 | if (value < 0 || value > 65535) { |
| 4012 | PyErr_SetString(PyExc_TypeError, |
| 4013 | "character mapping must be in range(65536)"); |
| 4014 | Py_DECREF(x); |
| 4015 | goto onError; |
| 4016 | } |
| 4017 | *p++ = (Py_UNICODE)value; |
| 4018 | } |
| 4019 | else if (x == Py_None) { |
| 4020 | /* undefined mapping */ |
| 4021 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4022 | startinpos = s-starts; |
| 4023 | endinpos = startinpos+1; |
| 4024 | if (unicode_decode_call_errorhandler( |
| 4025 | errors, &errorHandler, |
| 4026 | "charmap", "character maps to <undefined>", |
| 4027 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 4028 | &v, &outpos, &p)) { |
| 4029 | Py_DECREF(x); |
| 4030 | goto onError; |
| 4031 | } |
| 4032 | Py_DECREF(x); |
| 4033 | continue; |
| 4034 | } |
| 4035 | else if (PyUnicode_Check(x)) { |
| 4036 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4037 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4038 | if (targetsize == 1) |
| 4039 | /* 1-1 mapping */ |
| 4040 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4041 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4042 | else if (targetsize > 1) { |
| 4043 | /* 1-n mapping */ |
| 4044 | if (targetsize > extrachars) { |
| 4045 | /* resize first */ |
| 4046 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 4047 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 4048 | (targetsize << 2); |
| 4049 | extrachars += needed; |
| 4050 | /* XXX overflow detection missing */ |
| 4051 | if (_PyUnicode_Resize(&v, |
| 4052 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 4053 | Py_DECREF(x); |
| 4054 | goto onError; |
| 4055 | } |
| 4056 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 4057 | } |
| 4058 | Py_UNICODE_COPY(p, |
| 4059 | PyUnicode_AS_UNICODE(x), |
| 4060 | targetsize); |
| 4061 | p += targetsize; |
| 4062 | extrachars -= targetsize; |
| 4063 | } |
| 4064 | /* 1-0 mapping: skip the character */ |
| 4065 | } |
| 4066 | else { |
| 4067 | /* wrong return value */ |
| 4068 | PyErr_SetString(PyExc_TypeError, |
| 4069 | "character mapping must return integer, None or unicode"); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4070 | Py_DECREF(x); |
| 4071 | goto onError; |
| 4072 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4073 | Py_DECREF(x); |
| 4074 | ++s; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4075 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4076 | } |
| 4077 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4078 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4079 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4080 | Py_XDECREF(errorHandler); |
| 4081 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4082 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4083 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4084 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4085 | Py_XDECREF(errorHandler); |
| 4086 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4087 | Py_XDECREF(v); |
| 4088 | return NULL; |
| 4089 | } |
| 4090 | |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4091 | /* Charmap encoding: the lookup table */ |
| 4092 | |
| 4093 | struct encoding_map{ |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4094 | PyObject_HEAD |
| 4095 | unsigned char level1[32]; |
| 4096 | int count2, count3; |
| 4097 | unsigned char level23[1]; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4098 | }; |
| 4099 | |
| 4100 | static PyObject* |
| 4101 | encoding_map_size(PyObject *obj, PyObject* args) |
| 4102 | { |
| 4103 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4104 | return PyInt_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4105 | 128*map->count3); |
| 4106 | } |
| 4107 | |
| 4108 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4109 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4110 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 4111 | { 0 } |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4112 | }; |
| 4113 | |
| 4114 | static void |
| 4115 | encoding_map_dealloc(PyObject* o) |
| 4116 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4117 | PyObject_FREE(o); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4118 | } |
| 4119 | |
| 4120 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4121 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4122 | "EncodingMap", /*tp_name*/ |
| 4123 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 4124 | 0, /*tp_itemsize*/ |
| 4125 | /* methods */ |
| 4126 | encoding_map_dealloc, /*tp_dealloc*/ |
| 4127 | 0, /*tp_print*/ |
| 4128 | 0, /*tp_getattr*/ |
| 4129 | 0, /*tp_setattr*/ |
| 4130 | 0, /*tp_compare*/ |
| 4131 | 0, /*tp_repr*/ |
| 4132 | 0, /*tp_as_number*/ |
| 4133 | 0, /*tp_as_sequence*/ |
| 4134 | 0, /*tp_as_mapping*/ |
| 4135 | 0, /*tp_hash*/ |
| 4136 | 0, /*tp_call*/ |
| 4137 | 0, /*tp_str*/ |
| 4138 | 0, /*tp_getattro*/ |
| 4139 | 0, /*tp_setattro*/ |
| 4140 | 0, /*tp_as_buffer*/ |
| 4141 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 4142 | 0, /*tp_doc*/ |
| 4143 | 0, /*tp_traverse*/ |
| 4144 | 0, /*tp_clear*/ |
| 4145 | 0, /*tp_richcompare*/ |
| 4146 | 0, /*tp_weaklistoffset*/ |
| 4147 | 0, /*tp_iter*/ |
| 4148 | 0, /*tp_iternext*/ |
| 4149 | encoding_map_methods, /*tp_methods*/ |
| 4150 | 0, /*tp_members*/ |
| 4151 | 0, /*tp_getset*/ |
| 4152 | 0, /*tp_base*/ |
| 4153 | 0, /*tp_dict*/ |
| 4154 | 0, /*tp_descr_get*/ |
| 4155 | 0, /*tp_descr_set*/ |
| 4156 | 0, /*tp_dictoffset*/ |
| 4157 | 0, /*tp_init*/ |
| 4158 | 0, /*tp_alloc*/ |
| 4159 | 0, /*tp_new*/ |
| 4160 | 0, /*tp_free*/ |
| 4161 | 0, /*tp_is_gc*/ |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4162 | }; |
| 4163 | |
| 4164 | PyObject* |
| 4165 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 4166 | { |
| 4167 | Py_UNICODE *decode; |
| 4168 | PyObject *result; |
| 4169 | struct encoding_map *mresult; |
| 4170 | int i; |
| 4171 | int need_dict = 0; |
| 4172 | unsigned char level1[32]; |
| 4173 | unsigned char level2[512]; |
| 4174 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 4175 | int count2 = 0, count3 = 0; |
| 4176 | |
| 4177 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 4178 | PyErr_BadArgument(); |
| 4179 | return NULL; |
| 4180 | } |
| 4181 | decode = PyUnicode_AS_UNICODE(string); |
| 4182 | memset(level1, 0xFF, sizeof level1); |
| 4183 | memset(level2, 0xFF, sizeof level2); |
| 4184 | |
| 4185 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 4186 | or if there are non-BMP characters, we need to use |
| 4187 | a mapping dictionary. */ |
| 4188 | if (decode[0] != 0) |
| 4189 | need_dict = 1; |
| 4190 | for (i = 1; i < 256; i++) { |
| 4191 | int l1, l2; |
| 4192 | if (decode[i] == 0 |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4193 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4194 | || decode[i] > 0xFFFF |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4195 | #endif |
| 4196 | ) { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4197 | need_dict = 1; |
| 4198 | break; |
| 4199 | } |
| 4200 | if (decode[i] == 0xFFFE) |
| 4201 | /* unmapped character */ |
| 4202 | continue; |
| 4203 | l1 = decode[i] >> 11; |
| 4204 | l2 = decode[i] >> 7; |
| 4205 | if (level1[l1] == 0xFF) |
| 4206 | level1[l1] = count2++; |
| 4207 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4208 | level2[l2] = count3++; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4209 | } |
| 4210 | |
| 4211 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 4212 | need_dict = 1; |
| 4213 | |
| 4214 | if (need_dict) { |
| 4215 | PyObject *result = PyDict_New(); |
| 4216 | PyObject *key, *value; |
| 4217 | if (!result) |
| 4218 | return NULL; |
| 4219 | for (i = 0; i < 256; i++) { |
| 4220 | key = value = NULL; |
| 4221 | key = PyInt_FromLong(decode[i]); |
| 4222 | value = PyInt_FromLong(i); |
| 4223 | if (!key || !value) |
| 4224 | goto failed1; |
| 4225 | if (PyDict_SetItem(result, key, value) == -1) |
| 4226 | goto failed1; |
Georg Brandl | 9f16760 | 2006-06-04 21:46:16 +0000 | [diff] [blame] | 4227 | Py_DECREF(key); |
| 4228 | Py_DECREF(value); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4229 | } |
| 4230 | return result; |
| 4231 | failed1: |
| 4232 | Py_XDECREF(key); |
| 4233 | Py_XDECREF(value); |
| 4234 | Py_DECREF(result); |
| 4235 | return NULL; |
| 4236 | } |
| 4237 | |
| 4238 | /* Create a three-level trie */ |
| 4239 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 4240 | 16*count2 + 128*count3 - 1); |
| 4241 | if (!result) |
| 4242 | return PyErr_NoMemory(); |
| 4243 | PyObject_Init(result, &EncodingMapType); |
| 4244 | mresult = (struct encoding_map*)result; |
| 4245 | mresult->count2 = count2; |
| 4246 | mresult->count3 = count3; |
| 4247 | mlevel1 = mresult->level1; |
| 4248 | mlevel2 = mresult->level23; |
| 4249 | mlevel3 = mresult->level23 + 16*count2; |
| 4250 | memcpy(mlevel1, level1, 32); |
| 4251 | memset(mlevel2, 0xFF, 16*count2); |
| 4252 | memset(mlevel3, 0, 128*count3); |
| 4253 | count3 = 0; |
| 4254 | for (i = 1; i < 256; i++) { |
| 4255 | int o1, o2, o3, i2, i3; |
| 4256 | if (decode[i] == 0xFFFE) |
| 4257 | /* unmapped character */ |
| 4258 | continue; |
| 4259 | o1 = decode[i]>>11; |
| 4260 | o2 = (decode[i]>>7) & 0xF; |
| 4261 | i2 = 16*mlevel1[o1] + o2; |
| 4262 | if (mlevel2[i2] == 0xFF) |
| 4263 | mlevel2[i2] = count3++; |
| 4264 | o3 = decode[i] & 0x7F; |
| 4265 | i3 = 128*mlevel2[i2] + o3; |
| 4266 | mlevel3[i3] = i; |
| 4267 | } |
| 4268 | return result; |
| 4269 | } |
| 4270 | |
| 4271 | static int |
| 4272 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 4273 | { |
| 4274 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 4275 | int l1 = c>>11; |
| 4276 | int l2 = (c>>7) & 0xF; |
| 4277 | int l3 = c & 0x7F; |
| 4278 | int i; |
| 4279 | |
| 4280 | #ifdef Py_UNICODE_WIDE |
| 4281 | if (c > 0xFFFF) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4282 | return -1; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4283 | } |
| 4284 | #endif |
| 4285 | if (c == 0) |
| 4286 | return 0; |
| 4287 | /* level 1*/ |
| 4288 | i = map->level1[l1]; |
| 4289 | if (i == 0xFF) { |
| 4290 | return -1; |
| 4291 | } |
| 4292 | /* level 2*/ |
| 4293 | i = map->level23[16*i+l2]; |
| 4294 | if (i == 0xFF) { |
| 4295 | return -1; |
| 4296 | } |
| 4297 | /* level 3 */ |
| 4298 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 4299 | if (i == 0) { |
| 4300 | return -1; |
| 4301 | } |
| 4302 | return i; |
| 4303 | } |
| 4304 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4305 | /* Lookup the character ch in the mapping. If the character |
| 4306 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4307 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4308 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4309 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4310 | PyObject *w = PyInt_FromLong((long)c); |
| 4311 | PyObject *x; |
| 4312 | |
| 4313 | if (w == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4314 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4315 | x = PyObject_GetItem(mapping, w); |
| 4316 | Py_DECREF(w); |
| 4317 | if (x == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4318 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4319 | /* No mapping found means: mapping is undefined. */ |
| 4320 | PyErr_Clear(); |
| 4321 | x = Py_None; |
| 4322 | Py_INCREF(x); |
| 4323 | return x; |
| 4324 | } else |
| 4325 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4326 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 4327 | else if (x == Py_None) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4328 | return x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4329 | else if (PyInt_Check(x)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4330 | long value = PyInt_AS_LONG(x); |
| 4331 | if (value < 0 || value > 255) { |
| 4332 | PyErr_SetString(PyExc_TypeError, |
| 4333 | "character mapping must be in range(256)"); |
| 4334 | Py_DECREF(x); |
| 4335 | return NULL; |
| 4336 | } |
| 4337 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4338 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4339 | else if (PyString_Check(x)) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4340 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4341 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4342 | /* wrong return value */ |
| 4343 | PyErr_SetString(PyExc_TypeError, |
| 4344 | "character mapping must return integer, None or str"); |
| 4345 | Py_DECREF(x); |
| 4346 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4347 | } |
| 4348 | } |
| 4349 | |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4350 | static int |
| 4351 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
| 4352 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4353 | Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
| 4354 | /* exponentially overallocate to minimize reallocations */ |
| 4355 | if (requiredsize < 2*outsize) |
| 4356 | requiredsize = 2*outsize; |
| 4357 | if (_PyString_Resize(outobj, requiredsize)) { |
| 4358 | return 0; |
| 4359 | } |
| 4360 | return 1; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4361 | } |
| 4362 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4363 | typedef enum charmapencode_result { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4364 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4365 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4366 | /* lookup the character, put the result in the output string and adjust |
| 4367 | various state variables. Reallocate the output string if not enough |
| 4368 | space is available. Return a new reference to the object that |
| 4369 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 4370 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 4371 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4372 | static |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4373 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4374 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4375 | { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4376 | PyObject *rep; |
| 4377 | char *outstart; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4378 | Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4379 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4380 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4381 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4382 | Py_ssize_t requiredsize = *outpos+1; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4383 | if (res == -1) |
| 4384 | return enc_FAILED; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4385 | if (outsize<requiredsize) |
| 4386 | if (!charmapencode_resize(outobj, outpos, requiredsize)) |
| 4387 | return enc_EXCEPTION; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4388 | outstart = PyString_AS_STRING(*outobj); |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4389 | outstart[(*outpos)++] = (char)res; |
| 4390 | return enc_SUCCESS; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4391 | } |
| 4392 | |
| 4393 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4394 | if (rep==NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4395 | return enc_EXCEPTION; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4396 | else if (rep==Py_None) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4397 | Py_DECREF(rep); |
| 4398 | return enc_FAILED; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4399 | } else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4400 | if (PyInt_Check(rep)) { |
| 4401 | Py_ssize_t requiredsize = *outpos+1; |
| 4402 | if (outsize<requiredsize) |
| 4403 | if (!charmapencode_resize(outobj, outpos, requiredsize)) { |
| 4404 | Py_DECREF(rep); |
| 4405 | return enc_EXCEPTION; |
| 4406 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4407 | outstart = PyString_AS_STRING(*outobj); |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4408 | outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4409 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4410 | else { |
| 4411 | const char *repchars = PyString_AS_STRING(rep); |
| 4412 | Py_ssize_t repsize = PyString_GET_SIZE(rep); |
| 4413 | Py_ssize_t requiredsize = *outpos+repsize; |
| 4414 | if (outsize<requiredsize) |
| 4415 | if (!charmapencode_resize(outobj, outpos, requiredsize)) { |
| 4416 | Py_DECREF(rep); |
| 4417 | return enc_EXCEPTION; |
| 4418 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4419 | outstart = PyString_AS_STRING(*outobj); |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4420 | memcpy(outstart + *outpos, repchars, repsize); |
| 4421 | *outpos += repsize; |
| 4422 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4423 | } |
Georg Brandl | 9f16760 | 2006-06-04 21:46:16 +0000 | [diff] [blame] | 4424 | Py_DECREF(rep); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4425 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4426 | } |
| 4427 | |
| 4428 | /* handle an error in PyUnicode_EncodeCharmap |
| 4429 | Return 0 on success, -1 on error */ |
| 4430 | static |
| 4431 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4432 | 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] | 4433 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4434 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4435 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4436 | { |
| 4437 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4438 | Py_ssize_t repsize; |
| 4439 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4440 | Py_UNICODE *uni2; |
| 4441 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4442 | Py_ssize_t collstartpos = *inpos; |
| 4443 | Py_ssize_t collendpos = *inpos+1; |
| 4444 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4445 | char *encoding = "charmap"; |
| 4446 | char *reason = "character maps to <undefined>"; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4447 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4448 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4449 | /* find all unencodable characters */ |
| 4450 | while (collendpos < size) { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4451 | PyObject *rep; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4452 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4453 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 4454 | if (res != -1) |
| 4455 | break; |
| 4456 | ++collendpos; |
| 4457 | continue; |
| 4458 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4459 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4460 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 4461 | if (rep==NULL) |
| 4462 | return -1; |
| 4463 | else if (rep!=Py_None) { |
| 4464 | Py_DECREF(rep); |
| 4465 | break; |
| 4466 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4467 | Py_DECREF(rep); |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4468 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4469 | } |
| 4470 | /* cache callback name lookup |
| 4471 | * (if not done yet, i.e. it's the first error) */ |
| 4472 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4473 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4474 | *known_errorHandler = 1; |
| 4475 | else if (!strcmp(errors, "replace")) |
| 4476 | *known_errorHandler = 2; |
| 4477 | else if (!strcmp(errors, "ignore")) |
| 4478 | *known_errorHandler = 3; |
| 4479 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4480 | *known_errorHandler = 4; |
| 4481 | else |
| 4482 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4483 | } |
| 4484 | switch (*known_errorHandler) { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4485 | case 1: /* strict */ |
| 4486 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4487 | return -1; |
| 4488 | case 2: /* replace */ |
| 4489 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4490 | x = charmapencode_output('?', mapping, res, respos); |
| 4491 | if (x==enc_EXCEPTION) { |
| 4492 | return -1; |
| 4493 | } |
| 4494 | else if (x==enc_FAILED) { |
| 4495 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4496 | return -1; |
| 4497 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4498 | } |
| 4499 | /* fall through */ |
| 4500 | case 3: /* ignore */ |
| 4501 | *inpos = collendpos; |
| 4502 | break; |
| 4503 | case 4: /* xmlcharrefreplace */ |
| 4504 | /* generate replacement (temporarily (mis)uses p) */ |
| 4505 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4506 | char buffer[2+29+1+1]; |
| 4507 | char *cp; |
| 4508 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 4509 | for (cp = buffer; *cp; ++cp) { |
| 4510 | x = charmapencode_output(*cp, mapping, res, respos); |
| 4511 | if (x==enc_EXCEPTION) |
| 4512 | return -1; |
| 4513 | else if (x==enc_FAILED) { |
| 4514 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4515 | return -1; |
| 4516 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4517 | } |
| 4518 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4519 | *inpos = collendpos; |
| 4520 | break; |
| 4521 | default: |
| 4522 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4523 | encoding, reason, p, size, exceptionObject, |
| 4524 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4525 | if (repunicode == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4526 | return -1; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4527 | /* generate replacement */ |
| 4528 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4529 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4530 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 4531 | if (x==enc_EXCEPTION) { |
| 4532 | return -1; |
| 4533 | } |
| 4534 | else if (x==enc_FAILED) { |
| 4535 | Py_DECREF(repunicode); |
| 4536 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4537 | return -1; |
| 4538 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4539 | } |
| 4540 | *inpos = newpos; |
| 4541 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4542 | } |
| 4543 | return 0; |
| 4544 | } |
| 4545 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4546 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4547 | Py_ssize_t size, |
| 4548 | PyObject *mapping, |
| 4549 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4550 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4551 | /* output object */ |
| 4552 | PyObject *res = NULL; |
| 4553 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4554 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4555 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4556 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4557 | PyObject *errorHandler = NULL; |
| 4558 | PyObject *exc = NULL; |
| 4559 | /* the following variable is used for caching string comparisons |
| 4560 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4561 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4562 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4563 | |
| 4564 | /* Default to Latin-1 */ |
| 4565 | if (mapping == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4566 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4567 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4568 | /* allocate enough for a simple encoding without |
| 4569 | replacements, if we need more, we'll resize */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4570 | res = PyString_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4571 | if (res == NULL) |
| 4572 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 4573 | if (size == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4574 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4575 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4576 | while (inpos<size) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4577 | /* try to encode it */ |
| 4578 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 4579 | if (x==enc_EXCEPTION) /* error */ |
| 4580 | goto onError; |
| 4581 | if (x==enc_FAILED) { /* unencodable character */ |
| 4582 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 4583 | &exc, |
| 4584 | &known_errorHandler, &errorHandler, errors, |
| 4585 | &res, &respos)) { |
| 4586 | goto onError; |
| 4587 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4588 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4589 | else |
| 4590 | /* done with this character => adjust input position */ |
| 4591 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4592 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4593 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4594 | /* Resize if we allocated to much */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4595 | if (respos<PyString_GET_SIZE(res)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4596 | if (_PyString_Resize(&res, respos)) |
| 4597 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4598 | } |
| 4599 | Py_XDECREF(exc); |
| 4600 | Py_XDECREF(errorHandler); |
| 4601 | return res; |
| 4602 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4603 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4604 | Py_XDECREF(res); |
| 4605 | Py_XDECREF(exc); |
| 4606 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4607 | return NULL; |
| 4608 | } |
| 4609 | |
| 4610 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4611 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4612 | { |
| 4613 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4614 | PyErr_BadArgument(); |
| 4615 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4616 | } |
| 4617 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4618 | PyUnicode_GET_SIZE(unicode), |
| 4619 | mapping, |
| 4620 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4621 | } |
| 4622 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4623 | /* create or adjust a UnicodeTranslateError */ |
| 4624 | static void make_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4625 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4626 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4627 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4628 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4629 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4630 | *exceptionObject = PyUnicodeTranslateError_Create( |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4631 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4632 | } |
| 4633 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4634 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 4635 | goto onError; |
| 4636 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 4637 | goto onError; |
| 4638 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 4639 | goto onError; |
| 4640 | return; |
| 4641 | onError: |
| 4642 | Py_DECREF(*exceptionObject); |
| 4643 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4644 | } |
| 4645 | } |
| 4646 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4647 | /* raises a UnicodeTranslateError */ |
| 4648 | static void raise_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4649 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4650 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4651 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4652 | { |
| 4653 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4654 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4655 | if (*exceptionObject != NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4656 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4657 | } |
| 4658 | |
| 4659 | /* error handling callback helper: |
| 4660 | build arguments, call the callback and check the arguments, |
| 4661 | put the result into newpos and return the replacement string, which |
| 4662 | has to be freed by the caller */ |
| 4663 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4664 | PyObject **errorHandler, |
| 4665 | const char *reason, |
| 4666 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4667 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4668 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4669 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4670 | 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] | 4671 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4672 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4673 | PyObject *restuple; |
| 4674 | PyObject *resunicode; |
| 4675 | |
| 4676 | if (*errorHandler == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4677 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4678 | if (*errorHandler == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4679 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4680 | } |
| 4681 | |
| 4682 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4683 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4684 | if (*exceptionObject == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4685 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4686 | |
| 4687 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4688 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4689 | if (restuple == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4690 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4691 | if (!PyTuple_Check(restuple)) { |
Georg Brandl | 40e15ed | 2009-04-05 21:48:06 +0000 | [diff] [blame] | 4692 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4693 | Py_DECREF(restuple); |
| 4694 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4695 | } |
| 4696 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4697 | &resunicode, &i_newpos)) { |
| 4698 | Py_DECREF(restuple); |
| 4699 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4700 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4701 | if (i_newpos<0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4702 | *newpos = size+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4703 | else |
| 4704 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4705 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4706 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 4707 | Py_DECREF(restuple); |
| 4708 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4709 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4710 | Py_INCREF(resunicode); |
| 4711 | Py_DECREF(restuple); |
| 4712 | return resunicode; |
| 4713 | } |
| 4714 | |
| 4715 | /* Lookup the character ch in the mapping and put the result in result, |
| 4716 | which must be decrefed by the caller. |
| 4717 | Return 0 on success, -1 on error */ |
| 4718 | static |
| 4719 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 4720 | { |
| 4721 | PyObject *w = PyInt_FromLong((long)c); |
| 4722 | PyObject *x; |
| 4723 | |
| 4724 | if (w == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4725 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4726 | x = PyObject_GetItem(mapping, w); |
| 4727 | Py_DECREF(w); |
| 4728 | if (x == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4729 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4730 | /* No mapping found means: use 1:1 mapping. */ |
| 4731 | PyErr_Clear(); |
| 4732 | *result = NULL; |
| 4733 | return 0; |
| 4734 | } else |
| 4735 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4736 | } |
| 4737 | else if (x == Py_None) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4738 | *result = x; |
| 4739 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4740 | } |
| 4741 | else if (PyInt_Check(x)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4742 | long value = PyInt_AS_LONG(x); |
| 4743 | long max = PyUnicode_GetMax(); |
| 4744 | if (value < 0 || value > max) { |
| 4745 | PyErr_Format(PyExc_TypeError, |
| 4746 | "character mapping must be in range(0x%lx)", max+1); |
| 4747 | Py_DECREF(x); |
| 4748 | return -1; |
| 4749 | } |
| 4750 | *result = x; |
| 4751 | return 0; |
| 4752 | } |
| 4753 | else if (PyUnicode_Check(x)) { |
| 4754 | *result = x; |
| 4755 | return 0; |
| 4756 | } |
| 4757 | else { |
| 4758 | /* wrong return value */ |
| 4759 | PyErr_SetString(PyExc_TypeError, |
| 4760 | "character mapping must return integer, None or unicode"); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4761 | Py_DECREF(x); |
| 4762 | return -1; |
| 4763 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4764 | } |
| 4765 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4766 | if not reallocate and adjust various state variables. |
| 4767 | Return 0 on success, -1 on error */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4768 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4769 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4770 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4771 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4772 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4773 | if (requiredsize > oldsize) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4774 | /* remember old output position */ |
| 4775 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
| 4776 | /* exponentially overallocate to minimize reallocations */ |
| 4777 | if (requiredsize < 2 * oldsize) |
| 4778 | requiredsize = 2 * oldsize; |
| 4779 | if (PyUnicode_Resize(outobj, requiredsize) < 0) |
| 4780 | return -1; |
| 4781 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4782 | } |
| 4783 | return 0; |
| 4784 | } |
| 4785 | /* lookup the character, put the result in the output string and adjust |
| 4786 | various state variables. Return a new reference to the object that |
| 4787 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 4788 | undefined (in which case no character was written). |
| 4789 | The called must decref result. |
| 4790 | Return 0 on success, -1 on error. */ |
| 4791 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4792 | int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4793 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
| 4794 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4795 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4796 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4797 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4798 | if (*res==NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4799 | /* not found => default to 1:1 mapping */ |
| 4800 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4801 | } |
| 4802 | else if (*res==Py_None) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4803 | ; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4804 | else if (PyInt_Check(*res)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4805 | /* no overflow check, because we know that the space is enough */ |
| 4806 | *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4807 | } |
| 4808 | else if (PyUnicode_Check(*res)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4809 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
| 4810 | if (repsize==1) { |
| 4811 | /* no overflow check, because we know that the space is enough */ |
| 4812 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 4813 | } |
| 4814 | else if (repsize!=0) { |
| 4815 | /* more than one character */ |
| 4816 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
| 4817 | (insize - (curinp-startinp)) + |
| 4818 | repsize - 1; |
| 4819 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
| 4820 | return -1; |
| 4821 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 4822 | *outp += repsize; |
| 4823 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4824 | } |
| 4825 | else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4826 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4827 | return 0; |
| 4828 | } |
| 4829 | |
| 4830 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4831 | Py_ssize_t size, |
| 4832 | PyObject *mapping, |
| 4833 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4834 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4835 | /* output object */ |
| 4836 | PyObject *res = NULL; |
| 4837 | /* pointers to the beginning and end+1 of input */ |
| 4838 | const Py_UNICODE *startp = p; |
| 4839 | const Py_UNICODE *endp = p + size; |
| 4840 | /* pointer into the output */ |
| 4841 | Py_UNICODE *str; |
| 4842 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4843 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4844 | char *reason = "character maps to <undefined>"; |
| 4845 | PyObject *errorHandler = NULL; |
| 4846 | PyObject *exc = NULL; |
| 4847 | /* the following variable is used for caching string comparisons |
| 4848 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4849 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4850 | int known_errorHandler = -1; |
| 4851 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4852 | if (mapping == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4853 | PyErr_BadArgument(); |
| 4854 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4855 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4856 | |
| 4857 | /* allocate enough for a simple 1:1 translation without |
| 4858 | replacements, if we need more, we'll resize */ |
| 4859 | res = PyUnicode_FromUnicode(NULL, size); |
| 4860 | if (res == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4861 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4862 | if (size == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4863 | return res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4864 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4865 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4866 | while (p<endp) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4867 | /* try to encode it */ |
| 4868 | PyObject *x = NULL; |
| 4869 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
| 4870 | Py_XDECREF(x); |
| 4871 | goto onError; |
| 4872 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4873 | Py_XDECREF(x); |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4874 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 4875 | ++p; |
| 4876 | else { /* untranslatable character */ |
| 4877 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 4878 | Py_ssize_t repsize; |
| 4879 | Py_ssize_t newpos; |
| 4880 | Py_UNICODE *uni2; |
| 4881 | /* startpos for collecting untranslatable chars */ |
| 4882 | const Py_UNICODE *collstart = p; |
| 4883 | const Py_UNICODE *collend = p+1; |
| 4884 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4885 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4886 | /* find all untranslatable characters */ |
| 4887 | while (collend < endp) { |
| 4888 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
| 4889 | goto onError; |
| 4890 | Py_XDECREF(x); |
| 4891 | if (x!=Py_None) |
| 4892 | break; |
| 4893 | ++collend; |
| 4894 | } |
| 4895 | /* cache callback name lookup |
| 4896 | * (if not done yet, i.e. it's the first error) */ |
| 4897 | if (known_errorHandler==-1) { |
| 4898 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4899 | known_errorHandler = 1; |
| 4900 | else if (!strcmp(errors, "replace")) |
| 4901 | known_errorHandler = 2; |
| 4902 | else if (!strcmp(errors, "ignore")) |
| 4903 | known_errorHandler = 3; |
| 4904 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4905 | known_errorHandler = 4; |
| 4906 | else |
| 4907 | known_errorHandler = 0; |
| 4908 | } |
| 4909 | switch (known_errorHandler) { |
| 4910 | case 1: /* strict */ |
| 4911 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4912 | goto onError; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4913 | case 2: /* replace */ |
| 4914 | /* No need to check for space, this is a 1:1 replacement */ |
| 4915 | for (coll = collstart; coll<collend; ++coll) |
| 4916 | *str++ = '?'; |
| 4917 | /* fall through */ |
| 4918 | case 3: /* ignore */ |
| 4919 | p = collend; |
| 4920 | break; |
| 4921 | case 4: /* xmlcharrefreplace */ |
| 4922 | /* generate replacement (temporarily (mis)uses p) */ |
| 4923 | for (p = collstart; p < collend; ++p) { |
| 4924 | char buffer[2+29+1+1]; |
| 4925 | char *cp; |
| 4926 | sprintf(buffer, "&#%d;", (int)*p); |
| 4927 | if (charmaptranslate_makespace(&res, &str, |
| 4928 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 4929 | goto onError; |
| 4930 | for (cp = buffer; *cp; ++cp) |
| 4931 | *str++ = *cp; |
| 4932 | } |
| 4933 | p = collend; |
| 4934 | break; |
| 4935 | default: |
| 4936 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 4937 | reason, startp, size, &exc, |
| 4938 | collstart-startp, collend-startp, &newpos); |
| 4939 | if (repunicode == NULL) |
| 4940 | goto onError; |
| 4941 | /* generate replacement */ |
| 4942 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4943 | if (charmaptranslate_makespace(&res, &str, |
| 4944 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 4945 | Py_DECREF(repunicode); |
| 4946 | goto onError; |
| 4947 | } |
| 4948 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 4949 | *str++ = *uni2; |
| 4950 | p = startp + newpos; |
| 4951 | Py_DECREF(repunicode); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4952 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4953 | } |
| 4954 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4955 | /* Resize if we allocated to much */ |
| 4956 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4957 | if (respos<PyUnicode_GET_SIZE(res)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4958 | if (PyUnicode_Resize(&res, respos) < 0) |
| 4959 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4960 | } |
| 4961 | Py_XDECREF(exc); |
| 4962 | Py_XDECREF(errorHandler); |
| 4963 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4964 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4965 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4966 | Py_XDECREF(res); |
| 4967 | Py_XDECREF(exc); |
| 4968 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4969 | return NULL; |
| 4970 | } |
| 4971 | |
| 4972 | PyObject *PyUnicode_Translate(PyObject *str, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4973 | PyObject *mapping, |
| 4974 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4975 | { |
| 4976 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4977 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4978 | str = PyUnicode_FromObject(str); |
| 4979 | if (str == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4980 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4981 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4982 | PyUnicode_GET_SIZE(str), |
| 4983 | mapping, |
| 4984 | errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4985 | Py_DECREF(str); |
| 4986 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4987 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4988 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4989 | Py_XDECREF(str); |
| 4990 | return NULL; |
| 4991 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4992 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4993 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 4994 | |
| 4995 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4996 | Py_ssize_t length, |
| 4997 | char *output, |
| 4998 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4999 | { |
| 5000 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5001 | PyObject *errorHandler = NULL; |
| 5002 | PyObject *exc = NULL; |
| 5003 | const char *encoding = "decimal"; |
| 5004 | const char *reason = "invalid decimal Unicode string"; |
| 5005 | /* the following variable is used for caching string comparisons |
| 5006 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 5007 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5008 | |
| 5009 | if (output == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5010 | PyErr_BadArgument(); |
| 5011 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5012 | } |
| 5013 | |
| 5014 | p = s; |
| 5015 | end = s + length; |
| 5016 | while (p < end) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5017 | register Py_UNICODE ch = *p; |
| 5018 | int decimal; |
| 5019 | PyObject *repunicode; |
| 5020 | Py_ssize_t repsize; |
| 5021 | Py_ssize_t newpos; |
| 5022 | Py_UNICODE *uni2; |
| 5023 | Py_UNICODE *collstart; |
| 5024 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5025 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5026 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5027 | *output++ = ' '; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5028 | ++p; |
| 5029 | continue; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5030 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5031 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5032 | if (decimal >= 0) { |
| 5033 | *output++ = '0' + decimal; |
| 5034 | ++p; |
| 5035 | continue; |
| 5036 | } |
| 5037 | if (0 < ch && ch < 256) { |
| 5038 | *output++ = (char)ch; |
| 5039 | ++p; |
| 5040 | continue; |
| 5041 | } |
| 5042 | /* All other characters are considered unencodable */ |
| 5043 | collstart = p; |
| 5044 | collend = p+1; |
| 5045 | while (collend < end) { |
| 5046 | if ((0 < *collend && *collend < 256) || |
| 5047 | !Py_UNICODE_ISSPACE(*collend) || |
| 5048 | Py_UNICODE_TODECIMAL(*collend)) |
| 5049 | break; |
| 5050 | } |
| 5051 | /* cache callback name lookup |
| 5052 | * (if not done yet, i.e. it's the first error) */ |
| 5053 | if (known_errorHandler==-1) { |
| 5054 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5055 | known_errorHandler = 1; |
| 5056 | else if (!strcmp(errors, "replace")) |
| 5057 | known_errorHandler = 2; |
| 5058 | else if (!strcmp(errors, "ignore")) |
| 5059 | known_errorHandler = 3; |
| 5060 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5061 | known_errorHandler = 4; |
| 5062 | else |
| 5063 | known_errorHandler = 0; |
| 5064 | } |
| 5065 | switch (known_errorHandler) { |
| 5066 | case 1: /* strict */ |
| 5067 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 5068 | goto onError; |
| 5069 | case 2: /* replace */ |
| 5070 | for (p = collstart; p < collend; ++p) |
| 5071 | *output++ = '?'; |
| 5072 | /* fall through */ |
| 5073 | case 3: /* ignore */ |
| 5074 | p = collend; |
| 5075 | break; |
| 5076 | case 4: /* xmlcharrefreplace */ |
| 5077 | /* generate replacement (temporarily (mis)uses p) */ |
| 5078 | for (p = collstart; p < collend; ++p) |
| 5079 | output += sprintf(output, "&#%d;", (int)*p); |
| 5080 | p = collend; |
| 5081 | break; |
| 5082 | default: |
| 5083 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 5084 | encoding, reason, s, length, &exc, |
| 5085 | collstart-s, collend-s, &newpos); |
| 5086 | if (repunicode == NULL) |
| 5087 | goto onError; |
| 5088 | /* generate replacement */ |
| 5089 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5090 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 5091 | Py_UNICODE ch = *uni2; |
| 5092 | if (Py_UNICODE_ISSPACE(ch)) |
| 5093 | *output++ = ' '; |
| 5094 | else { |
| 5095 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5096 | if (decimal >= 0) |
| 5097 | *output++ = '0' + decimal; |
| 5098 | else if (0 < ch && ch < 256) |
| 5099 | *output++ = (char)ch; |
| 5100 | else { |
| 5101 | Py_DECREF(repunicode); |
| 5102 | raise_encode_exception(&exc, encoding, |
| 5103 | s, length, collstart-s, collend-s, reason); |
| 5104 | goto onError; |
| 5105 | } |
| 5106 | } |
| 5107 | } |
| 5108 | p = s + newpos; |
| 5109 | Py_DECREF(repunicode); |
| 5110 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5111 | } |
| 5112 | /* 0-terminate the output string */ |
| 5113 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5114 | Py_XDECREF(exc); |
| 5115 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5116 | return 0; |
| 5117 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5118 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5119 | Py_XDECREF(exc); |
| 5120 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5121 | return -1; |
| 5122 | } |
| 5123 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5124 | /* --- Helpers ------------------------------------------------------------ */ |
| 5125 | |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 5126 | #include "stringlib/unicodedefs.h" |
Fredrik Lundh | 6471ee4 | 2006-05-24 14:28:11 +0000 | [diff] [blame] | 5127 | |
Facundo Batista | 6f7e6fb | 2007-11-16 19:16:15 +0000 | [diff] [blame] | 5128 | #define FROM_UNICODE |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 5129 | |
Fredrik Lundh | a50d201 | 2006-05-26 17:04:58 +0000 | [diff] [blame] | 5130 | #include "stringlib/fastsearch.h" |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5131 | |
| 5132 | #include "stringlib/count.h" |
| 5133 | #include "stringlib/find.h" |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 5134 | #include "stringlib/partition.h" |
| 5135 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 5136 | /* helper macro to fixup start/end slice values */ |
| 5137 | #define FIX_START_END(obj) \ |
| 5138 | if (start < 0) \ |
| 5139 | start += (obj)->length; \ |
| 5140 | if (start < 0) \ |
| 5141 | start = 0; \ |
| 5142 | if (end > (obj)->length) \ |
| 5143 | end = (obj)->length; \ |
| 5144 | if (end < 0) \ |
| 5145 | end += (obj)->length; \ |
| 5146 | if (end < 0) \ |
| 5147 | end = 0; |
| 5148 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5149 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5150 | PyObject *substr, |
| 5151 | Py_ssize_t start, |
| 5152 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5153 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5154 | Py_ssize_t result; |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5155 | PyUnicodeObject* str_obj; |
| 5156 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5157 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5158 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 5159 | if (!str_obj) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5160 | return -1; |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5161 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 5162 | if (!sub_obj) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5163 | Py_DECREF(str_obj); |
| 5164 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5165 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5166 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 5167 | FIX_START_END(str_obj); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5168 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5169 | result = stringlib_count( |
| 5170 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
| 5171 | ); |
| 5172 | |
| 5173 | Py_DECREF(sub_obj); |
| 5174 | Py_DECREF(str_obj); |
| 5175 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5176 | return result; |
| 5177 | } |
| 5178 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5179 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5180 | PyObject *sub, |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 5181 | Py_ssize_t start, |
| 5182 | Py_ssize_t end, |
| 5183 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5184 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5185 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5186 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5187 | str = PyUnicode_FromObject(str); |
| 5188 | if (!str) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5189 | return -2; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5190 | sub = PyUnicode_FromObject(sub); |
| 5191 | if (!sub) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5192 | Py_DECREF(str); |
| 5193 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5194 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5195 | |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 5196 | if (direction > 0) |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 5197 | result = stringlib_find_slice( |
| 5198 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5199 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5200 | start, end |
| 5201 | ); |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 5202 | else |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 5203 | result = stringlib_rfind_slice( |
| 5204 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5205 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5206 | start, end |
| 5207 | ); |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 5208 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5209 | Py_DECREF(str); |
| 5210 | Py_DECREF(sub); |
| 5211 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5212 | return result; |
| 5213 | } |
| 5214 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5215 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5216 | int tailmatch(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5217 | PyUnicodeObject *substring, |
| 5218 | Py_ssize_t start, |
| 5219 | Py_ssize_t end, |
| 5220 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5221 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5222 | if (substring->length == 0) |
| 5223 | return 1; |
| 5224 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 5225 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5226 | |
| 5227 | end -= substring->length; |
| 5228 | if (end < start) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5229 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5230 | |
| 5231 | if (direction > 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5232 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 5233 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5234 | } else { |
| 5235 | if (Py_UNICODE_MATCH(self, start, substring)) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5236 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5237 | } |
| 5238 | |
| 5239 | return 0; |
| 5240 | } |
| 5241 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5242 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5243 | PyObject *substr, |
| 5244 | Py_ssize_t start, |
| 5245 | Py_ssize_t end, |
| 5246 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5247 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5248 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5249 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5250 | str = PyUnicode_FromObject(str); |
| 5251 | if (str == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5252 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5253 | substr = PyUnicode_FromObject(substr); |
| 5254 | if (substr == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5255 | Py_DECREF(str); |
| 5256 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5257 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5258 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5259 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5260 | (PyUnicodeObject *)substr, |
| 5261 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5262 | Py_DECREF(str); |
| 5263 | Py_DECREF(substr); |
| 5264 | return result; |
| 5265 | } |
| 5266 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5267 | /* Apply fixfct filter to the Unicode object self and return a |
| 5268 | reference to the modified object */ |
| 5269 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5270 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5271 | PyObject *fixup(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5272 | int (*fixfct)(PyUnicodeObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5273 | { |
| 5274 | |
| 5275 | PyUnicodeObject *u; |
| 5276 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5277 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5278 | if (u == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5279 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5280 | |
| 5281 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5282 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5283 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5284 | /* fixfct should return TRUE if it modified the buffer. If |
| 5285 | FALSE, return a reference to the original buffer instead |
| 5286 | (to save space, not time) */ |
| 5287 | Py_INCREF(self); |
| 5288 | Py_DECREF(u); |
| 5289 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5290 | } |
| 5291 | return (PyObject*) u; |
| 5292 | } |
| 5293 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5294 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5295 | int fixupper(PyUnicodeObject *self) |
| 5296 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5297 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5298 | Py_UNICODE *s = self->str; |
| 5299 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5300 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5301 | while (len-- > 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5302 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5303 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5304 | ch = Py_UNICODE_TOUPPER(*s); |
| 5305 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5306 | status = 1; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5307 | *s = ch; |
| 5308 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5309 | s++; |
| 5310 | } |
| 5311 | |
| 5312 | return status; |
| 5313 | } |
| 5314 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5315 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5316 | int fixlower(PyUnicodeObject *self) |
| 5317 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5318 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5319 | Py_UNICODE *s = self->str; |
| 5320 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5321 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5322 | while (len-- > 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5323 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5324 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5325 | ch = Py_UNICODE_TOLOWER(*s); |
| 5326 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5327 | status = 1; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5328 | *s = ch; |
| 5329 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5330 | s++; |
| 5331 | } |
| 5332 | |
| 5333 | return status; |
| 5334 | } |
| 5335 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5336 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5337 | int fixswapcase(PyUnicodeObject *self) |
| 5338 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5339 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5340 | Py_UNICODE *s = self->str; |
| 5341 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5342 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5343 | while (len-- > 0) { |
| 5344 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5345 | *s = Py_UNICODE_TOLOWER(*s); |
| 5346 | status = 1; |
| 5347 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 5348 | *s = Py_UNICODE_TOUPPER(*s); |
| 5349 | status = 1; |
| 5350 | } |
| 5351 | s++; |
| 5352 | } |
| 5353 | |
| 5354 | return status; |
| 5355 | } |
| 5356 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5357 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5358 | int fixcapitalize(PyUnicodeObject *self) |
| 5359 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5360 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5361 | Py_UNICODE *s = self->str; |
| 5362 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5363 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5364 | if (len == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5365 | return 0; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5366 | if (Py_UNICODE_ISLOWER(*s)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5367 | *s = Py_UNICODE_TOUPPER(*s); |
| 5368 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5369 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5370 | s++; |
| 5371 | while (--len > 0) { |
| 5372 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5373 | *s = Py_UNICODE_TOLOWER(*s); |
| 5374 | status = 1; |
| 5375 | } |
| 5376 | s++; |
| 5377 | } |
| 5378 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5379 | } |
| 5380 | |
| 5381 | static |
| 5382 | int fixtitle(PyUnicodeObject *self) |
| 5383 | { |
| 5384 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5385 | register Py_UNICODE *e; |
| 5386 | int previous_is_cased; |
| 5387 | |
| 5388 | /* Shortcut for single character strings */ |
| 5389 | if (PyUnicode_GET_SIZE(self) == 1) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5390 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 5391 | if (*p != ch) { |
| 5392 | *p = ch; |
| 5393 | return 1; |
| 5394 | } |
| 5395 | else |
| 5396 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5397 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5398 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5399 | e = p + PyUnicode_GET_SIZE(self); |
| 5400 | previous_is_cased = 0; |
| 5401 | for (; p < e; p++) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5402 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5403 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5404 | if (previous_is_cased) |
| 5405 | *p = Py_UNICODE_TOLOWER(ch); |
| 5406 | else |
| 5407 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5408 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5409 | if (Py_UNICODE_ISLOWER(ch) || |
| 5410 | Py_UNICODE_ISUPPER(ch) || |
| 5411 | Py_UNICODE_ISTITLE(ch)) |
| 5412 | previous_is_cased = 1; |
| 5413 | else |
| 5414 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5415 | } |
| 5416 | return 1; |
| 5417 | } |
| 5418 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5419 | PyObject * |
| 5420 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5421 | { |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5422 | PyObject *internal_separator = NULL; |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 5423 | const Py_UNICODE blank = ' '; |
| 5424 | const Py_UNICODE *sep = ␣ |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 5425 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5426 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 5427 | Py_ssize_t res_alloc = 100; /* # allocated bytes for string in res */ |
| 5428 | Py_ssize_t res_used; /* # used bytes */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5429 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 5430 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5431 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5432 | PyObject *item; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5433 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5434 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5435 | fseq = PySequence_Fast(seq, ""); |
| 5436 | if (fseq == NULL) { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5437 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5438 | } |
| 5439 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5440 | /* Grrrr. A codec may be invoked to convert str objects to |
| 5441 | * Unicode, and so it's possible to call back into Python code |
| 5442 | * during PyUnicode_FromObject(), and so it's possible for a sick |
| 5443 | * codec to change the size of fseq (if seq is a list). Therefore |
| 5444 | * we have to keep refetching the size -- can't assume seqlen |
| 5445 | * is invariant. |
| 5446 | */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5447 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5448 | /* If empty sequence, return u"". */ |
| 5449 | if (seqlen == 0) { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5450 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 5451 | goto Done; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5452 | } |
| 5453 | /* If singleton sequence with an exact Unicode, return that. */ |
| 5454 | if (seqlen == 1) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5455 | item = PySequence_Fast_GET_ITEM(fseq, 0); |
| 5456 | if (PyUnicode_CheckExact(item)) { |
| 5457 | Py_INCREF(item); |
| 5458 | res = (PyUnicodeObject *)item; |
| 5459 | goto Done; |
| 5460 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5461 | } |
| 5462 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5463 | /* At least two items to join, or one that isn't exact Unicode. */ |
| 5464 | if (seqlen > 1) { |
| 5465 | /* Set up sep and seplen -- they're needed. */ |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5466 | if (separator == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5467 | sep = ␣ |
| 5468 | seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5469 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5470 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5471 | internal_separator = PyUnicode_FromObject(separator); |
| 5472 | if (internal_separator == NULL) |
| 5473 | goto onError; |
| 5474 | sep = PyUnicode_AS_UNICODE(internal_separator); |
| 5475 | seplen = PyUnicode_GET_SIZE(internal_separator); |
| 5476 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 5477 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5478 | } |
| 5479 | } |
| 5480 | |
| 5481 | /* Get space. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5482 | res = _PyUnicode_New(res_alloc); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5483 | if (res == NULL) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5484 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5485 | res_p = PyUnicode_AS_UNICODE(res); |
| 5486 | res_used = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5487 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5488 | for (i = 0; i < seqlen; ++i) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5489 | Py_ssize_t itemlen; |
| 5490 | Py_ssize_t new_res_used; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5491 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5492 | item = PySequence_Fast_GET_ITEM(fseq, i); |
| 5493 | /* Convert item to Unicode. */ |
| 5494 | if (! PyUnicode_Check(item) && ! PyString_Check(item)) { |
| 5495 | PyErr_Format(PyExc_TypeError, |
| 5496 | "sequence item %zd: expected string or Unicode," |
| 5497 | " %.80s found", |
| 5498 | i, Py_TYPE(item)->tp_name); |
| 5499 | goto onError; |
| 5500 | } |
| 5501 | item = PyUnicode_FromObject(item); |
| 5502 | if (item == NULL) |
| 5503 | goto onError; |
| 5504 | /* We own a reference to item from here on. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5505 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5506 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 5507 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5508 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5509 | /* Make sure we have enough space for the separator and the item. */ |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5510 | itemlen = PyUnicode_GET_SIZE(item); |
| 5511 | new_res_used = res_used + itemlen; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5512 | if (new_res_used < 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5513 | goto Overflow; |
| 5514 | if (i < seqlen - 1) { |
| 5515 | new_res_used += seplen; |
| 5516 | if (new_res_used < 0) |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5517 | goto Overflow; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5518 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5519 | if (new_res_used > res_alloc) { |
| 5520 | /* double allocated size until it's big enough */ |
| 5521 | do { |
| 5522 | res_alloc += res_alloc; |
| 5523 | if (res_alloc <= 0) |
| 5524 | goto Overflow; |
| 5525 | } while (new_res_used > res_alloc); |
| 5526 | if (_PyUnicode_Resize(&res, res_alloc) < 0) { |
| 5527 | Py_DECREF(item); |
| 5528 | goto onError; |
| 5529 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5530 | res_p = PyUnicode_AS_UNICODE(res) + res_used; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5531 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5532 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5533 | /* Copy item, and maybe the separator. */ |
| 5534 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
| 5535 | res_p += itemlen; |
| 5536 | if (i < seqlen - 1) { |
| 5537 | Py_UNICODE_COPY(res_p, sep, seplen); |
| 5538 | res_p += seplen; |
| 5539 | } |
| 5540 | Py_DECREF(item); |
| 5541 | res_used = new_res_used; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5542 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5543 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5544 | /* Shrink res to match the used area; this probably can't fail, |
| 5545 | * but it's cheap to check. |
| 5546 | */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5547 | if (_PyUnicode_Resize(&res, res_used) < 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5548 | goto onError; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5549 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5550 | Done: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5551 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5552 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5553 | return (PyObject *)res; |
| 5554 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5555 | Overflow: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5556 | PyErr_SetString(PyExc_OverflowError, |
Georg Brandl | 90e27d3 | 2006-06-10 06:40:50 +0000 | [diff] [blame] | 5557 | "join() result is too long for a Python string"); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5558 | Py_DECREF(item); |
| 5559 | /* fall through */ |
| 5560 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5561 | onError: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5562 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5563 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5564 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5565 | return NULL; |
| 5566 | } |
| 5567 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5568 | static |
| 5569 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5570 | Py_ssize_t left, |
| 5571 | Py_ssize_t right, |
| 5572 | Py_UNICODE fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5573 | { |
| 5574 | PyUnicodeObject *u; |
| 5575 | |
| 5576 | if (left < 0) |
| 5577 | left = 0; |
| 5578 | if (right < 0) |
| 5579 | right = 0; |
| 5580 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5581 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5582 | Py_INCREF(self); |
| 5583 | return self; |
| 5584 | } |
| 5585 | |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 5586 | if (left > PY_SSIZE_T_MAX - self->length || |
| 5587 | right > PY_SSIZE_T_MAX - (left + self->length)) { |
| 5588 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 5589 | return NULL; |
| 5590 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5591 | u = _PyUnicode_New(left + self->length + right); |
| 5592 | if (u) { |
| 5593 | if (left) |
| 5594 | Py_UNICODE_FILL(u->str, fill, left); |
| 5595 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 5596 | if (right) |
| 5597 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 5598 | } |
| 5599 | |
| 5600 | return u; |
| 5601 | } |
| 5602 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5603 | #define SPLIT_APPEND(data, left, right) \ |
| 5604 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
| 5605 | if (!str) \ |
| 5606 | goto onError; \ |
| 5607 | if (PyList_Append(list, str)) { \ |
| 5608 | Py_DECREF(str); \ |
| 5609 | goto onError; \ |
| 5610 | } \ |
| 5611 | else \ |
| 5612 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5613 | |
| 5614 | static |
| 5615 | PyObject *split_whitespace(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5616 | PyObject *list, |
| 5617 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5618 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5619 | register Py_ssize_t i; |
| 5620 | register Py_ssize_t j; |
| 5621 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5622 | PyObject *str; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5623 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5624 | |
| 5625 | for (i = j = 0; i < len; ) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5626 | /* find a token */ |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5627 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5628 | i++; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5629 | j = i; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5630 | while (i < len && !Py_UNICODE_ISSPACE(buf[i])) |
| 5631 | i++; |
| 5632 | if (j < i) { |
| 5633 | if (maxcount-- <= 0) |
| 5634 | break; |
| 5635 | SPLIT_APPEND(buf, j, i); |
| 5636 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
| 5637 | i++; |
| 5638 | j = i; |
| 5639 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5640 | } |
| 5641 | if (j < len) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5642 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5643 | } |
| 5644 | return list; |
| 5645 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5646 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5647 | Py_DECREF(list); |
| 5648 | return NULL; |
| 5649 | } |
| 5650 | |
| 5651 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5652 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5653 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5654 | register Py_ssize_t i; |
| 5655 | register Py_ssize_t j; |
| 5656 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5657 | PyObject *list; |
| 5658 | PyObject *str; |
| 5659 | Py_UNICODE *data; |
| 5660 | |
| 5661 | string = PyUnicode_FromObject(string); |
| 5662 | if (string == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5663 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5664 | data = PyUnicode_AS_UNICODE(string); |
| 5665 | len = PyUnicode_GET_SIZE(string); |
| 5666 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5667 | list = PyList_New(0); |
| 5668 | if (!list) |
| 5669 | goto onError; |
| 5670 | |
| 5671 | for (i = j = 0; i < len; ) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5672 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5673 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5674 | /* Find a line and append it */ |
| 5675 | while (i < len && !BLOOM_LINEBREAK(data[i])) |
| 5676 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5677 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5678 | /* Skip the line break reading CRLF as one line break */ |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5679 | eol = i; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5680 | if (i < len) { |
| 5681 | if (data[i] == '\r' && i + 1 < len && |
| 5682 | data[i+1] == '\n') |
| 5683 | i += 2; |
| 5684 | else |
| 5685 | i++; |
| 5686 | if (keepends) |
| 5687 | eol = i; |
| 5688 | } |
| 5689 | SPLIT_APPEND(data, j, eol); |
| 5690 | j = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5691 | } |
| 5692 | if (j < len) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5693 | SPLIT_APPEND(data, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5694 | } |
| 5695 | |
| 5696 | Py_DECREF(string); |
| 5697 | return list; |
| 5698 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5699 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5700 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5701 | Py_DECREF(string); |
| 5702 | return NULL; |
| 5703 | } |
| 5704 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5705 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5706 | PyObject *split_char(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5707 | PyObject *list, |
| 5708 | Py_UNICODE ch, |
| 5709 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5710 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5711 | register Py_ssize_t i; |
| 5712 | register Py_ssize_t j; |
| 5713 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5714 | PyObject *str; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5715 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5716 | |
| 5717 | for (i = j = 0; i < len; ) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5718 | if (buf[i] == ch) { |
| 5719 | if (maxcount-- <= 0) |
| 5720 | break; |
| 5721 | SPLIT_APPEND(buf, j, i); |
| 5722 | i = j = i + 1; |
| 5723 | } else |
| 5724 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5725 | } |
| 5726 | if (j <= len) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5727 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5728 | } |
| 5729 | return list; |
| 5730 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5731 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5732 | Py_DECREF(list); |
| 5733 | return NULL; |
| 5734 | } |
| 5735 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5736 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5737 | PyObject *split_substring(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5738 | PyObject *list, |
| 5739 | PyUnicodeObject *substring, |
| 5740 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5741 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5742 | register Py_ssize_t i; |
| 5743 | register Py_ssize_t j; |
| 5744 | Py_ssize_t len = self->length; |
| 5745 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5746 | PyObject *str; |
| 5747 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 5748 | for (i = j = 0; i <= len - sublen; ) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5749 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5750 | if (maxcount-- <= 0) |
| 5751 | break; |
| 5752 | SPLIT_APPEND(self->str, j, i); |
| 5753 | i = j = i + sublen; |
| 5754 | } else |
| 5755 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5756 | } |
| 5757 | if (j <= len) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5758 | SPLIT_APPEND(self->str, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5759 | } |
| 5760 | return list; |
| 5761 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5762 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5763 | Py_DECREF(list); |
| 5764 | return NULL; |
| 5765 | } |
| 5766 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5767 | static |
| 5768 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5769 | PyObject *list, |
| 5770 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5771 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5772 | register Py_ssize_t i; |
| 5773 | register Py_ssize_t j; |
| 5774 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5775 | PyObject *str; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5776 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5777 | |
| 5778 | for (i = j = len - 1; i >= 0; ) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5779 | /* find a token */ |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5780 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5781 | i--; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5782 | j = i; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5783 | while (i >= 0 && !Py_UNICODE_ISSPACE(buf[i])) |
| 5784 | i--; |
| 5785 | if (j > i) { |
| 5786 | if (maxcount-- <= 0) |
| 5787 | break; |
| 5788 | SPLIT_APPEND(buf, i + 1, j + 1); |
| 5789 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
| 5790 | i--; |
| 5791 | j = i; |
| 5792 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5793 | } |
| 5794 | if (j >= 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5795 | SPLIT_APPEND(buf, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5796 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5797 | if (PyList_Reverse(list) < 0) |
| 5798 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5799 | return list; |
| 5800 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5801 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5802 | Py_DECREF(list); |
| 5803 | return NULL; |
| 5804 | } |
| 5805 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5806 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5807 | PyObject *rsplit_char(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5808 | PyObject *list, |
| 5809 | Py_UNICODE ch, |
| 5810 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5811 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5812 | register Py_ssize_t i; |
| 5813 | register Py_ssize_t j; |
| 5814 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5815 | PyObject *str; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5816 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5817 | |
| 5818 | for (i = j = len - 1; i >= 0; ) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5819 | if (buf[i] == ch) { |
| 5820 | if (maxcount-- <= 0) |
| 5821 | break; |
| 5822 | SPLIT_APPEND(buf, i + 1, j + 1); |
| 5823 | j = i = i - 1; |
| 5824 | } else |
| 5825 | i--; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5826 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 5827 | if (j >= -1) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5828 | SPLIT_APPEND(buf, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5829 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5830 | if (PyList_Reverse(list) < 0) |
| 5831 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5832 | return list; |
| 5833 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5834 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5835 | Py_DECREF(list); |
| 5836 | return NULL; |
| 5837 | } |
| 5838 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5839 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5840 | PyObject *rsplit_substring(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5841 | PyObject *list, |
| 5842 | PyUnicodeObject *substring, |
| 5843 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5844 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5845 | register Py_ssize_t i; |
| 5846 | register Py_ssize_t j; |
| 5847 | Py_ssize_t len = self->length; |
| 5848 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5849 | PyObject *str; |
| 5850 | |
| 5851 | for (i = len - sublen, j = len; i >= 0; ) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5852 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5853 | if (maxcount-- <= 0) |
| 5854 | break; |
| 5855 | SPLIT_APPEND(self->str, i + sublen, j); |
| 5856 | j = i; |
| 5857 | i -= sublen; |
| 5858 | } else |
| 5859 | i--; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5860 | } |
| 5861 | if (j >= 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5862 | SPLIT_APPEND(self->str, 0, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5863 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5864 | if (PyList_Reverse(list) < 0) |
| 5865 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5866 | return list; |
| 5867 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5868 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5869 | Py_DECREF(list); |
| 5870 | return NULL; |
| 5871 | } |
| 5872 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5873 | #undef SPLIT_APPEND |
| 5874 | |
| 5875 | static |
| 5876 | PyObject *split(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5877 | PyUnicodeObject *substring, |
| 5878 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5879 | { |
| 5880 | PyObject *list; |
| 5881 | |
| 5882 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5883 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5884 | |
| 5885 | list = PyList_New(0); |
| 5886 | if (!list) |
| 5887 | return NULL; |
| 5888 | |
| 5889 | if (substring == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5890 | return split_whitespace(self,list,maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5891 | |
| 5892 | else if (substring->length == 1) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5893 | return split_char(self,list,substring->str[0],maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5894 | |
| 5895 | else if (substring->length == 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5896 | Py_DECREF(list); |
| 5897 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5898 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5899 | } |
| 5900 | else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5901 | return split_substring(self,list,substring,maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5902 | } |
| 5903 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5904 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5905 | PyObject *rsplit(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5906 | PyUnicodeObject *substring, |
| 5907 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5908 | { |
| 5909 | PyObject *list; |
| 5910 | |
| 5911 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5912 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5913 | |
| 5914 | list = PyList_New(0); |
| 5915 | if (!list) |
| 5916 | return NULL; |
| 5917 | |
| 5918 | if (substring == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5919 | return rsplit_whitespace(self,list,maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5920 | |
| 5921 | else if (substring->length == 1) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5922 | return rsplit_char(self,list,substring->str[0],maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5923 | |
| 5924 | else if (substring->length == 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5925 | Py_DECREF(list); |
| 5926 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5927 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5928 | } |
| 5929 | else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5930 | return rsplit_substring(self,list,substring,maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5931 | } |
| 5932 | |
| 5933 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5934 | PyObject *replace(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5935 | PyUnicodeObject *str1, |
| 5936 | PyUnicodeObject *str2, |
| 5937 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5938 | { |
| 5939 | PyUnicodeObject *u; |
| 5940 | |
| 5941 | if (maxcount < 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5942 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5943 | |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5944 | if (str1->length == str2->length) { |
| 5945 | /* same length */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5946 | Py_ssize_t i; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5947 | if (str1->length == 1) { |
| 5948 | /* replace characters */ |
| 5949 | Py_UNICODE u1, u2; |
| 5950 | if (!findchar(self->str, self->length, str1->str[0])) |
| 5951 | goto nothing; |
| 5952 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5953 | if (!u) |
| 5954 | return NULL; |
| 5955 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5956 | u1 = str1->str[0]; |
| 5957 | u2 = str2->str[0]; |
| 5958 | for (i = 0; i < u->length; i++) |
| 5959 | if (u->str[i] == u1) { |
| 5960 | if (--maxcount < 0) |
| 5961 | break; |
| 5962 | u->str[i] = u2; |
| 5963 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5964 | } else { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5965 | i = fastsearch( |
| 5966 | self->str, self->length, str1->str, str1->length, FAST_SEARCH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5967 | ); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5968 | if (i < 0) |
| 5969 | goto nothing; |
| 5970 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5971 | if (!u) |
| 5972 | return NULL; |
| 5973 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5974 | while (i <= self->length - str1->length) |
| 5975 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 5976 | if (--maxcount < 0) |
| 5977 | break; |
| 5978 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 5979 | i += str1->length; |
| 5980 | } else |
| 5981 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5982 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5983 | } else { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5984 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 5985 | Py_ssize_t n, i, j, e; |
Fredrik Lundh | 0c71f88 | 2006-05-25 16:46:54 +0000 | [diff] [blame] | 5986 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5987 | Py_UNICODE *p; |
| 5988 | |
| 5989 | /* replace strings */ |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5990 | n = stringlib_count(self->str, self->length, str1->str, str1->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5991 | if (n > maxcount) |
| 5992 | n = maxcount; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5993 | if (n == 0) |
| 5994 | goto nothing; |
Fredrik Lundh | 0c71f88 | 2006-05-25 16:46:54 +0000 | [diff] [blame] | 5995 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 5996 | delta = (str2->length - str1->length); |
| 5997 | if (delta == 0) { |
| 5998 | new_size = self->length; |
| 5999 | } else { |
| 6000 | product = n * (str2->length - str1->length); |
| 6001 | if ((product / (str2->length - str1->length)) != n) { |
| 6002 | PyErr_SetString(PyExc_OverflowError, |
| 6003 | "replace string is too long"); |
| 6004 | return NULL; |
| 6005 | } |
| 6006 | new_size = self->length + product; |
| 6007 | if (new_size < 0) { |
| 6008 | PyErr_SetString(PyExc_OverflowError, |
| 6009 | "replace string is too long"); |
| 6010 | return NULL; |
| 6011 | } |
| 6012 | } |
| 6013 | u = _PyUnicode_New(new_size); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6014 | if (!u) |
| 6015 | return NULL; |
| 6016 | i = 0; |
| 6017 | p = u->str; |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 6018 | e = self->length - str1->length; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6019 | if (str1->length > 0) { |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 6020 | while (n-- > 0) { |
| 6021 | /* look for next match */ |
| 6022 | j = i; |
| 6023 | while (j <= e) { |
| 6024 | if (Py_UNICODE_MATCH(self, j, str1)) |
| 6025 | break; |
| 6026 | j++; |
| 6027 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6028 | if (j > i) { |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 6029 | if (j > e) |
| 6030 | break; |
| 6031 | /* copy unchanged part [i:j] */ |
| 6032 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 6033 | p += j - i; |
| 6034 | } |
| 6035 | /* copy substitution string */ |
| 6036 | if (str2->length > 0) { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6037 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6038 | p += str2->length; |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 6039 | } |
| 6040 | i = j + str1->length; |
| 6041 | } |
| 6042 | if (i < self->length) |
| 6043 | /* copy tail [i:] */ |
| 6044 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6045 | } else { |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 6046 | /* interleave */ |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6047 | while (n > 0) { |
| 6048 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6049 | p += str2->length; |
| 6050 | if (--n <= 0) |
| 6051 | break; |
| 6052 | *p++ = self->str[i++]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6053 | } |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6054 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6055 | } |
| 6056 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6057 | return (PyObject *) u; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6058 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6059 | nothing: |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6060 | /* nothing to replace; return original string (when possible) */ |
| 6061 | if (PyUnicode_CheckExact(self)) { |
| 6062 | Py_INCREF(self); |
| 6063 | return (PyObject *) self; |
| 6064 | } |
| 6065 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6066 | } |
| 6067 | |
| 6068 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 6069 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6070 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6071 | "S.title() -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6072 | \n\ |
| 6073 | 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] | 6074 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6075 | |
| 6076 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6077 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6078 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6079 | return fixup(self, fixtitle); |
| 6080 | } |
| 6081 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6082 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6083 | "S.capitalize() -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6084 | \n\ |
| 6085 | 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] | 6086 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6087 | |
| 6088 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6089 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6090 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6091 | return fixup(self, fixcapitalize); |
| 6092 | } |
| 6093 | |
| 6094 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6095 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6096 | "S.capwords() -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6097 | \n\ |
| 6098 | 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] | 6099 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6100 | |
| 6101 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6102 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6103 | { |
| 6104 | PyObject *list; |
| 6105 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6106 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6107 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6108 | /* Split into words */ |
| 6109 | list = split(self, NULL, -1); |
| 6110 | if (!list) |
| 6111 | return NULL; |
| 6112 | |
| 6113 | /* Capitalize each word */ |
| 6114 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 6115 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6116 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6117 | if (item == NULL) |
| 6118 | goto onError; |
| 6119 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 6120 | PyList_SET_ITEM(list, i, item); |
| 6121 | } |
| 6122 | |
| 6123 | /* Join the words to form a new string */ |
| 6124 | item = PyUnicode_Join(NULL, list); |
| 6125 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6126 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6127 | Py_DECREF(list); |
| 6128 | return (PyObject *)item; |
| 6129 | } |
| 6130 | #endif |
| 6131 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6132 | /* Argument converter. Coerces to a single unicode character */ |
| 6133 | |
| 6134 | static int |
| 6135 | convert_uc(PyObject *obj, void *addr) |
| 6136 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 6137 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 6138 | PyObject *uniobj; |
| 6139 | Py_UNICODE *unistr; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6140 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 6141 | uniobj = PyUnicode_FromObject(obj); |
| 6142 | if (uniobj == NULL) { |
| 6143 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6144 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 6145 | return 0; |
| 6146 | } |
| 6147 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 6148 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6149 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 6150 | Py_DECREF(uniobj); |
| 6151 | return 0; |
| 6152 | } |
| 6153 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 6154 | *fillcharloc = unistr[0]; |
| 6155 | Py_DECREF(uniobj); |
| 6156 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6157 | } |
| 6158 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6159 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6160 | "S.center(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6161 | \n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6162 | Return S centered in a Unicode string of length width. Padding is\n\ |
| 6163 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6164 | |
| 6165 | static PyObject * |
| 6166 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 6167 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6168 | Py_ssize_t marg, left; |
| 6169 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6170 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6171 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6172 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6173 | return NULL; |
| 6174 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6175 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6176 | Py_INCREF(self); |
| 6177 | return (PyObject*) self; |
| 6178 | } |
| 6179 | |
| 6180 | marg = width - self->length; |
| 6181 | left = marg / 2 + (marg & width & 1); |
| 6182 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6183 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6184 | } |
| 6185 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6186 | #if 0 |
| 6187 | |
| 6188 | /* This code should go into some future Unicode collation support |
| 6189 | module. The basic comparison should compare ordinals on a naive |
Georg Brandl | a3c242c | 2009-10-27 14:19:50 +0000 | [diff] [blame] | 6190 | basis (this is what Java does and thus Jython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6191 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6192 | /* speedy UTF-16 code point order comparison */ |
| 6193 | /* gleaned from: */ |
| 6194 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 6195 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6196 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6197 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6198 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6199 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 6200 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6201 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6202 | }; |
| 6203 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6204 | static int |
| 6205 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6206 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6207 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6208 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6209 | Py_UNICODE *s1 = str1->str; |
| 6210 | Py_UNICODE *s2 = str2->str; |
| 6211 | |
| 6212 | len1 = str1->length; |
| 6213 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6214 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6215 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6216 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6217 | |
| 6218 | c1 = *s1++; |
| 6219 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6220 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6221 | if (c1 > (1<<11) * 26) |
| 6222 | c1 += utf16Fixup[c1>>11]; |
| 6223 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6224 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6225 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6226 | |
| 6227 | if (c1 != c2) |
| 6228 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6229 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6230 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6231 | } |
| 6232 | |
| 6233 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6234 | } |
| 6235 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6236 | #else |
| 6237 | |
| 6238 | static int |
| 6239 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6240 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6241 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6242 | |
| 6243 | Py_UNICODE *s1 = str1->str; |
| 6244 | Py_UNICODE *s2 = str2->str; |
| 6245 | |
| 6246 | len1 = str1->length; |
| 6247 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6248 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6249 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6250 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6251 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6252 | c1 = *s1++; |
| 6253 | c2 = *s2++; |
| 6254 | |
| 6255 | if (c1 != c2) |
| 6256 | return (c1 < c2) ? -1 : 1; |
| 6257 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6258 | len1--; len2--; |
| 6259 | } |
| 6260 | |
| 6261 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6262 | } |
| 6263 | |
| 6264 | #endif |
| 6265 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6266 | int PyUnicode_Compare(PyObject *left, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6267 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6268 | { |
| 6269 | PyUnicodeObject *u = NULL, *v = NULL; |
| 6270 | int result; |
| 6271 | |
| 6272 | /* Coerce the two arguments */ |
| 6273 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 6274 | if (u == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6275 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6276 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 6277 | if (v == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6278 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6279 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6280 | /* Shortcut for empty or interned objects */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6281 | if (v == u) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6282 | Py_DECREF(u); |
| 6283 | Py_DECREF(v); |
| 6284 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6285 | } |
| 6286 | |
| 6287 | result = unicode_compare(u, v); |
| 6288 | |
| 6289 | Py_DECREF(u); |
| 6290 | Py_DECREF(v); |
| 6291 | return result; |
| 6292 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6293 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6294 | Py_XDECREF(u); |
| 6295 | Py_XDECREF(v); |
| 6296 | return -1; |
| 6297 | } |
| 6298 | |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 6299 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 6300 | PyObject *right, |
| 6301 | int op) |
| 6302 | { |
| 6303 | int result; |
| 6304 | |
| 6305 | result = PyUnicode_Compare(left, right); |
| 6306 | if (result == -1 && PyErr_Occurred()) |
| 6307 | goto onError; |
| 6308 | |
| 6309 | /* Convert the return value to a Boolean */ |
| 6310 | switch (op) { |
| 6311 | case Py_EQ: |
| 6312 | result = (result == 0); |
| 6313 | break; |
| 6314 | case Py_NE: |
| 6315 | result = (result != 0); |
| 6316 | break; |
| 6317 | case Py_LE: |
| 6318 | result = (result <= 0); |
| 6319 | break; |
| 6320 | case Py_GE: |
| 6321 | result = (result >= 0); |
| 6322 | break; |
| 6323 | case Py_LT: |
| 6324 | result = (result == -1); |
| 6325 | break; |
| 6326 | case Py_GT: |
| 6327 | result = (result == 1); |
| 6328 | break; |
| 6329 | } |
| 6330 | return PyBool_FromLong(result); |
| 6331 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6332 | onError: |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 6333 | |
| 6334 | /* Standard case |
| 6335 | |
| 6336 | Type errors mean that PyUnicode_FromObject() could not convert |
| 6337 | one of the arguments (usually the right hand side) to Unicode, |
| 6338 | ie. we can't handle the comparison request. However, it is |
| 6339 | possible that the other object knows a comparison method, which |
| 6340 | is why we return Py_NotImplemented to give the other object a |
| 6341 | chance. |
| 6342 | |
| 6343 | */ |
| 6344 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 6345 | PyErr_Clear(); |
| 6346 | Py_INCREF(Py_NotImplemented); |
| 6347 | return Py_NotImplemented; |
| 6348 | } |
| 6349 | if (op != Py_EQ && op != Py_NE) |
| 6350 | return NULL; |
| 6351 | |
| 6352 | /* Equality comparison. |
| 6353 | |
| 6354 | This is a special case: we silence any PyExc_UnicodeDecodeError |
| 6355 | and instead turn it into a PyErr_UnicodeWarning. |
| 6356 | |
| 6357 | */ |
| 6358 | if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) |
| 6359 | return NULL; |
| 6360 | PyErr_Clear(); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 6361 | if (PyErr_Warn(PyExc_UnicodeWarning, |
| 6362 | (op == Py_EQ) ? |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 6363 | "Unicode equal comparison " |
| 6364 | "failed to convert both arguments to Unicode - " |
| 6365 | "interpreting them as being unequal" : |
| 6366 | "Unicode unequal comparison " |
| 6367 | "failed to convert both arguments to Unicode - " |
| 6368 | "interpreting them as being unequal" |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6369 | ) < 0) |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 6370 | return NULL; |
| 6371 | result = (op == Py_NE); |
| 6372 | return PyBool_FromLong(result); |
| 6373 | } |
| 6374 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6375 | int PyUnicode_Contains(PyObject *container, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6376 | PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6377 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6378 | PyObject *str, *sub; |
| 6379 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6380 | |
| 6381 | /* Coerce the two arguments */ |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6382 | sub = PyUnicode_FromObject(element); |
| 6383 | if (!sub) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6384 | PyErr_SetString(PyExc_TypeError, |
| 6385 | "'in <string>' requires string as left operand"); |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 6386 | return -1; |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 6387 | } |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 6388 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6389 | str = PyUnicode_FromObject(container); |
| 6390 | if (!str) { |
| 6391 | Py_DECREF(sub); |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 6392 | return -1; |
| 6393 | } |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6394 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6395 | result = stringlib_contains_obj(str, sub); |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 6396 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6397 | Py_DECREF(str); |
| 6398 | Py_DECREF(sub); |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6399 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6400 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6401 | } |
| 6402 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6403 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 6404 | |
| 6405 | PyObject *PyUnicode_Concat(PyObject *left, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6406 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6407 | { |
| 6408 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 6409 | |
| 6410 | /* Coerce the two arguments */ |
| 6411 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 6412 | if (u == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6413 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6414 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 6415 | if (v == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6416 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6417 | |
| 6418 | /* Shortcuts */ |
| 6419 | if (v == unicode_empty) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6420 | Py_DECREF(v); |
| 6421 | return (PyObject *)u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6422 | } |
| 6423 | if (u == unicode_empty) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6424 | Py_DECREF(u); |
| 6425 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6426 | } |
| 6427 | |
| 6428 | /* Concat the two Unicode strings */ |
| 6429 | w = _PyUnicode_New(u->length + v->length); |
| 6430 | if (w == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6431 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6432 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 6433 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 6434 | |
| 6435 | Py_DECREF(u); |
| 6436 | Py_DECREF(v); |
| 6437 | return (PyObject *)w; |
| 6438 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6439 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6440 | Py_XDECREF(u); |
| 6441 | Py_XDECREF(v); |
| 6442 | return NULL; |
| 6443 | } |
| 6444 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6445 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6446 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6447 | \n\ |
Fredrik Lundh | 763b50f | 2006-05-22 15:35:12 +0000 | [diff] [blame] | 6448 | Return the number of non-overlapping occurrences of substring sub in\n\ |
| 6449 | 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] | 6450 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6451 | |
| 6452 | static PyObject * |
| 6453 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 6454 | { |
| 6455 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6456 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6457 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6458 | PyObject *result; |
| 6459 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6460 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6461 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6462 | return NULL; |
| 6463 | |
| 6464 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6465 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6466 | if (substring == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6467 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6468 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 6469 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6470 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6471 | result = PyInt_FromSsize_t( |
| 6472 | stringlib_count(self->str + start, end - start, |
| 6473 | substring->str, substring->length) |
| 6474 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6475 | |
| 6476 | Py_DECREF(substring); |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6477 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6478 | return result; |
| 6479 | } |
| 6480 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6481 | PyDoc_STRVAR(encode__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6482 | "S.encode([encoding[,errors]]) -> string or unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6483 | \n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6484 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 6485 | 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] | 6486 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6487 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 6488 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 6489 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6490 | |
| 6491 | static PyObject * |
| 6492 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 6493 | { |
| 6494 | char *encoding = NULL; |
| 6495 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6496 | PyObject *v; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 6497 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6498 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 6499 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6500 | v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6501 | if (v == NULL) |
| 6502 | goto onError; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 6503 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6504 | PyErr_Format(PyExc_TypeError, |
| 6505 | "encoder did not return a string/unicode object " |
| 6506 | "(type=%.400s)", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6507 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6508 | Py_DECREF(v); |
| 6509 | return NULL; |
| 6510 | } |
| 6511 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6512 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6513 | onError: |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6514 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6515 | } |
| 6516 | |
| 6517 | PyDoc_STRVAR(decode__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6518 | "S.decode([encoding[,errors]]) -> string or unicode\n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6519 | \n\ |
| 6520 | Decodes S using the codec registered for encoding. encoding defaults\n\ |
| 6521 | to the default encoding. errors may be given to set a different error\n\ |
| 6522 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 6523 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 6524 | as well as any other name registerd with codecs.register_error that is\n\ |
| 6525 | able to handle UnicodeDecodeErrors."); |
| 6526 | |
| 6527 | static PyObject * |
Marc-André Lemburg | 126b44c | 2004-07-10 12:04:20 +0000 | [diff] [blame] | 6528 | unicode_decode(PyUnicodeObject *self, PyObject *args) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6529 | { |
| 6530 | char *encoding = NULL; |
| 6531 | char *errors = NULL; |
| 6532 | PyObject *v; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 6533 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6534 | if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
| 6535 | return NULL; |
| 6536 | v = PyUnicode_AsDecodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6537 | if (v == NULL) |
| 6538 | goto onError; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 6539 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6540 | PyErr_Format(PyExc_TypeError, |
| 6541 | "decoder did not return a string/unicode object " |
| 6542 | "(type=%.400s)", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6543 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6544 | Py_DECREF(v); |
| 6545 | return NULL; |
| 6546 | } |
| 6547 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6548 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6549 | onError: |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6550 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6551 | } |
| 6552 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6553 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6554 | "S.expandtabs([tabsize]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6555 | \n\ |
| 6556 | 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] | 6557 | 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] | 6558 | |
| 6559 | static PyObject* |
| 6560 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 6561 | { |
| 6562 | Py_UNICODE *e; |
| 6563 | Py_UNICODE *p; |
| 6564 | Py_UNICODE *q; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6565 | Py_UNICODE *qe; |
| 6566 | Py_ssize_t i, j, incr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6567 | PyUnicodeObject *u; |
| 6568 | int tabsize = 8; |
| 6569 | |
| 6570 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6571 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6572 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6573 | /* First pass: determine size of output string */ |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6574 | i = 0; /* chars up to and including most recent \n or \r */ |
| 6575 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
| 6576 | e = self->str + self->length; /* end of input */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6577 | for (p = self->str; p < e; p++) |
| 6578 | if (*p == '\t') { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6579 | if (tabsize > 0) { |
| 6580 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 6581 | if (j > PY_SSIZE_T_MAX - incr) |
| 6582 | goto overflow1; |
| 6583 | j += incr; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6584 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6585 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6586 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6587 | if (j > PY_SSIZE_T_MAX - 1) |
| 6588 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6589 | j++; |
| 6590 | if (*p == '\n' || *p == '\r') { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6591 | if (i > PY_SSIZE_T_MAX - j) |
| 6592 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6593 | i += j; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6594 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6595 | } |
| 6596 | } |
| 6597 | |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6598 | if (i > PY_SSIZE_T_MAX - j) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6599 | goto overflow1; |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 6600 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6601 | /* Second pass: create output string and fill it */ |
| 6602 | u = _PyUnicode_New(i + j); |
| 6603 | if (!u) |
| 6604 | return NULL; |
| 6605 | |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6606 | j = 0; /* same as in first pass */ |
| 6607 | q = u->str; /* next output char */ |
| 6608 | qe = u->str + u->length; /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6609 | |
| 6610 | for (p = self->str; p < e; p++) |
| 6611 | if (*p == '\t') { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6612 | if (tabsize > 0) { |
| 6613 | i = tabsize - (j % tabsize); |
| 6614 | j += i; |
| 6615 | while (i--) { |
| 6616 | if (q >= qe) |
| 6617 | goto overflow2; |
| 6618 | *q++ = ' '; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6619 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6620 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 6621 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6622 | else { |
| 6623 | if (q >= qe) |
| 6624 | goto overflow2; |
| 6625 | *q++ = *p; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6626 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6627 | if (*p == '\n' || *p == '\r') |
| 6628 | j = 0; |
| 6629 | } |
| 6630 | |
| 6631 | return (PyObject*) u; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6632 | |
| 6633 | overflow2: |
| 6634 | Py_DECREF(u); |
| 6635 | overflow1: |
| 6636 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 6637 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6638 | } |
| 6639 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6640 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6641 | "S.find(sub [,start [,end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6642 | \n\ |
| 6643 | Return the lowest index in S where substring sub is found,\n\ |
Georg Brandl | 9efd9b6 | 2007-07-29 17:38:35 +0000 | [diff] [blame] | 6644 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6645 | arguments start and end are interpreted as in slice notation.\n\ |
| 6646 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6647 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6648 | |
| 6649 | static PyObject * |
| 6650 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 6651 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6652 | PyObject *substring; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 6653 | Py_ssize_t start; |
| 6654 | Py_ssize_t end; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6655 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6656 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 6657 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6658 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6659 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 6660 | result = stringlib_find_slice( |
| 6661 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6662 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6663 | start, end |
| 6664 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6665 | |
| 6666 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6667 | |
| 6668 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6669 | } |
| 6670 | |
| 6671 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6672 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6673 | { |
| 6674 | if (index < 0 || index >= self->length) { |
| 6675 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 6676 | return NULL; |
| 6677 | } |
| 6678 | |
| 6679 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 6680 | } |
| 6681 | |
| 6682 | static long |
| 6683 | unicode_hash(PyUnicodeObject *self) |
| 6684 | { |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6685 | /* Since Unicode objects compare equal to their ASCII string |
| 6686 | counterparts, they should use the individual character values |
| 6687 | as basis for their hash value. This is needed to assure that |
| 6688 | strings and Unicode objects behave in the same way as |
| 6689 | dictionary keys. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6690 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6691 | register Py_ssize_t len; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6692 | register Py_UNICODE *p; |
| 6693 | register long x; |
| 6694 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6695 | if (self->hash != -1) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6696 | return self->hash; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6697 | len = PyUnicode_GET_SIZE(self); |
| 6698 | p = PyUnicode_AS_UNICODE(self); |
| 6699 | x = *p << 7; |
| 6700 | while (--len >= 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6701 | x = (1000003*x) ^ *p++; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6702 | x ^= PyUnicode_GET_SIZE(self); |
| 6703 | if (x == -1) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6704 | x = -2; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6705 | self->hash = x; |
| 6706 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6707 | } |
| 6708 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6709 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6710 | "S.index(sub [,start [,end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6711 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6712 | 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] | 6713 | |
| 6714 | static PyObject * |
| 6715 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 6716 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6717 | Py_ssize_t result; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6718 | PyObject *substring; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 6719 | Py_ssize_t start; |
| 6720 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6721 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 6722 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6723 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6724 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 6725 | result = stringlib_find_slice( |
| 6726 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6727 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6728 | start, end |
| 6729 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6730 | |
| 6731 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6732 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6733 | if (result < 0) { |
| 6734 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 6735 | return NULL; |
| 6736 | } |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6737 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6738 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6739 | } |
| 6740 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6741 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6742 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6743 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6744 | 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] | 6745 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6746 | |
| 6747 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6748 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6749 | { |
| 6750 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6751 | register const Py_UNICODE *e; |
| 6752 | int cased; |
| 6753 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6754 | /* Shortcut for single character strings */ |
| 6755 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6756 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6757 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6758 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6759 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6760 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6761 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6762 | e = p + PyUnicode_GET_SIZE(self); |
| 6763 | cased = 0; |
| 6764 | for (; p < e; p++) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6765 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6766 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6767 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 6768 | return PyBool_FromLong(0); |
| 6769 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 6770 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6771 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6772 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6773 | } |
| 6774 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6775 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6776 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6777 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6778 | 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] | 6779 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6780 | |
| 6781 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6782 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6783 | { |
| 6784 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6785 | register const Py_UNICODE *e; |
| 6786 | int cased; |
| 6787 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6788 | /* Shortcut for single character strings */ |
| 6789 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6790 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6791 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6792 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6793 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6794 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6795 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6796 | e = p + PyUnicode_GET_SIZE(self); |
| 6797 | cased = 0; |
| 6798 | for (; p < e; p++) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6799 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6800 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6801 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 6802 | return PyBool_FromLong(0); |
| 6803 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 6804 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6805 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6806 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6807 | } |
| 6808 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6809 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6810 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6811 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6812 | Return True if S is a titlecased string and there is at least one\n\ |
| 6813 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 6814 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 6815 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6816 | |
| 6817 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6818 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6819 | { |
| 6820 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6821 | register const Py_UNICODE *e; |
| 6822 | int cased, previous_is_cased; |
| 6823 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6824 | /* Shortcut for single character strings */ |
| 6825 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6826 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 6827 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6828 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6829 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6830 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6831 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6832 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6833 | e = p + PyUnicode_GET_SIZE(self); |
| 6834 | cased = 0; |
| 6835 | previous_is_cased = 0; |
| 6836 | for (; p < e; p++) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6837 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6838 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6839 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 6840 | if (previous_is_cased) |
| 6841 | return PyBool_FromLong(0); |
| 6842 | previous_is_cased = 1; |
| 6843 | cased = 1; |
| 6844 | } |
| 6845 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 6846 | if (!previous_is_cased) |
| 6847 | return PyBool_FromLong(0); |
| 6848 | previous_is_cased = 1; |
| 6849 | cased = 1; |
| 6850 | } |
| 6851 | else |
| 6852 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6853 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6854 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6855 | } |
| 6856 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6857 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6858 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6859 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6860 | Return True if all characters in S are whitespace\n\ |
| 6861 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6862 | |
| 6863 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6864 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6865 | { |
| 6866 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6867 | register const Py_UNICODE *e; |
| 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 | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6871 | Py_UNICODE_ISSPACE(*p)) |
| 6872 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6873 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6874 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6875 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6876 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6877 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6878 | e = p + PyUnicode_GET_SIZE(self); |
| 6879 | for (; p < e; p++) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6880 | if (!Py_UNICODE_ISSPACE(*p)) |
| 6881 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6882 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6883 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6884 | } |
| 6885 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6886 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6887 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6888 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6889 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6890 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6891 | |
| 6892 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6893 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6894 | { |
| 6895 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6896 | register const Py_UNICODE *e; |
| 6897 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6898 | /* Shortcut for single character strings */ |
| 6899 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6900 | Py_UNICODE_ISALPHA(*p)) |
| 6901 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6902 | |
| 6903 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6904 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6905 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6906 | |
| 6907 | e = p + PyUnicode_GET_SIZE(self); |
| 6908 | for (; p < e; p++) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6909 | if (!Py_UNICODE_ISALPHA(*p)) |
| 6910 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6911 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6912 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6913 | } |
| 6914 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6915 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6916 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6917 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6918 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6919 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6920 | |
| 6921 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6922 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6923 | { |
| 6924 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6925 | register const Py_UNICODE *e; |
| 6926 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6927 | /* Shortcut for single character strings */ |
| 6928 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6929 | Py_UNICODE_ISALNUM(*p)) |
| 6930 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6931 | |
| 6932 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6933 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6934 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6935 | |
| 6936 | e = p + PyUnicode_GET_SIZE(self); |
| 6937 | for (; p < e; p++) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6938 | if (!Py_UNICODE_ISALNUM(*p)) |
| 6939 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6940 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6941 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6942 | } |
| 6943 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6944 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6945 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6946 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6947 | 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] | 6948 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6949 | |
| 6950 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6951 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6952 | { |
| 6953 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6954 | register const Py_UNICODE *e; |
| 6955 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6956 | /* Shortcut for single character strings */ |
| 6957 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6958 | Py_UNICODE_ISDECIMAL(*p)) |
| 6959 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6960 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6961 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6962 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6963 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6964 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6965 | e = p + PyUnicode_GET_SIZE(self); |
| 6966 | for (; p < e; p++) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6967 | if (!Py_UNICODE_ISDECIMAL(*p)) |
| 6968 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6969 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6970 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6971 | } |
| 6972 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6973 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6974 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6975 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6976 | Return True if all characters in S are digits\n\ |
| 6977 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6978 | |
| 6979 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6980 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6981 | { |
| 6982 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6983 | register const Py_UNICODE *e; |
| 6984 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6985 | /* Shortcut for single character strings */ |
| 6986 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6987 | Py_UNICODE_ISDIGIT(*p)) |
| 6988 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6989 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6990 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6991 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6992 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6993 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6994 | e = p + PyUnicode_GET_SIZE(self); |
| 6995 | for (; p < e; p++) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6996 | if (!Py_UNICODE_ISDIGIT(*p)) |
| 6997 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6998 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6999 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7000 | } |
| 7001 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7002 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7003 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7004 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7005 | 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] | 7006 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7007 | |
| 7008 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7009 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7010 | { |
| 7011 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7012 | register const Py_UNICODE *e; |
| 7013 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7014 | /* Shortcut for single character strings */ |
| 7015 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7016 | Py_UNICODE_ISNUMERIC(*p)) |
| 7017 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7018 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7019 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7020 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7021 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7022 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7023 | e = p + PyUnicode_GET_SIZE(self); |
| 7024 | for (; p < e; p++) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7025 | if (!Py_UNICODE_ISNUMERIC(*p)) |
| 7026 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7027 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7028 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7029 | } |
| 7030 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7031 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 5d2eb34 | 2009-10-27 15:08:27 +0000 | [diff] [blame] | 7032 | "S.join(iterable) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7033 | \n\ |
| 7034 | Return a string which is the concatenation of the strings in the\n\ |
Georg Brandl | 5d2eb34 | 2009-10-27 15:08:27 +0000 | [diff] [blame] | 7035 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7036 | |
| 7037 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7038 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7039 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7040 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7041 | } |
| 7042 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7043 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7044 | unicode_length(PyUnicodeObject *self) |
| 7045 | { |
| 7046 | return self->length; |
| 7047 | } |
| 7048 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7049 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7050 | "S.ljust(width[, fillchar]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7051 | \n\ |
Benjamin Peterson | be2c0a9 | 2008-10-04 21:33:08 +0000 | [diff] [blame] | 7052 | 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] | 7053 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7054 | |
| 7055 | static PyObject * |
| 7056 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 7057 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7058 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7059 | Py_UNICODE fillchar = ' '; |
| 7060 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7061 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7062 | return NULL; |
| 7063 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7064 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7065 | Py_INCREF(self); |
| 7066 | return (PyObject*) self; |
| 7067 | } |
| 7068 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7069 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7070 | } |
| 7071 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7072 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7073 | "S.lower() -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7074 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7075 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7076 | |
| 7077 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7078 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7079 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7080 | return fixup(self, fixlower); |
| 7081 | } |
| 7082 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7083 | #define LEFTSTRIP 0 |
| 7084 | #define RIGHTSTRIP 1 |
| 7085 | #define BOTHSTRIP 2 |
| 7086 | |
| 7087 | /* Arrays indexed by above */ |
| 7088 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 7089 | |
| 7090 | #define STRIPNAME(i) (stripformat[i]+3) |
| 7091 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7092 | /* externally visible for str.strip(unicode) */ |
| 7093 | PyObject * |
| 7094 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 7095 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7096 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7097 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
| 7098 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
| 7099 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 7100 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7101 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7102 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 7103 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7104 | i = 0; |
| 7105 | if (striptype != RIGHTSTRIP) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7106 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7107 | i++; |
| 7108 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7109 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7110 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7111 | j = len; |
| 7112 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7113 | do { |
| 7114 | j--; |
| 7115 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 7116 | j++; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7117 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7118 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7119 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7120 | Py_INCREF(self); |
| 7121 | return (PyObject*)self; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7122 | } |
| 7123 | else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7124 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7125 | } |
| 7126 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7127 | |
| 7128 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7129 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7130 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7131 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7132 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7133 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7134 | i = 0; |
| 7135 | if (striptype != RIGHTSTRIP) { |
| 7136 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 7137 | i++; |
| 7138 | } |
| 7139 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7140 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7141 | j = len; |
| 7142 | if (striptype != LEFTSTRIP) { |
| 7143 | do { |
| 7144 | j--; |
| 7145 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 7146 | j++; |
| 7147 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7148 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7149 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 7150 | Py_INCREF(self); |
| 7151 | return (PyObject*)self; |
| 7152 | } |
| 7153 | else |
| 7154 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7155 | } |
| 7156 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7157 | |
| 7158 | static PyObject * |
| 7159 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 7160 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7161 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7162 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7163 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 7164 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7165 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7166 | if (sep != NULL && sep != Py_None) { |
| 7167 | if (PyUnicode_Check(sep)) |
| 7168 | return _PyUnicode_XStrip(self, striptype, sep); |
| 7169 | else if (PyString_Check(sep)) { |
| 7170 | PyObject *res; |
| 7171 | sep = PyUnicode_FromObject(sep); |
| 7172 | if (sep==NULL) |
| 7173 | return NULL; |
| 7174 | res = _PyUnicode_XStrip(self, striptype, sep); |
| 7175 | Py_DECREF(sep); |
| 7176 | return res; |
| 7177 | } |
| 7178 | else { |
| 7179 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7180 | "%s arg must be None, unicode or str", |
| 7181 | STRIPNAME(striptype)); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7182 | return NULL; |
| 7183 | } |
| 7184 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7185 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7186 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7187 | } |
| 7188 | |
| 7189 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7190 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7191 | "S.strip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7192 | \n\ |
| 7193 | Return a copy of the string S with leading and trailing\n\ |
| 7194 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7195 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7196 | 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] | 7197 | |
| 7198 | static PyObject * |
| 7199 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 7200 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7201 | if (PyTuple_GET_SIZE(args) == 0) |
| 7202 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 7203 | else |
| 7204 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7205 | } |
| 7206 | |
| 7207 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7208 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7209 | "S.lstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7210 | \n\ |
| 7211 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7212 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7213 | 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] | 7214 | |
| 7215 | static PyObject * |
| 7216 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 7217 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7218 | if (PyTuple_GET_SIZE(args) == 0) |
| 7219 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 7220 | else |
| 7221 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7222 | } |
| 7223 | |
| 7224 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7225 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7226 | "S.rstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7227 | \n\ |
| 7228 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7229 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7230 | 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] | 7231 | |
| 7232 | static PyObject * |
| 7233 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 7234 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7235 | if (PyTuple_GET_SIZE(args) == 0) |
| 7236 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 7237 | else |
| 7238 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7239 | } |
| 7240 | |
| 7241 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7242 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7243 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7244 | { |
| 7245 | PyUnicodeObject *u; |
| 7246 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7247 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7248 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7249 | |
| 7250 | if (len < 0) |
| 7251 | len = 0; |
| 7252 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7253 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7254 | /* no repeat, return original string */ |
| 7255 | Py_INCREF(str); |
| 7256 | return (PyObject*) str; |
| 7257 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7258 | |
| 7259 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 7260 | * needed doesn't overflow size_t |
| 7261 | */ |
| 7262 | nchars = len * str->length; |
| 7263 | if (len && nchars / len != str->length) { |
| 7264 | PyErr_SetString(PyExc_OverflowError, |
| 7265 | "repeated string is too long"); |
| 7266 | return NULL; |
| 7267 | } |
| 7268 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 7269 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 7270 | PyErr_SetString(PyExc_OverflowError, |
| 7271 | "repeated string is too long"); |
| 7272 | return NULL; |
| 7273 | } |
| 7274 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7275 | if (!u) |
| 7276 | return NULL; |
| 7277 | |
| 7278 | p = u->str; |
| 7279 | |
Fredrik Lundh | f1d60a5 | 2006-05-22 16:29:30 +0000 | [diff] [blame] | 7280 | if (str->length == 1 && len > 0) { |
| 7281 | Py_UNICODE_FILL(p, str->str[0], len); |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 7282 | } else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7283 | Py_ssize_t done = 0; /* number of characters copied this far */ |
| 7284 | if (done < nchars) { |
Fredrik Lundh | f1d60a5 | 2006-05-22 16:29:30 +0000 | [diff] [blame] | 7285 | Py_UNICODE_COPY(p, str->str, str->length); |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 7286 | done = str->length; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7287 | } |
| 7288 | while (done < nchars) { |
Neal Norwitz | 4677fbf7 | 2008-03-25 04:18:18 +0000 | [diff] [blame] | 7289 | Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 7290 | Py_UNICODE_COPY(p+done, p, n); |
| 7291 | done += n; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7292 | } |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 7293 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7294 | |
| 7295 | return (PyObject*) u; |
| 7296 | } |
| 7297 | |
| 7298 | PyObject *PyUnicode_Replace(PyObject *obj, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7299 | PyObject *subobj, |
| 7300 | PyObject *replobj, |
| 7301 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7302 | { |
| 7303 | PyObject *self; |
| 7304 | PyObject *str1; |
| 7305 | PyObject *str2; |
| 7306 | PyObject *result; |
| 7307 | |
| 7308 | self = PyUnicode_FromObject(obj); |
| 7309 | if (self == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7310 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7311 | str1 = PyUnicode_FromObject(subobj); |
| 7312 | if (str1 == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7313 | Py_DECREF(self); |
| 7314 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7315 | } |
| 7316 | str2 = PyUnicode_FromObject(replobj); |
| 7317 | if (str2 == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7318 | Py_DECREF(self); |
| 7319 | Py_DECREF(str1); |
| 7320 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7321 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7322 | result = replace((PyUnicodeObject *)self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7323 | (PyUnicodeObject *)str1, |
| 7324 | (PyUnicodeObject *)str2, |
| 7325 | maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7326 | Py_DECREF(self); |
| 7327 | Py_DECREF(str1); |
| 7328 | Py_DECREF(str2); |
| 7329 | return result; |
| 7330 | } |
| 7331 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7332 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | 6327bf1 | 2010-06-26 18:47:01 +0000 | [diff] [blame] | 7333 | "S.replace(old, new[, count]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7334 | \n\ |
| 7335 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | 30fadc1 | 2008-05-30 07:54:16 +0000 | [diff] [blame] | 7336 | old replaced by new. If the optional argument count is\n\ |
| 7337 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7338 | |
| 7339 | static PyObject* |
| 7340 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 7341 | { |
| 7342 | PyUnicodeObject *str1; |
| 7343 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7344 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7345 | PyObject *result; |
| 7346 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7347 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7348 | return NULL; |
| 7349 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 7350 | if (str1 == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7351 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7352 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7353 | if (str2 == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7354 | Py_DECREF(str1); |
| 7355 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7356 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7357 | |
| 7358 | result = replace(self, str1, str2, maxcount); |
| 7359 | |
| 7360 | Py_DECREF(str1); |
| 7361 | Py_DECREF(str2); |
| 7362 | return result; |
| 7363 | } |
| 7364 | |
| 7365 | static |
| 7366 | PyObject *unicode_repr(PyObject *unicode) |
| 7367 | { |
| 7368 | return unicodeescape_string(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7369 | PyUnicode_GET_SIZE(unicode), |
| 7370 | 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7371 | } |
| 7372 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7373 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7374 | "S.rfind(sub [,start [,end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7375 | \n\ |
| 7376 | Return the highest index in S where substring sub is found,\n\ |
Georg Brandl | 9efd9b6 | 2007-07-29 17:38:35 +0000 | [diff] [blame] | 7377 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7378 | arguments start and end are interpreted as in slice notation.\n\ |
| 7379 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7380 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7381 | |
| 7382 | static PyObject * |
| 7383 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 7384 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7385 | PyObject *substring; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 7386 | Py_ssize_t start; |
| 7387 | Py_ssize_t end; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7388 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7389 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 7390 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7391 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7392 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 7393 | result = stringlib_rfind_slice( |
| 7394 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7395 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7396 | start, end |
| 7397 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7398 | |
| 7399 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7400 | |
| 7401 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7402 | } |
| 7403 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7404 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7405 | "S.rindex(sub [,start [,end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7406 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7407 | 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] | 7408 | |
| 7409 | static PyObject * |
| 7410 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 7411 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7412 | PyObject *substring; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 7413 | Py_ssize_t start; |
| 7414 | Py_ssize_t end; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7415 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7416 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 7417 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7418 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7419 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 7420 | result = stringlib_rfind_slice( |
| 7421 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7422 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7423 | start, end |
| 7424 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7425 | |
| 7426 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7427 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7428 | if (result < 0) { |
| 7429 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7430 | return NULL; |
| 7431 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7432 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7433 | } |
| 7434 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7435 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7436 | "S.rjust(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7437 | \n\ |
Benjamin Peterson | be2c0a9 | 2008-10-04 21:33:08 +0000 | [diff] [blame] | 7438 | 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] | 7439 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7440 | |
| 7441 | static PyObject * |
| 7442 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 7443 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7444 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7445 | Py_UNICODE fillchar = ' '; |
| 7446 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7447 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7448 | return NULL; |
| 7449 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7450 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7451 | Py_INCREF(self); |
| 7452 | return (PyObject*) self; |
| 7453 | } |
| 7454 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7455 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7456 | } |
| 7457 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7458 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7459 | 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] | 7460 | { |
| 7461 | /* standard clamping */ |
| 7462 | if (start < 0) |
| 7463 | start = 0; |
| 7464 | if (end < 0) |
| 7465 | end = 0; |
| 7466 | if (end > self->length) |
| 7467 | end = self->length; |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7468 | if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7469 | /* full slice, return original string */ |
| 7470 | Py_INCREF(self); |
| 7471 | return (PyObject*) self; |
| 7472 | } |
| 7473 | if (start > end) |
| 7474 | start = end; |
| 7475 | /* copy slice */ |
| 7476 | return (PyObject*) PyUnicode_FromUnicode(self->str + start, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7477 | end - start); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7478 | } |
| 7479 | |
| 7480 | PyObject *PyUnicode_Split(PyObject *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7481 | PyObject *sep, |
| 7482 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7483 | { |
| 7484 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7485 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7486 | s = PyUnicode_FromObject(s); |
| 7487 | if (s == NULL) |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7488 | return NULL; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7489 | if (sep != NULL) { |
| 7490 | sep = PyUnicode_FromObject(sep); |
| 7491 | if (sep == NULL) { |
| 7492 | Py_DECREF(s); |
| 7493 | return NULL; |
| 7494 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7495 | } |
| 7496 | |
| 7497 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7498 | |
| 7499 | Py_DECREF(s); |
| 7500 | Py_XDECREF(sep); |
| 7501 | return result; |
| 7502 | } |
| 7503 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7504 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7505 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7506 | \n\ |
| 7507 | Return a list of the words in S, using sep as the\n\ |
| 7508 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Georg Brandl | dfb77db | 2008-05-11 09:11:40 +0000 | [diff] [blame] | 7509 | 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] | 7510 | whitespace string is a separator and empty strings are\n\ |
| 7511 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7512 | |
| 7513 | static PyObject* |
| 7514 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 7515 | { |
| 7516 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7517 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7518 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7519 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7520 | return NULL; |
| 7521 | |
| 7522 | if (substring == Py_None) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7523 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7524 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7525 | return split(self, (PyUnicodeObject *)substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7526 | else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7527 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7528 | } |
| 7529 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7530 | PyObject * |
| 7531 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 7532 | { |
| 7533 | PyObject* str_obj; |
| 7534 | PyObject* sep_obj; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7535 | PyObject* out; |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 7536 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7537 | str_obj = PyUnicode_FromObject(str_in); |
| 7538 | if (!str_obj) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7539 | return NULL; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7540 | sep_obj = PyUnicode_FromObject(sep_in); |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 7541 | if (!sep_obj) { |
| 7542 | Py_DECREF(str_obj); |
| 7543 | return NULL; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7544 | } |
| 7545 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 7546 | out = stringlib_partition( |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 7547 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7548 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7549 | ); |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7550 | |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 7551 | Py_DECREF(sep_obj); |
| 7552 | Py_DECREF(str_obj); |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7553 | |
| 7554 | return out; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7555 | } |
| 7556 | |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7557 | |
| 7558 | PyObject * |
| 7559 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 7560 | { |
| 7561 | PyObject* str_obj; |
| 7562 | PyObject* sep_obj; |
| 7563 | PyObject* out; |
| 7564 | |
| 7565 | str_obj = PyUnicode_FromObject(str_in); |
| 7566 | if (!str_obj) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7567 | return NULL; |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7568 | sep_obj = PyUnicode_FromObject(sep_in); |
| 7569 | if (!sep_obj) { |
| 7570 | Py_DECREF(str_obj); |
| 7571 | return NULL; |
| 7572 | } |
| 7573 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 7574 | out = stringlib_rpartition( |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7575 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7576 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7577 | ); |
| 7578 | |
| 7579 | Py_DECREF(sep_obj); |
| 7580 | Py_DECREF(str_obj); |
| 7581 | |
| 7582 | return out; |
| 7583 | } |
| 7584 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7585 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7586 | "S.partition(sep) -> (head, sep, tail)\n\ |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7587 | \n\ |
Benjamin Peterson | be2c0a9 | 2008-10-04 21:33:08 +0000 | [diff] [blame] | 7588 | 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] | 7589 | the separator itself, and the part after it. If the separator is not\n\ |
Benjamin Peterson | be2c0a9 | 2008-10-04 21:33:08 +0000 | [diff] [blame] | 7590 | found, return S and two empty strings."); |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7591 | |
| 7592 | static PyObject* |
Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 7593 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7594 | { |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7595 | return PyUnicode_Partition((PyObject *)self, separator); |
| 7596 | } |
| 7597 | |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7598 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | dabb5f7 | 2010-01-25 11:46:11 +0000 | [diff] [blame] | 7599 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7600 | \n\ |
Benjamin Peterson | be2c0a9 | 2008-10-04 21:33:08 +0000 | [diff] [blame] | 7601 | 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] | 7602 | the part before it, the separator itself, and the part after it. If the\n\ |
Benjamin Peterson | be2c0a9 | 2008-10-04 21:33:08 +0000 | [diff] [blame] | 7603 | separator is not found, return two empty strings and S."); |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7604 | |
| 7605 | static PyObject* |
| 7606 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 7607 | { |
| 7608 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 7609 | } |
| 7610 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7611 | PyObject *PyUnicode_RSplit(PyObject *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7612 | PyObject *sep, |
| 7613 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7614 | { |
| 7615 | PyObject *result; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7616 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7617 | s = PyUnicode_FromObject(s); |
| 7618 | if (s == NULL) |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7619 | return NULL; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7620 | if (sep != NULL) { |
| 7621 | sep = PyUnicode_FromObject(sep); |
| 7622 | if (sep == NULL) { |
| 7623 | Py_DECREF(s); |
| 7624 | return NULL; |
| 7625 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7626 | } |
| 7627 | |
| 7628 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7629 | |
| 7630 | Py_DECREF(s); |
| 7631 | Py_XDECREF(sep); |
| 7632 | return result; |
| 7633 | } |
| 7634 | |
| 7635 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7636 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7637 | \n\ |
| 7638 | Return a list of the words in S, using sep as the\n\ |
| 7639 | delimiter string, starting at the end of the string and\n\ |
| 7640 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 7641 | splits are done. If sep is not specified, any whitespace string\n\ |
| 7642 | is a separator."); |
| 7643 | |
| 7644 | static PyObject* |
| 7645 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 7646 | { |
| 7647 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7648 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7649 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7650 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7651 | return NULL; |
| 7652 | |
| 7653 | if (substring == Py_None) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7654 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7655 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7656 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7657 | else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7658 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7659 | } |
| 7660 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7661 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7662 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7663 | \n\ |
| 7664 | 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] | 7665 | 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] | 7666 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7667 | |
| 7668 | static PyObject* |
| 7669 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 7670 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7671 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7672 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7673 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7674 | return NULL; |
| 7675 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7676 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7677 | } |
| 7678 | |
| 7679 | static |
| 7680 | PyObject *unicode_str(PyUnicodeObject *self) |
| 7681 | { |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7682 | return PyUnicode_AsEncodedString((PyObject *)self, NULL, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7683 | } |
| 7684 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7685 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7686 | "S.swapcase() -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7687 | \n\ |
| 7688 | 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] | 7689 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7690 | |
| 7691 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7692 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7693 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7694 | return fixup(self, fixswapcase); |
| 7695 | } |
| 7696 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7697 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7698 | "S.translate(table) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7699 | \n\ |
| 7700 | Return a copy of the string S, where all characters have been mapped\n\ |
| 7701 | 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] | 7702 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ |
| 7703 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 7704 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7705 | |
| 7706 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7707 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7708 | { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7709 | return PyUnicode_TranslateCharmap(self->str, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7710 | self->length, |
| 7711 | table, |
| 7712 | "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7713 | } |
| 7714 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7715 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7716 | "S.upper() -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7717 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7718 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7719 | |
| 7720 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7721 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7722 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7723 | return fixup(self, fixupper); |
| 7724 | } |
| 7725 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7726 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7727 | "S.zfill(width) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7728 | \n\ |
Georg Brandl | 9806407 | 2008-09-09 19:26:00 +0000 | [diff] [blame] | 7729 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 7730 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7731 | |
| 7732 | static PyObject * |
| 7733 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 7734 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7735 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7736 | PyUnicodeObject *u; |
| 7737 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7738 | Py_ssize_t width; |
| 7739 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7740 | return NULL; |
| 7741 | |
| 7742 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 7743 | if (PyUnicode_CheckExact(self)) { |
| 7744 | Py_INCREF(self); |
| 7745 | return (PyObject*) self; |
| 7746 | } |
| 7747 | else |
| 7748 | return PyUnicode_FromUnicode( |
| 7749 | PyUnicode_AS_UNICODE(self), |
| 7750 | PyUnicode_GET_SIZE(self) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7751 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7752 | } |
| 7753 | |
| 7754 | fill = width - self->length; |
| 7755 | |
| 7756 | u = pad(self, fill, 0, '0'); |
| 7757 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 7758 | if (u == NULL) |
| 7759 | return NULL; |
| 7760 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7761 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 7762 | /* move sign to beginning of string */ |
| 7763 | u->str[0] = u->str[fill]; |
| 7764 | u->str[fill] = '0'; |
| 7765 | } |
| 7766 | |
| 7767 | return (PyObject*) u; |
| 7768 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7769 | |
| 7770 | #if 0 |
| 7771 | static PyObject* |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 7772 | free_listsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7773 | { |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 7774 | return PyInt_FromLong(numfree); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7775 | } |
| 7776 | #endif |
| 7777 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7778 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7779 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7780 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7781 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 7782 | With optional start, test S beginning at that position.\n\ |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7783 | With optional end, stop comparing S at that position.\n\ |
| 7784 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7785 | |
| 7786 | static PyObject * |
| 7787 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7788 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7789 | { |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7790 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7791 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7792 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7793 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7794 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7795 | |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7796 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7797 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 7798 | return NULL; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7799 | if (PyTuple_Check(subobj)) { |
| 7800 | Py_ssize_t i; |
| 7801 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 7802 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7803 | PyTuple_GET_ITEM(subobj, i)); |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7804 | if (substring == NULL) |
| 7805 | return NULL; |
| 7806 | result = tailmatch(self, substring, start, end, -1); |
| 7807 | Py_DECREF(substring); |
| 7808 | if (result) { |
| 7809 | Py_RETURN_TRUE; |
| 7810 | } |
| 7811 | } |
| 7812 | /* nothing matched */ |
| 7813 | Py_RETURN_FALSE; |
| 7814 | } |
| 7815 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7816 | if (substring == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7817 | return NULL; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7818 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7819 | Py_DECREF(substring); |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7820 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7821 | } |
| 7822 | |
| 7823 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7824 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7825 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7826 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7827 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 7828 | With optional start, test S beginning at that position.\n\ |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7829 | With optional end, stop comparing S at that position.\n\ |
| 7830 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7831 | |
| 7832 | static PyObject * |
| 7833 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7834 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7835 | { |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7836 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7837 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7838 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7839 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7840 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7841 | |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7842 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7843 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 7844 | return NULL; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7845 | if (PyTuple_Check(subobj)) { |
| 7846 | Py_ssize_t i; |
| 7847 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 7848 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7849 | PyTuple_GET_ITEM(subobj, i)); |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7850 | if (substring == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7851 | return NULL; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7852 | result = tailmatch(self, substring, start, end, +1); |
| 7853 | Py_DECREF(substring); |
| 7854 | if (result) { |
| 7855 | Py_RETURN_TRUE; |
| 7856 | } |
| 7857 | } |
| 7858 | Py_RETURN_FALSE; |
| 7859 | } |
| 7860 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7861 | if (substring == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7862 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7863 | |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7864 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7865 | Py_DECREF(substring); |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7866 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7867 | } |
| 7868 | |
| 7869 | |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 7870 | /* Implements do_string_format, which is unicode because of stringlib */ |
| 7871 | #include "stringlib/string_format.h" |
| 7872 | |
| 7873 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7874 | "S.format(*args, **kwargs) -> unicode\n\ |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 7875 | \n\ |
| 7876 | "); |
| 7877 | |
Eric Smith | dc13b79 | 2008-05-30 18:10:04 +0000 | [diff] [blame] | 7878 | static PyObject * |
| 7879 | unicode__format__(PyObject *self, PyObject *args) |
| 7880 | { |
| 7881 | PyObject *format_spec; |
| 7882 | PyObject *result = NULL; |
| 7883 | PyObject *tmp = NULL; |
| 7884 | |
| 7885 | /* If 2.x, convert format_spec to the same type as value */ |
| 7886 | /* This is to allow things like u''.format('') */ |
| 7887 | if (!PyArg_ParseTuple(args, "O:__format__", &format_spec)) |
| 7888 | goto done; |
| 7889 | if (!(PyBytes_Check(format_spec) || PyUnicode_Check(format_spec))) { |
| 7890 | PyErr_Format(PyExc_TypeError, "__format__ arg must be str " |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7891 | "or unicode, not %s", Py_TYPE(format_spec)->tp_name); |
Eric Smith | dc13b79 | 2008-05-30 18:10:04 +0000 | [diff] [blame] | 7892 | goto done; |
| 7893 | } |
| 7894 | tmp = PyObject_Unicode(format_spec); |
| 7895 | if (tmp == NULL) |
| 7896 | goto done; |
| 7897 | format_spec = tmp; |
| 7898 | |
| 7899 | result = _PyUnicode_FormatAdvanced(self, |
| 7900 | PyUnicode_AS_UNICODE(format_spec), |
| 7901 | PyUnicode_GET_SIZE(format_spec)); |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7902 | done: |
Eric Smith | dc13b79 | 2008-05-30 18:10:04 +0000 | [diff] [blame] | 7903 | Py_XDECREF(tmp); |
| 7904 | return result; |
| 7905 | } |
| 7906 | |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 7907 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7908 | "S.__format__(format_spec) -> unicode\n\ |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 7909 | \n\ |
| 7910 | "); |
| 7911 | |
Robert Schuppenies | 901c997 | 2008-06-10 10:10:31 +0000 | [diff] [blame] | 7912 | static PyObject * |
| 7913 | unicode__sizeof__(PyUnicodeObject *v) |
| 7914 | { |
Robert Schuppenies | 9be2ec1 | 2008-07-10 15:24:04 +0000 | [diff] [blame] | 7915 | return PyInt_FromSsize_t(sizeof(PyUnicodeObject) + |
| 7916 | sizeof(Py_UNICODE) * (v->length + 1)); |
Robert Schuppenies | 901c997 | 2008-06-10 10:10:31 +0000 | [diff] [blame] | 7917 | } |
| 7918 | |
| 7919 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7920 | "S.__sizeof__() -> size of S in memory, in bytes\n\ |
Robert Schuppenies | 901c997 | 2008-06-10 10:10:31 +0000 | [diff] [blame] | 7921 | \n\ |
| 7922 | "); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 7923 | |
| 7924 | static PyObject * |
| 7925 | unicode_getnewargs(PyUnicodeObject *v) |
| 7926 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7927 | return Py_BuildValue("(u#)", v->str, v->length); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 7928 | } |
| 7929 | |
| 7930 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7931 | static PyMethodDef unicode_methods[] = { |
| 7932 | |
| 7933 | /* Order is according to common usage: often used methods should |
| 7934 | appear first, since lookup is done sequentially. */ |
| 7935 | |
Georg Brandl | ecdc0a9 | 2006-03-30 12:19:07 +0000 | [diff] [blame] | 7936 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7937 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 7938 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7939 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7940 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 7941 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 7942 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 7943 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 7944 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 7945 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 7946 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 7947 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7948 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 7949 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 7950 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7951 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7952 | {"decode", (PyCFunction) unicode_decode, METH_VARARGS, decode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7953 | /* {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */ |
| 7954 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 7955 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 7956 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7957 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7958 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7959 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7960 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7961 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 7962 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 7963 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 7964 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 7965 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 7966 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 7967 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 7968 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 7969 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 7970 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 7971 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 7972 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 7973 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 7974 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7975 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 7976 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
| 7977 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
| 7978 | {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
| 7979 | {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
Robert Schuppenies | 901c997 | 2008-06-10 10:10:31 +0000 | [diff] [blame] | 7980 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 7981 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7982 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7983 | #endif |
| 7984 | |
| 7985 | #if 0 |
| 7986 | /* This one is just used for debugging the implementation. */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 7987 | {"freelistsize", (PyCFunction) free_listsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7988 | #endif |
| 7989 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7990 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7991 | {NULL, NULL} |
| 7992 | }; |
| 7993 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7994 | static PyObject * |
| 7995 | unicode_mod(PyObject *v, PyObject *w) |
| 7996 | { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7997 | if (!PyUnicode_Check(v)) { |
| 7998 | Py_INCREF(Py_NotImplemented); |
| 7999 | return Py_NotImplemented; |
| 8000 | } |
| 8001 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8002 | } |
| 8003 | |
| 8004 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8005 | 0, /*nb_add*/ |
| 8006 | 0, /*nb_subtract*/ |
| 8007 | 0, /*nb_multiply*/ |
| 8008 | 0, /*nb_divide*/ |
| 8009 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8010 | }; |
| 8011 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8012 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8013 | (lenfunc) unicode_length, /* sq_length */ |
| 8014 | PyUnicode_Concat, /* sq_concat */ |
| 8015 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 8016 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 8017 | (ssizessizeargfunc) unicode_slice, /* sq_slice */ |
| 8018 | 0, /* sq_ass_item */ |
| 8019 | 0, /* sq_ass_slice */ |
| 8020 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8021 | }; |
| 8022 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8023 | static PyObject* |
| 8024 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 8025 | { |
Marc-André Lemburg | 3a45779 | 2006-08-14 12:57:27 +0000 | [diff] [blame] | 8026 | if (PyIndex_Check(item)) { |
| 8027 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8028 | if (i == -1 && PyErr_Occurred()) |
| 8029 | return NULL; |
| 8030 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8031 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8032 | return unicode_getitem(self, i); |
| 8033 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8034 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8035 | Py_UNICODE* source_buf; |
| 8036 | Py_UNICODE* result_buf; |
| 8037 | PyObject* result; |
| 8038 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8039 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8040 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8041 | return NULL; |
| 8042 | } |
| 8043 | |
| 8044 | if (slicelength <= 0) { |
| 8045 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 8046 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 8047 | PyUnicode_CheckExact(self)) { |
| 8048 | Py_INCREF(self); |
| 8049 | return (PyObject *)self; |
| 8050 | } else if (step == 1) { |
| 8051 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8052 | } else { |
| 8053 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 8054 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 8055 | sizeof(Py_UNICODE)); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8056 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8057 | if (result_buf == NULL) |
| 8058 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8059 | |
| 8060 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 8061 | result_buf[i] = source_buf[cur]; |
| 8062 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8063 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8064 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 8065 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8066 | return result; |
| 8067 | } |
| 8068 | } else { |
| 8069 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 8070 | return NULL; |
| 8071 | } |
| 8072 | } |
| 8073 | |
| 8074 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8075 | (lenfunc)unicode_length, /* mp_length */ |
| 8076 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 8077 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8078 | }; |
| 8079 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8080 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8081 | unicode_buffer_getreadbuf(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8082 | Py_ssize_t index, |
| 8083 | const void **ptr) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8084 | { |
| 8085 | if (index != 0) { |
| 8086 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8087 | "accessing non-existent unicode segment"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8088 | return -1; |
| 8089 | } |
| 8090 | *ptr = (void *) self->str; |
| 8091 | return PyUnicode_GET_DATA_SIZE(self); |
| 8092 | } |
| 8093 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8094 | static Py_ssize_t |
| 8095 | unicode_buffer_getwritebuf(PyUnicodeObject *self, Py_ssize_t index, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8096 | const void **ptr) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8097 | { |
| 8098 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8099 | "cannot use unicode as modifiable buffer"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8100 | return -1; |
| 8101 | } |
| 8102 | |
| 8103 | static int |
| 8104 | unicode_buffer_getsegcount(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8105 | Py_ssize_t *lenp) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8106 | { |
| 8107 | if (lenp) |
| 8108 | *lenp = PyUnicode_GET_DATA_SIZE(self); |
| 8109 | return 1; |
| 8110 | } |
| 8111 | |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 8112 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8113 | unicode_buffer_getcharbuf(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8114 | Py_ssize_t index, |
| 8115 | const void **ptr) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8116 | { |
| 8117 | PyObject *str; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8118 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8119 | if (index != 0) { |
| 8120 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8121 | "accessing non-existent unicode segment"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8122 | return -1; |
| 8123 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 8124 | str = _PyUnicode_AsDefaultEncodedString((PyObject *)self, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8125 | if (str == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8126 | return -1; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 8127 | *ptr = (void *) PyString_AS_STRING(str); |
| 8128 | return PyString_GET_SIZE(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8129 | } |
| 8130 | |
| 8131 | /* Helpers for PyUnicode_Format() */ |
| 8132 | |
| 8133 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8134 | 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] | 8135 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8136 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8137 | if (argidx < arglen) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8138 | (*p_argidx)++; |
| 8139 | if (arglen < 0) |
| 8140 | return args; |
| 8141 | else |
| 8142 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8143 | } |
| 8144 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8145 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8146 | return NULL; |
| 8147 | } |
| 8148 | |
| 8149 | #define F_LJUST (1<<0) |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8150 | #define F_SIGN (1<<1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8151 | #define F_BLANK (1<<2) |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8152 | #define F_ALT (1<<3) |
| 8153 | #define F_ZERO (1<<4) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8154 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8155 | static Py_ssize_t |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8156 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8157 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8158 | register Py_ssize_t i; |
| 8159 | Py_ssize_t len = strlen(charbuffer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8160 | for (i = len - 1; i >= 0; i--) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8161 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8162 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8163 | return len; |
| 8164 | } |
| 8165 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8166 | static int |
| 8167 | doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) |
| 8168 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8169 | Py_ssize_t result; |
| 8170 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8171 | PyOS_ascii_formatd((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8172 | result = strtounicode(buffer, (char *)buffer); |
| 8173 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8174 | } |
| 8175 | |
| 8176 | static int |
| 8177 | longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) |
| 8178 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8179 | Py_ssize_t result; |
| 8180 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8181 | PyOS_snprintf((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8182 | result = strtounicode(buffer, (char *)buffer); |
| 8183 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8184 | } |
| 8185 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8186 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 8187 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 8188 | formatting is done. */ |
| 8189 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8190 | static int |
| 8191 | formatfloat(Py_UNICODE *buf, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8192 | size_t buflen, |
| 8193 | int flags, |
| 8194 | int prec, |
| 8195 | int type, |
| 8196 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8197 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8198 | /* fmt = '%#.' + `prec` + `type` |
| 8199 | 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] | 8200 | char fmt[20]; |
| 8201 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8202 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8203 | x = PyFloat_AsDouble(v); |
| 8204 | if (x == -1.0 && PyErr_Occurred()) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8205 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8206 | if (prec < 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8207 | prec = 6; |
Mark Dickinson | 75be68b | 2009-08-28 20:57:42 +0000 | [diff] [blame] | 8208 | #if SIZEOF_INT > 4 |
Mark Dickinson | db6fa18 | 2009-03-29 16:25:46 +0000 | [diff] [blame] | 8209 | /* make sure that the decimal representation of precision really does |
| 8210 | need at most 10 digits: platforms with sizeof(int) == 8 exist! */ |
Mark Dickinson | 75be68b | 2009-08-28 20:57:42 +0000 | [diff] [blame] | 8211 | if (prec > 0x7fffffff) { |
Mark Dickinson | db6fa18 | 2009-03-29 16:25:46 +0000 | [diff] [blame] | 8212 | PyErr_SetString(PyExc_OverflowError, |
| 8213 | "outrageously large precision " |
| 8214 | "for formatted float"); |
| 8215 | return -1; |
| 8216 | } |
Mark Dickinson | 75be68b | 2009-08-28 20:57:42 +0000 | [diff] [blame] | 8217 | #endif |
Mark Dickinson | db6fa18 | 2009-03-29 16:25:46 +0000 | [diff] [blame] | 8218 | |
Mark Dickinson | a30f349 | 2009-03-29 15:06:29 +0000 | [diff] [blame] | 8219 | if (type == 'f' && fabs(x) >= 1e50) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8220 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8221 | /* Worst case length calc to ensure no buffer overrun: |
| 8222 | |
| 8223 | 'g' formats: |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8224 | fmt = %#.<prec>g |
| 8225 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 8226 | for any double rep.) |
| 8227 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8228 | |
| 8229 | 'f' formats: |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8230 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 8231 | len = 1 + 50 + 1 + prec = 52 + prec |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8232 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8233 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8234 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8235 | |
| 8236 | */ |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8237 | if (((type == 'g' || type == 'G') && |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8238 | buflen <= (size_t)10 + (size_t)prec) || |
| 8239 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
| 8240 | PyErr_SetString(PyExc_OverflowError, |
| 8241 | "formatted float is too long (precision too large?)"); |
| 8242 | return -1; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8243 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8244 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8245 | (flags&F_ALT) ? "#" : "", |
| 8246 | prec, type); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8247 | return doubletounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8248 | } |
| 8249 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8250 | static PyObject* |
| 8251 | formatlong(PyObject *val, int flags, int prec, int type) |
| 8252 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8253 | char *buf; |
| 8254 | int i, len; |
| 8255 | PyObject *str; /* temporary string object. */ |
| 8256 | PyUnicodeObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8257 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8258 | str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
| 8259 | if (!str) |
| 8260 | return NULL; |
| 8261 | result = _PyUnicode_New(len); |
| 8262 | if (!result) { |
| 8263 | Py_DECREF(str); |
| 8264 | return NULL; |
| 8265 | } |
| 8266 | for (i = 0; i < len; i++) |
| 8267 | result->str[i] = buf[i]; |
| 8268 | result->str[len] = 0; |
| 8269 | Py_DECREF(str); |
| 8270 | return (PyObject*)result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8271 | } |
| 8272 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8273 | static int |
| 8274 | formatint(Py_UNICODE *buf, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8275 | size_t buflen, |
| 8276 | int flags, |
| 8277 | int prec, |
| 8278 | int type, |
| 8279 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8280 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8281 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8282 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 8283 | * + 1 + 1 |
| 8284 | * = 24 |
| 8285 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8286 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8287 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8288 | long x; |
| 8289 | |
| 8290 | x = PyInt_AsLong(v); |
| 8291 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8292 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8293 | if (x < 0 && type == 'u') { |
| 8294 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8295 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8296 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 8297 | sign = "-"; |
| 8298 | else |
| 8299 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8300 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8301 | prec = 1; |
| 8302 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8303 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 8304 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8305 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8306 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8307 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8308 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8309 | return -1; |
| 8310 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8311 | |
| 8312 | if ((flags & F_ALT) && |
| 8313 | (type == 'x' || type == 'X')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8314 | /* When converting under %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8315 | * of issues that cause pain: |
| 8316 | * - when 0 is being converted, the C standard leaves off |
| 8317 | * the '0x' or '0X', which is inconsistent with other |
| 8318 | * %#x/%#X conversions and inconsistent with Python's |
| 8319 | * hex() function |
| 8320 | * - there are platforms that violate the standard and |
| 8321 | * convert 0 with the '0x' or '0X' |
| 8322 | * (Metrowerks, Compaq Tru64) |
| 8323 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8324 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8325 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8326 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8327 | * We can achieve the desired consistency by inserting our |
| 8328 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 8329 | * of %#x/%#X. |
| 8330 | * |
| 8331 | * Note that this is the same approach as used in |
| 8332 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8333 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8334 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 8335 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8336 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8337 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8338 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 8339 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8340 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 8341 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8342 | if (sign[0]) |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8343 | return longtounicode(buf, buflen, fmt, -x); |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8344 | else |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8345 | return longtounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8346 | } |
| 8347 | |
| 8348 | static int |
| 8349 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8350 | size_t buflen, |
| 8351 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8352 | { |
Ezio Melotti | 85ddea7 | 2010-02-25 17:51:33 +0000 | [diff] [blame] | 8353 | PyObject *unistr; |
| 8354 | char *str; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8355 | /* presume that the buffer is at least 2 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8356 | if (PyUnicode_Check(v)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8357 | if (PyUnicode_GET_SIZE(v) != 1) |
| 8358 | goto onError; |
| 8359 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8360 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8361 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 8362 | else if (PyString_Check(v)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8363 | if (PyString_GET_SIZE(v) != 1) |
| 8364 | goto onError; |
Ezio Melotti | 85ddea7 | 2010-02-25 17:51:33 +0000 | [diff] [blame] | 8365 | /* #7649: "u'%c' % char" should behave like "u'%s' % char" and fail |
| 8366 | with a UnicodeDecodeError if 'char' is not decodable with the |
| 8367 | default encoding (usually ASCII, but it might be something else) */ |
| 8368 | str = PyString_AS_STRING(v); |
| 8369 | if ((unsigned char)str[0] > 0x7F) { |
| 8370 | /* the char is not ASCII; try to decode the string using the |
| 8371 | default encoding and return -1 to let the UnicodeDecodeError |
| 8372 | be raised if the string can't be decoded */ |
| 8373 | unistr = PyUnicode_Decode(str, 1, NULL, "strict"); |
| 8374 | if (unistr == NULL) |
| 8375 | return -1; |
| 8376 | buf[0] = PyUnicode_AS_UNICODE(unistr)[0]; |
| 8377 | Py_DECREF(unistr); |
| 8378 | } |
| 8379 | else |
| 8380 | buf[0] = (Py_UNICODE)str[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8381 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8382 | |
| 8383 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8384 | /* Integer input truncated to a character */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8385 | long x; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8386 | x = PyInt_AsLong(v); |
| 8387 | if (x == -1 && PyErr_Occurred()) |
| 8388 | goto onError; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8389 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8390 | if (x < 0 || x > 0x10ffff) { |
| 8391 | PyErr_SetString(PyExc_OverflowError, |
| 8392 | "%c arg not in range(0x110000) " |
| 8393 | "(wide Python build)"); |
| 8394 | return -1; |
| 8395 | } |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8396 | #else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8397 | if (x < 0 || x > 0xffff) { |
| 8398 | PyErr_SetString(PyExc_OverflowError, |
| 8399 | "%c arg not in range(0x10000) " |
| 8400 | "(narrow Python build)"); |
| 8401 | return -1; |
| 8402 | } |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8403 | #endif |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8404 | buf[0] = (Py_UNICODE) x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8405 | } |
| 8406 | buf[1] = '\0'; |
| 8407 | return 1; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8408 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8409 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8410 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8411 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8412 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8413 | } |
| 8414 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8415 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 8416 | |
| 8417 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 8418 | chars are formatted. XXX This is a magic number. Each formatting |
| 8419 | routine does bounds checking to ensure no overflow, but a better |
| 8420 | solution may be to malloc a buffer of appropriate size for each |
| 8421 | format. For now, the current solution is sufficient. |
| 8422 | */ |
| 8423 | #define FORMATBUFLEN (size_t)120 |
| 8424 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8425 | PyObject *PyUnicode_Format(PyObject *format, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8426 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8427 | { |
| 8428 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8429 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8430 | int args_owned = 0; |
| 8431 | PyUnicodeObject *result = NULL; |
| 8432 | PyObject *dict = NULL; |
| 8433 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8434 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8435 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8436 | PyErr_BadInternalCall(); |
| 8437 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8438 | } |
| 8439 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8440 | if (uformat == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8441 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8442 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 8443 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 8444 | |
| 8445 | reslen = rescnt = fmtcnt + 100; |
| 8446 | result = _PyUnicode_New(reslen); |
| 8447 | if (result == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8448 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8449 | res = PyUnicode_AS_UNICODE(result); |
| 8450 | |
| 8451 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8452 | arglen = PyTuple_Size(args); |
| 8453 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8454 | } |
| 8455 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8456 | arglen = -1; |
| 8457 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8458 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 8459 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 8460 | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8461 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8462 | |
| 8463 | while (--fmtcnt >= 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8464 | if (*fmt != '%') { |
| 8465 | if (--rescnt < 0) { |
| 8466 | rescnt = fmtcnt + 100; |
| 8467 | reslen += rescnt; |
| 8468 | if (_PyUnicode_Resize(&result, reslen) < 0) |
| 8469 | goto onError; |
| 8470 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 8471 | --rescnt; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8472 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8473 | *res++ = *fmt++; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8474 | } |
| 8475 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8476 | /* Got a format specifier */ |
| 8477 | int flags = 0; |
| 8478 | Py_ssize_t width = -1; |
| 8479 | int prec = -1; |
| 8480 | Py_UNICODE c = '\0'; |
| 8481 | Py_UNICODE fill; |
| 8482 | int isnumok; |
| 8483 | PyObject *v = NULL; |
| 8484 | PyObject *temp = NULL; |
| 8485 | Py_UNICODE *pbuf; |
| 8486 | Py_UNICODE sign; |
| 8487 | Py_ssize_t len; |
| 8488 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
| 8489 | |
| 8490 | fmt++; |
| 8491 | if (*fmt == '(') { |
| 8492 | Py_UNICODE *keystart; |
| 8493 | Py_ssize_t keylen; |
| 8494 | PyObject *key; |
| 8495 | int pcount = 1; |
| 8496 | |
| 8497 | if (dict == NULL) { |
| 8498 | PyErr_SetString(PyExc_TypeError, |
| 8499 | "format requires a mapping"); |
| 8500 | goto onError; |
| 8501 | } |
| 8502 | ++fmt; |
| 8503 | --fmtcnt; |
| 8504 | keystart = fmt; |
| 8505 | /* Skip over balanced parentheses */ |
| 8506 | while (pcount > 0 && --fmtcnt >= 0) { |
| 8507 | if (*fmt == ')') |
| 8508 | --pcount; |
| 8509 | else if (*fmt == '(') |
| 8510 | ++pcount; |
| 8511 | fmt++; |
| 8512 | } |
| 8513 | keylen = fmt - keystart - 1; |
| 8514 | if (fmtcnt < 0 || pcount > 0) { |
| 8515 | PyErr_SetString(PyExc_ValueError, |
| 8516 | "incomplete format key"); |
| 8517 | goto onError; |
| 8518 | } |
| 8519 | #if 0 |
| 8520 | /* keys are converted to strings using UTF-8 and |
| 8521 | then looked up since Python uses strings to hold |
| 8522 | variables names etc. in its namespaces and we |
| 8523 | wouldn't want to break common idioms. */ |
| 8524 | key = PyUnicode_EncodeUTF8(keystart, |
| 8525 | keylen, |
| 8526 | NULL); |
| 8527 | #else |
| 8528 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 8529 | #endif |
| 8530 | if (key == NULL) |
| 8531 | goto onError; |
| 8532 | if (args_owned) { |
| 8533 | Py_DECREF(args); |
| 8534 | args_owned = 0; |
| 8535 | } |
| 8536 | args = PyObject_GetItem(dict, key); |
| 8537 | Py_DECREF(key); |
| 8538 | if (args == NULL) { |
| 8539 | goto onError; |
| 8540 | } |
| 8541 | args_owned = 1; |
| 8542 | arglen = -1; |
| 8543 | argidx = -2; |
| 8544 | } |
| 8545 | while (--fmtcnt >= 0) { |
| 8546 | switch (c = *fmt++) { |
| 8547 | case '-': flags |= F_LJUST; continue; |
| 8548 | case '+': flags |= F_SIGN; continue; |
| 8549 | case ' ': flags |= F_BLANK; continue; |
| 8550 | case '#': flags |= F_ALT; continue; |
| 8551 | case '0': flags |= F_ZERO; continue; |
| 8552 | } |
| 8553 | break; |
| 8554 | } |
| 8555 | if (c == '*') { |
| 8556 | v = getnextarg(args, arglen, &argidx); |
| 8557 | if (v == NULL) |
| 8558 | goto onError; |
| 8559 | if (!PyInt_Check(v)) { |
| 8560 | PyErr_SetString(PyExc_TypeError, |
| 8561 | "* wants int"); |
| 8562 | goto onError; |
| 8563 | } |
| 8564 | width = PyInt_AsLong(v); |
| 8565 | if (width < 0) { |
| 8566 | flags |= F_LJUST; |
| 8567 | width = -width; |
| 8568 | } |
| 8569 | if (--fmtcnt >= 0) |
| 8570 | c = *fmt++; |
| 8571 | } |
| 8572 | else if (c >= '0' && c <= '9') { |
| 8573 | width = c - '0'; |
| 8574 | while (--fmtcnt >= 0) { |
| 8575 | c = *fmt++; |
| 8576 | if (c < '0' || c > '9') |
| 8577 | break; |
| 8578 | if ((width*10) / 10 != width) { |
| 8579 | PyErr_SetString(PyExc_ValueError, |
| 8580 | "width too big"); |
| 8581 | goto onError; |
| 8582 | } |
| 8583 | width = width*10 + (c - '0'); |
| 8584 | } |
| 8585 | } |
| 8586 | if (c == '.') { |
| 8587 | prec = 0; |
| 8588 | if (--fmtcnt >= 0) |
| 8589 | c = *fmt++; |
| 8590 | if (c == '*') { |
| 8591 | v = getnextarg(args, arglen, &argidx); |
| 8592 | if (v == NULL) |
| 8593 | goto onError; |
| 8594 | if (!PyInt_Check(v)) { |
| 8595 | PyErr_SetString(PyExc_TypeError, |
| 8596 | "* wants int"); |
| 8597 | goto onError; |
| 8598 | } |
| 8599 | prec = PyInt_AsLong(v); |
| 8600 | if (prec < 0) |
| 8601 | prec = 0; |
| 8602 | if (--fmtcnt >= 0) |
| 8603 | c = *fmt++; |
| 8604 | } |
| 8605 | else if (c >= '0' && c <= '9') { |
| 8606 | prec = c - '0'; |
| 8607 | while (--fmtcnt >= 0) { |
Stefan Krah | ae7dd8f | 2010-07-19 18:24:18 +0000 | [diff] [blame] | 8608 | c = *fmt++; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8609 | if (c < '0' || c > '9') |
| 8610 | break; |
| 8611 | if ((prec*10) / 10 != prec) { |
| 8612 | PyErr_SetString(PyExc_ValueError, |
| 8613 | "prec too big"); |
| 8614 | goto onError; |
| 8615 | } |
| 8616 | prec = prec*10 + (c - '0'); |
| 8617 | } |
| 8618 | } |
| 8619 | } /* prec */ |
| 8620 | if (fmtcnt >= 0) { |
| 8621 | if (c == 'h' || c == 'l' || c == 'L') { |
| 8622 | if (--fmtcnt >= 0) |
| 8623 | c = *fmt++; |
| 8624 | } |
| 8625 | } |
| 8626 | if (fmtcnt < 0) { |
| 8627 | PyErr_SetString(PyExc_ValueError, |
| 8628 | "incomplete format"); |
| 8629 | goto onError; |
| 8630 | } |
| 8631 | if (c != '%') { |
| 8632 | v = getnextarg(args, arglen, &argidx); |
| 8633 | if (v == NULL) |
| 8634 | goto onError; |
| 8635 | } |
| 8636 | sign = 0; |
| 8637 | fill = ' '; |
| 8638 | switch (c) { |
| 8639 | |
| 8640 | case '%': |
| 8641 | pbuf = formatbuf; |
| 8642 | /* presume that buffer length is at least 1 */ |
| 8643 | pbuf[0] = '%'; |
| 8644 | len = 1; |
| 8645 | break; |
| 8646 | |
| 8647 | case 's': |
| 8648 | case 'r': |
Victor Stinner | 4fd2ff9 | 2010-03-22 12:56:39 +0000 | [diff] [blame] | 8649 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8650 | temp = v; |
| 8651 | Py_INCREF(temp); |
| 8652 | } |
| 8653 | else { |
| 8654 | PyObject *unicode; |
| 8655 | if (c == 's') |
| 8656 | temp = PyObject_Unicode(v); |
| 8657 | else |
| 8658 | temp = PyObject_Repr(v); |
| 8659 | if (temp == NULL) |
| 8660 | goto onError; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8661 | if (PyUnicode_Check(temp)) |
| 8662 | /* nothing to do */; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 8663 | else if (PyString_Check(temp)) { |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8664 | /* convert to string to Unicode */ |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8665 | unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
| 8666 | PyString_GET_SIZE(temp), |
| 8667 | NULL, |
| 8668 | "strict"); |
| 8669 | Py_DECREF(temp); |
| 8670 | temp = unicode; |
| 8671 | if (temp == NULL) |
| 8672 | goto onError; |
| 8673 | } |
| 8674 | else { |
| 8675 | Py_DECREF(temp); |
| 8676 | PyErr_SetString(PyExc_TypeError, |
| 8677 | "%s argument has non-string str()"); |
| 8678 | goto onError; |
| 8679 | } |
| 8680 | } |
| 8681 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 8682 | len = PyUnicode_GET_SIZE(temp); |
| 8683 | if (prec >= 0 && len > prec) |
| 8684 | len = prec; |
| 8685 | break; |
| 8686 | |
| 8687 | case 'i': |
| 8688 | case 'd': |
| 8689 | case 'u': |
| 8690 | case 'o': |
| 8691 | case 'x': |
| 8692 | case 'X': |
| 8693 | if (c == 'i') |
| 8694 | c = 'd'; |
| 8695 | isnumok = 0; |
| 8696 | if (PyNumber_Check(v)) { |
| 8697 | PyObject *iobj=NULL; |
| 8698 | |
| 8699 | if (PyInt_Check(v) || (PyLong_Check(v))) { |
| 8700 | iobj = v; |
| 8701 | Py_INCREF(iobj); |
| 8702 | } |
| 8703 | else { |
| 8704 | iobj = PyNumber_Int(v); |
| 8705 | if (iobj==NULL) iobj = PyNumber_Long(v); |
| 8706 | } |
| 8707 | if (iobj!=NULL) { |
| 8708 | if (PyInt_Check(iobj)) { |
| 8709 | isnumok = 1; |
| 8710 | pbuf = formatbuf; |
| 8711 | len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8712 | flags, prec, c, iobj); |
| 8713 | Py_DECREF(iobj); |
| 8714 | if (len < 0) |
| 8715 | goto onError; |
| 8716 | sign = 1; |
| 8717 | } |
| 8718 | else if (PyLong_Check(iobj)) { |
| 8719 | isnumok = 1; |
| 8720 | temp = formatlong(iobj, flags, prec, c); |
| 8721 | Py_DECREF(iobj); |
| 8722 | if (!temp) |
| 8723 | goto onError; |
| 8724 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 8725 | len = PyUnicode_GET_SIZE(temp); |
| 8726 | sign = 1; |
| 8727 | } |
| 8728 | else { |
| 8729 | Py_DECREF(iobj); |
| 8730 | } |
| 8731 | } |
| 8732 | } |
| 8733 | if (!isnumok) { |
| 8734 | PyErr_Format(PyExc_TypeError, |
| 8735 | "%%%c format: a number is required, " |
| 8736 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 8737 | goto onError; |
| 8738 | } |
| 8739 | if (flags & F_ZERO) |
| 8740 | fill = '0'; |
| 8741 | break; |
| 8742 | |
| 8743 | case 'e': |
| 8744 | case 'E': |
| 8745 | case 'f': |
| 8746 | case 'F': |
| 8747 | case 'g': |
| 8748 | case 'G': |
| 8749 | if (c == 'F') |
| 8750 | c = 'f'; |
| 8751 | pbuf = formatbuf; |
| 8752 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8753 | flags, prec, c, v); |
| 8754 | if (len < 0) |
| 8755 | goto onError; |
| 8756 | sign = 1; |
| 8757 | if (flags & F_ZERO) |
| 8758 | fill = '0'; |
| 8759 | break; |
| 8760 | |
| 8761 | case 'c': |
| 8762 | pbuf = formatbuf; |
| 8763 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
| 8764 | if (len < 0) |
| 8765 | goto onError; |
| 8766 | break; |
| 8767 | |
| 8768 | default: |
| 8769 | PyErr_Format(PyExc_ValueError, |
| 8770 | "unsupported format character '%c' (0x%x) " |
| 8771 | "at index %zd", |
| 8772 | (31<=c && c<=126) ? (char)c : '?', |
| 8773 | (int)c, |
| 8774 | (Py_ssize_t)(fmt - 1 - |
| 8775 | PyUnicode_AS_UNICODE(uformat))); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8776 | goto onError; |
| 8777 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8778 | if (sign) { |
| 8779 | if (*pbuf == '-' || *pbuf == '+') { |
| 8780 | sign = *pbuf++; |
| 8781 | len--; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8782 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8783 | else if (flags & F_SIGN) |
| 8784 | sign = '+'; |
| 8785 | else if (flags & F_BLANK) |
| 8786 | sign = ' '; |
| 8787 | else |
| 8788 | sign = 0; |
| 8789 | } |
| 8790 | if (width < len) |
| 8791 | width = len; |
| 8792 | if (rescnt - (sign != 0) < width) { |
| 8793 | reslen -= rescnt; |
| 8794 | rescnt = width + fmtcnt + 100; |
| 8795 | reslen += rescnt; |
| 8796 | if (reslen < 0) { |
| 8797 | Py_XDECREF(temp); |
| 8798 | PyErr_NoMemory(); |
| 8799 | goto onError; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8800 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8801 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 8802 | Py_XDECREF(temp); |
| 8803 | goto onError; |
| 8804 | } |
| 8805 | res = PyUnicode_AS_UNICODE(result) |
| 8806 | + reslen - rescnt; |
| 8807 | } |
| 8808 | if (sign) { |
| 8809 | if (fill != ' ') |
| 8810 | *res++ = sign; |
| 8811 | rescnt--; |
| 8812 | if (width > len) |
| 8813 | width--; |
| 8814 | } |
| 8815 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 8816 | assert(pbuf[0] == '0'); |
| 8817 | assert(pbuf[1] == c); |
| 8818 | if (fill != ' ') { |
| 8819 | *res++ = *pbuf++; |
| 8820 | *res++ = *pbuf++; |
| 8821 | } |
| 8822 | rescnt -= 2; |
| 8823 | width -= 2; |
| 8824 | if (width < 0) |
| 8825 | width = 0; |
| 8826 | len -= 2; |
| 8827 | } |
| 8828 | if (width > len && !(flags & F_LJUST)) { |
| 8829 | do { |
| 8830 | --rescnt; |
| 8831 | *res++ = fill; |
| 8832 | } while (--width > len); |
| 8833 | } |
| 8834 | if (fill == ' ') { |
| 8835 | if (sign) |
| 8836 | *res++ = sign; |
| 8837 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 8838 | assert(pbuf[0] == '0'); |
| 8839 | assert(pbuf[1] == c); |
| 8840 | *res++ = *pbuf++; |
| 8841 | *res++ = *pbuf++; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8842 | } |
| 8843 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8844 | Py_UNICODE_COPY(res, pbuf, len); |
| 8845 | res += len; |
| 8846 | rescnt -= len; |
| 8847 | while (--width >= len) { |
| 8848 | --rescnt; |
| 8849 | *res++ = ' '; |
| 8850 | } |
| 8851 | if (dict && (argidx < arglen) && c != '%') { |
| 8852 | PyErr_SetString(PyExc_TypeError, |
| 8853 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8854 | Py_XDECREF(temp); |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8855 | goto onError; |
| 8856 | } |
| 8857 | Py_XDECREF(temp); |
| 8858 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8859 | } /* until end */ |
| 8860 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8861 | PyErr_SetString(PyExc_TypeError, |
| 8862 | "not all arguments converted during string formatting"); |
| 8863 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8864 | } |
| 8865 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8866 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8867 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8868 | if (args_owned) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8869 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8870 | } |
| 8871 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8872 | return (PyObject *)result; |
| 8873 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8874 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8875 | Py_XDECREF(result); |
| 8876 | Py_DECREF(uformat); |
| 8877 | if (args_owned) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8878 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8879 | } |
| 8880 | return NULL; |
| 8881 | } |
| 8882 | |
| 8883 | static PyBufferProcs unicode_as_buffer = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8884 | (readbufferproc) unicode_buffer_getreadbuf, |
| 8885 | (writebufferproc) unicode_buffer_getwritebuf, |
| 8886 | (segcountproc) unicode_buffer_getsegcount, |
| 8887 | (charbufferproc) unicode_buffer_getcharbuf, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8888 | }; |
| 8889 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 8890 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8891 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 8892 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8893 | static PyObject * |
| 8894 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8895 | { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8896 | PyObject *x = NULL; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8897 | static char *kwlist[] = {"string", "encoding", "errors", 0}; |
| 8898 | char *encoding = NULL; |
| 8899 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8900 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8901 | if (type != &PyUnicode_Type) |
| 8902 | return unicode_subtype_new(type, args, kwds); |
| 8903 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8904 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8905 | return NULL; |
| 8906 | if (x == NULL) |
| 8907 | return (PyObject *)_PyUnicode_New(0); |
| 8908 | if (encoding == NULL && errors == NULL) |
| 8909 | return PyObject_Unicode(x); |
| 8910 | else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8911 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8912 | } |
| 8913 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8914 | static PyObject * |
| 8915 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8916 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8917 | PyUnicodeObject *tmp, *pnew; |
| 8918 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8919 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8920 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 8921 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 8922 | if (tmp == NULL) |
| 8923 | return NULL; |
| 8924 | assert(PyUnicode_Check(tmp)); |
| 8925 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
| 8926 | if (pnew == NULL) { |
| 8927 | Py_DECREF(tmp); |
| 8928 | return NULL; |
| 8929 | } |
| 8930 | pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
| 8931 | if (pnew->str == NULL) { |
| 8932 | _Py_ForgetReference((PyObject *)pnew); |
| 8933 | PyObject_Del(pnew); |
| 8934 | Py_DECREF(tmp); |
| 8935 | return PyErr_NoMemory(); |
| 8936 | } |
| 8937 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 8938 | pnew->length = n; |
| 8939 | pnew->hash = tmp->hash; |
| 8940 | Py_DECREF(tmp); |
| 8941 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8942 | } |
| 8943 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8944 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8945 | "unicode(string [, encoding[, errors]]) -> object\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8946 | \n\ |
| 8947 | Create a new Unicode object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 8948 | encoding defaults to the current default string encoding.\n\ |
| 8949 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8950 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8951 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 8952 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8953 | "unicode", /* tp_name */ |
| 8954 | sizeof(PyUnicodeObject), /* tp_size */ |
| 8955 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8956 | /* Slots */ |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8957 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 8958 | 0, /* tp_print */ |
| 8959 | 0, /* tp_getattr */ |
| 8960 | 0, /* tp_setattr */ |
| 8961 | 0, /* tp_compare */ |
| 8962 | unicode_repr, /* tp_repr */ |
| 8963 | &unicode_as_number, /* tp_as_number */ |
| 8964 | &unicode_as_sequence, /* tp_as_sequence */ |
| 8965 | &unicode_as_mapping, /* tp_as_mapping */ |
| 8966 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 8967 | 0, /* tp_call*/ |
| 8968 | (reprfunc) unicode_str, /* tp_str */ |
| 8969 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 8970 | 0, /* tp_setattro */ |
| 8971 | &unicode_as_buffer, /* tp_as_buffer */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8972 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8973 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8974 | unicode_doc, /* tp_doc */ |
| 8975 | 0, /* tp_traverse */ |
| 8976 | 0, /* tp_clear */ |
| 8977 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 8978 | 0, /* tp_weaklistoffset */ |
| 8979 | 0, /* tp_iter */ |
| 8980 | 0, /* tp_iternext */ |
| 8981 | unicode_methods, /* tp_methods */ |
| 8982 | 0, /* tp_members */ |
| 8983 | 0, /* tp_getset */ |
| 8984 | &PyBaseString_Type, /* tp_base */ |
| 8985 | 0, /* tp_dict */ |
| 8986 | 0, /* tp_descr_get */ |
| 8987 | 0, /* tp_descr_set */ |
| 8988 | 0, /* tp_dictoffset */ |
| 8989 | 0, /* tp_init */ |
| 8990 | 0, /* tp_alloc */ |
| 8991 | unicode_new, /* tp_new */ |
| 8992 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8993 | }; |
| 8994 | |
| 8995 | /* Initialize the Unicode implementation */ |
| 8996 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 8997 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8998 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8999 | int i; |
| 9000 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 9001 | /* XXX - move this array to unicodectype.c ? */ |
| 9002 | Py_UNICODE linebreak[] = { |
| 9003 | 0x000A, /* LINE FEED */ |
| 9004 | 0x000D, /* CARRIAGE RETURN */ |
| 9005 | 0x001C, /* FILE SEPARATOR */ |
| 9006 | 0x001D, /* GROUP SEPARATOR */ |
| 9007 | 0x001E, /* RECORD SEPARATOR */ |
| 9008 | 0x0085, /* NEXT LINE */ |
| 9009 | 0x2028, /* LINE SEPARATOR */ |
| 9010 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 9011 | }; |
| 9012 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9013 | /* Init the implementation */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 9014 | free_list = NULL; |
| 9015 | numfree = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9016 | unicode_empty = _PyUnicode_New(0); |
Neal Norwitz | e1fdb32 | 2006-07-21 05:32:28 +0000 | [diff] [blame] | 9017 | if (!unicode_empty) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 9018 | return; |
Neal Norwitz | e1fdb32 | 2006-07-21 05:32:28 +0000 | [diff] [blame] | 9019 | |
Marc-André Lemburg | 90e8147 | 2000-06-07 09:13:21 +0000 | [diff] [blame] | 9020 | strcpy(unicode_default_encoding, "ascii"); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9021 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 9022 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 9023 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 9024 | Py_FatalError("Can't initialize 'unicode'"); |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 9025 | |
| 9026 | /* initialize the linebreak bloom filter */ |
| 9027 | bloom_linebreak = make_bloom_mask( |
| 9028 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 9029 | ); |
Neal Norwitz | de4c78a | 2006-06-13 08:28:19 +0000 | [diff] [blame] | 9030 | |
| 9031 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9032 | } |
| 9033 | |
| 9034 | /* Finalize the Unicode implementation */ |
| 9035 | |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 9036 | int |
| 9037 | PyUnicode_ClearFreeList(void) |
| 9038 | { |
| 9039 | int freelist_size = numfree; |
| 9040 | PyUnicodeObject *u; |
| 9041 | |
| 9042 | for (u = free_list; u != NULL;) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 9043 | PyUnicodeObject *v = u; |
| 9044 | u = *(PyUnicodeObject **)u; |
| 9045 | if (v->str) |
| 9046 | PyObject_DEL(v->str); |
| 9047 | Py_XDECREF(v->defenc); |
| 9048 | PyObject_Del(v); |
| 9049 | numfree--; |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 9050 | } |
| 9051 | free_list = NULL; |
| 9052 | assert(numfree == 0); |
| 9053 | return freelist_size; |
| 9054 | } |
| 9055 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9056 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9057 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9058 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9059 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9060 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 9061 | Py_XDECREF(unicode_empty); |
| 9062 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9063 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9064 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 9065 | if (unicode_latin1[i]) { |
| 9066 | Py_DECREF(unicode_latin1[i]); |
| 9067 | unicode_latin1[i] = NULL; |
| 9068 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9069 | } |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 9070 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9071 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9072 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 9073 | #ifdef __cplusplus |
| 9074 | } |
| 9075 | #endif |
| 9076 | |
| 9077 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9078 | /* |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 9079 | Local variables: |
| 9080 | c-basic-offset: 4 |
| 9081 | indent-tabs-mode: nil |
| 9082 | End: |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9083 | */ |