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 | |
Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 7 | Copyright (c) Corporation for National Research Initiatives. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 9 | -------------------------------------------------------------------- |
| 10 | The original string type implementation is: |
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 | Copyright (c) 1999 by Secret Labs AB |
| 13 | Copyright (c) 1999 by Fredrik Lundh |
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 | By obtaining, using, and/or copying this software and/or its |
| 16 | associated documentation, you agree that you have read, understood, |
| 17 | and will comply with the following terms and conditions: |
| 18 | |
| 19 | Permission to use, copy, modify, and distribute this software and its |
| 20 | associated documentation for any purpose and without fee is hereby |
| 21 | granted, provided that the above copyright notice appears in all |
| 22 | copies, and that both that copyright notice and this permission notice |
| 23 | appear in supporting documentation, and that the name of Secret Labs |
| 24 | AB or the author not be used in advertising or publicity pertaining to |
| 25 | distribution of the software without specific, written prior |
| 26 | permission. |
| 27 | |
| 28 | SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 29 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 30 | FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR |
| 31 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 32 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 33 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 34 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 35 | -------------------------------------------------------------------- |
| 36 | |
| 37 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 38 | |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 39 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 40 | #include "Python.h" |
| 41 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 42 | #include "unicodeobject.h" |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 43 | #include "ucnhash.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 44 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 45 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 46 | #include <windows.h> |
| 47 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 48 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 49 | #undef USE_INLINE /* XXX - set via configure? */ |
| 50 | |
| 51 | #if defined(_MSC_VER) /* this is taken from _sre.c */ |
| 52 | #pragma warning(disable: 4710) |
| 53 | /* fastest possible local call under MSVC */ |
| 54 | #define LOCAL(type) static __inline type __fastcall |
| 55 | #elif defined(USE_INLINE) |
| 56 | #define LOCAL(type) static inline type |
| 57 | #else |
| 58 | #define LOCAL(type) static type |
| 59 | #endif |
| 60 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 61 | /* Limit for the Unicode object free list */ |
| 62 | |
| 63 | #define MAX_UNICODE_FREELIST_SIZE 1024 |
| 64 | |
| 65 | /* Limit for the Unicode object free list stay alive optimization. |
| 66 | |
| 67 | The implementation will keep allocated Unicode memory intact for |
| 68 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 69 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 70 | |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 71 | At worst this will result in MAX_UNICODE_FREELIST_SIZE * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 72 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 73 | malloc()-overhead) bytes of unused garbage. |
| 74 | |
| 75 | Setting the limit to 0 effectively turns the feature off. |
| 76 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 77 | Note: This is an experimental feature ! If you get core dumps when |
| 78 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 79 | |
| 80 | */ |
| 81 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 82 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 83 | |
| 84 | /* Endianness switches; defaults to little endian */ |
| 85 | |
| 86 | #ifdef WORDS_BIGENDIAN |
| 87 | # define BYTEORDER_IS_BIG_ENDIAN |
| 88 | #else |
| 89 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 90 | #endif |
| 91 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 92 | /* --- Globals ------------------------------------------------------------ |
| 93 | |
| 94 | The globals are initialized by the _PyUnicode_Init() API and should |
| 95 | not be used before calling that API. |
| 96 | |
| 97 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 98 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 99 | |
| 100 | #ifdef __cplusplus |
| 101 | extern "C" { |
| 102 | #endif |
| 103 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 104 | /* Free list for Unicode objects */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 105 | static PyUnicodeObject *unicode_freelist; |
| 106 | static int unicode_freelist_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 107 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 108 | /* The empty Unicode object is shared to improve performance. */ |
| 109 | static PyUnicodeObject *unicode_empty; |
| 110 | |
| 111 | /* Single character Unicode strings in the Latin-1 range are being |
| 112 | shared as well. */ |
| 113 | static PyUnicodeObject *unicode_latin1[256]; |
| 114 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 115 | /* Default encoding to use and assume when NULL is passed as encoding |
| 116 | parameter; it is initialized by _PyUnicode_Init(). |
| 117 | |
| 118 | Always use the PyUnicode_SetDefaultEncoding() and |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 119 | PyUnicode_GetDefaultEncoding() APIs to access this global. |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 120 | |
| 121 | */ |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 122 | static char unicode_default_encoding[100]; |
| 123 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 124 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 125 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 126 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 127 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 128 | return 0x10FFFF; |
| 129 | #else |
| 130 | /* This is actually an illegal character, so it should |
| 131 | not be passed to unichr. */ |
| 132 | return 0xFFFF; |
| 133 | #endif |
| 134 | } |
| 135 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 136 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 137 | |
| 138 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 139 | to keep things simple, we use a single bitmask, using the least 5 |
| 140 | bits from each unicode characters as the bit index. */ |
| 141 | |
| 142 | /* the linebreak mask is set up by Unicode_Init below */ |
| 143 | |
| 144 | #define BLOOM_MASK unsigned long |
| 145 | |
| 146 | static BLOOM_MASK bloom_linebreak; |
| 147 | |
| 148 | #define BLOOM(mask, ch) ((mask & (1 << ((ch) & 0x1F)))) |
| 149 | |
| 150 | #define BLOOM_LINEBREAK(ch)\ |
| 151 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK((ch))) |
| 152 | |
| 153 | LOCAL(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len) |
| 154 | { |
| 155 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 156 | |
| 157 | long mask; |
| 158 | Py_ssize_t i; |
| 159 | |
| 160 | mask = 0; |
| 161 | for (i = 0; i < len; i++) |
| 162 | mask |= (1 << (ptr[i] & 0x1F)); |
| 163 | |
| 164 | return mask; |
| 165 | } |
| 166 | |
| 167 | LOCAL(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen) |
| 168 | { |
| 169 | Py_ssize_t i; |
| 170 | |
| 171 | for (i = 0; i < setlen; i++) |
| 172 | if (set[i] == chr) |
| 173 | return 1; |
| 174 | |
Fredrik Lundh | 7763351 | 2006-05-23 19:47:35 +0000 | [diff] [blame] | 175 | return 0; |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | #define BLOOM_MEMBER(mask, chr, set, setlen)\ |
| 179 | BLOOM(mask, chr) && unicode_member(chr, set, setlen) |
| 180 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 181 | /* --- Unicode Object ----------------------------------------------------- */ |
| 182 | |
| 183 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 184 | int unicode_resize(register PyUnicodeObject *unicode, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 185 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 186 | { |
| 187 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 188 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 189 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 190 | if (unicode->length == length) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 191 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 192 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 193 | /* Resizing shared object (unicode_empty or single character |
| 194 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 195 | instead ! */ |
Martin v. Löwis | 80d2e59 | 2006-04-13 06:06:08 +0000 | [diff] [blame] | 196 | if (unicode == unicode_empty || |
| 197 | (unicode->length == 1 && |
| 198 | unicode->str[0] < 256U && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 199 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 200 | PyErr_SetString(PyExc_SystemError, |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 201 | "can't resize shared unicode objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 202 | return -1; |
| 203 | } |
| 204 | |
| 205 | /* We allocate one more byte to make sure the string is |
| 206 | Ux0000 terminated -- XXX is this needed ? */ |
| 207 | oldstr = unicode->str; |
| 208 | PyMem_RESIZE(unicode->str, Py_UNICODE, length + 1); |
| 209 | if (!unicode->str) { |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 210 | unicode->str = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 211 | PyErr_NoMemory(); |
| 212 | return -1; |
| 213 | } |
| 214 | unicode->str[length] = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 215 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 216 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 217 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 218 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 219 | if (unicode->defenc) { |
| 220 | Py_DECREF(unicode->defenc); |
| 221 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 222 | } |
| 223 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 224 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | /* We allocate one more byte to make sure the string is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 229 | Ux0000 terminated -- XXX is this needed ? |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 230 | |
| 231 | XXX This allocator could further be enhanced by assuring that the |
| 232 | free list never reduces its size below 1. |
| 233 | |
| 234 | */ |
| 235 | |
| 236 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 237 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 238 | { |
| 239 | register PyUnicodeObject *unicode; |
| 240 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 241 | /* Optimization fo empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 242 | if (length == 0 && unicode_empty != NULL) { |
| 243 | Py_INCREF(unicode_empty); |
| 244 | return unicode_empty; |
| 245 | } |
| 246 | |
| 247 | /* Unicode freelist & memory allocation */ |
| 248 | if (unicode_freelist) { |
| 249 | unicode = unicode_freelist; |
Marc-André Lemburg | bea47e7 | 2000-06-17 20:31:17 +0000 | [diff] [blame] | 250 | unicode_freelist = *(PyUnicodeObject **)unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 251 | unicode_freelist_size--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 252 | if (unicode->str) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 253 | /* Keep-Alive optimization: we only upsize the buffer, |
| 254 | never downsize it. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 255 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 256 | unicode_resize(unicode, length) < 0) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 257 | PyMem_DEL(unicode->str); |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 258 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 259 | } |
| 260 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 261 | else { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 262 | unicode->str = PyMem_NEW(Py_UNICODE, length + 1); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 263 | } |
| 264 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 265 | } |
| 266 | else { |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 267 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 268 | if (unicode == NULL) |
| 269 | return NULL; |
| 270 | unicode->str = PyMem_NEW(Py_UNICODE, length + 1); |
| 271 | } |
| 272 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 273 | if (!unicode->str) { |
| 274 | PyErr_NoMemory(); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 275 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 276 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 277 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 278 | * the caller fails before initializing str -- unicode_resize() |
| 279 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 280 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 281 | * We don't want unicode_resize to read uninitialized memory in |
| 282 | * that case. |
| 283 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 284 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 285 | unicode->str[length] = 0; |
Martin v. Löwis | f15da69 | 2006-04-13 07:24:50 +0000 | [diff] [blame] | 286 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 287 | unicode->hash = -1; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 288 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 289 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 290 | |
| 291 | onError: |
| 292 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 293 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 294 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 298 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 299 | { |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 300 | if (PyUnicode_CheckExact(unicode) && |
| 301 | unicode_freelist_size < MAX_UNICODE_FREELIST_SIZE) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 302 | /* Keep-Alive optimization */ |
| 303 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 304 | PyMem_DEL(unicode->str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 305 | unicode->str = NULL; |
| 306 | unicode->length = 0; |
| 307 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 308 | if (unicode->defenc) { |
| 309 | Py_DECREF(unicode->defenc); |
| 310 | unicode->defenc = NULL; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 311 | } |
| 312 | /* Add to free list */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 313 | *(PyUnicodeObject **)unicode = unicode_freelist; |
| 314 | unicode_freelist = unicode; |
| 315 | unicode_freelist_size++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 316 | } |
| 317 | else { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 318 | PyMem_DEL(unicode->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 319 | Py_XDECREF(unicode->defenc); |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 320 | unicode->ob_type->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 324 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 325 | { |
| 326 | register PyUnicodeObject *v; |
| 327 | |
| 328 | /* Argument checks */ |
| 329 | if (unicode == NULL) { |
| 330 | PyErr_BadInternalCall(); |
| 331 | return -1; |
| 332 | } |
| 333 | v = (PyUnicodeObject *)*unicode; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 334 | if (v == NULL || !PyUnicode_Check(v) || v->ob_refcnt != 1 || length < 0) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 335 | PyErr_BadInternalCall(); |
| 336 | return -1; |
| 337 | } |
| 338 | |
| 339 | /* Resizing unicode_empty and single character objects is not |
| 340 | possible since these are being shared. We simply return a fresh |
| 341 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 342 | if (v->length != length && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 343 | (v == unicode_empty || v->length == 1)) { |
| 344 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 345 | if (w == NULL) |
| 346 | return -1; |
| 347 | Py_UNICODE_COPY(w->str, v->str, |
| 348 | length < v->length ? length : v->length); |
Raymond Hettinger | c8df578 | 2003-03-09 07:30:43 +0000 | [diff] [blame] | 349 | Py_DECREF(*unicode); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 350 | *unicode = (PyObject *)w; |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 355 | objects, since we can modify them in-place. */ |
| 356 | return unicode_resize(v, length); |
| 357 | } |
| 358 | |
| 359 | /* Internal API for use in unicodeobject.c only ! */ |
| 360 | #define _PyUnicode_Resize(unicodevar, length) \ |
| 361 | PyUnicode_Resize(((PyObject **)(unicodevar)), length) |
| 362 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 363 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 364 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 365 | { |
| 366 | PyUnicodeObject *unicode; |
| 367 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 368 | /* If the Unicode data is known at construction time, we can apply |
| 369 | some optimizations which share commonly used objects. */ |
| 370 | if (u != NULL) { |
| 371 | |
| 372 | /* Optimization for empty strings */ |
| 373 | if (size == 0 && unicode_empty != NULL) { |
| 374 | Py_INCREF(unicode_empty); |
| 375 | return (PyObject *)unicode_empty; |
| 376 | } |
| 377 | |
| 378 | /* Single character Unicode objects in the Latin-1 range are |
| 379 | shared when using this constructor */ |
| 380 | if (size == 1 && *u < 256) { |
| 381 | unicode = unicode_latin1[*u]; |
| 382 | if (!unicode) { |
| 383 | unicode = _PyUnicode_New(1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 384 | if (!unicode) |
| 385 | return NULL; |
Marc-André Lemburg | 8879a33 | 2001-06-07 12:26:56 +0000 | [diff] [blame] | 386 | unicode->str[0] = *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 387 | unicode_latin1[*u] = unicode; |
| 388 | } |
| 389 | Py_INCREF(unicode); |
| 390 | return (PyObject *)unicode; |
| 391 | } |
| 392 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 393 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 394 | unicode = _PyUnicode_New(size); |
| 395 | if (!unicode) |
| 396 | return NULL; |
| 397 | |
| 398 | /* Copy the Unicode data into the new object */ |
| 399 | if (u != NULL) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 400 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 401 | |
| 402 | return (PyObject *)unicode; |
| 403 | } |
| 404 | |
| 405 | #ifdef HAVE_WCHAR_H |
| 406 | |
| 407 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 408 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 409 | { |
| 410 | PyUnicodeObject *unicode; |
| 411 | |
| 412 | if (w == NULL) { |
| 413 | PyErr_BadInternalCall(); |
| 414 | return NULL; |
| 415 | } |
| 416 | |
| 417 | unicode = _PyUnicode_New(size); |
| 418 | if (!unicode) |
| 419 | return NULL; |
| 420 | |
| 421 | /* Copy the wchar_t data into the new object */ |
| 422 | #ifdef HAVE_USABLE_WCHAR_T |
| 423 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 424 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 425 | { |
| 426 | register Py_UNICODE *u; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 427 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 428 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 429 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 430 | *u++ = *w++; |
| 431 | } |
| 432 | #endif |
| 433 | |
| 434 | return (PyObject *)unicode; |
| 435 | } |
| 436 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 437 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 438 | wchar_t *w, |
| 439 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 440 | { |
| 441 | if (unicode == NULL) { |
| 442 | PyErr_BadInternalCall(); |
| 443 | return -1; |
| 444 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 445 | |
| 446 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 447 | if (size > PyUnicode_GET_SIZE(unicode)) |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 448 | size = PyUnicode_GET_SIZE(unicode) + 1; |
| 449 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 450 | #ifdef HAVE_USABLE_WCHAR_T |
| 451 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 452 | #else |
| 453 | { |
| 454 | register Py_UNICODE *u; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 455 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 456 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 457 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 458 | *w++ = *u++; |
| 459 | } |
| 460 | #endif |
| 461 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 462 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 463 | return PyUnicode_GET_SIZE(unicode); |
| 464 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 465 | return size; |
| 466 | } |
| 467 | |
| 468 | #endif |
| 469 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 470 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 471 | { |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 472 | Py_UNICODE s[1]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 473 | |
| 474 | #ifdef Py_UNICODE_WIDE |
| 475 | if (ordinal < 0 || ordinal > 0x10ffff) { |
| 476 | PyErr_SetString(PyExc_ValueError, |
| 477 | "unichr() arg not in range(0x110000) " |
| 478 | "(wide Python build)"); |
| 479 | return NULL; |
| 480 | } |
| 481 | #else |
| 482 | if (ordinal < 0 || ordinal > 0xffff) { |
| 483 | PyErr_SetString(PyExc_ValueError, |
| 484 | "unichr() arg not in range(0x10000) " |
| 485 | "(narrow Python build)"); |
| 486 | return NULL; |
| 487 | } |
| 488 | #endif |
| 489 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 490 | s[0] = (Py_UNICODE)ordinal; |
| 491 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 494 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 495 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 496 | /* XXX Perhaps we should make this API an alias of |
| 497 | PyObject_Unicode() instead ?! */ |
| 498 | if (PyUnicode_CheckExact(obj)) { |
| 499 | Py_INCREF(obj); |
| 500 | return obj; |
| 501 | } |
| 502 | if (PyUnicode_Check(obj)) { |
| 503 | /* For a Unicode subtype that's not a Unicode object, |
| 504 | return a true Unicode object with the same data. */ |
| 505 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 506 | PyUnicode_GET_SIZE(obj)); |
| 507 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 508 | return PyUnicode_FromEncodedObject(obj, NULL, "strict"); |
| 509 | } |
| 510 | |
| 511 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
| 512 | const char *encoding, |
| 513 | const char *errors) |
| 514 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 515 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 516 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 517 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 518 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 519 | if (obj == NULL) { |
| 520 | PyErr_BadInternalCall(); |
| 521 | return NULL; |
| 522 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 523 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 524 | #if 0 |
| 525 | /* For b/w compatibility we also accept Unicode objects provided |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 526 | that no encodings is given and then redirect to |
| 527 | PyObject_Unicode() which then applies the additional logic for |
| 528 | Unicode subclasses. |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 529 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 530 | NOTE: This API should really only be used for object which |
| 531 | represent *encoded* Unicode ! |
| 532 | |
| 533 | */ |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 534 | if (PyUnicode_Check(obj)) { |
| 535 | if (encoding) { |
| 536 | PyErr_SetString(PyExc_TypeError, |
| 537 | "decoding Unicode is not supported"); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 538 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 539 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 540 | return PyObject_Unicode(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 541 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 542 | #else |
| 543 | if (PyUnicode_Check(obj)) { |
| 544 | PyErr_SetString(PyExc_TypeError, |
| 545 | "decoding Unicode is not supported"); |
| 546 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 547 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 548 | #endif |
| 549 | |
| 550 | /* Coerce object */ |
| 551 | if (PyString_Check(obj)) { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 552 | s = PyString_AS_STRING(obj); |
| 553 | len = PyString_GET_SIZE(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 554 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 555 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
| 556 | /* Overwrite the error message with something more useful in |
| 557 | case of a TypeError. */ |
| 558 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 559 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 560 | "coercing to Unicode: need string or buffer, " |
| 561 | "%.80s found", |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 562 | obj->ob_type->tp_name); |
| 563 | goto onError; |
| 564 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 565 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 566 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 567 | if (len == 0) { |
| 568 | Py_INCREF(unicode_empty); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 569 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 570 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 571 | else |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 572 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 573 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 574 | return v; |
| 575 | |
| 576 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 577 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | PyObject *PyUnicode_Decode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 581 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 582 | const char *encoding, |
| 583 | const char *errors) |
| 584 | { |
| 585 | PyObject *buffer = NULL, *unicode; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 586 | |
| 587 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 588 | encoding = PyUnicode_GetDefaultEncoding(); |
| 589 | |
| 590 | /* Shortcuts for common default encodings */ |
| 591 | if (strcmp(encoding, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 592 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 593 | else if (strcmp(encoding, "latin-1") == 0) |
| 594 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 595 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 596 | else if (strcmp(encoding, "mbcs") == 0) |
| 597 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 598 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 599 | else if (strcmp(encoding, "ascii") == 0) |
| 600 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 601 | |
| 602 | /* Decode via the codec registry */ |
| 603 | buffer = PyBuffer_FromMemory((void *)s, size); |
| 604 | if (buffer == NULL) |
| 605 | goto onError; |
| 606 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 607 | if (unicode == NULL) |
| 608 | goto onError; |
| 609 | if (!PyUnicode_Check(unicode)) { |
| 610 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 611 | "decoder did not return an unicode object (type=%.400s)", |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 612 | unicode->ob_type->tp_name); |
| 613 | Py_DECREF(unicode); |
| 614 | goto onError; |
| 615 | } |
| 616 | Py_DECREF(buffer); |
| 617 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 618 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 619 | onError: |
| 620 | Py_XDECREF(buffer); |
| 621 | return NULL; |
| 622 | } |
| 623 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 624 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 625 | const char *encoding, |
| 626 | const char *errors) |
| 627 | { |
| 628 | PyObject *v; |
| 629 | |
| 630 | if (!PyUnicode_Check(unicode)) { |
| 631 | PyErr_BadArgument(); |
| 632 | goto onError; |
| 633 | } |
| 634 | |
| 635 | if (encoding == NULL) |
| 636 | encoding = PyUnicode_GetDefaultEncoding(); |
| 637 | |
| 638 | /* Decode via the codec registry */ |
| 639 | v = PyCodec_Decode(unicode, encoding, errors); |
| 640 | if (v == NULL) |
| 641 | goto onError; |
| 642 | return v; |
| 643 | |
| 644 | onError: |
| 645 | return NULL; |
| 646 | } |
| 647 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 648 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 649 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 650 | const char *encoding, |
| 651 | const char *errors) |
| 652 | { |
| 653 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 654 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 655 | unicode = PyUnicode_FromUnicode(s, size); |
| 656 | if (unicode == NULL) |
| 657 | return NULL; |
| 658 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 659 | Py_DECREF(unicode); |
| 660 | return v; |
| 661 | } |
| 662 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 663 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 664 | const char *encoding, |
| 665 | const char *errors) |
| 666 | { |
| 667 | PyObject *v; |
| 668 | |
| 669 | if (!PyUnicode_Check(unicode)) { |
| 670 | PyErr_BadArgument(); |
| 671 | goto onError; |
| 672 | } |
| 673 | |
| 674 | if (encoding == NULL) |
| 675 | encoding = PyUnicode_GetDefaultEncoding(); |
| 676 | |
| 677 | /* Encode via the codec registry */ |
| 678 | v = PyCodec_Encode(unicode, encoding, errors); |
| 679 | if (v == NULL) |
| 680 | goto onError; |
| 681 | return v; |
| 682 | |
| 683 | onError: |
| 684 | return NULL; |
| 685 | } |
| 686 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 687 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 688 | const char *encoding, |
| 689 | const char *errors) |
| 690 | { |
| 691 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 692 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 693 | if (!PyUnicode_Check(unicode)) { |
| 694 | PyErr_BadArgument(); |
| 695 | goto onError; |
| 696 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 697 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 698 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 699 | encoding = PyUnicode_GetDefaultEncoding(); |
| 700 | |
| 701 | /* Shortcuts for common default encodings */ |
| 702 | if (errors == NULL) { |
| 703 | if (strcmp(encoding, "utf-8") == 0) |
Jeremy Hylton | 9cea41c | 2001-05-29 17:13:15 +0000 | [diff] [blame] | 704 | return PyUnicode_AsUTF8String(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 705 | else if (strcmp(encoding, "latin-1") == 0) |
| 706 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 707 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 708 | else if (strcmp(encoding, "mbcs") == 0) |
| 709 | return PyUnicode_AsMBCSString(unicode); |
| 710 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 711 | else if (strcmp(encoding, "ascii") == 0) |
| 712 | return PyUnicode_AsASCIIString(unicode); |
| 713 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 714 | |
| 715 | /* Encode via the codec registry */ |
| 716 | v = PyCodec_Encode(unicode, encoding, errors); |
| 717 | if (v == NULL) |
| 718 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 719 | if (!PyString_Check(v)) { |
| 720 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 721 | "encoder did not return a string object (type=%.400s)", |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 722 | v->ob_type->tp_name); |
| 723 | Py_DECREF(v); |
| 724 | goto onError; |
| 725 | } |
| 726 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 727 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 728 | onError: |
| 729 | return NULL; |
| 730 | } |
| 731 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 732 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 733 | const char *errors) |
| 734 | { |
| 735 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
| 736 | |
| 737 | if (v) |
| 738 | return v; |
| 739 | v = PyUnicode_AsEncodedString(unicode, NULL, errors); |
| 740 | if (v && errors == NULL) |
| 741 | ((PyUnicodeObject *)unicode)->defenc = v; |
| 742 | return v; |
| 743 | } |
| 744 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 745 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 746 | { |
| 747 | if (!PyUnicode_Check(unicode)) { |
| 748 | PyErr_BadArgument(); |
| 749 | goto onError; |
| 750 | } |
| 751 | return PyUnicode_AS_UNICODE(unicode); |
| 752 | |
| 753 | onError: |
| 754 | return NULL; |
| 755 | } |
| 756 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 757 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 758 | { |
| 759 | if (!PyUnicode_Check(unicode)) { |
| 760 | PyErr_BadArgument(); |
| 761 | goto onError; |
| 762 | } |
| 763 | return PyUnicode_GET_SIZE(unicode); |
| 764 | |
| 765 | onError: |
| 766 | return -1; |
| 767 | } |
| 768 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 769 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 770 | { |
| 771 | return unicode_default_encoding; |
| 772 | } |
| 773 | |
| 774 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 775 | { |
| 776 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 777 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 778 | /* Make sure the encoding is valid. As side effect, this also |
| 779 | loads the encoding into the codec registry cache. */ |
| 780 | v = _PyCodec_Lookup(encoding); |
| 781 | if (v == NULL) |
| 782 | goto onError; |
| 783 | Py_DECREF(v); |
| 784 | strncpy(unicode_default_encoding, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 785 | encoding, |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 786 | sizeof(unicode_default_encoding)); |
| 787 | return 0; |
| 788 | |
| 789 | onError: |
| 790 | return -1; |
| 791 | } |
| 792 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 793 | /* error handling callback helper: |
| 794 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 795 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 796 | and adjust various state variables. |
| 797 | return 0 on success, -1 on error |
| 798 | */ |
| 799 | |
| 800 | static |
| 801 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 802 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 803 | const char *input, Py_ssize_t insize, Py_ssize_t *startinpos, Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 804 | PyObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 805 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 806 | 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] | 807 | |
| 808 | PyObject *restuple = NULL; |
| 809 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 810 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
| 811 | Py_ssize_t requiredsize; |
| 812 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 813 | Py_UNICODE *repptr; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 814 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 815 | int res = -1; |
| 816 | |
| 817 | if (*errorHandler == NULL) { |
| 818 | *errorHandler = PyCodec_LookupError(errors); |
| 819 | if (*errorHandler == NULL) |
| 820 | goto onError; |
| 821 | } |
| 822 | |
| 823 | if (*exceptionObject == NULL) { |
| 824 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 825 | encoding, input, insize, *startinpos, *endinpos, reason); |
| 826 | if (*exceptionObject == NULL) |
| 827 | goto onError; |
| 828 | } |
| 829 | else { |
| 830 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 831 | goto onError; |
| 832 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 833 | goto onError; |
| 834 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 835 | goto onError; |
| 836 | } |
| 837 | |
| 838 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 839 | if (restuple == NULL) |
| 840 | goto onError; |
| 841 | if (!PyTuple_Check(restuple)) { |
| 842 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 843 | goto onError; |
| 844 | } |
| 845 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 846 | goto onError; |
| 847 | if (newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 848 | newpos = insize+newpos; |
| 849 | if (newpos<0 || newpos>insize) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 850 | 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] | 851 | goto onError; |
| 852 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 853 | |
| 854 | /* need more space? (at least enough for what we |
| 855 | have+the replacement+the rest of the string (starting |
| 856 | at the new input position), so we won't have to check space |
| 857 | when there are no errors in the rest of the string) */ |
| 858 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 859 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 860 | requiredsize = *outpos + repsize + insize-newpos; |
| 861 | if (requiredsize > outsize) { |
| 862 | if (requiredsize<2*outsize) |
| 863 | requiredsize = 2*outsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 864 | if (PyUnicode_Resize(output, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 865 | goto onError; |
| 866 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
| 867 | } |
| 868 | *endinpos = newpos; |
| 869 | *inptr = input + newpos; |
| 870 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 871 | *outptr += repsize; |
| 872 | *outpos += repsize; |
| 873 | /* we made it! */ |
| 874 | res = 0; |
| 875 | |
| 876 | onError: |
| 877 | Py_XDECREF(restuple); |
| 878 | return res; |
| 879 | } |
| 880 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 881 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 882 | |
| 883 | /* see RFC2152 for details */ |
| 884 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 885 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 886 | char utf7_special[128] = { |
| 887 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 888 | encoded: |
| 889 | 0 - not special |
| 890 | 1 - special |
| 891 | 2 - whitespace (optional) |
| 892 | 3 - RFC2152 Set O (optional) */ |
| 893 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 894 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 895 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 896 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 897 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 898 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 899 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 900 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 901 | |
| 902 | }; |
| 903 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 904 | /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
| 905 | warnings about the comparison always being false; since |
| 906 | utf7_special[0] is 1, we can safely make that one comparison |
| 907 | true */ |
| 908 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 909 | #define SPECIAL(c, encodeO, encodeWS) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 910 | ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 911 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 912 | (encodeO && (utf7_special[(c)] == 3))) |
| 913 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 914 | #define B64(n) \ |
| 915 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 916 | #define B64CHAR(c) \ |
| 917 | (isalnum(c) || (c) == '+' || (c) == '/') |
| 918 | #define UB64(c) \ |
| 919 | ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
| 920 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 921 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 922 | #define ENCODE(out, ch, bits) \ |
| 923 | while (bits >= 6) { \ |
| 924 | *out++ = B64(ch >> (bits-6)); \ |
| 925 | bits -= 6; \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 926 | } |
| 927 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 928 | #define DECODE(out, ch, bits, surrogate) \ |
| 929 | while (bits >= 16) { \ |
| 930 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 931 | bits -= 16; \ |
| 932 | if (surrogate) { \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 933 | /* We have already generated an error for the high surrogate \ |
| 934 | 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] | 935 | surrogate = 0; \ |
| 936 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 937 | /* This is a surrogate pair. Unfortunately we can't represent \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 938 | it in a 16-bit character */ \ |
| 939 | surrogate = 1; \ |
| 940 | errmsg = "code pairs are not supported"; \ |
| 941 | goto utf7Error; \ |
| 942 | } else { \ |
| 943 | *out++ = outCh; \ |
| 944 | } \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 945 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 946 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 947 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 948 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 949 | const char *errors) |
| 950 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 951 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 952 | Py_ssize_t startinpos; |
| 953 | Py_ssize_t endinpos; |
| 954 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 955 | const char *e; |
| 956 | PyUnicodeObject *unicode; |
| 957 | Py_UNICODE *p; |
| 958 | const char *errmsg = ""; |
| 959 | int inShift = 0; |
| 960 | unsigned int bitsleft = 0; |
| 961 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 962 | int surrogate = 0; |
| 963 | PyObject *errorHandler = NULL; |
| 964 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 965 | |
| 966 | unicode = _PyUnicode_New(size); |
| 967 | if (!unicode) |
| 968 | return NULL; |
| 969 | if (size == 0) |
| 970 | return (PyObject *)unicode; |
| 971 | |
| 972 | p = unicode->str; |
| 973 | e = s + size; |
| 974 | |
| 975 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 976 | Py_UNICODE ch; |
| 977 | restart: |
| 978 | ch = *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 979 | |
| 980 | if (inShift) { |
| 981 | if ((ch == '-') || !B64CHAR(ch)) { |
| 982 | inShift = 0; |
| 983 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 984 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 985 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 986 | if (bitsleft >= 6) { |
| 987 | /* The shift sequence has a partial character in it. If |
| 988 | bitsleft < 6 then we could just classify it as padding |
| 989 | but that is not the case here */ |
| 990 | |
| 991 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 992 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 993 | } |
| 994 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 995 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 996 | here so indicate the potential of a misencoded character. */ |
| 997 | |
| 998 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 999 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 1000 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1001 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | if (ch == '-') { |
| 1005 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1006 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1007 | inShift = 1; |
| 1008 | } |
| 1009 | } else if (SPECIAL(ch,0,0)) { |
| 1010 | errmsg = "unexpected special character"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1011 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1012 | } else { |
| 1013 | *p++ = ch; |
| 1014 | } |
| 1015 | } else { |
| 1016 | charsleft = (charsleft << 6) | UB64(ch); |
| 1017 | bitsleft += 6; |
| 1018 | s++; |
| 1019 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1020 | } |
| 1021 | } |
| 1022 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1023 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1024 | s++; |
| 1025 | if (s < e && *s == '-') { |
| 1026 | s++; |
| 1027 | *p++ = '+'; |
| 1028 | } else |
| 1029 | { |
| 1030 | inShift = 1; |
| 1031 | bitsleft = 0; |
| 1032 | } |
| 1033 | } |
| 1034 | else if (SPECIAL(ch,0,0)) { |
| 1035 | errmsg = "unexpected special character"; |
| 1036 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1037 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1038 | } |
| 1039 | else { |
| 1040 | *p++ = ch; |
| 1041 | s++; |
| 1042 | } |
| 1043 | continue; |
| 1044 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1045 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1046 | endinpos = s-starts; |
| 1047 | if (unicode_decode_call_errorhandler( |
| 1048 | errors, &errorHandler, |
| 1049 | "utf7", errmsg, |
| 1050 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1051 | (PyObject **)&unicode, &outpos, &p)) |
| 1052 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | if (inShift) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1056 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1057 | endinpos = size; |
| 1058 | if (unicode_decode_call_errorhandler( |
| 1059 | errors, &errorHandler, |
| 1060 | "utf7", "unterminated shift sequence", |
| 1061 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1062 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1063 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1064 | if (s < e) |
| 1065 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1068 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1069 | goto onError; |
| 1070 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1071 | Py_XDECREF(errorHandler); |
| 1072 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1073 | return (PyObject *)unicode; |
| 1074 | |
| 1075 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1076 | Py_XDECREF(errorHandler); |
| 1077 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1078 | Py_DECREF(unicode); |
| 1079 | return NULL; |
| 1080 | } |
| 1081 | |
| 1082 | |
| 1083 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1084 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1085 | int encodeSetO, |
| 1086 | int encodeWhiteSpace, |
| 1087 | const char *errors) |
| 1088 | { |
| 1089 | PyObject *v; |
| 1090 | /* It might be possible to tighten this worst case */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1091 | Py_ssize_t cbAllocated = 5 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1092 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1093 | Py_ssize_t i = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1094 | unsigned int bitsleft = 0; |
| 1095 | unsigned long charsleft = 0; |
| 1096 | char * out; |
| 1097 | char * start; |
| 1098 | |
| 1099 | if (size == 0) |
| 1100 | return PyString_FromStringAndSize(NULL, 0); |
| 1101 | |
| 1102 | v = PyString_FromStringAndSize(NULL, cbAllocated); |
| 1103 | if (v == NULL) |
| 1104 | return NULL; |
| 1105 | |
| 1106 | start = out = PyString_AS_STRING(v); |
| 1107 | for (;i < size; ++i) { |
| 1108 | Py_UNICODE ch = s[i]; |
| 1109 | |
| 1110 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1111 | if (ch == '+') { |
| 1112 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1113 | *out++ = '-'; |
| 1114 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1115 | charsleft = ch; |
| 1116 | bitsleft = 16; |
| 1117 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1118 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1119 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1120 | } else { |
| 1121 | *out++ = (char) ch; |
| 1122 | } |
| 1123 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1124 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1125 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1126 | charsleft = 0; |
| 1127 | bitsleft = 0; |
| 1128 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1129 | so no '-' is required, except if the character is itself a '-' */ |
| 1130 | if (B64CHAR(ch) || ch == '-') { |
| 1131 | *out++ = '-'; |
| 1132 | } |
| 1133 | inShift = 0; |
| 1134 | *out++ = (char) ch; |
| 1135 | } else { |
| 1136 | bitsleft += 16; |
| 1137 | charsleft = (charsleft << 16) | ch; |
| 1138 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1139 | |
| 1140 | /* If the next character is special then we dont' need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1141 | 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] | 1142 | or '-' then the shift sequence will be terminated implicitly and we |
| 1143 | don't have to insert a '-'. */ |
| 1144 | |
| 1145 | if (bitsleft == 0) { |
| 1146 | if (i + 1 < size) { |
| 1147 | Py_UNICODE ch2 = s[i+1]; |
| 1148 | |
| 1149 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1150 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1151 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1152 | *out++ = '-'; |
| 1153 | inShift = 0; |
| 1154 | } else { |
| 1155 | inShift = 0; |
| 1156 | } |
| 1157 | |
| 1158 | } |
| 1159 | else { |
| 1160 | *out++ = '-'; |
| 1161 | inShift = 0; |
| 1162 | } |
| 1163 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1164 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1165 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1166 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1167 | if (bitsleft) { |
| 1168 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1169 | *out++ = '-'; |
| 1170 | } |
| 1171 | |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 1172 | _PyString_Resize(&v, out - start); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1173 | return v; |
| 1174 | } |
| 1175 | |
| 1176 | #undef SPECIAL |
| 1177 | #undef B64 |
| 1178 | #undef B64CHAR |
| 1179 | #undef UB64 |
| 1180 | #undef ENCODE |
| 1181 | #undef DECODE |
| 1182 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1183 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1184 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1185 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1186 | char utf8_code_length[256] = { |
| 1187 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1188 | illegal prefix. see RFC 2279 for details */ |
| 1189 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1190 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1191 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1192 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1193 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1194 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1195 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1196 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1197 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1198 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1199 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1200 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1201 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1202 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1203 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1204 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1205 | }; |
| 1206 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1207 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1208 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1209 | const char *errors) |
| 1210 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1211 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 1212 | } |
| 1213 | |
| 1214 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1215 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1216 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1217 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1218 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1219 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1220 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1221 | Py_ssize_t startinpos; |
| 1222 | Py_ssize_t endinpos; |
| 1223 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1224 | const char *e; |
| 1225 | PyUnicodeObject *unicode; |
| 1226 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1227 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1228 | PyObject *errorHandler = NULL; |
| 1229 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1230 | |
| 1231 | /* Note: size will always be longer than the resulting Unicode |
| 1232 | character count */ |
| 1233 | unicode = _PyUnicode_New(size); |
| 1234 | if (!unicode) |
| 1235 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1236 | if (size == 0) { |
| 1237 | if (consumed) |
| 1238 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1239 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1240 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1241 | |
| 1242 | /* Unpack UTF-8 encoded data */ |
| 1243 | p = unicode->str; |
| 1244 | e = s + size; |
| 1245 | |
| 1246 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1247 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1248 | |
| 1249 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1250 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1251 | s++; |
| 1252 | continue; |
| 1253 | } |
| 1254 | |
| 1255 | n = utf8_code_length[ch]; |
| 1256 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1257 | if (s + n > e) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1258 | if (consumed) |
| 1259 | break; |
| 1260 | else { |
| 1261 | errmsg = "unexpected end of data"; |
| 1262 | startinpos = s-starts; |
| 1263 | endinpos = size; |
| 1264 | goto utf8Error; |
| 1265 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1266 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1267 | |
| 1268 | switch (n) { |
| 1269 | |
| 1270 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1271 | errmsg = "unexpected code byte"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1272 | startinpos = s-starts; |
| 1273 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1274 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1275 | |
| 1276 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1277 | errmsg = "internal error"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1278 | startinpos = s-starts; |
| 1279 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1280 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1281 | |
| 1282 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1283 | if ((s[1] & 0xc0) != 0x80) { |
| 1284 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1285 | startinpos = s-starts; |
| 1286 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1287 | goto utf8Error; |
| 1288 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1289 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1290 | if (ch < 0x80) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1291 | startinpos = s-starts; |
| 1292 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1293 | errmsg = "illegal encoding"; |
| 1294 | goto utf8Error; |
| 1295 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1296 | else |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1297 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1298 | break; |
| 1299 | |
| 1300 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1301 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1302 | (s[2] & 0xc0) != 0x80) { |
| 1303 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1304 | startinpos = s-starts; |
| 1305 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1306 | goto utf8Error; |
| 1307 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1308 | 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] | 1309 | if (ch < 0x0800) { |
| 1310 | /* Note: UTF-8 encodings of surrogates are considered |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1311 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1312 | |
| 1313 | XXX For wide builds (UCS-4) we should probably try |
| 1314 | to recombine the surrogates into a single code |
| 1315 | unit. |
| 1316 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1317 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1318 | startinpos = s-starts; |
| 1319 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1320 | goto utf8Error; |
| 1321 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1322 | else |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1323 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1324 | break; |
| 1325 | |
| 1326 | case 4: |
| 1327 | if ((s[1] & 0xc0) != 0x80 || |
| 1328 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1329 | (s[3] & 0xc0) != 0x80) { |
| 1330 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1331 | startinpos = s-starts; |
| 1332 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1333 | goto utf8Error; |
| 1334 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1335 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 1336 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 1337 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1338 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1339 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1340 | || (ch > 0x10ffff)) /* maximum value allowed for |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1341 | UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1342 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1343 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1344 | startinpos = s-starts; |
| 1345 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1346 | goto utf8Error; |
| 1347 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1348 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1349 | *p++ = (Py_UNICODE)ch; |
| 1350 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1351 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1352 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1353 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 1354 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1355 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1356 | /* high surrogate = top 10 bits added to D800 */ |
| 1357 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1358 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1359 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1360 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1361 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1362 | break; |
| 1363 | |
| 1364 | default: |
| 1365 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1366 | errmsg = "unsupported Unicode code range"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1367 | startinpos = s-starts; |
| 1368 | endinpos = startinpos+n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1369 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1370 | } |
| 1371 | s += n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1372 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1373 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1374 | utf8Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1375 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1376 | if (unicode_decode_call_errorhandler( |
| 1377 | errors, &errorHandler, |
| 1378 | "utf8", errmsg, |
| 1379 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1380 | (PyObject **)&unicode, &outpos, &p)) |
| 1381 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1382 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1383 | if (consumed) |
| 1384 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1385 | |
| 1386 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1387 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1388 | goto onError; |
| 1389 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1390 | Py_XDECREF(errorHandler); |
| 1391 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1392 | return (PyObject *)unicode; |
| 1393 | |
| 1394 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1395 | Py_XDECREF(errorHandler); |
| 1396 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1397 | Py_DECREF(unicode); |
| 1398 | return NULL; |
| 1399 | } |
| 1400 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1401 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 1402 | and allocate exactly as much space needed at the end. Else allocate the |
| 1403 | maximum possible needed (4 result bytes per Unicode character), and return |
| 1404 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1405 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1406 | PyObject * |
| 1407 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1408 | Py_ssize_t size, |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1409 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1410 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1411 | #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] | 1412 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1413 | Py_ssize_t i; /* index into s of next input byte */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1414 | PyObject *v; /* result string object */ |
| 1415 | char *p; /* next free byte in output buffer */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1416 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1417 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1418 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1419 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1420 | assert(s != NULL); |
| 1421 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1422 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1423 | if (size <= MAX_SHORT_UNICHARS) { |
| 1424 | /* Write into the stack buffer; nallocated can't overflow. |
| 1425 | * At the end, we'll allocate exactly as much heap space as it |
| 1426 | * turns out we need. |
| 1427 | */ |
| 1428 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
| 1429 | v = NULL; /* will allocate after we're done */ |
| 1430 | p = stackbuf; |
| 1431 | } |
| 1432 | else { |
| 1433 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 1434 | nallocated = size * 4; |
| 1435 | if (nallocated / 4 != size) /* overflow! */ |
| 1436 | return PyErr_NoMemory(); |
| 1437 | v = PyString_FromStringAndSize(NULL, nallocated); |
| 1438 | if (v == NULL) |
| 1439 | return NULL; |
| 1440 | p = PyString_AS_STRING(v); |
| 1441 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1442 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1443 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1444 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1445 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1446 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1447 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1448 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1449 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1450 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1451 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 1452 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 1453 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1454 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1455 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1456 | /* Encode UCS2 Unicode ordinals */ |
| 1457 | if (ch < 0x10000) { |
| 1458 | /* Special case: check for high surrogate */ |
| 1459 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 1460 | Py_UCS4 ch2 = s[i]; |
| 1461 | /* Check for low surrogate and combine the two to |
| 1462 | form a UCS4 value */ |
| 1463 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1464 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1465 | i++; |
| 1466 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1467 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1468 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1469 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1470 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1471 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1472 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1473 | continue; |
| 1474 | } |
| 1475 | encodeUCS4: |
| 1476 | /* Encode UCS4 Unicode ordinals */ |
| 1477 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 1478 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 1479 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1480 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1481 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1482 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 1483 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1484 | if (v == NULL) { |
| 1485 | /* This was stack allocated. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1486 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1487 | assert(nneeded <= nallocated); |
| 1488 | v = PyString_FromStringAndSize(stackbuf, nneeded); |
| 1489 | } |
| 1490 | else { |
| 1491 | /* Cut back to size actually needed. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1492 | nneeded = p - PyString_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1493 | assert(nneeded <= nallocated); |
| 1494 | _PyString_Resize(&v, nneeded); |
| 1495 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1496 | return v; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1497 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1498 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1501 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 1502 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1503 | if (!PyUnicode_Check(unicode)) { |
| 1504 | PyErr_BadArgument(); |
| 1505 | return NULL; |
| 1506 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 1507 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1508 | PyUnicode_GET_SIZE(unicode), |
| 1509 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 1513 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1514 | PyObject * |
| 1515 | PyUnicode_DecodeUTF16(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1516 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1517 | const char *errors, |
| 1518 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1519 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1520 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 1521 | } |
| 1522 | |
| 1523 | PyObject * |
| 1524 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1525 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1526 | const char *errors, |
| 1527 | int *byteorder, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1528 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1529 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1530 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1531 | Py_ssize_t startinpos; |
| 1532 | Py_ssize_t endinpos; |
| 1533 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1534 | PyUnicodeObject *unicode; |
| 1535 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1536 | const unsigned char *q, *e; |
| 1537 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1538 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1539 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 1540 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1541 | int ihi = 1, ilo = 0; |
| 1542 | #else |
| 1543 | int ihi = 0, ilo = 1; |
| 1544 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1545 | PyObject *errorHandler = NULL; |
| 1546 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1547 | |
| 1548 | /* Note: size will always be longer than the resulting Unicode |
| 1549 | character count */ |
| 1550 | unicode = _PyUnicode_New(size); |
| 1551 | if (!unicode) |
| 1552 | return NULL; |
| 1553 | if (size == 0) |
| 1554 | return (PyObject *)unicode; |
| 1555 | |
| 1556 | /* Unpack UTF-16 encoded data */ |
| 1557 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1558 | q = (unsigned char *)s; |
| 1559 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1560 | |
| 1561 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1562 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1563 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1564 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 1565 | byte order setting accordingly. In native mode, the leading BOM |
| 1566 | mark is skipped, in all other modes, it is copied to the output |
| 1567 | stream as-is (giving a ZWNBSP character). */ |
| 1568 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1569 | if (size >= 2) { |
| 1570 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1571 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1572 | if (bom == 0xFEFF) { |
| 1573 | q += 2; |
| 1574 | bo = -1; |
| 1575 | } |
| 1576 | else if (bom == 0xFFFE) { |
| 1577 | q += 2; |
| 1578 | bo = 1; |
| 1579 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1580 | #else |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1581 | if (bom == 0xFEFF) { |
| 1582 | q += 2; |
| 1583 | bo = 1; |
| 1584 | } |
| 1585 | else if (bom == 0xFFFE) { |
| 1586 | q += 2; |
| 1587 | bo = -1; |
| 1588 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1589 | #endif |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1590 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1591 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1592 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1593 | if (bo == -1) { |
| 1594 | /* force LE */ |
| 1595 | ihi = 1; |
| 1596 | ilo = 0; |
| 1597 | } |
| 1598 | else if (bo == 1) { |
| 1599 | /* force BE */ |
| 1600 | ihi = 0; |
| 1601 | ilo = 1; |
| 1602 | } |
| 1603 | |
| 1604 | while (q < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1605 | Py_UNICODE ch; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1606 | /* remaining bytes at the end? (size should be even) */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1607 | if (e-q<2) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1608 | if (consumed) |
| 1609 | break; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1610 | errmsg = "truncated data"; |
| 1611 | startinpos = ((const char *)q)-starts; |
| 1612 | endinpos = ((const char *)e)-starts; |
| 1613 | goto utf16Error; |
| 1614 | /* The remaining input chars are ignored if the callback |
| 1615 | chooses to skip the input */ |
| 1616 | } |
| 1617 | ch = (q[ihi] << 8) | q[ilo]; |
| 1618 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1619 | q += 2; |
| 1620 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1621 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 1622 | *p++ = ch; |
| 1623 | continue; |
| 1624 | } |
| 1625 | |
| 1626 | /* UTF-16 code pair: */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1627 | if (q >= e) { |
| 1628 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1629 | startinpos = (((const char *)q)-2)-starts; |
| 1630 | endinpos = ((const char *)e)-starts; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1631 | goto utf16Error; |
| 1632 | } |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 1633 | if (0xD800 <= ch && ch <= 0xDBFF) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1634 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 1635 | q += 2; |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 1636 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1637 | #ifndef Py_UNICODE_WIDE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1638 | *p++ = ch; |
| 1639 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1640 | #else |
| 1641 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1642 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1643 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1644 | } |
| 1645 | else { |
| 1646 | errmsg = "illegal UTF-16 surrogate"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1647 | startinpos = (((const char *)q)-4)-starts; |
| 1648 | endinpos = startinpos+2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1649 | goto utf16Error; |
| 1650 | } |
| 1651 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1652 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1653 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1654 | startinpos = (((const char *)q)-2)-starts; |
| 1655 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1656 | /* Fall through to report the error */ |
| 1657 | |
| 1658 | utf16Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1659 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1660 | if (unicode_decode_call_errorhandler( |
| 1661 | errors, &errorHandler, |
| 1662 | "utf16", errmsg, |
| 1663 | starts, size, &startinpos, &endinpos, &exc, (const char **)&q, |
| 1664 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1665 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1666 | } |
| 1667 | |
| 1668 | if (byteorder) |
| 1669 | *byteorder = bo; |
| 1670 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1671 | if (consumed) |
| 1672 | *consumed = (const char *)q-starts; |
| 1673 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1674 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1675 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1676 | goto onError; |
| 1677 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1678 | Py_XDECREF(errorHandler); |
| 1679 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1680 | return (PyObject *)unicode; |
| 1681 | |
| 1682 | onError: |
| 1683 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1684 | Py_XDECREF(errorHandler); |
| 1685 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1686 | return NULL; |
| 1687 | } |
| 1688 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1689 | PyObject * |
| 1690 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1691 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1692 | const char *errors, |
| 1693 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1694 | { |
| 1695 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1696 | unsigned char *p; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1697 | #ifdef Py_UNICODE_WIDE |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1698 | int i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1699 | #else |
| 1700 | const int pairs = 0; |
| 1701 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1702 | /* Offsets from p for storing byte pairs in the right order. */ |
| 1703 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1704 | int ihi = 1, ilo = 0; |
| 1705 | #else |
| 1706 | int ihi = 0, ilo = 1; |
| 1707 | #endif |
| 1708 | |
| 1709 | #define STORECHAR(CH) \ |
| 1710 | do { \ |
| 1711 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 1712 | p[ilo] = (CH) & 0xff; \ |
| 1713 | p += 2; \ |
| 1714 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1715 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1716 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1717 | for (i = pairs = 0; i < size; i++) |
| 1718 | if (s[i] >= 0x10000) |
| 1719 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1720 | #endif |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1721 | v = PyString_FromStringAndSize(NULL, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1722 | 2 * (size + pairs + (byteorder == 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1723 | if (v == NULL) |
| 1724 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1725 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1726 | p = (unsigned char *)PyString_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1727 | if (byteorder == 0) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1728 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 1729 | if (size == 0) |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 1730 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1731 | |
| 1732 | if (byteorder == -1) { |
| 1733 | /* force LE */ |
| 1734 | ihi = 1; |
| 1735 | ilo = 0; |
| 1736 | } |
| 1737 | else if (byteorder == 1) { |
| 1738 | /* force BE */ |
| 1739 | ihi = 0; |
| 1740 | ilo = 1; |
| 1741 | } |
| 1742 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1743 | while (size-- > 0) { |
| 1744 | Py_UNICODE ch = *s++; |
| 1745 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1746 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1747 | if (ch >= 0x10000) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1748 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 1749 | ch = 0xD800 | ((ch-0x10000) >> 10); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1750 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1751 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1752 | STORECHAR(ch); |
| 1753 | if (ch2) |
| 1754 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1755 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1756 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1757 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
| 1760 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 1761 | { |
| 1762 | if (!PyUnicode_Check(unicode)) { |
| 1763 | PyErr_BadArgument(); |
| 1764 | return NULL; |
| 1765 | } |
| 1766 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 1767 | PyUnicode_GET_SIZE(unicode), |
| 1768 | NULL, |
| 1769 | 0); |
| 1770 | } |
| 1771 | |
| 1772 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 1773 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1774 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 1775 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1776 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1777 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1778 | const char *errors) |
| 1779 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1780 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1781 | Py_ssize_t startinpos; |
| 1782 | Py_ssize_t endinpos; |
| 1783 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1784 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1785 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1786 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1787 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1788 | char* message; |
| 1789 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1790 | PyObject *errorHandler = NULL; |
| 1791 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1792 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1793 | /* Escaped strings will always be longer than the resulting |
| 1794 | 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] | 1795 | length after conversion to the true value. |
| 1796 | (but if the error callback returns a long replacement string |
| 1797 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1798 | v = _PyUnicode_New(size); |
| 1799 | if (v == NULL) |
| 1800 | goto onError; |
| 1801 | if (size == 0) |
| 1802 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1803 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1804 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1805 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1806 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1807 | while (s < end) { |
| 1808 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 1809 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1810 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1811 | |
| 1812 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 1813 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1814 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1815 | continue; |
| 1816 | } |
| 1817 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1818 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1819 | /* \ - Escapes */ |
| 1820 | s++; |
| 1821 | switch (*s++) { |
| 1822 | |
| 1823 | /* \x escapes */ |
| 1824 | case '\n': break; |
| 1825 | case '\\': *p++ = '\\'; break; |
| 1826 | case '\'': *p++ = '\''; break; |
| 1827 | case '\"': *p++ = '\"'; break; |
| 1828 | case 'b': *p++ = '\b'; break; |
| 1829 | case 'f': *p++ = '\014'; break; /* FF */ |
| 1830 | case 't': *p++ = '\t'; break; |
| 1831 | case 'n': *p++ = '\n'; break; |
| 1832 | case 'r': *p++ = '\r'; break; |
| 1833 | case 'v': *p++ = '\013'; break; /* VT */ |
| 1834 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 1835 | |
| 1836 | /* \OOO (octal) escapes */ |
| 1837 | case '0': case '1': case '2': case '3': |
| 1838 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1839 | x = s[-1] - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1840 | if ('0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1841 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1842 | if ('0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1843 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1844 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1845 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1846 | break; |
| 1847 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1848 | /* hex escapes */ |
| 1849 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1850 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1851 | digits = 2; |
| 1852 | message = "truncated \\xXX escape"; |
| 1853 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1854 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1855 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1856 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1857 | digits = 4; |
| 1858 | message = "truncated \\uXXXX escape"; |
| 1859 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1860 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1861 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1862 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1863 | digits = 8; |
| 1864 | message = "truncated \\UXXXXXXXX escape"; |
| 1865 | hexescape: |
| 1866 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1867 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1868 | if (s+digits>end) { |
| 1869 | endinpos = size; |
| 1870 | if (unicode_decode_call_errorhandler( |
| 1871 | errors, &errorHandler, |
| 1872 | "unicodeescape", "end of string in escape sequence", |
| 1873 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1874 | (PyObject **)&v, &outpos, &p)) |
| 1875 | goto onError; |
| 1876 | goto nextByte; |
| 1877 | } |
| 1878 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1879 | c = (unsigned char) s[i]; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1880 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1881 | endinpos = (s+i+1)-starts; |
| 1882 | if (unicode_decode_call_errorhandler( |
| 1883 | errors, &errorHandler, |
| 1884 | "unicodeescape", message, |
| 1885 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1886 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1887 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1888 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1889 | } |
| 1890 | chr = (chr<<4) & ~0xF; |
| 1891 | if (c >= '0' && c <= '9') |
| 1892 | chr += c - '0'; |
| 1893 | else if (c >= 'a' && c <= 'f') |
| 1894 | chr += 10 + c - 'a'; |
| 1895 | else |
| 1896 | chr += 10 + c - 'A'; |
| 1897 | } |
| 1898 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 1899 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1900 | /* _decoding_error will have already written into the |
| 1901 | target buffer. */ |
| 1902 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1903 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1904 | /* when we get here, chr is a 32-bit unicode character */ |
| 1905 | if (chr <= 0xffff) |
| 1906 | /* UCS-2 character */ |
| 1907 | *p++ = (Py_UNICODE) chr; |
| 1908 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1909 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1910 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1911 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1912 | *p++ = chr; |
| 1913 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1914 | chr -= 0x10000L; |
| 1915 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1916 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1917 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1918 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1919 | endinpos = s-starts; |
| 1920 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1921 | if (unicode_decode_call_errorhandler( |
| 1922 | errors, &errorHandler, |
| 1923 | "unicodeescape", "illegal Unicode character", |
| 1924 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1925 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1926 | goto onError; |
| 1927 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1928 | break; |
| 1929 | |
| 1930 | /* \N{name} */ |
| 1931 | case 'N': |
| 1932 | message = "malformed \\N character escape"; |
| 1933 | if (ucnhash_CAPI == NULL) { |
| 1934 | /* load the unicode data module */ |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1935 | PyObject *m, *api; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1936 | m = PyImport_ImportModule("unicodedata"); |
| 1937 | if (m == NULL) |
| 1938 | goto ucnhashError; |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1939 | api = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1940 | Py_DECREF(m); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1941 | if (api == NULL) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1942 | goto ucnhashError; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 1943 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1944 | Py_DECREF(api); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1945 | if (ucnhash_CAPI == NULL) |
| 1946 | goto ucnhashError; |
| 1947 | } |
| 1948 | if (*s == '{') { |
| 1949 | const char *start = s+1; |
| 1950 | /* look for the closing brace */ |
| 1951 | while (*s != '}' && s < end) |
| 1952 | s++; |
| 1953 | if (s > start && s < end && *s == '}') { |
| 1954 | /* found a name. look it up in the unicode database */ |
| 1955 | message = "unknown Unicode character name"; |
| 1956 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 1957 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1958 | goto store; |
| 1959 | } |
| 1960 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1961 | endinpos = s-starts; |
| 1962 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1963 | if (unicode_decode_call_errorhandler( |
| 1964 | errors, &errorHandler, |
| 1965 | "unicodeescape", message, |
| 1966 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1967 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1968 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1969 | break; |
| 1970 | |
| 1971 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1972 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1973 | message = "\\ at end of string"; |
| 1974 | s--; |
| 1975 | endinpos = s-starts; |
| 1976 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1977 | if (unicode_decode_call_errorhandler( |
| 1978 | errors, &errorHandler, |
| 1979 | "unicodeescape", message, |
| 1980 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1981 | (PyObject **)&v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1982 | goto onError; |
| 1983 | } |
| 1984 | else { |
| 1985 | *p++ = '\\'; |
| 1986 | *p++ = (unsigned char)s[-1]; |
| 1987 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1988 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1989 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1990 | nextByte: |
| 1991 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1992 | } |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1993 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1994 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 1995 | Py_XDECREF(errorHandler); |
| 1996 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1997 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1998 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1999 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2000 | PyErr_SetString( |
| 2001 | PyExc_UnicodeError, |
| 2002 | "\\N escapes not supported (can't load unicodedata module)" |
| 2003 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2004 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2005 | Py_XDECREF(errorHandler); |
| 2006 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 2007 | return NULL; |
| 2008 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2009 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2010 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2011 | Py_XDECREF(errorHandler); |
| 2012 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2013 | return NULL; |
| 2014 | } |
| 2015 | |
| 2016 | /* Return a Unicode-Escape string version of the Unicode object. |
| 2017 | |
| 2018 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 2019 | appropriate. |
| 2020 | |
| 2021 | */ |
| 2022 | |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 2023 | static const Py_UNICODE *findchar(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2024 | Py_ssize_t size, |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 2025 | Py_UNICODE ch); |
| 2026 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2027 | static |
| 2028 | PyObject *unicodeescape_string(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2029 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2030 | int quotes) |
| 2031 | { |
| 2032 | PyObject *repr; |
| 2033 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2034 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2035 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2036 | |
| 2037 | repr = PyString_FromStringAndSize(NULL, 2 + 6*size + 1); |
| 2038 | if (repr == NULL) |
| 2039 | return NULL; |
| 2040 | |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2041 | p = PyString_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2042 | |
| 2043 | if (quotes) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2044 | *p++ = 'u'; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2045 | *p++ = (findchar(s, size, '\'') && |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2046 | !findchar(s, size, '"')) ? '"' : '\''; |
| 2047 | } |
| 2048 | while (size-- > 0) { |
| 2049 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2050 | |
Hye-Shik Chang | 835b243 | 2005-12-17 04:38:31 +0000 | [diff] [blame] | 2051 | /* Escape quotes and backslashes */ |
| 2052 | if ((quotes && |
| 2053 | ch == (Py_UNICODE) PyString_AS_STRING(repr)[1]) || ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2054 | *p++ = '\\'; |
| 2055 | *p++ = (char) ch; |
Guido van Rossum | ad9744a | 2001-09-21 15:38:17 +0000 | [diff] [blame] | 2056 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2057 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2058 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 2059 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2060 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 2061 | else if (ch >= 0x10000) { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2062 | Py_ssize_t offset = p - PyString_AS_STRING(repr); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2063 | |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2064 | /* Resize the string if necessary */ |
| 2065 | if (offset + 12 > PyString_GET_SIZE(repr)) { |
| 2066 | if (_PyString_Resize(&repr, PyString_GET_SIZE(repr) + 100)) |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2067 | return NULL; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2068 | p = PyString_AS_STRING(repr) + offset; |
| 2069 | } |
| 2070 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2071 | *p++ = '\\'; |
| 2072 | *p++ = 'U'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2073 | *p++ = hexdigit[(ch >> 28) & 0x0000000F]; |
| 2074 | *p++ = hexdigit[(ch >> 24) & 0x0000000F]; |
| 2075 | *p++ = hexdigit[(ch >> 20) & 0x0000000F]; |
| 2076 | *p++ = hexdigit[(ch >> 16) & 0x0000000F]; |
| 2077 | *p++ = hexdigit[(ch >> 12) & 0x0000000F]; |
| 2078 | *p++ = hexdigit[(ch >> 8) & 0x0000000F]; |
| 2079 | *p++ = hexdigit[(ch >> 4) & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2080 | *p++ = hexdigit[ch & 0x0000000F]; |
| 2081 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2082 | } |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 2083 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2084 | /* Map UTF-16 surrogate pairs to Unicode \UXXXXXXXX escapes */ |
| 2085 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 2086 | Py_UNICODE ch2; |
| 2087 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2088 | |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2089 | ch2 = *s++; |
| 2090 | size--; |
| 2091 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 2092 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 2093 | *p++ = '\\'; |
| 2094 | *p++ = 'U'; |
| 2095 | *p++ = hexdigit[(ucs >> 28) & 0x0000000F]; |
| 2096 | *p++ = hexdigit[(ucs >> 24) & 0x0000000F]; |
| 2097 | *p++ = hexdigit[(ucs >> 20) & 0x0000000F]; |
| 2098 | *p++ = hexdigit[(ucs >> 16) & 0x0000000F]; |
| 2099 | *p++ = hexdigit[(ucs >> 12) & 0x0000000F]; |
| 2100 | *p++ = hexdigit[(ucs >> 8) & 0x0000000F]; |
| 2101 | *p++ = hexdigit[(ucs >> 4) & 0x0000000F]; |
| 2102 | *p++ = hexdigit[ucs & 0x0000000F]; |
| 2103 | continue; |
| 2104 | } |
| 2105 | /* Fall through: isolated surrogates are copied as-is */ |
| 2106 | s--; |
| 2107 | size++; |
| 2108 | } |
| 2109 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2110 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2111 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2112 | *p++ = '\\'; |
| 2113 | *p++ = 'u'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2114 | *p++ = hexdigit[(ch >> 12) & 0x000F]; |
| 2115 | *p++ = hexdigit[(ch >> 8) & 0x000F]; |
| 2116 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2117 | *p++ = hexdigit[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2118 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2119 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2120 | /* Map special whitespace to '\t', \n', '\r' */ |
| 2121 | else if (ch == '\t') { |
| 2122 | *p++ = '\\'; |
| 2123 | *p++ = 't'; |
| 2124 | } |
| 2125 | else if (ch == '\n') { |
| 2126 | *p++ = '\\'; |
| 2127 | *p++ = 'n'; |
| 2128 | } |
| 2129 | else if (ch == '\r') { |
| 2130 | *p++ = '\\'; |
| 2131 | *p++ = 'r'; |
| 2132 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2133 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2134 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 2135 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2136 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2137 | *p++ = 'x'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2138 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2139 | *p++ = hexdigit[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2140 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2141 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2142 | /* Copy everything else as-is */ |
| 2143 | else |
| 2144 | *p++ = (char) ch; |
| 2145 | } |
| 2146 | if (quotes) |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2147 | *p++ = PyString_AS_STRING(repr)[1]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2148 | |
| 2149 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2150 | _PyString_Resize(&repr, p - PyString_AS_STRING(repr)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2151 | return repr; |
| 2152 | } |
| 2153 | |
| 2154 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2155 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2156 | { |
| 2157 | return unicodeescape_string(s, size, 0); |
| 2158 | } |
| 2159 | |
| 2160 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 2161 | { |
| 2162 | if (!PyUnicode_Check(unicode)) { |
| 2163 | PyErr_BadArgument(); |
| 2164 | return NULL; |
| 2165 | } |
| 2166 | return PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2167 | PyUnicode_GET_SIZE(unicode)); |
| 2168 | } |
| 2169 | |
| 2170 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 2171 | |
| 2172 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2173 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2174 | const char *errors) |
| 2175 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2176 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2177 | Py_ssize_t startinpos; |
| 2178 | Py_ssize_t endinpos; |
| 2179 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2180 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2181 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2182 | const char *end; |
| 2183 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2184 | PyObject *errorHandler = NULL; |
| 2185 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2186 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2187 | /* Escaped strings will always be longer than the resulting |
| 2188 | 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] | 2189 | length after conversion to the true value. (But decoding error |
| 2190 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2191 | v = _PyUnicode_New(size); |
| 2192 | if (v == NULL) |
| 2193 | goto onError; |
| 2194 | if (size == 0) |
| 2195 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2196 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2197 | end = s + size; |
| 2198 | while (s < end) { |
| 2199 | unsigned char c; |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 2200 | Py_UCS4 x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2201 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2202 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2203 | |
| 2204 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2205 | if (*s != '\\') { |
| 2206 | *p++ = (unsigned char)*s++; |
| 2207 | continue; |
| 2208 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2209 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2210 | |
| 2211 | /* \u-escapes are only interpreted iff the number of leading |
| 2212 | backslashes if odd */ |
| 2213 | bs = s; |
| 2214 | for (;s < end;) { |
| 2215 | if (*s != '\\') |
| 2216 | break; |
| 2217 | *p++ = (unsigned char)*s++; |
| 2218 | } |
| 2219 | if (((s - bs) & 1) == 0 || |
| 2220 | s >= end || |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2221 | (*s != 'u' && *s != 'U')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2222 | continue; |
| 2223 | } |
| 2224 | p--; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2225 | count = *s=='u' ? 4 : 8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2226 | s++; |
| 2227 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2228 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2229 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2230 | for (x = 0, i = 0; i < count; ++i, ++s) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2231 | c = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2232 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2233 | endinpos = s-starts; |
| 2234 | if (unicode_decode_call_errorhandler( |
| 2235 | errors, &errorHandler, |
| 2236 | "rawunicodeescape", "truncated \\uXXXX", |
| 2237 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2238 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2239 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2240 | goto nextByte; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2241 | } |
| 2242 | x = (x<<4) & ~0xF; |
| 2243 | if (c >= '0' && c <= '9') |
| 2244 | x += c - '0'; |
| 2245 | else if (c >= 'a' && c <= 'f') |
| 2246 | x += 10 + c - 'a'; |
| 2247 | else |
| 2248 | x += 10 + c - 'A'; |
| 2249 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2250 | #ifndef Py_UNICODE_WIDE |
| 2251 | if (x > 0x10000) { |
| 2252 | if (unicode_decode_call_errorhandler( |
| 2253 | errors, &errorHandler, |
| 2254 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
| 2255 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2256 | (PyObject **)&v, &outpos, &p)) |
| 2257 | goto onError; |
| 2258 | } |
| 2259 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2260 | *p++ = x; |
| 2261 | nextByte: |
| 2262 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2263 | } |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2264 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2265 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2266 | Py_XDECREF(errorHandler); |
| 2267 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2268 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2269 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2270 | onError: |
| 2271 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2272 | Py_XDECREF(errorHandler); |
| 2273 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2274 | return NULL; |
| 2275 | } |
| 2276 | |
| 2277 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2278 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2279 | { |
| 2280 | PyObject *repr; |
| 2281 | char *p; |
| 2282 | char *q; |
| 2283 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2284 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2285 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2286 | #ifdef Py_UNICODE_WIDE |
| 2287 | repr = PyString_FromStringAndSize(NULL, 10 * size); |
| 2288 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2289 | repr = PyString_FromStringAndSize(NULL, 6 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2290 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2291 | if (repr == NULL) |
| 2292 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2293 | if (size == 0) |
| 2294 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2295 | |
| 2296 | p = q = PyString_AS_STRING(repr); |
| 2297 | while (size-- > 0) { |
| 2298 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2299 | #ifdef Py_UNICODE_WIDE |
| 2300 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2301 | if (ch >= 0x10000) { |
| 2302 | *p++ = '\\'; |
| 2303 | *p++ = 'U'; |
| 2304 | *p++ = hexdigit[(ch >> 28) & 0xf]; |
| 2305 | *p++ = hexdigit[(ch >> 24) & 0xf]; |
| 2306 | *p++ = hexdigit[(ch >> 20) & 0xf]; |
| 2307 | *p++ = hexdigit[(ch >> 16) & 0xf]; |
| 2308 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 2309 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 2310 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 2311 | *p++ = hexdigit[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2312 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2313 | else |
| 2314 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2315 | /* Map 16-bit characters to '\uxxxx' */ |
| 2316 | if (ch >= 256) { |
| 2317 | *p++ = '\\'; |
| 2318 | *p++ = 'u'; |
| 2319 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 2320 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 2321 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 2322 | *p++ = hexdigit[ch & 15]; |
| 2323 | } |
| 2324 | /* Copy everything else as-is */ |
| 2325 | else |
| 2326 | *p++ = (char) ch; |
| 2327 | } |
| 2328 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2329 | _PyString_Resize(&repr, p - q); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2330 | return repr; |
| 2331 | } |
| 2332 | |
| 2333 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 2334 | { |
| 2335 | if (!PyUnicode_Check(unicode)) { |
| 2336 | PyErr_BadArgument(); |
| 2337 | return NULL; |
| 2338 | } |
| 2339 | return PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2340 | PyUnicode_GET_SIZE(unicode)); |
| 2341 | } |
| 2342 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2343 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 2344 | |
| 2345 | PyObject *_PyUnicode_DecodeUnicodeInternal(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 | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2347 | const char *errors) |
| 2348 | { |
| 2349 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2350 | Py_ssize_t startinpos; |
| 2351 | Py_ssize_t endinpos; |
| 2352 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2353 | PyUnicodeObject *v; |
| 2354 | Py_UNICODE *p; |
| 2355 | const char *end; |
| 2356 | const char *reason; |
| 2357 | PyObject *errorHandler = NULL; |
| 2358 | PyObject *exc = NULL; |
| 2359 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 2360 | #ifdef Py_UNICODE_WIDE |
| 2361 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 2362 | #endif |
| 2363 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2364 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 2365 | if (v == NULL) |
| 2366 | goto onError; |
| 2367 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
| 2368 | return (PyObject *)v; |
| 2369 | p = PyUnicode_AS_UNICODE(v); |
| 2370 | end = s + size; |
| 2371 | |
| 2372 | while (s < end) { |
Neal Norwitz | 1004a53 | 2006-05-15 07:17:23 +0000 | [diff] [blame] | 2373 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2374 | /* We have to sanity check the raw data, otherwise doom looms for |
| 2375 | some malformed UCS-4 data. */ |
| 2376 | if ( |
| 2377 | #ifdef Py_UNICODE_WIDE |
| 2378 | *p > unimax || *p < 0 || |
| 2379 | #endif |
| 2380 | end-s < Py_UNICODE_SIZE |
| 2381 | ) |
| 2382 | { |
| 2383 | startinpos = s - starts; |
| 2384 | if (end-s < Py_UNICODE_SIZE) { |
| 2385 | endinpos = end-starts; |
| 2386 | reason = "truncated input"; |
| 2387 | } |
| 2388 | else { |
| 2389 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 2390 | reason = "illegal code point (> 0x10FFFF)"; |
| 2391 | } |
| 2392 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 2393 | if (unicode_decode_call_errorhandler( |
| 2394 | errors, &errorHandler, |
| 2395 | "unicode_internal", reason, |
| 2396 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2397 | (PyObject **)&v, &outpos, &p)) { |
| 2398 | goto onError; |
| 2399 | } |
| 2400 | } |
| 2401 | else { |
| 2402 | p++; |
| 2403 | s += Py_UNICODE_SIZE; |
| 2404 | } |
| 2405 | } |
| 2406 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2407 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2408 | goto onError; |
| 2409 | Py_XDECREF(errorHandler); |
| 2410 | Py_XDECREF(exc); |
| 2411 | return (PyObject *)v; |
| 2412 | |
| 2413 | onError: |
| 2414 | Py_XDECREF(v); |
| 2415 | Py_XDECREF(errorHandler); |
| 2416 | Py_XDECREF(exc); |
| 2417 | return NULL; |
| 2418 | } |
| 2419 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2420 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 2421 | |
| 2422 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2423 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2424 | const char *errors) |
| 2425 | { |
| 2426 | PyUnicodeObject *v; |
| 2427 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2428 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2429 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2430 | if (size == 1) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 2431 | Py_UNICODE r = *(unsigned char*)s; |
| 2432 | return PyUnicode_FromUnicode(&r, 1); |
| 2433 | } |
| 2434 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2435 | v = _PyUnicode_New(size); |
| 2436 | if (v == NULL) |
| 2437 | goto onError; |
| 2438 | if (size == 0) |
| 2439 | return (PyObject *)v; |
| 2440 | p = PyUnicode_AS_UNICODE(v); |
| 2441 | while (size-- > 0) |
| 2442 | *p++ = (unsigned char)*s++; |
| 2443 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2444 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2445 | onError: |
| 2446 | Py_XDECREF(v); |
| 2447 | return NULL; |
| 2448 | } |
| 2449 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2450 | /* create or adjust a UnicodeEncodeError */ |
| 2451 | static void make_encode_exception(PyObject **exceptionObject, |
| 2452 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2453 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 2454 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2455 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2456 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2457 | if (*exceptionObject == NULL) { |
| 2458 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 2459 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2460 | } |
| 2461 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2462 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 2463 | goto onError; |
| 2464 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 2465 | goto onError; |
| 2466 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 2467 | goto onError; |
| 2468 | return; |
| 2469 | onError: |
| 2470 | Py_DECREF(*exceptionObject); |
| 2471 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2472 | } |
| 2473 | } |
| 2474 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2475 | /* raises a UnicodeEncodeError */ |
| 2476 | static void raise_encode_exception(PyObject **exceptionObject, |
| 2477 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2478 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 2479 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2480 | const char *reason) |
| 2481 | { |
| 2482 | make_encode_exception(exceptionObject, |
| 2483 | encoding, unicode, size, startpos, endpos, reason); |
| 2484 | if (*exceptionObject != NULL) |
| 2485 | PyCodec_StrictErrors(*exceptionObject); |
| 2486 | } |
| 2487 | |
| 2488 | /* error handling callback helper: |
| 2489 | build arguments, call the callback and check the arguments, |
| 2490 | put the result into newpos and return the replacement string, which |
| 2491 | has to be freed by the caller */ |
| 2492 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 2493 | PyObject **errorHandler, |
| 2494 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2495 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 2496 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 2497 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2498 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2499 | 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] | 2500 | |
| 2501 | PyObject *restuple; |
| 2502 | PyObject *resunicode; |
| 2503 | |
| 2504 | if (*errorHandler == NULL) { |
| 2505 | *errorHandler = PyCodec_LookupError(errors); |
| 2506 | if (*errorHandler == NULL) |
| 2507 | return NULL; |
| 2508 | } |
| 2509 | |
| 2510 | make_encode_exception(exceptionObject, |
| 2511 | encoding, unicode, size, startpos, endpos, reason); |
| 2512 | if (*exceptionObject == NULL) |
| 2513 | return NULL; |
| 2514 | |
| 2515 | restuple = PyObject_CallFunctionObjArgs( |
| 2516 | *errorHandler, *exceptionObject, NULL); |
| 2517 | if (restuple == NULL) |
| 2518 | return NULL; |
| 2519 | if (!PyTuple_Check(restuple)) { |
| 2520 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 2521 | Py_DECREF(restuple); |
| 2522 | return NULL; |
| 2523 | } |
| 2524 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 2525 | &resunicode, newpos)) { |
| 2526 | Py_DECREF(restuple); |
| 2527 | return NULL; |
| 2528 | } |
| 2529 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2530 | *newpos = size+*newpos; |
| 2531 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 2532 | 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] | 2533 | Py_DECREF(restuple); |
| 2534 | return NULL; |
| 2535 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2536 | Py_INCREF(resunicode); |
| 2537 | Py_DECREF(restuple); |
| 2538 | return resunicode; |
| 2539 | } |
| 2540 | |
| 2541 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2542 | Py_ssize_t size, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2543 | const char *errors, |
| 2544 | int limit) |
| 2545 | { |
| 2546 | /* output object */ |
| 2547 | PyObject *res; |
| 2548 | /* pointers to the beginning and end+1 of input */ |
| 2549 | const Py_UNICODE *startp = p; |
| 2550 | const Py_UNICODE *endp = p + size; |
| 2551 | /* pointer to the beginning of the unencodable characters */ |
| 2552 | /* const Py_UNICODE *badp = NULL; */ |
| 2553 | /* pointer into the output */ |
| 2554 | char *str; |
| 2555 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2556 | Py_ssize_t respos = 0; |
| 2557 | Py_ssize_t ressize; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2558 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 2559 | 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] | 2560 | PyObject *errorHandler = NULL; |
| 2561 | PyObject *exc = NULL; |
| 2562 | /* the following variable is used for caching string comparisons |
| 2563 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 2564 | int known_errorHandler = -1; |
| 2565 | |
| 2566 | /* allocate enough for a simple encoding without |
| 2567 | replacements, if we need more, we'll resize */ |
| 2568 | res = PyString_FromStringAndSize(NULL, size); |
| 2569 | if (res == NULL) |
| 2570 | goto onError; |
| 2571 | if (size == 0) |
| 2572 | return res; |
| 2573 | str = PyString_AS_STRING(res); |
| 2574 | ressize = size; |
| 2575 | |
| 2576 | while (p<endp) { |
| 2577 | Py_UNICODE c = *p; |
| 2578 | |
| 2579 | /* can we encode this? */ |
| 2580 | if (c<limit) { |
| 2581 | /* no overflow check, because we know that the space is enough */ |
| 2582 | *str++ = (char)c; |
| 2583 | ++p; |
| 2584 | } |
| 2585 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2586 | Py_ssize_t unicodepos = p-startp; |
| 2587 | Py_ssize_t requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2588 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2589 | Py_ssize_t repsize; |
| 2590 | Py_ssize_t newpos; |
| 2591 | Py_ssize_t respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2592 | Py_UNICODE *uni2; |
| 2593 | /* startpos for collecting unencodable chars */ |
| 2594 | const Py_UNICODE *collstart = p; |
| 2595 | const Py_UNICODE *collend = p; |
| 2596 | /* find all unecodable characters */ |
| 2597 | while ((collend < endp) && ((*collend)>=limit)) |
| 2598 | ++collend; |
| 2599 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 2600 | if (known_errorHandler==-1) { |
| 2601 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 2602 | known_errorHandler = 1; |
| 2603 | else if (!strcmp(errors, "replace")) |
| 2604 | known_errorHandler = 2; |
| 2605 | else if (!strcmp(errors, "ignore")) |
| 2606 | known_errorHandler = 3; |
| 2607 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 2608 | known_errorHandler = 4; |
| 2609 | else |
| 2610 | known_errorHandler = 0; |
| 2611 | } |
| 2612 | switch (known_errorHandler) { |
| 2613 | case 1: /* strict */ |
| 2614 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 2615 | goto onError; |
| 2616 | case 2: /* replace */ |
| 2617 | while (collstart++<collend) |
| 2618 | *str++ = '?'; /* fall through */ |
| 2619 | case 3: /* ignore */ |
| 2620 | p = collend; |
| 2621 | break; |
| 2622 | case 4: /* xmlcharrefreplace */ |
| 2623 | respos = str-PyString_AS_STRING(res); |
| 2624 | /* determine replacement size (temporarily (mis)uses p) */ |
| 2625 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 2626 | if (*p<10) |
| 2627 | repsize += 2+1+1; |
| 2628 | else if (*p<100) |
| 2629 | repsize += 2+2+1; |
| 2630 | else if (*p<1000) |
| 2631 | repsize += 2+3+1; |
| 2632 | else if (*p<10000) |
| 2633 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 2634 | #ifndef Py_UNICODE_WIDE |
| 2635 | else |
| 2636 | repsize += 2+5+1; |
| 2637 | #else |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2638 | else if (*p<100000) |
| 2639 | repsize += 2+5+1; |
| 2640 | else if (*p<1000000) |
| 2641 | repsize += 2+6+1; |
| 2642 | else |
| 2643 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2644 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2645 | } |
| 2646 | requiredsize = respos+repsize+(endp-collend); |
| 2647 | if (requiredsize > ressize) { |
| 2648 | if (requiredsize<2*ressize) |
| 2649 | requiredsize = 2*ressize; |
| 2650 | if (_PyString_Resize(&res, requiredsize)) |
| 2651 | goto onError; |
| 2652 | str = PyString_AS_STRING(res) + respos; |
| 2653 | ressize = requiredsize; |
| 2654 | } |
| 2655 | /* generate replacement (temporarily (mis)uses p) */ |
| 2656 | for (p = collstart; p < collend; ++p) { |
| 2657 | str += sprintf(str, "&#%d;", (int)*p); |
| 2658 | } |
| 2659 | p = collend; |
| 2660 | break; |
| 2661 | default: |
| 2662 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 2663 | encoding, reason, startp, size, &exc, |
| 2664 | collstart-startp, collend-startp, &newpos); |
| 2665 | if (repunicode == NULL) |
| 2666 | goto onError; |
| 2667 | /* need more space? (at least enough for what we |
| 2668 | have+the replacement+the rest of the string, so |
| 2669 | we won't have to check space for encodable characters) */ |
| 2670 | respos = str-PyString_AS_STRING(res); |
| 2671 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 2672 | requiredsize = respos+repsize+(endp-collend); |
| 2673 | if (requiredsize > ressize) { |
| 2674 | if (requiredsize<2*ressize) |
| 2675 | requiredsize = 2*ressize; |
| 2676 | if (_PyString_Resize(&res, requiredsize)) { |
| 2677 | Py_DECREF(repunicode); |
| 2678 | goto onError; |
| 2679 | } |
| 2680 | str = PyString_AS_STRING(res) + respos; |
| 2681 | ressize = requiredsize; |
| 2682 | } |
| 2683 | /* check if there is anything unencodable in the replacement |
| 2684 | and copy it to the output */ |
| 2685 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 2686 | c = *uni2; |
| 2687 | if (c >= limit) { |
| 2688 | raise_encode_exception(&exc, encoding, startp, size, |
| 2689 | unicodepos, unicodepos+1, reason); |
| 2690 | Py_DECREF(repunicode); |
| 2691 | goto onError; |
| 2692 | } |
| 2693 | *str = (char)c; |
| 2694 | } |
| 2695 | p = startp + newpos; |
| 2696 | Py_DECREF(repunicode); |
| 2697 | } |
| 2698 | } |
| 2699 | } |
| 2700 | /* Resize if we allocated to much */ |
| 2701 | respos = str-PyString_AS_STRING(res); |
| 2702 | if (respos<ressize) |
| 2703 | /* If this falls res will be NULL */ |
| 2704 | _PyString_Resize(&res, respos); |
| 2705 | Py_XDECREF(errorHandler); |
| 2706 | Py_XDECREF(exc); |
| 2707 | return res; |
| 2708 | |
| 2709 | onError: |
| 2710 | Py_XDECREF(res); |
| 2711 | Py_XDECREF(errorHandler); |
| 2712 | Py_XDECREF(exc); |
| 2713 | return NULL; |
| 2714 | } |
| 2715 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2716 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2717 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2718 | const char *errors) |
| 2719 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2720 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2721 | } |
| 2722 | |
| 2723 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 2724 | { |
| 2725 | if (!PyUnicode_Check(unicode)) { |
| 2726 | PyErr_BadArgument(); |
| 2727 | return NULL; |
| 2728 | } |
| 2729 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 2730 | PyUnicode_GET_SIZE(unicode), |
| 2731 | NULL); |
| 2732 | } |
| 2733 | |
| 2734 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 2735 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2736 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2737 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2738 | const char *errors) |
| 2739 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2740 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2741 | PyUnicodeObject *v; |
| 2742 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2743 | Py_ssize_t startinpos; |
| 2744 | Py_ssize_t endinpos; |
| 2745 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2746 | const char *e; |
| 2747 | PyObject *errorHandler = NULL; |
| 2748 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2749 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2750 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 2751 | if (size == 1 && *(unsigned char*)s < 128) { |
| 2752 | Py_UNICODE r = *(unsigned char*)s; |
| 2753 | return PyUnicode_FromUnicode(&r, 1); |
| 2754 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2755 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2756 | v = _PyUnicode_New(size); |
| 2757 | if (v == NULL) |
| 2758 | goto onError; |
| 2759 | if (size == 0) |
| 2760 | return (PyObject *)v; |
| 2761 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2762 | e = s + size; |
| 2763 | while (s < e) { |
| 2764 | register unsigned char c = (unsigned char)*s; |
| 2765 | if (c < 128) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2766 | *p++ = c; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2767 | ++s; |
| 2768 | } |
| 2769 | else { |
| 2770 | startinpos = s-starts; |
| 2771 | endinpos = startinpos + 1; |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 2772 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2773 | if (unicode_decode_call_errorhandler( |
| 2774 | errors, &errorHandler, |
| 2775 | "ascii", "ordinal not in range(128)", |
| 2776 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2777 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2778 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2779 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2780 | } |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2781 | if (p - PyUnicode_AS_UNICODE(v) < PyString_GET_SIZE(v)) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2782 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2783 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2784 | Py_XDECREF(errorHandler); |
| 2785 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2786 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2787 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2788 | onError: |
| 2789 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2790 | Py_XDECREF(errorHandler); |
| 2791 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2792 | return NULL; |
| 2793 | } |
| 2794 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2795 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2796 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2797 | const char *errors) |
| 2798 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2799 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2800 | } |
| 2801 | |
| 2802 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 2803 | { |
| 2804 | if (!PyUnicode_Check(unicode)) { |
| 2805 | PyErr_BadArgument(); |
| 2806 | return NULL; |
| 2807 | } |
| 2808 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 2809 | PyUnicode_GET_SIZE(unicode), |
| 2810 | NULL); |
| 2811 | } |
| 2812 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2813 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2814 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2815 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2816 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2817 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2818 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2819 | const char *errors) |
| 2820 | { |
| 2821 | PyUnicodeObject *v; |
| 2822 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2823 | DWORD usize; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2824 | |
| 2825 | /* First get the size of the result */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2826 | assert(size < INT_MAX); |
| 2827 | usize = MultiByteToWideChar(CP_ACP, 0, s, (int)size, NULL, 0); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 2828 | if (size > 0 && usize==0) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2829 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2830 | |
| 2831 | v = _PyUnicode_New(usize); |
| 2832 | if (v == NULL) |
| 2833 | return NULL; |
| 2834 | if (usize == 0) |
| 2835 | return (PyObject *)v; |
| 2836 | p = PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2837 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, (int)size, p, usize)) { |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2838 | Py_DECREF(v); |
| 2839 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2840 | } |
| 2841 | |
| 2842 | return (PyObject *)v; |
| 2843 | } |
| 2844 | |
| 2845 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2846 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2847 | const char *errors) |
| 2848 | { |
| 2849 | PyObject *repr; |
| 2850 | char *s; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 2851 | DWORD mbcssize; |
| 2852 | |
| 2853 | /* If there are no characters, bail now! */ |
| 2854 | if (size==0) |
| 2855 | return PyString_FromString(""); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2856 | |
| 2857 | /* First get the size of the result */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2858 | assert(size<INT_MAX); |
| 2859 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, (int)size, NULL, 0, NULL, NULL); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2860 | if (mbcssize==0) |
| 2861 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2862 | |
| 2863 | repr = PyString_FromStringAndSize(NULL, mbcssize); |
| 2864 | if (repr == NULL) |
| 2865 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2866 | if (mbcssize == 0) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2867 | return repr; |
| 2868 | |
| 2869 | /* Do the conversion */ |
| 2870 | s = PyString_AS_STRING(repr); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2871 | assert(size < INT_MAX); |
| 2872 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, (int)size, s, mbcssize, NULL, NULL)) { |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2873 | Py_DECREF(repr); |
| 2874 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2875 | } |
| 2876 | return repr; |
| 2877 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2878 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2879 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 2880 | { |
| 2881 | if (!PyUnicode_Check(unicode)) { |
| 2882 | PyErr_BadArgument(); |
| 2883 | return NULL; |
| 2884 | } |
| 2885 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2886 | PyUnicode_GET_SIZE(unicode), |
| 2887 | NULL); |
| 2888 | } |
| 2889 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2890 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2891 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2892 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 2893 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2894 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2895 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2896 | PyObject *mapping, |
| 2897 | const char *errors) |
| 2898 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2899 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2900 | Py_ssize_t startinpos; |
| 2901 | Py_ssize_t endinpos; |
| 2902 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2903 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2904 | PyUnicodeObject *v; |
| 2905 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2906 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2907 | PyObject *errorHandler = NULL; |
| 2908 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2909 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2910 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2911 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2912 | /* Default to Latin-1 */ |
| 2913 | if (mapping == NULL) |
| 2914 | return PyUnicode_DecodeLatin1(s, size, errors); |
| 2915 | |
| 2916 | v = _PyUnicode_New(size); |
| 2917 | if (v == NULL) |
| 2918 | goto onError; |
| 2919 | if (size == 0) |
| 2920 | return (PyObject *)v; |
| 2921 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2922 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2923 | if (PyUnicode_CheckExact(mapping)) { |
| 2924 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 2925 | maplen = PyUnicode_GET_SIZE(mapping); |
| 2926 | while (s < e) { |
| 2927 | unsigned char ch = *s; |
| 2928 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2929 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2930 | if (ch < maplen) |
| 2931 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2932 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2933 | if (x == 0xfffe) { |
| 2934 | /* undefined mapping */ |
| 2935 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2936 | startinpos = s-starts; |
| 2937 | endinpos = startinpos+1; |
| 2938 | if (unicode_decode_call_errorhandler( |
| 2939 | errors, &errorHandler, |
| 2940 | "charmap", "character maps to <undefined>", |
| 2941 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2942 | (PyObject **)&v, &outpos, &p)) { |
| 2943 | goto onError; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 2944 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2945 | continue; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 2946 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2947 | *p++ = x; |
| 2948 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2949 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2950 | } |
| 2951 | else { |
| 2952 | while (s < e) { |
| 2953 | unsigned char ch = *s; |
| 2954 | PyObject *w, *x; |
| 2955 | |
| 2956 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 2957 | w = PyInt_FromLong((long)ch); |
| 2958 | if (w == NULL) |
| 2959 | goto onError; |
| 2960 | x = PyObject_GetItem(mapping, w); |
| 2961 | Py_DECREF(w); |
| 2962 | if (x == NULL) { |
| 2963 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 2964 | /* No mapping found means: mapping is undefined. */ |
| 2965 | PyErr_Clear(); |
| 2966 | x = Py_None; |
| 2967 | Py_INCREF(x); |
| 2968 | } else |
| 2969 | goto onError; |
| 2970 | } |
| 2971 | |
| 2972 | /* Apply mapping */ |
| 2973 | if (PyInt_Check(x)) { |
| 2974 | long value = PyInt_AS_LONG(x); |
| 2975 | if (value < 0 || value > 65535) { |
| 2976 | PyErr_SetString(PyExc_TypeError, |
| 2977 | "character mapping must be in range(65536)"); |
| 2978 | Py_DECREF(x); |
| 2979 | goto onError; |
| 2980 | } |
| 2981 | *p++ = (Py_UNICODE)value; |
| 2982 | } |
| 2983 | else if (x == Py_None) { |
| 2984 | /* undefined mapping */ |
| 2985 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2986 | startinpos = s-starts; |
| 2987 | endinpos = startinpos+1; |
| 2988 | if (unicode_decode_call_errorhandler( |
| 2989 | errors, &errorHandler, |
| 2990 | "charmap", "character maps to <undefined>", |
| 2991 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2992 | (PyObject **)&v, &outpos, &p)) { |
| 2993 | Py_DECREF(x); |
| 2994 | goto onError; |
| 2995 | } |
Walter Dörwald | d4fff17 | 2005-11-28 22:15:56 +0000 | [diff] [blame] | 2996 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2997 | continue; |
| 2998 | } |
| 2999 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3000 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3001 | |
| 3002 | if (targetsize == 1) |
| 3003 | /* 1-1 mapping */ |
| 3004 | *p++ = *PyUnicode_AS_UNICODE(x); |
| 3005 | |
| 3006 | else if (targetsize > 1) { |
| 3007 | /* 1-n mapping */ |
| 3008 | if (targetsize > extrachars) { |
| 3009 | /* resize first */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3010 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 3011 | Py_ssize_t needed = (targetsize - extrachars) + \ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3012 | (targetsize << 2); |
| 3013 | extrachars += needed; |
| 3014 | if (_PyUnicode_Resize(&v, |
| 3015 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 3016 | Py_DECREF(x); |
| 3017 | goto onError; |
| 3018 | } |
| 3019 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 3020 | } |
| 3021 | Py_UNICODE_COPY(p, |
| 3022 | PyUnicode_AS_UNICODE(x), |
| 3023 | targetsize); |
| 3024 | p += targetsize; |
| 3025 | extrachars -= targetsize; |
| 3026 | } |
| 3027 | /* 1-0 mapping: skip the character */ |
| 3028 | } |
| 3029 | else { |
| 3030 | /* wrong return value */ |
| 3031 | PyErr_SetString(PyExc_TypeError, |
| 3032 | "character mapping must return integer, None or unicode"); |
| 3033 | Py_DECREF(x); |
| 3034 | goto onError; |
| 3035 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3036 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3037 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3038 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3039 | } |
| 3040 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3041 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3042 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3043 | Py_XDECREF(errorHandler); |
| 3044 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3045 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3046 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3047 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3048 | Py_XDECREF(errorHandler); |
| 3049 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3050 | Py_XDECREF(v); |
| 3051 | return NULL; |
| 3052 | } |
| 3053 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3054 | /* Lookup the character ch in the mapping. If the character |
| 3055 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3056 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3057 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3058 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3059 | PyObject *w = PyInt_FromLong((long)c); |
| 3060 | PyObject *x; |
| 3061 | |
| 3062 | if (w == NULL) |
| 3063 | return NULL; |
| 3064 | x = PyObject_GetItem(mapping, w); |
| 3065 | Py_DECREF(w); |
| 3066 | if (x == NULL) { |
| 3067 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3068 | /* No mapping found means: mapping is undefined. */ |
| 3069 | PyErr_Clear(); |
| 3070 | x = Py_None; |
| 3071 | Py_INCREF(x); |
| 3072 | return x; |
| 3073 | } else |
| 3074 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3075 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 3076 | else if (x == Py_None) |
| 3077 | return x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3078 | else if (PyInt_Check(x)) { |
| 3079 | long value = PyInt_AS_LONG(x); |
| 3080 | if (value < 0 || value > 255) { |
| 3081 | PyErr_SetString(PyExc_TypeError, |
| 3082 | "character mapping must be in range(256)"); |
| 3083 | Py_DECREF(x); |
| 3084 | return NULL; |
| 3085 | } |
| 3086 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3087 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3088 | else if (PyString_Check(x)) |
| 3089 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3090 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3091 | /* wrong return value */ |
| 3092 | PyErr_SetString(PyExc_TypeError, |
| 3093 | "character mapping must return integer, None or str"); |
| 3094 | Py_DECREF(x); |
| 3095 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3096 | } |
| 3097 | } |
| 3098 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3099 | /* lookup the character, put the result in the output string and adjust |
| 3100 | various state variables. Reallocate the output string if not enough |
| 3101 | space is available. Return a new reference to the object that |
| 3102 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 3103 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 3104 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3105 | static |
| 3106 | PyObject *charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3107 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3108 | { |
| 3109 | PyObject *rep = charmapencode_lookup(c, mapping); |
| 3110 | |
| 3111 | if (rep==NULL) |
| 3112 | return NULL; |
| 3113 | else if (rep==Py_None) |
| 3114 | return rep; |
| 3115 | else { |
| 3116 | char *outstart = PyString_AS_STRING(*outobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3117 | Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3118 | if (PyInt_Check(rep)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3119 | Py_ssize_t requiredsize = *outpos+1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3120 | if (outsize<requiredsize) { |
| 3121 | /* exponentially overallocate to minimize reallocations */ |
| 3122 | if (requiredsize < 2*outsize) |
| 3123 | requiredsize = 2*outsize; |
| 3124 | if (_PyString_Resize(outobj, requiredsize)) { |
| 3125 | Py_DECREF(rep); |
| 3126 | return NULL; |
| 3127 | } |
| 3128 | outstart = PyString_AS_STRING(*outobj); |
| 3129 | } |
| 3130 | outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); |
| 3131 | } |
| 3132 | else { |
| 3133 | const char *repchars = PyString_AS_STRING(rep); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3134 | Py_ssize_t repsize = PyString_GET_SIZE(rep); |
| 3135 | Py_ssize_t requiredsize = *outpos+repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3136 | if (outsize<requiredsize) { |
| 3137 | /* exponentially overallocate to minimize reallocations */ |
| 3138 | if (requiredsize < 2*outsize) |
| 3139 | requiredsize = 2*outsize; |
| 3140 | if (_PyString_Resize(outobj, requiredsize)) { |
| 3141 | Py_DECREF(rep); |
| 3142 | return NULL; |
| 3143 | } |
| 3144 | outstart = PyString_AS_STRING(*outobj); |
| 3145 | } |
| 3146 | memcpy(outstart + *outpos, repchars, repsize); |
| 3147 | *outpos += repsize; |
| 3148 | } |
| 3149 | } |
| 3150 | return rep; |
| 3151 | } |
| 3152 | |
| 3153 | /* handle an error in PyUnicode_EncodeCharmap |
| 3154 | Return 0 on success, -1 on error */ |
| 3155 | static |
| 3156 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3157 | 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] | 3158 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3159 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3160 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3161 | { |
| 3162 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3163 | Py_ssize_t repsize; |
| 3164 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3165 | Py_UNICODE *uni2; |
| 3166 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3167 | Py_ssize_t collstartpos = *inpos; |
| 3168 | Py_ssize_t collendpos = *inpos+1; |
| 3169 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3170 | char *encoding = "charmap"; |
| 3171 | char *reason = "character maps to <undefined>"; |
| 3172 | |
| 3173 | PyObject *x; |
| 3174 | /* find all unencodable characters */ |
| 3175 | while (collendpos < size) { |
| 3176 | x = charmapencode_lookup(p[collendpos], mapping); |
| 3177 | if (x==NULL) |
| 3178 | return -1; |
| 3179 | else if (x!=Py_None) { |
| 3180 | Py_DECREF(x); |
| 3181 | break; |
| 3182 | } |
| 3183 | Py_DECREF(x); |
| 3184 | ++collendpos; |
| 3185 | } |
| 3186 | /* cache callback name lookup |
| 3187 | * (if not done yet, i.e. it's the first error) */ |
| 3188 | if (*known_errorHandler==-1) { |
| 3189 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3190 | *known_errorHandler = 1; |
| 3191 | else if (!strcmp(errors, "replace")) |
| 3192 | *known_errorHandler = 2; |
| 3193 | else if (!strcmp(errors, "ignore")) |
| 3194 | *known_errorHandler = 3; |
| 3195 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3196 | *known_errorHandler = 4; |
| 3197 | else |
| 3198 | *known_errorHandler = 0; |
| 3199 | } |
| 3200 | switch (*known_errorHandler) { |
| 3201 | case 1: /* strict */ |
| 3202 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3203 | return -1; |
| 3204 | case 2: /* replace */ |
| 3205 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 3206 | x = charmapencode_output('?', mapping, res, respos); |
| 3207 | if (x==NULL) { |
| 3208 | return -1; |
| 3209 | } |
| 3210 | else if (x==Py_None) { |
| 3211 | Py_DECREF(x); |
| 3212 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3213 | return -1; |
| 3214 | } |
| 3215 | Py_DECREF(x); |
| 3216 | } |
| 3217 | /* fall through */ |
| 3218 | case 3: /* ignore */ |
| 3219 | *inpos = collendpos; |
| 3220 | break; |
| 3221 | case 4: /* xmlcharrefreplace */ |
| 3222 | /* generate replacement (temporarily (mis)uses p) */ |
| 3223 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 3224 | char buffer[2+29+1+1]; |
| 3225 | char *cp; |
| 3226 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 3227 | for (cp = buffer; *cp; ++cp) { |
| 3228 | x = charmapencode_output(*cp, mapping, res, respos); |
| 3229 | if (x==NULL) |
| 3230 | return -1; |
| 3231 | else if (x==Py_None) { |
| 3232 | Py_DECREF(x); |
| 3233 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3234 | return -1; |
| 3235 | } |
| 3236 | Py_DECREF(x); |
| 3237 | } |
| 3238 | } |
| 3239 | *inpos = collendpos; |
| 3240 | break; |
| 3241 | default: |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3242 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3243 | encoding, reason, p, size, exceptionObject, |
| 3244 | collstartpos, collendpos, &newpos); |
| 3245 | if (repunicode == NULL) |
| 3246 | return -1; |
| 3247 | /* generate replacement */ |
| 3248 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3249 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 3250 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 3251 | if (x==NULL) { |
| 3252 | Py_DECREF(repunicode); |
| 3253 | return -1; |
| 3254 | } |
| 3255 | else if (x==Py_None) { |
| 3256 | Py_DECREF(repunicode); |
| 3257 | Py_DECREF(x); |
| 3258 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3259 | return -1; |
| 3260 | } |
| 3261 | Py_DECREF(x); |
| 3262 | } |
| 3263 | *inpos = newpos; |
| 3264 | Py_DECREF(repunicode); |
| 3265 | } |
| 3266 | return 0; |
| 3267 | } |
| 3268 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3269 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3270 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3271 | PyObject *mapping, |
| 3272 | const char *errors) |
| 3273 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3274 | /* output object */ |
| 3275 | PyObject *res = NULL; |
| 3276 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3277 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3278 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3279 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3280 | PyObject *errorHandler = NULL; |
| 3281 | PyObject *exc = NULL; |
| 3282 | /* the following variable is used for caching string comparisons |
| 3283 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 3284 | * 3=ignore, 4=xmlcharrefreplace */ |
| 3285 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3286 | |
| 3287 | /* Default to Latin-1 */ |
| 3288 | if (mapping == NULL) |
| 3289 | return PyUnicode_EncodeLatin1(p, size, errors); |
| 3290 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3291 | /* allocate enough for a simple encoding without |
| 3292 | replacements, if we need more, we'll resize */ |
| 3293 | res = PyString_FromStringAndSize(NULL, size); |
| 3294 | if (res == NULL) |
| 3295 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3296 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3297 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3298 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3299 | while (inpos<size) { |
| 3300 | /* try to encode it */ |
| 3301 | PyObject *x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 3302 | if (x==NULL) /* error */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3303 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3304 | if (x==Py_None) { /* unencodable character */ |
| 3305 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 3306 | &exc, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3307 | &known_errorHandler, &errorHandler, errors, |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 3308 | &res, &respos)) { |
| 3309 | Py_DECREF(x); |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3310 | goto onError; |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 3311 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3312 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3313 | else |
| 3314 | /* done with this character => adjust input position */ |
| 3315 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3316 | Py_DECREF(x); |
| 3317 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3318 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3319 | /* Resize if we allocated to much */ |
| 3320 | if (respos<PyString_GET_SIZE(res)) { |
| 3321 | if (_PyString_Resize(&res, respos)) |
| 3322 | goto onError; |
| 3323 | } |
| 3324 | Py_XDECREF(exc); |
| 3325 | Py_XDECREF(errorHandler); |
| 3326 | return res; |
| 3327 | |
| 3328 | onError: |
| 3329 | Py_XDECREF(res); |
| 3330 | Py_XDECREF(exc); |
| 3331 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3332 | return NULL; |
| 3333 | } |
| 3334 | |
| 3335 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 3336 | PyObject *mapping) |
| 3337 | { |
| 3338 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 3339 | PyErr_BadArgument(); |
| 3340 | return NULL; |
| 3341 | } |
| 3342 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 3343 | PyUnicode_GET_SIZE(unicode), |
| 3344 | mapping, |
| 3345 | NULL); |
| 3346 | } |
| 3347 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3348 | /* create or adjust a UnicodeTranslateError */ |
| 3349 | static void make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3350 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3351 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3352 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3353 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3354 | if (*exceptionObject == NULL) { |
| 3355 | *exceptionObject = PyUnicodeTranslateError_Create( |
| 3356 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3357 | } |
| 3358 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3359 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 3360 | goto onError; |
| 3361 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 3362 | goto onError; |
| 3363 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 3364 | goto onError; |
| 3365 | return; |
| 3366 | onError: |
| 3367 | Py_DECREF(*exceptionObject); |
| 3368 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3369 | } |
| 3370 | } |
| 3371 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3372 | /* raises a UnicodeTranslateError */ |
| 3373 | static void raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3374 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3375 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3376 | const char *reason) |
| 3377 | { |
| 3378 | make_translate_exception(exceptionObject, |
| 3379 | unicode, size, startpos, endpos, reason); |
| 3380 | if (*exceptionObject != NULL) |
| 3381 | PyCodec_StrictErrors(*exceptionObject); |
| 3382 | } |
| 3383 | |
| 3384 | /* error handling callback helper: |
| 3385 | build arguments, call the callback and check the arguments, |
| 3386 | put the result into newpos and return the replacement string, which |
| 3387 | has to be freed by the caller */ |
| 3388 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 3389 | PyObject **errorHandler, |
| 3390 | const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3391 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 3392 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3393 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3394 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3395 | 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] | 3396 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3397 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3398 | PyObject *restuple; |
| 3399 | PyObject *resunicode; |
| 3400 | |
| 3401 | if (*errorHandler == NULL) { |
| 3402 | *errorHandler = PyCodec_LookupError(errors); |
| 3403 | if (*errorHandler == NULL) |
| 3404 | return NULL; |
| 3405 | } |
| 3406 | |
| 3407 | make_translate_exception(exceptionObject, |
| 3408 | unicode, size, startpos, endpos, reason); |
| 3409 | if (*exceptionObject == NULL) |
| 3410 | return NULL; |
| 3411 | |
| 3412 | restuple = PyObject_CallFunctionObjArgs( |
| 3413 | *errorHandler, *exceptionObject, NULL); |
| 3414 | if (restuple == NULL) |
| 3415 | return NULL; |
| 3416 | if (!PyTuple_Check(restuple)) { |
| 3417 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 3418 | Py_DECREF(restuple); |
| 3419 | return NULL; |
| 3420 | } |
| 3421 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3422 | &resunicode, &i_newpos)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3423 | Py_DECREF(restuple); |
| 3424 | return NULL; |
| 3425 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3426 | if (i_newpos<0) |
| 3427 | *newpos = size+i_newpos; |
| 3428 | else |
| 3429 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3430 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 3431 | 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] | 3432 | Py_DECREF(restuple); |
| 3433 | return NULL; |
| 3434 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3435 | Py_INCREF(resunicode); |
| 3436 | Py_DECREF(restuple); |
| 3437 | return resunicode; |
| 3438 | } |
| 3439 | |
| 3440 | /* Lookup the character ch in the mapping and put the result in result, |
| 3441 | which must be decrefed by the caller. |
| 3442 | Return 0 on success, -1 on error */ |
| 3443 | static |
| 3444 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 3445 | { |
| 3446 | PyObject *w = PyInt_FromLong((long)c); |
| 3447 | PyObject *x; |
| 3448 | |
| 3449 | if (w == NULL) |
| 3450 | return -1; |
| 3451 | x = PyObject_GetItem(mapping, w); |
| 3452 | Py_DECREF(w); |
| 3453 | if (x == NULL) { |
| 3454 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3455 | /* No mapping found means: use 1:1 mapping. */ |
| 3456 | PyErr_Clear(); |
| 3457 | *result = NULL; |
| 3458 | return 0; |
| 3459 | } else |
| 3460 | return -1; |
| 3461 | } |
| 3462 | else if (x == Py_None) { |
| 3463 | *result = x; |
| 3464 | return 0; |
| 3465 | } |
| 3466 | else if (PyInt_Check(x)) { |
| 3467 | long value = PyInt_AS_LONG(x); |
| 3468 | long max = PyUnicode_GetMax(); |
| 3469 | if (value < 0 || value > max) { |
| 3470 | PyErr_Format(PyExc_TypeError, |
| 3471 | "character mapping must be in range(0x%lx)", max+1); |
| 3472 | Py_DECREF(x); |
| 3473 | return -1; |
| 3474 | } |
| 3475 | *result = x; |
| 3476 | return 0; |
| 3477 | } |
| 3478 | else if (PyUnicode_Check(x)) { |
| 3479 | *result = x; |
| 3480 | return 0; |
| 3481 | } |
| 3482 | else { |
| 3483 | /* wrong return value */ |
| 3484 | PyErr_SetString(PyExc_TypeError, |
| 3485 | "character mapping must return integer, None or unicode"); |
Walter Dörwald | 150523e | 2003-08-15 16:52:19 +0000 | [diff] [blame] | 3486 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3487 | return -1; |
| 3488 | } |
| 3489 | } |
| 3490 | /* ensure that *outobj is at least requiredsize characters long, |
| 3491 | if not reallocate and adjust various state variables. |
| 3492 | Return 0 on success, -1 on error */ |
| 3493 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3494 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3495 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3496 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3497 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3498 | if (requiredsize > oldsize) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3499 | /* remember old output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3500 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3501 | /* exponentially overallocate to minimize reallocations */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3502 | if (requiredsize < 2 * oldsize) |
| 3503 | requiredsize = 2 * oldsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3504 | if (_PyUnicode_Resize(outobj, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3505 | return -1; |
| 3506 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3507 | } |
| 3508 | return 0; |
| 3509 | } |
| 3510 | /* lookup the character, put the result in the output string and adjust |
| 3511 | various state variables. Return a new reference to the object that |
| 3512 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 3513 | undefined (in which case no character was written). |
| 3514 | The called must decref result. |
| 3515 | Return 0 on success, -1 on error. */ |
| 3516 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3517 | 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] | 3518 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3519 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3520 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3521 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3522 | return -1; |
| 3523 | if (*res==NULL) { |
| 3524 | /* not found => default to 1:1 mapping */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3525 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3526 | } |
| 3527 | else if (*res==Py_None) |
| 3528 | ; |
| 3529 | else if (PyInt_Check(*res)) { |
| 3530 | /* no overflow check, because we know that the space is enough */ |
| 3531 | *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); |
| 3532 | } |
| 3533 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3534 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3535 | if (repsize==1) { |
| 3536 | /* no overflow check, because we know that the space is enough */ |
| 3537 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 3538 | } |
| 3539 | else if (repsize!=0) { |
| 3540 | /* more than one character */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3541 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
Walter Dörwald | cd736e7 | 2004-02-05 17:36:00 +0000 | [diff] [blame] | 3542 | (insize - (curinp-startinp)) + |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3543 | repsize - 1; |
| 3544 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3545 | return -1; |
| 3546 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 3547 | *outp += repsize; |
| 3548 | } |
| 3549 | } |
| 3550 | else |
| 3551 | return -1; |
| 3552 | return 0; |
| 3553 | } |
| 3554 | |
| 3555 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3556 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3557 | PyObject *mapping, |
| 3558 | const char *errors) |
| 3559 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3560 | /* output object */ |
| 3561 | PyObject *res = NULL; |
| 3562 | /* pointers to the beginning and end+1 of input */ |
| 3563 | const Py_UNICODE *startp = p; |
| 3564 | const Py_UNICODE *endp = p + size; |
| 3565 | /* pointer into the output */ |
| 3566 | Py_UNICODE *str; |
| 3567 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3568 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3569 | char *reason = "character maps to <undefined>"; |
| 3570 | PyObject *errorHandler = NULL; |
| 3571 | PyObject *exc = NULL; |
| 3572 | /* the following variable is used for caching string comparisons |
| 3573 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 3574 | * 3=ignore, 4=xmlcharrefreplace */ |
| 3575 | int known_errorHandler = -1; |
| 3576 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3577 | if (mapping == NULL) { |
| 3578 | PyErr_BadArgument(); |
| 3579 | return NULL; |
| 3580 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3581 | |
| 3582 | /* allocate enough for a simple 1:1 translation without |
| 3583 | replacements, if we need more, we'll resize */ |
| 3584 | res = PyUnicode_FromUnicode(NULL, size); |
| 3585 | if (res == NULL) |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3586 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3587 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3588 | return res; |
| 3589 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3590 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3591 | while (p<endp) { |
| 3592 | /* try to encode it */ |
| 3593 | PyObject *x = NULL; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3594 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3595 | Py_XDECREF(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3596 | goto onError; |
| 3597 | } |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 3598 | Py_XDECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3599 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 3600 | ++p; |
| 3601 | else { /* untranslatable character */ |
| 3602 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3603 | Py_ssize_t repsize; |
| 3604 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3605 | Py_UNICODE *uni2; |
| 3606 | /* startpos for collecting untranslatable chars */ |
| 3607 | const Py_UNICODE *collstart = p; |
| 3608 | const Py_UNICODE *collend = p+1; |
| 3609 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3610 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3611 | /* find all untranslatable characters */ |
| 3612 | while (collend < endp) { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3613 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3614 | goto onError; |
| 3615 | Py_XDECREF(x); |
| 3616 | if (x!=Py_None) |
| 3617 | break; |
| 3618 | ++collend; |
| 3619 | } |
| 3620 | /* cache callback name lookup |
| 3621 | * (if not done yet, i.e. it's the first error) */ |
| 3622 | if (known_errorHandler==-1) { |
| 3623 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3624 | known_errorHandler = 1; |
| 3625 | else if (!strcmp(errors, "replace")) |
| 3626 | known_errorHandler = 2; |
| 3627 | else if (!strcmp(errors, "ignore")) |
| 3628 | known_errorHandler = 3; |
| 3629 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3630 | known_errorHandler = 4; |
| 3631 | else |
| 3632 | known_errorHandler = 0; |
| 3633 | } |
| 3634 | switch (known_errorHandler) { |
| 3635 | case 1: /* strict */ |
| 3636 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
| 3637 | goto onError; |
| 3638 | case 2: /* replace */ |
| 3639 | /* No need to check for space, this is a 1:1 replacement */ |
| 3640 | for (coll = collstart; coll<collend; ++coll) |
| 3641 | *str++ = '?'; |
| 3642 | /* fall through */ |
| 3643 | case 3: /* ignore */ |
| 3644 | p = collend; |
| 3645 | break; |
| 3646 | case 4: /* xmlcharrefreplace */ |
| 3647 | /* generate replacement (temporarily (mis)uses p) */ |
| 3648 | for (p = collstart; p < collend; ++p) { |
| 3649 | char buffer[2+29+1+1]; |
| 3650 | char *cp; |
| 3651 | sprintf(buffer, "&#%d;", (int)*p); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3652 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3653 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 3654 | goto onError; |
| 3655 | for (cp = buffer; *cp; ++cp) |
| 3656 | *str++ = *cp; |
| 3657 | } |
| 3658 | p = collend; |
| 3659 | break; |
| 3660 | default: |
| 3661 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 3662 | reason, startp, size, &exc, |
| 3663 | collstart-startp, collend-startp, &newpos); |
| 3664 | if (repunicode == NULL) |
| 3665 | goto onError; |
| 3666 | /* generate replacement */ |
| 3667 | repsize = PyUnicode_GET_SIZE(repunicode); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3668 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3669 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 3670 | Py_DECREF(repunicode); |
| 3671 | goto onError; |
| 3672 | } |
| 3673 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 3674 | *str++ = *uni2; |
| 3675 | p = startp + newpos; |
| 3676 | Py_DECREF(repunicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3677 | } |
| 3678 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3679 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3680 | /* Resize if we allocated to much */ |
| 3681 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3682 | if (respos<PyUnicode_GET_SIZE(res)) { |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3683 | if (_PyUnicode_Resize(&res, respos) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3684 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3685 | } |
| 3686 | Py_XDECREF(exc); |
| 3687 | Py_XDECREF(errorHandler); |
| 3688 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3689 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3690 | onError: |
| 3691 | Py_XDECREF(res); |
| 3692 | Py_XDECREF(exc); |
| 3693 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3694 | return NULL; |
| 3695 | } |
| 3696 | |
| 3697 | PyObject *PyUnicode_Translate(PyObject *str, |
| 3698 | PyObject *mapping, |
| 3699 | const char *errors) |
| 3700 | { |
| 3701 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3702 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3703 | str = PyUnicode_FromObject(str); |
| 3704 | if (str == NULL) |
| 3705 | goto onError; |
| 3706 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 3707 | PyUnicode_GET_SIZE(str), |
| 3708 | mapping, |
| 3709 | errors); |
| 3710 | Py_DECREF(str); |
| 3711 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3712 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3713 | onError: |
| 3714 | Py_XDECREF(str); |
| 3715 | return NULL; |
| 3716 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3717 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3718 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 3719 | |
| 3720 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3721 | Py_ssize_t length, |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3722 | char *output, |
| 3723 | const char *errors) |
| 3724 | { |
| 3725 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3726 | PyObject *errorHandler = NULL; |
| 3727 | PyObject *exc = NULL; |
| 3728 | const char *encoding = "decimal"; |
| 3729 | const char *reason = "invalid decimal Unicode string"; |
| 3730 | /* the following variable is used for caching string comparisons |
| 3731 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 3732 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3733 | |
| 3734 | if (output == NULL) { |
| 3735 | PyErr_BadArgument(); |
| 3736 | return -1; |
| 3737 | } |
| 3738 | |
| 3739 | p = s; |
| 3740 | end = s + length; |
| 3741 | while (p < end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3742 | register Py_UNICODE ch = *p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3743 | int decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3744 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3745 | Py_ssize_t repsize; |
| 3746 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3747 | Py_UNICODE *uni2; |
| 3748 | Py_UNICODE *collstart; |
| 3749 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3750 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3751 | if (Py_UNICODE_ISSPACE(ch)) { |
| 3752 | *output++ = ' '; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3753 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3754 | continue; |
| 3755 | } |
| 3756 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 3757 | if (decimal >= 0) { |
| 3758 | *output++ = '0' + decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3759 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3760 | continue; |
| 3761 | } |
Guido van Rossum | ba47704 | 2000-04-06 18:18:10 +0000 | [diff] [blame] | 3762 | if (0 < ch && ch < 256) { |
Guido van Rossum | 42c29aa | 2000-05-03 23:58:29 +0000 | [diff] [blame] | 3763 | *output++ = (char)ch; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3764 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3765 | continue; |
| 3766 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3767 | /* All other characters are considered unencodable */ |
| 3768 | collstart = p; |
| 3769 | collend = p+1; |
| 3770 | while (collend < end) { |
| 3771 | if ((0 < *collend && *collend < 256) || |
| 3772 | !Py_UNICODE_ISSPACE(*collend) || |
| 3773 | Py_UNICODE_TODECIMAL(*collend)) |
| 3774 | break; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3775 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3776 | /* cache callback name lookup |
| 3777 | * (if not done yet, i.e. it's the first error) */ |
| 3778 | if (known_errorHandler==-1) { |
| 3779 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3780 | known_errorHandler = 1; |
| 3781 | else if (!strcmp(errors, "replace")) |
| 3782 | known_errorHandler = 2; |
| 3783 | else if (!strcmp(errors, "ignore")) |
| 3784 | known_errorHandler = 3; |
| 3785 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3786 | known_errorHandler = 4; |
| 3787 | else |
| 3788 | known_errorHandler = 0; |
| 3789 | } |
| 3790 | switch (known_errorHandler) { |
| 3791 | case 1: /* strict */ |
| 3792 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 3793 | goto onError; |
| 3794 | case 2: /* replace */ |
| 3795 | for (p = collstart; p < collend; ++p) |
| 3796 | *output++ = '?'; |
| 3797 | /* fall through */ |
| 3798 | case 3: /* ignore */ |
| 3799 | p = collend; |
| 3800 | break; |
| 3801 | case 4: /* xmlcharrefreplace */ |
| 3802 | /* generate replacement (temporarily (mis)uses p) */ |
| 3803 | for (p = collstart; p < collend; ++p) |
| 3804 | output += sprintf(output, "&#%d;", (int)*p); |
| 3805 | p = collend; |
| 3806 | break; |
| 3807 | default: |
| 3808 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 3809 | encoding, reason, s, length, &exc, |
| 3810 | collstart-s, collend-s, &newpos); |
| 3811 | if (repunicode == NULL) |
| 3812 | goto onError; |
| 3813 | /* generate replacement */ |
| 3814 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3815 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 3816 | Py_UNICODE ch = *uni2; |
| 3817 | if (Py_UNICODE_ISSPACE(ch)) |
| 3818 | *output++ = ' '; |
| 3819 | else { |
| 3820 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 3821 | if (decimal >= 0) |
| 3822 | *output++ = '0' + decimal; |
| 3823 | else if (0 < ch && ch < 256) |
| 3824 | *output++ = (char)ch; |
| 3825 | else { |
| 3826 | Py_DECREF(repunicode); |
| 3827 | raise_encode_exception(&exc, encoding, |
| 3828 | s, length, collstart-s, collend-s, reason); |
| 3829 | goto onError; |
| 3830 | } |
| 3831 | } |
| 3832 | } |
| 3833 | p = s + newpos; |
| 3834 | Py_DECREF(repunicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3835 | } |
| 3836 | } |
| 3837 | /* 0-terminate the output string */ |
| 3838 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3839 | Py_XDECREF(exc); |
| 3840 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3841 | return 0; |
| 3842 | |
| 3843 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3844 | Py_XDECREF(exc); |
| 3845 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3846 | return -1; |
| 3847 | } |
| 3848 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3849 | /* --- Helpers ------------------------------------------------------------ */ |
| 3850 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 3851 | static Py_ssize_t count(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3852 | Py_ssize_t start, |
| 3853 | Py_ssize_t end, |
| 3854 | PyUnicodeObject *substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3855 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3856 | Py_ssize_t count = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3857 | |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3858 | if (start < 0) |
| 3859 | start += self->length; |
| 3860 | if (start < 0) |
| 3861 | start = 0; |
| 3862 | if (end > self->length) |
| 3863 | end = self->length; |
| 3864 | if (end < 0) |
| 3865 | end += self->length; |
| 3866 | if (end < 0) |
| 3867 | end = 0; |
| 3868 | |
Marc-André Lemburg | 49ef6dc | 2000-06-18 22:25:22 +0000 | [diff] [blame] | 3869 | if (substring->length == 0) |
| 3870 | return (end - start + 1); |
| 3871 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3872 | end -= substring->length; |
| 3873 | |
| 3874 | while (start <= end) |
| 3875 | if (Py_UNICODE_MATCH(self, start, substring)) { |
| 3876 | count++; |
| 3877 | start += substring->length; |
| 3878 | } else |
| 3879 | start++; |
| 3880 | |
| 3881 | return count; |
| 3882 | } |
| 3883 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3884 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3885 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3886 | Py_ssize_t start, |
| 3887 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3888 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3889 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3890 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3891 | str = PyUnicode_FromObject(str); |
| 3892 | if (str == NULL) |
| 3893 | return -1; |
| 3894 | substr = PyUnicode_FromObject(substr); |
| 3895 | if (substr == NULL) { |
Marc-André Lemburg | 49ef6dc | 2000-06-18 22:25:22 +0000 | [diff] [blame] | 3896 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3897 | return -1; |
| 3898 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3899 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3900 | result = count((PyUnicodeObject *)str, |
| 3901 | start, end, |
| 3902 | (PyUnicodeObject *)substr); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3903 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3904 | Py_DECREF(str); |
| 3905 | Py_DECREF(substr); |
| 3906 | return result; |
| 3907 | } |
| 3908 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 3909 | static Py_ssize_t findstring(PyUnicodeObject *self, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3910 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3911 | Py_ssize_t start, |
| 3912 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3913 | int direction) |
| 3914 | { |
| 3915 | if (start < 0) |
| 3916 | start += self->length; |
| 3917 | if (start < 0) |
| 3918 | start = 0; |
| 3919 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3920 | if (end > self->length) |
| 3921 | end = self->length; |
| 3922 | if (end < 0) |
| 3923 | end += self->length; |
| 3924 | if (end < 0) |
| 3925 | end = 0; |
| 3926 | |
Guido van Rossum | 76afbd9 | 2002-08-20 17:29:29 +0000 | [diff] [blame] | 3927 | if (substring->length == 0) |
| 3928 | return (direction > 0) ? start : end; |
| 3929 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3930 | end -= substring->length; |
| 3931 | |
| 3932 | if (direction < 0) { |
| 3933 | for (; end >= start; end--) |
| 3934 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 3935 | return end; |
| 3936 | } else { |
| 3937 | for (; start <= end; start++) |
| 3938 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 3939 | return start; |
| 3940 | } |
| 3941 | |
| 3942 | return -1; |
| 3943 | } |
| 3944 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3945 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3946 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3947 | Py_ssize_t start, |
| 3948 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3949 | int direction) |
| 3950 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3951 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3952 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3953 | str = PyUnicode_FromObject(str); |
| 3954 | if (str == NULL) |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 3955 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3956 | substr = PyUnicode_FromObject(substr); |
| 3957 | if (substr == NULL) { |
Marc-André Lemburg | 4164439 | 2002-05-29 13:46:29 +0000 | [diff] [blame] | 3958 | Py_DECREF(str); |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 3959 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3960 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3961 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3962 | result = findstring((PyUnicodeObject *)str, |
| 3963 | (PyUnicodeObject *)substr, |
| 3964 | start, end, direction); |
| 3965 | Py_DECREF(str); |
| 3966 | Py_DECREF(substr); |
| 3967 | return result; |
| 3968 | } |
| 3969 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3970 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3971 | int tailmatch(PyUnicodeObject *self, |
| 3972 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3973 | Py_ssize_t start, |
| 3974 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3975 | int direction) |
| 3976 | { |
| 3977 | if (start < 0) |
| 3978 | start += self->length; |
| 3979 | if (start < 0) |
| 3980 | start = 0; |
| 3981 | |
| 3982 | if (substring->length == 0) |
| 3983 | return 1; |
| 3984 | |
| 3985 | if (end > self->length) |
| 3986 | end = self->length; |
| 3987 | if (end < 0) |
| 3988 | end += self->length; |
| 3989 | if (end < 0) |
| 3990 | end = 0; |
| 3991 | |
| 3992 | end -= substring->length; |
| 3993 | if (end < start) |
| 3994 | return 0; |
| 3995 | |
| 3996 | if (direction > 0) { |
| 3997 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 3998 | return 1; |
| 3999 | } else { |
| 4000 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 4001 | return 1; |
| 4002 | } |
| 4003 | |
| 4004 | return 0; |
| 4005 | } |
| 4006 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4007 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4008 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4009 | Py_ssize_t start, |
| 4010 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4011 | int direction) |
| 4012 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4013 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4014 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4015 | str = PyUnicode_FromObject(str); |
| 4016 | if (str == NULL) |
| 4017 | return -1; |
| 4018 | substr = PyUnicode_FromObject(substr); |
| 4019 | if (substr == NULL) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 4020 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4021 | return -1; |
| 4022 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4023 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4024 | result = tailmatch((PyUnicodeObject *)str, |
| 4025 | (PyUnicodeObject *)substr, |
| 4026 | start, end, direction); |
| 4027 | Py_DECREF(str); |
| 4028 | Py_DECREF(substr); |
| 4029 | return result; |
| 4030 | } |
| 4031 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4032 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4033 | const Py_UNICODE *findchar(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4034 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4035 | Py_UNICODE ch) |
| 4036 | { |
| 4037 | /* like wcschr, but doesn't stop at NULL characters */ |
| 4038 | |
| 4039 | while (size-- > 0) { |
| 4040 | if (*s == ch) |
| 4041 | return s; |
| 4042 | s++; |
| 4043 | } |
| 4044 | |
| 4045 | return NULL; |
| 4046 | } |
| 4047 | |
| 4048 | /* Apply fixfct filter to the Unicode object self and return a |
| 4049 | reference to the modified object */ |
| 4050 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4051 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4052 | PyObject *fixup(PyUnicodeObject *self, |
| 4053 | int (*fixfct)(PyUnicodeObject *s)) |
| 4054 | { |
| 4055 | |
| 4056 | PyUnicodeObject *u; |
| 4057 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4058 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4059 | if (u == NULL) |
| 4060 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4061 | |
| 4062 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 4063 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4064 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4065 | /* fixfct should return TRUE if it modified the buffer. If |
| 4066 | FALSE, return a reference to the original buffer instead |
| 4067 | (to save space, not time) */ |
| 4068 | Py_INCREF(self); |
| 4069 | Py_DECREF(u); |
| 4070 | return (PyObject*) self; |
| 4071 | } |
| 4072 | return (PyObject*) u; |
| 4073 | } |
| 4074 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4075 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4076 | int fixupper(PyUnicodeObject *self) |
| 4077 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4078 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4079 | Py_UNICODE *s = self->str; |
| 4080 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4081 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4082 | while (len-- > 0) { |
| 4083 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4084 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4085 | ch = Py_UNICODE_TOUPPER(*s); |
| 4086 | if (ch != *s) { |
| 4087 | status = 1; |
| 4088 | *s = ch; |
| 4089 | } |
| 4090 | s++; |
| 4091 | } |
| 4092 | |
| 4093 | return status; |
| 4094 | } |
| 4095 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4096 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4097 | int fixlower(PyUnicodeObject *self) |
| 4098 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4099 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4100 | Py_UNICODE *s = self->str; |
| 4101 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4102 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4103 | while (len-- > 0) { |
| 4104 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4105 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4106 | ch = Py_UNICODE_TOLOWER(*s); |
| 4107 | if (ch != *s) { |
| 4108 | status = 1; |
| 4109 | *s = ch; |
| 4110 | } |
| 4111 | s++; |
| 4112 | } |
| 4113 | |
| 4114 | return status; |
| 4115 | } |
| 4116 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4117 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4118 | int fixswapcase(PyUnicodeObject *self) |
| 4119 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4120 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4121 | Py_UNICODE *s = self->str; |
| 4122 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4123 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4124 | while (len-- > 0) { |
| 4125 | if (Py_UNICODE_ISUPPER(*s)) { |
| 4126 | *s = Py_UNICODE_TOLOWER(*s); |
| 4127 | status = 1; |
| 4128 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 4129 | *s = Py_UNICODE_TOUPPER(*s); |
| 4130 | status = 1; |
| 4131 | } |
| 4132 | s++; |
| 4133 | } |
| 4134 | |
| 4135 | return status; |
| 4136 | } |
| 4137 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4138 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4139 | int fixcapitalize(PyUnicodeObject *self) |
| 4140 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4141 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4142 | Py_UNICODE *s = self->str; |
| 4143 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4144 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4145 | if (len == 0) |
| 4146 | return 0; |
| 4147 | if (Py_UNICODE_ISLOWER(*s)) { |
| 4148 | *s = Py_UNICODE_TOUPPER(*s); |
| 4149 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4150 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4151 | s++; |
| 4152 | while (--len > 0) { |
| 4153 | if (Py_UNICODE_ISUPPER(*s)) { |
| 4154 | *s = Py_UNICODE_TOLOWER(*s); |
| 4155 | status = 1; |
| 4156 | } |
| 4157 | s++; |
| 4158 | } |
| 4159 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4160 | } |
| 4161 | |
| 4162 | static |
| 4163 | int fixtitle(PyUnicodeObject *self) |
| 4164 | { |
| 4165 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 4166 | register Py_UNICODE *e; |
| 4167 | int previous_is_cased; |
| 4168 | |
| 4169 | /* Shortcut for single character strings */ |
| 4170 | if (PyUnicode_GET_SIZE(self) == 1) { |
| 4171 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 4172 | if (*p != ch) { |
| 4173 | *p = ch; |
| 4174 | return 1; |
| 4175 | } |
| 4176 | else |
| 4177 | return 0; |
| 4178 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4179 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4180 | e = p + PyUnicode_GET_SIZE(self); |
| 4181 | previous_is_cased = 0; |
| 4182 | for (; p < e; p++) { |
| 4183 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4184 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4185 | if (previous_is_cased) |
| 4186 | *p = Py_UNICODE_TOLOWER(ch); |
| 4187 | else |
| 4188 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4189 | |
| 4190 | if (Py_UNICODE_ISLOWER(ch) || |
| 4191 | Py_UNICODE_ISUPPER(ch) || |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4192 | Py_UNICODE_ISTITLE(ch)) |
| 4193 | previous_is_cased = 1; |
| 4194 | else |
| 4195 | previous_is_cased = 0; |
| 4196 | } |
| 4197 | return 1; |
| 4198 | } |
| 4199 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4200 | PyObject * |
| 4201 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4202 | { |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4203 | PyObject *internal_separator = NULL; |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 4204 | const Py_UNICODE blank = ' '; |
| 4205 | const Py_UNICODE *sep = ␣ |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4206 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4207 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4208 | Py_ssize_t res_alloc = 100; /* # allocated bytes for string in res */ |
| 4209 | Py_ssize_t res_used; /* # used bytes */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4210 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 4211 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4212 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4213 | PyObject *item; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4214 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4215 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4216 | fseq = PySequence_Fast(seq, ""); |
| 4217 | if (fseq == NULL) { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4218 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4219 | } |
| 4220 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4221 | /* Grrrr. A codec may be invoked to convert str objects to |
| 4222 | * Unicode, and so it's possible to call back into Python code |
| 4223 | * during PyUnicode_FromObject(), and so it's possible for a sick |
| 4224 | * codec to change the size of fseq (if seq is a list). Therefore |
| 4225 | * we have to keep refetching the size -- can't assume seqlen |
| 4226 | * is invariant. |
| 4227 | */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4228 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 4229 | /* If empty sequence, return u"". */ |
| 4230 | if (seqlen == 0) { |
| 4231 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 4232 | goto Done; |
| 4233 | } |
| 4234 | /* If singleton sequence with an exact Unicode, return that. */ |
| 4235 | if (seqlen == 1) { |
| 4236 | item = PySequence_Fast_GET_ITEM(fseq, 0); |
| 4237 | if (PyUnicode_CheckExact(item)) { |
| 4238 | Py_INCREF(item); |
| 4239 | res = (PyUnicodeObject *)item; |
| 4240 | goto Done; |
| 4241 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4242 | } |
| 4243 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4244 | /* At least two items to join, or one that isn't exact Unicode. */ |
| 4245 | if (seqlen > 1) { |
| 4246 | /* Set up sep and seplen -- they're needed. */ |
| 4247 | if (separator == NULL) { |
| 4248 | sep = ␣ |
| 4249 | seplen = 1; |
| 4250 | } |
| 4251 | else { |
| 4252 | internal_separator = PyUnicode_FromObject(separator); |
| 4253 | if (internal_separator == NULL) |
| 4254 | goto onError; |
| 4255 | sep = PyUnicode_AS_UNICODE(internal_separator); |
| 4256 | seplen = PyUnicode_GET_SIZE(internal_separator); |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4257 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 4258 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4259 | } |
| 4260 | } |
| 4261 | |
| 4262 | /* Get space. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4263 | res = _PyUnicode_New(res_alloc); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4264 | if (res == NULL) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4265 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4266 | res_p = PyUnicode_AS_UNICODE(res); |
| 4267 | res_used = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4268 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4269 | for (i = 0; i < seqlen; ++i) { |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4270 | Py_ssize_t itemlen; |
| 4271 | Py_ssize_t new_res_used; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4272 | |
| 4273 | item = PySequence_Fast_GET_ITEM(fseq, i); |
| 4274 | /* Convert item to Unicode. */ |
| 4275 | if (! PyUnicode_Check(item) && ! PyString_Check(item)) { |
| 4276 | PyErr_Format(PyExc_TypeError, |
Thomas Wouters | 715a4cd | 2006-04-16 22:04:49 +0000 | [diff] [blame] | 4277 | "sequence item %zd: expected string or Unicode," |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4278 | " %.80s found", |
| 4279 | i, item->ob_type->tp_name); |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 4280 | goto onError; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4281 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4282 | item = PyUnicode_FromObject(item); |
| 4283 | if (item == NULL) |
| 4284 | goto onError; |
| 4285 | /* We own a reference to item from here on. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4286 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4287 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 4288 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 4289 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4290 | /* 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] | 4291 | itemlen = PyUnicode_GET_SIZE(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4292 | new_res_used = res_used + itemlen; |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4293 | if (new_res_used <= 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4294 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4295 | if (i < seqlen - 1) { |
| 4296 | new_res_used += seplen; |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4297 | if (new_res_used <= 0) |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4298 | goto Overflow; |
| 4299 | } |
| 4300 | if (new_res_used > res_alloc) { |
| 4301 | /* double allocated size until it's big enough */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4302 | do { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4303 | res_alloc += res_alloc; |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4304 | if (res_alloc <= 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4305 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4306 | } while (new_res_used > res_alloc); |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4307 | if (_PyUnicode_Resize(&res, res_alloc) < 0) { |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4308 | Py_DECREF(item); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4309 | goto onError; |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4310 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4311 | res_p = PyUnicode_AS_UNICODE(res) + res_used; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4312 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4313 | |
| 4314 | /* Copy item, and maybe the separator. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4315 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4316 | res_p += itemlen; |
| 4317 | if (i < seqlen - 1) { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4318 | Py_UNICODE_COPY(res_p, sep, seplen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4319 | res_p += seplen; |
| 4320 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4321 | Py_DECREF(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4322 | res_used = new_res_used; |
| 4323 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4324 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4325 | /* Shrink res to match the used area; this probably can't fail, |
| 4326 | * but it's cheap to check. |
| 4327 | */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4328 | if (_PyUnicode_Resize(&res, res_used) < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4329 | goto onError; |
| 4330 | |
| 4331 | Done: |
| 4332 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4333 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4334 | return (PyObject *)res; |
| 4335 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4336 | Overflow: |
| 4337 | PyErr_SetString(PyExc_OverflowError, |
| 4338 | "join() is too long for a Python string"); |
| 4339 | Py_DECREF(item); |
| 4340 | /* fall through */ |
| 4341 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4342 | onError: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4343 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4344 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4345 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4346 | return NULL; |
| 4347 | } |
| 4348 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4349 | static |
| 4350 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4351 | Py_ssize_t left, |
| 4352 | Py_ssize_t right, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4353 | Py_UNICODE fill) |
| 4354 | { |
| 4355 | PyUnicodeObject *u; |
| 4356 | |
| 4357 | if (left < 0) |
| 4358 | left = 0; |
| 4359 | if (right < 0) |
| 4360 | right = 0; |
| 4361 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4362 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4363 | Py_INCREF(self); |
| 4364 | return self; |
| 4365 | } |
| 4366 | |
| 4367 | u = _PyUnicode_New(left + self->length + right); |
| 4368 | if (u) { |
| 4369 | if (left) |
| 4370 | Py_UNICODE_FILL(u->str, fill, left); |
| 4371 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 4372 | if (right) |
| 4373 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 4374 | } |
| 4375 | |
| 4376 | return u; |
| 4377 | } |
| 4378 | |
| 4379 | #define SPLIT_APPEND(data, left, right) \ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4380 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4381 | if (!str) \ |
| 4382 | goto onError; \ |
| 4383 | if (PyList_Append(list, str)) { \ |
| 4384 | Py_DECREF(str); \ |
| 4385 | goto onError; \ |
| 4386 | } \ |
| 4387 | else \ |
| 4388 | Py_DECREF(str); |
| 4389 | |
| 4390 | static |
| 4391 | PyObject *split_whitespace(PyUnicodeObject *self, |
| 4392 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4393 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4394 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4395 | register Py_ssize_t i; |
| 4396 | register Py_ssize_t j; |
| 4397 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4398 | PyObject *str; |
| 4399 | |
| 4400 | for (i = j = 0; i < len; ) { |
| 4401 | /* find a token */ |
| 4402 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 4403 | i++; |
| 4404 | j = i; |
| 4405 | while (i < len && !Py_UNICODE_ISSPACE(self->str[i])) |
| 4406 | i++; |
| 4407 | if (j < i) { |
| 4408 | if (maxcount-- <= 0) |
| 4409 | break; |
| 4410 | SPLIT_APPEND(self->str, j, i); |
| 4411 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 4412 | i++; |
| 4413 | j = i; |
| 4414 | } |
| 4415 | } |
| 4416 | if (j < len) { |
| 4417 | SPLIT_APPEND(self->str, j, len); |
| 4418 | } |
| 4419 | return list; |
| 4420 | |
| 4421 | onError: |
| 4422 | Py_DECREF(list); |
| 4423 | return NULL; |
| 4424 | } |
| 4425 | |
| 4426 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4427 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4428 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4429 | register Py_ssize_t i; |
| 4430 | register Py_ssize_t j; |
| 4431 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4432 | PyObject *list; |
| 4433 | PyObject *str; |
| 4434 | Py_UNICODE *data; |
| 4435 | |
| 4436 | string = PyUnicode_FromObject(string); |
| 4437 | if (string == NULL) |
| 4438 | return NULL; |
| 4439 | data = PyUnicode_AS_UNICODE(string); |
| 4440 | len = PyUnicode_GET_SIZE(string); |
| 4441 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4442 | list = PyList_New(0); |
| 4443 | if (!list) |
| 4444 | goto onError; |
| 4445 | |
| 4446 | for (i = j = 0; i < len; ) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4447 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4448 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4449 | /* Find a line and append it */ |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4450 | while (i < len && !BLOOM_LINEBREAK(data[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4451 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4452 | |
| 4453 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4454 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4455 | if (i < len) { |
| 4456 | if (data[i] == '\r' && i + 1 < len && |
| 4457 | data[i+1] == '\n') |
| 4458 | i += 2; |
| 4459 | else |
| 4460 | i++; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4461 | if (keepends) |
| 4462 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4463 | } |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4464 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4465 | j = i; |
| 4466 | } |
| 4467 | if (j < len) { |
| 4468 | SPLIT_APPEND(data, j, len); |
| 4469 | } |
| 4470 | |
| 4471 | Py_DECREF(string); |
| 4472 | return list; |
| 4473 | |
| 4474 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 4475 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4476 | Py_DECREF(string); |
| 4477 | return NULL; |
| 4478 | } |
| 4479 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4480 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4481 | PyObject *split_char(PyUnicodeObject *self, |
| 4482 | PyObject *list, |
| 4483 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4484 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4485 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4486 | register Py_ssize_t i; |
| 4487 | register Py_ssize_t j; |
| 4488 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4489 | PyObject *str; |
| 4490 | |
| 4491 | for (i = j = 0; i < len; ) { |
| 4492 | if (self->str[i] == ch) { |
| 4493 | if (maxcount-- <= 0) |
| 4494 | break; |
| 4495 | SPLIT_APPEND(self->str, j, i); |
| 4496 | i = j = i + 1; |
| 4497 | } else |
| 4498 | i++; |
| 4499 | } |
| 4500 | if (j <= len) { |
| 4501 | SPLIT_APPEND(self->str, j, len); |
| 4502 | } |
| 4503 | return list; |
| 4504 | |
| 4505 | onError: |
| 4506 | Py_DECREF(list); |
| 4507 | return NULL; |
| 4508 | } |
| 4509 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4510 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4511 | PyObject *split_substring(PyUnicodeObject *self, |
| 4512 | PyObject *list, |
| 4513 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4514 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4515 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4516 | register Py_ssize_t i; |
| 4517 | register Py_ssize_t j; |
| 4518 | Py_ssize_t len = self->length; |
| 4519 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4520 | PyObject *str; |
| 4521 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 4522 | for (i = j = 0; i <= len - sublen; ) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4523 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 4524 | if (maxcount-- <= 0) |
| 4525 | break; |
| 4526 | SPLIT_APPEND(self->str, j, i); |
| 4527 | i = j = i + sublen; |
| 4528 | } else |
| 4529 | i++; |
| 4530 | } |
| 4531 | if (j <= len) { |
| 4532 | SPLIT_APPEND(self->str, j, len); |
| 4533 | } |
| 4534 | return list; |
| 4535 | |
| 4536 | onError: |
| 4537 | Py_DECREF(list); |
| 4538 | return NULL; |
| 4539 | } |
| 4540 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4541 | static |
| 4542 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
| 4543 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4544 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4545 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4546 | register Py_ssize_t i; |
| 4547 | register Py_ssize_t j; |
| 4548 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4549 | PyObject *str; |
| 4550 | |
| 4551 | for (i = j = len - 1; i >= 0; ) { |
| 4552 | /* find a token */ |
| 4553 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 4554 | i--; |
| 4555 | j = i; |
| 4556 | while (i >= 0 && !Py_UNICODE_ISSPACE(self->str[i])) |
| 4557 | i--; |
| 4558 | if (j > i) { |
| 4559 | if (maxcount-- <= 0) |
| 4560 | break; |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4561 | SPLIT_APPEND(self->str, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4562 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 4563 | i--; |
| 4564 | j = i; |
| 4565 | } |
| 4566 | } |
| 4567 | if (j >= 0) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4568 | SPLIT_APPEND(self->str, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4569 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4570 | if (PyList_Reverse(list) < 0) |
| 4571 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4572 | return list; |
| 4573 | |
| 4574 | onError: |
| 4575 | Py_DECREF(list); |
| 4576 | return NULL; |
| 4577 | } |
| 4578 | |
| 4579 | static |
| 4580 | PyObject *rsplit_char(PyUnicodeObject *self, |
| 4581 | PyObject *list, |
| 4582 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4583 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4584 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4585 | register Py_ssize_t i; |
| 4586 | register Py_ssize_t j; |
| 4587 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4588 | PyObject *str; |
| 4589 | |
| 4590 | for (i = j = len - 1; i >= 0; ) { |
| 4591 | if (self->str[i] == ch) { |
| 4592 | if (maxcount-- <= 0) |
| 4593 | break; |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4594 | SPLIT_APPEND(self->str, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4595 | j = i = i - 1; |
| 4596 | } else |
| 4597 | i--; |
| 4598 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 4599 | if (j >= -1) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4600 | SPLIT_APPEND(self->str, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4601 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4602 | if (PyList_Reverse(list) < 0) |
| 4603 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4604 | return list; |
| 4605 | |
| 4606 | onError: |
| 4607 | Py_DECREF(list); |
| 4608 | return NULL; |
| 4609 | } |
| 4610 | |
| 4611 | static |
| 4612 | PyObject *rsplit_substring(PyUnicodeObject *self, |
| 4613 | PyObject *list, |
| 4614 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4615 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4616 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4617 | register Py_ssize_t i; |
| 4618 | register Py_ssize_t j; |
| 4619 | Py_ssize_t len = self->length; |
| 4620 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4621 | PyObject *str; |
| 4622 | |
| 4623 | for (i = len - sublen, j = len; i >= 0; ) { |
| 4624 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 4625 | if (maxcount-- <= 0) |
| 4626 | break; |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4627 | SPLIT_APPEND(self->str, i + sublen, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4628 | j = i; |
| 4629 | i -= sublen; |
| 4630 | } else |
| 4631 | i--; |
| 4632 | } |
| 4633 | if (j >= 0) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4634 | SPLIT_APPEND(self->str, 0, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4635 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4636 | if (PyList_Reverse(list) < 0) |
| 4637 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4638 | return list; |
| 4639 | |
| 4640 | onError: |
| 4641 | Py_DECREF(list); |
| 4642 | return NULL; |
| 4643 | } |
| 4644 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4645 | #undef SPLIT_APPEND |
| 4646 | |
| 4647 | static |
| 4648 | PyObject *split(PyUnicodeObject *self, |
| 4649 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4650 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4651 | { |
| 4652 | PyObject *list; |
| 4653 | |
| 4654 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4655 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4656 | |
| 4657 | list = PyList_New(0); |
| 4658 | if (!list) |
| 4659 | return NULL; |
| 4660 | |
| 4661 | if (substring == NULL) |
| 4662 | return split_whitespace(self,list,maxcount); |
| 4663 | |
| 4664 | else if (substring->length == 1) |
| 4665 | return split_char(self,list,substring->str[0],maxcount); |
| 4666 | |
| 4667 | else if (substring->length == 0) { |
| 4668 | Py_DECREF(list); |
| 4669 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 4670 | return NULL; |
| 4671 | } |
| 4672 | else |
| 4673 | return split_substring(self,list,substring,maxcount); |
| 4674 | } |
| 4675 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4676 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4677 | PyObject *rsplit(PyUnicodeObject *self, |
| 4678 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4679 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4680 | { |
| 4681 | PyObject *list; |
| 4682 | |
| 4683 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4684 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4685 | |
| 4686 | list = PyList_New(0); |
| 4687 | if (!list) |
| 4688 | return NULL; |
| 4689 | |
| 4690 | if (substring == NULL) |
| 4691 | return rsplit_whitespace(self,list,maxcount); |
| 4692 | |
| 4693 | else if (substring->length == 1) |
| 4694 | return rsplit_char(self,list,substring->str[0],maxcount); |
| 4695 | |
| 4696 | else if (substring->length == 0) { |
| 4697 | Py_DECREF(list); |
| 4698 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 4699 | return NULL; |
| 4700 | } |
| 4701 | else |
| 4702 | return rsplit_substring(self,list,substring,maxcount); |
| 4703 | } |
| 4704 | |
| 4705 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4706 | PyObject *replace(PyUnicodeObject *self, |
| 4707 | PyUnicodeObject *str1, |
| 4708 | PyUnicodeObject *str2, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4709 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4710 | { |
| 4711 | PyUnicodeObject *u; |
| 4712 | |
| 4713 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4714 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4715 | |
| 4716 | if (str1->length == 1 && str2->length == 1) { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4717 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4718 | |
| 4719 | /* replace characters */ |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4720 | if (!findchar(self->str, self->length, str1->str[0]) && |
| 4721 | PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4722 | /* nothing to replace, return original string */ |
| 4723 | Py_INCREF(self); |
| 4724 | u = self; |
| 4725 | } else { |
| 4726 | Py_UNICODE u1 = str1->str[0]; |
| 4727 | Py_UNICODE u2 = str2->str[0]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4728 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4729 | u = (PyUnicodeObject*) PyUnicode_FromUnicode( |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4730 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4731 | self->length |
| 4732 | ); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4733 | if (u != NULL) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4734 | Py_UNICODE_COPY(u->str, self->str, |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4735 | self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4736 | for (i = 0; i < u->length; i++) |
| 4737 | if (u->str[i] == u1) { |
| 4738 | if (--maxcount < 0) |
| 4739 | break; |
| 4740 | u->str[i] = u2; |
| 4741 | } |
| 4742 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4743 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4744 | |
| 4745 | } else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4746 | Py_ssize_t n, i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4747 | Py_UNICODE *p; |
| 4748 | |
| 4749 | /* replace strings */ |
| 4750 | n = count(self, 0, self->length, str1); |
| 4751 | if (n > maxcount) |
| 4752 | n = maxcount; |
Guido van Rossum | 2023c9b | 2002-08-23 18:50:21 +0000 | [diff] [blame] | 4753 | if (n == 0) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4754 | /* nothing to replace, return original string */ |
Guido van Rossum | 2023c9b | 2002-08-23 18:50:21 +0000 | [diff] [blame] | 4755 | if (PyUnicode_CheckExact(self)) { |
| 4756 | Py_INCREF(self); |
| 4757 | u = self; |
| 4758 | } |
| 4759 | else { |
| 4760 | u = (PyUnicodeObject *) |
| 4761 | PyUnicode_FromUnicode(self->str, self->length); |
| 4762 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4763 | } else { |
| 4764 | u = _PyUnicode_New( |
| 4765 | self->length + n * (str2->length - str1->length)); |
| 4766 | if (u) { |
| 4767 | i = 0; |
| 4768 | p = u->str; |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 4769 | if (str1->length > 0) { |
| 4770 | while (i <= self->length - str1->length) |
| 4771 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 4772 | /* replace string segment */ |
| 4773 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 4774 | p += str2->length; |
| 4775 | i += str1->length; |
| 4776 | if (--n <= 0) { |
| 4777 | /* copy remaining part */ |
| 4778 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 4779 | break; |
| 4780 | } |
| 4781 | } else |
| 4782 | *p++ = self->str[i++]; |
| 4783 | } else { |
| 4784 | while (n > 0) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4785 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 4786 | p += str2->length; |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 4787 | if (--n <= 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4788 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4789 | *p++ = self->str[i++]; |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 4790 | } |
| 4791 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 4792 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4793 | } |
| 4794 | } |
| 4795 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4796 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4797 | return (PyObject *) u; |
| 4798 | } |
| 4799 | |
| 4800 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 4801 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4802 | PyDoc_STRVAR(title__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4803 | "S.title() -> unicode\n\ |
| 4804 | \n\ |
| 4805 | 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] | 4806 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4807 | |
| 4808 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 4809 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4810 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4811 | return fixup(self, fixtitle); |
| 4812 | } |
| 4813 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4814 | PyDoc_STRVAR(capitalize__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4815 | "S.capitalize() -> unicode\n\ |
| 4816 | \n\ |
| 4817 | 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] | 4818 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4819 | |
| 4820 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 4821 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4822 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4823 | return fixup(self, fixcapitalize); |
| 4824 | } |
| 4825 | |
| 4826 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4827 | PyDoc_STRVAR(capwords__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4828 | "S.capwords() -> unicode\n\ |
| 4829 | \n\ |
| 4830 | 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] | 4831 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4832 | |
| 4833 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 4834 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4835 | { |
| 4836 | PyObject *list; |
| 4837 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4838 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4839 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4840 | /* Split into words */ |
| 4841 | list = split(self, NULL, -1); |
| 4842 | if (!list) |
| 4843 | return NULL; |
| 4844 | |
| 4845 | /* Capitalize each word */ |
| 4846 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 4847 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 4848 | fixcapitalize); |
| 4849 | if (item == NULL) |
| 4850 | goto onError; |
| 4851 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 4852 | PyList_SET_ITEM(list, i, item); |
| 4853 | } |
| 4854 | |
| 4855 | /* Join the words to form a new string */ |
| 4856 | item = PyUnicode_Join(NULL, list); |
| 4857 | |
| 4858 | onError: |
| 4859 | Py_DECREF(list); |
| 4860 | return (PyObject *)item; |
| 4861 | } |
| 4862 | #endif |
| 4863 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4864 | /* Argument converter. Coerces to a single unicode character */ |
| 4865 | |
| 4866 | static int |
| 4867 | convert_uc(PyObject *obj, void *addr) |
| 4868 | { |
| 4869 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 4870 | PyObject *uniobj; |
| 4871 | Py_UNICODE *unistr; |
| 4872 | |
| 4873 | uniobj = PyUnicode_FromObject(obj); |
| 4874 | if (uniobj == NULL) { |
| 4875 | PyErr_SetString(PyExc_TypeError, |
| 4876 | "The fill character cannot be converted to Unicode"); |
| 4877 | return 0; |
| 4878 | } |
| 4879 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 4880 | PyErr_SetString(PyExc_TypeError, |
| 4881 | "The fill character must be exactly one character long"); |
| 4882 | Py_DECREF(uniobj); |
| 4883 | return 0; |
| 4884 | } |
| 4885 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 4886 | *fillcharloc = unistr[0]; |
| 4887 | Py_DECREF(uniobj); |
| 4888 | return 1; |
| 4889 | } |
| 4890 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4891 | PyDoc_STRVAR(center__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4892 | "S.center(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4893 | \n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4894 | Return S centered in a Unicode string of length width. Padding is\n\ |
| 4895 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4896 | |
| 4897 | static PyObject * |
| 4898 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 4899 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4900 | Py_ssize_t marg, left; |
| 4901 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4902 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4903 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 4904 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4905 | return NULL; |
| 4906 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4907 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4908 | Py_INCREF(self); |
| 4909 | return (PyObject*) self; |
| 4910 | } |
| 4911 | |
| 4912 | marg = width - self->length; |
| 4913 | left = marg / 2 + (marg & width & 1); |
| 4914 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4915 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4916 | } |
| 4917 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4918 | #if 0 |
| 4919 | |
| 4920 | /* This code should go into some future Unicode collation support |
| 4921 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 4922 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4923 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4924 | /* speedy UTF-16 code point order comparison */ |
| 4925 | /* gleaned from: */ |
| 4926 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 4927 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4928 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4929 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4930 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4931 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 4932 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4933 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4934 | }; |
| 4935 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4936 | static int |
| 4937 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 4938 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4939 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4940 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4941 | Py_UNICODE *s1 = str1->str; |
| 4942 | Py_UNICODE *s2 = str2->str; |
| 4943 | |
| 4944 | len1 = str1->length; |
| 4945 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4946 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4947 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4948 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4949 | |
| 4950 | c1 = *s1++; |
| 4951 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 4952 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4953 | if (c1 > (1<<11) * 26) |
| 4954 | c1 += utf16Fixup[c1>>11]; |
| 4955 | if (c2 > (1<<11) * 26) |
| 4956 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4957 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 4958 | |
| 4959 | if (c1 != c2) |
| 4960 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4961 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4962 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4963 | } |
| 4964 | |
| 4965 | return (len1 < len2) ? -1 : (len1 != len2); |
| 4966 | } |
| 4967 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4968 | #else |
| 4969 | |
| 4970 | static int |
| 4971 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 4972 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4973 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4974 | |
| 4975 | Py_UNICODE *s1 = str1->str; |
| 4976 | Py_UNICODE *s2 = str2->str; |
| 4977 | |
| 4978 | len1 = str1->length; |
| 4979 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4980 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4981 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4982 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4983 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 4984 | c1 = *s1++; |
| 4985 | c2 = *s2++; |
| 4986 | |
| 4987 | if (c1 != c2) |
| 4988 | return (c1 < c2) ? -1 : 1; |
| 4989 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4990 | len1--; len2--; |
| 4991 | } |
| 4992 | |
| 4993 | return (len1 < len2) ? -1 : (len1 != len2); |
| 4994 | } |
| 4995 | |
| 4996 | #endif |
| 4997 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4998 | int PyUnicode_Compare(PyObject *left, |
| 4999 | PyObject *right) |
| 5000 | { |
| 5001 | PyUnicodeObject *u = NULL, *v = NULL; |
| 5002 | int result; |
| 5003 | |
| 5004 | /* Coerce the two arguments */ |
| 5005 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 5006 | if (u == NULL) |
| 5007 | goto onError; |
| 5008 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 5009 | if (v == NULL) |
| 5010 | goto onError; |
| 5011 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5012 | /* Shortcut for empty or interned objects */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5013 | if (v == u) { |
| 5014 | Py_DECREF(u); |
| 5015 | Py_DECREF(v); |
| 5016 | return 0; |
| 5017 | } |
| 5018 | |
| 5019 | result = unicode_compare(u, v); |
| 5020 | |
| 5021 | Py_DECREF(u); |
| 5022 | Py_DECREF(v); |
| 5023 | return result; |
| 5024 | |
| 5025 | onError: |
| 5026 | Py_XDECREF(u); |
| 5027 | Py_XDECREF(v); |
| 5028 | return -1; |
| 5029 | } |
| 5030 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5031 | int PyUnicode_Contains(PyObject *container, |
| 5032 | PyObject *element) |
| 5033 | { |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5034 | PyUnicodeObject *u, *v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5035 | int result; |
| 5036 | Py_ssize_t size; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5037 | |
| 5038 | /* Coerce the two arguments */ |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5039 | v = (PyUnicodeObject *) PyUnicode_FromObject(element); |
| 5040 | if (!v) { |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 5041 | PyErr_SetString(PyExc_TypeError, |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 5042 | "'in <string>' requires string as left operand"); |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5043 | return -1; |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 5044 | } |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5045 | |
| 5046 | u = (PyUnicodeObject *) PyUnicode_FromObject(container); |
| 5047 | if (!u) { |
| 5048 | Py_DECREF(v); |
| 5049 | return -1; |
| 5050 | } |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5051 | |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 5052 | size = PyUnicode_GET_SIZE(v); |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5053 | if (!size) { |
| 5054 | result = 1; |
| 5055 | goto done; |
| 5056 | } |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 5057 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5058 | result = 0; |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5059 | |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 5060 | if (size == 1) { |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5061 | Py_UNICODE chr = PyUnicode_AS_UNICODE(v)[0]; |
| 5062 | Py_UNICODE* ptr = PyUnicode_AS_UNICODE(u); |
| 5063 | Py_UNICODE* end = ptr + PyUnicode_GET_SIZE(u); |
| 5064 | for (; ptr < end; ptr++) { |
| 5065 | if (*ptr == chr) { |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 5066 | result = 1; |
| 5067 | break; |
| 5068 | } |
| 5069 | } |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5070 | } else { |
Fredrik Lundh | 240bf2a | 2006-05-24 10:20:36 +0000 | [diff] [blame^] | 5071 | Py_ssize_t start = 0; |
| 5072 | Py_ssize_t end = PyUnicode_GET_SIZE(u) - size; |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5073 | for (; start <= end; start++) |
| 5074 | if (Py_UNICODE_MATCH(u, start, v)) { |
| 5075 | result = 1; |
| 5076 | break; |
| 5077 | } |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5078 | } |
| 5079 | |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5080 | done: |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5081 | Py_DECREF(u); |
| 5082 | Py_DECREF(v); |
| 5083 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5084 | } |
| 5085 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5086 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 5087 | |
| 5088 | PyObject *PyUnicode_Concat(PyObject *left, |
| 5089 | PyObject *right) |
| 5090 | { |
| 5091 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 5092 | |
| 5093 | /* Coerce the two arguments */ |
| 5094 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 5095 | if (u == NULL) |
| 5096 | goto onError; |
| 5097 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 5098 | if (v == NULL) |
| 5099 | goto onError; |
| 5100 | |
| 5101 | /* Shortcuts */ |
| 5102 | if (v == unicode_empty) { |
| 5103 | Py_DECREF(v); |
| 5104 | return (PyObject *)u; |
| 5105 | } |
| 5106 | if (u == unicode_empty) { |
| 5107 | Py_DECREF(u); |
| 5108 | return (PyObject *)v; |
| 5109 | } |
| 5110 | |
| 5111 | /* Concat the two Unicode strings */ |
| 5112 | w = _PyUnicode_New(u->length + v->length); |
| 5113 | if (w == NULL) |
| 5114 | goto onError; |
| 5115 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 5116 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 5117 | |
| 5118 | Py_DECREF(u); |
| 5119 | Py_DECREF(v); |
| 5120 | return (PyObject *)w; |
| 5121 | |
| 5122 | onError: |
| 5123 | Py_XDECREF(u); |
| 5124 | Py_XDECREF(v); |
| 5125 | return NULL; |
| 5126 | } |
| 5127 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5128 | PyDoc_STRVAR(count__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5129 | "S.count(sub[, start[, end]]) -> int\n\ |
| 5130 | \n\ |
Fredrik Lundh | 763b50f | 2006-05-22 15:35:12 +0000 | [diff] [blame] | 5131 | Return the number of non-overlapping occurrences of substring sub in\n\ |
| 5132 | 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] | 5133 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5134 | |
| 5135 | static PyObject * |
| 5136 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 5137 | { |
| 5138 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5139 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5140 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5141 | PyObject *result; |
| 5142 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5143 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 5144 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5145 | return NULL; |
| 5146 | |
| 5147 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 5148 | (PyObject *)substring); |
| 5149 | if (substring == NULL) |
| 5150 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5151 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5152 | if (start < 0) |
| 5153 | start += self->length; |
| 5154 | if (start < 0) |
| 5155 | start = 0; |
| 5156 | if (end > self->length) |
| 5157 | end = self->length; |
| 5158 | if (end < 0) |
| 5159 | end += self->length; |
| 5160 | if (end < 0) |
| 5161 | end = 0; |
| 5162 | |
| 5163 | result = PyInt_FromLong((long) count(self, start, end, substring)); |
| 5164 | |
| 5165 | Py_DECREF(substring); |
| 5166 | return result; |
| 5167 | } |
| 5168 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5169 | PyDoc_STRVAR(encode__doc__, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5170 | "S.encode([encoding[,errors]]) -> string or unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5171 | \n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5172 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 5173 | 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] | 5174 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5175 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 5176 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 5177 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5178 | |
| 5179 | static PyObject * |
| 5180 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 5181 | { |
| 5182 | char *encoding = NULL; |
| 5183 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5184 | PyObject *v; |
| 5185 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5186 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 5187 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5188 | v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5189 | if (v == NULL) |
| 5190 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5191 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 5192 | PyErr_Format(PyExc_TypeError, |
| 5193 | "encoder did not return a string/unicode object " |
| 5194 | "(type=%.400s)", |
| 5195 | v->ob_type->tp_name); |
| 5196 | Py_DECREF(v); |
| 5197 | return NULL; |
| 5198 | } |
| 5199 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5200 | |
| 5201 | onError: |
| 5202 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5203 | } |
| 5204 | |
| 5205 | PyDoc_STRVAR(decode__doc__, |
| 5206 | "S.decode([encoding[,errors]]) -> string or unicode\n\ |
| 5207 | \n\ |
| 5208 | Decodes S using the codec registered for encoding. encoding defaults\n\ |
| 5209 | to the default encoding. errors may be given to set a different error\n\ |
| 5210 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 5211 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 5212 | as well as any other name registerd with codecs.register_error that is\n\ |
| 5213 | able to handle UnicodeDecodeErrors."); |
| 5214 | |
| 5215 | static PyObject * |
Marc-André Lemburg | 126b44c | 2004-07-10 12:04:20 +0000 | [diff] [blame] | 5216 | unicode_decode(PyUnicodeObject *self, PyObject *args) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5217 | { |
| 5218 | char *encoding = NULL; |
| 5219 | char *errors = NULL; |
| 5220 | PyObject *v; |
| 5221 | |
| 5222 | if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
| 5223 | return NULL; |
| 5224 | v = PyUnicode_AsDecodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5225 | if (v == NULL) |
| 5226 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5227 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 5228 | PyErr_Format(PyExc_TypeError, |
| 5229 | "decoder did not return a string/unicode object " |
| 5230 | "(type=%.400s)", |
| 5231 | v->ob_type->tp_name); |
| 5232 | Py_DECREF(v); |
| 5233 | return NULL; |
| 5234 | } |
| 5235 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5236 | |
| 5237 | onError: |
| 5238 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5239 | } |
| 5240 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5241 | PyDoc_STRVAR(expandtabs__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5242 | "S.expandtabs([tabsize]) -> unicode\n\ |
| 5243 | \n\ |
| 5244 | 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] | 5245 | 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] | 5246 | |
| 5247 | static PyObject* |
| 5248 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 5249 | { |
| 5250 | Py_UNICODE *e; |
| 5251 | Py_UNICODE *p; |
| 5252 | Py_UNICODE *q; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5253 | Py_ssize_t i, j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5254 | PyUnicodeObject *u; |
| 5255 | int tabsize = 8; |
| 5256 | |
| 5257 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 5258 | return NULL; |
| 5259 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5260 | /* First pass: determine size of output string */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5261 | i = j = 0; |
| 5262 | e = self->str + self->length; |
| 5263 | for (p = self->str; p < e; p++) |
| 5264 | if (*p == '\t') { |
| 5265 | if (tabsize > 0) |
| 5266 | j += tabsize - (j % tabsize); |
| 5267 | } |
| 5268 | else { |
| 5269 | j++; |
| 5270 | if (*p == '\n' || *p == '\r') { |
| 5271 | i += j; |
| 5272 | j = 0; |
| 5273 | } |
| 5274 | } |
| 5275 | |
| 5276 | /* Second pass: create output string and fill it */ |
| 5277 | u = _PyUnicode_New(i + j); |
| 5278 | if (!u) |
| 5279 | return NULL; |
| 5280 | |
| 5281 | j = 0; |
| 5282 | q = u->str; |
| 5283 | |
| 5284 | for (p = self->str; p < e; p++) |
| 5285 | if (*p == '\t') { |
| 5286 | if (tabsize > 0) { |
| 5287 | i = tabsize - (j % tabsize); |
| 5288 | j += i; |
| 5289 | while (i--) |
| 5290 | *q++ = ' '; |
| 5291 | } |
| 5292 | } |
| 5293 | else { |
| 5294 | j++; |
| 5295 | *q++ = *p; |
| 5296 | if (*p == '\n' || *p == '\r') |
| 5297 | j = 0; |
| 5298 | } |
| 5299 | |
| 5300 | return (PyObject*) u; |
| 5301 | } |
| 5302 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5303 | PyDoc_STRVAR(find__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5304 | "S.find(sub [,start [,end]]) -> int\n\ |
| 5305 | \n\ |
| 5306 | Return the lowest index in S where substring sub is found,\n\ |
| 5307 | such that sub is contained within s[start,end]. Optional\n\ |
| 5308 | arguments start and end are interpreted as in slice notation.\n\ |
| 5309 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5310 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5311 | |
| 5312 | static PyObject * |
| 5313 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 5314 | { |
| 5315 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5316 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5317 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5318 | PyObject *result; |
| 5319 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5320 | if (!PyArg_ParseTuple(args, "O|O&O&:find", &substring, |
| 5321 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5322 | return NULL; |
| 5323 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 5324 | (PyObject *)substring); |
| 5325 | if (substring == NULL) |
| 5326 | return NULL; |
| 5327 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5328 | result = PyInt_FromSsize_t(findstring(self, substring, start, end, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5329 | |
| 5330 | Py_DECREF(substring); |
| 5331 | return result; |
| 5332 | } |
| 5333 | |
| 5334 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5335 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5336 | { |
| 5337 | if (index < 0 || index >= self->length) { |
| 5338 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 5339 | return NULL; |
| 5340 | } |
| 5341 | |
| 5342 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 5343 | } |
| 5344 | |
| 5345 | static long |
| 5346 | unicode_hash(PyUnicodeObject *self) |
| 5347 | { |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5348 | /* Since Unicode objects compare equal to their ASCII string |
| 5349 | counterparts, they should use the individual character values |
| 5350 | as basis for their hash value. This is needed to assure that |
| 5351 | strings and Unicode objects behave in the same way as |
| 5352 | dictionary keys. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5353 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5354 | register Py_ssize_t len; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5355 | register Py_UNICODE *p; |
| 5356 | register long x; |
| 5357 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5358 | if (self->hash != -1) |
| 5359 | return self->hash; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5360 | len = PyUnicode_GET_SIZE(self); |
| 5361 | p = PyUnicode_AS_UNICODE(self); |
| 5362 | x = *p << 7; |
| 5363 | while (--len >= 0) |
| 5364 | x = (1000003*x) ^ *p++; |
| 5365 | x ^= PyUnicode_GET_SIZE(self); |
| 5366 | if (x == -1) |
| 5367 | x = -2; |
| 5368 | self->hash = x; |
| 5369 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5370 | } |
| 5371 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5372 | PyDoc_STRVAR(index__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5373 | "S.index(sub [,start [,end]]) -> int\n\ |
| 5374 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5375 | 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] | 5376 | |
| 5377 | static PyObject * |
| 5378 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 5379 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5380 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5381 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5382 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5383 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5384 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5385 | if (!PyArg_ParseTuple(args, "O|O&O&:index", &substring, |
| 5386 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5387 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5388 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5389 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 5390 | (PyObject *)substring); |
| 5391 | if (substring == NULL) |
| 5392 | return NULL; |
| 5393 | |
| 5394 | result = findstring(self, substring, start, end, 1); |
| 5395 | |
| 5396 | Py_DECREF(substring); |
| 5397 | if (result < 0) { |
| 5398 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 5399 | return NULL; |
| 5400 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5401 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5402 | } |
| 5403 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5404 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5405 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5406 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5407 | 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] | 5408 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5409 | |
| 5410 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5411 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5412 | { |
| 5413 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5414 | register const Py_UNICODE *e; |
| 5415 | int cased; |
| 5416 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5417 | /* Shortcut for single character strings */ |
| 5418 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5419 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5420 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5421 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5422 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5423 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5424 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5425 | e = p + PyUnicode_GET_SIZE(self); |
| 5426 | cased = 0; |
| 5427 | for (; p < e; p++) { |
| 5428 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5429 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5430 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5431 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5432 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 5433 | cased = 1; |
| 5434 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5435 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5436 | } |
| 5437 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5438 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5439 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5440 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5441 | 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] | 5442 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5443 | |
| 5444 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5445 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5446 | { |
| 5447 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5448 | register const Py_UNICODE *e; |
| 5449 | int cased; |
| 5450 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5451 | /* Shortcut for single character strings */ |
| 5452 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5453 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5454 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5455 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5456 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5457 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5458 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5459 | e = p + PyUnicode_GET_SIZE(self); |
| 5460 | cased = 0; |
| 5461 | for (; p < e; p++) { |
| 5462 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5463 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5464 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5465 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5466 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 5467 | cased = 1; |
| 5468 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5469 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5470 | } |
| 5471 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5472 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5473 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5474 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5475 | Return True if S is a titlecased string and there is at least one\n\ |
| 5476 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 5477 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 5478 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5479 | |
| 5480 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5481 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5482 | { |
| 5483 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5484 | register const Py_UNICODE *e; |
| 5485 | int cased, previous_is_cased; |
| 5486 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5487 | /* Shortcut for single character strings */ |
| 5488 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5489 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 5490 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5491 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5492 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5493 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5494 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5495 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5496 | e = p + PyUnicode_GET_SIZE(self); |
| 5497 | cased = 0; |
| 5498 | previous_is_cased = 0; |
| 5499 | for (; p < e; p++) { |
| 5500 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5501 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5502 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 5503 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5504 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5505 | previous_is_cased = 1; |
| 5506 | cased = 1; |
| 5507 | } |
| 5508 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 5509 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5510 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5511 | previous_is_cased = 1; |
| 5512 | cased = 1; |
| 5513 | } |
| 5514 | else |
| 5515 | previous_is_cased = 0; |
| 5516 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5517 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5518 | } |
| 5519 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5520 | PyDoc_STRVAR(isspace__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5521 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5522 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5523 | Return True if all characters in S are whitespace\n\ |
| 5524 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5525 | |
| 5526 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5527 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5528 | { |
| 5529 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5530 | register const Py_UNICODE *e; |
| 5531 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5532 | /* Shortcut for single character strings */ |
| 5533 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5534 | Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5535 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5536 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5537 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5538 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5539 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5540 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5541 | e = p + PyUnicode_GET_SIZE(self); |
| 5542 | for (; p < e; p++) { |
| 5543 | if (!Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5544 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5545 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5546 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5547 | } |
| 5548 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5549 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5550 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5551 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5552 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5553 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5554 | |
| 5555 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5556 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5557 | { |
| 5558 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5559 | register const Py_UNICODE *e; |
| 5560 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5561 | /* Shortcut for single character strings */ |
| 5562 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5563 | Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5564 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5565 | |
| 5566 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5567 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5568 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5569 | |
| 5570 | e = p + PyUnicode_GET_SIZE(self); |
| 5571 | for (; p < e; p++) { |
| 5572 | if (!Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5573 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5574 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5575 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5576 | } |
| 5577 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5578 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5579 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5580 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5581 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5582 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5583 | |
| 5584 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5585 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5586 | { |
| 5587 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5588 | register const Py_UNICODE *e; |
| 5589 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5590 | /* Shortcut for single character strings */ |
| 5591 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5592 | Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5593 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5594 | |
| 5595 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5596 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5597 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5598 | |
| 5599 | e = p + PyUnicode_GET_SIZE(self); |
| 5600 | for (; p < e; p++) { |
| 5601 | if (!Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5602 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5603 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5604 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5605 | } |
| 5606 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5607 | PyDoc_STRVAR(isdecimal__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5608 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5609 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5610 | 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] | 5611 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5612 | |
| 5613 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5614 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5615 | { |
| 5616 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5617 | register const Py_UNICODE *e; |
| 5618 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5619 | /* Shortcut for single character strings */ |
| 5620 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5621 | Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5622 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5623 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5624 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5625 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5626 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5627 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5628 | e = p + PyUnicode_GET_SIZE(self); |
| 5629 | for (; p < e; p++) { |
| 5630 | if (!Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5631 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5632 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5633 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5634 | } |
| 5635 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5636 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5637 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5638 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5639 | Return True if all characters in S are digits\n\ |
| 5640 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5641 | |
| 5642 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5643 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5644 | { |
| 5645 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5646 | register const Py_UNICODE *e; |
| 5647 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5648 | /* Shortcut for single character strings */ |
| 5649 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5650 | Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5651 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5652 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5653 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5654 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5655 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5656 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5657 | e = p + PyUnicode_GET_SIZE(self); |
| 5658 | for (; p < e; p++) { |
| 5659 | if (!Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5660 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5661 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5662 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5663 | } |
| 5664 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5665 | PyDoc_STRVAR(isnumeric__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5666 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5667 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5668 | 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] | 5669 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5670 | |
| 5671 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5672 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5673 | { |
| 5674 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5675 | register const Py_UNICODE *e; |
| 5676 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5677 | /* Shortcut for single character strings */ |
| 5678 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5679 | Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5680 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5681 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5682 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5683 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5684 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5685 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5686 | e = p + PyUnicode_GET_SIZE(self); |
| 5687 | for (; p < e; p++) { |
| 5688 | if (!Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5689 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5690 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5691 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5692 | } |
| 5693 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5694 | PyDoc_STRVAR(join__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5695 | "S.join(sequence) -> unicode\n\ |
| 5696 | \n\ |
| 5697 | 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] | 5698 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5699 | |
| 5700 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5701 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5702 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5703 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5704 | } |
| 5705 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5706 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5707 | unicode_length(PyUnicodeObject *self) |
| 5708 | { |
| 5709 | return self->length; |
| 5710 | } |
| 5711 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5712 | PyDoc_STRVAR(ljust__doc__, |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 5713 | "S.ljust(width[, fillchar]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5714 | \n\ |
| 5715 | 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] | 5716 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5717 | |
| 5718 | static PyObject * |
| 5719 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 5720 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5721 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5722 | Py_UNICODE fillchar = ' '; |
| 5723 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5724 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5725 | return NULL; |
| 5726 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5727 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5728 | Py_INCREF(self); |
| 5729 | return (PyObject*) self; |
| 5730 | } |
| 5731 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5732 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5733 | } |
| 5734 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5735 | PyDoc_STRVAR(lower__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5736 | "S.lower() -> unicode\n\ |
| 5737 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5738 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5739 | |
| 5740 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5741 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5742 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5743 | return fixup(self, fixlower); |
| 5744 | } |
| 5745 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5746 | #define LEFTSTRIP 0 |
| 5747 | #define RIGHTSTRIP 1 |
| 5748 | #define BOTHSTRIP 2 |
| 5749 | |
| 5750 | /* Arrays indexed by above */ |
| 5751 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 5752 | |
| 5753 | #define STRIPNAME(i) (stripformat[i]+3) |
| 5754 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5755 | /* externally visible for str.strip(unicode) */ |
| 5756 | PyObject * |
| 5757 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 5758 | { |
| 5759 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5760 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5761 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5762 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 5763 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5764 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5765 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
| 5766 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5767 | i = 0; |
| 5768 | if (striptype != RIGHTSTRIP) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5769 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 5770 | i++; |
| 5771 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5772 | } |
| 5773 | |
| 5774 | j = len; |
| 5775 | if (striptype != LEFTSTRIP) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5776 | do { |
| 5777 | j--; |
| 5778 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 5779 | j++; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5780 | } |
| 5781 | |
| 5782 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5783 | Py_INCREF(self); |
| 5784 | return (PyObject*)self; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5785 | } |
| 5786 | else |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5787 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5788 | } |
| 5789 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5790 | |
| 5791 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5792 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5793 | { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5794 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5795 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5796 | |
| 5797 | i = 0; |
| 5798 | if (striptype != RIGHTSTRIP) { |
| 5799 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 5800 | i++; |
| 5801 | } |
| 5802 | } |
| 5803 | |
| 5804 | j = len; |
| 5805 | if (striptype != LEFTSTRIP) { |
| 5806 | do { |
| 5807 | j--; |
| 5808 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 5809 | j++; |
| 5810 | } |
| 5811 | |
| 5812 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 5813 | Py_INCREF(self); |
| 5814 | return (PyObject*)self; |
| 5815 | } |
| 5816 | else |
| 5817 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5818 | } |
| 5819 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5820 | |
| 5821 | static PyObject * |
| 5822 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 5823 | { |
| 5824 | PyObject *sep = NULL; |
| 5825 | |
| 5826 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 5827 | return NULL; |
| 5828 | |
| 5829 | if (sep != NULL && sep != Py_None) { |
| 5830 | if (PyUnicode_Check(sep)) |
| 5831 | return _PyUnicode_XStrip(self, striptype, sep); |
| 5832 | else if (PyString_Check(sep)) { |
| 5833 | PyObject *res; |
| 5834 | sep = PyUnicode_FromObject(sep); |
| 5835 | if (sep==NULL) |
| 5836 | return NULL; |
| 5837 | res = _PyUnicode_XStrip(self, striptype, sep); |
| 5838 | Py_DECREF(sep); |
| 5839 | return res; |
| 5840 | } |
| 5841 | else { |
| 5842 | PyErr_Format(PyExc_TypeError, |
| 5843 | "%s arg must be None, unicode or str", |
| 5844 | STRIPNAME(striptype)); |
| 5845 | return NULL; |
| 5846 | } |
| 5847 | } |
| 5848 | |
| 5849 | return do_strip(self, striptype); |
| 5850 | } |
| 5851 | |
| 5852 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5853 | PyDoc_STRVAR(strip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5854 | "S.strip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5855 | \n\ |
| 5856 | Return a copy of the string S with leading and trailing\n\ |
| 5857 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5858 | If chars is given and not None, remove characters in chars instead.\n\ |
| 5859 | 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] | 5860 | |
| 5861 | static PyObject * |
| 5862 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 5863 | { |
| 5864 | if (PyTuple_GET_SIZE(args) == 0) |
| 5865 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 5866 | else |
| 5867 | return do_argstrip(self, BOTHSTRIP, args); |
| 5868 | } |
| 5869 | |
| 5870 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5871 | PyDoc_STRVAR(lstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5872 | "S.lstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5873 | \n\ |
| 5874 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5875 | If chars is given and not None, remove characters in chars instead.\n\ |
| 5876 | 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] | 5877 | |
| 5878 | static PyObject * |
| 5879 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 5880 | { |
| 5881 | if (PyTuple_GET_SIZE(args) == 0) |
| 5882 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 5883 | else |
| 5884 | return do_argstrip(self, LEFTSTRIP, args); |
| 5885 | } |
| 5886 | |
| 5887 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5888 | PyDoc_STRVAR(rstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5889 | "S.rstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5890 | \n\ |
| 5891 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5892 | If chars is given and not None, remove characters in chars instead.\n\ |
| 5893 | 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] | 5894 | |
| 5895 | static PyObject * |
| 5896 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 5897 | { |
| 5898 | if (PyTuple_GET_SIZE(args) == 0) |
| 5899 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 5900 | else |
| 5901 | return do_argstrip(self, RIGHTSTRIP, args); |
| 5902 | } |
| 5903 | |
| 5904 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5905 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5906 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5907 | { |
| 5908 | PyUnicodeObject *u; |
| 5909 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5910 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 5911 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5912 | |
| 5913 | if (len < 0) |
| 5914 | len = 0; |
| 5915 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5916 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5917 | /* no repeat, return original string */ |
| 5918 | Py_INCREF(str); |
| 5919 | return (PyObject*) str; |
| 5920 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 5921 | |
| 5922 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 5923 | * needed doesn't overflow size_t |
| 5924 | */ |
| 5925 | nchars = len * str->length; |
| 5926 | if (len && nchars / len != str->length) { |
| 5927 | PyErr_SetString(PyExc_OverflowError, |
| 5928 | "repeated string is too long"); |
| 5929 | return NULL; |
| 5930 | } |
| 5931 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 5932 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 5933 | PyErr_SetString(PyExc_OverflowError, |
| 5934 | "repeated string is too long"); |
| 5935 | return NULL; |
| 5936 | } |
| 5937 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5938 | if (!u) |
| 5939 | return NULL; |
| 5940 | |
| 5941 | p = u->str; |
| 5942 | |
Fredrik Lundh | f1d60a5 | 2006-05-22 16:29:30 +0000 | [diff] [blame] | 5943 | if (str->length == 1 && len > 0) { |
| 5944 | Py_UNICODE_FILL(p, str->str[0], len); |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 5945 | } else { |
Tim Peters | 1bacc64 | 2006-05-23 05:47:16 +0000 | [diff] [blame] | 5946 | Py_ssize_t done = 0; /* number of characters copied this far */ |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 5947 | if (done < nchars) { |
Fredrik Lundh | f1d60a5 | 2006-05-22 16:29:30 +0000 | [diff] [blame] | 5948 | Py_UNICODE_COPY(p, str->str, str->length); |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 5949 | done = str->length; |
| 5950 | } |
| 5951 | while (done < nchars) { |
| 5952 | int n = (done <= nchars-done) ? done : nchars-done; |
| 5953 | Py_UNICODE_COPY(p+done, p, n); |
| 5954 | done += n; |
| 5955 | } |
| 5956 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5957 | |
| 5958 | return (PyObject*) u; |
| 5959 | } |
| 5960 | |
| 5961 | PyObject *PyUnicode_Replace(PyObject *obj, |
| 5962 | PyObject *subobj, |
| 5963 | PyObject *replobj, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5964 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5965 | { |
| 5966 | PyObject *self; |
| 5967 | PyObject *str1; |
| 5968 | PyObject *str2; |
| 5969 | PyObject *result; |
| 5970 | |
| 5971 | self = PyUnicode_FromObject(obj); |
| 5972 | if (self == NULL) |
| 5973 | return NULL; |
| 5974 | str1 = PyUnicode_FromObject(subobj); |
| 5975 | if (str1 == NULL) { |
| 5976 | Py_DECREF(self); |
| 5977 | return NULL; |
| 5978 | } |
| 5979 | str2 = PyUnicode_FromObject(replobj); |
| 5980 | if (str2 == NULL) { |
| 5981 | Py_DECREF(self); |
| 5982 | Py_DECREF(str1); |
| 5983 | return NULL; |
| 5984 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5985 | result = replace((PyUnicodeObject *)self, |
| 5986 | (PyUnicodeObject *)str1, |
| 5987 | (PyUnicodeObject *)str2, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5988 | maxcount); |
| 5989 | Py_DECREF(self); |
| 5990 | Py_DECREF(str1); |
| 5991 | Py_DECREF(str2); |
| 5992 | return result; |
| 5993 | } |
| 5994 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5995 | PyDoc_STRVAR(replace__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5996 | "S.replace (old, new[, maxsplit]) -> unicode\n\ |
| 5997 | \n\ |
| 5998 | Return a copy of S with all occurrences of substring\n\ |
| 5999 | old replaced by new. If the optional argument maxsplit is\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6000 | given, only the first maxsplit occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6001 | |
| 6002 | static PyObject* |
| 6003 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 6004 | { |
| 6005 | PyUnicodeObject *str1; |
| 6006 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6007 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6008 | PyObject *result; |
| 6009 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6010 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6011 | return NULL; |
| 6012 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 6013 | if (str1 == NULL) |
| 6014 | return NULL; |
| 6015 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 6016 | if (str2 == NULL) { |
| 6017 | Py_DECREF(str1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6018 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 6019 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6020 | |
| 6021 | result = replace(self, str1, str2, maxcount); |
| 6022 | |
| 6023 | Py_DECREF(str1); |
| 6024 | Py_DECREF(str2); |
| 6025 | return result; |
| 6026 | } |
| 6027 | |
| 6028 | static |
| 6029 | PyObject *unicode_repr(PyObject *unicode) |
| 6030 | { |
| 6031 | return unicodeescape_string(PyUnicode_AS_UNICODE(unicode), |
| 6032 | PyUnicode_GET_SIZE(unicode), |
| 6033 | 1); |
| 6034 | } |
| 6035 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6036 | PyDoc_STRVAR(rfind__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6037 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 6038 | \n\ |
| 6039 | Return the highest index in S where substring sub is found,\n\ |
| 6040 | such that sub is contained within s[start,end]. Optional\n\ |
| 6041 | arguments start and end are interpreted as in slice notation.\n\ |
| 6042 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6043 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6044 | |
| 6045 | static PyObject * |
| 6046 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 6047 | { |
| 6048 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6049 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6050 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6051 | PyObject *result; |
| 6052 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6053 | if (!PyArg_ParseTuple(args, "O|O&O&:rfind", &substring, |
| 6054 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6055 | return NULL; |
| 6056 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6057 | (PyObject *)substring); |
| 6058 | if (substring == NULL) |
| 6059 | return NULL; |
| 6060 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6061 | result = PyInt_FromSsize_t(findstring(self, substring, start, end, -1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6062 | |
| 6063 | Py_DECREF(substring); |
| 6064 | return result; |
| 6065 | } |
| 6066 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6067 | PyDoc_STRVAR(rindex__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6068 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 6069 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6070 | 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] | 6071 | |
| 6072 | static PyObject * |
| 6073 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 6074 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6075 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6076 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6077 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6078 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6079 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6080 | if (!PyArg_ParseTuple(args, "O|O&O&:rindex", &substring, |
| 6081 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6082 | return NULL; |
| 6083 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6084 | (PyObject *)substring); |
| 6085 | if (substring == NULL) |
| 6086 | return NULL; |
| 6087 | |
| 6088 | result = findstring(self, substring, start, end, -1); |
| 6089 | |
| 6090 | Py_DECREF(substring); |
| 6091 | if (result < 0) { |
| 6092 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 6093 | return NULL; |
| 6094 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6095 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6096 | } |
| 6097 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6098 | PyDoc_STRVAR(rjust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6099 | "S.rjust(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6100 | \n\ |
| 6101 | 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] | 6102 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6103 | |
| 6104 | static PyObject * |
| 6105 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 6106 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6107 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6108 | Py_UNICODE fillchar = ' '; |
| 6109 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6110 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6111 | return NULL; |
| 6112 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6113 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6114 | Py_INCREF(self); |
| 6115 | return (PyObject*) self; |
| 6116 | } |
| 6117 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6118 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6119 | } |
| 6120 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6121 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6122 | 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] | 6123 | { |
| 6124 | /* standard clamping */ |
| 6125 | if (start < 0) |
| 6126 | start = 0; |
| 6127 | if (end < 0) |
| 6128 | end = 0; |
| 6129 | if (end > self->length) |
| 6130 | end = self->length; |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6131 | if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6132 | /* full slice, return original string */ |
| 6133 | Py_INCREF(self); |
| 6134 | return (PyObject*) self; |
| 6135 | } |
| 6136 | if (start > end) |
| 6137 | start = end; |
| 6138 | /* copy slice */ |
| 6139 | return (PyObject*) PyUnicode_FromUnicode(self->str + start, |
| 6140 | end - start); |
| 6141 | } |
| 6142 | |
| 6143 | PyObject *PyUnicode_Split(PyObject *s, |
| 6144 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6145 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6146 | { |
| 6147 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6148 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6149 | s = PyUnicode_FromObject(s); |
| 6150 | if (s == NULL) |
| 6151 | return NULL; |
| 6152 | if (sep != NULL) { |
| 6153 | sep = PyUnicode_FromObject(sep); |
| 6154 | if (sep == NULL) { |
| 6155 | Py_DECREF(s); |
| 6156 | return NULL; |
| 6157 | } |
| 6158 | } |
| 6159 | |
| 6160 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 6161 | |
| 6162 | Py_DECREF(s); |
| 6163 | Py_XDECREF(sep); |
| 6164 | return result; |
| 6165 | } |
| 6166 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6167 | PyDoc_STRVAR(split__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6168 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 6169 | \n\ |
| 6170 | Return a list of the words in S, using sep as the\n\ |
| 6171 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Thomas Heller | ca0d2cb | 2004-09-15 11:41:32 +0000 | [diff] [blame] | 6172 | splits are done. If sep is not specified or is None,\n\ |
Walter Dörwald | 782afc5 | 2004-09-14 09:40:45 +0000 | [diff] [blame] | 6173 | any whitespace string is a separator."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6174 | |
| 6175 | static PyObject* |
| 6176 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 6177 | { |
| 6178 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6179 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6180 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6181 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6182 | return NULL; |
| 6183 | |
| 6184 | if (substring == Py_None) |
| 6185 | return split(self, NULL, maxcount); |
| 6186 | else if (PyUnicode_Check(substring)) |
| 6187 | return split(self, (PyUnicodeObject *)substring, maxcount); |
| 6188 | else |
| 6189 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
| 6190 | } |
| 6191 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6192 | PyObject *PyUnicode_RSplit(PyObject *s, |
| 6193 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6194 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6195 | { |
| 6196 | PyObject *result; |
| 6197 | |
| 6198 | s = PyUnicode_FromObject(s); |
| 6199 | if (s == NULL) |
| 6200 | return NULL; |
| 6201 | if (sep != NULL) { |
| 6202 | sep = PyUnicode_FromObject(sep); |
| 6203 | if (sep == NULL) { |
| 6204 | Py_DECREF(s); |
| 6205 | return NULL; |
| 6206 | } |
| 6207 | } |
| 6208 | |
| 6209 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 6210 | |
| 6211 | Py_DECREF(s); |
| 6212 | Py_XDECREF(sep); |
| 6213 | return result; |
| 6214 | } |
| 6215 | |
| 6216 | PyDoc_STRVAR(rsplit__doc__, |
| 6217 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
| 6218 | \n\ |
| 6219 | Return a list of the words in S, using sep as the\n\ |
| 6220 | delimiter string, starting at the end of the string and\n\ |
| 6221 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 6222 | splits are done. If sep is not specified, any whitespace string\n\ |
| 6223 | is a separator."); |
| 6224 | |
| 6225 | static PyObject* |
| 6226 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 6227 | { |
| 6228 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6229 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6230 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6231 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6232 | return NULL; |
| 6233 | |
| 6234 | if (substring == Py_None) |
| 6235 | return rsplit(self, NULL, maxcount); |
| 6236 | else if (PyUnicode_Check(substring)) |
| 6237 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
| 6238 | else |
| 6239 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
| 6240 | } |
| 6241 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6242 | PyDoc_STRVAR(splitlines__doc__, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6243 | "S.splitlines([keepends]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6244 | \n\ |
| 6245 | 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] | 6246 | 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] | 6247 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6248 | |
| 6249 | static PyObject* |
| 6250 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 6251 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6252 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6253 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6254 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6255 | return NULL; |
| 6256 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6257 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6258 | } |
| 6259 | |
| 6260 | static |
| 6261 | PyObject *unicode_str(PyUnicodeObject *self) |
| 6262 | { |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6263 | return PyUnicode_AsEncodedString((PyObject *)self, NULL, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6264 | } |
| 6265 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6266 | PyDoc_STRVAR(swapcase__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6267 | "S.swapcase() -> unicode\n\ |
| 6268 | \n\ |
| 6269 | 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] | 6270 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6271 | |
| 6272 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6273 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6274 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6275 | return fixup(self, fixswapcase); |
| 6276 | } |
| 6277 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6278 | PyDoc_STRVAR(translate__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6279 | "S.translate(table) -> unicode\n\ |
| 6280 | \n\ |
| 6281 | Return a copy of the string S, where all characters have been mapped\n\ |
| 6282 | 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] | 6283 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ |
| 6284 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 6285 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6286 | |
| 6287 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6288 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6289 | { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6290 | return PyUnicode_TranslateCharmap(self->str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6291 | self->length, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6292 | table, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6293 | "ignore"); |
| 6294 | } |
| 6295 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6296 | PyDoc_STRVAR(upper__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6297 | "S.upper() -> unicode\n\ |
| 6298 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6299 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6300 | |
| 6301 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6302 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6303 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6304 | return fixup(self, fixupper); |
| 6305 | } |
| 6306 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6307 | PyDoc_STRVAR(zfill__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6308 | "S.zfill(width) -> unicode\n\ |
| 6309 | \n\ |
| 6310 | 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] | 6311 | of the specified width. The string x is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6312 | |
| 6313 | static PyObject * |
| 6314 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 6315 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6316 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6317 | PyUnicodeObject *u; |
| 6318 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6319 | Py_ssize_t width; |
| 6320 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6321 | return NULL; |
| 6322 | |
| 6323 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 6324 | if (PyUnicode_CheckExact(self)) { |
| 6325 | Py_INCREF(self); |
| 6326 | return (PyObject*) self; |
| 6327 | } |
| 6328 | else |
| 6329 | return PyUnicode_FromUnicode( |
| 6330 | PyUnicode_AS_UNICODE(self), |
| 6331 | PyUnicode_GET_SIZE(self) |
| 6332 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6333 | } |
| 6334 | |
| 6335 | fill = width - self->length; |
| 6336 | |
| 6337 | u = pad(self, fill, 0, '0'); |
| 6338 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 6339 | if (u == NULL) |
| 6340 | return NULL; |
| 6341 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6342 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 6343 | /* move sign to beginning of string */ |
| 6344 | u->str[0] = u->str[fill]; |
| 6345 | u->str[fill] = '0'; |
| 6346 | } |
| 6347 | |
| 6348 | return (PyObject*) u; |
| 6349 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6350 | |
| 6351 | #if 0 |
| 6352 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6353 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6354 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6355 | return PyInt_FromLong(unicode_freelist_size); |
| 6356 | } |
| 6357 | #endif |
| 6358 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6359 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6360 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6361 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 6362 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 6363 | With optional start, test S beginning at that position.\n\ |
| 6364 | With optional end, stop comparing S at that position."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6365 | |
| 6366 | static PyObject * |
| 6367 | unicode_startswith(PyUnicodeObject *self, |
| 6368 | PyObject *args) |
| 6369 | { |
| 6370 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6371 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6372 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6373 | PyObject *result; |
| 6374 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6375 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &substring, |
| 6376 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6377 | return NULL; |
| 6378 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6379 | (PyObject *)substring); |
| 6380 | if (substring == NULL) |
| 6381 | return NULL; |
| 6382 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6383 | result = PyBool_FromLong(tailmatch(self, substring, start, end, -1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6384 | |
| 6385 | Py_DECREF(substring); |
| 6386 | return result; |
| 6387 | } |
| 6388 | |
| 6389 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6390 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6391 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6392 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 6393 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 6394 | With optional start, test S beginning at that position.\n\ |
| 6395 | With optional end, stop comparing S at that position."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6396 | |
| 6397 | static PyObject * |
| 6398 | unicode_endswith(PyUnicodeObject *self, |
| 6399 | PyObject *args) |
| 6400 | { |
| 6401 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6402 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6403 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6404 | PyObject *result; |
| 6405 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6406 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &substring, |
| 6407 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6408 | return NULL; |
| 6409 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6410 | (PyObject *)substring); |
| 6411 | if (substring == NULL) |
| 6412 | return NULL; |
| 6413 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6414 | result = PyBool_FromLong(tailmatch(self, substring, start, end, +1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6415 | |
| 6416 | Py_DECREF(substring); |
| 6417 | return result; |
| 6418 | } |
| 6419 | |
| 6420 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 6421 | |
| 6422 | static PyObject * |
| 6423 | unicode_getnewargs(PyUnicodeObject *v) |
| 6424 | { |
| 6425 | return Py_BuildValue("(u#)", v->str, v->length); |
| 6426 | } |
| 6427 | |
| 6428 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6429 | static PyMethodDef unicode_methods[] = { |
| 6430 | |
| 6431 | /* Order is according to common usage: often used methods should |
| 6432 | appear first, since lookup is done sequentially. */ |
| 6433 | |
Georg Brandl | ecdc0a9 | 2006-03-30 12:19:07 +0000 | [diff] [blame] | 6434 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6435 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 6436 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6437 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6438 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 6439 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 6440 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 6441 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 6442 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 6443 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 6444 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
| 6445 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 6446 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 6447 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6448 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6449 | {"decode", (PyCFunction) unicode_decode, METH_VARARGS, decode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6450 | /* {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */ |
| 6451 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 6452 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 6453 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6454 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6455 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6456 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6457 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 6458 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 6459 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 6460 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 6461 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 6462 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 6463 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 6464 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 6465 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 6466 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 6467 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 6468 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 6469 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 6470 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6471 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 6472 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6473 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6474 | #endif |
| 6475 | |
| 6476 | #if 0 |
| 6477 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6478 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6479 | #endif |
| 6480 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 6481 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6482 | {NULL, NULL} |
| 6483 | }; |
| 6484 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 6485 | static PyObject * |
| 6486 | unicode_mod(PyObject *v, PyObject *w) |
| 6487 | { |
| 6488 | if (!PyUnicode_Check(v)) { |
| 6489 | Py_INCREF(Py_NotImplemented); |
| 6490 | return Py_NotImplemented; |
| 6491 | } |
| 6492 | return PyUnicode_Format(v, w); |
| 6493 | } |
| 6494 | |
| 6495 | static PyNumberMethods unicode_as_number = { |
| 6496 | 0, /*nb_add*/ |
| 6497 | 0, /*nb_subtract*/ |
| 6498 | 0, /*nb_multiply*/ |
| 6499 | 0, /*nb_divide*/ |
| 6500 | unicode_mod, /*nb_remainder*/ |
| 6501 | }; |
| 6502 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6503 | static PySequenceMethods unicode_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6504 | (lenfunc) unicode_length, /* sq_length */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 6505 | PyUnicode_Concat, /* sq_concat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6506 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 6507 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 6508 | (ssizessizeargfunc) unicode_slice, /* sq_slice */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6509 | 0, /* sq_ass_item */ |
| 6510 | 0, /* sq_ass_slice */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 6511 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6512 | }; |
| 6513 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 6514 | #define HASINDEX(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_HAVE_INDEX) |
| 6515 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6516 | static PyObject* |
| 6517 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 6518 | { |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 6519 | PyNumberMethods *nb = item->ob_type->tp_as_number; |
| 6520 | if (nb != NULL && HASINDEX(item) && nb->nb_index != NULL) { |
| 6521 | Py_ssize_t i = nb->nb_index(item); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6522 | if (i == -1 && PyErr_Occurred()) |
| 6523 | return NULL; |
| 6524 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6525 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6526 | return unicode_getitem(self, i); |
| 6527 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6528 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6529 | Py_UNICODE* source_buf; |
| 6530 | Py_UNICODE* result_buf; |
| 6531 | PyObject* result; |
| 6532 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6533 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6534 | &start, &stop, &step, &slicelength) < 0) { |
| 6535 | return NULL; |
| 6536 | } |
| 6537 | |
| 6538 | if (slicelength <= 0) { |
| 6539 | return PyUnicode_FromUnicode(NULL, 0); |
| 6540 | } else { |
| 6541 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 6542 | result_buf = (Py_UNICODE *)PyMem_MALLOC(slicelength* |
| 6543 | sizeof(Py_UNICODE)); |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6544 | |
| 6545 | if (result_buf == NULL) |
| 6546 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6547 | |
| 6548 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 6549 | result_buf[i] = source_buf[cur]; |
| 6550 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6551 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6552 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
| 6553 | PyMem_FREE(result_buf); |
| 6554 | return result; |
| 6555 | } |
| 6556 | } else { |
| 6557 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 6558 | return NULL; |
| 6559 | } |
| 6560 | } |
| 6561 | |
| 6562 | static PyMappingMethods unicode_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6563 | (lenfunc)unicode_length, /* mp_length */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6564 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 6565 | (objobjargproc)0, /* mp_ass_subscript */ |
| 6566 | }; |
| 6567 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6568 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6569 | unicode_buffer_getreadbuf(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6570 | Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6571 | const void **ptr) |
| 6572 | { |
| 6573 | if (index != 0) { |
| 6574 | PyErr_SetString(PyExc_SystemError, |
| 6575 | "accessing non-existent unicode segment"); |
| 6576 | return -1; |
| 6577 | } |
| 6578 | *ptr = (void *) self->str; |
| 6579 | return PyUnicode_GET_DATA_SIZE(self); |
| 6580 | } |
| 6581 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6582 | static Py_ssize_t |
| 6583 | unicode_buffer_getwritebuf(PyUnicodeObject *self, Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6584 | const void **ptr) |
| 6585 | { |
| 6586 | PyErr_SetString(PyExc_TypeError, |
Neal Norwitz | 20e7213 | 2002-06-13 21:25:17 +0000 | [diff] [blame] | 6587 | "cannot use unicode as modifiable buffer"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6588 | return -1; |
| 6589 | } |
| 6590 | |
| 6591 | static int |
| 6592 | unicode_buffer_getsegcount(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6593 | Py_ssize_t *lenp) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6594 | { |
| 6595 | if (lenp) |
| 6596 | *lenp = PyUnicode_GET_DATA_SIZE(self); |
| 6597 | return 1; |
| 6598 | } |
| 6599 | |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 6600 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6601 | unicode_buffer_getcharbuf(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6602 | Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6603 | const void **ptr) |
| 6604 | { |
| 6605 | PyObject *str; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6606 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6607 | if (index != 0) { |
| 6608 | PyErr_SetString(PyExc_SystemError, |
| 6609 | "accessing non-existent unicode segment"); |
| 6610 | return -1; |
| 6611 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 6612 | str = _PyUnicode_AsDefaultEncodedString((PyObject *)self, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6613 | if (str == NULL) |
| 6614 | return -1; |
| 6615 | *ptr = (void *) PyString_AS_STRING(str); |
| 6616 | return PyString_GET_SIZE(str); |
| 6617 | } |
| 6618 | |
| 6619 | /* Helpers for PyUnicode_Format() */ |
| 6620 | |
| 6621 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6622 | 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] | 6623 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6624 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6625 | if (argidx < arglen) { |
| 6626 | (*p_argidx)++; |
| 6627 | if (arglen < 0) |
| 6628 | return args; |
| 6629 | else |
| 6630 | return PyTuple_GetItem(args, argidx); |
| 6631 | } |
| 6632 | PyErr_SetString(PyExc_TypeError, |
| 6633 | "not enough arguments for format string"); |
| 6634 | return NULL; |
| 6635 | } |
| 6636 | |
| 6637 | #define F_LJUST (1<<0) |
| 6638 | #define F_SIGN (1<<1) |
| 6639 | #define F_BLANK (1<<2) |
| 6640 | #define F_ALT (1<<3) |
| 6641 | #define F_ZERO (1<<4) |
| 6642 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6643 | static Py_ssize_t |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6644 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6645 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6646 | register Py_ssize_t i; |
| 6647 | Py_ssize_t len = strlen(charbuffer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6648 | for (i = len - 1; i >= 0; i--) |
| 6649 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
| 6650 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6651 | return len; |
| 6652 | } |
| 6653 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6654 | static int |
| 6655 | doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) |
| 6656 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 6657 | Py_ssize_t result; |
| 6658 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6659 | PyOS_ascii_formatd((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 6660 | result = strtounicode(buffer, (char *)buffer); |
| 6661 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6662 | } |
| 6663 | |
| 6664 | static int |
| 6665 | longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) |
| 6666 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 6667 | Py_ssize_t result; |
| 6668 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6669 | PyOS_snprintf((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 6670 | result = strtounicode(buffer, (char *)buffer); |
| 6671 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6672 | } |
| 6673 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 6674 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 6675 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 6676 | formatting is done. */ |
| 6677 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6678 | static int |
| 6679 | formatfloat(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6680 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6681 | int flags, |
| 6682 | int prec, |
| 6683 | int type, |
| 6684 | PyObject *v) |
| 6685 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6686 | /* fmt = '%#.' + `prec` + `type` |
| 6687 | 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] | 6688 | char fmt[20]; |
| 6689 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6690 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6691 | x = PyFloat_AsDouble(v); |
| 6692 | if (x == -1.0 && PyErr_Occurred()) |
| 6693 | return -1; |
| 6694 | if (prec < 0) |
| 6695 | prec = 6; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6696 | if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
| 6697 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6698 | /* Worst case length calc to ensure no buffer overrun: |
| 6699 | |
| 6700 | 'g' formats: |
| 6701 | fmt = %#.<prec>g |
| 6702 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 6703 | for any double rep.) |
| 6704 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 6705 | |
| 6706 | 'f' formats: |
| 6707 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 6708 | len = 1 + 50 + 1 + prec = 52 + prec |
| 6709 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6710 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6711 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6712 | |
| 6713 | */ |
| 6714 | if ((type == 'g' && buflen <= (size_t)10 + (size_t)prec) || |
| 6715 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6716 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6717 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6718 | return -1; |
| 6719 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6720 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 6721 | (flags&F_ALT) ? "#" : "", |
| 6722 | prec, type); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6723 | return doubletounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6724 | } |
| 6725 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6726 | static PyObject* |
| 6727 | formatlong(PyObject *val, int flags, int prec, int type) |
| 6728 | { |
| 6729 | char *buf; |
| 6730 | int i, len; |
| 6731 | PyObject *str; /* temporary string object. */ |
| 6732 | PyUnicodeObject *result; |
| 6733 | |
| 6734 | str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
| 6735 | if (!str) |
| 6736 | return NULL; |
| 6737 | result = _PyUnicode_New(len); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 6738 | if (!result) { |
| 6739 | Py_DECREF(str); |
| 6740 | return NULL; |
| 6741 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6742 | for (i = 0; i < len; i++) |
| 6743 | result->str[i] = buf[i]; |
| 6744 | result->str[len] = 0; |
| 6745 | Py_DECREF(str); |
| 6746 | return (PyObject*)result; |
| 6747 | } |
| 6748 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6749 | static int |
| 6750 | formatint(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6751 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6752 | int flags, |
| 6753 | int prec, |
| 6754 | int type, |
| 6755 | PyObject *v) |
| 6756 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6757 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6758 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 6759 | * + 1 + 1 |
| 6760 | * = 24 |
| 6761 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6762 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6763 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6764 | long x; |
| 6765 | |
| 6766 | x = PyInt_AsLong(v); |
| 6767 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6768 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6769 | if (x < 0 && type == 'u') { |
| 6770 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 6771 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6772 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 6773 | sign = "-"; |
| 6774 | else |
| 6775 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6776 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6777 | prec = 1; |
| 6778 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6779 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 6780 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6781 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6782 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6783 | PyErr_SetString(PyExc_OverflowError, |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6784 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6785 | return -1; |
| 6786 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6787 | |
| 6788 | if ((flags & F_ALT) && |
| 6789 | (type == 'x' || type == 'X')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6790 | /* When converting under %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6791 | * of issues that cause pain: |
| 6792 | * - when 0 is being converted, the C standard leaves off |
| 6793 | * the '0x' or '0X', which is inconsistent with other |
| 6794 | * %#x/%#X conversions and inconsistent with Python's |
| 6795 | * hex() function |
| 6796 | * - there are platforms that violate the standard and |
| 6797 | * convert 0 with the '0x' or '0X' |
| 6798 | * (Metrowerks, Compaq Tru64) |
| 6799 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6800 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6801 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6802 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6803 | * We can achieve the desired consistency by inserting our |
| 6804 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 6805 | * of %#x/%#X. |
| 6806 | * |
| 6807 | * Note that this is the same approach as used in |
| 6808 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 6809 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6810 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 6811 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 6812 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6813 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6814 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 6815 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6816 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 6817 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6818 | if (sign[0]) |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6819 | return longtounicode(buf, buflen, fmt, -x); |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6820 | else |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6821 | return longtounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6822 | } |
| 6823 | |
| 6824 | static int |
| 6825 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6826 | size_t buflen, |
| 6827 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6828 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6829 | /* presume that the buffer is at least 2 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6830 | if (PyUnicode_Check(v)) { |
| 6831 | if (PyUnicode_GET_SIZE(v) != 1) |
| 6832 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6833 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6834 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6835 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6836 | else if (PyString_Check(v)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6837 | if (PyString_GET_SIZE(v) != 1) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6838 | goto onError; |
| 6839 | buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0]; |
| 6840 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6841 | |
| 6842 | else { |
| 6843 | /* Integer input truncated to a character */ |
| 6844 | long x; |
| 6845 | x = PyInt_AsLong(v); |
| 6846 | if (x == -1 && PyErr_Occurred()) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6847 | goto onError; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 6848 | #ifdef Py_UNICODE_WIDE |
| 6849 | if (x < 0 || x > 0x10ffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 6850 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 6851 | "%c arg not in range(0x110000) " |
| 6852 | "(wide Python build)"); |
| 6853 | return -1; |
| 6854 | } |
| 6855 | #else |
| 6856 | if (x < 0 || x > 0xffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 6857 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 6858 | "%c arg not in range(0x10000) " |
| 6859 | "(narrow Python build)"); |
| 6860 | return -1; |
| 6861 | } |
| 6862 | #endif |
| 6863 | buf[0] = (Py_UNICODE) x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6864 | } |
| 6865 | buf[1] = '\0'; |
| 6866 | return 1; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6867 | |
| 6868 | onError: |
| 6869 | PyErr_SetString(PyExc_TypeError, |
| 6870 | "%c requires int or char"); |
| 6871 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6872 | } |
| 6873 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6874 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 6875 | |
| 6876 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 6877 | chars are formatted. XXX This is a magic number. Each formatting |
| 6878 | routine does bounds checking to ensure no overflow, but a better |
| 6879 | solution may be to malloc a buffer of appropriate size for each |
| 6880 | format. For now, the current solution is sufficient. |
| 6881 | */ |
| 6882 | #define FORMATBUFLEN (size_t)120 |
| 6883 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6884 | PyObject *PyUnicode_Format(PyObject *format, |
| 6885 | PyObject *args) |
| 6886 | { |
| 6887 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6888 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6889 | int args_owned = 0; |
| 6890 | PyUnicodeObject *result = NULL; |
| 6891 | PyObject *dict = NULL; |
| 6892 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6893 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6894 | if (format == NULL || args == NULL) { |
| 6895 | PyErr_BadInternalCall(); |
| 6896 | return NULL; |
| 6897 | } |
| 6898 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6899 | if (uformat == NULL) |
| 6900 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6901 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 6902 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 6903 | |
| 6904 | reslen = rescnt = fmtcnt + 100; |
| 6905 | result = _PyUnicode_New(reslen); |
| 6906 | if (result == NULL) |
| 6907 | goto onError; |
| 6908 | res = PyUnicode_AS_UNICODE(result); |
| 6909 | |
| 6910 | if (PyTuple_Check(args)) { |
| 6911 | arglen = PyTuple_Size(args); |
| 6912 | argidx = 0; |
| 6913 | } |
| 6914 | else { |
| 6915 | arglen = -1; |
| 6916 | argidx = -2; |
| 6917 | } |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 6918 | if (args->ob_type->tp_as_mapping && !PyTuple_Check(args) && |
| 6919 | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6920 | dict = args; |
| 6921 | |
| 6922 | while (--fmtcnt >= 0) { |
| 6923 | if (*fmt != '%') { |
| 6924 | if (--rescnt < 0) { |
| 6925 | rescnt = fmtcnt + 100; |
| 6926 | reslen += rescnt; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6927 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 6928 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6929 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 6930 | --rescnt; |
| 6931 | } |
| 6932 | *res++ = *fmt++; |
| 6933 | } |
| 6934 | else { |
| 6935 | /* Got a format specifier */ |
| 6936 | int flags = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6937 | Py_ssize_t width = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6938 | int prec = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6939 | Py_UNICODE c = '\0'; |
| 6940 | Py_UNICODE fill; |
| 6941 | PyObject *v = NULL; |
| 6942 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6943 | Py_UNICODE *pbuf; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6944 | Py_UNICODE sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6945 | Py_ssize_t len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6946 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6947 | |
| 6948 | fmt++; |
| 6949 | if (*fmt == '(') { |
| 6950 | Py_UNICODE *keystart; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6951 | Py_ssize_t keylen; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6952 | PyObject *key; |
| 6953 | int pcount = 1; |
| 6954 | |
| 6955 | if (dict == NULL) { |
| 6956 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6957 | "format requires a mapping"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6958 | goto onError; |
| 6959 | } |
| 6960 | ++fmt; |
| 6961 | --fmtcnt; |
| 6962 | keystart = fmt; |
| 6963 | /* Skip over balanced parentheses */ |
| 6964 | while (pcount > 0 && --fmtcnt >= 0) { |
| 6965 | if (*fmt == ')') |
| 6966 | --pcount; |
| 6967 | else if (*fmt == '(') |
| 6968 | ++pcount; |
| 6969 | fmt++; |
| 6970 | } |
| 6971 | keylen = fmt - keystart - 1; |
| 6972 | if (fmtcnt < 0 || pcount > 0) { |
| 6973 | PyErr_SetString(PyExc_ValueError, |
| 6974 | "incomplete format key"); |
| 6975 | goto onError; |
| 6976 | } |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 6977 | #if 0 |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6978 | /* keys are converted to strings using UTF-8 and |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6979 | then looked up since Python uses strings to hold |
| 6980 | variables names etc. in its namespaces and we |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6981 | wouldn't want to break common idioms. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6982 | key = PyUnicode_EncodeUTF8(keystart, |
| 6983 | keylen, |
| 6984 | NULL); |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 6985 | #else |
| 6986 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 6987 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6988 | if (key == NULL) |
| 6989 | goto onError; |
| 6990 | if (args_owned) { |
| 6991 | Py_DECREF(args); |
| 6992 | args_owned = 0; |
| 6993 | } |
| 6994 | args = PyObject_GetItem(dict, key); |
| 6995 | Py_DECREF(key); |
| 6996 | if (args == NULL) { |
| 6997 | goto onError; |
| 6998 | } |
| 6999 | args_owned = 1; |
| 7000 | arglen = -1; |
| 7001 | argidx = -2; |
| 7002 | } |
| 7003 | while (--fmtcnt >= 0) { |
| 7004 | switch (c = *fmt++) { |
| 7005 | case '-': flags |= F_LJUST; continue; |
| 7006 | case '+': flags |= F_SIGN; continue; |
| 7007 | case ' ': flags |= F_BLANK; continue; |
| 7008 | case '#': flags |= F_ALT; continue; |
| 7009 | case '0': flags |= F_ZERO; continue; |
| 7010 | } |
| 7011 | break; |
| 7012 | } |
| 7013 | if (c == '*') { |
| 7014 | v = getnextarg(args, arglen, &argidx); |
| 7015 | if (v == NULL) |
| 7016 | goto onError; |
| 7017 | if (!PyInt_Check(v)) { |
| 7018 | PyErr_SetString(PyExc_TypeError, |
| 7019 | "* wants int"); |
| 7020 | goto onError; |
| 7021 | } |
| 7022 | width = PyInt_AsLong(v); |
| 7023 | if (width < 0) { |
| 7024 | flags |= F_LJUST; |
| 7025 | width = -width; |
| 7026 | } |
| 7027 | if (--fmtcnt >= 0) |
| 7028 | c = *fmt++; |
| 7029 | } |
| 7030 | else if (c >= '0' && c <= '9') { |
| 7031 | width = c - '0'; |
| 7032 | while (--fmtcnt >= 0) { |
| 7033 | c = *fmt++; |
| 7034 | if (c < '0' || c > '9') |
| 7035 | break; |
| 7036 | if ((width*10) / 10 != width) { |
| 7037 | PyErr_SetString(PyExc_ValueError, |
| 7038 | "width too big"); |
| 7039 | goto onError; |
| 7040 | } |
| 7041 | width = width*10 + (c - '0'); |
| 7042 | } |
| 7043 | } |
| 7044 | if (c == '.') { |
| 7045 | prec = 0; |
| 7046 | if (--fmtcnt >= 0) |
| 7047 | c = *fmt++; |
| 7048 | if (c == '*') { |
| 7049 | v = getnextarg(args, arglen, &argidx); |
| 7050 | if (v == NULL) |
| 7051 | goto onError; |
| 7052 | if (!PyInt_Check(v)) { |
| 7053 | PyErr_SetString(PyExc_TypeError, |
| 7054 | "* wants int"); |
| 7055 | goto onError; |
| 7056 | } |
| 7057 | prec = PyInt_AsLong(v); |
| 7058 | if (prec < 0) |
| 7059 | prec = 0; |
| 7060 | if (--fmtcnt >= 0) |
| 7061 | c = *fmt++; |
| 7062 | } |
| 7063 | else if (c >= '0' && c <= '9') { |
| 7064 | prec = c - '0'; |
| 7065 | while (--fmtcnt >= 0) { |
| 7066 | c = Py_CHARMASK(*fmt++); |
| 7067 | if (c < '0' || c > '9') |
| 7068 | break; |
| 7069 | if ((prec*10) / 10 != prec) { |
| 7070 | PyErr_SetString(PyExc_ValueError, |
| 7071 | "prec too big"); |
| 7072 | goto onError; |
| 7073 | } |
| 7074 | prec = prec*10 + (c - '0'); |
| 7075 | } |
| 7076 | } |
| 7077 | } /* prec */ |
| 7078 | if (fmtcnt >= 0) { |
| 7079 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7080 | if (--fmtcnt >= 0) |
| 7081 | c = *fmt++; |
| 7082 | } |
| 7083 | } |
| 7084 | if (fmtcnt < 0) { |
| 7085 | PyErr_SetString(PyExc_ValueError, |
| 7086 | "incomplete format"); |
| 7087 | goto onError; |
| 7088 | } |
| 7089 | if (c != '%') { |
| 7090 | v = getnextarg(args, arglen, &argidx); |
| 7091 | if (v == NULL) |
| 7092 | goto onError; |
| 7093 | } |
| 7094 | sign = 0; |
| 7095 | fill = ' '; |
| 7096 | switch (c) { |
| 7097 | |
| 7098 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7099 | pbuf = formatbuf; |
| 7100 | /* presume that buffer length is at least 1 */ |
| 7101 | pbuf[0] = '%'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7102 | len = 1; |
| 7103 | break; |
| 7104 | |
| 7105 | case 's': |
| 7106 | case 'r': |
| 7107 | if (PyUnicode_Check(v) && c == 's') { |
| 7108 | temp = v; |
| 7109 | Py_INCREF(temp); |
| 7110 | } |
| 7111 | else { |
| 7112 | PyObject *unicode; |
| 7113 | if (c == 's') |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 7114 | temp = PyObject_Unicode(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7115 | else |
| 7116 | temp = PyObject_Repr(v); |
| 7117 | if (temp == NULL) |
| 7118 | goto onError; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 7119 | if (PyUnicode_Check(temp)) |
| 7120 | /* nothing to do */; |
| 7121 | else if (PyString_Check(temp)) { |
| 7122 | /* convert to string to Unicode */ |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7123 | unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7124 | PyString_GET_SIZE(temp), |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7125 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7126 | "strict"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7127 | Py_DECREF(temp); |
| 7128 | temp = unicode; |
| 7129 | if (temp == NULL) |
| 7130 | goto onError; |
| 7131 | } |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 7132 | else { |
| 7133 | Py_DECREF(temp); |
| 7134 | PyErr_SetString(PyExc_TypeError, |
| 7135 | "%s argument has non-string str()"); |
| 7136 | goto onError; |
| 7137 | } |
| 7138 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7139 | pbuf = PyUnicode_AS_UNICODE(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7140 | len = PyUnicode_GET_SIZE(temp); |
| 7141 | if (prec >= 0 && len > prec) |
| 7142 | len = prec; |
| 7143 | break; |
| 7144 | |
| 7145 | case 'i': |
| 7146 | case 'd': |
| 7147 | case 'u': |
| 7148 | case 'o': |
| 7149 | case 'x': |
| 7150 | case 'X': |
| 7151 | if (c == 'i') |
| 7152 | c = 'd'; |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 7153 | if (PyLong_Check(v)) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7154 | temp = formatlong(v, flags, prec, c); |
| 7155 | if (!temp) |
| 7156 | goto onError; |
| 7157 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 7158 | len = PyUnicode_GET_SIZE(temp); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7159 | sign = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7160 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7161 | else { |
| 7162 | pbuf = formatbuf; |
| 7163 | len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 7164 | flags, prec, c, v); |
| 7165 | if (len < 0) |
| 7166 | goto onError; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7167 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7168 | } |
| 7169 | if (flags & F_ZERO) |
| 7170 | fill = '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7171 | break; |
| 7172 | |
| 7173 | case 'e': |
| 7174 | case 'E': |
| 7175 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 7176 | case 'F': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7177 | case 'g': |
| 7178 | case 'G': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 7179 | if (c == 'F') |
| 7180 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7181 | pbuf = formatbuf; |
| 7182 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 7183 | flags, prec, c, v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7184 | if (len < 0) |
| 7185 | goto onError; |
| 7186 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7187 | if (flags & F_ZERO) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7188 | fill = '0'; |
| 7189 | break; |
| 7190 | |
| 7191 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7192 | pbuf = formatbuf; |
| 7193 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7194 | if (len < 0) |
| 7195 | goto onError; |
| 7196 | break; |
| 7197 | |
| 7198 | default: |
| 7199 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 7200 | "unsupported format character '%c' (0x%x) " |
| 7201 | "at index %i", |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7202 | (31<=c && c<=126) ? (char)c : '?', |
Marc-André Lemburg | 24e53b6 | 2002-09-24 09:32:14 +0000 | [diff] [blame] | 7203 | (int)c, |
Guido van Rossum | efc1188 | 2002-09-12 14:43:41 +0000 | [diff] [blame] | 7204 | (int)(fmt -1 - PyUnicode_AS_UNICODE(uformat))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7205 | goto onError; |
| 7206 | } |
| 7207 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7208 | if (*pbuf == '-' || *pbuf == '+') { |
| 7209 | sign = *pbuf++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7210 | len--; |
| 7211 | } |
| 7212 | else if (flags & F_SIGN) |
| 7213 | sign = '+'; |
| 7214 | else if (flags & F_BLANK) |
| 7215 | sign = ' '; |
| 7216 | else |
| 7217 | sign = 0; |
| 7218 | } |
| 7219 | if (width < len) |
| 7220 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 7221 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7222 | reslen -= rescnt; |
| 7223 | rescnt = width + fmtcnt + 100; |
| 7224 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 7225 | if (reslen < 0) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 7226 | Py_XDECREF(temp); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7227 | PyErr_NoMemory(); |
| 7228 | goto onError; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 7229 | } |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7230 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 7231 | Py_XDECREF(temp); |
| 7232 | goto onError; |
| 7233 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7234 | res = PyUnicode_AS_UNICODE(result) |
| 7235 | + reslen - rescnt; |
| 7236 | } |
| 7237 | if (sign) { |
| 7238 | if (fill != ' ') |
| 7239 | *res++ = sign; |
| 7240 | rescnt--; |
| 7241 | if (width > len) |
| 7242 | width--; |
| 7243 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7244 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 7245 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7246 | assert(pbuf[1] == c); |
| 7247 | if (fill != ' ') { |
| 7248 | *res++ = *pbuf++; |
| 7249 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7250 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7251 | rescnt -= 2; |
| 7252 | width -= 2; |
| 7253 | if (width < 0) |
| 7254 | width = 0; |
| 7255 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7256 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7257 | if (width > len && !(flags & F_LJUST)) { |
| 7258 | do { |
| 7259 | --rescnt; |
| 7260 | *res++ = fill; |
| 7261 | } while (--width > len); |
| 7262 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7263 | if (fill == ' ') { |
| 7264 | if (sign) |
| 7265 | *res++ = sign; |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7266 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7267 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7268 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7269 | *res++ = *pbuf++; |
| 7270 | *res++ = *pbuf++; |
| 7271 | } |
| 7272 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7273 | Py_UNICODE_COPY(res, pbuf, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7274 | res += len; |
| 7275 | rescnt -= len; |
| 7276 | while (--width >= len) { |
| 7277 | --rescnt; |
| 7278 | *res++ = ' '; |
| 7279 | } |
| 7280 | if (dict && (argidx < arglen) && c != '%') { |
| 7281 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 7282 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7283 | Py_XDECREF(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7284 | goto onError; |
| 7285 | } |
| 7286 | Py_XDECREF(temp); |
| 7287 | } /* '%' */ |
| 7288 | } /* until end */ |
| 7289 | if (argidx < arglen && !dict) { |
| 7290 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 7291 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7292 | goto onError; |
| 7293 | } |
| 7294 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7295 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
| 7296 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7297 | if (args_owned) { |
| 7298 | Py_DECREF(args); |
| 7299 | } |
| 7300 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7301 | return (PyObject *)result; |
| 7302 | |
| 7303 | onError: |
| 7304 | Py_XDECREF(result); |
| 7305 | Py_DECREF(uformat); |
| 7306 | if (args_owned) { |
| 7307 | Py_DECREF(args); |
| 7308 | } |
| 7309 | return NULL; |
| 7310 | } |
| 7311 | |
| 7312 | static PyBufferProcs unicode_as_buffer = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7313 | (readbufferproc) unicode_buffer_getreadbuf, |
| 7314 | (writebufferproc) unicode_buffer_getwritebuf, |
| 7315 | (segcountproc) unicode_buffer_getsegcount, |
| 7316 | (charbufferproc) unicode_buffer_getcharbuf, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7317 | }; |
| 7318 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 7319 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7320 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 7321 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7322 | static PyObject * |
| 7323 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 7324 | { |
| 7325 | PyObject *x = NULL; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 7326 | static char *kwlist[] = {"string", "encoding", "errors", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7327 | char *encoding = NULL; |
| 7328 | char *errors = NULL; |
| 7329 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7330 | if (type != &PyUnicode_Type) |
| 7331 | return unicode_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7332 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
| 7333 | kwlist, &x, &encoding, &errors)) |
| 7334 | return NULL; |
| 7335 | if (x == NULL) |
| 7336 | return (PyObject *)_PyUnicode_New(0); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 7337 | if (encoding == NULL && errors == NULL) |
| 7338 | return PyObject_Unicode(x); |
| 7339 | else |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7340 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
| 7341 | } |
| 7342 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7343 | static PyObject * |
| 7344 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 7345 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7346 | PyUnicodeObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7347 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7348 | |
| 7349 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 7350 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 7351 | if (tmp == NULL) |
| 7352 | return NULL; |
| 7353 | assert(PyUnicode_Check(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7354 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7355 | if (pnew == NULL) { |
| 7356 | Py_DECREF(tmp); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7357 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7358 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7359 | pnew->str = PyMem_NEW(Py_UNICODE, n+1); |
| 7360 | if (pnew->str == NULL) { |
| 7361 | _Py_ForgetReference((PyObject *)pnew); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 7362 | PyObject_Del(pnew); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7363 | Py_DECREF(tmp); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 7364 | return PyErr_NoMemory(); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7365 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7366 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 7367 | pnew->length = n; |
| 7368 | pnew->hash = tmp->hash; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7369 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7370 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7371 | } |
| 7372 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7373 | PyDoc_STRVAR(unicode_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7374 | "unicode(string [, encoding[, errors]]) -> object\n\ |
| 7375 | \n\ |
| 7376 | Create a new Unicode object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 7377 | encoding defaults to the current default string encoding.\n\ |
| 7378 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7379 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7380 | PyTypeObject PyUnicode_Type = { |
| 7381 | PyObject_HEAD_INIT(&PyType_Type) |
| 7382 | 0, /* ob_size */ |
| 7383 | "unicode", /* tp_name */ |
| 7384 | sizeof(PyUnicodeObject), /* tp_size */ |
| 7385 | 0, /* tp_itemsize */ |
| 7386 | /* Slots */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 7387 | (destructor)unicode_dealloc, /* tp_dealloc */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7388 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7389 | 0, /* tp_getattr */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7390 | 0, /* tp_setattr */ |
| 7391 | (cmpfunc) unicode_compare, /* tp_compare */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 7392 | unicode_repr, /* tp_repr */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7393 | &unicode_as_number, /* tp_as_number */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7394 | &unicode_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7395 | &unicode_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7396 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 7397 | 0, /* tp_call*/ |
| 7398 | (reprfunc) unicode_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7399 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 7400 | 0, /* tp_setattro */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7401 | &unicode_as_buffer, /* tp_as_buffer */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7402 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
| 7403 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7404 | unicode_doc, /* tp_doc */ |
| 7405 | 0, /* tp_traverse */ |
| 7406 | 0, /* tp_clear */ |
| 7407 | 0, /* tp_richcompare */ |
| 7408 | 0, /* tp_weaklistoffset */ |
| 7409 | 0, /* tp_iter */ |
| 7410 | 0, /* tp_iternext */ |
| 7411 | unicode_methods, /* tp_methods */ |
| 7412 | 0, /* tp_members */ |
| 7413 | 0, /* tp_getset */ |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 7414 | &PyBaseString_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7415 | 0, /* tp_dict */ |
| 7416 | 0, /* tp_descr_get */ |
| 7417 | 0, /* tp_descr_set */ |
| 7418 | 0, /* tp_dictoffset */ |
| 7419 | 0, /* tp_init */ |
| 7420 | 0, /* tp_alloc */ |
| 7421 | unicode_new, /* tp_new */ |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 7422 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7423 | }; |
| 7424 | |
| 7425 | /* Initialize the Unicode implementation */ |
| 7426 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 7427 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7428 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7429 | int i; |
| 7430 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 7431 | /* XXX - move this array to unicodectype.c ? */ |
| 7432 | Py_UNICODE linebreak[] = { |
| 7433 | 0x000A, /* LINE FEED */ |
| 7434 | 0x000D, /* CARRIAGE RETURN */ |
| 7435 | 0x001C, /* FILE SEPARATOR */ |
| 7436 | 0x001D, /* GROUP SEPARATOR */ |
| 7437 | 0x001E, /* RECORD SEPARATOR */ |
| 7438 | 0x0085, /* NEXT LINE */ |
| 7439 | 0x2028, /* LINE SEPARATOR */ |
| 7440 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 7441 | }; |
| 7442 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7443 | /* Init the implementation */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7444 | unicode_freelist = NULL; |
| 7445 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7446 | unicode_empty = _PyUnicode_New(0); |
Marc-André Lemburg | 90e8147 | 2000-06-07 09:13:21 +0000 | [diff] [blame] | 7447 | strcpy(unicode_default_encoding, "ascii"); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7448 | for (i = 0; i < 256; i++) |
| 7449 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 7450 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 7451 | Py_FatalError("Can't initialize 'unicode'"); |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 7452 | |
| 7453 | /* initialize the linebreak bloom filter */ |
| 7454 | bloom_linebreak = make_bloom_mask( |
| 7455 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 7456 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7457 | } |
| 7458 | |
| 7459 | /* Finalize the Unicode implementation */ |
| 7460 | |
| 7461 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 7462 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7463 | { |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 7464 | PyUnicodeObject *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7465 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7466 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 7467 | Py_XDECREF(unicode_empty); |
| 7468 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 7469 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7470 | for (i = 0; i < 256; i++) { |
| 7471 | if (unicode_latin1[i]) { |
| 7472 | Py_DECREF(unicode_latin1[i]); |
| 7473 | unicode_latin1[i] = NULL; |
| 7474 | } |
| 7475 | } |
| 7476 | |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 7477 | for (u = unicode_freelist; u != NULL;) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7478 | PyUnicodeObject *v = u; |
| 7479 | u = *(PyUnicodeObject **)u; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 7480 | if (v->str) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 7481 | PyMem_DEL(v->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 7482 | Py_XDECREF(v->defenc); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 7483 | PyObject_Del(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7484 | } |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7485 | unicode_freelist = NULL; |
| 7486 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7487 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 7488 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 7489 | #ifdef __cplusplus |
| 7490 | } |
| 7491 | #endif |
| 7492 | |
| 7493 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 7494 | /* |
| 7495 | Local variables: |
| 7496 | c-basic-offset: 4 |
| 7497 | indent-tabs-mode: nil |
| 7498 | End: |
| 7499 | */ |