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 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 15 | Copyright (c) 1999 by Secret Labs AB |
| 16 | Copyright (c) 1999 by Fredrik Lundh |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 17 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 18 | By obtaining, using, and/or copying this software and/or its |
| 19 | associated documentation, you agree that you have read, understood, |
| 20 | and will comply with the following terms and conditions: |
| 21 | |
| 22 | Permission to use, copy, modify, and distribute this software and its |
| 23 | associated documentation for any purpose and without fee is hereby |
| 24 | granted, provided that the above copyright notice appears in all |
| 25 | copies, and that both that copyright notice and this permission notice |
| 26 | appear in supporting documentation, and that the name of Secret Labs |
| 27 | AB or the author not be used in advertising or publicity pertaining to |
| 28 | distribution of the software without specific, written prior |
| 29 | permission. |
| 30 | |
| 31 | SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 32 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 33 | FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR |
| 34 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 35 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 36 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 37 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 38 | -------------------------------------------------------------------- |
| 39 | |
| 40 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 41 | |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 42 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 43 | #include "Python.h" |
| 44 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 45 | #include "unicodeobject.h" |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 46 | #include "ucnhash.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 47 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 48 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 49 | #include <windows.h> |
| 50 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 52 | /* Limit for the Unicode object free list */ |
| 53 | |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 54 | #define PyUnicode_MAXFREELIST 1024 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 55 | |
| 56 | /* Limit for the Unicode object free list stay alive optimization. |
| 57 | |
| 58 | The implementation will keep allocated Unicode memory intact for |
| 59 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 60 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 61 | |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 62 | At worst this will result in PyUnicode_MAXFREELIST * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 63 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 64 | malloc()-overhead) bytes of unused garbage. |
| 65 | |
| 66 | Setting the limit to 0 effectively turns the feature off. |
| 67 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 68 | Note: This is an experimental feature ! If you get core dumps when |
| 69 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 70 | |
| 71 | */ |
| 72 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 73 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 74 | |
| 75 | /* Endianness switches; defaults to little endian */ |
| 76 | |
| 77 | #ifdef WORDS_BIGENDIAN |
| 78 | # define BYTEORDER_IS_BIG_ENDIAN |
| 79 | #else |
| 80 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 81 | #endif |
| 82 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 83 | /* --- Globals ------------------------------------------------------------ |
| 84 | |
| 85 | The globals are initialized by the _PyUnicode_Init() API and should |
| 86 | not be used before calling that API. |
| 87 | |
| 88 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 89 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 90 | |
| 91 | #ifdef __cplusplus |
| 92 | extern "C" { |
| 93 | #endif |
| 94 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 95 | /* Free list for Unicode objects */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 96 | static PyUnicodeObject *free_list; |
| 97 | static int numfree; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 98 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 99 | /* The empty Unicode object is shared to improve performance. */ |
| 100 | static PyUnicodeObject *unicode_empty; |
| 101 | |
| 102 | /* Single character Unicode strings in the Latin-1 range are being |
| 103 | shared as well. */ |
| 104 | static PyUnicodeObject *unicode_latin1[256]; |
| 105 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 106 | /* Default encoding to use and assume when NULL is passed as encoding |
| 107 | parameter; it is initialized by _PyUnicode_Init(). |
| 108 | |
| 109 | Always use the PyUnicode_SetDefaultEncoding() and |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 110 | PyUnicode_GetDefaultEncoding() APIs to access this global. |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 111 | |
| 112 | */ |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 113 | static char unicode_default_encoding[100]; |
| 114 | |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 115 | /* Fast detection of the most frequent whitespace characters */ |
| 116 | const unsigned char _Py_ascii_whitespace[] = { |
| 117 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 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 */ |
| 123 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 124 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 125 | // case 0x001C: /* FILE SEPARATOR */ |
| 126 | // case 0x001D: /* GROUP SEPARATOR */ |
| 127 | // case 0x001E: /* RECORD SEPARATOR */ |
| 128 | // case 0x001F: /* UNIT SEPARATOR */ |
| 129 | 0, 0, 0, 0, 1, 1, 1, 1, |
| 130 | // case 0x0020: /* SPACE */ |
| 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, |
| 135 | |
| 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 |
| 144 | }; |
| 145 | |
| 146 | /* Same for linebreaks */ |
| 147 | static unsigned char ascii_linebreak[] = { |
| 148 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 149 | // 0x000A, /* LINE FEED */ |
| 150 | // 0x000D, /* CARRIAGE RETURN */ |
| 151 | 0, 0, 1, 0, 0, 1, 0, 0, |
| 152 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 153 | // 0x001C, /* FILE SEPARATOR */ |
| 154 | // 0x001D, /* GROUP SEPARATOR */ |
| 155 | // 0x001E, /* RECORD SEPARATOR */ |
| 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, |
| 161 | |
| 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 |
| 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 |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 177 | return 0x10FFFF; |
| 178 | #else |
| 179 | /* This is actually an illegal character, so it should |
| 180 | not be passed to unichr. */ |
| 181 | return 0xFFFF; |
| 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 | |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +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 | |
| 228 | #define BLOOM_MEMBER(mask, chr, set, setlen)\ |
| 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, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +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) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +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 | |
Martin v. Löwis | 80d2e59 | 2006-04-13 06:06:08 +0000 | [diff] [blame] | 247 | if (unicode == unicode_empty || |
| 248 | (unicode->length == 1 && |
| 249 | unicode->str[0] < 256U && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 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, |
| 263 | sizeof(Py_UNICODE) * (length + 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 264 | if (!unicode->str) { |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +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 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +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 |
| 287 | free list never reduces its size below 1. |
| 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 | |
| 302 | /* Unicode freelist & memory allocation */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 303 | if (free_list) { |
| 304 | unicode = free_list; |
| 305 | free_list = *(PyUnicodeObject **)unicode; |
| 306 | numfree--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 307 | if (unicode->str) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 308 | /* Keep-Alive optimization: we only upsize the buffer, |
| 309 | never downsize it. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 310 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 311 | unicode_resize(unicode, length) < 0) { |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 312 | PyObject_DEL(unicode->str); |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 313 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 314 | } |
| 315 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 316 | else { |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 317 | size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 318 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 319 | } |
| 320 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 321 | } |
| 322 | else { |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 323 | size_t new_size; |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 324 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 325 | if (unicode == NULL) |
| 326 | return NULL; |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 327 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 328 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 331 | if (!unicode->str) { |
| 332 | PyErr_NoMemory(); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 333 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 334 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 335 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 336 | * the caller fails before initializing str -- unicode_resize() |
| 337 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 338 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 339 | * We don't want unicode_resize to read uninitialized memory in |
| 340 | * that case. |
| 341 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 342 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 343 | unicode->str[length] = 0; |
Martin v. Löwis | f15da69 | 2006-04-13 07:24:50 +0000 | [diff] [blame] | 344 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 345 | unicode->hash = -1; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 346 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 347 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 348 | |
| 349 | onError: |
| 350 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 351 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 352 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 356 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 357 | { |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 358 | if (PyUnicode_CheckExact(unicode) && |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 359 | numfree < PyUnicode_MAXFREELIST) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 360 | /* Keep-Alive optimization */ |
| 361 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 362 | PyObject_DEL(unicode->str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 363 | unicode->str = NULL; |
| 364 | unicode->length = 0; |
| 365 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 366 | if (unicode->defenc) { |
| 367 | Py_DECREF(unicode->defenc); |
| 368 | unicode->defenc = NULL; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 369 | } |
| 370 | /* Add to free list */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 371 | *(PyUnicodeObject **)unicode = free_list; |
| 372 | free_list = unicode; |
| 373 | numfree++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 374 | } |
| 375 | else { |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 376 | PyObject_DEL(unicode->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 377 | Py_XDECREF(unicode->defenc); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 378 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 382 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 383 | { |
| 384 | register PyUnicodeObject *v; |
| 385 | |
| 386 | /* Argument checks */ |
| 387 | if (unicode == NULL) { |
| 388 | PyErr_BadInternalCall(); |
| 389 | return -1; |
| 390 | } |
| 391 | v = (PyUnicodeObject *)*unicode; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 392 | if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 393 | PyErr_BadInternalCall(); |
| 394 | return -1; |
| 395 | } |
| 396 | |
| 397 | /* Resizing unicode_empty and single character objects is not |
| 398 | possible since these are being shared. We simply return a fresh |
| 399 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 400 | if (v->length != length && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 401 | (v == unicode_empty || v->length == 1)) { |
| 402 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 403 | if (w == NULL) |
| 404 | return -1; |
| 405 | Py_UNICODE_COPY(w->str, v->str, |
| 406 | length < v->length ? length : v->length); |
Raymond Hettinger | c8df578 | 2003-03-09 07:30:43 +0000 | [diff] [blame] | 407 | Py_DECREF(*unicode); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 408 | *unicode = (PyObject *)w; |
| 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 413 | objects, since we can modify them in-place. */ |
| 414 | return unicode_resize(v, length); |
| 415 | } |
| 416 | |
| 417 | /* Internal API for use in unicodeobject.c only ! */ |
| 418 | #define _PyUnicode_Resize(unicodevar, length) \ |
| 419 | PyUnicode_Resize(((PyObject **)(unicodevar)), length) |
| 420 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 421 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 422 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 423 | { |
| 424 | PyUnicodeObject *unicode; |
| 425 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 426 | /* If the Unicode data is known at construction time, we can apply |
| 427 | some optimizations which share commonly used objects. */ |
| 428 | if (u != NULL) { |
| 429 | |
| 430 | /* Optimization for empty strings */ |
| 431 | if (size == 0 && unicode_empty != NULL) { |
| 432 | Py_INCREF(unicode_empty); |
| 433 | return (PyObject *)unicode_empty; |
| 434 | } |
| 435 | |
| 436 | /* Single character Unicode objects in the Latin-1 range are |
| 437 | shared when using this constructor */ |
| 438 | if (size == 1 && *u < 256) { |
| 439 | unicode = unicode_latin1[*u]; |
| 440 | if (!unicode) { |
| 441 | unicode = _PyUnicode_New(1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 442 | if (!unicode) |
| 443 | return NULL; |
Marc-André Lemburg | 8879a33 | 2001-06-07 12:26:56 +0000 | [diff] [blame] | 444 | unicode->str[0] = *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 445 | unicode_latin1[*u] = unicode; |
| 446 | } |
| 447 | Py_INCREF(unicode); |
| 448 | return (PyObject *)unicode; |
| 449 | } |
| 450 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 451 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 452 | unicode = _PyUnicode_New(size); |
| 453 | if (!unicode) |
| 454 | return NULL; |
| 455 | |
| 456 | /* Copy the Unicode data into the new object */ |
| 457 | if (u != NULL) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 458 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 459 | |
| 460 | return (PyObject *)unicode; |
| 461 | } |
| 462 | |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 463 | PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
| 464 | { |
| 465 | PyUnicodeObject *unicode; |
Gregory P. Smith | c00eb73 | 2008-04-09 23:16:37 +0000 | [diff] [blame] | 466 | |
Gregory P. Smith | c00eb73 | 2008-04-09 23:16:37 +0000 | [diff] [blame] | 467 | if (size < 0) { |
| 468 | PyErr_SetString(PyExc_SystemError, |
| 469 | "Negative size passed to PyUnicode_FromStringAndSize"); |
| 470 | return NULL; |
| 471 | } |
| 472 | |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 473 | /* If the Unicode data is known at construction time, we can apply |
| 474 | some optimizations which share commonly used objects. |
| 475 | Also, this means the input must be UTF-8, so fall back to the |
| 476 | UTF-8 decoder at the end. */ |
| 477 | if (u != NULL) { |
| 478 | |
| 479 | /* Optimization for empty strings */ |
| 480 | if (size == 0 && unicode_empty != NULL) { |
| 481 | Py_INCREF(unicode_empty); |
| 482 | return (PyObject *)unicode_empty; |
| 483 | } |
| 484 | |
| 485 | /* Single characters are shared when using this constructor. |
| 486 | Restrict to ASCII, since the input must be UTF-8. */ |
| 487 | if (size == 1 && Py_CHARMASK(*u) < 128) { |
Neal Norwitz | d183bdd | 2008-03-28 04:58:51 +0000 | [diff] [blame] | 488 | unicode = unicode_latin1[Py_CHARMASK(*u)]; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 489 | if (!unicode) { |
| 490 | unicode = _PyUnicode_New(1); |
| 491 | if (!unicode) |
| 492 | return NULL; |
| 493 | unicode->str[0] = Py_CHARMASK(*u); |
Neal Norwitz | d183bdd | 2008-03-28 04:58:51 +0000 | [diff] [blame] | 494 | unicode_latin1[Py_CHARMASK(*u)] = unicode; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 495 | } |
| 496 | Py_INCREF(unicode); |
| 497 | return (PyObject *)unicode; |
| 498 | } |
| 499 | |
| 500 | return PyUnicode_DecodeUTF8(u, size, NULL); |
| 501 | } |
| 502 | |
| 503 | unicode = _PyUnicode_New(size); |
| 504 | if (!unicode) |
| 505 | return NULL; |
| 506 | |
| 507 | return (PyObject *)unicode; |
| 508 | } |
| 509 | |
| 510 | PyObject *PyUnicode_FromString(const char *u) |
| 511 | { |
| 512 | size_t size = strlen(u); |
| 513 | if (size > PY_SSIZE_T_MAX) { |
| 514 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 515 | return NULL; |
| 516 | } |
| 517 | |
| 518 | return PyUnicode_FromStringAndSize(u, size); |
| 519 | } |
| 520 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 521 | #ifdef HAVE_WCHAR_H |
| 522 | |
| 523 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 524 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 525 | { |
| 526 | PyUnicodeObject *unicode; |
| 527 | |
| 528 | if (w == NULL) { |
| 529 | PyErr_BadInternalCall(); |
| 530 | return NULL; |
| 531 | } |
| 532 | |
| 533 | unicode = _PyUnicode_New(size); |
| 534 | if (!unicode) |
| 535 | return NULL; |
| 536 | |
| 537 | /* Copy the wchar_t data into the new object */ |
| 538 | #ifdef HAVE_USABLE_WCHAR_T |
| 539 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 540 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 541 | { |
| 542 | register Py_UNICODE *u; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 543 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 544 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 545 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 546 | *u++ = *w++; |
| 547 | } |
| 548 | #endif |
| 549 | |
| 550 | return (PyObject *)unicode; |
| 551 | } |
| 552 | |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 553 | static void |
| 554 | makefmt(char *fmt, int longflag, int size_tflag, int zeropad, int width, int precision, char c) |
| 555 | { |
| 556 | *fmt++ = '%'; |
| 557 | if (width) { |
| 558 | if (zeropad) |
| 559 | *fmt++ = '0'; |
| 560 | fmt += sprintf(fmt, "%d", width); |
| 561 | } |
| 562 | if (precision) |
| 563 | fmt += sprintf(fmt, ".%d", precision); |
| 564 | if (longflag) |
| 565 | *fmt++ = 'l'; |
| 566 | else if (size_tflag) { |
| 567 | char *f = PY_FORMAT_SIZE_T; |
| 568 | while (*f) |
| 569 | *fmt++ = *f++; |
| 570 | } |
| 571 | *fmt++ = c; |
| 572 | *fmt = '\0'; |
| 573 | } |
| 574 | |
| 575 | #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} |
| 576 | |
| 577 | PyObject * |
| 578 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 579 | { |
| 580 | va_list count; |
| 581 | Py_ssize_t callcount = 0; |
| 582 | PyObject **callresults = NULL; |
| 583 | PyObject **callresult = NULL; |
| 584 | Py_ssize_t n = 0; |
| 585 | int width = 0; |
| 586 | int precision = 0; |
| 587 | int zeropad; |
| 588 | const char* f; |
| 589 | Py_UNICODE *s; |
| 590 | PyObject *string; |
| 591 | /* used by sprintf */ |
| 592 | char buffer[21]; |
| 593 | /* use abuffer instead of buffer, if we need more space |
| 594 | * (which can happen if there's a format specifier with width). */ |
| 595 | char *abuffer = NULL; |
| 596 | char *realbuffer; |
| 597 | Py_ssize_t abuffersize = 0; |
| 598 | char fmt[60]; /* should be enough for %0width.precisionld */ |
| 599 | const char *copy; |
| 600 | |
| 601 | #ifdef VA_LIST_IS_ARRAY |
| 602 | Py_MEMCPY(count, vargs, sizeof(va_list)); |
| 603 | #else |
| 604 | #ifdef __va_copy |
| 605 | __va_copy(count, vargs); |
| 606 | #else |
| 607 | count = vargs; |
| 608 | #endif |
| 609 | #endif |
| 610 | /* step 1: count the number of %S/%R format specifications |
| 611 | * (we call PyObject_Str()/PyObject_Repr() for these objects |
| 612 | * once during step 3 and put the result in an array) */ |
| 613 | for (f = format; *f; f++) { |
| 614 | if (*f == '%' && (*(f+1)=='S' || *(f+1)=='R')) |
| 615 | ++callcount; |
| 616 | } |
| 617 | /* step 2: allocate memory for the results of |
| 618 | * PyObject_Str()/PyObject_Repr() calls */ |
| 619 | if (callcount) { |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 620 | callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 621 | if (!callresults) { |
| 622 | PyErr_NoMemory(); |
| 623 | return NULL; |
| 624 | } |
| 625 | callresult = callresults; |
| 626 | } |
| 627 | /* step 3: figure out how large a buffer we need */ |
| 628 | for (f = format; *f; f++) { |
| 629 | if (*f == '%') { |
| 630 | const char* p = f; |
| 631 | width = 0; |
Neal Norwitz | ade57d0 | 2008-03-23 06:19:57 +0000 | [diff] [blame] | 632 | while (isdigit((unsigned)*f)) |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 633 | width = (width*10) + *f++ - '0'; |
Neal Norwitz | ade57d0 | 2008-03-23 06:19:57 +0000 | [diff] [blame] | 634 | while (*++f && *f != '%' && !isalpha((unsigned)*f)) |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 635 | ; |
| 636 | |
| 637 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 638 | * they don't affect the amount of space we reserve. |
| 639 | */ |
| 640 | if ((*f == 'l' || *f == 'z') && |
| 641 | (f[1] == 'd' || f[1] == 'u')) |
| 642 | ++f; |
| 643 | |
| 644 | switch (*f) { |
| 645 | case 'c': |
| 646 | (void)va_arg(count, int); |
| 647 | /* fall through... */ |
| 648 | case '%': |
| 649 | n++; |
| 650 | break; |
| 651 | case 'd': case 'u': case 'i': case 'x': |
| 652 | (void) va_arg(count, int); |
| 653 | /* 20 bytes is enough to hold a 64-bit |
| 654 | integer. Decimal takes the most space. |
| 655 | This isn't enough for octal. |
| 656 | If a width is specified we need more |
| 657 | (which we allocate later). */ |
| 658 | if (width < 20) |
| 659 | width = 20; |
| 660 | n += width; |
| 661 | if (abuffersize < width) |
| 662 | abuffersize = width; |
| 663 | break; |
| 664 | case 's': |
| 665 | { |
| 666 | /* UTF-8 */ |
| 667 | unsigned char*s; |
| 668 | s = va_arg(count, unsigned char*); |
| 669 | while (*s) { |
| 670 | if (*s < 128) { |
| 671 | n++; s++; |
| 672 | } else if (*s < 0xc0) { |
| 673 | /* invalid UTF-8 */ |
| 674 | n++; s++; |
| 675 | } else if (*s < 0xc0) { |
| 676 | n++; |
| 677 | s++; if(!*s)break; |
| 678 | s++; |
| 679 | } else if (*s < 0xe0) { |
| 680 | n++; |
| 681 | s++; if(!*s)break; |
| 682 | s++; if(!*s)break; |
| 683 | s++; |
| 684 | } else { |
| 685 | #ifdef Py_UNICODE_WIDE |
| 686 | n++; |
| 687 | #else |
| 688 | n+=2; |
| 689 | #endif |
| 690 | s++; if(!*s)break; |
| 691 | s++; if(!*s)break; |
| 692 | s++; if(!*s)break; |
| 693 | s++; |
| 694 | } |
| 695 | } |
| 696 | break; |
| 697 | } |
| 698 | case 'U': |
| 699 | { |
| 700 | PyObject *obj = va_arg(count, PyObject *); |
| 701 | assert(obj && PyUnicode_Check(obj)); |
| 702 | n += PyUnicode_GET_SIZE(obj); |
| 703 | break; |
| 704 | } |
| 705 | case 'V': |
| 706 | { |
| 707 | PyObject *obj = va_arg(count, PyObject *); |
| 708 | const char *str = va_arg(count, const char *); |
| 709 | assert(obj || str); |
| 710 | assert(!obj || PyUnicode_Check(obj)); |
| 711 | if (obj) |
| 712 | n += PyUnicode_GET_SIZE(obj); |
| 713 | else |
| 714 | n += strlen(str); |
| 715 | break; |
| 716 | } |
| 717 | case 'S': |
| 718 | { |
| 719 | PyObject *obj = va_arg(count, PyObject *); |
| 720 | PyObject *str; |
| 721 | assert(obj); |
| 722 | str = PyObject_Str(obj); |
| 723 | if (!str) |
| 724 | goto fail; |
| 725 | n += PyUnicode_GET_SIZE(str); |
| 726 | /* Remember the str and switch to the next slot */ |
| 727 | *callresult++ = str; |
| 728 | break; |
| 729 | } |
| 730 | case 'R': |
| 731 | { |
| 732 | PyObject *obj = va_arg(count, PyObject *); |
| 733 | PyObject *repr; |
| 734 | assert(obj); |
| 735 | repr = PyObject_Repr(obj); |
| 736 | if (!repr) |
| 737 | goto fail; |
| 738 | n += PyUnicode_GET_SIZE(repr); |
| 739 | /* Remember the repr and switch to the next slot */ |
| 740 | *callresult++ = repr; |
| 741 | break; |
| 742 | } |
| 743 | case 'p': |
| 744 | (void) va_arg(count, int); |
| 745 | /* maximum 64-bit pointer representation: |
| 746 | * 0xffffffffffffffff |
| 747 | * so 19 characters is enough. |
| 748 | * XXX I count 18 -- what's the extra for? |
| 749 | */ |
| 750 | n += 19; |
| 751 | break; |
| 752 | default: |
| 753 | /* if we stumble upon an unknown |
| 754 | formatting code, copy the rest of |
| 755 | the format string to the output |
| 756 | string. (we cannot just skip the |
| 757 | code, since there's no way to know |
| 758 | what's in the argument list) */ |
| 759 | n += strlen(p); |
| 760 | goto expand; |
| 761 | } |
| 762 | } else |
| 763 | n++; |
| 764 | } |
| 765 | expand: |
| 766 | if (abuffersize > 20) { |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 767 | abuffer = PyObject_Malloc(abuffersize); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 768 | if (!abuffer) { |
| 769 | PyErr_NoMemory(); |
| 770 | goto fail; |
| 771 | } |
| 772 | realbuffer = abuffer; |
| 773 | } |
| 774 | else |
| 775 | realbuffer = buffer; |
| 776 | /* step 4: fill the buffer */ |
| 777 | /* Since we've analyzed how much space we need for the worst case, |
| 778 | we don't have to resize the string. |
| 779 | There can be no errors beyond this point. */ |
| 780 | string = PyUnicode_FromUnicode(NULL, n); |
| 781 | if (!string) |
| 782 | goto fail; |
| 783 | |
| 784 | s = PyUnicode_AS_UNICODE(string); |
| 785 | callresult = callresults; |
| 786 | |
| 787 | for (f = format; *f; f++) { |
| 788 | if (*f == '%') { |
| 789 | const char* p = f++; |
| 790 | int longflag = 0; |
| 791 | int size_tflag = 0; |
| 792 | zeropad = (*f == '0'); |
| 793 | /* parse the width.precision part */ |
| 794 | width = 0; |
Neal Norwitz | ade57d0 | 2008-03-23 06:19:57 +0000 | [diff] [blame] | 795 | while (isdigit((unsigned)*f)) |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 796 | width = (width*10) + *f++ - '0'; |
| 797 | precision = 0; |
| 798 | if (*f == '.') { |
| 799 | f++; |
Neal Norwitz | ade57d0 | 2008-03-23 06:19:57 +0000 | [diff] [blame] | 800 | while (isdigit((unsigned)*f)) |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 801 | precision = (precision*10) + *f++ - '0'; |
| 802 | } |
| 803 | /* handle the long flag, but only for %ld and %lu. |
| 804 | others can be added when necessary. */ |
| 805 | if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
| 806 | longflag = 1; |
| 807 | ++f; |
| 808 | } |
| 809 | /* handle the size_t flag. */ |
| 810 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 811 | size_tflag = 1; |
| 812 | ++f; |
| 813 | } |
| 814 | |
| 815 | switch (*f) { |
| 816 | case 'c': |
| 817 | *s++ = va_arg(vargs, int); |
| 818 | break; |
| 819 | case 'd': |
| 820 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'd'); |
| 821 | if (longflag) |
| 822 | sprintf(realbuffer, fmt, va_arg(vargs, long)); |
| 823 | else if (size_tflag) |
| 824 | sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
| 825 | else |
| 826 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 827 | appendstring(realbuffer); |
| 828 | break; |
| 829 | case 'u': |
| 830 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'u'); |
| 831 | if (longflag) |
| 832 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
| 833 | else if (size_tflag) |
| 834 | sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
| 835 | else |
| 836 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
| 837 | appendstring(realbuffer); |
| 838 | break; |
| 839 | case 'i': |
| 840 | makefmt(fmt, 0, 0, zeropad, width, precision, 'i'); |
| 841 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 842 | appendstring(realbuffer); |
| 843 | break; |
| 844 | case 'x': |
| 845 | makefmt(fmt, 0, 0, zeropad, width, precision, 'x'); |
| 846 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 847 | appendstring(realbuffer); |
| 848 | break; |
| 849 | case 's': |
| 850 | { |
| 851 | /* Parameter must be UTF-8 encoded. |
| 852 | In case of encoding errors, use |
| 853 | the replacement character. */ |
| 854 | PyObject *u; |
| 855 | p = va_arg(vargs, char*); |
| 856 | u = PyUnicode_DecodeUTF8(p, strlen(p), |
| 857 | "replace"); |
| 858 | if (!u) |
| 859 | goto fail; |
| 860 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(u), |
| 861 | PyUnicode_GET_SIZE(u)); |
| 862 | s += PyUnicode_GET_SIZE(u); |
| 863 | Py_DECREF(u); |
| 864 | break; |
| 865 | } |
| 866 | case 'U': |
| 867 | { |
| 868 | PyObject *obj = va_arg(vargs, PyObject *); |
| 869 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 870 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 871 | s += size; |
| 872 | break; |
| 873 | } |
| 874 | case 'V': |
| 875 | { |
| 876 | PyObject *obj = va_arg(vargs, PyObject *); |
| 877 | const char *str = va_arg(vargs, const char *); |
| 878 | if (obj) { |
| 879 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 880 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 881 | s += size; |
| 882 | } else { |
| 883 | appendstring(str); |
| 884 | } |
| 885 | break; |
| 886 | } |
| 887 | case 'S': |
| 888 | case 'R': |
| 889 | { |
| 890 | Py_UNICODE *ucopy; |
| 891 | Py_ssize_t usize; |
| 892 | Py_ssize_t upos; |
| 893 | /* unused, since we already have the result */ |
| 894 | (void) va_arg(vargs, PyObject *); |
| 895 | ucopy = PyUnicode_AS_UNICODE(*callresult); |
| 896 | usize = PyUnicode_GET_SIZE(*callresult); |
| 897 | for (upos = 0; upos<usize;) |
| 898 | *s++ = ucopy[upos++]; |
| 899 | /* We're done with the unicode()/repr() => forget it */ |
| 900 | Py_DECREF(*callresult); |
| 901 | /* switch to next unicode()/repr() result */ |
| 902 | ++callresult; |
| 903 | break; |
| 904 | } |
| 905 | case 'p': |
| 906 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 907 | /* %p is ill-defined: ensure leading 0x. */ |
| 908 | if (buffer[1] == 'X') |
| 909 | buffer[1] = 'x'; |
| 910 | else if (buffer[1] != 'x') { |
| 911 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 912 | buffer[0] = '0'; |
| 913 | buffer[1] = 'x'; |
| 914 | } |
| 915 | appendstring(buffer); |
| 916 | break; |
| 917 | case '%': |
| 918 | *s++ = '%'; |
| 919 | break; |
| 920 | default: |
| 921 | appendstring(p); |
| 922 | goto end; |
| 923 | } |
| 924 | } else |
| 925 | *s++ = *f; |
| 926 | } |
| 927 | |
| 928 | end: |
| 929 | if (callresults) |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 930 | PyObject_Free(callresults); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 931 | if (abuffer) |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 932 | PyObject_Free(abuffer); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 933 | _PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
| 934 | return string; |
| 935 | fail: |
| 936 | if (callresults) { |
| 937 | PyObject **callresult2 = callresults; |
| 938 | while (callresult2 < callresult) { |
| 939 | Py_DECREF(*callresult2); |
| 940 | ++callresult2; |
| 941 | } |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 942 | PyObject_Free(callresults); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 943 | } |
| 944 | if (abuffer) |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 945 | PyObject_Free(abuffer); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 946 | return NULL; |
| 947 | } |
| 948 | |
| 949 | #undef appendstring |
| 950 | |
| 951 | PyObject * |
| 952 | PyUnicode_FromFormat(const char *format, ...) |
| 953 | { |
| 954 | PyObject* ret; |
| 955 | va_list vargs; |
| 956 | |
| 957 | #ifdef HAVE_STDARG_PROTOTYPES |
| 958 | va_start(vargs, format); |
| 959 | #else |
| 960 | va_start(vargs); |
| 961 | #endif |
| 962 | ret = PyUnicode_FromFormatV(format, vargs); |
| 963 | va_end(vargs); |
| 964 | return ret; |
| 965 | } |
| 966 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 967 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 968 | wchar_t *w, |
| 969 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 970 | { |
| 971 | if (unicode == NULL) { |
| 972 | PyErr_BadInternalCall(); |
| 973 | return -1; |
| 974 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 975 | |
| 976 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 977 | if (size > PyUnicode_GET_SIZE(unicode)) |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 978 | size = PyUnicode_GET_SIZE(unicode) + 1; |
| 979 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 980 | #ifdef HAVE_USABLE_WCHAR_T |
| 981 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 982 | #else |
| 983 | { |
| 984 | register Py_UNICODE *u; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 985 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 986 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 987 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 988 | *w++ = *u++; |
| 989 | } |
| 990 | #endif |
| 991 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 992 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 993 | return PyUnicode_GET_SIZE(unicode); |
| 994 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 995 | return size; |
| 996 | } |
| 997 | |
| 998 | #endif |
| 999 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1000 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 1001 | { |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1002 | Py_UNICODE s[1]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1003 | |
| 1004 | #ifdef Py_UNICODE_WIDE |
| 1005 | if (ordinal < 0 || ordinal > 0x10ffff) { |
| 1006 | PyErr_SetString(PyExc_ValueError, |
| 1007 | "unichr() arg not in range(0x110000) " |
| 1008 | "(wide Python build)"); |
| 1009 | return NULL; |
| 1010 | } |
| 1011 | #else |
| 1012 | if (ordinal < 0 || ordinal > 0xffff) { |
| 1013 | PyErr_SetString(PyExc_ValueError, |
| 1014 | "unichr() arg not in range(0x10000) " |
| 1015 | "(narrow Python build)"); |
| 1016 | return NULL; |
| 1017 | } |
| 1018 | #endif |
| 1019 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1020 | s[0] = (Py_UNICODE)ordinal; |
| 1021 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1024 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 1025 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1026 | /* XXX Perhaps we should make this API an alias of |
| 1027 | PyObject_Unicode() instead ?! */ |
| 1028 | if (PyUnicode_CheckExact(obj)) { |
| 1029 | Py_INCREF(obj); |
| 1030 | return obj; |
| 1031 | } |
| 1032 | if (PyUnicode_Check(obj)) { |
| 1033 | /* For a Unicode subtype that's not a Unicode object, |
| 1034 | return a true Unicode object with the same data. */ |
| 1035 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 1036 | PyUnicode_GET_SIZE(obj)); |
| 1037 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1038 | return PyUnicode_FromEncodedObject(obj, NULL, "strict"); |
| 1039 | } |
| 1040 | |
| 1041 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
| 1042 | const char *encoding, |
| 1043 | const char *errors) |
| 1044 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1045 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1046 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1047 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1048 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1049 | if (obj == NULL) { |
| 1050 | PyErr_BadInternalCall(); |
| 1051 | return NULL; |
| 1052 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1053 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1054 | #if 0 |
| 1055 | /* For b/w compatibility we also accept Unicode objects provided |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 1056 | that no encodings is given and then redirect to |
| 1057 | PyObject_Unicode() which then applies the additional logic for |
| 1058 | Unicode subclasses. |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1059 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1060 | NOTE: This API should really only be used for object which |
| 1061 | represent *encoded* Unicode ! |
| 1062 | |
| 1063 | */ |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1064 | if (PyUnicode_Check(obj)) { |
| 1065 | if (encoding) { |
| 1066 | PyErr_SetString(PyExc_TypeError, |
| 1067 | "decoding Unicode is not supported"); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1068 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1069 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1070 | return PyObject_Unicode(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1071 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1072 | #else |
| 1073 | if (PyUnicode_Check(obj)) { |
| 1074 | PyErr_SetString(PyExc_TypeError, |
| 1075 | "decoding Unicode is not supported"); |
| 1076 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1077 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1078 | #endif |
| 1079 | |
| 1080 | /* Coerce object */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1081 | if (PyString_Check(obj)) { |
| 1082 | s = PyString_AS_STRING(obj); |
| 1083 | len = PyString_GET_SIZE(obj); |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 1084 | } |
Christian Heimes | 3497f94 | 2008-05-26 12:29:14 +0000 | [diff] [blame] | 1085 | else if (PyByteArray_Check(obj)) { |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 1086 | /* Python 2.x specific */ |
| 1087 | PyErr_Format(PyExc_TypeError, |
| 1088 | "decoding bytearray is not supported"); |
| 1089 | return NULL; |
| 1090 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1091 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
| 1092 | /* Overwrite the error message with something more useful in |
| 1093 | case of a TypeError. */ |
| 1094 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1095 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1096 | "coercing to Unicode: need string or buffer, " |
| 1097 | "%.80s found", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1098 | Py_TYPE(obj)->tp_name); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1099 | goto onError; |
| 1100 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1101 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1102 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1103 | if (len == 0) { |
| 1104 | Py_INCREF(unicode_empty); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1105 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1106 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1107 | else |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1108 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1109 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1110 | return v; |
| 1111 | |
| 1112 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1113 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | PyObject *PyUnicode_Decode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1117 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1118 | const char *encoding, |
| 1119 | const char *errors) |
| 1120 | { |
| 1121 | PyObject *buffer = NULL, *unicode; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1122 | |
| 1123 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1124 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1125 | |
| 1126 | /* Shortcuts for common default encodings */ |
| 1127 | if (strcmp(encoding, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1128 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1129 | else if (strcmp(encoding, "latin-1") == 0) |
| 1130 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1131 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1132 | else if (strcmp(encoding, "mbcs") == 0) |
| 1133 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 1134 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1135 | else if (strcmp(encoding, "ascii") == 0) |
| 1136 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1137 | |
| 1138 | /* Decode via the codec registry */ |
| 1139 | buffer = PyBuffer_FromMemory((void *)s, size); |
| 1140 | if (buffer == NULL) |
| 1141 | goto onError; |
| 1142 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1143 | if (unicode == NULL) |
| 1144 | goto onError; |
| 1145 | if (!PyUnicode_Check(unicode)) { |
| 1146 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 1147 | "decoder did not return an unicode object (type=%.400s)", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1148 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1149 | Py_DECREF(unicode); |
| 1150 | goto onError; |
| 1151 | } |
| 1152 | Py_DECREF(buffer); |
| 1153 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1154 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1155 | onError: |
| 1156 | Py_XDECREF(buffer); |
| 1157 | return NULL; |
| 1158 | } |
| 1159 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1160 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1161 | const char *encoding, |
| 1162 | const char *errors) |
| 1163 | { |
| 1164 | PyObject *v; |
| 1165 | |
| 1166 | if (!PyUnicode_Check(unicode)) { |
| 1167 | PyErr_BadArgument(); |
| 1168 | goto onError; |
| 1169 | } |
| 1170 | |
| 1171 | if (encoding == NULL) |
| 1172 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1173 | |
| 1174 | /* Decode via the codec registry */ |
| 1175 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1176 | if (v == NULL) |
| 1177 | goto onError; |
| 1178 | return v; |
| 1179 | |
| 1180 | onError: |
| 1181 | return NULL; |
| 1182 | } |
| 1183 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1184 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1185 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1186 | const char *encoding, |
| 1187 | const char *errors) |
| 1188 | { |
| 1189 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1190 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1191 | unicode = PyUnicode_FromUnicode(s, size); |
| 1192 | if (unicode == NULL) |
| 1193 | return NULL; |
| 1194 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1195 | Py_DECREF(unicode); |
| 1196 | return v; |
| 1197 | } |
| 1198 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1199 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1200 | const char *encoding, |
| 1201 | const char *errors) |
| 1202 | { |
| 1203 | PyObject *v; |
| 1204 | |
| 1205 | if (!PyUnicode_Check(unicode)) { |
| 1206 | PyErr_BadArgument(); |
| 1207 | goto onError; |
| 1208 | } |
| 1209 | |
| 1210 | if (encoding == NULL) |
| 1211 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1212 | |
| 1213 | /* Encode via the codec registry */ |
| 1214 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1215 | if (v == NULL) |
| 1216 | goto onError; |
| 1217 | return v; |
| 1218 | |
| 1219 | onError: |
| 1220 | return NULL; |
| 1221 | } |
| 1222 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1223 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1224 | const char *encoding, |
| 1225 | const char *errors) |
| 1226 | { |
| 1227 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1228 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1229 | if (!PyUnicode_Check(unicode)) { |
| 1230 | PyErr_BadArgument(); |
| 1231 | goto onError; |
| 1232 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1233 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1234 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1235 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1236 | |
| 1237 | /* Shortcuts for common default encodings */ |
| 1238 | if (errors == NULL) { |
| 1239 | if (strcmp(encoding, "utf-8") == 0) |
Jeremy Hylton | 9cea41c | 2001-05-29 17:13:15 +0000 | [diff] [blame] | 1240 | return PyUnicode_AsUTF8String(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1241 | else if (strcmp(encoding, "latin-1") == 0) |
| 1242 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1243 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1244 | else if (strcmp(encoding, "mbcs") == 0) |
| 1245 | return PyUnicode_AsMBCSString(unicode); |
| 1246 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1247 | else if (strcmp(encoding, "ascii") == 0) |
| 1248 | return PyUnicode_AsASCIIString(unicode); |
| 1249 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1250 | |
| 1251 | /* Encode via the codec registry */ |
| 1252 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1253 | if (v == NULL) |
| 1254 | goto onError; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1255 | if (!PyString_Check(v)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1256 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 1257 | "encoder did not return a string object (type=%.400s)", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1258 | Py_TYPE(v)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1259 | Py_DECREF(v); |
| 1260 | goto onError; |
| 1261 | } |
| 1262 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1263 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1264 | onError: |
| 1265 | return NULL; |
| 1266 | } |
| 1267 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1268 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 1269 | const char *errors) |
| 1270 | { |
| 1271 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
| 1272 | |
| 1273 | if (v) |
| 1274 | return v; |
| 1275 | v = PyUnicode_AsEncodedString(unicode, NULL, errors); |
| 1276 | if (v && errors == NULL) |
| 1277 | ((PyUnicodeObject *)unicode)->defenc = v; |
| 1278 | return v; |
| 1279 | } |
| 1280 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1281 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1282 | { |
| 1283 | if (!PyUnicode_Check(unicode)) { |
| 1284 | PyErr_BadArgument(); |
| 1285 | goto onError; |
| 1286 | } |
| 1287 | return PyUnicode_AS_UNICODE(unicode); |
| 1288 | |
| 1289 | onError: |
| 1290 | return NULL; |
| 1291 | } |
| 1292 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1293 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1294 | { |
| 1295 | if (!PyUnicode_Check(unicode)) { |
| 1296 | PyErr_BadArgument(); |
| 1297 | goto onError; |
| 1298 | } |
| 1299 | return PyUnicode_GET_SIZE(unicode); |
| 1300 | |
| 1301 | onError: |
| 1302 | return -1; |
| 1303 | } |
| 1304 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1305 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1306 | { |
| 1307 | return unicode_default_encoding; |
| 1308 | } |
| 1309 | |
| 1310 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 1311 | { |
| 1312 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1313 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1314 | /* Make sure the encoding is valid. As side effect, this also |
| 1315 | loads the encoding into the codec registry cache. */ |
| 1316 | v = _PyCodec_Lookup(encoding); |
| 1317 | if (v == NULL) |
| 1318 | goto onError; |
| 1319 | Py_DECREF(v); |
| 1320 | strncpy(unicode_default_encoding, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1321 | encoding, |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1322 | sizeof(unicode_default_encoding)); |
| 1323 | return 0; |
| 1324 | |
| 1325 | onError: |
| 1326 | return -1; |
| 1327 | } |
| 1328 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1329 | /* error handling callback helper: |
| 1330 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1331 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1332 | and adjust various state variables. |
| 1333 | return 0 on success, -1 on error |
| 1334 | */ |
| 1335 | |
| 1336 | static |
| 1337 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 1338 | const char *encoding, const char *reason, |
Walter Dörwald | 8757878 | 2007-08-30 15:30:09 +0000 | [diff] [blame] | 1339 | const char *input, Py_ssize_t insize, Py_ssize_t *startinpos, |
| 1340 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1341 | PyObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1342 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1343 | 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] | 1344 | |
| 1345 | PyObject *restuple = NULL; |
| 1346 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1347 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
| 1348 | Py_ssize_t requiredsize; |
| 1349 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1350 | Py_UNICODE *repptr; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1351 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1352 | int res = -1; |
| 1353 | |
| 1354 | if (*errorHandler == NULL) { |
| 1355 | *errorHandler = PyCodec_LookupError(errors); |
| 1356 | if (*errorHandler == NULL) |
| 1357 | goto onError; |
| 1358 | } |
| 1359 | |
| 1360 | if (*exceptionObject == NULL) { |
| 1361 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 1362 | encoding, input, insize, *startinpos, *endinpos, reason); |
| 1363 | if (*exceptionObject == NULL) |
| 1364 | goto onError; |
| 1365 | } |
| 1366 | else { |
| 1367 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 1368 | goto onError; |
| 1369 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 1370 | goto onError; |
| 1371 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1372 | goto onError; |
| 1373 | } |
| 1374 | |
| 1375 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1376 | if (restuple == NULL) |
| 1377 | goto onError; |
| 1378 | if (!PyTuple_Check(restuple)) { |
| 1379 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 1380 | goto onError; |
| 1381 | } |
| 1382 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 1383 | goto onError; |
| 1384 | if (newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1385 | newpos = insize+newpos; |
| 1386 | if (newpos<0 || newpos>insize) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 1387 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1388 | goto onError; |
| 1389 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1390 | |
| 1391 | /* need more space? (at least enough for what we |
| 1392 | have+the replacement+the rest of the string (starting |
| 1393 | at the new input position), so we won't have to check space |
| 1394 | when there are no errors in the rest of the string) */ |
| 1395 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 1396 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 1397 | requiredsize = *outpos + repsize + insize-newpos; |
| 1398 | if (requiredsize > outsize) { |
| 1399 | if (requiredsize<2*outsize) |
| 1400 | requiredsize = 2*outsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1401 | if (PyUnicode_Resize(output, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1402 | goto onError; |
| 1403 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
| 1404 | } |
| 1405 | *endinpos = newpos; |
| 1406 | *inptr = input + newpos; |
| 1407 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 1408 | *outptr += repsize; |
| 1409 | *outpos += repsize; |
| 1410 | /* we made it! */ |
| 1411 | res = 0; |
| 1412 | |
| 1413 | onError: |
| 1414 | Py_XDECREF(restuple); |
| 1415 | return res; |
| 1416 | } |
| 1417 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1418 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 1419 | |
| 1420 | /* see RFC2152 for details */ |
| 1421 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1422 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1423 | char utf7_special[128] = { |
| 1424 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 1425 | encoded: |
| 1426 | 0 - not special |
| 1427 | 1 - special |
| 1428 | 2 - whitespace (optional) |
| 1429 | 3 - RFC2152 Set O (optional) */ |
| 1430 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 1431 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1432 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 1433 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 1434 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1435 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 1436 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1437 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 1438 | |
| 1439 | }; |
| 1440 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1441 | /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
| 1442 | warnings about the comparison always being false; since |
| 1443 | utf7_special[0] is 1, we can safely make that one comparison |
| 1444 | true */ |
| 1445 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1446 | #define SPECIAL(c, encodeO, encodeWS) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1447 | ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1448 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1449 | (encodeO && (utf7_special[(c)] == 3))) |
| 1450 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1451 | #define B64(n) \ |
| 1452 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 1453 | #define B64CHAR(c) \ |
| 1454 | (isalnum(c) || (c) == '+' || (c) == '/') |
| 1455 | #define UB64(c) \ |
| 1456 | ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
| 1457 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1458 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1459 | #define ENCODE(out, ch, bits) \ |
| 1460 | while (bits >= 6) { \ |
| 1461 | *out++ = B64(ch >> (bits-6)); \ |
| 1462 | bits -= 6; \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1465 | #define DECODE(out, ch, bits, surrogate) \ |
| 1466 | while (bits >= 16) { \ |
| 1467 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 1468 | bits -= 16; \ |
| 1469 | if (surrogate) { \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1470 | /* We have already generated an error for the high surrogate \ |
| 1471 | 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] | 1472 | surrogate = 0; \ |
| 1473 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1474 | /* This is a surrogate pair. Unfortunately we can't represent \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1475 | it in a 16-bit character */ \ |
| 1476 | surrogate = 1; \ |
| 1477 | errmsg = "code pairs are not supported"; \ |
| 1478 | goto utf7Error; \ |
| 1479 | } else { \ |
| 1480 | *out++ = outCh; \ |
| 1481 | } \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1482 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1483 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1484 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1485 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1486 | const char *errors) |
| 1487 | { |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1488 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 1489 | } |
| 1490 | |
| 1491 | PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
| 1492 | Py_ssize_t size, |
| 1493 | const char *errors, |
| 1494 | Py_ssize_t *consumed) |
| 1495 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1496 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1497 | Py_ssize_t startinpos; |
| 1498 | Py_ssize_t endinpos; |
| 1499 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1500 | const char *e; |
| 1501 | PyUnicodeObject *unicode; |
| 1502 | Py_UNICODE *p; |
| 1503 | const char *errmsg = ""; |
| 1504 | int inShift = 0; |
| 1505 | unsigned int bitsleft = 0; |
| 1506 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1507 | int surrogate = 0; |
| 1508 | PyObject *errorHandler = NULL; |
| 1509 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1510 | |
| 1511 | unicode = _PyUnicode_New(size); |
| 1512 | if (!unicode) |
| 1513 | return NULL; |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1514 | if (size == 0) { |
| 1515 | if (consumed) |
| 1516 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1517 | return (PyObject *)unicode; |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1518 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1519 | |
| 1520 | p = unicode->str; |
| 1521 | e = s + size; |
| 1522 | |
| 1523 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1524 | Py_UNICODE ch; |
| 1525 | restart: |
| 1526 | ch = *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1527 | |
| 1528 | if (inShift) { |
| 1529 | if ((ch == '-') || !B64CHAR(ch)) { |
| 1530 | inShift = 0; |
| 1531 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1532 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1533 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1534 | if (bitsleft >= 6) { |
| 1535 | /* The shift sequence has a partial character in it. If |
| 1536 | bitsleft < 6 then we could just classify it as padding |
| 1537 | but that is not the case here */ |
| 1538 | |
| 1539 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1540 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1541 | } |
| 1542 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1543 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1544 | here so indicate the potential of a misencoded character. */ |
| 1545 | |
| 1546 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 1547 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 1548 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1549 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | if (ch == '-') { |
| 1553 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1554 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1555 | inShift = 1; |
| 1556 | } |
| 1557 | } else if (SPECIAL(ch,0,0)) { |
| 1558 | errmsg = "unexpected special character"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1559 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1560 | } else { |
| 1561 | *p++ = ch; |
| 1562 | } |
| 1563 | } else { |
| 1564 | charsleft = (charsleft << 6) | UB64(ch); |
| 1565 | bitsleft += 6; |
| 1566 | s++; |
| 1567 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1568 | } |
| 1569 | } |
| 1570 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1571 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1572 | s++; |
| 1573 | if (s < e && *s == '-') { |
| 1574 | s++; |
| 1575 | *p++ = '+'; |
| 1576 | } else |
| 1577 | { |
| 1578 | inShift = 1; |
| 1579 | bitsleft = 0; |
| 1580 | } |
| 1581 | } |
| 1582 | else if (SPECIAL(ch,0,0)) { |
Walter Dörwald | 9d04542 | 2007-08-30 15:34:55 +0000 | [diff] [blame] | 1583 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1584 | errmsg = "unexpected special character"; |
| 1585 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1586 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1587 | } |
| 1588 | else { |
| 1589 | *p++ = ch; |
| 1590 | s++; |
| 1591 | } |
| 1592 | continue; |
| 1593 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1594 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1595 | endinpos = s-starts; |
| 1596 | if (unicode_decode_call_errorhandler( |
| 1597 | errors, &errorHandler, |
| 1598 | "utf7", errmsg, |
| 1599 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1600 | (PyObject **)&unicode, &outpos, &p)) |
| 1601 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1604 | if (inShift && !consumed) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1605 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1606 | endinpos = size; |
| 1607 | if (unicode_decode_call_errorhandler( |
| 1608 | errors, &errorHandler, |
| 1609 | "utf7", "unterminated shift sequence", |
| 1610 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1611 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1612 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1613 | if (s < e) |
| 1614 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1615 | } |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1616 | if (consumed) { |
| 1617 | if(inShift) |
| 1618 | *consumed = startinpos; |
| 1619 | else |
| 1620 | *consumed = s-starts; |
| 1621 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1622 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1623 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1624 | goto onError; |
| 1625 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1626 | Py_XDECREF(errorHandler); |
| 1627 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1628 | return (PyObject *)unicode; |
| 1629 | |
| 1630 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1631 | Py_XDECREF(errorHandler); |
| 1632 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1633 | Py_DECREF(unicode); |
| 1634 | return NULL; |
| 1635 | } |
| 1636 | |
| 1637 | |
| 1638 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1639 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1640 | int encodeSetO, |
| 1641 | int encodeWhiteSpace, |
| 1642 | const char *errors) |
| 1643 | { |
| 1644 | PyObject *v; |
| 1645 | /* It might be possible to tighten this worst case */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1646 | Py_ssize_t cbAllocated = 5 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1647 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1648 | Py_ssize_t i = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1649 | unsigned int bitsleft = 0; |
| 1650 | unsigned long charsleft = 0; |
| 1651 | char * out; |
| 1652 | char * start; |
| 1653 | |
| 1654 | if (size == 0) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1655 | return PyString_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1656 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1657 | v = PyString_FromStringAndSize(NULL, cbAllocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1658 | if (v == NULL) |
| 1659 | return NULL; |
| 1660 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1661 | start = out = PyString_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1662 | for (;i < size; ++i) { |
| 1663 | Py_UNICODE ch = s[i]; |
| 1664 | |
| 1665 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1666 | if (ch == '+') { |
| 1667 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1668 | *out++ = '-'; |
| 1669 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1670 | charsleft = ch; |
| 1671 | bitsleft = 16; |
| 1672 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1673 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1674 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1675 | } else { |
| 1676 | *out++ = (char) ch; |
| 1677 | } |
| 1678 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1679 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1680 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1681 | charsleft = 0; |
| 1682 | bitsleft = 0; |
| 1683 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1684 | so no '-' is required, except if the character is itself a '-' */ |
| 1685 | if (B64CHAR(ch) || ch == '-') { |
| 1686 | *out++ = '-'; |
| 1687 | } |
| 1688 | inShift = 0; |
| 1689 | *out++ = (char) ch; |
| 1690 | } else { |
| 1691 | bitsleft += 16; |
| 1692 | charsleft = (charsleft << 16) | ch; |
| 1693 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1694 | |
| 1695 | /* If the next character is special then we dont' need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1696 | 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] | 1697 | or '-' then the shift sequence will be terminated implicitly and we |
| 1698 | don't have to insert a '-'. */ |
| 1699 | |
| 1700 | if (bitsleft == 0) { |
| 1701 | if (i + 1 < size) { |
| 1702 | Py_UNICODE ch2 = s[i+1]; |
| 1703 | |
| 1704 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1705 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1706 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1707 | *out++ = '-'; |
| 1708 | inShift = 0; |
| 1709 | } else { |
| 1710 | inShift = 0; |
| 1711 | } |
| 1712 | |
| 1713 | } |
| 1714 | else { |
| 1715 | *out++ = '-'; |
| 1716 | inShift = 0; |
| 1717 | } |
| 1718 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1719 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1720 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1721 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1722 | if (bitsleft) { |
| 1723 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1724 | *out++ = '-'; |
| 1725 | } |
| 1726 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1727 | _PyString_Resize(&v, out - start); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1728 | return v; |
| 1729 | } |
| 1730 | |
| 1731 | #undef SPECIAL |
| 1732 | #undef B64 |
| 1733 | #undef B64CHAR |
| 1734 | #undef UB64 |
| 1735 | #undef ENCODE |
| 1736 | #undef DECODE |
| 1737 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1738 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1739 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1740 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1741 | char utf8_code_length[256] = { |
| 1742 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1743 | illegal prefix. see RFC 2279 for details */ |
| 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 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1749 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1750 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1751 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1752 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1753 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1754 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1755 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1756 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1757 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1758 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1759 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1760 | }; |
| 1761 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1762 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1763 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1764 | const char *errors) |
| 1765 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1766 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 1767 | } |
| 1768 | |
| 1769 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1770 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1771 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1772 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1773 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1774 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1775 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1776 | Py_ssize_t startinpos; |
| 1777 | Py_ssize_t endinpos; |
| 1778 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1779 | const char *e; |
| 1780 | PyUnicodeObject *unicode; |
| 1781 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1782 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1783 | PyObject *errorHandler = NULL; |
| 1784 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1785 | |
| 1786 | /* Note: size will always be longer than the resulting Unicode |
| 1787 | character count */ |
| 1788 | unicode = _PyUnicode_New(size); |
| 1789 | if (!unicode) |
| 1790 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1791 | if (size == 0) { |
| 1792 | if (consumed) |
| 1793 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1794 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1795 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1796 | |
| 1797 | /* Unpack UTF-8 encoded data */ |
| 1798 | p = unicode->str; |
| 1799 | e = s + size; |
| 1800 | |
| 1801 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1802 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1803 | |
| 1804 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1805 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1806 | s++; |
| 1807 | continue; |
| 1808 | } |
| 1809 | |
| 1810 | n = utf8_code_length[ch]; |
| 1811 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1812 | if (s + n > e) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1813 | if (consumed) |
| 1814 | break; |
| 1815 | else { |
| 1816 | errmsg = "unexpected end of data"; |
| 1817 | startinpos = s-starts; |
| 1818 | endinpos = size; |
| 1819 | goto utf8Error; |
| 1820 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1821 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1822 | |
| 1823 | switch (n) { |
| 1824 | |
| 1825 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1826 | errmsg = "unexpected code byte"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1827 | startinpos = s-starts; |
| 1828 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1829 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1830 | |
| 1831 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1832 | errmsg = "internal error"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1833 | startinpos = s-starts; |
| 1834 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1835 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1836 | |
| 1837 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1838 | if ((s[1] & 0xc0) != 0x80) { |
| 1839 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1840 | startinpos = s-starts; |
| 1841 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1842 | goto utf8Error; |
| 1843 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1844 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1845 | if (ch < 0x80) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1846 | startinpos = s-starts; |
| 1847 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1848 | errmsg = "illegal encoding"; |
| 1849 | goto utf8Error; |
| 1850 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1851 | else |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1852 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1853 | break; |
| 1854 | |
| 1855 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1856 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1857 | (s[2] & 0xc0) != 0x80) { |
| 1858 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1859 | startinpos = s-starts; |
| 1860 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1861 | goto utf8Error; |
| 1862 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1863 | 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] | 1864 | if (ch < 0x0800) { |
| 1865 | /* Note: UTF-8 encodings of surrogates are considered |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1866 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1867 | |
| 1868 | XXX For wide builds (UCS-4) we should probably try |
| 1869 | to recombine the surrogates into a single code |
| 1870 | unit. |
| 1871 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1872 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1873 | startinpos = s-starts; |
| 1874 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1875 | goto utf8Error; |
| 1876 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1877 | else |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1878 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1879 | break; |
| 1880 | |
| 1881 | case 4: |
| 1882 | if ((s[1] & 0xc0) != 0x80 || |
| 1883 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1884 | (s[3] & 0xc0) != 0x80) { |
| 1885 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1886 | startinpos = s-starts; |
| 1887 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1888 | goto utf8Error; |
| 1889 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1890 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 1891 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 1892 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1893 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1894 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1895 | || (ch > 0x10ffff)) /* maximum value allowed for |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1896 | UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1897 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1898 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1899 | startinpos = s-starts; |
| 1900 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1901 | goto utf8Error; |
| 1902 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1903 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1904 | *p++ = (Py_UNICODE)ch; |
| 1905 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1906 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1907 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1908 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 1909 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1910 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1911 | /* high surrogate = top 10 bits added to D800 */ |
| 1912 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1913 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1914 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1915 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1916 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1917 | break; |
| 1918 | |
| 1919 | default: |
| 1920 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1921 | errmsg = "unsupported Unicode code range"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1922 | startinpos = s-starts; |
| 1923 | endinpos = startinpos+n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1924 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1925 | } |
| 1926 | s += n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1927 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1928 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1929 | utf8Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1930 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1931 | if (unicode_decode_call_errorhandler( |
| 1932 | errors, &errorHandler, |
| 1933 | "utf8", errmsg, |
| 1934 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1935 | (PyObject **)&unicode, &outpos, &p)) |
| 1936 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1937 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1938 | if (consumed) |
| 1939 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1940 | |
| 1941 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1942 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1943 | goto onError; |
| 1944 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1945 | Py_XDECREF(errorHandler); |
| 1946 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1947 | return (PyObject *)unicode; |
| 1948 | |
| 1949 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1950 | Py_XDECREF(errorHandler); |
| 1951 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1952 | Py_DECREF(unicode); |
| 1953 | return NULL; |
| 1954 | } |
| 1955 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1956 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 1957 | and allocate exactly as much space needed at the end. Else allocate the |
| 1958 | maximum possible needed (4 result bytes per Unicode character), and return |
| 1959 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1960 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1961 | PyObject * |
| 1962 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1963 | Py_ssize_t size, |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1964 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1965 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1966 | #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] | 1967 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1968 | Py_ssize_t i; /* index into s of next input byte */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1969 | PyObject *v; /* result string object */ |
| 1970 | char *p; /* next free byte in output buffer */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1971 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1972 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1973 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1974 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1975 | assert(s != NULL); |
| 1976 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1977 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1978 | if (size <= MAX_SHORT_UNICHARS) { |
| 1979 | /* Write into the stack buffer; nallocated can't overflow. |
| 1980 | * At the end, we'll allocate exactly as much heap space as it |
| 1981 | * turns out we need. |
| 1982 | */ |
| 1983 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
| 1984 | v = NULL; /* will allocate after we're done */ |
| 1985 | p = stackbuf; |
| 1986 | } |
| 1987 | else { |
| 1988 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 1989 | nallocated = size * 4; |
| 1990 | if (nallocated / 4 != size) /* overflow! */ |
| 1991 | return PyErr_NoMemory(); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1992 | v = PyString_FromStringAndSize(NULL, nallocated); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1993 | if (v == NULL) |
| 1994 | return NULL; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1995 | p = PyString_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1996 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1997 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1998 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1999 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2000 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2001 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2002 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2003 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2004 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2005 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2006 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 2007 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 2008 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2009 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2010 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2011 | /* Encode UCS2 Unicode ordinals */ |
| 2012 | if (ch < 0x10000) { |
| 2013 | /* Special case: check for high surrogate */ |
| 2014 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 2015 | Py_UCS4 ch2 = s[i]; |
| 2016 | /* Check for low surrogate and combine the two to |
| 2017 | form a UCS4 value */ |
| 2018 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2019 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2020 | i++; |
| 2021 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2022 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2023 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2024 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2025 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2026 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2027 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2028 | continue; |
| 2029 | } |
| 2030 | encodeUCS4: |
| 2031 | /* Encode UCS4 Unicode ordinals */ |
| 2032 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2033 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 2034 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2035 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2036 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2037 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2038 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2039 | if (v == NULL) { |
| 2040 | /* This was stack allocated. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2041 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2042 | assert(nneeded <= nallocated); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2043 | v = PyString_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2044 | } |
| 2045 | else { |
| 2046 | /* Cut back to size actually needed. */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2047 | nneeded = p - PyString_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2048 | assert(nneeded <= nallocated); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2049 | _PyString_Resize(&v, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2050 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2051 | return v; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2052 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2053 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2054 | } |
| 2055 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2056 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 2057 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2058 | if (!PyUnicode_Check(unicode)) { |
| 2059 | PyErr_BadArgument(); |
| 2060 | return NULL; |
| 2061 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 2062 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 2063 | PyUnicode_GET_SIZE(unicode), |
| 2064 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2067 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 2068 | |
| 2069 | PyObject * |
| 2070 | PyUnicode_DecodeUTF32(const char *s, |
| 2071 | Py_ssize_t size, |
| 2072 | const char *errors, |
| 2073 | int *byteorder) |
| 2074 | { |
| 2075 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 2076 | } |
| 2077 | |
| 2078 | PyObject * |
| 2079 | PyUnicode_DecodeUTF32Stateful(const char *s, |
| 2080 | Py_ssize_t size, |
| 2081 | const char *errors, |
| 2082 | int *byteorder, |
| 2083 | Py_ssize_t *consumed) |
| 2084 | { |
| 2085 | const char *starts = s; |
| 2086 | Py_ssize_t startinpos; |
| 2087 | Py_ssize_t endinpos; |
| 2088 | Py_ssize_t outpos; |
| 2089 | PyUnicodeObject *unicode; |
| 2090 | Py_UNICODE *p; |
| 2091 | #ifndef Py_UNICODE_WIDE |
| 2092 | int i, pairs; |
| 2093 | #else |
| 2094 | const int pairs = 0; |
| 2095 | #endif |
| 2096 | const unsigned char *q, *e; |
| 2097 | int bo = 0; /* assume native ordering by default */ |
| 2098 | const char *errmsg = ""; |
Walter Dörwald | 20b40d3 | 2007-08-17 16:52:50 +0000 | [diff] [blame] | 2099 | /* Offsets from q for retrieving bytes in the right order. */ |
| 2100 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2101 | int iorder[] = {0, 1, 2, 3}; |
| 2102 | #else |
| 2103 | int iorder[] = {3, 2, 1, 0}; |
| 2104 | #endif |
Walter Dörwald | 9ab80a9 | 2007-08-17 16:58:43 +0000 | [diff] [blame] | 2105 | PyObject *errorHandler = NULL; |
| 2106 | PyObject *exc = NULL; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2107 | /* On narrow builds we split characters outside the BMP into two |
| 2108 | codepoints => count how much extra space we need. */ |
| 2109 | #ifndef Py_UNICODE_WIDE |
| 2110 | for (i = pairs = 0; i < size/4; i++) |
| 2111 | if (((Py_UCS4 *)s)[i] >= 0x10000) |
| 2112 | pairs++; |
| 2113 | #endif |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2114 | |
| 2115 | /* This might be one to much, because of a BOM */ |
| 2116 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 2117 | if (!unicode) |
| 2118 | return NULL; |
| 2119 | if (size == 0) |
| 2120 | return (PyObject *)unicode; |
| 2121 | |
| 2122 | /* Unpack UTF-32 encoded data */ |
| 2123 | p = unicode->str; |
| 2124 | q = (unsigned char *)s; |
| 2125 | e = q + size; |
| 2126 | |
| 2127 | if (byteorder) |
| 2128 | bo = *byteorder; |
| 2129 | |
| 2130 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2131 | byte order setting accordingly. In native mode, the leading BOM |
| 2132 | mark is skipped, in all other modes, it is copied to the output |
| 2133 | stream as-is (giving a ZWNBSP character). */ |
| 2134 | if (bo == 0) { |
| 2135 | if (size >= 4) { |
| 2136 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2137 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 2138 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2139 | if (bom == 0x0000FEFF) { |
| 2140 | q += 4; |
| 2141 | bo = -1; |
| 2142 | } |
| 2143 | else if (bom == 0xFFFE0000) { |
| 2144 | q += 4; |
| 2145 | bo = 1; |
| 2146 | } |
| 2147 | #else |
| 2148 | if (bom == 0x0000FEFF) { |
| 2149 | q += 4; |
| 2150 | bo = 1; |
| 2151 | } |
| 2152 | else if (bom == 0xFFFE0000) { |
| 2153 | q += 4; |
| 2154 | bo = -1; |
| 2155 | } |
| 2156 | #endif |
| 2157 | } |
| 2158 | } |
| 2159 | |
| 2160 | if (bo == -1) { |
| 2161 | /* force LE */ |
| 2162 | iorder[0] = 0; |
| 2163 | iorder[1] = 1; |
| 2164 | iorder[2] = 2; |
| 2165 | iorder[3] = 3; |
| 2166 | } |
| 2167 | else if (bo == 1) { |
| 2168 | /* force BE */ |
| 2169 | iorder[0] = 3; |
| 2170 | iorder[1] = 2; |
| 2171 | iorder[2] = 1; |
| 2172 | iorder[3] = 0; |
| 2173 | } |
| 2174 | |
| 2175 | while (q < e) { |
| 2176 | Py_UCS4 ch; |
| 2177 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 2178 | if (e-q<4) { |
| 2179 | if (consumed) |
| 2180 | break; |
| 2181 | errmsg = "truncated data"; |
| 2182 | startinpos = ((const char *)q)-starts; |
| 2183 | endinpos = ((const char *)e)-starts; |
| 2184 | goto utf32Error; |
| 2185 | /* The remaining input chars are ignored if the callback |
| 2186 | chooses to skip the input */ |
| 2187 | } |
| 2188 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2189 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 2190 | |
| 2191 | if (ch >= 0x110000) |
| 2192 | { |
| 2193 | errmsg = "codepoint not in range(0x110000)"; |
| 2194 | startinpos = ((const char *)q)-starts; |
| 2195 | endinpos = startinpos+4; |
| 2196 | goto utf32Error; |
| 2197 | } |
| 2198 | #ifndef Py_UNICODE_WIDE |
| 2199 | if (ch >= 0x10000) |
| 2200 | { |
| 2201 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 2202 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2203 | } |
| 2204 | else |
| 2205 | #endif |
| 2206 | *p++ = ch; |
| 2207 | q += 4; |
| 2208 | continue; |
| 2209 | utf32Error: |
| 2210 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2211 | if (unicode_decode_call_errorhandler( |
| 2212 | errors, &errorHandler, |
| 2213 | "utf32", errmsg, |
| 2214 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2215 | (PyObject **)&unicode, &outpos, &p)) |
| 2216 | goto onError; |
| 2217 | } |
| 2218 | |
| 2219 | if (byteorder) |
| 2220 | *byteorder = bo; |
| 2221 | |
| 2222 | if (consumed) |
| 2223 | *consumed = (const char *)q-starts; |
| 2224 | |
| 2225 | /* Adjust length */ |
| 2226 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 2227 | goto onError; |
| 2228 | |
| 2229 | Py_XDECREF(errorHandler); |
| 2230 | Py_XDECREF(exc); |
| 2231 | return (PyObject *)unicode; |
| 2232 | |
| 2233 | onError: |
| 2234 | Py_DECREF(unicode); |
| 2235 | Py_XDECREF(errorHandler); |
| 2236 | Py_XDECREF(exc); |
| 2237 | return NULL; |
| 2238 | } |
| 2239 | |
| 2240 | PyObject * |
| 2241 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 2242 | Py_ssize_t size, |
| 2243 | const char *errors, |
| 2244 | int byteorder) |
| 2245 | { |
| 2246 | PyObject *v; |
| 2247 | unsigned char *p; |
| 2248 | #ifndef Py_UNICODE_WIDE |
| 2249 | int i, pairs; |
| 2250 | #else |
| 2251 | const int pairs = 0; |
| 2252 | #endif |
| 2253 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2254 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2255 | int iorder[] = {0, 1, 2, 3}; |
| 2256 | #else |
| 2257 | int iorder[] = {3, 2, 1, 0}; |
| 2258 | #endif |
| 2259 | |
| 2260 | #define STORECHAR(CH) \ |
| 2261 | do { \ |
| 2262 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 2263 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 2264 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 2265 | p[iorder[0]] = (CH) & 0xff; \ |
| 2266 | p += 4; \ |
| 2267 | } while(0) |
| 2268 | |
| 2269 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 2270 | so we need less space. */ |
| 2271 | #ifndef Py_UNICODE_WIDE |
| 2272 | for (i = pairs = 0; i < size-1; i++) |
| 2273 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2274 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 2275 | pairs++; |
| 2276 | #endif |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2277 | v = PyString_FromStringAndSize(NULL, |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2278 | 4 * (size - pairs + (byteorder == 0))); |
| 2279 | if (v == NULL) |
| 2280 | return NULL; |
| 2281 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2282 | p = (unsigned char *)PyString_AS_STRING(v); |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2283 | if (byteorder == 0) |
| 2284 | STORECHAR(0xFEFF); |
| 2285 | if (size == 0) |
| 2286 | return v; |
| 2287 | |
| 2288 | if (byteorder == -1) { |
| 2289 | /* force LE */ |
| 2290 | iorder[0] = 0; |
| 2291 | iorder[1] = 1; |
| 2292 | iorder[2] = 2; |
| 2293 | iorder[3] = 3; |
| 2294 | } |
| 2295 | else if (byteorder == 1) { |
| 2296 | /* force BE */ |
| 2297 | iorder[0] = 3; |
| 2298 | iorder[1] = 2; |
| 2299 | iorder[2] = 1; |
| 2300 | iorder[3] = 0; |
| 2301 | } |
| 2302 | |
| 2303 | while (size-- > 0) { |
| 2304 | Py_UCS4 ch = *s++; |
| 2305 | #ifndef Py_UNICODE_WIDE |
| 2306 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 2307 | Py_UCS4 ch2 = *s; |
| 2308 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 2309 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 2310 | s++; |
| 2311 | size--; |
| 2312 | } |
| 2313 | } |
| 2314 | #endif |
| 2315 | STORECHAR(ch); |
| 2316 | } |
| 2317 | return v; |
| 2318 | #undef STORECHAR |
| 2319 | } |
| 2320 | |
| 2321 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 2322 | { |
| 2323 | if (!PyUnicode_Check(unicode)) { |
| 2324 | PyErr_BadArgument(); |
| 2325 | return NULL; |
| 2326 | } |
| 2327 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
| 2328 | PyUnicode_GET_SIZE(unicode), |
| 2329 | NULL, |
| 2330 | 0); |
| 2331 | } |
| 2332 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2333 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 2334 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2335 | PyObject * |
| 2336 | PyUnicode_DecodeUTF16(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2337 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2338 | const char *errors, |
| 2339 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2340 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2341 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 2342 | } |
| 2343 | |
| 2344 | PyObject * |
| 2345 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2346 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2347 | const char *errors, |
| 2348 | int *byteorder, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2349 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2350 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2351 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2352 | Py_ssize_t startinpos; |
| 2353 | Py_ssize_t endinpos; |
| 2354 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2355 | PyUnicodeObject *unicode; |
| 2356 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2357 | const unsigned char *q, *e; |
| 2358 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2359 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2360 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 2361 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2362 | int ihi = 1, ilo = 0; |
| 2363 | #else |
| 2364 | int ihi = 0, ilo = 1; |
| 2365 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2366 | PyObject *errorHandler = NULL; |
| 2367 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2368 | |
| 2369 | /* Note: size will always be longer than the resulting Unicode |
| 2370 | character count */ |
| 2371 | unicode = _PyUnicode_New(size); |
| 2372 | if (!unicode) |
| 2373 | return NULL; |
| 2374 | if (size == 0) |
| 2375 | return (PyObject *)unicode; |
| 2376 | |
| 2377 | /* Unpack UTF-16 encoded data */ |
| 2378 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2379 | q = (unsigned char *)s; |
| 2380 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2381 | |
| 2382 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2383 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2384 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2385 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2386 | byte order setting accordingly. In native mode, the leading BOM |
| 2387 | mark is skipped, in all other modes, it is copied to the output |
| 2388 | stream as-is (giving a ZWNBSP character). */ |
| 2389 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2390 | if (size >= 2) { |
| 2391 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2392 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2393 | if (bom == 0xFEFF) { |
| 2394 | q += 2; |
| 2395 | bo = -1; |
| 2396 | } |
| 2397 | else if (bom == 0xFFFE) { |
| 2398 | q += 2; |
| 2399 | bo = 1; |
| 2400 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2401 | #else |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2402 | if (bom == 0xFEFF) { |
| 2403 | q += 2; |
| 2404 | bo = 1; |
| 2405 | } |
| 2406 | else if (bom == 0xFFFE) { |
| 2407 | q += 2; |
| 2408 | bo = -1; |
| 2409 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2410 | #endif |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2411 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2412 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2413 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2414 | if (bo == -1) { |
| 2415 | /* force LE */ |
| 2416 | ihi = 1; |
| 2417 | ilo = 0; |
| 2418 | } |
| 2419 | else if (bo == 1) { |
| 2420 | /* force BE */ |
| 2421 | ihi = 0; |
| 2422 | ilo = 1; |
| 2423 | } |
| 2424 | |
| 2425 | while (q < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2426 | Py_UNICODE ch; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2427 | /* remaining bytes at the end? (size should be even) */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2428 | if (e-q<2) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2429 | if (consumed) |
| 2430 | break; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2431 | errmsg = "truncated data"; |
| 2432 | startinpos = ((const char *)q)-starts; |
| 2433 | endinpos = ((const char *)e)-starts; |
| 2434 | goto utf16Error; |
| 2435 | /* The remaining input chars are ignored if the callback |
| 2436 | chooses to skip the input */ |
| 2437 | } |
| 2438 | ch = (q[ihi] << 8) | q[ilo]; |
| 2439 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2440 | q += 2; |
| 2441 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2442 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 2443 | *p++ = ch; |
| 2444 | continue; |
| 2445 | } |
| 2446 | |
| 2447 | /* UTF-16 code pair: */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2448 | if (q >= e) { |
| 2449 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2450 | startinpos = (((const char *)q)-2)-starts; |
| 2451 | endinpos = ((const char *)e)-starts; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2452 | goto utf16Error; |
| 2453 | } |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2454 | if (0xD800 <= ch && ch <= 0xDBFF) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2455 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 2456 | q += 2; |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2457 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2458 | #ifndef Py_UNICODE_WIDE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2459 | *p++ = ch; |
| 2460 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2461 | #else |
| 2462 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2463 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2464 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2465 | } |
| 2466 | else { |
| 2467 | errmsg = "illegal UTF-16 surrogate"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2468 | startinpos = (((const char *)q)-4)-starts; |
| 2469 | endinpos = startinpos+2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2470 | goto utf16Error; |
| 2471 | } |
| 2472 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2473 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2474 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2475 | startinpos = (((const char *)q)-2)-starts; |
| 2476 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2477 | /* Fall through to report the error */ |
| 2478 | |
| 2479 | utf16Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2480 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2481 | if (unicode_decode_call_errorhandler( |
| 2482 | errors, &errorHandler, |
| 2483 | "utf16", errmsg, |
| 2484 | starts, size, &startinpos, &endinpos, &exc, (const char **)&q, |
| 2485 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2486 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2487 | } |
| 2488 | |
| 2489 | if (byteorder) |
| 2490 | *byteorder = bo; |
| 2491 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2492 | if (consumed) |
| 2493 | *consumed = (const char *)q-starts; |
| 2494 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2495 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2496 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2497 | goto onError; |
| 2498 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2499 | Py_XDECREF(errorHandler); |
| 2500 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2501 | return (PyObject *)unicode; |
| 2502 | |
| 2503 | onError: |
| 2504 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2505 | Py_XDECREF(errorHandler); |
| 2506 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2507 | return NULL; |
| 2508 | } |
| 2509 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2510 | PyObject * |
| 2511 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2512 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2513 | const char *errors, |
| 2514 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2515 | { |
| 2516 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2517 | unsigned char *p; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2518 | #ifdef Py_UNICODE_WIDE |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2519 | int i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2520 | #else |
| 2521 | const int pairs = 0; |
| 2522 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2523 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2524 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2525 | int ihi = 1, ilo = 0; |
| 2526 | #else |
| 2527 | int ihi = 0, ilo = 1; |
| 2528 | #endif |
| 2529 | |
| 2530 | #define STORECHAR(CH) \ |
| 2531 | do { \ |
| 2532 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 2533 | p[ilo] = (CH) & 0xff; \ |
| 2534 | p += 2; \ |
| 2535 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2536 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2537 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2538 | for (i = pairs = 0; i < size; i++) |
| 2539 | if (s[i] >= 0x10000) |
| 2540 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2541 | #endif |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2542 | v = PyString_FromStringAndSize(NULL, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2543 | 2 * (size + pairs + (byteorder == 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2544 | if (v == NULL) |
| 2545 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2546 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2547 | p = (unsigned char *)PyString_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2548 | if (byteorder == 0) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2549 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2550 | if (size == 0) |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2551 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2552 | |
| 2553 | if (byteorder == -1) { |
| 2554 | /* force LE */ |
| 2555 | ihi = 1; |
| 2556 | ilo = 0; |
| 2557 | } |
| 2558 | else if (byteorder == 1) { |
| 2559 | /* force BE */ |
| 2560 | ihi = 0; |
| 2561 | ilo = 1; |
| 2562 | } |
| 2563 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2564 | while (size-- > 0) { |
| 2565 | Py_UNICODE ch = *s++; |
| 2566 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2567 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2568 | if (ch >= 0x10000) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2569 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2570 | ch = 0xD800 | ((ch-0x10000) >> 10); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2571 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2572 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2573 | STORECHAR(ch); |
| 2574 | if (ch2) |
| 2575 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2576 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2577 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2578 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
| 2581 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 2582 | { |
| 2583 | if (!PyUnicode_Check(unicode)) { |
| 2584 | PyErr_BadArgument(); |
| 2585 | return NULL; |
| 2586 | } |
| 2587 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 2588 | PyUnicode_GET_SIZE(unicode), |
| 2589 | NULL, |
| 2590 | 0); |
| 2591 | } |
| 2592 | |
| 2593 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 2594 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2595 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 2596 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2597 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2598 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2599 | const char *errors) |
| 2600 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2601 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2602 | Py_ssize_t startinpos; |
| 2603 | Py_ssize_t endinpos; |
| 2604 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2605 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2606 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2607 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2608 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2609 | char* message; |
| 2610 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2611 | PyObject *errorHandler = NULL; |
| 2612 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2613 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2614 | /* Escaped strings will always be longer than the resulting |
| 2615 | 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] | 2616 | length after conversion to the true value. |
| 2617 | (but if the error callback returns a long replacement string |
| 2618 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2619 | v = _PyUnicode_New(size); |
| 2620 | if (v == NULL) |
| 2621 | goto onError; |
| 2622 | if (size == 0) |
| 2623 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2624 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2625 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2626 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2627 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2628 | while (s < end) { |
| 2629 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2630 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2631 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2632 | |
| 2633 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2634 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2635 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2636 | continue; |
| 2637 | } |
| 2638 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2639 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2640 | /* \ - Escapes */ |
| 2641 | s++; |
Guido van Rossum | 1c1ac38 | 2007-10-29 22:15:05 +0000 | [diff] [blame] | 2642 | c = *s++; |
| 2643 | if (s > end) |
| 2644 | c = '\0'; /* Invalid after \ */ |
| 2645 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2646 | |
| 2647 | /* \x escapes */ |
| 2648 | case '\n': break; |
| 2649 | case '\\': *p++ = '\\'; break; |
| 2650 | case '\'': *p++ = '\''; break; |
| 2651 | case '\"': *p++ = '\"'; break; |
| 2652 | case 'b': *p++ = '\b'; break; |
| 2653 | case 'f': *p++ = '\014'; break; /* FF */ |
| 2654 | case 't': *p++ = '\t'; break; |
| 2655 | case 'n': *p++ = '\n'; break; |
| 2656 | case 'r': *p++ = '\r'; break; |
| 2657 | case 'v': *p++ = '\013'; break; /* VT */ |
| 2658 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 2659 | |
| 2660 | /* \OOO (octal) escapes */ |
| 2661 | case '0': case '1': case '2': case '3': |
| 2662 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2663 | x = s[-1] - '0'; |
Guido van Rossum | 1c1ac38 | 2007-10-29 22:15:05 +0000 | [diff] [blame] | 2664 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2665 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 1c1ac38 | 2007-10-29 22:15:05 +0000 | [diff] [blame] | 2666 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2667 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2668 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2669 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2670 | break; |
| 2671 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2672 | /* hex escapes */ |
| 2673 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2674 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2675 | digits = 2; |
| 2676 | message = "truncated \\xXX escape"; |
| 2677 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2678 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2679 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2680 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2681 | digits = 4; |
| 2682 | message = "truncated \\uXXXX escape"; |
| 2683 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2684 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2685 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2686 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2687 | digits = 8; |
| 2688 | message = "truncated \\UXXXXXXXX escape"; |
| 2689 | hexescape: |
| 2690 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2691 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2692 | if (s+digits>end) { |
| 2693 | endinpos = size; |
| 2694 | if (unicode_decode_call_errorhandler( |
| 2695 | errors, &errorHandler, |
| 2696 | "unicodeescape", "end of string in escape sequence", |
| 2697 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2698 | (PyObject **)&v, &outpos, &p)) |
| 2699 | goto onError; |
| 2700 | goto nextByte; |
| 2701 | } |
| 2702 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2703 | c = (unsigned char) s[i]; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2704 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2705 | endinpos = (s+i+1)-starts; |
| 2706 | if (unicode_decode_call_errorhandler( |
| 2707 | errors, &errorHandler, |
| 2708 | "unicodeescape", message, |
| 2709 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2710 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2711 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2712 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2713 | } |
| 2714 | chr = (chr<<4) & ~0xF; |
| 2715 | if (c >= '0' && c <= '9') |
| 2716 | chr += c - '0'; |
| 2717 | else if (c >= 'a' && c <= 'f') |
| 2718 | chr += 10 + c - 'a'; |
| 2719 | else |
| 2720 | chr += 10 + c - 'A'; |
| 2721 | } |
| 2722 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 2723 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2724 | /* _decoding_error will have already written into the |
| 2725 | target buffer. */ |
| 2726 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2727 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2728 | /* when we get here, chr is a 32-bit unicode character */ |
| 2729 | if (chr <= 0xffff) |
| 2730 | /* UCS-2 character */ |
| 2731 | *p++ = (Py_UNICODE) chr; |
| 2732 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2733 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2734 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2735 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2736 | *p++ = chr; |
| 2737 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2738 | chr -= 0x10000L; |
| 2739 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2740 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2741 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2742 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2743 | endinpos = s-starts; |
| 2744 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2745 | if (unicode_decode_call_errorhandler( |
| 2746 | errors, &errorHandler, |
| 2747 | "unicodeescape", "illegal Unicode character", |
| 2748 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2749 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2750 | goto onError; |
| 2751 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2752 | break; |
| 2753 | |
| 2754 | /* \N{name} */ |
| 2755 | case 'N': |
| 2756 | message = "malformed \\N character escape"; |
| 2757 | if (ucnhash_CAPI == NULL) { |
| 2758 | /* load the unicode data module */ |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2759 | PyObject *m, *api; |
Christian Heimes | 000a074 | 2008-01-03 22:16:32 +0000 | [diff] [blame] | 2760 | m = PyImport_ImportModuleNoBlock("unicodedata"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2761 | if (m == NULL) |
| 2762 | goto ucnhashError; |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2763 | api = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2764 | Py_DECREF(m); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2765 | if (api == NULL) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2766 | goto ucnhashError; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2767 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2768 | Py_DECREF(api); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2769 | if (ucnhash_CAPI == NULL) |
| 2770 | goto ucnhashError; |
| 2771 | } |
| 2772 | if (*s == '{') { |
| 2773 | const char *start = s+1; |
| 2774 | /* look for the closing brace */ |
| 2775 | while (*s != '}' && s < end) |
| 2776 | s++; |
| 2777 | if (s > start && s < end && *s == '}') { |
| 2778 | /* found a name. look it up in the unicode database */ |
| 2779 | message = "unknown Unicode character name"; |
| 2780 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 2781 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2782 | goto store; |
| 2783 | } |
| 2784 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2785 | endinpos = s-starts; |
| 2786 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2787 | if (unicode_decode_call_errorhandler( |
| 2788 | errors, &errorHandler, |
| 2789 | "unicodeescape", message, |
| 2790 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2791 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2792 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2793 | break; |
| 2794 | |
| 2795 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2796 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2797 | message = "\\ at end of string"; |
| 2798 | s--; |
| 2799 | endinpos = s-starts; |
| 2800 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2801 | if (unicode_decode_call_errorhandler( |
| 2802 | errors, &errorHandler, |
| 2803 | "unicodeescape", message, |
| 2804 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2805 | (PyObject **)&v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2806 | goto onError; |
| 2807 | } |
| 2808 | else { |
| 2809 | *p++ = '\\'; |
| 2810 | *p++ = (unsigned char)s[-1]; |
| 2811 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2812 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2813 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2814 | nextByte: |
| 2815 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2816 | } |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2817 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2818 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 2819 | Py_XDECREF(errorHandler); |
| 2820 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2821 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2822 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2823 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2824 | PyErr_SetString( |
| 2825 | PyExc_UnicodeError, |
| 2826 | "\\N escapes not supported (can't load unicodedata module)" |
| 2827 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2828 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2829 | Py_XDECREF(errorHandler); |
| 2830 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 2831 | return NULL; |
| 2832 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2833 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2834 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2835 | Py_XDECREF(errorHandler); |
| 2836 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2837 | return NULL; |
| 2838 | } |
| 2839 | |
| 2840 | /* Return a Unicode-Escape string version of the Unicode object. |
| 2841 | |
| 2842 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 2843 | appropriate. |
| 2844 | |
| 2845 | */ |
| 2846 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 2847 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
Fredrik Lundh | 95e2a91 | 2006-05-26 11:38:15 +0000 | [diff] [blame] | 2848 | Py_ssize_t size, |
| 2849 | Py_UNICODE ch) |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 2850 | { |
| 2851 | /* like wcschr, but doesn't stop at NULL characters */ |
| 2852 | |
| 2853 | while (size-- > 0) { |
| 2854 | if (*s == ch) |
| 2855 | return s; |
| 2856 | s++; |
| 2857 | } |
| 2858 | |
| 2859 | return NULL; |
| 2860 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 2861 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2862 | static |
| 2863 | PyObject *unicodeescape_string(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2864 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2865 | int quotes) |
| 2866 | { |
| 2867 | PyObject *repr; |
| 2868 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2869 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2870 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2871 | |
Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 2872 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 2873 | better to choose a different scheme. Perhaps scan the |
| 2874 | first N-chars of the string and allocate based on that size. |
| 2875 | */ |
| 2876 | /* Initial allocation is based on the longest-possible unichr |
| 2877 | escape. |
| 2878 | |
| 2879 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 2880 | unichr, so in this case it's the longest unichr escape. In |
| 2881 | narrow (UTF-16) builds this is five chars per source unichr |
| 2882 | since there are two unichrs in the surrogate pair, so in narrow |
| 2883 | (UTF-16) builds it's not the longest unichr escape. |
| 2884 | |
| 2885 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 2886 | so in the narrow (UTF-16) build case it's the longest unichr |
| 2887 | escape. |
| 2888 | */ |
| 2889 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2890 | repr = PyString_FromStringAndSize(NULL, |
Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 2891 | 2 |
| 2892 | #ifdef Py_UNICODE_WIDE |
| 2893 | + 10*size |
| 2894 | #else |
| 2895 | + 6*size |
| 2896 | #endif |
| 2897 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2898 | if (repr == NULL) |
| 2899 | return NULL; |
| 2900 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2901 | p = PyString_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2902 | |
| 2903 | if (quotes) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2904 | *p++ = 'u'; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2905 | *p++ = (findchar(s, size, '\'') && |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2906 | !findchar(s, size, '"')) ? '"' : '\''; |
| 2907 | } |
| 2908 | while (size-- > 0) { |
| 2909 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2910 | |
Hye-Shik Chang | 835b243 | 2005-12-17 04:38:31 +0000 | [diff] [blame] | 2911 | /* Escape quotes and backslashes */ |
| 2912 | if ((quotes && |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2913 | ch == (Py_UNICODE) PyString_AS_STRING(repr)[1]) || ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2914 | *p++ = '\\'; |
| 2915 | *p++ = (char) ch; |
Guido van Rossum | ad9744a | 2001-09-21 15:38:17 +0000 | [diff] [blame] | 2916 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2917 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2918 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 2919 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2920 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 2921 | else if (ch >= 0x10000) { |
| 2922 | *p++ = '\\'; |
| 2923 | *p++ = 'U'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2924 | *p++ = hexdigit[(ch >> 28) & 0x0000000F]; |
| 2925 | *p++ = hexdigit[(ch >> 24) & 0x0000000F]; |
| 2926 | *p++ = hexdigit[(ch >> 20) & 0x0000000F]; |
| 2927 | *p++ = hexdigit[(ch >> 16) & 0x0000000F]; |
| 2928 | *p++ = hexdigit[(ch >> 12) & 0x0000000F]; |
| 2929 | *p++ = hexdigit[(ch >> 8) & 0x0000000F]; |
| 2930 | *p++ = hexdigit[(ch >> 4) & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2931 | *p++ = hexdigit[ch & 0x0000000F]; |
| 2932 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2933 | } |
Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 2934 | #else |
| 2935 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2936 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 2937 | Py_UNICODE ch2; |
| 2938 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2939 | |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2940 | ch2 = *s++; |
| 2941 | size--; |
| 2942 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 2943 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 2944 | *p++ = '\\'; |
| 2945 | *p++ = 'U'; |
| 2946 | *p++ = hexdigit[(ucs >> 28) & 0x0000000F]; |
| 2947 | *p++ = hexdigit[(ucs >> 24) & 0x0000000F]; |
| 2948 | *p++ = hexdigit[(ucs >> 20) & 0x0000000F]; |
| 2949 | *p++ = hexdigit[(ucs >> 16) & 0x0000000F]; |
| 2950 | *p++ = hexdigit[(ucs >> 12) & 0x0000000F]; |
| 2951 | *p++ = hexdigit[(ucs >> 8) & 0x0000000F]; |
| 2952 | *p++ = hexdigit[(ucs >> 4) & 0x0000000F]; |
| 2953 | *p++ = hexdigit[ucs & 0x0000000F]; |
| 2954 | continue; |
| 2955 | } |
| 2956 | /* Fall through: isolated surrogates are copied as-is */ |
| 2957 | s--; |
| 2958 | size++; |
| 2959 | } |
Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 2960 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2961 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2962 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2963 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2964 | *p++ = '\\'; |
| 2965 | *p++ = 'u'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2966 | *p++ = hexdigit[(ch >> 12) & 0x000F]; |
| 2967 | *p++ = hexdigit[(ch >> 8) & 0x000F]; |
| 2968 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2969 | *p++ = hexdigit[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2970 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2971 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2972 | /* Map special whitespace to '\t', \n', '\r' */ |
| 2973 | else if (ch == '\t') { |
| 2974 | *p++ = '\\'; |
| 2975 | *p++ = 't'; |
| 2976 | } |
| 2977 | else if (ch == '\n') { |
| 2978 | *p++ = '\\'; |
| 2979 | *p++ = 'n'; |
| 2980 | } |
| 2981 | else if (ch == '\r') { |
| 2982 | *p++ = '\\'; |
| 2983 | *p++ = 'r'; |
| 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 non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 2987 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2988 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2989 | *p++ = 'x'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2990 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2991 | *p++ = hexdigit[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2992 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2993 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2994 | /* Copy everything else as-is */ |
| 2995 | else |
| 2996 | *p++ = (char) ch; |
| 2997 | } |
| 2998 | if (quotes) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2999 | *p++ = PyString_AS_STRING(repr)[1]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3000 | |
| 3001 | *p = '\0'; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3002 | _PyString_Resize(&repr, p - PyString_AS_STRING(repr)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3003 | return repr; |
| 3004 | } |
| 3005 | |
| 3006 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3007 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3008 | { |
| 3009 | return unicodeescape_string(s, size, 0); |
| 3010 | } |
| 3011 | |
| 3012 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 3013 | { |
| 3014 | if (!PyUnicode_Check(unicode)) { |
| 3015 | PyErr_BadArgument(); |
| 3016 | return NULL; |
| 3017 | } |
| 3018 | return PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3019 | PyUnicode_GET_SIZE(unicode)); |
| 3020 | } |
| 3021 | |
| 3022 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 3023 | |
| 3024 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3025 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3026 | const char *errors) |
| 3027 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3028 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3029 | Py_ssize_t startinpos; |
| 3030 | Py_ssize_t endinpos; |
| 3031 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3032 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3033 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3034 | const char *end; |
| 3035 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3036 | PyObject *errorHandler = NULL; |
| 3037 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3038 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3039 | /* Escaped strings will always be longer than the resulting |
| 3040 | 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] | 3041 | length after conversion to the true value. (But decoding error |
| 3042 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3043 | v = _PyUnicode_New(size); |
| 3044 | if (v == NULL) |
| 3045 | goto onError; |
| 3046 | if (size == 0) |
| 3047 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3048 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3049 | end = s + size; |
| 3050 | while (s < end) { |
| 3051 | unsigned char c; |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 3052 | Py_UCS4 x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3053 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3054 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3055 | |
| 3056 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3057 | if (*s != '\\') { |
| 3058 | *p++ = (unsigned char)*s++; |
| 3059 | continue; |
| 3060 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3061 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3062 | |
| 3063 | /* \u-escapes are only interpreted iff the number of leading |
| 3064 | backslashes if odd */ |
| 3065 | bs = s; |
| 3066 | for (;s < end;) { |
| 3067 | if (*s != '\\') |
| 3068 | break; |
| 3069 | *p++ = (unsigned char)*s++; |
| 3070 | } |
| 3071 | if (((s - bs) & 1) == 0 || |
| 3072 | s >= end || |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3073 | (*s != 'u' && *s != 'U')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3074 | continue; |
| 3075 | } |
| 3076 | p--; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3077 | count = *s=='u' ? 4 : 8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3078 | s++; |
| 3079 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3080 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3081 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3082 | for (x = 0, i = 0; i < count; ++i, ++s) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3083 | c = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3084 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3085 | endinpos = s-starts; |
| 3086 | if (unicode_decode_call_errorhandler( |
| 3087 | errors, &errorHandler, |
| 3088 | "rawunicodeescape", "truncated \\uXXXX", |
| 3089 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3090 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3091 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3092 | goto nextByte; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3093 | } |
| 3094 | x = (x<<4) & ~0xF; |
| 3095 | if (c >= '0' && c <= '9') |
| 3096 | x += c - '0'; |
| 3097 | else if (c >= 'a' && c <= 'f') |
| 3098 | x += 10 + c - 'a'; |
| 3099 | else |
| 3100 | x += 10 + c - 'A'; |
| 3101 | } |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3102 | if (x <= 0xffff) |
| 3103 | /* UCS-2 character */ |
| 3104 | *p++ = (Py_UNICODE) x; |
| 3105 | else if (x <= 0x10ffff) { |
| 3106 | /* UCS-4 character. Either store directly, or as |
| 3107 | surrogate pair. */ |
| 3108 | #ifdef Py_UNICODE_WIDE |
Amaury Forgeot d'Arc | fac02fa | 2008-03-24 21:04:10 +0000 | [diff] [blame] | 3109 | *p++ = (Py_UNICODE) x; |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3110 | #else |
| 3111 | x -= 0x10000L; |
| 3112 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 3113 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
| 3114 | #endif |
| 3115 | } else { |
| 3116 | endinpos = s-starts; |
| 3117 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3118 | if (unicode_decode_call_errorhandler( |
| 3119 | errors, &errorHandler, |
| 3120 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
| 3121 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3122 | (PyObject **)&v, &outpos, &p)) |
| 3123 | goto onError; |
| 3124 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3125 | nextByte: |
| 3126 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3127 | } |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3128 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3129 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3130 | Py_XDECREF(errorHandler); |
| 3131 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3132 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3133 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3134 | onError: |
| 3135 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3136 | Py_XDECREF(errorHandler); |
| 3137 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3138 | return NULL; |
| 3139 | } |
| 3140 | |
| 3141 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3142 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3143 | { |
| 3144 | PyObject *repr; |
| 3145 | char *p; |
| 3146 | char *q; |
| 3147 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3148 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3149 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3150 | #ifdef Py_UNICODE_WIDE |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3151 | repr = PyString_FromStringAndSize(NULL, 10 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3152 | #else |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3153 | repr = PyString_FromStringAndSize(NULL, 6 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3154 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3155 | if (repr == NULL) |
| 3156 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3157 | if (size == 0) |
| 3158 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3159 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3160 | p = q = PyString_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3161 | while (size-- > 0) { |
| 3162 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3163 | #ifdef Py_UNICODE_WIDE |
| 3164 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 3165 | if (ch >= 0x10000) { |
| 3166 | *p++ = '\\'; |
| 3167 | *p++ = 'U'; |
| 3168 | *p++ = hexdigit[(ch >> 28) & 0xf]; |
| 3169 | *p++ = hexdigit[(ch >> 24) & 0xf]; |
| 3170 | *p++ = hexdigit[(ch >> 20) & 0xf]; |
| 3171 | *p++ = hexdigit[(ch >> 16) & 0xf]; |
| 3172 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 3173 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 3174 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 3175 | *p++ = hexdigit[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3176 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3177 | else |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3178 | #else |
| 3179 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3180 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 3181 | Py_UNICODE ch2; |
| 3182 | Py_UCS4 ucs; |
| 3183 | |
| 3184 | ch2 = *s++; |
| 3185 | size--; |
| 3186 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3187 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3188 | *p++ = '\\'; |
| 3189 | *p++ = 'U'; |
| 3190 | *p++ = hexdigit[(ucs >> 28) & 0xf]; |
| 3191 | *p++ = hexdigit[(ucs >> 24) & 0xf]; |
| 3192 | *p++ = hexdigit[(ucs >> 20) & 0xf]; |
| 3193 | *p++ = hexdigit[(ucs >> 16) & 0xf]; |
| 3194 | *p++ = hexdigit[(ucs >> 12) & 0xf]; |
| 3195 | *p++ = hexdigit[(ucs >> 8) & 0xf]; |
| 3196 | *p++ = hexdigit[(ucs >> 4) & 0xf]; |
| 3197 | *p++ = hexdigit[ucs & 0xf]; |
| 3198 | continue; |
| 3199 | } |
| 3200 | /* Fall through: isolated surrogates are copied as-is */ |
| 3201 | s--; |
| 3202 | size++; |
| 3203 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3204 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3205 | /* Map 16-bit characters to '\uxxxx' */ |
| 3206 | if (ch >= 256) { |
| 3207 | *p++ = '\\'; |
| 3208 | *p++ = 'u'; |
| 3209 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 3210 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 3211 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 3212 | *p++ = hexdigit[ch & 15]; |
| 3213 | } |
| 3214 | /* Copy everything else as-is */ |
| 3215 | else |
| 3216 | *p++ = (char) ch; |
| 3217 | } |
| 3218 | *p = '\0'; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3219 | _PyString_Resize(&repr, p - q); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3220 | return repr; |
| 3221 | } |
| 3222 | |
| 3223 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 3224 | { |
| 3225 | if (!PyUnicode_Check(unicode)) { |
| 3226 | PyErr_BadArgument(); |
| 3227 | return NULL; |
| 3228 | } |
| 3229 | return PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3230 | PyUnicode_GET_SIZE(unicode)); |
| 3231 | } |
| 3232 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3233 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 3234 | |
| 3235 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3236 | Py_ssize_t size, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3237 | const char *errors) |
| 3238 | { |
| 3239 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3240 | Py_ssize_t startinpos; |
| 3241 | Py_ssize_t endinpos; |
| 3242 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3243 | PyUnicodeObject *v; |
| 3244 | Py_UNICODE *p; |
| 3245 | const char *end; |
| 3246 | const char *reason; |
| 3247 | PyObject *errorHandler = NULL; |
| 3248 | PyObject *exc = NULL; |
| 3249 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 3250 | #ifdef Py_UNICODE_WIDE |
| 3251 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 3252 | #endif |
| 3253 | |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 3254 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3255 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 3256 | if (v == NULL) |
| 3257 | goto onError; |
| 3258 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
| 3259 | return (PyObject *)v; |
| 3260 | p = PyUnicode_AS_UNICODE(v); |
| 3261 | end = s + size; |
| 3262 | |
| 3263 | while (s < end) { |
Neal Norwitz | 1004a53 | 2006-05-15 07:17:23 +0000 | [diff] [blame] | 3264 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3265 | /* We have to sanity check the raw data, otherwise doom looms for |
| 3266 | some malformed UCS-4 data. */ |
| 3267 | if ( |
| 3268 | #ifdef Py_UNICODE_WIDE |
| 3269 | *p > unimax || *p < 0 || |
| 3270 | #endif |
| 3271 | end-s < Py_UNICODE_SIZE |
| 3272 | ) |
| 3273 | { |
| 3274 | startinpos = s - starts; |
| 3275 | if (end-s < Py_UNICODE_SIZE) { |
| 3276 | endinpos = end-starts; |
| 3277 | reason = "truncated input"; |
| 3278 | } |
| 3279 | else { |
| 3280 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 3281 | reason = "illegal code point (> 0x10FFFF)"; |
| 3282 | } |
| 3283 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 3284 | if (unicode_decode_call_errorhandler( |
| 3285 | errors, &errorHandler, |
| 3286 | "unicode_internal", reason, |
| 3287 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3288 | (PyObject **)&v, &outpos, &p)) { |
| 3289 | goto onError; |
| 3290 | } |
| 3291 | } |
| 3292 | else { |
| 3293 | p++; |
| 3294 | s += Py_UNICODE_SIZE; |
| 3295 | } |
| 3296 | } |
| 3297 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3298 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3299 | goto onError; |
| 3300 | Py_XDECREF(errorHandler); |
| 3301 | Py_XDECREF(exc); |
| 3302 | return (PyObject *)v; |
| 3303 | |
| 3304 | onError: |
| 3305 | Py_XDECREF(v); |
| 3306 | Py_XDECREF(errorHandler); |
| 3307 | Py_XDECREF(exc); |
| 3308 | return NULL; |
| 3309 | } |
| 3310 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3311 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 3312 | |
| 3313 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3314 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3315 | const char *errors) |
| 3316 | { |
| 3317 | PyUnicodeObject *v; |
| 3318 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3319 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3320 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3321 | if (size == 1) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3322 | Py_UNICODE r = *(unsigned char*)s; |
| 3323 | return PyUnicode_FromUnicode(&r, 1); |
| 3324 | } |
| 3325 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3326 | v = _PyUnicode_New(size); |
| 3327 | if (v == NULL) |
| 3328 | goto onError; |
| 3329 | if (size == 0) |
| 3330 | return (PyObject *)v; |
| 3331 | p = PyUnicode_AS_UNICODE(v); |
| 3332 | while (size-- > 0) |
| 3333 | *p++ = (unsigned char)*s++; |
| 3334 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3335 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3336 | onError: |
| 3337 | Py_XDECREF(v); |
| 3338 | return NULL; |
| 3339 | } |
| 3340 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3341 | /* create or adjust a UnicodeEncodeError */ |
| 3342 | static void make_encode_exception(PyObject **exceptionObject, |
| 3343 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3344 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3345 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3346 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3347 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3348 | if (*exceptionObject == NULL) { |
| 3349 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 3350 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3351 | } |
| 3352 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3353 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 3354 | goto onError; |
| 3355 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 3356 | goto onError; |
| 3357 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 3358 | goto onError; |
| 3359 | return; |
| 3360 | onError: |
| 3361 | Py_DECREF(*exceptionObject); |
| 3362 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3363 | } |
| 3364 | } |
| 3365 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3366 | /* raises a UnicodeEncodeError */ |
| 3367 | static void raise_encode_exception(PyObject **exceptionObject, |
| 3368 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3369 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3370 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3371 | const char *reason) |
| 3372 | { |
| 3373 | make_encode_exception(exceptionObject, |
| 3374 | encoding, unicode, size, startpos, endpos, reason); |
| 3375 | if (*exceptionObject != NULL) |
| 3376 | PyCodec_StrictErrors(*exceptionObject); |
| 3377 | } |
| 3378 | |
| 3379 | /* error handling callback helper: |
| 3380 | build arguments, call the callback and check the arguments, |
| 3381 | put the result into newpos and return the replacement string, which |
| 3382 | has to be freed by the caller */ |
| 3383 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 3384 | PyObject **errorHandler, |
| 3385 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3386 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 3387 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3388 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3389 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3390 | 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] | 3391 | |
| 3392 | PyObject *restuple; |
| 3393 | PyObject *resunicode; |
| 3394 | |
| 3395 | if (*errorHandler == NULL) { |
| 3396 | *errorHandler = PyCodec_LookupError(errors); |
| 3397 | if (*errorHandler == NULL) |
| 3398 | return NULL; |
| 3399 | } |
| 3400 | |
| 3401 | make_encode_exception(exceptionObject, |
| 3402 | encoding, unicode, size, startpos, endpos, reason); |
| 3403 | if (*exceptionObject == NULL) |
| 3404 | return NULL; |
| 3405 | |
| 3406 | restuple = PyObject_CallFunctionObjArgs( |
| 3407 | *errorHandler, *exceptionObject, NULL); |
| 3408 | if (restuple == NULL) |
| 3409 | return NULL; |
| 3410 | if (!PyTuple_Check(restuple)) { |
| 3411 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 3412 | Py_DECREF(restuple); |
| 3413 | return NULL; |
| 3414 | } |
| 3415 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 3416 | &resunicode, newpos)) { |
| 3417 | Py_DECREF(restuple); |
| 3418 | return NULL; |
| 3419 | } |
| 3420 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3421 | *newpos = size+*newpos; |
| 3422 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 3423 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3424 | Py_DECREF(restuple); |
| 3425 | return NULL; |
| 3426 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3427 | Py_INCREF(resunicode); |
| 3428 | Py_DECREF(restuple); |
| 3429 | return resunicode; |
| 3430 | } |
| 3431 | |
| 3432 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3433 | Py_ssize_t size, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3434 | const char *errors, |
| 3435 | int limit) |
| 3436 | { |
| 3437 | /* output object */ |
| 3438 | PyObject *res; |
| 3439 | /* pointers to the beginning and end+1 of input */ |
| 3440 | const Py_UNICODE *startp = p; |
| 3441 | const Py_UNICODE *endp = p + size; |
| 3442 | /* pointer to the beginning of the unencodable characters */ |
| 3443 | /* const Py_UNICODE *badp = NULL; */ |
| 3444 | /* pointer into the output */ |
| 3445 | char *str; |
| 3446 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3447 | Py_ssize_t respos = 0; |
| 3448 | Py_ssize_t ressize; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3449 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 3450 | 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] | 3451 | PyObject *errorHandler = NULL; |
| 3452 | PyObject *exc = NULL; |
| 3453 | /* the following variable is used for caching string comparisons |
| 3454 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 3455 | int known_errorHandler = -1; |
| 3456 | |
| 3457 | /* allocate enough for a simple encoding without |
| 3458 | replacements, if we need more, we'll resize */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3459 | res = PyString_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3460 | if (res == NULL) |
| 3461 | goto onError; |
| 3462 | if (size == 0) |
| 3463 | return res; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3464 | str = PyString_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3465 | ressize = size; |
| 3466 | |
| 3467 | while (p<endp) { |
| 3468 | Py_UNICODE c = *p; |
| 3469 | |
| 3470 | /* can we encode this? */ |
| 3471 | if (c<limit) { |
| 3472 | /* no overflow check, because we know that the space is enough */ |
| 3473 | *str++ = (char)c; |
| 3474 | ++p; |
| 3475 | } |
| 3476 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3477 | Py_ssize_t unicodepos = p-startp; |
| 3478 | Py_ssize_t requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3479 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3480 | Py_ssize_t repsize; |
| 3481 | Py_ssize_t newpos; |
| 3482 | Py_ssize_t respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3483 | Py_UNICODE *uni2; |
| 3484 | /* startpos for collecting unencodable chars */ |
| 3485 | const Py_UNICODE *collstart = p; |
| 3486 | const Py_UNICODE *collend = p; |
| 3487 | /* find all unecodable characters */ |
| 3488 | while ((collend < endp) && ((*collend)>=limit)) |
| 3489 | ++collend; |
| 3490 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 3491 | if (known_errorHandler==-1) { |
| 3492 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3493 | known_errorHandler = 1; |
| 3494 | else if (!strcmp(errors, "replace")) |
| 3495 | known_errorHandler = 2; |
| 3496 | else if (!strcmp(errors, "ignore")) |
| 3497 | known_errorHandler = 3; |
| 3498 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3499 | known_errorHandler = 4; |
| 3500 | else |
| 3501 | known_errorHandler = 0; |
| 3502 | } |
| 3503 | switch (known_errorHandler) { |
| 3504 | case 1: /* strict */ |
| 3505 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 3506 | goto onError; |
| 3507 | case 2: /* replace */ |
| 3508 | while (collstart++<collend) |
| 3509 | *str++ = '?'; /* fall through */ |
| 3510 | case 3: /* ignore */ |
| 3511 | p = collend; |
| 3512 | break; |
| 3513 | case 4: /* xmlcharrefreplace */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3514 | respos = str-PyString_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3515 | /* determine replacement size (temporarily (mis)uses p) */ |
| 3516 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 3517 | if (*p<10) |
| 3518 | repsize += 2+1+1; |
| 3519 | else if (*p<100) |
| 3520 | repsize += 2+2+1; |
| 3521 | else if (*p<1000) |
| 3522 | repsize += 2+3+1; |
| 3523 | else if (*p<10000) |
| 3524 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 3525 | #ifndef Py_UNICODE_WIDE |
| 3526 | else |
| 3527 | repsize += 2+5+1; |
| 3528 | #else |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3529 | else if (*p<100000) |
| 3530 | repsize += 2+5+1; |
| 3531 | else if (*p<1000000) |
| 3532 | repsize += 2+6+1; |
| 3533 | else |
| 3534 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3535 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3536 | } |
| 3537 | requiredsize = respos+repsize+(endp-collend); |
| 3538 | if (requiredsize > ressize) { |
| 3539 | if (requiredsize<2*ressize) |
| 3540 | requiredsize = 2*ressize; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3541 | if (_PyString_Resize(&res, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3542 | goto onError; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3543 | str = PyString_AS_STRING(res) + respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3544 | ressize = requiredsize; |
| 3545 | } |
| 3546 | /* generate replacement (temporarily (mis)uses p) */ |
| 3547 | for (p = collstart; p < collend; ++p) { |
| 3548 | str += sprintf(str, "&#%d;", (int)*p); |
| 3549 | } |
| 3550 | p = collend; |
| 3551 | break; |
| 3552 | default: |
| 3553 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 3554 | encoding, reason, startp, size, &exc, |
| 3555 | collstart-startp, collend-startp, &newpos); |
| 3556 | if (repunicode == NULL) |
| 3557 | goto onError; |
| 3558 | /* need more space? (at least enough for what we |
| 3559 | have+the replacement+the rest of the string, so |
| 3560 | we won't have to check space for encodable characters) */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3561 | respos = str-PyString_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3562 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3563 | requiredsize = respos+repsize+(endp-collend); |
| 3564 | if (requiredsize > ressize) { |
| 3565 | if (requiredsize<2*ressize) |
| 3566 | requiredsize = 2*ressize; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3567 | if (_PyString_Resize(&res, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3568 | Py_DECREF(repunicode); |
| 3569 | goto onError; |
| 3570 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3571 | str = PyString_AS_STRING(res) + respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3572 | ressize = requiredsize; |
| 3573 | } |
| 3574 | /* check if there is anything unencodable in the replacement |
| 3575 | and copy it to the output */ |
| 3576 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 3577 | c = *uni2; |
| 3578 | if (c >= limit) { |
| 3579 | raise_encode_exception(&exc, encoding, startp, size, |
| 3580 | unicodepos, unicodepos+1, reason); |
| 3581 | Py_DECREF(repunicode); |
| 3582 | goto onError; |
| 3583 | } |
| 3584 | *str = (char)c; |
| 3585 | } |
| 3586 | p = startp + newpos; |
| 3587 | Py_DECREF(repunicode); |
| 3588 | } |
| 3589 | } |
| 3590 | } |
| 3591 | /* Resize if we allocated to much */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3592 | respos = str-PyString_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3593 | if (respos<ressize) |
| 3594 | /* If this falls res will be NULL */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3595 | _PyString_Resize(&res, respos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3596 | Py_XDECREF(errorHandler); |
| 3597 | Py_XDECREF(exc); |
| 3598 | return res; |
| 3599 | |
| 3600 | onError: |
| 3601 | Py_XDECREF(res); |
| 3602 | Py_XDECREF(errorHandler); |
| 3603 | Py_XDECREF(exc); |
| 3604 | return NULL; |
| 3605 | } |
| 3606 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3607 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3608 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3609 | const char *errors) |
| 3610 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3611 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3612 | } |
| 3613 | |
| 3614 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 3615 | { |
| 3616 | if (!PyUnicode_Check(unicode)) { |
| 3617 | PyErr_BadArgument(); |
| 3618 | return NULL; |
| 3619 | } |
| 3620 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 3621 | PyUnicode_GET_SIZE(unicode), |
| 3622 | NULL); |
| 3623 | } |
| 3624 | |
| 3625 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 3626 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3627 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3628 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3629 | const char *errors) |
| 3630 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3631 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3632 | PyUnicodeObject *v; |
| 3633 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3634 | Py_ssize_t startinpos; |
| 3635 | Py_ssize_t endinpos; |
| 3636 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3637 | const char *e; |
| 3638 | PyObject *errorHandler = NULL; |
| 3639 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3640 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3641 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3642 | if (size == 1 && *(unsigned char*)s < 128) { |
| 3643 | Py_UNICODE r = *(unsigned char*)s; |
| 3644 | return PyUnicode_FromUnicode(&r, 1); |
| 3645 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3646 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3647 | v = _PyUnicode_New(size); |
| 3648 | if (v == NULL) |
| 3649 | goto onError; |
| 3650 | if (size == 0) |
| 3651 | return (PyObject *)v; |
| 3652 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3653 | e = s + size; |
| 3654 | while (s < e) { |
| 3655 | register unsigned char c = (unsigned char)*s; |
| 3656 | if (c < 128) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3657 | *p++ = c; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3658 | ++s; |
| 3659 | } |
| 3660 | else { |
| 3661 | startinpos = s-starts; |
| 3662 | endinpos = startinpos + 1; |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 3663 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3664 | if (unicode_decode_call_errorhandler( |
| 3665 | errors, &errorHandler, |
| 3666 | "ascii", "ordinal not in range(128)", |
| 3667 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3668 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3669 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3670 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3671 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3672 | if (p - PyUnicode_AS_UNICODE(v) < PyString_GET_SIZE(v)) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3673 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3674 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3675 | Py_XDECREF(errorHandler); |
| 3676 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3677 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3678 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3679 | onError: |
| 3680 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3681 | Py_XDECREF(errorHandler); |
| 3682 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3683 | return NULL; |
| 3684 | } |
| 3685 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3686 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3687 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3688 | const char *errors) |
| 3689 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3690 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3691 | } |
| 3692 | |
| 3693 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 3694 | { |
| 3695 | if (!PyUnicode_Check(unicode)) { |
| 3696 | PyErr_BadArgument(); |
| 3697 | return NULL; |
| 3698 | } |
| 3699 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 3700 | PyUnicode_GET_SIZE(unicode), |
| 3701 | NULL); |
| 3702 | } |
| 3703 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3704 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3705 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3706 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3707 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3708 | #if SIZEOF_INT < SIZEOF_SSIZE_T |
| 3709 | #define NEED_RETRY |
| 3710 | #endif |
| 3711 | |
| 3712 | /* XXX This code is limited to "true" double-byte encodings, as |
| 3713 | a) it assumes an incomplete character consists of a single byte, and |
| 3714 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
| 3715 | encodings, see IsDBCSLeadByteEx documentation. */ |
| 3716 | |
| 3717 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 3718 | { |
| 3719 | const char *curr = s + offset; |
| 3720 | |
| 3721 | if (IsDBCSLeadByte(*curr)) { |
| 3722 | const char *prev = CharPrev(s, curr); |
| 3723 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
| 3724 | } |
| 3725 | return 0; |
| 3726 | } |
| 3727 | |
| 3728 | /* |
| 3729 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 3730 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 3731 | */ |
| 3732 | static int decode_mbcs(PyUnicodeObject **v, |
| 3733 | const char *s, /* MBCS string */ |
| 3734 | int size, /* sizeof MBCS string */ |
| 3735 | int final) |
| 3736 | { |
| 3737 | Py_UNICODE *p; |
| 3738 | Py_ssize_t n = 0; |
| 3739 | int usize = 0; |
| 3740 | |
| 3741 | assert(size >= 0); |
| 3742 | |
| 3743 | /* Skip trailing lead-byte unless 'final' is set */ |
| 3744 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
| 3745 | --size; |
| 3746 | |
| 3747 | /* First get the size of the result */ |
| 3748 | if (size > 0) { |
| 3749 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 3750 | if (usize == 0) { |
| 3751 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3752 | return -1; |
| 3753 | } |
| 3754 | } |
| 3755 | |
| 3756 | if (*v == NULL) { |
| 3757 | /* Create unicode object */ |
| 3758 | *v = _PyUnicode_New(usize); |
| 3759 | if (*v == NULL) |
| 3760 | return -1; |
| 3761 | } |
| 3762 | else { |
| 3763 | /* Extend unicode object */ |
| 3764 | n = PyUnicode_GET_SIZE(*v); |
| 3765 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 3766 | return -1; |
| 3767 | } |
| 3768 | |
| 3769 | /* Do the conversion */ |
| 3770 | if (size > 0) { |
| 3771 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 3772 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 3773 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3774 | return -1; |
| 3775 | } |
| 3776 | } |
| 3777 | |
| 3778 | return size; |
| 3779 | } |
| 3780 | |
| 3781 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
| 3782 | Py_ssize_t size, |
| 3783 | const char *errors, |
| 3784 | Py_ssize_t *consumed) |
| 3785 | { |
| 3786 | PyUnicodeObject *v = NULL; |
| 3787 | int done; |
| 3788 | |
| 3789 | if (consumed) |
| 3790 | *consumed = 0; |
| 3791 | |
| 3792 | #ifdef NEED_RETRY |
| 3793 | retry: |
| 3794 | if (size > INT_MAX) |
| 3795 | done = decode_mbcs(&v, s, INT_MAX, 0); |
| 3796 | else |
| 3797 | #endif |
| 3798 | done = decode_mbcs(&v, s, (int)size, !consumed); |
| 3799 | |
| 3800 | if (done < 0) { |
| 3801 | Py_XDECREF(v); |
| 3802 | return NULL; |
| 3803 | } |
| 3804 | |
| 3805 | if (consumed) |
| 3806 | *consumed += done; |
| 3807 | |
| 3808 | #ifdef NEED_RETRY |
| 3809 | if (size > INT_MAX) { |
| 3810 | s += done; |
| 3811 | size -= done; |
| 3812 | goto retry; |
| 3813 | } |
| 3814 | #endif |
| 3815 | |
| 3816 | return (PyObject *)v; |
| 3817 | } |
| 3818 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3819 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3820 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3821 | const char *errors) |
| 3822 | { |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3823 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 3824 | } |
| 3825 | |
| 3826 | /* |
| 3827 | * Convert unicode into string object (MBCS). |
| 3828 | * Returns 0 if succeed, -1 otherwise. |
| 3829 | */ |
| 3830 | static int encode_mbcs(PyObject **repr, |
| 3831 | const Py_UNICODE *p, /* unicode */ |
| 3832 | int size) /* size of unicode */ |
| 3833 | { |
| 3834 | int mbcssize = 0; |
| 3835 | Py_ssize_t n = 0; |
| 3836 | |
| 3837 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3838 | |
| 3839 | /* First get the size of the result */ |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3840 | if (size > 0) { |
| 3841 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 3842 | if (mbcssize == 0) { |
| 3843 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3844 | return -1; |
| 3845 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3846 | } |
| 3847 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3848 | if (*repr == NULL) { |
| 3849 | /* Create string object */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3850 | *repr = PyString_FromStringAndSize(NULL, mbcssize); |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3851 | if (*repr == NULL) |
| 3852 | return -1; |
| 3853 | } |
| 3854 | else { |
| 3855 | /* Extend string object */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3856 | n = PyString_Size(*repr); |
| 3857 | if (_PyString_Resize(repr, n + mbcssize) < 0) |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3858 | return -1; |
| 3859 | } |
| 3860 | |
| 3861 | /* Do the conversion */ |
| 3862 | if (size > 0) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3863 | char *s = PyString_AS_STRING(*repr) + n; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3864 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 3865 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3866 | return -1; |
| 3867 | } |
| 3868 | } |
| 3869 | |
| 3870 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3871 | } |
| 3872 | |
| 3873 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3874 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3875 | const char *errors) |
| 3876 | { |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3877 | PyObject *repr = NULL; |
| 3878 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 3879 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3880 | #ifdef NEED_RETRY |
| 3881 | retry: |
| 3882 | if (size > INT_MAX) |
| 3883 | ret = encode_mbcs(&repr, p, INT_MAX); |
| 3884 | else |
| 3885 | #endif |
| 3886 | ret = encode_mbcs(&repr, p, (int)size); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3887 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3888 | if (ret < 0) { |
| 3889 | Py_XDECREF(repr); |
| 3890 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3891 | } |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3892 | |
| 3893 | #ifdef NEED_RETRY |
| 3894 | if (size > INT_MAX) { |
| 3895 | p += INT_MAX; |
| 3896 | size -= INT_MAX; |
| 3897 | goto retry; |
| 3898 | } |
| 3899 | #endif |
| 3900 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3901 | return repr; |
| 3902 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3903 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3904 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 3905 | { |
| 3906 | if (!PyUnicode_Check(unicode)) { |
| 3907 | PyErr_BadArgument(); |
| 3908 | return NULL; |
| 3909 | } |
| 3910 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 3911 | PyUnicode_GET_SIZE(unicode), |
| 3912 | NULL); |
| 3913 | } |
| 3914 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3915 | #undef NEED_RETRY |
| 3916 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3917 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3918 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3919 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 3920 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3921 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3922 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3923 | PyObject *mapping, |
| 3924 | const char *errors) |
| 3925 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3926 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3927 | Py_ssize_t startinpos; |
| 3928 | Py_ssize_t endinpos; |
| 3929 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3930 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3931 | PyUnicodeObject *v; |
| 3932 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3933 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3934 | PyObject *errorHandler = NULL; |
| 3935 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3936 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3937 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3938 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3939 | /* Default to Latin-1 */ |
| 3940 | if (mapping == NULL) |
| 3941 | return PyUnicode_DecodeLatin1(s, size, errors); |
| 3942 | |
| 3943 | v = _PyUnicode_New(size); |
| 3944 | if (v == NULL) |
| 3945 | goto onError; |
| 3946 | if (size == 0) |
| 3947 | return (PyObject *)v; |
| 3948 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3949 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3950 | if (PyUnicode_CheckExact(mapping)) { |
| 3951 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 3952 | maplen = PyUnicode_GET_SIZE(mapping); |
| 3953 | while (s < e) { |
| 3954 | unsigned char ch = *s; |
| 3955 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3956 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3957 | if (ch < maplen) |
| 3958 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3959 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3960 | if (x == 0xfffe) { |
| 3961 | /* undefined mapping */ |
| 3962 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3963 | startinpos = s-starts; |
| 3964 | endinpos = startinpos+1; |
| 3965 | if (unicode_decode_call_errorhandler( |
| 3966 | errors, &errorHandler, |
| 3967 | "charmap", "character maps to <undefined>", |
| 3968 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3969 | (PyObject **)&v, &outpos, &p)) { |
| 3970 | goto onError; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 3971 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3972 | continue; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 3973 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3974 | *p++ = x; |
| 3975 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3976 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3977 | } |
| 3978 | else { |
| 3979 | while (s < e) { |
| 3980 | unsigned char ch = *s; |
| 3981 | PyObject *w, *x; |
| 3982 | |
| 3983 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 3984 | w = PyInt_FromLong((long)ch); |
| 3985 | if (w == NULL) |
| 3986 | goto onError; |
| 3987 | x = PyObject_GetItem(mapping, w); |
| 3988 | Py_DECREF(w); |
| 3989 | if (x == NULL) { |
| 3990 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3991 | /* No mapping found means: mapping is undefined. */ |
| 3992 | PyErr_Clear(); |
| 3993 | x = Py_None; |
| 3994 | Py_INCREF(x); |
| 3995 | } else |
| 3996 | goto onError; |
| 3997 | } |
| 3998 | |
| 3999 | /* Apply mapping */ |
| 4000 | if (PyInt_Check(x)) { |
| 4001 | long value = PyInt_AS_LONG(x); |
| 4002 | if (value < 0 || value > 65535) { |
| 4003 | PyErr_SetString(PyExc_TypeError, |
| 4004 | "character mapping must be in range(65536)"); |
| 4005 | Py_DECREF(x); |
| 4006 | goto onError; |
| 4007 | } |
| 4008 | *p++ = (Py_UNICODE)value; |
| 4009 | } |
| 4010 | else if (x == Py_None) { |
| 4011 | /* undefined mapping */ |
| 4012 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4013 | startinpos = s-starts; |
| 4014 | endinpos = startinpos+1; |
| 4015 | if (unicode_decode_call_errorhandler( |
| 4016 | errors, &errorHandler, |
| 4017 | "charmap", "character maps to <undefined>", |
| 4018 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 4019 | (PyObject **)&v, &outpos, &p)) { |
| 4020 | Py_DECREF(x); |
| 4021 | goto onError; |
| 4022 | } |
Walter Dörwald | d4fff17 | 2005-11-28 22:15:56 +0000 | [diff] [blame] | 4023 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4024 | continue; |
| 4025 | } |
| 4026 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4027 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4028 | |
| 4029 | if (targetsize == 1) |
| 4030 | /* 1-1 mapping */ |
| 4031 | *p++ = *PyUnicode_AS_UNICODE(x); |
| 4032 | |
| 4033 | else if (targetsize > 1) { |
| 4034 | /* 1-n mapping */ |
| 4035 | if (targetsize > extrachars) { |
| 4036 | /* resize first */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4037 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 4038 | Py_ssize_t needed = (targetsize - extrachars) + \ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4039 | (targetsize << 2); |
| 4040 | extrachars += needed; |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 4041 | /* XXX overflow detection missing */ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4042 | if (_PyUnicode_Resize(&v, |
| 4043 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 4044 | Py_DECREF(x); |
| 4045 | goto onError; |
| 4046 | } |
| 4047 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 4048 | } |
| 4049 | Py_UNICODE_COPY(p, |
| 4050 | PyUnicode_AS_UNICODE(x), |
| 4051 | targetsize); |
| 4052 | p += targetsize; |
| 4053 | extrachars -= targetsize; |
| 4054 | } |
| 4055 | /* 1-0 mapping: skip the character */ |
| 4056 | } |
| 4057 | else { |
| 4058 | /* wrong return value */ |
| 4059 | PyErr_SetString(PyExc_TypeError, |
| 4060 | "character mapping must return integer, None or unicode"); |
| 4061 | Py_DECREF(x); |
| 4062 | goto onError; |
| 4063 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4064 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4065 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4066 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4067 | } |
| 4068 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4069 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4070 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4071 | Py_XDECREF(errorHandler); |
| 4072 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4073 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4074 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4075 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4076 | Py_XDECREF(errorHandler); |
| 4077 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4078 | Py_XDECREF(v); |
| 4079 | return NULL; |
| 4080 | } |
| 4081 | |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4082 | /* Charmap encoding: the lookup table */ |
| 4083 | |
| 4084 | struct encoding_map{ |
| 4085 | PyObject_HEAD |
| 4086 | unsigned char level1[32]; |
| 4087 | int count2, count3; |
| 4088 | unsigned char level23[1]; |
| 4089 | }; |
| 4090 | |
| 4091 | static PyObject* |
| 4092 | encoding_map_size(PyObject *obj, PyObject* args) |
| 4093 | { |
| 4094 | struct encoding_map *map = (struct encoding_map*)obj; |
| 4095 | return PyInt_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
| 4096 | 128*map->count3); |
| 4097 | } |
| 4098 | |
| 4099 | static PyMethodDef encoding_map_methods[] = { |
| 4100 | {"size", encoding_map_size, METH_NOARGS, |
| 4101 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 4102 | { 0 } |
| 4103 | }; |
| 4104 | |
| 4105 | static void |
| 4106 | encoding_map_dealloc(PyObject* o) |
| 4107 | { |
| 4108 | PyObject_FREE(o); |
| 4109 | } |
| 4110 | |
| 4111 | static PyTypeObject EncodingMapType = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 4112 | PyVarObject_HEAD_INIT(NULL, 0) |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4113 | "EncodingMap", /*tp_name*/ |
| 4114 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 4115 | 0, /*tp_itemsize*/ |
| 4116 | /* methods */ |
| 4117 | encoding_map_dealloc, /*tp_dealloc*/ |
| 4118 | 0, /*tp_print*/ |
| 4119 | 0, /*tp_getattr*/ |
| 4120 | 0, /*tp_setattr*/ |
| 4121 | 0, /*tp_compare*/ |
| 4122 | 0, /*tp_repr*/ |
| 4123 | 0, /*tp_as_number*/ |
| 4124 | 0, /*tp_as_sequence*/ |
| 4125 | 0, /*tp_as_mapping*/ |
| 4126 | 0, /*tp_hash*/ |
| 4127 | 0, /*tp_call*/ |
| 4128 | 0, /*tp_str*/ |
| 4129 | 0, /*tp_getattro*/ |
| 4130 | 0, /*tp_setattro*/ |
| 4131 | 0, /*tp_as_buffer*/ |
| 4132 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 4133 | 0, /*tp_doc*/ |
| 4134 | 0, /*tp_traverse*/ |
| 4135 | 0, /*tp_clear*/ |
| 4136 | 0, /*tp_richcompare*/ |
| 4137 | 0, /*tp_weaklistoffset*/ |
| 4138 | 0, /*tp_iter*/ |
| 4139 | 0, /*tp_iternext*/ |
| 4140 | encoding_map_methods, /*tp_methods*/ |
| 4141 | 0, /*tp_members*/ |
| 4142 | 0, /*tp_getset*/ |
| 4143 | 0, /*tp_base*/ |
| 4144 | 0, /*tp_dict*/ |
| 4145 | 0, /*tp_descr_get*/ |
| 4146 | 0, /*tp_descr_set*/ |
| 4147 | 0, /*tp_dictoffset*/ |
| 4148 | 0, /*tp_init*/ |
| 4149 | 0, /*tp_alloc*/ |
| 4150 | 0, /*tp_new*/ |
| 4151 | 0, /*tp_free*/ |
| 4152 | 0, /*tp_is_gc*/ |
| 4153 | }; |
| 4154 | |
| 4155 | PyObject* |
| 4156 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 4157 | { |
| 4158 | Py_UNICODE *decode; |
| 4159 | PyObject *result; |
| 4160 | struct encoding_map *mresult; |
| 4161 | int i; |
| 4162 | int need_dict = 0; |
| 4163 | unsigned char level1[32]; |
| 4164 | unsigned char level2[512]; |
| 4165 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 4166 | int count2 = 0, count3 = 0; |
| 4167 | |
| 4168 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 4169 | PyErr_BadArgument(); |
| 4170 | return NULL; |
| 4171 | } |
| 4172 | decode = PyUnicode_AS_UNICODE(string); |
| 4173 | memset(level1, 0xFF, sizeof level1); |
| 4174 | memset(level2, 0xFF, sizeof level2); |
| 4175 | |
| 4176 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 4177 | or if there are non-BMP characters, we need to use |
| 4178 | a mapping dictionary. */ |
| 4179 | if (decode[0] != 0) |
| 4180 | need_dict = 1; |
| 4181 | for (i = 1; i < 256; i++) { |
| 4182 | int l1, l2; |
| 4183 | if (decode[i] == 0 |
| 4184 | #ifdef Py_UNICODE_WIDE |
| 4185 | || decode[i] > 0xFFFF |
| 4186 | #endif |
| 4187 | ) { |
| 4188 | need_dict = 1; |
| 4189 | break; |
| 4190 | } |
| 4191 | if (decode[i] == 0xFFFE) |
| 4192 | /* unmapped character */ |
| 4193 | continue; |
| 4194 | l1 = decode[i] >> 11; |
| 4195 | l2 = decode[i] >> 7; |
| 4196 | if (level1[l1] == 0xFF) |
| 4197 | level1[l1] = count2++; |
| 4198 | if (level2[l2] == 0xFF) |
| 4199 | level2[l2] = count3++; |
| 4200 | } |
| 4201 | |
| 4202 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 4203 | need_dict = 1; |
| 4204 | |
| 4205 | if (need_dict) { |
| 4206 | PyObject *result = PyDict_New(); |
| 4207 | PyObject *key, *value; |
| 4208 | if (!result) |
| 4209 | return NULL; |
| 4210 | for (i = 0; i < 256; i++) { |
| 4211 | key = value = NULL; |
| 4212 | key = PyInt_FromLong(decode[i]); |
| 4213 | value = PyInt_FromLong(i); |
| 4214 | if (!key || !value) |
| 4215 | goto failed1; |
| 4216 | if (PyDict_SetItem(result, key, value) == -1) |
| 4217 | goto failed1; |
Georg Brandl | 9f16760 | 2006-06-04 21:46:16 +0000 | [diff] [blame] | 4218 | Py_DECREF(key); |
| 4219 | Py_DECREF(value); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4220 | } |
| 4221 | return result; |
| 4222 | failed1: |
| 4223 | Py_XDECREF(key); |
| 4224 | Py_XDECREF(value); |
| 4225 | Py_DECREF(result); |
| 4226 | return NULL; |
| 4227 | } |
| 4228 | |
| 4229 | /* Create a three-level trie */ |
| 4230 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 4231 | 16*count2 + 128*count3 - 1); |
| 4232 | if (!result) |
| 4233 | return PyErr_NoMemory(); |
| 4234 | PyObject_Init(result, &EncodingMapType); |
| 4235 | mresult = (struct encoding_map*)result; |
| 4236 | mresult->count2 = count2; |
| 4237 | mresult->count3 = count3; |
| 4238 | mlevel1 = mresult->level1; |
| 4239 | mlevel2 = mresult->level23; |
| 4240 | mlevel3 = mresult->level23 + 16*count2; |
| 4241 | memcpy(mlevel1, level1, 32); |
| 4242 | memset(mlevel2, 0xFF, 16*count2); |
| 4243 | memset(mlevel3, 0, 128*count3); |
| 4244 | count3 = 0; |
| 4245 | for (i = 1; i < 256; i++) { |
| 4246 | int o1, o2, o3, i2, i3; |
| 4247 | if (decode[i] == 0xFFFE) |
| 4248 | /* unmapped character */ |
| 4249 | continue; |
| 4250 | o1 = decode[i]>>11; |
| 4251 | o2 = (decode[i]>>7) & 0xF; |
| 4252 | i2 = 16*mlevel1[o1] + o2; |
| 4253 | if (mlevel2[i2] == 0xFF) |
| 4254 | mlevel2[i2] = count3++; |
| 4255 | o3 = decode[i] & 0x7F; |
| 4256 | i3 = 128*mlevel2[i2] + o3; |
| 4257 | mlevel3[i3] = i; |
| 4258 | } |
| 4259 | return result; |
| 4260 | } |
| 4261 | |
| 4262 | static int |
| 4263 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 4264 | { |
| 4265 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 4266 | int l1 = c>>11; |
| 4267 | int l2 = (c>>7) & 0xF; |
| 4268 | int l3 = c & 0x7F; |
| 4269 | int i; |
| 4270 | |
| 4271 | #ifdef Py_UNICODE_WIDE |
| 4272 | if (c > 0xFFFF) { |
| 4273 | return -1; |
| 4274 | } |
| 4275 | #endif |
| 4276 | if (c == 0) |
| 4277 | return 0; |
| 4278 | /* level 1*/ |
| 4279 | i = map->level1[l1]; |
| 4280 | if (i == 0xFF) { |
| 4281 | return -1; |
| 4282 | } |
| 4283 | /* level 2*/ |
| 4284 | i = map->level23[16*i+l2]; |
| 4285 | if (i == 0xFF) { |
| 4286 | return -1; |
| 4287 | } |
| 4288 | /* level 3 */ |
| 4289 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 4290 | if (i == 0) { |
| 4291 | return -1; |
| 4292 | } |
| 4293 | return i; |
| 4294 | } |
| 4295 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4296 | /* Lookup the character ch in the mapping. If the character |
| 4297 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4298 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4299 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4300 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4301 | PyObject *w = PyInt_FromLong((long)c); |
| 4302 | PyObject *x; |
| 4303 | |
| 4304 | if (w == NULL) |
| 4305 | return NULL; |
| 4306 | x = PyObject_GetItem(mapping, w); |
| 4307 | Py_DECREF(w); |
| 4308 | if (x == NULL) { |
| 4309 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4310 | /* No mapping found means: mapping is undefined. */ |
| 4311 | PyErr_Clear(); |
| 4312 | x = Py_None; |
| 4313 | Py_INCREF(x); |
| 4314 | return x; |
| 4315 | } else |
| 4316 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4317 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 4318 | else if (x == Py_None) |
| 4319 | return x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4320 | else if (PyInt_Check(x)) { |
| 4321 | long value = PyInt_AS_LONG(x); |
| 4322 | if (value < 0 || value > 255) { |
| 4323 | PyErr_SetString(PyExc_TypeError, |
| 4324 | "character mapping must be in range(256)"); |
| 4325 | Py_DECREF(x); |
| 4326 | return NULL; |
| 4327 | } |
| 4328 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4329 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4330 | else if (PyString_Check(x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4331 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4332 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4333 | /* wrong return value */ |
| 4334 | PyErr_SetString(PyExc_TypeError, |
| 4335 | "character mapping must return integer, None or str"); |
| 4336 | Py_DECREF(x); |
| 4337 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4338 | } |
| 4339 | } |
| 4340 | |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4341 | static int |
| 4342 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
| 4343 | { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4344 | Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4345 | /* exponentially overallocate to minimize reallocations */ |
| 4346 | if (requiredsize < 2*outsize) |
| 4347 | requiredsize = 2*outsize; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4348 | if (_PyString_Resize(outobj, requiredsize)) { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4349 | return 0; |
| 4350 | } |
| 4351 | return 1; |
| 4352 | } |
| 4353 | |
| 4354 | typedef enum charmapencode_result { |
| 4355 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
| 4356 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4357 | /* lookup the character, put the result in the output string and adjust |
| 4358 | various state variables. Reallocate the output string if not enough |
| 4359 | space is available. Return a new reference to the object that |
| 4360 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 4361 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 4362 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4363 | static |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4364 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4365 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4366 | { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4367 | PyObject *rep; |
| 4368 | char *outstart; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4369 | Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4370 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4371 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4372 | int res = encoding_map_lookup(c, mapping); |
| 4373 | Py_ssize_t requiredsize = *outpos+1; |
| 4374 | if (res == -1) |
| 4375 | return enc_FAILED; |
| 4376 | if (outsize<requiredsize) |
| 4377 | if (!charmapencode_resize(outobj, outpos, requiredsize)) |
| 4378 | return enc_EXCEPTION; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4379 | outstart = PyString_AS_STRING(*outobj); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4380 | outstart[(*outpos)++] = (char)res; |
| 4381 | return enc_SUCCESS; |
| 4382 | } |
| 4383 | |
| 4384 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4385 | if (rep==NULL) |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4386 | return enc_EXCEPTION; |
| 4387 | else if (rep==Py_None) { |
| 4388 | Py_DECREF(rep); |
| 4389 | return enc_FAILED; |
| 4390 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4391 | if (PyInt_Check(rep)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4392 | Py_ssize_t requiredsize = *outpos+1; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4393 | if (outsize<requiredsize) |
| 4394 | if (!charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4395 | Py_DECREF(rep); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4396 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4397 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4398 | outstart = PyString_AS_STRING(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4399 | outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); |
| 4400 | } |
| 4401 | else { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4402 | const char *repchars = PyString_AS_STRING(rep); |
| 4403 | Py_ssize_t repsize = PyString_GET_SIZE(rep); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4404 | Py_ssize_t requiredsize = *outpos+repsize; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4405 | if (outsize<requiredsize) |
| 4406 | if (!charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4407 | Py_DECREF(rep); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4408 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4409 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4410 | outstart = PyString_AS_STRING(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4411 | memcpy(outstart + *outpos, repchars, repsize); |
| 4412 | *outpos += repsize; |
| 4413 | } |
| 4414 | } |
Georg Brandl | 9f16760 | 2006-06-04 21:46:16 +0000 | [diff] [blame] | 4415 | Py_DECREF(rep); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4416 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4417 | } |
| 4418 | |
| 4419 | /* handle an error in PyUnicode_EncodeCharmap |
| 4420 | Return 0 on success, -1 on error */ |
| 4421 | static |
| 4422 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4423 | 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] | 4424 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4425 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4426 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4427 | { |
| 4428 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4429 | Py_ssize_t repsize; |
| 4430 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4431 | Py_UNICODE *uni2; |
| 4432 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4433 | Py_ssize_t collstartpos = *inpos; |
| 4434 | Py_ssize_t collendpos = *inpos+1; |
| 4435 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4436 | char *encoding = "charmap"; |
| 4437 | char *reason = "character maps to <undefined>"; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4438 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4439 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4440 | /* find all unencodable characters */ |
| 4441 | while (collendpos < size) { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4442 | PyObject *rep; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4443 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4444 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 4445 | if (res != -1) |
| 4446 | break; |
| 4447 | ++collendpos; |
| 4448 | continue; |
| 4449 | } |
| 4450 | |
| 4451 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 4452 | if (rep==NULL) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4453 | return -1; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4454 | else if (rep!=Py_None) { |
| 4455 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4456 | break; |
| 4457 | } |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4458 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4459 | ++collendpos; |
| 4460 | } |
| 4461 | /* cache callback name lookup |
| 4462 | * (if not done yet, i.e. it's the first error) */ |
| 4463 | if (*known_errorHandler==-1) { |
| 4464 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4465 | *known_errorHandler = 1; |
| 4466 | else if (!strcmp(errors, "replace")) |
| 4467 | *known_errorHandler = 2; |
| 4468 | else if (!strcmp(errors, "ignore")) |
| 4469 | *known_errorHandler = 3; |
| 4470 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4471 | *known_errorHandler = 4; |
| 4472 | else |
| 4473 | *known_errorHandler = 0; |
| 4474 | } |
| 4475 | switch (*known_errorHandler) { |
| 4476 | case 1: /* strict */ |
| 4477 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4478 | return -1; |
| 4479 | case 2: /* replace */ |
| 4480 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 4481 | x = charmapencode_output('?', mapping, res, respos); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4482 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4483 | return -1; |
| 4484 | } |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4485 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4486 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4487 | return -1; |
| 4488 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4489 | } |
| 4490 | /* fall through */ |
| 4491 | case 3: /* ignore */ |
| 4492 | *inpos = collendpos; |
| 4493 | break; |
| 4494 | case 4: /* xmlcharrefreplace */ |
| 4495 | /* generate replacement (temporarily (mis)uses p) */ |
| 4496 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 4497 | char buffer[2+29+1+1]; |
| 4498 | char *cp; |
| 4499 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 4500 | for (cp = buffer; *cp; ++cp) { |
| 4501 | x = charmapencode_output(*cp, mapping, res, respos); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4502 | if (x==enc_EXCEPTION) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4503 | return -1; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4504 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4505 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4506 | return -1; |
| 4507 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4508 | } |
| 4509 | } |
| 4510 | *inpos = collendpos; |
| 4511 | break; |
| 4512 | default: |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4513 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4514 | encoding, reason, p, size, exceptionObject, |
| 4515 | collstartpos, collendpos, &newpos); |
| 4516 | if (repunicode == NULL) |
| 4517 | return -1; |
| 4518 | /* generate replacement */ |
| 4519 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4520 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 4521 | x = charmapencode_output(*uni2, mapping, res, respos); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4522 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4523 | return -1; |
| 4524 | } |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4525 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4526 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4527 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4528 | return -1; |
| 4529 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4530 | } |
| 4531 | *inpos = newpos; |
| 4532 | Py_DECREF(repunicode); |
| 4533 | } |
| 4534 | return 0; |
| 4535 | } |
| 4536 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4537 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4538 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4539 | PyObject *mapping, |
| 4540 | const char *errors) |
| 4541 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4542 | /* output object */ |
| 4543 | PyObject *res = NULL; |
| 4544 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4545 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4546 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4547 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4548 | PyObject *errorHandler = NULL; |
| 4549 | PyObject *exc = NULL; |
| 4550 | /* the following variable is used for caching string comparisons |
| 4551 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4552 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4553 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4554 | |
| 4555 | /* Default to Latin-1 */ |
| 4556 | if (mapping == NULL) |
| 4557 | return PyUnicode_EncodeLatin1(p, size, errors); |
| 4558 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4559 | /* allocate enough for a simple encoding without |
| 4560 | replacements, if we need more, we'll resize */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4561 | res = PyString_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4562 | if (res == NULL) |
| 4563 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 4564 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4565 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4566 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4567 | while (inpos<size) { |
| 4568 | /* try to encode it */ |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4569 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 4570 | if (x==enc_EXCEPTION) /* error */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4571 | goto onError; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4572 | if (x==enc_FAILED) { /* unencodable character */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4573 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 4574 | &exc, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4575 | &known_errorHandler, &errorHandler, errors, |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 4576 | &res, &respos)) { |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 4577 | goto onError; |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 4578 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4579 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4580 | else |
| 4581 | /* done with this character => adjust input position */ |
| 4582 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4583 | } |
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 | /* Resize if we allocated to much */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4586 | if (respos<PyString_GET_SIZE(res)) { |
| 4587 | if (_PyString_Resize(&res, respos)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4588 | goto onError; |
| 4589 | } |
| 4590 | Py_XDECREF(exc); |
| 4591 | Py_XDECREF(errorHandler); |
| 4592 | return res; |
| 4593 | |
| 4594 | onError: |
| 4595 | Py_XDECREF(res); |
| 4596 | Py_XDECREF(exc); |
| 4597 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4598 | return NULL; |
| 4599 | } |
| 4600 | |
| 4601 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 4602 | PyObject *mapping) |
| 4603 | { |
| 4604 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 4605 | PyErr_BadArgument(); |
| 4606 | return NULL; |
| 4607 | } |
| 4608 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 4609 | PyUnicode_GET_SIZE(unicode), |
| 4610 | mapping, |
| 4611 | NULL); |
| 4612 | } |
| 4613 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4614 | /* create or adjust a UnicodeTranslateError */ |
| 4615 | static void make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4616 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4617 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4618 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4619 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4620 | if (*exceptionObject == NULL) { |
| 4621 | *exceptionObject = PyUnicodeTranslateError_Create( |
| 4622 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4623 | } |
| 4624 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4625 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 4626 | goto onError; |
| 4627 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 4628 | goto onError; |
| 4629 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 4630 | goto onError; |
| 4631 | return; |
| 4632 | onError: |
| 4633 | Py_DECREF(*exceptionObject); |
| 4634 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4635 | } |
| 4636 | } |
| 4637 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4638 | /* raises a UnicodeTranslateError */ |
| 4639 | static void raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4640 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4641 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4642 | const char *reason) |
| 4643 | { |
| 4644 | make_translate_exception(exceptionObject, |
| 4645 | unicode, size, startpos, endpos, reason); |
| 4646 | if (*exceptionObject != NULL) |
| 4647 | PyCodec_StrictErrors(*exceptionObject); |
| 4648 | } |
| 4649 | |
| 4650 | /* error handling callback helper: |
| 4651 | build arguments, call the callback and check the arguments, |
| 4652 | put the result into newpos and return the replacement string, which |
| 4653 | has to be freed by the caller */ |
| 4654 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 4655 | PyObject **errorHandler, |
| 4656 | const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4657 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4658 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4659 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4660 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4661 | 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] | 4662 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4663 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4664 | PyObject *restuple; |
| 4665 | PyObject *resunicode; |
| 4666 | |
| 4667 | if (*errorHandler == NULL) { |
| 4668 | *errorHandler = PyCodec_LookupError(errors); |
| 4669 | if (*errorHandler == NULL) |
| 4670 | return NULL; |
| 4671 | } |
| 4672 | |
| 4673 | make_translate_exception(exceptionObject, |
| 4674 | unicode, size, startpos, endpos, reason); |
| 4675 | if (*exceptionObject == NULL) |
| 4676 | return NULL; |
| 4677 | |
| 4678 | restuple = PyObject_CallFunctionObjArgs( |
| 4679 | *errorHandler, *exceptionObject, NULL); |
| 4680 | if (restuple == NULL) |
| 4681 | return NULL; |
| 4682 | if (!PyTuple_Check(restuple)) { |
| 4683 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 4684 | Py_DECREF(restuple); |
| 4685 | return NULL; |
| 4686 | } |
| 4687 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4688 | &resunicode, &i_newpos)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4689 | Py_DECREF(restuple); |
| 4690 | return NULL; |
| 4691 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4692 | if (i_newpos<0) |
| 4693 | *newpos = size+i_newpos; |
| 4694 | else |
| 4695 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4696 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 4697 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4698 | Py_DECREF(restuple); |
| 4699 | return NULL; |
| 4700 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4701 | Py_INCREF(resunicode); |
| 4702 | Py_DECREF(restuple); |
| 4703 | return resunicode; |
| 4704 | } |
| 4705 | |
| 4706 | /* Lookup the character ch in the mapping and put the result in result, |
| 4707 | which must be decrefed by the caller. |
| 4708 | Return 0 on success, -1 on error */ |
| 4709 | static |
| 4710 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 4711 | { |
| 4712 | PyObject *w = PyInt_FromLong((long)c); |
| 4713 | PyObject *x; |
| 4714 | |
| 4715 | if (w == NULL) |
| 4716 | return -1; |
| 4717 | x = PyObject_GetItem(mapping, w); |
| 4718 | Py_DECREF(w); |
| 4719 | if (x == NULL) { |
| 4720 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4721 | /* No mapping found means: use 1:1 mapping. */ |
| 4722 | PyErr_Clear(); |
| 4723 | *result = NULL; |
| 4724 | return 0; |
| 4725 | } else |
| 4726 | return -1; |
| 4727 | } |
| 4728 | else if (x == Py_None) { |
| 4729 | *result = x; |
| 4730 | return 0; |
| 4731 | } |
| 4732 | else if (PyInt_Check(x)) { |
| 4733 | long value = PyInt_AS_LONG(x); |
| 4734 | long max = PyUnicode_GetMax(); |
| 4735 | if (value < 0 || value > max) { |
| 4736 | PyErr_Format(PyExc_TypeError, |
| 4737 | "character mapping must be in range(0x%lx)", max+1); |
| 4738 | Py_DECREF(x); |
| 4739 | return -1; |
| 4740 | } |
| 4741 | *result = x; |
| 4742 | return 0; |
| 4743 | } |
| 4744 | else if (PyUnicode_Check(x)) { |
| 4745 | *result = x; |
| 4746 | return 0; |
| 4747 | } |
| 4748 | else { |
| 4749 | /* wrong return value */ |
| 4750 | PyErr_SetString(PyExc_TypeError, |
| 4751 | "character mapping must return integer, None or unicode"); |
Walter Dörwald | 150523e | 2003-08-15 16:52:19 +0000 | [diff] [blame] | 4752 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4753 | return -1; |
| 4754 | } |
| 4755 | } |
| 4756 | /* ensure that *outobj is at least requiredsize characters long, |
| 4757 | if not reallocate and adjust various state variables. |
| 4758 | Return 0 on success, -1 on error */ |
| 4759 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4760 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4761 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4762 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4763 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4764 | if (requiredsize > oldsize) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4765 | /* remember old output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4766 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4767 | /* exponentially overallocate to minimize reallocations */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4768 | if (requiredsize < 2 * oldsize) |
| 4769 | requiredsize = 2 * oldsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4770 | if (_PyUnicode_Resize(outobj, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4771 | return -1; |
| 4772 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4773 | } |
| 4774 | return 0; |
| 4775 | } |
| 4776 | /* lookup the character, put the result in the output string and adjust |
| 4777 | various state variables. Return a new reference to the object that |
| 4778 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 4779 | undefined (in which case no character was written). |
| 4780 | The called must decref result. |
| 4781 | Return 0 on success, -1 on error. */ |
| 4782 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4783 | int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4784 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4785 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4786 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4787 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4788 | return -1; |
| 4789 | if (*res==NULL) { |
| 4790 | /* not found => default to 1:1 mapping */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4791 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4792 | } |
| 4793 | else if (*res==Py_None) |
| 4794 | ; |
| 4795 | else if (PyInt_Check(*res)) { |
| 4796 | /* no overflow check, because we know that the space is enough */ |
| 4797 | *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); |
| 4798 | } |
| 4799 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4800 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4801 | if (repsize==1) { |
| 4802 | /* no overflow check, because we know that the space is enough */ |
| 4803 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 4804 | } |
| 4805 | else if (repsize!=0) { |
| 4806 | /* more than one character */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4807 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
Walter Dörwald | cd736e7 | 2004-02-05 17:36:00 +0000 | [diff] [blame] | 4808 | (insize - (curinp-startinp)) + |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4809 | repsize - 1; |
| 4810 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4811 | return -1; |
| 4812 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 4813 | *outp += repsize; |
| 4814 | } |
| 4815 | } |
| 4816 | else |
| 4817 | return -1; |
| 4818 | return 0; |
| 4819 | } |
| 4820 | |
| 4821 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4822 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4823 | PyObject *mapping, |
| 4824 | const char *errors) |
| 4825 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4826 | /* output object */ |
| 4827 | PyObject *res = NULL; |
| 4828 | /* pointers to the beginning and end+1 of input */ |
| 4829 | const Py_UNICODE *startp = p; |
| 4830 | const Py_UNICODE *endp = p + size; |
| 4831 | /* pointer into the output */ |
| 4832 | Py_UNICODE *str; |
| 4833 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4834 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4835 | char *reason = "character maps to <undefined>"; |
| 4836 | PyObject *errorHandler = NULL; |
| 4837 | PyObject *exc = NULL; |
| 4838 | /* the following variable is used for caching string comparisons |
| 4839 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4840 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4841 | int known_errorHandler = -1; |
| 4842 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4843 | if (mapping == NULL) { |
| 4844 | PyErr_BadArgument(); |
| 4845 | return NULL; |
| 4846 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4847 | |
| 4848 | /* allocate enough for a simple 1:1 translation without |
| 4849 | replacements, if we need more, we'll resize */ |
| 4850 | res = PyUnicode_FromUnicode(NULL, size); |
| 4851 | if (res == NULL) |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4852 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4853 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4854 | return res; |
| 4855 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4856 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4857 | while (p<endp) { |
| 4858 | /* try to encode it */ |
| 4859 | PyObject *x = NULL; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4860 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4861 | Py_XDECREF(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4862 | goto onError; |
| 4863 | } |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 4864 | Py_XDECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4865 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 4866 | ++p; |
| 4867 | else { /* untranslatable character */ |
| 4868 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4869 | Py_ssize_t repsize; |
| 4870 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4871 | Py_UNICODE *uni2; |
| 4872 | /* startpos for collecting untranslatable chars */ |
| 4873 | const Py_UNICODE *collstart = p; |
| 4874 | const Py_UNICODE *collend = p+1; |
| 4875 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4876 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4877 | /* find all untranslatable characters */ |
| 4878 | while (collend < endp) { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4879 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4880 | goto onError; |
| 4881 | Py_XDECREF(x); |
| 4882 | if (x!=Py_None) |
| 4883 | break; |
| 4884 | ++collend; |
| 4885 | } |
| 4886 | /* cache callback name lookup |
| 4887 | * (if not done yet, i.e. it's the first error) */ |
| 4888 | if (known_errorHandler==-1) { |
| 4889 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4890 | known_errorHandler = 1; |
| 4891 | else if (!strcmp(errors, "replace")) |
| 4892 | known_errorHandler = 2; |
| 4893 | else if (!strcmp(errors, "ignore")) |
| 4894 | known_errorHandler = 3; |
| 4895 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4896 | known_errorHandler = 4; |
| 4897 | else |
| 4898 | known_errorHandler = 0; |
| 4899 | } |
| 4900 | switch (known_errorHandler) { |
| 4901 | case 1: /* strict */ |
| 4902 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
| 4903 | goto onError; |
| 4904 | case 2: /* replace */ |
| 4905 | /* No need to check for space, this is a 1:1 replacement */ |
| 4906 | for (coll = collstart; coll<collend; ++coll) |
| 4907 | *str++ = '?'; |
| 4908 | /* fall through */ |
| 4909 | case 3: /* ignore */ |
| 4910 | p = collend; |
| 4911 | break; |
| 4912 | case 4: /* xmlcharrefreplace */ |
| 4913 | /* generate replacement (temporarily (mis)uses p) */ |
| 4914 | for (p = collstart; p < collend; ++p) { |
| 4915 | char buffer[2+29+1+1]; |
| 4916 | char *cp; |
| 4917 | sprintf(buffer, "&#%d;", (int)*p); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4918 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4919 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 4920 | goto onError; |
| 4921 | for (cp = buffer; *cp; ++cp) |
| 4922 | *str++ = *cp; |
| 4923 | } |
| 4924 | p = collend; |
| 4925 | break; |
| 4926 | default: |
| 4927 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 4928 | reason, startp, size, &exc, |
| 4929 | collstart-startp, collend-startp, &newpos); |
| 4930 | if (repunicode == NULL) |
| 4931 | goto onError; |
| 4932 | /* generate replacement */ |
| 4933 | repsize = PyUnicode_GET_SIZE(repunicode); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4934 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4935 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 4936 | Py_DECREF(repunicode); |
| 4937 | goto onError; |
| 4938 | } |
| 4939 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 4940 | *str++ = *uni2; |
| 4941 | p = startp + newpos; |
| 4942 | Py_DECREF(repunicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4943 | } |
| 4944 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4945 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4946 | /* Resize if we allocated to much */ |
| 4947 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4948 | if (respos<PyUnicode_GET_SIZE(res)) { |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4949 | if (_PyUnicode_Resize(&res, respos) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 4950 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4951 | } |
| 4952 | Py_XDECREF(exc); |
| 4953 | Py_XDECREF(errorHandler); |
| 4954 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4955 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4956 | onError: |
| 4957 | Py_XDECREF(res); |
| 4958 | Py_XDECREF(exc); |
| 4959 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4960 | return NULL; |
| 4961 | } |
| 4962 | |
| 4963 | PyObject *PyUnicode_Translate(PyObject *str, |
| 4964 | PyObject *mapping, |
| 4965 | const char *errors) |
| 4966 | { |
| 4967 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4968 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4969 | str = PyUnicode_FromObject(str); |
| 4970 | if (str == NULL) |
| 4971 | goto onError; |
| 4972 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 4973 | PyUnicode_GET_SIZE(str), |
| 4974 | mapping, |
| 4975 | errors); |
| 4976 | Py_DECREF(str); |
| 4977 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4978 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4979 | onError: |
| 4980 | Py_XDECREF(str); |
| 4981 | return NULL; |
| 4982 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4983 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4984 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 4985 | |
| 4986 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4987 | Py_ssize_t length, |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4988 | char *output, |
| 4989 | const char *errors) |
| 4990 | { |
| 4991 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4992 | PyObject *errorHandler = NULL; |
| 4993 | PyObject *exc = NULL; |
| 4994 | const char *encoding = "decimal"; |
| 4995 | const char *reason = "invalid decimal Unicode string"; |
| 4996 | /* the following variable is used for caching string comparisons |
| 4997 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 4998 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4999 | |
| 5000 | if (output == NULL) { |
| 5001 | PyErr_BadArgument(); |
| 5002 | return -1; |
| 5003 | } |
| 5004 | |
| 5005 | p = s; |
| 5006 | end = s + length; |
| 5007 | while (p < end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5008 | register Py_UNICODE ch = *p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5009 | int decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5010 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5011 | Py_ssize_t repsize; |
| 5012 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5013 | Py_UNICODE *uni2; |
| 5014 | Py_UNICODE *collstart; |
| 5015 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5016 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5017 | if (Py_UNICODE_ISSPACE(ch)) { |
| 5018 | *output++ = ' '; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5019 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5020 | continue; |
| 5021 | } |
| 5022 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5023 | if (decimal >= 0) { |
| 5024 | *output++ = '0' + decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5025 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5026 | continue; |
| 5027 | } |
Guido van Rossum | ba47704 | 2000-04-06 18:18:10 +0000 | [diff] [blame] | 5028 | if (0 < ch && ch < 256) { |
Guido van Rossum | 42c29aa | 2000-05-03 23:58:29 +0000 | [diff] [blame] | 5029 | *output++ = (char)ch; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5030 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5031 | continue; |
| 5032 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5033 | /* All other characters are considered unencodable */ |
| 5034 | collstart = p; |
| 5035 | collend = p+1; |
| 5036 | while (collend < end) { |
| 5037 | if ((0 < *collend && *collend < 256) || |
| 5038 | !Py_UNICODE_ISSPACE(*collend) || |
| 5039 | Py_UNICODE_TODECIMAL(*collend)) |
| 5040 | break; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5041 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5042 | /* cache callback name lookup |
| 5043 | * (if not done yet, i.e. it's the first error) */ |
| 5044 | if (known_errorHandler==-1) { |
| 5045 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5046 | known_errorHandler = 1; |
| 5047 | else if (!strcmp(errors, "replace")) |
| 5048 | known_errorHandler = 2; |
| 5049 | else if (!strcmp(errors, "ignore")) |
| 5050 | known_errorHandler = 3; |
| 5051 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5052 | known_errorHandler = 4; |
| 5053 | else |
| 5054 | known_errorHandler = 0; |
| 5055 | } |
| 5056 | switch (known_errorHandler) { |
| 5057 | case 1: /* strict */ |
| 5058 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 5059 | goto onError; |
| 5060 | case 2: /* replace */ |
| 5061 | for (p = collstart; p < collend; ++p) |
| 5062 | *output++ = '?'; |
| 5063 | /* fall through */ |
| 5064 | case 3: /* ignore */ |
| 5065 | p = collend; |
| 5066 | break; |
| 5067 | case 4: /* xmlcharrefreplace */ |
| 5068 | /* generate replacement (temporarily (mis)uses p) */ |
| 5069 | for (p = collstart; p < collend; ++p) |
| 5070 | output += sprintf(output, "&#%d;", (int)*p); |
| 5071 | p = collend; |
| 5072 | break; |
| 5073 | default: |
| 5074 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 5075 | encoding, reason, s, length, &exc, |
| 5076 | collstart-s, collend-s, &newpos); |
| 5077 | if (repunicode == NULL) |
| 5078 | goto onError; |
| 5079 | /* generate replacement */ |
| 5080 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5081 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 5082 | Py_UNICODE ch = *uni2; |
| 5083 | if (Py_UNICODE_ISSPACE(ch)) |
| 5084 | *output++ = ' '; |
| 5085 | else { |
| 5086 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5087 | if (decimal >= 0) |
| 5088 | *output++ = '0' + decimal; |
| 5089 | else if (0 < ch && ch < 256) |
| 5090 | *output++ = (char)ch; |
| 5091 | else { |
| 5092 | Py_DECREF(repunicode); |
| 5093 | raise_encode_exception(&exc, encoding, |
| 5094 | s, length, collstart-s, collend-s, reason); |
| 5095 | goto onError; |
| 5096 | } |
| 5097 | } |
| 5098 | } |
| 5099 | p = s + newpos; |
| 5100 | Py_DECREF(repunicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5101 | } |
| 5102 | } |
| 5103 | /* 0-terminate the output string */ |
| 5104 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5105 | Py_XDECREF(exc); |
| 5106 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5107 | return 0; |
| 5108 | |
| 5109 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5110 | Py_XDECREF(exc); |
| 5111 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5112 | return -1; |
| 5113 | } |
| 5114 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5115 | /* --- Helpers ------------------------------------------------------------ */ |
| 5116 | |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 5117 | #include "stringlib/unicodedefs.h" |
Fredrik Lundh | 6471ee4 | 2006-05-24 14:28:11 +0000 | [diff] [blame] | 5118 | |
Facundo Batista | 6f7e6fb | 2007-11-16 19:16:15 +0000 | [diff] [blame] | 5119 | #define FROM_UNICODE |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 5120 | |
Fredrik Lundh | a50d201 | 2006-05-26 17:04:58 +0000 | [diff] [blame] | 5121 | #include "stringlib/fastsearch.h" |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5122 | |
| 5123 | #include "stringlib/count.h" |
| 5124 | #include "stringlib/find.h" |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 5125 | #include "stringlib/partition.h" |
| 5126 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 5127 | /* helper macro to fixup start/end slice values */ |
| 5128 | #define FIX_START_END(obj) \ |
| 5129 | if (start < 0) \ |
| 5130 | start += (obj)->length; \ |
| 5131 | if (start < 0) \ |
| 5132 | start = 0; \ |
| 5133 | if (end > (obj)->length) \ |
| 5134 | end = (obj)->length; \ |
| 5135 | if (end < 0) \ |
| 5136 | end += (obj)->length; \ |
| 5137 | if (end < 0) \ |
| 5138 | end = 0; |
| 5139 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5140 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5141 | PyObject *substr, |
| 5142 | Py_ssize_t start, |
| 5143 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5144 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5145 | Py_ssize_t result; |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5146 | PyUnicodeObject* str_obj; |
| 5147 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5148 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5149 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 5150 | if (!str_obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5151 | return -1; |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5152 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 5153 | if (!sub_obj) { |
| 5154 | Py_DECREF(str_obj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5155 | return -1; |
| 5156 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5157 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 5158 | FIX_START_END(str_obj); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5159 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5160 | result = stringlib_count( |
| 5161 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
| 5162 | ); |
| 5163 | |
| 5164 | Py_DECREF(sub_obj); |
| 5165 | Py_DECREF(str_obj); |
| 5166 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5167 | return result; |
| 5168 | } |
| 5169 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5170 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5171 | PyObject *sub, |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 5172 | Py_ssize_t start, |
| 5173 | Py_ssize_t end, |
| 5174 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5175 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5176 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5177 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5178 | str = PyUnicode_FromObject(str); |
| 5179 | if (!str) |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 5180 | return -2; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5181 | sub = PyUnicode_FromObject(sub); |
| 5182 | if (!sub) { |
| 5183 | Py_DECREF(str); |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 5184 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5185 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5186 | |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 5187 | if (direction > 0) |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 5188 | result = stringlib_find_slice( |
| 5189 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5190 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5191 | start, end |
| 5192 | ); |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 5193 | else |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 5194 | result = stringlib_rfind_slice( |
| 5195 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5196 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5197 | start, end |
| 5198 | ); |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 5199 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5200 | Py_DECREF(str); |
| 5201 | Py_DECREF(sub); |
| 5202 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5203 | return result; |
| 5204 | } |
| 5205 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5206 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5207 | int tailmatch(PyUnicodeObject *self, |
| 5208 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5209 | Py_ssize_t start, |
| 5210 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5211 | int direction) |
| 5212 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5213 | if (substring->length == 0) |
| 5214 | return 1; |
| 5215 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 5216 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5217 | |
| 5218 | end -= substring->length; |
| 5219 | if (end < start) |
| 5220 | return 0; |
| 5221 | |
| 5222 | if (direction > 0) { |
| 5223 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 5224 | return 1; |
| 5225 | } else { |
| 5226 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 5227 | return 1; |
| 5228 | } |
| 5229 | |
| 5230 | return 0; |
| 5231 | } |
| 5232 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5233 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5234 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5235 | Py_ssize_t start, |
| 5236 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5237 | int direction) |
| 5238 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5239 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5240 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5241 | str = PyUnicode_FromObject(str); |
| 5242 | if (str == NULL) |
| 5243 | return -1; |
| 5244 | substr = PyUnicode_FromObject(substr); |
| 5245 | if (substr == NULL) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5246 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5247 | return -1; |
| 5248 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5249 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5250 | result = tailmatch((PyUnicodeObject *)str, |
| 5251 | (PyUnicodeObject *)substr, |
| 5252 | start, end, direction); |
| 5253 | Py_DECREF(str); |
| 5254 | Py_DECREF(substr); |
| 5255 | return result; |
| 5256 | } |
| 5257 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5258 | /* Apply fixfct filter to the Unicode object self and return a |
| 5259 | reference to the modified object */ |
| 5260 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5261 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5262 | PyObject *fixup(PyUnicodeObject *self, |
| 5263 | int (*fixfct)(PyUnicodeObject *s)) |
| 5264 | { |
| 5265 | |
| 5266 | PyUnicodeObject *u; |
| 5267 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5268 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5269 | if (u == NULL) |
| 5270 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5271 | |
| 5272 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5273 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5274 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5275 | /* fixfct should return TRUE if it modified the buffer. If |
| 5276 | FALSE, return a reference to the original buffer instead |
| 5277 | (to save space, not time) */ |
| 5278 | Py_INCREF(self); |
| 5279 | Py_DECREF(u); |
| 5280 | return (PyObject*) self; |
| 5281 | } |
| 5282 | return (PyObject*) u; |
| 5283 | } |
| 5284 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5285 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5286 | int fixupper(PyUnicodeObject *self) |
| 5287 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5288 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5289 | Py_UNICODE *s = self->str; |
| 5290 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5291 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5292 | while (len-- > 0) { |
| 5293 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5294 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5295 | ch = Py_UNICODE_TOUPPER(*s); |
| 5296 | if (ch != *s) { |
| 5297 | status = 1; |
| 5298 | *s = ch; |
| 5299 | } |
| 5300 | s++; |
| 5301 | } |
| 5302 | |
| 5303 | return status; |
| 5304 | } |
| 5305 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5306 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5307 | int fixlower(PyUnicodeObject *self) |
| 5308 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5309 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5310 | Py_UNICODE *s = self->str; |
| 5311 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5312 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5313 | while (len-- > 0) { |
| 5314 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5315 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5316 | ch = Py_UNICODE_TOLOWER(*s); |
| 5317 | if (ch != *s) { |
| 5318 | status = 1; |
| 5319 | *s = ch; |
| 5320 | } |
| 5321 | s++; |
| 5322 | } |
| 5323 | |
| 5324 | return status; |
| 5325 | } |
| 5326 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5327 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5328 | int fixswapcase(PyUnicodeObject *self) |
| 5329 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5330 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5331 | Py_UNICODE *s = self->str; |
| 5332 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5333 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5334 | while (len-- > 0) { |
| 5335 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5336 | *s = Py_UNICODE_TOLOWER(*s); |
| 5337 | status = 1; |
| 5338 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 5339 | *s = Py_UNICODE_TOUPPER(*s); |
| 5340 | status = 1; |
| 5341 | } |
| 5342 | s++; |
| 5343 | } |
| 5344 | |
| 5345 | return status; |
| 5346 | } |
| 5347 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5348 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5349 | int fixcapitalize(PyUnicodeObject *self) |
| 5350 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5351 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5352 | Py_UNICODE *s = self->str; |
| 5353 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5354 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5355 | if (len == 0) |
| 5356 | return 0; |
| 5357 | if (Py_UNICODE_ISLOWER(*s)) { |
| 5358 | *s = Py_UNICODE_TOUPPER(*s); |
| 5359 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5360 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5361 | s++; |
| 5362 | while (--len > 0) { |
| 5363 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5364 | *s = Py_UNICODE_TOLOWER(*s); |
| 5365 | status = 1; |
| 5366 | } |
| 5367 | s++; |
| 5368 | } |
| 5369 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5370 | } |
| 5371 | |
| 5372 | static |
| 5373 | int fixtitle(PyUnicodeObject *self) |
| 5374 | { |
| 5375 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5376 | register Py_UNICODE *e; |
| 5377 | int previous_is_cased; |
| 5378 | |
| 5379 | /* Shortcut for single character strings */ |
| 5380 | if (PyUnicode_GET_SIZE(self) == 1) { |
| 5381 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 5382 | if (*p != ch) { |
| 5383 | *p = ch; |
| 5384 | return 1; |
| 5385 | } |
| 5386 | else |
| 5387 | return 0; |
| 5388 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5389 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5390 | e = p + PyUnicode_GET_SIZE(self); |
| 5391 | previous_is_cased = 0; |
| 5392 | for (; p < e; p++) { |
| 5393 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5394 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5395 | if (previous_is_cased) |
| 5396 | *p = Py_UNICODE_TOLOWER(ch); |
| 5397 | else |
| 5398 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5399 | |
| 5400 | if (Py_UNICODE_ISLOWER(ch) || |
| 5401 | Py_UNICODE_ISUPPER(ch) || |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5402 | Py_UNICODE_ISTITLE(ch)) |
| 5403 | previous_is_cased = 1; |
| 5404 | else |
| 5405 | previous_is_cased = 0; |
| 5406 | } |
| 5407 | return 1; |
| 5408 | } |
| 5409 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5410 | PyObject * |
| 5411 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5412 | { |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5413 | PyObject *internal_separator = NULL; |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 5414 | const Py_UNICODE blank = ' '; |
| 5415 | const Py_UNICODE *sep = ␣ |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 5416 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5417 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 5418 | Py_ssize_t res_alloc = 100; /* # allocated bytes for string in res */ |
| 5419 | Py_ssize_t res_used; /* # used bytes */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5420 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 5421 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5422 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5423 | PyObject *item; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5424 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5425 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5426 | fseq = PySequence_Fast(seq, ""); |
| 5427 | if (fseq == NULL) { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5428 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5429 | } |
| 5430 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5431 | /* Grrrr. A codec may be invoked to convert str objects to |
| 5432 | * Unicode, and so it's possible to call back into Python code |
| 5433 | * during PyUnicode_FromObject(), and so it's possible for a sick |
| 5434 | * codec to change the size of fseq (if seq is a list). Therefore |
| 5435 | * we have to keep refetching the size -- can't assume seqlen |
| 5436 | * is invariant. |
| 5437 | */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5438 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5439 | /* If empty sequence, return u"". */ |
| 5440 | if (seqlen == 0) { |
| 5441 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 5442 | goto Done; |
| 5443 | } |
| 5444 | /* If singleton sequence with an exact Unicode, return that. */ |
| 5445 | if (seqlen == 1) { |
| 5446 | item = PySequence_Fast_GET_ITEM(fseq, 0); |
| 5447 | if (PyUnicode_CheckExact(item)) { |
| 5448 | Py_INCREF(item); |
| 5449 | res = (PyUnicodeObject *)item; |
| 5450 | goto Done; |
| 5451 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5452 | } |
| 5453 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5454 | /* At least two items to join, or one that isn't exact Unicode. */ |
| 5455 | if (seqlen > 1) { |
| 5456 | /* Set up sep and seplen -- they're needed. */ |
| 5457 | if (separator == NULL) { |
| 5458 | sep = ␣ |
| 5459 | seplen = 1; |
| 5460 | } |
| 5461 | else { |
| 5462 | internal_separator = PyUnicode_FromObject(separator); |
| 5463 | if (internal_separator == NULL) |
| 5464 | goto onError; |
| 5465 | sep = PyUnicode_AS_UNICODE(internal_separator); |
| 5466 | seplen = PyUnicode_GET_SIZE(internal_separator); |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5467 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 5468 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5469 | } |
| 5470 | } |
| 5471 | |
| 5472 | /* Get space. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5473 | res = _PyUnicode_New(res_alloc); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5474 | if (res == NULL) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5475 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5476 | res_p = PyUnicode_AS_UNICODE(res); |
| 5477 | res_used = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5478 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5479 | for (i = 0; i < seqlen; ++i) { |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 5480 | Py_ssize_t itemlen; |
| 5481 | Py_ssize_t new_res_used; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5482 | |
| 5483 | item = PySequence_Fast_GET_ITEM(fseq, i); |
| 5484 | /* Convert item to Unicode. */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 5485 | if (! PyUnicode_Check(item) && ! PyString_Check(item)) { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5486 | PyErr_Format(PyExc_TypeError, |
Thomas Wouters | 715a4cd | 2006-04-16 22:04:49 +0000 | [diff] [blame] | 5487 | "sequence item %zd: expected string or Unicode," |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5488 | " %.80s found", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5489 | i, Py_TYPE(item)->tp_name); |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 5490 | goto onError; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5491 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5492 | item = PyUnicode_FromObject(item); |
| 5493 | if (item == NULL) |
| 5494 | goto onError; |
| 5495 | /* We own a reference to item from here on. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5496 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5497 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 5498 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5499 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5500 | /* Make sure we have enough space for the separator and the item. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5501 | itemlen = PyUnicode_GET_SIZE(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5502 | new_res_used = res_used + itemlen; |
Georg Brandl | 90e27d3 | 2006-06-10 06:40:50 +0000 | [diff] [blame] | 5503 | if (new_res_used < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5504 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5505 | if (i < seqlen - 1) { |
| 5506 | new_res_used += seplen; |
Georg Brandl | 90e27d3 | 2006-06-10 06:40:50 +0000 | [diff] [blame] | 5507 | if (new_res_used < 0) |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5508 | goto Overflow; |
| 5509 | } |
| 5510 | if (new_res_used > res_alloc) { |
| 5511 | /* double allocated size until it's big enough */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5512 | do { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5513 | res_alloc += res_alloc; |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 5514 | if (res_alloc <= 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5515 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5516 | } while (new_res_used > res_alloc); |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5517 | if (_PyUnicode_Resize(&res, res_alloc) < 0) { |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 5518 | Py_DECREF(item); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5519 | goto onError; |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 5520 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5521 | res_p = PyUnicode_AS_UNICODE(res) + res_used; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5522 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5523 | |
| 5524 | /* Copy item, and maybe the separator. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5525 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5526 | res_p += itemlen; |
| 5527 | if (i < seqlen - 1) { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5528 | Py_UNICODE_COPY(res_p, sep, seplen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5529 | res_p += seplen; |
| 5530 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5531 | Py_DECREF(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5532 | res_used = new_res_used; |
| 5533 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5534 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5535 | /* Shrink res to match the used area; this probably can't fail, |
| 5536 | * but it's cheap to check. |
| 5537 | */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5538 | if (_PyUnicode_Resize(&res, res_used) < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5539 | goto onError; |
| 5540 | |
| 5541 | Done: |
| 5542 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5543 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5544 | return (PyObject *)res; |
| 5545 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5546 | Overflow: |
| 5547 | PyErr_SetString(PyExc_OverflowError, |
Georg Brandl | 90e27d3 | 2006-06-10 06:40:50 +0000 | [diff] [blame] | 5548 | "join() result is too long for a Python string"); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5549 | Py_DECREF(item); |
| 5550 | /* fall through */ |
| 5551 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5552 | onError: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5553 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5554 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5555 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5556 | return NULL; |
| 5557 | } |
| 5558 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5559 | static |
| 5560 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5561 | Py_ssize_t left, |
| 5562 | Py_ssize_t right, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5563 | Py_UNICODE fill) |
| 5564 | { |
| 5565 | PyUnicodeObject *u; |
| 5566 | |
| 5567 | if (left < 0) |
| 5568 | left = 0; |
| 5569 | if (right < 0) |
| 5570 | right = 0; |
| 5571 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5572 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5573 | Py_INCREF(self); |
| 5574 | return self; |
| 5575 | } |
| 5576 | |
| 5577 | u = _PyUnicode_New(left + self->length + right); |
| 5578 | if (u) { |
| 5579 | if (left) |
| 5580 | Py_UNICODE_FILL(u->str, fill, left); |
| 5581 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 5582 | if (right) |
| 5583 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 5584 | } |
| 5585 | |
| 5586 | return u; |
| 5587 | } |
| 5588 | |
| 5589 | #define SPLIT_APPEND(data, left, right) \ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5590 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5591 | if (!str) \ |
| 5592 | goto onError; \ |
| 5593 | if (PyList_Append(list, str)) { \ |
| 5594 | Py_DECREF(str); \ |
| 5595 | goto onError; \ |
| 5596 | } \ |
| 5597 | else \ |
| 5598 | Py_DECREF(str); |
| 5599 | |
| 5600 | static |
| 5601 | PyObject *split_whitespace(PyUnicodeObject *self, |
| 5602 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5603 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5604 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5605 | register Py_ssize_t i; |
| 5606 | register Py_ssize_t j; |
| 5607 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5608 | PyObject *str; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5609 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5610 | |
| 5611 | for (i = j = 0; i < len; ) { |
| 5612 | /* find a token */ |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5613 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5614 | i++; |
| 5615 | j = i; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5616 | while (i < len && !Py_UNICODE_ISSPACE(buf[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5617 | i++; |
| 5618 | if (j < i) { |
| 5619 | if (maxcount-- <= 0) |
| 5620 | break; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5621 | SPLIT_APPEND(buf, j, i); |
| 5622 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5623 | i++; |
| 5624 | j = i; |
| 5625 | } |
| 5626 | } |
| 5627 | if (j < len) { |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5628 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5629 | } |
| 5630 | return list; |
| 5631 | |
| 5632 | onError: |
| 5633 | Py_DECREF(list); |
| 5634 | return NULL; |
| 5635 | } |
| 5636 | |
| 5637 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5638 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5639 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5640 | register Py_ssize_t i; |
| 5641 | register Py_ssize_t j; |
| 5642 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5643 | PyObject *list; |
| 5644 | PyObject *str; |
| 5645 | Py_UNICODE *data; |
| 5646 | |
| 5647 | string = PyUnicode_FromObject(string); |
| 5648 | if (string == NULL) |
| 5649 | return NULL; |
| 5650 | data = PyUnicode_AS_UNICODE(string); |
| 5651 | len = PyUnicode_GET_SIZE(string); |
| 5652 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5653 | list = PyList_New(0); |
| 5654 | if (!list) |
| 5655 | goto onError; |
| 5656 | |
| 5657 | for (i = j = 0; i < len; ) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5658 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5659 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5660 | /* Find a line and append it */ |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5661 | while (i < len && !BLOOM_LINEBREAK(data[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5662 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5663 | |
| 5664 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5665 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5666 | if (i < len) { |
| 5667 | if (data[i] == '\r' && i + 1 < len && |
| 5668 | data[i+1] == '\n') |
| 5669 | i += 2; |
| 5670 | else |
| 5671 | i++; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5672 | if (keepends) |
| 5673 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5674 | } |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5675 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5676 | j = i; |
| 5677 | } |
| 5678 | if (j < len) { |
| 5679 | SPLIT_APPEND(data, j, len); |
| 5680 | } |
| 5681 | |
| 5682 | Py_DECREF(string); |
| 5683 | return list; |
| 5684 | |
| 5685 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5686 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5687 | Py_DECREF(string); |
| 5688 | return NULL; |
| 5689 | } |
| 5690 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5691 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5692 | PyObject *split_char(PyUnicodeObject *self, |
| 5693 | PyObject *list, |
| 5694 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5695 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5696 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5697 | register Py_ssize_t i; |
| 5698 | register Py_ssize_t j; |
| 5699 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5700 | PyObject *str; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5701 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5702 | |
| 5703 | for (i = j = 0; i < len; ) { |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5704 | if (buf[i] == ch) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5705 | if (maxcount-- <= 0) |
| 5706 | break; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5707 | SPLIT_APPEND(buf, j, i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5708 | i = j = i + 1; |
| 5709 | } else |
| 5710 | i++; |
| 5711 | } |
| 5712 | if (j <= len) { |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5713 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5714 | } |
| 5715 | return list; |
| 5716 | |
| 5717 | onError: |
| 5718 | Py_DECREF(list); |
| 5719 | return NULL; |
| 5720 | } |
| 5721 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5722 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5723 | PyObject *split_substring(PyUnicodeObject *self, |
| 5724 | PyObject *list, |
| 5725 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5726 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5727 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5728 | register Py_ssize_t i; |
| 5729 | register Py_ssize_t j; |
| 5730 | Py_ssize_t len = self->length; |
| 5731 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5732 | PyObject *str; |
| 5733 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 5734 | for (i = j = 0; i <= len - sublen; ) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5735 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5736 | if (maxcount-- <= 0) |
| 5737 | break; |
| 5738 | SPLIT_APPEND(self->str, j, i); |
| 5739 | i = j = i + sublen; |
| 5740 | } else |
| 5741 | i++; |
| 5742 | } |
| 5743 | if (j <= len) { |
| 5744 | SPLIT_APPEND(self->str, j, len); |
| 5745 | } |
| 5746 | return list; |
| 5747 | |
| 5748 | onError: |
| 5749 | Py_DECREF(list); |
| 5750 | return NULL; |
| 5751 | } |
| 5752 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5753 | static |
| 5754 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
| 5755 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5756 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5757 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5758 | register Py_ssize_t i; |
| 5759 | register Py_ssize_t j; |
| 5760 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5761 | PyObject *str; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5762 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5763 | |
| 5764 | for (i = j = len - 1; i >= 0; ) { |
| 5765 | /* find a token */ |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5766 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5767 | i--; |
| 5768 | j = i; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5769 | while (i >= 0 && !Py_UNICODE_ISSPACE(buf[i])) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5770 | i--; |
| 5771 | if (j > i) { |
| 5772 | if (maxcount-- <= 0) |
| 5773 | break; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5774 | SPLIT_APPEND(buf, i + 1, j + 1); |
| 5775 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5776 | i--; |
| 5777 | j = i; |
| 5778 | } |
| 5779 | } |
| 5780 | if (j >= 0) { |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5781 | SPLIT_APPEND(buf, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5782 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5783 | if (PyList_Reverse(list) < 0) |
| 5784 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5785 | return list; |
| 5786 | |
| 5787 | onError: |
| 5788 | Py_DECREF(list); |
| 5789 | return NULL; |
| 5790 | } |
| 5791 | |
| 5792 | static |
| 5793 | PyObject *rsplit_char(PyUnicodeObject *self, |
| 5794 | PyObject *list, |
| 5795 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5796 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5797 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5798 | register Py_ssize_t i; |
| 5799 | register Py_ssize_t j; |
| 5800 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5801 | PyObject *str; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5802 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5803 | |
| 5804 | for (i = j = len - 1; i >= 0; ) { |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5805 | if (buf[i] == ch) { |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5806 | if (maxcount-- <= 0) |
| 5807 | break; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5808 | SPLIT_APPEND(buf, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5809 | j = i = i - 1; |
| 5810 | } else |
| 5811 | i--; |
| 5812 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 5813 | if (j >= -1) { |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5814 | SPLIT_APPEND(buf, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5815 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5816 | if (PyList_Reverse(list) < 0) |
| 5817 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5818 | return list; |
| 5819 | |
| 5820 | onError: |
| 5821 | Py_DECREF(list); |
| 5822 | return NULL; |
| 5823 | } |
| 5824 | |
| 5825 | static |
| 5826 | PyObject *rsplit_substring(PyUnicodeObject *self, |
| 5827 | PyObject *list, |
| 5828 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5829 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5830 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5831 | register Py_ssize_t i; |
| 5832 | register Py_ssize_t j; |
| 5833 | Py_ssize_t len = self->length; |
| 5834 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5835 | PyObject *str; |
| 5836 | |
| 5837 | for (i = len - sublen, j = len; i >= 0; ) { |
| 5838 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5839 | if (maxcount-- <= 0) |
| 5840 | break; |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5841 | SPLIT_APPEND(self->str, i + sublen, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5842 | j = i; |
| 5843 | i -= sublen; |
| 5844 | } else |
| 5845 | i--; |
| 5846 | } |
| 5847 | if (j >= 0) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5848 | SPLIT_APPEND(self->str, 0, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5849 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5850 | if (PyList_Reverse(list) < 0) |
| 5851 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5852 | return list; |
| 5853 | |
| 5854 | onError: |
| 5855 | Py_DECREF(list); |
| 5856 | return NULL; |
| 5857 | } |
| 5858 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5859 | #undef SPLIT_APPEND |
| 5860 | |
| 5861 | static |
| 5862 | PyObject *split(PyUnicodeObject *self, |
| 5863 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5864 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5865 | { |
| 5866 | PyObject *list; |
| 5867 | |
| 5868 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5869 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5870 | |
| 5871 | list = PyList_New(0); |
| 5872 | if (!list) |
| 5873 | return NULL; |
| 5874 | |
| 5875 | if (substring == NULL) |
| 5876 | return split_whitespace(self,list,maxcount); |
| 5877 | |
| 5878 | else if (substring->length == 1) |
| 5879 | return split_char(self,list,substring->str[0],maxcount); |
| 5880 | |
| 5881 | else if (substring->length == 0) { |
| 5882 | Py_DECREF(list); |
| 5883 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5884 | return NULL; |
| 5885 | } |
| 5886 | else |
| 5887 | return split_substring(self,list,substring,maxcount); |
| 5888 | } |
| 5889 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5890 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5891 | PyObject *rsplit(PyUnicodeObject *self, |
| 5892 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5893 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5894 | { |
| 5895 | PyObject *list; |
| 5896 | |
| 5897 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5898 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5899 | |
| 5900 | list = PyList_New(0); |
| 5901 | if (!list) |
| 5902 | return NULL; |
| 5903 | |
| 5904 | if (substring == NULL) |
| 5905 | return rsplit_whitespace(self,list,maxcount); |
| 5906 | |
| 5907 | else if (substring->length == 1) |
| 5908 | return rsplit_char(self,list,substring->str[0],maxcount); |
| 5909 | |
| 5910 | else if (substring->length == 0) { |
| 5911 | Py_DECREF(list); |
| 5912 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5913 | return NULL; |
| 5914 | } |
| 5915 | else |
| 5916 | return rsplit_substring(self,list,substring,maxcount); |
| 5917 | } |
| 5918 | |
| 5919 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5920 | PyObject *replace(PyUnicodeObject *self, |
| 5921 | PyUnicodeObject *str1, |
| 5922 | PyUnicodeObject *str2, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5923 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5924 | { |
| 5925 | PyUnicodeObject *u; |
| 5926 | |
| 5927 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5928 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5929 | |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5930 | if (str1->length == str2->length) { |
| 5931 | /* same length */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5932 | Py_ssize_t i; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5933 | if (str1->length == 1) { |
| 5934 | /* replace characters */ |
| 5935 | Py_UNICODE u1, u2; |
| 5936 | if (!findchar(self->str, self->length, str1->str[0])) |
| 5937 | goto nothing; |
| 5938 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5939 | if (!u) |
| 5940 | return NULL; |
| 5941 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5942 | u1 = str1->str[0]; |
| 5943 | u2 = str2->str[0]; |
| 5944 | for (i = 0; i < u->length; i++) |
| 5945 | if (u->str[i] == u1) { |
| 5946 | if (--maxcount < 0) |
| 5947 | break; |
| 5948 | u->str[i] = u2; |
| 5949 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5950 | } else { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5951 | i = fastsearch( |
| 5952 | self->str, self->length, str1->str, str1->length, FAST_SEARCH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5953 | ); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5954 | if (i < 0) |
| 5955 | goto nothing; |
| 5956 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5957 | if (!u) |
| 5958 | return NULL; |
| 5959 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5960 | while (i <= self->length - str1->length) |
| 5961 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 5962 | if (--maxcount < 0) |
| 5963 | break; |
| 5964 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 5965 | i += str1->length; |
| 5966 | } else |
| 5967 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5968 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5969 | } else { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5970 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 5971 | Py_ssize_t n, i, j, e; |
Fredrik Lundh | 0c71f88 | 2006-05-25 16:46:54 +0000 | [diff] [blame] | 5972 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5973 | Py_UNICODE *p; |
| 5974 | |
| 5975 | /* replace strings */ |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5976 | n = stringlib_count(self->str, self->length, str1->str, str1->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5977 | if (n > maxcount) |
| 5978 | n = maxcount; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5979 | if (n == 0) |
| 5980 | goto nothing; |
Fredrik Lundh | 0c71f88 | 2006-05-25 16:46:54 +0000 | [diff] [blame] | 5981 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 5982 | delta = (str2->length - str1->length); |
| 5983 | if (delta == 0) { |
| 5984 | new_size = self->length; |
| 5985 | } else { |
| 5986 | product = n * (str2->length - str1->length); |
| 5987 | if ((product / (str2->length - str1->length)) != n) { |
| 5988 | PyErr_SetString(PyExc_OverflowError, |
| 5989 | "replace string is too long"); |
| 5990 | return NULL; |
| 5991 | } |
| 5992 | new_size = self->length + product; |
| 5993 | if (new_size < 0) { |
| 5994 | PyErr_SetString(PyExc_OverflowError, |
| 5995 | "replace string is too long"); |
| 5996 | return NULL; |
| 5997 | } |
| 5998 | } |
| 5999 | u = _PyUnicode_New(new_size); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6000 | if (!u) |
| 6001 | return NULL; |
| 6002 | i = 0; |
| 6003 | p = u->str; |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 6004 | e = self->length - str1->length; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6005 | if (str1->length > 0) { |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 6006 | while (n-- > 0) { |
| 6007 | /* look for next match */ |
| 6008 | j = i; |
| 6009 | while (j <= e) { |
| 6010 | if (Py_UNICODE_MATCH(self, j, str1)) |
| 6011 | break; |
| 6012 | j++; |
| 6013 | } |
| 6014 | if (j > i) { |
| 6015 | if (j > e) |
| 6016 | break; |
| 6017 | /* copy unchanged part [i:j] */ |
| 6018 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 6019 | p += j - i; |
| 6020 | } |
| 6021 | /* copy substitution string */ |
| 6022 | if (str2->length > 0) { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6023 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6024 | p += str2->length; |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 6025 | } |
| 6026 | i = j + str1->length; |
| 6027 | } |
| 6028 | if (i < self->length) |
| 6029 | /* copy tail [i:] */ |
| 6030 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6031 | } else { |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 6032 | /* interleave */ |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6033 | while (n > 0) { |
| 6034 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6035 | p += str2->length; |
| 6036 | if (--n <= 0) |
| 6037 | break; |
| 6038 | *p++ = self->str[i++]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6039 | } |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6040 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6041 | } |
| 6042 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6043 | return (PyObject *) u; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6044 | |
| 6045 | nothing: |
| 6046 | /* nothing to replace; return original string (when possible) */ |
| 6047 | if (PyUnicode_CheckExact(self)) { |
| 6048 | Py_INCREF(self); |
| 6049 | return (PyObject *) self; |
| 6050 | } |
| 6051 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6052 | } |
| 6053 | |
| 6054 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 6055 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6056 | PyDoc_STRVAR(title__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6057 | "S.title() -> unicode\n\ |
| 6058 | \n\ |
| 6059 | 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] | 6060 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6061 | |
| 6062 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6063 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6064 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6065 | return fixup(self, fixtitle); |
| 6066 | } |
| 6067 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6068 | PyDoc_STRVAR(capitalize__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6069 | "S.capitalize() -> unicode\n\ |
| 6070 | \n\ |
| 6071 | 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] | 6072 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6073 | |
| 6074 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6075 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6076 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6077 | return fixup(self, fixcapitalize); |
| 6078 | } |
| 6079 | |
| 6080 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6081 | PyDoc_STRVAR(capwords__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6082 | "S.capwords() -> unicode\n\ |
| 6083 | \n\ |
| 6084 | 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] | 6085 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6086 | |
| 6087 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6088 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6089 | { |
| 6090 | PyObject *list; |
| 6091 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6092 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6093 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6094 | /* Split into words */ |
| 6095 | list = split(self, NULL, -1); |
| 6096 | if (!list) |
| 6097 | return NULL; |
| 6098 | |
| 6099 | /* Capitalize each word */ |
| 6100 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 6101 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 6102 | fixcapitalize); |
| 6103 | if (item == NULL) |
| 6104 | goto onError; |
| 6105 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 6106 | PyList_SET_ITEM(list, i, item); |
| 6107 | } |
| 6108 | |
| 6109 | /* Join the words to form a new string */ |
| 6110 | item = PyUnicode_Join(NULL, list); |
| 6111 | |
| 6112 | onError: |
| 6113 | Py_DECREF(list); |
| 6114 | return (PyObject *)item; |
| 6115 | } |
| 6116 | #endif |
| 6117 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6118 | /* Argument converter. Coerces to a single unicode character */ |
| 6119 | |
| 6120 | static int |
| 6121 | convert_uc(PyObject *obj, void *addr) |
| 6122 | { |
| 6123 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 6124 | PyObject *uniobj; |
| 6125 | Py_UNICODE *unistr; |
| 6126 | |
| 6127 | uniobj = PyUnicode_FromObject(obj); |
| 6128 | if (uniobj == NULL) { |
| 6129 | PyErr_SetString(PyExc_TypeError, |
| 6130 | "The fill character cannot be converted to Unicode"); |
| 6131 | return 0; |
| 6132 | } |
| 6133 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 6134 | PyErr_SetString(PyExc_TypeError, |
| 6135 | "The fill character must be exactly one character long"); |
| 6136 | Py_DECREF(uniobj); |
| 6137 | return 0; |
| 6138 | } |
| 6139 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 6140 | *fillcharloc = unistr[0]; |
| 6141 | Py_DECREF(uniobj); |
| 6142 | return 1; |
| 6143 | } |
| 6144 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6145 | PyDoc_STRVAR(center__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6146 | "S.center(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6147 | \n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6148 | Return S centered in a Unicode string of length width. Padding is\n\ |
| 6149 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6150 | |
| 6151 | static PyObject * |
| 6152 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 6153 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6154 | Py_ssize_t marg, left; |
| 6155 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6156 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6157 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6158 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6159 | return NULL; |
| 6160 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6161 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6162 | Py_INCREF(self); |
| 6163 | return (PyObject*) self; |
| 6164 | } |
| 6165 | |
| 6166 | marg = width - self->length; |
| 6167 | left = marg / 2 + (marg & width & 1); |
| 6168 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6169 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6170 | } |
| 6171 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6172 | #if 0 |
| 6173 | |
| 6174 | /* This code should go into some future Unicode collation support |
| 6175 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 6176 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6177 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6178 | /* speedy UTF-16 code point order comparison */ |
| 6179 | /* gleaned from: */ |
| 6180 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 6181 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6182 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6183 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6184 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6185 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 6186 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6187 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6188 | }; |
| 6189 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6190 | static int |
| 6191 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6192 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6193 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6194 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6195 | Py_UNICODE *s1 = str1->str; |
| 6196 | Py_UNICODE *s2 = str2->str; |
| 6197 | |
| 6198 | len1 = str1->length; |
| 6199 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6200 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6201 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6202 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6203 | |
| 6204 | c1 = *s1++; |
| 6205 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6206 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6207 | if (c1 > (1<<11) * 26) |
| 6208 | c1 += utf16Fixup[c1>>11]; |
| 6209 | if (c2 > (1<<11) * 26) |
| 6210 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6211 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6212 | |
| 6213 | if (c1 != c2) |
| 6214 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6215 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6216 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6217 | } |
| 6218 | |
| 6219 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6220 | } |
| 6221 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6222 | #else |
| 6223 | |
| 6224 | static int |
| 6225 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6226 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6227 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6228 | |
| 6229 | Py_UNICODE *s1 = str1->str; |
| 6230 | Py_UNICODE *s2 = str2->str; |
| 6231 | |
| 6232 | len1 = str1->length; |
| 6233 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6234 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6235 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6236 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6237 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6238 | c1 = *s1++; |
| 6239 | c2 = *s2++; |
| 6240 | |
| 6241 | if (c1 != c2) |
| 6242 | return (c1 < c2) ? -1 : 1; |
| 6243 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6244 | len1--; len2--; |
| 6245 | } |
| 6246 | |
| 6247 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6248 | } |
| 6249 | |
| 6250 | #endif |
| 6251 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6252 | int PyUnicode_Compare(PyObject *left, |
| 6253 | PyObject *right) |
| 6254 | { |
| 6255 | PyUnicodeObject *u = NULL, *v = NULL; |
| 6256 | int result; |
| 6257 | |
| 6258 | /* Coerce the two arguments */ |
| 6259 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 6260 | if (u == NULL) |
| 6261 | goto onError; |
| 6262 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 6263 | if (v == NULL) |
| 6264 | goto onError; |
| 6265 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6266 | /* Shortcut for empty or interned objects */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6267 | if (v == u) { |
| 6268 | Py_DECREF(u); |
| 6269 | Py_DECREF(v); |
| 6270 | return 0; |
| 6271 | } |
| 6272 | |
| 6273 | result = unicode_compare(u, v); |
| 6274 | |
| 6275 | Py_DECREF(u); |
| 6276 | Py_DECREF(v); |
| 6277 | return result; |
| 6278 | |
| 6279 | onError: |
| 6280 | Py_XDECREF(u); |
| 6281 | Py_XDECREF(v); |
| 6282 | return -1; |
| 6283 | } |
| 6284 | |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 6285 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 6286 | PyObject *right, |
| 6287 | int op) |
| 6288 | { |
| 6289 | int result; |
| 6290 | |
| 6291 | result = PyUnicode_Compare(left, right); |
| 6292 | if (result == -1 && PyErr_Occurred()) |
| 6293 | goto onError; |
| 6294 | |
| 6295 | /* Convert the return value to a Boolean */ |
| 6296 | switch (op) { |
| 6297 | case Py_EQ: |
| 6298 | result = (result == 0); |
| 6299 | break; |
| 6300 | case Py_NE: |
| 6301 | result = (result != 0); |
| 6302 | break; |
| 6303 | case Py_LE: |
| 6304 | result = (result <= 0); |
| 6305 | break; |
| 6306 | case Py_GE: |
| 6307 | result = (result >= 0); |
| 6308 | break; |
| 6309 | case Py_LT: |
| 6310 | result = (result == -1); |
| 6311 | break; |
| 6312 | case Py_GT: |
| 6313 | result = (result == 1); |
| 6314 | break; |
| 6315 | } |
| 6316 | return PyBool_FromLong(result); |
| 6317 | |
| 6318 | onError: |
| 6319 | |
| 6320 | /* Standard case |
| 6321 | |
| 6322 | Type errors mean that PyUnicode_FromObject() could not convert |
| 6323 | one of the arguments (usually the right hand side) to Unicode, |
| 6324 | ie. we can't handle the comparison request. However, it is |
| 6325 | possible that the other object knows a comparison method, which |
| 6326 | is why we return Py_NotImplemented to give the other object a |
| 6327 | chance. |
| 6328 | |
| 6329 | */ |
| 6330 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 6331 | PyErr_Clear(); |
| 6332 | Py_INCREF(Py_NotImplemented); |
| 6333 | return Py_NotImplemented; |
| 6334 | } |
| 6335 | if (op != Py_EQ && op != Py_NE) |
| 6336 | return NULL; |
| 6337 | |
| 6338 | /* Equality comparison. |
| 6339 | |
| 6340 | This is a special case: we silence any PyExc_UnicodeDecodeError |
| 6341 | and instead turn it into a PyErr_UnicodeWarning. |
| 6342 | |
| 6343 | */ |
| 6344 | if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) |
| 6345 | return NULL; |
| 6346 | PyErr_Clear(); |
| 6347 | if (PyErr_Warn(PyExc_UnicodeWarning, |
| 6348 | (op == Py_EQ) ? |
| 6349 | "Unicode equal comparison " |
| 6350 | "failed to convert both arguments to Unicode - " |
| 6351 | "interpreting them as being unequal" : |
| 6352 | "Unicode unequal comparison " |
| 6353 | "failed to convert both arguments to Unicode - " |
| 6354 | "interpreting them as being unequal" |
| 6355 | ) < 0) |
| 6356 | return NULL; |
| 6357 | result = (op == Py_NE); |
| 6358 | return PyBool_FromLong(result); |
| 6359 | } |
| 6360 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6361 | int PyUnicode_Contains(PyObject *container, |
| 6362 | PyObject *element) |
| 6363 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6364 | PyObject *str, *sub; |
| 6365 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6366 | |
| 6367 | /* Coerce the two arguments */ |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6368 | sub = PyUnicode_FromObject(element); |
| 6369 | if (!sub) { |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 6370 | PyErr_SetString(PyExc_TypeError, |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 6371 | "'in <string>' requires string as left operand"); |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 6372 | return -1; |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 6373 | } |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 6374 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6375 | str = PyUnicode_FromObject(container); |
| 6376 | if (!str) { |
| 6377 | Py_DECREF(sub); |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 6378 | return -1; |
| 6379 | } |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6380 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6381 | result = stringlib_contains_obj(str, sub); |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 6382 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6383 | Py_DECREF(str); |
| 6384 | Py_DECREF(sub); |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6385 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6386 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6387 | } |
| 6388 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6389 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 6390 | |
| 6391 | PyObject *PyUnicode_Concat(PyObject *left, |
| 6392 | PyObject *right) |
| 6393 | { |
| 6394 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 6395 | |
| 6396 | /* Coerce the two arguments */ |
| 6397 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 6398 | if (u == NULL) |
| 6399 | goto onError; |
| 6400 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 6401 | if (v == NULL) |
| 6402 | goto onError; |
| 6403 | |
| 6404 | /* Shortcuts */ |
| 6405 | if (v == unicode_empty) { |
| 6406 | Py_DECREF(v); |
| 6407 | return (PyObject *)u; |
| 6408 | } |
| 6409 | if (u == unicode_empty) { |
| 6410 | Py_DECREF(u); |
| 6411 | return (PyObject *)v; |
| 6412 | } |
| 6413 | |
| 6414 | /* Concat the two Unicode strings */ |
| 6415 | w = _PyUnicode_New(u->length + v->length); |
| 6416 | if (w == NULL) |
| 6417 | goto onError; |
| 6418 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 6419 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 6420 | |
| 6421 | Py_DECREF(u); |
| 6422 | Py_DECREF(v); |
| 6423 | return (PyObject *)w; |
| 6424 | |
| 6425 | onError: |
| 6426 | Py_XDECREF(u); |
| 6427 | Py_XDECREF(v); |
| 6428 | return NULL; |
| 6429 | } |
| 6430 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6431 | PyDoc_STRVAR(count__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6432 | "S.count(sub[, start[, end]]) -> int\n\ |
| 6433 | \n\ |
Fredrik Lundh | 763b50f | 2006-05-22 15:35:12 +0000 | [diff] [blame] | 6434 | Return the number of non-overlapping occurrences of substring sub in\n\ |
| 6435 | 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] | 6436 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6437 | |
| 6438 | static PyObject * |
| 6439 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 6440 | { |
| 6441 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6442 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6443 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6444 | PyObject *result; |
| 6445 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6446 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 6447 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6448 | return NULL; |
| 6449 | |
| 6450 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6451 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6452 | if (substring == NULL) |
| 6453 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6454 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 6455 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6456 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6457 | result = PyInt_FromSsize_t( |
| 6458 | stringlib_count(self->str + start, end - start, |
| 6459 | substring->str, substring->length) |
| 6460 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6461 | |
| 6462 | Py_DECREF(substring); |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6463 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6464 | return result; |
| 6465 | } |
| 6466 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6467 | PyDoc_STRVAR(encode__doc__, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6468 | "S.encode([encoding[,errors]]) -> string or unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6469 | \n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6470 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 6471 | 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] | 6472 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6473 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 6474 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 6475 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6476 | |
| 6477 | static PyObject * |
| 6478 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 6479 | { |
| 6480 | char *encoding = NULL; |
| 6481 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6482 | PyObject *v; |
| 6483 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6484 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 6485 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6486 | v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6487 | if (v == NULL) |
| 6488 | goto onError; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 6489 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6490 | PyErr_Format(PyExc_TypeError, |
| 6491 | "encoder did not return a string/unicode object " |
| 6492 | "(type=%.400s)", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6493 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6494 | Py_DECREF(v); |
| 6495 | return NULL; |
| 6496 | } |
| 6497 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6498 | |
| 6499 | onError: |
| 6500 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6501 | } |
| 6502 | |
| 6503 | PyDoc_STRVAR(decode__doc__, |
| 6504 | "S.decode([encoding[,errors]]) -> string or unicode\n\ |
| 6505 | \n\ |
| 6506 | Decodes S using the codec registered for encoding. encoding defaults\n\ |
| 6507 | to the default encoding. errors may be given to set a different error\n\ |
| 6508 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 6509 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 6510 | as well as any other name registerd with codecs.register_error that is\n\ |
| 6511 | able to handle UnicodeDecodeErrors."); |
| 6512 | |
| 6513 | static PyObject * |
Marc-André Lemburg | 126b44c | 2004-07-10 12:04:20 +0000 | [diff] [blame] | 6514 | unicode_decode(PyUnicodeObject *self, PyObject *args) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6515 | { |
| 6516 | char *encoding = NULL; |
| 6517 | char *errors = NULL; |
| 6518 | PyObject *v; |
| 6519 | |
| 6520 | if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
| 6521 | return NULL; |
| 6522 | v = PyUnicode_AsDecodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6523 | if (v == NULL) |
| 6524 | goto onError; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 6525 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6526 | PyErr_Format(PyExc_TypeError, |
| 6527 | "decoder did not return a string/unicode object " |
| 6528 | "(type=%.400s)", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6529 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6530 | Py_DECREF(v); |
| 6531 | return NULL; |
| 6532 | } |
| 6533 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6534 | |
| 6535 | onError: |
| 6536 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6537 | } |
| 6538 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6539 | PyDoc_STRVAR(expandtabs__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6540 | "S.expandtabs([tabsize]) -> unicode\n\ |
| 6541 | \n\ |
| 6542 | 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] | 6543 | 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] | 6544 | |
| 6545 | static PyObject* |
| 6546 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 6547 | { |
| 6548 | Py_UNICODE *e; |
| 6549 | Py_UNICODE *p; |
| 6550 | Py_UNICODE *q; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6551 | Py_UNICODE *qe; |
| 6552 | Py_ssize_t i, j, incr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6553 | PyUnicodeObject *u; |
| 6554 | int tabsize = 8; |
| 6555 | |
| 6556 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 6557 | return NULL; |
| 6558 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6559 | /* First pass: determine size of output string */ |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6560 | i = 0; /* chars up to and including most recent \n or \r */ |
| 6561 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
| 6562 | e = self->str + self->length; /* end of input */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6563 | for (p = self->str; p < e; p++) |
| 6564 | if (*p == '\t') { |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 6565 | if (tabsize > 0) { |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6566 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 6567 | if (j > PY_SSIZE_T_MAX - incr) |
| 6568 | goto overflow1; |
| 6569 | j += incr; |
| 6570 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6571 | } |
| 6572 | else { |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6573 | if (j > PY_SSIZE_T_MAX - 1) |
| 6574 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6575 | j++; |
| 6576 | if (*p == '\n' || *p == '\r') { |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6577 | if (i > PY_SSIZE_T_MAX - j) |
| 6578 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6579 | i += j; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6580 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6581 | } |
| 6582 | } |
| 6583 | |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6584 | if (i > PY_SSIZE_T_MAX - j) |
| 6585 | goto overflow1; |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 6586 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6587 | /* Second pass: create output string and fill it */ |
| 6588 | u = _PyUnicode_New(i + j); |
| 6589 | if (!u) |
| 6590 | return NULL; |
| 6591 | |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6592 | j = 0; /* same as in first pass */ |
| 6593 | q = u->str; /* next output char */ |
| 6594 | qe = u->str + u->length; /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6595 | |
| 6596 | for (p = self->str; p < e; p++) |
| 6597 | if (*p == '\t') { |
| 6598 | if (tabsize > 0) { |
| 6599 | i = tabsize - (j % tabsize); |
| 6600 | j += i; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6601 | while (i--) { |
| 6602 | if (q >= qe) |
| 6603 | goto overflow2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6604 | *q++ = ' '; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6605 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6606 | } |
| 6607 | } |
| 6608 | else { |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6609 | if (q >= qe) |
| 6610 | goto overflow2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6611 | *q++ = *p; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6612 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6613 | if (*p == '\n' || *p == '\r') |
| 6614 | j = 0; |
| 6615 | } |
| 6616 | |
| 6617 | return (PyObject*) u; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6618 | |
| 6619 | overflow2: |
| 6620 | Py_DECREF(u); |
| 6621 | overflow1: |
| 6622 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 6623 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6624 | } |
| 6625 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6626 | PyDoc_STRVAR(find__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6627 | "S.find(sub [,start [,end]]) -> int\n\ |
| 6628 | \n\ |
| 6629 | Return the lowest index in S where substring sub is found,\n\ |
Georg Brandl | 9efd9b6 | 2007-07-29 17:38:35 +0000 | [diff] [blame] | 6630 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6631 | arguments start and end are interpreted as in slice notation.\n\ |
| 6632 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6633 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6634 | |
| 6635 | static PyObject * |
| 6636 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 6637 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6638 | PyObject *substring; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 6639 | Py_ssize_t start; |
| 6640 | Py_ssize_t end; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6641 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6642 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 6643 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6644 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6645 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 6646 | result = stringlib_find_slice( |
| 6647 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6648 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6649 | start, end |
| 6650 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6651 | |
| 6652 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6653 | |
| 6654 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6655 | } |
| 6656 | |
| 6657 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6658 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6659 | { |
| 6660 | if (index < 0 || index >= self->length) { |
| 6661 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 6662 | return NULL; |
| 6663 | } |
| 6664 | |
| 6665 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 6666 | } |
| 6667 | |
| 6668 | static long |
| 6669 | unicode_hash(PyUnicodeObject *self) |
| 6670 | { |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6671 | /* Since Unicode objects compare equal to their ASCII string |
| 6672 | counterparts, they should use the individual character values |
| 6673 | as basis for their hash value. This is needed to assure that |
| 6674 | strings and Unicode objects behave in the same way as |
| 6675 | dictionary keys. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6676 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6677 | register Py_ssize_t len; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6678 | register Py_UNICODE *p; |
| 6679 | register long x; |
| 6680 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6681 | if (self->hash != -1) |
| 6682 | return self->hash; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6683 | len = PyUnicode_GET_SIZE(self); |
| 6684 | p = PyUnicode_AS_UNICODE(self); |
| 6685 | x = *p << 7; |
| 6686 | while (--len >= 0) |
| 6687 | x = (1000003*x) ^ *p++; |
| 6688 | x ^= PyUnicode_GET_SIZE(self); |
| 6689 | if (x == -1) |
| 6690 | x = -2; |
| 6691 | self->hash = x; |
| 6692 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6693 | } |
| 6694 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6695 | PyDoc_STRVAR(index__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6696 | "S.index(sub [,start [,end]]) -> int\n\ |
| 6697 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6698 | 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] | 6699 | |
| 6700 | static PyObject * |
| 6701 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 6702 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6703 | Py_ssize_t result; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6704 | PyObject *substring; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 6705 | Py_ssize_t start; |
| 6706 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6707 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 6708 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6709 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6710 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 6711 | result = stringlib_find_slice( |
| 6712 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6713 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6714 | start, end |
| 6715 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6716 | |
| 6717 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6718 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6719 | if (result < 0) { |
| 6720 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 6721 | return NULL; |
| 6722 | } |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6723 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6724 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6725 | } |
| 6726 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6727 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6728 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6729 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6730 | 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] | 6731 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6732 | |
| 6733 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6734 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6735 | { |
| 6736 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6737 | register const Py_UNICODE *e; |
| 6738 | int cased; |
| 6739 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6740 | /* Shortcut for single character strings */ |
| 6741 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6742 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6743 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6744 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6745 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6746 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6747 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6748 | e = p + PyUnicode_GET_SIZE(self); |
| 6749 | cased = 0; |
| 6750 | for (; p < e; p++) { |
| 6751 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6752 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6753 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6754 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6755 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 6756 | cased = 1; |
| 6757 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6758 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6759 | } |
| 6760 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6761 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6762 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6763 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6764 | 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] | 6765 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6766 | |
| 6767 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6768 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6769 | { |
| 6770 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6771 | register const Py_UNICODE *e; |
| 6772 | int cased; |
| 6773 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6774 | /* Shortcut for single character strings */ |
| 6775 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6776 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6777 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6778 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6779 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6780 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6781 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6782 | e = p + PyUnicode_GET_SIZE(self); |
| 6783 | cased = 0; |
| 6784 | for (; p < e; p++) { |
| 6785 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6786 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6787 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6788 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6789 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 6790 | cased = 1; |
| 6791 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6792 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6793 | } |
| 6794 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6795 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6796 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6797 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6798 | Return True if S is a titlecased string and there is at least one\n\ |
| 6799 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 6800 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 6801 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6802 | |
| 6803 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6804 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6805 | { |
| 6806 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6807 | register const Py_UNICODE *e; |
| 6808 | int cased, previous_is_cased; |
| 6809 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6810 | /* Shortcut for single character strings */ |
| 6811 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6812 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 6813 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6814 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6815 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6816 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6817 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6818 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6819 | e = p + PyUnicode_GET_SIZE(self); |
| 6820 | cased = 0; |
| 6821 | previous_is_cased = 0; |
| 6822 | for (; p < e; p++) { |
| 6823 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6824 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6825 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 6826 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6827 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6828 | previous_is_cased = 1; |
| 6829 | cased = 1; |
| 6830 | } |
| 6831 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 6832 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6833 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6834 | previous_is_cased = 1; |
| 6835 | cased = 1; |
| 6836 | } |
| 6837 | else |
| 6838 | previous_is_cased = 0; |
| 6839 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6840 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6841 | } |
| 6842 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6843 | PyDoc_STRVAR(isspace__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6844 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6845 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6846 | Return True if all characters in S are whitespace\n\ |
| 6847 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6848 | |
| 6849 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6850 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6851 | { |
| 6852 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6853 | register const Py_UNICODE *e; |
| 6854 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6855 | /* Shortcut for single character strings */ |
| 6856 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6857 | Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6858 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6859 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6860 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6861 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6862 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6863 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6864 | e = p + PyUnicode_GET_SIZE(self); |
| 6865 | for (; p < e; p++) { |
| 6866 | if (!Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6867 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6868 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6869 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6870 | } |
| 6871 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6872 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6873 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6874 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6875 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6876 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6877 | |
| 6878 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6879 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6880 | { |
| 6881 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6882 | register const Py_UNICODE *e; |
| 6883 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6884 | /* Shortcut for single character strings */ |
| 6885 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6886 | Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6887 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6888 | |
| 6889 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6890 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6891 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6892 | |
| 6893 | e = p + PyUnicode_GET_SIZE(self); |
| 6894 | for (; p < e; p++) { |
| 6895 | if (!Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6896 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6897 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6898 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6899 | } |
| 6900 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6901 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6902 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6903 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6904 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6905 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6906 | |
| 6907 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6908 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6909 | { |
| 6910 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6911 | register const Py_UNICODE *e; |
| 6912 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6913 | /* Shortcut for single character strings */ |
| 6914 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6915 | Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6916 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6917 | |
| 6918 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6919 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6920 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6921 | |
| 6922 | e = p + PyUnicode_GET_SIZE(self); |
| 6923 | for (; p < e; p++) { |
| 6924 | if (!Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6925 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6926 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6927 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6928 | } |
| 6929 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6930 | PyDoc_STRVAR(isdecimal__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6931 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6932 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6933 | 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] | 6934 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6935 | |
| 6936 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6937 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6938 | { |
| 6939 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6940 | register const Py_UNICODE *e; |
| 6941 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6942 | /* Shortcut for single character strings */ |
| 6943 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6944 | Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6945 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6946 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6947 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6948 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6949 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6950 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6951 | e = p + PyUnicode_GET_SIZE(self); |
| 6952 | for (; p < e; p++) { |
| 6953 | if (!Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6954 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6955 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6956 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6957 | } |
| 6958 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6959 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6960 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6961 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6962 | Return True if all characters in S are digits\n\ |
| 6963 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6964 | |
| 6965 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6966 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6967 | { |
| 6968 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6969 | register const Py_UNICODE *e; |
| 6970 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6971 | /* Shortcut for single character strings */ |
| 6972 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6973 | Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6974 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6975 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6976 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6977 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6978 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6979 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6980 | e = p + PyUnicode_GET_SIZE(self); |
| 6981 | for (; p < e; p++) { |
| 6982 | if (!Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6983 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6984 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6985 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6986 | } |
| 6987 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6988 | PyDoc_STRVAR(isnumeric__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6989 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6990 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6991 | 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] | 6992 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6993 | |
| 6994 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6995 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6996 | { |
| 6997 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6998 | register const Py_UNICODE *e; |
| 6999 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7000 | /* Shortcut for single character strings */ |
| 7001 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7002 | Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7003 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7004 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7005 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7006 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7007 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7008 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7009 | e = p + PyUnicode_GET_SIZE(self); |
| 7010 | for (; p < e; p++) { |
| 7011 | if (!Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7012 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7013 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7014 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7015 | } |
| 7016 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7017 | PyDoc_STRVAR(join__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7018 | "S.join(sequence) -> unicode\n\ |
| 7019 | \n\ |
| 7020 | Return a string which is the concatenation of the strings in the\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7021 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7022 | |
| 7023 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7024 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7025 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7026 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7027 | } |
| 7028 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7029 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7030 | unicode_length(PyUnicodeObject *self) |
| 7031 | { |
| 7032 | return self->length; |
| 7033 | } |
| 7034 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7035 | PyDoc_STRVAR(ljust__doc__, |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 7036 | "S.ljust(width[, fillchar]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7037 | \n\ |
| 7038 | 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] | 7039 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7040 | |
| 7041 | static PyObject * |
| 7042 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 7043 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7044 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7045 | Py_UNICODE fillchar = ' '; |
| 7046 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7047 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7048 | return NULL; |
| 7049 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7050 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7051 | Py_INCREF(self); |
| 7052 | return (PyObject*) self; |
| 7053 | } |
| 7054 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7055 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7056 | } |
| 7057 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7058 | PyDoc_STRVAR(lower__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7059 | "S.lower() -> unicode\n\ |
| 7060 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7061 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7062 | |
| 7063 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7064 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7065 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7066 | return fixup(self, fixlower); |
| 7067 | } |
| 7068 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7069 | #define LEFTSTRIP 0 |
| 7070 | #define RIGHTSTRIP 1 |
| 7071 | #define BOTHSTRIP 2 |
| 7072 | |
| 7073 | /* Arrays indexed by above */ |
| 7074 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 7075 | |
| 7076 | #define STRIPNAME(i) (stripformat[i]+3) |
| 7077 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7078 | /* externally visible for str.strip(unicode) */ |
| 7079 | PyObject * |
| 7080 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 7081 | { |
| 7082 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7083 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7084 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7085 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 7086 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7087 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 7088 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
| 7089 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7090 | i = 0; |
| 7091 | if (striptype != RIGHTSTRIP) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 7092 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7093 | i++; |
| 7094 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7095 | } |
| 7096 | |
| 7097 | j = len; |
| 7098 | if (striptype != LEFTSTRIP) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 7099 | do { |
| 7100 | j--; |
| 7101 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 7102 | j++; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7103 | } |
| 7104 | |
| 7105 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 7106 | Py_INCREF(self); |
| 7107 | return (PyObject*)self; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7108 | } |
| 7109 | else |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 7110 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7111 | } |
| 7112 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7113 | |
| 7114 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7115 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7116 | { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7117 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7118 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7119 | |
| 7120 | i = 0; |
| 7121 | if (striptype != RIGHTSTRIP) { |
| 7122 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 7123 | i++; |
| 7124 | } |
| 7125 | } |
| 7126 | |
| 7127 | j = len; |
| 7128 | if (striptype != LEFTSTRIP) { |
| 7129 | do { |
| 7130 | j--; |
| 7131 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 7132 | j++; |
| 7133 | } |
| 7134 | |
| 7135 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 7136 | Py_INCREF(self); |
| 7137 | return (PyObject*)self; |
| 7138 | } |
| 7139 | else |
| 7140 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7141 | } |
| 7142 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7143 | |
| 7144 | static PyObject * |
| 7145 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 7146 | { |
| 7147 | PyObject *sep = NULL; |
| 7148 | |
| 7149 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 7150 | return NULL; |
| 7151 | |
| 7152 | if (sep != NULL && sep != Py_None) { |
| 7153 | if (PyUnicode_Check(sep)) |
| 7154 | return _PyUnicode_XStrip(self, striptype, sep); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 7155 | else if (PyString_Check(sep)) { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7156 | PyObject *res; |
| 7157 | sep = PyUnicode_FromObject(sep); |
| 7158 | if (sep==NULL) |
| 7159 | return NULL; |
| 7160 | res = _PyUnicode_XStrip(self, striptype, sep); |
| 7161 | Py_DECREF(sep); |
| 7162 | return res; |
| 7163 | } |
| 7164 | else { |
| 7165 | PyErr_Format(PyExc_TypeError, |
| 7166 | "%s arg must be None, unicode or str", |
| 7167 | STRIPNAME(striptype)); |
| 7168 | return NULL; |
| 7169 | } |
| 7170 | } |
| 7171 | |
| 7172 | return do_strip(self, striptype); |
| 7173 | } |
| 7174 | |
| 7175 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7176 | PyDoc_STRVAR(strip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7177 | "S.strip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7178 | \n\ |
| 7179 | Return a copy of the string S with leading and trailing\n\ |
| 7180 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7181 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7182 | 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] | 7183 | |
| 7184 | static PyObject * |
| 7185 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 7186 | { |
| 7187 | if (PyTuple_GET_SIZE(args) == 0) |
| 7188 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 7189 | else |
| 7190 | return do_argstrip(self, BOTHSTRIP, args); |
| 7191 | } |
| 7192 | |
| 7193 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7194 | PyDoc_STRVAR(lstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7195 | "S.lstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7196 | \n\ |
| 7197 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7198 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7199 | 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] | 7200 | |
| 7201 | static PyObject * |
| 7202 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 7203 | { |
| 7204 | if (PyTuple_GET_SIZE(args) == 0) |
| 7205 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 7206 | else |
| 7207 | return do_argstrip(self, LEFTSTRIP, args); |
| 7208 | } |
| 7209 | |
| 7210 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7211 | PyDoc_STRVAR(rstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7212 | "S.rstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7213 | \n\ |
| 7214 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7215 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7216 | 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] | 7217 | |
| 7218 | static PyObject * |
| 7219 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 7220 | { |
| 7221 | if (PyTuple_GET_SIZE(args) == 0) |
| 7222 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 7223 | else |
| 7224 | return do_argstrip(self, RIGHTSTRIP, args); |
| 7225 | } |
| 7226 | |
| 7227 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7228 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7229 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7230 | { |
| 7231 | PyUnicodeObject *u; |
| 7232 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7233 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7234 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7235 | |
| 7236 | if (len < 0) |
| 7237 | len = 0; |
| 7238 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7239 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7240 | /* no repeat, return original string */ |
| 7241 | Py_INCREF(str); |
| 7242 | return (PyObject*) str; |
| 7243 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7244 | |
| 7245 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 7246 | * needed doesn't overflow size_t |
| 7247 | */ |
| 7248 | nchars = len * str->length; |
| 7249 | if (len && nchars / len != str->length) { |
| 7250 | PyErr_SetString(PyExc_OverflowError, |
| 7251 | "repeated string is too long"); |
| 7252 | return NULL; |
| 7253 | } |
| 7254 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 7255 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 7256 | PyErr_SetString(PyExc_OverflowError, |
| 7257 | "repeated string is too long"); |
| 7258 | return NULL; |
| 7259 | } |
| 7260 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7261 | if (!u) |
| 7262 | return NULL; |
| 7263 | |
| 7264 | p = u->str; |
| 7265 | |
Fredrik Lundh | f1d60a5 | 2006-05-22 16:29:30 +0000 | [diff] [blame] | 7266 | if (str->length == 1 && len > 0) { |
| 7267 | Py_UNICODE_FILL(p, str->str[0], len); |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 7268 | } else { |
Tim Peters | 1bacc64 | 2006-05-23 05:47:16 +0000 | [diff] [blame] | 7269 | Py_ssize_t done = 0; /* number of characters copied this far */ |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 7270 | if (done < nchars) { |
Fredrik Lundh | f1d60a5 | 2006-05-22 16:29:30 +0000 | [diff] [blame] | 7271 | Py_UNICODE_COPY(p, str->str, str->length); |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 7272 | done = str->length; |
| 7273 | } |
| 7274 | while (done < nchars) { |
Neal Norwitz | 4677fbf7 | 2008-03-25 04:18:18 +0000 | [diff] [blame] | 7275 | Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 7276 | Py_UNICODE_COPY(p+done, p, n); |
| 7277 | done += n; |
| 7278 | } |
| 7279 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7280 | |
| 7281 | return (PyObject*) u; |
| 7282 | } |
| 7283 | |
| 7284 | PyObject *PyUnicode_Replace(PyObject *obj, |
| 7285 | PyObject *subobj, |
| 7286 | PyObject *replobj, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7287 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7288 | { |
| 7289 | PyObject *self; |
| 7290 | PyObject *str1; |
| 7291 | PyObject *str2; |
| 7292 | PyObject *result; |
| 7293 | |
| 7294 | self = PyUnicode_FromObject(obj); |
| 7295 | if (self == NULL) |
| 7296 | return NULL; |
| 7297 | str1 = PyUnicode_FromObject(subobj); |
| 7298 | if (str1 == NULL) { |
| 7299 | Py_DECREF(self); |
| 7300 | return NULL; |
| 7301 | } |
| 7302 | str2 = PyUnicode_FromObject(replobj); |
| 7303 | if (str2 == NULL) { |
| 7304 | Py_DECREF(self); |
| 7305 | Py_DECREF(str1); |
| 7306 | return NULL; |
| 7307 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7308 | result = replace((PyUnicodeObject *)self, |
| 7309 | (PyUnicodeObject *)str1, |
| 7310 | (PyUnicodeObject *)str2, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7311 | maxcount); |
| 7312 | Py_DECREF(self); |
| 7313 | Py_DECREF(str1); |
| 7314 | Py_DECREF(str2); |
| 7315 | return result; |
| 7316 | } |
| 7317 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7318 | PyDoc_STRVAR(replace__doc__, |
Georg Brandl | 30fadc1 | 2008-05-30 07:54:16 +0000 | [diff] [blame] | 7319 | "S.replace (old, new[, count]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7320 | \n\ |
| 7321 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | 30fadc1 | 2008-05-30 07:54:16 +0000 | [diff] [blame] | 7322 | old replaced by new. If the optional argument count is\n\ |
| 7323 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7324 | |
| 7325 | static PyObject* |
| 7326 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 7327 | { |
| 7328 | PyUnicodeObject *str1; |
| 7329 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7330 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7331 | PyObject *result; |
| 7332 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7333 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7334 | return NULL; |
| 7335 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 7336 | if (str1 == NULL) |
| 7337 | return NULL; |
| 7338 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7339 | if (str2 == NULL) { |
| 7340 | Py_DECREF(str1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7341 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7342 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7343 | |
| 7344 | result = replace(self, str1, str2, maxcount); |
| 7345 | |
| 7346 | Py_DECREF(str1); |
| 7347 | Py_DECREF(str2); |
| 7348 | return result; |
| 7349 | } |
| 7350 | |
| 7351 | static |
| 7352 | PyObject *unicode_repr(PyObject *unicode) |
| 7353 | { |
| 7354 | return unicodeescape_string(PyUnicode_AS_UNICODE(unicode), |
| 7355 | PyUnicode_GET_SIZE(unicode), |
| 7356 | 1); |
| 7357 | } |
| 7358 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7359 | PyDoc_STRVAR(rfind__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7360 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 7361 | \n\ |
| 7362 | Return the highest index in S where substring sub is found,\n\ |
Georg Brandl | 9efd9b6 | 2007-07-29 17:38:35 +0000 | [diff] [blame] | 7363 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7364 | arguments start and end are interpreted as in slice notation.\n\ |
| 7365 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7366 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7367 | |
| 7368 | static PyObject * |
| 7369 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 7370 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7371 | PyObject *substring; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 7372 | Py_ssize_t start; |
| 7373 | Py_ssize_t end; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7374 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7375 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 7376 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
| 7377 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7378 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 7379 | result = stringlib_rfind_slice( |
| 7380 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7381 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7382 | start, end |
| 7383 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7384 | |
| 7385 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7386 | |
| 7387 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7388 | } |
| 7389 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7390 | PyDoc_STRVAR(rindex__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7391 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 7392 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7393 | 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] | 7394 | |
| 7395 | static PyObject * |
| 7396 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 7397 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7398 | PyObject *substring; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 7399 | Py_ssize_t start; |
| 7400 | Py_ssize_t end; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7401 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7402 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 7403 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
| 7404 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7405 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 7406 | result = stringlib_rfind_slice( |
| 7407 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7408 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7409 | start, end |
| 7410 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7411 | |
| 7412 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7413 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7414 | if (result < 0) { |
| 7415 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7416 | return NULL; |
| 7417 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7418 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7419 | } |
| 7420 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7421 | PyDoc_STRVAR(rjust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7422 | "S.rjust(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7423 | \n\ |
| 7424 | 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] | 7425 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7426 | |
| 7427 | static PyObject * |
| 7428 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 7429 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7430 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7431 | Py_UNICODE fillchar = ' '; |
| 7432 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7433 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7434 | return NULL; |
| 7435 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7436 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7437 | Py_INCREF(self); |
| 7438 | return (PyObject*) self; |
| 7439 | } |
| 7440 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7441 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7442 | } |
| 7443 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7444 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7445 | 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] | 7446 | { |
| 7447 | /* standard clamping */ |
| 7448 | if (start < 0) |
| 7449 | start = 0; |
| 7450 | if (end < 0) |
| 7451 | end = 0; |
| 7452 | if (end > self->length) |
| 7453 | end = self->length; |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7454 | if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7455 | /* full slice, return original string */ |
| 7456 | Py_INCREF(self); |
| 7457 | return (PyObject*) self; |
| 7458 | } |
| 7459 | if (start > end) |
| 7460 | start = end; |
| 7461 | /* copy slice */ |
| 7462 | return (PyObject*) PyUnicode_FromUnicode(self->str + start, |
| 7463 | end - start); |
| 7464 | } |
| 7465 | |
| 7466 | PyObject *PyUnicode_Split(PyObject *s, |
| 7467 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7468 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7469 | { |
| 7470 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7471 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7472 | s = PyUnicode_FromObject(s); |
| 7473 | if (s == NULL) |
| 7474 | return NULL; |
| 7475 | if (sep != NULL) { |
| 7476 | sep = PyUnicode_FromObject(sep); |
| 7477 | if (sep == NULL) { |
| 7478 | Py_DECREF(s); |
| 7479 | return NULL; |
| 7480 | } |
| 7481 | } |
| 7482 | |
| 7483 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7484 | |
| 7485 | Py_DECREF(s); |
| 7486 | Py_XDECREF(sep); |
| 7487 | return result; |
| 7488 | } |
| 7489 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7490 | PyDoc_STRVAR(split__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7491 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 7492 | \n\ |
| 7493 | Return a list of the words in S, using sep as the\n\ |
| 7494 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Georg Brandl | dfb77db | 2008-05-11 09:11:40 +0000 | [diff] [blame] | 7495 | 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] | 7496 | whitespace string is a separator and empty strings are\n\ |
| 7497 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7498 | |
| 7499 | static PyObject* |
| 7500 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 7501 | { |
| 7502 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7503 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7504 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7505 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7506 | return NULL; |
| 7507 | |
| 7508 | if (substring == Py_None) |
| 7509 | return split(self, NULL, maxcount); |
| 7510 | else if (PyUnicode_Check(substring)) |
| 7511 | return split(self, (PyUnicodeObject *)substring, maxcount); |
| 7512 | else |
| 7513 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
| 7514 | } |
| 7515 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7516 | PyObject * |
| 7517 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 7518 | { |
| 7519 | PyObject* str_obj; |
| 7520 | PyObject* sep_obj; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7521 | PyObject* out; |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 7522 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7523 | str_obj = PyUnicode_FromObject(str_in); |
| 7524 | if (!str_obj) |
| 7525 | return NULL; |
| 7526 | sep_obj = PyUnicode_FromObject(sep_in); |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 7527 | if (!sep_obj) { |
| 7528 | Py_DECREF(str_obj); |
| 7529 | return NULL; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7530 | } |
| 7531 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 7532 | out = stringlib_partition( |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 7533 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7534 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7535 | ); |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7536 | |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 7537 | Py_DECREF(sep_obj); |
| 7538 | Py_DECREF(str_obj); |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7539 | |
| 7540 | return out; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7541 | } |
| 7542 | |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7543 | |
| 7544 | PyObject * |
| 7545 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 7546 | { |
| 7547 | PyObject* str_obj; |
| 7548 | PyObject* sep_obj; |
| 7549 | PyObject* out; |
| 7550 | |
| 7551 | str_obj = PyUnicode_FromObject(str_in); |
| 7552 | if (!str_obj) |
| 7553 | return NULL; |
| 7554 | sep_obj = PyUnicode_FromObject(sep_in); |
| 7555 | if (!sep_obj) { |
| 7556 | Py_DECREF(str_obj); |
| 7557 | return NULL; |
| 7558 | } |
| 7559 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 7560 | out = stringlib_rpartition( |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7561 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7562 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7563 | ); |
| 7564 | |
| 7565 | Py_DECREF(sep_obj); |
| 7566 | Py_DECREF(str_obj); |
| 7567 | |
| 7568 | return out; |
| 7569 | } |
| 7570 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7571 | PyDoc_STRVAR(partition__doc__, |
| 7572 | "S.partition(sep) -> (head, sep, tail)\n\ |
| 7573 | \n\ |
| 7574 | Searches for the separator sep in S, and returns the part before it,\n\ |
| 7575 | the separator itself, and the part after it. If the separator is not\n\ |
| 7576 | found, returns S and two empty strings."); |
| 7577 | |
| 7578 | static PyObject* |
Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 7579 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7580 | { |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7581 | return PyUnicode_Partition((PyObject *)self, separator); |
| 7582 | } |
| 7583 | |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7584 | PyDoc_STRVAR(rpartition__doc__, |
Raymond Hettinger | a0c95fa | 2006-09-04 15:32:48 +0000 | [diff] [blame] | 7585 | "S.rpartition(sep) -> (tail, sep, head)\n\ |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7586 | \n\ |
| 7587 | Searches for the separator sep in S, starting at the end of S, and returns\n\ |
| 7588 | the part before it, the separator itself, and the part after it. If the\n\ |
Raymond Hettinger | a0c95fa | 2006-09-04 15:32:48 +0000 | [diff] [blame] | 7589 | separator is not found, returns two empty strings and S."); |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7590 | |
| 7591 | static PyObject* |
| 7592 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 7593 | { |
| 7594 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 7595 | } |
| 7596 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7597 | PyObject *PyUnicode_RSplit(PyObject *s, |
| 7598 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7599 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7600 | { |
| 7601 | PyObject *result; |
| 7602 | |
| 7603 | s = PyUnicode_FromObject(s); |
| 7604 | if (s == NULL) |
| 7605 | return NULL; |
| 7606 | if (sep != NULL) { |
| 7607 | sep = PyUnicode_FromObject(sep); |
| 7608 | if (sep == NULL) { |
| 7609 | Py_DECREF(s); |
| 7610 | return NULL; |
| 7611 | } |
| 7612 | } |
| 7613 | |
| 7614 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7615 | |
| 7616 | Py_DECREF(s); |
| 7617 | Py_XDECREF(sep); |
| 7618 | return result; |
| 7619 | } |
| 7620 | |
| 7621 | PyDoc_STRVAR(rsplit__doc__, |
| 7622 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
| 7623 | \n\ |
| 7624 | Return a list of the words in S, using sep as the\n\ |
| 7625 | delimiter string, starting at the end of the string and\n\ |
| 7626 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 7627 | splits are done. If sep is not specified, any whitespace string\n\ |
| 7628 | is a separator."); |
| 7629 | |
| 7630 | static PyObject* |
| 7631 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 7632 | { |
| 7633 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7634 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7635 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7636 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7637 | return NULL; |
| 7638 | |
| 7639 | if (substring == Py_None) |
| 7640 | return rsplit(self, NULL, maxcount); |
| 7641 | else if (PyUnicode_Check(substring)) |
| 7642 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
| 7643 | else |
| 7644 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
| 7645 | } |
| 7646 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7647 | PyDoc_STRVAR(splitlines__doc__, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7648 | "S.splitlines([keepends]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7649 | \n\ |
| 7650 | 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] | 7651 | 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] | 7652 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7653 | |
| 7654 | static PyObject* |
| 7655 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 7656 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7657 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7658 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7659 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7660 | return NULL; |
| 7661 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7662 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7663 | } |
| 7664 | |
| 7665 | static |
| 7666 | PyObject *unicode_str(PyUnicodeObject *self) |
| 7667 | { |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7668 | return PyUnicode_AsEncodedString((PyObject *)self, NULL, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7669 | } |
| 7670 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7671 | PyDoc_STRVAR(swapcase__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7672 | "S.swapcase() -> unicode\n\ |
| 7673 | \n\ |
| 7674 | 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] | 7675 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7676 | |
| 7677 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7678 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7679 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7680 | return fixup(self, fixswapcase); |
| 7681 | } |
| 7682 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7683 | PyDoc_STRVAR(translate__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7684 | "S.translate(table) -> unicode\n\ |
| 7685 | \n\ |
| 7686 | Return a copy of the string S, where all characters have been mapped\n\ |
| 7687 | 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] | 7688 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ |
| 7689 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 7690 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7691 | |
| 7692 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7693 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7694 | { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7695 | return PyUnicode_TranslateCharmap(self->str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7696 | self->length, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7697 | table, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7698 | "ignore"); |
| 7699 | } |
| 7700 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7701 | PyDoc_STRVAR(upper__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7702 | "S.upper() -> unicode\n\ |
| 7703 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7704 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7705 | |
| 7706 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7707 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7708 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7709 | return fixup(self, fixupper); |
| 7710 | } |
| 7711 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7712 | PyDoc_STRVAR(zfill__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7713 | "S.zfill(width) -> unicode\n\ |
| 7714 | \n\ |
| 7715 | Pad a numeric string x with zeros on the left, to fill a field\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7716 | of the specified width. The string x is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7717 | |
| 7718 | static PyObject * |
| 7719 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 7720 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7721 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7722 | PyUnicodeObject *u; |
| 7723 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7724 | Py_ssize_t width; |
| 7725 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7726 | return NULL; |
| 7727 | |
| 7728 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 7729 | if (PyUnicode_CheckExact(self)) { |
| 7730 | Py_INCREF(self); |
| 7731 | return (PyObject*) self; |
| 7732 | } |
| 7733 | else |
| 7734 | return PyUnicode_FromUnicode( |
| 7735 | PyUnicode_AS_UNICODE(self), |
| 7736 | PyUnicode_GET_SIZE(self) |
| 7737 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7738 | } |
| 7739 | |
| 7740 | fill = width - self->length; |
| 7741 | |
| 7742 | u = pad(self, fill, 0, '0'); |
| 7743 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 7744 | if (u == NULL) |
| 7745 | return NULL; |
| 7746 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7747 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 7748 | /* move sign to beginning of string */ |
| 7749 | u->str[0] = u->str[fill]; |
| 7750 | u->str[fill] = '0'; |
| 7751 | } |
| 7752 | |
| 7753 | return (PyObject*) u; |
| 7754 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7755 | |
| 7756 | #if 0 |
| 7757 | static PyObject* |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 7758 | free_listsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7759 | { |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 7760 | return PyInt_FromLong(numfree); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7761 | } |
| 7762 | #endif |
| 7763 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7764 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7765 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7766 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7767 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 7768 | With optional start, test S beginning at that position.\n\ |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7769 | With optional end, stop comparing S at that position.\n\ |
| 7770 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7771 | |
| 7772 | static PyObject * |
| 7773 | unicode_startswith(PyUnicodeObject *self, |
| 7774 | PyObject *args) |
| 7775 | { |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7776 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7777 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7778 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7779 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7780 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7781 | |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7782 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7783 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7784 | return NULL; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7785 | if (PyTuple_Check(subobj)) { |
| 7786 | Py_ssize_t i; |
| 7787 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 7788 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 7789 | PyTuple_GET_ITEM(subobj, i)); |
| 7790 | if (substring == NULL) |
| 7791 | return NULL; |
| 7792 | result = tailmatch(self, substring, start, end, -1); |
| 7793 | Py_DECREF(substring); |
| 7794 | if (result) { |
| 7795 | Py_RETURN_TRUE; |
| 7796 | } |
| 7797 | } |
| 7798 | /* nothing matched */ |
| 7799 | Py_RETURN_FALSE; |
| 7800 | } |
| 7801 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7802 | if (substring == NULL) |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7803 | return NULL; |
| 7804 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7805 | Py_DECREF(substring); |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7806 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7807 | } |
| 7808 | |
| 7809 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7810 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7811 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7812 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7813 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 7814 | With optional start, test S beginning at that position.\n\ |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7815 | With optional end, stop comparing S at that position.\n\ |
| 7816 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7817 | |
| 7818 | static PyObject * |
| 7819 | unicode_endswith(PyUnicodeObject *self, |
| 7820 | PyObject *args) |
| 7821 | { |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7822 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7823 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7824 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7825 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7826 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7827 | |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7828 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 7829 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7830 | return NULL; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7831 | if (PyTuple_Check(subobj)) { |
| 7832 | Py_ssize_t i; |
| 7833 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 7834 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 7835 | PyTuple_GET_ITEM(subobj, i)); |
| 7836 | if (substring == NULL) |
| 7837 | return NULL; |
| 7838 | result = tailmatch(self, substring, start, end, +1); |
| 7839 | Py_DECREF(substring); |
| 7840 | if (result) { |
| 7841 | Py_RETURN_TRUE; |
| 7842 | } |
| 7843 | } |
| 7844 | Py_RETURN_FALSE; |
| 7845 | } |
| 7846 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7847 | if (substring == NULL) |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7848 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7849 | |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7850 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7851 | Py_DECREF(substring); |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7852 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7853 | } |
| 7854 | |
| 7855 | |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 7856 | /* Implements do_string_format, which is unicode because of stringlib */ |
| 7857 | #include "stringlib/string_format.h" |
| 7858 | |
| 7859 | PyDoc_STRVAR(format__doc__, |
| 7860 | "S.format(*args, **kwargs) -> unicode\n\ |
| 7861 | \n\ |
| 7862 | "); |
| 7863 | |
Eric Smith | dc13b79 | 2008-05-30 18:10:04 +0000 | [diff] [blame] | 7864 | static PyObject * |
| 7865 | unicode__format__(PyObject *self, PyObject *args) |
| 7866 | { |
| 7867 | PyObject *format_spec; |
| 7868 | PyObject *result = NULL; |
| 7869 | PyObject *tmp = NULL; |
| 7870 | |
| 7871 | /* If 2.x, convert format_spec to the same type as value */ |
| 7872 | /* This is to allow things like u''.format('') */ |
| 7873 | if (!PyArg_ParseTuple(args, "O:__format__", &format_spec)) |
| 7874 | goto done; |
| 7875 | if (!(PyBytes_Check(format_spec) || PyUnicode_Check(format_spec))) { |
| 7876 | PyErr_Format(PyExc_TypeError, "__format__ arg must be str " |
| 7877 | "or unicode, not %s", Py_TYPE(format_spec)->tp_name); |
| 7878 | goto done; |
| 7879 | } |
| 7880 | tmp = PyObject_Unicode(format_spec); |
| 7881 | if (tmp == NULL) |
| 7882 | goto done; |
| 7883 | format_spec = tmp; |
| 7884 | |
| 7885 | result = _PyUnicode_FormatAdvanced(self, |
| 7886 | PyUnicode_AS_UNICODE(format_spec), |
| 7887 | PyUnicode_GET_SIZE(format_spec)); |
| 7888 | done: |
| 7889 | Py_XDECREF(tmp); |
| 7890 | return result; |
| 7891 | } |
| 7892 | |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 7893 | PyDoc_STRVAR(p_format__doc__, |
| 7894 | "S.__format__(format_spec) -> unicode\n\ |
| 7895 | \n\ |
| 7896 | "); |
| 7897 | |
Robert Schuppenies | 901c997 | 2008-06-10 10:10:31 +0000 | [diff] [blame] | 7898 | static PyObject * |
| 7899 | unicode__sizeof__(PyUnicodeObject *v) |
| 7900 | { |
| 7901 | PyObject *res = NULL, *defsize = NULL; |
| 7902 | |
| 7903 | res = PyInt_FromSsize_t(sizeof(PyUnicodeObject) + |
| 7904 | sizeof(Py_UNICODE) * (v->length + 1)); |
| 7905 | if (v->defenc) { |
| 7906 | defsize = PyObject_CallMethod(v->defenc, "__sizeof__", NULL); |
| 7907 | if (defsize == NULL) { |
| 7908 | Py_DECREF(res); |
| 7909 | return NULL; |
| 7910 | } |
| 7911 | res = PyNumber_Add(res, defsize); |
| 7912 | Py_DECREF(defsize); |
| 7913 | } |
| 7914 | return res; |
| 7915 | } |
| 7916 | |
| 7917 | PyDoc_STRVAR(sizeof__doc__, |
| 7918 | "S.__sizeof__() -> size of S in memory, in bytes\n\ |
| 7919 | \n\ |
| 7920 | "); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 7921 | |
| 7922 | static PyObject * |
| 7923 | unicode_getnewargs(PyUnicodeObject *v) |
| 7924 | { |
| 7925 | return Py_BuildValue("(u#)", v->str, v->length); |
| 7926 | } |
| 7927 | |
| 7928 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7929 | static PyMethodDef unicode_methods[] = { |
| 7930 | |
| 7931 | /* Order is according to common usage: often used methods should |
| 7932 | appear first, since lookup is done sequentially. */ |
| 7933 | |
Georg Brandl | ecdc0a9 | 2006-03-30 12:19:07 +0000 | [diff] [blame] | 7934 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7935 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 7936 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7937 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7938 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 7939 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 7940 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 7941 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 7942 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 7943 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 7944 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 7945 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7946 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 7947 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 7948 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7949 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7950 | {"decode", (PyCFunction) unicode_decode, METH_VARARGS, decode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7951 | /* {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */ |
| 7952 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 7953 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 7954 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7955 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7956 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7957 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7958 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7959 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 7960 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 7961 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 7962 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 7963 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 7964 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 7965 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 7966 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 7967 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 7968 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 7969 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 7970 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 7971 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 7972 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7973 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 7974 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
| 7975 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
| 7976 | {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
| 7977 | {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
Robert Schuppenies | 901c997 | 2008-06-10 10:10:31 +0000 | [diff] [blame] | 7978 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 7979 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7980 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7981 | #endif |
| 7982 | |
| 7983 | #if 0 |
| 7984 | /* This one is just used for debugging the implementation. */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 7985 | {"freelistsize", (PyCFunction) free_listsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7986 | #endif |
| 7987 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 7988 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7989 | {NULL, NULL} |
| 7990 | }; |
| 7991 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7992 | static PyObject * |
| 7993 | unicode_mod(PyObject *v, PyObject *w) |
| 7994 | { |
| 7995 | if (!PyUnicode_Check(v)) { |
| 7996 | Py_INCREF(Py_NotImplemented); |
| 7997 | return Py_NotImplemented; |
| 7998 | } |
| 7999 | return PyUnicode_Format(v, w); |
| 8000 | } |
| 8001 | |
| 8002 | static PyNumberMethods unicode_as_number = { |
| 8003 | 0, /*nb_add*/ |
| 8004 | 0, /*nb_subtract*/ |
| 8005 | 0, /*nb_multiply*/ |
| 8006 | 0, /*nb_divide*/ |
| 8007 | unicode_mod, /*nb_remainder*/ |
| 8008 | }; |
| 8009 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8010 | static PySequenceMethods unicode_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8011 | (lenfunc) unicode_length, /* sq_length */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 8012 | PyUnicode_Concat, /* sq_concat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8013 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 8014 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 8015 | (ssizessizeargfunc) unicode_slice, /* sq_slice */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8016 | 0, /* sq_ass_item */ |
| 8017 | 0, /* sq_ass_slice */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 8018 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8019 | }; |
| 8020 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8021 | static PyObject* |
| 8022 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 8023 | { |
Marc-André Lemburg | 3a45779 | 2006-08-14 12:57:27 +0000 | [diff] [blame] | 8024 | if (PyIndex_Check(item)) { |
| 8025 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8026 | if (i == -1 && PyErr_Occurred()) |
| 8027 | return NULL; |
| 8028 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8029 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8030 | return unicode_getitem(self, i); |
| 8031 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8032 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8033 | Py_UNICODE* source_buf; |
| 8034 | Py_UNICODE* result_buf; |
| 8035 | PyObject* result; |
| 8036 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8037 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8038 | &start, &stop, &step, &slicelength) < 0) { |
| 8039 | return NULL; |
| 8040 | } |
| 8041 | |
| 8042 | if (slicelength <= 0) { |
| 8043 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 8044 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 8045 | PyUnicode_CheckExact(self)) { |
| 8046 | Py_INCREF(self); |
| 8047 | return (PyObject *)self; |
| 8048 | } else if (step == 1) { |
| 8049 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8050 | } else { |
| 8051 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 8052 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 8053 | sizeof(Py_UNICODE)); |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8054 | |
| 8055 | if (result_buf == NULL) |
| 8056 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8057 | |
| 8058 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 8059 | result_buf[i] = source_buf[cur]; |
| 8060 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8061 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8062 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 8063 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8064 | return result; |
| 8065 | } |
| 8066 | } else { |
| 8067 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 8068 | return NULL; |
| 8069 | } |
| 8070 | } |
| 8071 | |
| 8072 | static PyMappingMethods unicode_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8073 | (lenfunc)unicode_length, /* mp_length */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8074 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 8075 | (objobjargproc)0, /* mp_ass_subscript */ |
| 8076 | }; |
| 8077 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8078 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8079 | unicode_buffer_getreadbuf(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8080 | Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8081 | const void **ptr) |
| 8082 | { |
| 8083 | if (index != 0) { |
| 8084 | PyErr_SetString(PyExc_SystemError, |
| 8085 | "accessing non-existent unicode segment"); |
| 8086 | return -1; |
| 8087 | } |
| 8088 | *ptr = (void *) self->str; |
| 8089 | return PyUnicode_GET_DATA_SIZE(self); |
| 8090 | } |
| 8091 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8092 | static Py_ssize_t |
| 8093 | unicode_buffer_getwritebuf(PyUnicodeObject *self, Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8094 | const void **ptr) |
| 8095 | { |
| 8096 | PyErr_SetString(PyExc_TypeError, |
Neal Norwitz | 20e7213 | 2002-06-13 21:25:17 +0000 | [diff] [blame] | 8097 | "cannot use unicode as modifiable buffer"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8098 | return -1; |
| 8099 | } |
| 8100 | |
| 8101 | static int |
| 8102 | unicode_buffer_getsegcount(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8103 | Py_ssize_t *lenp) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8104 | { |
| 8105 | if (lenp) |
| 8106 | *lenp = PyUnicode_GET_DATA_SIZE(self); |
| 8107 | return 1; |
| 8108 | } |
| 8109 | |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 8110 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8111 | unicode_buffer_getcharbuf(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8112 | Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8113 | const void **ptr) |
| 8114 | { |
| 8115 | PyObject *str; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8116 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8117 | if (index != 0) { |
| 8118 | PyErr_SetString(PyExc_SystemError, |
| 8119 | "accessing non-existent unicode segment"); |
| 8120 | return -1; |
| 8121 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 8122 | str = _PyUnicode_AsDefaultEncodedString((PyObject *)self, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8123 | if (str == NULL) |
| 8124 | return -1; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 8125 | *ptr = (void *) PyString_AS_STRING(str); |
| 8126 | return PyString_GET_SIZE(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8127 | } |
| 8128 | |
| 8129 | /* Helpers for PyUnicode_Format() */ |
| 8130 | |
| 8131 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8132 | 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] | 8133 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8134 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8135 | if (argidx < arglen) { |
| 8136 | (*p_argidx)++; |
| 8137 | if (arglen < 0) |
| 8138 | return args; |
| 8139 | else |
| 8140 | return PyTuple_GetItem(args, argidx); |
| 8141 | } |
| 8142 | PyErr_SetString(PyExc_TypeError, |
| 8143 | "not enough arguments for format string"); |
| 8144 | return NULL; |
| 8145 | } |
| 8146 | |
| 8147 | #define F_LJUST (1<<0) |
| 8148 | #define F_SIGN (1<<1) |
| 8149 | #define F_BLANK (1<<2) |
| 8150 | #define F_ALT (1<<3) |
| 8151 | #define F_ZERO (1<<4) |
| 8152 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8153 | static Py_ssize_t |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8154 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8155 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8156 | register Py_ssize_t i; |
| 8157 | Py_ssize_t len = strlen(charbuffer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8158 | for (i = len - 1; i >= 0; i--) |
| 8159 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
| 8160 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8161 | return len; |
| 8162 | } |
| 8163 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8164 | static int |
| 8165 | doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) |
| 8166 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8167 | Py_ssize_t result; |
| 8168 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8169 | PyOS_ascii_formatd((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8170 | result = strtounicode(buffer, (char *)buffer); |
| 8171 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8172 | } |
| 8173 | |
| 8174 | static int |
| 8175 | longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) |
| 8176 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8177 | Py_ssize_t result; |
| 8178 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8179 | PyOS_snprintf((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8180 | result = strtounicode(buffer, (char *)buffer); |
| 8181 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8182 | } |
| 8183 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8184 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 8185 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 8186 | formatting is done. */ |
| 8187 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8188 | static int |
| 8189 | formatfloat(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8190 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8191 | int flags, |
| 8192 | int prec, |
| 8193 | int type, |
| 8194 | PyObject *v) |
| 8195 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8196 | /* fmt = '%#.' + `prec` + `type` |
| 8197 | 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] | 8198 | char fmt[20]; |
| 8199 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8200 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8201 | x = PyFloat_AsDouble(v); |
| 8202 | if (x == -1.0 && PyErr_Occurred()) |
| 8203 | return -1; |
| 8204 | if (prec < 0) |
| 8205 | prec = 6; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8206 | if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
| 8207 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8208 | /* Worst case length calc to ensure no buffer overrun: |
| 8209 | |
| 8210 | 'g' formats: |
| 8211 | fmt = %#.<prec>g |
| 8212 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 8213 | for any double rep.) |
| 8214 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 8215 | |
| 8216 | 'f' formats: |
| 8217 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 8218 | len = 1 + 50 + 1 + prec = 52 + prec |
| 8219 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8220 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8221 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8222 | |
| 8223 | */ |
Georg Brandl | 7c3b50d | 2007-07-12 08:38:00 +0000 | [diff] [blame] | 8224 | if (((type == 'g' || type == 'G') && |
| 8225 | buflen <= (size_t)10 + (size_t)prec) || |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8226 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8227 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8228 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8229 | return -1; |
| 8230 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8231 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 8232 | (flags&F_ALT) ? "#" : "", |
| 8233 | prec, type); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8234 | return doubletounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8235 | } |
| 8236 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8237 | static PyObject* |
| 8238 | formatlong(PyObject *val, int flags, int prec, int type) |
| 8239 | { |
| 8240 | char *buf; |
| 8241 | int i, len; |
| 8242 | PyObject *str; /* temporary string object. */ |
| 8243 | PyUnicodeObject *result; |
| 8244 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 8245 | str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8246 | if (!str) |
| 8247 | return NULL; |
| 8248 | result = _PyUnicode_New(len); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8249 | if (!result) { |
| 8250 | Py_DECREF(str); |
| 8251 | return NULL; |
| 8252 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8253 | for (i = 0; i < len; i++) |
| 8254 | result->str[i] = buf[i]; |
| 8255 | result->str[len] = 0; |
| 8256 | Py_DECREF(str); |
| 8257 | return (PyObject*)result; |
| 8258 | } |
| 8259 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8260 | static int |
| 8261 | formatint(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8262 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8263 | int flags, |
| 8264 | int prec, |
| 8265 | int type, |
| 8266 | PyObject *v) |
| 8267 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8268 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8269 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 8270 | * + 1 + 1 |
| 8271 | * = 24 |
| 8272 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8273 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8274 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8275 | long x; |
| 8276 | |
| 8277 | x = PyInt_AsLong(v); |
| 8278 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8279 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8280 | if (x < 0 && type == 'u') { |
| 8281 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8282 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8283 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 8284 | sign = "-"; |
| 8285 | else |
| 8286 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8287 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8288 | prec = 1; |
| 8289 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8290 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 8291 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8292 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8293 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8294 | PyErr_SetString(PyExc_OverflowError, |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8295 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8296 | return -1; |
| 8297 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8298 | |
| 8299 | if ((flags & F_ALT) && |
| 8300 | (type == 'x' || type == 'X')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8301 | /* When converting under %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8302 | * of issues that cause pain: |
| 8303 | * - when 0 is being converted, the C standard leaves off |
| 8304 | * the '0x' or '0X', which is inconsistent with other |
| 8305 | * %#x/%#X conversions and inconsistent with Python's |
| 8306 | * hex() function |
| 8307 | * - there are platforms that violate the standard and |
| 8308 | * convert 0 with the '0x' or '0X' |
| 8309 | * (Metrowerks, Compaq Tru64) |
| 8310 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8311 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8312 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8313 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8314 | * We can achieve the desired consistency by inserting our |
| 8315 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 8316 | * of %#x/%#X. |
| 8317 | * |
| 8318 | * Note that this is the same approach as used in |
| 8319 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8320 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8321 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 8322 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8323 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8324 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8325 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 8326 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8327 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 8328 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8329 | if (sign[0]) |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8330 | return longtounicode(buf, buflen, fmt, -x); |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8331 | else |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8332 | return longtounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8333 | } |
| 8334 | |
| 8335 | static int |
| 8336 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8337 | size_t buflen, |
| 8338 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8339 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8340 | /* presume that the buffer is at least 2 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8341 | if (PyUnicode_Check(v)) { |
| 8342 | if (PyUnicode_GET_SIZE(v) != 1) |
| 8343 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8344 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8345 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8346 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 8347 | else if (PyString_Check(v)) { |
| 8348 | if (PyString_GET_SIZE(v) != 1) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8349 | goto onError; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 8350 | buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8351 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8352 | |
| 8353 | else { |
| 8354 | /* Integer input truncated to a character */ |
| 8355 | long x; |
| 8356 | x = PyInt_AsLong(v); |
| 8357 | if (x == -1 && PyErr_Occurred()) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8358 | goto onError; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8359 | #ifdef Py_UNICODE_WIDE |
| 8360 | if (x < 0 || x > 0x10ffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 8361 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8362 | "%c arg not in range(0x110000) " |
| 8363 | "(wide Python build)"); |
| 8364 | return -1; |
| 8365 | } |
| 8366 | #else |
| 8367 | if (x < 0 || x > 0xffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 8368 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8369 | "%c arg not in range(0x10000) " |
| 8370 | "(narrow Python build)"); |
| 8371 | return -1; |
| 8372 | } |
| 8373 | #endif |
| 8374 | buf[0] = (Py_UNICODE) x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8375 | } |
| 8376 | buf[1] = '\0'; |
| 8377 | return 1; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8378 | |
| 8379 | onError: |
| 8380 | PyErr_SetString(PyExc_TypeError, |
| 8381 | "%c requires int or char"); |
| 8382 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8383 | } |
| 8384 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8385 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 8386 | |
| 8387 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 8388 | chars are formatted. XXX This is a magic number. Each formatting |
| 8389 | routine does bounds checking to ensure no overflow, but a better |
| 8390 | solution may be to malloc a buffer of appropriate size for each |
| 8391 | format. For now, the current solution is sufficient. |
| 8392 | */ |
| 8393 | #define FORMATBUFLEN (size_t)120 |
| 8394 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8395 | PyObject *PyUnicode_Format(PyObject *format, |
| 8396 | PyObject *args) |
| 8397 | { |
| 8398 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8399 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8400 | int args_owned = 0; |
| 8401 | PyUnicodeObject *result = NULL; |
| 8402 | PyObject *dict = NULL; |
| 8403 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8404 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8405 | if (format == NULL || args == NULL) { |
| 8406 | PyErr_BadInternalCall(); |
| 8407 | return NULL; |
| 8408 | } |
| 8409 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8410 | if (uformat == NULL) |
| 8411 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8412 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 8413 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 8414 | |
| 8415 | reslen = rescnt = fmtcnt + 100; |
| 8416 | result = _PyUnicode_New(reslen); |
| 8417 | if (result == NULL) |
| 8418 | goto onError; |
| 8419 | res = PyUnicode_AS_UNICODE(result); |
| 8420 | |
| 8421 | if (PyTuple_Check(args)) { |
| 8422 | arglen = PyTuple_Size(args); |
| 8423 | argidx = 0; |
| 8424 | } |
| 8425 | else { |
| 8426 | arglen = -1; |
| 8427 | argidx = -2; |
| 8428 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 8429 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 8430 | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8431 | dict = args; |
| 8432 | |
| 8433 | while (--fmtcnt >= 0) { |
| 8434 | if (*fmt != '%') { |
| 8435 | if (--rescnt < 0) { |
| 8436 | rescnt = fmtcnt + 100; |
| 8437 | reslen += rescnt; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8438 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8439 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8440 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 8441 | --rescnt; |
| 8442 | } |
| 8443 | *res++ = *fmt++; |
| 8444 | } |
| 8445 | else { |
| 8446 | /* Got a format specifier */ |
| 8447 | int flags = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8448 | Py_ssize_t width = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8449 | int prec = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8450 | Py_UNICODE c = '\0'; |
| 8451 | Py_UNICODE fill; |
Facundo Batista | c11cecf | 2008-02-24 03:17:21 +0000 | [diff] [blame] | 8452 | int isnumok; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8453 | PyObject *v = NULL; |
| 8454 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8455 | Py_UNICODE *pbuf; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8456 | Py_UNICODE sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8457 | Py_ssize_t len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8458 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8459 | |
| 8460 | fmt++; |
| 8461 | if (*fmt == '(') { |
| 8462 | Py_UNICODE *keystart; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8463 | Py_ssize_t keylen; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8464 | PyObject *key; |
| 8465 | int pcount = 1; |
| 8466 | |
| 8467 | if (dict == NULL) { |
| 8468 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8469 | "format requires a mapping"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8470 | goto onError; |
| 8471 | } |
| 8472 | ++fmt; |
| 8473 | --fmtcnt; |
| 8474 | keystart = fmt; |
| 8475 | /* Skip over balanced parentheses */ |
| 8476 | while (pcount > 0 && --fmtcnt >= 0) { |
| 8477 | if (*fmt == ')') |
| 8478 | --pcount; |
| 8479 | else if (*fmt == '(') |
| 8480 | ++pcount; |
| 8481 | fmt++; |
| 8482 | } |
| 8483 | keylen = fmt - keystart - 1; |
| 8484 | if (fmtcnt < 0 || pcount > 0) { |
| 8485 | PyErr_SetString(PyExc_ValueError, |
| 8486 | "incomplete format key"); |
| 8487 | goto onError; |
| 8488 | } |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 8489 | #if 0 |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8490 | /* keys are converted to strings using UTF-8 and |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8491 | then looked up since Python uses strings to hold |
| 8492 | variables names etc. in its namespaces and we |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8493 | wouldn't want to break common idioms. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8494 | key = PyUnicode_EncodeUTF8(keystart, |
| 8495 | keylen, |
| 8496 | NULL); |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 8497 | #else |
| 8498 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 8499 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8500 | if (key == NULL) |
| 8501 | goto onError; |
| 8502 | if (args_owned) { |
| 8503 | Py_DECREF(args); |
| 8504 | args_owned = 0; |
| 8505 | } |
| 8506 | args = PyObject_GetItem(dict, key); |
| 8507 | Py_DECREF(key); |
| 8508 | if (args == NULL) { |
| 8509 | goto onError; |
| 8510 | } |
| 8511 | args_owned = 1; |
| 8512 | arglen = -1; |
| 8513 | argidx = -2; |
| 8514 | } |
| 8515 | while (--fmtcnt >= 0) { |
| 8516 | switch (c = *fmt++) { |
| 8517 | case '-': flags |= F_LJUST; continue; |
| 8518 | case '+': flags |= F_SIGN; continue; |
| 8519 | case ' ': flags |= F_BLANK; continue; |
| 8520 | case '#': flags |= F_ALT; continue; |
| 8521 | case '0': flags |= F_ZERO; continue; |
| 8522 | } |
| 8523 | break; |
| 8524 | } |
| 8525 | if (c == '*') { |
| 8526 | v = getnextarg(args, arglen, &argidx); |
| 8527 | if (v == NULL) |
| 8528 | goto onError; |
| 8529 | if (!PyInt_Check(v)) { |
| 8530 | PyErr_SetString(PyExc_TypeError, |
| 8531 | "* wants int"); |
| 8532 | goto onError; |
| 8533 | } |
| 8534 | width = PyInt_AsLong(v); |
| 8535 | if (width < 0) { |
| 8536 | flags |= F_LJUST; |
| 8537 | width = -width; |
| 8538 | } |
| 8539 | if (--fmtcnt >= 0) |
| 8540 | c = *fmt++; |
| 8541 | } |
| 8542 | else if (c >= '0' && c <= '9') { |
| 8543 | width = c - '0'; |
| 8544 | while (--fmtcnt >= 0) { |
| 8545 | c = *fmt++; |
| 8546 | if (c < '0' || c > '9') |
| 8547 | break; |
| 8548 | if ((width*10) / 10 != width) { |
| 8549 | PyErr_SetString(PyExc_ValueError, |
| 8550 | "width too big"); |
| 8551 | goto onError; |
| 8552 | } |
| 8553 | width = width*10 + (c - '0'); |
| 8554 | } |
| 8555 | } |
| 8556 | if (c == '.') { |
| 8557 | prec = 0; |
| 8558 | if (--fmtcnt >= 0) |
| 8559 | c = *fmt++; |
| 8560 | if (c == '*') { |
| 8561 | v = getnextarg(args, arglen, &argidx); |
| 8562 | if (v == NULL) |
| 8563 | goto onError; |
| 8564 | if (!PyInt_Check(v)) { |
| 8565 | PyErr_SetString(PyExc_TypeError, |
| 8566 | "* wants int"); |
| 8567 | goto onError; |
| 8568 | } |
| 8569 | prec = PyInt_AsLong(v); |
| 8570 | if (prec < 0) |
| 8571 | prec = 0; |
| 8572 | if (--fmtcnt >= 0) |
| 8573 | c = *fmt++; |
| 8574 | } |
| 8575 | else if (c >= '0' && c <= '9') { |
| 8576 | prec = c - '0'; |
| 8577 | while (--fmtcnt >= 0) { |
| 8578 | c = Py_CHARMASK(*fmt++); |
| 8579 | if (c < '0' || c > '9') |
| 8580 | break; |
| 8581 | if ((prec*10) / 10 != prec) { |
| 8582 | PyErr_SetString(PyExc_ValueError, |
| 8583 | "prec too big"); |
| 8584 | goto onError; |
| 8585 | } |
| 8586 | prec = prec*10 + (c - '0'); |
| 8587 | } |
| 8588 | } |
| 8589 | } /* prec */ |
| 8590 | if (fmtcnt >= 0) { |
| 8591 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8592 | if (--fmtcnt >= 0) |
| 8593 | c = *fmt++; |
| 8594 | } |
| 8595 | } |
| 8596 | if (fmtcnt < 0) { |
| 8597 | PyErr_SetString(PyExc_ValueError, |
| 8598 | "incomplete format"); |
| 8599 | goto onError; |
| 8600 | } |
| 8601 | if (c != '%') { |
| 8602 | v = getnextarg(args, arglen, &argidx); |
| 8603 | if (v == NULL) |
| 8604 | goto onError; |
| 8605 | } |
| 8606 | sign = 0; |
| 8607 | fill = ' '; |
| 8608 | switch (c) { |
| 8609 | |
| 8610 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8611 | pbuf = formatbuf; |
| 8612 | /* presume that buffer length is at least 1 */ |
| 8613 | pbuf[0] = '%'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8614 | len = 1; |
| 8615 | break; |
| 8616 | |
| 8617 | case 's': |
| 8618 | case 'r': |
| 8619 | if (PyUnicode_Check(v) && c == 's') { |
| 8620 | temp = v; |
| 8621 | Py_INCREF(temp); |
| 8622 | } |
| 8623 | else { |
| 8624 | PyObject *unicode; |
| 8625 | if (c == 's') |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8626 | temp = PyObject_Unicode(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8627 | else |
| 8628 | temp = PyObject_Repr(v); |
| 8629 | if (temp == NULL) |
| 8630 | goto onError; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8631 | if (PyUnicode_Check(temp)) |
| 8632 | /* nothing to do */; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 8633 | else if (PyString_Check(temp)) { |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8634 | /* convert to string to Unicode */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 8635 | unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
| 8636 | PyString_GET_SIZE(temp), |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8637 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8638 | "strict"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8639 | Py_DECREF(temp); |
| 8640 | temp = unicode; |
| 8641 | if (temp == NULL) |
| 8642 | goto onError; |
| 8643 | } |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8644 | else { |
| 8645 | Py_DECREF(temp); |
| 8646 | PyErr_SetString(PyExc_TypeError, |
| 8647 | "%s argument has non-string str()"); |
| 8648 | goto onError; |
| 8649 | } |
| 8650 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8651 | pbuf = PyUnicode_AS_UNICODE(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8652 | len = PyUnicode_GET_SIZE(temp); |
| 8653 | if (prec >= 0 && len > prec) |
| 8654 | len = prec; |
| 8655 | break; |
| 8656 | |
| 8657 | case 'i': |
| 8658 | case 'd': |
| 8659 | case 'u': |
| 8660 | case 'o': |
| 8661 | case 'x': |
| 8662 | case 'X': |
| 8663 | if (c == 'i') |
| 8664 | c = 'd'; |
Facundo Batista | c11cecf | 2008-02-24 03:17:21 +0000 | [diff] [blame] | 8665 | isnumok = 0; |
| 8666 | if (PyNumber_Check(v)) { |
| 8667 | PyObject *iobj=NULL; |
| 8668 | |
| 8669 | if (PyInt_Check(v) || (PyLong_Check(v))) { |
| 8670 | iobj = v; |
| 8671 | Py_INCREF(iobj); |
| 8672 | } |
| 8673 | else { |
| 8674 | iobj = PyNumber_Int(v); |
| 8675 | if (iobj==NULL) iobj = PyNumber_Long(v); |
| 8676 | } |
| 8677 | if (iobj!=NULL) { |
| 8678 | if (PyInt_Check(iobj)) { |
| 8679 | isnumok = 1; |
| 8680 | pbuf = formatbuf; |
| 8681 | len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8682 | flags, prec, c, iobj); |
| 8683 | Py_DECREF(iobj); |
| 8684 | if (len < 0) |
| 8685 | goto onError; |
| 8686 | sign = 1; |
| 8687 | } |
| 8688 | else if (PyLong_Check(iobj)) { |
| 8689 | isnumok = 1; |
| 8690 | temp = formatlong(iobj, flags, prec, c); |
| 8691 | Py_DECREF(iobj); |
| 8692 | if (!temp) |
| 8693 | goto onError; |
| 8694 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 8695 | len = PyUnicode_GET_SIZE(temp); |
| 8696 | sign = 1; |
| 8697 | } |
| 8698 | else { |
| 8699 | Py_DECREF(iobj); |
| 8700 | } |
| 8701 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8702 | } |
Facundo Batista | c11cecf | 2008-02-24 03:17:21 +0000 | [diff] [blame] | 8703 | if (!isnumok) { |
| 8704 | PyErr_Format(PyExc_TypeError, |
| 8705 | "%%%c format: a number is required, " |
Martin v. Löwis | d918e4e | 2008-04-07 03:08:28 +0000 | [diff] [blame] | 8706 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8707 | goto onError; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8708 | } |
| 8709 | if (flags & F_ZERO) |
| 8710 | fill = '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8711 | break; |
| 8712 | |
| 8713 | case 'e': |
| 8714 | case 'E': |
| 8715 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 8716 | case 'F': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8717 | case 'g': |
| 8718 | case 'G': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 8719 | if (c == 'F') |
| 8720 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8721 | pbuf = formatbuf; |
| 8722 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8723 | flags, prec, c, v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8724 | if (len < 0) |
| 8725 | goto onError; |
| 8726 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8727 | if (flags & F_ZERO) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8728 | fill = '0'; |
| 8729 | break; |
| 8730 | |
| 8731 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8732 | pbuf = formatbuf; |
| 8733 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8734 | if (len < 0) |
| 8735 | goto onError; |
| 8736 | break; |
| 8737 | |
| 8738 | default: |
| 8739 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 8740 | "unsupported format character '%c' (0x%x) " |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 8741 | "at index %zd", |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8742 | (31<=c && c<=126) ? (char)c : '?', |
Marc-André Lemburg | 24e53b6 | 2002-09-24 09:32:14 +0000 | [diff] [blame] | 8743 | (int)c, |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 8744 | (Py_ssize_t)(fmt - 1 - |
| 8745 | PyUnicode_AS_UNICODE(uformat))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8746 | goto onError; |
| 8747 | } |
| 8748 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8749 | if (*pbuf == '-' || *pbuf == '+') { |
| 8750 | sign = *pbuf++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8751 | len--; |
| 8752 | } |
| 8753 | else if (flags & F_SIGN) |
| 8754 | sign = '+'; |
| 8755 | else if (flags & F_BLANK) |
| 8756 | sign = ' '; |
| 8757 | else |
| 8758 | sign = 0; |
| 8759 | } |
| 8760 | if (width < len) |
| 8761 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8762 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8763 | reslen -= rescnt; |
| 8764 | rescnt = width + fmtcnt + 100; |
| 8765 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8766 | if (reslen < 0) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8767 | Py_XDECREF(temp); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8768 | PyErr_NoMemory(); |
| 8769 | goto onError; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8770 | } |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8771 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 8772 | Py_XDECREF(temp); |
| 8773 | goto onError; |
| 8774 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8775 | res = PyUnicode_AS_UNICODE(result) |
| 8776 | + reslen - rescnt; |
| 8777 | } |
| 8778 | if (sign) { |
| 8779 | if (fill != ' ') |
| 8780 | *res++ = sign; |
| 8781 | rescnt--; |
| 8782 | if (width > len) |
| 8783 | width--; |
| 8784 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8785 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 8786 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8787 | assert(pbuf[1] == c); |
| 8788 | if (fill != ' ') { |
| 8789 | *res++ = *pbuf++; |
| 8790 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8791 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8792 | rescnt -= 2; |
| 8793 | width -= 2; |
| 8794 | if (width < 0) |
| 8795 | width = 0; |
| 8796 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8797 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8798 | if (width > len && !(flags & F_LJUST)) { |
| 8799 | do { |
| 8800 | --rescnt; |
| 8801 | *res++ = fill; |
| 8802 | } while (--width > len); |
| 8803 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8804 | if (fill == ' ') { |
| 8805 | if (sign) |
| 8806 | *res++ = sign; |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8807 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8808 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8809 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8810 | *res++ = *pbuf++; |
| 8811 | *res++ = *pbuf++; |
| 8812 | } |
| 8813 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8814 | Py_UNICODE_COPY(res, pbuf, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8815 | res += len; |
| 8816 | rescnt -= len; |
| 8817 | while (--width >= len) { |
| 8818 | --rescnt; |
| 8819 | *res++ = ' '; |
| 8820 | } |
| 8821 | if (dict && (argidx < arglen) && c != '%') { |
| 8822 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 8823 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8824 | Py_XDECREF(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8825 | goto onError; |
| 8826 | } |
| 8827 | Py_XDECREF(temp); |
| 8828 | } /* '%' */ |
| 8829 | } /* until end */ |
| 8830 | if (argidx < arglen && !dict) { |
| 8831 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 8832 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8833 | goto onError; |
| 8834 | } |
| 8835 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8836 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
| 8837 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8838 | if (args_owned) { |
| 8839 | Py_DECREF(args); |
| 8840 | } |
| 8841 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8842 | return (PyObject *)result; |
| 8843 | |
| 8844 | onError: |
| 8845 | Py_XDECREF(result); |
| 8846 | Py_DECREF(uformat); |
| 8847 | if (args_owned) { |
| 8848 | Py_DECREF(args); |
| 8849 | } |
| 8850 | return NULL; |
| 8851 | } |
| 8852 | |
| 8853 | static PyBufferProcs unicode_as_buffer = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8854 | (readbufferproc) unicode_buffer_getreadbuf, |
| 8855 | (writebufferproc) unicode_buffer_getwritebuf, |
| 8856 | (segcountproc) unicode_buffer_getsegcount, |
| 8857 | (charbufferproc) unicode_buffer_getcharbuf, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8858 | }; |
| 8859 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 8860 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8861 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 8862 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8863 | static PyObject * |
| 8864 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8865 | { |
| 8866 | PyObject *x = NULL; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 8867 | static char *kwlist[] = {"string", "encoding", "errors", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8868 | char *encoding = NULL; |
| 8869 | char *errors = NULL; |
| 8870 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8871 | if (type != &PyUnicode_Type) |
| 8872 | return unicode_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8873 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
| 8874 | kwlist, &x, &encoding, &errors)) |
| 8875 | return NULL; |
| 8876 | if (x == NULL) |
| 8877 | return (PyObject *)_PyUnicode_New(0); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 8878 | if (encoding == NULL && errors == NULL) |
| 8879 | return PyObject_Unicode(x); |
| 8880 | else |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8881 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
| 8882 | } |
| 8883 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8884 | static PyObject * |
| 8885 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8886 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8887 | PyUnicodeObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8888 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8889 | |
| 8890 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 8891 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 8892 | if (tmp == NULL) |
| 8893 | return NULL; |
| 8894 | assert(PyUnicode_Check(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8895 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8896 | if (pnew == NULL) { |
| 8897 | Py_DECREF(tmp); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8898 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8899 | } |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 8900 | pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8901 | if (pnew->str == NULL) { |
| 8902 | _Py_ForgetReference((PyObject *)pnew); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8903 | PyObject_Del(pnew); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8904 | Py_DECREF(tmp); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 8905 | return PyErr_NoMemory(); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8906 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8907 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 8908 | pnew->length = n; |
| 8909 | pnew->hash = tmp->hash; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8910 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8911 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8912 | } |
| 8913 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8914 | PyDoc_STRVAR(unicode_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8915 | "unicode(string [, encoding[, errors]]) -> object\n\ |
| 8916 | \n\ |
| 8917 | Create a new Unicode object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 8918 | encoding defaults to the current default string encoding.\n\ |
| 8919 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8920 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8921 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 8922 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8923 | "unicode", /* tp_name */ |
| 8924 | sizeof(PyUnicodeObject), /* tp_size */ |
| 8925 | 0, /* tp_itemsize */ |
| 8926 | /* Slots */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 8927 | (destructor)unicode_dealloc, /* tp_dealloc */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8928 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8929 | 0, /* tp_getattr */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8930 | 0, /* tp_setattr */ |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 8931 | 0, /* tp_compare */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 8932 | unicode_repr, /* tp_repr */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8933 | &unicode_as_number, /* tp_as_number */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8934 | &unicode_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8935 | &unicode_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8936 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 8937 | 0, /* tp_call*/ |
| 8938 | (reprfunc) unicode_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8939 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 8940 | 0, /* tp_setattro */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8941 | &unicode_as_buffer, /* tp_as_buffer */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8942 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
Neal Norwitz | ee3a1b5 | 2007-02-25 19:44:48 +0000 | [diff] [blame] | 8943 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8944 | unicode_doc, /* tp_doc */ |
| 8945 | 0, /* tp_traverse */ |
| 8946 | 0, /* tp_clear */ |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 8947 | PyUnicode_RichCompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8948 | 0, /* tp_weaklistoffset */ |
| 8949 | 0, /* tp_iter */ |
| 8950 | 0, /* tp_iternext */ |
| 8951 | unicode_methods, /* tp_methods */ |
| 8952 | 0, /* tp_members */ |
| 8953 | 0, /* tp_getset */ |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 8954 | &PyBaseString_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8955 | 0, /* tp_dict */ |
| 8956 | 0, /* tp_descr_get */ |
| 8957 | 0, /* tp_descr_set */ |
| 8958 | 0, /* tp_dictoffset */ |
| 8959 | 0, /* tp_init */ |
| 8960 | 0, /* tp_alloc */ |
| 8961 | unicode_new, /* tp_new */ |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8962 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8963 | }; |
| 8964 | |
| 8965 | /* Initialize the Unicode implementation */ |
| 8966 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 8967 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8968 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8969 | int i; |
| 8970 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 8971 | /* XXX - move this array to unicodectype.c ? */ |
| 8972 | Py_UNICODE linebreak[] = { |
| 8973 | 0x000A, /* LINE FEED */ |
| 8974 | 0x000D, /* CARRIAGE RETURN */ |
| 8975 | 0x001C, /* FILE SEPARATOR */ |
| 8976 | 0x001D, /* GROUP SEPARATOR */ |
| 8977 | 0x001E, /* RECORD SEPARATOR */ |
| 8978 | 0x0085, /* NEXT LINE */ |
| 8979 | 0x2028, /* LINE SEPARATOR */ |
| 8980 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 8981 | }; |
| 8982 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8983 | /* Init the implementation */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 8984 | free_list = NULL; |
| 8985 | numfree = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8986 | unicode_empty = _PyUnicode_New(0); |
Neal Norwitz | e1fdb32 | 2006-07-21 05:32:28 +0000 | [diff] [blame] | 8987 | if (!unicode_empty) |
| 8988 | return; |
| 8989 | |
Marc-André Lemburg | 90e8147 | 2000-06-07 09:13:21 +0000 | [diff] [blame] | 8990 | strcpy(unicode_default_encoding, "ascii"); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8991 | for (i = 0; i < 256; i++) |
| 8992 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 8993 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 8994 | Py_FatalError("Can't initialize 'unicode'"); |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 8995 | |
| 8996 | /* initialize the linebreak bloom filter */ |
| 8997 | bloom_linebreak = make_bloom_mask( |
| 8998 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 8999 | ); |
Neal Norwitz | de4c78a | 2006-06-13 08:28:19 +0000 | [diff] [blame] | 9000 | |
| 9001 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9002 | } |
| 9003 | |
| 9004 | /* Finalize the Unicode implementation */ |
| 9005 | |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 9006 | int |
| 9007 | PyUnicode_ClearFreeList(void) |
| 9008 | { |
| 9009 | int freelist_size = numfree; |
| 9010 | PyUnicodeObject *u; |
| 9011 | |
| 9012 | for (u = free_list; u != NULL;) { |
| 9013 | PyUnicodeObject *v = u; |
| 9014 | u = *(PyUnicodeObject **)u; |
| 9015 | if (v->str) |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 9016 | PyObject_DEL(v->str); |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 9017 | Py_XDECREF(v->defenc); |
| 9018 | PyObject_Del(v); |
| 9019 | numfree--; |
| 9020 | } |
| 9021 | free_list = NULL; |
| 9022 | assert(numfree == 0); |
| 9023 | return freelist_size; |
| 9024 | } |
| 9025 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9026 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9027 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9028 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9029 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9030 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 9031 | Py_XDECREF(unicode_empty); |
| 9032 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9033 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9034 | for (i = 0; i < 256; i++) { |
| 9035 | if (unicode_latin1[i]) { |
| 9036 | Py_DECREF(unicode_latin1[i]); |
| 9037 | unicode_latin1[i] = NULL; |
| 9038 | } |
| 9039 | } |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 9040 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9041 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9042 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 9043 | #ifdef __cplusplus |
| 9044 | } |
| 9045 | #endif |
| 9046 | |
| 9047 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9048 | /* |
| 9049 | Local variables: |
| 9050 | c-basic-offset: 4 |
| 9051 | indent-tabs-mode: nil |
| 9052 | End: |
| 9053 | */ |