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) { |
| 275 | Py_DECREF(unicode->defenc); |
| 276 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 277 | } |
| 278 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 279 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | /* We allocate one more byte to make sure the string is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 284 | Ux0000 terminated -- XXX is this needed ? |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 285 | |
| 286 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 287 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 288 | |
| 289 | */ |
| 290 | |
| 291 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 292 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 293 | { |
| 294 | register PyUnicodeObject *unicode; |
| 295 | |
Andrew Dalke | e0df762 | 2006-05-27 11:04:36 +0000 | [diff] [blame] | 296 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 297 | if (length == 0 && unicode_empty != NULL) { |
| 298 | Py_INCREF(unicode_empty); |
| 299 | return unicode_empty; |
| 300 | } |
| 301 | |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 302 | /* Ensure we won't overflow the size. */ |
| 303 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 304 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 305 | } |
| 306 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 307 | /* Unicode freelist & memory allocation */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 308 | if (free_list) { |
| 309 | unicode = free_list; |
| 310 | free_list = *(PyUnicodeObject **)unicode; |
| 311 | numfree--; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 312 | if (unicode->str) { |
| 313 | /* Keep-Alive optimization: we only upsize the buffer, |
| 314 | never downsize it. */ |
| 315 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 316 | unicode_resize(unicode, length) < 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 317 | PyObject_DEL(unicode->str); |
| 318 | unicode->str = NULL; |
| 319 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 320 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 321 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 322 | size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 323 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 324 | } |
| 325 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 326 | } |
| 327 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 328 | size_t new_size; |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 329 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 330 | if (unicode == NULL) |
| 331 | return NULL; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 332 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 333 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 336 | if (!unicode->str) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 337 | PyErr_NoMemory(); |
| 338 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 339 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 340 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 341 | * the caller fails before initializing str -- unicode_resize() |
| 342 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 343 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 344 | * We don't want unicode_resize to read uninitialized memory in |
| 345 | * that case. |
| 346 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 347 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 348 | unicode->str[length] = 0; |
Martin v. Löwis | f15da69 | 2006-04-13 07:24:50 +0000 | [diff] [blame] | 349 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 350 | unicode->hash = -1; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 351 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 352 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 353 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 354 | onError: |
Amaury Forgeot d'Arc | 06847b1 | 2008-07-31 23:39:05 +0000 | [diff] [blame] | 355 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 356 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 357 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 358 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 359 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 363 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 364 | { |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 365 | if (PyUnicode_CheckExact(unicode) && |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 366 | numfree < PyUnicode_MAXFREELIST) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 367 | /* Keep-Alive optimization */ |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 368 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
| 369 | PyObject_DEL(unicode->str); |
| 370 | unicode->str = NULL; |
| 371 | unicode->length = 0; |
| 372 | } |
| 373 | if (unicode->defenc) { |
| 374 | Py_DECREF(unicode->defenc); |
| 375 | unicode->defenc = NULL; |
| 376 | } |
| 377 | /* Add to free list */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 378 | *(PyUnicodeObject **)unicode = free_list; |
| 379 | free_list = unicode; |
| 380 | numfree++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 381 | } |
| 382 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 383 | PyObject_DEL(unicode->str); |
| 384 | Py_XDECREF(unicode->defenc); |
| 385 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
Benjamin Peterson | 828a706 | 2008-12-27 17:05:29 +0000 | [diff] [blame] | 389 | static |
| 390 | int _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 391 | { |
| 392 | register PyUnicodeObject *v; |
| 393 | |
| 394 | /* Argument checks */ |
| 395 | if (unicode == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 396 | PyErr_BadInternalCall(); |
| 397 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 398 | } |
Benjamin Peterson | 828a706 | 2008-12-27 17:05:29 +0000 | [diff] [blame] | 399 | v = *unicode; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 400 | if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 401 | PyErr_BadInternalCall(); |
| 402 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /* Resizing unicode_empty and single character objects is not |
| 406 | possible since these are being shared. We simply return a fresh |
| 407 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 408 | if (v->length != length && |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 409 | (v == unicode_empty || v->length == 1)) { |
| 410 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 411 | if (w == NULL) |
| 412 | return -1; |
| 413 | Py_UNICODE_COPY(w->str, v->str, |
| 414 | length < v->length ? length : v->length); |
| 415 | Py_DECREF(*unicode); |
| 416 | *unicode = w; |
| 417 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 421 | objects, since we can modify them in-place. */ |
| 422 | return unicode_resize(v, length); |
| 423 | } |
| 424 | |
Benjamin Peterson | 828a706 | 2008-12-27 17:05:29 +0000 | [diff] [blame] | 425 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
| 426 | { |
| 427 | return _PyUnicode_Resize((PyUnicodeObject **)unicode, length); |
| 428 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 429 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 430 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 431 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 432 | { |
| 433 | PyUnicodeObject *unicode; |
| 434 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 435 | /* If the Unicode data is known at construction time, we can apply |
| 436 | some optimizations which share commonly used objects. */ |
| 437 | if (u != NULL) { |
| 438 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 439 | /* Optimization for empty strings */ |
| 440 | if (size == 0 && unicode_empty != NULL) { |
| 441 | Py_INCREF(unicode_empty); |
| 442 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 443 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 444 | |
| 445 | /* Single character Unicode objects in the Latin-1 range are |
| 446 | shared when using this constructor */ |
| 447 | if (size == 1 && *u < 256) { |
| 448 | unicode = unicode_latin1[*u]; |
| 449 | if (!unicode) { |
| 450 | unicode = _PyUnicode_New(1); |
| 451 | if (!unicode) |
| 452 | return NULL; |
| 453 | unicode->str[0] = *u; |
| 454 | unicode_latin1[*u] = unicode; |
| 455 | } |
| 456 | Py_INCREF(unicode); |
| 457 | return (PyObject *)unicode; |
| 458 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 459 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 460 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 461 | unicode = _PyUnicode_New(size); |
| 462 | if (!unicode) |
| 463 | return NULL; |
| 464 | |
| 465 | /* Copy the Unicode data into the new object */ |
| 466 | if (u != NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 467 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 468 | |
| 469 | return (PyObject *)unicode; |
| 470 | } |
| 471 | |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 472 | PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
| 473 | { |
| 474 | PyUnicodeObject *unicode; |
Gregory P. Smith | c00eb73 | 2008-04-09 23:16:37 +0000 | [diff] [blame] | 475 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 476 | if (size < 0) { |
| 477 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 478 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 479 | return NULL; |
| 480 | } |
Gregory P. Smith | c00eb73 | 2008-04-09 23:16:37 +0000 | [diff] [blame] | 481 | |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 482 | /* If the Unicode data is known at construction time, we can apply |
| 483 | some optimizations which share commonly used objects. |
| 484 | Also, this means the input must be UTF-8, so fall back to the |
| 485 | UTF-8 decoder at the end. */ |
| 486 | if (u != NULL) { |
| 487 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 488 | /* Optimization for empty strings */ |
| 489 | if (size == 0 && unicode_empty != NULL) { |
| 490 | Py_INCREF(unicode_empty); |
| 491 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 492 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 493 | |
| 494 | /* Single characters are shared when using this constructor. |
| 495 | Restrict to ASCII, since the input must be UTF-8. */ |
| 496 | if (size == 1 && Py_CHARMASK(*u) < 128) { |
| 497 | unicode = unicode_latin1[Py_CHARMASK(*u)]; |
| 498 | if (!unicode) { |
| 499 | unicode = _PyUnicode_New(1); |
| 500 | if (!unicode) |
| 501 | return NULL; |
| 502 | unicode->str[0] = Py_CHARMASK(*u); |
| 503 | unicode_latin1[Py_CHARMASK(*u)] = unicode; |
| 504 | } |
| 505 | Py_INCREF(unicode); |
| 506 | return (PyObject *)unicode; |
| 507 | } |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 508 | |
| 509 | return PyUnicode_DecodeUTF8(u, size, NULL); |
| 510 | } |
| 511 | |
| 512 | unicode = _PyUnicode_New(size); |
| 513 | if (!unicode) |
| 514 | return NULL; |
| 515 | |
| 516 | return (PyObject *)unicode; |
| 517 | } |
| 518 | |
| 519 | PyObject *PyUnicode_FromString(const char *u) |
| 520 | { |
| 521 | size_t size = strlen(u); |
| 522 | if (size > PY_SSIZE_T_MAX) { |
| 523 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 524 | return NULL; |
| 525 | } |
| 526 | |
| 527 | return PyUnicode_FromStringAndSize(u, size); |
| 528 | } |
| 529 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 530 | #ifdef HAVE_WCHAR_H |
| 531 | |
| 532 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 533 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 534 | { |
| 535 | PyUnicodeObject *unicode; |
| 536 | |
| 537 | if (w == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 538 | PyErr_BadInternalCall(); |
| 539 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | unicode = _PyUnicode_New(size); |
| 543 | if (!unicode) |
| 544 | return NULL; |
| 545 | |
| 546 | /* Copy the wchar_t data into the new object */ |
| 547 | #ifdef HAVE_USABLE_WCHAR_T |
| 548 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 549 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 550 | { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 551 | register Py_UNICODE *u; |
| 552 | register Py_ssize_t i; |
| 553 | u = PyUnicode_AS_UNICODE(unicode); |
| 554 | for (i = size; i > 0; i--) |
| 555 | *u++ = *w++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 556 | } |
| 557 | #endif |
| 558 | |
| 559 | return (PyObject *)unicode; |
| 560 | } |
| 561 | |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 562 | static void |
| 563 | makefmt(char *fmt, int longflag, int size_tflag, int zeropad, int width, int precision, char c) |
| 564 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 565 | *fmt++ = '%'; |
| 566 | if (width) { |
| 567 | if (zeropad) |
| 568 | *fmt++ = '0'; |
| 569 | fmt += sprintf(fmt, "%d", width); |
| 570 | } |
| 571 | if (precision) |
| 572 | fmt += sprintf(fmt, ".%d", precision); |
| 573 | if (longflag) |
| 574 | *fmt++ = 'l'; |
| 575 | else if (size_tflag) { |
| 576 | char *f = PY_FORMAT_SIZE_T; |
| 577 | while (*f) |
| 578 | *fmt++ = *f++; |
| 579 | } |
| 580 | *fmt++ = c; |
| 581 | *fmt = '\0'; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} |
| 585 | |
| 586 | PyObject * |
| 587 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 588 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 589 | va_list count; |
| 590 | Py_ssize_t callcount = 0; |
| 591 | PyObject **callresults = NULL; |
| 592 | PyObject **callresult = NULL; |
| 593 | Py_ssize_t n = 0; |
| 594 | int width = 0; |
| 595 | int precision = 0; |
| 596 | int zeropad; |
| 597 | const char* f; |
| 598 | Py_UNICODE *s; |
| 599 | PyObject *string; |
| 600 | /* used by sprintf */ |
| 601 | char buffer[21]; |
| 602 | /* use abuffer instead of buffer, if we need more space |
| 603 | * (which can happen if there's a format specifier with width). */ |
| 604 | char *abuffer = NULL; |
| 605 | char *realbuffer; |
| 606 | Py_ssize_t abuffersize = 0; |
| 607 | char fmt[60]; /* should be enough for %0width.precisionld */ |
| 608 | const char *copy; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 609 | |
| 610 | #ifdef VA_LIST_IS_ARRAY |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 611 | Py_MEMCPY(count, vargs, sizeof(va_list)); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 612 | #else |
| 613 | #ifdef __va_copy |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 614 | __va_copy(count, vargs); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 615 | #else |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 616 | count = vargs; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 617 | #endif |
| 618 | #endif |
Walter Dörwald | f11232e | 2009-05-03 22:38:54 +0000 | [diff] [blame] | 619 | /* step 1: count the number of %S/%R/%s format specifications |
| 620 | * (we call PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() for these |
| 621 | * objects once during step 3 and put the result in an array) */ |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 622 | for (f = format; *f; f++) { |
Walter Dörwald | f11232e | 2009-05-03 22:38:54 +0000 | [diff] [blame] | 623 | if (*f == '%') { |
| 624 | if (*(f+1)=='%') |
| 625 | continue; |
Walter Dörwald | 6703225 | 2009-05-03 22:46:50 +0000 | [diff] [blame] | 626 | if (*(f+1)=='S' || *(f+1)=='R') |
Walter Dörwald | f11232e | 2009-05-03 22:38:54 +0000 | [diff] [blame] | 627 | ++callcount; |
| 628 | while (isdigit((unsigned)*f)) |
| 629 | width = (width*10) + *f++ - '0'; |
| 630 | while (*++f && *f != '%' && !isalpha((unsigned)*f)) |
| 631 | ; |
| 632 | if (*f == 's') |
| 633 | ++callcount; |
| 634 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 635 | } |
| 636 | /* step 2: allocate memory for the results of |
Walter Dörwald | f11232e | 2009-05-03 22:38:54 +0000 | [diff] [blame] | 637 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 638 | if (callcount) { |
| 639 | callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); |
| 640 | if (!callresults) { |
| 641 | PyErr_NoMemory(); |
| 642 | return NULL; |
| 643 | } |
| 644 | callresult = callresults; |
| 645 | } |
| 646 | /* step 3: figure out how large a buffer we need */ |
| 647 | for (f = format; *f; f++) { |
| 648 | if (*f == '%') { |
| 649 | const char* p = f; |
| 650 | width = 0; |
| 651 | while (isdigit((unsigned)*f)) |
| 652 | width = (width*10) + *f++ - '0'; |
| 653 | while (*++f && *f != '%' && !isalpha((unsigned)*f)) |
| 654 | ; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 655 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 656 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 657 | * they don't affect the amount of space we reserve. |
| 658 | */ |
| 659 | if ((*f == 'l' || *f == 'z') && |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 660 | (f[1] == 'd' || f[1] == 'u')) |
| 661 | ++f; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 662 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 663 | switch (*f) { |
| 664 | case 'c': |
| 665 | (void)va_arg(count, int); |
| 666 | /* fall through... */ |
| 667 | case '%': |
| 668 | n++; |
| 669 | break; |
| 670 | case 'd': case 'u': case 'i': case 'x': |
| 671 | (void) va_arg(count, int); |
| 672 | /* 20 bytes is enough to hold a 64-bit |
| 673 | integer. Decimal takes the most space. |
| 674 | This isn't enough for octal. |
| 675 | If a width is specified we need more |
| 676 | (which we allocate later). */ |
| 677 | if (width < 20) |
| 678 | width = 20; |
| 679 | n += width; |
| 680 | if (abuffersize < width) |
| 681 | abuffersize = width; |
| 682 | break; |
| 683 | case 's': |
| 684 | { |
| 685 | /* UTF-8 */ |
Walter Dörwald | f11232e | 2009-05-03 22:38:54 +0000 | [diff] [blame] | 686 | unsigned char *s = va_arg(count, unsigned char*); |
| 687 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 688 | if (!str) |
| 689 | goto fail; |
| 690 | n += PyUnicode_GET_SIZE(str); |
| 691 | /* Remember the str and switch to the next slot */ |
| 692 | *callresult++ = str; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 693 | break; |
| 694 | } |
| 695 | case 'U': |
| 696 | { |
| 697 | PyObject *obj = va_arg(count, PyObject *); |
| 698 | assert(obj && PyUnicode_Check(obj)); |
| 699 | n += PyUnicode_GET_SIZE(obj); |
| 700 | break; |
| 701 | } |
| 702 | case 'V': |
| 703 | { |
| 704 | PyObject *obj = va_arg(count, PyObject *); |
| 705 | const char *str = va_arg(count, const char *); |
| 706 | assert(obj || str); |
| 707 | assert(!obj || PyUnicode_Check(obj)); |
| 708 | if (obj) |
| 709 | n += PyUnicode_GET_SIZE(obj); |
| 710 | else |
| 711 | n += strlen(str); |
| 712 | break; |
| 713 | } |
| 714 | case 'S': |
| 715 | { |
| 716 | PyObject *obj = va_arg(count, PyObject *); |
| 717 | PyObject *str; |
| 718 | assert(obj); |
| 719 | str = PyObject_Str(obj); |
| 720 | if (!str) |
| 721 | goto fail; |
| 722 | n += PyUnicode_GET_SIZE(str); |
| 723 | /* Remember the str and switch to the next slot */ |
| 724 | *callresult++ = str; |
| 725 | break; |
| 726 | } |
| 727 | case 'R': |
| 728 | { |
| 729 | PyObject *obj = va_arg(count, PyObject *); |
| 730 | PyObject *repr; |
| 731 | assert(obj); |
| 732 | repr = PyObject_Repr(obj); |
| 733 | if (!repr) |
| 734 | goto fail; |
| 735 | n += PyUnicode_GET_SIZE(repr); |
| 736 | /* Remember the repr and switch to the next slot */ |
| 737 | *callresult++ = repr; |
| 738 | break; |
| 739 | } |
| 740 | case 'p': |
| 741 | (void) va_arg(count, int); |
| 742 | /* maximum 64-bit pointer representation: |
| 743 | * 0xffffffffffffffff |
| 744 | * so 19 characters is enough. |
| 745 | * XXX I count 18 -- what's the extra for? |
| 746 | */ |
| 747 | n += 19; |
| 748 | break; |
| 749 | default: |
| 750 | /* if we stumble upon an unknown |
| 751 | formatting code, copy the rest of |
| 752 | the format string to the output |
| 753 | string. (we cannot just skip the |
| 754 | code, since there's no way to know |
| 755 | what's in the argument list) */ |
| 756 | n += strlen(p); |
| 757 | goto expand; |
| 758 | } |
| 759 | } else |
| 760 | n++; |
| 761 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 762 | expand: |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 763 | if (abuffersize > 20) { |
| 764 | abuffer = PyObject_Malloc(abuffersize); |
| 765 | if (!abuffer) { |
| 766 | PyErr_NoMemory(); |
| 767 | goto fail; |
| 768 | } |
| 769 | realbuffer = abuffer; |
| 770 | } |
| 771 | else |
| 772 | realbuffer = buffer; |
| 773 | /* step 4: fill the buffer */ |
| 774 | /* Since we've analyzed how much space we need for the worst case, |
| 775 | we don't have to resize the string. |
| 776 | There can be no errors beyond this point. */ |
| 777 | string = PyUnicode_FromUnicode(NULL, n); |
| 778 | if (!string) |
| 779 | goto fail; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 780 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 781 | s = PyUnicode_AS_UNICODE(string); |
| 782 | callresult = callresults; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 783 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 784 | for (f = format; *f; f++) { |
| 785 | if (*f == '%') { |
| 786 | const char* p = f++; |
| 787 | int longflag = 0; |
| 788 | int size_tflag = 0; |
| 789 | zeropad = (*f == '0'); |
| 790 | /* parse the width.precision part */ |
| 791 | width = 0; |
| 792 | while (isdigit((unsigned)*f)) |
| 793 | width = (width*10) + *f++ - '0'; |
| 794 | precision = 0; |
| 795 | if (*f == '.') { |
| 796 | f++; |
| 797 | while (isdigit((unsigned)*f)) |
| 798 | precision = (precision*10) + *f++ - '0'; |
| 799 | } |
| 800 | /* handle the long flag, but only for %ld and %lu. |
| 801 | others can be added when necessary. */ |
| 802 | if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
| 803 | longflag = 1; |
| 804 | ++f; |
| 805 | } |
| 806 | /* handle the size_t flag. */ |
| 807 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 808 | size_tflag = 1; |
| 809 | ++f; |
| 810 | } |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 811 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 812 | switch (*f) { |
| 813 | case 'c': |
| 814 | *s++ = va_arg(vargs, int); |
| 815 | break; |
| 816 | case 'd': |
| 817 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'd'); |
| 818 | if (longflag) |
| 819 | sprintf(realbuffer, fmt, va_arg(vargs, long)); |
| 820 | else if (size_tflag) |
| 821 | sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
| 822 | else |
| 823 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 824 | appendstring(realbuffer); |
| 825 | break; |
| 826 | case 'u': |
| 827 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'u'); |
| 828 | if (longflag) |
| 829 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
| 830 | else if (size_tflag) |
| 831 | sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
| 832 | else |
| 833 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
| 834 | appendstring(realbuffer); |
| 835 | break; |
| 836 | case 'i': |
| 837 | makefmt(fmt, 0, 0, zeropad, width, precision, 'i'); |
| 838 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 839 | appendstring(realbuffer); |
| 840 | break; |
| 841 | case 'x': |
| 842 | makefmt(fmt, 0, 0, zeropad, width, precision, 'x'); |
| 843 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 844 | appendstring(realbuffer); |
| 845 | break; |
| 846 | case 's': |
| 847 | { |
Walter Dörwald | f11232e | 2009-05-03 22:38:54 +0000 | [diff] [blame] | 848 | /* unused, since we already have the result */ |
| 849 | (void) va_arg(vargs, char *); |
| 850 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(*callresult), |
| 851 | PyUnicode_GET_SIZE(*callresult)); |
| 852 | s += PyUnicode_GET_SIZE(*callresult); |
| 853 | /* We're done with the unicode()/repr() => forget it */ |
| 854 | Py_DECREF(*callresult); |
| 855 | /* switch to next unicode()/repr() result */ |
| 856 | ++callresult; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 857 | break; |
| 858 | } |
| 859 | case 'U': |
| 860 | { |
| 861 | PyObject *obj = va_arg(vargs, PyObject *); |
| 862 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 863 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 864 | s += size; |
| 865 | break; |
| 866 | } |
| 867 | case 'V': |
| 868 | { |
| 869 | PyObject *obj = va_arg(vargs, PyObject *); |
| 870 | const char *str = va_arg(vargs, const char *); |
| 871 | if (obj) { |
| 872 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 873 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 874 | s += size; |
| 875 | } else { |
| 876 | appendstring(str); |
| 877 | } |
| 878 | break; |
| 879 | } |
| 880 | case 'S': |
| 881 | case 'R': |
| 882 | { |
| 883 | Py_UNICODE *ucopy; |
| 884 | Py_ssize_t usize; |
| 885 | Py_ssize_t upos; |
| 886 | /* unused, since we already have the result */ |
| 887 | (void) va_arg(vargs, PyObject *); |
| 888 | ucopy = PyUnicode_AS_UNICODE(*callresult); |
| 889 | usize = PyUnicode_GET_SIZE(*callresult); |
| 890 | for (upos = 0; upos<usize;) |
| 891 | *s++ = ucopy[upos++]; |
| 892 | /* We're done with the unicode()/repr() => forget it */ |
| 893 | Py_DECREF(*callresult); |
| 894 | /* switch to next unicode()/repr() result */ |
| 895 | ++callresult; |
| 896 | break; |
| 897 | } |
| 898 | case 'p': |
| 899 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 900 | /* %p is ill-defined: ensure leading 0x. */ |
| 901 | if (buffer[1] == 'X') |
| 902 | buffer[1] = 'x'; |
| 903 | else if (buffer[1] != 'x') { |
| 904 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 905 | buffer[0] = '0'; |
| 906 | buffer[1] = 'x'; |
| 907 | } |
| 908 | appendstring(buffer); |
| 909 | break; |
| 910 | case '%': |
| 911 | *s++ = '%'; |
| 912 | break; |
| 913 | default: |
| 914 | appendstring(p); |
| 915 | goto end; |
| 916 | } |
| 917 | } else |
| 918 | *s++ = *f; |
| 919 | } |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 920 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 921 | end: |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 922 | if (callresults) |
| 923 | PyObject_Free(callresults); |
| 924 | if (abuffer) |
| 925 | PyObject_Free(abuffer); |
| 926 | PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
| 927 | return string; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 928 | fail: |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 929 | if (callresults) { |
| 930 | PyObject **callresult2 = callresults; |
| 931 | while (callresult2 < callresult) { |
| 932 | Py_DECREF(*callresult2); |
| 933 | ++callresult2; |
| 934 | } |
| 935 | PyObject_Free(callresults); |
| 936 | } |
| 937 | if (abuffer) |
| 938 | PyObject_Free(abuffer); |
| 939 | return NULL; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | #undef appendstring |
| 943 | |
| 944 | PyObject * |
| 945 | PyUnicode_FromFormat(const char *format, ...) |
| 946 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 947 | PyObject* ret; |
| 948 | va_list vargs; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 949 | |
| 950 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 951 | va_start(vargs, format); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 952 | #else |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 953 | va_start(vargs); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 954 | #endif |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 955 | ret = PyUnicode_FromFormatV(format, vargs); |
| 956 | va_end(vargs); |
| 957 | return ret; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 960 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 961 | wchar_t *w, |
| 962 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 963 | { |
| 964 | if (unicode == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 965 | PyErr_BadInternalCall(); |
| 966 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 967 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 968 | |
| 969 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 970 | if (size > PyUnicode_GET_SIZE(unicode)) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 971 | size = PyUnicode_GET_SIZE(unicode) + 1; |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 972 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 973 | #ifdef HAVE_USABLE_WCHAR_T |
| 974 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 975 | #else |
| 976 | { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 977 | register Py_UNICODE *u; |
| 978 | register Py_ssize_t i; |
| 979 | u = PyUnicode_AS_UNICODE(unicode); |
| 980 | for (i = size; i > 0; i--) |
| 981 | *w++ = *u++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 982 | } |
| 983 | #endif |
| 984 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 985 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 986 | return PyUnicode_GET_SIZE(unicode); |
| 987 | else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 988 | return size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | #endif |
| 992 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 993 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 994 | { |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 995 | Py_UNICODE s[1]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 996 | |
| 997 | #ifdef Py_UNICODE_WIDE |
| 998 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 999 | PyErr_SetString(PyExc_ValueError, |
| 1000 | "unichr() arg not in range(0x110000) " |
| 1001 | "(wide Python build)"); |
| 1002 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1003 | } |
| 1004 | #else |
| 1005 | if (ordinal < 0 || ordinal > 0xffff) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1006 | PyErr_SetString(PyExc_ValueError, |
| 1007 | "unichr() arg not in range(0x10000) " |
| 1008 | "(narrow Python build)"); |
| 1009 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1010 | } |
| 1011 | #endif |
| 1012 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1013 | s[0] = (Py_UNICODE)ordinal; |
| 1014 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1017 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 1018 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1019 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1020 | PyObject_Unicode() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1021 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1022 | Py_INCREF(obj); |
| 1023 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1024 | } |
| 1025 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1026 | /* For a Unicode subtype that's not a Unicode object, |
| 1027 | return a true Unicode object with the same data. */ |
| 1028 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 1029 | PyUnicode_GET_SIZE(obj)); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1030 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1031 | return PyUnicode_FromEncodedObject(obj, NULL, "strict"); |
| 1032 | } |
| 1033 | |
| 1034 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1035 | const char *encoding, |
| 1036 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1037 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1038 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1039 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1040 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1041 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1042 | if (obj == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1043 | PyErr_BadInternalCall(); |
| 1044 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1045 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1046 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1047 | #if 0 |
| 1048 | /* For b/w compatibility we also accept Unicode objects provided |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 1049 | that no encodings is given and then redirect to |
| 1050 | PyObject_Unicode() which then applies the additional logic for |
| 1051 | Unicode subclasses. |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1052 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1053 | NOTE: This API should really only be used for object which |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1054 | represent *encoded* Unicode ! |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1055 | |
| 1056 | */ |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 1057 | if (PyUnicode_Check(obj)) { |
| 1058 | if (encoding) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1059 | PyErr_SetString(PyExc_TypeError, |
| 1060 | "decoding Unicode is not supported"); |
| 1061 | return NULL; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 1062 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1063 | return PyObject_Unicode(obj); |
| 1064 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1065 | #else |
| 1066 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1067 | PyErr_SetString(PyExc_TypeError, |
| 1068 | "decoding Unicode is not supported"); |
| 1069 | return NULL; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 1070 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1071 | #endif |
| 1072 | |
| 1073 | /* Coerce object */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1074 | if (PyString_Check(obj)) { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 1075 | s = PyString_AS_STRING(obj); |
| 1076 | len = PyString_GET_SIZE(obj); |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 1077 | } |
Christian Heimes | 3497f94 | 2008-05-26 12:29:14 +0000 | [diff] [blame] | 1078 | else if (PyByteArray_Check(obj)) { |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 1079 | /* Python 2.x specific */ |
| 1080 | PyErr_Format(PyExc_TypeError, |
| 1081 | "decoding bytearray is not supported"); |
| 1082 | return NULL; |
| 1083 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1084 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1085 | /* Overwrite the error message with something more useful in |
| 1086 | case of a TypeError. */ |
| 1087 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1088 | PyErr_Format(PyExc_TypeError, |
| 1089 | "coercing to Unicode: need string or buffer, " |
| 1090 | "%.80s found", |
| 1091 | Py_TYPE(obj)->tp_name); |
| 1092 | goto onError; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1093 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1094 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1095 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1096 | if (len == 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1097 | Py_INCREF(unicode_empty); |
| 1098 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1099 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1100 | else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1101 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1102 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1103 | return v; |
| 1104 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1105 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1106 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | PyObject *PyUnicode_Decode(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1110 | Py_ssize_t size, |
| 1111 | const char *encoding, |
| 1112 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1113 | { |
| 1114 | PyObject *buffer = NULL, *unicode; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1115 | |
| 1116 | if (encoding == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1117 | encoding = PyUnicode_GetDefaultEncoding(); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1118 | |
| 1119 | /* Shortcuts for common default encodings */ |
| 1120 | if (strcmp(encoding, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1121 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1122 | else if (strcmp(encoding, "latin-1") == 0) |
| 1123 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1124 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1125 | else if (strcmp(encoding, "mbcs") == 0) |
| 1126 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 1127 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1128 | else if (strcmp(encoding, "ascii") == 0) |
| 1129 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1130 | |
| 1131 | /* Decode via the codec registry */ |
| 1132 | buffer = PyBuffer_FromMemory((void *)s, size); |
| 1133 | if (buffer == NULL) |
| 1134 | goto onError; |
| 1135 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1136 | if (unicode == NULL) |
| 1137 | goto onError; |
| 1138 | if (!PyUnicode_Check(unicode)) { |
| 1139 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 1140 | "decoder did not return an unicode object (type=%.400s)", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1141 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1142 | Py_DECREF(unicode); |
| 1143 | goto onError; |
| 1144 | } |
| 1145 | Py_DECREF(buffer); |
| 1146 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1147 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1148 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1149 | Py_XDECREF(buffer); |
| 1150 | return NULL; |
| 1151 | } |
| 1152 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1153 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1154 | const char *encoding, |
| 1155 | const char *errors) |
| 1156 | { |
| 1157 | PyObject *v; |
| 1158 | |
| 1159 | if (!PyUnicode_Check(unicode)) { |
| 1160 | PyErr_BadArgument(); |
| 1161 | goto onError; |
| 1162 | } |
| 1163 | |
| 1164 | if (encoding == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1165 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1166 | |
| 1167 | /* Decode via the codec registry */ |
| 1168 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1169 | if (v == NULL) |
| 1170 | goto onError; |
| 1171 | return v; |
| 1172 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1173 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1174 | return NULL; |
| 1175 | } |
| 1176 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1177 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1178 | Py_ssize_t size, |
| 1179 | const char *encoding, |
| 1180 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1181 | { |
| 1182 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1183 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1184 | unicode = PyUnicode_FromUnicode(s, size); |
| 1185 | if (unicode == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1186 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1187 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1188 | Py_DECREF(unicode); |
| 1189 | return v; |
| 1190 | } |
| 1191 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1192 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1193 | const char *encoding, |
| 1194 | const char *errors) |
| 1195 | { |
| 1196 | PyObject *v; |
| 1197 | |
| 1198 | if (!PyUnicode_Check(unicode)) { |
| 1199 | PyErr_BadArgument(); |
| 1200 | goto onError; |
| 1201 | } |
| 1202 | |
| 1203 | if (encoding == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1204 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1205 | |
| 1206 | /* Encode via the codec registry */ |
| 1207 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1208 | if (v == NULL) |
| 1209 | goto onError; |
| 1210 | return v; |
| 1211 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1212 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1213 | return NULL; |
| 1214 | } |
| 1215 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1216 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1217 | const char *encoding, |
| 1218 | const char *errors) |
| 1219 | { |
| 1220 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1221 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1222 | if (!PyUnicode_Check(unicode)) { |
| 1223 | PyErr_BadArgument(); |
| 1224 | goto onError; |
| 1225 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1226 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1227 | if (encoding == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1228 | encoding = PyUnicode_GetDefaultEncoding(); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1229 | |
| 1230 | /* Shortcuts for common default encodings */ |
| 1231 | if (errors == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1232 | if (strcmp(encoding, "utf-8") == 0) |
| 1233 | return PyUnicode_AsUTF8String(unicode); |
| 1234 | else if (strcmp(encoding, "latin-1") == 0) |
| 1235 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1236 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1237 | else if (strcmp(encoding, "mbcs") == 0) |
| 1238 | return PyUnicode_AsMBCSString(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1239 | #endif |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1240 | else if (strcmp(encoding, "ascii") == 0) |
| 1241 | return PyUnicode_AsASCIIString(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1242 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1243 | |
| 1244 | /* Encode via the codec registry */ |
| 1245 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1246 | if (v == NULL) |
| 1247 | goto onError; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1248 | if (!PyString_Check(v)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1249 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 1250 | "encoder did not return a string object (type=%.400s)", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1251 | Py_TYPE(v)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1252 | Py_DECREF(v); |
| 1253 | goto onError; |
| 1254 | } |
| 1255 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1256 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1257 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1258 | return NULL; |
| 1259 | } |
| 1260 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1261 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1262 | const char *errors) |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1263 | { |
| 1264 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
| 1265 | |
| 1266 | if (v) |
| 1267 | return v; |
| 1268 | v = PyUnicode_AsEncodedString(unicode, NULL, errors); |
| 1269 | if (v && errors == NULL) |
| 1270 | ((PyUnicodeObject *)unicode)->defenc = v; |
| 1271 | return v; |
| 1272 | } |
| 1273 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1274 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1275 | { |
| 1276 | if (!PyUnicode_Check(unicode)) { |
| 1277 | PyErr_BadArgument(); |
| 1278 | goto onError; |
| 1279 | } |
| 1280 | return PyUnicode_AS_UNICODE(unicode); |
| 1281 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1282 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1283 | return NULL; |
| 1284 | } |
| 1285 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1286 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1287 | { |
| 1288 | if (!PyUnicode_Check(unicode)) { |
| 1289 | PyErr_BadArgument(); |
| 1290 | goto onError; |
| 1291 | } |
| 1292 | return PyUnicode_GET_SIZE(unicode); |
| 1293 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1294 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1295 | return -1; |
| 1296 | } |
| 1297 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1298 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1299 | { |
| 1300 | return unicode_default_encoding; |
| 1301 | } |
| 1302 | |
| 1303 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 1304 | { |
| 1305 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1306 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1307 | /* Make sure the encoding is valid. As side effect, this also |
| 1308 | loads the encoding into the codec registry cache. */ |
| 1309 | v = _PyCodec_Lookup(encoding); |
| 1310 | if (v == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1311 | goto onError; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1312 | Py_DECREF(v); |
| 1313 | strncpy(unicode_default_encoding, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1314 | encoding, |
| 1315 | sizeof(unicode_default_encoding)); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1316 | return 0; |
| 1317 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1318 | onError: |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1319 | return -1; |
| 1320 | } |
| 1321 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1322 | /* error handling callback helper: |
| 1323 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1324 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1325 | and adjust various state variables. |
| 1326 | return 0 on success, -1 on error |
| 1327 | */ |
| 1328 | |
| 1329 | static |
| 1330 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1331 | const char *encoding, const char *reason, |
| 1332 | const char *input, Py_ssize_t insize, Py_ssize_t *startinpos, |
| 1333 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 1334 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1335 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1336 | 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] | 1337 | |
| 1338 | PyObject *restuple = NULL; |
| 1339 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1340 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
| 1341 | Py_ssize_t requiredsize; |
| 1342 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1343 | Py_UNICODE *repptr; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1344 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1345 | int res = -1; |
| 1346 | |
| 1347 | if (*errorHandler == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1348 | *errorHandler = PyCodec_LookupError(errors); |
| 1349 | if (*errorHandler == NULL) |
| 1350 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 1354 | *exceptionObject = PyUnicodeDecodeError_Create( |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1355 | encoding, input, insize, *startinpos, *endinpos, reason); |
| 1356 | if (*exceptionObject == NULL) |
| 1357 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1358 | } |
| 1359 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1360 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 1361 | goto onError; |
| 1362 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 1363 | goto onError; |
| 1364 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1365 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1369 | if (restuple == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1370 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1371 | if (!PyTuple_Check(restuple)) { |
Georg Brandl | 40e15ed | 2009-04-05 21:48:06 +0000 | [diff] [blame] | 1372 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1373 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1374 | } |
| 1375 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1376 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1377 | if (newpos<0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1378 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1379 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1380 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 1381 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1382 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1383 | |
| 1384 | /* need more space? (at least enough for what we |
| 1385 | have+the replacement+the rest of the string (starting |
| 1386 | at the new input position), so we won't have to check space |
| 1387 | when there are no errors in the rest of the string) */ |
| 1388 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 1389 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 1390 | requiredsize = *outpos + repsize + insize-newpos; |
| 1391 | if (requiredsize > outsize) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1392 | if (requiredsize<2*outsize) |
| 1393 | requiredsize = 2*outsize; |
| 1394 | if (_PyUnicode_Resize(output, requiredsize) < 0) |
| 1395 | goto onError; |
| 1396 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1397 | } |
| 1398 | *endinpos = newpos; |
| 1399 | *inptr = input + newpos; |
| 1400 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 1401 | *outptr += repsize; |
| 1402 | *outpos += repsize; |
| 1403 | /* we made it! */ |
| 1404 | res = 0; |
| 1405 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1406 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1407 | Py_XDECREF(restuple); |
| 1408 | return res; |
| 1409 | } |
| 1410 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1411 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 1412 | |
| 1413 | /* see RFC2152 for details */ |
| 1414 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1415 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1416 | char utf7_special[128] = { |
| 1417 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 1418 | encoded: |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 1419 | 0 - not special |
| 1420 | 1 - special |
| 1421 | 2 - whitespace (optional) |
| 1422 | 3 - RFC2152 Set O (optional) */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1423 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 1424 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1425 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 1426 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 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, 1, 3, 3, 3, |
| 1429 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1430 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 1431 | |
| 1432 | }; |
| 1433 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1434 | /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
| 1435 | warnings about the comparison always being false; since |
| 1436 | utf7_special[0] is 1, we can safely make that one comparison |
| 1437 | true */ |
| 1438 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1439 | #define SPECIAL(c, encodeO, encodeWS) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1440 | ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1441 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1442 | (encodeO && (utf7_special[(c)] == 3))) |
| 1443 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1444 | #define B64(n) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1445 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1446 | #define B64CHAR(c) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1447 | (isalnum(c) || (c) == '+' || (c) == '/') |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1448 | #define UB64(c) \ |
| 1449 | ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1450 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1451 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1452 | #define ENCODE(out, ch, bits) \ |
| 1453 | while (bits >= 6) { \ |
| 1454 | *out++ = B64(ch >> (bits-6)); \ |
| 1455 | bits -= 6; \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1458 | #define DECODE(out, ch, bits, surrogate) \ |
| 1459 | while (bits >= 16) { \ |
| 1460 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 1461 | bits -= 16; \ |
| 1462 | if (surrogate) { \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1463 | /* We have already generated an error for the high surrogate \ |
| 1464 | 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] | 1465 | surrogate = 0; \ |
| 1466 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1467 | /* This is a surrogate pair. Unfortunately we can't represent \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1468 | it in a 16-bit character */ \ |
| 1469 | surrogate = 1; \ |
| 1470 | errmsg = "code pairs are not supported"; \ |
| 1471 | goto utf7Error; \ |
| 1472 | } else { \ |
| 1473 | *out++ = outCh; \ |
| 1474 | } \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1475 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1476 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1477 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1478 | Py_ssize_t size, |
| 1479 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1480 | { |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1481 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 1482 | } |
| 1483 | |
| 1484 | PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1485 | Py_ssize_t size, |
| 1486 | const char *errors, |
| 1487 | Py_ssize_t *consumed) |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1488 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1489 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1490 | Py_ssize_t startinpos; |
| 1491 | Py_ssize_t endinpos; |
| 1492 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1493 | const char *e; |
| 1494 | PyUnicodeObject *unicode; |
| 1495 | Py_UNICODE *p; |
| 1496 | const char *errmsg = ""; |
| 1497 | int inShift = 0; |
| 1498 | unsigned int bitsleft = 0; |
| 1499 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1500 | int surrogate = 0; |
| 1501 | PyObject *errorHandler = NULL; |
| 1502 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1503 | |
| 1504 | unicode = _PyUnicode_New(size); |
| 1505 | if (!unicode) |
| 1506 | return NULL; |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1507 | if (size == 0) { |
| 1508 | if (consumed) |
| 1509 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1510 | return (PyObject *)unicode; |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1511 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1512 | |
| 1513 | p = unicode->str; |
| 1514 | e = s + size; |
| 1515 | |
| 1516 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1517 | Py_UNICODE ch; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1518 | restart: |
Antoine Pitrou | 4982d5d | 2008-07-25 17:45:59 +0000 | [diff] [blame] | 1519 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1520 | |
| 1521 | if (inShift) { |
| 1522 | if ((ch == '-') || !B64CHAR(ch)) { |
| 1523 | inShift = 0; |
| 1524 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1525 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1526 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1527 | if (bitsleft >= 6) { |
| 1528 | /* The shift sequence has a partial character in it. If |
| 1529 | bitsleft < 6 then we could just classify it as padding |
| 1530 | but that is not the case here */ |
| 1531 | |
| 1532 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1533 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1534 | } |
| 1535 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1536 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1537 | here so indicate the potential of a misencoded character. */ |
| 1538 | |
| 1539 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 1540 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 1541 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1542 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | if (ch == '-') { |
| 1546 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1547 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1548 | inShift = 1; |
| 1549 | } |
| 1550 | } else if (SPECIAL(ch,0,0)) { |
| 1551 | errmsg = "unexpected special character"; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 1552 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1553 | } else { |
| 1554 | *p++ = ch; |
| 1555 | } |
| 1556 | } else { |
| 1557 | charsleft = (charsleft << 6) | UB64(ch); |
| 1558 | bitsleft += 6; |
| 1559 | s++; |
| 1560 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1561 | } |
| 1562 | } |
| 1563 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1564 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1565 | s++; |
| 1566 | if (s < e && *s == '-') { |
| 1567 | s++; |
| 1568 | *p++ = '+'; |
| 1569 | } else |
| 1570 | { |
| 1571 | inShift = 1; |
| 1572 | bitsleft = 0; |
| 1573 | } |
| 1574 | } |
| 1575 | else if (SPECIAL(ch,0,0)) { |
Walter Dörwald | 9d04542 | 2007-08-30 15:34:55 +0000 | [diff] [blame] | 1576 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1577 | errmsg = "unexpected special character"; |
| 1578 | s++; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 1579 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1580 | } |
| 1581 | else { |
| 1582 | *p++ = ch; |
| 1583 | s++; |
| 1584 | } |
| 1585 | continue; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1586 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1587 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1588 | endinpos = s-starts; |
| 1589 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1590 | errors, &errorHandler, |
| 1591 | "utf7", errmsg, |
| 1592 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1593 | &unicode, &outpos, &p)) |
| 1594 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1597 | if (inShift && !consumed) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1598 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1599 | endinpos = size; |
| 1600 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1601 | errors, &errorHandler, |
| 1602 | "utf7", "unterminated shift sequence", |
| 1603 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1604 | &unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1605 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1606 | if (s < e) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1607 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1608 | } |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1609 | if (consumed) { |
| 1610 | if(inShift) |
| 1611 | *consumed = startinpos; |
| 1612 | else |
| 1613 | *consumed = s-starts; |
| 1614 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1615 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1616 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1617 | goto onError; |
| 1618 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1619 | Py_XDECREF(errorHandler); |
| 1620 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1621 | return (PyObject *)unicode; |
| 1622 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1623 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1624 | Py_XDECREF(errorHandler); |
| 1625 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1626 | Py_DECREF(unicode); |
| 1627 | return NULL; |
| 1628 | } |
| 1629 | |
| 1630 | |
| 1631 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1632 | Py_ssize_t size, |
| 1633 | int encodeSetO, |
| 1634 | int encodeWhiteSpace, |
| 1635 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1636 | { |
| 1637 | PyObject *v; |
| 1638 | /* It might be possible to tighten this worst case */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1639 | Py_ssize_t cbAllocated = 5 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1640 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1641 | Py_ssize_t i = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1642 | unsigned int bitsleft = 0; |
| 1643 | unsigned long charsleft = 0; |
| 1644 | char * out; |
| 1645 | char * start; |
| 1646 | |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 1647 | if (cbAllocated / 5 != size) |
| 1648 | return PyErr_NoMemory(); |
| 1649 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1650 | if (size == 0) |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 1651 | return PyString_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1652 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1653 | v = PyString_FromStringAndSize(NULL, cbAllocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1654 | if (v == NULL) |
| 1655 | return NULL; |
| 1656 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1657 | start = out = PyString_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1658 | for (;i < size; ++i) { |
| 1659 | Py_UNICODE ch = s[i]; |
| 1660 | |
| 1661 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1662 | if (ch == '+') { |
| 1663 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1664 | *out++ = '-'; |
| 1665 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1666 | charsleft = ch; |
| 1667 | bitsleft = 16; |
| 1668 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1669 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1670 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1671 | } else { |
| 1672 | *out++ = (char) ch; |
| 1673 | } |
| 1674 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1675 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1676 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1677 | charsleft = 0; |
| 1678 | bitsleft = 0; |
| 1679 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1680 | so no '-' is required, except if the character is itself a '-' */ |
| 1681 | if (B64CHAR(ch) || ch == '-') { |
| 1682 | *out++ = '-'; |
| 1683 | } |
| 1684 | inShift = 0; |
| 1685 | *out++ = (char) ch; |
| 1686 | } else { |
| 1687 | bitsleft += 16; |
| 1688 | charsleft = (charsleft << 16) | ch; |
| 1689 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1690 | |
Jesus Cea | 585ad8a | 2009-07-02 15:37:21 +0000 | [diff] [blame] | 1691 | /* 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] | 1692 | 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] | 1693 | or '-' then the shift sequence will be terminated implicitly and we |
| 1694 | don't have to insert a '-'. */ |
| 1695 | |
| 1696 | if (bitsleft == 0) { |
| 1697 | if (i + 1 < size) { |
| 1698 | Py_UNICODE ch2 = s[i+1]; |
| 1699 | |
| 1700 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1701 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1702 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1703 | *out++ = '-'; |
| 1704 | inShift = 0; |
| 1705 | } else { |
| 1706 | inShift = 0; |
| 1707 | } |
| 1708 | |
| 1709 | } |
| 1710 | else { |
| 1711 | *out++ = '-'; |
| 1712 | inShift = 0; |
| 1713 | } |
| 1714 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1715 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1716 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1717 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1718 | if (bitsleft) { |
| 1719 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1720 | *out++ = '-'; |
| 1721 | } |
| 1722 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1723 | _PyString_Resize(&v, out - start); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1724 | return v; |
| 1725 | } |
| 1726 | |
| 1727 | #undef SPECIAL |
| 1728 | #undef B64 |
| 1729 | #undef B64CHAR |
| 1730 | #undef UB64 |
| 1731 | #undef ENCODE |
| 1732 | #undef DECODE |
| 1733 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1734 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1735 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1736 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1737 | char utf8_code_length[256] = { |
| 1738 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1739 | illegal prefix. see RFC 2279 for details */ |
| 1740 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 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, |
| 1745 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1746 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1747 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1748 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1749 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1750 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1751 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1752 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1753 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1754 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1755 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1756 | }; |
| 1757 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1758 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1759 | Py_ssize_t size, |
| 1760 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1761 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1762 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 1763 | } |
| 1764 | |
| 1765 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1766 | Py_ssize_t size, |
| 1767 | const char *errors, |
| 1768 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1769 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1770 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1771 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1772 | Py_ssize_t startinpos; |
| 1773 | Py_ssize_t endinpos; |
| 1774 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1775 | const char *e; |
| 1776 | PyUnicodeObject *unicode; |
| 1777 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1778 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1779 | PyObject *errorHandler = NULL; |
| 1780 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1781 | |
| 1782 | /* Note: size will always be longer than the resulting Unicode |
| 1783 | character count */ |
| 1784 | unicode = _PyUnicode_New(size); |
| 1785 | if (!unicode) |
| 1786 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1787 | if (size == 0) { |
| 1788 | if (consumed) |
| 1789 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1790 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1791 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1792 | |
| 1793 | /* Unpack UTF-8 encoded data */ |
| 1794 | p = unicode->str; |
| 1795 | e = s + size; |
| 1796 | |
| 1797 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1798 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1799 | |
| 1800 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1801 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1802 | s++; |
| 1803 | continue; |
| 1804 | } |
| 1805 | |
| 1806 | n = utf8_code_length[ch]; |
| 1807 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1808 | if (s + n > e) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1809 | if (consumed) |
| 1810 | break; |
| 1811 | else { |
| 1812 | errmsg = "unexpected end of data"; |
| 1813 | startinpos = s-starts; |
| 1814 | endinpos = size; |
| 1815 | goto utf8Error; |
| 1816 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 1817 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1818 | |
| 1819 | switch (n) { |
| 1820 | |
| 1821 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1822 | errmsg = "unexpected code byte"; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1823 | startinpos = s-starts; |
| 1824 | endinpos = startinpos+1; |
| 1825 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1826 | |
| 1827 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1828 | errmsg = "internal error"; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1829 | startinpos = s-starts; |
| 1830 | endinpos = startinpos+1; |
| 1831 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1832 | |
| 1833 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1834 | if ((s[1] & 0xc0) != 0x80) { |
| 1835 | errmsg = "invalid data"; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1836 | startinpos = s-starts; |
| 1837 | endinpos = startinpos+2; |
| 1838 | goto utf8Error; |
| 1839 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1840 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1841 | if (ch < 0x80) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1842 | startinpos = s-starts; |
| 1843 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1844 | errmsg = "illegal encoding"; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1845 | goto utf8Error; |
| 1846 | } |
| 1847 | else |
| 1848 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1849 | break; |
| 1850 | |
| 1851 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1852 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1853 | (s[2] & 0xc0) != 0x80) { |
| 1854 | errmsg = "invalid data"; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1855 | startinpos = s-starts; |
| 1856 | endinpos = startinpos+3; |
| 1857 | goto utf8Error; |
| 1858 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1859 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1860 | if (ch < 0x0800) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1861 | /* Note: UTF-8 encodings of surrogates are considered |
| 1862 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1863 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1864 | XXX For wide builds (UCS-4) we should probably try |
| 1865 | to recombine the surrogates into a single code |
| 1866 | unit. |
| 1867 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1868 | errmsg = "illegal encoding"; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1869 | startinpos = s-starts; |
| 1870 | endinpos = startinpos+3; |
| 1871 | goto utf8Error; |
| 1872 | } |
| 1873 | else |
| 1874 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1875 | break; |
| 1876 | |
| 1877 | case 4: |
| 1878 | if ((s[1] & 0xc0) != 0x80 || |
| 1879 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1880 | (s[3] & 0xc0) != 0x80) { |
| 1881 | errmsg = "invalid data"; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1882 | startinpos = s-starts; |
| 1883 | endinpos = startinpos+4; |
| 1884 | goto utf8Error; |
| 1885 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1886 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1887 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1888 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1889 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1890 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1891 | || (ch > 0x10ffff)) /* maximum value allowed for |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1892 | UTF-16 */ |
| 1893 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1894 | errmsg = "illegal encoding"; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1895 | startinpos = s-starts; |
| 1896 | endinpos = startinpos+4; |
| 1897 | goto utf8Error; |
| 1898 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1899 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1900 | *p++ = (Py_UNICODE)ch; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1901 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1902 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1903 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1904 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 1905 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1906 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1907 | /* high surrogate = top 10 bits added to D800 */ |
| 1908 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1909 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1910 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1911 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1912 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1913 | break; |
| 1914 | |
| 1915 | default: |
| 1916 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1917 | errmsg = "unsupported Unicode code range"; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1918 | startinpos = s-starts; |
| 1919 | endinpos = startinpos+n; |
| 1920 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1921 | } |
| 1922 | s += n; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1923 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1924 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1925 | utf8Error: |
| 1926 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1927 | if (unicode_decode_call_errorhandler( |
| 1928 | errors, &errorHandler, |
| 1929 | "utf8", errmsg, |
| 1930 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1931 | &unicode, &outpos, &p)) |
| 1932 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1933 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1934 | if (consumed) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1935 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1936 | |
| 1937 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1938 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1939 | goto onError; |
| 1940 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1941 | Py_XDECREF(errorHandler); |
| 1942 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1943 | return (PyObject *)unicode; |
| 1944 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1945 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1946 | Py_XDECREF(errorHandler); |
| 1947 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1948 | Py_DECREF(unicode); |
| 1949 | return NULL; |
| 1950 | } |
| 1951 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1952 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 1953 | and allocate exactly as much space needed at the end. Else allocate the |
| 1954 | maximum possible needed (4 result bytes per Unicode character), and return |
| 1955 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1956 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1957 | PyObject * |
| 1958 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 1959 | Py_ssize_t size, |
| 1960 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1961 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1962 | #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] | 1963 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1964 | Py_ssize_t i; /* index into s of next input byte */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1965 | PyObject *v; /* result string object */ |
| 1966 | char *p; /* next free byte in output buffer */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1967 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1968 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1969 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1970 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1971 | assert(s != NULL); |
| 1972 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1973 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1974 | if (size <= MAX_SHORT_UNICHARS) { |
| 1975 | /* Write into the stack buffer; nallocated can't overflow. |
| 1976 | * At the end, we'll allocate exactly as much heap space as it |
| 1977 | * turns out we need. |
| 1978 | */ |
| 1979 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
| 1980 | v = NULL; /* will allocate after we're done */ |
| 1981 | p = stackbuf; |
| 1982 | } |
| 1983 | else { |
| 1984 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 1985 | nallocated = size * 4; |
| 1986 | if (nallocated / 4 != size) /* overflow! */ |
| 1987 | return PyErr_NoMemory(); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1988 | v = PyString_FromStringAndSize(NULL, nallocated); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1989 | if (v == NULL) |
| 1990 | return NULL; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1991 | p = PyString_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1992 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1993 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1994 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1995 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1996 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1997 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1998 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1999 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2000 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2001 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2002 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 2003 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 2004 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2005 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2006 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2007 | /* Encode UCS2 Unicode ordinals */ |
| 2008 | if (ch < 0x10000) { |
| 2009 | /* Special case: check for high surrogate */ |
| 2010 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 2011 | Py_UCS4 ch2 = s[i]; |
| 2012 | /* Check for low surrogate and combine the two to |
| 2013 | form a UCS4 value */ |
| 2014 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2015 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2016 | i++; |
| 2017 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2018 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2019 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2020 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2021 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2022 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2023 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2024 | continue; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 2025 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2026 | encodeUCS4: |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2027 | /* Encode UCS4 Unicode ordinals */ |
| 2028 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2029 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 2030 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2031 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2032 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2033 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2034 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2035 | if (v == NULL) { |
| 2036 | /* This was stack allocated. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2037 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2038 | assert(nneeded <= nallocated); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2039 | v = PyString_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2040 | } |
| 2041 | else { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 2042 | /* Cut back to size actually needed. */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2043 | nneeded = p - PyString_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2044 | assert(nneeded <= nallocated); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2045 | _PyString_Resize(&v, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2046 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2047 | return v; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2048 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2049 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2052 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 2053 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2054 | if (!PyUnicode_Check(unicode)) { |
| 2055 | PyErr_BadArgument(); |
| 2056 | return NULL; |
| 2057 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 2058 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2059 | PyUnicode_GET_SIZE(unicode), |
| 2060 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2063 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 2064 | |
| 2065 | PyObject * |
| 2066 | PyUnicode_DecodeUTF32(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) |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2070 | { |
| 2071 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 2072 | } |
| 2073 | |
| 2074 | PyObject * |
| 2075 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2076 | Py_ssize_t size, |
| 2077 | const char *errors, |
| 2078 | int *byteorder, |
| 2079 | Py_ssize_t *consumed) |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2080 | { |
| 2081 | const char *starts = s; |
| 2082 | Py_ssize_t startinpos; |
| 2083 | Py_ssize_t endinpos; |
| 2084 | Py_ssize_t outpos; |
| 2085 | PyUnicodeObject *unicode; |
| 2086 | Py_UNICODE *p; |
| 2087 | #ifndef Py_UNICODE_WIDE |
Antoine Pitrou | 4595e51 | 2010-06-11 21:48:02 +0000 | [diff] [blame^] | 2088 | int pairs = 0; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2089 | #else |
| 2090 | const int pairs = 0; |
| 2091 | #endif |
Antoine Pitrou | 4595e51 | 2010-06-11 21:48:02 +0000 | [diff] [blame^] | 2092 | const unsigned char *q, *e, *qq; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2093 | int bo = 0; /* assume native ordering by default */ |
| 2094 | const char *errmsg = ""; |
Walter Dörwald | 20b40d3 | 2007-08-17 16:52:50 +0000 | [diff] [blame] | 2095 | /* Offsets from q for retrieving bytes in the right order. */ |
| 2096 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2097 | int iorder[] = {0, 1, 2, 3}; |
| 2098 | #else |
| 2099 | int iorder[] = {3, 2, 1, 0}; |
| 2100 | #endif |
Walter Dörwald | 9ab80a9 | 2007-08-17 16:58:43 +0000 | [diff] [blame] | 2101 | PyObject *errorHandler = NULL; |
| 2102 | PyObject *exc = NULL; |
Antoine Pitrou | 4595e51 | 2010-06-11 21:48:02 +0000 | [diff] [blame^] | 2103 | |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2104 | q = (unsigned char *)s; |
| 2105 | e = q + size; |
| 2106 | |
| 2107 | if (byteorder) |
| 2108 | bo = *byteorder; |
| 2109 | |
| 2110 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2111 | byte order setting accordingly. In native mode, the leading BOM |
| 2112 | mark is skipped, in all other modes, it is copied to the output |
| 2113 | stream as-is (giving a ZWNBSP character). */ |
| 2114 | if (bo == 0) { |
| 2115 | if (size >= 4) { |
| 2116 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2117 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2118 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
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 | #else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2128 | if (bom == 0x0000FEFF) { |
| 2129 | q += 4; |
| 2130 | bo = 1; |
| 2131 | } |
| 2132 | else if (bom == 0xFFFE0000) { |
| 2133 | q += 4; |
| 2134 | bo = -1; |
| 2135 | } |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2136 | #endif |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2137 | } |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2138 | } |
| 2139 | |
| 2140 | if (bo == -1) { |
| 2141 | /* force LE */ |
| 2142 | iorder[0] = 0; |
| 2143 | iorder[1] = 1; |
| 2144 | iorder[2] = 2; |
| 2145 | iorder[3] = 3; |
| 2146 | } |
| 2147 | else if (bo == 1) { |
| 2148 | /* force BE */ |
| 2149 | iorder[0] = 3; |
| 2150 | iorder[1] = 2; |
| 2151 | iorder[2] = 1; |
| 2152 | iorder[3] = 0; |
| 2153 | } |
| 2154 | |
Antoine Pitrou | 4595e51 | 2010-06-11 21:48:02 +0000 | [diff] [blame^] | 2155 | /* On narrow builds we split characters outside the BMP into two |
| 2156 | codepoints => count how much extra space we need. */ |
| 2157 | #ifndef Py_UNICODE_WIDE |
| 2158 | for (qq = q; qq < e; qq += 4) |
| 2159 | if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) |
| 2160 | pairs++; |
| 2161 | #endif |
| 2162 | |
| 2163 | /* This might be one to much, because of a BOM */ |
| 2164 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 2165 | if (!unicode) |
| 2166 | return NULL; |
| 2167 | if (size == 0) |
| 2168 | return (PyObject *)unicode; |
| 2169 | |
| 2170 | /* Unpack UTF-32 encoded data */ |
| 2171 | p = unicode->str; |
| 2172 | |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2173 | while (q < e) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2174 | Py_UCS4 ch; |
| 2175 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 2176 | if (e-q<4) { |
| 2177 | if (consumed) |
| 2178 | break; |
| 2179 | errmsg = "truncated data"; |
| 2180 | startinpos = ((const char *)q)-starts; |
| 2181 | endinpos = ((const char *)e)-starts; |
| 2182 | goto utf32Error; |
| 2183 | /* The remaining input chars are ignored if the callback |
| 2184 | chooses to skip the input */ |
| 2185 | } |
| 2186 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2187 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2188 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2189 | if (ch >= 0x110000) |
| 2190 | { |
| 2191 | errmsg = "codepoint not in range(0x110000)"; |
| 2192 | startinpos = ((const char *)q)-starts; |
| 2193 | endinpos = startinpos+4; |
| 2194 | goto utf32Error; |
| 2195 | } |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2196 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2197 | if (ch >= 0x10000) |
| 2198 | { |
| 2199 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 2200 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2201 | } |
| 2202 | else |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2203 | #endif |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2204 | *p++ = ch; |
| 2205 | q += 4; |
| 2206 | continue; |
| 2207 | utf32Error: |
| 2208 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2209 | if (unicode_decode_call_errorhandler( |
| 2210 | errors, &errorHandler, |
| 2211 | "utf32", errmsg, |
Georg Brandl | f7a09be | 2009-09-17 11:33:31 +0000 | [diff] [blame] | 2212 | starts, size, &startinpos, &endinpos, &exc, (const char **)&q, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2213 | &unicode, &outpos, &p)) |
| 2214 | goto onError; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2215 | } |
| 2216 | |
| 2217 | if (byteorder) |
| 2218 | *byteorder = bo; |
| 2219 | |
| 2220 | if (consumed) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2221 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2222 | |
| 2223 | /* Adjust length */ |
| 2224 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 2225 | goto onError; |
| 2226 | |
| 2227 | Py_XDECREF(errorHandler); |
| 2228 | Py_XDECREF(exc); |
| 2229 | return (PyObject *)unicode; |
| 2230 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2231 | onError: |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2232 | Py_DECREF(unicode); |
| 2233 | Py_XDECREF(errorHandler); |
| 2234 | Py_XDECREF(exc); |
| 2235 | return NULL; |
| 2236 | } |
| 2237 | |
| 2238 | PyObject * |
| 2239 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2240 | Py_ssize_t size, |
| 2241 | const char *errors, |
| 2242 | int byteorder) |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2243 | { |
| 2244 | PyObject *v; |
| 2245 | unsigned char *p; |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2246 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2247 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2248 | Py_ssize_t i, pairs; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2249 | #else |
| 2250 | const int pairs = 0; |
| 2251 | #endif |
| 2252 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2253 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2254 | int iorder[] = {0, 1, 2, 3}; |
| 2255 | #else |
| 2256 | int iorder[] = {3, 2, 1, 0}; |
| 2257 | #endif |
| 2258 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2259 | #define STORECHAR(CH) \ |
| 2260 | do { \ |
| 2261 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 2262 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 2263 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 2264 | p[iorder[0]] = (CH) & 0xff; \ |
| 2265 | p += 4; \ |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2266 | } while(0) |
| 2267 | |
| 2268 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 2269 | so we need less space. */ |
| 2270 | #ifndef Py_UNICODE_WIDE |
| 2271 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2272 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2273 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 2274 | pairs++; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2275 | #endif |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2276 | nsize = (size - pairs + (byteorder == 0)); |
| 2277 | bytesize = nsize * 4; |
| 2278 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2279 | return PyErr_NoMemory(); |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2280 | v = PyString_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2281 | if (v == NULL) |
| 2282 | return NULL; |
| 2283 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2284 | p = (unsigned char *)PyString_AS_STRING(v); |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2285 | if (byteorder == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2286 | STORECHAR(0xFEFF); |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2287 | if (size == 0) |
| 2288 | return v; |
| 2289 | |
| 2290 | if (byteorder == -1) { |
| 2291 | /* force LE */ |
| 2292 | iorder[0] = 0; |
| 2293 | iorder[1] = 1; |
| 2294 | iorder[2] = 2; |
| 2295 | iorder[3] = 3; |
| 2296 | } |
| 2297 | else if (byteorder == 1) { |
| 2298 | /* force BE */ |
| 2299 | iorder[0] = 3; |
| 2300 | iorder[1] = 2; |
| 2301 | iorder[2] = 1; |
| 2302 | iorder[3] = 0; |
| 2303 | } |
| 2304 | |
| 2305 | while (size-- > 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2306 | Py_UCS4 ch = *s++; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2307 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2308 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 2309 | Py_UCS4 ch2 = *s; |
| 2310 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 2311 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 2312 | s++; |
| 2313 | size--; |
| 2314 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 2315 | } |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2316 | #endif |
| 2317 | STORECHAR(ch); |
| 2318 | } |
| 2319 | return v; |
| 2320 | #undef STORECHAR |
| 2321 | } |
| 2322 | |
| 2323 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 2324 | { |
| 2325 | if (!PyUnicode_Check(unicode)) { |
| 2326 | PyErr_BadArgument(); |
| 2327 | return NULL; |
| 2328 | } |
| 2329 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2330 | PyUnicode_GET_SIZE(unicode), |
| 2331 | NULL, |
| 2332 | 0); |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2333 | } |
| 2334 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2335 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 2336 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2337 | PyObject * |
| 2338 | PyUnicode_DecodeUTF16(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) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2342 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2343 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 2344 | } |
| 2345 | |
| 2346 | PyObject * |
| 2347 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2348 | Py_ssize_t size, |
| 2349 | const char *errors, |
| 2350 | int *byteorder, |
| 2351 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2352 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2353 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2354 | Py_ssize_t startinpos; |
| 2355 | Py_ssize_t endinpos; |
| 2356 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2357 | PyUnicodeObject *unicode; |
| 2358 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2359 | const unsigned char *q, *e; |
| 2360 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2361 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2362 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 2363 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2364 | int ihi = 1, ilo = 0; |
| 2365 | #else |
| 2366 | int ihi = 0, ilo = 1; |
| 2367 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2368 | PyObject *errorHandler = NULL; |
| 2369 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2370 | |
| 2371 | /* Note: size will always be longer than the resulting Unicode |
| 2372 | character count */ |
| 2373 | unicode = _PyUnicode_New(size); |
| 2374 | if (!unicode) |
| 2375 | return NULL; |
| 2376 | if (size == 0) |
| 2377 | return (PyObject *)unicode; |
| 2378 | |
| 2379 | /* Unpack UTF-16 encoded data */ |
| 2380 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2381 | q = (unsigned char *)s; |
| 2382 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2383 | |
| 2384 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2385 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2386 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2387 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2388 | byte order setting accordingly. In native mode, the leading BOM |
| 2389 | mark is skipped, in all other modes, it is copied to the output |
| 2390 | stream as-is (giving a ZWNBSP character). */ |
| 2391 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2392 | if (size >= 2) { |
| 2393 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2394 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
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 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2403 | #else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2404 | if (bom == 0xFEFF) { |
| 2405 | q += 2; |
| 2406 | bo = 1; |
| 2407 | } |
| 2408 | else if (bom == 0xFFFE) { |
| 2409 | q += 2; |
| 2410 | bo = -1; |
| 2411 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2412 | #endif |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2413 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2414 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2415 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2416 | if (bo == -1) { |
| 2417 | /* force LE */ |
| 2418 | ihi = 1; |
| 2419 | ilo = 0; |
| 2420 | } |
| 2421 | else if (bo == 1) { |
| 2422 | /* force BE */ |
| 2423 | ihi = 0; |
| 2424 | ilo = 1; |
| 2425 | } |
| 2426 | |
| 2427 | while (q < e) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2428 | Py_UNICODE ch; |
| 2429 | /* remaining bytes at the end? (size should be even) */ |
| 2430 | if (e-q<2) { |
| 2431 | if (consumed) |
| 2432 | break; |
| 2433 | errmsg = "truncated data"; |
| 2434 | startinpos = ((const char *)q)-starts; |
| 2435 | endinpos = ((const char *)e)-starts; |
| 2436 | goto utf16Error; |
| 2437 | /* The remaining input chars are ignored if the callback |
| 2438 | chooses to skip the input */ |
| 2439 | } |
| 2440 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2441 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 2442 | q += 2; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2443 | |
| 2444 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 2445 | *p++ = ch; |
| 2446 | continue; |
| 2447 | } |
| 2448 | |
| 2449 | /* UTF-16 code pair: */ |
| 2450 | if (q >= e) { |
| 2451 | errmsg = "unexpected end of data"; |
| 2452 | startinpos = (((const char *)q)-2)-starts; |
| 2453 | endinpos = ((const char *)e)-starts; |
| 2454 | goto utf16Error; |
| 2455 | } |
| 2456 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 2457 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 2458 | q += 2; |
| 2459 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2460 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2461 | *p++ = ch; |
| 2462 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2463 | #else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2464 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2465 | #endif |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2466 | continue; |
| 2467 | } |
| 2468 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2469 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2470 | startinpos = (((const char *)q)-4)-starts; |
| 2471 | endinpos = startinpos+2; |
| 2472 | goto utf16Error; |
| 2473 | } |
| 2474 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 2475 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2476 | errmsg = "illegal encoding"; |
| 2477 | startinpos = (((const char *)q)-2)-starts; |
| 2478 | endinpos = startinpos+2; |
| 2479 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2480 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2481 | utf16Error: |
| 2482 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2483 | if (unicode_decode_call_errorhandler( |
| 2484 | errors, &errorHandler, |
| 2485 | "utf16", errmsg, |
| 2486 | starts, size, &startinpos, &endinpos, &exc, (const char **)&q, |
| 2487 | &unicode, &outpos, &p)) |
| 2488 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2489 | } |
| 2490 | |
| 2491 | if (byteorder) |
| 2492 | *byteorder = bo; |
| 2493 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2494 | if (consumed) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2495 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2496 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2497 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2498 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2499 | goto onError; |
| 2500 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2501 | Py_XDECREF(errorHandler); |
| 2502 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2503 | return (PyObject *)unicode; |
| 2504 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2505 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2506 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2507 | Py_XDECREF(errorHandler); |
| 2508 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2509 | return NULL; |
| 2510 | } |
| 2511 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2512 | PyObject * |
| 2513 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2514 | Py_ssize_t size, |
| 2515 | const char *errors, |
| 2516 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2517 | { |
| 2518 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2519 | unsigned char *p; |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2520 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2521 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2522 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2523 | #else |
| 2524 | const int pairs = 0; |
| 2525 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2526 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2527 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2528 | int ihi = 1, ilo = 0; |
| 2529 | #else |
| 2530 | int ihi = 0, ilo = 1; |
| 2531 | #endif |
| 2532 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2533 | #define STORECHAR(CH) \ |
| 2534 | do { \ |
| 2535 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 2536 | p[ilo] = (CH) & 0xff; \ |
| 2537 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2538 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2539 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2540 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2541 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2542 | if (s[i] >= 0x10000) |
| 2543 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2544 | #endif |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2545 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 2546 | if (size > PY_SSIZE_T_MAX || |
| 2547 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2548 | return PyErr_NoMemory(); |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2549 | nsize = size + pairs + (byteorder == 0); |
| 2550 | bytesize = nsize * 2; |
| 2551 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2552 | return PyErr_NoMemory(); |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2553 | v = PyString_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2554 | if (v == NULL) |
| 2555 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2556 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2557 | p = (unsigned char *)PyString_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2558 | if (byteorder == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2559 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2560 | if (size == 0) |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2561 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2562 | |
| 2563 | if (byteorder == -1) { |
| 2564 | /* force LE */ |
| 2565 | ihi = 1; |
| 2566 | ilo = 0; |
| 2567 | } |
| 2568 | else if (byteorder == 1) { |
| 2569 | /* force BE */ |
| 2570 | ihi = 0; |
| 2571 | ilo = 1; |
| 2572 | } |
| 2573 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2574 | while (size-- > 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2575 | Py_UNICODE ch = *s++; |
| 2576 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2577 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2578 | if (ch >= 0x10000) { |
| 2579 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2580 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 2581 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2582 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2583 | STORECHAR(ch); |
| 2584 | if (ch2) |
| 2585 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2586 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2587 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2588 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2589 | } |
| 2590 | |
| 2591 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 2592 | { |
| 2593 | if (!PyUnicode_Check(unicode)) { |
| 2594 | PyErr_BadArgument(); |
| 2595 | return NULL; |
| 2596 | } |
| 2597 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2598 | PyUnicode_GET_SIZE(unicode), |
| 2599 | NULL, |
| 2600 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2601 | } |
| 2602 | |
| 2603 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 2604 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2605 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 2606 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2607 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2608 | Py_ssize_t size, |
| 2609 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2610 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2611 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2612 | Py_ssize_t startinpos; |
| 2613 | Py_ssize_t endinpos; |
| 2614 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2615 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2616 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2617 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2618 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2619 | char* message; |
| 2620 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2621 | PyObject *errorHandler = NULL; |
| 2622 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2623 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2624 | /* Escaped strings will always be longer than the resulting |
| 2625 | 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] | 2626 | length after conversion to the true value. |
| 2627 | (but if the error callback returns a long replacement string |
| 2628 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2629 | v = _PyUnicode_New(size); |
| 2630 | if (v == NULL) |
| 2631 | goto onError; |
| 2632 | if (size == 0) |
| 2633 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2634 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2635 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2636 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2637 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2638 | while (s < end) { |
| 2639 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2640 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2641 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2642 | |
| 2643 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2644 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2645 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2646 | continue; |
| 2647 | } |
| 2648 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2649 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2650 | /* \ - Escapes */ |
| 2651 | s++; |
Guido van Rossum | 1c1ac38 | 2007-10-29 22:15:05 +0000 | [diff] [blame] | 2652 | c = *s++; |
| 2653 | if (s > end) |
| 2654 | c = '\0'; /* Invalid after \ */ |
| 2655 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2656 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2657 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2658 | case '\n': break; |
| 2659 | case '\\': *p++ = '\\'; break; |
| 2660 | case '\'': *p++ = '\''; break; |
| 2661 | case '\"': *p++ = '\"'; break; |
| 2662 | case 'b': *p++ = '\b'; break; |
| 2663 | case 'f': *p++ = '\014'; break; /* FF */ |
| 2664 | case 't': *p++ = '\t'; break; |
| 2665 | case 'n': *p++ = '\n'; break; |
| 2666 | case 'r': *p++ = '\r'; break; |
| 2667 | case 'v': *p++ = '\013'; break; /* VT */ |
| 2668 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 2669 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2670 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2671 | case '0': case '1': case '2': case '3': |
| 2672 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2673 | x = s[-1] - '0'; |
Guido van Rossum | 1c1ac38 | 2007-10-29 22:15:05 +0000 | [diff] [blame] | 2674 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2675 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 1c1ac38 | 2007-10-29 22:15:05 +0000 | [diff] [blame] | 2676 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2677 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2678 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2679 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2680 | break; |
| 2681 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2682 | /* hex escapes */ |
| 2683 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2684 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2685 | digits = 2; |
| 2686 | message = "truncated \\xXX escape"; |
| 2687 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2688 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2689 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2690 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2691 | digits = 4; |
| 2692 | message = "truncated \\uXXXX escape"; |
| 2693 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2694 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2695 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2696 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2697 | digits = 8; |
| 2698 | message = "truncated \\UXXXXXXXX escape"; |
| 2699 | hexescape: |
| 2700 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2701 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2702 | if (s+digits>end) { |
| 2703 | endinpos = size; |
| 2704 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2705 | errors, &errorHandler, |
| 2706 | "unicodeescape", "end of string in escape sequence", |
| 2707 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2708 | &v, &outpos, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2709 | goto onError; |
| 2710 | goto nextByte; |
| 2711 | } |
| 2712 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2713 | c = (unsigned char) s[i]; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2714 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2715 | endinpos = (s+i+1)-starts; |
| 2716 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2717 | errors, &errorHandler, |
| 2718 | "unicodeescape", message, |
| 2719 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2720 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2721 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2722 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2723 | } |
| 2724 | chr = (chr<<4) & ~0xF; |
| 2725 | if (c >= '0' && c <= '9') |
| 2726 | chr += c - '0'; |
| 2727 | else if (c >= 'a' && c <= 'f') |
| 2728 | chr += 10 + c - 'a'; |
| 2729 | else |
| 2730 | chr += 10 + c - 'A'; |
| 2731 | } |
| 2732 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 2733 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2734 | /* _decoding_error will have already written into the |
| 2735 | target buffer. */ |
| 2736 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2737 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2738 | /* when we get here, chr is a 32-bit unicode character */ |
| 2739 | if (chr <= 0xffff) |
| 2740 | /* UCS-2 character */ |
| 2741 | *p++ = (Py_UNICODE) chr; |
| 2742 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2743 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2744 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2745 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2746 | *p++ = chr; |
| 2747 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2748 | chr -= 0x10000L; |
| 2749 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2750 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2751 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2752 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2753 | endinpos = s-starts; |
| 2754 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2755 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2756 | errors, &errorHandler, |
| 2757 | "unicodeescape", "illegal Unicode character", |
| 2758 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2759 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2760 | goto onError; |
| 2761 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2762 | break; |
| 2763 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2764 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2765 | case 'N': |
| 2766 | message = "malformed \\N character escape"; |
| 2767 | if (ucnhash_CAPI == NULL) { |
| 2768 | /* load the unicode data module */ |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2769 | PyObject *m, *api; |
Christian Heimes | 000a074 | 2008-01-03 22:16:32 +0000 | [diff] [blame] | 2770 | m = PyImport_ImportModuleNoBlock("unicodedata"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2771 | if (m == NULL) |
| 2772 | goto ucnhashError; |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2773 | api = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2774 | Py_DECREF(m); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2775 | if (api == NULL) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2776 | goto ucnhashError; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2777 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2778 | Py_DECREF(api); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2779 | if (ucnhash_CAPI == NULL) |
| 2780 | goto ucnhashError; |
| 2781 | } |
| 2782 | if (*s == '{') { |
| 2783 | const char *start = s+1; |
| 2784 | /* look for the closing brace */ |
| 2785 | while (*s != '}' && s < end) |
| 2786 | s++; |
| 2787 | if (s > start && s < end && *s == '}') { |
| 2788 | /* found a name. look it up in the unicode database */ |
| 2789 | message = "unknown Unicode character name"; |
| 2790 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 2791 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2792 | goto store; |
| 2793 | } |
| 2794 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2795 | endinpos = s-starts; |
| 2796 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2797 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2798 | errors, &errorHandler, |
| 2799 | "unicodeescape", message, |
| 2800 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2801 | &v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2802 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2803 | break; |
| 2804 | |
| 2805 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2806 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2807 | message = "\\ at end of string"; |
| 2808 | s--; |
| 2809 | endinpos = s-starts; |
| 2810 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2811 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2812 | errors, &errorHandler, |
| 2813 | "unicodeescape", message, |
| 2814 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2815 | &v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2816 | goto onError; |
| 2817 | } |
| 2818 | else { |
| 2819 | *p++ = '\\'; |
| 2820 | *p++ = (unsigned char)s[-1]; |
| 2821 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2822 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2823 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2824 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2825 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2826 | } |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2827 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2828 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 2829 | Py_XDECREF(errorHandler); |
| 2830 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2831 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2832 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2833 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2834 | PyErr_SetString( |
| 2835 | PyExc_UnicodeError, |
| 2836 | "\\N escapes not supported (can't load unicodedata module)" |
| 2837 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2838 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2839 | Py_XDECREF(errorHandler); |
| 2840 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 2841 | return NULL; |
| 2842 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2843 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2844 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2845 | Py_XDECREF(errorHandler); |
| 2846 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2847 | return NULL; |
| 2848 | } |
| 2849 | |
| 2850 | /* Return a Unicode-Escape string version of the Unicode object. |
| 2851 | |
| 2852 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 2853 | appropriate. |
| 2854 | |
| 2855 | */ |
| 2856 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 2857 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2858 | Py_ssize_t size, |
| 2859 | Py_UNICODE ch) |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 2860 | { |
| 2861 | /* like wcschr, but doesn't stop at NULL characters */ |
| 2862 | |
| 2863 | while (size-- > 0) { |
| 2864 | if (*s == ch) |
| 2865 | return s; |
| 2866 | s++; |
| 2867 | } |
| 2868 | |
| 2869 | return NULL; |
| 2870 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 2871 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2872 | static |
| 2873 | PyObject *unicodeescape_string(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2874 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2875 | int quotes) |
| 2876 | { |
| 2877 | PyObject *repr; |
| 2878 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2879 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2880 | static const char *hexdigit = "0123456789abcdef"; |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2881 | #ifdef Py_UNICODE_WIDE |
| 2882 | const Py_ssize_t expandsize = 10; |
| 2883 | #else |
| 2884 | const Py_ssize_t expandsize = 6; |
| 2885 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2886 | |
Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 2887 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 2888 | better to choose a different scheme. Perhaps scan the |
| 2889 | first N-chars of the string and allocate based on that size. |
| 2890 | */ |
| 2891 | /* Initial allocation is based on the longest-possible unichr |
| 2892 | escape. |
| 2893 | |
| 2894 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 2895 | unichr, so in this case it's the longest unichr escape. In |
| 2896 | narrow (UTF-16) builds this is five chars per source unichr |
| 2897 | since there are two unichrs in the surrogate pair, so in narrow |
| 2898 | (UTF-16) builds it's not the longest unichr escape. |
| 2899 | |
| 2900 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 2901 | so in the narrow (UTF-16) build case it's the longest unichr |
| 2902 | escape. |
| 2903 | */ |
| 2904 | |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2905 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2906 | return PyErr_NoMemory(); |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2907 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2908 | repr = PyString_FromStringAndSize(NULL, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2909 | 2 |
| 2910 | + expandsize*size |
| 2911 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2912 | if (repr == NULL) |
| 2913 | return NULL; |
| 2914 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2915 | p = PyString_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2916 | |
| 2917 | if (quotes) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2918 | *p++ = 'u'; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2919 | *p++ = (findchar(s, size, '\'') && |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2920 | !findchar(s, size, '"')) ? '"' : '\''; |
| 2921 | } |
| 2922 | while (size-- > 0) { |
| 2923 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2924 | |
Hye-Shik Chang | 835b243 | 2005-12-17 04:38:31 +0000 | [diff] [blame] | 2925 | /* Escape quotes and backslashes */ |
| 2926 | if ((quotes && |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2927 | ch == (Py_UNICODE) PyString_AS_STRING(repr)[1]) || ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2928 | *p++ = '\\'; |
| 2929 | *p++ = (char) ch; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2930 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2931 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2932 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 2933 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2934 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 2935 | else if (ch >= 0x10000) { |
| 2936 | *p++ = '\\'; |
| 2937 | *p++ = 'U'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2938 | *p++ = hexdigit[(ch >> 28) & 0x0000000F]; |
| 2939 | *p++ = hexdigit[(ch >> 24) & 0x0000000F]; |
| 2940 | *p++ = hexdigit[(ch >> 20) & 0x0000000F]; |
| 2941 | *p++ = hexdigit[(ch >> 16) & 0x0000000F]; |
| 2942 | *p++ = hexdigit[(ch >> 12) & 0x0000000F]; |
| 2943 | *p++ = hexdigit[(ch >> 8) & 0x0000000F]; |
| 2944 | *p++ = hexdigit[(ch >> 4) & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2945 | *p++ = hexdigit[ch & 0x0000000F]; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2946 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2947 | } |
Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 2948 | #else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2949 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 2950 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 2951 | Py_UNICODE ch2; |
| 2952 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2953 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 2954 | ch2 = *s++; |
| 2955 | size--; |
| 2956 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 2957 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 2958 | *p++ = '\\'; |
| 2959 | *p++ = 'U'; |
| 2960 | *p++ = hexdigit[(ucs >> 28) & 0x0000000F]; |
| 2961 | *p++ = hexdigit[(ucs >> 24) & 0x0000000F]; |
| 2962 | *p++ = hexdigit[(ucs >> 20) & 0x0000000F]; |
| 2963 | *p++ = hexdigit[(ucs >> 16) & 0x0000000F]; |
| 2964 | *p++ = hexdigit[(ucs >> 12) & 0x0000000F]; |
| 2965 | *p++ = hexdigit[(ucs >> 8) & 0x0000000F]; |
| 2966 | *p++ = hexdigit[(ucs >> 4) & 0x0000000F]; |
| 2967 | *p++ = hexdigit[ucs & 0x0000000F]; |
| 2968 | continue; |
| 2969 | } |
| 2970 | /* Fall through: isolated surrogates are copied as-is */ |
| 2971 | s--; |
| 2972 | size++; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 2973 | } |
Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 2974 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2975 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2976 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2977 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2978 | *p++ = '\\'; |
| 2979 | *p++ = 'u'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2980 | *p++ = hexdigit[(ch >> 12) & 0x000F]; |
| 2981 | *p++ = hexdigit[(ch >> 8) & 0x000F]; |
| 2982 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2983 | *p++ = hexdigit[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2984 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2985 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2986 | /* Map special whitespace to '\t', \n', '\r' */ |
| 2987 | else if (ch == '\t') { |
| 2988 | *p++ = '\\'; |
| 2989 | *p++ = 't'; |
| 2990 | } |
| 2991 | else if (ch == '\n') { |
| 2992 | *p++ = '\\'; |
| 2993 | *p++ = 'n'; |
| 2994 | } |
| 2995 | else if (ch == '\r') { |
| 2996 | *p++ = '\\'; |
| 2997 | *p++ = 'r'; |
| 2998 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2999 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3000 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 3001 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3002 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3003 | *p++ = 'x'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3004 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 3005 | *p++ = hexdigit[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3006 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3007 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3008 | /* Copy everything else as-is */ |
| 3009 | else |
| 3010 | *p++ = (char) ch; |
| 3011 | } |
| 3012 | if (quotes) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3013 | *p++ = PyString_AS_STRING(repr)[1]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3014 | |
| 3015 | *p = '\0'; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3016 | _PyString_Resize(&repr, p - PyString_AS_STRING(repr)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3017 | return repr; |
| 3018 | } |
| 3019 | |
| 3020 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3021 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3022 | { |
| 3023 | return unicodeescape_string(s, size, 0); |
| 3024 | } |
| 3025 | |
| 3026 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 3027 | { |
| 3028 | if (!PyUnicode_Check(unicode)) { |
| 3029 | PyErr_BadArgument(); |
| 3030 | return NULL; |
| 3031 | } |
| 3032 | return PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3033 | PyUnicode_GET_SIZE(unicode)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3034 | } |
| 3035 | |
| 3036 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 3037 | |
| 3038 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3039 | Py_ssize_t size, |
| 3040 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3041 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3042 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3043 | Py_ssize_t startinpos; |
| 3044 | Py_ssize_t endinpos; |
| 3045 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3046 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3047 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3048 | const char *end; |
| 3049 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3050 | PyObject *errorHandler = NULL; |
| 3051 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3052 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3053 | /* Escaped strings will always be longer than the resulting |
| 3054 | 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] | 3055 | length after conversion to the true value. (But decoding error |
| 3056 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3057 | v = _PyUnicode_New(size); |
| 3058 | if (v == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3059 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3060 | if (size == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3061 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3062 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3063 | end = s + size; |
| 3064 | while (s < end) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3065 | unsigned char c; |
| 3066 | Py_UCS4 x; |
| 3067 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3068 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3069 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3070 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3071 | if (*s != '\\') { |
| 3072 | *p++ = (unsigned char)*s++; |
| 3073 | continue; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 3074 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3075 | startinpos = s-starts; |
| 3076 | |
| 3077 | /* \u-escapes are only interpreted iff the number of leading |
| 3078 | backslashes if odd */ |
| 3079 | bs = s; |
| 3080 | for (;s < end;) { |
| 3081 | if (*s != '\\') |
| 3082 | break; |
| 3083 | *p++ = (unsigned char)*s++; |
| 3084 | } |
| 3085 | if (((s - bs) & 1) == 0 || |
| 3086 | s >= end || |
| 3087 | (*s != 'u' && *s != 'U')) { |
| 3088 | continue; |
| 3089 | } |
| 3090 | p--; |
| 3091 | count = *s=='u' ? 4 : 8; |
| 3092 | s++; |
| 3093 | |
| 3094 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 3095 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3096 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 3097 | c = (unsigned char)*s; |
| 3098 | if (!isxdigit(c)) { |
| 3099 | endinpos = s-starts; |
| 3100 | if (unicode_decode_call_errorhandler( |
| 3101 | errors, &errorHandler, |
| 3102 | "rawunicodeescape", "truncated \\uXXXX", |
| 3103 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3104 | &v, &outpos, &p)) |
| 3105 | goto onError; |
| 3106 | goto nextByte; |
| 3107 | } |
| 3108 | x = (x<<4) & ~0xF; |
| 3109 | if (c >= '0' && c <= '9') |
| 3110 | x += c - '0'; |
| 3111 | else if (c >= 'a' && c <= 'f') |
| 3112 | x += 10 + c - 'a'; |
| 3113 | else |
| 3114 | x += 10 + c - 'A'; |
| 3115 | } |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3116 | if (x <= 0xffff) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3117 | /* UCS-2 character */ |
| 3118 | *p++ = (Py_UNICODE) x; |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3119 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3120 | /* UCS-4 character. Either store directly, or as |
| 3121 | surrogate pair. */ |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3122 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3123 | *p++ = (Py_UNICODE) x; |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3124 | #else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3125 | x -= 0x10000L; |
| 3126 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 3127 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3128 | #endif |
| 3129 | } else { |
| 3130 | endinpos = s-starts; |
| 3131 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3132 | if (unicode_decode_call_errorhandler( |
| 3133 | errors, &errorHandler, |
| 3134 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3135 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3136 | &v, &outpos, &p)) |
| 3137 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3138 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3139 | nextByte: |
| 3140 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3141 | } |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3142 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3143 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3144 | Py_XDECREF(errorHandler); |
| 3145 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3146 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3147 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3148 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3149 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3150 | Py_XDECREF(errorHandler); |
| 3151 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3152 | return NULL; |
| 3153 | } |
| 3154 | |
| 3155 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3156 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3157 | { |
| 3158 | PyObject *repr; |
| 3159 | char *p; |
| 3160 | char *q; |
| 3161 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3162 | static const char *hexdigit = "0123456789abcdef"; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3163 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 3164 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3165 | #else |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 3166 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3167 | #endif |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 3168 | |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 3169 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3170 | return PyErr_NoMemory(); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 3171 | |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 3172 | repr = PyString_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3173 | if (repr == NULL) |
| 3174 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3175 | if (size == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3176 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3177 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3178 | p = q = PyString_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3179 | while (size-- > 0) { |
| 3180 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3181 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3182 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 3183 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3184 | *p++ = '\\'; |
| 3185 | *p++ = 'U'; |
| 3186 | *p++ = hexdigit[(ch >> 28) & 0xf]; |
| 3187 | *p++ = hexdigit[(ch >> 24) & 0xf]; |
| 3188 | *p++ = hexdigit[(ch >> 20) & 0xf]; |
| 3189 | *p++ = hexdigit[(ch >> 16) & 0xf]; |
| 3190 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 3191 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 3192 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 3193 | *p++ = hexdigit[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3194 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3195 | else |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3196 | #else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3197 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3198 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 3199 | Py_UNICODE ch2; |
| 3200 | Py_UCS4 ucs; |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3201 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3202 | ch2 = *s++; |
| 3203 | size--; |
| 3204 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3205 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3206 | *p++ = '\\'; |
| 3207 | *p++ = 'U'; |
| 3208 | *p++ = hexdigit[(ucs >> 28) & 0xf]; |
| 3209 | *p++ = hexdigit[(ucs >> 24) & 0xf]; |
| 3210 | *p++ = hexdigit[(ucs >> 20) & 0xf]; |
| 3211 | *p++ = hexdigit[(ucs >> 16) & 0xf]; |
| 3212 | *p++ = hexdigit[(ucs >> 12) & 0xf]; |
| 3213 | *p++ = hexdigit[(ucs >> 8) & 0xf]; |
| 3214 | *p++ = hexdigit[(ucs >> 4) & 0xf]; |
| 3215 | *p++ = hexdigit[ucs & 0xf]; |
| 3216 | continue; |
| 3217 | } |
| 3218 | /* Fall through: isolated surrogates are copied as-is */ |
| 3219 | s--; |
| 3220 | size++; |
| 3221 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3222 | #endif |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3223 | /* Map 16-bit characters to '\uxxxx' */ |
| 3224 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3225 | *p++ = '\\'; |
| 3226 | *p++ = 'u'; |
| 3227 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 3228 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 3229 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 3230 | *p++ = hexdigit[ch & 15]; |
| 3231 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3232 | /* Copy everything else as-is */ |
| 3233 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3234 | *p++ = (char) ch; |
| 3235 | } |
| 3236 | *p = '\0'; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3237 | _PyString_Resize(&repr, p - q); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3238 | return repr; |
| 3239 | } |
| 3240 | |
| 3241 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 3242 | { |
| 3243 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3244 | PyErr_BadArgument(); |
| 3245 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3246 | } |
| 3247 | return PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3248 | PyUnicode_GET_SIZE(unicode)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3249 | } |
| 3250 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3251 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 3252 | |
| 3253 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3254 | Py_ssize_t size, |
| 3255 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3256 | { |
| 3257 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3258 | Py_ssize_t startinpos; |
| 3259 | Py_ssize_t endinpos; |
| 3260 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3261 | PyUnicodeObject *v; |
| 3262 | Py_UNICODE *p; |
| 3263 | const char *end; |
| 3264 | const char *reason; |
| 3265 | PyObject *errorHandler = NULL; |
| 3266 | PyObject *exc = NULL; |
| 3267 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 3268 | #ifdef Py_UNICODE_WIDE |
| 3269 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 3270 | #endif |
| 3271 | |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 3272 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3273 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 3274 | if (v == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3275 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3276 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3277 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3278 | p = PyUnicode_AS_UNICODE(v); |
| 3279 | end = s + size; |
| 3280 | |
| 3281 | while (s < end) { |
Neal Norwitz | 1004a53 | 2006-05-15 07:17:23 +0000 | [diff] [blame] | 3282 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3283 | /* We have to sanity check the raw data, otherwise doom looms for |
| 3284 | some malformed UCS-4 data. */ |
| 3285 | if ( |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3286 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3287 | *p > unimax || *p < 0 || |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3288 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3289 | end-s < Py_UNICODE_SIZE |
| 3290 | ) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3291 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3292 | startinpos = s - starts; |
| 3293 | if (end-s < Py_UNICODE_SIZE) { |
| 3294 | endinpos = end-starts; |
| 3295 | reason = "truncated input"; |
| 3296 | } |
| 3297 | else { |
| 3298 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 3299 | reason = "illegal code point (> 0x10FFFF)"; |
| 3300 | } |
| 3301 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 3302 | if (unicode_decode_call_errorhandler( |
| 3303 | errors, &errorHandler, |
| 3304 | "unicode_internal", reason, |
| 3305 | starts, size, &startinpos, &endinpos, &exc, &s, |
Benjamin Peterson | 828a706 | 2008-12-27 17:05:29 +0000 | [diff] [blame] | 3306 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3307 | goto onError; |
| 3308 | } |
| 3309 | } |
| 3310 | else { |
| 3311 | p++; |
| 3312 | s += Py_UNICODE_SIZE; |
| 3313 | } |
| 3314 | } |
| 3315 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3316 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3317 | goto onError; |
| 3318 | Py_XDECREF(errorHandler); |
| 3319 | Py_XDECREF(exc); |
| 3320 | return (PyObject *)v; |
| 3321 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3322 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3323 | Py_XDECREF(v); |
| 3324 | Py_XDECREF(errorHandler); |
| 3325 | Py_XDECREF(exc); |
| 3326 | return NULL; |
| 3327 | } |
| 3328 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3329 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 3330 | |
| 3331 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3332 | Py_ssize_t size, |
| 3333 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3334 | { |
| 3335 | PyUnicodeObject *v; |
| 3336 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3337 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3338 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3339 | if (size == 1) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3340 | Py_UNICODE r = *(unsigned char*)s; |
| 3341 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3342 | } |
| 3343 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3344 | v = _PyUnicode_New(size); |
| 3345 | if (v == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3346 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3347 | if (size == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3348 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3349 | p = PyUnicode_AS_UNICODE(v); |
| 3350 | while (size-- > 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3351 | *p++ = (unsigned char)*s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3352 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3353 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3354 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3355 | Py_XDECREF(v); |
| 3356 | return NULL; |
| 3357 | } |
| 3358 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3359 | /* create or adjust a UnicodeEncodeError */ |
| 3360 | static void make_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3361 | const char *encoding, |
| 3362 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3363 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3364 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3365 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3366 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3367 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 3368 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3369 | } |
| 3370 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3371 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 3372 | goto onError; |
| 3373 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 3374 | goto onError; |
| 3375 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 3376 | goto onError; |
| 3377 | return; |
| 3378 | onError: |
| 3379 | Py_DECREF(*exceptionObject); |
| 3380 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3381 | } |
| 3382 | } |
| 3383 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3384 | /* raises a UnicodeEncodeError */ |
| 3385 | static void raise_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3386 | const char *encoding, |
| 3387 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3388 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3389 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3390 | { |
| 3391 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3392 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3393 | if (*exceptionObject != NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3394 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3395 | } |
| 3396 | |
| 3397 | /* error handling callback helper: |
| 3398 | build arguments, call the callback and check the arguments, |
| 3399 | put the result into newpos and return the replacement string, which |
| 3400 | has to be freed by the caller */ |
| 3401 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3402 | PyObject **errorHandler, |
| 3403 | const char *encoding, const char *reason, |
| 3404 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 3405 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3406 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3407 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3408 | 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] | 3409 | |
| 3410 | PyObject *restuple; |
| 3411 | PyObject *resunicode; |
| 3412 | |
| 3413 | if (*errorHandler == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3414 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3415 | if (*errorHandler == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3416 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3417 | } |
| 3418 | |
| 3419 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3420 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3421 | if (*exceptionObject == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3422 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3423 | |
| 3424 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3425 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3426 | if (restuple == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3427 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3428 | if (!PyTuple_Check(restuple)) { |
Georg Brandl | 40e15ed | 2009-04-05 21:48:06 +0000 | [diff] [blame] | 3429 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3430 | Py_DECREF(restuple); |
| 3431 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3432 | } |
| 3433 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3434 | &resunicode, newpos)) { |
| 3435 | Py_DECREF(restuple); |
| 3436 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3437 | } |
| 3438 | if (*newpos<0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3439 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3440 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3441 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 3442 | Py_DECREF(restuple); |
| 3443 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3444 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3445 | Py_INCREF(resunicode); |
| 3446 | Py_DECREF(restuple); |
| 3447 | return resunicode; |
| 3448 | } |
| 3449 | |
| 3450 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3451 | Py_ssize_t size, |
| 3452 | const char *errors, |
| 3453 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3454 | { |
| 3455 | /* output object */ |
| 3456 | PyObject *res; |
| 3457 | /* pointers to the beginning and end+1 of input */ |
| 3458 | const Py_UNICODE *startp = p; |
| 3459 | const Py_UNICODE *endp = p + size; |
| 3460 | /* pointer to the beginning of the unencodable characters */ |
| 3461 | /* const Py_UNICODE *badp = NULL; */ |
| 3462 | /* pointer into the output */ |
| 3463 | char *str; |
| 3464 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3465 | Py_ssize_t respos = 0; |
| 3466 | Py_ssize_t ressize; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3467 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 3468 | 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] | 3469 | PyObject *errorHandler = NULL; |
| 3470 | PyObject *exc = NULL; |
| 3471 | /* the following variable is used for caching string comparisons |
| 3472 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 3473 | int known_errorHandler = -1; |
| 3474 | |
| 3475 | /* allocate enough for a simple encoding without |
| 3476 | replacements, if we need more, we'll resize */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3477 | res = PyString_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3478 | if (res == NULL) |
| 3479 | goto onError; |
| 3480 | if (size == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3481 | return res; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3482 | str = PyString_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3483 | ressize = size; |
| 3484 | |
| 3485 | while (p<endp) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3486 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3487 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3488 | /* can we encode this? */ |
| 3489 | if (c<limit) { |
| 3490 | /* no overflow check, because we know that the space is enough */ |
| 3491 | *str++ = (char)c; |
| 3492 | ++p; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 3493 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3494 | else { |
| 3495 | Py_ssize_t unicodepos = p-startp; |
| 3496 | Py_ssize_t requiredsize; |
| 3497 | PyObject *repunicode; |
| 3498 | Py_ssize_t repsize; |
| 3499 | Py_ssize_t newpos; |
| 3500 | Py_ssize_t respos; |
| 3501 | Py_UNICODE *uni2; |
| 3502 | /* startpos for collecting unencodable chars */ |
| 3503 | const Py_UNICODE *collstart = p; |
| 3504 | const Py_UNICODE *collend = p; |
| 3505 | /* find all unecodable characters */ |
| 3506 | while ((collend < endp) && ((*collend)>=limit)) |
| 3507 | ++collend; |
| 3508 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 3509 | if (known_errorHandler==-1) { |
| 3510 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3511 | known_errorHandler = 1; |
| 3512 | else if (!strcmp(errors, "replace")) |
| 3513 | known_errorHandler = 2; |
| 3514 | else if (!strcmp(errors, "ignore")) |
| 3515 | known_errorHandler = 3; |
| 3516 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3517 | known_errorHandler = 4; |
| 3518 | else |
| 3519 | known_errorHandler = 0; |
| 3520 | } |
| 3521 | switch (known_errorHandler) { |
| 3522 | case 1: /* strict */ |
| 3523 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 3524 | goto onError; |
| 3525 | case 2: /* replace */ |
| 3526 | while (collstart++<collend) |
| 3527 | *str++ = '?'; /* fall through */ |
| 3528 | case 3: /* ignore */ |
| 3529 | p = collend; |
| 3530 | break; |
| 3531 | case 4: /* xmlcharrefreplace */ |
| 3532 | respos = str-PyString_AS_STRING(res); |
| 3533 | /* determine replacement size (temporarily (mis)uses p) */ |
| 3534 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 3535 | if (*p<10) |
| 3536 | repsize += 2+1+1; |
| 3537 | else if (*p<100) |
| 3538 | repsize += 2+2+1; |
| 3539 | else if (*p<1000) |
| 3540 | repsize += 2+3+1; |
| 3541 | else if (*p<10000) |
| 3542 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 3543 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3544 | else |
| 3545 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 3546 | #else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3547 | else if (*p<100000) |
| 3548 | repsize += 2+5+1; |
| 3549 | else if (*p<1000000) |
| 3550 | repsize += 2+6+1; |
| 3551 | else |
| 3552 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3553 | #endif |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3554 | } |
| 3555 | requiredsize = respos+repsize+(endp-collend); |
| 3556 | if (requiredsize > ressize) { |
| 3557 | if (requiredsize<2*ressize) |
| 3558 | requiredsize = 2*ressize; |
| 3559 | if (_PyString_Resize(&res, requiredsize)) |
| 3560 | goto onError; |
| 3561 | str = PyString_AS_STRING(res) + respos; |
| 3562 | ressize = requiredsize; |
| 3563 | } |
| 3564 | /* generate replacement (temporarily (mis)uses p) */ |
| 3565 | for (p = collstart; p < collend; ++p) { |
| 3566 | str += sprintf(str, "&#%d;", (int)*p); |
| 3567 | } |
| 3568 | p = collend; |
| 3569 | break; |
| 3570 | default: |
| 3571 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 3572 | encoding, reason, startp, size, &exc, |
| 3573 | collstart-startp, collend-startp, &newpos); |
| 3574 | if (repunicode == NULL) |
| 3575 | goto onError; |
| 3576 | /* need more space? (at least enough for what we |
| 3577 | have+the replacement+the rest of the string, so |
| 3578 | we won't have to check space for encodable characters) */ |
| 3579 | respos = str-PyString_AS_STRING(res); |
| 3580 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3581 | requiredsize = respos+repsize+(endp-collend); |
| 3582 | if (requiredsize > ressize) { |
| 3583 | if (requiredsize<2*ressize) |
| 3584 | requiredsize = 2*ressize; |
| 3585 | if (_PyString_Resize(&res, requiredsize)) { |
| 3586 | Py_DECREF(repunicode); |
| 3587 | goto onError; |
| 3588 | } |
| 3589 | str = PyString_AS_STRING(res) + respos; |
| 3590 | ressize = requiredsize; |
| 3591 | } |
| 3592 | /* check if there is anything unencodable in the replacement |
| 3593 | and copy it to the output */ |
| 3594 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 3595 | c = *uni2; |
| 3596 | if (c >= limit) { |
| 3597 | raise_encode_exception(&exc, encoding, startp, size, |
| 3598 | unicodepos, unicodepos+1, reason); |
| 3599 | Py_DECREF(repunicode); |
| 3600 | goto onError; |
| 3601 | } |
| 3602 | *str = (char)c; |
| 3603 | } |
| 3604 | p = startp + newpos; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 3605 | Py_DECREF(repunicode); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 3606 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 3607 | } |
| 3608 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3609 | /* Resize if we allocated to much */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3610 | respos = str-PyString_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3611 | if (respos<ressize) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3612 | /* If this falls res will be NULL */ |
| 3613 | _PyString_Resize(&res, respos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3614 | Py_XDECREF(errorHandler); |
| 3615 | Py_XDECREF(exc); |
| 3616 | return res; |
| 3617 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3618 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3619 | Py_XDECREF(res); |
| 3620 | Py_XDECREF(errorHandler); |
| 3621 | Py_XDECREF(exc); |
| 3622 | return NULL; |
| 3623 | } |
| 3624 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3625 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3626 | Py_ssize_t size, |
| 3627 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3628 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3629 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3630 | } |
| 3631 | |
| 3632 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 3633 | { |
| 3634 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3635 | PyErr_BadArgument(); |
| 3636 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3637 | } |
| 3638 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3639 | PyUnicode_GET_SIZE(unicode), |
| 3640 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3641 | } |
| 3642 | |
| 3643 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 3644 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3645 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3646 | Py_ssize_t size, |
| 3647 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3648 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3649 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3650 | PyUnicodeObject *v; |
| 3651 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3652 | Py_ssize_t startinpos; |
| 3653 | Py_ssize_t endinpos; |
| 3654 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3655 | const char *e; |
| 3656 | PyObject *errorHandler = NULL; |
| 3657 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3658 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3659 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3660 | if (size == 1 && *(unsigned char*)s < 128) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3661 | Py_UNICODE r = *(unsigned char*)s; |
| 3662 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3663 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3664 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3665 | v = _PyUnicode_New(size); |
| 3666 | if (v == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3667 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3668 | if (size == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3669 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3670 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3671 | e = s + size; |
| 3672 | while (s < e) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3673 | register unsigned char c = (unsigned char)*s; |
| 3674 | if (c < 128) { |
| 3675 | *p++ = c; |
| 3676 | ++s; |
| 3677 | } |
| 3678 | else { |
| 3679 | startinpos = s-starts; |
| 3680 | endinpos = startinpos + 1; |
| 3681 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
| 3682 | if (unicode_decode_call_errorhandler( |
| 3683 | errors, &errorHandler, |
| 3684 | "ascii", "ordinal not in range(128)", |
| 3685 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3686 | &v, &outpos, &p)) |
| 3687 | goto onError; |
| 3688 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3689 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3690 | if (p - PyUnicode_AS_UNICODE(v) < PyString_GET_SIZE(v)) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3691 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 3692 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3693 | Py_XDECREF(errorHandler); |
| 3694 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3695 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3696 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3697 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3698 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3699 | Py_XDECREF(errorHandler); |
| 3700 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3701 | return NULL; |
| 3702 | } |
| 3703 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3704 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3705 | Py_ssize_t size, |
| 3706 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3707 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3708 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3709 | } |
| 3710 | |
| 3711 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 3712 | { |
| 3713 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3714 | PyErr_BadArgument(); |
| 3715 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3716 | } |
| 3717 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3718 | PyUnicode_GET_SIZE(unicode), |
| 3719 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3720 | } |
| 3721 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3722 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3723 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3724 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3725 | |
Hirokazu Yamamoto | 68e075e | 2009-03-21 13:04:41 +0000 | [diff] [blame] | 3726 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3727 | #define NEED_RETRY |
| 3728 | #endif |
| 3729 | |
| 3730 | /* XXX This code is limited to "true" double-byte encodings, as |
| 3731 | a) it assumes an incomplete character consists of a single byte, and |
| 3732 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3733 | encodings, see IsDBCSLeadByteEx documentation. */ |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3734 | |
| 3735 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 3736 | { |
| 3737 | const char *curr = s + offset; |
| 3738 | |
| 3739 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3740 | const char *prev = CharPrev(s, curr); |
| 3741 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3742 | } |
| 3743 | return 0; |
| 3744 | } |
| 3745 | |
| 3746 | /* |
| 3747 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 3748 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 3749 | */ |
| 3750 | static int decode_mbcs(PyUnicodeObject **v, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3751 | const char *s, /* MBCS string */ |
| 3752 | int size, /* sizeof MBCS string */ |
| 3753 | int final) |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3754 | { |
| 3755 | Py_UNICODE *p; |
| 3756 | Py_ssize_t n = 0; |
| 3757 | int usize = 0; |
| 3758 | |
| 3759 | assert(size >= 0); |
| 3760 | |
| 3761 | /* Skip trailing lead-byte unless 'final' is set */ |
| 3762 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3763 | --size; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3764 | |
| 3765 | /* First get the size of the result */ |
| 3766 | if (size > 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3767 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 3768 | if (usize == 0) { |
| 3769 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3770 | return -1; |
| 3771 | } |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3772 | } |
| 3773 | |
| 3774 | if (*v == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3775 | /* Create unicode object */ |
| 3776 | *v = _PyUnicode_New(usize); |
| 3777 | if (*v == NULL) |
| 3778 | return -1; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3779 | } |
| 3780 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3781 | /* Extend unicode object */ |
| 3782 | n = PyUnicode_GET_SIZE(*v); |
| 3783 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 3784 | return -1; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3785 | } |
| 3786 | |
| 3787 | /* Do the conversion */ |
| 3788 | if (size > 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3789 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 3790 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 3791 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3792 | return -1; |
| 3793 | } |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3794 | } |
| 3795 | |
| 3796 | return size; |
| 3797 | } |
| 3798 | |
| 3799 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3800 | Py_ssize_t size, |
| 3801 | const char *errors, |
| 3802 | Py_ssize_t *consumed) |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3803 | { |
| 3804 | PyUnicodeObject *v = NULL; |
| 3805 | int done; |
| 3806 | |
| 3807 | if (consumed) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3808 | *consumed = 0; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3809 | |
| 3810 | #ifdef NEED_RETRY |
| 3811 | retry: |
| 3812 | if (size > INT_MAX) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3813 | done = decode_mbcs(&v, s, INT_MAX, 0); |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3814 | else |
| 3815 | #endif |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3816 | done = decode_mbcs(&v, s, (int)size, !consumed); |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3817 | |
| 3818 | if (done < 0) { |
| 3819 | Py_XDECREF(v); |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3820 | return NULL; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3821 | } |
| 3822 | |
| 3823 | if (consumed) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3824 | *consumed += done; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3825 | |
| 3826 | #ifdef NEED_RETRY |
| 3827 | if (size > INT_MAX) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3828 | s += done; |
| 3829 | size -= done; |
| 3830 | goto retry; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3831 | } |
| 3832 | #endif |
| 3833 | |
| 3834 | return (PyObject *)v; |
| 3835 | } |
| 3836 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3837 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3838 | Py_ssize_t size, |
| 3839 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3840 | { |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3841 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 3842 | } |
| 3843 | |
| 3844 | /* |
| 3845 | * Convert unicode into string object (MBCS). |
| 3846 | * Returns 0 if succeed, -1 otherwise. |
| 3847 | */ |
| 3848 | static int encode_mbcs(PyObject **repr, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3849 | const Py_UNICODE *p, /* unicode */ |
| 3850 | int size) /* size of unicode */ |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3851 | { |
| 3852 | int mbcssize = 0; |
| 3853 | Py_ssize_t n = 0; |
| 3854 | |
| 3855 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3856 | |
| 3857 | /* First get the size of the result */ |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3858 | if (size > 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3859 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 3860 | if (mbcssize == 0) { |
| 3861 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3862 | return -1; |
| 3863 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3864 | } |
| 3865 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3866 | if (*repr == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3867 | /* Create string object */ |
| 3868 | *repr = PyString_FromStringAndSize(NULL, mbcssize); |
| 3869 | if (*repr == NULL) |
| 3870 | return -1; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3871 | } |
| 3872 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3873 | /* Extend string object */ |
| 3874 | n = PyString_Size(*repr); |
| 3875 | if (_PyString_Resize(repr, n + mbcssize) < 0) |
| 3876 | return -1; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3877 | } |
| 3878 | |
| 3879 | /* Do the conversion */ |
| 3880 | if (size > 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3881 | char *s = PyString_AS_STRING(*repr) + n; |
| 3882 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 3883 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3884 | return -1; |
| 3885 | } |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3886 | } |
| 3887 | |
| 3888 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3889 | } |
| 3890 | |
| 3891 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3892 | Py_ssize_t size, |
| 3893 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3894 | { |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3895 | PyObject *repr = NULL; |
| 3896 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 3897 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3898 | #ifdef NEED_RETRY |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3899 | retry: |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3900 | if (size > INT_MAX) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3901 | ret = encode_mbcs(&repr, p, INT_MAX); |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3902 | else |
| 3903 | #endif |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3904 | ret = encode_mbcs(&repr, p, (int)size); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3905 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3906 | if (ret < 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3907 | Py_XDECREF(repr); |
| 3908 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3909 | } |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3910 | |
| 3911 | #ifdef NEED_RETRY |
| 3912 | if (size > INT_MAX) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3913 | p += INT_MAX; |
| 3914 | size -= INT_MAX; |
| 3915 | goto retry; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3916 | } |
| 3917 | #endif |
| 3918 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3919 | return repr; |
| 3920 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3921 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3922 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 3923 | { |
| 3924 | if (!PyUnicode_Check(unicode)) { |
| 3925 | PyErr_BadArgument(); |
| 3926 | return NULL; |
| 3927 | } |
| 3928 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3929 | PyUnicode_GET_SIZE(unicode), |
| 3930 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3931 | } |
| 3932 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3933 | #undef NEED_RETRY |
| 3934 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3935 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3936 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3937 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 3938 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3939 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3940 | Py_ssize_t size, |
| 3941 | PyObject *mapping, |
| 3942 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3943 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3944 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3945 | Py_ssize_t startinpos; |
| 3946 | Py_ssize_t endinpos; |
| 3947 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3948 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3949 | PyUnicodeObject *v; |
| 3950 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3951 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3952 | PyObject *errorHandler = NULL; |
| 3953 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3954 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3955 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3956 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3957 | /* Default to Latin-1 */ |
| 3958 | if (mapping == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3959 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3960 | |
| 3961 | v = _PyUnicode_New(size); |
| 3962 | if (v == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3963 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3964 | if (size == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3965 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3966 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3967 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3968 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3969 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 3970 | maplen = PyUnicode_GET_SIZE(mapping); |
| 3971 | while (s < e) { |
| 3972 | unsigned char ch = *s; |
| 3973 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3974 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3975 | if (ch < maplen) |
| 3976 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3977 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3978 | if (x == 0xfffe) { |
| 3979 | /* undefined mapping */ |
| 3980 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3981 | startinpos = s-starts; |
| 3982 | endinpos = startinpos+1; |
| 3983 | if (unicode_decode_call_errorhandler( |
| 3984 | errors, &errorHandler, |
| 3985 | "charmap", "character maps to <undefined>", |
| 3986 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3987 | &v, &outpos, &p)) { |
| 3988 | goto onError; |
| 3989 | } |
| 3990 | continue; |
| 3991 | } |
| 3992 | *p++ = x; |
| 3993 | ++s; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 3994 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3995 | } |
| 3996 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 3997 | while (s < e) { |
| 3998 | unsigned char ch = *s; |
| 3999 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4000 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4001 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 4002 | w = PyInt_FromLong((long)ch); |
| 4003 | if (w == NULL) |
| 4004 | goto onError; |
| 4005 | x = PyObject_GetItem(mapping, w); |
| 4006 | Py_DECREF(w); |
| 4007 | if (x == NULL) { |
| 4008 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4009 | /* No mapping found means: mapping is undefined. */ |
| 4010 | PyErr_Clear(); |
| 4011 | x = Py_None; |
| 4012 | Py_INCREF(x); |
| 4013 | } else |
| 4014 | goto onError; |
| 4015 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4016 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4017 | /* Apply mapping */ |
| 4018 | if (PyInt_Check(x)) { |
| 4019 | long value = PyInt_AS_LONG(x); |
| 4020 | if (value < 0 || value > 65535) { |
| 4021 | PyErr_SetString(PyExc_TypeError, |
| 4022 | "character mapping must be in range(65536)"); |
| 4023 | Py_DECREF(x); |
| 4024 | goto onError; |
| 4025 | } |
| 4026 | *p++ = (Py_UNICODE)value; |
| 4027 | } |
| 4028 | else if (x == Py_None) { |
| 4029 | /* undefined mapping */ |
| 4030 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4031 | startinpos = s-starts; |
| 4032 | endinpos = startinpos+1; |
| 4033 | if (unicode_decode_call_errorhandler( |
| 4034 | errors, &errorHandler, |
| 4035 | "charmap", "character maps to <undefined>", |
| 4036 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 4037 | &v, &outpos, &p)) { |
| 4038 | Py_DECREF(x); |
| 4039 | goto onError; |
| 4040 | } |
| 4041 | Py_DECREF(x); |
| 4042 | continue; |
| 4043 | } |
| 4044 | else if (PyUnicode_Check(x)) { |
| 4045 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4046 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4047 | if (targetsize == 1) |
| 4048 | /* 1-1 mapping */ |
| 4049 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4050 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4051 | else if (targetsize > 1) { |
| 4052 | /* 1-n mapping */ |
| 4053 | if (targetsize > extrachars) { |
| 4054 | /* resize first */ |
| 4055 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 4056 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 4057 | (targetsize << 2); |
| 4058 | extrachars += needed; |
| 4059 | /* XXX overflow detection missing */ |
| 4060 | if (_PyUnicode_Resize(&v, |
| 4061 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 4062 | Py_DECREF(x); |
| 4063 | goto onError; |
| 4064 | } |
| 4065 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 4066 | } |
| 4067 | Py_UNICODE_COPY(p, |
| 4068 | PyUnicode_AS_UNICODE(x), |
| 4069 | targetsize); |
| 4070 | p += targetsize; |
| 4071 | extrachars -= targetsize; |
| 4072 | } |
| 4073 | /* 1-0 mapping: skip the character */ |
| 4074 | } |
| 4075 | else { |
| 4076 | /* wrong return value */ |
| 4077 | PyErr_SetString(PyExc_TypeError, |
| 4078 | "character mapping must return integer, None or unicode"); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4079 | Py_DECREF(x); |
| 4080 | goto onError; |
| 4081 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4082 | Py_DECREF(x); |
| 4083 | ++s; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4084 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4085 | } |
| 4086 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4087 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4088 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4089 | Py_XDECREF(errorHandler); |
| 4090 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4091 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4092 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4093 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4094 | Py_XDECREF(errorHandler); |
| 4095 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4096 | Py_XDECREF(v); |
| 4097 | return NULL; |
| 4098 | } |
| 4099 | |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4100 | /* Charmap encoding: the lookup table */ |
| 4101 | |
| 4102 | struct encoding_map{ |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4103 | PyObject_HEAD |
| 4104 | unsigned char level1[32]; |
| 4105 | int count2, count3; |
| 4106 | unsigned char level23[1]; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4107 | }; |
| 4108 | |
| 4109 | static PyObject* |
| 4110 | encoding_map_size(PyObject *obj, PyObject* args) |
| 4111 | { |
| 4112 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4113 | return PyInt_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4114 | 128*map->count3); |
| 4115 | } |
| 4116 | |
| 4117 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4118 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4119 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 4120 | { 0 } |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4121 | }; |
| 4122 | |
| 4123 | static void |
| 4124 | encoding_map_dealloc(PyObject* o) |
| 4125 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4126 | PyObject_FREE(o); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4127 | } |
| 4128 | |
| 4129 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4130 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4131 | "EncodingMap", /*tp_name*/ |
| 4132 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 4133 | 0, /*tp_itemsize*/ |
| 4134 | /* methods */ |
| 4135 | encoding_map_dealloc, /*tp_dealloc*/ |
| 4136 | 0, /*tp_print*/ |
| 4137 | 0, /*tp_getattr*/ |
| 4138 | 0, /*tp_setattr*/ |
| 4139 | 0, /*tp_compare*/ |
| 4140 | 0, /*tp_repr*/ |
| 4141 | 0, /*tp_as_number*/ |
| 4142 | 0, /*tp_as_sequence*/ |
| 4143 | 0, /*tp_as_mapping*/ |
| 4144 | 0, /*tp_hash*/ |
| 4145 | 0, /*tp_call*/ |
| 4146 | 0, /*tp_str*/ |
| 4147 | 0, /*tp_getattro*/ |
| 4148 | 0, /*tp_setattro*/ |
| 4149 | 0, /*tp_as_buffer*/ |
| 4150 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 4151 | 0, /*tp_doc*/ |
| 4152 | 0, /*tp_traverse*/ |
| 4153 | 0, /*tp_clear*/ |
| 4154 | 0, /*tp_richcompare*/ |
| 4155 | 0, /*tp_weaklistoffset*/ |
| 4156 | 0, /*tp_iter*/ |
| 4157 | 0, /*tp_iternext*/ |
| 4158 | encoding_map_methods, /*tp_methods*/ |
| 4159 | 0, /*tp_members*/ |
| 4160 | 0, /*tp_getset*/ |
| 4161 | 0, /*tp_base*/ |
| 4162 | 0, /*tp_dict*/ |
| 4163 | 0, /*tp_descr_get*/ |
| 4164 | 0, /*tp_descr_set*/ |
| 4165 | 0, /*tp_dictoffset*/ |
| 4166 | 0, /*tp_init*/ |
| 4167 | 0, /*tp_alloc*/ |
| 4168 | 0, /*tp_new*/ |
| 4169 | 0, /*tp_free*/ |
| 4170 | 0, /*tp_is_gc*/ |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4171 | }; |
| 4172 | |
| 4173 | PyObject* |
| 4174 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 4175 | { |
| 4176 | Py_UNICODE *decode; |
| 4177 | PyObject *result; |
| 4178 | struct encoding_map *mresult; |
| 4179 | int i; |
| 4180 | int need_dict = 0; |
| 4181 | unsigned char level1[32]; |
| 4182 | unsigned char level2[512]; |
| 4183 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 4184 | int count2 = 0, count3 = 0; |
| 4185 | |
| 4186 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 4187 | PyErr_BadArgument(); |
| 4188 | return NULL; |
| 4189 | } |
| 4190 | decode = PyUnicode_AS_UNICODE(string); |
| 4191 | memset(level1, 0xFF, sizeof level1); |
| 4192 | memset(level2, 0xFF, sizeof level2); |
| 4193 | |
| 4194 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 4195 | or if there are non-BMP characters, we need to use |
| 4196 | a mapping dictionary. */ |
| 4197 | if (decode[0] != 0) |
| 4198 | need_dict = 1; |
| 4199 | for (i = 1; i < 256; i++) { |
| 4200 | int l1, l2; |
| 4201 | if (decode[i] == 0 |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4202 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4203 | || decode[i] > 0xFFFF |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4204 | #endif |
| 4205 | ) { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4206 | need_dict = 1; |
| 4207 | break; |
| 4208 | } |
| 4209 | if (decode[i] == 0xFFFE) |
| 4210 | /* unmapped character */ |
| 4211 | continue; |
| 4212 | l1 = decode[i] >> 11; |
| 4213 | l2 = decode[i] >> 7; |
| 4214 | if (level1[l1] == 0xFF) |
| 4215 | level1[l1] = count2++; |
| 4216 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4217 | level2[l2] = count3++; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4218 | } |
| 4219 | |
| 4220 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 4221 | need_dict = 1; |
| 4222 | |
| 4223 | if (need_dict) { |
| 4224 | PyObject *result = PyDict_New(); |
| 4225 | PyObject *key, *value; |
| 4226 | if (!result) |
| 4227 | return NULL; |
| 4228 | for (i = 0; i < 256; i++) { |
| 4229 | key = value = NULL; |
| 4230 | key = PyInt_FromLong(decode[i]); |
| 4231 | value = PyInt_FromLong(i); |
| 4232 | if (!key || !value) |
| 4233 | goto failed1; |
| 4234 | if (PyDict_SetItem(result, key, value) == -1) |
| 4235 | goto failed1; |
Georg Brandl | 9f16760 | 2006-06-04 21:46:16 +0000 | [diff] [blame] | 4236 | Py_DECREF(key); |
| 4237 | Py_DECREF(value); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4238 | } |
| 4239 | return result; |
| 4240 | failed1: |
| 4241 | Py_XDECREF(key); |
| 4242 | Py_XDECREF(value); |
| 4243 | Py_DECREF(result); |
| 4244 | return NULL; |
| 4245 | } |
| 4246 | |
| 4247 | /* Create a three-level trie */ |
| 4248 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 4249 | 16*count2 + 128*count3 - 1); |
| 4250 | if (!result) |
| 4251 | return PyErr_NoMemory(); |
| 4252 | PyObject_Init(result, &EncodingMapType); |
| 4253 | mresult = (struct encoding_map*)result; |
| 4254 | mresult->count2 = count2; |
| 4255 | mresult->count3 = count3; |
| 4256 | mlevel1 = mresult->level1; |
| 4257 | mlevel2 = mresult->level23; |
| 4258 | mlevel3 = mresult->level23 + 16*count2; |
| 4259 | memcpy(mlevel1, level1, 32); |
| 4260 | memset(mlevel2, 0xFF, 16*count2); |
| 4261 | memset(mlevel3, 0, 128*count3); |
| 4262 | count3 = 0; |
| 4263 | for (i = 1; i < 256; i++) { |
| 4264 | int o1, o2, o3, i2, i3; |
| 4265 | if (decode[i] == 0xFFFE) |
| 4266 | /* unmapped character */ |
| 4267 | continue; |
| 4268 | o1 = decode[i]>>11; |
| 4269 | o2 = (decode[i]>>7) & 0xF; |
| 4270 | i2 = 16*mlevel1[o1] + o2; |
| 4271 | if (mlevel2[i2] == 0xFF) |
| 4272 | mlevel2[i2] = count3++; |
| 4273 | o3 = decode[i] & 0x7F; |
| 4274 | i3 = 128*mlevel2[i2] + o3; |
| 4275 | mlevel3[i3] = i; |
| 4276 | } |
| 4277 | return result; |
| 4278 | } |
| 4279 | |
| 4280 | static int |
| 4281 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 4282 | { |
| 4283 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 4284 | int l1 = c>>11; |
| 4285 | int l2 = (c>>7) & 0xF; |
| 4286 | int l3 = c & 0x7F; |
| 4287 | int i; |
| 4288 | |
| 4289 | #ifdef Py_UNICODE_WIDE |
| 4290 | if (c > 0xFFFF) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4291 | return -1; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4292 | } |
| 4293 | #endif |
| 4294 | if (c == 0) |
| 4295 | return 0; |
| 4296 | /* level 1*/ |
| 4297 | i = map->level1[l1]; |
| 4298 | if (i == 0xFF) { |
| 4299 | return -1; |
| 4300 | } |
| 4301 | /* level 2*/ |
| 4302 | i = map->level23[16*i+l2]; |
| 4303 | if (i == 0xFF) { |
| 4304 | return -1; |
| 4305 | } |
| 4306 | /* level 3 */ |
| 4307 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 4308 | if (i == 0) { |
| 4309 | return -1; |
| 4310 | } |
| 4311 | return i; |
| 4312 | } |
| 4313 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4314 | /* Lookup the character ch in the mapping. If the character |
| 4315 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4316 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4317 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4318 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4319 | PyObject *w = PyInt_FromLong((long)c); |
| 4320 | PyObject *x; |
| 4321 | |
| 4322 | if (w == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4323 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4324 | x = PyObject_GetItem(mapping, w); |
| 4325 | Py_DECREF(w); |
| 4326 | if (x == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4327 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4328 | /* No mapping found means: mapping is undefined. */ |
| 4329 | PyErr_Clear(); |
| 4330 | x = Py_None; |
| 4331 | Py_INCREF(x); |
| 4332 | return x; |
| 4333 | } else |
| 4334 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4335 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 4336 | else if (x == Py_None) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4337 | return x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4338 | else if (PyInt_Check(x)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4339 | long value = PyInt_AS_LONG(x); |
| 4340 | if (value < 0 || value > 255) { |
| 4341 | PyErr_SetString(PyExc_TypeError, |
| 4342 | "character mapping must be in range(256)"); |
| 4343 | Py_DECREF(x); |
| 4344 | return NULL; |
| 4345 | } |
| 4346 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4347 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4348 | else if (PyString_Check(x)) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4349 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4350 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4351 | /* wrong return value */ |
| 4352 | PyErr_SetString(PyExc_TypeError, |
| 4353 | "character mapping must return integer, None or str"); |
| 4354 | Py_DECREF(x); |
| 4355 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4356 | } |
| 4357 | } |
| 4358 | |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4359 | static int |
| 4360 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
| 4361 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4362 | Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
| 4363 | /* exponentially overallocate to minimize reallocations */ |
| 4364 | if (requiredsize < 2*outsize) |
| 4365 | requiredsize = 2*outsize; |
| 4366 | if (_PyString_Resize(outobj, requiredsize)) { |
| 4367 | return 0; |
| 4368 | } |
| 4369 | return 1; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4370 | } |
| 4371 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4372 | typedef enum charmapencode_result { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4373 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4374 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4375 | /* lookup the character, put the result in the output string and adjust |
| 4376 | various state variables. Reallocate the output string if not enough |
| 4377 | space is available. Return a new reference to the object that |
| 4378 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 4379 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 4380 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4381 | static |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4382 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4383 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4384 | { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4385 | PyObject *rep; |
| 4386 | char *outstart; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4387 | Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4388 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4389 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4390 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4391 | Py_ssize_t requiredsize = *outpos+1; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4392 | if (res == -1) |
| 4393 | return enc_FAILED; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4394 | if (outsize<requiredsize) |
| 4395 | if (!charmapencode_resize(outobj, outpos, requiredsize)) |
| 4396 | return enc_EXCEPTION; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4397 | outstart = PyString_AS_STRING(*outobj); |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4398 | outstart[(*outpos)++] = (char)res; |
| 4399 | return enc_SUCCESS; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4400 | } |
| 4401 | |
| 4402 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4403 | if (rep==NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4404 | return enc_EXCEPTION; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4405 | else if (rep==Py_None) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4406 | Py_DECREF(rep); |
| 4407 | return enc_FAILED; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4408 | } else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4409 | if (PyInt_Check(rep)) { |
| 4410 | Py_ssize_t requiredsize = *outpos+1; |
| 4411 | if (outsize<requiredsize) |
| 4412 | if (!charmapencode_resize(outobj, outpos, requiredsize)) { |
| 4413 | Py_DECREF(rep); |
| 4414 | return enc_EXCEPTION; |
| 4415 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4416 | outstart = PyString_AS_STRING(*outobj); |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4417 | outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4418 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4419 | else { |
| 4420 | const char *repchars = PyString_AS_STRING(rep); |
| 4421 | Py_ssize_t repsize = PyString_GET_SIZE(rep); |
| 4422 | Py_ssize_t requiredsize = *outpos+repsize; |
| 4423 | if (outsize<requiredsize) |
| 4424 | if (!charmapencode_resize(outobj, outpos, requiredsize)) { |
| 4425 | Py_DECREF(rep); |
| 4426 | return enc_EXCEPTION; |
| 4427 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4428 | outstart = PyString_AS_STRING(*outobj); |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4429 | memcpy(outstart + *outpos, repchars, repsize); |
| 4430 | *outpos += repsize; |
| 4431 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4432 | } |
Georg Brandl | 9f16760 | 2006-06-04 21:46:16 +0000 | [diff] [blame] | 4433 | Py_DECREF(rep); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4434 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4435 | } |
| 4436 | |
| 4437 | /* handle an error in PyUnicode_EncodeCharmap |
| 4438 | Return 0 on success, -1 on error */ |
| 4439 | static |
| 4440 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4441 | 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] | 4442 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4443 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4444 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4445 | { |
| 4446 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4447 | Py_ssize_t repsize; |
| 4448 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4449 | Py_UNICODE *uni2; |
| 4450 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4451 | Py_ssize_t collstartpos = *inpos; |
| 4452 | Py_ssize_t collendpos = *inpos+1; |
| 4453 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4454 | char *encoding = "charmap"; |
| 4455 | char *reason = "character maps to <undefined>"; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4456 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4457 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4458 | /* find all unencodable characters */ |
| 4459 | while (collendpos < size) { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4460 | PyObject *rep; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4461 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4462 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 4463 | if (res != -1) |
| 4464 | break; |
| 4465 | ++collendpos; |
| 4466 | continue; |
| 4467 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4468 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4469 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 4470 | if (rep==NULL) |
| 4471 | return -1; |
| 4472 | else if (rep!=Py_None) { |
| 4473 | Py_DECREF(rep); |
| 4474 | break; |
| 4475 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4476 | Py_DECREF(rep); |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4477 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4478 | } |
| 4479 | /* cache callback name lookup |
| 4480 | * (if not done yet, i.e. it's the first error) */ |
| 4481 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4482 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4483 | *known_errorHandler = 1; |
| 4484 | else if (!strcmp(errors, "replace")) |
| 4485 | *known_errorHandler = 2; |
| 4486 | else if (!strcmp(errors, "ignore")) |
| 4487 | *known_errorHandler = 3; |
| 4488 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4489 | *known_errorHandler = 4; |
| 4490 | else |
| 4491 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4492 | } |
| 4493 | switch (*known_errorHandler) { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4494 | case 1: /* strict */ |
| 4495 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4496 | return -1; |
| 4497 | case 2: /* replace */ |
| 4498 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4499 | x = charmapencode_output('?', mapping, res, respos); |
| 4500 | if (x==enc_EXCEPTION) { |
| 4501 | return -1; |
| 4502 | } |
| 4503 | else if (x==enc_FAILED) { |
| 4504 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4505 | return -1; |
| 4506 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4507 | } |
| 4508 | /* fall through */ |
| 4509 | case 3: /* ignore */ |
| 4510 | *inpos = collendpos; |
| 4511 | break; |
| 4512 | case 4: /* xmlcharrefreplace */ |
| 4513 | /* generate replacement (temporarily (mis)uses p) */ |
| 4514 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4515 | char buffer[2+29+1+1]; |
| 4516 | char *cp; |
| 4517 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 4518 | for (cp = buffer; *cp; ++cp) { |
| 4519 | x = charmapencode_output(*cp, mapping, res, respos); |
| 4520 | if (x==enc_EXCEPTION) |
| 4521 | return -1; |
| 4522 | else if (x==enc_FAILED) { |
| 4523 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4524 | return -1; |
| 4525 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4526 | } |
| 4527 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4528 | *inpos = collendpos; |
| 4529 | break; |
| 4530 | default: |
| 4531 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4532 | encoding, reason, p, size, exceptionObject, |
| 4533 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4534 | if (repunicode == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4535 | return -1; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4536 | /* generate replacement */ |
| 4537 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4538 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4539 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 4540 | if (x==enc_EXCEPTION) { |
| 4541 | return -1; |
| 4542 | } |
| 4543 | else if (x==enc_FAILED) { |
| 4544 | Py_DECREF(repunicode); |
| 4545 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4546 | return -1; |
| 4547 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4548 | } |
| 4549 | *inpos = newpos; |
| 4550 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4551 | } |
| 4552 | return 0; |
| 4553 | } |
| 4554 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4555 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4556 | Py_ssize_t size, |
| 4557 | PyObject *mapping, |
| 4558 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4559 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4560 | /* output object */ |
| 4561 | PyObject *res = NULL; |
| 4562 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4563 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4564 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4565 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4566 | PyObject *errorHandler = NULL; |
| 4567 | PyObject *exc = NULL; |
| 4568 | /* the following variable is used for caching string comparisons |
| 4569 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4570 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4571 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4572 | |
| 4573 | /* Default to Latin-1 */ |
| 4574 | if (mapping == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4575 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4576 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4577 | /* allocate enough for a simple encoding without |
| 4578 | replacements, if we need more, we'll resize */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4579 | res = PyString_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4580 | if (res == NULL) |
| 4581 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 4582 | if (size == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4583 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4584 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4585 | while (inpos<size) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4586 | /* try to encode it */ |
| 4587 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 4588 | if (x==enc_EXCEPTION) /* error */ |
| 4589 | goto onError; |
| 4590 | if (x==enc_FAILED) { /* unencodable character */ |
| 4591 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 4592 | &exc, |
| 4593 | &known_errorHandler, &errorHandler, errors, |
| 4594 | &res, &respos)) { |
| 4595 | goto onError; |
| 4596 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4597 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4598 | else |
| 4599 | /* done with this character => adjust input position */ |
| 4600 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4601 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4602 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4603 | /* Resize if we allocated to much */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4604 | if (respos<PyString_GET_SIZE(res)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4605 | if (_PyString_Resize(&res, respos)) |
| 4606 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4607 | } |
| 4608 | Py_XDECREF(exc); |
| 4609 | Py_XDECREF(errorHandler); |
| 4610 | return res; |
| 4611 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4612 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4613 | Py_XDECREF(res); |
| 4614 | Py_XDECREF(exc); |
| 4615 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4616 | return NULL; |
| 4617 | } |
| 4618 | |
| 4619 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4620 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4621 | { |
| 4622 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4623 | PyErr_BadArgument(); |
| 4624 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4625 | } |
| 4626 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4627 | PyUnicode_GET_SIZE(unicode), |
| 4628 | mapping, |
| 4629 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4630 | } |
| 4631 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4632 | /* create or adjust a UnicodeTranslateError */ |
| 4633 | static void make_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4634 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4635 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4636 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4637 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4638 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4639 | *exceptionObject = PyUnicodeTranslateError_Create( |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4640 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4641 | } |
| 4642 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4643 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 4644 | goto onError; |
| 4645 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 4646 | goto onError; |
| 4647 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 4648 | goto onError; |
| 4649 | return; |
| 4650 | onError: |
| 4651 | Py_DECREF(*exceptionObject); |
| 4652 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4653 | } |
| 4654 | } |
| 4655 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4656 | /* raises a UnicodeTranslateError */ |
| 4657 | static void raise_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4658 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4659 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4660 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4661 | { |
| 4662 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4663 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4664 | if (*exceptionObject != NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4665 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4666 | } |
| 4667 | |
| 4668 | /* error handling callback helper: |
| 4669 | build arguments, call the callback and check the arguments, |
| 4670 | put the result into newpos and return the replacement string, which |
| 4671 | has to be freed by the caller */ |
| 4672 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4673 | PyObject **errorHandler, |
| 4674 | const char *reason, |
| 4675 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4676 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4677 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4678 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4679 | 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] | 4680 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4681 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4682 | PyObject *restuple; |
| 4683 | PyObject *resunicode; |
| 4684 | |
| 4685 | if (*errorHandler == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4686 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4687 | if (*errorHandler == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4688 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4689 | } |
| 4690 | |
| 4691 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4692 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4693 | if (*exceptionObject == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4694 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4695 | |
| 4696 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4697 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4698 | if (restuple == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4699 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4700 | if (!PyTuple_Check(restuple)) { |
Georg Brandl | 40e15ed | 2009-04-05 21:48:06 +0000 | [diff] [blame] | 4701 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4702 | Py_DECREF(restuple); |
| 4703 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4704 | } |
| 4705 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4706 | &resunicode, &i_newpos)) { |
| 4707 | Py_DECREF(restuple); |
| 4708 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4709 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4710 | if (i_newpos<0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4711 | *newpos = size+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4712 | else |
| 4713 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4714 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4715 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 4716 | Py_DECREF(restuple); |
| 4717 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4718 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4719 | Py_INCREF(resunicode); |
| 4720 | Py_DECREF(restuple); |
| 4721 | return resunicode; |
| 4722 | } |
| 4723 | |
| 4724 | /* Lookup the character ch in the mapping and put the result in result, |
| 4725 | which must be decrefed by the caller. |
| 4726 | Return 0 on success, -1 on error */ |
| 4727 | static |
| 4728 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 4729 | { |
| 4730 | PyObject *w = PyInt_FromLong((long)c); |
| 4731 | PyObject *x; |
| 4732 | |
| 4733 | if (w == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4734 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4735 | x = PyObject_GetItem(mapping, w); |
| 4736 | Py_DECREF(w); |
| 4737 | if (x == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4738 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4739 | /* No mapping found means: use 1:1 mapping. */ |
| 4740 | PyErr_Clear(); |
| 4741 | *result = NULL; |
| 4742 | return 0; |
| 4743 | } else |
| 4744 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4745 | } |
| 4746 | else if (x == Py_None) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4747 | *result = x; |
| 4748 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4749 | } |
| 4750 | else if (PyInt_Check(x)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4751 | long value = PyInt_AS_LONG(x); |
| 4752 | long max = PyUnicode_GetMax(); |
| 4753 | if (value < 0 || value > max) { |
| 4754 | PyErr_Format(PyExc_TypeError, |
| 4755 | "character mapping must be in range(0x%lx)", max+1); |
| 4756 | Py_DECREF(x); |
| 4757 | return -1; |
| 4758 | } |
| 4759 | *result = x; |
| 4760 | return 0; |
| 4761 | } |
| 4762 | else if (PyUnicode_Check(x)) { |
| 4763 | *result = x; |
| 4764 | return 0; |
| 4765 | } |
| 4766 | else { |
| 4767 | /* wrong return value */ |
| 4768 | PyErr_SetString(PyExc_TypeError, |
| 4769 | "character mapping must return integer, None or unicode"); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4770 | Py_DECREF(x); |
| 4771 | return -1; |
| 4772 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4773 | } |
| 4774 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4775 | if not reallocate and adjust various state variables. |
| 4776 | Return 0 on success, -1 on error */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4777 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4778 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4779 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4780 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4781 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4782 | if (requiredsize > oldsize) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4783 | /* remember old output position */ |
| 4784 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
| 4785 | /* exponentially overallocate to minimize reallocations */ |
| 4786 | if (requiredsize < 2 * oldsize) |
| 4787 | requiredsize = 2 * oldsize; |
| 4788 | if (PyUnicode_Resize(outobj, requiredsize) < 0) |
| 4789 | return -1; |
| 4790 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4791 | } |
| 4792 | return 0; |
| 4793 | } |
| 4794 | /* lookup the character, put the result in the output string and adjust |
| 4795 | various state variables. Return a new reference to the object that |
| 4796 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 4797 | undefined (in which case no character was written). |
| 4798 | The called must decref result. |
| 4799 | Return 0 on success, -1 on error. */ |
| 4800 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4801 | int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4802 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
| 4803 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4804 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4805 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4806 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4807 | if (*res==NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4808 | /* not found => default to 1:1 mapping */ |
| 4809 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4810 | } |
| 4811 | else if (*res==Py_None) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4812 | ; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4813 | else if (PyInt_Check(*res)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4814 | /* no overflow check, because we know that the space is enough */ |
| 4815 | *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4816 | } |
| 4817 | else if (PyUnicode_Check(*res)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4818 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
| 4819 | if (repsize==1) { |
| 4820 | /* no overflow check, because we know that the space is enough */ |
| 4821 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 4822 | } |
| 4823 | else if (repsize!=0) { |
| 4824 | /* more than one character */ |
| 4825 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
| 4826 | (insize - (curinp-startinp)) + |
| 4827 | repsize - 1; |
| 4828 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
| 4829 | return -1; |
| 4830 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 4831 | *outp += repsize; |
| 4832 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4833 | } |
| 4834 | else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4835 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4836 | return 0; |
| 4837 | } |
| 4838 | |
| 4839 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4840 | Py_ssize_t size, |
| 4841 | PyObject *mapping, |
| 4842 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4843 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4844 | /* output object */ |
| 4845 | PyObject *res = NULL; |
| 4846 | /* pointers to the beginning and end+1 of input */ |
| 4847 | const Py_UNICODE *startp = p; |
| 4848 | const Py_UNICODE *endp = p + size; |
| 4849 | /* pointer into the output */ |
| 4850 | Py_UNICODE *str; |
| 4851 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4852 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4853 | char *reason = "character maps to <undefined>"; |
| 4854 | PyObject *errorHandler = NULL; |
| 4855 | PyObject *exc = NULL; |
| 4856 | /* the following variable is used for caching string comparisons |
| 4857 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4858 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4859 | int known_errorHandler = -1; |
| 4860 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4861 | if (mapping == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4862 | PyErr_BadArgument(); |
| 4863 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4864 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4865 | |
| 4866 | /* allocate enough for a simple 1:1 translation without |
| 4867 | replacements, if we need more, we'll resize */ |
| 4868 | res = PyUnicode_FromUnicode(NULL, size); |
| 4869 | if (res == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4870 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4871 | if (size == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4872 | return res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4873 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4874 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4875 | while (p<endp) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4876 | /* try to encode it */ |
| 4877 | PyObject *x = NULL; |
| 4878 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
| 4879 | Py_XDECREF(x); |
| 4880 | goto onError; |
| 4881 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4882 | Py_XDECREF(x); |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4883 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 4884 | ++p; |
| 4885 | else { /* untranslatable character */ |
| 4886 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 4887 | Py_ssize_t repsize; |
| 4888 | Py_ssize_t newpos; |
| 4889 | Py_UNICODE *uni2; |
| 4890 | /* startpos for collecting untranslatable chars */ |
| 4891 | const Py_UNICODE *collstart = p; |
| 4892 | const Py_UNICODE *collend = p+1; |
| 4893 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4894 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4895 | /* find all untranslatable characters */ |
| 4896 | while (collend < endp) { |
| 4897 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
| 4898 | goto onError; |
| 4899 | Py_XDECREF(x); |
| 4900 | if (x!=Py_None) |
| 4901 | break; |
| 4902 | ++collend; |
| 4903 | } |
| 4904 | /* cache callback name lookup |
| 4905 | * (if not done yet, i.e. it's the first error) */ |
| 4906 | if (known_errorHandler==-1) { |
| 4907 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4908 | known_errorHandler = 1; |
| 4909 | else if (!strcmp(errors, "replace")) |
| 4910 | known_errorHandler = 2; |
| 4911 | else if (!strcmp(errors, "ignore")) |
| 4912 | known_errorHandler = 3; |
| 4913 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4914 | known_errorHandler = 4; |
| 4915 | else |
| 4916 | known_errorHandler = 0; |
| 4917 | } |
| 4918 | switch (known_errorHandler) { |
| 4919 | case 1: /* strict */ |
| 4920 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4921 | goto onError; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4922 | case 2: /* replace */ |
| 4923 | /* No need to check for space, this is a 1:1 replacement */ |
| 4924 | for (coll = collstart; coll<collend; ++coll) |
| 4925 | *str++ = '?'; |
| 4926 | /* fall through */ |
| 4927 | case 3: /* ignore */ |
| 4928 | p = collend; |
| 4929 | break; |
| 4930 | case 4: /* xmlcharrefreplace */ |
| 4931 | /* generate replacement (temporarily (mis)uses p) */ |
| 4932 | for (p = collstart; p < collend; ++p) { |
| 4933 | char buffer[2+29+1+1]; |
| 4934 | char *cp; |
| 4935 | sprintf(buffer, "&#%d;", (int)*p); |
| 4936 | if (charmaptranslate_makespace(&res, &str, |
| 4937 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 4938 | goto onError; |
| 4939 | for (cp = buffer; *cp; ++cp) |
| 4940 | *str++ = *cp; |
| 4941 | } |
| 4942 | p = collend; |
| 4943 | break; |
| 4944 | default: |
| 4945 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 4946 | reason, startp, size, &exc, |
| 4947 | collstart-startp, collend-startp, &newpos); |
| 4948 | if (repunicode == NULL) |
| 4949 | goto onError; |
| 4950 | /* generate replacement */ |
| 4951 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4952 | if (charmaptranslate_makespace(&res, &str, |
| 4953 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 4954 | Py_DECREF(repunicode); |
| 4955 | goto onError; |
| 4956 | } |
| 4957 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 4958 | *str++ = *uni2; |
| 4959 | p = startp + newpos; |
| 4960 | Py_DECREF(repunicode); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4961 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 4962 | } |
| 4963 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4964 | /* Resize if we allocated to much */ |
| 4965 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4966 | if (respos<PyUnicode_GET_SIZE(res)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4967 | if (PyUnicode_Resize(&res, respos) < 0) |
| 4968 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4969 | } |
| 4970 | Py_XDECREF(exc); |
| 4971 | Py_XDECREF(errorHandler); |
| 4972 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4973 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4974 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4975 | Py_XDECREF(res); |
| 4976 | Py_XDECREF(exc); |
| 4977 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4978 | return NULL; |
| 4979 | } |
| 4980 | |
| 4981 | PyObject *PyUnicode_Translate(PyObject *str, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4982 | PyObject *mapping, |
| 4983 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4984 | { |
| 4985 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4986 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4987 | str = PyUnicode_FromObject(str); |
| 4988 | if (str == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4989 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4990 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4991 | PyUnicode_GET_SIZE(str), |
| 4992 | mapping, |
| 4993 | errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4994 | Py_DECREF(str); |
| 4995 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4996 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 4997 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4998 | Py_XDECREF(str); |
| 4999 | return NULL; |
| 5000 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5001 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5002 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 5003 | |
| 5004 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5005 | Py_ssize_t length, |
| 5006 | char *output, |
| 5007 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5008 | { |
| 5009 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5010 | PyObject *errorHandler = NULL; |
| 5011 | PyObject *exc = NULL; |
| 5012 | const char *encoding = "decimal"; |
| 5013 | const char *reason = "invalid decimal Unicode string"; |
| 5014 | /* the following variable is used for caching string comparisons |
| 5015 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 5016 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5017 | |
| 5018 | if (output == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5019 | PyErr_BadArgument(); |
| 5020 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5021 | } |
| 5022 | |
| 5023 | p = s; |
| 5024 | end = s + length; |
| 5025 | while (p < end) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5026 | register Py_UNICODE ch = *p; |
| 5027 | int decimal; |
| 5028 | PyObject *repunicode; |
| 5029 | Py_ssize_t repsize; |
| 5030 | Py_ssize_t newpos; |
| 5031 | Py_UNICODE *uni2; |
| 5032 | Py_UNICODE *collstart; |
| 5033 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5034 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5035 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5036 | *output++ = ' '; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5037 | ++p; |
| 5038 | continue; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5039 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5040 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5041 | if (decimal >= 0) { |
| 5042 | *output++ = '0' + decimal; |
| 5043 | ++p; |
| 5044 | continue; |
| 5045 | } |
| 5046 | if (0 < ch && ch < 256) { |
| 5047 | *output++ = (char)ch; |
| 5048 | ++p; |
| 5049 | continue; |
| 5050 | } |
| 5051 | /* All other characters are considered unencodable */ |
| 5052 | collstart = p; |
| 5053 | collend = p+1; |
| 5054 | while (collend < end) { |
| 5055 | if ((0 < *collend && *collend < 256) || |
| 5056 | !Py_UNICODE_ISSPACE(*collend) || |
| 5057 | Py_UNICODE_TODECIMAL(*collend)) |
| 5058 | break; |
| 5059 | } |
| 5060 | /* cache callback name lookup |
| 5061 | * (if not done yet, i.e. it's the first error) */ |
| 5062 | if (known_errorHandler==-1) { |
| 5063 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5064 | known_errorHandler = 1; |
| 5065 | else if (!strcmp(errors, "replace")) |
| 5066 | known_errorHandler = 2; |
| 5067 | else if (!strcmp(errors, "ignore")) |
| 5068 | known_errorHandler = 3; |
| 5069 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5070 | known_errorHandler = 4; |
| 5071 | else |
| 5072 | known_errorHandler = 0; |
| 5073 | } |
| 5074 | switch (known_errorHandler) { |
| 5075 | case 1: /* strict */ |
| 5076 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 5077 | goto onError; |
| 5078 | case 2: /* replace */ |
| 5079 | for (p = collstart; p < collend; ++p) |
| 5080 | *output++ = '?'; |
| 5081 | /* fall through */ |
| 5082 | case 3: /* ignore */ |
| 5083 | p = collend; |
| 5084 | break; |
| 5085 | case 4: /* xmlcharrefreplace */ |
| 5086 | /* generate replacement (temporarily (mis)uses p) */ |
| 5087 | for (p = collstart; p < collend; ++p) |
| 5088 | output += sprintf(output, "&#%d;", (int)*p); |
| 5089 | p = collend; |
| 5090 | break; |
| 5091 | default: |
| 5092 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 5093 | encoding, reason, s, length, &exc, |
| 5094 | collstart-s, collend-s, &newpos); |
| 5095 | if (repunicode == NULL) |
| 5096 | goto onError; |
| 5097 | /* generate replacement */ |
| 5098 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5099 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 5100 | Py_UNICODE ch = *uni2; |
| 5101 | if (Py_UNICODE_ISSPACE(ch)) |
| 5102 | *output++ = ' '; |
| 5103 | else { |
| 5104 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5105 | if (decimal >= 0) |
| 5106 | *output++ = '0' + decimal; |
| 5107 | else if (0 < ch && ch < 256) |
| 5108 | *output++ = (char)ch; |
| 5109 | else { |
| 5110 | Py_DECREF(repunicode); |
| 5111 | raise_encode_exception(&exc, encoding, |
| 5112 | s, length, collstart-s, collend-s, reason); |
| 5113 | goto onError; |
| 5114 | } |
| 5115 | } |
| 5116 | } |
| 5117 | p = s + newpos; |
| 5118 | Py_DECREF(repunicode); |
| 5119 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5120 | } |
| 5121 | /* 0-terminate the output string */ |
| 5122 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5123 | Py_XDECREF(exc); |
| 5124 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5125 | return 0; |
| 5126 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5127 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5128 | Py_XDECREF(exc); |
| 5129 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5130 | return -1; |
| 5131 | } |
| 5132 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5133 | /* --- Helpers ------------------------------------------------------------ */ |
| 5134 | |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 5135 | #include "stringlib/unicodedefs.h" |
Fredrik Lundh | 6471ee4 | 2006-05-24 14:28:11 +0000 | [diff] [blame] | 5136 | |
Facundo Batista | 6f7e6fb | 2007-11-16 19:16:15 +0000 | [diff] [blame] | 5137 | #define FROM_UNICODE |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 5138 | |
Fredrik Lundh | a50d201 | 2006-05-26 17:04:58 +0000 | [diff] [blame] | 5139 | #include "stringlib/fastsearch.h" |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5140 | |
| 5141 | #include "stringlib/count.h" |
| 5142 | #include "stringlib/find.h" |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 5143 | #include "stringlib/partition.h" |
| 5144 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 5145 | /* helper macro to fixup start/end slice values */ |
| 5146 | #define FIX_START_END(obj) \ |
| 5147 | if (start < 0) \ |
| 5148 | start += (obj)->length; \ |
| 5149 | if (start < 0) \ |
| 5150 | start = 0; \ |
| 5151 | if (end > (obj)->length) \ |
| 5152 | end = (obj)->length; \ |
| 5153 | if (end < 0) \ |
| 5154 | end += (obj)->length; \ |
| 5155 | if (end < 0) \ |
| 5156 | end = 0; |
| 5157 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5158 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5159 | PyObject *substr, |
| 5160 | Py_ssize_t start, |
| 5161 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5162 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5163 | Py_ssize_t result; |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5164 | PyUnicodeObject* str_obj; |
| 5165 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5166 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5167 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 5168 | if (!str_obj) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5169 | return -1; |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5170 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 5171 | if (!sub_obj) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5172 | Py_DECREF(str_obj); |
| 5173 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5174 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5175 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 5176 | FIX_START_END(str_obj); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5177 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5178 | result = stringlib_count( |
| 5179 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
| 5180 | ); |
| 5181 | |
| 5182 | Py_DECREF(sub_obj); |
| 5183 | Py_DECREF(str_obj); |
| 5184 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5185 | return result; |
| 5186 | } |
| 5187 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5188 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5189 | PyObject *sub, |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 5190 | Py_ssize_t start, |
| 5191 | Py_ssize_t end, |
| 5192 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5193 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5194 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5195 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5196 | str = PyUnicode_FromObject(str); |
| 5197 | if (!str) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5198 | return -2; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5199 | sub = PyUnicode_FromObject(sub); |
| 5200 | if (!sub) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5201 | Py_DECREF(str); |
| 5202 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5203 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5204 | |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 5205 | if (direction > 0) |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 5206 | result = stringlib_find_slice( |
| 5207 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5208 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5209 | start, end |
| 5210 | ); |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 5211 | else |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 5212 | result = stringlib_rfind_slice( |
| 5213 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5214 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5215 | start, end |
| 5216 | ); |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 5217 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5218 | Py_DECREF(str); |
| 5219 | Py_DECREF(sub); |
| 5220 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5221 | return result; |
| 5222 | } |
| 5223 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5224 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5225 | int tailmatch(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5226 | PyUnicodeObject *substring, |
| 5227 | Py_ssize_t start, |
| 5228 | Py_ssize_t end, |
| 5229 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5230 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5231 | if (substring->length == 0) |
| 5232 | return 1; |
| 5233 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 5234 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5235 | |
| 5236 | end -= substring->length; |
| 5237 | if (end < start) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5238 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5239 | |
| 5240 | if (direction > 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5241 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 5242 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5243 | } else { |
| 5244 | if (Py_UNICODE_MATCH(self, start, substring)) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5245 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5246 | } |
| 5247 | |
| 5248 | return 0; |
| 5249 | } |
| 5250 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5251 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5252 | PyObject *substr, |
| 5253 | Py_ssize_t start, |
| 5254 | Py_ssize_t end, |
| 5255 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5256 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5257 | Py_ssize_t result; |
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 | str = PyUnicode_FromObject(str); |
| 5260 | if (str == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5261 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5262 | substr = PyUnicode_FromObject(substr); |
| 5263 | if (substr == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5264 | Py_DECREF(str); |
| 5265 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5266 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5267 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5268 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5269 | (PyUnicodeObject *)substr, |
| 5270 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5271 | Py_DECREF(str); |
| 5272 | Py_DECREF(substr); |
| 5273 | return result; |
| 5274 | } |
| 5275 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5276 | /* Apply fixfct filter to the Unicode object self and return a |
| 5277 | reference to the modified object */ |
| 5278 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5279 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5280 | PyObject *fixup(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5281 | int (*fixfct)(PyUnicodeObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5282 | { |
| 5283 | |
| 5284 | PyUnicodeObject *u; |
| 5285 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5286 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5287 | if (u == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5288 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5289 | |
| 5290 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5291 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5292 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5293 | /* fixfct should return TRUE if it modified the buffer. If |
| 5294 | FALSE, return a reference to the original buffer instead |
| 5295 | (to save space, not time) */ |
| 5296 | Py_INCREF(self); |
| 5297 | Py_DECREF(u); |
| 5298 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5299 | } |
| 5300 | return (PyObject*) u; |
| 5301 | } |
| 5302 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5303 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5304 | int fixupper(PyUnicodeObject *self) |
| 5305 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5306 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5307 | Py_UNICODE *s = self->str; |
| 5308 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5309 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5310 | while (len-- > 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5311 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5312 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5313 | ch = Py_UNICODE_TOUPPER(*s); |
| 5314 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5315 | status = 1; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5316 | *s = ch; |
| 5317 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5318 | s++; |
| 5319 | } |
| 5320 | |
| 5321 | return status; |
| 5322 | } |
| 5323 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5324 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5325 | int fixlower(PyUnicodeObject *self) |
| 5326 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5327 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5328 | Py_UNICODE *s = self->str; |
| 5329 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5330 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5331 | while (len-- > 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5332 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5333 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5334 | ch = Py_UNICODE_TOLOWER(*s); |
| 5335 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5336 | status = 1; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5337 | *s = ch; |
| 5338 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5339 | s++; |
| 5340 | } |
| 5341 | |
| 5342 | return status; |
| 5343 | } |
| 5344 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5345 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5346 | int fixswapcase(PyUnicodeObject *self) |
| 5347 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5348 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5349 | Py_UNICODE *s = self->str; |
| 5350 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5351 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5352 | while (len-- > 0) { |
| 5353 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5354 | *s = Py_UNICODE_TOLOWER(*s); |
| 5355 | status = 1; |
| 5356 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 5357 | *s = Py_UNICODE_TOUPPER(*s); |
| 5358 | status = 1; |
| 5359 | } |
| 5360 | s++; |
| 5361 | } |
| 5362 | |
| 5363 | return status; |
| 5364 | } |
| 5365 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5366 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5367 | int fixcapitalize(PyUnicodeObject *self) |
| 5368 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5369 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5370 | Py_UNICODE *s = self->str; |
| 5371 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5372 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5373 | if (len == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5374 | return 0; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5375 | if (Py_UNICODE_ISLOWER(*s)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5376 | *s = Py_UNICODE_TOUPPER(*s); |
| 5377 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5378 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5379 | s++; |
| 5380 | while (--len > 0) { |
| 5381 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5382 | *s = Py_UNICODE_TOLOWER(*s); |
| 5383 | status = 1; |
| 5384 | } |
| 5385 | s++; |
| 5386 | } |
| 5387 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5388 | } |
| 5389 | |
| 5390 | static |
| 5391 | int fixtitle(PyUnicodeObject *self) |
| 5392 | { |
| 5393 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5394 | register Py_UNICODE *e; |
| 5395 | int previous_is_cased; |
| 5396 | |
| 5397 | /* Shortcut for single character strings */ |
| 5398 | if (PyUnicode_GET_SIZE(self) == 1) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5399 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 5400 | if (*p != ch) { |
| 5401 | *p = ch; |
| 5402 | return 1; |
| 5403 | } |
| 5404 | else |
| 5405 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5406 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5407 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5408 | e = p + PyUnicode_GET_SIZE(self); |
| 5409 | previous_is_cased = 0; |
| 5410 | for (; p < e; p++) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5411 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5412 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5413 | if (previous_is_cased) |
| 5414 | *p = Py_UNICODE_TOLOWER(ch); |
| 5415 | else |
| 5416 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5417 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5418 | if (Py_UNICODE_ISLOWER(ch) || |
| 5419 | Py_UNICODE_ISUPPER(ch) || |
| 5420 | Py_UNICODE_ISTITLE(ch)) |
| 5421 | previous_is_cased = 1; |
| 5422 | else |
| 5423 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5424 | } |
| 5425 | return 1; |
| 5426 | } |
| 5427 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5428 | PyObject * |
| 5429 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5430 | { |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5431 | PyObject *internal_separator = NULL; |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 5432 | const Py_UNICODE blank = ' '; |
| 5433 | const Py_UNICODE *sep = ␣ |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 5434 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5435 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 5436 | Py_ssize_t res_alloc = 100; /* # allocated bytes for string in res */ |
| 5437 | Py_ssize_t res_used; /* # used bytes */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5438 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 5439 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5440 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5441 | PyObject *item; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5442 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5443 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5444 | fseq = PySequence_Fast(seq, ""); |
| 5445 | if (fseq == NULL) { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5446 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5447 | } |
| 5448 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5449 | /* Grrrr. A codec may be invoked to convert str objects to |
| 5450 | * Unicode, and so it's possible to call back into Python code |
| 5451 | * during PyUnicode_FromObject(), and so it's possible for a sick |
| 5452 | * codec to change the size of fseq (if seq is a list). Therefore |
| 5453 | * we have to keep refetching the size -- can't assume seqlen |
| 5454 | * is invariant. |
| 5455 | */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5456 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5457 | /* If empty sequence, return u"". */ |
| 5458 | if (seqlen == 0) { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5459 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 5460 | goto Done; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5461 | } |
| 5462 | /* If singleton sequence with an exact Unicode, return that. */ |
| 5463 | if (seqlen == 1) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5464 | item = PySequence_Fast_GET_ITEM(fseq, 0); |
| 5465 | if (PyUnicode_CheckExact(item)) { |
| 5466 | Py_INCREF(item); |
| 5467 | res = (PyUnicodeObject *)item; |
| 5468 | goto Done; |
| 5469 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5470 | } |
| 5471 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5472 | /* At least two items to join, or one that isn't exact Unicode. */ |
| 5473 | if (seqlen > 1) { |
| 5474 | /* Set up sep and seplen -- they're needed. */ |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5475 | if (separator == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5476 | sep = ␣ |
| 5477 | seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5478 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5479 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5480 | internal_separator = PyUnicode_FromObject(separator); |
| 5481 | if (internal_separator == NULL) |
| 5482 | goto onError; |
| 5483 | sep = PyUnicode_AS_UNICODE(internal_separator); |
| 5484 | seplen = PyUnicode_GET_SIZE(internal_separator); |
| 5485 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 5486 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5487 | } |
| 5488 | } |
| 5489 | |
| 5490 | /* Get space. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5491 | res = _PyUnicode_New(res_alloc); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5492 | if (res == NULL) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5493 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5494 | res_p = PyUnicode_AS_UNICODE(res); |
| 5495 | res_used = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5496 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5497 | for (i = 0; i < seqlen; ++i) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5498 | Py_ssize_t itemlen; |
| 5499 | Py_ssize_t new_res_used; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5500 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5501 | item = PySequence_Fast_GET_ITEM(fseq, i); |
| 5502 | /* Convert item to Unicode. */ |
| 5503 | if (! PyUnicode_Check(item) && ! PyString_Check(item)) { |
| 5504 | PyErr_Format(PyExc_TypeError, |
| 5505 | "sequence item %zd: expected string or Unicode," |
| 5506 | " %.80s found", |
| 5507 | i, Py_TYPE(item)->tp_name); |
| 5508 | goto onError; |
| 5509 | } |
| 5510 | item = PyUnicode_FromObject(item); |
| 5511 | if (item == NULL) |
| 5512 | goto onError; |
| 5513 | /* We own a reference to item from here on. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5514 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5515 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 5516 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5517 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5518 | /* Make sure we have enough space for the separator and the item. */ |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5519 | itemlen = PyUnicode_GET_SIZE(item); |
| 5520 | new_res_used = res_used + itemlen; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5521 | if (new_res_used < 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5522 | goto Overflow; |
| 5523 | if (i < seqlen - 1) { |
| 5524 | new_res_used += seplen; |
| 5525 | if (new_res_used < 0) |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5526 | goto Overflow; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5527 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5528 | if (new_res_used > res_alloc) { |
| 5529 | /* double allocated size until it's big enough */ |
| 5530 | do { |
| 5531 | res_alloc += res_alloc; |
| 5532 | if (res_alloc <= 0) |
| 5533 | goto Overflow; |
| 5534 | } while (new_res_used > res_alloc); |
| 5535 | if (_PyUnicode_Resize(&res, res_alloc) < 0) { |
| 5536 | Py_DECREF(item); |
| 5537 | goto onError; |
| 5538 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5539 | res_p = PyUnicode_AS_UNICODE(res) + res_used; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5540 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5541 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5542 | /* Copy item, and maybe the separator. */ |
| 5543 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
| 5544 | res_p += itemlen; |
| 5545 | if (i < seqlen - 1) { |
| 5546 | Py_UNICODE_COPY(res_p, sep, seplen); |
| 5547 | res_p += seplen; |
| 5548 | } |
| 5549 | Py_DECREF(item); |
| 5550 | res_used = new_res_used; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5551 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5552 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5553 | /* Shrink res to match the used area; this probably can't fail, |
| 5554 | * but it's cheap to check. |
| 5555 | */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5556 | if (_PyUnicode_Resize(&res, res_used) < 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5557 | goto onError; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5558 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5559 | Done: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5560 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5561 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5562 | return (PyObject *)res; |
| 5563 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5564 | Overflow: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5565 | PyErr_SetString(PyExc_OverflowError, |
Georg Brandl | 90e27d3 | 2006-06-10 06:40:50 +0000 | [diff] [blame] | 5566 | "join() result is too long for a Python string"); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5567 | Py_DECREF(item); |
| 5568 | /* fall through */ |
| 5569 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5570 | onError: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5571 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5572 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5573 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5574 | return NULL; |
| 5575 | } |
| 5576 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5577 | static |
| 5578 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5579 | Py_ssize_t left, |
| 5580 | Py_ssize_t right, |
| 5581 | Py_UNICODE fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5582 | { |
| 5583 | PyUnicodeObject *u; |
| 5584 | |
| 5585 | if (left < 0) |
| 5586 | left = 0; |
| 5587 | if (right < 0) |
| 5588 | right = 0; |
| 5589 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5590 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5591 | Py_INCREF(self); |
| 5592 | return self; |
| 5593 | } |
| 5594 | |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 5595 | if (left > PY_SSIZE_T_MAX - self->length || |
| 5596 | right > PY_SSIZE_T_MAX - (left + self->length)) { |
| 5597 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 5598 | return NULL; |
| 5599 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5600 | u = _PyUnicode_New(left + self->length + right); |
| 5601 | if (u) { |
| 5602 | if (left) |
| 5603 | Py_UNICODE_FILL(u->str, fill, left); |
| 5604 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 5605 | if (right) |
| 5606 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 5607 | } |
| 5608 | |
| 5609 | return u; |
| 5610 | } |
| 5611 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5612 | #define SPLIT_APPEND(data, left, right) \ |
| 5613 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
| 5614 | if (!str) \ |
| 5615 | goto onError; \ |
| 5616 | if (PyList_Append(list, str)) { \ |
| 5617 | Py_DECREF(str); \ |
| 5618 | goto onError; \ |
| 5619 | } \ |
| 5620 | else \ |
| 5621 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5622 | |
| 5623 | static |
| 5624 | PyObject *split_whitespace(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5625 | PyObject *list, |
| 5626 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5627 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5628 | register Py_ssize_t i; |
| 5629 | register Py_ssize_t j; |
| 5630 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5631 | PyObject *str; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5632 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5633 | |
| 5634 | for (i = j = 0; i < len; ) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5635 | /* find a token */ |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5636 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5637 | i++; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5638 | j = i; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5639 | while (i < len && !Py_UNICODE_ISSPACE(buf[i])) |
| 5640 | i++; |
| 5641 | if (j < i) { |
| 5642 | if (maxcount-- <= 0) |
| 5643 | break; |
| 5644 | SPLIT_APPEND(buf, j, i); |
| 5645 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
| 5646 | i++; |
| 5647 | j = i; |
| 5648 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5649 | } |
| 5650 | if (j < len) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5651 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5652 | } |
| 5653 | return list; |
| 5654 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5655 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5656 | Py_DECREF(list); |
| 5657 | return NULL; |
| 5658 | } |
| 5659 | |
| 5660 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5661 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5662 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5663 | register Py_ssize_t i; |
| 5664 | register Py_ssize_t j; |
| 5665 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5666 | PyObject *list; |
| 5667 | PyObject *str; |
| 5668 | Py_UNICODE *data; |
| 5669 | |
| 5670 | string = PyUnicode_FromObject(string); |
| 5671 | if (string == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5672 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5673 | data = PyUnicode_AS_UNICODE(string); |
| 5674 | len = PyUnicode_GET_SIZE(string); |
| 5675 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5676 | list = PyList_New(0); |
| 5677 | if (!list) |
| 5678 | goto onError; |
| 5679 | |
| 5680 | for (i = j = 0; i < len; ) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5681 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5682 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5683 | /* Find a line and append it */ |
| 5684 | while (i < len && !BLOOM_LINEBREAK(data[i])) |
| 5685 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5686 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5687 | /* Skip the line break reading CRLF as one line break */ |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5688 | eol = i; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5689 | if (i < len) { |
| 5690 | if (data[i] == '\r' && i + 1 < len && |
| 5691 | data[i+1] == '\n') |
| 5692 | i += 2; |
| 5693 | else |
| 5694 | i++; |
| 5695 | if (keepends) |
| 5696 | eol = i; |
| 5697 | } |
| 5698 | SPLIT_APPEND(data, j, eol); |
| 5699 | j = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5700 | } |
| 5701 | if (j < len) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5702 | SPLIT_APPEND(data, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5703 | } |
| 5704 | |
| 5705 | Py_DECREF(string); |
| 5706 | return list; |
| 5707 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5708 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5709 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5710 | Py_DECREF(string); |
| 5711 | return NULL; |
| 5712 | } |
| 5713 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5714 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5715 | PyObject *split_char(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5716 | PyObject *list, |
| 5717 | Py_UNICODE ch, |
| 5718 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5719 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5720 | register Py_ssize_t i; |
| 5721 | register Py_ssize_t j; |
| 5722 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5723 | PyObject *str; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5724 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5725 | |
| 5726 | for (i = j = 0; i < len; ) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5727 | if (buf[i] == ch) { |
| 5728 | if (maxcount-- <= 0) |
| 5729 | break; |
| 5730 | SPLIT_APPEND(buf, j, i); |
| 5731 | i = j = i + 1; |
| 5732 | } else |
| 5733 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5734 | } |
| 5735 | if (j <= len) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5736 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5737 | } |
| 5738 | return list; |
| 5739 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5740 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5741 | Py_DECREF(list); |
| 5742 | return NULL; |
| 5743 | } |
| 5744 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5745 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5746 | PyObject *split_substring(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5747 | PyObject *list, |
| 5748 | PyUnicodeObject *substring, |
| 5749 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5750 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5751 | register Py_ssize_t i; |
| 5752 | register Py_ssize_t j; |
| 5753 | Py_ssize_t len = self->length; |
| 5754 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5755 | PyObject *str; |
| 5756 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 5757 | for (i = j = 0; i <= len - sublen; ) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5758 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5759 | if (maxcount-- <= 0) |
| 5760 | break; |
| 5761 | SPLIT_APPEND(self->str, j, i); |
| 5762 | i = j = i + sublen; |
| 5763 | } else |
| 5764 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5765 | } |
| 5766 | if (j <= len) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5767 | SPLIT_APPEND(self->str, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5768 | } |
| 5769 | return list; |
| 5770 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5771 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5772 | Py_DECREF(list); |
| 5773 | return NULL; |
| 5774 | } |
| 5775 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5776 | static |
| 5777 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5778 | PyObject *list, |
| 5779 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5780 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5781 | register Py_ssize_t i; |
| 5782 | register Py_ssize_t j; |
| 5783 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5784 | PyObject *str; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5785 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5786 | |
| 5787 | for (i = j = len - 1; i >= 0; ) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5788 | /* find a token */ |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5789 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5790 | i--; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5791 | j = i; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5792 | while (i >= 0 && !Py_UNICODE_ISSPACE(buf[i])) |
| 5793 | i--; |
| 5794 | if (j > i) { |
| 5795 | if (maxcount-- <= 0) |
| 5796 | break; |
| 5797 | SPLIT_APPEND(buf, i + 1, j + 1); |
| 5798 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
| 5799 | i--; |
| 5800 | j = i; |
| 5801 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5802 | } |
| 5803 | if (j >= 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5804 | SPLIT_APPEND(buf, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5805 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5806 | if (PyList_Reverse(list) < 0) |
| 5807 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5808 | return list; |
| 5809 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5810 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5811 | Py_DECREF(list); |
| 5812 | return NULL; |
| 5813 | } |
| 5814 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5815 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5816 | PyObject *rsplit_char(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5817 | PyObject *list, |
| 5818 | Py_UNICODE ch, |
| 5819 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5820 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5821 | register Py_ssize_t i; |
| 5822 | register Py_ssize_t j; |
| 5823 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5824 | PyObject *str; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5825 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5826 | |
| 5827 | for (i = j = len - 1; i >= 0; ) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5828 | if (buf[i] == ch) { |
| 5829 | if (maxcount-- <= 0) |
| 5830 | break; |
| 5831 | SPLIT_APPEND(buf, i + 1, j + 1); |
| 5832 | j = i = i - 1; |
| 5833 | } else |
| 5834 | i--; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5835 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 5836 | if (j >= -1) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5837 | SPLIT_APPEND(buf, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5838 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5839 | if (PyList_Reverse(list) < 0) |
| 5840 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5841 | return list; |
| 5842 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5843 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5844 | Py_DECREF(list); |
| 5845 | return NULL; |
| 5846 | } |
| 5847 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 5848 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5849 | PyObject *rsplit_substring(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5850 | PyObject *list, |
| 5851 | PyUnicodeObject *substring, |
| 5852 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5853 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5854 | register Py_ssize_t i; |
| 5855 | register Py_ssize_t j; |
| 5856 | Py_ssize_t len = self->length; |
| 5857 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5858 | PyObject *str; |
| 5859 | |
| 5860 | for (i = len - sublen, j = len; i >= 0; ) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5861 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5862 | if (maxcount-- <= 0) |
| 5863 | break; |
| 5864 | SPLIT_APPEND(self->str, i + sublen, j); |
| 5865 | j = i; |
| 5866 | i -= sublen; |
| 5867 | } else |
| 5868 | i--; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5869 | } |
| 5870 | if (j >= 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5871 | SPLIT_APPEND(self->str, 0, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5872 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5873 | if (PyList_Reverse(list) < 0) |
| 5874 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5875 | return list; |
| 5876 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5877 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5878 | Py_DECREF(list); |
| 5879 | return NULL; |
| 5880 | } |
| 5881 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5882 | #undef SPLIT_APPEND |
| 5883 | |
| 5884 | static |
| 5885 | PyObject *split(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5886 | PyUnicodeObject *substring, |
| 5887 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5888 | { |
| 5889 | PyObject *list; |
| 5890 | |
| 5891 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5892 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5893 | |
| 5894 | list = PyList_New(0); |
| 5895 | if (!list) |
| 5896 | return NULL; |
| 5897 | |
| 5898 | if (substring == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5899 | return split_whitespace(self,list,maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5900 | |
| 5901 | else if (substring->length == 1) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5902 | return split_char(self,list,substring->str[0],maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5903 | |
| 5904 | else if (substring->length == 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5905 | Py_DECREF(list); |
| 5906 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5907 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5908 | } |
| 5909 | else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5910 | return split_substring(self,list,substring,maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5911 | } |
| 5912 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5913 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5914 | PyObject *rsplit(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5915 | PyUnicodeObject *substring, |
| 5916 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5917 | { |
| 5918 | PyObject *list; |
| 5919 | |
| 5920 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5921 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5922 | |
| 5923 | list = PyList_New(0); |
| 5924 | if (!list) |
| 5925 | return NULL; |
| 5926 | |
| 5927 | if (substring == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5928 | return rsplit_whitespace(self,list,maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5929 | |
| 5930 | else if (substring->length == 1) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5931 | return rsplit_char(self,list,substring->str[0],maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5932 | |
| 5933 | else if (substring->length == 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5934 | Py_DECREF(list); |
| 5935 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5936 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5937 | } |
| 5938 | else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5939 | return rsplit_substring(self,list,substring,maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5940 | } |
| 5941 | |
| 5942 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5943 | PyObject *replace(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5944 | PyUnicodeObject *str1, |
| 5945 | PyUnicodeObject *str2, |
| 5946 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5947 | { |
| 5948 | PyUnicodeObject *u; |
| 5949 | |
| 5950 | if (maxcount < 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 5951 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5952 | |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5953 | if (str1->length == str2->length) { |
| 5954 | /* same length */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5955 | Py_ssize_t i; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5956 | if (str1->length == 1) { |
| 5957 | /* replace characters */ |
| 5958 | Py_UNICODE u1, u2; |
| 5959 | if (!findchar(self->str, self->length, str1->str[0])) |
| 5960 | goto nothing; |
| 5961 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5962 | if (!u) |
| 5963 | return NULL; |
| 5964 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5965 | u1 = str1->str[0]; |
| 5966 | u2 = str2->str[0]; |
| 5967 | for (i = 0; i < u->length; i++) |
| 5968 | if (u->str[i] == u1) { |
| 5969 | if (--maxcount < 0) |
| 5970 | break; |
| 5971 | u->str[i] = u2; |
| 5972 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5973 | } else { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5974 | i = fastsearch( |
| 5975 | self->str, self->length, str1->str, str1->length, FAST_SEARCH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5976 | ); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5977 | if (i < 0) |
| 5978 | goto nothing; |
| 5979 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5980 | if (!u) |
| 5981 | return NULL; |
| 5982 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5983 | while (i <= self->length - str1->length) |
| 5984 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 5985 | if (--maxcount < 0) |
| 5986 | break; |
| 5987 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 5988 | i += str1->length; |
| 5989 | } else |
| 5990 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5991 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5992 | } else { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5993 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 5994 | Py_ssize_t n, i, j, e; |
Fredrik Lundh | 0c71f88 | 2006-05-25 16:46:54 +0000 | [diff] [blame] | 5995 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5996 | Py_UNICODE *p; |
| 5997 | |
| 5998 | /* replace strings */ |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5999 | n = stringlib_count(self->str, self->length, str1->str, str1->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6000 | if (n > maxcount) |
| 6001 | n = maxcount; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6002 | if (n == 0) |
| 6003 | goto nothing; |
Fredrik Lundh | 0c71f88 | 2006-05-25 16:46:54 +0000 | [diff] [blame] | 6004 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 6005 | delta = (str2->length - str1->length); |
| 6006 | if (delta == 0) { |
| 6007 | new_size = self->length; |
| 6008 | } else { |
| 6009 | product = n * (str2->length - str1->length); |
| 6010 | if ((product / (str2->length - str1->length)) != n) { |
| 6011 | PyErr_SetString(PyExc_OverflowError, |
| 6012 | "replace string is too long"); |
| 6013 | return NULL; |
| 6014 | } |
| 6015 | new_size = self->length + product; |
| 6016 | if (new_size < 0) { |
| 6017 | PyErr_SetString(PyExc_OverflowError, |
| 6018 | "replace string is too long"); |
| 6019 | return NULL; |
| 6020 | } |
| 6021 | } |
| 6022 | u = _PyUnicode_New(new_size); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6023 | if (!u) |
| 6024 | return NULL; |
| 6025 | i = 0; |
| 6026 | p = u->str; |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 6027 | e = self->length - str1->length; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6028 | if (str1->length > 0) { |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 6029 | while (n-- > 0) { |
| 6030 | /* look for next match */ |
| 6031 | j = i; |
| 6032 | while (j <= e) { |
| 6033 | if (Py_UNICODE_MATCH(self, j, str1)) |
| 6034 | break; |
| 6035 | j++; |
| 6036 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6037 | if (j > i) { |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 6038 | if (j > e) |
| 6039 | break; |
| 6040 | /* copy unchanged part [i:j] */ |
| 6041 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 6042 | p += j - i; |
| 6043 | } |
| 6044 | /* copy substitution string */ |
| 6045 | if (str2->length > 0) { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6046 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6047 | p += str2->length; |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 6048 | } |
| 6049 | i = j + str1->length; |
| 6050 | } |
| 6051 | if (i < self->length) |
| 6052 | /* copy tail [i:] */ |
| 6053 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6054 | } else { |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 6055 | /* interleave */ |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6056 | while (n > 0) { |
| 6057 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6058 | p += str2->length; |
| 6059 | if (--n <= 0) |
| 6060 | break; |
| 6061 | *p++ = self->str[i++]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6062 | } |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6063 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6064 | } |
| 6065 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6066 | return (PyObject *) u; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6067 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6068 | nothing: |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6069 | /* nothing to replace; return original string (when possible) */ |
| 6070 | if (PyUnicode_CheckExact(self)) { |
| 6071 | Py_INCREF(self); |
| 6072 | return (PyObject *) self; |
| 6073 | } |
| 6074 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6075 | } |
| 6076 | |
| 6077 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 6078 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6079 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6080 | "S.title() -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6081 | \n\ |
| 6082 | 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] | 6083 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6084 | |
| 6085 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6086 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6087 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6088 | return fixup(self, fixtitle); |
| 6089 | } |
| 6090 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6091 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6092 | "S.capitalize() -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6093 | \n\ |
| 6094 | 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] | 6095 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6096 | |
| 6097 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6098 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6099 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6100 | return fixup(self, fixcapitalize); |
| 6101 | } |
| 6102 | |
| 6103 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6104 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6105 | "S.capwords() -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6106 | \n\ |
| 6107 | 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] | 6108 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6109 | |
| 6110 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6111 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6112 | { |
| 6113 | PyObject *list; |
| 6114 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6115 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6116 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6117 | /* Split into words */ |
| 6118 | list = split(self, NULL, -1); |
| 6119 | if (!list) |
| 6120 | return NULL; |
| 6121 | |
| 6122 | /* Capitalize each word */ |
| 6123 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 6124 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6125 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6126 | if (item == NULL) |
| 6127 | goto onError; |
| 6128 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 6129 | PyList_SET_ITEM(list, i, item); |
| 6130 | } |
| 6131 | |
| 6132 | /* Join the words to form a new string */ |
| 6133 | item = PyUnicode_Join(NULL, list); |
| 6134 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6135 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6136 | Py_DECREF(list); |
| 6137 | return (PyObject *)item; |
| 6138 | } |
| 6139 | #endif |
| 6140 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6141 | /* Argument converter. Coerces to a single unicode character */ |
| 6142 | |
| 6143 | static int |
| 6144 | convert_uc(PyObject *obj, void *addr) |
| 6145 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 6146 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 6147 | PyObject *uniobj; |
| 6148 | Py_UNICODE *unistr; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6149 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 6150 | uniobj = PyUnicode_FromObject(obj); |
| 6151 | if (uniobj == NULL) { |
| 6152 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6153 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 6154 | return 0; |
| 6155 | } |
| 6156 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 6157 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6158 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 6159 | Py_DECREF(uniobj); |
| 6160 | return 0; |
| 6161 | } |
| 6162 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 6163 | *fillcharloc = unistr[0]; |
| 6164 | Py_DECREF(uniobj); |
| 6165 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6166 | } |
| 6167 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6168 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6169 | "S.center(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6170 | \n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6171 | Return S centered in a Unicode string of length width. Padding is\n\ |
| 6172 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6173 | |
| 6174 | static PyObject * |
| 6175 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 6176 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6177 | Py_ssize_t marg, left; |
| 6178 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6179 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6180 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6181 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6182 | return NULL; |
| 6183 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6184 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6185 | Py_INCREF(self); |
| 6186 | return (PyObject*) self; |
| 6187 | } |
| 6188 | |
| 6189 | marg = width - self->length; |
| 6190 | left = marg / 2 + (marg & width & 1); |
| 6191 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6192 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6193 | } |
| 6194 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6195 | #if 0 |
| 6196 | |
| 6197 | /* This code should go into some future Unicode collation support |
| 6198 | module. The basic comparison should compare ordinals on a naive |
Georg Brandl | a3c242c | 2009-10-27 14:19:50 +0000 | [diff] [blame] | 6199 | basis (this is what Java does and thus Jython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6200 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6201 | /* speedy UTF-16 code point order comparison */ |
| 6202 | /* gleaned from: */ |
| 6203 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 6204 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6205 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6206 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6207 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6208 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 6209 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6210 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6211 | }; |
| 6212 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6213 | static int |
| 6214 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6215 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6216 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6217 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6218 | Py_UNICODE *s1 = str1->str; |
| 6219 | Py_UNICODE *s2 = str2->str; |
| 6220 | |
| 6221 | len1 = str1->length; |
| 6222 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6223 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6224 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6225 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6226 | |
| 6227 | c1 = *s1++; |
| 6228 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6229 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6230 | if (c1 > (1<<11) * 26) |
| 6231 | c1 += utf16Fixup[c1>>11]; |
| 6232 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6233 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6234 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6235 | |
| 6236 | if (c1 != c2) |
| 6237 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6238 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6239 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6240 | } |
| 6241 | |
| 6242 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6243 | } |
| 6244 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6245 | #else |
| 6246 | |
| 6247 | static int |
| 6248 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6249 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6250 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6251 | |
| 6252 | Py_UNICODE *s1 = str1->str; |
| 6253 | Py_UNICODE *s2 = str2->str; |
| 6254 | |
| 6255 | len1 = str1->length; |
| 6256 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6257 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6258 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6259 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6260 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6261 | c1 = *s1++; |
| 6262 | c2 = *s2++; |
| 6263 | |
| 6264 | if (c1 != c2) |
| 6265 | return (c1 < c2) ? -1 : 1; |
| 6266 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6267 | len1--; len2--; |
| 6268 | } |
| 6269 | |
| 6270 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6271 | } |
| 6272 | |
| 6273 | #endif |
| 6274 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6275 | int PyUnicode_Compare(PyObject *left, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6276 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6277 | { |
| 6278 | PyUnicodeObject *u = NULL, *v = NULL; |
| 6279 | int result; |
| 6280 | |
| 6281 | /* Coerce the two arguments */ |
| 6282 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 6283 | if (u == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6284 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6285 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 6286 | if (v == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6287 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6288 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6289 | /* Shortcut for empty or interned objects */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6290 | if (v == u) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6291 | Py_DECREF(u); |
| 6292 | Py_DECREF(v); |
| 6293 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6294 | } |
| 6295 | |
| 6296 | result = unicode_compare(u, v); |
| 6297 | |
| 6298 | Py_DECREF(u); |
| 6299 | Py_DECREF(v); |
| 6300 | return result; |
| 6301 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6302 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6303 | Py_XDECREF(u); |
| 6304 | Py_XDECREF(v); |
| 6305 | return -1; |
| 6306 | } |
| 6307 | |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 6308 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 6309 | PyObject *right, |
| 6310 | int op) |
| 6311 | { |
| 6312 | int result; |
| 6313 | |
| 6314 | result = PyUnicode_Compare(left, right); |
| 6315 | if (result == -1 && PyErr_Occurred()) |
| 6316 | goto onError; |
| 6317 | |
| 6318 | /* Convert the return value to a Boolean */ |
| 6319 | switch (op) { |
| 6320 | case Py_EQ: |
| 6321 | result = (result == 0); |
| 6322 | break; |
| 6323 | case Py_NE: |
| 6324 | result = (result != 0); |
| 6325 | break; |
| 6326 | case Py_LE: |
| 6327 | result = (result <= 0); |
| 6328 | break; |
| 6329 | case Py_GE: |
| 6330 | result = (result >= 0); |
| 6331 | break; |
| 6332 | case Py_LT: |
| 6333 | result = (result == -1); |
| 6334 | break; |
| 6335 | case Py_GT: |
| 6336 | result = (result == 1); |
| 6337 | break; |
| 6338 | } |
| 6339 | return PyBool_FromLong(result); |
| 6340 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6341 | onError: |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 6342 | |
| 6343 | /* Standard case |
| 6344 | |
| 6345 | Type errors mean that PyUnicode_FromObject() could not convert |
| 6346 | one of the arguments (usually the right hand side) to Unicode, |
| 6347 | ie. we can't handle the comparison request. However, it is |
| 6348 | possible that the other object knows a comparison method, which |
| 6349 | is why we return Py_NotImplemented to give the other object a |
| 6350 | chance. |
| 6351 | |
| 6352 | */ |
| 6353 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 6354 | PyErr_Clear(); |
| 6355 | Py_INCREF(Py_NotImplemented); |
| 6356 | return Py_NotImplemented; |
| 6357 | } |
| 6358 | if (op != Py_EQ && op != Py_NE) |
| 6359 | return NULL; |
| 6360 | |
| 6361 | /* Equality comparison. |
| 6362 | |
| 6363 | This is a special case: we silence any PyExc_UnicodeDecodeError |
| 6364 | and instead turn it into a PyErr_UnicodeWarning. |
| 6365 | |
| 6366 | */ |
| 6367 | if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) |
| 6368 | return NULL; |
| 6369 | PyErr_Clear(); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 6370 | if (PyErr_Warn(PyExc_UnicodeWarning, |
| 6371 | (op == Py_EQ) ? |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 6372 | "Unicode equal comparison " |
| 6373 | "failed to convert both arguments to Unicode - " |
| 6374 | "interpreting them as being unequal" : |
| 6375 | "Unicode unequal comparison " |
| 6376 | "failed to convert both arguments to Unicode - " |
| 6377 | "interpreting them as being unequal" |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6378 | ) < 0) |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 6379 | return NULL; |
| 6380 | result = (op == Py_NE); |
| 6381 | return PyBool_FromLong(result); |
| 6382 | } |
| 6383 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6384 | int PyUnicode_Contains(PyObject *container, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6385 | PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6386 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6387 | PyObject *str, *sub; |
| 6388 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6389 | |
| 6390 | /* Coerce the two arguments */ |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6391 | sub = PyUnicode_FromObject(element); |
| 6392 | if (!sub) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6393 | PyErr_SetString(PyExc_TypeError, |
| 6394 | "'in <string>' requires string as left operand"); |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 6395 | return -1; |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 6396 | } |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 6397 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6398 | str = PyUnicode_FromObject(container); |
| 6399 | if (!str) { |
| 6400 | Py_DECREF(sub); |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 6401 | return -1; |
| 6402 | } |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6403 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6404 | result = stringlib_contains_obj(str, sub); |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 6405 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6406 | Py_DECREF(str); |
| 6407 | Py_DECREF(sub); |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6408 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6409 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6410 | } |
| 6411 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6412 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 6413 | |
| 6414 | PyObject *PyUnicode_Concat(PyObject *left, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6415 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6416 | { |
| 6417 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 6418 | |
| 6419 | /* Coerce the two arguments */ |
| 6420 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 6421 | if (u == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6422 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6423 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 6424 | if (v == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6425 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6426 | |
| 6427 | /* Shortcuts */ |
| 6428 | if (v == unicode_empty) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6429 | Py_DECREF(v); |
| 6430 | return (PyObject *)u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6431 | } |
| 6432 | if (u == unicode_empty) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6433 | Py_DECREF(u); |
| 6434 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6435 | } |
| 6436 | |
| 6437 | /* Concat the two Unicode strings */ |
| 6438 | w = _PyUnicode_New(u->length + v->length); |
| 6439 | if (w == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6440 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6441 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 6442 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 6443 | |
| 6444 | Py_DECREF(u); |
| 6445 | Py_DECREF(v); |
| 6446 | return (PyObject *)w; |
| 6447 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6448 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6449 | Py_XDECREF(u); |
| 6450 | Py_XDECREF(v); |
| 6451 | return NULL; |
| 6452 | } |
| 6453 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6454 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6455 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6456 | \n\ |
Fredrik Lundh | 763b50f | 2006-05-22 15:35:12 +0000 | [diff] [blame] | 6457 | Return the number of non-overlapping occurrences of substring sub in\n\ |
| 6458 | 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] | 6459 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6460 | |
| 6461 | static PyObject * |
| 6462 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 6463 | { |
| 6464 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6465 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6466 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6467 | PyObject *result; |
| 6468 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6469 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6470 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6471 | return NULL; |
| 6472 | |
| 6473 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6474 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6475 | if (substring == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6476 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6477 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 6478 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6479 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6480 | result = PyInt_FromSsize_t( |
| 6481 | stringlib_count(self->str + start, end - start, |
| 6482 | substring->str, substring->length) |
| 6483 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6484 | |
| 6485 | Py_DECREF(substring); |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6486 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6487 | return result; |
| 6488 | } |
| 6489 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6490 | PyDoc_STRVAR(encode__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6491 | "S.encode([encoding[,errors]]) -> string or unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6492 | \n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6493 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 6494 | 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] | 6495 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6496 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 6497 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 6498 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6499 | |
| 6500 | static PyObject * |
| 6501 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 6502 | { |
| 6503 | char *encoding = NULL; |
| 6504 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6505 | PyObject *v; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 6506 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6507 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 6508 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6509 | v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6510 | if (v == NULL) |
| 6511 | goto onError; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 6512 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6513 | PyErr_Format(PyExc_TypeError, |
| 6514 | "encoder did not return a string/unicode object " |
| 6515 | "(type=%.400s)", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6516 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6517 | Py_DECREF(v); |
| 6518 | return NULL; |
| 6519 | } |
| 6520 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6521 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6522 | onError: |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6523 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6524 | } |
| 6525 | |
| 6526 | PyDoc_STRVAR(decode__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6527 | "S.decode([encoding[,errors]]) -> string or unicode\n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6528 | \n\ |
| 6529 | Decodes S using the codec registered for encoding. encoding defaults\n\ |
| 6530 | to the default encoding. errors may be given to set a different error\n\ |
| 6531 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 6532 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 6533 | as well as any other name registerd with codecs.register_error that is\n\ |
| 6534 | able to handle UnicodeDecodeErrors."); |
| 6535 | |
| 6536 | static PyObject * |
Marc-André Lemburg | 126b44c | 2004-07-10 12:04:20 +0000 | [diff] [blame] | 6537 | unicode_decode(PyUnicodeObject *self, PyObject *args) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6538 | { |
| 6539 | char *encoding = NULL; |
| 6540 | char *errors = NULL; |
| 6541 | PyObject *v; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 6542 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6543 | if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
| 6544 | return NULL; |
| 6545 | v = PyUnicode_AsDecodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6546 | if (v == NULL) |
| 6547 | goto onError; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 6548 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6549 | PyErr_Format(PyExc_TypeError, |
| 6550 | "decoder did not return a string/unicode object " |
| 6551 | "(type=%.400s)", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6552 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6553 | Py_DECREF(v); |
| 6554 | return NULL; |
| 6555 | } |
| 6556 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6557 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6558 | onError: |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6559 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6560 | } |
| 6561 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6562 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6563 | "S.expandtabs([tabsize]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6564 | \n\ |
| 6565 | 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] | 6566 | 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] | 6567 | |
| 6568 | static PyObject* |
| 6569 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 6570 | { |
| 6571 | Py_UNICODE *e; |
| 6572 | Py_UNICODE *p; |
| 6573 | Py_UNICODE *q; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6574 | Py_UNICODE *qe; |
| 6575 | Py_ssize_t i, j, incr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6576 | PyUnicodeObject *u; |
| 6577 | int tabsize = 8; |
| 6578 | |
| 6579 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6580 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6581 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6582 | /* First pass: determine size of output string */ |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6583 | i = 0; /* chars up to and including most recent \n or \r */ |
| 6584 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
| 6585 | e = self->str + self->length; /* end of input */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6586 | for (p = self->str; p < e; p++) |
| 6587 | if (*p == '\t') { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6588 | if (tabsize > 0) { |
| 6589 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 6590 | if (j > PY_SSIZE_T_MAX - incr) |
| 6591 | goto overflow1; |
| 6592 | j += incr; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6593 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6594 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6595 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6596 | if (j > PY_SSIZE_T_MAX - 1) |
| 6597 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6598 | j++; |
| 6599 | if (*p == '\n' || *p == '\r') { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6600 | if (i > PY_SSIZE_T_MAX - j) |
| 6601 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6602 | i += j; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6603 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6604 | } |
| 6605 | } |
| 6606 | |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6607 | if (i > PY_SSIZE_T_MAX - j) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6608 | goto overflow1; |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 6609 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6610 | /* Second pass: create output string and fill it */ |
| 6611 | u = _PyUnicode_New(i + j); |
| 6612 | if (!u) |
| 6613 | return NULL; |
| 6614 | |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6615 | j = 0; /* same as in first pass */ |
| 6616 | q = u->str; /* next output char */ |
| 6617 | qe = u->str + u->length; /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6618 | |
| 6619 | for (p = self->str; p < e; p++) |
| 6620 | if (*p == '\t') { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6621 | if (tabsize > 0) { |
| 6622 | i = tabsize - (j % tabsize); |
| 6623 | j += i; |
| 6624 | while (i--) { |
| 6625 | if (q >= qe) |
| 6626 | goto overflow2; |
| 6627 | *q++ = ' '; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6628 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6629 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 6630 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6631 | else { |
| 6632 | if (q >= qe) |
| 6633 | goto overflow2; |
| 6634 | *q++ = *p; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6635 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6636 | if (*p == '\n' || *p == '\r') |
| 6637 | j = 0; |
| 6638 | } |
| 6639 | |
| 6640 | return (PyObject*) u; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6641 | |
| 6642 | overflow2: |
| 6643 | Py_DECREF(u); |
| 6644 | overflow1: |
| 6645 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 6646 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6647 | } |
| 6648 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6649 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6650 | "S.find(sub [,start [,end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6651 | \n\ |
| 6652 | Return the lowest index in S where substring sub is found,\n\ |
Georg Brandl | 9efd9b6 | 2007-07-29 17:38:35 +0000 | [diff] [blame] | 6653 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6654 | arguments start and end are interpreted as in slice notation.\n\ |
| 6655 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6656 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6657 | |
| 6658 | static PyObject * |
| 6659 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 6660 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6661 | PyObject *substring; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 6662 | Py_ssize_t start; |
| 6663 | Py_ssize_t end; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6664 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6665 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 6666 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6667 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6668 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 6669 | result = stringlib_find_slice( |
| 6670 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6671 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6672 | start, end |
| 6673 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6674 | |
| 6675 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6676 | |
| 6677 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6678 | } |
| 6679 | |
| 6680 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6681 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6682 | { |
| 6683 | if (index < 0 || index >= self->length) { |
| 6684 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 6685 | return NULL; |
| 6686 | } |
| 6687 | |
| 6688 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 6689 | } |
| 6690 | |
| 6691 | static long |
| 6692 | unicode_hash(PyUnicodeObject *self) |
| 6693 | { |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6694 | /* Since Unicode objects compare equal to their ASCII string |
| 6695 | counterparts, they should use the individual character values |
| 6696 | as basis for their hash value. This is needed to assure that |
| 6697 | strings and Unicode objects behave in the same way as |
| 6698 | dictionary keys. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6699 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6700 | register Py_ssize_t len; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6701 | register Py_UNICODE *p; |
| 6702 | register long x; |
| 6703 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6704 | if (self->hash != -1) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6705 | return self->hash; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6706 | len = PyUnicode_GET_SIZE(self); |
| 6707 | p = PyUnicode_AS_UNICODE(self); |
| 6708 | x = *p << 7; |
| 6709 | while (--len >= 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6710 | x = (1000003*x) ^ *p++; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6711 | x ^= PyUnicode_GET_SIZE(self); |
| 6712 | if (x == -1) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6713 | x = -2; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6714 | self->hash = x; |
| 6715 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6716 | } |
| 6717 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6718 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6719 | "S.index(sub [,start [,end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6720 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6721 | 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] | 6722 | |
| 6723 | static PyObject * |
| 6724 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 6725 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6726 | Py_ssize_t result; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6727 | PyObject *substring; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 6728 | Py_ssize_t start; |
| 6729 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6730 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 6731 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6732 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6733 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 6734 | result = stringlib_find_slice( |
| 6735 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6736 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6737 | start, end |
| 6738 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6739 | |
| 6740 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6741 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6742 | if (result < 0) { |
| 6743 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 6744 | return NULL; |
| 6745 | } |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6746 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6747 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6748 | } |
| 6749 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6750 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6751 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6752 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6753 | 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] | 6754 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6755 | |
| 6756 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6757 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6758 | { |
| 6759 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6760 | register const Py_UNICODE *e; |
| 6761 | int cased; |
| 6762 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6763 | /* Shortcut for single character strings */ |
| 6764 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6765 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6766 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6767 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6768 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6769 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6770 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6771 | e = p + PyUnicode_GET_SIZE(self); |
| 6772 | cased = 0; |
| 6773 | for (; p < e; p++) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6774 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6775 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6776 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 6777 | return PyBool_FromLong(0); |
| 6778 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 6779 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6780 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6781 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6782 | } |
| 6783 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6784 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6785 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6786 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6787 | 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] | 6788 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6789 | |
| 6790 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6791 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6792 | { |
| 6793 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6794 | register const Py_UNICODE *e; |
| 6795 | int cased; |
| 6796 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6797 | /* Shortcut for single character strings */ |
| 6798 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6799 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6800 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6801 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6802 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6803 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6804 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6805 | e = p + PyUnicode_GET_SIZE(self); |
| 6806 | cased = 0; |
| 6807 | for (; p < e; p++) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6808 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6809 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6810 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 6811 | return PyBool_FromLong(0); |
| 6812 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 6813 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6814 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6815 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6816 | } |
| 6817 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6818 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6819 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6820 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6821 | Return True if S is a titlecased string and there is at least one\n\ |
| 6822 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 6823 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 6824 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6825 | |
| 6826 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6827 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6828 | { |
| 6829 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6830 | register const Py_UNICODE *e; |
| 6831 | int cased, previous_is_cased; |
| 6832 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6833 | /* Shortcut for single character strings */ |
| 6834 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6835 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 6836 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6837 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6838 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6839 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6840 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6841 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6842 | e = p + PyUnicode_GET_SIZE(self); |
| 6843 | cased = 0; |
| 6844 | previous_is_cased = 0; |
| 6845 | for (; p < e; p++) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6846 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6847 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6848 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 6849 | if (previous_is_cased) |
| 6850 | return PyBool_FromLong(0); |
| 6851 | previous_is_cased = 1; |
| 6852 | cased = 1; |
| 6853 | } |
| 6854 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 6855 | if (!previous_is_cased) |
| 6856 | return PyBool_FromLong(0); |
| 6857 | previous_is_cased = 1; |
| 6858 | cased = 1; |
| 6859 | } |
| 6860 | else |
| 6861 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6862 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6863 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6864 | } |
| 6865 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6866 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6867 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6868 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6869 | Return True if all characters in S are whitespace\n\ |
| 6870 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6871 | |
| 6872 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6873 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6874 | { |
| 6875 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6876 | register const Py_UNICODE *e; |
| 6877 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6878 | /* Shortcut for single character strings */ |
| 6879 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6880 | Py_UNICODE_ISSPACE(*p)) |
| 6881 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6882 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6883 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6884 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6885 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6886 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6887 | e = p + PyUnicode_GET_SIZE(self); |
| 6888 | for (; p < e; p++) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6889 | if (!Py_UNICODE_ISSPACE(*p)) |
| 6890 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6891 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6892 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6893 | } |
| 6894 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6895 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6896 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6897 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6898 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6899 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6900 | |
| 6901 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6902 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6903 | { |
| 6904 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6905 | register const Py_UNICODE *e; |
| 6906 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6907 | /* Shortcut for single character strings */ |
| 6908 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6909 | Py_UNICODE_ISALPHA(*p)) |
| 6910 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6911 | |
| 6912 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6913 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6914 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6915 | |
| 6916 | e = p + PyUnicode_GET_SIZE(self); |
| 6917 | for (; p < e; p++) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6918 | if (!Py_UNICODE_ISALPHA(*p)) |
| 6919 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6920 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6921 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6922 | } |
| 6923 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6924 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6925 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6926 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6927 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6928 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6929 | |
| 6930 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6931 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6932 | { |
| 6933 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6934 | register const Py_UNICODE *e; |
| 6935 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6936 | /* Shortcut for single character strings */ |
| 6937 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6938 | Py_UNICODE_ISALNUM(*p)) |
| 6939 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6940 | |
| 6941 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6942 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6943 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6944 | |
| 6945 | e = p + PyUnicode_GET_SIZE(self); |
| 6946 | for (; p < e; p++) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6947 | if (!Py_UNICODE_ISALNUM(*p)) |
| 6948 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6949 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6950 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6951 | } |
| 6952 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6953 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6954 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6955 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6956 | 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] | 6957 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6958 | |
| 6959 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6960 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6961 | { |
| 6962 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6963 | register const Py_UNICODE *e; |
| 6964 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6965 | /* Shortcut for single character strings */ |
| 6966 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6967 | Py_UNICODE_ISDECIMAL(*p)) |
| 6968 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6969 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6970 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6971 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6972 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6973 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6974 | e = p + PyUnicode_GET_SIZE(self); |
| 6975 | for (; p < e; p++) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6976 | if (!Py_UNICODE_ISDECIMAL(*p)) |
| 6977 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6978 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6979 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6980 | } |
| 6981 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6982 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6983 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6984 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6985 | Return True if all characters in S are digits\n\ |
| 6986 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6987 | |
| 6988 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6989 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6990 | { |
| 6991 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6992 | register const Py_UNICODE *e; |
| 6993 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6994 | /* Shortcut for single character strings */ |
| 6995 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 6996 | Py_UNICODE_ISDIGIT(*p)) |
| 6997 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6998 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6999 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7000 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7001 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7002 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7003 | e = p + PyUnicode_GET_SIZE(self); |
| 7004 | for (; p < e; p++) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7005 | if (!Py_UNICODE_ISDIGIT(*p)) |
| 7006 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7007 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7008 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7009 | } |
| 7010 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7011 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7012 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7013 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7014 | 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] | 7015 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7016 | |
| 7017 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7018 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7019 | { |
| 7020 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7021 | register const Py_UNICODE *e; |
| 7022 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7023 | /* Shortcut for single character strings */ |
| 7024 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7025 | Py_UNICODE_ISNUMERIC(*p)) |
| 7026 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7027 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7028 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7029 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7030 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7031 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7032 | e = p + PyUnicode_GET_SIZE(self); |
| 7033 | for (; p < e; p++) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7034 | if (!Py_UNICODE_ISNUMERIC(*p)) |
| 7035 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7036 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7037 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7038 | } |
| 7039 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7040 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 5d2eb34 | 2009-10-27 15:08:27 +0000 | [diff] [blame] | 7041 | "S.join(iterable) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7042 | \n\ |
| 7043 | 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] | 7044 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7045 | |
| 7046 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7047 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7048 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7049 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7050 | } |
| 7051 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7052 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7053 | unicode_length(PyUnicodeObject *self) |
| 7054 | { |
| 7055 | return self->length; |
| 7056 | } |
| 7057 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7058 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7059 | "S.ljust(width[, fillchar]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7060 | \n\ |
Benjamin Peterson | be2c0a9 | 2008-10-04 21:33:08 +0000 | [diff] [blame] | 7061 | 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] | 7062 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7063 | |
| 7064 | static PyObject * |
| 7065 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 7066 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7067 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7068 | Py_UNICODE fillchar = ' '; |
| 7069 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7070 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7071 | return NULL; |
| 7072 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7073 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7074 | Py_INCREF(self); |
| 7075 | return (PyObject*) self; |
| 7076 | } |
| 7077 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7078 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7079 | } |
| 7080 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7081 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7082 | "S.lower() -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7083 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7084 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7085 | |
| 7086 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7087 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7088 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7089 | return fixup(self, fixlower); |
| 7090 | } |
| 7091 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7092 | #define LEFTSTRIP 0 |
| 7093 | #define RIGHTSTRIP 1 |
| 7094 | #define BOTHSTRIP 2 |
| 7095 | |
| 7096 | /* Arrays indexed by above */ |
| 7097 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 7098 | |
| 7099 | #define STRIPNAME(i) (stripformat[i]+3) |
| 7100 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7101 | /* externally visible for str.strip(unicode) */ |
| 7102 | PyObject * |
| 7103 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 7104 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7105 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7106 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
| 7107 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
| 7108 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 7109 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7110 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7111 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 7112 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7113 | i = 0; |
| 7114 | if (striptype != RIGHTSTRIP) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7115 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7116 | i++; |
| 7117 | } |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7118 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7119 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7120 | j = len; |
| 7121 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7122 | do { |
| 7123 | j--; |
| 7124 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 7125 | j++; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7126 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7127 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7128 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7129 | Py_INCREF(self); |
| 7130 | return (PyObject*)self; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7131 | } |
| 7132 | else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7133 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7134 | } |
| 7135 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7136 | |
| 7137 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7138 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7139 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7140 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7141 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7142 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7143 | i = 0; |
| 7144 | if (striptype != RIGHTSTRIP) { |
| 7145 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 7146 | i++; |
| 7147 | } |
| 7148 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7149 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7150 | j = len; |
| 7151 | if (striptype != LEFTSTRIP) { |
| 7152 | do { |
| 7153 | j--; |
| 7154 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 7155 | j++; |
| 7156 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7157 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7158 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 7159 | Py_INCREF(self); |
| 7160 | return (PyObject*)self; |
| 7161 | } |
| 7162 | else |
| 7163 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7164 | } |
| 7165 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7166 | |
| 7167 | static PyObject * |
| 7168 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 7169 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7170 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7171 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7172 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 7173 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7174 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7175 | if (sep != NULL && sep != Py_None) { |
| 7176 | if (PyUnicode_Check(sep)) |
| 7177 | return _PyUnicode_XStrip(self, striptype, sep); |
| 7178 | else if (PyString_Check(sep)) { |
| 7179 | PyObject *res; |
| 7180 | sep = PyUnicode_FromObject(sep); |
| 7181 | if (sep==NULL) |
| 7182 | return NULL; |
| 7183 | res = _PyUnicode_XStrip(self, striptype, sep); |
| 7184 | Py_DECREF(sep); |
| 7185 | return res; |
| 7186 | } |
| 7187 | else { |
| 7188 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7189 | "%s arg must be None, unicode or str", |
| 7190 | STRIPNAME(striptype)); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7191 | return NULL; |
| 7192 | } |
| 7193 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7194 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7195 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7196 | } |
| 7197 | |
| 7198 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7199 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7200 | "S.strip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7201 | \n\ |
| 7202 | Return a copy of the string S with leading and trailing\n\ |
| 7203 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7204 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7205 | 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] | 7206 | |
| 7207 | static PyObject * |
| 7208 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 7209 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7210 | if (PyTuple_GET_SIZE(args) == 0) |
| 7211 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 7212 | else |
| 7213 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7214 | } |
| 7215 | |
| 7216 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7217 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7218 | "S.lstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7219 | \n\ |
| 7220 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7221 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7222 | 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] | 7223 | |
| 7224 | static PyObject * |
| 7225 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 7226 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7227 | if (PyTuple_GET_SIZE(args) == 0) |
| 7228 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 7229 | else |
| 7230 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7231 | } |
| 7232 | |
| 7233 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7234 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7235 | "S.rstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7236 | \n\ |
| 7237 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7238 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7239 | 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] | 7240 | |
| 7241 | static PyObject * |
| 7242 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 7243 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7244 | if (PyTuple_GET_SIZE(args) == 0) |
| 7245 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 7246 | else |
| 7247 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7248 | } |
| 7249 | |
| 7250 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7251 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7252 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7253 | { |
| 7254 | PyUnicodeObject *u; |
| 7255 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7256 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7257 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7258 | |
| 7259 | if (len < 0) |
| 7260 | len = 0; |
| 7261 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7262 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7263 | /* no repeat, return original string */ |
| 7264 | Py_INCREF(str); |
| 7265 | return (PyObject*) str; |
| 7266 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7267 | |
| 7268 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 7269 | * needed doesn't overflow size_t |
| 7270 | */ |
| 7271 | nchars = len * str->length; |
| 7272 | if (len && nchars / len != str->length) { |
| 7273 | PyErr_SetString(PyExc_OverflowError, |
| 7274 | "repeated string is too long"); |
| 7275 | return NULL; |
| 7276 | } |
| 7277 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 7278 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 7279 | PyErr_SetString(PyExc_OverflowError, |
| 7280 | "repeated string is too long"); |
| 7281 | return NULL; |
| 7282 | } |
| 7283 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7284 | if (!u) |
| 7285 | return NULL; |
| 7286 | |
| 7287 | p = u->str; |
| 7288 | |
Fredrik Lundh | f1d60a5 | 2006-05-22 16:29:30 +0000 | [diff] [blame] | 7289 | if (str->length == 1 && len > 0) { |
| 7290 | Py_UNICODE_FILL(p, str->str[0], len); |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 7291 | } else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7292 | Py_ssize_t done = 0; /* number of characters copied this far */ |
| 7293 | if (done < nchars) { |
Fredrik Lundh | f1d60a5 | 2006-05-22 16:29:30 +0000 | [diff] [blame] | 7294 | Py_UNICODE_COPY(p, str->str, str->length); |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 7295 | done = str->length; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7296 | } |
| 7297 | while (done < nchars) { |
Neal Norwitz | 4677fbf7 | 2008-03-25 04:18:18 +0000 | [diff] [blame] | 7298 | Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 7299 | Py_UNICODE_COPY(p+done, p, n); |
| 7300 | done += n; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7301 | } |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 7302 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7303 | |
| 7304 | return (PyObject*) u; |
| 7305 | } |
| 7306 | |
| 7307 | PyObject *PyUnicode_Replace(PyObject *obj, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7308 | PyObject *subobj, |
| 7309 | PyObject *replobj, |
| 7310 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7311 | { |
| 7312 | PyObject *self; |
| 7313 | PyObject *str1; |
| 7314 | PyObject *str2; |
| 7315 | PyObject *result; |
| 7316 | |
| 7317 | self = PyUnicode_FromObject(obj); |
| 7318 | if (self == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7319 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7320 | str1 = PyUnicode_FromObject(subobj); |
| 7321 | if (str1 == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7322 | Py_DECREF(self); |
| 7323 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7324 | } |
| 7325 | str2 = PyUnicode_FromObject(replobj); |
| 7326 | if (str2 == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7327 | Py_DECREF(self); |
| 7328 | Py_DECREF(str1); |
| 7329 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7330 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7331 | result = replace((PyUnicodeObject *)self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7332 | (PyUnicodeObject *)str1, |
| 7333 | (PyUnicodeObject *)str2, |
| 7334 | maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7335 | Py_DECREF(self); |
| 7336 | Py_DECREF(str1); |
| 7337 | Py_DECREF(str2); |
| 7338 | return result; |
| 7339 | } |
| 7340 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7341 | PyDoc_STRVAR(replace__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7342 | "S.replace (old, new[, count]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7343 | \n\ |
| 7344 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | 30fadc1 | 2008-05-30 07:54:16 +0000 | [diff] [blame] | 7345 | old replaced by new. If the optional argument count is\n\ |
| 7346 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7347 | |
| 7348 | static PyObject* |
| 7349 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 7350 | { |
| 7351 | PyUnicodeObject *str1; |
| 7352 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7353 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7354 | PyObject *result; |
| 7355 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7356 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7357 | return NULL; |
| 7358 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 7359 | if (str1 == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7360 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7361 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7362 | if (str2 == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7363 | Py_DECREF(str1); |
| 7364 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7365 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7366 | |
| 7367 | result = replace(self, str1, str2, maxcount); |
| 7368 | |
| 7369 | Py_DECREF(str1); |
| 7370 | Py_DECREF(str2); |
| 7371 | return result; |
| 7372 | } |
| 7373 | |
| 7374 | static |
| 7375 | PyObject *unicode_repr(PyObject *unicode) |
| 7376 | { |
| 7377 | return unicodeescape_string(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7378 | PyUnicode_GET_SIZE(unicode), |
| 7379 | 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7380 | } |
| 7381 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7382 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7383 | "S.rfind(sub [,start [,end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7384 | \n\ |
| 7385 | Return the highest index in S where substring sub is found,\n\ |
Georg Brandl | 9efd9b6 | 2007-07-29 17:38:35 +0000 | [diff] [blame] | 7386 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7387 | arguments start and end are interpreted as in slice notation.\n\ |
| 7388 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7389 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7390 | |
| 7391 | static PyObject * |
| 7392 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 7393 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7394 | PyObject *substring; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 7395 | Py_ssize_t start; |
| 7396 | Py_ssize_t end; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7397 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7398 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 7399 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7400 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7401 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 7402 | result = stringlib_rfind_slice( |
| 7403 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7404 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7405 | start, end |
| 7406 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7407 | |
| 7408 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7409 | |
| 7410 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7411 | } |
| 7412 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7413 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7414 | "S.rindex(sub [,start [,end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7415 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7416 | 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] | 7417 | |
| 7418 | static PyObject * |
| 7419 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 7420 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7421 | PyObject *substring; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 7422 | Py_ssize_t start; |
| 7423 | Py_ssize_t end; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7424 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7425 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 7426 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7427 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7428 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 7429 | result = stringlib_rfind_slice( |
| 7430 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7431 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7432 | start, end |
| 7433 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7434 | |
| 7435 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7436 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7437 | if (result < 0) { |
| 7438 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7439 | return NULL; |
| 7440 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7441 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7442 | } |
| 7443 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7444 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7445 | "S.rjust(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7446 | \n\ |
Benjamin Peterson | be2c0a9 | 2008-10-04 21:33:08 +0000 | [diff] [blame] | 7447 | 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] | 7448 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7449 | |
| 7450 | static PyObject * |
| 7451 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 7452 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7453 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7454 | Py_UNICODE fillchar = ' '; |
| 7455 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7456 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7457 | return NULL; |
| 7458 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7459 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7460 | Py_INCREF(self); |
| 7461 | return (PyObject*) self; |
| 7462 | } |
| 7463 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7464 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7465 | } |
| 7466 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7467 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7468 | 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] | 7469 | { |
| 7470 | /* standard clamping */ |
| 7471 | if (start < 0) |
| 7472 | start = 0; |
| 7473 | if (end < 0) |
| 7474 | end = 0; |
| 7475 | if (end > self->length) |
| 7476 | end = self->length; |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7477 | if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7478 | /* full slice, return original string */ |
| 7479 | Py_INCREF(self); |
| 7480 | return (PyObject*) self; |
| 7481 | } |
| 7482 | if (start > end) |
| 7483 | start = end; |
| 7484 | /* copy slice */ |
| 7485 | return (PyObject*) PyUnicode_FromUnicode(self->str + start, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7486 | end - start); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7487 | } |
| 7488 | |
| 7489 | PyObject *PyUnicode_Split(PyObject *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7490 | PyObject *sep, |
| 7491 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7492 | { |
| 7493 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7494 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7495 | s = PyUnicode_FromObject(s); |
| 7496 | if (s == NULL) |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7497 | return NULL; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7498 | if (sep != NULL) { |
| 7499 | sep = PyUnicode_FromObject(sep); |
| 7500 | if (sep == NULL) { |
| 7501 | Py_DECREF(s); |
| 7502 | return NULL; |
| 7503 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7504 | } |
| 7505 | |
| 7506 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7507 | |
| 7508 | Py_DECREF(s); |
| 7509 | Py_XDECREF(sep); |
| 7510 | return result; |
| 7511 | } |
| 7512 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7513 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7514 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7515 | \n\ |
| 7516 | Return a list of the words in S, using sep as the\n\ |
| 7517 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Georg Brandl | dfb77db | 2008-05-11 09:11:40 +0000 | [diff] [blame] | 7518 | 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] | 7519 | whitespace string is a separator and empty strings are\n\ |
| 7520 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7521 | |
| 7522 | static PyObject* |
| 7523 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 7524 | { |
| 7525 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7526 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7527 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7528 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7529 | return NULL; |
| 7530 | |
| 7531 | if (substring == Py_None) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7532 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7533 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7534 | return split(self, (PyUnicodeObject *)substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7535 | else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7536 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7537 | } |
| 7538 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7539 | PyObject * |
| 7540 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 7541 | { |
| 7542 | PyObject* str_obj; |
| 7543 | PyObject* sep_obj; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7544 | PyObject* out; |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 7545 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7546 | str_obj = PyUnicode_FromObject(str_in); |
| 7547 | if (!str_obj) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7548 | return NULL; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7549 | sep_obj = PyUnicode_FromObject(sep_in); |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 7550 | if (!sep_obj) { |
| 7551 | Py_DECREF(str_obj); |
| 7552 | return NULL; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7553 | } |
| 7554 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 7555 | out = stringlib_partition( |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 7556 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7557 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7558 | ); |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7559 | |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 7560 | Py_DECREF(sep_obj); |
| 7561 | Py_DECREF(str_obj); |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7562 | |
| 7563 | return out; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7564 | } |
| 7565 | |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7566 | |
| 7567 | PyObject * |
| 7568 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 7569 | { |
| 7570 | PyObject* str_obj; |
| 7571 | PyObject* sep_obj; |
| 7572 | PyObject* out; |
| 7573 | |
| 7574 | str_obj = PyUnicode_FromObject(str_in); |
| 7575 | if (!str_obj) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7576 | return NULL; |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7577 | sep_obj = PyUnicode_FromObject(sep_in); |
| 7578 | if (!sep_obj) { |
| 7579 | Py_DECREF(str_obj); |
| 7580 | return NULL; |
| 7581 | } |
| 7582 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 7583 | out = stringlib_rpartition( |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7584 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7585 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7586 | ); |
| 7587 | |
| 7588 | Py_DECREF(sep_obj); |
| 7589 | Py_DECREF(str_obj); |
| 7590 | |
| 7591 | return out; |
| 7592 | } |
| 7593 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7594 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7595 | "S.partition(sep) -> (head, sep, tail)\n\ |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7596 | \n\ |
Benjamin Peterson | be2c0a9 | 2008-10-04 21:33:08 +0000 | [diff] [blame] | 7597 | 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] | 7598 | 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] | 7599 | found, return S and two empty strings."); |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7600 | |
| 7601 | static PyObject* |
Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 7602 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7603 | { |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7604 | return PyUnicode_Partition((PyObject *)self, separator); |
| 7605 | } |
| 7606 | |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7607 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | dabb5f7 | 2010-01-25 11:46:11 +0000 | [diff] [blame] | 7608 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7609 | \n\ |
Benjamin Peterson | be2c0a9 | 2008-10-04 21:33:08 +0000 | [diff] [blame] | 7610 | 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] | 7611 | 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] | 7612 | separator is not found, return two empty strings and S."); |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7613 | |
| 7614 | static PyObject* |
| 7615 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 7616 | { |
| 7617 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 7618 | } |
| 7619 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7620 | PyObject *PyUnicode_RSplit(PyObject *s, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7621 | PyObject *sep, |
| 7622 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7623 | { |
| 7624 | PyObject *result; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7625 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7626 | s = PyUnicode_FromObject(s); |
| 7627 | if (s == NULL) |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7628 | return NULL; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7629 | if (sep != NULL) { |
| 7630 | sep = PyUnicode_FromObject(sep); |
| 7631 | if (sep == NULL) { |
| 7632 | Py_DECREF(s); |
| 7633 | return NULL; |
| 7634 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7635 | } |
| 7636 | |
| 7637 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7638 | |
| 7639 | Py_DECREF(s); |
| 7640 | Py_XDECREF(sep); |
| 7641 | return result; |
| 7642 | } |
| 7643 | |
| 7644 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7645 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7646 | \n\ |
| 7647 | Return a list of the words in S, using sep as the\n\ |
| 7648 | delimiter string, starting at the end of the string and\n\ |
| 7649 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 7650 | splits are done. If sep is not specified, any whitespace string\n\ |
| 7651 | is a separator."); |
| 7652 | |
| 7653 | static PyObject* |
| 7654 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 7655 | { |
| 7656 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7657 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7658 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7659 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7660 | return NULL; |
| 7661 | |
| 7662 | if (substring == Py_None) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7663 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7664 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7665 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7666 | else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7667 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7668 | } |
| 7669 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7670 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7671 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7672 | \n\ |
| 7673 | 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] | 7674 | 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] | 7675 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7676 | |
| 7677 | static PyObject* |
| 7678 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 7679 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7680 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7681 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7682 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7683 | return NULL; |
| 7684 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7685 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7686 | } |
| 7687 | |
| 7688 | static |
| 7689 | PyObject *unicode_str(PyUnicodeObject *self) |
| 7690 | { |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7691 | return PyUnicode_AsEncodedString((PyObject *)self, NULL, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7692 | } |
| 7693 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7694 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7695 | "S.swapcase() -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7696 | \n\ |
| 7697 | 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] | 7698 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7699 | |
| 7700 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7701 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7702 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7703 | return fixup(self, fixswapcase); |
| 7704 | } |
| 7705 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7706 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7707 | "S.translate(table) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7708 | \n\ |
| 7709 | Return a copy of the string S, where all characters have been mapped\n\ |
| 7710 | 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] | 7711 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ |
| 7712 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 7713 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7714 | |
| 7715 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7716 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7717 | { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7718 | return PyUnicode_TranslateCharmap(self->str, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7719 | self->length, |
| 7720 | table, |
| 7721 | "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7722 | } |
| 7723 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7724 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7725 | "S.upper() -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7726 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7727 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7728 | |
| 7729 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7730 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7731 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7732 | return fixup(self, fixupper); |
| 7733 | } |
| 7734 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7735 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7736 | "S.zfill(width) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7737 | \n\ |
Georg Brandl | 9806407 | 2008-09-09 19:26:00 +0000 | [diff] [blame] | 7738 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 7739 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7740 | |
| 7741 | static PyObject * |
| 7742 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 7743 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7744 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7745 | PyUnicodeObject *u; |
| 7746 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7747 | Py_ssize_t width; |
| 7748 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7749 | return NULL; |
| 7750 | |
| 7751 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 7752 | if (PyUnicode_CheckExact(self)) { |
| 7753 | Py_INCREF(self); |
| 7754 | return (PyObject*) self; |
| 7755 | } |
| 7756 | else |
| 7757 | return PyUnicode_FromUnicode( |
| 7758 | PyUnicode_AS_UNICODE(self), |
| 7759 | PyUnicode_GET_SIZE(self) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7760 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7761 | } |
| 7762 | |
| 7763 | fill = width - self->length; |
| 7764 | |
| 7765 | u = pad(self, fill, 0, '0'); |
| 7766 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 7767 | if (u == NULL) |
| 7768 | return NULL; |
| 7769 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7770 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 7771 | /* move sign to beginning of string */ |
| 7772 | u->str[0] = u->str[fill]; |
| 7773 | u->str[fill] = '0'; |
| 7774 | } |
| 7775 | |
| 7776 | return (PyObject*) u; |
| 7777 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7778 | |
| 7779 | #if 0 |
| 7780 | static PyObject* |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 7781 | free_listsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7782 | { |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 7783 | return PyInt_FromLong(numfree); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7784 | } |
| 7785 | #endif |
| 7786 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7787 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7788 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7789 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7790 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 7791 | With optional start, test S beginning at that position.\n\ |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7792 | With optional end, stop comparing S at that position.\n\ |
| 7793 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7794 | |
| 7795 | static PyObject * |
| 7796 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7797 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7798 | { |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7799 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7800 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7801 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7802 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7803 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7804 | |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7805 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7806 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 7807 | return NULL; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7808 | if (PyTuple_Check(subobj)) { |
| 7809 | Py_ssize_t i; |
| 7810 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 7811 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7812 | PyTuple_GET_ITEM(subobj, i)); |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7813 | if (substring == NULL) |
| 7814 | return NULL; |
| 7815 | result = tailmatch(self, substring, start, end, -1); |
| 7816 | Py_DECREF(substring); |
| 7817 | if (result) { |
| 7818 | Py_RETURN_TRUE; |
| 7819 | } |
| 7820 | } |
| 7821 | /* nothing matched */ |
| 7822 | Py_RETURN_FALSE; |
| 7823 | } |
| 7824 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7825 | if (substring == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7826 | return NULL; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7827 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7828 | Py_DECREF(substring); |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7829 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7830 | } |
| 7831 | |
| 7832 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7833 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7834 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7835 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7836 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 7837 | With optional start, test S beginning at that position.\n\ |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7838 | With optional end, stop comparing S at that position.\n\ |
| 7839 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7840 | |
| 7841 | static PyObject * |
| 7842 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7843 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7844 | { |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7845 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7846 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7847 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7848 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7849 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7850 | |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7851 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7852 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 7853 | return NULL; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7854 | if (PyTuple_Check(subobj)) { |
| 7855 | Py_ssize_t i; |
| 7856 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 7857 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7858 | PyTuple_GET_ITEM(subobj, i)); |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7859 | if (substring == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7860 | return NULL; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7861 | result = tailmatch(self, substring, start, end, +1); |
| 7862 | Py_DECREF(substring); |
| 7863 | if (result) { |
| 7864 | Py_RETURN_TRUE; |
| 7865 | } |
| 7866 | } |
| 7867 | Py_RETURN_FALSE; |
| 7868 | } |
| 7869 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7870 | if (substring == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7871 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7872 | |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7873 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7874 | Py_DECREF(substring); |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7875 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7876 | } |
| 7877 | |
| 7878 | |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 7879 | /* Implements do_string_format, which is unicode because of stringlib */ |
| 7880 | #include "stringlib/string_format.h" |
| 7881 | |
| 7882 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7883 | "S.format(*args, **kwargs) -> unicode\n\ |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 7884 | \n\ |
| 7885 | "); |
| 7886 | |
Eric Smith | dc13b79 | 2008-05-30 18:10:04 +0000 | [diff] [blame] | 7887 | static PyObject * |
| 7888 | unicode__format__(PyObject *self, PyObject *args) |
| 7889 | { |
| 7890 | PyObject *format_spec; |
| 7891 | PyObject *result = NULL; |
| 7892 | PyObject *tmp = NULL; |
| 7893 | |
| 7894 | /* If 2.x, convert format_spec to the same type as value */ |
| 7895 | /* This is to allow things like u''.format('') */ |
| 7896 | if (!PyArg_ParseTuple(args, "O:__format__", &format_spec)) |
| 7897 | goto done; |
| 7898 | if (!(PyBytes_Check(format_spec) || PyUnicode_Check(format_spec))) { |
| 7899 | PyErr_Format(PyExc_TypeError, "__format__ arg must be str " |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7900 | "or unicode, not %s", Py_TYPE(format_spec)->tp_name); |
Eric Smith | dc13b79 | 2008-05-30 18:10:04 +0000 | [diff] [blame] | 7901 | goto done; |
| 7902 | } |
| 7903 | tmp = PyObject_Unicode(format_spec); |
| 7904 | if (tmp == NULL) |
| 7905 | goto done; |
| 7906 | format_spec = tmp; |
| 7907 | |
| 7908 | result = _PyUnicode_FormatAdvanced(self, |
| 7909 | PyUnicode_AS_UNICODE(format_spec), |
| 7910 | PyUnicode_GET_SIZE(format_spec)); |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7911 | done: |
Eric Smith | dc13b79 | 2008-05-30 18:10:04 +0000 | [diff] [blame] | 7912 | Py_XDECREF(tmp); |
| 7913 | return result; |
| 7914 | } |
| 7915 | |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 7916 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7917 | "S.__format__(format_spec) -> unicode\n\ |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 7918 | \n\ |
| 7919 | "); |
| 7920 | |
Robert Schuppenies | 901c997 | 2008-06-10 10:10:31 +0000 | [diff] [blame] | 7921 | static PyObject * |
| 7922 | unicode__sizeof__(PyUnicodeObject *v) |
| 7923 | { |
Robert Schuppenies | 9be2ec1 | 2008-07-10 15:24:04 +0000 | [diff] [blame] | 7924 | return PyInt_FromSsize_t(sizeof(PyUnicodeObject) + |
| 7925 | sizeof(Py_UNICODE) * (v->length + 1)); |
Robert Schuppenies | 901c997 | 2008-06-10 10:10:31 +0000 | [diff] [blame] | 7926 | } |
| 7927 | |
| 7928 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 7929 | "S.__sizeof__() -> size of S in memory, in bytes\n\ |
Robert Schuppenies | 901c997 | 2008-06-10 10:10:31 +0000 | [diff] [blame] | 7930 | \n\ |
| 7931 | "); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 7932 | |
| 7933 | static PyObject * |
| 7934 | unicode_getnewargs(PyUnicodeObject *v) |
| 7935 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7936 | return Py_BuildValue("(u#)", v->str, v->length); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 7937 | } |
| 7938 | |
| 7939 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7940 | static PyMethodDef unicode_methods[] = { |
| 7941 | |
| 7942 | /* Order is according to common usage: often used methods should |
| 7943 | appear first, since lookup is done sequentially. */ |
| 7944 | |
Georg Brandl | ecdc0a9 | 2006-03-30 12:19:07 +0000 | [diff] [blame] | 7945 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7946 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 7947 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7948 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7949 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 7950 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 7951 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 7952 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 7953 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 7954 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 7955 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 7956 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7957 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 7958 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 7959 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7960 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7961 | {"decode", (PyCFunction) unicode_decode, METH_VARARGS, decode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7962 | /* {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */ |
| 7963 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 7964 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 7965 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7966 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7967 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7968 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7969 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7970 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 7971 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 7972 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 7973 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 7974 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 7975 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 7976 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 7977 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 7978 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 7979 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 7980 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 7981 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 7982 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 7983 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7984 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 7985 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
| 7986 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
| 7987 | {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
| 7988 | {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
Robert Schuppenies | 901c997 | 2008-06-10 10:10:31 +0000 | [diff] [blame] | 7989 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 7990 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7991 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7992 | #endif |
| 7993 | |
| 7994 | #if 0 |
| 7995 | /* This one is just used for debugging the implementation. */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 7996 | {"freelistsize", (PyCFunction) free_listsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7997 | #endif |
| 7998 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 7999 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8000 | {NULL, NULL} |
| 8001 | }; |
| 8002 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8003 | static PyObject * |
| 8004 | unicode_mod(PyObject *v, PyObject *w) |
| 8005 | { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8006 | if (!PyUnicode_Check(v)) { |
| 8007 | Py_INCREF(Py_NotImplemented); |
| 8008 | return Py_NotImplemented; |
| 8009 | } |
| 8010 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8011 | } |
| 8012 | |
| 8013 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8014 | 0, /*nb_add*/ |
| 8015 | 0, /*nb_subtract*/ |
| 8016 | 0, /*nb_multiply*/ |
| 8017 | 0, /*nb_divide*/ |
| 8018 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8019 | }; |
| 8020 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8021 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8022 | (lenfunc) unicode_length, /* sq_length */ |
| 8023 | PyUnicode_Concat, /* sq_concat */ |
| 8024 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 8025 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 8026 | (ssizessizeargfunc) unicode_slice, /* sq_slice */ |
| 8027 | 0, /* sq_ass_item */ |
| 8028 | 0, /* sq_ass_slice */ |
| 8029 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8030 | }; |
| 8031 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8032 | static PyObject* |
| 8033 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 8034 | { |
Marc-André Lemburg | 3a45779 | 2006-08-14 12:57:27 +0000 | [diff] [blame] | 8035 | if (PyIndex_Check(item)) { |
| 8036 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8037 | if (i == -1 && PyErr_Occurred()) |
| 8038 | return NULL; |
| 8039 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8040 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8041 | return unicode_getitem(self, i); |
| 8042 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8043 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8044 | Py_UNICODE* source_buf; |
| 8045 | Py_UNICODE* result_buf; |
| 8046 | PyObject* result; |
| 8047 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8048 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8049 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8050 | return NULL; |
| 8051 | } |
| 8052 | |
| 8053 | if (slicelength <= 0) { |
| 8054 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 8055 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 8056 | PyUnicode_CheckExact(self)) { |
| 8057 | Py_INCREF(self); |
| 8058 | return (PyObject *)self; |
| 8059 | } else if (step == 1) { |
| 8060 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8061 | } else { |
| 8062 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 8063 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 8064 | sizeof(Py_UNICODE)); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8065 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8066 | if (result_buf == NULL) |
| 8067 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8068 | |
| 8069 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 8070 | result_buf[i] = source_buf[cur]; |
| 8071 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8072 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8073 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 8074 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8075 | return result; |
| 8076 | } |
| 8077 | } else { |
| 8078 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 8079 | return NULL; |
| 8080 | } |
| 8081 | } |
| 8082 | |
| 8083 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8084 | (lenfunc)unicode_length, /* mp_length */ |
| 8085 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 8086 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8087 | }; |
| 8088 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8089 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8090 | unicode_buffer_getreadbuf(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8091 | Py_ssize_t index, |
| 8092 | const void **ptr) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8093 | { |
| 8094 | if (index != 0) { |
| 8095 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8096 | "accessing non-existent unicode segment"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8097 | return -1; |
| 8098 | } |
| 8099 | *ptr = (void *) self->str; |
| 8100 | return PyUnicode_GET_DATA_SIZE(self); |
| 8101 | } |
| 8102 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8103 | static Py_ssize_t |
| 8104 | unicode_buffer_getwritebuf(PyUnicodeObject *self, Py_ssize_t index, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8105 | const void **ptr) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8106 | { |
| 8107 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8108 | "cannot use unicode as modifiable buffer"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8109 | return -1; |
| 8110 | } |
| 8111 | |
| 8112 | static int |
| 8113 | unicode_buffer_getsegcount(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8114 | Py_ssize_t *lenp) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8115 | { |
| 8116 | if (lenp) |
| 8117 | *lenp = PyUnicode_GET_DATA_SIZE(self); |
| 8118 | return 1; |
| 8119 | } |
| 8120 | |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 8121 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8122 | unicode_buffer_getcharbuf(PyUnicodeObject *self, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8123 | Py_ssize_t index, |
| 8124 | const void **ptr) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8125 | { |
| 8126 | PyObject *str; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8127 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8128 | if (index != 0) { |
| 8129 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8130 | "accessing non-existent unicode segment"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8131 | return -1; |
| 8132 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 8133 | str = _PyUnicode_AsDefaultEncodedString((PyObject *)self, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8134 | if (str == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8135 | return -1; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 8136 | *ptr = (void *) PyString_AS_STRING(str); |
| 8137 | return PyString_GET_SIZE(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8138 | } |
| 8139 | |
| 8140 | /* Helpers for PyUnicode_Format() */ |
| 8141 | |
| 8142 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8143 | 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] | 8144 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8145 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8146 | if (argidx < arglen) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8147 | (*p_argidx)++; |
| 8148 | if (arglen < 0) |
| 8149 | return args; |
| 8150 | else |
| 8151 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8152 | } |
| 8153 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8154 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8155 | return NULL; |
| 8156 | } |
| 8157 | |
| 8158 | #define F_LJUST (1<<0) |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8159 | #define F_SIGN (1<<1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8160 | #define F_BLANK (1<<2) |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8161 | #define F_ALT (1<<3) |
| 8162 | #define F_ZERO (1<<4) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8163 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8164 | static Py_ssize_t |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8165 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8166 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8167 | register Py_ssize_t i; |
| 8168 | Py_ssize_t len = strlen(charbuffer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8169 | for (i = len - 1; i >= 0; i--) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8170 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8171 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8172 | return len; |
| 8173 | } |
| 8174 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8175 | static int |
| 8176 | doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) |
| 8177 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8178 | Py_ssize_t result; |
| 8179 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8180 | PyOS_ascii_formatd((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8181 | result = strtounicode(buffer, (char *)buffer); |
| 8182 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8183 | } |
| 8184 | |
| 8185 | static int |
| 8186 | longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) |
| 8187 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8188 | Py_ssize_t result; |
| 8189 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8190 | PyOS_snprintf((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8191 | result = strtounicode(buffer, (char *)buffer); |
| 8192 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8193 | } |
| 8194 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8195 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 8196 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 8197 | formatting is done. */ |
| 8198 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8199 | static int |
| 8200 | formatfloat(Py_UNICODE *buf, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8201 | size_t buflen, |
| 8202 | int flags, |
| 8203 | int prec, |
| 8204 | int type, |
| 8205 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8206 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8207 | /* fmt = '%#.' + `prec` + `type` |
| 8208 | 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] | 8209 | char fmt[20]; |
| 8210 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8211 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8212 | x = PyFloat_AsDouble(v); |
| 8213 | if (x == -1.0 && PyErr_Occurred()) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8214 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8215 | if (prec < 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8216 | prec = 6; |
Mark Dickinson | 75be68b | 2009-08-28 20:57:42 +0000 | [diff] [blame] | 8217 | #if SIZEOF_INT > 4 |
Mark Dickinson | db6fa18 | 2009-03-29 16:25:46 +0000 | [diff] [blame] | 8218 | /* make sure that the decimal representation of precision really does |
| 8219 | need at most 10 digits: platforms with sizeof(int) == 8 exist! */ |
Mark Dickinson | 75be68b | 2009-08-28 20:57:42 +0000 | [diff] [blame] | 8220 | if (prec > 0x7fffffff) { |
Mark Dickinson | db6fa18 | 2009-03-29 16:25:46 +0000 | [diff] [blame] | 8221 | PyErr_SetString(PyExc_OverflowError, |
| 8222 | "outrageously large precision " |
| 8223 | "for formatted float"); |
| 8224 | return -1; |
| 8225 | } |
Mark Dickinson | 75be68b | 2009-08-28 20:57:42 +0000 | [diff] [blame] | 8226 | #endif |
Mark Dickinson | db6fa18 | 2009-03-29 16:25:46 +0000 | [diff] [blame] | 8227 | |
Mark Dickinson | a30f349 | 2009-03-29 15:06:29 +0000 | [diff] [blame] | 8228 | if (type == 'f' && fabs(x) >= 1e50) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8229 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8230 | /* Worst case length calc to ensure no buffer overrun: |
| 8231 | |
| 8232 | 'g' formats: |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8233 | fmt = %#.<prec>g |
| 8234 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 8235 | for any double rep.) |
| 8236 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8237 | |
| 8238 | 'f' formats: |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8239 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 8240 | len = 1 + 50 + 1 + prec = 52 + prec |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8241 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8242 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8243 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8244 | |
| 8245 | */ |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8246 | if (((type == 'g' || type == 'G') && |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8247 | buflen <= (size_t)10 + (size_t)prec) || |
| 8248 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
| 8249 | PyErr_SetString(PyExc_OverflowError, |
| 8250 | "formatted float is too long (precision too large?)"); |
| 8251 | return -1; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8252 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8253 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8254 | (flags&F_ALT) ? "#" : "", |
| 8255 | prec, type); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8256 | return doubletounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8257 | } |
| 8258 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8259 | static PyObject* |
| 8260 | formatlong(PyObject *val, int flags, int prec, int type) |
| 8261 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8262 | char *buf; |
| 8263 | int i, len; |
| 8264 | PyObject *str; /* temporary string object. */ |
| 8265 | PyUnicodeObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8266 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8267 | str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
| 8268 | if (!str) |
| 8269 | return NULL; |
| 8270 | result = _PyUnicode_New(len); |
| 8271 | if (!result) { |
| 8272 | Py_DECREF(str); |
| 8273 | return NULL; |
| 8274 | } |
| 8275 | for (i = 0; i < len; i++) |
| 8276 | result->str[i] = buf[i]; |
| 8277 | result->str[len] = 0; |
| 8278 | Py_DECREF(str); |
| 8279 | return (PyObject*)result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8280 | } |
| 8281 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8282 | static int |
| 8283 | formatint(Py_UNICODE *buf, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8284 | size_t buflen, |
| 8285 | int flags, |
| 8286 | int prec, |
| 8287 | int type, |
| 8288 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8289 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8290 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8291 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 8292 | * + 1 + 1 |
| 8293 | * = 24 |
| 8294 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8295 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8296 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8297 | long x; |
| 8298 | |
| 8299 | x = PyInt_AsLong(v); |
| 8300 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8301 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8302 | if (x < 0 && type == 'u') { |
| 8303 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8304 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8305 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 8306 | sign = "-"; |
| 8307 | else |
| 8308 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8309 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8310 | prec = 1; |
| 8311 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8312 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 8313 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8314 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8315 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8316 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8317 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8318 | return -1; |
| 8319 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8320 | |
| 8321 | if ((flags & F_ALT) && |
| 8322 | (type == 'x' || type == 'X')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8323 | /* When converting under %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8324 | * of issues that cause pain: |
| 8325 | * - when 0 is being converted, the C standard leaves off |
| 8326 | * the '0x' or '0X', which is inconsistent with other |
| 8327 | * %#x/%#X conversions and inconsistent with Python's |
| 8328 | * hex() function |
| 8329 | * - there are platforms that violate the standard and |
| 8330 | * convert 0 with the '0x' or '0X' |
| 8331 | * (Metrowerks, Compaq Tru64) |
| 8332 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8333 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8334 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8335 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8336 | * We can achieve the desired consistency by inserting our |
| 8337 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 8338 | * of %#x/%#X. |
| 8339 | * |
| 8340 | * Note that this is the same approach as used in |
| 8341 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8342 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8343 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 8344 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8345 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8346 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8347 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 8348 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8349 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 8350 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8351 | if (sign[0]) |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8352 | return longtounicode(buf, buflen, fmt, -x); |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8353 | else |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8354 | return longtounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8355 | } |
| 8356 | |
| 8357 | static int |
| 8358 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8359 | size_t buflen, |
| 8360 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8361 | { |
Ezio Melotti | 85ddea7 | 2010-02-25 17:51:33 +0000 | [diff] [blame] | 8362 | PyObject *unistr; |
| 8363 | char *str; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8364 | /* presume that the buffer is at least 2 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8365 | if (PyUnicode_Check(v)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8366 | if (PyUnicode_GET_SIZE(v) != 1) |
| 8367 | goto onError; |
| 8368 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8369 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8370 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 8371 | else if (PyString_Check(v)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8372 | if (PyString_GET_SIZE(v) != 1) |
| 8373 | goto onError; |
Ezio Melotti | 85ddea7 | 2010-02-25 17:51:33 +0000 | [diff] [blame] | 8374 | /* #7649: "u'%c' % char" should behave like "u'%s' % char" and fail |
| 8375 | with a UnicodeDecodeError if 'char' is not decodable with the |
| 8376 | default encoding (usually ASCII, but it might be something else) */ |
| 8377 | str = PyString_AS_STRING(v); |
| 8378 | if ((unsigned char)str[0] > 0x7F) { |
| 8379 | /* the char is not ASCII; try to decode the string using the |
| 8380 | default encoding and return -1 to let the UnicodeDecodeError |
| 8381 | be raised if the string can't be decoded */ |
| 8382 | unistr = PyUnicode_Decode(str, 1, NULL, "strict"); |
| 8383 | if (unistr == NULL) |
| 8384 | return -1; |
| 8385 | buf[0] = PyUnicode_AS_UNICODE(unistr)[0]; |
| 8386 | Py_DECREF(unistr); |
| 8387 | } |
| 8388 | else |
| 8389 | buf[0] = (Py_UNICODE)str[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8390 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8391 | |
| 8392 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8393 | /* Integer input truncated to a character */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8394 | long x; |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8395 | x = PyInt_AsLong(v); |
| 8396 | if (x == -1 && PyErr_Occurred()) |
| 8397 | goto onError; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8398 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8399 | if (x < 0 || x > 0x10ffff) { |
| 8400 | PyErr_SetString(PyExc_OverflowError, |
| 8401 | "%c arg not in range(0x110000) " |
| 8402 | "(wide Python build)"); |
| 8403 | return -1; |
| 8404 | } |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8405 | #else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8406 | if (x < 0 || x > 0xffff) { |
| 8407 | PyErr_SetString(PyExc_OverflowError, |
| 8408 | "%c arg not in range(0x10000) " |
| 8409 | "(narrow Python build)"); |
| 8410 | return -1; |
| 8411 | } |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8412 | #endif |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8413 | buf[0] = (Py_UNICODE) x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8414 | } |
| 8415 | buf[1] = '\0'; |
| 8416 | return 1; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8417 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8418 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8419 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8420 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8421 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8422 | } |
| 8423 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8424 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 8425 | |
| 8426 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 8427 | chars are formatted. XXX This is a magic number. Each formatting |
| 8428 | routine does bounds checking to ensure no overflow, but a better |
| 8429 | solution may be to malloc a buffer of appropriate size for each |
| 8430 | format. For now, the current solution is sufficient. |
| 8431 | */ |
| 8432 | #define FORMATBUFLEN (size_t)120 |
| 8433 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8434 | PyObject *PyUnicode_Format(PyObject *format, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8435 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8436 | { |
| 8437 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8438 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8439 | int args_owned = 0; |
| 8440 | PyUnicodeObject *result = NULL; |
| 8441 | PyObject *dict = NULL; |
| 8442 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8443 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8444 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8445 | PyErr_BadInternalCall(); |
| 8446 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8447 | } |
| 8448 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8449 | if (uformat == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8450 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8451 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 8452 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 8453 | |
| 8454 | reslen = rescnt = fmtcnt + 100; |
| 8455 | result = _PyUnicode_New(reslen); |
| 8456 | if (result == NULL) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8457 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8458 | res = PyUnicode_AS_UNICODE(result); |
| 8459 | |
| 8460 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8461 | arglen = PyTuple_Size(args); |
| 8462 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8463 | } |
| 8464 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8465 | arglen = -1; |
| 8466 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8467 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 8468 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 8469 | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8470 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8471 | |
| 8472 | while (--fmtcnt >= 0) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8473 | if (*fmt != '%') { |
| 8474 | if (--rescnt < 0) { |
| 8475 | rescnt = fmtcnt + 100; |
| 8476 | reslen += rescnt; |
| 8477 | if (_PyUnicode_Resize(&result, reslen) < 0) |
| 8478 | goto onError; |
| 8479 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 8480 | --rescnt; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8481 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8482 | *res++ = *fmt++; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8483 | } |
| 8484 | else { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8485 | /* Got a format specifier */ |
| 8486 | int flags = 0; |
| 8487 | Py_ssize_t width = -1; |
| 8488 | int prec = -1; |
| 8489 | Py_UNICODE c = '\0'; |
| 8490 | Py_UNICODE fill; |
| 8491 | int isnumok; |
| 8492 | PyObject *v = NULL; |
| 8493 | PyObject *temp = NULL; |
| 8494 | Py_UNICODE *pbuf; |
| 8495 | Py_UNICODE sign; |
| 8496 | Py_ssize_t len; |
| 8497 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
| 8498 | |
| 8499 | fmt++; |
| 8500 | if (*fmt == '(') { |
| 8501 | Py_UNICODE *keystart; |
| 8502 | Py_ssize_t keylen; |
| 8503 | PyObject *key; |
| 8504 | int pcount = 1; |
| 8505 | |
| 8506 | if (dict == NULL) { |
| 8507 | PyErr_SetString(PyExc_TypeError, |
| 8508 | "format requires a mapping"); |
| 8509 | goto onError; |
| 8510 | } |
| 8511 | ++fmt; |
| 8512 | --fmtcnt; |
| 8513 | keystart = fmt; |
| 8514 | /* Skip over balanced parentheses */ |
| 8515 | while (pcount > 0 && --fmtcnt >= 0) { |
| 8516 | if (*fmt == ')') |
| 8517 | --pcount; |
| 8518 | else if (*fmt == '(') |
| 8519 | ++pcount; |
| 8520 | fmt++; |
| 8521 | } |
| 8522 | keylen = fmt - keystart - 1; |
| 8523 | if (fmtcnt < 0 || pcount > 0) { |
| 8524 | PyErr_SetString(PyExc_ValueError, |
| 8525 | "incomplete format key"); |
| 8526 | goto onError; |
| 8527 | } |
| 8528 | #if 0 |
| 8529 | /* keys are converted to strings using UTF-8 and |
| 8530 | then looked up since Python uses strings to hold |
| 8531 | variables names etc. in its namespaces and we |
| 8532 | wouldn't want to break common idioms. */ |
| 8533 | key = PyUnicode_EncodeUTF8(keystart, |
| 8534 | keylen, |
| 8535 | NULL); |
| 8536 | #else |
| 8537 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 8538 | #endif |
| 8539 | if (key == NULL) |
| 8540 | goto onError; |
| 8541 | if (args_owned) { |
| 8542 | Py_DECREF(args); |
| 8543 | args_owned = 0; |
| 8544 | } |
| 8545 | args = PyObject_GetItem(dict, key); |
| 8546 | Py_DECREF(key); |
| 8547 | if (args == NULL) { |
| 8548 | goto onError; |
| 8549 | } |
| 8550 | args_owned = 1; |
| 8551 | arglen = -1; |
| 8552 | argidx = -2; |
| 8553 | } |
| 8554 | while (--fmtcnt >= 0) { |
| 8555 | switch (c = *fmt++) { |
| 8556 | case '-': flags |= F_LJUST; continue; |
| 8557 | case '+': flags |= F_SIGN; continue; |
| 8558 | case ' ': flags |= F_BLANK; continue; |
| 8559 | case '#': flags |= F_ALT; continue; |
| 8560 | case '0': flags |= F_ZERO; continue; |
| 8561 | } |
| 8562 | break; |
| 8563 | } |
| 8564 | if (c == '*') { |
| 8565 | v = getnextarg(args, arglen, &argidx); |
| 8566 | if (v == NULL) |
| 8567 | goto onError; |
| 8568 | if (!PyInt_Check(v)) { |
| 8569 | PyErr_SetString(PyExc_TypeError, |
| 8570 | "* wants int"); |
| 8571 | goto onError; |
| 8572 | } |
| 8573 | width = PyInt_AsLong(v); |
| 8574 | if (width < 0) { |
| 8575 | flags |= F_LJUST; |
| 8576 | width = -width; |
| 8577 | } |
| 8578 | if (--fmtcnt >= 0) |
| 8579 | c = *fmt++; |
| 8580 | } |
| 8581 | else if (c >= '0' && c <= '9') { |
| 8582 | width = c - '0'; |
| 8583 | while (--fmtcnt >= 0) { |
| 8584 | c = *fmt++; |
| 8585 | if (c < '0' || c > '9') |
| 8586 | break; |
| 8587 | if ((width*10) / 10 != width) { |
| 8588 | PyErr_SetString(PyExc_ValueError, |
| 8589 | "width too big"); |
| 8590 | goto onError; |
| 8591 | } |
| 8592 | width = width*10 + (c - '0'); |
| 8593 | } |
| 8594 | } |
| 8595 | if (c == '.') { |
| 8596 | prec = 0; |
| 8597 | if (--fmtcnt >= 0) |
| 8598 | c = *fmt++; |
| 8599 | if (c == '*') { |
| 8600 | v = getnextarg(args, arglen, &argidx); |
| 8601 | if (v == NULL) |
| 8602 | goto onError; |
| 8603 | if (!PyInt_Check(v)) { |
| 8604 | PyErr_SetString(PyExc_TypeError, |
| 8605 | "* wants int"); |
| 8606 | goto onError; |
| 8607 | } |
| 8608 | prec = PyInt_AsLong(v); |
| 8609 | if (prec < 0) |
| 8610 | prec = 0; |
| 8611 | if (--fmtcnt >= 0) |
| 8612 | c = *fmt++; |
| 8613 | } |
| 8614 | else if (c >= '0' && c <= '9') { |
| 8615 | prec = c - '0'; |
| 8616 | while (--fmtcnt >= 0) { |
| 8617 | c = Py_CHARMASK(*fmt++); |
| 8618 | if (c < '0' || c > '9') |
| 8619 | break; |
| 8620 | if ((prec*10) / 10 != prec) { |
| 8621 | PyErr_SetString(PyExc_ValueError, |
| 8622 | "prec too big"); |
| 8623 | goto onError; |
| 8624 | } |
| 8625 | prec = prec*10 + (c - '0'); |
| 8626 | } |
| 8627 | } |
| 8628 | } /* prec */ |
| 8629 | if (fmtcnt >= 0) { |
| 8630 | if (c == 'h' || c == 'l' || c == 'L') { |
| 8631 | if (--fmtcnt >= 0) |
| 8632 | c = *fmt++; |
| 8633 | } |
| 8634 | } |
| 8635 | if (fmtcnt < 0) { |
| 8636 | PyErr_SetString(PyExc_ValueError, |
| 8637 | "incomplete format"); |
| 8638 | goto onError; |
| 8639 | } |
| 8640 | if (c != '%') { |
| 8641 | v = getnextarg(args, arglen, &argidx); |
| 8642 | if (v == NULL) |
| 8643 | goto onError; |
| 8644 | } |
| 8645 | sign = 0; |
| 8646 | fill = ' '; |
| 8647 | switch (c) { |
| 8648 | |
| 8649 | case '%': |
| 8650 | pbuf = formatbuf; |
| 8651 | /* presume that buffer length is at least 1 */ |
| 8652 | pbuf[0] = '%'; |
| 8653 | len = 1; |
| 8654 | break; |
| 8655 | |
| 8656 | case 's': |
| 8657 | case 'r': |
Victor Stinner | 4fd2ff9 | 2010-03-22 12:56:39 +0000 | [diff] [blame] | 8658 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8659 | temp = v; |
| 8660 | Py_INCREF(temp); |
| 8661 | } |
| 8662 | else { |
| 8663 | PyObject *unicode; |
| 8664 | if (c == 's') |
| 8665 | temp = PyObject_Unicode(v); |
| 8666 | else |
| 8667 | temp = PyObject_Repr(v); |
| 8668 | if (temp == NULL) |
| 8669 | goto onError; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8670 | if (PyUnicode_Check(temp)) |
| 8671 | /* nothing to do */; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 8672 | else if (PyString_Check(temp)) { |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8673 | /* convert to string to Unicode */ |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8674 | unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
| 8675 | PyString_GET_SIZE(temp), |
| 8676 | NULL, |
| 8677 | "strict"); |
| 8678 | Py_DECREF(temp); |
| 8679 | temp = unicode; |
| 8680 | if (temp == NULL) |
| 8681 | goto onError; |
| 8682 | } |
| 8683 | else { |
| 8684 | Py_DECREF(temp); |
| 8685 | PyErr_SetString(PyExc_TypeError, |
| 8686 | "%s argument has non-string str()"); |
| 8687 | goto onError; |
| 8688 | } |
| 8689 | } |
| 8690 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 8691 | len = PyUnicode_GET_SIZE(temp); |
| 8692 | if (prec >= 0 && len > prec) |
| 8693 | len = prec; |
| 8694 | break; |
| 8695 | |
| 8696 | case 'i': |
| 8697 | case 'd': |
| 8698 | case 'u': |
| 8699 | case 'o': |
| 8700 | case 'x': |
| 8701 | case 'X': |
| 8702 | if (c == 'i') |
| 8703 | c = 'd'; |
| 8704 | isnumok = 0; |
| 8705 | if (PyNumber_Check(v)) { |
| 8706 | PyObject *iobj=NULL; |
| 8707 | |
| 8708 | if (PyInt_Check(v) || (PyLong_Check(v))) { |
| 8709 | iobj = v; |
| 8710 | Py_INCREF(iobj); |
| 8711 | } |
| 8712 | else { |
| 8713 | iobj = PyNumber_Int(v); |
| 8714 | if (iobj==NULL) iobj = PyNumber_Long(v); |
| 8715 | } |
| 8716 | if (iobj!=NULL) { |
| 8717 | if (PyInt_Check(iobj)) { |
| 8718 | isnumok = 1; |
| 8719 | pbuf = formatbuf; |
| 8720 | len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8721 | flags, prec, c, iobj); |
| 8722 | Py_DECREF(iobj); |
| 8723 | if (len < 0) |
| 8724 | goto onError; |
| 8725 | sign = 1; |
| 8726 | } |
| 8727 | else if (PyLong_Check(iobj)) { |
| 8728 | isnumok = 1; |
| 8729 | temp = formatlong(iobj, flags, prec, c); |
| 8730 | Py_DECREF(iobj); |
| 8731 | if (!temp) |
| 8732 | goto onError; |
| 8733 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 8734 | len = PyUnicode_GET_SIZE(temp); |
| 8735 | sign = 1; |
| 8736 | } |
| 8737 | else { |
| 8738 | Py_DECREF(iobj); |
| 8739 | } |
| 8740 | } |
| 8741 | } |
| 8742 | if (!isnumok) { |
| 8743 | PyErr_Format(PyExc_TypeError, |
| 8744 | "%%%c format: a number is required, " |
| 8745 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 8746 | goto onError; |
| 8747 | } |
| 8748 | if (flags & F_ZERO) |
| 8749 | fill = '0'; |
| 8750 | break; |
| 8751 | |
| 8752 | case 'e': |
| 8753 | case 'E': |
| 8754 | case 'f': |
| 8755 | case 'F': |
| 8756 | case 'g': |
| 8757 | case 'G': |
| 8758 | if (c == 'F') |
| 8759 | c = 'f'; |
| 8760 | pbuf = formatbuf; |
| 8761 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8762 | flags, prec, c, v); |
| 8763 | if (len < 0) |
| 8764 | goto onError; |
| 8765 | sign = 1; |
| 8766 | if (flags & F_ZERO) |
| 8767 | fill = '0'; |
| 8768 | break; |
| 8769 | |
| 8770 | case 'c': |
| 8771 | pbuf = formatbuf; |
| 8772 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
| 8773 | if (len < 0) |
| 8774 | goto onError; |
| 8775 | break; |
| 8776 | |
| 8777 | default: |
| 8778 | PyErr_Format(PyExc_ValueError, |
| 8779 | "unsupported format character '%c' (0x%x) " |
| 8780 | "at index %zd", |
| 8781 | (31<=c && c<=126) ? (char)c : '?', |
| 8782 | (int)c, |
| 8783 | (Py_ssize_t)(fmt - 1 - |
| 8784 | PyUnicode_AS_UNICODE(uformat))); |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8785 | goto onError; |
| 8786 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8787 | if (sign) { |
| 8788 | if (*pbuf == '-' || *pbuf == '+') { |
| 8789 | sign = *pbuf++; |
| 8790 | len--; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8791 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8792 | else if (flags & F_SIGN) |
| 8793 | sign = '+'; |
| 8794 | else if (flags & F_BLANK) |
| 8795 | sign = ' '; |
| 8796 | else |
| 8797 | sign = 0; |
| 8798 | } |
| 8799 | if (width < len) |
| 8800 | width = len; |
| 8801 | if (rescnt - (sign != 0) < width) { |
| 8802 | reslen -= rescnt; |
| 8803 | rescnt = width + fmtcnt + 100; |
| 8804 | reslen += rescnt; |
| 8805 | if (reslen < 0) { |
| 8806 | Py_XDECREF(temp); |
| 8807 | PyErr_NoMemory(); |
| 8808 | goto onError; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8809 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8810 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 8811 | Py_XDECREF(temp); |
| 8812 | goto onError; |
| 8813 | } |
| 8814 | res = PyUnicode_AS_UNICODE(result) |
| 8815 | + reslen - rescnt; |
| 8816 | } |
| 8817 | if (sign) { |
| 8818 | if (fill != ' ') |
| 8819 | *res++ = sign; |
| 8820 | rescnt--; |
| 8821 | if (width > len) |
| 8822 | width--; |
| 8823 | } |
| 8824 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 8825 | assert(pbuf[0] == '0'); |
| 8826 | assert(pbuf[1] == c); |
| 8827 | if (fill != ' ') { |
| 8828 | *res++ = *pbuf++; |
| 8829 | *res++ = *pbuf++; |
| 8830 | } |
| 8831 | rescnt -= 2; |
| 8832 | width -= 2; |
| 8833 | if (width < 0) |
| 8834 | width = 0; |
| 8835 | len -= 2; |
| 8836 | } |
| 8837 | if (width > len && !(flags & F_LJUST)) { |
| 8838 | do { |
| 8839 | --rescnt; |
| 8840 | *res++ = fill; |
| 8841 | } while (--width > len); |
| 8842 | } |
| 8843 | if (fill == ' ') { |
| 8844 | if (sign) |
| 8845 | *res++ = sign; |
| 8846 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 8847 | assert(pbuf[0] == '0'); |
| 8848 | assert(pbuf[1] == c); |
| 8849 | *res++ = *pbuf++; |
| 8850 | *res++ = *pbuf++; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8851 | } |
| 8852 | } |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8853 | Py_UNICODE_COPY(res, pbuf, len); |
| 8854 | res += len; |
| 8855 | rescnt -= len; |
| 8856 | while (--width >= len) { |
| 8857 | --rescnt; |
| 8858 | *res++ = ' '; |
| 8859 | } |
| 8860 | if (dict && (argidx < arglen) && c != '%') { |
| 8861 | PyErr_SetString(PyExc_TypeError, |
| 8862 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8863 | Py_XDECREF(temp); |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8864 | goto onError; |
| 8865 | } |
| 8866 | Py_XDECREF(temp); |
| 8867 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8868 | } /* until end */ |
| 8869 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8870 | PyErr_SetString(PyExc_TypeError, |
| 8871 | "not all arguments converted during string formatting"); |
| 8872 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8873 | } |
| 8874 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8875 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8876 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 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 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8881 | return (PyObject *)result; |
| 8882 | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8883 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8884 | Py_XDECREF(result); |
| 8885 | Py_DECREF(uformat); |
| 8886 | if (args_owned) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8887 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8888 | } |
| 8889 | return NULL; |
| 8890 | } |
| 8891 | |
| 8892 | static PyBufferProcs unicode_as_buffer = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8893 | (readbufferproc) unicode_buffer_getreadbuf, |
| 8894 | (writebufferproc) unicode_buffer_getwritebuf, |
| 8895 | (segcountproc) unicode_buffer_getsegcount, |
| 8896 | (charbufferproc) unicode_buffer_getcharbuf, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8897 | }; |
| 8898 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 8899 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8900 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 8901 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8902 | static PyObject * |
| 8903 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8904 | { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8905 | PyObject *x = NULL; |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8906 | static char *kwlist[] = {"string", "encoding", "errors", 0}; |
| 8907 | char *encoding = NULL; |
| 8908 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8909 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8910 | if (type != &PyUnicode_Type) |
| 8911 | return unicode_subtype_new(type, args, kwds); |
| 8912 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8913 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8914 | return NULL; |
| 8915 | if (x == NULL) |
| 8916 | return (PyObject *)_PyUnicode_New(0); |
| 8917 | if (encoding == NULL && errors == NULL) |
| 8918 | return PyObject_Unicode(x); |
| 8919 | else |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8920 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8921 | } |
| 8922 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8923 | static PyObject * |
| 8924 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8925 | { |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8926 | PyUnicodeObject *tmp, *pnew; |
| 8927 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8928 | |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8929 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 8930 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 8931 | if (tmp == NULL) |
| 8932 | return NULL; |
| 8933 | assert(PyUnicode_Check(tmp)); |
| 8934 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
| 8935 | if (pnew == NULL) { |
| 8936 | Py_DECREF(tmp); |
| 8937 | return NULL; |
| 8938 | } |
| 8939 | pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
| 8940 | if (pnew->str == NULL) { |
| 8941 | _Py_ForgetReference((PyObject *)pnew); |
| 8942 | PyObject_Del(pnew); |
| 8943 | Py_DECREF(tmp); |
| 8944 | return PyErr_NoMemory(); |
| 8945 | } |
| 8946 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 8947 | pnew->length = n; |
| 8948 | pnew->hash = tmp->hash; |
| 8949 | Py_DECREF(tmp); |
| 8950 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8951 | } |
| 8952 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8953 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8954 | "unicode(string [, encoding[, errors]]) -> object\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8955 | \n\ |
| 8956 | Create a new Unicode object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 8957 | encoding defaults to the current default string encoding.\n\ |
| 8958 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8959 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8960 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 8961 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8962 | "unicode", /* tp_name */ |
| 8963 | sizeof(PyUnicodeObject), /* tp_size */ |
| 8964 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8965 | /* Slots */ |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8966 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 8967 | 0, /* tp_print */ |
| 8968 | 0, /* tp_getattr */ |
| 8969 | 0, /* tp_setattr */ |
| 8970 | 0, /* tp_compare */ |
| 8971 | unicode_repr, /* tp_repr */ |
| 8972 | &unicode_as_number, /* tp_as_number */ |
| 8973 | &unicode_as_sequence, /* tp_as_sequence */ |
| 8974 | &unicode_as_mapping, /* tp_as_mapping */ |
| 8975 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 8976 | 0, /* tp_call*/ |
| 8977 | (reprfunc) unicode_str, /* tp_str */ |
| 8978 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 8979 | 0, /* tp_setattro */ |
| 8980 | &unicode_as_buffer, /* tp_as_buffer */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8981 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 8982 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 186d9b3 | 2009-01-31 16:34:44 +0000 | [diff] [blame] | 8983 | unicode_doc, /* tp_doc */ |
| 8984 | 0, /* tp_traverse */ |
| 8985 | 0, /* tp_clear */ |
| 8986 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 8987 | 0, /* tp_weaklistoffset */ |
| 8988 | 0, /* tp_iter */ |
| 8989 | 0, /* tp_iternext */ |
| 8990 | unicode_methods, /* tp_methods */ |
| 8991 | 0, /* tp_members */ |
| 8992 | 0, /* tp_getset */ |
| 8993 | &PyBaseString_Type, /* tp_base */ |
| 8994 | 0, /* tp_dict */ |
| 8995 | 0, /* tp_descr_get */ |
| 8996 | 0, /* tp_descr_set */ |
| 8997 | 0, /* tp_dictoffset */ |
| 8998 | 0, /* tp_init */ |
| 8999 | 0, /* tp_alloc */ |
| 9000 | unicode_new, /* tp_new */ |
| 9001 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9002 | }; |
| 9003 | |
| 9004 | /* Initialize the Unicode implementation */ |
| 9005 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9006 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9007 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9008 | int i; |
| 9009 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 9010 | /* XXX - move this array to unicodectype.c ? */ |
| 9011 | Py_UNICODE linebreak[] = { |
| 9012 | 0x000A, /* LINE FEED */ |
| 9013 | 0x000D, /* CARRIAGE RETURN */ |
| 9014 | 0x001C, /* FILE SEPARATOR */ |
| 9015 | 0x001D, /* GROUP SEPARATOR */ |
| 9016 | 0x001E, /* RECORD SEPARATOR */ |
| 9017 | 0x0085, /* NEXT LINE */ |
| 9018 | 0x2028, /* LINE SEPARATOR */ |
| 9019 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 9020 | }; |
| 9021 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9022 | /* Init the implementation */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 9023 | free_list = NULL; |
| 9024 | numfree = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9025 | unicode_empty = _PyUnicode_New(0); |
Neal Norwitz | e1fdb32 | 2006-07-21 05:32:28 +0000 | [diff] [blame] | 9026 | if (!unicode_empty) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 9027 | return; |
Neal Norwitz | e1fdb32 | 2006-07-21 05:32:28 +0000 | [diff] [blame] | 9028 | |
Marc-André Lemburg | 90e8147 | 2000-06-07 09:13:21 +0000 | [diff] [blame] | 9029 | strcpy(unicode_default_encoding, "ascii"); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9030 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 9031 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 9032 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 9033 | Py_FatalError("Can't initialize 'unicode'"); |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 9034 | |
| 9035 | /* initialize the linebreak bloom filter */ |
| 9036 | bloom_linebreak = make_bloom_mask( |
| 9037 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 9038 | ); |
Neal Norwitz | de4c78a | 2006-06-13 08:28:19 +0000 | [diff] [blame] | 9039 | |
| 9040 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9041 | } |
| 9042 | |
| 9043 | /* Finalize the Unicode implementation */ |
| 9044 | |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 9045 | int |
| 9046 | PyUnicode_ClearFreeList(void) |
| 9047 | { |
| 9048 | int freelist_size = numfree; |
| 9049 | PyUnicodeObject *u; |
| 9050 | |
| 9051 | for (u = free_list; u != NULL;) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 9052 | PyUnicodeObject *v = u; |
| 9053 | u = *(PyUnicodeObject **)u; |
| 9054 | if (v->str) |
| 9055 | PyObject_DEL(v->str); |
| 9056 | Py_XDECREF(v->defenc); |
| 9057 | PyObject_Del(v); |
| 9058 | numfree--; |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 9059 | } |
| 9060 | free_list = NULL; |
| 9061 | assert(numfree == 0); |
| 9062 | return freelist_size; |
| 9063 | } |
| 9064 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9065 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9066 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9067 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9068 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9069 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 9070 | Py_XDECREF(unicode_empty); |
| 9071 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9072 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9073 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 9074 | if (unicode_latin1[i]) { |
| 9075 | Py_DECREF(unicode_latin1[i]); |
| 9076 | unicode_latin1[i] = NULL; |
| 9077 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9078 | } |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 9079 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9080 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9081 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 9082 | #ifdef __cplusplus |
| 9083 | } |
| 9084 | #endif |
| 9085 | |
| 9086 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9087 | /* |
Benjamin Peterson | 339f8c6 | 2009-01-31 22:25:08 +0000 | [diff] [blame] | 9088 | Local variables: |
| 9089 | c-basic-offset: 4 |
| 9090 | indent-tabs-mode: nil |
| 9091 | End: |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9092 | */ |