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 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7 | Major speed upgrades to the method implementations at the Reykjavik |
| 8 | NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke. |
| 9 | |
Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 10 | Copyright (c) Corporation for National Research Initiatives. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 12 | -------------------------------------------------------------------- |
| 13 | The original string type implementation is: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 15 | Copyright (c) 1999 by Secret Labs AB |
| 16 | Copyright (c) 1999 by Fredrik Lundh |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 17 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 18 | By obtaining, using, and/or copying this software and/or its |
| 19 | associated documentation, you agree that you have read, understood, |
| 20 | and will comply with the following terms and conditions: |
| 21 | |
| 22 | Permission to use, copy, modify, and distribute this software and its |
| 23 | associated documentation for any purpose and without fee is hereby |
| 24 | granted, provided that the above copyright notice appears in all |
| 25 | copies, and that both that copyright notice and this permission notice |
| 26 | appear in supporting documentation, and that the name of Secret Labs |
| 27 | AB or the author not be used in advertising or publicity pertaining to |
| 28 | distribution of the software without specific, written prior |
| 29 | permission. |
| 30 | |
| 31 | SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 32 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 33 | FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR |
| 34 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 35 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 36 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 37 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 38 | -------------------------------------------------------------------- |
| 39 | |
| 40 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 41 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 42 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 43 | #include "Python.h" |
| 44 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 45 | #include "unicodeobject.h" |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 46 | #include "ucnhash.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 47 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 48 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 49 | #include <windows.h> |
| 50 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 52 | /* Limit for the Unicode object free list */ |
| 53 | |
| 54 | #define MAX_UNICODE_FREELIST_SIZE 1024 |
| 55 | |
| 56 | /* Limit for the Unicode object free list stay alive optimization. |
| 57 | |
| 58 | The implementation will keep allocated Unicode memory intact for |
| 59 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 60 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 61 | |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 62 | At worst this will result in MAX_UNICODE_FREELIST_SIZE * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 63 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 64 | malloc()-overhead) bytes of unused garbage. |
| 65 | |
| 66 | Setting the limit to 0 effectively turns the feature off. |
| 67 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 68 | Note: This is an experimental feature ! If you get core dumps when |
| 69 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 70 | |
| 71 | */ |
| 72 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 73 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 74 | |
| 75 | /* Endianness switches; defaults to little endian */ |
| 76 | |
| 77 | #ifdef WORDS_BIGENDIAN |
| 78 | # define BYTEORDER_IS_BIG_ENDIAN |
| 79 | #else |
| 80 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 81 | #endif |
| 82 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 83 | /* --- Globals ------------------------------------------------------------ |
| 84 | |
| 85 | The globals are initialized by the _PyUnicode_Init() API and should |
| 86 | not be used before calling that API. |
| 87 | |
| 88 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 89 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 90 | |
| 91 | #ifdef __cplusplus |
| 92 | extern "C" { |
| 93 | #endif |
| 94 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 95 | /* Free list for Unicode objects */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 96 | static PyUnicodeObject *unicode_freelist; |
| 97 | static int unicode_freelist_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 98 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 99 | /* The empty Unicode object is shared to improve performance. */ |
| 100 | static PyUnicodeObject *unicode_empty; |
| 101 | |
| 102 | /* Single character Unicode strings in the Latin-1 range are being |
| 103 | shared as well. */ |
| 104 | static PyUnicodeObject *unicode_latin1[256]; |
| 105 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 106 | /* Default encoding to use and assume when NULL is passed as encoding |
| 107 | parameter; it is initialized by _PyUnicode_Init(). |
| 108 | |
| 109 | Always use the PyUnicode_SetDefaultEncoding() and |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 110 | PyUnicode_GetDefaultEncoding() APIs to access this global. |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 111 | |
| 112 | */ |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 113 | static char unicode_default_encoding[100]; |
| 114 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 115 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 116 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 117 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 118 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 119 | return 0x10FFFF; |
| 120 | #else |
| 121 | /* This is actually an illegal character, so it should |
| 122 | not be passed to unichr. */ |
| 123 | return 0xFFFF; |
| 124 | #endif |
| 125 | } |
| 126 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 127 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 128 | |
| 129 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 130 | to keep things simple, we use a single bitmask, using the least 5 |
| 131 | bits from each unicode characters as the bit index. */ |
| 132 | |
| 133 | /* the linebreak mask is set up by Unicode_Init below */ |
| 134 | |
| 135 | #define BLOOM_MASK unsigned long |
| 136 | |
| 137 | static BLOOM_MASK bloom_linebreak; |
| 138 | |
| 139 | #define BLOOM(mask, ch) ((mask & (1 << ((ch) & 0x1F)))) |
| 140 | |
| 141 | #define BLOOM_LINEBREAK(ch)\ |
| 142 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK((ch))) |
| 143 | |
| 144 | Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len) |
| 145 | { |
| 146 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 147 | |
| 148 | long mask; |
| 149 | Py_ssize_t i; |
| 150 | |
| 151 | mask = 0; |
| 152 | for (i = 0; i < len; i++) |
| 153 | mask |= (1 << (ptr[i] & 0x1F)); |
| 154 | |
| 155 | return mask; |
| 156 | } |
| 157 | |
| 158 | Py_LOCAL_INLINE(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen) |
| 159 | { |
| 160 | Py_ssize_t i; |
| 161 | |
| 162 | for (i = 0; i < setlen; i++) |
| 163 | if (set[i] == chr) |
| 164 | return 1; |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | #define BLOOM_MEMBER(mask, chr, set, setlen)\ |
| 170 | BLOOM(mask, chr) && unicode_member(chr, set, setlen) |
| 171 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 172 | /* --- Unicode Object ----------------------------------------------------- */ |
| 173 | |
| 174 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 175 | int unicode_resize(register PyUnicodeObject *unicode, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 176 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 177 | { |
| 178 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 179 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 180 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 181 | if (unicode->length == length) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 182 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 183 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 184 | /* Resizing shared object (unicode_empty or single character |
| 185 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 186 | instead ! */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 187 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 188 | if (unicode == unicode_empty || |
| 189 | (unicode->length == 1 && |
| 190 | unicode->str[0] < 256U && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 191 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 192 | PyErr_SetString(PyExc_SystemError, |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 193 | "can't resize shared unicode objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 194 | return -1; |
| 195 | } |
| 196 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 197 | /* We allocate one more byte to make sure the string is Ux0000 terminated. |
| 198 | The overallocation is also used by fastsearch, which assumes that it's |
| 199 | safe to look at str[length] (without making any assumptions about what |
| 200 | it contains). */ |
| 201 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 202 | oldstr = unicode->str; |
| 203 | PyMem_RESIZE(unicode->str, Py_UNICODE, length + 1); |
| 204 | if (!unicode->str) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 205 | unicode->str = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 206 | PyErr_NoMemory(); |
| 207 | return -1; |
| 208 | } |
| 209 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 210 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 211 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 212 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 213 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 214 | if (unicode->defenc) { |
| 215 | Py_DECREF(unicode->defenc); |
| 216 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 217 | } |
| 218 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 219 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | /* We allocate one more byte to make sure the string is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 224 | Ux0000 terminated -- XXX is this needed ? |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 225 | |
| 226 | XXX This allocator could further be enhanced by assuring that the |
| 227 | free list never reduces its size below 1. |
| 228 | |
| 229 | */ |
| 230 | |
| 231 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 232 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 233 | { |
| 234 | register PyUnicodeObject *unicode; |
| 235 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 236 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 237 | if (length == 0 && unicode_empty != NULL) { |
| 238 | Py_INCREF(unicode_empty); |
| 239 | return unicode_empty; |
| 240 | } |
| 241 | |
| 242 | /* Unicode freelist & memory allocation */ |
| 243 | if (unicode_freelist) { |
| 244 | unicode = unicode_freelist; |
Marc-André Lemburg | bea47e7 | 2000-06-17 20:31:17 +0000 | [diff] [blame] | 245 | unicode_freelist = *(PyUnicodeObject **)unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 246 | unicode_freelist_size--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 247 | if (unicode->str) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 248 | /* Keep-Alive optimization: we only upsize the buffer, |
| 249 | never downsize it. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 250 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 251 | unicode_resize(unicode, length) < 0) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 252 | PyMem_DEL(unicode->str); |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 253 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 254 | } |
| 255 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 256 | else { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 257 | unicode->str = PyMem_NEW(Py_UNICODE, length + 1); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 258 | } |
| 259 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 260 | } |
| 261 | else { |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 262 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 263 | if (unicode == NULL) |
| 264 | return NULL; |
| 265 | unicode->str = PyMem_NEW(Py_UNICODE, length + 1); |
| 266 | } |
| 267 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 268 | if (!unicode->str) { |
| 269 | PyErr_NoMemory(); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 270 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 271 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 272 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 273 | * the caller fails before initializing str -- unicode_resize() |
| 274 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 275 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 276 | * We don't want unicode_resize to read uninitialized memory in |
| 277 | * that case. |
| 278 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 279 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 280 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 281 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 282 | unicode->hash = -1; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 283 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 284 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 285 | |
| 286 | onError: |
| 287 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 288 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 289 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 293 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 294 | { |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 295 | if (PyUnicode_CheckExact(unicode) && |
| 296 | unicode_freelist_size < MAX_UNICODE_FREELIST_SIZE) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 297 | /* Keep-Alive optimization */ |
| 298 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 299 | PyMem_DEL(unicode->str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 300 | unicode->str = NULL; |
| 301 | unicode->length = 0; |
| 302 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 303 | if (unicode->defenc) { |
| 304 | Py_DECREF(unicode->defenc); |
| 305 | unicode->defenc = NULL; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 306 | } |
| 307 | /* Add to free list */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 308 | *(PyUnicodeObject **)unicode = unicode_freelist; |
| 309 | unicode_freelist = unicode; |
| 310 | unicode_freelist_size++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 311 | } |
| 312 | else { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 313 | PyMem_DEL(unicode->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 314 | Py_XDECREF(unicode->defenc); |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 315 | unicode->ob_type->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 319 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 320 | { |
| 321 | register PyUnicodeObject *v; |
| 322 | |
| 323 | /* Argument checks */ |
| 324 | if (unicode == NULL) { |
| 325 | PyErr_BadInternalCall(); |
| 326 | return -1; |
| 327 | } |
| 328 | v = (PyUnicodeObject *)*unicode; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 329 | 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] | 330 | PyErr_BadInternalCall(); |
| 331 | return -1; |
| 332 | } |
| 333 | |
| 334 | /* Resizing unicode_empty and single character objects is not |
| 335 | possible since these are being shared. We simply return a fresh |
| 336 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 337 | if (v->length != length && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 338 | (v == unicode_empty || v->length == 1)) { |
| 339 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 340 | if (w == NULL) |
| 341 | return -1; |
| 342 | Py_UNICODE_COPY(w->str, v->str, |
| 343 | length < v->length ? length : v->length); |
Raymond Hettinger | c8df578 | 2003-03-09 07:30:43 +0000 | [diff] [blame] | 344 | Py_DECREF(*unicode); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 345 | *unicode = (PyObject *)w; |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 350 | objects, since we can modify them in-place. */ |
| 351 | return unicode_resize(v, length); |
| 352 | } |
| 353 | |
| 354 | /* Internal API for use in unicodeobject.c only ! */ |
| 355 | #define _PyUnicode_Resize(unicodevar, length) \ |
| 356 | PyUnicode_Resize(((PyObject **)(unicodevar)), length) |
| 357 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 358 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 359 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 360 | { |
| 361 | PyUnicodeObject *unicode; |
| 362 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 363 | /* If the Unicode data is known at construction time, we can apply |
| 364 | some optimizations which share commonly used objects. */ |
| 365 | if (u != NULL) { |
| 366 | |
| 367 | /* Optimization for empty strings */ |
| 368 | if (size == 0 && unicode_empty != NULL) { |
| 369 | Py_INCREF(unicode_empty); |
| 370 | return (PyObject *)unicode_empty; |
| 371 | } |
| 372 | |
| 373 | /* Single character Unicode objects in the Latin-1 range are |
| 374 | shared when using this constructor */ |
| 375 | if (size == 1 && *u < 256) { |
| 376 | unicode = unicode_latin1[*u]; |
| 377 | if (!unicode) { |
| 378 | unicode = _PyUnicode_New(1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 379 | if (!unicode) |
| 380 | return NULL; |
Marc-André Lemburg | 8879a33 | 2001-06-07 12:26:56 +0000 | [diff] [blame] | 381 | unicode->str[0] = *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 382 | unicode_latin1[*u] = unicode; |
| 383 | } |
| 384 | Py_INCREF(unicode); |
| 385 | return (PyObject *)unicode; |
| 386 | } |
| 387 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 388 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 389 | unicode = _PyUnicode_New(size); |
| 390 | if (!unicode) |
| 391 | return NULL; |
| 392 | |
| 393 | /* Copy the Unicode data into the new object */ |
| 394 | if (u != NULL) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 395 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 396 | |
| 397 | return (PyObject *)unicode; |
| 398 | } |
| 399 | |
| 400 | #ifdef HAVE_WCHAR_H |
| 401 | |
| 402 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 403 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 404 | { |
| 405 | PyUnicodeObject *unicode; |
| 406 | |
| 407 | if (w == NULL) { |
| 408 | PyErr_BadInternalCall(); |
| 409 | return NULL; |
| 410 | } |
| 411 | |
| 412 | unicode = _PyUnicode_New(size); |
| 413 | if (!unicode) |
| 414 | return NULL; |
| 415 | |
| 416 | /* Copy the wchar_t data into the new object */ |
| 417 | #ifdef HAVE_USABLE_WCHAR_T |
| 418 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 419 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 420 | { |
| 421 | register Py_UNICODE *u; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 422 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 423 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 424 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 425 | *u++ = *w++; |
| 426 | } |
| 427 | #endif |
| 428 | |
| 429 | return (PyObject *)unicode; |
| 430 | } |
| 431 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 432 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 433 | wchar_t *w, |
| 434 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 435 | { |
| 436 | if (unicode == NULL) { |
| 437 | PyErr_BadInternalCall(); |
| 438 | return -1; |
| 439 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 440 | |
| 441 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 442 | if (size > PyUnicode_GET_SIZE(unicode)) |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 443 | size = PyUnicode_GET_SIZE(unicode) + 1; |
| 444 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 445 | #ifdef HAVE_USABLE_WCHAR_T |
| 446 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 447 | #else |
| 448 | { |
| 449 | register Py_UNICODE *u; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 450 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 451 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 452 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 453 | *w++ = *u++; |
| 454 | } |
| 455 | #endif |
| 456 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 457 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 458 | return PyUnicode_GET_SIZE(unicode); |
| 459 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 460 | return size; |
| 461 | } |
| 462 | |
| 463 | #endif |
| 464 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 465 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 466 | { |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 467 | Py_UNICODE s[1]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 468 | |
| 469 | #ifdef Py_UNICODE_WIDE |
| 470 | if (ordinal < 0 || ordinal > 0x10ffff) { |
| 471 | PyErr_SetString(PyExc_ValueError, |
| 472 | "unichr() arg not in range(0x110000) " |
| 473 | "(wide Python build)"); |
| 474 | return NULL; |
| 475 | } |
| 476 | #else |
| 477 | if (ordinal < 0 || ordinal > 0xffff) { |
| 478 | PyErr_SetString(PyExc_ValueError, |
| 479 | "unichr() arg not in range(0x10000) " |
| 480 | "(narrow Python build)"); |
| 481 | return NULL; |
| 482 | } |
| 483 | #endif |
| 484 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 485 | s[0] = (Py_UNICODE)ordinal; |
| 486 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 489 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 490 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 491 | /* XXX Perhaps we should make this API an alias of |
| 492 | PyObject_Unicode() instead ?! */ |
| 493 | if (PyUnicode_CheckExact(obj)) { |
| 494 | Py_INCREF(obj); |
| 495 | return obj; |
| 496 | } |
| 497 | if (PyUnicode_Check(obj)) { |
| 498 | /* For a Unicode subtype that's not a Unicode object, |
| 499 | return a true Unicode object with the same data. */ |
| 500 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 501 | PyUnicode_GET_SIZE(obj)); |
| 502 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 503 | return PyUnicode_FromEncodedObject(obj, NULL, "strict"); |
| 504 | } |
| 505 | |
| 506 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
| 507 | const char *encoding, |
| 508 | const char *errors) |
| 509 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 510 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 511 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 512 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 513 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 514 | if (obj == NULL) { |
| 515 | PyErr_BadInternalCall(); |
| 516 | return NULL; |
| 517 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 518 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 519 | #if 0 |
| 520 | /* For b/w compatibility we also accept Unicode objects provided |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 521 | that no encodings is given and then redirect to |
| 522 | PyObject_Unicode() which then applies the additional logic for |
| 523 | Unicode subclasses. |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 524 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 525 | NOTE: This API should really only be used for object which |
| 526 | represent *encoded* Unicode ! |
| 527 | |
| 528 | */ |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 529 | if (PyUnicode_Check(obj)) { |
| 530 | if (encoding) { |
| 531 | PyErr_SetString(PyExc_TypeError, |
| 532 | "decoding Unicode is not supported"); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 533 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 534 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 535 | return PyObject_Unicode(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 536 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 537 | #else |
| 538 | if (PyUnicode_Check(obj)) { |
| 539 | PyErr_SetString(PyExc_TypeError, |
| 540 | "decoding Unicode is not supported"); |
| 541 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 542 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 543 | #endif |
| 544 | |
| 545 | /* Coerce object */ |
| 546 | if (PyString_Check(obj)) { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 547 | s = PyString_AS_STRING(obj); |
| 548 | len = PyString_GET_SIZE(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 549 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 550 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
| 551 | /* Overwrite the error message with something more useful in |
| 552 | case of a TypeError. */ |
| 553 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 554 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 555 | "coercing to Unicode: need string or buffer, " |
| 556 | "%.80s found", |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 557 | obj->ob_type->tp_name); |
| 558 | goto onError; |
| 559 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 560 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 561 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 562 | if (len == 0) { |
| 563 | Py_INCREF(unicode_empty); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 564 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 565 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 566 | else |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 567 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 568 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 569 | return v; |
| 570 | |
| 571 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 572 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | PyObject *PyUnicode_Decode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 576 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 577 | const char *encoding, |
| 578 | const char *errors) |
| 579 | { |
| 580 | PyObject *buffer = NULL, *unicode; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 581 | |
| 582 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 583 | encoding = PyUnicode_GetDefaultEncoding(); |
| 584 | |
| 585 | /* Shortcuts for common default encodings */ |
| 586 | if (strcmp(encoding, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 587 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 588 | else if (strcmp(encoding, "latin-1") == 0) |
| 589 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 590 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 591 | else if (strcmp(encoding, "mbcs") == 0) |
| 592 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 593 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 594 | else if (strcmp(encoding, "ascii") == 0) |
| 595 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 596 | |
| 597 | /* Decode via the codec registry */ |
| 598 | buffer = PyBuffer_FromMemory((void *)s, size); |
| 599 | if (buffer == NULL) |
| 600 | goto onError; |
| 601 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 602 | if (unicode == NULL) |
| 603 | goto onError; |
| 604 | if (!PyUnicode_Check(unicode)) { |
| 605 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 606 | "decoder did not return an unicode object (type=%.400s)", |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 607 | unicode->ob_type->tp_name); |
| 608 | Py_DECREF(unicode); |
| 609 | goto onError; |
| 610 | } |
| 611 | Py_DECREF(buffer); |
| 612 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 613 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 614 | onError: |
| 615 | Py_XDECREF(buffer); |
| 616 | return NULL; |
| 617 | } |
| 618 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 619 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 620 | const char *encoding, |
| 621 | const char *errors) |
| 622 | { |
| 623 | PyObject *v; |
| 624 | |
| 625 | if (!PyUnicode_Check(unicode)) { |
| 626 | PyErr_BadArgument(); |
| 627 | goto onError; |
| 628 | } |
| 629 | |
| 630 | if (encoding == NULL) |
| 631 | encoding = PyUnicode_GetDefaultEncoding(); |
| 632 | |
| 633 | /* Decode via the codec registry */ |
| 634 | v = PyCodec_Decode(unicode, encoding, errors); |
| 635 | if (v == NULL) |
| 636 | goto onError; |
| 637 | return v; |
| 638 | |
| 639 | onError: |
| 640 | return NULL; |
| 641 | } |
| 642 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 643 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 644 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 645 | const char *encoding, |
| 646 | const char *errors) |
| 647 | { |
| 648 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 649 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 650 | unicode = PyUnicode_FromUnicode(s, size); |
| 651 | if (unicode == NULL) |
| 652 | return NULL; |
| 653 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 654 | Py_DECREF(unicode); |
| 655 | return v; |
| 656 | } |
| 657 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 658 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 659 | const char *encoding, |
| 660 | const char *errors) |
| 661 | { |
| 662 | PyObject *v; |
| 663 | |
| 664 | if (!PyUnicode_Check(unicode)) { |
| 665 | PyErr_BadArgument(); |
| 666 | goto onError; |
| 667 | } |
| 668 | |
| 669 | if (encoding == NULL) |
| 670 | encoding = PyUnicode_GetDefaultEncoding(); |
| 671 | |
| 672 | /* Encode via the codec registry */ |
| 673 | v = PyCodec_Encode(unicode, encoding, errors); |
| 674 | if (v == NULL) |
| 675 | goto onError; |
| 676 | return v; |
| 677 | |
| 678 | onError: |
| 679 | return NULL; |
| 680 | } |
| 681 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 682 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 683 | const char *encoding, |
| 684 | const char *errors) |
| 685 | { |
| 686 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 687 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 688 | if (!PyUnicode_Check(unicode)) { |
| 689 | PyErr_BadArgument(); |
| 690 | goto onError; |
| 691 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 692 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 693 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 694 | encoding = PyUnicode_GetDefaultEncoding(); |
| 695 | |
| 696 | /* Shortcuts for common default encodings */ |
| 697 | if (errors == NULL) { |
| 698 | if (strcmp(encoding, "utf-8") == 0) |
Jeremy Hylton | 9cea41c | 2001-05-29 17:13:15 +0000 | [diff] [blame] | 699 | return PyUnicode_AsUTF8String(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 700 | else if (strcmp(encoding, "latin-1") == 0) |
| 701 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 702 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 703 | else if (strcmp(encoding, "mbcs") == 0) |
| 704 | return PyUnicode_AsMBCSString(unicode); |
| 705 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 706 | else if (strcmp(encoding, "ascii") == 0) |
| 707 | return PyUnicode_AsASCIIString(unicode); |
| 708 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 709 | |
| 710 | /* Encode via the codec registry */ |
| 711 | v = PyCodec_Encode(unicode, encoding, errors); |
| 712 | if (v == NULL) |
| 713 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 714 | if (!PyString_Check(v)) { |
| 715 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 716 | "encoder did not return a string object (type=%.400s)", |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 717 | v->ob_type->tp_name); |
| 718 | Py_DECREF(v); |
| 719 | goto onError; |
| 720 | } |
| 721 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 722 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 723 | onError: |
| 724 | return NULL; |
| 725 | } |
| 726 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 727 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 728 | const char *errors) |
| 729 | { |
| 730 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
| 731 | |
| 732 | if (v) |
| 733 | return v; |
| 734 | v = PyUnicode_AsEncodedString(unicode, NULL, errors); |
| 735 | if (v && errors == NULL) |
| 736 | ((PyUnicodeObject *)unicode)->defenc = v; |
| 737 | return v; |
| 738 | } |
| 739 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 740 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 741 | { |
| 742 | if (!PyUnicode_Check(unicode)) { |
| 743 | PyErr_BadArgument(); |
| 744 | goto onError; |
| 745 | } |
| 746 | return PyUnicode_AS_UNICODE(unicode); |
| 747 | |
| 748 | onError: |
| 749 | return NULL; |
| 750 | } |
| 751 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 752 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 753 | { |
| 754 | if (!PyUnicode_Check(unicode)) { |
| 755 | PyErr_BadArgument(); |
| 756 | goto onError; |
| 757 | } |
| 758 | return PyUnicode_GET_SIZE(unicode); |
| 759 | |
| 760 | onError: |
| 761 | return -1; |
| 762 | } |
| 763 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 764 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 765 | { |
| 766 | return unicode_default_encoding; |
| 767 | } |
| 768 | |
| 769 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 770 | { |
| 771 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 772 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 773 | /* Make sure the encoding is valid. As side effect, this also |
| 774 | loads the encoding into the codec registry cache. */ |
| 775 | v = _PyCodec_Lookup(encoding); |
| 776 | if (v == NULL) |
| 777 | goto onError; |
| 778 | Py_DECREF(v); |
| 779 | strncpy(unicode_default_encoding, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 780 | encoding, |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 781 | sizeof(unicode_default_encoding)); |
| 782 | return 0; |
| 783 | |
| 784 | onError: |
| 785 | return -1; |
| 786 | } |
| 787 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 788 | /* error handling callback helper: |
| 789 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 790 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 791 | and adjust various state variables. |
| 792 | return 0 on success, -1 on error |
| 793 | */ |
| 794 | |
| 795 | static |
| 796 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 797 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 798 | const char *input, Py_ssize_t insize, Py_ssize_t *startinpos, Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 799 | PyObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 800 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 801 | 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] | 802 | |
| 803 | PyObject *restuple = NULL; |
| 804 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 805 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
| 806 | Py_ssize_t requiredsize; |
| 807 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 808 | Py_UNICODE *repptr; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 809 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 810 | int res = -1; |
| 811 | |
| 812 | if (*errorHandler == NULL) { |
| 813 | *errorHandler = PyCodec_LookupError(errors); |
| 814 | if (*errorHandler == NULL) |
| 815 | goto onError; |
| 816 | } |
| 817 | |
| 818 | if (*exceptionObject == NULL) { |
| 819 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 820 | encoding, input, insize, *startinpos, *endinpos, reason); |
| 821 | if (*exceptionObject == NULL) |
| 822 | goto onError; |
| 823 | } |
| 824 | else { |
| 825 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 826 | goto onError; |
| 827 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 828 | goto onError; |
| 829 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 830 | goto onError; |
| 831 | } |
| 832 | |
| 833 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 834 | if (restuple == NULL) |
| 835 | goto onError; |
| 836 | if (!PyTuple_Check(restuple)) { |
| 837 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 838 | goto onError; |
| 839 | } |
| 840 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 841 | goto onError; |
| 842 | if (newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 843 | newpos = insize+newpos; |
| 844 | if (newpos<0 || newpos>insize) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 845 | 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] | 846 | goto onError; |
| 847 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 848 | |
| 849 | /* need more space? (at least enough for what we |
| 850 | have+the replacement+the rest of the string (starting |
| 851 | at the new input position), so we won't have to check space |
| 852 | when there are no errors in the rest of the string) */ |
| 853 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 854 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 855 | requiredsize = *outpos + repsize + insize-newpos; |
| 856 | if (requiredsize > outsize) { |
| 857 | if (requiredsize<2*outsize) |
| 858 | requiredsize = 2*outsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 859 | if (PyUnicode_Resize(output, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 860 | goto onError; |
| 861 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
| 862 | } |
| 863 | *endinpos = newpos; |
| 864 | *inptr = input + newpos; |
| 865 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 866 | *outptr += repsize; |
| 867 | *outpos += repsize; |
| 868 | /* we made it! */ |
| 869 | res = 0; |
| 870 | |
| 871 | onError: |
| 872 | Py_XDECREF(restuple); |
| 873 | return res; |
| 874 | } |
| 875 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 876 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 877 | |
| 878 | /* see RFC2152 for details */ |
| 879 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 880 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 881 | char utf7_special[128] = { |
| 882 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 883 | encoded: |
| 884 | 0 - not special |
| 885 | 1 - special |
| 886 | 2 - whitespace (optional) |
| 887 | 3 - RFC2152 Set O (optional) */ |
| 888 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 889 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 890 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 891 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 892 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 893 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 894 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 895 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 896 | |
| 897 | }; |
| 898 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 899 | /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
| 900 | warnings about the comparison always being false; since |
| 901 | utf7_special[0] is 1, we can safely make that one comparison |
| 902 | true */ |
| 903 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 904 | #define SPECIAL(c, encodeO, encodeWS) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 905 | ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 906 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 907 | (encodeO && (utf7_special[(c)] == 3))) |
| 908 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 909 | #define B64(n) \ |
| 910 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 911 | #define B64CHAR(c) \ |
| 912 | (isalnum(c) || (c) == '+' || (c) == '/') |
| 913 | #define UB64(c) \ |
| 914 | ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
| 915 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 916 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 917 | #define ENCODE(out, ch, bits) \ |
| 918 | while (bits >= 6) { \ |
| 919 | *out++ = B64(ch >> (bits-6)); \ |
| 920 | bits -= 6; \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 921 | } |
| 922 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 923 | #define DECODE(out, ch, bits, surrogate) \ |
| 924 | while (bits >= 16) { \ |
| 925 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 926 | bits -= 16; \ |
| 927 | if (surrogate) { \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 928 | /* We have already generated an error for the high surrogate \ |
| 929 | 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] | 930 | surrogate = 0; \ |
| 931 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 932 | /* This is a surrogate pair. Unfortunately we can't represent \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 933 | it in a 16-bit character */ \ |
| 934 | surrogate = 1; \ |
| 935 | errmsg = "code pairs are not supported"; \ |
| 936 | goto utf7Error; \ |
| 937 | } else { \ |
| 938 | *out++ = outCh; \ |
| 939 | } \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 940 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 941 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 942 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 943 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 944 | const char *errors) |
| 945 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 946 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 947 | Py_ssize_t startinpos; |
| 948 | Py_ssize_t endinpos; |
| 949 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 950 | const char *e; |
| 951 | PyUnicodeObject *unicode; |
| 952 | Py_UNICODE *p; |
| 953 | const char *errmsg = ""; |
| 954 | int inShift = 0; |
| 955 | unsigned int bitsleft = 0; |
| 956 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 957 | int surrogate = 0; |
| 958 | PyObject *errorHandler = NULL; |
| 959 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 960 | |
| 961 | unicode = _PyUnicode_New(size); |
| 962 | if (!unicode) |
| 963 | return NULL; |
| 964 | if (size == 0) |
| 965 | return (PyObject *)unicode; |
| 966 | |
| 967 | p = unicode->str; |
| 968 | e = s + size; |
| 969 | |
| 970 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 971 | Py_UNICODE ch; |
| 972 | restart: |
| 973 | ch = *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 974 | |
| 975 | if (inShift) { |
| 976 | if ((ch == '-') || !B64CHAR(ch)) { |
| 977 | inShift = 0; |
| 978 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 979 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 980 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 981 | if (bitsleft >= 6) { |
| 982 | /* The shift sequence has a partial character in it. If |
| 983 | bitsleft < 6 then we could just classify it as padding |
| 984 | but that is not the case here */ |
| 985 | |
| 986 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 987 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 988 | } |
| 989 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 990 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 991 | here so indicate the potential of a misencoded character. */ |
| 992 | |
| 993 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 994 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 995 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 996 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | if (ch == '-') { |
| 1000 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1001 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1002 | inShift = 1; |
| 1003 | } |
| 1004 | } else if (SPECIAL(ch,0,0)) { |
| 1005 | errmsg = "unexpected special character"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1006 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1007 | } else { |
| 1008 | *p++ = ch; |
| 1009 | } |
| 1010 | } else { |
| 1011 | charsleft = (charsleft << 6) | UB64(ch); |
| 1012 | bitsleft += 6; |
| 1013 | s++; |
| 1014 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1015 | } |
| 1016 | } |
| 1017 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1018 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1019 | s++; |
| 1020 | if (s < e && *s == '-') { |
| 1021 | s++; |
| 1022 | *p++ = '+'; |
| 1023 | } else |
| 1024 | { |
| 1025 | inShift = 1; |
| 1026 | bitsleft = 0; |
| 1027 | } |
| 1028 | } |
| 1029 | else if (SPECIAL(ch,0,0)) { |
| 1030 | errmsg = "unexpected special character"; |
| 1031 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1032 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1033 | } |
| 1034 | else { |
| 1035 | *p++ = ch; |
| 1036 | s++; |
| 1037 | } |
| 1038 | continue; |
| 1039 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1040 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1041 | endinpos = s-starts; |
| 1042 | if (unicode_decode_call_errorhandler( |
| 1043 | errors, &errorHandler, |
| 1044 | "utf7", errmsg, |
| 1045 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1046 | (PyObject **)&unicode, &outpos, &p)) |
| 1047 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | if (inShift) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1051 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1052 | endinpos = size; |
| 1053 | if (unicode_decode_call_errorhandler( |
| 1054 | errors, &errorHandler, |
| 1055 | "utf7", "unterminated shift sequence", |
| 1056 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1057 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1058 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1059 | if (s < e) |
| 1060 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1063 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1064 | goto onError; |
| 1065 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1066 | Py_XDECREF(errorHandler); |
| 1067 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1068 | return (PyObject *)unicode; |
| 1069 | |
| 1070 | onError: |
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 | Py_DECREF(unicode); |
| 1074 | return NULL; |
| 1075 | } |
| 1076 | |
| 1077 | |
| 1078 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1079 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1080 | int encodeSetO, |
| 1081 | int encodeWhiteSpace, |
| 1082 | const char *errors) |
| 1083 | { |
| 1084 | PyObject *v; |
| 1085 | /* It might be possible to tighten this worst case */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1086 | Py_ssize_t cbAllocated = 5 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1087 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1088 | Py_ssize_t i = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1089 | unsigned int bitsleft = 0; |
| 1090 | unsigned long charsleft = 0; |
| 1091 | char * out; |
| 1092 | char * start; |
| 1093 | |
| 1094 | if (size == 0) |
| 1095 | return PyString_FromStringAndSize(NULL, 0); |
| 1096 | |
| 1097 | v = PyString_FromStringAndSize(NULL, cbAllocated); |
| 1098 | if (v == NULL) |
| 1099 | return NULL; |
| 1100 | |
| 1101 | start = out = PyString_AS_STRING(v); |
| 1102 | for (;i < size; ++i) { |
| 1103 | Py_UNICODE ch = s[i]; |
| 1104 | |
| 1105 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1106 | if (ch == '+') { |
| 1107 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1108 | *out++ = '-'; |
| 1109 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1110 | charsleft = ch; |
| 1111 | bitsleft = 16; |
| 1112 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1113 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1114 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1115 | } else { |
| 1116 | *out++ = (char) ch; |
| 1117 | } |
| 1118 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1119 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1120 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1121 | charsleft = 0; |
| 1122 | bitsleft = 0; |
| 1123 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1124 | so no '-' is required, except if the character is itself a '-' */ |
| 1125 | if (B64CHAR(ch) || ch == '-') { |
| 1126 | *out++ = '-'; |
| 1127 | } |
| 1128 | inShift = 0; |
| 1129 | *out++ = (char) ch; |
| 1130 | } else { |
| 1131 | bitsleft += 16; |
| 1132 | charsleft = (charsleft << 16) | ch; |
| 1133 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1134 | |
| 1135 | /* If the next character is special then we dont' need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1136 | 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] | 1137 | or '-' then the shift sequence will be terminated implicitly and we |
| 1138 | don't have to insert a '-'. */ |
| 1139 | |
| 1140 | if (bitsleft == 0) { |
| 1141 | if (i + 1 < size) { |
| 1142 | Py_UNICODE ch2 = s[i+1]; |
| 1143 | |
| 1144 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1145 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1146 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1147 | *out++ = '-'; |
| 1148 | inShift = 0; |
| 1149 | } else { |
| 1150 | inShift = 0; |
| 1151 | } |
| 1152 | |
| 1153 | } |
| 1154 | else { |
| 1155 | *out++ = '-'; |
| 1156 | inShift = 0; |
| 1157 | } |
| 1158 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1159 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1160 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1161 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1162 | if (bitsleft) { |
| 1163 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1164 | *out++ = '-'; |
| 1165 | } |
| 1166 | |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 1167 | _PyString_Resize(&v, out - start); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1168 | return v; |
| 1169 | } |
| 1170 | |
| 1171 | #undef SPECIAL |
| 1172 | #undef B64 |
| 1173 | #undef B64CHAR |
| 1174 | #undef UB64 |
| 1175 | #undef ENCODE |
| 1176 | #undef DECODE |
| 1177 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1178 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1179 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1180 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1181 | char utf8_code_length[256] = { |
| 1182 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1183 | illegal prefix. see RFC 2279 for details */ |
| 1184 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1185 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1186 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1187 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1188 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 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 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1193 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1194 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1195 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1196 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1197 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1198 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1199 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1200 | }; |
| 1201 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1202 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1203 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1204 | const char *errors) |
| 1205 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1206 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 1207 | } |
| 1208 | |
| 1209 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1210 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1211 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1212 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1213 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1214 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1215 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1216 | Py_ssize_t startinpos; |
| 1217 | Py_ssize_t endinpos; |
| 1218 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1219 | const char *e; |
| 1220 | PyUnicodeObject *unicode; |
| 1221 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1222 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1223 | PyObject *errorHandler = NULL; |
| 1224 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1225 | |
| 1226 | /* Note: size will always be longer than the resulting Unicode |
| 1227 | character count */ |
| 1228 | unicode = _PyUnicode_New(size); |
| 1229 | if (!unicode) |
| 1230 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1231 | if (size == 0) { |
| 1232 | if (consumed) |
| 1233 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1234 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1235 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1236 | |
| 1237 | /* Unpack UTF-8 encoded data */ |
| 1238 | p = unicode->str; |
| 1239 | e = s + size; |
| 1240 | |
| 1241 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1242 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1243 | |
| 1244 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1245 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1246 | s++; |
| 1247 | continue; |
| 1248 | } |
| 1249 | |
| 1250 | n = utf8_code_length[ch]; |
| 1251 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1252 | if (s + n > e) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1253 | if (consumed) |
| 1254 | break; |
| 1255 | else { |
| 1256 | errmsg = "unexpected end of data"; |
| 1257 | startinpos = s-starts; |
| 1258 | endinpos = size; |
| 1259 | goto utf8Error; |
| 1260 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1261 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1262 | |
| 1263 | switch (n) { |
| 1264 | |
| 1265 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1266 | errmsg = "unexpected code byte"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1267 | startinpos = s-starts; |
| 1268 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1269 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1270 | |
| 1271 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1272 | errmsg = "internal error"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1273 | startinpos = s-starts; |
| 1274 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1275 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1276 | |
| 1277 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1278 | if ((s[1] & 0xc0) != 0x80) { |
| 1279 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1280 | startinpos = s-starts; |
| 1281 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1282 | goto utf8Error; |
| 1283 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1284 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1285 | if (ch < 0x80) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1286 | startinpos = s-starts; |
| 1287 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1288 | errmsg = "illegal encoding"; |
| 1289 | goto utf8Error; |
| 1290 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1291 | else |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1292 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1293 | break; |
| 1294 | |
| 1295 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1296 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1297 | (s[2] & 0xc0) != 0x80) { |
| 1298 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1299 | startinpos = s-starts; |
| 1300 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1301 | goto utf8Error; |
| 1302 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1303 | 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] | 1304 | if (ch < 0x0800) { |
| 1305 | /* Note: UTF-8 encodings of surrogates are considered |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1306 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1307 | |
| 1308 | XXX For wide builds (UCS-4) we should probably try |
| 1309 | to recombine the surrogates into a single code |
| 1310 | unit. |
| 1311 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1312 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1313 | startinpos = s-starts; |
| 1314 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1315 | goto utf8Error; |
| 1316 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1317 | else |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1318 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1319 | break; |
| 1320 | |
| 1321 | case 4: |
| 1322 | if ((s[1] & 0xc0) != 0x80 || |
| 1323 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1324 | (s[3] & 0xc0) != 0x80) { |
| 1325 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1326 | startinpos = s-starts; |
| 1327 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1328 | goto utf8Error; |
| 1329 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1330 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 1331 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 1332 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1333 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1334 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1335 | || (ch > 0x10ffff)) /* maximum value allowed for |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1336 | UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1337 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1338 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1339 | startinpos = s-starts; |
| 1340 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1341 | goto utf8Error; |
| 1342 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1343 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1344 | *p++ = (Py_UNICODE)ch; |
| 1345 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1346 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1347 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1348 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 1349 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1350 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1351 | /* high surrogate = top 10 bits added to D800 */ |
| 1352 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1353 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1354 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1355 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1356 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1357 | break; |
| 1358 | |
| 1359 | default: |
| 1360 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1361 | errmsg = "unsupported Unicode code range"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1362 | startinpos = s-starts; |
| 1363 | endinpos = startinpos+n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1364 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1365 | } |
| 1366 | s += n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1367 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1368 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1369 | utf8Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1370 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1371 | if (unicode_decode_call_errorhandler( |
| 1372 | errors, &errorHandler, |
| 1373 | "utf8", errmsg, |
| 1374 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1375 | (PyObject **)&unicode, &outpos, &p)) |
| 1376 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1377 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1378 | if (consumed) |
| 1379 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1380 | |
| 1381 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1382 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1383 | goto onError; |
| 1384 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1385 | Py_XDECREF(errorHandler); |
| 1386 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1387 | return (PyObject *)unicode; |
| 1388 | |
| 1389 | onError: |
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 | Py_DECREF(unicode); |
| 1393 | return NULL; |
| 1394 | } |
| 1395 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1396 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 1397 | and allocate exactly as much space needed at the end. Else allocate the |
| 1398 | maximum possible needed (4 result bytes per Unicode character), and return |
| 1399 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1400 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1401 | PyObject * |
| 1402 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1403 | Py_ssize_t size, |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1404 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1405 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1406 | #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] | 1407 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1408 | Py_ssize_t i; /* index into s of next input byte */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1409 | PyObject *v; /* result string object */ |
| 1410 | char *p; /* next free byte in output buffer */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1411 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1412 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1413 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1414 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1415 | assert(s != NULL); |
| 1416 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1417 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1418 | if (size <= MAX_SHORT_UNICHARS) { |
| 1419 | /* Write into the stack buffer; nallocated can't overflow. |
| 1420 | * At the end, we'll allocate exactly as much heap space as it |
| 1421 | * turns out we need. |
| 1422 | */ |
| 1423 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
| 1424 | v = NULL; /* will allocate after we're done */ |
| 1425 | p = stackbuf; |
| 1426 | } |
| 1427 | else { |
| 1428 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 1429 | nallocated = size * 4; |
| 1430 | if (nallocated / 4 != size) /* overflow! */ |
| 1431 | return PyErr_NoMemory(); |
| 1432 | v = PyString_FromStringAndSize(NULL, nallocated); |
| 1433 | if (v == NULL) |
| 1434 | return NULL; |
| 1435 | p = PyString_AS_STRING(v); |
| 1436 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1437 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1438 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1439 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1440 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1441 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1442 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1443 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1444 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1445 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1446 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 1447 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 1448 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1449 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1450 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1451 | /* Encode UCS2 Unicode ordinals */ |
| 1452 | if (ch < 0x10000) { |
| 1453 | /* Special case: check for high surrogate */ |
| 1454 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 1455 | Py_UCS4 ch2 = s[i]; |
| 1456 | /* Check for low surrogate and combine the two to |
| 1457 | form a UCS4 value */ |
| 1458 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1459 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1460 | i++; |
| 1461 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1462 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1463 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1464 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1465 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1466 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1467 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1468 | continue; |
| 1469 | } |
| 1470 | encodeUCS4: |
| 1471 | /* Encode UCS4 Unicode ordinals */ |
| 1472 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 1473 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 1474 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1475 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1476 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1477 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 1478 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1479 | if (v == NULL) { |
| 1480 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1481 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1482 | assert(nneeded <= nallocated); |
| 1483 | v = PyString_FromStringAndSize(stackbuf, nneeded); |
| 1484 | } |
| 1485 | else { |
| 1486 | /* Cut back to size actually needed. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1487 | nneeded = p - PyString_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1488 | assert(nneeded <= nallocated); |
| 1489 | _PyString_Resize(&v, nneeded); |
| 1490 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1491 | return v; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1492 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1493 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1496 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 1497 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1498 | if (!PyUnicode_Check(unicode)) { |
| 1499 | PyErr_BadArgument(); |
| 1500 | return NULL; |
| 1501 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 1502 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1503 | PyUnicode_GET_SIZE(unicode), |
| 1504 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 1508 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1509 | PyObject * |
| 1510 | PyUnicode_DecodeUTF16(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1511 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1512 | const char *errors, |
| 1513 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1514 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1515 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 1516 | } |
| 1517 | |
| 1518 | PyObject * |
| 1519 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1520 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1521 | const char *errors, |
| 1522 | int *byteorder, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1523 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1524 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1525 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1526 | Py_ssize_t startinpos; |
| 1527 | Py_ssize_t endinpos; |
| 1528 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1529 | PyUnicodeObject *unicode; |
| 1530 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1531 | const unsigned char *q, *e; |
| 1532 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1533 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1534 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 1535 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1536 | int ihi = 1, ilo = 0; |
| 1537 | #else |
| 1538 | int ihi = 0, ilo = 1; |
| 1539 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1540 | PyObject *errorHandler = NULL; |
| 1541 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1542 | |
| 1543 | /* Note: size will always be longer than the resulting Unicode |
| 1544 | character count */ |
| 1545 | unicode = _PyUnicode_New(size); |
| 1546 | if (!unicode) |
| 1547 | return NULL; |
| 1548 | if (size == 0) |
| 1549 | return (PyObject *)unicode; |
| 1550 | |
| 1551 | /* Unpack UTF-16 encoded data */ |
| 1552 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1553 | q = (unsigned char *)s; |
| 1554 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1555 | |
| 1556 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1557 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1558 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1559 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 1560 | byte order setting accordingly. In native mode, the leading BOM |
| 1561 | mark is skipped, in all other modes, it is copied to the output |
| 1562 | stream as-is (giving a ZWNBSP character). */ |
| 1563 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1564 | if (size >= 2) { |
| 1565 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1566 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1567 | if (bom == 0xFEFF) { |
| 1568 | q += 2; |
| 1569 | bo = -1; |
| 1570 | } |
| 1571 | else if (bom == 0xFFFE) { |
| 1572 | q += 2; |
| 1573 | bo = 1; |
| 1574 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1575 | #else |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1576 | if (bom == 0xFEFF) { |
| 1577 | q += 2; |
| 1578 | bo = 1; |
| 1579 | } |
| 1580 | else if (bom == 0xFFFE) { |
| 1581 | q += 2; |
| 1582 | bo = -1; |
| 1583 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1584 | #endif |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1585 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1586 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1587 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1588 | if (bo == -1) { |
| 1589 | /* force LE */ |
| 1590 | ihi = 1; |
| 1591 | ilo = 0; |
| 1592 | } |
| 1593 | else if (bo == 1) { |
| 1594 | /* force BE */ |
| 1595 | ihi = 0; |
| 1596 | ilo = 1; |
| 1597 | } |
| 1598 | |
| 1599 | while (q < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1600 | Py_UNICODE ch; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1601 | /* remaining bytes at the end? (size should be even) */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1602 | if (e-q<2) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1603 | if (consumed) |
| 1604 | break; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1605 | errmsg = "truncated data"; |
| 1606 | startinpos = ((const char *)q)-starts; |
| 1607 | endinpos = ((const char *)e)-starts; |
| 1608 | goto utf16Error; |
| 1609 | /* The remaining input chars are ignored if the callback |
| 1610 | chooses to skip the input */ |
| 1611 | } |
| 1612 | ch = (q[ihi] << 8) | q[ilo]; |
| 1613 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1614 | q += 2; |
| 1615 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1616 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 1617 | *p++ = ch; |
| 1618 | continue; |
| 1619 | } |
| 1620 | |
| 1621 | /* UTF-16 code pair: */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1622 | if (q >= e) { |
| 1623 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1624 | startinpos = (((const char *)q)-2)-starts; |
| 1625 | endinpos = ((const char *)e)-starts; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1626 | goto utf16Error; |
| 1627 | } |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 1628 | if (0xD800 <= ch && ch <= 0xDBFF) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1629 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 1630 | q += 2; |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 1631 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1632 | #ifndef Py_UNICODE_WIDE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1633 | *p++ = ch; |
| 1634 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1635 | #else |
| 1636 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1637 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1638 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1639 | } |
| 1640 | else { |
| 1641 | errmsg = "illegal UTF-16 surrogate"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1642 | startinpos = (((const char *)q)-4)-starts; |
| 1643 | endinpos = startinpos+2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1644 | goto utf16Error; |
| 1645 | } |
| 1646 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1647 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1648 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1649 | startinpos = (((const char *)q)-2)-starts; |
| 1650 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1651 | /* Fall through to report the error */ |
| 1652 | |
| 1653 | utf16Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1654 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1655 | if (unicode_decode_call_errorhandler( |
| 1656 | errors, &errorHandler, |
| 1657 | "utf16", errmsg, |
| 1658 | starts, size, &startinpos, &endinpos, &exc, (const char **)&q, |
| 1659 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1660 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | if (byteorder) |
| 1664 | *byteorder = bo; |
| 1665 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1666 | if (consumed) |
| 1667 | *consumed = (const char *)q-starts; |
| 1668 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1669 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1670 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1671 | goto onError; |
| 1672 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1673 | Py_XDECREF(errorHandler); |
| 1674 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1675 | return (PyObject *)unicode; |
| 1676 | |
| 1677 | onError: |
| 1678 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1679 | Py_XDECREF(errorHandler); |
| 1680 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1681 | return NULL; |
| 1682 | } |
| 1683 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1684 | PyObject * |
| 1685 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1686 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1687 | const char *errors, |
| 1688 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1689 | { |
| 1690 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1691 | unsigned char *p; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1692 | #ifdef Py_UNICODE_WIDE |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1693 | int i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1694 | #else |
| 1695 | const int pairs = 0; |
| 1696 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1697 | /* Offsets from p for storing byte pairs in the right order. */ |
| 1698 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1699 | int ihi = 1, ilo = 0; |
| 1700 | #else |
| 1701 | int ihi = 0, ilo = 1; |
| 1702 | #endif |
| 1703 | |
| 1704 | #define STORECHAR(CH) \ |
| 1705 | do { \ |
| 1706 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 1707 | p[ilo] = (CH) & 0xff; \ |
| 1708 | p += 2; \ |
| 1709 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1710 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1711 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1712 | for (i = pairs = 0; i < size; i++) |
| 1713 | if (s[i] >= 0x10000) |
| 1714 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1715 | #endif |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1716 | v = PyString_FromStringAndSize(NULL, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1717 | 2 * (size + pairs + (byteorder == 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1718 | if (v == NULL) |
| 1719 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1720 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1721 | p = (unsigned char *)PyString_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1722 | if (byteorder == 0) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1723 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 1724 | if (size == 0) |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 1725 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1726 | |
| 1727 | if (byteorder == -1) { |
| 1728 | /* force LE */ |
| 1729 | ihi = 1; |
| 1730 | ilo = 0; |
| 1731 | } |
| 1732 | else if (byteorder == 1) { |
| 1733 | /* force BE */ |
| 1734 | ihi = 0; |
| 1735 | ilo = 1; |
| 1736 | } |
| 1737 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1738 | while (size-- > 0) { |
| 1739 | Py_UNICODE ch = *s++; |
| 1740 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1741 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1742 | if (ch >= 0x10000) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1743 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 1744 | ch = 0xD800 | ((ch-0x10000) >> 10); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1745 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1746 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1747 | STORECHAR(ch); |
| 1748 | if (ch2) |
| 1749 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1750 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1751 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1752 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 1756 | { |
| 1757 | if (!PyUnicode_Check(unicode)) { |
| 1758 | PyErr_BadArgument(); |
| 1759 | return NULL; |
| 1760 | } |
| 1761 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 1762 | PyUnicode_GET_SIZE(unicode), |
| 1763 | NULL, |
| 1764 | 0); |
| 1765 | } |
| 1766 | |
| 1767 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 1768 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1769 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 1770 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1771 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1772 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1773 | const char *errors) |
| 1774 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1775 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1776 | Py_ssize_t startinpos; |
| 1777 | Py_ssize_t endinpos; |
| 1778 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1779 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1780 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1781 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1782 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1783 | char* message; |
| 1784 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1785 | PyObject *errorHandler = NULL; |
| 1786 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1787 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1788 | /* Escaped strings will always be longer than the resulting |
| 1789 | 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] | 1790 | length after conversion to the true value. |
| 1791 | (but if the error callback returns a long replacement string |
| 1792 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1793 | v = _PyUnicode_New(size); |
| 1794 | if (v == NULL) |
| 1795 | goto onError; |
| 1796 | if (size == 0) |
| 1797 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1798 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1799 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1800 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1801 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1802 | while (s < end) { |
| 1803 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 1804 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1805 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1806 | |
| 1807 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 1808 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1809 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1810 | continue; |
| 1811 | } |
| 1812 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1813 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1814 | /* \ - Escapes */ |
| 1815 | s++; |
| 1816 | switch (*s++) { |
| 1817 | |
| 1818 | /* \x escapes */ |
| 1819 | case '\n': break; |
| 1820 | case '\\': *p++ = '\\'; break; |
| 1821 | case '\'': *p++ = '\''; break; |
| 1822 | case '\"': *p++ = '\"'; break; |
| 1823 | case 'b': *p++ = '\b'; break; |
| 1824 | case 'f': *p++ = '\014'; break; /* FF */ |
| 1825 | case 't': *p++ = '\t'; break; |
| 1826 | case 'n': *p++ = '\n'; break; |
| 1827 | case 'r': *p++ = '\r'; break; |
| 1828 | case 'v': *p++ = '\013'; break; /* VT */ |
| 1829 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 1830 | |
| 1831 | /* \OOO (octal) escapes */ |
| 1832 | case '0': case '1': case '2': case '3': |
| 1833 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1834 | x = s[-1] - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1835 | if ('0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1836 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1837 | if ('0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1838 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1839 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1840 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1841 | break; |
| 1842 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1843 | /* hex escapes */ |
| 1844 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1845 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1846 | digits = 2; |
| 1847 | message = "truncated \\xXX escape"; |
| 1848 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1849 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1850 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1851 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1852 | digits = 4; |
| 1853 | message = "truncated \\uXXXX escape"; |
| 1854 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1855 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1856 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1857 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1858 | digits = 8; |
| 1859 | message = "truncated \\UXXXXXXXX escape"; |
| 1860 | hexescape: |
| 1861 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1862 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1863 | if (s+digits>end) { |
| 1864 | endinpos = size; |
| 1865 | if (unicode_decode_call_errorhandler( |
| 1866 | errors, &errorHandler, |
| 1867 | "unicodeescape", "end of string in escape sequence", |
| 1868 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1869 | (PyObject **)&v, &outpos, &p)) |
| 1870 | goto onError; |
| 1871 | goto nextByte; |
| 1872 | } |
| 1873 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1874 | c = (unsigned char) s[i]; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1875 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1876 | endinpos = (s+i+1)-starts; |
| 1877 | if (unicode_decode_call_errorhandler( |
| 1878 | errors, &errorHandler, |
| 1879 | "unicodeescape", message, |
| 1880 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1881 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1882 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1883 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1884 | } |
| 1885 | chr = (chr<<4) & ~0xF; |
| 1886 | if (c >= '0' && c <= '9') |
| 1887 | chr += c - '0'; |
| 1888 | else if (c >= 'a' && c <= 'f') |
| 1889 | chr += 10 + c - 'a'; |
| 1890 | else |
| 1891 | chr += 10 + c - 'A'; |
| 1892 | } |
| 1893 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 1894 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1895 | /* _decoding_error will have already written into the |
| 1896 | target buffer. */ |
| 1897 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1898 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1899 | /* when we get here, chr is a 32-bit unicode character */ |
| 1900 | if (chr <= 0xffff) |
| 1901 | /* UCS-2 character */ |
| 1902 | *p++ = (Py_UNICODE) chr; |
| 1903 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1904 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1905 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1906 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1907 | *p++ = chr; |
| 1908 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1909 | chr -= 0x10000L; |
| 1910 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1911 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1912 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1913 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1914 | endinpos = s-starts; |
| 1915 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1916 | if (unicode_decode_call_errorhandler( |
| 1917 | errors, &errorHandler, |
| 1918 | "unicodeescape", "illegal Unicode character", |
| 1919 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1920 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1921 | goto onError; |
| 1922 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1923 | break; |
| 1924 | |
| 1925 | /* \N{name} */ |
| 1926 | case 'N': |
| 1927 | message = "malformed \\N character escape"; |
| 1928 | if (ucnhash_CAPI == NULL) { |
| 1929 | /* load the unicode data module */ |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1930 | PyObject *m, *api; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1931 | m = PyImport_ImportModule("unicodedata"); |
| 1932 | if (m == NULL) |
| 1933 | goto ucnhashError; |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1934 | api = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1935 | Py_DECREF(m); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1936 | if (api == NULL) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1937 | goto ucnhashError; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1938 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1939 | Py_DECREF(api); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1940 | if (ucnhash_CAPI == NULL) |
| 1941 | goto ucnhashError; |
| 1942 | } |
| 1943 | if (*s == '{') { |
| 1944 | const char *start = s+1; |
| 1945 | /* look for the closing brace */ |
| 1946 | while (*s != '}' && s < end) |
| 1947 | s++; |
| 1948 | if (s > start && s < end && *s == '}') { |
| 1949 | /* found a name. look it up in the unicode database */ |
| 1950 | message = "unknown Unicode character name"; |
| 1951 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 1952 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1953 | goto store; |
| 1954 | } |
| 1955 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1956 | endinpos = s-starts; |
| 1957 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1958 | if (unicode_decode_call_errorhandler( |
| 1959 | errors, &errorHandler, |
| 1960 | "unicodeescape", message, |
| 1961 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1962 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1963 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1964 | break; |
| 1965 | |
| 1966 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1967 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1968 | message = "\\ at end of string"; |
| 1969 | s--; |
| 1970 | endinpos = s-starts; |
| 1971 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1972 | if (unicode_decode_call_errorhandler( |
| 1973 | errors, &errorHandler, |
| 1974 | "unicodeescape", message, |
| 1975 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1976 | (PyObject **)&v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1977 | goto onError; |
| 1978 | } |
| 1979 | else { |
| 1980 | *p++ = '\\'; |
| 1981 | *p++ = (unsigned char)s[-1]; |
| 1982 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1983 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1984 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1985 | nextByte: |
| 1986 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1987 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1988 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1989 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 1990 | Py_XDECREF(errorHandler); |
| 1991 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1992 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1993 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1994 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1995 | PyErr_SetString( |
| 1996 | PyExc_UnicodeError, |
| 1997 | "\\N escapes not supported (can't load unicodedata module)" |
| 1998 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1999 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2000 | Py_XDECREF(errorHandler); |
| 2001 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 2002 | return NULL; |
| 2003 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2004 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2005 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2006 | Py_XDECREF(errorHandler); |
| 2007 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2008 | return NULL; |
| 2009 | } |
| 2010 | |
| 2011 | /* Return a Unicode-Escape string version of the Unicode object. |
| 2012 | |
| 2013 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 2014 | appropriate. |
| 2015 | |
| 2016 | */ |
| 2017 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2018 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
| 2019 | Py_ssize_t size, |
| 2020 | Py_UNICODE ch) |
| 2021 | { |
| 2022 | /* like wcschr, but doesn't stop at NULL characters */ |
| 2023 | |
| 2024 | while (size-- > 0) { |
| 2025 | if (*s == ch) |
| 2026 | return s; |
| 2027 | s++; |
| 2028 | } |
| 2029 | |
| 2030 | return NULL; |
| 2031 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 2032 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2033 | static |
| 2034 | PyObject *unicodeescape_string(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2035 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2036 | int quotes) |
| 2037 | { |
| 2038 | PyObject *repr; |
| 2039 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2040 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2041 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2042 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2043 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 2044 | better to choose a different scheme. Perhaps scan the |
| 2045 | first N-chars of the string and allocate based on that size. |
| 2046 | */ |
| 2047 | /* Initial allocation is based on the longest-possible unichr |
| 2048 | escape. |
| 2049 | |
| 2050 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 2051 | unichr, so in this case it's the longest unichr escape. In |
| 2052 | narrow (UTF-16) builds this is five chars per source unichr |
| 2053 | since there are two unichrs in the surrogate pair, so in narrow |
| 2054 | (UTF-16) builds it's not the longest unichr escape. |
| 2055 | |
| 2056 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 2057 | so in the narrow (UTF-16) build case it's the longest unichr |
| 2058 | escape. |
| 2059 | */ |
| 2060 | |
| 2061 | repr = PyString_FromStringAndSize(NULL, |
| 2062 | 2 |
| 2063 | #ifdef Py_UNICODE_WIDE |
| 2064 | + 10*size |
| 2065 | #else |
| 2066 | + 6*size |
| 2067 | #endif |
| 2068 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2069 | if (repr == NULL) |
| 2070 | return NULL; |
| 2071 | |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2072 | p = PyString_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2073 | |
| 2074 | if (quotes) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2075 | *p++ = 'u'; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2076 | *p++ = (findchar(s, size, '\'') && |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2077 | !findchar(s, size, '"')) ? '"' : '\''; |
| 2078 | } |
| 2079 | while (size-- > 0) { |
| 2080 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2081 | |
Hye-Shik Chang | 835b243 | 2005-12-17 04:38:31 +0000 | [diff] [blame] | 2082 | /* Escape quotes and backslashes */ |
| 2083 | if ((quotes && |
| 2084 | ch == (Py_UNICODE) PyString_AS_STRING(repr)[1]) || ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2085 | *p++ = '\\'; |
| 2086 | *p++ = (char) ch; |
Guido van Rossum | ad9744a | 2001-09-21 15:38:17 +0000 | [diff] [blame] | 2087 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2088 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2089 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 2090 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2091 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 2092 | else if (ch >= 0x10000) { |
| 2093 | *p++ = '\\'; |
| 2094 | *p++ = 'U'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2095 | *p++ = hexdigit[(ch >> 28) & 0x0000000F]; |
| 2096 | *p++ = hexdigit[(ch >> 24) & 0x0000000F]; |
| 2097 | *p++ = hexdigit[(ch >> 20) & 0x0000000F]; |
| 2098 | *p++ = hexdigit[(ch >> 16) & 0x0000000F]; |
| 2099 | *p++ = hexdigit[(ch >> 12) & 0x0000000F]; |
| 2100 | *p++ = hexdigit[(ch >> 8) & 0x0000000F]; |
| 2101 | *p++ = hexdigit[(ch >> 4) & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2102 | *p++ = hexdigit[ch & 0x0000000F]; |
| 2103 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2104 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2105 | #else |
| 2106 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2107 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 2108 | Py_UNICODE ch2; |
| 2109 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2110 | |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2111 | ch2 = *s++; |
| 2112 | size--; |
| 2113 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 2114 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 2115 | *p++ = '\\'; |
| 2116 | *p++ = 'U'; |
| 2117 | *p++ = hexdigit[(ucs >> 28) & 0x0000000F]; |
| 2118 | *p++ = hexdigit[(ucs >> 24) & 0x0000000F]; |
| 2119 | *p++ = hexdigit[(ucs >> 20) & 0x0000000F]; |
| 2120 | *p++ = hexdigit[(ucs >> 16) & 0x0000000F]; |
| 2121 | *p++ = hexdigit[(ucs >> 12) & 0x0000000F]; |
| 2122 | *p++ = hexdigit[(ucs >> 8) & 0x0000000F]; |
| 2123 | *p++ = hexdigit[(ucs >> 4) & 0x0000000F]; |
| 2124 | *p++ = hexdigit[ucs & 0x0000000F]; |
| 2125 | continue; |
| 2126 | } |
| 2127 | /* Fall through: isolated surrogates are copied as-is */ |
| 2128 | s--; |
| 2129 | size++; |
| 2130 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2131 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2132 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2133 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2134 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2135 | *p++ = '\\'; |
| 2136 | *p++ = 'u'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2137 | *p++ = hexdigit[(ch >> 12) & 0x000F]; |
| 2138 | *p++ = hexdigit[(ch >> 8) & 0x000F]; |
| 2139 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2140 | *p++ = hexdigit[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2141 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2142 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2143 | /* Map special whitespace to '\t', \n', '\r' */ |
| 2144 | else if (ch == '\t') { |
| 2145 | *p++ = '\\'; |
| 2146 | *p++ = 't'; |
| 2147 | } |
| 2148 | else if (ch == '\n') { |
| 2149 | *p++ = '\\'; |
| 2150 | *p++ = 'n'; |
| 2151 | } |
| 2152 | else if (ch == '\r') { |
| 2153 | *p++ = '\\'; |
| 2154 | *p++ = 'r'; |
| 2155 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2156 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2157 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 2158 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2159 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2160 | *p++ = 'x'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2161 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2162 | *p++ = hexdigit[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2163 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2164 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2165 | /* Copy everything else as-is */ |
| 2166 | else |
| 2167 | *p++ = (char) ch; |
| 2168 | } |
| 2169 | if (quotes) |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2170 | *p++ = PyString_AS_STRING(repr)[1]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2171 | |
| 2172 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2173 | _PyString_Resize(&repr, p - PyString_AS_STRING(repr)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2174 | return repr; |
| 2175 | } |
| 2176 | |
| 2177 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2178 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2179 | { |
| 2180 | return unicodeescape_string(s, size, 0); |
| 2181 | } |
| 2182 | |
| 2183 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 2184 | { |
| 2185 | if (!PyUnicode_Check(unicode)) { |
| 2186 | PyErr_BadArgument(); |
| 2187 | return NULL; |
| 2188 | } |
| 2189 | return PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2190 | PyUnicode_GET_SIZE(unicode)); |
| 2191 | } |
| 2192 | |
| 2193 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 2194 | |
| 2195 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2196 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2197 | const char *errors) |
| 2198 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2199 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2200 | Py_ssize_t startinpos; |
| 2201 | Py_ssize_t endinpos; |
| 2202 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2203 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2204 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2205 | const char *end; |
| 2206 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2207 | PyObject *errorHandler = NULL; |
| 2208 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2209 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2210 | /* Escaped strings will always be longer than the resulting |
| 2211 | 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] | 2212 | length after conversion to the true value. (But decoding error |
| 2213 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2214 | v = _PyUnicode_New(size); |
| 2215 | if (v == NULL) |
| 2216 | goto onError; |
| 2217 | if (size == 0) |
| 2218 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2219 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2220 | end = s + size; |
| 2221 | while (s < end) { |
| 2222 | unsigned char c; |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 2223 | Py_UCS4 x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2224 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2225 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2226 | |
| 2227 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2228 | if (*s != '\\') { |
| 2229 | *p++ = (unsigned char)*s++; |
| 2230 | continue; |
| 2231 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2232 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2233 | |
| 2234 | /* \u-escapes are only interpreted iff the number of leading |
| 2235 | backslashes if odd */ |
| 2236 | bs = s; |
| 2237 | for (;s < end;) { |
| 2238 | if (*s != '\\') |
| 2239 | break; |
| 2240 | *p++ = (unsigned char)*s++; |
| 2241 | } |
| 2242 | if (((s - bs) & 1) == 0 || |
| 2243 | s >= end || |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2244 | (*s != 'u' && *s != 'U')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2245 | continue; |
| 2246 | } |
| 2247 | p--; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2248 | count = *s=='u' ? 4 : 8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2249 | s++; |
| 2250 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2251 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2252 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2253 | for (x = 0, i = 0; i < count; ++i, ++s) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2254 | c = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2255 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2256 | endinpos = s-starts; |
| 2257 | if (unicode_decode_call_errorhandler( |
| 2258 | errors, &errorHandler, |
| 2259 | "rawunicodeescape", "truncated \\uXXXX", |
| 2260 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2261 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2262 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2263 | goto nextByte; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2264 | } |
| 2265 | x = (x<<4) & ~0xF; |
| 2266 | if (c >= '0' && c <= '9') |
| 2267 | x += c - '0'; |
| 2268 | else if (c >= 'a' && c <= 'f') |
| 2269 | x += 10 + c - 'a'; |
| 2270 | else |
| 2271 | x += 10 + c - 'A'; |
| 2272 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2273 | #ifndef Py_UNICODE_WIDE |
| 2274 | if (x > 0x10000) { |
| 2275 | if (unicode_decode_call_errorhandler( |
| 2276 | errors, &errorHandler, |
| 2277 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
| 2278 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2279 | (PyObject **)&v, &outpos, &p)) |
| 2280 | goto onError; |
| 2281 | } |
| 2282 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2283 | *p++ = x; |
| 2284 | nextByte: |
| 2285 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2286 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2287 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2288 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2289 | Py_XDECREF(errorHandler); |
| 2290 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2291 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2292 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2293 | onError: |
| 2294 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2295 | Py_XDECREF(errorHandler); |
| 2296 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2297 | return NULL; |
| 2298 | } |
| 2299 | |
| 2300 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2301 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2302 | { |
| 2303 | PyObject *repr; |
| 2304 | char *p; |
| 2305 | char *q; |
| 2306 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2307 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2308 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2309 | #ifdef Py_UNICODE_WIDE |
| 2310 | repr = PyString_FromStringAndSize(NULL, 10 * size); |
| 2311 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2312 | repr = PyString_FromStringAndSize(NULL, 6 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2313 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2314 | if (repr == NULL) |
| 2315 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2316 | if (size == 0) |
| 2317 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2318 | |
| 2319 | p = q = PyString_AS_STRING(repr); |
| 2320 | while (size-- > 0) { |
| 2321 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2322 | #ifdef Py_UNICODE_WIDE |
| 2323 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2324 | if (ch >= 0x10000) { |
| 2325 | *p++ = '\\'; |
| 2326 | *p++ = 'U'; |
| 2327 | *p++ = hexdigit[(ch >> 28) & 0xf]; |
| 2328 | *p++ = hexdigit[(ch >> 24) & 0xf]; |
| 2329 | *p++ = hexdigit[(ch >> 20) & 0xf]; |
| 2330 | *p++ = hexdigit[(ch >> 16) & 0xf]; |
| 2331 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 2332 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 2333 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 2334 | *p++ = hexdigit[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2335 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2336 | else |
| 2337 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2338 | /* Map 16-bit characters to '\uxxxx' */ |
| 2339 | if (ch >= 256) { |
| 2340 | *p++ = '\\'; |
| 2341 | *p++ = 'u'; |
| 2342 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 2343 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 2344 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 2345 | *p++ = hexdigit[ch & 15]; |
| 2346 | } |
| 2347 | /* Copy everything else as-is */ |
| 2348 | else |
| 2349 | *p++ = (char) ch; |
| 2350 | } |
| 2351 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2352 | _PyString_Resize(&repr, p - q); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2353 | return repr; |
| 2354 | } |
| 2355 | |
| 2356 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 2357 | { |
| 2358 | if (!PyUnicode_Check(unicode)) { |
| 2359 | PyErr_BadArgument(); |
| 2360 | return NULL; |
| 2361 | } |
| 2362 | return PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2363 | PyUnicode_GET_SIZE(unicode)); |
| 2364 | } |
| 2365 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2366 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 2367 | |
| 2368 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2369 | Py_ssize_t size, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2370 | const char *errors) |
| 2371 | { |
| 2372 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2373 | Py_ssize_t startinpos; |
| 2374 | Py_ssize_t endinpos; |
| 2375 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2376 | PyUnicodeObject *v; |
| 2377 | Py_UNICODE *p; |
| 2378 | const char *end; |
| 2379 | const char *reason; |
| 2380 | PyObject *errorHandler = NULL; |
| 2381 | PyObject *exc = NULL; |
| 2382 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 2383 | #ifdef Py_UNICODE_WIDE |
| 2384 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 2385 | #endif |
| 2386 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2387 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2388 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 2389 | if (v == NULL) |
| 2390 | goto onError; |
| 2391 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
| 2392 | return (PyObject *)v; |
| 2393 | p = PyUnicode_AS_UNICODE(v); |
| 2394 | end = s + size; |
| 2395 | |
| 2396 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2397 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2398 | /* We have to sanity check the raw data, otherwise doom looms for |
| 2399 | some malformed UCS-4 data. */ |
| 2400 | if ( |
| 2401 | #ifdef Py_UNICODE_WIDE |
| 2402 | *p > unimax || *p < 0 || |
| 2403 | #endif |
| 2404 | end-s < Py_UNICODE_SIZE |
| 2405 | ) |
| 2406 | { |
| 2407 | startinpos = s - starts; |
| 2408 | if (end-s < Py_UNICODE_SIZE) { |
| 2409 | endinpos = end-starts; |
| 2410 | reason = "truncated input"; |
| 2411 | } |
| 2412 | else { |
| 2413 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 2414 | reason = "illegal code point (> 0x10FFFF)"; |
| 2415 | } |
| 2416 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 2417 | if (unicode_decode_call_errorhandler( |
| 2418 | errors, &errorHandler, |
| 2419 | "unicode_internal", reason, |
| 2420 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2421 | (PyObject **)&v, &outpos, &p)) { |
| 2422 | goto onError; |
| 2423 | } |
| 2424 | } |
| 2425 | else { |
| 2426 | p++; |
| 2427 | s += Py_UNICODE_SIZE; |
| 2428 | } |
| 2429 | } |
| 2430 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2431 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2432 | goto onError; |
| 2433 | Py_XDECREF(errorHandler); |
| 2434 | Py_XDECREF(exc); |
| 2435 | return (PyObject *)v; |
| 2436 | |
| 2437 | onError: |
| 2438 | Py_XDECREF(v); |
| 2439 | Py_XDECREF(errorHandler); |
| 2440 | Py_XDECREF(exc); |
| 2441 | return NULL; |
| 2442 | } |
| 2443 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2444 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 2445 | |
| 2446 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2447 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2448 | const char *errors) |
| 2449 | { |
| 2450 | PyUnicodeObject *v; |
| 2451 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2452 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2453 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2454 | if (size == 1) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 2455 | Py_UNICODE r = *(unsigned char*)s; |
| 2456 | return PyUnicode_FromUnicode(&r, 1); |
| 2457 | } |
| 2458 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2459 | v = _PyUnicode_New(size); |
| 2460 | if (v == NULL) |
| 2461 | goto onError; |
| 2462 | if (size == 0) |
| 2463 | return (PyObject *)v; |
| 2464 | p = PyUnicode_AS_UNICODE(v); |
| 2465 | while (size-- > 0) |
| 2466 | *p++ = (unsigned char)*s++; |
| 2467 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2468 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2469 | onError: |
| 2470 | Py_XDECREF(v); |
| 2471 | return NULL; |
| 2472 | } |
| 2473 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2474 | /* create or adjust a UnicodeEncodeError */ |
| 2475 | static void make_encode_exception(PyObject **exceptionObject, |
| 2476 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2477 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 2478 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2479 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2480 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2481 | if (*exceptionObject == NULL) { |
| 2482 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 2483 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2484 | } |
| 2485 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2486 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 2487 | goto onError; |
| 2488 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 2489 | goto onError; |
| 2490 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 2491 | goto onError; |
| 2492 | return; |
| 2493 | onError: |
| 2494 | Py_DECREF(*exceptionObject); |
| 2495 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2496 | } |
| 2497 | } |
| 2498 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2499 | /* raises a UnicodeEncodeError */ |
| 2500 | static void raise_encode_exception(PyObject **exceptionObject, |
| 2501 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2502 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 2503 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2504 | const char *reason) |
| 2505 | { |
| 2506 | make_encode_exception(exceptionObject, |
| 2507 | encoding, unicode, size, startpos, endpos, reason); |
| 2508 | if (*exceptionObject != NULL) |
| 2509 | PyCodec_StrictErrors(*exceptionObject); |
| 2510 | } |
| 2511 | |
| 2512 | /* error handling callback helper: |
| 2513 | build arguments, call the callback and check the arguments, |
| 2514 | put the result into newpos and return the replacement string, which |
| 2515 | has to be freed by the caller */ |
| 2516 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 2517 | PyObject **errorHandler, |
| 2518 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2519 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 2520 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 2521 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2522 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2523 | 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] | 2524 | |
| 2525 | PyObject *restuple; |
| 2526 | PyObject *resunicode; |
| 2527 | |
| 2528 | if (*errorHandler == NULL) { |
| 2529 | *errorHandler = PyCodec_LookupError(errors); |
| 2530 | if (*errorHandler == NULL) |
| 2531 | return NULL; |
| 2532 | } |
| 2533 | |
| 2534 | make_encode_exception(exceptionObject, |
| 2535 | encoding, unicode, size, startpos, endpos, reason); |
| 2536 | if (*exceptionObject == NULL) |
| 2537 | return NULL; |
| 2538 | |
| 2539 | restuple = PyObject_CallFunctionObjArgs( |
| 2540 | *errorHandler, *exceptionObject, NULL); |
| 2541 | if (restuple == NULL) |
| 2542 | return NULL; |
| 2543 | if (!PyTuple_Check(restuple)) { |
| 2544 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 2545 | Py_DECREF(restuple); |
| 2546 | return NULL; |
| 2547 | } |
| 2548 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 2549 | &resunicode, newpos)) { |
| 2550 | Py_DECREF(restuple); |
| 2551 | return NULL; |
| 2552 | } |
| 2553 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2554 | *newpos = size+*newpos; |
| 2555 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 2556 | 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] | 2557 | Py_DECREF(restuple); |
| 2558 | return NULL; |
| 2559 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2560 | Py_INCREF(resunicode); |
| 2561 | Py_DECREF(restuple); |
| 2562 | return resunicode; |
| 2563 | } |
| 2564 | |
| 2565 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2566 | Py_ssize_t size, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2567 | const char *errors, |
| 2568 | int limit) |
| 2569 | { |
| 2570 | /* output object */ |
| 2571 | PyObject *res; |
| 2572 | /* pointers to the beginning and end+1 of input */ |
| 2573 | const Py_UNICODE *startp = p; |
| 2574 | const Py_UNICODE *endp = p + size; |
| 2575 | /* pointer to the beginning of the unencodable characters */ |
| 2576 | /* const Py_UNICODE *badp = NULL; */ |
| 2577 | /* pointer into the output */ |
| 2578 | char *str; |
| 2579 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2580 | Py_ssize_t respos = 0; |
| 2581 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2582 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 2583 | 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] | 2584 | PyObject *errorHandler = NULL; |
| 2585 | PyObject *exc = NULL; |
| 2586 | /* the following variable is used for caching string comparisons |
| 2587 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 2588 | int known_errorHandler = -1; |
| 2589 | |
| 2590 | /* allocate enough for a simple encoding without |
| 2591 | replacements, if we need more, we'll resize */ |
| 2592 | res = PyString_FromStringAndSize(NULL, size); |
| 2593 | if (res == NULL) |
| 2594 | goto onError; |
| 2595 | if (size == 0) |
| 2596 | return res; |
| 2597 | str = PyString_AS_STRING(res); |
| 2598 | ressize = size; |
| 2599 | |
| 2600 | while (p<endp) { |
| 2601 | Py_UNICODE c = *p; |
| 2602 | |
| 2603 | /* can we encode this? */ |
| 2604 | if (c<limit) { |
| 2605 | /* no overflow check, because we know that the space is enough */ |
| 2606 | *str++ = (char)c; |
| 2607 | ++p; |
| 2608 | } |
| 2609 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2610 | Py_ssize_t unicodepos = p-startp; |
| 2611 | Py_ssize_t requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2612 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2613 | Py_ssize_t repsize; |
| 2614 | Py_ssize_t newpos; |
| 2615 | Py_ssize_t respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2616 | Py_UNICODE *uni2; |
| 2617 | /* startpos for collecting unencodable chars */ |
| 2618 | const Py_UNICODE *collstart = p; |
| 2619 | const Py_UNICODE *collend = p; |
| 2620 | /* find all unecodable characters */ |
| 2621 | while ((collend < endp) && ((*collend)>=limit)) |
| 2622 | ++collend; |
| 2623 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 2624 | if (known_errorHandler==-1) { |
| 2625 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 2626 | known_errorHandler = 1; |
| 2627 | else if (!strcmp(errors, "replace")) |
| 2628 | known_errorHandler = 2; |
| 2629 | else if (!strcmp(errors, "ignore")) |
| 2630 | known_errorHandler = 3; |
| 2631 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 2632 | known_errorHandler = 4; |
| 2633 | else |
| 2634 | known_errorHandler = 0; |
| 2635 | } |
| 2636 | switch (known_errorHandler) { |
| 2637 | case 1: /* strict */ |
| 2638 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 2639 | goto onError; |
| 2640 | case 2: /* replace */ |
| 2641 | while (collstart++<collend) |
| 2642 | *str++ = '?'; /* fall through */ |
| 2643 | case 3: /* ignore */ |
| 2644 | p = collend; |
| 2645 | break; |
| 2646 | case 4: /* xmlcharrefreplace */ |
| 2647 | respos = str-PyString_AS_STRING(res); |
| 2648 | /* determine replacement size (temporarily (mis)uses p) */ |
| 2649 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 2650 | if (*p<10) |
| 2651 | repsize += 2+1+1; |
| 2652 | else if (*p<100) |
| 2653 | repsize += 2+2+1; |
| 2654 | else if (*p<1000) |
| 2655 | repsize += 2+3+1; |
| 2656 | else if (*p<10000) |
| 2657 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 2658 | #ifndef Py_UNICODE_WIDE |
| 2659 | else |
| 2660 | repsize += 2+5+1; |
| 2661 | #else |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2662 | else if (*p<100000) |
| 2663 | repsize += 2+5+1; |
| 2664 | else if (*p<1000000) |
| 2665 | repsize += 2+6+1; |
| 2666 | else |
| 2667 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2668 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2669 | } |
| 2670 | requiredsize = respos+repsize+(endp-collend); |
| 2671 | if (requiredsize > ressize) { |
| 2672 | if (requiredsize<2*ressize) |
| 2673 | requiredsize = 2*ressize; |
| 2674 | if (_PyString_Resize(&res, requiredsize)) |
| 2675 | goto onError; |
| 2676 | str = PyString_AS_STRING(res) + respos; |
| 2677 | ressize = requiredsize; |
| 2678 | } |
| 2679 | /* generate replacement (temporarily (mis)uses p) */ |
| 2680 | for (p = collstart; p < collend; ++p) { |
| 2681 | str += sprintf(str, "&#%d;", (int)*p); |
| 2682 | } |
| 2683 | p = collend; |
| 2684 | break; |
| 2685 | default: |
| 2686 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 2687 | encoding, reason, startp, size, &exc, |
| 2688 | collstart-startp, collend-startp, &newpos); |
| 2689 | if (repunicode == NULL) |
| 2690 | goto onError; |
| 2691 | /* need more space? (at least enough for what we |
| 2692 | have+the replacement+the rest of the string, so |
| 2693 | we won't have to check space for encodable characters) */ |
| 2694 | respos = str-PyString_AS_STRING(res); |
| 2695 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 2696 | requiredsize = respos+repsize+(endp-collend); |
| 2697 | if (requiredsize > ressize) { |
| 2698 | if (requiredsize<2*ressize) |
| 2699 | requiredsize = 2*ressize; |
| 2700 | if (_PyString_Resize(&res, requiredsize)) { |
| 2701 | Py_DECREF(repunicode); |
| 2702 | goto onError; |
| 2703 | } |
| 2704 | str = PyString_AS_STRING(res) + respos; |
| 2705 | ressize = requiredsize; |
| 2706 | } |
| 2707 | /* check if there is anything unencodable in the replacement |
| 2708 | and copy it to the output */ |
| 2709 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 2710 | c = *uni2; |
| 2711 | if (c >= limit) { |
| 2712 | raise_encode_exception(&exc, encoding, startp, size, |
| 2713 | unicodepos, unicodepos+1, reason); |
| 2714 | Py_DECREF(repunicode); |
| 2715 | goto onError; |
| 2716 | } |
| 2717 | *str = (char)c; |
| 2718 | } |
| 2719 | p = startp + newpos; |
| 2720 | Py_DECREF(repunicode); |
| 2721 | } |
| 2722 | } |
| 2723 | } |
| 2724 | /* Resize if we allocated to much */ |
| 2725 | respos = str-PyString_AS_STRING(res); |
| 2726 | if (respos<ressize) |
| 2727 | /* If this falls res will be NULL */ |
| 2728 | _PyString_Resize(&res, respos); |
| 2729 | Py_XDECREF(errorHandler); |
| 2730 | Py_XDECREF(exc); |
| 2731 | return res; |
| 2732 | |
| 2733 | onError: |
| 2734 | Py_XDECREF(res); |
| 2735 | Py_XDECREF(errorHandler); |
| 2736 | Py_XDECREF(exc); |
| 2737 | return NULL; |
| 2738 | } |
| 2739 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2740 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2741 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2742 | const char *errors) |
| 2743 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2744 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2745 | } |
| 2746 | |
| 2747 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 2748 | { |
| 2749 | if (!PyUnicode_Check(unicode)) { |
| 2750 | PyErr_BadArgument(); |
| 2751 | return NULL; |
| 2752 | } |
| 2753 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 2754 | PyUnicode_GET_SIZE(unicode), |
| 2755 | NULL); |
| 2756 | } |
| 2757 | |
| 2758 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 2759 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2760 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2761 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2762 | const char *errors) |
| 2763 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2764 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2765 | PyUnicodeObject *v; |
| 2766 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2767 | Py_ssize_t startinpos; |
| 2768 | Py_ssize_t endinpos; |
| 2769 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2770 | const char *e; |
| 2771 | PyObject *errorHandler = NULL; |
| 2772 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2773 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2774 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 2775 | if (size == 1 && *(unsigned char*)s < 128) { |
| 2776 | Py_UNICODE r = *(unsigned char*)s; |
| 2777 | return PyUnicode_FromUnicode(&r, 1); |
| 2778 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2779 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2780 | v = _PyUnicode_New(size); |
| 2781 | if (v == NULL) |
| 2782 | goto onError; |
| 2783 | if (size == 0) |
| 2784 | return (PyObject *)v; |
| 2785 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2786 | e = s + size; |
| 2787 | while (s < e) { |
| 2788 | register unsigned char c = (unsigned char)*s; |
| 2789 | if (c < 128) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2790 | *p++ = c; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2791 | ++s; |
| 2792 | } |
| 2793 | else { |
| 2794 | startinpos = s-starts; |
| 2795 | endinpos = startinpos + 1; |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 2796 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2797 | if (unicode_decode_call_errorhandler( |
| 2798 | errors, &errorHandler, |
| 2799 | "ascii", "ordinal not in range(128)", |
| 2800 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2801 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2802 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2803 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2804 | } |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2805 | if (p - PyUnicode_AS_UNICODE(v) < PyString_GET_SIZE(v)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2806 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2807 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2808 | Py_XDECREF(errorHandler); |
| 2809 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2810 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2811 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2812 | onError: |
| 2813 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2814 | Py_XDECREF(errorHandler); |
| 2815 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2816 | return NULL; |
| 2817 | } |
| 2818 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2819 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2820 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2821 | const char *errors) |
| 2822 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2823 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2824 | } |
| 2825 | |
| 2826 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 2827 | { |
| 2828 | if (!PyUnicode_Check(unicode)) { |
| 2829 | PyErr_BadArgument(); |
| 2830 | return NULL; |
| 2831 | } |
| 2832 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 2833 | PyUnicode_GET_SIZE(unicode), |
| 2834 | NULL); |
| 2835 | } |
| 2836 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2837 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2838 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2839 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2840 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2841 | #if SIZEOF_INT < SIZEOF_SSIZE_T |
| 2842 | #define NEED_RETRY |
| 2843 | #endif |
| 2844 | |
| 2845 | /* XXX This code is limited to "true" double-byte encodings, as |
| 2846 | a) it assumes an incomplete character consists of a single byte, and |
| 2847 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
| 2848 | encodings, see IsDBCSLeadByteEx documentation. */ |
| 2849 | |
| 2850 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 2851 | { |
| 2852 | const char *curr = s + offset; |
| 2853 | |
| 2854 | if (IsDBCSLeadByte(*curr)) { |
| 2855 | const char *prev = CharPrev(s, curr); |
| 2856 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
| 2857 | } |
| 2858 | return 0; |
| 2859 | } |
| 2860 | |
| 2861 | /* |
| 2862 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 2863 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 2864 | */ |
| 2865 | static int decode_mbcs(PyUnicodeObject **v, |
| 2866 | const char *s, /* MBCS string */ |
| 2867 | int size, /* sizeof MBCS string */ |
| 2868 | int final) |
| 2869 | { |
| 2870 | Py_UNICODE *p; |
| 2871 | Py_ssize_t n = 0; |
| 2872 | int usize = 0; |
| 2873 | |
| 2874 | assert(size >= 0); |
| 2875 | |
| 2876 | /* Skip trailing lead-byte unless 'final' is set */ |
| 2877 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
| 2878 | --size; |
| 2879 | |
| 2880 | /* First get the size of the result */ |
| 2881 | if (size > 0) { |
| 2882 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 2883 | if (usize == 0) { |
| 2884 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2885 | return -1; |
| 2886 | } |
| 2887 | } |
| 2888 | |
| 2889 | if (*v == NULL) { |
| 2890 | /* Create unicode object */ |
| 2891 | *v = _PyUnicode_New(usize); |
| 2892 | if (*v == NULL) |
| 2893 | return -1; |
| 2894 | } |
| 2895 | else { |
| 2896 | /* Extend unicode object */ |
| 2897 | n = PyUnicode_GET_SIZE(*v); |
| 2898 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 2899 | return -1; |
| 2900 | } |
| 2901 | |
| 2902 | /* Do the conversion */ |
| 2903 | if (size > 0) { |
| 2904 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 2905 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 2906 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2907 | return -1; |
| 2908 | } |
| 2909 | } |
| 2910 | |
| 2911 | return size; |
| 2912 | } |
| 2913 | |
| 2914 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
| 2915 | Py_ssize_t size, |
| 2916 | const char *errors, |
| 2917 | Py_ssize_t *consumed) |
| 2918 | { |
| 2919 | PyUnicodeObject *v = NULL; |
| 2920 | int done; |
| 2921 | |
| 2922 | if (consumed) |
| 2923 | *consumed = 0; |
| 2924 | |
| 2925 | #ifdef NEED_RETRY |
| 2926 | retry: |
| 2927 | if (size > INT_MAX) |
| 2928 | done = decode_mbcs(&v, s, INT_MAX, 0); |
| 2929 | else |
| 2930 | #endif |
| 2931 | done = decode_mbcs(&v, s, (int)size, !consumed); |
| 2932 | |
| 2933 | if (done < 0) { |
| 2934 | Py_XDECREF(v); |
| 2935 | return NULL; |
| 2936 | } |
| 2937 | |
| 2938 | if (consumed) |
| 2939 | *consumed += done; |
| 2940 | |
| 2941 | #ifdef NEED_RETRY |
| 2942 | if (size > INT_MAX) { |
| 2943 | s += done; |
| 2944 | size -= done; |
| 2945 | goto retry; |
| 2946 | } |
| 2947 | #endif |
| 2948 | |
| 2949 | return (PyObject *)v; |
| 2950 | } |
| 2951 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2952 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2953 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2954 | const char *errors) |
| 2955 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2956 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 2957 | } |
| 2958 | |
| 2959 | /* |
| 2960 | * Convert unicode into string object (MBCS). |
| 2961 | * Returns 0 if succeed, -1 otherwise. |
| 2962 | */ |
| 2963 | static int encode_mbcs(PyObject **repr, |
| 2964 | const Py_UNICODE *p, /* unicode */ |
| 2965 | int size) /* size of unicode */ |
| 2966 | { |
| 2967 | int mbcssize = 0; |
| 2968 | Py_ssize_t n = 0; |
| 2969 | |
| 2970 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2971 | |
| 2972 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2973 | if (size > 0) { |
| 2974 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 2975 | if (mbcssize == 0) { |
| 2976 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2977 | return -1; |
| 2978 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2979 | } |
| 2980 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2981 | if (*repr == NULL) { |
| 2982 | /* Create string object */ |
| 2983 | *repr = PyString_FromStringAndSize(NULL, mbcssize); |
| 2984 | if (*repr == NULL) |
| 2985 | return -1; |
| 2986 | } |
| 2987 | else { |
| 2988 | /* Extend string object */ |
| 2989 | n = PyString_Size(*repr); |
| 2990 | if (_PyString_Resize(repr, n + mbcssize) < 0) |
| 2991 | return -1; |
| 2992 | } |
| 2993 | |
| 2994 | /* Do the conversion */ |
| 2995 | if (size > 0) { |
| 2996 | char *s = PyString_AS_STRING(*repr) + n; |
| 2997 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 2998 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2999 | return -1; |
| 3000 | } |
| 3001 | } |
| 3002 | |
| 3003 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3004 | } |
| 3005 | |
| 3006 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3007 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3008 | const char *errors) |
| 3009 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3010 | PyObject *repr = NULL; |
| 3011 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 3012 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3013 | #ifdef NEED_RETRY |
| 3014 | retry: |
| 3015 | if (size > INT_MAX) |
| 3016 | ret = encode_mbcs(&repr, p, INT_MAX); |
| 3017 | else |
| 3018 | #endif |
| 3019 | ret = encode_mbcs(&repr, p, (int)size); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3020 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3021 | if (ret < 0) { |
| 3022 | Py_XDECREF(repr); |
| 3023 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3024 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3025 | |
| 3026 | #ifdef NEED_RETRY |
| 3027 | if (size > INT_MAX) { |
| 3028 | p += INT_MAX; |
| 3029 | size -= INT_MAX; |
| 3030 | goto retry; |
| 3031 | } |
| 3032 | #endif |
| 3033 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3034 | return repr; |
| 3035 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3036 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3037 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 3038 | { |
| 3039 | if (!PyUnicode_Check(unicode)) { |
| 3040 | PyErr_BadArgument(); |
| 3041 | return NULL; |
| 3042 | } |
| 3043 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 3044 | PyUnicode_GET_SIZE(unicode), |
| 3045 | NULL); |
| 3046 | } |
| 3047 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3048 | #undef NEED_RETRY |
| 3049 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3050 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3051 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3052 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 3053 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3054 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3055 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3056 | PyObject *mapping, |
| 3057 | const char *errors) |
| 3058 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3059 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3060 | Py_ssize_t startinpos; |
| 3061 | Py_ssize_t endinpos; |
| 3062 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3063 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3064 | PyUnicodeObject *v; |
| 3065 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3066 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3067 | PyObject *errorHandler = NULL; |
| 3068 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3069 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3070 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3071 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3072 | /* Default to Latin-1 */ |
| 3073 | if (mapping == NULL) |
| 3074 | return PyUnicode_DecodeLatin1(s, size, errors); |
| 3075 | |
| 3076 | v = _PyUnicode_New(size); |
| 3077 | if (v == NULL) |
| 3078 | goto onError; |
| 3079 | if (size == 0) |
| 3080 | return (PyObject *)v; |
| 3081 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3082 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3083 | if (PyUnicode_CheckExact(mapping)) { |
| 3084 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 3085 | maplen = PyUnicode_GET_SIZE(mapping); |
| 3086 | while (s < e) { |
| 3087 | unsigned char ch = *s; |
| 3088 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3089 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3090 | if (ch < maplen) |
| 3091 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3092 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3093 | if (x == 0xfffe) { |
| 3094 | /* undefined mapping */ |
| 3095 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3096 | startinpos = s-starts; |
| 3097 | endinpos = startinpos+1; |
| 3098 | if (unicode_decode_call_errorhandler( |
| 3099 | errors, &errorHandler, |
| 3100 | "charmap", "character maps to <undefined>", |
| 3101 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3102 | (PyObject **)&v, &outpos, &p)) { |
| 3103 | goto onError; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 3104 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3105 | continue; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 3106 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3107 | *p++ = x; |
| 3108 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3109 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3110 | } |
| 3111 | else { |
| 3112 | while (s < e) { |
| 3113 | unsigned char ch = *s; |
| 3114 | PyObject *w, *x; |
| 3115 | |
| 3116 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 3117 | w = PyInt_FromLong((long)ch); |
| 3118 | if (w == NULL) |
| 3119 | goto onError; |
| 3120 | x = PyObject_GetItem(mapping, w); |
| 3121 | Py_DECREF(w); |
| 3122 | if (x == NULL) { |
| 3123 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3124 | /* No mapping found means: mapping is undefined. */ |
| 3125 | PyErr_Clear(); |
| 3126 | x = Py_None; |
| 3127 | Py_INCREF(x); |
| 3128 | } else |
| 3129 | goto onError; |
| 3130 | } |
| 3131 | |
| 3132 | /* Apply mapping */ |
| 3133 | if (PyInt_Check(x)) { |
| 3134 | long value = PyInt_AS_LONG(x); |
| 3135 | if (value < 0 || value > 65535) { |
| 3136 | PyErr_SetString(PyExc_TypeError, |
| 3137 | "character mapping must be in range(65536)"); |
| 3138 | Py_DECREF(x); |
| 3139 | goto onError; |
| 3140 | } |
| 3141 | *p++ = (Py_UNICODE)value; |
| 3142 | } |
| 3143 | else if (x == Py_None) { |
| 3144 | /* undefined mapping */ |
| 3145 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3146 | startinpos = s-starts; |
| 3147 | endinpos = startinpos+1; |
| 3148 | if (unicode_decode_call_errorhandler( |
| 3149 | errors, &errorHandler, |
| 3150 | "charmap", "character maps to <undefined>", |
| 3151 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3152 | (PyObject **)&v, &outpos, &p)) { |
| 3153 | Py_DECREF(x); |
| 3154 | goto onError; |
| 3155 | } |
Walter Dörwald | d4fff17 | 2005-11-28 22:15:56 +0000 | [diff] [blame] | 3156 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3157 | continue; |
| 3158 | } |
| 3159 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3160 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3161 | |
| 3162 | if (targetsize == 1) |
| 3163 | /* 1-1 mapping */ |
| 3164 | *p++ = *PyUnicode_AS_UNICODE(x); |
| 3165 | |
| 3166 | else if (targetsize > 1) { |
| 3167 | /* 1-n mapping */ |
| 3168 | if (targetsize > extrachars) { |
| 3169 | /* resize first */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3170 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 3171 | Py_ssize_t needed = (targetsize - extrachars) + \ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3172 | (targetsize << 2); |
| 3173 | extrachars += needed; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3174 | /* XXX overflow detection missing */ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3175 | if (_PyUnicode_Resize(&v, |
| 3176 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 3177 | Py_DECREF(x); |
| 3178 | goto onError; |
| 3179 | } |
| 3180 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 3181 | } |
| 3182 | Py_UNICODE_COPY(p, |
| 3183 | PyUnicode_AS_UNICODE(x), |
| 3184 | targetsize); |
| 3185 | p += targetsize; |
| 3186 | extrachars -= targetsize; |
| 3187 | } |
| 3188 | /* 1-0 mapping: skip the character */ |
| 3189 | } |
| 3190 | else { |
| 3191 | /* wrong return value */ |
| 3192 | PyErr_SetString(PyExc_TypeError, |
| 3193 | "character mapping must return integer, None or unicode"); |
| 3194 | Py_DECREF(x); |
| 3195 | goto onError; |
| 3196 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3197 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3198 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3199 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3200 | } |
| 3201 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3202 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3203 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3204 | Py_XDECREF(errorHandler); |
| 3205 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3206 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3207 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3208 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3209 | Py_XDECREF(errorHandler); |
| 3210 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3211 | Py_XDECREF(v); |
| 3212 | return NULL; |
| 3213 | } |
| 3214 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3215 | /* Charmap encoding: the lookup table */ |
| 3216 | |
| 3217 | struct encoding_map{ |
| 3218 | PyObject_HEAD |
| 3219 | unsigned char level1[32]; |
| 3220 | int count2, count3; |
| 3221 | unsigned char level23[1]; |
| 3222 | }; |
| 3223 | |
| 3224 | static PyObject* |
| 3225 | encoding_map_size(PyObject *obj, PyObject* args) |
| 3226 | { |
| 3227 | struct encoding_map *map = (struct encoding_map*)obj; |
| 3228 | return PyInt_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
| 3229 | 128*map->count3); |
| 3230 | } |
| 3231 | |
| 3232 | static PyMethodDef encoding_map_methods[] = { |
| 3233 | {"size", encoding_map_size, METH_NOARGS, |
| 3234 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 3235 | { 0 } |
| 3236 | }; |
| 3237 | |
| 3238 | static void |
| 3239 | encoding_map_dealloc(PyObject* o) |
| 3240 | { |
| 3241 | PyObject_FREE(o); |
| 3242 | } |
| 3243 | |
| 3244 | static PyTypeObject EncodingMapType = { |
| 3245 | PyObject_HEAD_INIT(NULL) |
| 3246 | 0, /*ob_size*/ |
| 3247 | "EncodingMap", /*tp_name*/ |
| 3248 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 3249 | 0, /*tp_itemsize*/ |
| 3250 | /* methods */ |
| 3251 | encoding_map_dealloc, /*tp_dealloc*/ |
| 3252 | 0, /*tp_print*/ |
| 3253 | 0, /*tp_getattr*/ |
| 3254 | 0, /*tp_setattr*/ |
| 3255 | 0, /*tp_compare*/ |
| 3256 | 0, /*tp_repr*/ |
| 3257 | 0, /*tp_as_number*/ |
| 3258 | 0, /*tp_as_sequence*/ |
| 3259 | 0, /*tp_as_mapping*/ |
| 3260 | 0, /*tp_hash*/ |
| 3261 | 0, /*tp_call*/ |
| 3262 | 0, /*tp_str*/ |
| 3263 | 0, /*tp_getattro*/ |
| 3264 | 0, /*tp_setattro*/ |
| 3265 | 0, /*tp_as_buffer*/ |
| 3266 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 3267 | 0, /*tp_doc*/ |
| 3268 | 0, /*tp_traverse*/ |
| 3269 | 0, /*tp_clear*/ |
| 3270 | 0, /*tp_richcompare*/ |
| 3271 | 0, /*tp_weaklistoffset*/ |
| 3272 | 0, /*tp_iter*/ |
| 3273 | 0, /*tp_iternext*/ |
| 3274 | encoding_map_methods, /*tp_methods*/ |
| 3275 | 0, /*tp_members*/ |
| 3276 | 0, /*tp_getset*/ |
| 3277 | 0, /*tp_base*/ |
| 3278 | 0, /*tp_dict*/ |
| 3279 | 0, /*tp_descr_get*/ |
| 3280 | 0, /*tp_descr_set*/ |
| 3281 | 0, /*tp_dictoffset*/ |
| 3282 | 0, /*tp_init*/ |
| 3283 | 0, /*tp_alloc*/ |
| 3284 | 0, /*tp_new*/ |
| 3285 | 0, /*tp_free*/ |
| 3286 | 0, /*tp_is_gc*/ |
| 3287 | }; |
| 3288 | |
| 3289 | PyObject* |
| 3290 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 3291 | { |
| 3292 | Py_UNICODE *decode; |
| 3293 | PyObject *result; |
| 3294 | struct encoding_map *mresult; |
| 3295 | int i; |
| 3296 | int need_dict = 0; |
| 3297 | unsigned char level1[32]; |
| 3298 | unsigned char level2[512]; |
| 3299 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 3300 | int count2 = 0, count3 = 0; |
| 3301 | |
| 3302 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 3303 | PyErr_BadArgument(); |
| 3304 | return NULL; |
| 3305 | } |
| 3306 | decode = PyUnicode_AS_UNICODE(string); |
| 3307 | memset(level1, 0xFF, sizeof level1); |
| 3308 | memset(level2, 0xFF, sizeof level2); |
| 3309 | |
| 3310 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 3311 | or if there are non-BMP characters, we need to use |
| 3312 | a mapping dictionary. */ |
| 3313 | if (decode[0] != 0) |
| 3314 | need_dict = 1; |
| 3315 | for (i = 1; i < 256; i++) { |
| 3316 | int l1, l2; |
| 3317 | if (decode[i] == 0 |
| 3318 | #ifdef Py_UNICODE_WIDE |
| 3319 | || decode[i] > 0xFFFF |
| 3320 | #endif |
| 3321 | ) { |
| 3322 | need_dict = 1; |
| 3323 | break; |
| 3324 | } |
| 3325 | if (decode[i] == 0xFFFE) |
| 3326 | /* unmapped character */ |
| 3327 | continue; |
| 3328 | l1 = decode[i] >> 11; |
| 3329 | l2 = decode[i] >> 7; |
| 3330 | if (level1[l1] == 0xFF) |
| 3331 | level1[l1] = count2++; |
| 3332 | if (level2[l2] == 0xFF) |
| 3333 | level2[l2] = count3++; |
| 3334 | } |
| 3335 | |
| 3336 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 3337 | need_dict = 1; |
| 3338 | |
| 3339 | if (need_dict) { |
| 3340 | PyObject *result = PyDict_New(); |
| 3341 | PyObject *key, *value; |
| 3342 | if (!result) |
| 3343 | return NULL; |
| 3344 | for (i = 0; i < 256; i++) { |
| 3345 | key = value = NULL; |
| 3346 | key = PyInt_FromLong(decode[i]); |
| 3347 | value = PyInt_FromLong(i); |
| 3348 | if (!key || !value) |
| 3349 | goto failed1; |
| 3350 | if (PyDict_SetItem(result, key, value) == -1) |
| 3351 | goto failed1; |
| 3352 | Py_DECREF(key); |
| 3353 | Py_DECREF(value); |
| 3354 | } |
| 3355 | return result; |
| 3356 | failed1: |
| 3357 | Py_XDECREF(key); |
| 3358 | Py_XDECREF(value); |
| 3359 | Py_DECREF(result); |
| 3360 | return NULL; |
| 3361 | } |
| 3362 | |
| 3363 | /* Create a three-level trie */ |
| 3364 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 3365 | 16*count2 + 128*count3 - 1); |
| 3366 | if (!result) |
| 3367 | return PyErr_NoMemory(); |
| 3368 | PyObject_Init(result, &EncodingMapType); |
| 3369 | mresult = (struct encoding_map*)result; |
| 3370 | mresult->count2 = count2; |
| 3371 | mresult->count3 = count3; |
| 3372 | mlevel1 = mresult->level1; |
| 3373 | mlevel2 = mresult->level23; |
| 3374 | mlevel3 = mresult->level23 + 16*count2; |
| 3375 | memcpy(mlevel1, level1, 32); |
| 3376 | memset(mlevel2, 0xFF, 16*count2); |
| 3377 | memset(mlevel3, 0, 128*count3); |
| 3378 | count3 = 0; |
| 3379 | for (i = 1; i < 256; i++) { |
| 3380 | int o1, o2, o3, i2, i3; |
| 3381 | if (decode[i] == 0xFFFE) |
| 3382 | /* unmapped character */ |
| 3383 | continue; |
| 3384 | o1 = decode[i]>>11; |
| 3385 | o2 = (decode[i]>>7) & 0xF; |
| 3386 | i2 = 16*mlevel1[o1] + o2; |
| 3387 | if (mlevel2[i2] == 0xFF) |
| 3388 | mlevel2[i2] = count3++; |
| 3389 | o3 = decode[i] & 0x7F; |
| 3390 | i3 = 128*mlevel2[i2] + o3; |
| 3391 | mlevel3[i3] = i; |
| 3392 | } |
| 3393 | return result; |
| 3394 | } |
| 3395 | |
| 3396 | static int |
| 3397 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 3398 | { |
| 3399 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 3400 | int l1 = c>>11; |
| 3401 | int l2 = (c>>7) & 0xF; |
| 3402 | int l3 = c & 0x7F; |
| 3403 | int i; |
| 3404 | |
| 3405 | #ifdef Py_UNICODE_WIDE |
| 3406 | if (c > 0xFFFF) { |
| 3407 | return -1; |
| 3408 | } |
| 3409 | #endif |
| 3410 | if (c == 0) |
| 3411 | return 0; |
| 3412 | /* level 1*/ |
| 3413 | i = map->level1[l1]; |
| 3414 | if (i == 0xFF) { |
| 3415 | return -1; |
| 3416 | } |
| 3417 | /* level 2*/ |
| 3418 | i = map->level23[16*i+l2]; |
| 3419 | if (i == 0xFF) { |
| 3420 | return -1; |
| 3421 | } |
| 3422 | /* level 3 */ |
| 3423 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 3424 | if (i == 0) { |
| 3425 | return -1; |
| 3426 | } |
| 3427 | return i; |
| 3428 | } |
| 3429 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3430 | /* Lookup the character ch in the mapping. If the character |
| 3431 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3432 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3433 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3434 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3435 | PyObject *w = PyInt_FromLong((long)c); |
| 3436 | PyObject *x; |
| 3437 | |
| 3438 | if (w == NULL) |
| 3439 | return NULL; |
| 3440 | x = PyObject_GetItem(mapping, w); |
| 3441 | Py_DECREF(w); |
| 3442 | if (x == NULL) { |
| 3443 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3444 | /* No mapping found means: mapping is undefined. */ |
| 3445 | PyErr_Clear(); |
| 3446 | x = Py_None; |
| 3447 | Py_INCREF(x); |
| 3448 | return x; |
| 3449 | } else |
| 3450 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3451 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 3452 | else if (x == Py_None) |
| 3453 | return x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3454 | else if (PyInt_Check(x)) { |
| 3455 | long value = PyInt_AS_LONG(x); |
| 3456 | if (value < 0 || value > 255) { |
| 3457 | PyErr_SetString(PyExc_TypeError, |
| 3458 | "character mapping must be in range(256)"); |
| 3459 | Py_DECREF(x); |
| 3460 | return NULL; |
| 3461 | } |
| 3462 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3463 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3464 | else if (PyString_Check(x)) |
| 3465 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3466 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3467 | /* wrong return value */ |
| 3468 | PyErr_SetString(PyExc_TypeError, |
| 3469 | "character mapping must return integer, None or str"); |
| 3470 | Py_DECREF(x); |
| 3471 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3472 | } |
| 3473 | } |
| 3474 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3475 | static int |
| 3476 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
| 3477 | { |
| 3478 | Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
| 3479 | /* exponentially overallocate to minimize reallocations */ |
| 3480 | if (requiredsize < 2*outsize) |
| 3481 | requiredsize = 2*outsize; |
| 3482 | if (_PyString_Resize(outobj, requiredsize)) { |
| 3483 | return 0; |
| 3484 | } |
| 3485 | return 1; |
| 3486 | } |
| 3487 | |
| 3488 | typedef enum charmapencode_result { |
| 3489 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
| 3490 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3491 | /* lookup the character, put the result in the output string and adjust |
| 3492 | various state variables. Reallocate the output string if not enough |
| 3493 | space is available. Return a new reference to the object that |
| 3494 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 3495 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 3496 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3497 | static |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3498 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3499 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3500 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3501 | PyObject *rep; |
| 3502 | char *outstart; |
| 3503 | Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3504 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3505 | if (mapping->ob_type == &EncodingMapType) { |
| 3506 | int res = encoding_map_lookup(c, mapping); |
| 3507 | Py_ssize_t requiredsize = *outpos+1; |
| 3508 | if (res == -1) |
| 3509 | return enc_FAILED; |
| 3510 | if (outsize<requiredsize) |
| 3511 | if (!charmapencode_resize(outobj, outpos, requiredsize)) |
| 3512 | return enc_EXCEPTION; |
| 3513 | outstart = PyString_AS_STRING(*outobj); |
| 3514 | outstart[(*outpos)++] = (char)res; |
| 3515 | return enc_SUCCESS; |
| 3516 | } |
| 3517 | |
| 3518 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3519 | if (rep==NULL) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3520 | return enc_EXCEPTION; |
| 3521 | else if (rep==Py_None) { |
| 3522 | Py_DECREF(rep); |
| 3523 | return enc_FAILED; |
| 3524 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3525 | if (PyInt_Check(rep)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3526 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3527 | if (outsize<requiredsize) |
| 3528 | if (!charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3529 | Py_DECREF(rep); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3530 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3531 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3532 | outstart = PyString_AS_STRING(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3533 | outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); |
| 3534 | } |
| 3535 | else { |
| 3536 | const char *repchars = PyString_AS_STRING(rep); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3537 | Py_ssize_t repsize = PyString_GET_SIZE(rep); |
| 3538 | Py_ssize_t requiredsize = *outpos+repsize; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3539 | if (outsize<requiredsize) |
| 3540 | if (!charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3541 | Py_DECREF(rep); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3542 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3543 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3544 | outstart = PyString_AS_STRING(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3545 | memcpy(outstart + *outpos, repchars, repsize); |
| 3546 | *outpos += repsize; |
| 3547 | } |
| 3548 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3549 | Py_DECREF(rep); |
| 3550 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3551 | } |
| 3552 | |
| 3553 | /* handle an error in PyUnicode_EncodeCharmap |
| 3554 | Return 0 on success, -1 on error */ |
| 3555 | static |
| 3556 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3557 | 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] | 3558 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3559 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3560 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3561 | { |
| 3562 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3563 | Py_ssize_t repsize; |
| 3564 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3565 | Py_UNICODE *uni2; |
| 3566 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3567 | Py_ssize_t collstartpos = *inpos; |
| 3568 | Py_ssize_t collendpos = *inpos+1; |
| 3569 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3570 | char *encoding = "charmap"; |
| 3571 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3572 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3573 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3574 | /* find all unencodable characters */ |
| 3575 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3576 | PyObject *rep; |
| 3577 | if (mapping->ob_type == &EncodingMapType) { |
| 3578 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 3579 | if (res != -1) |
| 3580 | break; |
| 3581 | ++collendpos; |
| 3582 | continue; |
| 3583 | } |
| 3584 | |
| 3585 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 3586 | if (rep==NULL) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3587 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3588 | else if (rep!=Py_None) { |
| 3589 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3590 | break; |
| 3591 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3592 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3593 | ++collendpos; |
| 3594 | } |
| 3595 | /* cache callback name lookup |
| 3596 | * (if not done yet, i.e. it's the first error) */ |
| 3597 | if (*known_errorHandler==-1) { |
| 3598 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3599 | *known_errorHandler = 1; |
| 3600 | else if (!strcmp(errors, "replace")) |
| 3601 | *known_errorHandler = 2; |
| 3602 | else if (!strcmp(errors, "ignore")) |
| 3603 | *known_errorHandler = 3; |
| 3604 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3605 | *known_errorHandler = 4; |
| 3606 | else |
| 3607 | *known_errorHandler = 0; |
| 3608 | } |
| 3609 | switch (*known_errorHandler) { |
| 3610 | case 1: /* strict */ |
| 3611 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3612 | return -1; |
| 3613 | case 2: /* replace */ |
| 3614 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 3615 | x = charmapencode_output('?', mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3616 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3617 | return -1; |
| 3618 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3619 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3620 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3621 | return -1; |
| 3622 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3623 | } |
| 3624 | /* fall through */ |
| 3625 | case 3: /* ignore */ |
| 3626 | *inpos = collendpos; |
| 3627 | break; |
| 3628 | case 4: /* xmlcharrefreplace */ |
| 3629 | /* generate replacement (temporarily (mis)uses p) */ |
| 3630 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 3631 | char buffer[2+29+1+1]; |
| 3632 | char *cp; |
| 3633 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 3634 | for (cp = buffer; *cp; ++cp) { |
| 3635 | x = charmapencode_output(*cp, mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3636 | if (x==enc_EXCEPTION) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3637 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3638 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3639 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3640 | return -1; |
| 3641 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3642 | } |
| 3643 | } |
| 3644 | *inpos = collendpos; |
| 3645 | break; |
| 3646 | default: |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3647 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3648 | encoding, reason, p, size, exceptionObject, |
| 3649 | collstartpos, collendpos, &newpos); |
| 3650 | if (repunicode == NULL) |
| 3651 | return -1; |
| 3652 | /* generate replacement */ |
| 3653 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3654 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 3655 | x = charmapencode_output(*uni2, mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3656 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3657 | return -1; |
| 3658 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3659 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3660 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3661 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3662 | return -1; |
| 3663 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3664 | } |
| 3665 | *inpos = newpos; |
| 3666 | Py_DECREF(repunicode); |
| 3667 | } |
| 3668 | return 0; |
| 3669 | } |
| 3670 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3671 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3672 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3673 | PyObject *mapping, |
| 3674 | const char *errors) |
| 3675 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3676 | /* output object */ |
| 3677 | PyObject *res = NULL; |
| 3678 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3679 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3680 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3681 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3682 | PyObject *errorHandler = NULL; |
| 3683 | PyObject *exc = NULL; |
| 3684 | /* the following variable is used for caching string comparisons |
| 3685 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 3686 | * 3=ignore, 4=xmlcharrefreplace */ |
| 3687 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3688 | |
| 3689 | /* Default to Latin-1 */ |
| 3690 | if (mapping == NULL) |
| 3691 | return PyUnicode_EncodeLatin1(p, size, errors); |
| 3692 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3693 | /* allocate enough for a simple encoding without |
| 3694 | replacements, if we need more, we'll resize */ |
| 3695 | res = PyString_FromStringAndSize(NULL, size); |
| 3696 | if (res == NULL) |
| 3697 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3698 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3699 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3700 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3701 | while (inpos<size) { |
| 3702 | /* try to encode it */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3703 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 3704 | if (x==enc_EXCEPTION) /* error */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3705 | goto onError; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3706 | if (x==enc_FAILED) { /* unencodable character */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3707 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 3708 | &exc, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3709 | &known_errorHandler, &errorHandler, errors, |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 3710 | &res, &respos)) { |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3711 | goto onError; |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 3712 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3713 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3714 | else |
| 3715 | /* done with this character => adjust input position */ |
| 3716 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3717 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3718 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3719 | /* Resize if we allocated to much */ |
| 3720 | if (respos<PyString_GET_SIZE(res)) { |
| 3721 | if (_PyString_Resize(&res, respos)) |
| 3722 | goto onError; |
| 3723 | } |
| 3724 | Py_XDECREF(exc); |
| 3725 | Py_XDECREF(errorHandler); |
| 3726 | return res; |
| 3727 | |
| 3728 | onError: |
| 3729 | Py_XDECREF(res); |
| 3730 | Py_XDECREF(exc); |
| 3731 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3732 | return NULL; |
| 3733 | } |
| 3734 | |
| 3735 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 3736 | PyObject *mapping) |
| 3737 | { |
| 3738 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 3739 | PyErr_BadArgument(); |
| 3740 | return NULL; |
| 3741 | } |
| 3742 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 3743 | PyUnicode_GET_SIZE(unicode), |
| 3744 | mapping, |
| 3745 | NULL); |
| 3746 | } |
| 3747 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3748 | /* create or adjust a UnicodeTranslateError */ |
| 3749 | static void make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3750 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3751 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3752 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3753 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3754 | if (*exceptionObject == NULL) { |
| 3755 | *exceptionObject = PyUnicodeTranslateError_Create( |
| 3756 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3757 | } |
| 3758 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3759 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 3760 | goto onError; |
| 3761 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 3762 | goto onError; |
| 3763 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 3764 | goto onError; |
| 3765 | return; |
| 3766 | onError: |
| 3767 | Py_DECREF(*exceptionObject); |
| 3768 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3769 | } |
| 3770 | } |
| 3771 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3772 | /* raises a UnicodeTranslateError */ |
| 3773 | static void raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3774 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3775 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3776 | const char *reason) |
| 3777 | { |
| 3778 | make_translate_exception(exceptionObject, |
| 3779 | unicode, size, startpos, endpos, reason); |
| 3780 | if (*exceptionObject != NULL) |
| 3781 | PyCodec_StrictErrors(*exceptionObject); |
| 3782 | } |
| 3783 | |
| 3784 | /* error handling callback helper: |
| 3785 | build arguments, call the callback and check the arguments, |
| 3786 | put the result into newpos and return the replacement string, which |
| 3787 | has to be freed by the caller */ |
| 3788 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 3789 | PyObject **errorHandler, |
| 3790 | const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3791 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 3792 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3793 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3794 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3795 | 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] | 3796 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3797 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3798 | PyObject *restuple; |
| 3799 | PyObject *resunicode; |
| 3800 | |
| 3801 | if (*errorHandler == NULL) { |
| 3802 | *errorHandler = PyCodec_LookupError(errors); |
| 3803 | if (*errorHandler == NULL) |
| 3804 | return NULL; |
| 3805 | } |
| 3806 | |
| 3807 | make_translate_exception(exceptionObject, |
| 3808 | unicode, size, startpos, endpos, reason); |
| 3809 | if (*exceptionObject == NULL) |
| 3810 | return NULL; |
| 3811 | |
| 3812 | restuple = PyObject_CallFunctionObjArgs( |
| 3813 | *errorHandler, *exceptionObject, NULL); |
| 3814 | if (restuple == NULL) |
| 3815 | return NULL; |
| 3816 | if (!PyTuple_Check(restuple)) { |
| 3817 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 3818 | Py_DECREF(restuple); |
| 3819 | return NULL; |
| 3820 | } |
| 3821 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3822 | &resunicode, &i_newpos)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3823 | Py_DECREF(restuple); |
| 3824 | return NULL; |
| 3825 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3826 | if (i_newpos<0) |
| 3827 | *newpos = size+i_newpos; |
| 3828 | else |
| 3829 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3830 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 3831 | 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] | 3832 | Py_DECREF(restuple); |
| 3833 | return NULL; |
| 3834 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3835 | Py_INCREF(resunicode); |
| 3836 | Py_DECREF(restuple); |
| 3837 | return resunicode; |
| 3838 | } |
| 3839 | |
| 3840 | /* Lookup the character ch in the mapping and put the result in result, |
| 3841 | which must be decrefed by the caller. |
| 3842 | Return 0 on success, -1 on error */ |
| 3843 | static |
| 3844 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 3845 | { |
| 3846 | PyObject *w = PyInt_FromLong((long)c); |
| 3847 | PyObject *x; |
| 3848 | |
| 3849 | if (w == NULL) |
| 3850 | return -1; |
| 3851 | x = PyObject_GetItem(mapping, w); |
| 3852 | Py_DECREF(w); |
| 3853 | if (x == NULL) { |
| 3854 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3855 | /* No mapping found means: use 1:1 mapping. */ |
| 3856 | PyErr_Clear(); |
| 3857 | *result = NULL; |
| 3858 | return 0; |
| 3859 | } else |
| 3860 | return -1; |
| 3861 | } |
| 3862 | else if (x == Py_None) { |
| 3863 | *result = x; |
| 3864 | return 0; |
| 3865 | } |
| 3866 | else if (PyInt_Check(x)) { |
| 3867 | long value = PyInt_AS_LONG(x); |
| 3868 | long max = PyUnicode_GetMax(); |
| 3869 | if (value < 0 || value > max) { |
| 3870 | PyErr_Format(PyExc_TypeError, |
| 3871 | "character mapping must be in range(0x%lx)", max+1); |
| 3872 | Py_DECREF(x); |
| 3873 | return -1; |
| 3874 | } |
| 3875 | *result = x; |
| 3876 | return 0; |
| 3877 | } |
| 3878 | else if (PyUnicode_Check(x)) { |
| 3879 | *result = x; |
| 3880 | return 0; |
| 3881 | } |
| 3882 | else { |
| 3883 | /* wrong return value */ |
| 3884 | PyErr_SetString(PyExc_TypeError, |
| 3885 | "character mapping must return integer, None or unicode"); |
Walter Dörwald | 150523e | 2003-08-15 16:52:19 +0000 | [diff] [blame] | 3886 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3887 | return -1; |
| 3888 | } |
| 3889 | } |
| 3890 | /* ensure that *outobj is at least requiredsize characters long, |
| 3891 | if not reallocate and adjust various state variables. |
| 3892 | Return 0 on success, -1 on error */ |
| 3893 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3894 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3895 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3896 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3897 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3898 | if (requiredsize > oldsize) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3899 | /* remember old output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3900 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3901 | /* exponentially overallocate to minimize reallocations */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3902 | if (requiredsize < 2 * oldsize) |
| 3903 | requiredsize = 2 * oldsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3904 | if (_PyUnicode_Resize(outobj, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3905 | return -1; |
| 3906 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3907 | } |
| 3908 | return 0; |
| 3909 | } |
| 3910 | /* lookup the character, put the result in the output string and adjust |
| 3911 | various state variables. Return a new reference to the object that |
| 3912 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 3913 | undefined (in which case no character was written). |
| 3914 | The called must decref result. |
| 3915 | Return 0 on success, -1 on error. */ |
| 3916 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3917 | 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] | 3918 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3919 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3920 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3921 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3922 | return -1; |
| 3923 | if (*res==NULL) { |
| 3924 | /* not found => default to 1:1 mapping */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3925 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3926 | } |
| 3927 | else if (*res==Py_None) |
| 3928 | ; |
| 3929 | else if (PyInt_Check(*res)) { |
| 3930 | /* no overflow check, because we know that the space is enough */ |
| 3931 | *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); |
| 3932 | } |
| 3933 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3934 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3935 | if (repsize==1) { |
| 3936 | /* no overflow check, because we know that the space is enough */ |
| 3937 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 3938 | } |
| 3939 | else if (repsize!=0) { |
| 3940 | /* more than one character */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3941 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
Walter Dörwald | cd736e7 | 2004-02-05 17:36:00 +0000 | [diff] [blame] | 3942 | (insize - (curinp-startinp)) + |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3943 | repsize - 1; |
| 3944 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3945 | return -1; |
| 3946 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 3947 | *outp += repsize; |
| 3948 | } |
| 3949 | } |
| 3950 | else |
| 3951 | return -1; |
| 3952 | return 0; |
| 3953 | } |
| 3954 | |
| 3955 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3956 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3957 | PyObject *mapping, |
| 3958 | const char *errors) |
| 3959 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3960 | /* output object */ |
| 3961 | PyObject *res = NULL; |
| 3962 | /* pointers to the beginning and end+1 of input */ |
| 3963 | const Py_UNICODE *startp = p; |
| 3964 | const Py_UNICODE *endp = p + size; |
| 3965 | /* pointer into the output */ |
| 3966 | Py_UNICODE *str; |
| 3967 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3968 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3969 | char *reason = "character maps to <undefined>"; |
| 3970 | PyObject *errorHandler = NULL; |
| 3971 | PyObject *exc = NULL; |
| 3972 | /* the following variable is used for caching string comparisons |
| 3973 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 3974 | * 3=ignore, 4=xmlcharrefreplace */ |
| 3975 | int known_errorHandler = -1; |
| 3976 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3977 | if (mapping == NULL) { |
| 3978 | PyErr_BadArgument(); |
| 3979 | return NULL; |
| 3980 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3981 | |
| 3982 | /* allocate enough for a simple 1:1 translation without |
| 3983 | replacements, if we need more, we'll resize */ |
| 3984 | res = PyUnicode_FromUnicode(NULL, size); |
| 3985 | if (res == NULL) |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3986 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3987 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3988 | return res; |
| 3989 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3990 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3991 | while (p<endp) { |
| 3992 | /* try to encode it */ |
| 3993 | PyObject *x = NULL; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3994 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3995 | Py_XDECREF(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3996 | goto onError; |
| 3997 | } |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 3998 | Py_XDECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3999 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 4000 | ++p; |
| 4001 | else { /* untranslatable character */ |
| 4002 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4003 | Py_ssize_t repsize; |
| 4004 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4005 | Py_UNICODE *uni2; |
| 4006 | /* startpos for collecting untranslatable chars */ |
| 4007 | const Py_UNICODE *collstart = p; |
| 4008 | const Py_UNICODE *collend = p+1; |
| 4009 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4010 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4011 | /* find all untranslatable characters */ |
| 4012 | while (collend < endp) { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4013 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4014 | goto onError; |
| 4015 | Py_XDECREF(x); |
| 4016 | if (x!=Py_None) |
| 4017 | break; |
| 4018 | ++collend; |
| 4019 | } |
| 4020 | /* cache callback name lookup |
| 4021 | * (if not done yet, i.e. it's the first error) */ |
| 4022 | if (known_errorHandler==-1) { |
| 4023 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4024 | known_errorHandler = 1; |
| 4025 | else if (!strcmp(errors, "replace")) |
| 4026 | known_errorHandler = 2; |
| 4027 | else if (!strcmp(errors, "ignore")) |
| 4028 | known_errorHandler = 3; |
| 4029 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4030 | known_errorHandler = 4; |
| 4031 | else |
| 4032 | known_errorHandler = 0; |
| 4033 | } |
| 4034 | switch (known_errorHandler) { |
| 4035 | case 1: /* strict */ |
| 4036 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
| 4037 | goto onError; |
| 4038 | case 2: /* replace */ |
| 4039 | /* No need to check for space, this is a 1:1 replacement */ |
| 4040 | for (coll = collstart; coll<collend; ++coll) |
| 4041 | *str++ = '?'; |
| 4042 | /* fall through */ |
| 4043 | case 3: /* ignore */ |
| 4044 | p = collend; |
| 4045 | break; |
| 4046 | case 4: /* xmlcharrefreplace */ |
| 4047 | /* generate replacement (temporarily (mis)uses p) */ |
| 4048 | for (p = collstart; p < collend; ++p) { |
| 4049 | char buffer[2+29+1+1]; |
| 4050 | char *cp; |
| 4051 | sprintf(buffer, "&#%d;", (int)*p); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4052 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4053 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 4054 | goto onError; |
| 4055 | for (cp = buffer; *cp; ++cp) |
| 4056 | *str++ = *cp; |
| 4057 | } |
| 4058 | p = collend; |
| 4059 | break; |
| 4060 | default: |
| 4061 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 4062 | reason, startp, size, &exc, |
| 4063 | collstart-startp, collend-startp, &newpos); |
| 4064 | if (repunicode == NULL) |
| 4065 | goto onError; |
| 4066 | /* generate replacement */ |
| 4067 | repsize = PyUnicode_GET_SIZE(repunicode); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4068 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4069 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 4070 | Py_DECREF(repunicode); |
| 4071 | goto onError; |
| 4072 | } |
| 4073 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 4074 | *str++ = *uni2; |
| 4075 | p = startp + newpos; |
| 4076 | Py_DECREF(repunicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4077 | } |
| 4078 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4079 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4080 | /* Resize if we allocated to much */ |
| 4081 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4082 | if (respos<PyUnicode_GET_SIZE(res)) { |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4083 | if (_PyUnicode_Resize(&res, respos) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 4084 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4085 | } |
| 4086 | Py_XDECREF(exc); |
| 4087 | Py_XDECREF(errorHandler); |
| 4088 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4089 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4090 | onError: |
| 4091 | Py_XDECREF(res); |
| 4092 | Py_XDECREF(exc); |
| 4093 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4094 | return NULL; |
| 4095 | } |
| 4096 | |
| 4097 | PyObject *PyUnicode_Translate(PyObject *str, |
| 4098 | PyObject *mapping, |
| 4099 | const char *errors) |
| 4100 | { |
| 4101 | PyObject *result; |
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 | str = PyUnicode_FromObject(str); |
| 4104 | if (str == NULL) |
| 4105 | goto onError; |
| 4106 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 4107 | PyUnicode_GET_SIZE(str), |
| 4108 | mapping, |
| 4109 | errors); |
| 4110 | Py_DECREF(str); |
| 4111 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4112 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4113 | onError: |
| 4114 | Py_XDECREF(str); |
| 4115 | return NULL; |
| 4116 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4117 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4118 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 4119 | |
| 4120 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4121 | Py_ssize_t length, |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4122 | char *output, |
| 4123 | const char *errors) |
| 4124 | { |
| 4125 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4126 | PyObject *errorHandler = NULL; |
| 4127 | PyObject *exc = NULL; |
| 4128 | const char *encoding = "decimal"; |
| 4129 | const char *reason = "invalid decimal Unicode string"; |
| 4130 | /* the following variable is used for caching string comparisons |
| 4131 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 4132 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4133 | |
| 4134 | if (output == NULL) { |
| 4135 | PyErr_BadArgument(); |
| 4136 | return -1; |
| 4137 | } |
| 4138 | |
| 4139 | p = s; |
| 4140 | end = s + length; |
| 4141 | while (p < end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4142 | register Py_UNICODE ch = *p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4143 | int decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4144 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4145 | Py_ssize_t repsize; |
| 4146 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4147 | Py_UNICODE *uni2; |
| 4148 | Py_UNICODE *collstart; |
| 4149 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4150 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4151 | if (Py_UNICODE_ISSPACE(ch)) { |
| 4152 | *output++ = ' '; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4153 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4154 | continue; |
| 4155 | } |
| 4156 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 4157 | if (decimal >= 0) { |
| 4158 | *output++ = '0' + decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4159 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4160 | continue; |
| 4161 | } |
Guido van Rossum | ba47704 | 2000-04-06 18:18:10 +0000 | [diff] [blame] | 4162 | if (0 < ch && ch < 256) { |
Guido van Rossum | 42c29aa | 2000-05-03 23:58:29 +0000 | [diff] [blame] | 4163 | *output++ = (char)ch; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4164 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4165 | continue; |
| 4166 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4167 | /* All other characters are considered unencodable */ |
| 4168 | collstart = p; |
| 4169 | collend = p+1; |
| 4170 | while (collend < end) { |
| 4171 | if ((0 < *collend && *collend < 256) || |
| 4172 | !Py_UNICODE_ISSPACE(*collend) || |
| 4173 | Py_UNICODE_TODECIMAL(*collend)) |
| 4174 | break; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4175 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4176 | /* cache callback name lookup |
| 4177 | * (if not done yet, i.e. it's the first error) */ |
| 4178 | if (known_errorHandler==-1) { |
| 4179 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4180 | known_errorHandler = 1; |
| 4181 | else if (!strcmp(errors, "replace")) |
| 4182 | known_errorHandler = 2; |
| 4183 | else if (!strcmp(errors, "ignore")) |
| 4184 | known_errorHandler = 3; |
| 4185 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4186 | known_errorHandler = 4; |
| 4187 | else |
| 4188 | known_errorHandler = 0; |
| 4189 | } |
| 4190 | switch (known_errorHandler) { |
| 4191 | case 1: /* strict */ |
| 4192 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 4193 | goto onError; |
| 4194 | case 2: /* replace */ |
| 4195 | for (p = collstart; p < collend; ++p) |
| 4196 | *output++ = '?'; |
| 4197 | /* fall through */ |
| 4198 | case 3: /* ignore */ |
| 4199 | p = collend; |
| 4200 | break; |
| 4201 | case 4: /* xmlcharrefreplace */ |
| 4202 | /* generate replacement (temporarily (mis)uses p) */ |
| 4203 | for (p = collstart; p < collend; ++p) |
| 4204 | output += sprintf(output, "&#%d;", (int)*p); |
| 4205 | p = collend; |
| 4206 | break; |
| 4207 | default: |
| 4208 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 4209 | encoding, reason, s, length, &exc, |
| 4210 | collstart-s, collend-s, &newpos); |
| 4211 | if (repunicode == NULL) |
| 4212 | goto onError; |
| 4213 | /* generate replacement */ |
| 4214 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4215 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 4216 | Py_UNICODE ch = *uni2; |
| 4217 | if (Py_UNICODE_ISSPACE(ch)) |
| 4218 | *output++ = ' '; |
| 4219 | else { |
| 4220 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 4221 | if (decimal >= 0) |
| 4222 | *output++ = '0' + decimal; |
| 4223 | else if (0 < ch && ch < 256) |
| 4224 | *output++ = (char)ch; |
| 4225 | else { |
| 4226 | Py_DECREF(repunicode); |
| 4227 | raise_encode_exception(&exc, encoding, |
| 4228 | s, length, collstart-s, collend-s, reason); |
| 4229 | goto onError; |
| 4230 | } |
| 4231 | } |
| 4232 | } |
| 4233 | p = s + newpos; |
| 4234 | Py_DECREF(repunicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4235 | } |
| 4236 | } |
| 4237 | /* 0-terminate the output string */ |
| 4238 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4239 | Py_XDECREF(exc); |
| 4240 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4241 | return 0; |
| 4242 | |
| 4243 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4244 | Py_XDECREF(exc); |
| 4245 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4246 | return -1; |
| 4247 | } |
| 4248 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4249 | /* --- Helpers ------------------------------------------------------------ */ |
| 4250 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4251 | #define STRINGLIB_CHAR Py_UNICODE |
| 4252 | |
| 4253 | #define STRINGLIB_LEN PyUnicode_GET_SIZE |
| 4254 | #define STRINGLIB_NEW PyUnicode_FromUnicode |
| 4255 | #define STRINGLIB_STR PyUnicode_AS_UNICODE |
| 4256 | |
| 4257 | Py_LOCAL_INLINE(int) |
| 4258 | STRINGLIB_CMP(const Py_UNICODE* str, const Py_UNICODE* other, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4259 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4260 | if (str[0] != other[0]) |
| 4261 | return 1; |
| 4262 | return memcmp((void*) str, (void*) other, len * sizeof(Py_UNICODE)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4263 | } |
| 4264 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4265 | #define STRINGLIB_EMPTY unicode_empty |
| 4266 | |
| 4267 | #include "stringlib/fastsearch.h" |
| 4268 | |
| 4269 | #include "stringlib/count.h" |
| 4270 | #include "stringlib/find.h" |
| 4271 | #include "stringlib/partition.h" |
| 4272 | |
| 4273 | /* helper macro to fixup start/end slice values */ |
| 4274 | #define FIX_START_END(obj) \ |
| 4275 | if (start < 0) \ |
| 4276 | start += (obj)->length; \ |
| 4277 | if (start < 0) \ |
| 4278 | start = 0; \ |
| 4279 | if (end > (obj)->length) \ |
| 4280 | end = (obj)->length; \ |
| 4281 | if (end < 0) \ |
| 4282 | end += (obj)->length; \ |
| 4283 | if (end < 0) \ |
| 4284 | end = 0; |
| 4285 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4286 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4287 | PyObject *substr, |
| 4288 | Py_ssize_t start, |
| 4289 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4290 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4291 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4292 | PyUnicodeObject* str_obj; |
| 4293 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4294 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4295 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 4296 | if (!str_obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4297 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4298 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 4299 | if (!sub_obj) { |
| 4300 | Py_DECREF(str_obj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4301 | return -1; |
| 4302 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4303 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4304 | FIX_START_END(str_obj); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4305 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4306 | result = stringlib_count( |
| 4307 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
| 4308 | ); |
| 4309 | |
| 4310 | Py_DECREF(sub_obj); |
| 4311 | Py_DECREF(str_obj); |
| 4312 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4313 | return result; |
| 4314 | } |
| 4315 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4316 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4317 | PyObject *sub, |
| 4318 | Py_ssize_t start, |
| 4319 | Py_ssize_t end, |
| 4320 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4321 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4322 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4323 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4324 | str = PyUnicode_FromObject(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4325 | if (!str) |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 4326 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4327 | sub = PyUnicode_FromObject(sub); |
| 4328 | if (!sub) { |
Marc-André Lemburg | 4164439 | 2002-05-29 13:46:29 +0000 | [diff] [blame] | 4329 | Py_DECREF(str); |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 4330 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4331 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4332 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4333 | if (direction > 0) |
| 4334 | result = stringlib_find_slice( |
| 4335 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 4336 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 4337 | start, end |
| 4338 | ); |
| 4339 | else |
| 4340 | result = stringlib_rfind_slice( |
| 4341 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 4342 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 4343 | start, end |
| 4344 | ); |
| 4345 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4346 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4347 | Py_DECREF(sub); |
| 4348 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4349 | return result; |
| 4350 | } |
| 4351 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4352 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4353 | int tailmatch(PyUnicodeObject *self, |
| 4354 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4355 | Py_ssize_t start, |
| 4356 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4357 | int direction) |
| 4358 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4359 | if (substring->length == 0) |
| 4360 | return 1; |
| 4361 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4362 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4363 | |
| 4364 | end -= substring->length; |
| 4365 | if (end < start) |
| 4366 | return 0; |
| 4367 | |
| 4368 | if (direction > 0) { |
| 4369 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 4370 | return 1; |
| 4371 | } else { |
| 4372 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 4373 | return 1; |
| 4374 | } |
| 4375 | |
| 4376 | return 0; |
| 4377 | } |
| 4378 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4379 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4380 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4381 | Py_ssize_t start, |
| 4382 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4383 | int direction) |
| 4384 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4385 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4386 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4387 | str = PyUnicode_FromObject(str); |
| 4388 | if (str == NULL) |
| 4389 | return -1; |
| 4390 | substr = PyUnicode_FromObject(substr); |
| 4391 | if (substr == NULL) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 4392 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4393 | return -1; |
| 4394 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4395 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4396 | result = tailmatch((PyUnicodeObject *)str, |
| 4397 | (PyUnicodeObject *)substr, |
| 4398 | start, end, direction); |
| 4399 | Py_DECREF(str); |
| 4400 | Py_DECREF(substr); |
| 4401 | return result; |
| 4402 | } |
| 4403 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4404 | /* Apply fixfct filter to the Unicode object self and return a |
| 4405 | reference to the modified object */ |
| 4406 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4407 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4408 | PyObject *fixup(PyUnicodeObject *self, |
| 4409 | int (*fixfct)(PyUnicodeObject *s)) |
| 4410 | { |
| 4411 | |
| 4412 | PyUnicodeObject *u; |
| 4413 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4414 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4415 | if (u == NULL) |
| 4416 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4417 | |
| 4418 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 4419 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4420 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4421 | /* fixfct should return TRUE if it modified the buffer. If |
| 4422 | FALSE, return a reference to the original buffer instead |
| 4423 | (to save space, not time) */ |
| 4424 | Py_INCREF(self); |
| 4425 | Py_DECREF(u); |
| 4426 | return (PyObject*) self; |
| 4427 | } |
| 4428 | return (PyObject*) u; |
| 4429 | } |
| 4430 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4431 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4432 | int fixupper(PyUnicodeObject *self) |
| 4433 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4434 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4435 | Py_UNICODE *s = self->str; |
| 4436 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4437 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4438 | while (len-- > 0) { |
| 4439 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4440 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4441 | ch = Py_UNICODE_TOUPPER(*s); |
| 4442 | if (ch != *s) { |
| 4443 | status = 1; |
| 4444 | *s = ch; |
| 4445 | } |
| 4446 | s++; |
| 4447 | } |
| 4448 | |
| 4449 | return status; |
| 4450 | } |
| 4451 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4452 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4453 | int fixlower(PyUnicodeObject *self) |
| 4454 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4455 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4456 | Py_UNICODE *s = self->str; |
| 4457 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4458 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4459 | while (len-- > 0) { |
| 4460 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4461 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4462 | ch = Py_UNICODE_TOLOWER(*s); |
| 4463 | if (ch != *s) { |
| 4464 | status = 1; |
| 4465 | *s = ch; |
| 4466 | } |
| 4467 | s++; |
| 4468 | } |
| 4469 | |
| 4470 | return status; |
| 4471 | } |
| 4472 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4473 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4474 | int fixswapcase(PyUnicodeObject *self) |
| 4475 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4476 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4477 | Py_UNICODE *s = self->str; |
| 4478 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4479 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4480 | while (len-- > 0) { |
| 4481 | if (Py_UNICODE_ISUPPER(*s)) { |
| 4482 | *s = Py_UNICODE_TOLOWER(*s); |
| 4483 | status = 1; |
| 4484 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 4485 | *s = Py_UNICODE_TOUPPER(*s); |
| 4486 | status = 1; |
| 4487 | } |
| 4488 | s++; |
| 4489 | } |
| 4490 | |
| 4491 | return status; |
| 4492 | } |
| 4493 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4494 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4495 | int fixcapitalize(PyUnicodeObject *self) |
| 4496 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4497 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4498 | Py_UNICODE *s = self->str; |
| 4499 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4500 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4501 | if (len == 0) |
| 4502 | return 0; |
| 4503 | if (Py_UNICODE_ISLOWER(*s)) { |
| 4504 | *s = Py_UNICODE_TOUPPER(*s); |
| 4505 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4506 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4507 | s++; |
| 4508 | while (--len > 0) { |
| 4509 | if (Py_UNICODE_ISUPPER(*s)) { |
| 4510 | *s = Py_UNICODE_TOLOWER(*s); |
| 4511 | status = 1; |
| 4512 | } |
| 4513 | s++; |
| 4514 | } |
| 4515 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4516 | } |
| 4517 | |
| 4518 | static |
| 4519 | int fixtitle(PyUnicodeObject *self) |
| 4520 | { |
| 4521 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 4522 | register Py_UNICODE *e; |
| 4523 | int previous_is_cased; |
| 4524 | |
| 4525 | /* Shortcut for single character strings */ |
| 4526 | if (PyUnicode_GET_SIZE(self) == 1) { |
| 4527 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 4528 | if (*p != ch) { |
| 4529 | *p = ch; |
| 4530 | return 1; |
| 4531 | } |
| 4532 | else |
| 4533 | return 0; |
| 4534 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4535 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4536 | e = p + PyUnicode_GET_SIZE(self); |
| 4537 | previous_is_cased = 0; |
| 4538 | for (; p < e; p++) { |
| 4539 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4540 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4541 | if (previous_is_cased) |
| 4542 | *p = Py_UNICODE_TOLOWER(ch); |
| 4543 | else |
| 4544 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4545 | |
| 4546 | if (Py_UNICODE_ISLOWER(ch) || |
| 4547 | Py_UNICODE_ISUPPER(ch) || |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4548 | Py_UNICODE_ISTITLE(ch)) |
| 4549 | previous_is_cased = 1; |
| 4550 | else |
| 4551 | previous_is_cased = 0; |
| 4552 | } |
| 4553 | return 1; |
| 4554 | } |
| 4555 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4556 | PyObject * |
| 4557 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4558 | { |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4559 | PyObject *internal_separator = NULL; |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 4560 | const Py_UNICODE blank = ' '; |
| 4561 | const Py_UNICODE *sep = ␣ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4562 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4563 | PyUnicodeObject *res = NULL; /* the result */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4564 | Py_ssize_t res_alloc = 100; /* # allocated bytes for string in res */ |
| 4565 | Py_ssize_t res_used; /* # used bytes */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4566 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 4567 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4568 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4569 | PyObject *item; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4570 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4571 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4572 | fseq = PySequence_Fast(seq, ""); |
| 4573 | if (fseq == NULL) { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4574 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4575 | } |
| 4576 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4577 | /* Grrrr. A codec may be invoked to convert str objects to |
| 4578 | * Unicode, and so it's possible to call back into Python code |
| 4579 | * during PyUnicode_FromObject(), and so it's possible for a sick |
| 4580 | * codec to change the size of fseq (if seq is a list). Therefore |
| 4581 | * we have to keep refetching the size -- can't assume seqlen |
| 4582 | * is invariant. |
| 4583 | */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4584 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 4585 | /* If empty sequence, return u"". */ |
| 4586 | if (seqlen == 0) { |
| 4587 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 4588 | goto Done; |
| 4589 | } |
| 4590 | /* If singleton sequence with an exact Unicode, return that. */ |
| 4591 | if (seqlen == 1) { |
| 4592 | item = PySequence_Fast_GET_ITEM(fseq, 0); |
| 4593 | if (PyUnicode_CheckExact(item)) { |
| 4594 | Py_INCREF(item); |
| 4595 | res = (PyUnicodeObject *)item; |
| 4596 | goto Done; |
| 4597 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4598 | } |
| 4599 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4600 | /* At least two items to join, or one that isn't exact Unicode. */ |
| 4601 | if (seqlen > 1) { |
| 4602 | /* Set up sep and seplen -- they're needed. */ |
| 4603 | if (separator == NULL) { |
| 4604 | sep = ␣ |
| 4605 | seplen = 1; |
| 4606 | } |
| 4607 | else { |
| 4608 | internal_separator = PyUnicode_FromObject(separator); |
| 4609 | if (internal_separator == NULL) |
| 4610 | goto onError; |
| 4611 | sep = PyUnicode_AS_UNICODE(internal_separator); |
| 4612 | seplen = PyUnicode_GET_SIZE(internal_separator); |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4613 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 4614 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4615 | } |
| 4616 | } |
| 4617 | |
| 4618 | /* Get space. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4619 | res = _PyUnicode_New(res_alloc); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4620 | if (res == NULL) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4621 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4622 | res_p = PyUnicode_AS_UNICODE(res); |
| 4623 | res_used = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4624 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4625 | for (i = 0; i < seqlen; ++i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4626 | Py_ssize_t itemlen; |
| 4627 | Py_ssize_t new_res_used; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4628 | |
| 4629 | item = PySequence_Fast_GET_ITEM(fseq, i); |
| 4630 | /* Convert item to Unicode. */ |
| 4631 | if (! PyUnicode_Check(item) && ! PyString_Check(item)) { |
| 4632 | PyErr_Format(PyExc_TypeError, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4633 | "sequence item %zd: expected string or Unicode," |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4634 | " %.80s found", |
| 4635 | i, item->ob_type->tp_name); |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 4636 | goto onError; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4637 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4638 | item = PyUnicode_FromObject(item); |
| 4639 | if (item == NULL) |
| 4640 | goto onError; |
| 4641 | /* We own a reference to item from here on. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4642 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4643 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 4644 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 4645 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4646 | /* 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] | 4647 | itemlen = PyUnicode_GET_SIZE(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4648 | new_res_used = res_used + itemlen; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4649 | if (new_res_used < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4650 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4651 | if (i < seqlen - 1) { |
| 4652 | new_res_used += seplen; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4653 | if (new_res_used < 0) |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4654 | goto Overflow; |
| 4655 | } |
| 4656 | if (new_res_used > res_alloc) { |
| 4657 | /* double allocated size until it's big enough */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4658 | do { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4659 | res_alloc += res_alloc; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4660 | if (res_alloc <= 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4661 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4662 | } while (new_res_used > res_alloc); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4663 | if (_PyUnicode_Resize(&res, res_alloc) < 0) { |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4664 | Py_DECREF(item); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4665 | goto onError; |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4666 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4667 | res_p = PyUnicode_AS_UNICODE(res) + res_used; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4668 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4669 | |
| 4670 | /* Copy item, and maybe the separator. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4671 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4672 | res_p += itemlen; |
| 4673 | if (i < seqlen - 1) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4674 | Py_UNICODE_COPY(res_p, sep, seplen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4675 | res_p += seplen; |
| 4676 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4677 | Py_DECREF(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4678 | res_used = new_res_used; |
| 4679 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4680 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4681 | /* Shrink res to match the used area; this probably can't fail, |
| 4682 | * but it's cheap to check. |
| 4683 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4684 | if (_PyUnicode_Resize(&res, res_used) < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4685 | goto onError; |
| 4686 | |
| 4687 | Done: |
| 4688 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4689 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4690 | return (PyObject *)res; |
| 4691 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4692 | Overflow: |
| 4693 | PyErr_SetString(PyExc_OverflowError, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4694 | "join() result is too long for a Python string"); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4695 | Py_DECREF(item); |
| 4696 | /* fall through */ |
| 4697 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4698 | onError: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4699 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4700 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4701 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4702 | return NULL; |
| 4703 | } |
| 4704 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4705 | static |
| 4706 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4707 | Py_ssize_t left, |
| 4708 | Py_ssize_t right, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4709 | Py_UNICODE fill) |
| 4710 | { |
| 4711 | PyUnicodeObject *u; |
| 4712 | |
| 4713 | if (left < 0) |
| 4714 | left = 0; |
| 4715 | if (right < 0) |
| 4716 | right = 0; |
| 4717 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4718 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4719 | Py_INCREF(self); |
| 4720 | return self; |
| 4721 | } |
| 4722 | |
| 4723 | u = _PyUnicode_New(left + self->length + right); |
| 4724 | if (u) { |
| 4725 | if (left) |
| 4726 | Py_UNICODE_FILL(u->str, fill, left); |
| 4727 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 4728 | if (right) |
| 4729 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 4730 | } |
| 4731 | |
| 4732 | return u; |
| 4733 | } |
| 4734 | |
| 4735 | #define SPLIT_APPEND(data, left, right) \ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4736 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4737 | if (!str) \ |
| 4738 | goto onError; \ |
| 4739 | if (PyList_Append(list, str)) { \ |
| 4740 | Py_DECREF(str); \ |
| 4741 | goto onError; \ |
| 4742 | } \ |
| 4743 | else \ |
| 4744 | Py_DECREF(str); |
| 4745 | |
| 4746 | static |
| 4747 | PyObject *split_whitespace(PyUnicodeObject *self, |
| 4748 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4749 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4750 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4751 | register Py_ssize_t i; |
| 4752 | register Py_ssize_t j; |
| 4753 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4754 | PyObject *str; |
| 4755 | |
| 4756 | for (i = j = 0; i < len; ) { |
| 4757 | /* find a token */ |
| 4758 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 4759 | i++; |
| 4760 | j = i; |
| 4761 | while (i < len && !Py_UNICODE_ISSPACE(self->str[i])) |
| 4762 | i++; |
| 4763 | if (j < i) { |
| 4764 | if (maxcount-- <= 0) |
| 4765 | break; |
| 4766 | SPLIT_APPEND(self->str, j, i); |
| 4767 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 4768 | i++; |
| 4769 | j = i; |
| 4770 | } |
| 4771 | } |
| 4772 | if (j < len) { |
| 4773 | SPLIT_APPEND(self->str, j, len); |
| 4774 | } |
| 4775 | return list; |
| 4776 | |
| 4777 | onError: |
| 4778 | Py_DECREF(list); |
| 4779 | return NULL; |
| 4780 | } |
| 4781 | |
| 4782 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4783 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4784 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4785 | register Py_ssize_t i; |
| 4786 | register Py_ssize_t j; |
| 4787 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4788 | PyObject *list; |
| 4789 | PyObject *str; |
| 4790 | Py_UNICODE *data; |
| 4791 | |
| 4792 | string = PyUnicode_FromObject(string); |
| 4793 | if (string == NULL) |
| 4794 | return NULL; |
| 4795 | data = PyUnicode_AS_UNICODE(string); |
| 4796 | len = PyUnicode_GET_SIZE(string); |
| 4797 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4798 | list = PyList_New(0); |
| 4799 | if (!list) |
| 4800 | goto onError; |
| 4801 | |
| 4802 | for (i = j = 0; i < len; ) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4803 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4804 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4805 | /* Find a line and append it */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4806 | while (i < len && !BLOOM_LINEBREAK(data[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4807 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4808 | |
| 4809 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4810 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4811 | if (i < len) { |
| 4812 | if (data[i] == '\r' && i + 1 < len && |
| 4813 | data[i+1] == '\n') |
| 4814 | i += 2; |
| 4815 | else |
| 4816 | i++; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4817 | if (keepends) |
| 4818 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4819 | } |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4820 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4821 | j = i; |
| 4822 | } |
| 4823 | if (j < len) { |
| 4824 | SPLIT_APPEND(data, j, len); |
| 4825 | } |
| 4826 | |
| 4827 | Py_DECREF(string); |
| 4828 | return list; |
| 4829 | |
| 4830 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 4831 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4832 | Py_DECREF(string); |
| 4833 | return NULL; |
| 4834 | } |
| 4835 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4836 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4837 | PyObject *split_char(PyUnicodeObject *self, |
| 4838 | PyObject *list, |
| 4839 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4840 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4841 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4842 | register Py_ssize_t i; |
| 4843 | register Py_ssize_t j; |
| 4844 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4845 | PyObject *str; |
| 4846 | |
| 4847 | for (i = j = 0; i < len; ) { |
| 4848 | if (self->str[i] == ch) { |
| 4849 | if (maxcount-- <= 0) |
| 4850 | break; |
| 4851 | SPLIT_APPEND(self->str, j, i); |
| 4852 | i = j = i + 1; |
| 4853 | } else |
| 4854 | i++; |
| 4855 | } |
| 4856 | if (j <= len) { |
| 4857 | SPLIT_APPEND(self->str, j, len); |
| 4858 | } |
| 4859 | return list; |
| 4860 | |
| 4861 | onError: |
| 4862 | Py_DECREF(list); |
| 4863 | return NULL; |
| 4864 | } |
| 4865 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4866 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4867 | PyObject *split_substring(PyUnicodeObject *self, |
| 4868 | PyObject *list, |
| 4869 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4870 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4871 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4872 | register Py_ssize_t i; |
| 4873 | register Py_ssize_t j; |
| 4874 | Py_ssize_t len = self->length; |
| 4875 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4876 | PyObject *str; |
| 4877 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 4878 | for (i = j = 0; i <= len - sublen; ) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4879 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 4880 | if (maxcount-- <= 0) |
| 4881 | break; |
| 4882 | SPLIT_APPEND(self->str, j, i); |
| 4883 | i = j = i + sublen; |
| 4884 | } else |
| 4885 | i++; |
| 4886 | } |
| 4887 | if (j <= len) { |
| 4888 | SPLIT_APPEND(self->str, j, len); |
| 4889 | } |
| 4890 | return list; |
| 4891 | |
| 4892 | onError: |
| 4893 | Py_DECREF(list); |
| 4894 | return NULL; |
| 4895 | } |
| 4896 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4897 | static |
| 4898 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
| 4899 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4900 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4901 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4902 | register Py_ssize_t i; |
| 4903 | register Py_ssize_t j; |
| 4904 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4905 | PyObject *str; |
| 4906 | |
| 4907 | for (i = j = len - 1; i >= 0; ) { |
| 4908 | /* find a token */ |
| 4909 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 4910 | i--; |
| 4911 | j = i; |
| 4912 | while (i >= 0 && !Py_UNICODE_ISSPACE(self->str[i])) |
| 4913 | i--; |
| 4914 | if (j > i) { |
| 4915 | if (maxcount-- <= 0) |
| 4916 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4917 | SPLIT_APPEND(self->str, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4918 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 4919 | i--; |
| 4920 | j = i; |
| 4921 | } |
| 4922 | } |
| 4923 | if (j >= 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4924 | SPLIT_APPEND(self->str, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4925 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4926 | if (PyList_Reverse(list) < 0) |
| 4927 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4928 | return list; |
| 4929 | |
| 4930 | onError: |
| 4931 | Py_DECREF(list); |
| 4932 | return NULL; |
| 4933 | } |
| 4934 | |
| 4935 | static |
| 4936 | PyObject *rsplit_char(PyUnicodeObject *self, |
| 4937 | PyObject *list, |
| 4938 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4939 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4940 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4941 | register Py_ssize_t i; |
| 4942 | register Py_ssize_t j; |
| 4943 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4944 | PyObject *str; |
| 4945 | |
| 4946 | for (i = j = len - 1; i >= 0; ) { |
| 4947 | if (self->str[i] == ch) { |
| 4948 | if (maxcount-- <= 0) |
| 4949 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4950 | SPLIT_APPEND(self->str, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4951 | j = i = i - 1; |
| 4952 | } else |
| 4953 | i--; |
| 4954 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 4955 | if (j >= -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4956 | SPLIT_APPEND(self->str, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4957 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4958 | if (PyList_Reverse(list) < 0) |
| 4959 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4960 | return list; |
| 4961 | |
| 4962 | onError: |
| 4963 | Py_DECREF(list); |
| 4964 | return NULL; |
| 4965 | } |
| 4966 | |
| 4967 | static |
| 4968 | PyObject *rsplit_substring(PyUnicodeObject *self, |
| 4969 | PyObject *list, |
| 4970 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4971 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4972 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4973 | register Py_ssize_t i; |
| 4974 | register Py_ssize_t j; |
| 4975 | Py_ssize_t len = self->length; |
| 4976 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4977 | PyObject *str; |
| 4978 | |
| 4979 | for (i = len - sublen, j = len; i >= 0; ) { |
| 4980 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 4981 | if (maxcount-- <= 0) |
| 4982 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4983 | SPLIT_APPEND(self->str, i + sublen, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4984 | j = i; |
| 4985 | i -= sublen; |
| 4986 | } else |
| 4987 | i--; |
| 4988 | } |
| 4989 | if (j >= 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4990 | SPLIT_APPEND(self->str, 0, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4991 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4992 | if (PyList_Reverse(list) < 0) |
| 4993 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4994 | return list; |
| 4995 | |
| 4996 | onError: |
| 4997 | Py_DECREF(list); |
| 4998 | return NULL; |
| 4999 | } |
| 5000 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5001 | #undef SPLIT_APPEND |
| 5002 | |
| 5003 | static |
| 5004 | PyObject *split(PyUnicodeObject *self, |
| 5005 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5006 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5007 | { |
| 5008 | PyObject *list; |
| 5009 | |
| 5010 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5011 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5012 | |
| 5013 | list = PyList_New(0); |
| 5014 | if (!list) |
| 5015 | return NULL; |
| 5016 | |
| 5017 | if (substring == NULL) |
| 5018 | return split_whitespace(self,list,maxcount); |
| 5019 | |
| 5020 | else if (substring->length == 1) |
| 5021 | return split_char(self,list,substring->str[0],maxcount); |
| 5022 | |
| 5023 | else if (substring->length == 0) { |
| 5024 | Py_DECREF(list); |
| 5025 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5026 | return NULL; |
| 5027 | } |
| 5028 | else |
| 5029 | return split_substring(self,list,substring,maxcount); |
| 5030 | } |
| 5031 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5032 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5033 | PyObject *rsplit(PyUnicodeObject *self, |
| 5034 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5035 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5036 | { |
| 5037 | PyObject *list; |
| 5038 | |
| 5039 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5040 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5041 | |
| 5042 | list = PyList_New(0); |
| 5043 | if (!list) |
| 5044 | return NULL; |
| 5045 | |
| 5046 | if (substring == NULL) |
| 5047 | return rsplit_whitespace(self,list,maxcount); |
| 5048 | |
| 5049 | else if (substring->length == 1) |
| 5050 | return rsplit_char(self,list,substring->str[0],maxcount); |
| 5051 | |
| 5052 | else if (substring->length == 0) { |
| 5053 | Py_DECREF(list); |
| 5054 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5055 | return NULL; |
| 5056 | } |
| 5057 | else |
| 5058 | return rsplit_substring(self,list,substring,maxcount); |
| 5059 | } |
| 5060 | |
| 5061 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5062 | PyObject *replace(PyUnicodeObject *self, |
| 5063 | PyUnicodeObject *str1, |
| 5064 | PyUnicodeObject *str2, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5065 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5066 | { |
| 5067 | PyUnicodeObject *u; |
| 5068 | |
| 5069 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5070 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5071 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5072 | if (str1->length == str2->length) { |
| 5073 | /* same length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5074 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5075 | if (str1->length == 1) { |
| 5076 | /* replace characters */ |
| 5077 | Py_UNICODE u1, u2; |
| 5078 | if (!findchar(self->str, self->length, str1->str[0])) |
| 5079 | goto nothing; |
| 5080 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5081 | if (!u) |
| 5082 | return NULL; |
| 5083 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5084 | u1 = str1->str[0]; |
| 5085 | u2 = str2->str[0]; |
| 5086 | for (i = 0; i < u->length; i++) |
| 5087 | if (u->str[i] == u1) { |
| 5088 | if (--maxcount < 0) |
| 5089 | break; |
| 5090 | u->str[i] = u2; |
| 5091 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5092 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5093 | i = fastsearch( |
| 5094 | self->str, self->length, str1->str, str1->length, FAST_SEARCH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5095 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5096 | if (i < 0) |
| 5097 | goto nothing; |
| 5098 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5099 | if (!u) |
| 5100 | return NULL; |
| 5101 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5102 | while (i <= self->length - str1->length) |
| 5103 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 5104 | if (--maxcount < 0) |
| 5105 | break; |
| 5106 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 5107 | i += str1->length; |
| 5108 | } else |
| 5109 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5110 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5111 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5112 | |
| 5113 | Py_ssize_t n, i, j, e; |
| 5114 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5115 | Py_UNICODE *p; |
| 5116 | |
| 5117 | /* replace strings */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5118 | n = stringlib_count(self->str, self->length, str1->str, str1->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5119 | if (n > maxcount) |
| 5120 | n = maxcount; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5121 | if (n == 0) |
| 5122 | goto nothing; |
| 5123 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 5124 | delta = (str2->length - str1->length); |
| 5125 | if (delta == 0) { |
| 5126 | new_size = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5127 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5128 | product = n * (str2->length - str1->length); |
| 5129 | if ((product / (str2->length - str1->length)) != n) { |
| 5130 | PyErr_SetString(PyExc_OverflowError, |
| 5131 | "replace string is too long"); |
| 5132 | return NULL; |
| 5133 | } |
| 5134 | new_size = self->length + product; |
| 5135 | if (new_size < 0) { |
| 5136 | PyErr_SetString(PyExc_OverflowError, |
| 5137 | "replace string is too long"); |
| 5138 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5139 | } |
| 5140 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5141 | u = _PyUnicode_New(new_size); |
| 5142 | if (!u) |
| 5143 | return NULL; |
| 5144 | i = 0; |
| 5145 | p = u->str; |
| 5146 | e = self->length - str1->length; |
| 5147 | if (str1->length > 0) { |
| 5148 | while (n-- > 0) { |
| 5149 | /* look for next match */ |
| 5150 | j = i; |
| 5151 | while (j <= e) { |
| 5152 | if (Py_UNICODE_MATCH(self, j, str1)) |
| 5153 | break; |
| 5154 | j++; |
| 5155 | } |
| 5156 | if (j > i) { |
| 5157 | if (j > e) |
| 5158 | break; |
| 5159 | /* copy unchanged part [i:j] */ |
| 5160 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 5161 | p += j - i; |
| 5162 | } |
| 5163 | /* copy substitution string */ |
| 5164 | if (str2->length > 0) { |
| 5165 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 5166 | p += str2->length; |
| 5167 | } |
| 5168 | i = j + str1->length; |
| 5169 | } |
| 5170 | if (i < self->length) |
| 5171 | /* copy tail [i:] */ |
| 5172 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 5173 | } else { |
| 5174 | /* interleave */ |
| 5175 | while (n > 0) { |
| 5176 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 5177 | p += str2->length; |
| 5178 | if (--n <= 0) |
| 5179 | break; |
| 5180 | *p++ = self->str[i++]; |
| 5181 | } |
| 5182 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 5183 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5184 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5185 | return (PyObject *) u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5186 | |
| 5187 | nothing: |
| 5188 | /* nothing to replace; return original string (when possible) */ |
| 5189 | if (PyUnicode_CheckExact(self)) { |
| 5190 | Py_INCREF(self); |
| 5191 | return (PyObject *) self; |
| 5192 | } |
| 5193 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5194 | } |
| 5195 | |
| 5196 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 5197 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5198 | PyDoc_STRVAR(title__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5199 | "S.title() -> unicode\n\ |
| 5200 | \n\ |
| 5201 | 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] | 5202 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5203 | |
| 5204 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5205 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5206 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5207 | return fixup(self, fixtitle); |
| 5208 | } |
| 5209 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5210 | PyDoc_STRVAR(capitalize__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5211 | "S.capitalize() -> unicode\n\ |
| 5212 | \n\ |
| 5213 | 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] | 5214 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5215 | |
| 5216 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5217 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5218 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5219 | return fixup(self, fixcapitalize); |
| 5220 | } |
| 5221 | |
| 5222 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5223 | PyDoc_STRVAR(capwords__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5224 | "S.capwords() -> unicode\n\ |
| 5225 | \n\ |
| 5226 | 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] | 5227 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5228 | |
| 5229 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5230 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5231 | { |
| 5232 | PyObject *list; |
| 5233 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5234 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5235 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5236 | /* Split into words */ |
| 5237 | list = split(self, NULL, -1); |
| 5238 | if (!list) |
| 5239 | return NULL; |
| 5240 | |
| 5241 | /* Capitalize each word */ |
| 5242 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 5243 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 5244 | fixcapitalize); |
| 5245 | if (item == NULL) |
| 5246 | goto onError; |
| 5247 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 5248 | PyList_SET_ITEM(list, i, item); |
| 5249 | } |
| 5250 | |
| 5251 | /* Join the words to form a new string */ |
| 5252 | item = PyUnicode_Join(NULL, list); |
| 5253 | |
| 5254 | onError: |
| 5255 | Py_DECREF(list); |
| 5256 | return (PyObject *)item; |
| 5257 | } |
| 5258 | #endif |
| 5259 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5260 | /* Argument converter. Coerces to a single unicode character */ |
| 5261 | |
| 5262 | static int |
| 5263 | convert_uc(PyObject *obj, void *addr) |
| 5264 | { |
| 5265 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 5266 | PyObject *uniobj; |
| 5267 | Py_UNICODE *unistr; |
| 5268 | |
| 5269 | uniobj = PyUnicode_FromObject(obj); |
| 5270 | if (uniobj == NULL) { |
| 5271 | PyErr_SetString(PyExc_TypeError, |
| 5272 | "The fill character cannot be converted to Unicode"); |
| 5273 | return 0; |
| 5274 | } |
| 5275 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 5276 | PyErr_SetString(PyExc_TypeError, |
| 5277 | "The fill character must be exactly one character long"); |
| 5278 | Py_DECREF(uniobj); |
| 5279 | return 0; |
| 5280 | } |
| 5281 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 5282 | *fillcharloc = unistr[0]; |
| 5283 | Py_DECREF(uniobj); |
| 5284 | return 1; |
| 5285 | } |
| 5286 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5287 | PyDoc_STRVAR(center__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5288 | "S.center(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5289 | \n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5290 | Return S centered in a Unicode string of length width. Padding is\n\ |
| 5291 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5292 | |
| 5293 | static PyObject * |
| 5294 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 5295 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5296 | Py_ssize_t marg, left; |
| 5297 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5298 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5299 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 5300 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5301 | return NULL; |
| 5302 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5303 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5304 | Py_INCREF(self); |
| 5305 | return (PyObject*) self; |
| 5306 | } |
| 5307 | |
| 5308 | marg = width - self->length; |
| 5309 | left = marg / 2 + (marg & width & 1); |
| 5310 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5311 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5312 | } |
| 5313 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5314 | #if 0 |
| 5315 | |
| 5316 | /* This code should go into some future Unicode collation support |
| 5317 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 5318 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5319 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5320 | /* speedy UTF-16 code point order comparison */ |
| 5321 | /* gleaned from: */ |
| 5322 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 5323 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 5324 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5325 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5326 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5327 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 5328 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 5329 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5330 | }; |
| 5331 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5332 | static int |
| 5333 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 5334 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5335 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5336 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5337 | Py_UNICODE *s1 = str1->str; |
| 5338 | Py_UNICODE *s2 = str2->str; |
| 5339 | |
| 5340 | len1 = str1->length; |
| 5341 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5342 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5343 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5344 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5345 | |
| 5346 | c1 = *s1++; |
| 5347 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 5348 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5349 | if (c1 > (1<<11) * 26) |
| 5350 | c1 += utf16Fixup[c1>>11]; |
| 5351 | if (c2 > (1<<11) * 26) |
| 5352 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5353 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 5354 | |
| 5355 | if (c1 != c2) |
| 5356 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5357 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5358 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5359 | } |
| 5360 | |
| 5361 | return (len1 < len2) ? -1 : (len1 != len2); |
| 5362 | } |
| 5363 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5364 | #else |
| 5365 | |
| 5366 | static int |
| 5367 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 5368 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5369 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5370 | |
| 5371 | Py_UNICODE *s1 = str1->str; |
| 5372 | Py_UNICODE *s2 = str2->str; |
| 5373 | |
| 5374 | len1 = str1->length; |
| 5375 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5376 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5377 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5378 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5379 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 5380 | c1 = *s1++; |
| 5381 | c2 = *s2++; |
| 5382 | |
| 5383 | if (c1 != c2) |
| 5384 | return (c1 < c2) ? -1 : 1; |
| 5385 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5386 | len1--; len2--; |
| 5387 | } |
| 5388 | |
| 5389 | return (len1 < len2) ? -1 : (len1 != len2); |
| 5390 | } |
| 5391 | |
| 5392 | #endif |
| 5393 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5394 | int PyUnicode_Compare(PyObject *left, |
| 5395 | PyObject *right) |
| 5396 | { |
| 5397 | PyUnicodeObject *u = NULL, *v = NULL; |
| 5398 | int result; |
| 5399 | |
| 5400 | /* Coerce the two arguments */ |
| 5401 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 5402 | if (u == NULL) |
| 5403 | goto onError; |
| 5404 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 5405 | if (v == NULL) |
| 5406 | goto onError; |
| 5407 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5408 | /* Shortcut for empty or interned objects */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5409 | if (v == u) { |
| 5410 | Py_DECREF(u); |
| 5411 | Py_DECREF(v); |
| 5412 | return 0; |
| 5413 | } |
| 5414 | |
| 5415 | result = unicode_compare(u, v); |
| 5416 | |
| 5417 | Py_DECREF(u); |
| 5418 | Py_DECREF(v); |
| 5419 | return result; |
| 5420 | |
| 5421 | onError: |
| 5422 | Py_XDECREF(u); |
| 5423 | Py_XDECREF(v); |
| 5424 | return -1; |
| 5425 | } |
| 5426 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 5427 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 5428 | PyObject *right, |
| 5429 | int op) |
| 5430 | { |
| 5431 | int result; |
| 5432 | |
| 5433 | result = PyUnicode_Compare(left, right); |
| 5434 | if (result == -1 && PyErr_Occurred()) |
| 5435 | goto onError; |
| 5436 | |
| 5437 | /* Convert the return value to a Boolean */ |
| 5438 | switch (op) { |
| 5439 | case Py_EQ: |
| 5440 | result = (result == 0); |
| 5441 | break; |
| 5442 | case Py_NE: |
| 5443 | result = (result != 0); |
| 5444 | break; |
| 5445 | case Py_LE: |
| 5446 | result = (result <= 0); |
| 5447 | break; |
| 5448 | case Py_GE: |
| 5449 | result = (result >= 0); |
| 5450 | break; |
| 5451 | case Py_LT: |
| 5452 | result = (result == -1); |
| 5453 | break; |
| 5454 | case Py_GT: |
| 5455 | result = (result == 1); |
| 5456 | break; |
| 5457 | } |
| 5458 | return PyBool_FromLong(result); |
| 5459 | |
| 5460 | onError: |
| 5461 | |
| 5462 | /* Standard case |
| 5463 | |
| 5464 | Type errors mean that PyUnicode_FromObject() could not convert |
| 5465 | one of the arguments (usually the right hand side) to Unicode, |
| 5466 | ie. we can't handle the comparison request. However, it is |
| 5467 | possible that the other object knows a comparison method, which |
| 5468 | is why we return Py_NotImplemented to give the other object a |
| 5469 | chance. |
| 5470 | |
| 5471 | */ |
| 5472 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 5473 | PyErr_Clear(); |
| 5474 | Py_INCREF(Py_NotImplemented); |
| 5475 | return Py_NotImplemented; |
| 5476 | } |
| 5477 | if (op != Py_EQ && op != Py_NE) |
| 5478 | return NULL; |
| 5479 | |
| 5480 | /* Equality comparison. |
| 5481 | |
| 5482 | This is a special case: we silence any PyExc_UnicodeDecodeError |
| 5483 | and instead turn it into a PyErr_UnicodeWarning. |
| 5484 | |
| 5485 | */ |
| 5486 | if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) |
| 5487 | return NULL; |
| 5488 | PyErr_Clear(); |
| 5489 | if (PyErr_Warn(PyExc_UnicodeWarning, |
| 5490 | (op == Py_EQ) ? |
| 5491 | "Unicode equal comparison " |
| 5492 | "failed to convert both arguments to Unicode - " |
| 5493 | "interpreting them as being unequal" : |
| 5494 | "Unicode unequal comparison " |
| 5495 | "failed to convert both arguments to Unicode - " |
| 5496 | "interpreting them as being unequal" |
| 5497 | ) < 0) |
| 5498 | return NULL; |
| 5499 | result = (op == Py_NE); |
| 5500 | return PyBool_FromLong(result); |
| 5501 | } |
| 5502 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5503 | int PyUnicode_Contains(PyObject *container, |
| 5504 | PyObject *element) |
| 5505 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5506 | PyObject *str, *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5507 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5508 | |
| 5509 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5510 | sub = PyUnicode_FromObject(element); |
| 5511 | if (!sub) { |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 5512 | PyErr_SetString(PyExc_TypeError, |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 5513 | "'in <string>' requires string as left operand"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5514 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5515 | } |
| 5516 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5517 | str = PyUnicode_FromObject(container); |
| 5518 | if (!str) { |
| 5519 | Py_DECREF(sub); |
| 5520 | return -1; |
| 5521 | } |
| 5522 | |
| 5523 | result = stringlib_contains_obj(str, sub); |
| 5524 | |
| 5525 | Py_DECREF(str); |
| 5526 | Py_DECREF(sub); |
| 5527 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5528 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5529 | } |
| 5530 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5531 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 5532 | |
| 5533 | PyObject *PyUnicode_Concat(PyObject *left, |
| 5534 | PyObject *right) |
| 5535 | { |
| 5536 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 5537 | |
| 5538 | /* Coerce the two arguments */ |
| 5539 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 5540 | if (u == NULL) |
| 5541 | goto onError; |
| 5542 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 5543 | if (v == NULL) |
| 5544 | goto onError; |
| 5545 | |
| 5546 | /* Shortcuts */ |
| 5547 | if (v == unicode_empty) { |
| 5548 | Py_DECREF(v); |
| 5549 | return (PyObject *)u; |
| 5550 | } |
| 5551 | if (u == unicode_empty) { |
| 5552 | Py_DECREF(u); |
| 5553 | return (PyObject *)v; |
| 5554 | } |
| 5555 | |
| 5556 | /* Concat the two Unicode strings */ |
| 5557 | w = _PyUnicode_New(u->length + v->length); |
| 5558 | if (w == NULL) |
| 5559 | goto onError; |
| 5560 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 5561 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 5562 | |
| 5563 | Py_DECREF(u); |
| 5564 | Py_DECREF(v); |
| 5565 | return (PyObject *)w; |
| 5566 | |
| 5567 | onError: |
| 5568 | Py_XDECREF(u); |
| 5569 | Py_XDECREF(v); |
| 5570 | return NULL; |
| 5571 | } |
| 5572 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5573 | PyDoc_STRVAR(count__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5574 | "S.count(sub[, start[, end]]) -> int\n\ |
| 5575 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5576 | Return the number of non-overlapping occurrences of substring sub in\n\ |
| 5577 | 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] | 5578 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5579 | |
| 5580 | static PyObject * |
| 5581 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 5582 | { |
| 5583 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5584 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5585 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5586 | PyObject *result; |
| 5587 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5588 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 5589 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5590 | return NULL; |
| 5591 | |
| 5592 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5593 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5594 | if (substring == NULL) |
| 5595 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5596 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5597 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5598 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5599 | result = PyInt_FromSsize_t( |
| 5600 | stringlib_count(self->str + start, end - start, |
| 5601 | substring->str, substring->length) |
| 5602 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5603 | |
| 5604 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5605 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5606 | return result; |
| 5607 | } |
| 5608 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5609 | PyDoc_STRVAR(encode__doc__, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5610 | "S.encode([encoding[,errors]]) -> string or unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5611 | \n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5612 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 5613 | 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] | 5614 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5615 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 5616 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 5617 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5618 | |
| 5619 | static PyObject * |
| 5620 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 5621 | { |
| 5622 | char *encoding = NULL; |
| 5623 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5624 | PyObject *v; |
| 5625 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5626 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 5627 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5628 | v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5629 | if (v == NULL) |
| 5630 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5631 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 5632 | PyErr_Format(PyExc_TypeError, |
| 5633 | "encoder did not return a string/unicode object " |
| 5634 | "(type=%.400s)", |
| 5635 | v->ob_type->tp_name); |
| 5636 | Py_DECREF(v); |
| 5637 | return NULL; |
| 5638 | } |
| 5639 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5640 | |
| 5641 | onError: |
| 5642 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5643 | } |
| 5644 | |
| 5645 | PyDoc_STRVAR(decode__doc__, |
| 5646 | "S.decode([encoding[,errors]]) -> string or unicode\n\ |
| 5647 | \n\ |
| 5648 | Decodes S using the codec registered for encoding. encoding defaults\n\ |
| 5649 | to the default encoding. errors may be given to set a different error\n\ |
| 5650 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 5651 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 5652 | as well as any other name registerd with codecs.register_error that is\n\ |
| 5653 | able to handle UnicodeDecodeErrors."); |
| 5654 | |
| 5655 | static PyObject * |
Marc-André Lemburg | 126b44c | 2004-07-10 12:04:20 +0000 | [diff] [blame] | 5656 | unicode_decode(PyUnicodeObject *self, PyObject *args) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5657 | { |
| 5658 | char *encoding = NULL; |
| 5659 | char *errors = NULL; |
| 5660 | PyObject *v; |
| 5661 | |
| 5662 | if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
| 5663 | return NULL; |
| 5664 | v = PyUnicode_AsDecodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5665 | if (v == NULL) |
| 5666 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5667 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 5668 | PyErr_Format(PyExc_TypeError, |
| 5669 | "decoder did not return a string/unicode object " |
| 5670 | "(type=%.400s)", |
| 5671 | v->ob_type->tp_name); |
| 5672 | Py_DECREF(v); |
| 5673 | return NULL; |
| 5674 | } |
| 5675 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5676 | |
| 5677 | onError: |
| 5678 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5679 | } |
| 5680 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5681 | PyDoc_STRVAR(expandtabs__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5682 | "S.expandtabs([tabsize]) -> unicode\n\ |
| 5683 | \n\ |
| 5684 | 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] | 5685 | 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] | 5686 | |
| 5687 | static PyObject* |
| 5688 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 5689 | { |
| 5690 | Py_UNICODE *e; |
| 5691 | Py_UNICODE *p; |
| 5692 | Py_UNICODE *q; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5693 | Py_ssize_t i, j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5694 | PyUnicodeObject *u; |
| 5695 | int tabsize = 8; |
| 5696 | |
| 5697 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 5698 | return NULL; |
| 5699 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5700 | /* First pass: determine size of output string */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5701 | i = j = 0; |
| 5702 | e = self->str + self->length; |
| 5703 | for (p = self->str; p < e; p++) |
| 5704 | if (*p == '\t') { |
| 5705 | if (tabsize > 0) |
| 5706 | j += tabsize - (j % tabsize); |
| 5707 | } |
| 5708 | else { |
| 5709 | j++; |
| 5710 | if (*p == '\n' || *p == '\r') { |
| 5711 | i += j; |
| 5712 | j = 0; |
| 5713 | } |
| 5714 | } |
| 5715 | |
| 5716 | /* Second pass: create output string and fill it */ |
| 5717 | u = _PyUnicode_New(i + j); |
| 5718 | if (!u) |
| 5719 | return NULL; |
| 5720 | |
| 5721 | j = 0; |
| 5722 | q = u->str; |
| 5723 | |
| 5724 | for (p = self->str; p < e; p++) |
| 5725 | if (*p == '\t') { |
| 5726 | if (tabsize > 0) { |
| 5727 | i = tabsize - (j % tabsize); |
| 5728 | j += i; |
| 5729 | while (i--) |
| 5730 | *q++ = ' '; |
| 5731 | } |
| 5732 | } |
| 5733 | else { |
| 5734 | j++; |
| 5735 | *q++ = *p; |
| 5736 | if (*p == '\n' || *p == '\r') |
| 5737 | j = 0; |
| 5738 | } |
| 5739 | |
| 5740 | return (PyObject*) u; |
| 5741 | } |
| 5742 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5743 | PyDoc_STRVAR(find__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5744 | "S.find(sub [,start [,end]]) -> int\n\ |
| 5745 | \n\ |
| 5746 | Return the lowest index in S where substring sub is found,\n\ |
| 5747 | such that sub is contained within s[start,end]. Optional\n\ |
| 5748 | arguments start and end are interpreted as in slice notation.\n\ |
| 5749 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5750 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5751 | |
| 5752 | static PyObject * |
| 5753 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 5754 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5755 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5756 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5757 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5758 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5759 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5760 | if (!PyArg_ParseTuple(args, "O|O&O&:find", &substring, |
| 5761 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5762 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5763 | substring = PyUnicode_FromObject(substring); |
| 5764 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5765 | return NULL; |
| 5766 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5767 | result = stringlib_find_slice( |
| 5768 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 5769 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 5770 | start, end |
| 5771 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5772 | |
| 5773 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5774 | |
| 5775 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5776 | } |
| 5777 | |
| 5778 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5779 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5780 | { |
| 5781 | if (index < 0 || index >= self->length) { |
| 5782 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 5783 | return NULL; |
| 5784 | } |
| 5785 | |
| 5786 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 5787 | } |
| 5788 | |
| 5789 | static long |
| 5790 | unicode_hash(PyUnicodeObject *self) |
| 5791 | { |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5792 | /* Since Unicode objects compare equal to their ASCII string |
| 5793 | counterparts, they should use the individual character values |
| 5794 | as basis for their hash value. This is needed to assure that |
| 5795 | strings and Unicode objects behave in the same way as |
| 5796 | dictionary keys. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5797 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5798 | register Py_ssize_t len; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5799 | register Py_UNICODE *p; |
| 5800 | register long x; |
| 5801 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5802 | if (self->hash != -1) |
| 5803 | return self->hash; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5804 | len = PyUnicode_GET_SIZE(self); |
| 5805 | p = PyUnicode_AS_UNICODE(self); |
| 5806 | x = *p << 7; |
| 5807 | while (--len >= 0) |
| 5808 | x = (1000003*x) ^ *p++; |
| 5809 | x ^= PyUnicode_GET_SIZE(self); |
| 5810 | if (x == -1) |
| 5811 | x = -2; |
| 5812 | self->hash = x; |
| 5813 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5814 | } |
| 5815 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5816 | PyDoc_STRVAR(index__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5817 | "S.index(sub [,start [,end]]) -> int\n\ |
| 5818 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5819 | 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] | 5820 | |
| 5821 | static PyObject * |
| 5822 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 5823 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5824 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5825 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5826 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5827 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5828 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5829 | if (!PyArg_ParseTuple(args, "O|O&O&:index", &substring, |
| 5830 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5831 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5832 | substring = PyUnicode_FromObject(substring); |
| 5833 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5834 | return NULL; |
| 5835 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5836 | result = stringlib_find_slice( |
| 5837 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 5838 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 5839 | start, end |
| 5840 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5841 | |
| 5842 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5843 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5844 | if (result < 0) { |
| 5845 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 5846 | return NULL; |
| 5847 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5848 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5849 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5850 | } |
| 5851 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5852 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5853 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5854 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5855 | 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] | 5856 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5857 | |
| 5858 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5859 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5860 | { |
| 5861 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5862 | register const Py_UNICODE *e; |
| 5863 | int cased; |
| 5864 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5865 | /* Shortcut for single character strings */ |
| 5866 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5867 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5868 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5869 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5870 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5871 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5872 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5873 | e = p + PyUnicode_GET_SIZE(self); |
| 5874 | cased = 0; |
| 5875 | for (; p < e; p++) { |
| 5876 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5877 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5878 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5879 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5880 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 5881 | cased = 1; |
| 5882 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5883 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5884 | } |
| 5885 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5886 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5887 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5888 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5889 | 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] | 5890 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5891 | |
| 5892 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5893 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5894 | { |
| 5895 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5896 | register const Py_UNICODE *e; |
| 5897 | int cased; |
| 5898 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5899 | /* Shortcut for single character strings */ |
| 5900 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5901 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5902 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5903 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5904 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5905 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5906 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5907 | e = p + PyUnicode_GET_SIZE(self); |
| 5908 | cased = 0; |
| 5909 | for (; p < e; p++) { |
| 5910 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5911 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5912 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5913 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5914 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 5915 | cased = 1; |
| 5916 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5917 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5918 | } |
| 5919 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5920 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5921 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5922 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5923 | Return True if S is a titlecased string and there is at least one\n\ |
| 5924 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 5925 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 5926 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5927 | |
| 5928 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5929 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5930 | { |
| 5931 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5932 | register const Py_UNICODE *e; |
| 5933 | int cased, previous_is_cased; |
| 5934 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5935 | /* Shortcut for single character strings */ |
| 5936 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5937 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 5938 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5939 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5940 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5941 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5942 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5943 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5944 | e = p + PyUnicode_GET_SIZE(self); |
| 5945 | cased = 0; |
| 5946 | previous_is_cased = 0; |
| 5947 | for (; p < e; p++) { |
| 5948 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5949 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5950 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 5951 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5952 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5953 | previous_is_cased = 1; |
| 5954 | cased = 1; |
| 5955 | } |
| 5956 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 5957 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5958 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5959 | previous_is_cased = 1; |
| 5960 | cased = 1; |
| 5961 | } |
| 5962 | else |
| 5963 | previous_is_cased = 0; |
| 5964 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5965 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5966 | } |
| 5967 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5968 | PyDoc_STRVAR(isspace__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5969 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5970 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5971 | Return True if all characters in S are whitespace\n\ |
| 5972 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5973 | |
| 5974 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5975 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5976 | { |
| 5977 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5978 | register const Py_UNICODE *e; |
| 5979 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5980 | /* Shortcut for single character strings */ |
| 5981 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5982 | Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5983 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5984 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5985 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5986 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5987 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5988 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5989 | e = p + PyUnicode_GET_SIZE(self); |
| 5990 | for (; p < e; p++) { |
| 5991 | if (!Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5992 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5993 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5994 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5995 | } |
| 5996 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5997 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5998 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5999 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6000 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6001 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6002 | |
| 6003 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6004 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6005 | { |
| 6006 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6007 | register const Py_UNICODE *e; |
| 6008 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6009 | /* Shortcut for single character strings */ |
| 6010 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6011 | Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6012 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6013 | |
| 6014 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6015 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6016 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6017 | |
| 6018 | e = p + PyUnicode_GET_SIZE(self); |
| 6019 | for (; p < e; p++) { |
| 6020 | if (!Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6021 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6022 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6023 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6024 | } |
| 6025 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6026 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6027 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6028 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6029 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6030 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6031 | |
| 6032 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6033 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6034 | { |
| 6035 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6036 | register const Py_UNICODE *e; |
| 6037 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6038 | /* Shortcut for single character strings */ |
| 6039 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6040 | Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6041 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6042 | |
| 6043 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6044 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6045 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6046 | |
| 6047 | e = p + PyUnicode_GET_SIZE(self); |
| 6048 | for (; p < e; p++) { |
| 6049 | if (!Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6050 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6051 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6052 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6053 | } |
| 6054 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6055 | PyDoc_STRVAR(isdecimal__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6056 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6057 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6058 | 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] | 6059 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6060 | |
| 6061 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6062 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6063 | { |
| 6064 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6065 | register const Py_UNICODE *e; |
| 6066 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6067 | /* Shortcut for single character strings */ |
| 6068 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6069 | Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6070 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6071 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6072 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6073 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6074 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6075 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6076 | e = p + PyUnicode_GET_SIZE(self); |
| 6077 | for (; p < e; p++) { |
| 6078 | if (!Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6079 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6080 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6081 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6082 | } |
| 6083 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6084 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6085 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6086 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6087 | Return True if all characters in S are digits\n\ |
| 6088 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6089 | |
| 6090 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6091 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6092 | { |
| 6093 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6094 | register const Py_UNICODE *e; |
| 6095 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6096 | /* Shortcut for single character strings */ |
| 6097 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6098 | Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6099 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6100 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6101 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6102 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6103 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6104 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6105 | e = p + PyUnicode_GET_SIZE(self); |
| 6106 | for (; p < e; p++) { |
| 6107 | if (!Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6108 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6109 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6110 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6111 | } |
| 6112 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6113 | PyDoc_STRVAR(isnumeric__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6114 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6115 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6116 | 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] | 6117 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6118 | |
| 6119 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6120 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6121 | { |
| 6122 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6123 | register const Py_UNICODE *e; |
| 6124 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6125 | /* Shortcut for single character strings */ |
| 6126 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6127 | Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6128 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6129 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6130 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6131 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6132 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6133 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6134 | e = p + PyUnicode_GET_SIZE(self); |
| 6135 | for (; p < e; p++) { |
| 6136 | if (!Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6137 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6138 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6139 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6140 | } |
| 6141 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6142 | PyDoc_STRVAR(join__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6143 | "S.join(sequence) -> unicode\n\ |
| 6144 | \n\ |
| 6145 | 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] | 6146 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6147 | |
| 6148 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6149 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6150 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6151 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6152 | } |
| 6153 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6154 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6155 | unicode_length(PyUnicodeObject *self) |
| 6156 | { |
| 6157 | return self->length; |
| 6158 | } |
| 6159 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6160 | PyDoc_STRVAR(ljust__doc__, |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 6161 | "S.ljust(width[, fillchar]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6162 | \n\ |
| 6163 | 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] | 6164 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6165 | |
| 6166 | static PyObject * |
| 6167 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 6168 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6169 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6170 | Py_UNICODE fillchar = ' '; |
| 6171 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6172 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6173 | return NULL; |
| 6174 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6175 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6176 | Py_INCREF(self); |
| 6177 | return (PyObject*) self; |
| 6178 | } |
| 6179 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6180 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6181 | } |
| 6182 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6183 | PyDoc_STRVAR(lower__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6184 | "S.lower() -> unicode\n\ |
| 6185 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6186 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6187 | |
| 6188 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6189 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6190 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6191 | return fixup(self, fixlower); |
| 6192 | } |
| 6193 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6194 | #define LEFTSTRIP 0 |
| 6195 | #define RIGHTSTRIP 1 |
| 6196 | #define BOTHSTRIP 2 |
| 6197 | |
| 6198 | /* Arrays indexed by above */ |
| 6199 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 6200 | |
| 6201 | #define STRIPNAME(i) (stripformat[i]+3) |
| 6202 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6203 | /* externally visible for str.strip(unicode) */ |
| 6204 | PyObject * |
| 6205 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 6206 | { |
| 6207 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6208 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6209 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6210 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 6211 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6212 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6213 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
| 6214 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6215 | i = 0; |
| 6216 | if (striptype != RIGHTSTRIP) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6217 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 6218 | i++; |
| 6219 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6220 | } |
| 6221 | |
| 6222 | j = len; |
| 6223 | if (striptype != LEFTSTRIP) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6224 | do { |
| 6225 | j--; |
| 6226 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 6227 | j++; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6228 | } |
| 6229 | |
| 6230 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6231 | Py_INCREF(self); |
| 6232 | return (PyObject*)self; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6233 | } |
| 6234 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6235 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6236 | } |
| 6237 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6238 | |
| 6239 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6240 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6241 | { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6242 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6243 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6244 | |
| 6245 | i = 0; |
| 6246 | if (striptype != RIGHTSTRIP) { |
| 6247 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 6248 | i++; |
| 6249 | } |
| 6250 | } |
| 6251 | |
| 6252 | j = len; |
| 6253 | if (striptype != LEFTSTRIP) { |
| 6254 | do { |
| 6255 | j--; |
| 6256 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 6257 | j++; |
| 6258 | } |
| 6259 | |
| 6260 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 6261 | Py_INCREF(self); |
| 6262 | return (PyObject*)self; |
| 6263 | } |
| 6264 | else |
| 6265 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6266 | } |
| 6267 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6268 | |
| 6269 | static PyObject * |
| 6270 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 6271 | { |
| 6272 | PyObject *sep = NULL; |
| 6273 | |
| 6274 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 6275 | return NULL; |
| 6276 | |
| 6277 | if (sep != NULL && sep != Py_None) { |
| 6278 | if (PyUnicode_Check(sep)) |
| 6279 | return _PyUnicode_XStrip(self, striptype, sep); |
| 6280 | else if (PyString_Check(sep)) { |
| 6281 | PyObject *res; |
| 6282 | sep = PyUnicode_FromObject(sep); |
| 6283 | if (sep==NULL) |
| 6284 | return NULL; |
| 6285 | res = _PyUnicode_XStrip(self, striptype, sep); |
| 6286 | Py_DECREF(sep); |
| 6287 | return res; |
| 6288 | } |
| 6289 | else { |
| 6290 | PyErr_Format(PyExc_TypeError, |
| 6291 | "%s arg must be None, unicode or str", |
| 6292 | STRIPNAME(striptype)); |
| 6293 | return NULL; |
| 6294 | } |
| 6295 | } |
| 6296 | |
| 6297 | return do_strip(self, striptype); |
| 6298 | } |
| 6299 | |
| 6300 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6301 | PyDoc_STRVAR(strip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6302 | "S.strip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6303 | \n\ |
| 6304 | Return a copy of the string S with leading and trailing\n\ |
| 6305 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6306 | If chars is given and not None, remove characters in chars instead.\n\ |
| 6307 | 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] | 6308 | |
| 6309 | static PyObject * |
| 6310 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 6311 | { |
| 6312 | if (PyTuple_GET_SIZE(args) == 0) |
| 6313 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 6314 | else |
| 6315 | return do_argstrip(self, BOTHSTRIP, args); |
| 6316 | } |
| 6317 | |
| 6318 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6319 | PyDoc_STRVAR(lstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6320 | "S.lstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6321 | \n\ |
| 6322 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6323 | If chars is given and not None, remove characters in chars instead.\n\ |
| 6324 | 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] | 6325 | |
| 6326 | static PyObject * |
| 6327 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 6328 | { |
| 6329 | if (PyTuple_GET_SIZE(args) == 0) |
| 6330 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 6331 | else |
| 6332 | return do_argstrip(self, LEFTSTRIP, args); |
| 6333 | } |
| 6334 | |
| 6335 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6336 | PyDoc_STRVAR(rstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6337 | "S.rstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6338 | \n\ |
| 6339 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6340 | If chars is given and not None, remove characters in chars instead.\n\ |
| 6341 | 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] | 6342 | |
| 6343 | static PyObject * |
| 6344 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 6345 | { |
| 6346 | if (PyTuple_GET_SIZE(args) == 0) |
| 6347 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 6348 | else |
| 6349 | return do_argstrip(self, RIGHTSTRIP, args); |
| 6350 | } |
| 6351 | |
| 6352 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6353 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6354 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6355 | { |
| 6356 | PyUnicodeObject *u; |
| 6357 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6358 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 6359 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6360 | |
| 6361 | if (len < 0) |
| 6362 | len = 0; |
| 6363 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6364 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6365 | /* no repeat, return original string */ |
| 6366 | Py_INCREF(str); |
| 6367 | return (PyObject*) str; |
| 6368 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 6369 | |
| 6370 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 6371 | * needed doesn't overflow size_t |
| 6372 | */ |
| 6373 | nchars = len * str->length; |
| 6374 | if (len && nchars / len != str->length) { |
| 6375 | PyErr_SetString(PyExc_OverflowError, |
| 6376 | "repeated string is too long"); |
| 6377 | return NULL; |
| 6378 | } |
| 6379 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 6380 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 6381 | PyErr_SetString(PyExc_OverflowError, |
| 6382 | "repeated string is too long"); |
| 6383 | return NULL; |
| 6384 | } |
| 6385 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6386 | if (!u) |
| 6387 | return NULL; |
| 6388 | |
| 6389 | p = u->str; |
| 6390 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6391 | if (str->length == 1 && len > 0) { |
| 6392 | Py_UNICODE_FILL(p, str->str[0], len); |
| 6393 | } else { |
| 6394 | Py_ssize_t done = 0; /* number of characters copied this far */ |
| 6395 | if (done < nchars) { |
| 6396 | Py_UNICODE_COPY(p, str->str, str->length); |
| 6397 | done = str->length; |
| 6398 | } |
| 6399 | while (done < nchars) { |
| 6400 | int n = (done <= nchars-done) ? done : nchars-done; |
| 6401 | Py_UNICODE_COPY(p+done, p, n); |
| 6402 | done += n; |
| 6403 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6404 | } |
| 6405 | |
| 6406 | return (PyObject*) u; |
| 6407 | } |
| 6408 | |
| 6409 | PyObject *PyUnicode_Replace(PyObject *obj, |
| 6410 | PyObject *subobj, |
| 6411 | PyObject *replobj, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6412 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6413 | { |
| 6414 | PyObject *self; |
| 6415 | PyObject *str1; |
| 6416 | PyObject *str2; |
| 6417 | PyObject *result; |
| 6418 | |
| 6419 | self = PyUnicode_FromObject(obj); |
| 6420 | if (self == NULL) |
| 6421 | return NULL; |
| 6422 | str1 = PyUnicode_FromObject(subobj); |
| 6423 | if (str1 == NULL) { |
| 6424 | Py_DECREF(self); |
| 6425 | return NULL; |
| 6426 | } |
| 6427 | str2 = PyUnicode_FromObject(replobj); |
| 6428 | if (str2 == NULL) { |
| 6429 | Py_DECREF(self); |
| 6430 | Py_DECREF(str1); |
| 6431 | return NULL; |
| 6432 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6433 | result = replace((PyUnicodeObject *)self, |
| 6434 | (PyUnicodeObject *)str1, |
| 6435 | (PyUnicodeObject *)str2, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6436 | maxcount); |
| 6437 | Py_DECREF(self); |
| 6438 | Py_DECREF(str1); |
| 6439 | Py_DECREF(str2); |
| 6440 | return result; |
| 6441 | } |
| 6442 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6443 | PyDoc_STRVAR(replace__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6444 | "S.replace (old, new[, maxsplit]) -> unicode\n\ |
| 6445 | \n\ |
| 6446 | Return a copy of S with all occurrences of substring\n\ |
| 6447 | 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] | 6448 | given, only the first maxsplit occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6449 | |
| 6450 | static PyObject* |
| 6451 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 6452 | { |
| 6453 | PyUnicodeObject *str1; |
| 6454 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6455 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6456 | PyObject *result; |
| 6457 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6458 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6459 | return NULL; |
| 6460 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 6461 | if (str1 == NULL) |
| 6462 | return NULL; |
| 6463 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 6464 | if (str2 == NULL) { |
| 6465 | Py_DECREF(str1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6466 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 6467 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6468 | |
| 6469 | result = replace(self, str1, str2, maxcount); |
| 6470 | |
| 6471 | Py_DECREF(str1); |
| 6472 | Py_DECREF(str2); |
| 6473 | return result; |
| 6474 | } |
| 6475 | |
| 6476 | static |
| 6477 | PyObject *unicode_repr(PyObject *unicode) |
| 6478 | { |
| 6479 | return unicodeescape_string(PyUnicode_AS_UNICODE(unicode), |
| 6480 | PyUnicode_GET_SIZE(unicode), |
| 6481 | 1); |
| 6482 | } |
| 6483 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6484 | PyDoc_STRVAR(rfind__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6485 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 6486 | \n\ |
| 6487 | Return the highest index in S where substring sub is found,\n\ |
| 6488 | such that sub is contained within s[start,end]. Optional\n\ |
| 6489 | arguments start and end are interpreted as in slice notation.\n\ |
| 6490 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6491 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6492 | |
| 6493 | static PyObject * |
| 6494 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 6495 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6496 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6497 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6498 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6499 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6500 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6501 | if (!PyArg_ParseTuple(args, "O|O&O&:rfind", &substring, |
| 6502 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6503 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6504 | substring = PyUnicode_FromObject(substring); |
| 6505 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6506 | return NULL; |
| 6507 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6508 | result = stringlib_rfind_slice( |
| 6509 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6510 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6511 | start, end |
| 6512 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6513 | |
| 6514 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6515 | |
| 6516 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6517 | } |
| 6518 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6519 | PyDoc_STRVAR(rindex__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6520 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 6521 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6522 | 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] | 6523 | |
| 6524 | static PyObject * |
| 6525 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 6526 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6527 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6528 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6529 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6530 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6531 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6532 | if (!PyArg_ParseTuple(args, "O|O&O&:rindex", &substring, |
| 6533 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6534 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6535 | substring = PyUnicode_FromObject(substring); |
| 6536 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6537 | return NULL; |
| 6538 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6539 | result = stringlib_rfind_slice( |
| 6540 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6541 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6542 | start, end |
| 6543 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6544 | |
| 6545 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6546 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6547 | if (result < 0) { |
| 6548 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 6549 | return NULL; |
| 6550 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6551 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6552 | } |
| 6553 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6554 | PyDoc_STRVAR(rjust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6555 | "S.rjust(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6556 | \n\ |
| 6557 | 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] | 6558 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6559 | |
| 6560 | static PyObject * |
| 6561 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 6562 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6563 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6564 | Py_UNICODE fillchar = ' '; |
| 6565 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6566 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6567 | return NULL; |
| 6568 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6569 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6570 | Py_INCREF(self); |
| 6571 | return (PyObject*) self; |
| 6572 | } |
| 6573 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6574 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6575 | } |
| 6576 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6577 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6578 | 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] | 6579 | { |
| 6580 | /* standard clamping */ |
| 6581 | if (start < 0) |
| 6582 | start = 0; |
| 6583 | if (end < 0) |
| 6584 | end = 0; |
| 6585 | if (end > self->length) |
| 6586 | end = self->length; |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6587 | if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6588 | /* full slice, return original string */ |
| 6589 | Py_INCREF(self); |
| 6590 | return (PyObject*) self; |
| 6591 | } |
| 6592 | if (start > end) |
| 6593 | start = end; |
| 6594 | /* copy slice */ |
| 6595 | return (PyObject*) PyUnicode_FromUnicode(self->str + start, |
| 6596 | end - start); |
| 6597 | } |
| 6598 | |
| 6599 | PyObject *PyUnicode_Split(PyObject *s, |
| 6600 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6601 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6602 | { |
| 6603 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6604 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6605 | s = PyUnicode_FromObject(s); |
| 6606 | if (s == NULL) |
| 6607 | return NULL; |
| 6608 | if (sep != NULL) { |
| 6609 | sep = PyUnicode_FromObject(sep); |
| 6610 | if (sep == NULL) { |
| 6611 | Py_DECREF(s); |
| 6612 | return NULL; |
| 6613 | } |
| 6614 | } |
| 6615 | |
| 6616 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 6617 | |
| 6618 | Py_DECREF(s); |
| 6619 | Py_XDECREF(sep); |
| 6620 | return result; |
| 6621 | } |
| 6622 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6623 | PyDoc_STRVAR(split__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6624 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 6625 | \n\ |
| 6626 | Return a list of the words in S, using sep as the\n\ |
| 6627 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Thomas Heller | ca0d2cb | 2004-09-15 11:41:32 +0000 | [diff] [blame] | 6628 | 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] | 6629 | any whitespace string is a separator."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6630 | |
| 6631 | static PyObject* |
| 6632 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 6633 | { |
| 6634 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6635 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6636 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6637 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6638 | return NULL; |
| 6639 | |
| 6640 | if (substring == Py_None) |
| 6641 | return split(self, NULL, maxcount); |
| 6642 | else if (PyUnicode_Check(substring)) |
| 6643 | return split(self, (PyUnicodeObject *)substring, maxcount); |
| 6644 | else |
| 6645 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
| 6646 | } |
| 6647 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6648 | PyObject * |
| 6649 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 6650 | { |
| 6651 | PyObject* str_obj; |
| 6652 | PyObject* sep_obj; |
| 6653 | PyObject* out; |
| 6654 | |
| 6655 | str_obj = PyUnicode_FromObject(str_in); |
| 6656 | if (!str_obj) |
| 6657 | return NULL; |
| 6658 | sep_obj = PyUnicode_FromObject(sep_in); |
| 6659 | if (!sep_obj) { |
| 6660 | Py_DECREF(str_obj); |
| 6661 | return NULL; |
| 6662 | } |
| 6663 | |
| 6664 | out = stringlib_partition( |
| 6665 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 6666 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 6667 | ); |
| 6668 | |
| 6669 | Py_DECREF(sep_obj); |
| 6670 | Py_DECREF(str_obj); |
| 6671 | |
| 6672 | return out; |
| 6673 | } |
| 6674 | |
| 6675 | |
| 6676 | PyObject * |
| 6677 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 6678 | { |
| 6679 | PyObject* str_obj; |
| 6680 | PyObject* sep_obj; |
| 6681 | PyObject* out; |
| 6682 | |
| 6683 | str_obj = PyUnicode_FromObject(str_in); |
| 6684 | if (!str_obj) |
| 6685 | return NULL; |
| 6686 | sep_obj = PyUnicode_FromObject(sep_in); |
| 6687 | if (!sep_obj) { |
| 6688 | Py_DECREF(str_obj); |
| 6689 | return NULL; |
| 6690 | } |
| 6691 | |
| 6692 | out = stringlib_rpartition( |
| 6693 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 6694 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 6695 | ); |
| 6696 | |
| 6697 | Py_DECREF(sep_obj); |
| 6698 | Py_DECREF(str_obj); |
| 6699 | |
| 6700 | return out; |
| 6701 | } |
| 6702 | |
| 6703 | PyDoc_STRVAR(partition__doc__, |
| 6704 | "S.partition(sep) -> (head, sep, tail)\n\ |
| 6705 | \n\ |
| 6706 | Searches for the separator sep in S, and returns the part before it,\n\ |
| 6707 | the separator itself, and the part after it. If the separator is not\n\ |
| 6708 | found, returns S and two empty strings."); |
| 6709 | |
| 6710 | static PyObject* |
| 6711 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 6712 | { |
| 6713 | return PyUnicode_Partition((PyObject *)self, separator); |
| 6714 | } |
| 6715 | |
| 6716 | PyDoc_STRVAR(rpartition__doc__, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6717 | "S.rpartition(sep) -> (tail, sep, head)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6718 | \n\ |
| 6719 | Searches for the separator sep in S, starting at the end of S, and returns\n\ |
| 6720 | the part before it, the separator itself, and the part after it. If the\n\ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6721 | separator is not found, returns two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6722 | |
| 6723 | static PyObject* |
| 6724 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 6725 | { |
| 6726 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 6727 | } |
| 6728 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6729 | PyObject *PyUnicode_RSplit(PyObject *s, |
| 6730 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6731 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6732 | { |
| 6733 | PyObject *result; |
| 6734 | |
| 6735 | s = PyUnicode_FromObject(s); |
| 6736 | if (s == NULL) |
| 6737 | return NULL; |
| 6738 | if (sep != NULL) { |
| 6739 | sep = PyUnicode_FromObject(sep); |
| 6740 | if (sep == NULL) { |
| 6741 | Py_DECREF(s); |
| 6742 | return NULL; |
| 6743 | } |
| 6744 | } |
| 6745 | |
| 6746 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 6747 | |
| 6748 | Py_DECREF(s); |
| 6749 | Py_XDECREF(sep); |
| 6750 | return result; |
| 6751 | } |
| 6752 | |
| 6753 | PyDoc_STRVAR(rsplit__doc__, |
| 6754 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
| 6755 | \n\ |
| 6756 | Return a list of the words in S, using sep as the\n\ |
| 6757 | delimiter string, starting at the end of the string and\n\ |
| 6758 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 6759 | splits are done. If sep is not specified, any whitespace string\n\ |
| 6760 | is a separator."); |
| 6761 | |
| 6762 | static PyObject* |
| 6763 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 6764 | { |
| 6765 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6766 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6767 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6768 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6769 | return NULL; |
| 6770 | |
| 6771 | if (substring == Py_None) |
| 6772 | return rsplit(self, NULL, maxcount); |
| 6773 | else if (PyUnicode_Check(substring)) |
| 6774 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
| 6775 | else |
| 6776 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
| 6777 | } |
| 6778 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6779 | PyDoc_STRVAR(splitlines__doc__, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6780 | "S.splitlines([keepends]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6781 | \n\ |
| 6782 | 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] | 6783 | 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] | 6784 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6785 | |
| 6786 | static PyObject* |
| 6787 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 6788 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6789 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6790 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6791 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6792 | return NULL; |
| 6793 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6794 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6795 | } |
| 6796 | |
| 6797 | static |
| 6798 | PyObject *unicode_str(PyUnicodeObject *self) |
| 6799 | { |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6800 | return PyUnicode_AsEncodedString((PyObject *)self, NULL, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6801 | } |
| 6802 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6803 | PyDoc_STRVAR(swapcase__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6804 | "S.swapcase() -> unicode\n\ |
| 6805 | \n\ |
| 6806 | 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] | 6807 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6808 | |
| 6809 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6810 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6811 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6812 | return fixup(self, fixswapcase); |
| 6813 | } |
| 6814 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6815 | PyDoc_STRVAR(translate__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6816 | "S.translate(table) -> unicode\n\ |
| 6817 | \n\ |
| 6818 | Return a copy of the string S, where all characters have been mapped\n\ |
| 6819 | 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] | 6820 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ |
| 6821 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 6822 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6823 | |
| 6824 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6825 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6826 | { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6827 | return PyUnicode_TranslateCharmap(self->str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6828 | self->length, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6829 | table, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6830 | "ignore"); |
| 6831 | } |
| 6832 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6833 | PyDoc_STRVAR(upper__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6834 | "S.upper() -> unicode\n\ |
| 6835 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6836 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6837 | |
| 6838 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6839 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6840 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6841 | return fixup(self, fixupper); |
| 6842 | } |
| 6843 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6844 | PyDoc_STRVAR(zfill__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6845 | "S.zfill(width) -> unicode\n\ |
| 6846 | \n\ |
| 6847 | 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] | 6848 | of the specified width. The string x is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6849 | |
| 6850 | static PyObject * |
| 6851 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 6852 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6853 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6854 | PyUnicodeObject *u; |
| 6855 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6856 | Py_ssize_t width; |
| 6857 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6858 | return NULL; |
| 6859 | |
| 6860 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 6861 | if (PyUnicode_CheckExact(self)) { |
| 6862 | Py_INCREF(self); |
| 6863 | return (PyObject*) self; |
| 6864 | } |
| 6865 | else |
| 6866 | return PyUnicode_FromUnicode( |
| 6867 | PyUnicode_AS_UNICODE(self), |
| 6868 | PyUnicode_GET_SIZE(self) |
| 6869 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6870 | } |
| 6871 | |
| 6872 | fill = width - self->length; |
| 6873 | |
| 6874 | u = pad(self, fill, 0, '0'); |
| 6875 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 6876 | if (u == NULL) |
| 6877 | return NULL; |
| 6878 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6879 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 6880 | /* move sign to beginning of string */ |
| 6881 | u->str[0] = u->str[fill]; |
| 6882 | u->str[fill] = '0'; |
| 6883 | } |
| 6884 | |
| 6885 | return (PyObject*) u; |
| 6886 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6887 | |
| 6888 | #if 0 |
| 6889 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6890 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6891 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6892 | return PyInt_FromLong(unicode_freelist_size); |
| 6893 | } |
| 6894 | #endif |
| 6895 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6896 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6897 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6898 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 6899 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 6900 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6901 | With optional end, stop comparing S at that position.\n\ |
| 6902 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6903 | |
| 6904 | static PyObject * |
| 6905 | unicode_startswith(PyUnicodeObject *self, |
| 6906 | PyObject *args) |
| 6907 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6908 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6909 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6910 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6911 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6912 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6913 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6914 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6915 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6916 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6917 | if (PyTuple_Check(subobj)) { |
| 6918 | Py_ssize_t i; |
| 6919 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 6920 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6921 | PyTuple_GET_ITEM(subobj, i)); |
| 6922 | if (substring == NULL) |
| 6923 | return NULL; |
| 6924 | result = tailmatch(self, substring, start, end, -1); |
| 6925 | Py_DECREF(substring); |
| 6926 | if (result) { |
| 6927 | Py_RETURN_TRUE; |
| 6928 | } |
| 6929 | } |
| 6930 | /* nothing matched */ |
| 6931 | Py_RETURN_FALSE; |
| 6932 | } |
| 6933 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6934 | if (substring == NULL) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6935 | return NULL; |
| 6936 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6937 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6938 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6939 | } |
| 6940 | |
| 6941 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6942 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6943 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6944 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 6945 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 6946 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6947 | With optional end, stop comparing S at that position.\n\ |
| 6948 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6949 | |
| 6950 | static PyObject * |
| 6951 | unicode_endswith(PyUnicodeObject *self, |
| 6952 | PyObject *args) |
| 6953 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6954 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6955 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6956 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6957 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6958 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6959 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6960 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 6961 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6962 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6963 | if (PyTuple_Check(subobj)) { |
| 6964 | Py_ssize_t i; |
| 6965 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 6966 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6967 | PyTuple_GET_ITEM(subobj, i)); |
| 6968 | if (substring == NULL) |
| 6969 | return NULL; |
| 6970 | result = tailmatch(self, substring, start, end, +1); |
| 6971 | Py_DECREF(substring); |
| 6972 | if (result) { |
| 6973 | Py_RETURN_TRUE; |
| 6974 | } |
| 6975 | } |
| 6976 | Py_RETURN_FALSE; |
| 6977 | } |
| 6978 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6979 | if (substring == NULL) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6980 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6981 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6982 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6983 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6984 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6985 | } |
| 6986 | |
| 6987 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 6988 | |
| 6989 | static PyObject * |
| 6990 | unicode_getnewargs(PyUnicodeObject *v) |
| 6991 | { |
| 6992 | return Py_BuildValue("(u#)", v->str, v->length); |
| 6993 | } |
| 6994 | |
| 6995 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6996 | static PyMethodDef unicode_methods[] = { |
| 6997 | |
| 6998 | /* Order is according to common usage: often used methods should |
| 6999 | appear first, since lookup is done sequentially. */ |
| 7000 | |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7001 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
| 7002 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 7003 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7004 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7005 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 7006 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 7007 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 7008 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 7009 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 7010 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 7011 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7012 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7013 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 7014 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 7015 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7016 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7017 | {"decode", (PyCFunction) unicode_decode, METH_VARARGS, decode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7018 | /* {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */ |
| 7019 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 7020 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 7021 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7022 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7023 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7024 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7025 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7026 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 7027 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 7028 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 7029 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 7030 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 7031 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 7032 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 7033 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 7034 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 7035 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 7036 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 7037 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 7038 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 7039 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7040 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 7041 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7042 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7043 | #endif |
| 7044 | |
| 7045 | #if 0 |
| 7046 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7047 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7048 | #endif |
| 7049 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 7050 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7051 | {NULL, NULL} |
| 7052 | }; |
| 7053 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7054 | static PyObject * |
| 7055 | unicode_mod(PyObject *v, PyObject *w) |
| 7056 | { |
| 7057 | if (!PyUnicode_Check(v)) { |
| 7058 | Py_INCREF(Py_NotImplemented); |
| 7059 | return Py_NotImplemented; |
| 7060 | } |
| 7061 | return PyUnicode_Format(v, w); |
| 7062 | } |
| 7063 | |
| 7064 | static PyNumberMethods unicode_as_number = { |
| 7065 | 0, /*nb_add*/ |
| 7066 | 0, /*nb_subtract*/ |
| 7067 | 0, /*nb_multiply*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7068 | unicode_mod, /*nb_remainder*/ |
| 7069 | }; |
| 7070 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7071 | static PySequenceMethods unicode_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7072 | (lenfunc) unicode_length, /* sq_length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7073 | PyUnicode_Concat, /* sq_concat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7074 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 7075 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 7076 | (ssizessizeargfunc) unicode_slice, /* sq_slice */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7077 | 0, /* sq_ass_item */ |
| 7078 | 0, /* sq_ass_slice */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7079 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7080 | }; |
| 7081 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7082 | static PyObject* |
| 7083 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 7084 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7085 | if (PyIndex_Check(item)) { |
| 7086 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7087 | if (i == -1 && PyErr_Occurred()) |
| 7088 | return NULL; |
| 7089 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7090 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7091 | return unicode_getitem(self, i); |
| 7092 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7093 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7094 | Py_UNICODE* source_buf; |
| 7095 | Py_UNICODE* result_buf; |
| 7096 | PyObject* result; |
| 7097 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7098 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7099 | &start, &stop, &step, &slicelength) < 0) { |
| 7100 | return NULL; |
| 7101 | } |
| 7102 | |
| 7103 | if (slicelength <= 0) { |
| 7104 | return PyUnicode_FromUnicode(NULL, 0); |
| 7105 | } else { |
| 7106 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7107 | result_buf = (Py_UNICODE *)PyMem_MALLOC(slicelength* |
| 7108 | sizeof(Py_UNICODE)); |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7109 | |
| 7110 | if (result_buf == NULL) |
| 7111 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7112 | |
| 7113 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 7114 | result_buf[i] = source_buf[cur]; |
| 7115 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7116 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7117 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
| 7118 | PyMem_FREE(result_buf); |
| 7119 | return result; |
| 7120 | } |
| 7121 | } else { |
| 7122 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 7123 | return NULL; |
| 7124 | } |
| 7125 | } |
| 7126 | |
| 7127 | static PyMappingMethods unicode_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7128 | (lenfunc)unicode_length, /* mp_length */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7129 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 7130 | (objobjargproc)0, /* mp_ass_subscript */ |
| 7131 | }; |
| 7132 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7133 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7134 | unicode_buffer_getreadbuf(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7135 | Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7136 | const void **ptr) |
| 7137 | { |
| 7138 | if (index != 0) { |
| 7139 | PyErr_SetString(PyExc_SystemError, |
| 7140 | "accessing non-existent unicode segment"); |
| 7141 | return -1; |
| 7142 | } |
| 7143 | *ptr = (void *) self->str; |
| 7144 | return PyUnicode_GET_DATA_SIZE(self); |
| 7145 | } |
| 7146 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7147 | static Py_ssize_t |
| 7148 | unicode_buffer_getwritebuf(PyUnicodeObject *self, Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7149 | const void **ptr) |
| 7150 | { |
| 7151 | PyErr_SetString(PyExc_TypeError, |
Neal Norwitz | 20e7213 | 2002-06-13 21:25:17 +0000 | [diff] [blame] | 7152 | "cannot use unicode as modifiable buffer"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7153 | return -1; |
| 7154 | } |
| 7155 | |
| 7156 | static int |
| 7157 | unicode_buffer_getsegcount(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7158 | Py_ssize_t *lenp) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7159 | { |
| 7160 | if (lenp) |
| 7161 | *lenp = PyUnicode_GET_DATA_SIZE(self); |
| 7162 | return 1; |
| 7163 | } |
| 7164 | |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 7165 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7166 | unicode_buffer_getcharbuf(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7167 | Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7168 | const void **ptr) |
| 7169 | { |
| 7170 | PyObject *str; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7171 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7172 | if (index != 0) { |
| 7173 | PyErr_SetString(PyExc_SystemError, |
| 7174 | "accessing non-existent unicode segment"); |
| 7175 | return -1; |
| 7176 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 7177 | str = _PyUnicode_AsDefaultEncodedString((PyObject *)self, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7178 | if (str == NULL) |
| 7179 | return -1; |
| 7180 | *ptr = (void *) PyString_AS_STRING(str); |
| 7181 | return PyString_GET_SIZE(str); |
| 7182 | } |
| 7183 | |
| 7184 | /* Helpers for PyUnicode_Format() */ |
| 7185 | |
| 7186 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7187 | 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] | 7188 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7189 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7190 | if (argidx < arglen) { |
| 7191 | (*p_argidx)++; |
| 7192 | if (arglen < 0) |
| 7193 | return args; |
| 7194 | else |
| 7195 | return PyTuple_GetItem(args, argidx); |
| 7196 | } |
| 7197 | PyErr_SetString(PyExc_TypeError, |
| 7198 | "not enough arguments for format string"); |
| 7199 | return NULL; |
| 7200 | } |
| 7201 | |
| 7202 | #define F_LJUST (1<<0) |
| 7203 | #define F_SIGN (1<<1) |
| 7204 | #define F_BLANK (1<<2) |
| 7205 | #define F_ALT (1<<3) |
| 7206 | #define F_ZERO (1<<4) |
| 7207 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7208 | static Py_ssize_t |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7209 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7210 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7211 | register Py_ssize_t i; |
| 7212 | Py_ssize_t len = strlen(charbuffer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7213 | for (i = len - 1; i >= 0; i--) |
| 7214 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
| 7215 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7216 | return len; |
| 7217 | } |
| 7218 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7219 | static int |
| 7220 | doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) |
| 7221 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 7222 | Py_ssize_t result; |
| 7223 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7224 | PyOS_ascii_formatd((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 7225 | result = strtounicode(buffer, (char *)buffer); |
| 7226 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7227 | } |
| 7228 | |
| 7229 | static int |
| 7230 | longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) |
| 7231 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 7232 | Py_ssize_t result; |
| 7233 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7234 | PyOS_snprintf((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 7235 | result = strtounicode(buffer, (char *)buffer); |
| 7236 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7237 | } |
| 7238 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 7239 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 7240 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 7241 | formatting is done. */ |
| 7242 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7243 | static int |
| 7244 | formatfloat(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7245 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7246 | int flags, |
| 7247 | int prec, |
| 7248 | int type, |
| 7249 | PyObject *v) |
| 7250 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7251 | /* fmt = '%#.' + `prec` + `type` |
| 7252 | 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] | 7253 | char fmt[20]; |
| 7254 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7255 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7256 | x = PyFloat_AsDouble(v); |
| 7257 | if (x == -1.0 && PyErr_Occurred()) |
| 7258 | return -1; |
| 7259 | if (prec < 0) |
| 7260 | prec = 6; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7261 | if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
| 7262 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7263 | /* Worst case length calc to ensure no buffer overrun: |
| 7264 | |
| 7265 | 'g' formats: |
| 7266 | fmt = %#.<prec>g |
| 7267 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 7268 | for any double rep.) |
| 7269 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 7270 | |
| 7271 | 'f' formats: |
| 7272 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 7273 | len = 1 + 50 + 1 + prec = 52 + prec |
| 7274 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7275 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7276 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7277 | |
| 7278 | */ |
| 7279 | if ((type == 'g' && buflen <= (size_t)10 + (size_t)prec) || |
| 7280 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7281 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7282 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7283 | return -1; |
| 7284 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7285 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 7286 | (flags&F_ALT) ? "#" : "", |
| 7287 | prec, type); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7288 | return doubletounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7289 | } |
| 7290 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7291 | static PyObject* |
| 7292 | formatlong(PyObject *val, int flags, int prec, int type) |
| 7293 | { |
| 7294 | char *buf; |
| 7295 | int i, len; |
| 7296 | PyObject *str; /* temporary string object. */ |
| 7297 | PyUnicodeObject *result; |
| 7298 | |
| 7299 | str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
| 7300 | if (!str) |
| 7301 | return NULL; |
| 7302 | result = _PyUnicode_New(len); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 7303 | if (!result) { |
| 7304 | Py_DECREF(str); |
| 7305 | return NULL; |
| 7306 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7307 | for (i = 0; i < len; i++) |
| 7308 | result->str[i] = buf[i]; |
| 7309 | result->str[len] = 0; |
| 7310 | Py_DECREF(str); |
| 7311 | return (PyObject*)result; |
| 7312 | } |
| 7313 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7314 | static int |
| 7315 | formatint(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7316 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7317 | int flags, |
| 7318 | int prec, |
| 7319 | int type, |
| 7320 | PyObject *v) |
| 7321 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7322 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7323 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 7324 | * + 1 + 1 |
| 7325 | * = 24 |
| 7326 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7327 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7328 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7329 | long x; |
| 7330 | |
| 7331 | x = PyInt_AsLong(v); |
| 7332 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7333 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7334 | if (x < 0 && type == 'u') { |
| 7335 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 7336 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7337 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 7338 | sign = "-"; |
| 7339 | else |
| 7340 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7341 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7342 | prec = 1; |
| 7343 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7344 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 7345 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7346 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7347 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7348 | PyErr_SetString(PyExc_OverflowError, |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7349 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7350 | return -1; |
| 7351 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7352 | |
| 7353 | if ((flags & F_ALT) && |
| 7354 | (type == 'x' || type == 'X')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7355 | /* When converting under %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7356 | * of issues that cause pain: |
| 7357 | * - when 0 is being converted, the C standard leaves off |
| 7358 | * the '0x' or '0X', which is inconsistent with other |
| 7359 | * %#x/%#X conversions and inconsistent with Python's |
| 7360 | * hex() function |
| 7361 | * - there are platforms that violate the standard and |
| 7362 | * convert 0 with the '0x' or '0X' |
| 7363 | * (Metrowerks, Compaq Tru64) |
| 7364 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7365 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7366 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7367 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7368 | * We can achieve the desired consistency by inserting our |
| 7369 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 7370 | * of %#x/%#X. |
| 7371 | * |
| 7372 | * Note that this is the same approach as used in |
| 7373 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 7374 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7375 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 7376 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 7377 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7378 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7379 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 7380 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7381 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 7382 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7383 | if (sign[0]) |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7384 | return longtounicode(buf, buflen, fmt, -x); |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7385 | else |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7386 | return longtounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7387 | } |
| 7388 | |
| 7389 | static int |
| 7390 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7391 | size_t buflen, |
| 7392 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7393 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7394 | /* presume that the buffer is at least 2 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7395 | if (PyUnicode_Check(v)) { |
| 7396 | if (PyUnicode_GET_SIZE(v) != 1) |
| 7397 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7398 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7399 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7400 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7401 | else if (PyString_Check(v)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7402 | if (PyString_GET_SIZE(v) != 1) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7403 | goto onError; |
| 7404 | buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0]; |
| 7405 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7406 | |
| 7407 | else { |
| 7408 | /* Integer input truncated to a character */ |
| 7409 | long x; |
| 7410 | x = PyInt_AsLong(v); |
| 7411 | if (x == -1 && PyErr_Occurred()) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7412 | goto onError; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 7413 | #ifdef Py_UNICODE_WIDE |
| 7414 | if (x < 0 || x > 0x10ffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 7415 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 7416 | "%c arg not in range(0x110000) " |
| 7417 | "(wide Python build)"); |
| 7418 | return -1; |
| 7419 | } |
| 7420 | #else |
| 7421 | if (x < 0 || x > 0xffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 7422 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 7423 | "%c arg not in range(0x10000) " |
| 7424 | "(narrow Python build)"); |
| 7425 | return -1; |
| 7426 | } |
| 7427 | #endif |
| 7428 | buf[0] = (Py_UNICODE) x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7429 | } |
| 7430 | buf[1] = '\0'; |
| 7431 | return 1; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7432 | |
| 7433 | onError: |
| 7434 | PyErr_SetString(PyExc_TypeError, |
| 7435 | "%c requires int or char"); |
| 7436 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7437 | } |
| 7438 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7439 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 7440 | |
| 7441 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 7442 | chars are formatted. XXX This is a magic number. Each formatting |
| 7443 | routine does bounds checking to ensure no overflow, but a better |
| 7444 | solution may be to malloc a buffer of appropriate size for each |
| 7445 | format. For now, the current solution is sufficient. |
| 7446 | */ |
| 7447 | #define FORMATBUFLEN (size_t)120 |
| 7448 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7449 | PyObject *PyUnicode_Format(PyObject *format, |
| 7450 | PyObject *args) |
| 7451 | { |
| 7452 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7453 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7454 | int args_owned = 0; |
| 7455 | PyUnicodeObject *result = NULL; |
| 7456 | PyObject *dict = NULL; |
| 7457 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7458 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7459 | if (format == NULL || args == NULL) { |
| 7460 | PyErr_BadInternalCall(); |
| 7461 | return NULL; |
| 7462 | } |
| 7463 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7464 | if (uformat == NULL) |
| 7465 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7466 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 7467 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 7468 | |
| 7469 | reslen = rescnt = fmtcnt + 100; |
| 7470 | result = _PyUnicode_New(reslen); |
| 7471 | if (result == NULL) |
| 7472 | goto onError; |
| 7473 | res = PyUnicode_AS_UNICODE(result); |
| 7474 | |
| 7475 | if (PyTuple_Check(args)) { |
| 7476 | arglen = PyTuple_Size(args); |
| 7477 | argidx = 0; |
| 7478 | } |
| 7479 | else { |
| 7480 | arglen = -1; |
| 7481 | argidx = -2; |
| 7482 | } |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 7483 | if (args->ob_type->tp_as_mapping && !PyTuple_Check(args) && |
| 7484 | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7485 | dict = args; |
| 7486 | |
| 7487 | while (--fmtcnt >= 0) { |
| 7488 | if (*fmt != '%') { |
| 7489 | if (--rescnt < 0) { |
| 7490 | rescnt = fmtcnt + 100; |
| 7491 | reslen += rescnt; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7492 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 7493 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7494 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 7495 | --rescnt; |
| 7496 | } |
| 7497 | *res++ = *fmt++; |
| 7498 | } |
| 7499 | else { |
| 7500 | /* Got a format specifier */ |
| 7501 | int flags = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7502 | Py_ssize_t width = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7503 | int prec = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7504 | Py_UNICODE c = '\0'; |
| 7505 | Py_UNICODE fill; |
| 7506 | PyObject *v = NULL; |
| 7507 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7508 | Py_UNICODE *pbuf; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7509 | Py_UNICODE sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7510 | Py_ssize_t len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7511 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7512 | |
| 7513 | fmt++; |
| 7514 | if (*fmt == '(') { |
| 7515 | Py_UNICODE *keystart; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7516 | Py_ssize_t keylen; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7517 | PyObject *key; |
| 7518 | int pcount = 1; |
| 7519 | |
| 7520 | if (dict == NULL) { |
| 7521 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7522 | "format requires a mapping"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7523 | goto onError; |
| 7524 | } |
| 7525 | ++fmt; |
| 7526 | --fmtcnt; |
| 7527 | keystart = fmt; |
| 7528 | /* Skip over balanced parentheses */ |
| 7529 | while (pcount > 0 && --fmtcnt >= 0) { |
| 7530 | if (*fmt == ')') |
| 7531 | --pcount; |
| 7532 | else if (*fmt == '(') |
| 7533 | ++pcount; |
| 7534 | fmt++; |
| 7535 | } |
| 7536 | keylen = fmt - keystart - 1; |
| 7537 | if (fmtcnt < 0 || pcount > 0) { |
| 7538 | PyErr_SetString(PyExc_ValueError, |
| 7539 | "incomplete format key"); |
| 7540 | goto onError; |
| 7541 | } |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 7542 | #if 0 |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7543 | /* keys are converted to strings using UTF-8 and |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7544 | then looked up since Python uses strings to hold |
| 7545 | variables names etc. in its namespaces and we |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7546 | wouldn't want to break common idioms. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7547 | key = PyUnicode_EncodeUTF8(keystart, |
| 7548 | keylen, |
| 7549 | NULL); |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 7550 | #else |
| 7551 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 7552 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7553 | if (key == NULL) |
| 7554 | goto onError; |
| 7555 | if (args_owned) { |
| 7556 | Py_DECREF(args); |
| 7557 | args_owned = 0; |
| 7558 | } |
| 7559 | args = PyObject_GetItem(dict, key); |
| 7560 | Py_DECREF(key); |
| 7561 | if (args == NULL) { |
| 7562 | goto onError; |
| 7563 | } |
| 7564 | args_owned = 1; |
| 7565 | arglen = -1; |
| 7566 | argidx = -2; |
| 7567 | } |
| 7568 | while (--fmtcnt >= 0) { |
| 7569 | switch (c = *fmt++) { |
| 7570 | case '-': flags |= F_LJUST; continue; |
| 7571 | case '+': flags |= F_SIGN; continue; |
| 7572 | case ' ': flags |= F_BLANK; continue; |
| 7573 | case '#': flags |= F_ALT; continue; |
| 7574 | case '0': flags |= F_ZERO; continue; |
| 7575 | } |
| 7576 | break; |
| 7577 | } |
| 7578 | if (c == '*') { |
| 7579 | v = getnextarg(args, arglen, &argidx); |
| 7580 | if (v == NULL) |
| 7581 | goto onError; |
| 7582 | if (!PyInt_Check(v)) { |
| 7583 | PyErr_SetString(PyExc_TypeError, |
| 7584 | "* wants int"); |
| 7585 | goto onError; |
| 7586 | } |
| 7587 | width = PyInt_AsLong(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 7588 | if (width == -1 && PyErr_Occurred()) |
| 7589 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7590 | if (width < 0) { |
| 7591 | flags |= F_LJUST; |
| 7592 | width = -width; |
| 7593 | } |
| 7594 | if (--fmtcnt >= 0) |
| 7595 | c = *fmt++; |
| 7596 | } |
| 7597 | else if (c >= '0' && c <= '9') { |
| 7598 | width = c - '0'; |
| 7599 | while (--fmtcnt >= 0) { |
| 7600 | c = *fmt++; |
| 7601 | if (c < '0' || c > '9') |
| 7602 | break; |
| 7603 | if ((width*10) / 10 != width) { |
| 7604 | PyErr_SetString(PyExc_ValueError, |
| 7605 | "width too big"); |
| 7606 | goto onError; |
| 7607 | } |
| 7608 | width = width*10 + (c - '0'); |
| 7609 | } |
| 7610 | } |
| 7611 | if (c == '.') { |
| 7612 | prec = 0; |
| 7613 | if (--fmtcnt >= 0) |
| 7614 | c = *fmt++; |
| 7615 | if (c == '*') { |
| 7616 | v = getnextarg(args, arglen, &argidx); |
| 7617 | if (v == NULL) |
| 7618 | goto onError; |
| 7619 | if (!PyInt_Check(v)) { |
| 7620 | PyErr_SetString(PyExc_TypeError, |
| 7621 | "* wants int"); |
| 7622 | goto onError; |
| 7623 | } |
| 7624 | prec = PyInt_AsLong(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 7625 | if (prec == -1 && PyErr_Occurred()) |
| 7626 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7627 | if (prec < 0) |
| 7628 | prec = 0; |
| 7629 | if (--fmtcnt >= 0) |
| 7630 | c = *fmt++; |
| 7631 | } |
| 7632 | else if (c >= '0' && c <= '9') { |
| 7633 | prec = c - '0'; |
| 7634 | while (--fmtcnt >= 0) { |
| 7635 | c = Py_CHARMASK(*fmt++); |
| 7636 | if (c < '0' || c > '9') |
| 7637 | break; |
| 7638 | if ((prec*10) / 10 != prec) { |
| 7639 | PyErr_SetString(PyExc_ValueError, |
| 7640 | "prec too big"); |
| 7641 | goto onError; |
| 7642 | } |
| 7643 | prec = prec*10 + (c - '0'); |
| 7644 | } |
| 7645 | } |
| 7646 | } /* prec */ |
| 7647 | if (fmtcnt >= 0) { |
| 7648 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7649 | if (--fmtcnt >= 0) |
| 7650 | c = *fmt++; |
| 7651 | } |
| 7652 | } |
| 7653 | if (fmtcnt < 0) { |
| 7654 | PyErr_SetString(PyExc_ValueError, |
| 7655 | "incomplete format"); |
| 7656 | goto onError; |
| 7657 | } |
| 7658 | if (c != '%') { |
| 7659 | v = getnextarg(args, arglen, &argidx); |
| 7660 | if (v == NULL) |
| 7661 | goto onError; |
| 7662 | } |
| 7663 | sign = 0; |
| 7664 | fill = ' '; |
| 7665 | switch (c) { |
| 7666 | |
| 7667 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7668 | pbuf = formatbuf; |
| 7669 | /* presume that buffer length is at least 1 */ |
| 7670 | pbuf[0] = '%'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7671 | len = 1; |
| 7672 | break; |
| 7673 | |
| 7674 | case 's': |
| 7675 | case 'r': |
| 7676 | if (PyUnicode_Check(v) && c == 's') { |
| 7677 | temp = v; |
| 7678 | Py_INCREF(temp); |
| 7679 | } |
| 7680 | else { |
| 7681 | PyObject *unicode; |
| 7682 | if (c == 's') |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 7683 | temp = PyObject_Unicode(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7684 | else |
| 7685 | temp = PyObject_Repr(v); |
| 7686 | if (temp == NULL) |
| 7687 | goto onError; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 7688 | if (PyUnicode_Check(temp)) |
| 7689 | /* nothing to do */; |
| 7690 | else if (PyString_Check(temp)) { |
| 7691 | /* convert to string to Unicode */ |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7692 | unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7693 | PyString_GET_SIZE(temp), |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7694 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7695 | "strict"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7696 | Py_DECREF(temp); |
| 7697 | temp = unicode; |
| 7698 | if (temp == NULL) |
| 7699 | goto onError; |
| 7700 | } |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 7701 | else { |
| 7702 | Py_DECREF(temp); |
| 7703 | PyErr_SetString(PyExc_TypeError, |
| 7704 | "%s argument has non-string str()"); |
| 7705 | goto onError; |
| 7706 | } |
| 7707 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7708 | pbuf = PyUnicode_AS_UNICODE(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7709 | len = PyUnicode_GET_SIZE(temp); |
| 7710 | if (prec >= 0 && len > prec) |
| 7711 | len = prec; |
| 7712 | break; |
| 7713 | |
| 7714 | case 'i': |
| 7715 | case 'd': |
| 7716 | case 'u': |
| 7717 | case 'o': |
| 7718 | case 'x': |
| 7719 | case 'X': |
| 7720 | if (c == 'i') |
| 7721 | c = 'd'; |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 7722 | if (PyLong_Check(v)) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7723 | temp = formatlong(v, flags, prec, c); |
| 7724 | if (!temp) |
| 7725 | goto onError; |
| 7726 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 7727 | len = PyUnicode_GET_SIZE(temp); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7728 | sign = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7729 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7730 | else { |
| 7731 | pbuf = formatbuf; |
| 7732 | len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 7733 | flags, prec, c, v); |
| 7734 | if (len < 0) |
| 7735 | goto onError; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7736 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7737 | } |
| 7738 | if (flags & F_ZERO) |
| 7739 | fill = '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7740 | break; |
| 7741 | |
| 7742 | case 'e': |
| 7743 | case 'E': |
| 7744 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 7745 | case 'F': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7746 | case 'g': |
| 7747 | case 'G': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 7748 | if (c == 'F') |
| 7749 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7750 | pbuf = formatbuf; |
| 7751 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 7752 | flags, prec, c, v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7753 | if (len < 0) |
| 7754 | goto onError; |
| 7755 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7756 | if (flags & F_ZERO) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7757 | fill = '0'; |
| 7758 | break; |
| 7759 | |
| 7760 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7761 | pbuf = formatbuf; |
| 7762 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7763 | if (len < 0) |
| 7764 | goto onError; |
| 7765 | break; |
| 7766 | |
| 7767 | default: |
| 7768 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 7769 | "unsupported format character '%c' (0x%x) " |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7770 | "at index %zd", |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7771 | (31<=c && c<=126) ? (char)c : '?', |
Marc-André Lemburg | 24e53b6 | 2002-09-24 09:32:14 +0000 | [diff] [blame] | 7772 | (int)c, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7773 | (Py_ssize_t)(fmt - 1 - |
| 7774 | PyUnicode_AS_UNICODE(uformat))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7775 | goto onError; |
| 7776 | } |
| 7777 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7778 | if (*pbuf == '-' || *pbuf == '+') { |
| 7779 | sign = *pbuf++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7780 | len--; |
| 7781 | } |
| 7782 | else if (flags & F_SIGN) |
| 7783 | sign = '+'; |
| 7784 | else if (flags & F_BLANK) |
| 7785 | sign = ' '; |
| 7786 | else |
| 7787 | sign = 0; |
| 7788 | } |
| 7789 | if (width < len) |
| 7790 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 7791 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7792 | reslen -= rescnt; |
| 7793 | rescnt = width + fmtcnt + 100; |
| 7794 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 7795 | if (reslen < 0) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 7796 | Py_XDECREF(temp); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7797 | PyErr_NoMemory(); |
| 7798 | goto onError; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 7799 | } |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7800 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 7801 | Py_XDECREF(temp); |
| 7802 | goto onError; |
| 7803 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7804 | res = PyUnicode_AS_UNICODE(result) |
| 7805 | + reslen - rescnt; |
| 7806 | } |
| 7807 | if (sign) { |
| 7808 | if (fill != ' ') |
| 7809 | *res++ = sign; |
| 7810 | rescnt--; |
| 7811 | if (width > len) |
| 7812 | width--; |
| 7813 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7814 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 7815 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7816 | assert(pbuf[1] == c); |
| 7817 | if (fill != ' ') { |
| 7818 | *res++ = *pbuf++; |
| 7819 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7820 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7821 | rescnt -= 2; |
| 7822 | width -= 2; |
| 7823 | if (width < 0) |
| 7824 | width = 0; |
| 7825 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7826 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7827 | if (width > len && !(flags & F_LJUST)) { |
| 7828 | do { |
| 7829 | --rescnt; |
| 7830 | *res++ = fill; |
| 7831 | } while (--width > len); |
| 7832 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7833 | if (fill == ' ') { |
| 7834 | if (sign) |
| 7835 | *res++ = sign; |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7836 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7837 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7838 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7839 | *res++ = *pbuf++; |
| 7840 | *res++ = *pbuf++; |
| 7841 | } |
| 7842 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7843 | Py_UNICODE_COPY(res, pbuf, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7844 | res += len; |
| 7845 | rescnt -= len; |
| 7846 | while (--width >= len) { |
| 7847 | --rescnt; |
| 7848 | *res++ = ' '; |
| 7849 | } |
| 7850 | if (dict && (argidx < arglen) && c != '%') { |
| 7851 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 7852 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7853 | Py_XDECREF(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7854 | goto onError; |
| 7855 | } |
| 7856 | Py_XDECREF(temp); |
| 7857 | } /* '%' */ |
| 7858 | } /* until end */ |
| 7859 | if (argidx < arglen && !dict) { |
| 7860 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 7861 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7862 | goto onError; |
| 7863 | } |
| 7864 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7865 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
| 7866 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7867 | if (args_owned) { |
| 7868 | Py_DECREF(args); |
| 7869 | } |
| 7870 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7871 | return (PyObject *)result; |
| 7872 | |
| 7873 | onError: |
| 7874 | Py_XDECREF(result); |
| 7875 | Py_DECREF(uformat); |
| 7876 | if (args_owned) { |
| 7877 | Py_DECREF(args); |
| 7878 | } |
| 7879 | return NULL; |
| 7880 | } |
| 7881 | |
| 7882 | static PyBufferProcs unicode_as_buffer = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7883 | (readbufferproc) unicode_buffer_getreadbuf, |
| 7884 | (writebufferproc) unicode_buffer_getwritebuf, |
| 7885 | (segcountproc) unicode_buffer_getsegcount, |
| 7886 | (charbufferproc) unicode_buffer_getcharbuf, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7887 | }; |
| 7888 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 7889 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7890 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 7891 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7892 | static PyObject * |
| 7893 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 7894 | { |
| 7895 | PyObject *x = NULL; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 7896 | static char *kwlist[] = {"string", "encoding", "errors", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7897 | char *encoding = NULL; |
| 7898 | char *errors = NULL; |
| 7899 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7900 | if (type != &PyUnicode_Type) |
| 7901 | return unicode_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7902 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
| 7903 | kwlist, &x, &encoding, &errors)) |
| 7904 | return NULL; |
| 7905 | if (x == NULL) |
| 7906 | return (PyObject *)_PyUnicode_New(0); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 7907 | if (encoding == NULL && errors == NULL) |
| 7908 | return PyObject_Unicode(x); |
| 7909 | else |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7910 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
| 7911 | } |
| 7912 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7913 | static PyObject * |
| 7914 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 7915 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7916 | PyUnicodeObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7917 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7918 | |
| 7919 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 7920 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 7921 | if (tmp == NULL) |
| 7922 | return NULL; |
| 7923 | assert(PyUnicode_Check(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7924 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7925 | if (pnew == NULL) { |
| 7926 | Py_DECREF(tmp); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7927 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7928 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7929 | pnew->str = PyMem_NEW(Py_UNICODE, n+1); |
| 7930 | if (pnew->str == NULL) { |
| 7931 | _Py_ForgetReference((PyObject *)pnew); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 7932 | PyObject_Del(pnew); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7933 | Py_DECREF(tmp); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 7934 | return PyErr_NoMemory(); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7935 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7936 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 7937 | pnew->length = n; |
| 7938 | pnew->hash = tmp->hash; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7939 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7940 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7941 | } |
| 7942 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7943 | PyDoc_STRVAR(unicode_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7944 | "unicode(string [, encoding[, errors]]) -> object\n\ |
| 7945 | \n\ |
| 7946 | Create a new Unicode object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 7947 | encoding defaults to the current default string encoding.\n\ |
| 7948 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7949 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 7950 | static PyObject *unicode_iter(PyObject *seq); |
| 7951 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7952 | PyTypeObject PyUnicode_Type = { |
| 7953 | PyObject_HEAD_INIT(&PyType_Type) |
| 7954 | 0, /* ob_size */ |
| 7955 | "unicode", /* tp_name */ |
| 7956 | sizeof(PyUnicodeObject), /* tp_size */ |
| 7957 | 0, /* tp_itemsize */ |
| 7958 | /* Slots */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 7959 | (destructor)unicode_dealloc, /* tp_dealloc */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7960 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7961 | 0, /* tp_getattr */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7962 | 0, /* tp_setattr */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7963 | 0, /* tp_compare */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7964 | unicode_repr, /* tp_repr */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7965 | &unicode_as_number, /* tp_as_number */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7966 | &unicode_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7967 | &unicode_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7968 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 7969 | 0, /* tp_call*/ |
| 7970 | (reprfunc) unicode_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7971 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 7972 | 0, /* tp_setattro */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7973 | &unicode_as_buffer, /* tp_as_buffer */ |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame^] | 7974 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 7975 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7976 | unicode_doc, /* tp_doc */ |
| 7977 | 0, /* tp_traverse */ |
| 7978 | 0, /* tp_clear */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7979 | PyUnicode_RichCompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7980 | 0, /* tp_weaklistoffset */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 7981 | unicode_iter, /* tp_iter */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7982 | 0, /* tp_iternext */ |
| 7983 | unicode_methods, /* tp_methods */ |
| 7984 | 0, /* tp_members */ |
| 7985 | 0, /* tp_getset */ |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 7986 | &PyBaseString_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7987 | 0, /* tp_dict */ |
| 7988 | 0, /* tp_descr_get */ |
| 7989 | 0, /* tp_descr_set */ |
| 7990 | 0, /* tp_dictoffset */ |
| 7991 | 0, /* tp_init */ |
| 7992 | 0, /* tp_alloc */ |
| 7993 | unicode_new, /* tp_new */ |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 7994 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7995 | }; |
| 7996 | |
| 7997 | /* Initialize the Unicode implementation */ |
| 7998 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 7999 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8000 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8001 | int i; |
| 8002 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8003 | /* XXX - move this array to unicodectype.c ? */ |
| 8004 | Py_UNICODE linebreak[] = { |
| 8005 | 0x000A, /* LINE FEED */ |
| 8006 | 0x000D, /* CARRIAGE RETURN */ |
| 8007 | 0x001C, /* FILE SEPARATOR */ |
| 8008 | 0x001D, /* GROUP SEPARATOR */ |
| 8009 | 0x001E, /* RECORD SEPARATOR */ |
| 8010 | 0x0085, /* NEXT LINE */ |
| 8011 | 0x2028, /* LINE SEPARATOR */ |
| 8012 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 8013 | }; |
| 8014 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8015 | /* Init the implementation */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8016 | unicode_freelist = NULL; |
| 8017 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8018 | unicode_empty = _PyUnicode_New(0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8019 | if (!unicode_empty) |
| 8020 | return; |
| 8021 | |
Marc-André Lemburg | 90e8147 | 2000-06-07 09:13:21 +0000 | [diff] [blame] | 8022 | strcpy(unicode_default_encoding, "ascii"); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8023 | for (i = 0; i < 256; i++) |
| 8024 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 8025 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 8026 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8027 | |
| 8028 | /* initialize the linebreak bloom filter */ |
| 8029 | bloom_linebreak = make_bloom_mask( |
| 8030 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 8031 | ); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8032 | |
| 8033 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8034 | } |
| 8035 | |
| 8036 | /* Finalize the Unicode implementation */ |
| 8037 | |
| 8038 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 8039 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8040 | { |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8041 | PyUnicodeObject *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8042 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8043 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 8044 | Py_XDECREF(unicode_empty); |
| 8045 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8046 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8047 | for (i = 0; i < 256; i++) { |
| 8048 | if (unicode_latin1[i]) { |
| 8049 | Py_DECREF(unicode_latin1[i]); |
| 8050 | unicode_latin1[i] = NULL; |
| 8051 | } |
| 8052 | } |
| 8053 | |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8054 | for (u = unicode_freelist; u != NULL;) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8055 | PyUnicodeObject *v = u; |
| 8056 | u = *(PyUnicodeObject **)u; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 8057 | if (v->str) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 8058 | PyMem_DEL(v->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 8059 | Py_XDECREF(v->defenc); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8060 | PyObject_Del(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8061 | } |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8062 | unicode_freelist = NULL; |
| 8063 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8064 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 8065 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8066 | |
| 8067 | |
| 8068 | /********************* Unicode Iterator **************************/ |
| 8069 | |
| 8070 | typedef struct { |
| 8071 | PyObject_HEAD |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 8072 | Py_ssize_t it_index; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8073 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
| 8074 | } unicodeiterobject; |
| 8075 | |
| 8076 | static void |
| 8077 | unicodeiter_dealloc(unicodeiterobject *it) |
| 8078 | { |
| 8079 | _PyObject_GC_UNTRACK(it); |
| 8080 | Py_XDECREF(it->it_seq); |
| 8081 | PyObject_GC_Del(it); |
| 8082 | } |
| 8083 | |
| 8084 | static int |
| 8085 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 8086 | { |
| 8087 | Py_VISIT(it->it_seq); |
| 8088 | return 0; |
| 8089 | } |
| 8090 | |
| 8091 | static PyObject * |
| 8092 | unicodeiter_next(unicodeiterobject *it) |
| 8093 | { |
| 8094 | PyUnicodeObject *seq; |
| 8095 | PyObject *item; |
| 8096 | |
| 8097 | assert(it != NULL); |
| 8098 | seq = it->it_seq; |
| 8099 | if (seq == NULL) |
| 8100 | return NULL; |
| 8101 | assert(PyUnicode_Check(seq)); |
| 8102 | |
| 8103 | if (it->it_index < PyUnicode_GET_SIZE(seq)) { |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 8104 | item = PyUnicode_FromUnicode( |
| 8105 | PyUnicode_AS_UNICODE(seq)+it->it_index, 1); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8106 | if (item != NULL) |
| 8107 | ++it->it_index; |
| 8108 | return item; |
| 8109 | } |
| 8110 | |
| 8111 | Py_DECREF(seq); |
| 8112 | it->it_seq = NULL; |
| 8113 | return NULL; |
| 8114 | } |
| 8115 | |
| 8116 | static PyObject * |
| 8117 | unicodeiter_len(unicodeiterobject *it) |
| 8118 | { |
| 8119 | Py_ssize_t len = 0; |
| 8120 | if (it->it_seq) |
| 8121 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 8122 | return PyInt_FromSsize_t(len); |
| 8123 | } |
| 8124 | |
| 8125 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 8126 | |
| 8127 | static PyMethodDef unicodeiter_methods[] = { |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 8128 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
| 8129 | length_hint_doc}, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8130 | {NULL, NULL} /* sentinel */ |
| 8131 | }; |
| 8132 | |
| 8133 | PyTypeObject PyUnicodeIter_Type = { |
| 8134 | PyObject_HEAD_INIT(&PyType_Type) |
| 8135 | 0, /* ob_size */ |
| 8136 | "unicodeiterator", /* tp_name */ |
| 8137 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 8138 | 0, /* tp_itemsize */ |
| 8139 | /* methods */ |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 8140 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8141 | 0, /* tp_print */ |
| 8142 | 0, /* tp_getattr */ |
| 8143 | 0, /* tp_setattr */ |
| 8144 | 0, /* tp_compare */ |
| 8145 | 0, /* tp_repr */ |
| 8146 | 0, /* tp_as_number */ |
| 8147 | 0, /* tp_as_sequence */ |
| 8148 | 0, /* tp_as_mapping */ |
| 8149 | 0, /* tp_hash */ |
| 8150 | 0, /* tp_call */ |
| 8151 | 0, /* tp_str */ |
| 8152 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 8153 | 0, /* tp_setattro */ |
| 8154 | 0, /* tp_as_buffer */ |
| 8155 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 8156 | 0, /* tp_doc */ |
| 8157 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 8158 | 0, /* tp_clear */ |
| 8159 | 0, /* tp_richcompare */ |
| 8160 | 0, /* tp_weaklistoffset */ |
| 8161 | PyObject_SelfIter, /* tp_iter */ |
| 8162 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 8163 | unicodeiter_methods, /* tp_methods */ |
| 8164 | 0, |
| 8165 | }; |
| 8166 | |
| 8167 | static PyObject * |
| 8168 | unicode_iter(PyObject *seq) |
| 8169 | { |
| 8170 | unicodeiterobject *it; |
| 8171 | |
| 8172 | if (!PyUnicode_Check(seq)) { |
| 8173 | PyErr_BadInternalCall(); |
| 8174 | return NULL; |
| 8175 | } |
| 8176 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 8177 | if (it == NULL) |
| 8178 | return NULL; |
| 8179 | it->it_index = 0; |
| 8180 | Py_INCREF(seq); |
| 8181 | it->it_seq = (PyUnicodeObject *)seq; |
| 8182 | _PyObject_GC_TRACK(it); |
| 8183 | return (PyObject *)it; |
| 8184 | } |
| 8185 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8186 | #ifdef __cplusplus |
| 8187 | } |
| 8188 | #endif |
| 8189 | |
| 8190 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 8191 | /* |
| 8192 | Local variables: |
| 8193 | c-basic-offset: 4 |
| 8194 | indent-tabs-mode: nil |
| 8195 | End: |
| 8196 | */ |