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 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 48 | #include "formatter_unicode.h" |
| 49 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 50 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 51 | #include <windows.h> |
| 52 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 54 | /* Limit for the Unicode object free list */ |
| 55 | |
| 56 | #define MAX_UNICODE_FREELIST_SIZE 1024 |
| 57 | |
| 58 | /* Limit for the Unicode object free list stay alive optimization. |
| 59 | |
| 60 | The implementation will keep allocated Unicode memory intact for |
| 61 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 62 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 63 | |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 64 | At worst this will result in MAX_UNICODE_FREELIST_SIZE * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 65 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 66 | malloc()-overhead) bytes of unused garbage. |
| 67 | |
| 68 | Setting the limit to 0 effectively turns the feature off. |
| 69 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 70 | Note: This is an experimental feature ! If you get core dumps when |
| 71 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 72 | |
| 73 | */ |
| 74 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 75 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 76 | |
| 77 | /* Endianness switches; defaults to little endian */ |
| 78 | |
| 79 | #ifdef WORDS_BIGENDIAN |
| 80 | # define BYTEORDER_IS_BIG_ENDIAN |
| 81 | #else |
| 82 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 83 | #endif |
| 84 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 85 | /* --- Globals ------------------------------------------------------------ |
| 86 | |
| 87 | The globals are initialized by the _PyUnicode_Init() API and should |
| 88 | not be used before calling that API. |
| 89 | |
| 90 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 91 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 92 | |
| 93 | #ifdef __cplusplus |
| 94 | extern "C" { |
| 95 | #endif |
| 96 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 97 | /* This dictionary holds all interned unicode strings. Note that references |
| 98 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 99 | When the interned string reaches a refcnt of 0 the string deallocation |
| 100 | function will delete the reference from this dictionary. |
| 101 | |
| 102 | Another way to look at this is that to say that the actual reference |
| 103 | count of a string is: s->ob_refcnt + (s->ob_sstate?2:0) |
| 104 | */ |
| 105 | static PyObject *interned; |
| 106 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 107 | /* Free list for Unicode objects */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 108 | static PyUnicodeObject *unicode_freelist; |
| 109 | static int unicode_freelist_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 110 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 111 | /* The empty Unicode object is shared to improve performance. */ |
| 112 | static PyUnicodeObject *unicode_empty; |
| 113 | |
| 114 | /* Single character Unicode strings in the Latin-1 range are being |
| 115 | shared as well. */ |
| 116 | static PyUnicodeObject *unicode_latin1[256]; |
| 117 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 118 | /* Default encoding to use and assume when NULL is passed as encoding |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 119 | parameter; it is fixed to "utf-8". Always use the |
| 120 | PyUnicode_GetDefaultEncoding() API to access this global. */ |
| 121 | static const char unicode_default_encoding[] = "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 122 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 123 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 124 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 125 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 126 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 127 | return 0x10FFFF; |
| 128 | #else |
| 129 | /* This is actually an illegal character, so it should |
| 130 | not be passed to unichr. */ |
| 131 | return 0xFFFF; |
| 132 | #endif |
| 133 | } |
| 134 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 135 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 136 | |
| 137 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 138 | to keep things simple, we use a single bitmask, using the least 5 |
| 139 | bits from each unicode characters as the bit index. */ |
| 140 | |
| 141 | /* the linebreak mask is set up by Unicode_Init below */ |
| 142 | |
| 143 | #define BLOOM_MASK unsigned long |
| 144 | |
| 145 | static BLOOM_MASK bloom_linebreak; |
| 146 | |
| 147 | #define BLOOM(mask, ch) ((mask & (1 << ((ch) & 0x1F)))) |
| 148 | |
| 149 | #define BLOOM_LINEBREAK(ch)\ |
| 150 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK((ch))) |
| 151 | |
| 152 | Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len) |
| 153 | { |
| 154 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 155 | |
| 156 | long mask; |
| 157 | Py_ssize_t i; |
| 158 | |
| 159 | mask = 0; |
| 160 | for (i = 0; i < len; i++) |
| 161 | mask |= (1 << (ptr[i] & 0x1F)); |
| 162 | |
| 163 | return mask; |
| 164 | } |
| 165 | |
| 166 | Py_LOCAL_INLINE(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen) |
| 167 | { |
| 168 | Py_ssize_t i; |
| 169 | |
| 170 | for (i = 0; i < setlen; i++) |
| 171 | if (set[i] == chr) |
| 172 | return 1; |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | #define BLOOM_MEMBER(mask, chr, set, setlen)\ |
| 178 | BLOOM(mask, chr) && unicode_member(chr, set, setlen) |
| 179 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 180 | /* --- Unicode Object ----------------------------------------------------- */ |
| 181 | |
| 182 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 183 | int unicode_resize(register PyUnicodeObject *unicode, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 184 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 185 | { |
| 186 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 187 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 188 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 189 | if (unicode->length == length) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 190 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 191 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 192 | /* Resizing shared object (unicode_empty or single character |
| 193 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 194 | instead ! */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 195 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 196 | if (unicode == unicode_empty || |
| 197 | (unicode->length == 1 && |
| 198 | unicode->str[0] < 256U && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 199 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 200 | PyErr_SetString(PyExc_SystemError, |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 201 | "can't resize shared unicode objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 202 | return -1; |
| 203 | } |
| 204 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 205 | /* We allocate one more byte to make sure the string is Ux0000 terminated. |
| 206 | The overallocation is also used by fastsearch, which assumes that it's |
| 207 | safe to look at str[length] (without making any assumptions about what |
| 208 | it contains). */ |
| 209 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 210 | oldstr = unicode->str; |
| 211 | PyMem_RESIZE(unicode->str, Py_UNICODE, length + 1); |
| 212 | if (!unicode->str) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 213 | unicode->str = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 214 | PyErr_NoMemory(); |
| 215 | return -1; |
| 216 | } |
| 217 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 218 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 219 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 220 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 221 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 222 | if (unicode->defenc) { |
| 223 | Py_DECREF(unicode->defenc); |
| 224 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 225 | } |
| 226 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 227 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | /* We allocate one more byte to make sure the string is |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 232 | Ux0000 terminated; some code (e.g. new_identifier) |
| 233 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 234 | |
| 235 | XXX This allocator could further be enhanced by assuring that the |
| 236 | free list never reduces its size below 1. |
| 237 | |
| 238 | */ |
| 239 | |
| 240 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 241 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 242 | { |
| 243 | register PyUnicodeObject *unicode; |
| 244 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 245 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 246 | if (length == 0 && unicode_empty != NULL) { |
| 247 | Py_INCREF(unicode_empty); |
| 248 | return unicode_empty; |
| 249 | } |
| 250 | |
| 251 | /* Unicode freelist & memory allocation */ |
| 252 | if (unicode_freelist) { |
| 253 | unicode = unicode_freelist; |
Marc-André Lemburg | bea47e7 | 2000-06-17 20:31:17 +0000 | [diff] [blame] | 254 | unicode_freelist = *(PyUnicodeObject **)unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 255 | unicode_freelist_size--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 256 | if (unicode->str) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 257 | /* Keep-Alive optimization: we only upsize the buffer, |
| 258 | never downsize it. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 259 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 260 | unicode_resize(unicode, length) < 0) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 261 | PyMem_DEL(unicode->str); |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 262 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 263 | } |
| 264 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 265 | else { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 266 | unicode->str = PyMem_NEW(Py_UNICODE, length + 1); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 267 | } |
| 268 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 269 | } |
| 270 | else { |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 271 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 272 | if (unicode == NULL) |
| 273 | return NULL; |
| 274 | unicode->str = PyMem_NEW(Py_UNICODE, length + 1); |
| 275 | } |
| 276 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 277 | if (!unicode->str) { |
| 278 | PyErr_NoMemory(); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 279 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 280 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 281 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 282 | * the caller fails before initializing str -- unicode_resize() |
| 283 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 284 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 285 | * We don't want unicode_resize to read uninitialized memory in |
| 286 | * that case. |
| 287 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 288 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 289 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 290 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 291 | unicode->hash = -1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 292 | unicode->state = 0; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 293 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 294 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 295 | |
| 296 | onError: |
| 297 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 298 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 299 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 303 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 304 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 305 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
| 306 | case SSTATE_NOT_INTERNED: |
| 307 | break; |
| 308 | |
| 309 | case SSTATE_INTERNED_MORTAL: |
| 310 | /* revive dead object temporarily for DelItem */ |
Martin v. Löwis | 5d7428b | 2007-07-21 18:47:48 +0000 | [diff] [blame] | 311 | Py_Refcnt(unicode) = 3; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 312 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 313 | Py_FatalError( |
| 314 | "deletion of interned unicode string failed"); |
| 315 | break; |
| 316 | |
| 317 | case SSTATE_INTERNED_IMMORTAL: |
| 318 | Py_FatalError("Immortal interned unicode string died."); |
| 319 | |
| 320 | default: |
| 321 | Py_FatalError("Inconsistent interned unicode string state."); |
| 322 | } |
| 323 | |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 324 | if (PyUnicode_CheckExact(unicode) && |
| 325 | unicode_freelist_size < MAX_UNICODE_FREELIST_SIZE) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 326 | /* Keep-Alive optimization */ |
| 327 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 328 | PyMem_DEL(unicode->str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 329 | unicode->str = NULL; |
| 330 | unicode->length = 0; |
| 331 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 332 | if (unicode->defenc) { |
| 333 | Py_DECREF(unicode->defenc); |
| 334 | unicode->defenc = NULL; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 335 | } |
| 336 | /* Add to free list */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 337 | *(PyUnicodeObject **)unicode = unicode_freelist; |
| 338 | unicode_freelist = unicode; |
| 339 | unicode_freelist_size++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 340 | } |
| 341 | else { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 342 | PyMem_DEL(unicode->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 343 | Py_XDECREF(unicode->defenc); |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 344 | Py_Type(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 348 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 349 | { |
| 350 | register PyUnicodeObject *v; |
| 351 | |
| 352 | /* Argument checks */ |
| 353 | if (unicode == NULL) { |
| 354 | PyErr_BadInternalCall(); |
| 355 | return -1; |
| 356 | } |
| 357 | v = (PyUnicodeObject *)*unicode; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 358 | if (v == NULL || !PyUnicode_Check(v) || Py_Refcnt(v) != 1 || length < 0) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 359 | PyErr_BadInternalCall(); |
| 360 | return -1; |
| 361 | } |
| 362 | |
| 363 | /* Resizing unicode_empty and single character objects is not |
| 364 | possible since these are being shared. We simply return a fresh |
| 365 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 366 | if (v->length != length && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 367 | (v == unicode_empty || v->length == 1)) { |
| 368 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 369 | if (w == NULL) |
| 370 | return -1; |
| 371 | Py_UNICODE_COPY(w->str, v->str, |
| 372 | length < v->length ? length : v->length); |
Raymond Hettinger | c8df578 | 2003-03-09 07:30:43 +0000 | [diff] [blame] | 373 | Py_DECREF(*unicode); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 374 | *unicode = (PyObject *)w; |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 379 | objects, since we can modify them in-place. */ |
| 380 | return unicode_resize(v, length); |
| 381 | } |
| 382 | |
| 383 | /* Internal API for use in unicodeobject.c only ! */ |
| 384 | #define _PyUnicode_Resize(unicodevar, length) \ |
| 385 | PyUnicode_Resize(((PyObject **)(unicodevar)), length) |
| 386 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 387 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 388 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 389 | { |
| 390 | PyUnicodeObject *unicode; |
| 391 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 392 | /* If the Unicode data is known at construction time, we can apply |
| 393 | some optimizations which share commonly used objects. */ |
| 394 | if (u != NULL) { |
| 395 | |
| 396 | /* Optimization for empty strings */ |
| 397 | if (size == 0 && unicode_empty != NULL) { |
| 398 | Py_INCREF(unicode_empty); |
| 399 | return (PyObject *)unicode_empty; |
| 400 | } |
| 401 | |
| 402 | /* Single character Unicode objects in the Latin-1 range are |
| 403 | shared when using this constructor */ |
| 404 | if (size == 1 && *u < 256) { |
| 405 | unicode = unicode_latin1[*u]; |
| 406 | if (!unicode) { |
| 407 | unicode = _PyUnicode_New(1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 408 | if (!unicode) |
| 409 | return NULL; |
Marc-André Lemburg | 8879a33 | 2001-06-07 12:26:56 +0000 | [diff] [blame] | 410 | unicode->str[0] = *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 411 | unicode_latin1[*u] = unicode; |
| 412 | } |
| 413 | Py_INCREF(unicode); |
| 414 | return (PyObject *)unicode; |
| 415 | } |
| 416 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 417 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 418 | unicode = _PyUnicode_New(size); |
| 419 | if (!unicode) |
| 420 | return NULL; |
| 421 | |
| 422 | /* Copy the Unicode data into the new object */ |
| 423 | if (u != NULL) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 424 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 425 | |
| 426 | return (PyObject *)unicode; |
| 427 | } |
| 428 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 429 | PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 430 | { |
| 431 | PyUnicodeObject *unicode; |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 432 | /* If the Unicode data is known at construction time, we can apply |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 433 | some optimizations which share commonly used objects. |
| 434 | Also, this means the input must be UTF-8, so fall back to the |
| 435 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 436 | if (u != NULL) { |
| 437 | |
| 438 | /* Optimization for empty strings */ |
| 439 | if (size == 0 && unicode_empty != NULL) { |
| 440 | Py_INCREF(unicode_empty); |
| 441 | return (PyObject *)unicode_empty; |
| 442 | } |
| 443 | |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 444 | /* Single characters are shared when using this constructor. |
| 445 | Restrict to ASCII, since the input must be UTF-8. */ |
| 446 | if (size == 1 && Py_CHARMASK(*u) < 128) { |
Guido van Rossum | 00058aa | 2007-07-19 18:21:28 +0000 | [diff] [blame] | 447 | unicode = unicode_latin1[Py_CHARMASK(*u)]; |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 448 | if (!unicode) { |
| 449 | unicode = _PyUnicode_New(1); |
| 450 | if (!unicode) |
| 451 | return NULL; |
Guido van Rossum | 00058aa | 2007-07-19 18:21:28 +0000 | [diff] [blame] | 452 | unicode->str[0] = Py_CHARMASK(*u); |
| 453 | unicode_latin1[Py_CHARMASK(*u)] = unicode; |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 454 | } |
| 455 | Py_INCREF(unicode); |
| 456 | return (PyObject *)unicode; |
| 457 | } |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 458 | |
| 459 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 462 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 463 | if (!unicode) |
| 464 | return NULL; |
| 465 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 466 | return (PyObject *)unicode; |
| 467 | } |
| 468 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 469 | PyObject *PyUnicode_FromString(const char *u) |
| 470 | { |
| 471 | size_t size = strlen(u); |
| 472 | if (size > PY_SSIZE_T_MAX) { |
| 473 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 474 | return NULL; |
| 475 | } |
| 476 | |
| 477 | return PyUnicode_FromStringAndSize(u, size); |
| 478 | } |
| 479 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 480 | #ifdef HAVE_WCHAR_H |
| 481 | |
| 482 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 483 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 484 | { |
| 485 | PyUnicodeObject *unicode; |
| 486 | |
| 487 | if (w == NULL) { |
| 488 | PyErr_BadInternalCall(); |
| 489 | return NULL; |
| 490 | } |
| 491 | |
| 492 | unicode = _PyUnicode_New(size); |
| 493 | if (!unicode) |
| 494 | return NULL; |
| 495 | |
| 496 | /* Copy the wchar_t data into the new object */ |
| 497 | #ifdef HAVE_USABLE_WCHAR_T |
| 498 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 499 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 500 | { |
| 501 | register Py_UNICODE *u; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 502 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 503 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 504 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 505 | *u++ = *w++; |
| 506 | } |
| 507 | #endif |
| 508 | |
| 509 | return (PyObject *)unicode; |
| 510 | } |
| 511 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 512 | static void |
| 513 | makefmt(char *fmt, int longflag, int size_tflag, int zeropad, int width, int precision, char c) |
| 514 | { |
| 515 | *fmt++ = '%'; |
| 516 | if (width) { |
| 517 | if (zeropad) |
| 518 | *fmt++ = '0'; |
| 519 | fmt += sprintf(fmt, "%d", width); |
| 520 | } |
| 521 | if (precision) |
| 522 | fmt += sprintf(fmt, ".%d", precision); |
| 523 | if (longflag) |
| 524 | *fmt++ = 'l'; |
| 525 | else if (size_tflag) { |
| 526 | char *f = PY_FORMAT_SIZE_T; |
| 527 | while (*f) |
| 528 | *fmt++ = *f++; |
| 529 | } |
| 530 | *fmt++ = c; |
| 531 | *fmt = '\0'; |
| 532 | } |
| 533 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 534 | #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} |
| 535 | |
| 536 | PyObject * |
| 537 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 538 | { |
| 539 | va_list count; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 540 | Py_ssize_t callcount = 0; |
| 541 | PyObject **callresults = NULL; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 542 | PyObject **callresult = NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 543 | Py_ssize_t n = 0; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 544 | int width = 0; |
| 545 | int precision = 0; |
| 546 | int zeropad; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 547 | const char* f; |
| 548 | Py_UNICODE *s; |
| 549 | PyObject *string; |
| 550 | /* used by sprintf */ |
| 551 | char buffer[21]; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 552 | /* use abuffer instead of buffer, if we need more space |
| 553 | * (which can happen if there's a format specifier with width). */ |
| 554 | char *abuffer = NULL; |
| 555 | char *realbuffer; |
| 556 | Py_ssize_t abuffersize = 0; |
| 557 | char fmt[60]; /* should be enough for %0width.precisionld */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 558 | const char *copy; |
| 559 | |
| 560 | #ifdef VA_LIST_IS_ARRAY |
| 561 | Py_MEMCPY(count, vargs, sizeof(va_list)); |
| 562 | #else |
| 563 | #ifdef __va_copy |
| 564 | __va_copy(count, vargs); |
| 565 | #else |
| 566 | count = vargs; |
| 567 | #endif |
| 568 | #endif |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 569 | /* step 1: count the number of %S/%R format specifications |
| 570 | * (we call PyObject_Unicode()/PyObject_Repr() for these objects |
| 571 | * once during step 3 and put the result in an array) */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 572 | for (f = format; *f; f++) { |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 573 | if (*f == '%' && (*(f+1)=='S' || *(f+1)=='R')) |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 574 | ++callcount; |
| 575 | } |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 576 | /* step 2: allocate memory for the results of |
| 577 | * PyObject_Unicode()/PyObject_Repr() calls */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 578 | if (callcount) { |
| 579 | callresults = PyMem_Malloc(sizeof(PyObject *)*callcount); |
| 580 | if (!callresults) { |
| 581 | PyErr_NoMemory(); |
| 582 | return NULL; |
| 583 | } |
| 584 | callresult = callresults; |
| 585 | } |
| 586 | /* step 3: figure out how large a buffer we need */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 587 | for (f = format; *f; f++) { |
| 588 | if (*f == '%') { |
| 589 | const char* p = f; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 590 | width = 0; |
| 591 | while (isdigit(Py_CHARMASK(*f))) |
| 592 | width = (width*10) + *f++ - '0'; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 593 | while (*++f && *f != '%' && !isalpha(Py_CHARMASK(*f))) |
| 594 | ; |
| 595 | |
| 596 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 597 | * they don't affect the amount of space we reserve. |
| 598 | */ |
| 599 | if ((*f == 'l' || *f == 'z') && |
| 600 | (f[1] == 'd' || f[1] == 'u')) |
Eric Smith | ddd2582 | 2007-08-27 11:33:42 +0000 | [diff] [blame] | 601 | ++f; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 602 | |
| 603 | switch (*f) { |
| 604 | case 'c': |
| 605 | (void)va_arg(count, int); |
| 606 | /* fall through... */ |
| 607 | case '%': |
| 608 | n++; |
| 609 | break; |
| 610 | case 'd': case 'u': case 'i': case 'x': |
| 611 | (void) va_arg(count, int); |
| 612 | /* 20 bytes is enough to hold a 64-bit |
| 613 | integer. Decimal takes the most space. |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 614 | This isn't enough for octal. |
| 615 | If a width is specified we need more |
| 616 | (which we allocate later). */ |
| 617 | if (width < 20) |
| 618 | width = 20; |
| 619 | n += width; |
| 620 | if (abuffersize < width) |
| 621 | abuffersize = width; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 622 | break; |
| 623 | case 's': |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 624 | { |
| 625 | /* UTF-8 */ |
| 626 | unsigned char*s; |
| 627 | s = va_arg(count, unsigned char*); |
| 628 | while (*s) { |
| 629 | if (*s < 128) { |
| 630 | n++; s++; |
| 631 | } else if (*s < 0xc0) { |
| 632 | /* invalid UTF-8 */ |
| 633 | n++; s++; |
| 634 | } else if (*s < 0xc0) { |
| 635 | n++; |
| 636 | s++; if(!*s)break; |
| 637 | s++; |
| 638 | } else if (*s < 0xe0) { |
| 639 | n++; |
| 640 | s++; if(!*s)break; |
| 641 | s++; if(!*s)break; |
| 642 | s++; |
| 643 | } else { |
| 644 | #ifdef Py_UNICODE_WIDE |
| 645 | n++; |
| 646 | #else |
| 647 | n+=2; |
| 648 | #endif |
| 649 | s++; if(!*s)break; |
| 650 | s++; if(!*s)break; |
| 651 | s++; if(!*s)break; |
| 652 | s++; |
| 653 | } |
| 654 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 655 | break; |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 656 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 657 | case 'U': |
| 658 | { |
| 659 | PyObject *obj = va_arg(count, PyObject *); |
| 660 | assert(obj && PyUnicode_Check(obj)); |
| 661 | n += PyUnicode_GET_SIZE(obj); |
| 662 | break; |
| 663 | } |
Walter Dörwald | d7fb764 | 2007-06-11 16:36:59 +0000 | [diff] [blame] | 664 | case 'V': |
| 665 | { |
| 666 | PyObject *obj = va_arg(count, PyObject *); |
| 667 | const char *str = va_arg(count, const char *); |
| 668 | assert(obj || str); |
| 669 | assert(!obj || PyUnicode_Check(obj)); |
| 670 | if (obj) |
| 671 | n += PyUnicode_GET_SIZE(obj); |
| 672 | else |
| 673 | n += strlen(str); |
| 674 | break; |
| 675 | } |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 676 | case 'S': |
| 677 | { |
| 678 | PyObject *obj = va_arg(count, PyObject *); |
| 679 | PyObject *str; |
| 680 | assert(obj); |
| 681 | str = PyObject_Unicode(obj); |
| 682 | if (!str) |
| 683 | goto fail; |
| 684 | n += PyUnicode_GET_SIZE(str); |
| 685 | /* Remember the str and switch to the next slot */ |
| 686 | *callresult++ = str; |
| 687 | break; |
| 688 | } |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 689 | case 'R': |
| 690 | { |
| 691 | PyObject *obj = va_arg(count, PyObject *); |
| 692 | PyObject *repr; |
| 693 | assert(obj); |
| 694 | repr = PyObject_Repr(obj); |
| 695 | if (!repr) |
| 696 | goto fail; |
| 697 | n += PyUnicode_GET_SIZE(repr); |
| 698 | /* Remember the repr and switch to the next slot */ |
| 699 | *callresult++ = repr; |
| 700 | break; |
| 701 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 702 | case 'p': |
| 703 | (void) va_arg(count, int); |
| 704 | /* maximum 64-bit pointer representation: |
| 705 | * 0xffffffffffffffff |
| 706 | * so 19 characters is enough. |
| 707 | * XXX I count 18 -- what's the extra for? |
| 708 | */ |
| 709 | n += 19; |
| 710 | break; |
| 711 | default: |
| 712 | /* if we stumble upon an unknown |
| 713 | formatting code, copy the rest of |
| 714 | the format string to the output |
| 715 | string. (we cannot just skip the |
| 716 | code, since there's no way to know |
| 717 | what's in the argument list) */ |
| 718 | n += strlen(p); |
| 719 | goto expand; |
| 720 | } |
| 721 | } else |
| 722 | n++; |
| 723 | } |
| 724 | expand: |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 725 | if (abuffersize > 20) { |
| 726 | abuffer = PyMem_Malloc(abuffersize); |
| 727 | if (!abuffer) { |
| 728 | PyErr_NoMemory(); |
| 729 | goto fail; |
| 730 | } |
| 731 | realbuffer = abuffer; |
| 732 | } |
| 733 | else |
| 734 | realbuffer = buffer; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 735 | /* step 4: fill the buffer */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 736 | /* Since we've analyzed how much space we need for the worst case, |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 737 | we don't have to resize the string. |
| 738 | There can be no errors beyond this point. */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 739 | string = PyUnicode_FromUnicode(NULL, n); |
| 740 | if (!string) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 741 | goto fail; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 742 | |
| 743 | s = PyUnicode_AS_UNICODE(string); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 744 | callresult = callresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 745 | |
| 746 | for (f = format; *f; f++) { |
| 747 | if (*f == '%') { |
| 748 | const char* p = f++; |
| 749 | int longflag = 0; |
| 750 | int size_tflag = 0; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 751 | zeropad = (*f == '0'); |
| 752 | /* parse the width.precision part */ |
| 753 | width = 0; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 754 | while (isdigit(Py_CHARMASK(*f))) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 755 | width = (width*10) + *f++ - '0'; |
| 756 | precision = 0; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 757 | if (*f == '.') { |
| 758 | f++; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 759 | while (isdigit(Py_CHARMASK(*f))) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 760 | precision = (precision*10) + *f++ - '0'; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 761 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 762 | /* handle the long flag, but only for %ld and %lu. |
| 763 | others can be added when necessary. */ |
| 764 | if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
| 765 | longflag = 1; |
| 766 | ++f; |
| 767 | } |
| 768 | /* handle the size_t flag. */ |
| 769 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 770 | size_tflag = 1; |
| 771 | ++f; |
| 772 | } |
| 773 | |
| 774 | switch (*f) { |
| 775 | case 'c': |
| 776 | *s++ = va_arg(vargs, int); |
| 777 | break; |
| 778 | case 'd': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 779 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'd'); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 780 | if (longflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 781 | sprintf(realbuffer, fmt, va_arg(vargs, long)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 782 | else if (size_tflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 783 | sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 784 | else |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 785 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 786 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 787 | break; |
| 788 | case 'u': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 789 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'u'); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 790 | if (longflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 791 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 792 | else if (size_tflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 793 | sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 794 | else |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 795 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
| 796 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 797 | break; |
| 798 | case 'i': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 799 | makefmt(fmt, 0, 0, zeropad, width, precision, 'i'); |
| 800 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 801 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 802 | break; |
| 803 | case 'x': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 804 | makefmt(fmt, 0, 0, zeropad, width, precision, 'x'); |
| 805 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 806 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 807 | break; |
| 808 | case 's': |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 809 | { |
| 810 | /* Parameter must be UTF-8 encoded. |
| 811 | In case of encoding errors, use |
| 812 | the replacement character. */ |
| 813 | PyObject *u; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 814 | p = va_arg(vargs, char*); |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 815 | u = PyUnicode_DecodeUTF8(p, strlen(p), |
| 816 | "replace"); |
| 817 | if (!u) |
| 818 | goto fail; |
| 819 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(u), |
| 820 | PyUnicode_GET_SIZE(u)); |
| 821 | s += PyUnicode_GET_SIZE(u); |
| 822 | Py_DECREF(u); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 823 | break; |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 824 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 825 | case 'U': |
| 826 | { |
| 827 | PyObject *obj = va_arg(vargs, PyObject *); |
Walter Dörwald | 5c2fab6 | 2007-05-24 19:51:02 +0000 | [diff] [blame] | 828 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 829 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 830 | s += size; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 831 | break; |
| 832 | } |
Walter Dörwald | d7fb764 | 2007-06-11 16:36:59 +0000 | [diff] [blame] | 833 | case 'V': |
| 834 | { |
| 835 | PyObject *obj = va_arg(vargs, PyObject *); |
| 836 | const char *str = va_arg(vargs, const char *); |
| 837 | if (obj) { |
| 838 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 839 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 840 | s += size; |
| 841 | } else { |
| 842 | appendstring(str); |
| 843 | } |
| 844 | break; |
| 845 | } |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 846 | case 'S': |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 847 | case 'R': |
| 848 | { |
Guido van Rossum | 755114a | 2007-06-13 01:04:27 +0000 | [diff] [blame] | 849 | Py_UNICODE *ucopy; |
| 850 | Py_ssize_t usize; |
| 851 | Py_ssize_t upos; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 852 | /* unused, since we already have the result */ |
| 853 | (void) va_arg(vargs, PyObject *); |
Guido van Rossum | 755114a | 2007-06-13 01:04:27 +0000 | [diff] [blame] | 854 | ucopy = PyUnicode_AS_UNICODE(*callresult); |
| 855 | usize = PyUnicode_GET_SIZE(*callresult); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 856 | for (upos = 0; upos<usize;) |
| 857 | *s++ = ucopy[upos++]; |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 858 | /* We're done with the unicode()/repr() => forget it */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 859 | Py_DECREF(*callresult); |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 860 | /* switch to next unicode()/repr() result */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 861 | ++callresult; |
| 862 | break; |
| 863 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 864 | case 'p': |
| 865 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 866 | /* %p is ill-defined: ensure leading 0x. */ |
| 867 | if (buffer[1] == 'X') |
| 868 | buffer[1] = 'x'; |
| 869 | else if (buffer[1] != 'x') { |
| 870 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 871 | buffer[0] = '0'; |
| 872 | buffer[1] = 'x'; |
| 873 | } |
| 874 | appendstring(buffer); |
| 875 | break; |
| 876 | case '%': |
| 877 | *s++ = '%'; |
| 878 | break; |
| 879 | default: |
| 880 | appendstring(p); |
| 881 | goto end; |
| 882 | } |
| 883 | } else |
| 884 | *s++ = *f; |
| 885 | } |
| 886 | |
| 887 | end: |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 888 | if (callresults) |
| 889 | PyMem_Free(callresults); |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 890 | if (abuffer) |
| 891 | PyMem_Free(abuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 892 | _PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
| 893 | return string; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 894 | fail: |
| 895 | if (callresults) { |
| 896 | PyObject **callresult2 = callresults; |
Guido van Rossum | 307fa8c | 2007-07-16 20:46:27 +0000 | [diff] [blame] | 897 | while (callresult2 < callresult) { |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 898 | Py_DECREF(*callresult2); |
| 899 | ++callresult2; |
| 900 | } |
| 901 | PyMem_Free(callresults); |
| 902 | } |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 903 | if (abuffer) |
| 904 | PyMem_Free(abuffer); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 905 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | #undef appendstring |
| 909 | |
| 910 | PyObject * |
| 911 | PyUnicode_FromFormat(const char *format, ...) |
| 912 | { |
| 913 | PyObject* ret; |
| 914 | va_list vargs; |
| 915 | |
| 916 | #ifdef HAVE_STDARG_PROTOTYPES |
| 917 | va_start(vargs, format); |
| 918 | #else |
| 919 | va_start(vargs); |
| 920 | #endif |
| 921 | ret = PyUnicode_FromFormatV(format, vargs); |
| 922 | va_end(vargs); |
| 923 | return ret; |
| 924 | } |
| 925 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 926 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 927 | wchar_t *w, |
| 928 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 929 | { |
| 930 | if (unicode == NULL) { |
| 931 | PyErr_BadInternalCall(); |
| 932 | return -1; |
| 933 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 934 | |
| 935 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 936 | if (size > PyUnicode_GET_SIZE(unicode)) |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 937 | size = PyUnicode_GET_SIZE(unicode) + 1; |
| 938 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 939 | #ifdef HAVE_USABLE_WCHAR_T |
| 940 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 941 | #else |
| 942 | { |
| 943 | register Py_UNICODE *u; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 944 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 945 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 946 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 947 | *w++ = *u++; |
| 948 | } |
| 949 | #endif |
| 950 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 951 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 952 | return PyUnicode_GET_SIZE(unicode); |
| 953 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 954 | return size; |
| 955 | } |
| 956 | |
| 957 | #endif |
| 958 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 959 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 960 | { |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 961 | Py_UNICODE s[2]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 962 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 963 | if (ordinal < 0 || ordinal > 0x10ffff) { |
| 964 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 965 | "chr() arg not in range(0x110000)"); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 966 | return NULL; |
| 967 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 968 | |
| 969 | #ifndef Py_UNICODE_WIDE |
| 970 | if (ordinal > 0xffff) { |
| 971 | ordinal -= 0x10000; |
| 972 | s[0] = 0xD800 | (ordinal >> 10); |
| 973 | s[1] = 0xDC00 | (ordinal & 0x3FF); |
| 974 | return PyUnicode_FromUnicode(s, 2); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 975 | } |
| 976 | #endif |
| 977 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 978 | s[0] = (Py_UNICODE)ordinal; |
| 979 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 980 | } |
| 981 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 982 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 983 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 984 | /* XXX Perhaps we should make this API an alias of |
| 985 | PyObject_Unicode() instead ?! */ |
| 986 | if (PyUnicode_CheckExact(obj)) { |
| 987 | Py_INCREF(obj); |
| 988 | return obj; |
| 989 | } |
| 990 | if (PyUnicode_Check(obj)) { |
| 991 | /* For a Unicode subtype that's not a Unicode object, |
| 992 | return a true Unicode object with the same data. */ |
| 993 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 994 | PyUnicode_GET_SIZE(obj)); |
| 995 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 996 | return PyUnicode_FromEncodedObject(obj, NULL, "strict"); |
| 997 | } |
| 998 | |
| 999 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
| 1000 | const char *encoding, |
| 1001 | const char *errors) |
| 1002 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1003 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1004 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1005 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1006 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1007 | if (obj == NULL) { |
| 1008 | PyErr_BadInternalCall(); |
| 1009 | return NULL; |
| 1010 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1011 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1012 | if (PyUnicode_Check(obj)) { |
| 1013 | PyErr_SetString(PyExc_TypeError, |
| 1014 | "decoding Unicode is not supported"); |
| 1015 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1016 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1017 | |
| 1018 | /* Coerce object */ |
| 1019 | if (PyString_Check(obj)) { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1020 | s = PyString_AS_STRING(obj); |
| 1021 | len = PyString_GET_SIZE(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1022 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1023 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
| 1024 | /* Overwrite the error message with something more useful in |
| 1025 | case of a TypeError. */ |
| 1026 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1027 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1028 | "coercing to Unicode: need string or buffer, " |
| 1029 | "%.80s found", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1030 | Py_Type(obj)->tp_name); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1031 | goto onError; |
| 1032 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1033 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1034 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1035 | if (len == 0) { |
| 1036 | Py_INCREF(unicode_empty); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1037 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1038 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1039 | else |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1040 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1041 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1042 | return v; |
| 1043 | |
| 1044 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1045 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | PyObject *PyUnicode_Decode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1049 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1050 | const char *encoding, |
| 1051 | const char *errors) |
| 1052 | { |
| 1053 | PyObject *buffer = NULL, *unicode; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1054 | |
| 1055 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1056 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1057 | |
| 1058 | /* Shortcuts for common default encodings */ |
| 1059 | if (strcmp(encoding, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1060 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1061 | else if (strcmp(encoding, "latin-1") == 0) |
| 1062 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1063 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1064 | else if (strcmp(encoding, "mbcs") == 0) |
| 1065 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 1066 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1067 | else if (strcmp(encoding, "ascii") == 0) |
| 1068 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1069 | |
| 1070 | /* Decode via the codec registry */ |
| 1071 | buffer = PyBuffer_FromMemory((void *)s, size); |
| 1072 | if (buffer == NULL) |
| 1073 | goto onError; |
| 1074 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1075 | if (unicode == NULL) |
| 1076 | goto onError; |
| 1077 | if (!PyUnicode_Check(unicode)) { |
| 1078 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 1079 | "decoder did not return an unicode object (type=%.400s)", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1080 | Py_Type(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1081 | Py_DECREF(unicode); |
| 1082 | goto onError; |
| 1083 | } |
| 1084 | Py_DECREF(buffer); |
| 1085 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1086 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1087 | onError: |
| 1088 | Py_XDECREF(buffer); |
| 1089 | return NULL; |
| 1090 | } |
| 1091 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1092 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1093 | const char *encoding, |
| 1094 | const char *errors) |
| 1095 | { |
| 1096 | PyObject *v; |
| 1097 | |
| 1098 | if (!PyUnicode_Check(unicode)) { |
| 1099 | PyErr_BadArgument(); |
| 1100 | goto onError; |
| 1101 | } |
| 1102 | |
| 1103 | if (encoding == NULL) |
| 1104 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1105 | |
| 1106 | /* Decode via the codec registry */ |
| 1107 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1108 | if (v == NULL) |
| 1109 | goto onError; |
| 1110 | return v; |
| 1111 | |
| 1112 | onError: |
| 1113 | return NULL; |
| 1114 | } |
| 1115 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1116 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1117 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1118 | const char *encoding, |
| 1119 | const char *errors) |
| 1120 | { |
| 1121 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1122 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1123 | unicode = PyUnicode_FromUnicode(s, size); |
| 1124 | if (unicode == NULL) |
| 1125 | return NULL; |
| 1126 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1127 | Py_DECREF(unicode); |
| 1128 | return v; |
| 1129 | } |
| 1130 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1131 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1132 | const char *encoding, |
| 1133 | const char *errors) |
| 1134 | { |
| 1135 | PyObject *v; |
| 1136 | |
| 1137 | if (!PyUnicode_Check(unicode)) { |
| 1138 | PyErr_BadArgument(); |
| 1139 | goto onError; |
| 1140 | } |
| 1141 | |
| 1142 | if (encoding == NULL) |
| 1143 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1144 | |
| 1145 | /* Encode via the codec registry */ |
| 1146 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1147 | if (v == NULL) |
| 1148 | goto onError; |
| 1149 | return v; |
| 1150 | |
| 1151 | onError: |
| 1152 | return NULL; |
| 1153 | } |
| 1154 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1155 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1156 | const char *encoding, |
| 1157 | const char *errors) |
| 1158 | { |
| 1159 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1160 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1161 | if (!PyUnicode_Check(unicode)) { |
| 1162 | PyErr_BadArgument(); |
| 1163 | goto onError; |
| 1164 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1165 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1166 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1167 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1168 | |
| 1169 | /* Shortcuts for common default encodings */ |
| 1170 | if (errors == NULL) { |
| 1171 | if (strcmp(encoding, "utf-8") == 0) |
Jeremy Hylton | 9cea41c | 2001-05-29 17:13:15 +0000 | [diff] [blame] | 1172 | return PyUnicode_AsUTF8String(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1173 | else if (strcmp(encoding, "latin-1") == 0) |
| 1174 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1175 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1176 | else if (strcmp(encoding, "mbcs") == 0) |
| 1177 | return PyUnicode_AsMBCSString(unicode); |
| 1178 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1179 | else if (strcmp(encoding, "ascii") == 0) |
| 1180 | return PyUnicode_AsASCIIString(unicode); |
| 1181 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1182 | |
| 1183 | /* Encode via the codec registry */ |
| 1184 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1185 | if (v == NULL) |
| 1186 | goto onError; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1187 | if (!PyBytes_Check(v)) { |
| 1188 | if (PyString_Check(v)) { |
| 1189 | /* Old codec, turn it into bytes */ |
| 1190 | PyObject *b = PyBytes_FromObject(v); |
| 1191 | Py_DECREF(v); |
| 1192 | return b; |
| 1193 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1194 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1195 | "encoder did not return a bytes object " |
| 1196 | "(type=%.400s, encoding=%.20s, errors=%.20s)", |
| 1197 | v->ob_type->tp_name, |
| 1198 | encoding ? encoding : "NULL", |
| 1199 | errors ? errors : "NULL"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1200 | Py_DECREF(v); |
| 1201 | goto onError; |
| 1202 | } |
| 1203 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1204 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1205 | onError: |
| 1206 | return NULL; |
| 1207 | } |
| 1208 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1209 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 1210 | const char *errors) |
| 1211 | { |
| 1212 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1213 | PyObject *b; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1214 | if (v) |
| 1215 | return v; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1216 | if (errors != NULL) |
| 1217 | Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString"); |
Guido van Rossum | 0661009 | 2007-08-16 21:02:22 +0000 | [diff] [blame] | 1218 | b = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1219 | PyUnicode_GET_SIZE(unicode), |
| 1220 | NULL); |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1221 | if (!b) |
| 1222 | return NULL; |
| 1223 | v = PyString_FromStringAndSize(PyBytes_AsString(b), |
| 1224 | PyBytes_Size(b)); |
| 1225 | Py_DECREF(b); |
Guido van Rossum | e7a0d39 | 2007-07-12 07:53:00 +0000 | [diff] [blame] | 1226 | ((PyUnicodeObject *)unicode)->defenc = v; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1227 | return v; |
| 1228 | } |
| 1229 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1230 | char* |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1231 | PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1232 | { |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1233 | PyObject *str8; |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 1234 | if (!PyUnicode_Check(unicode)) { |
| 1235 | PyErr_BadArgument(); |
| 1236 | return NULL; |
| 1237 | } |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1238 | str8 = _PyUnicode_AsDefaultEncodedString(unicode, NULL); |
| 1239 | if (str8 == NULL) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1240 | return NULL; |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1241 | if (psize != NULL) |
| 1242 | *psize = PyString_GET_SIZE(str8); |
| 1243 | return PyString_AS_STRING(str8); |
| 1244 | } |
| 1245 | |
| 1246 | char* |
| 1247 | PyUnicode_AsString(PyObject *unicode) |
| 1248 | { |
| 1249 | return PyUnicode_AsStringAndSize(unicode, NULL); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1252 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1253 | { |
| 1254 | if (!PyUnicode_Check(unicode)) { |
| 1255 | PyErr_BadArgument(); |
| 1256 | goto onError; |
| 1257 | } |
| 1258 | return PyUnicode_AS_UNICODE(unicode); |
| 1259 | |
| 1260 | onError: |
| 1261 | return NULL; |
| 1262 | } |
| 1263 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1264 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1265 | { |
| 1266 | if (!PyUnicode_Check(unicode)) { |
| 1267 | PyErr_BadArgument(); |
| 1268 | goto onError; |
| 1269 | } |
| 1270 | return PyUnicode_GET_SIZE(unicode); |
| 1271 | |
| 1272 | onError: |
| 1273 | return -1; |
| 1274 | } |
| 1275 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1276 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1277 | { |
| 1278 | return unicode_default_encoding; |
| 1279 | } |
| 1280 | |
| 1281 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 1282 | { |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1283 | if (strcmp(encoding, unicode_default_encoding) != 0) { |
| 1284 | PyErr_Format(PyExc_ValueError, |
| 1285 | "Can only set default encoding to %s", |
| 1286 | unicode_default_encoding); |
| 1287 | return -1; |
| 1288 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1289 | return 0; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1292 | /* error handling callback helper: |
| 1293 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1294 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1295 | and adjust various state variables. |
| 1296 | return 0 on success, -1 on error |
| 1297 | */ |
| 1298 | |
| 1299 | static |
| 1300 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 1301 | const char *encoding, const char *reason, |
Walter Dörwald | a651d3d | 2007-08-30 15:29:21 +0000 | [diff] [blame] | 1302 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 1303 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1304 | PyObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1305 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1306 | 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] | 1307 | |
| 1308 | PyObject *restuple = NULL; |
| 1309 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1310 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1311 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1312 | Py_ssize_t requiredsize; |
| 1313 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1314 | Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1315 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1316 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1317 | int res = -1; |
| 1318 | |
| 1319 | if (*errorHandler == NULL) { |
| 1320 | *errorHandler = PyCodec_LookupError(errors); |
| 1321 | if (*errorHandler == NULL) |
| 1322 | goto onError; |
| 1323 | } |
| 1324 | |
| 1325 | if (*exceptionObject == NULL) { |
| 1326 | *exceptionObject = PyUnicodeDecodeError_Create( |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1327 | encoding, *input, *inend-*input, *startinpos, *endinpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1328 | if (*exceptionObject == NULL) |
| 1329 | goto onError; |
| 1330 | } |
| 1331 | else { |
| 1332 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 1333 | goto onError; |
| 1334 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 1335 | goto onError; |
| 1336 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1337 | goto onError; |
| 1338 | } |
| 1339 | |
| 1340 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1341 | if (restuple == NULL) |
| 1342 | goto onError; |
| 1343 | if (!PyTuple_Check(restuple)) { |
| 1344 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 1345 | goto onError; |
| 1346 | } |
| 1347 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 1348 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1349 | |
| 1350 | /* Copy back the bytes variables, which might have been modified by the |
| 1351 | callback */ |
| 1352 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 1353 | if (!inputobj) |
| 1354 | goto onError; |
| 1355 | if (!PyBytes_Check(inputobj)) { |
| 1356 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
| 1357 | } |
| 1358 | *input = PyBytes_AS_STRING(inputobj); |
| 1359 | insize = PyBytes_GET_SIZE(inputobj); |
| 1360 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 1361 | /* we can DECREF safely, as the exception has another reference, |
| 1362 | so the object won't go away. */ |
| 1363 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1364 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1365 | if (newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1366 | newpos = insize+newpos; |
| 1367 | if (newpos<0 || newpos>insize) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 1368 | 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] | 1369 | goto onError; |
| 1370 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1371 | |
| 1372 | /* need more space? (at least enough for what we |
| 1373 | have+the replacement+the rest of the string (starting |
| 1374 | at the new input position), so we won't have to check space |
| 1375 | when there are no errors in the rest of the string) */ |
| 1376 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 1377 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 1378 | requiredsize = *outpos + repsize + insize-newpos; |
| 1379 | if (requiredsize > outsize) { |
| 1380 | if (requiredsize<2*outsize) |
| 1381 | requiredsize = 2*outsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1382 | if (PyUnicode_Resize(output, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1383 | goto onError; |
| 1384 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
| 1385 | } |
| 1386 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1387 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1388 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 1389 | *outptr += repsize; |
| 1390 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1391 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1392 | /* we made it! */ |
| 1393 | res = 0; |
| 1394 | |
| 1395 | onError: |
| 1396 | Py_XDECREF(restuple); |
| 1397 | return res; |
| 1398 | } |
| 1399 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1400 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 1401 | |
| 1402 | /* see RFC2152 for details */ |
| 1403 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1404 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1405 | char utf7_special[128] = { |
| 1406 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 1407 | encoded: |
| 1408 | 0 - not special |
| 1409 | 1 - special |
| 1410 | 2 - whitespace (optional) |
| 1411 | 3 - RFC2152 Set O (optional) */ |
| 1412 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 1413 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1414 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 1415 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 1416 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1417 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 1418 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1419 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 1420 | |
| 1421 | }; |
| 1422 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1423 | /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
| 1424 | warnings about the comparison always being false; since |
| 1425 | utf7_special[0] is 1, we can safely make that one comparison |
| 1426 | true */ |
| 1427 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1428 | #define SPECIAL(c, encodeO, encodeWS) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1429 | ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1430 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1431 | (encodeO && (utf7_special[(c)] == 3))) |
| 1432 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1433 | #define B64(n) \ |
| 1434 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 1435 | #define B64CHAR(c) \ |
| 1436 | (isalnum(c) || (c) == '+' || (c) == '/') |
| 1437 | #define UB64(c) \ |
| 1438 | ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
| 1439 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1440 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1441 | #define ENCODE(out, ch, bits) \ |
| 1442 | while (bits >= 6) { \ |
| 1443 | *out++ = B64(ch >> (bits-6)); \ |
| 1444 | bits -= 6; \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1447 | #define DECODE(out, ch, bits, surrogate) \ |
| 1448 | while (bits >= 16) { \ |
| 1449 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 1450 | bits -= 16; \ |
| 1451 | if (surrogate) { \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1452 | /* We have already generated an error for the high surrogate \ |
| 1453 | 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] | 1454 | surrogate = 0; \ |
| 1455 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1456 | /* This is a surrogate pair. Unfortunately we can't represent \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1457 | it in a 16-bit character */ \ |
| 1458 | surrogate = 1; \ |
| 1459 | errmsg = "code pairs are not supported"; \ |
| 1460 | goto utf7Error; \ |
| 1461 | } else { \ |
| 1462 | *out++ = outCh; \ |
| 1463 | } \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1464 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1465 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1466 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1467 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1468 | const char *errors) |
| 1469 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1470 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1471 | Py_ssize_t startinpos; |
| 1472 | Py_ssize_t endinpos; |
| 1473 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1474 | const char *e; |
| 1475 | PyUnicodeObject *unicode; |
| 1476 | Py_UNICODE *p; |
| 1477 | const char *errmsg = ""; |
| 1478 | int inShift = 0; |
| 1479 | unsigned int bitsleft = 0; |
| 1480 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1481 | int surrogate = 0; |
| 1482 | PyObject *errorHandler = NULL; |
| 1483 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1484 | |
| 1485 | unicode = _PyUnicode_New(size); |
| 1486 | if (!unicode) |
| 1487 | return NULL; |
| 1488 | if (size == 0) |
| 1489 | return (PyObject *)unicode; |
| 1490 | |
| 1491 | p = unicode->str; |
| 1492 | e = s + size; |
| 1493 | |
| 1494 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1495 | Py_UNICODE ch; |
| 1496 | restart: |
| 1497 | ch = *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1498 | |
| 1499 | if (inShift) { |
| 1500 | if ((ch == '-') || !B64CHAR(ch)) { |
| 1501 | inShift = 0; |
| 1502 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1503 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1504 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1505 | if (bitsleft >= 6) { |
| 1506 | /* The shift sequence has a partial character in it. If |
| 1507 | bitsleft < 6 then we could just classify it as padding |
| 1508 | but that is not the case here */ |
| 1509 | |
| 1510 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1511 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1512 | } |
| 1513 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1514 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1515 | here so indicate the potential of a misencoded character. */ |
| 1516 | |
| 1517 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 1518 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 1519 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1520 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | if (ch == '-') { |
| 1524 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1525 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1526 | inShift = 1; |
| 1527 | } |
| 1528 | } else if (SPECIAL(ch,0,0)) { |
| 1529 | errmsg = "unexpected special character"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1530 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1531 | } else { |
| 1532 | *p++ = ch; |
| 1533 | } |
| 1534 | } else { |
| 1535 | charsleft = (charsleft << 6) | UB64(ch); |
| 1536 | bitsleft += 6; |
| 1537 | s++; |
| 1538 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1539 | } |
| 1540 | } |
| 1541 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1542 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1543 | s++; |
| 1544 | if (s < e && *s == '-') { |
| 1545 | s++; |
| 1546 | *p++ = '+'; |
| 1547 | } else |
| 1548 | { |
| 1549 | inShift = 1; |
| 1550 | bitsleft = 0; |
| 1551 | } |
| 1552 | } |
| 1553 | else if (SPECIAL(ch,0,0)) { |
Walter Dörwald | 2b65c75 | 2007-08-30 15:35:26 +0000 | [diff] [blame] | 1554 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1555 | errmsg = "unexpected special character"; |
| 1556 | s++; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1557 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1558 | } |
| 1559 | else { |
| 1560 | *p++ = ch; |
| 1561 | s++; |
| 1562 | } |
| 1563 | continue; |
| 1564 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1565 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1566 | endinpos = s-starts; |
| 1567 | if (unicode_decode_call_errorhandler( |
| 1568 | errors, &errorHandler, |
| 1569 | "utf7", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1570 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1571 | (PyObject **)&unicode, &outpos, &p)) |
| 1572 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | if (inShift) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1576 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1577 | endinpos = size; |
| 1578 | if (unicode_decode_call_errorhandler( |
| 1579 | errors, &errorHandler, |
| 1580 | "utf7", "unterminated shift sequence", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1581 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1582 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1583 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1584 | if (s < e) |
| 1585 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1588 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1589 | goto onError; |
| 1590 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1591 | Py_XDECREF(errorHandler); |
| 1592 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1593 | return (PyObject *)unicode; |
| 1594 | |
| 1595 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1596 | Py_XDECREF(errorHandler); |
| 1597 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1598 | Py_DECREF(unicode); |
| 1599 | return NULL; |
| 1600 | } |
| 1601 | |
| 1602 | |
| 1603 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1604 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1605 | int encodeSetO, |
| 1606 | int encodeWhiteSpace, |
| 1607 | const char *errors) |
| 1608 | { |
| 1609 | PyObject *v; |
| 1610 | /* It might be possible to tighten this worst case */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1611 | Py_ssize_t cbAllocated = 5 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1612 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1613 | Py_ssize_t i = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1614 | unsigned int bitsleft = 0; |
| 1615 | unsigned long charsleft = 0; |
| 1616 | char * out; |
| 1617 | char * start; |
| 1618 | |
| 1619 | if (size == 0) |
Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1620 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1621 | |
Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1622 | v = PyBytes_FromStringAndSize(NULL, cbAllocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1623 | if (v == NULL) |
| 1624 | return NULL; |
| 1625 | |
Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1626 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1627 | for (;i < size; ++i) { |
| 1628 | Py_UNICODE ch = s[i]; |
| 1629 | |
| 1630 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1631 | if (ch == '+') { |
| 1632 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1633 | *out++ = '-'; |
| 1634 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1635 | charsleft = ch; |
| 1636 | bitsleft = 16; |
| 1637 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1638 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1639 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1640 | } else { |
| 1641 | *out++ = (char) ch; |
| 1642 | } |
| 1643 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1644 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1645 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1646 | charsleft = 0; |
| 1647 | bitsleft = 0; |
| 1648 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1649 | so no '-' is required, except if the character is itself a '-' */ |
| 1650 | if (B64CHAR(ch) || ch == '-') { |
| 1651 | *out++ = '-'; |
| 1652 | } |
| 1653 | inShift = 0; |
| 1654 | *out++ = (char) ch; |
| 1655 | } else { |
| 1656 | bitsleft += 16; |
| 1657 | charsleft = (charsleft << 16) | ch; |
| 1658 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1659 | |
| 1660 | /* If the next character is special then we dont' need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1661 | 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] | 1662 | or '-' then the shift sequence will be terminated implicitly and we |
| 1663 | don't have to insert a '-'. */ |
| 1664 | |
| 1665 | if (bitsleft == 0) { |
| 1666 | if (i + 1 < size) { |
| 1667 | Py_UNICODE ch2 = s[i+1]; |
| 1668 | |
| 1669 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1670 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1671 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1672 | *out++ = '-'; |
| 1673 | inShift = 0; |
| 1674 | } else { |
| 1675 | inShift = 0; |
| 1676 | } |
| 1677 | |
| 1678 | } |
| 1679 | else { |
| 1680 | *out++ = '-'; |
| 1681 | inShift = 0; |
| 1682 | } |
| 1683 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1684 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1685 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1686 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1687 | if (bitsleft) { |
| 1688 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1689 | *out++ = '-'; |
| 1690 | } |
| 1691 | |
Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1692 | if (PyBytes_Resize(v, out - start)) { |
| 1693 | Py_DECREF(v); |
| 1694 | return NULL; |
| 1695 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1696 | return v; |
| 1697 | } |
| 1698 | |
| 1699 | #undef SPECIAL |
| 1700 | #undef B64 |
| 1701 | #undef B64CHAR |
| 1702 | #undef UB64 |
| 1703 | #undef ENCODE |
| 1704 | #undef DECODE |
| 1705 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1706 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1707 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1708 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1709 | char utf8_code_length[256] = { |
| 1710 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1711 | illegal prefix. see RFC 2279 for details */ |
| 1712 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1713 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1714 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1715 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1716 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1717 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1718 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1719 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1720 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1721 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1722 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1723 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1724 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1725 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1726 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1727 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1728 | }; |
| 1729 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1730 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1731 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1732 | const char *errors) |
| 1733 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1734 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 1735 | } |
| 1736 | |
| 1737 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1738 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1739 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1740 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1741 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1742 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1743 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1744 | Py_ssize_t startinpos; |
| 1745 | Py_ssize_t endinpos; |
| 1746 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1747 | const char *e; |
| 1748 | PyUnicodeObject *unicode; |
| 1749 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1750 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1751 | PyObject *errorHandler = NULL; |
| 1752 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1753 | |
| 1754 | /* Note: size will always be longer than the resulting Unicode |
| 1755 | character count */ |
| 1756 | unicode = _PyUnicode_New(size); |
| 1757 | if (!unicode) |
| 1758 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1759 | if (size == 0) { |
| 1760 | if (consumed) |
| 1761 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1762 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1763 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1764 | |
| 1765 | /* Unpack UTF-8 encoded data */ |
| 1766 | p = unicode->str; |
| 1767 | e = s + size; |
| 1768 | |
| 1769 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1770 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1771 | |
| 1772 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1773 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1774 | s++; |
| 1775 | continue; |
| 1776 | } |
| 1777 | |
| 1778 | n = utf8_code_length[ch]; |
| 1779 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1780 | if (s + n > e) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1781 | if (consumed) |
| 1782 | break; |
| 1783 | else { |
| 1784 | errmsg = "unexpected end of data"; |
| 1785 | startinpos = s-starts; |
| 1786 | endinpos = size; |
| 1787 | goto utf8Error; |
| 1788 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1789 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1790 | |
| 1791 | switch (n) { |
| 1792 | |
| 1793 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1794 | errmsg = "unexpected code byte"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1795 | startinpos = s-starts; |
| 1796 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1797 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1798 | |
| 1799 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1800 | errmsg = "internal error"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1801 | startinpos = s-starts; |
| 1802 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1803 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1804 | |
| 1805 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1806 | if ((s[1] & 0xc0) != 0x80) { |
| 1807 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1808 | startinpos = s-starts; |
| 1809 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1810 | goto utf8Error; |
| 1811 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1812 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1813 | if (ch < 0x80) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1814 | startinpos = s-starts; |
| 1815 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1816 | errmsg = "illegal encoding"; |
| 1817 | goto utf8Error; |
| 1818 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1819 | else |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1820 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1821 | break; |
| 1822 | |
| 1823 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1824 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1825 | (s[2] & 0xc0) != 0x80) { |
| 1826 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1827 | startinpos = s-starts; |
| 1828 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1829 | goto utf8Error; |
| 1830 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1831 | 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] | 1832 | if (ch < 0x0800) { |
| 1833 | /* Note: UTF-8 encodings of surrogates are considered |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1834 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1835 | |
| 1836 | XXX For wide builds (UCS-4) we should probably try |
| 1837 | to recombine the surrogates into a single code |
| 1838 | unit. |
| 1839 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1840 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1841 | startinpos = s-starts; |
| 1842 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1843 | goto utf8Error; |
| 1844 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1845 | else |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1846 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1847 | break; |
| 1848 | |
| 1849 | case 4: |
| 1850 | if ((s[1] & 0xc0) != 0x80 || |
| 1851 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1852 | (s[3] & 0xc0) != 0x80) { |
| 1853 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1854 | startinpos = s-starts; |
| 1855 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1856 | goto utf8Error; |
| 1857 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1858 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 1859 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 1860 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1861 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1862 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1863 | || (ch > 0x10ffff)) /* maximum value allowed for |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1864 | UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1865 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1866 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1867 | startinpos = s-starts; |
| 1868 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1869 | goto utf8Error; |
| 1870 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1871 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1872 | *p++ = (Py_UNICODE)ch; |
| 1873 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1874 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1875 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1876 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 1877 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1878 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1879 | /* high surrogate = top 10 bits added to D800 */ |
| 1880 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1881 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1882 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1883 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1884 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1885 | break; |
| 1886 | |
| 1887 | default: |
| 1888 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1889 | errmsg = "unsupported Unicode code range"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1890 | startinpos = s-starts; |
| 1891 | endinpos = startinpos+n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1892 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1893 | } |
| 1894 | s += n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1895 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1896 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1897 | utf8Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1898 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1899 | if (unicode_decode_call_errorhandler( |
| 1900 | errors, &errorHandler, |
| 1901 | "utf8", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1902 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1903 | (PyObject **)&unicode, &outpos, &p)) |
| 1904 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1905 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1906 | if (consumed) |
| 1907 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1908 | |
| 1909 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1910 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1911 | goto onError; |
| 1912 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1913 | Py_XDECREF(errorHandler); |
| 1914 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1915 | return (PyObject *)unicode; |
| 1916 | |
| 1917 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1918 | Py_XDECREF(errorHandler); |
| 1919 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1920 | Py_DECREF(unicode); |
| 1921 | return NULL; |
| 1922 | } |
| 1923 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1924 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 1925 | and allocate exactly as much space needed at the end. Else allocate the |
| 1926 | maximum possible needed (4 result bytes per Unicode character), and return |
| 1927 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1928 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1929 | PyObject * |
| 1930 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1931 | Py_ssize_t size, |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1932 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1933 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1934 | #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] | 1935 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1936 | Py_ssize_t i; /* index into s of next input byte */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1937 | PyObject *v; /* result string object */ |
| 1938 | char *p; /* next free byte in output buffer */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1939 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1940 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1941 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1942 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1943 | assert(s != NULL); |
| 1944 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1945 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1946 | if (size <= MAX_SHORT_UNICHARS) { |
| 1947 | /* Write into the stack buffer; nallocated can't overflow. |
| 1948 | * At the end, we'll allocate exactly as much heap space as it |
| 1949 | * turns out we need. |
| 1950 | */ |
| 1951 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
| 1952 | v = NULL; /* will allocate after we're done */ |
| 1953 | p = stackbuf; |
| 1954 | } |
| 1955 | else { |
| 1956 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 1957 | nallocated = size * 4; |
| 1958 | if (nallocated / 4 != size) /* overflow! */ |
| 1959 | return PyErr_NoMemory(); |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1960 | v = PyBytes_FromStringAndSize(NULL, nallocated); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1961 | if (v == NULL) |
| 1962 | return NULL; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1963 | p = PyBytes_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1964 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1965 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1966 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1967 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1968 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1969 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1970 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1971 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1972 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1973 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1974 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 1975 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 1976 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1977 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1978 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1979 | /* Encode UCS2 Unicode ordinals */ |
| 1980 | if (ch < 0x10000) { |
| 1981 | /* Special case: check for high surrogate */ |
| 1982 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 1983 | Py_UCS4 ch2 = s[i]; |
| 1984 | /* Check for low surrogate and combine the two to |
| 1985 | form a UCS4 value */ |
| 1986 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1987 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1988 | i++; |
| 1989 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1990 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1991 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1992 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1993 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1994 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1995 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1996 | continue; |
| 1997 | } |
| 1998 | encodeUCS4: |
| 1999 | /* Encode UCS4 Unicode ordinals */ |
| 2000 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2001 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 2002 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2003 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2004 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2005 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2006 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2007 | if (v == NULL) { |
| 2008 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2009 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2010 | assert(nneeded <= nallocated); |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 2011 | v = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2012 | } |
| 2013 | else { |
| 2014 | /* Cut back to size actually needed. */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 2015 | nneeded = p - PyBytes_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2016 | assert(nneeded <= nallocated); |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 2017 | PyBytes_Resize(v, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2018 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2019 | return v; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2020 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2021 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2022 | } |
| 2023 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2024 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 2025 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2026 | if (!PyUnicode_Check(unicode)) { |
| 2027 | PyErr_BadArgument(); |
| 2028 | return NULL; |
| 2029 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 2030 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 2031 | PyUnicode_GET_SIZE(unicode), |
| 2032 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2033 | } |
| 2034 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2035 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 2036 | |
| 2037 | PyObject * |
| 2038 | PyUnicode_DecodeUTF32(const char *s, |
| 2039 | Py_ssize_t size, |
| 2040 | const char *errors, |
| 2041 | int *byteorder) |
| 2042 | { |
| 2043 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 2044 | } |
| 2045 | |
| 2046 | PyObject * |
| 2047 | PyUnicode_DecodeUTF32Stateful(const char *s, |
| 2048 | Py_ssize_t size, |
| 2049 | const char *errors, |
| 2050 | int *byteorder, |
| 2051 | Py_ssize_t *consumed) |
| 2052 | { |
| 2053 | const char *starts = s; |
| 2054 | Py_ssize_t startinpos; |
| 2055 | Py_ssize_t endinpos; |
| 2056 | Py_ssize_t outpos; |
| 2057 | PyUnicodeObject *unicode; |
| 2058 | Py_UNICODE *p; |
| 2059 | #ifndef Py_UNICODE_WIDE |
| 2060 | int i, pairs; |
| 2061 | #else |
| 2062 | const int pairs = 0; |
| 2063 | #endif |
| 2064 | const unsigned char *q, *e; |
| 2065 | int bo = 0; /* assume native ordering by default */ |
| 2066 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2067 | /* Offsets from q for retrieving bytes in the right order. */ |
| 2068 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2069 | int iorder[] = {0, 1, 2, 3}; |
| 2070 | #else |
| 2071 | int iorder[] = {3, 2, 1, 0}; |
| 2072 | #endif |
| 2073 | PyObject *errorHandler = NULL; |
| 2074 | PyObject *exc = NULL; |
Guido van Rossum | 8d991ed | 2007-08-17 15:41:00 +0000 | [diff] [blame] | 2075 | /* On narrow builds we split characters outside the BMP into two |
| 2076 | codepoints => count how much extra space we need. */ |
| 2077 | #ifndef Py_UNICODE_WIDE |
| 2078 | for (i = pairs = 0; i < size/4; i++) |
| 2079 | if (((Py_UCS4 *)s)[i] >= 0x10000) |
| 2080 | pairs++; |
| 2081 | #endif |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2082 | |
| 2083 | /* This might be one to much, because of a BOM */ |
| 2084 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 2085 | if (!unicode) |
| 2086 | return NULL; |
| 2087 | if (size == 0) |
| 2088 | return (PyObject *)unicode; |
| 2089 | |
| 2090 | /* Unpack UTF-32 encoded data */ |
| 2091 | p = unicode->str; |
| 2092 | q = (unsigned char *)s; |
| 2093 | e = q + size; |
| 2094 | |
| 2095 | if (byteorder) |
| 2096 | bo = *byteorder; |
| 2097 | |
| 2098 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2099 | byte order setting accordingly. In native mode, the leading BOM |
| 2100 | mark is skipped, in all other modes, it is copied to the output |
| 2101 | stream as-is (giving a ZWNBSP character). */ |
| 2102 | if (bo == 0) { |
| 2103 | if (size >= 4) { |
| 2104 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2105 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 2106 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2107 | if (bom == 0x0000FEFF) { |
| 2108 | q += 4; |
| 2109 | bo = -1; |
| 2110 | } |
| 2111 | else if (bom == 0xFFFE0000) { |
| 2112 | q += 4; |
| 2113 | bo = 1; |
| 2114 | } |
| 2115 | #else |
| 2116 | if (bom == 0x0000FEFF) { |
| 2117 | q += 4; |
| 2118 | bo = 1; |
| 2119 | } |
| 2120 | else if (bom == 0xFFFE0000) { |
| 2121 | q += 4; |
| 2122 | bo = -1; |
| 2123 | } |
| 2124 | #endif |
| 2125 | } |
| 2126 | } |
| 2127 | |
| 2128 | if (bo == -1) { |
| 2129 | /* force LE */ |
| 2130 | iorder[0] = 0; |
| 2131 | iorder[1] = 1; |
| 2132 | iorder[2] = 2; |
| 2133 | iorder[3] = 3; |
| 2134 | } |
| 2135 | else if (bo == 1) { |
| 2136 | /* force BE */ |
| 2137 | iorder[0] = 3; |
| 2138 | iorder[1] = 2; |
| 2139 | iorder[2] = 1; |
| 2140 | iorder[3] = 0; |
| 2141 | } |
| 2142 | |
| 2143 | while (q < e) { |
| 2144 | Py_UCS4 ch; |
| 2145 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 2146 | if (e-q<4) { |
| 2147 | if (consumed) |
| 2148 | break; |
| 2149 | errmsg = "truncated data"; |
| 2150 | startinpos = ((const char *)q)-starts; |
| 2151 | endinpos = ((const char *)e)-starts; |
| 2152 | goto utf32Error; |
| 2153 | /* The remaining input chars are ignored if the callback |
| 2154 | chooses to skip the input */ |
| 2155 | } |
| 2156 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2157 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 2158 | |
| 2159 | if (ch >= 0x110000) |
| 2160 | { |
| 2161 | errmsg = "codepoint not in range(0x110000)"; |
| 2162 | startinpos = ((const char *)q)-starts; |
| 2163 | endinpos = startinpos+4; |
| 2164 | goto utf32Error; |
| 2165 | } |
| 2166 | #ifndef Py_UNICODE_WIDE |
| 2167 | if (ch >= 0x10000) |
| 2168 | { |
| 2169 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 2170 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2171 | } |
| 2172 | else |
| 2173 | #endif |
| 2174 | *p++ = ch; |
| 2175 | q += 4; |
| 2176 | continue; |
| 2177 | utf32Error: |
| 2178 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2179 | if (unicode_decode_call_errorhandler( |
| 2180 | errors, &errorHandler, |
| 2181 | "utf32", errmsg, |
| 2182 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 2183 | (PyObject **)&unicode, &outpos, &p)) |
| 2184 | goto onError; |
| 2185 | } |
| 2186 | |
| 2187 | if (byteorder) |
| 2188 | *byteorder = bo; |
| 2189 | |
| 2190 | if (consumed) |
| 2191 | *consumed = (const char *)q-starts; |
| 2192 | |
| 2193 | /* Adjust length */ |
| 2194 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 2195 | goto onError; |
| 2196 | |
| 2197 | Py_XDECREF(errorHandler); |
| 2198 | Py_XDECREF(exc); |
| 2199 | return (PyObject *)unicode; |
| 2200 | |
| 2201 | onError: |
| 2202 | Py_DECREF(unicode); |
| 2203 | Py_XDECREF(errorHandler); |
| 2204 | Py_XDECREF(exc); |
| 2205 | return NULL; |
| 2206 | } |
| 2207 | |
| 2208 | PyObject * |
| 2209 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 2210 | Py_ssize_t size, |
| 2211 | const char *errors, |
| 2212 | int byteorder) |
| 2213 | { |
| 2214 | PyObject *v; |
| 2215 | unsigned char *p; |
| 2216 | #ifndef Py_UNICODE_WIDE |
| 2217 | int i, pairs; |
| 2218 | #else |
| 2219 | const int pairs = 0; |
| 2220 | #endif |
| 2221 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2222 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2223 | int iorder[] = {0, 1, 2, 3}; |
| 2224 | #else |
| 2225 | int iorder[] = {3, 2, 1, 0}; |
| 2226 | #endif |
| 2227 | |
| 2228 | #define STORECHAR(CH) \ |
| 2229 | do { \ |
| 2230 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 2231 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 2232 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 2233 | p[iorder[0]] = (CH) & 0xff; \ |
| 2234 | p += 4; \ |
| 2235 | } while(0) |
| 2236 | |
| 2237 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 2238 | so we need less space. */ |
| 2239 | #ifndef Py_UNICODE_WIDE |
| 2240 | for (i = pairs = 0; i < size-1; i++) |
| 2241 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2242 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 2243 | pairs++; |
| 2244 | #endif |
| 2245 | v = PyBytes_FromStringAndSize(NULL, |
| 2246 | 4 * (size - pairs + (byteorder == 0))); |
| 2247 | if (v == NULL) |
| 2248 | return NULL; |
| 2249 | |
| 2250 | p = (unsigned char *)PyBytes_AS_STRING(v); |
| 2251 | if (byteorder == 0) |
| 2252 | STORECHAR(0xFEFF); |
| 2253 | if (size == 0) |
| 2254 | return v; |
| 2255 | |
| 2256 | if (byteorder == -1) { |
| 2257 | /* force LE */ |
| 2258 | iorder[0] = 0; |
| 2259 | iorder[1] = 1; |
| 2260 | iorder[2] = 2; |
| 2261 | iorder[3] = 3; |
| 2262 | } |
| 2263 | else if (byteorder == 1) { |
| 2264 | /* force BE */ |
| 2265 | iorder[0] = 3; |
| 2266 | iorder[1] = 2; |
| 2267 | iorder[2] = 1; |
| 2268 | iorder[3] = 0; |
| 2269 | } |
| 2270 | |
| 2271 | while (size-- > 0) { |
| 2272 | Py_UCS4 ch = *s++; |
| 2273 | #ifndef Py_UNICODE_WIDE |
| 2274 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 2275 | Py_UCS4 ch2 = *s; |
| 2276 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 2277 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 2278 | s++; |
| 2279 | size--; |
| 2280 | } |
| 2281 | } |
| 2282 | #endif |
| 2283 | STORECHAR(ch); |
| 2284 | } |
| 2285 | return v; |
| 2286 | #undef STORECHAR |
| 2287 | } |
| 2288 | |
| 2289 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 2290 | { |
| 2291 | if (!PyUnicode_Check(unicode)) { |
| 2292 | PyErr_BadArgument(); |
| 2293 | return NULL; |
| 2294 | } |
| 2295 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
| 2296 | PyUnicode_GET_SIZE(unicode), |
| 2297 | NULL, |
| 2298 | 0); |
| 2299 | } |
| 2300 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2301 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 2302 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2303 | PyObject * |
| 2304 | PyUnicode_DecodeUTF16(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2305 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2306 | const char *errors, |
| 2307 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2308 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2309 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 2310 | } |
| 2311 | |
| 2312 | PyObject * |
| 2313 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2314 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2315 | const char *errors, |
| 2316 | int *byteorder, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2317 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2318 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2319 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2320 | Py_ssize_t startinpos; |
| 2321 | Py_ssize_t endinpos; |
| 2322 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2323 | PyUnicodeObject *unicode; |
| 2324 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2325 | const unsigned char *q, *e; |
| 2326 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2327 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2328 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 2329 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2330 | int ihi = 1, ilo = 0; |
| 2331 | #else |
| 2332 | int ihi = 0, ilo = 1; |
| 2333 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2334 | PyObject *errorHandler = NULL; |
| 2335 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2336 | |
| 2337 | /* Note: size will always be longer than the resulting Unicode |
| 2338 | character count */ |
| 2339 | unicode = _PyUnicode_New(size); |
| 2340 | if (!unicode) |
| 2341 | return NULL; |
| 2342 | if (size == 0) |
| 2343 | return (PyObject *)unicode; |
| 2344 | |
| 2345 | /* Unpack UTF-16 encoded data */ |
| 2346 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2347 | q = (unsigned char *)s; |
| 2348 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2349 | |
| 2350 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2351 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2352 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2353 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2354 | byte order setting accordingly. In native mode, the leading BOM |
| 2355 | mark is skipped, in all other modes, it is copied to the output |
| 2356 | stream as-is (giving a ZWNBSP character). */ |
| 2357 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2358 | if (size >= 2) { |
| 2359 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2360 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2361 | if (bom == 0xFEFF) { |
| 2362 | q += 2; |
| 2363 | bo = -1; |
| 2364 | } |
| 2365 | else if (bom == 0xFFFE) { |
| 2366 | q += 2; |
| 2367 | bo = 1; |
| 2368 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2369 | #else |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2370 | if (bom == 0xFEFF) { |
| 2371 | q += 2; |
| 2372 | bo = 1; |
| 2373 | } |
| 2374 | else if (bom == 0xFFFE) { |
| 2375 | q += 2; |
| 2376 | bo = -1; |
| 2377 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2378 | #endif |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2379 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2380 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2381 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2382 | if (bo == -1) { |
| 2383 | /* force LE */ |
| 2384 | ihi = 1; |
| 2385 | ilo = 0; |
| 2386 | } |
| 2387 | else if (bo == 1) { |
| 2388 | /* force BE */ |
| 2389 | ihi = 0; |
| 2390 | ilo = 1; |
| 2391 | } |
| 2392 | |
| 2393 | while (q < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2394 | Py_UNICODE ch; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2395 | /* remaining bytes at the end? (size should be even) */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2396 | if (e-q<2) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2397 | if (consumed) |
| 2398 | break; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2399 | errmsg = "truncated data"; |
| 2400 | startinpos = ((const char *)q)-starts; |
| 2401 | endinpos = ((const char *)e)-starts; |
| 2402 | goto utf16Error; |
| 2403 | /* The remaining input chars are ignored if the callback |
| 2404 | chooses to skip the input */ |
| 2405 | } |
| 2406 | ch = (q[ihi] << 8) | q[ilo]; |
| 2407 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2408 | q += 2; |
| 2409 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2410 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 2411 | *p++ = ch; |
| 2412 | continue; |
| 2413 | } |
| 2414 | |
| 2415 | /* UTF-16 code pair: */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2416 | if (q >= e) { |
| 2417 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2418 | startinpos = (((const char *)q)-2)-starts; |
| 2419 | endinpos = ((const char *)e)-starts; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2420 | goto utf16Error; |
| 2421 | } |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2422 | if (0xD800 <= ch && ch <= 0xDBFF) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2423 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 2424 | q += 2; |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2425 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2426 | #ifndef Py_UNICODE_WIDE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2427 | *p++ = ch; |
| 2428 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2429 | #else |
| 2430 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2431 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2432 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2433 | } |
| 2434 | else { |
| 2435 | errmsg = "illegal UTF-16 surrogate"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2436 | startinpos = (((const char *)q)-4)-starts; |
| 2437 | endinpos = startinpos+2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2438 | goto utf16Error; |
| 2439 | } |
| 2440 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2441 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2442 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2443 | startinpos = (((const char *)q)-2)-starts; |
| 2444 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2445 | /* Fall through to report the error */ |
| 2446 | |
| 2447 | utf16Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2448 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2449 | if (unicode_decode_call_errorhandler( |
| 2450 | errors, &errorHandler, |
| 2451 | "utf16", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2452 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2453 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2454 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2455 | } |
| 2456 | |
| 2457 | if (byteorder) |
| 2458 | *byteorder = bo; |
| 2459 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2460 | if (consumed) |
| 2461 | *consumed = (const char *)q-starts; |
| 2462 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2463 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2464 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2465 | goto onError; |
| 2466 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2467 | Py_XDECREF(errorHandler); |
| 2468 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2469 | return (PyObject *)unicode; |
| 2470 | |
| 2471 | onError: |
| 2472 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2473 | Py_XDECREF(errorHandler); |
| 2474 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2475 | return NULL; |
| 2476 | } |
| 2477 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2478 | PyObject * |
| 2479 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2480 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2481 | const char *errors, |
| 2482 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2483 | { |
| 2484 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2485 | unsigned char *p; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2486 | #ifdef Py_UNICODE_WIDE |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2487 | int i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2488 | #else |
| 2489 | const int pairs = 0; |
| 2490 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2491 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2492 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2493 | int ihi = 1, ilo = 0; |
| 2494 | #else |
| 2495 | int ihi = 0, ilo = 1; |
| 2496 | #endif |
| 2497 | |
| 2498 | #define STORECHAR(CH) \ |
| 2499 | do { \ |
| 2500 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 2501 | p[ilo] = (CH) & 0xff; \ |
| 2502 | p += 2; \ |
| 2503 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2504 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2505 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2506 | for (i = pairs = 0; i < size; i++) |
| 2507 | if (s[i] >= 0x10000) |
| 2508 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2509 | #endif |
Walter Dörwald | 3cc3452 | 2007-05-04 10:48:27 +0000 | [diff] [blame] | 2510 | v = PyBytes_FromStringAndSize(NULL, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2511 | 2 * (size + pairs + (byteorder == 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2512 | if (v == NULL) |
| 2513 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2514 | |
Walter Dörwald | 3cc3452 | 2007-05-04 10:48:27 +0000 | [diff] [blame] | 2515 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2516 | if (byteorder == 0) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2517 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2518 | if (size == 0) |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2519 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2520 | |
| 2521 | if (byteorder == -1) { |
| 2522 | /* force LE */ |
| 2523 | ihi = 1; |
| 2524 | ilo = 0; |
| 2525 | } |
| 2526 | else if (byteorder == 1) { |
| 2527 | /* force BE */ |
| 2528 | ihi = 0; |
| 2529 | ilo = 1; |
| 2530 | } |
| 2531 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2532 | while (size-- > 0) { |
| 2533 | Py_UNICODE ch = *s++; |
| 2534 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2535 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2536 | if (ch >= 0x10000) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2537 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2538 | ch = 0xD800 | ((ch-0x10000) >> 10); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2539 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2540 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2541 | STORECHAR(ch); |
| 2542 | if (ch2) |
| 2543 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2544 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2545 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2546 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2547 | } |
| 2548 | |
| 2549 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 2550 | { |
| 2551 | if (!PyUnicode_Check(unicode)) { |
| 2552 | PyErr_BadArgument(); |
| 2553 | return NULL; |
| 2554 | } |
| 2555 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 2556 | PyUnicode_GET_SIZE(unicode), |
| 2557 | NULL, |
| 2558 | 0); |
| 2559 | } |
| 2560 | |
| 2561 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 2562 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2563 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 2564 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2565 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2566 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2567 | const char *errors) |
| 2568 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2569 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2570 | Py_ssize_t startinpos; |
| 2571 | Py_ssize_t endinpos; |
| 2572 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2573 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2574 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2575 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2576 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2577 | char* message; |
| 2578 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2579 | PyObject *errorHandler = NULL; |
| 2580 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2581 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2582 | /* Escaped strings will always be longer than the resulting |
| 2583 | 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] | 2584 | length after conversion to the true value. |
| 2585 | (but if the error callback returns a long replacement string |
| 2586 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2587 | v = _PyUnicode_New(size); |
| 2588 | if (v == NULL) |
| 2589 | goto onError; |
| 2590 | if (size == 0) |
| 2591 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2592 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2593 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2594 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2595 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2596 | while (s < end) { |
| 2597 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2598 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2599 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2600 | |
| 2601 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2602 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2603 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2604 | continue; |
| 2605 | } |
| 2606 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2607 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2608 | /* \ - Escapes */ |
| 2609 | s++; |
| 2610 | switch (*s++) { |
| 2611 | |
| 2612 | /* \x escapes */ |
| 2613 | case '\n': break; |
| 2614 | case '\\': *p++ = '\\'; break; |
| 2615 | case '\'': *p++ = '\''; break; |
| 2616 | case '\"': *p++ = '\"'; break; |
| 2617 | case 'b': *p++ = '\b'; break; |
| 2618 | case 'f': *p++ = '\014'; break; /* FF */ |
| 2619 | case 't': *p++ = '\t'; break; |
| 2620 | case 'n': *p++ = '\n'; break; |
| 2621 | case 'r': *p++ = '\r'; break; |
| 2622 | case 'v': *p++ = '\013'; break; /* VT */ |
| 2623 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 2624 | |
| 2625 | /* \OOO (octal) escapes */ |
| 2626 | case '0': case '1': case '2': case '3': |
| 2627 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2628 | x = s[-1] - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2629 | if ('0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2630 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2631 | if ('0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2632 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2633 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2634 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2635 | break; |
| 2636 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2637 | /* hex escapes */ |
| 2638 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2639 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2640 | digits = 2; |
| 2641 | message = "truncated \\xXX escape"; |
| 2642 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2643 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2644 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2645 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2646 | digits = 4; |
| 2647 | message = "truncated \\uXXXX escape"; |
| 2648 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2649 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2650 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2651 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2652 | digits = 8; |
| 2653 | message = "truncated \\UXXXXXXXX escape"; |
| 2654 | hexescape: |
| 2655 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2656 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2657 | if (s+digits>end) { |
| 2658 | endinpos = size; |
| 2659 | if (unicode_decode_call_errorhandler( |
| 2660 | errors, &errorHandler, |
| 2661 | "unicodeescape", "end of string in escape sequence", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2662 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2663 | (PyObject **)&v, &outpos, &p)) |
| 2664 | goto onError; |
| 2665 | goto nextByte; |
| 2666 | } |
| 2667 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2668 | c = (unsigned char) s[i]; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2669 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2670 | endinpos = (s+i+1)-starts; |
| 2671 | if (unicode_decode_call_errorhandler( |
| 2672 | errors, &errorHandler, |
| 2673 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2674 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2675 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2676 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2677 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2678 | } |
| 2679 | chr = (chr<<4) & ~0xF; |
| 2680 | if (c >= '0' && c <= '9') |
| 2681 | chr += c - '0'; |
| 2682 | else if (c >= 'a' && c <= 'f') |
| 2683 | chr += 10 + c - 'a'; |
| 2684 | else |
| 2685 | chr += 10 + c - 'A'; |
| 2686 | } |
| 2687 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 2688 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2689 | /* _decoding_error will have already written into the |
| 2690 | target buffer. */ |
| 2691 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2692 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2693 | /* when we get here, chr is a 32-bit unicode character */ |
| 2694 | if (chr <= 0xffff) |
| 2695 | /* UCS-2 character */ |
| 2696 | *p++ = (Py_UNICODE) chr; |
| 2697 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2698 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2699 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2700 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2701 | *p++ = chr; |
| 2702 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2703 | chr -= 0x10000L; |
| 2704 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2705 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2706 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2707 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2708 | endinpos = s-starts; |
| 2709 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2710 | if (unicode_decode_call_errorhandler( |
| 2711 | errors, &errorHandler, |
| 2712 | "unicodeescape", "illegal Unicode character", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2713 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2714 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2715 | goto onError; |
| 2716 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2717 | break; |
| 2718 | |
| 2719 | /* \N{name} */ |
| 2720 | case 'N': |
| 2721 | message = "malformed \\N character escape"; |
| 2722 | if (ucnhash_CAPI == NULL) { |
| 2723 | /* load the unicode data module */ |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2724 | PyObject *m, *api; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2725 | m = PyImport_ImportModule("unicodedata"); |
| 2726 | if (m == NULL) |
| 2727 | goto ucnhashError; |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2728 | api = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2729 | Py_DECREF(m); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2730 | if (api == NULL) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2731 | goto ucnhashError; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2732 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2733 | Py_DECREF(api); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2734 | if (ucnhash_CAPI == NULL) |
| 2735 | goto ucnhashError; |
| 2736 | } |
| 2737 | if (*s == '{') { |
| 2738 | const char *start = s+1; |
| 2739 | /* look for the closing brace */ |
| 2740 | while (*s != '}' && s < end) |
| 2741 | s++; |
| 2742 | if (s > start && s < end && *s == '}') { |
| 2743 | /* found a name. look it up in the unicode database */ |
| 2744 | message = "unknown Unicode character name"; |
| 2745 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 2746 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2747 | goto store; |
| 2748 | } |
| 2749 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2750 | endinpos = s-starts; |
| 2751 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2752 | if (unicode_decode_call_errorhandler( |
| 2753 | errors, &errorHandler, |
| 2754 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2755 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2756 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2757 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2758 | break; |
| 2759 | |
| 2760 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2761 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2762 | message = "\\ at end of string"; |
| 2763 | s--; |
| 2764 | endinpos = s-starts; |
| 2765 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2766 | if (unicode_decode_call_errorhandler( |
| 2767 | errors, &errorHandler, |
| 2768 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2769 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2770 | (PyObject **)&v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2771 | goto onError; |
| 2772 | } |
| 2773 | else { |
| 2774 | *p++ = '\\'; |
| 2775 | *p++ = (unsigned char)s[-1]; |
| 2776 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2777 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2778 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2779 | nextByte: |
| 2780 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2781 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2782 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2783 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 2784 | Py_XDECREF(errorHandler); |
| 2785 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2786 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2787 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2788 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2789 | PyErr_SetString( |
| 2790 | PyExc_UnicodeError, |
| 2791 | "\\N escapes not supported (can't load unicodedata module)" |
| 2792 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2793 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2794 | Py_XDECREF(errorHandler); |
| 2795 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 2796 | return NULL; |
| 2797 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2798 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2799 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2800 | Py_XDECREF(errorHandler); |
| 2801 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2802 | return NULL; |
| 2803 | } |
| 2804 | |
| 2805 | /* Return a Unicode-Escape string version of the Unicode object. |
| 2806 | |
| 2807 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 2808 | appropriate. |
| 2809 | |
| 2810 | */ |
| 2811 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2812 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
| 2813 | Py_ssize_t size, |
| 2814 | Py_UNICODE ch) |
| 2815 | { |
| 2816 | /* like wcschr, but doesn't stop at NULL characters */ |
| 2817 | |
| 2818 | while (size-- > 0) { |
| 2819 | if (*s == ch) |
| 2820 | return s; |
| 2821 | s++; |
| 2822 | } |
| 2823 | |
| 2824 | return NULL; |
| 2825 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 2826 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2827 | static const char *hexdigits = "0123456789abcdef"; |
| 2828 | |
| 2829 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 2830 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2831 | { |
| 2832 | PyObject *repr; |
| 2833 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2834 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2835 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 2836 | better to choose a different scheme. Perhaps scan the |
| 2837 | first N-chars of the string and allocate based on that size. |
| 2838 | */ |
| 2839 | /* Initial allocation is based on the longest-possible unichr |
| 2840 | escape. |
| 2841 | |
| 2842 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 2843 | unichr, so in this case it's the longest unichr escape. In |
| 2844 | narrow (UTF-16) builds this is five chars per source unichr |
| 2845 | since there are two unichrs in the surrogate pair, so in narrow |
| 2846 | (UTF-16) builds it's not the longest unichr escape. |
| 2847 | |
| 2848 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 2849 | so in the narrow (UTF-16) build case it's the longest unichr |
| 2850 | escape. |
| 2851 | */ |
| 2852 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2853 | repr = PyBytes_FromStringAndSize(NULL, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2854 | #ifdef Py_UNICODE_WIDE |
| 2855 | + 10*size |
| 2856 | #else |
| 2857 | + 6*size |
| 2858 | #endif |
| 2859 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2860 | if (repr == NULL) |
| 2861 | return NULL; |
| 2862 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2863 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2864 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2865 | while (size-- > 0) { |
| 2866 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2867 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2868 | /* Escape backslashes */ |
| 2869 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2870 | *p++ = '\\'; |
| 2871 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2872 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2873 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2874 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 2875 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2876 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 2877 | else if (ch >= 0x10000) { |
| 2878 | *p++ = '\\'; |
| 2879 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2880 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 2881 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 2882 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 2883 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 2884 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 2885 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 2886 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 2887 | *p++ = hexdigits[ch & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2888 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2889 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2890 | #else |
| 2891 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2892 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 2893 | Py_UNICODE ch2; |
| 2894 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2895 | |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2896 | ch2 = *s++; |
| 2897 | size--; |
| 2898 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 2899 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 2900 | *p++ = '\\'; |
| 2901 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2902 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 2903 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 2904 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 2905 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 2906 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 2907 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 2908 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 2909 | *p++ = hexdigits[ucs & 0x0000000F]; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2910 | continue; |
| 2911 | } |
| 2912 | /* Fall through: isolated surrogates are copied as-is */ |
| 2913 | s--; |
| 2914 | size++; |
| 2915 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2916 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2917 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2918 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2919 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2920 | *p++ = '\\'; |
| 2921 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2922 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 2923 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 2924 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 2925 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2926 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2927 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2928 | /* Map special whitespace to '\t', \n', '\r' */ |
| 2929 | else if (ch == '\t') { |
| 2930 | *p++ = '\\'; |
| 2931 | *p++ = 't'; |
| 2932 | } |
| 2933 | else if (ch == '\n') { |
| 2934 | *p++ = '\\'; |
| 2935 | *p++ = 'n'; |
| 2936 | } |
| 2937 | else if (ch == '\r') { |
| 2938 | *p++ = '\\'; |
| 2939 | *p++ = 'r'; |
| 2940 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2941 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2942 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 2943 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2944 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2945 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2946 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 2947 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2948 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2949 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2950 | /* Copy everything else as-is */ |
| 2951 | else |
| 2952 | *p++ = (char) ch; |
| 2953 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2954 | |
| 2955 | *p = '\0'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2956 | if (PyBytes_Resize(repr, p - PyBytes_AS_STRING(repr))) { |
| 2957 | Py_DECREF(repr); |
| 2958 | return NULL; |
| 2959 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2960 | return repr; |
| 2961 | } |
| 2962 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2963 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 2964 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2965 | PyObject *s, *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2966 | if (!PyUnicode_Check(unicode)) { |
| 2967 | PyErr_BadArgument(); |
| 2968 | return NULL; |
| 2969 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2970 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2971 | PyUnicode_GET_SIZE(unicode)); |
| 2972 | |
| 2973 | if (!s) |
| 2974 | return NULL; |
| 2975 | result = PyString_FromStringAndSize(PyBytes_AS_STRING(s), |
| 2976 | PyBytes_GET_SIZE(s)); |
| 2977 | Py_DECREF(s); |
| 2978 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2979 | } |
| 2980 | |
| 2981 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 2982 | |
| 2983 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2984 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2985 | const char *errors) |
| 2986 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2987 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2988 | Py_ssize_t startinpos; |
| 2989 | Py_ssize_t endinpos; |
| 2990 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2991 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2992 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2993 | const char *end; |
| 2994 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2995 | PyObject *errorHandler = NULL; |
| 2996 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2997 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2998 | /* Escaped strings will always be longer than the resulting |
| 2999 | 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] | 3000 | length after conversion to the true value. (But decoding error |
| 3001 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3002 | v = _PyUnicode_New(size); |
| 3003 | if (v == NULL) |
| 3004 | goto onError; |
| 3005 | if (size == 0) |
| 3006 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3007 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3008 | end = s + size; |
| 3009 | while (s < end) { |
| 3010 | unsigned char c; |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 3011 | Py_UCS4 x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3012 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3013 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3014 | |
| 3015 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3016 | if (*s != '\\') { |
| 3017 | *p++ = (unsigned char)*s++; |
| 3018 | continue; |
| 3019 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3020 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3021 | |
| 3022 | /* \u-escapes are only interpreted iff the number of leading |
| 3023 | backslashes if odd */ |
| 3024 | bs = s; |
| 3025 | for (;s < end;) { |
| 3026 | if (*s != '\\') |
| 3027 | break; |
| 3028 | *p++ = (unsigned char)*s++; |
| 3029 | } |
| 3030 | if (((s - bs) & 1) == 0 || |
| 3031 | s >= end || |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3032 | (*s != 'u' && *s != 'U')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3033 | continue; |
| 3034 | } |
| 3035 | p--; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3036 | count = *s=='u' ? 4 : 8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3037 | s++; |
| 3038 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3039 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3040 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3041 | for (x = 0, i = 0; i < count; ++i, ++s) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3042 | c = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3043 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3044 | endinpos = s-starts; |
| 3045 | if (unicode_decode_call_errorhandler( |
| 3046 | errors, &errorHandler, |
| 3047 | "rawunicodeescape", "truncated \\uXXXX", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3048 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3049 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3050 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3051 | goto nextByte; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3052 | } |
| 3053 | x = (x<<4) & ~0xF; |
| 3054 | if (c >= '0' && c <= '9') |
| 3055 | x += c - '0'; |
| 3056 | else if (c >= 'a' && c <= 'f') |
| 3057 | x += 10 + c - 'a'; |
| 3058 | else |
| 3059 | x += 10 + c - 'A'; |
| 3060 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3061 | #ifndef Py_UNICODE_WIDE |
| 3062 | if (x > 0x10000) { |
| 3063 | if (unicode_decode_call_errorhandler( |
| 3064 | errors, &errorHandler, |
| 3065 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3066 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3067 | (PyObject **)&v, &outpos, &p)) |
| 3068 | goto onError; |
| 3069 | } |
| 3070 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3071 | *p++ = x; |
| 3072 | nextByte: |
| 3073 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3074 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3075 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3076 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3077 | Py_XDECREF(errorHandler); |
| 3078 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3079 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3080 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3081 | onError: |
| 3082 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3083 | Py_XDECREF(errorHandler); |
| 3084 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3085 | return NULL; |
| 3086 | } |
| 3087 | |
| 3088 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3089 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3090 | { |
| 3091 | PyObject *repr; |
| 3092 | char *p; |
| 3093 | char *q; |
| 3094 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3095 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3096 | repr = PyBytes_FromStringAndSize(NULL, 10 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3097 | #else |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3098 | repr = PyBytes_FromStringAndSize(NULL, 6 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3099 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3100 | if (repr == NULL) |
| 3101 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3102 | if (size == 0) |
| 3103 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3104 | |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3105 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3106 | while (size-- > 0) { |
| 3107 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3108 | #ifdef Py_UNICODE_WIDE |
| 3109 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 3110 | if (ch >= 0x10000) { |
| 3111 | *p++ = '\\'; |
| 3112 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3113 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 3114 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 3115 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 3116 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 3117 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3118 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3119 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3120 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3121 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3122 | else |
| 3123 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3124 | /* Map 16-bit characters to '\uxxxx' */ |
| 3125 | if (ch >= 256) { |
| 3126 | *p++ = '\\'; |
| 3127 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3128 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3129 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3130 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3131 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3132 | } |
| 3133 | /* Copy everything else as-is */ |
| 3134 | else |
| 3135 | *p++ = (char) ch; |
| 3136 | } |
| 3137 | *p = '\0'; |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3138 | if (PyBytes_Resize(repr, p - q)) { |
| 3139 | Py_DECREF(repr); |
| 3140 | return NULL; |
| 3141 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3142 | return repr; |
| 3143 | } |
| 3144 | |
| 3145 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 3146 | { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3147 | PyObject *s, *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3148 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3149 | PyErr_BadArgument(); |
| 3150 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3151 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3152 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3153 | PyUnicode_GET_SIZE(unicode)); |
| 3154 | |
| 3155 | if (!s) |
| 3156 | return NULL; |
| 3157 | result = PyString_FromStringAndSize(PyBytes_AS_STRING(s), |
| 3158 | PyBytes_GET_SIZE(s)); |
| 3159 | Py_DECREF(s); |
| 3160 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3161 | } |
| 3162 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3163 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 3164 | |
| 3165 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3166 | Py_ssize_t size, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3167 | const char *errors) |
| 3168 | { |
| 3169 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3170 | Py_ssize_t startinpos; |
| 3171 | Py_ssize_t endinpos; |
| 3172 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3173 | PyUnicodeObject *v; |
| 3174 | Py_UNICODE *p; |
| 3175 | const char *end; |
| 3176 | const char *reason; |
| 3177 | PyObject *errorHandler = NULL; |
| 3178 | PyObject *exc = NULL; |
| 3179 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 3180 | #ifdef Py_UNICODE_WIDE |
| 3181 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 3182 | #endif |
| 3183 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3184 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3185 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 3186 | if (v == NULL) |
| 3187 | goto onError; |
| 3188 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
| 3189 | return (PyObject *)v; |
| 3190 | p = PyUnicode_AS_UNICODE(v); |
| 3191 | end = s + size; |
| 3192 | |
| 3193 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3194 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3195 | /* We have to sanity check the raw data, otherwise doom looms for |
| 3196 | some malformed UCS-4 data. */ |
| 3197 | if ( |
| 3198 | #ifdef Py_UNICODE_WIDE |
| 3199 | *p > unimax || *p < 0 || |
| 3200 | #endif |
| 3201 | end-s < Py_UNICODE_SIZE |
| 3202 | ) |
| 3203 | { |
| 3204 | startinpos = s - starts; |
| 3205 | if (end-s < Py_UNICODE_SIZE) { |
| 3206 | endinpos = end-starts; |
| 3207 | reason = "truncated input"; |
| 3208 | } |
| 3209 | else { |
| 3210 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 3211 | reason = "illegal code point (> 0x10FFFF)"; |
| 3212 | } |
| 3213 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 3214 | if (unicode_decode_call_errorhandler( |
| 3215 | errors, &errorHandler, |
| 3216 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3217 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3218 | (PyObject **)&v, &outpos, &p)) { |
| 3219 | goto onError; |
| 3220 | } |
| 3221 | } |
| 3222 | else { |
| 3223 | p++; |
| 3224 | s += Py_UNICODE_SIZE; |
| 3225 | } |
| 3226 | } |
| 3227 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3228 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3229 | goto onError; |
| 3230 | Py_XDECREF(errorHandler); |
| 3231 | Py_XDECREF(exc); |
| 3232 | return (PyObject *)v; |
| 3233 | |
| 3234 | onError: |
| 3235 | Py_XDECREF(v); |
| 3236 | Py_XDECREF(errorHandler); |
| 3237 | Py_XDECREF(exc); |
| 3238 | return NULL; |
| 3239 | } |
| 3240 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3241 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 3242 | |
| 3243 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3244 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3245 | const char *errors) |
| 3246 | { |
| 3247 | PyUnicodeObject *v; |
| 3248 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3249 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3250 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3251 | if (size == 1) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3252 | Py_UNICODE r = *(unsigned char*)s; |
| 3253 | return PyUnicode_FromUnicode(&r, 1); |
| 3254 | } |
| 3255 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3256 | v = _PyUnicode_New(size); |
| 3257 | if (v == NULL) |
| 3258 | goto onError; |
| 3259 | if (size == 0) |
| 3260 | return (PyObject *)v; |
| 3261 | p = PyUnicode_AS_UNICODE(v); |
| 3262 | while (size-- > 0) |
| 3263 | *p++ = (unsigned char)*s++; |
| 3264 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3265 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3266 | onError: |
| 3267 | Py_XDECREF(v); |
| 3268 | return NULL; |
| 3269 | } |
| 3270 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3271 | /* create or adjust a UnicodeEncodeError */ |
| 3272 | static void make_encode_exception(PyObject **exceptionObject, |
| 3273 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3274 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3275 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3276 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3277 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3278 | if (*exceptionObject == NULL) { |
| 3279 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 3280 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3281 | } |
| 3282 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3283 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 3284 | goto onError; |
| 3285 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 3286 | goto onError; |
| 3287 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 3288 | goto onError; |
| 3289 | return; |
| 3290 | onError: |
| 3291 | Py_DECREF(*exceptionObject); |
| 3292 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3293 | } |
| 3294 | } |
| 3295 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3296 | /* raises a UnicodeEncodeError */ |
| 3297 | static void raise_encode_exception(PyObject **exceptionObject, |
| 3298 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3299 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3300 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3301 | const char *reason) |
| 3302 | { |
| 3303 | make_encode_exception(exceptionObject, |
| 3304 | encoding, unicode, size, startpos, endpos, reason); |
| 3305 | if (*exceptionObject != NULL) |
| 3306 | PyCodec_StrictErrors(*exceptionObject); |
| 3307 | } |
| 3308 | |
| 3309 | /* error handling callback helper: |
| 3310 | build arguments, call the callback and check the arguments, |
| 3311 | put the result into newpos and return the replacement string, which |
| 3312 | has to be freed by the caller */ |
| 3313 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 3314 | PyObject **errorHandler, |
| 3315 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3316 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 3317 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3318 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3319 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3320 | 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] | 3321 | |
| 3322 | PyObject *restuple; |
| 3323 | PyObject *resunicode; |
| 3324 | |
| 3325 | if (*errorHandler == NULL) { |
| 3326 | *errorHandler = PyCodec_LookupError(errors); |
| 3327 | if (*errorHandler == NULL) |
| 3328 | return NULL; |
| 3329 | } |
| 3330 | |
| 3331 | make_encode_exception(exceptionObject, |
| 3332 | encoding, unicode, size, startpos, endpos, reason); |
| 3333 | if (*exceptionObject == NULL) |
| 3334 | return NULL; |
| 3335 | |
| 3336 | restuple = PyObject_CallFunctionObjArgs( |
| 3337 | *errorHandler, *exceptionObject, NULL); |
| 3338 | if (restuple == NULL) |
| 3339 | return NULL; |
| 3340 | if (!PyTuple_Check(restuple)) { |
| 3341 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 3342 | Py_DECREF(restuple); |
| 3343 | return NULL; |
| 3344 | } |
| 3345 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 3346 | &resunicode, newpos)) { |
| 3347 | Py_DECREF(restuple); |
| 3348 | return NULL; |
| 3349 | } |
| 3350 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3351 | *newpos = size+*newpos; |
| 3352 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 3353 | 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] | 3354 | Py_DECREF(restuple); |
| 3355 | return NULL; |
| 3356 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3357 | Py_INCREF(resunicode); |
| 3358 | Py_DECREF(restuple); |
| 3359 | return resunicode; |
| 3360 | } |
| 3361 | |
| 3362 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3363 | Py_ssize_t size, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3364 | const char *errors, |
| 3365 | int limit) |
| 3366 | { |
| 3367 | /* output object */ |
| 3368 | PyObject *res; |
| 3369 | /* pointers to the beginning and end+1 of input */ |
| 3370 | const Py_UNICODE *startp = p; |
| 3371 | const Py_UNICODE *endp = p + size; |
| 3372 | /* pointer to the beginning of the unencodable characters */ |
| 3373 | /* const Py_UNICODE *badp = NULL; */ |
| 3374 | /* pointer into the output */ |
| 3375 | char *str; |
| 3376 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3377 | Py_ssize_t respos = 0; |
| 3378 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3379 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 3380 | 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] | 3381 | PyObject *errorHandler = NULL; |
| 3382 | PyObject *exc = NULL; |
| 3383 | /* the following variable is used for caching string comparisons |
| 3384 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 3385 | int known_errorHandler = -1; |
| 3386 | |
| 3387 | /* allocate enough for a simple encoding without |
| 3388 | replacements, if we need more, we'll resize */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3389 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3390 | if (res == NULL) |
| 3391 | goto onError; |
| 3392 | if (size == 0) |
| 3393 | return res; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3394 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3395 | ressize = size; |
| 3396 | |
| 3397 | while (p<endp) { |
| 3398 | Py_UNICODE c = *p; |
| 3399 | |
| 3400 | /* can we encode this? */ |
| 3401 | if (c<limit) { |
| 3402 | /* no overflow check, because we know that the space is enough */ |
| 3403 | *str++ = (char)c; |
| 3404 | ++p; |
| 3405 | } |
| 3406 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3407 | Py_ssize_t unicodepos = p-startp; |
| 3408 | Py_ssize_t requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3409 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3410 | Py_ssize_t repsize; |
| 3411 | Py_ssize_t newpos; |
| 3412 | Py_ssize_t respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3413 | Py_UNICODE *uni2; |
| 3414 | /* startpos for collecting unencodable chars */ |
| 3415 | const Py_UNICODE *collstart = p; |
| 3416 | const Py_UNICODE *collend = p; |
| 3417 | /* find all unecodable characters */ |
| 3418 | while ((collend < endp) && ((*collend)>=limit)) |
| 3419 | ++collend; |
| 3420 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 3421 | if (known_errorHandler==-1) { |
| 3422 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3423 | known_errorHandler = 1; |
| 3424 | else if (!strcmp(errors, "replace")) |
| 3425 | known_errorHandler = 2; |
| 3426 | else if (!strcmp(errors, "ignore")) |
| 3427 | known_errorHandler = 3; |
| 3428 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3429 | known_errorHandler = 4; |
| 3430 | else |
| 3431 | known_errorHandler = 0; |
| 3432 | } |
| 3433 | switch (known_errorHandler) { |
| 3434 | case 1: /* strict */ |
| 3435 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 3436 | goto onError; |
| 3437 | case 2: /* replace */ |
| 3438 | while (collstart++<collend) |
| 3439 | *str++ = '?'; /* fall through */ |
| 3440 | case 3: /* ignore */ |
| 3441 | p = collend; |
| 3442 | break; |
| 3443 | case 4: /* xmlcharrefreplace */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3444 | respos = str - PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3445 | /* determine replacement size (temporarily (mis)uses p) */ |
| 3446 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 3447 | if (*p<10) |
| 3448 | repsize += 2+1+1; |
| 3449 | else if (*p<100) |
| 3450 | repsize += 2+2+1; |
| 3451 | else if (*p<1000) |
| 3452 | repsize += 2+3+1; |
| 3453 | else if (*p<10000) |
| 3454 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 3455 | #ifndef Py_UNICODE_WIDE |
| 3456 | else |
| 3457 | repsize += 2+5+1; |
| 3458 | #else |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3459 | else if (*p<100000) |
| 3460 | repsize += 2+5+1; |
| 3461 | else if (*p<1000000) |
| 3462 | repsize += 2+6+1; |
| 3463 | else |
| 3464 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3465 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3466 | } |
| 3467 | requiredsize = respos+repsize+(endp-collend); |
| 3468 | if (requiredsize > ressize) { |
| 3469 | if (requiredsize<2*ressize) |
| 3470 | requiredsize = 2*ressize; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3471 | if (PyBytes_Resize(res, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3472 | goto onError; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3473 | str = PyBytes_AS_STRING(res) + respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3474 | ressize = requiredsize; |
| 3475 | } |
| 3476 | /* generate replacement (temporarily (mis)uses p) */ |
| 3477 | for (p = collstart; p < collend; ++p) { |
| 3478 | str += sprintf(str, "&#%d;", (int)*p); |
| 3479 | } |
| 3480 | p = collend; |
| 3481 | break; |
| 3482 | default: |
| 3483 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 3484 | encoding, reason, startp, size, &exc, |
| 3485 | collstart-startp, collend-startp, &newpos); |
| 3486 | if (repunicode == NULL) |
| 3487 | goto onError; |
| 3488 | /* need more space? (at least enough for what we |
| 3489 | have+the replacement+the rest of the string, so |
| 3490 | we won't have to check space for encodable characters) */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3491 | respos = str - PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3492 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3493 | requiredsize = respos+repsize+(endp-collend); |
| 3494 | if (requiredsize > ressize) { |
| 3495 | if (requiredsize<2*ressize) |
| 3496 | requiredsize = 2*ressize; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3497 | if (PyBytes_Resize(res, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3498 | Py_DECREF(repunicode); |
| 3499 | goto onError; |
| 3500 | } |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3501 | str = PyBytes_AS_STRING(res) + respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3502 | ressize = requiredsize; |
| 3503 | } |
| 3504 | /* check if there is anything unencodable in the replacement |
| 3505 | and copy it to the output */ |
| 3506 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 3507 | c = *uni2; |
| 3508 | if (c >= limit) { |
| 3509 | raise_encode_exception(&exc, encoding, startp, size, |
| 3510 | unicodepos, unicodepos+1, reason); |
| 3511 | Py_DECREF(repunicode); |
| 3512 | goto onError; |
| 3513 | } |
| 3514 | *str = (char)c; |
| 3515 | } |
| 3516 | p = startp + newpos; |
| 3517 | Py_DECREF(repunicode); |
| 3518 | } |
| 3519 | } |
| 3520 | } |
| 3521 | /* Resize if we allocated to much */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3522 | respos = str - PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3523 | if (respos<ressize) |
| 3524 | /* If this falls res will be NULL */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3525 | PyBytes_Resize(res, respos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3526 | Py_XDECREF(errorHandler); |
| 3527 | Py_XDECREF(exc); |
| 3528 | return res; |
| 3529 | |
| 3530 | onError: |
| 3531 | Py_XDECREF(res); |
| 3532 | Py_XDECREF(errorHandler); |
| 3533 | Py_XDECREF(exc); |
| 3534 | return NULL; |
| 3535 | } |
| 3536 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3537 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3538 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3539 | const char *errors) |
| 3540 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3541 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3542 | } |
| 3543 | |
| 3544 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 3545 | { |
| 3546 | if (!PyUnicode_Check(unicode)) { |
| 3547 | PyErr_BadArgument(); |
| 3548 | return NULL; |
| 3549 | } |
| 3550 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 3551 | PyUnicode_GET_SIZE(unicode), |
| 3552 | NULL); |
| 3553 | } |
| 3554 | |
| 3555 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 3556 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3557 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3558 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3559 | const char *errors) |
| 3560 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3561 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3562 | PyUnicodeObject *v; |
| 3563 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3564 | Py_ssize_t startinpos; |
| 3565 | Py_ssize_t endinpos; |
| 3566 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3567 | const char *e; |
| 3568 | PyObject *errorHandler = NULL; |
| 3569 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3570 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3571 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3572 | if (size == 1 && *(unsigned char*)s < 128) { |
| 3573 | Py_UNICODE r = *(unsigned char*)s; |
| 3574 | return PyUnicode_FromUnicode(&r, 1); |
| 3575 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3576 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3577 | v = _PyUnicode_New(size); |
| 3578 | if (v == NULL) |
| 3579 | goto onError; |
| 3580 | if (size == 0) |
| 3581 | return (PyObject *)v; |
| 3582 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3583 | e = s + size; |
| 3584 | while (s < e) { |
| 3585 | register unsigned char c = (unsigned char)*s; |
| 3586 | if (c < 128) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3587 | *p++ = c; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3588 | ++s; |
| 3589 | } |
| 3590 | else { |
| 3591 | startinpos = s-starts; |
| 3592 | endinpos = startinpos + 1; |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 3593 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3594 | if (unicode_decode_call_errorhandler( |
| 3595 | errors, &errorHandler, |
| 3596 | "ascii", "ordinal not in range(128)", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3597 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3598 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3599 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3600 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3601 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3602 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3603 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3604 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3605 | Py_XDECREF(errorHandler); |
| 3606 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3607 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3608 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3609 | onError: |
| 3610 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3611 | Py_XDECREF(errorHandler); |
| 3612 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3613 | return NULL; |
| 3614 | } |
| 3615 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3616 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3617 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3618 | const char *errors) |
| 3619 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3620 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3621 | } |
| 3622 | |
| 3623 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 3624 | { |
| 3625 | if (!PyUnicode_Check(unicode)) { |
| 3626 | PyErr_BadArgument(); |
| 3627 | return NULL; |
| 3628 | } |
| 3629 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 3630 | PyUnicode_GET_SIZE(unicode), |
| 3631 | NULL); |
| 3632 | } |
| 3633 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3634 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3635 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3636 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3637 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3638 | #if SIZEOF_INT < SIZEOF_SSIZE_T |
| 3639 | #define NEED_RETRY |
| 3640 | #endif |
| 3641 | |
| 3642 | /* XXX This code is limited to "true" double-byte encodings, as |
| 3643 | a) it assumes an incomplete character consists of a single byte, and |
| 3644 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
| 3645 | encodings, see IsDBCSLeadByteEx documentation. */ |
| 3646 | |
| 3647 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 3648 | { |
| 3649 | const char *curr = s + offset; |
| 3650 | |
| 3651 | if (IsDBCSLeadByte(*curr)) { |
| 3652 | const char *prev = CharPrev(s, curr); |
| 3653 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
| 3654 | } |
| 3655 | return 0; |
| 3656 | } |
| 3657 | |
| 3658 | /* |
| 3659 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 3660 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 3661 | */ |
| 3662 | static int decode_mbcs(PyUnicodeObject **v, |
| 3663 | const char *s, /* MBCS string */ |
| 3664 | int size, /* sizeof MBCS string */ |
| 3665 | int final) |
| 3666 | { |
| 3667 | Py_UNICODE *p; |
| 3668 | Py_ssize_t n = 0; |
| 3669 | int usize = 0; |
| 3670 | |
| 3671 | assert(size >= 0); |
| 3672 | |
| 3673 | /* Skip trailing lead-byte unless 'final' is set */ |
| 3674 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
| 3675 | --size; |
| 3676 | |
| 3677 | /* First get the size of the result */ |
| 3678 | if (size > 0) { |
| 3679 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 3680 | if (usize == 0) { |
| 3681 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3682 | return -1; |
| 3683 | } |
| 3684 | } |
| 3685 | |
| 3686 | if (*v == NULL) { |
| 3687 | /* Create unicode object */ |
| 3688 | *v = _PyUnicode_New(usize); |
| 3689 | if (*v == NULL) |
| 3690 | return -1; |
| 3691 | } |
| 3692 | else { |
| 3693 | /* Extend unicode object */ |
| 3694 | n = PyUnicode_GET_SIZE(*v); |
| 3695 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 3696 | return -1; |
| 3697 | } |
| 3698 | |
| 3699 | /* Do the conversion */ |
| 3700 | if (size > 0) { |
| 3701 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 3702 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 3703 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3704 | return -1; |
| 3705 | } |
| 3706 | } |
| 3707 | |
| 3708 | return size; |
| 3709 | } |
| 3710 | |
| 3711 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
| 3712 | Py_ssize_t size, |
| 3713 | const char *errors, |
| 3714 | Py_ssize_t *consumed) |
| 3715 | { |
| 3716 | PyUnicodeObject *v = NULL; |
| 3717 | int done; |
| 3718 | |
| 3719 | if (consumed) |
| 3720 | *consumed = 0; |
| 3721 | |
| 3722 | #ifdef NEED_RETRY |
| 3723 | retry: |
| 3724 | if (size > INT_MAX) |
| 3725 | done = decode_mbcs(&v, s, INT_MAX, 0); |
| 3726 | else |
| 3727 | #endif |
| 3728 | done = decode_mbcs(&v, s, (int)size, !consumed); |
| 3729 | |
| 3730 | if (done < 0) { |
| 3731 | Py_XDECREF(v); |
| 3732 | return NULL; |
| 3733 | } |
| 3734 | |
| 3735 | if (consumed) |
| 3736 | *consumed += done; |
| 3737 | |
| 3738 | #ifdef NEED_RETRY |
| 3739 | if (size > INT_MAX) { |
| 3740 | s += done; |
| 3741 | size -= done; |
| 3742 | goto retry; |
| 3743 | } |
| 3744 | #endif |
| 3745 | |
| 3746 | return (PyObject *)v; |
| 3747 | } |
| 3748 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3749 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3750 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3751 | const char *errors) |
| 3752 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3753 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 3754 | } |
| 3755 | |
| 3756 | /* |
| 3757 | * Convert unicode into string object (MBCS). |
| 3758 | * Returns 0 if succeed, -1 otherwise. |
| 3759 | */ |
| 3760 | static int encode_mbcs(PyObject **repr, |
| 3761 | const Py_UNICODE *p, /* unicode */ |
| 3762 | int size) /* size of unicode */ |
| 3763 | { |
| 3764 | int mbcssize = 0; |
| 3765 | Py_ssize_t n = 0; |
| 3766 | |
| 3767 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3768 | |
| 3769 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3770 | if (size > 0) { |
| 3771 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 3772 | if (mbcssize == 0) { |
| 3773 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3774 | return -1; |
| 3775 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3776 | } |
| 3777 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3778 | if (*repr == NULL) { |
| 3779 | /* Create string object */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3780 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3781 | if (*repr == NULL) |
| 3782 | return -1; |
| 3783 | } |
| 3784 | else { |
| 3785 | /* Extend string object */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3786 | n = PyBytes_Size(*repr); |
| 3787 | if (PyBytes_Resize(*repr, n + mbcssize) < 0) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3788 | return -1; |
| 3789 | } |
| 3790 | |
| 3791 | /* Do the conversion */ |
| 3792 | if (size > 0) { |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3793 | char *s = PyBytes_AS_STRING(*repr) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3794 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 3795 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3796 | return -1; |
| 3797 | } |
| 3798 | } |
| 3799 | |
| 3800 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3801 | } |
| 3802 | |
| 3803 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3804 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3805 | const char *errors) |
| 3806 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3807 | PyObject *repr = NULL; |
| 3808 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 3809 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3810 | #ifdef NEED_RETRY |
| 3811 | retry: |
| 3812 | if (size > INT_MAX) |
| 3813 | ret = encode_mbcs(&repr, p, INT_MAX); |
| 3814 | else |
| 3815 | #endif |
| 3816 | ret = encode_mbcs(&repr, p, (int)size); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3817 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3818 | if (ret < 0) { |
| 3819 | Py_XDECREF(repr); |
| 3820 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3821 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3822 | |
| 3823 | #ifdef NEED_RETRY |
| 3824 | if (size > INT_MAX) { |
| 3825 | p += INT_MAX; |
| 3826 | size -= INT_MAX; |
| 3827 | goto retry; |
| 3828 | } |
| 3829 | #endif |
| 3830 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3831 | return repr; |
| 3832 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3833 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3834 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 3835 | { |
| 3836 | if (!PyUnicode_Check(unicode)) { |
| 3837 | PyErr_BadArgument(); |
| 3838 | return NULL; |
| 3839 | } |
| 3840 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 3841 | PyUnicode_GET_SIZE(unicode), |
| 3842 | NULL); |
| 3843 | } |
| 3844 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3845 | #undef NEED_RETRY |
| 3846 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3847 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3848 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3849 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 3850 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3851 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3852 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3853 | PyObject *mapping, |
| 3854 | const char *errors) |
| 3855 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3856 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3857 | Py_ssize_t startinpos; |
| 3858 | Py_ssize_t endinpos; |
| 3859 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3860 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3861 | PyUnicodeObject *v; |
| 3862 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3863 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3864 | PyObject *errorHandler = NULL; |
| 3865 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3866 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3867 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3868 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3869 | /* Default to Latin-1 */ |
| 3870 | if (mapping == NULL) |
| 3871 | return PyUnicode_DecodeLatin1(s, size, errors); |
| 3872 | |
| 3873 | v = _PyUnicode_New(size); |
| 3874 | if (v == NULL) |
| 3875 | goto onError; |
| 3876 | if (size == 0) |
| 3877 | return (PyObject *)v; |
| 3878 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3879 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3880 | if (PyUnicode_CheckExact(mapping)) { |
| 3881 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 3882 | maplen = PyUnicode_GET_SIZE(mapping); |
| 3883 | while (s < e) { |
| 3884 | unsigned char ch = *s; |
| 3885 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3886 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3887 | if (ch < maplen) |
| 3888 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3889 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3890 | if (x == 0xfffe) { |
| 3891 | /* undefined mapping */ |
| 3892 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3893 | startinpos = s-starts; |
| 3894 | endinpos = startinpos+1; |
| 3895 | if (unicode_decode_call_errorhandler( |
| 3896 | errors, &errorHandler, |
| 3897 | "charmap", "character maps to <undefined>", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3898 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3899 | (PyObject **)&v, &outpos, &p)) { |
| 3900 | goto onError; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 3901 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3902 | continue; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 3903 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3904 | *p++ = x; |
| 3905 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3906 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3907 | } |
| 3908 | else { |
| 3909 | while (s < e) { |
| 3910 | unsigned char ch = *s; |
| 3911 | PyObject *w, *x; |
| 3912 | |
| 3913 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 3914 | w = PyInt_FromLong((long)ch); |
| 3915 | if (w == NULL) |
| 3916 | goto onError; |
| 3917 | x = PyObject_GetItem(mapping, w); |
| 3918 | Py_DECREF(w); |
| 3919 | if (x == NULL) { |
| 3920 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3921 | /* No mapping found means: mapping is undefined. */ |
| 3922 | PyErr_Clear(); |
| 3923 | x = Py_None; |
| 3924 | Py_INCREF(x); |
| 3925 | } else |
| 3926 | goto onError; |
| 3927 | } |
| 3928 | |
| 3929 | /* Apply mapping */ |
| 3930 | if (PyInt_Check(x)) { |
| 3931 | long value = PyInt_AS_LONG(x); |
| 3932 | if (value < 0 || value > 65535) { |
| 3933 | PyErr_SetString(PyExc_TypeError, |
| 3934 | "character mapping must be in range(65536)"); |
| 3935 | Py_DECREF(x); |
| 3936 | goto onError; |
| 3937 | } |
| 3938 | *p++ = (Py_UNICODE)value; |
| 3939 | } |
| 3940 | else if (x == Py_None) { |
| 3941 | /* undefined mapping */ |
| 3942 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3943 | startinpos = s-starts; |
| 3944 | endinpos = startinpos+1; |
| 3945 | if (unicode_decode_call_errorhandler( |
| 3946 | errors, &errorHandler, |
| 3947 | "charmap", "character maps to <undefined>", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3948 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3949 | (PyObject **)&v, &outpos, &p)) { |
| 3950 | Py_DECREF(x); |
| 3951 | goto onError; |
| 3952 | } |
Walter Dörwald | d4fff17 | 2005-11-28 22:15:56 +0000 | [diff] [blame] | 3953 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3954 | continue; |
| 3955 | } |
| 3956 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3957 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3958 | |
| 3959 | if (targetsize == 1) |
| 3960 | /* 1-1 mapping */ |
| 3961 | *p++ = *PyUnicode_AS_UNICODE(x); |
| 3962 | |
| 3963 | else if (targetsize > 1) { |
| 3964 | /* 1-n mapping */ |
| 3965 | if (targetsize > extrachars) { |
| 3966 | /* resize first */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3967 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 3968 | Py_ssize_t needed = (targetsize - extrachars) + \ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3969 | (targetsize << 2); |
| 3970 | extrachars += needed; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3971 | /* XXX overflow detection missing */ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3972 | if (_PyUnicode_Resize(&v, |
| 3973 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 3974 | Py_DECREF(x); |
| 3975 | goto onError; |
| 3976 | } |
| 3977 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 3978 | } |
| 3979 | Py_UNICODE_COPY(p, |
| 3980 | PyUnicode_AS_UNICODE(x), |
| 3981 | targetsize); |
| 3982 | p += targetsize; |
| 3983 | extrachars -= targetsize; |
| 3984 | } |
| 3985 | /* 1-0 mapping: skip the character */ |
| 3986 | } |
| 3987 | else { |
| 3988 | /* wrong return value */ |
| 3989 | PyErr_SetString(PyExc_TypeError, |
| 3990 | "character mapping must return integer, None or unicode"); |
| 3991 | Py_DECREF(x); |
| 3992 | goto onError; |
| 3993 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3994 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3995 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3996 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3997 | } |
| 3998 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3999 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4000 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4001 | Py_XDECREF(errorHandler); |
| 4002 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4003 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4004 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4005 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4006 | Py_XDECREF(errorHandler); |
| 4007 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4008 | Py_XDECREF(v); |
| 4009 | return NULL; |
| 4010 | } |
| 4011 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4012 | /* Charmap encoding: the lookup table */ |
| 4013 | |
| 4014 | struct encoding_map{ |
| 4015 | PyObject_HEAD |
| 4016 | unsigned char level1[32]; |
| 4017 | int count2, count3; |
| 4018 | unsigned char level23[1]; |
| 4019 | }; |
| 4020 | |
| 4021 | static PyObject* |
| 4022 | encoding_map_size(PyObject *obj, PyObject* args) |
| 4023 | { |
| 4024 | struct encoding_map *map = (struct encoding_map*)obj; |
| 4025 | return PyInt_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
| 4026 | 128*map->count3); |
| 4027 | } |
| 4028 | |
| 4029 | static PyMethodDef encoding_map_methods[] = { |
| 4030 | {"size", encoding_map_size, METH_NOARGS, |
| 4031 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 4032 | { 0 } |
| 4033 | }; |
| 4034 | |
| 4035 | static void |
| 4036 | encoding_map_dealloc(PyObject* o) |
| 4037 | { |
| 4038 | PyObject_FREE(o); |
| 4039 | } |
| 4040 | |
| 4041 | static PyTypeObject EncodingMapType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 4042 | PyVarObject_HEAD_INIT(NULL, 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4043 | "EncodingMap", /*tp_name*/ |
| 4044 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 4045 | 0, /*tp_itemsize*/ |
| 4046 | /* methods */ |
| 4047 | encoding_map_dealloc, /*tp_dealloc*/ |
| 4048 | 0, /*tp_print*/ |
| 4049 | 0, /*tp_getattr*/ |
| 4050 | 0, /*tp_setattr*/ |
| 4051 | 0, /*tp_compare*/ |
| 4052 | 0, /*tp_repr*/ |
| 4053 | 0, /*tp_as_number*/ |
| 4054 | 0, /*tp_as_sequence*/ |
| 4055 | 0, /*tp_as_mapping*/ |
| 4056 | 0, /*tp_hash*/ |
| 4057 | 0, /*tp_call*/ |
| 4058 | 0, /*tp_str*/ |
| 4059 | 0, /*tp_getattro*/ |
| 4060 | 0, /*tp_setattro*/ |
| 4061 | 0, /*tp_as_buffer*/ |
| 4062 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 4063 | 0, /*tp_doc*/ |
| 4064 | 0, /*tp_traverse*/ |
| 4065 | 0, /*tp_clear*/ |
| 4066 | 0, /*tp_richcompare*/ |
| 4067 | 0, /*tp_weaklistoffset*/ |
| 4068 | 0, /*tp_iter*/ |
| 4069 | 0, /*tp_iternext*/ |
| 4070 | encoding_map_methods, /*tp_methods*/ |
| 4071 | 0, /*tp_members*/ |
| 4072 | 0, /*tp_getset*/ |
| 4073 | 0, /*tp_base*/ |
| 4074 | 0, /*tp_dict*/ |
| 4075 | 0, /*tp_descr_get*/ |
| 4076 | 0, /*tp_descr_set*/ |
| 4077 | 0, /*tp_dictoffset*/ |
| 4078 | 0, /*tp_init*/ |
| 4079 | 0, /*tp_alloc*/ |
| 4080 | 0, /*tp_new*/ |
| 4081 | 0, /*tp_free*/ |
| 4082 | 0, /*tp_is_gc*/ |
| 4083 | }; |
| 4084 | |
| 4085 | PyObject* |
| 4086 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 4087 | { |
| 4088 | Py_UNICODE *decode; |
| 4089 | PyObject *result; |
| 4090 | struct encoding_map *mresult; |
| 4091 | int i; |
| 4092 | int need_dict = 0; |
| 4093 | unsigned char level1[32]; |
| 4094 | unsigned char level2[512]; |
| 4095 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 4096 | int count2 = 0, count3 = 0; |
| 4097 | |
| 4098 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 4099 | PyErr_BadArgument(); |
| 4100 | return NULL; |
| 4101 | } |
| 4102 | decode = PyUnicode_AS_UNICODE(string); |
| 4103 | memset(level1, 0xFF, sizeof level1); |
| 4104 | memset(level2, 0xFF, sizeof level2); |
| 4105 | |
| 4106 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 4107 | or if there are non-BMP characters, we need to use |
| 4108 | a mapping dictionary. */ |
| 4109 | if (decode[0] != 0) |
| 4110 | need_dict = 1; |
| 4111 | for (i = 1; i < 256; i++) { |
| 4112 | int l1, l2; |
| 4113 | if (decode[i] == 0 |
| 4114 | #ifdef Py_UNICODE_WIDE |
| 4115 | || decode[i] > 0xFFFF |
| 4116 | #endif |
| 4117 | ) { |
| 4118 | need_dict = 1; |
| 4119 | break; |
| 4120 | } |
| 4121 | if (decode[i] == 0xFFFE) |
| 4122 | /* unmapped character */ |
| 4123 | continue; |
| 4124 | l1 = decode[i] >> 11; |
| 4125 | l2 = decode[i] >> 7; |
| 4126 | if (level1[l1] == 0xFF) |
| 4127 | level1[l1] = count2++; |
| 4128 | if (level2[l2] == 0xFF) |
| 4129 | level2[l2] = count3++; |
| 4130 | } |
| 4131 | |
| 4132 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 4133 | need_dict = 1; |
| 4134 | |
| 4135 | if (need_dict) { |
| 4136 | PyObject *result = PyDict_New(); |
| 4137 | PyObject *key, *value; |
| 4138 | if (!result) |
| 4139 | return NULL; |
| 4140 | for (i = 0; i < 256; i++) { |
| 4141 | key = value = NULL; |
| 4142 | key = PyInt_FromLong(decode[i]); |
| 4143 | value = PyInt_FromLong(i); |
| 4144 | if (!key || !value) |
| 4145 | goto failed1; |
| 4146 | if (PyDict_SetItem(result, key, value) == -1) |
| 4147 | goto failed1; |
| 4148 | Py_DECREF(key); |
| 4149 | Py_DECREF(value); |
| 4150 | } |
| 4151 | return result; |
| 4152 | failed1: |
| 4153 | Py_XDECREF(key); |
| 4154 | Py_XDECREF(value); |
| 4155 | Py_DECREF(result); |
| 4156 | return NULL; |
| 4157 | } |
| 4158 | |
| 4159 | /* Create a three-level trie */ |
| 4160 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 4161 | 16*count2 + 128*count3 - 1); |
| 4162 | if (!result) |
| 4163 | return PyErr_NoMemory(); |
| 4164 | PyObject_Init(result, &EncodingMapType); |
| 4165 | mresult = (struct encoding_map*)result; |
| 4166 | mresult->count2 = count2; |
| 4167 | mresult->count3 = count3; |
| 4168 | mlevel1 = mresult->level1; |
| 4169 | mlevel2 = mresult->level23; |
| 4170 | mlevel3 = mresult->level23 + 16*count2; |
| 4171 | memcpy(mlevel1, level1, 32); |
| 4172 | memset(mlevel2, 0xFF, 16*count2); |
| 4173 | memset(mlevel3, 0, 128*count3); |
| 4174 | count3 = 0; |
| 4175 | for (i = 1; i < 256; i++) { |
| 4176 | int o1, o2, o3, i2, i3; |
| 4177 | if (decode[i] == 0xFFFE) |
| 4178 | /* unmapped character */ |
| 4179 | continue; |
| 4180 | o1 = decode[i]>>11; |
| 4181 | o2 = (decode[i]>>7) & 0xF; |
| 4182 | i2 = 16*mlevel1[o1] + o2; |
| 4183 | if (mlevel2[i2] == 0xFF) |
| 4184 | mlevel2[i2] = count3++; |
| 4185 | o3 = decode[i] & 0x7F; |
| 4186 | i3 = 128*mlevel2[i2] + o3; |
| 4187 | mlevel3[i3] = i; |
| 4188 | } |
| 4189 | return result; |
| 4190 | } |
| 4191 | |
| 4192 | static int |
| 4193 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 4194 | { |
| 4195 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 4196 | int l1 = c>>11; |
| 4197 | int l2 = (c>>7) & 0xF; |
| 4198 | int l3 = c & 0x7F; |
| 4199 | int i; |
| 4200 | |
| 4201 | #ifdef Py_UNICODE_WIDE |
| 4202 | if (c > 0xFFFF) { |
| 4203 | return -1; |
| 4204 | } |
| 4205 | #endif |
| 4206 | if (c == 0) |
| 4207 | return 0; |
| 4208 | /* level 1*/ |
| 4209 | i = map->level1[l1]; |
| 4210 | if (i == 0xFF) { |
| 4211 | return -1; |
| 4212 | } |
| 4213 | /* level 2*/ |
| 4214 | i = map->level23[16*i+l2]; |
| 4215 | if (i == 0xFF) { |
| 4216 | return -1; |
| 4217 | } |
| 4218 | /* level 3 */ |
| 4219 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 4220 | if (i == 0) { |
| 4221 | return -1; |
| 4222 | } |
| 4223 | return i; |
| 4224 | } |
| 4225 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4226 | /* Lookup the character ch in the mapping. If the character |
| 4227 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4228 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4229 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4230 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4231 | PyObject *w = PyInt_FromLong((long)c); |
| 4232 | PyObject *x; |
| 4233 | |
| 4234 | if (w == NULL) |
| 4235 | return NULL; |
| 4236 | x = PyObject_GetItem(mapping, w); |
| 4237 | Py_DECREF(w); |
| 4238 | if (x == NULL) { |
| 4239 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4240 | /* No mapping found means: mapping is undefined. */ |
| 4241 | PyErr_Clear(); |
| 4242 | x = Py_None; |
| 4243 | Py_INCREF(x); |
| 4244 | return x; |
| 4245 | } else |
| 4246 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4247 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 4248 | else if (x == Py_None) |
| 4249 | return x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4250 | else if (PyInt_Check(x)) { |
| 4251 | long value = PyInt_AS_LONG(x); |
| 4252 | if (value < 0 || value > 255) { |
| 4253 | PyErr_SetString(PyExc_TypeError, |
| 4254 | "character mapping must be in range(256)"); |
| 4255 | Py_DECREF(x); |
| 4256 | return NULL; |
| 4257 | } |
| 4258 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4259 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4260 | else if (PyString_Check(x)) |
| 4261 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4262 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4263 | /* wrong return value */ |
Walter Dörwald | 580ceed | 2007-05-09 10:39:19 +0000 | [diff] [blame] | 4264 | PyErr_Format(PyExc_TypeError, |
| 4265 | "character mapping must return integer, None or str8, not %.400s", |
| 4266 | x->ob_type->tp_name); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4267 | Py_DECREF(x); |
| 4268 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4269 | } |
| 4270 | } |
| 4271 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4272 | static int |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4273 | charmapencode_resize(PyObject *outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4274 | { |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4275 | Py_ssize_t outsize = PyBytes_GET_SIZE( outobj); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4276 | /* exponentially overallocate to minimize reallocations */ |
| 4277 | if (requiredsize < 2*outsize) |
| 4278 | requiredsize = 2*outsize; |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4279 | if (PyBytes_Resize(outobj, requiredsize)) { |
| 4280 | Py_DECREF(outobj); |
| 4281 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4282 | } |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4283 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4284 | } |
| 4285 | |
| 4286 | typedef enum charmapencode_result { |
| 4287 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
| 4288 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4289 | /* lookup the character, put the result in the output string and adjust |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4290 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4291 | space is available. Return a new reference to the object that |
| 4292 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 4293 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 4294 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4295 | static |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4296 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4297 | PyObject *outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4298 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4299 | PyObject *rep; |
| 4300 | char *outstart; |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4301 | Py_ssize_t outsize = PyBytes_GET_SIZE(outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4302 | |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 4303 | if (Py_Type(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4304 | int res = encoding_map_lookup(c, mapping); |
| 4305 | Py_ssize_t requiredsize = *outpos+1; |
| 4306 | if (res == -1) |
| 4307 | return enc_FAILED; |
| 4308 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4309 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4310 | return enc_EXCEPTION; |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4311 | outstart = PyBytes_AS_STRING(outobj); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4312 | outstart[(*outpos)++] = (char)res; |
| 4313 | return enc_SUCCESS; |
| 4314 | } |
| 4315 | |
| 4316 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4317 | if (rep==NULL) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4318 | return enc_EXCEPTION; |
| 4319 | else if (rep==Py_None) { |
| 4320 | Py_DECREF(rep); |
| 4321 | return enc_FAILED; |
| 4322 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4323 | if (PyInt_Check(rep)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4324 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4325 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4326 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4327 | Py_DECREF(rep); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4328 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4329 | } |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4330 | outstart = PyBytes_AS_STRING(outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4331 | outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); |
| 4332 | } |
| 4333 | else { |
| 4334 | const char *repchars = PyString_AS_STRING(rep); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4335 | Py_ssize_t repsize = PyString_GET_SIZE(rep); |
| 4336 | Py_ssize_t requiredsize = *outpos+repsize; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4337 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4338 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4339 | Py_DECREF(rep); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4340 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4341 | } |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4342 | outstart = PyBytes_AS_STRING(outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4343 | memcpy(outstart + *outpos, repchars, repsize); |
| 4344 | *outpos += repsize; |
| 4345 | } |
| 4346 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4347 | Py_DECREF(rep); |
| 4348 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4349 | } |
| 4350 | |
| 4351 | /* handle an error in PyUnicode_EncodeCharmap |
| 4352 | Return 0 on success, -1 on error */ |
| 4353 | static |
| 4354 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4355 | 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] | 4356 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4357 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4358 | PyObject *res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4359 | { |
| 4360 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4361 | Py_ssize_t repsize; |
| 4362 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4363 | Py_UNICODE *uni2; |
| 4364 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4365 | Py_ssize_t collstartpos = *inpos; |
| 4366 | Py_ssize_t collendpos = *inpos+1; |
| 4367 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4368 | char *encoding = "charmap"; |
| 4369 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4370 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4371 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4372 | /* find all unencodable characters */ |
| 4373 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4374 | PyObject *rep; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 4375 | if (Py_Type(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4376 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 4377 | if (res != -1) |
| 4378 | break; |
| 4379 | ++collendpos; |
| 4380 | continue; |
| 4381 | } |
| 4382 | |
| 4383 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 4384 | if (rep==NULL) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4385 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4386 | else if (rep!=Py_None) { |
| 4387 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4388 | break; |
| 4389 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4390 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4391 | ++collendpos; |
| 4392 | } |
| 4393 | /* cache callback name lookup |
| 4394 | * (if not done yet, i.e. it's the first error) */ |
| 4395 | if (*known_errorHandler==-1) { |
| 4396 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4397 | *known_errorHandler = 1; |
| 4398 | else if (!strcmp(errors, "replace")) |
| 4399 | *known_errorHandler = 2; |
| 4400 | else if (!strcmp(errors, "ignore")) |
| 4401 | *known_errorHandler = 3; |
| 4402 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4403 | *known_errorHandler = 4; |
| 4404 | else |
| 4405 | *known_errorHandler = 0; |
| 4406 | } |
| 4407 | switch (*known_errorHandler) { |
| 4408 | case 1: /* strict */ |
| 4409 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4410 | return -1; |
| 4411 | case 2: /* replace */ |
| 4412 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 4413 | x = charmapencode_output('?', mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4414 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4415 | return -1; |
| 4416 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4417 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4418 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4419 | return -1; |
| 4420 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4421 | } |
| 4422 | /* fall through */ |
| 4423 | case 3: /* ignore */ |
| 4424 | *inpos = collendpos; |
| 4425 | break; |
| 4426 | case 4: /* xmlcharrefreplace */ |
| 4427 | /* generate replacement (temporarily (mis)uses p) */ |
| 4428 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 4429 | char buffer[2+29+1+1]; |
| 4430 | char *cp; |
| 4431 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 4432 | for (cp = buffer; *cp; ++cp) { |
| 4433 | x = charmapencode_output(*cp, mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4434 | if (x==enc_EXCEPTION) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4435 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4436 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4437 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4438 | return -1; |
| 4439 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4440 | } |
| 4441 | } |
| 4442 | *inpos = collendpos; |
| 4443 | break; |
| 4444 | default: |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4445 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4446 | encoding, reason, p, size, exceptionObject, |
| 4447 | collstartpos, collendpos, &newpos); |
| 4448 | if (repunicode == NULL) |
| 4449 | return -1; |
| 4450 | /* generate replacement */ |
| 4451 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4452 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 4453 | x = charmapencode_output(*uni2, mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4454 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4455 | return -1; |
| 4456 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4457 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4458 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4459 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4460 | return -1; |
| 4461 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4462 | } |
| 4463 | *inpos = newpos; |
| 4464 | Py_DECREF(repunicode); |
| 4465 | } |
| 4466 | return 0; |
| 4467 | } |
| 4468 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4469 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4470 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4471 | PyObject *mapping, |
| 4472 | const char *errors) |
| 4473 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4474 | /* output object */ |
| 4475 | PyObject *res = NULL; |
| 4476 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4477 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4478 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4479 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4480 | PyObject *errorHandler = NULL; |
| 4481 | PyObject *exc = NULL; |
| 4482 | /* the following variable is used for caching string comparisons |
| 4483 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4484 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4485 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4486 | |
| 4487 | /* Default to Latin-1 */ |
| 4488 | if (mapping == NULL) |
| 4489 | return PyUnicode_EncodeLatin1(p, size, errors); |
| 4490 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4491 | /* allocate enough for a simple encoding without |
| 4492 | replacements, if we need more, we'll resize */ |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4493 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4494 | if (res == NULL) |
| 4495 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 4496 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4497 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4498 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4499 | while (inpos<size) { |
| 4500 | /* try to encode it */ |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4501 | charmapencode_result x = charmapencode_output(p[inpos], mapping, res, &respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4502 | if (x==enc_EXCEPTION) /* error */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4503 | goto onError; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4504 | if (x==enc_FAILED) { /* unencodable character */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4505 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 4506 | &exc, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4507 | &known_errorHandler, &errorHandler, errors, |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4508 | res, &respos)) { |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 4509 | goto onError; |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 4510 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4511 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4512 | else |
| 4513 | /* done with this character => adjust input position */ |
| 4514 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4515 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4516 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4517 | /* Resize if we allocated to much */ |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4518 | if (respos<PyBytes_GET_SIZE(res)) { |
| 4519 | if (PyBytes_Resize(res, respos)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4520 | goto onError; |
| 4521 | } |
| 4522 | Py_XDECREF(exc); |
| 4523 | Py_XDECREF(errorHandler); |
| 4524 | return res; |
| 4525 | |
| 4526 | onError: |
| 4527 | Py_XDECREF(res); |
| 4528 | Py_XDECREF(exc); |
| 4529 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4530 | return NULL; |
| 4531 | } |
| 4532 | |
| 4533 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 4534 | PyObject *mapping) |
| 4535 | { |
| 4536 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 4537 | PyErr_BadArgument(); |
| 4538 | return NULL; |
| 4539 | } |
| 4540 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 4541 | PyUnicode_GET_SIZE(unicode), |
| 4542 | mapping, |
| 4543 | NULL); |
| 4544 | } |
| 4545 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4546 | /* create or adjust a UnicodeTranslateError */ |
| 4547 | static void make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4548 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4549 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4550 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4551 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4552 | if (*exceptionObject == NULL) { |
| 4553 | *exceptionObject = PyUnicodeTranslateError_Create( |
| 4554 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4555 | } |
| 4556 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4557 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 4558 | goto onError; |
| 4559 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 4560 | goto onError; |
| 4561 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 4562 | goto onError; |
| 4563 | return; |
| 4564 | onError: |
| 4565 | Py_DECREF(*exceptionObject); |
| 4566 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4567 | } |
| 4568 | } |
| 4569 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4570 | /* raises a UnicodeTranslateError */ |
| 4571 | static void raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4572 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4573 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4574 | const char *reason) |
| 4575 | { |
| 4576 | make_translate_exception(exceptionObject, |
| 4577 | unicode, size, startpos, endpos, reason); |
| 4578 | if (*exceptionObject != NULL) |
| 4579 | PyCodec_StrictErrors(*exceptionObject); |
| 4580 | } |
| 4581 | |
| 4582 | /* error handling callback helper: |
| 4583 | build arguments, call the callback and check the arguments, |
| 4584 | put the result into newpos and return the replacement string, which |
| 4585 | has to be freed by the caller */ |
| 4586 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 4587 | PyObject **errorHandler, |
| 4588 | const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4589 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4590 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4591 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4592 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4593 | 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] | 4594 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4595 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4596 | PyObject *restuple; |
| 4597 | PyObject *resunicode; |
| 4598 | |
| 4599 | if (*errorHandler == NULL) { |
| 4600 | *errorHandler = PyCodec_LookupError(errors); |
| 4601 | if (*errorHandler == NULL) |
| 4602 | return NULL; |
| 4603 | } |
| 4604 | |
| 4605 | make_translate_exception(exceptionObject, |
| 4606 | unicode, size, startpos, endpos, reason); |
| 4607 | if (*exceptionObject == NULL) |
| 4608 | return NULL; |
| 4609 | |
| 4610 | restuple = PyObject_CallFunctionObjArgs( |
| 4611 | *errorHandler, *exceptionObject, NULL); |
| 4612 | if (restuple == NULL) |
| 4613 | return NULL; |
| 4614 | if (!PyTuple_Check(restuple)) { |
| 4615 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 4616 | Py_DECREF(restuple); |
| 4617 | return NULL; |
| 4618 | } |
| 4619 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4620 | &resunicode, &i_newpos)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4621 | Py_DECREF(restuple); |
| 4622 | return NULL; |
| 4623 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4624 | if (i_newpos<0) |
| 4625 | *newpos = size+i_newpos; |
| 4626 | else |
| 4627 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4628 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 4629 | 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] | 4630 | Py_DECREF(restuple); |
| 4631 | return NULL; |
| 4632 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4633 | Py_INCREF(resunicode); |
| 4634 | Py_DECREF(restuple); |
| 4635 | return resunicode; |
| 4636 | } |
| 4637 | |
| 4638 | /* Lookup the character ch in the mapping and put the result in result, |
| 4639 | which must be decrefed by the caller. |
| 4640 | Return 0 on success, -1 on error */ |
| 4641 | static |
| 4642 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 4643 | { |
| 4644 | PyObject *w = PyInt_FromLong((long)c); |
| 4645 | PyObject *x; |
| 4646 | |
| 4647 | if (w == NULL) |
| 4648 | return -1; |
| 4649 | x = PyObject_GetItem(mapping, w); |
| 4650 | Py_DECREF(w); |
| 4651 | if (x == NULL) { |
| 4652 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4653 | /* No mapping found means: use 1:1 mapping. */ |
| 4654 | PyErr_Clear(); |
| 4655 | *result = NULL; |
| 4656 | return 0; |
| 4657 | } else |
| 4658 | return -1; |
| 4659 | } |
| 4660 | else if (x == Py_None) { |
| 4661 | *result = x; |
| 4662 | return 0; |
| 4663 | } |
| 4664 | else if (PyInt_Check(x)) { |
| 4665 | long value = PyInt_AS_LONG(x); |
| 4666 | long max = PyUnicode_GetMax(); |
| 4667 | if (value < 0 || value > max) { |
| 4668 | PyErr_Format(PyExc_TypeError, |
| 4669 | "character mapping must be in range(0x%lx)", max+1); |
| 4670 | Py_DECREF(x); |
| 4671 | return -1; |
| 4672 | } |
| 4673 | *result = x; |
| 4674 | return 0; |
| 4675 | } |
| 4676 | else if (PyUnicode_Check(x)) { |
| 4677 | *result = x; |
| 4678 | return 0; |
| 4679 | } |
| 4680 | else { |
| 4681 | /* wrong return value */ |
| 4682 | PyErr_SetString(PyExc_TypeError, |
| 4683 | "character mapping must return integer, None or unicode"); |
Walter Dörwald | 150523e | 2003-08-15 16:52:19 +0000 | [diff] [blame] | 4684 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4685 | return -1; |
| 4686 | } |
| 4687 | } |
| 4688 | /* ensure that *outobj is at least requiredsize characters long, |
| 4689 | if not reallocate and adjust various state variables. |
| 4690 | Return 0 on success, -1 on error */ |
| 4691 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4692 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4693 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4694 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4695 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4696 | if (requiredsize > oldsize) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4697 | /* remember old output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4698 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4699 | /* exponentially overallocate to minimize reallocations */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4700 | if (requiredsize < 2 * oldsize) |
| 4701 | requiredsize = 2 * oldsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4702 | if (_PyUnicode_Resize(outobj, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4703 | return -1; |
| 4704 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4705 | } |
| 4706 | return 0; |
| 4707 | } |
| 4708 | /* lookup the character, put the result in the output string and adjust |
| 4709 | various state variables. Return a new reference to the object that |
| 4710 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 4711 | undefined (in which case no character was written). |
| 4712 | The called must decref result. |
| 4713 | Return 0 on success, -1 on error. */ |
| 4714 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4715 | 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] | 4716 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4717 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4718 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4719 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4720 | return -1; |
| 4721 | if (*res==NULL) { |
| 4722 | /* not found => default to 1:1 mapping */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4723 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4724 | } |
| 4725 | else if (*res==Py_None) |
| 4726 | ; |
| 4727 | else if (PyInt_Check(*res)) { |
| 4728 | /* no overflow check, because we know that the space is enough */ |
| 4729 | *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); |
| 4730 | } |
| 4731 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4732 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4733 | if (repsize==1) { |
| 4734 | /* no overflow check, because we know that the space is enough */ |
| 4735 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 4736 | } |
| 4737 | else if (repsize!=0) { |
| 4738 | /* more than one character */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4739 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
Walter Dörwald | cd736e7 | 2004-02-05 17:36:00 +0000 | [diff] [blame] | 4740 | (insize - (curinp-startinp)) + |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4741 | repsize - 1; |
| 4742 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4743 | return -1; |
| 4744 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 4745 | *outp += repsize; |
| 4746 | } |
| 4747 | } |
| 4748 | else |
| 4749 | return -1; |
| 4750 | return 0; |
| 4751 | } |
| 4752 | |
| 4753 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4754 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4755 | PyObject *mapping, |
| 4756 | const char *errors) |
| 4757 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4758 | /* output object */ |
| 4759 | PyObject *res = NULL; |
| 4760 | /* pointers to the beginning and end+1 of input */ |
| 4761 | const Py_UNICODE *startp = p; |
| 4762 | const Py_UNICODE *endp = p + size; |
| 4763 | /* pointer into the output */ |
| 4764 | Py_UNICODE *str; |
| 4765 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4766 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4767 | char *reason = "character maps to <undefined>"; |
| 4768 | PyObject *errorHandler = NULL; |
| 4769 | PyObject *exc = NULL; |
| 4770 | /* the following variable is used for caching string comparisons |
| 4771 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4772 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4773 | int known_errorHandler = -1; |
| 4774 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4775 | if (mapping == NULL) { |
| 4776 | PyErr_BadArgument(); |
| 4777 | return NULL; |
| 4778 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4779 | |
| 4780 | /* allocate enough for a simple 1:1 translation without |
| 4781 | replacements, if we need more, we'll resize */ |
| 4782 | res = PyUnicode_FromUnicode(NULL, size); |
| 4783 | if (res == NULL) |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4784 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4785 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4786 | return res; |
| 4787 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4788 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4789 | while (p<endp) { |
| 4790 | /* try to encode it */ |
| 4791 | PyObject *x = NULL; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4792 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4793 | Py_XDECREF(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4794 | goto onError; |
| 4795 | } |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 4796 | Py_XDECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4797 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 4798 | ++p; |
| 4799 | else { /* untranslatable character */ |
| 4800 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4801 | Py_ssize_t repsize; |
| 4802 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4803 | Py_UNICODE *uni2; |
| 4804 | /* startpos for collecting untranslatable chars */ |
| 4805 | const Py_UNICODE *collstart = p; |
| 4806 | const Py_UNICODE *collend = p+1; |
| 4807 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4808 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4809 | /* find all untranslatable characters */ |
| 4810 | while (collend < endp) { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4811 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4812 | goto onError; |
| 4813 | Py_XDECREF(x); |
| 4814 | if (x!=Py_None) |
| 4815 | break; |
| 4816 | ++collend; |
| 4817 | } |
| 4818 | /* cache callback name lookup |
| 4819 | * (if not done yet, i.e. it's the first error) */ |
| 4820 | if (known_errorHandler==-1) { |
| 4821 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4822 | known_errorHandler = 1; |
| 4823 | else if (!strcmp(errors, "replace")) |
| 4824 | known_errorHandler = 2; |
| 4825 | else if (!strcmp(errors, "ignore")) |
| 4826 | known_errorHandler = 3; |
| 4827 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4828 | known_errorHandler = 4; |
| 4829 | else |
| 4830 | known_errorHandler = 0; |
| 4831 | } |
| 4832 | switch (known_errorHandler) { |
| 4833 | case 1: /* strict */ |
| 4834 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
| 4835 | goto onError; |
| 4836 | case 2: /* replace */ |
| 4837 | /* No need to check for space, this is a 1:1 replacement */ |
| 4838 | for (coll = collstart; coll<collend; ++coll) |
| 4839 | *str++ = '?'; |
| 4840 | /* fall through */ |
| 4841 | case 3: /* ignore */ |
| 4842 | p = collend; |
| 4843 | break; |
| 4844 | case 4: /* xmlcharrefreplace */ |
| 4845 | /* generate replacement (temporarily (mis)uses p) */ |
| 4846 | for (p = collstart; p < collend; ++p) { |
| 4847 | char buffer[2+29+1+1]; |
| 4848 | char *cp; |
| 4849 | sprintf(buffer, "&#%d;", (int)*p); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4850 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4851 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 4852 | goto onError; |
| 4853 | for (cp = buffer; *cp; ++cp) |
| 4854 | *str++ = *cp; |
| 4855 | } |
| 4856 | p = collend; |
| 4857 | break; |
| 4858 | default: |
| 4859 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 4860 | reason, startp, size, &exc, |
| 4861 | collstart-startp, collend-startp, &newpos); |
| 4862 | if (repunicode == NULL) |
| 4863 | goto onError; |
| 4864 | /* generate replacement */ |
| 4865 | repsize = PyUnicode_GET_SIZE(repunicode); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4866 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4867 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 4868 | Py_DECREF(repunicode); |
| 4869 | goto onError; |
| 4870 | } |
| 4871 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 4872 | *str++ = *uni2; |
| 4873 | p = startp + newpos; |
| 4874 | Py_DECREF(repunicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4875 | } |
| 4876 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4877 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4878 | /* Resize if we allocated to much */ |
| 4879 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4880 | if (respos<PyUnicode_GET_SIZE(res)) { |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4881 | if (_PyUnicode_Resize(&res, respos) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 4882 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4883 | } |
| 4884 | Py_XDECREF(exc); |
| 4885 | Py_XDECREF(errorHandler); |
| 4886 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4887 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4888 | onError: |
| 4889 | Py_XDECREF(res); |
| 4890 | Py_XDECREF(exc); |
| 4891 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4892 | return NULL; |
| 4893 | } |
| 4894 | |
| 4895 | PyObject *PyUnicode_Translate(PyObject *str, |
| 4896 | PyObject *mapping, |
| 4897 | const char *errors) |
| 4898 | { |
| 4899 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4900 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4901 | str = PyUnicode_FromObject(str); |
| 4902 | if (str == NULL) |
| 4903 | goto onError; |
| 4904 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 4905 | PyUnicode_GET_SIZE(str), |
| 4906 | mapping, |
| 4907 | errors); |
| 4908 | Py_DECREF(str); |
| 4909 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4910 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4911 | onError: |
| 4912 | Py_XDECREF(str); |
| 4913 | return NULL; |
| 4914 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4915 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4916 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 4917 | |
| 4918 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4919 | Py_ssize_t length, |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4920 | char *output, |
| 4921 | const char *errors) |
| 4922 | { |
| 4923 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4924 | PyObject *errorHandler = NULL; |
| 4925 | PyObject *exc = NULL; |
| 4926 | const char *encoding = "decimal"; |
| 4927 | const char *reason = "invalid decimal Unicode string"; |
| 4928 | /* the following variable is used for caching string comparisons |
| 4929 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 4930 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4931 | |
| 4932 | if (output == NULL) { |
| 4933 | PyErr_BadArgument(); |
| 4934 | return -1; |
| 4935 | } |
| 4936 | |
| 4937 | p = s; |
| 4938 | end = s + length; |
| 4939 | while (p < end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4940 | register Py_UNICODE ch = *p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4941 | int decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4942 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4943 | Py_ssize_t repsize; |
| 4944 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4945 | Py_UNICODE *uni2; |
| 4946 | Py_UNICODE *collstart; |
| 4947 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4948 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4949 | if (Py_UNICODE_ISSPACE(ch)) { |
| 4950 | *output++ = ' '; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4951 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4952 | continue; |
| 4953 | } |
| 4954 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 4955 | if (decimal >= 0) { |
| 4956 | *output++ = '0' + decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4957 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4958 | continue; |
| 4959 | } |
Guido van Rossum | ba47704 | 2000-04-06 18:18:10 +0000 | [diff] [blame] | 4960 | if (0 < ch && ch < 256) { |
Guido van Rossum | 42c29aa | 2000-05-03 23:58:29 +0000 | [diff] [blame] | 4961 | *output++ = (char)ch; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4962 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4963 | continue; |
| 4964 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4965 | /* All other characters are considered unencodable */ |
| 4966 | collstart = p; |
| 4967 | collend = p+1; |
| 4968 | while (collend < end) { |
| 4969 | if ((0 < *collend && *collend < 256) || |
| 4970 | !Py_UNICODE_ISSPACE(*collend) || |
| 4971 | Py_UNICODE_TODECIMAL(*collend)) |
| 4972 | break; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4973 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4974 | /* cache callback name lookup |
| 4975 | * (if not done yet, i.e. it's the first error) */ |
| 4976 | if (known_errorHandler==-1) { |
| 4977 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4978 | known_errorHandler = 1; |
| 4979 | else if (!strcmp(errors, "replace")) |
| 4980 | known_errorHandler = 2; |
| 4981 | else if (!strcmp(errors, "ignore")) |
| 4982 | known_errorHandler = 3; |
| 4983 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4984 | known_errorHandler = 4; |
| 4985 | else |
| 4986 | known_errorHandler = 0; |
| 4987 | } |
| 4988 | switch (known_errorHandler) { |
| 4989 | case 1: /* strict */ |
| 4990 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 4991 | goto onError; |
| 4992 | case 2: /* replace */ |
| 4993 | for (p = collstart; p < collend; ++p) |
| 4994 | *output++ = '?'; |
| 4995 | /* fall through */ |
| 4996 | case 3: /* ignore */ |
| 4997 | p = collend; |
| 4998 | break; |
| 4999 | case 4: /* xmlcharrefreplace */ |
| 5000 | /* generate replacement (temporarily (mis)uses p) */ |
| 5001 | for (p = collstart; p < collend; ++p) |
| 5002 | output += sprintf(output, "&#%d;", (int)*p); |
| 5003 | p = collend; |
| 5004 | break; |
| 5005 | default: |
| 5006 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 5007 | encoding, reason, s, length, &exc, |
| 5008 | collstart-s, collend-s, &newpos); |
| 5009 | if (repunicode == NULL) |
| 5010 | goto onError; |
| 5011 | /* generate replacement */ |
| 5012 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5013 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 5014 | Py_UNICODE ch = *uni2; |
| 5015 | if (Py_UNICODE_ISSPACE(ch)) |
| 5016 | *output++ = ' '; |
| 5017 | else { |
| 5018 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5019 | if (decimal >= 0) |
| 5020 | *output++ = '0' + decimal; |
| 5021 | else if (0 < ch && ch < 256) |
| 5022 | *output++ = (char)ch; |
| 5023 | else { |
| 5024 | Py_DECREF(repunicode); |
| 5025 | raise_encode_exception(&exc, encoding, |
| 5026 | s, length, collstart-s, collend-s, reason); |
| 5027 | goto onError; |
| 5028 | } |
| 5029 | } |
| 5030 | } |
| 5031 | p = s + newpos; |
| 5032 | Py_DECREF(repunicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5033 | } |
| 5034 | } |
| 5035 | /* 0-terminate the output string */ |
| 5036 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5037 | Py_XDECREF(exc); |
| 5038 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5039 | return 0; |
| 5040 | |
| 5041 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5042 | Py_XDECREF(exc); |
| 5043 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5044 | return -1; |
| 5045 | } |
| 5046 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5047 | /* --- Helpers ------------------------------------------------------------ */ |
| 5048 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 5049 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5050 | |
| 5051 | #include "stringlib/fastsearch.h" |
| 5052 | |
| 5053 | #include "stringlib/count.h" |
| 5054 | #include "stringlib/find.h" |
| 5055 | #include "stringlib/partition.h" |
| 5056 | |
| 5057 | /* helper macro to fixup start/end slice values */ |
| 5058 | #define FIX_START_END(obj) \ |
| 5059 | if (start < 0) \ |
| 5060 | start += (obj)->length; \ |
| 5061 | if (start < 0) \ |
| 5062 | start = 0; \ |
| 5063 | if (end > (obj)->length) \ |
| 5064 | end = (obj)->length; \ |
| 5065 | if (end < 0) \ |
| 5066 | end += (obj)->length; \ |
| 5067 | if (end < 0) \ |
| 5068 | end = 0; |
| 5069 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5070 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5071 | PyObject *substr, |
| 5072 | Py_ssize_t start, |
| 5073 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5074 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5075 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5076 | PyUnicodeObject* str_obj; |
| 5077 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5078 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5079 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 5080 | if (!str_obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5081 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5082 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 5083 | if (!sub_obj) { |
| 5084 | Py_DECREF(str_obj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5085 | return -1; |
| 5086 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5087 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5088 | FIX_START_END(str_obj); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5089 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5090 | result = stringlib_count( |
| 5091 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
| 5092 | ); |
| 5093 | |
| 5094 | Py_DECREF(sub_obj); |
| 5095 | Py_DECREF(str_obj); |
| 5096 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5097 | return result; |
| 5098 | } |
| 5099 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5100 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5101 | PyObject *sub, |
| 5102 | Py_ssize_t start, |
| 5103 | Py_ssize_t end, |
| 5104 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5105 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5106 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5107 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5108 | str = PyUnicode_FromObject(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5109 | if (!str) |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 5110 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5111 | sub = PyUnicode_FromObject(sub); |
| 5112 | if (!sub) { |
Marc-André Lemburg | 4164439 | 2002-05-29 13:46:29 +0000 | [diff] [blame] | 5113 | Py_DECREF(str); |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 5114 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5115 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5116 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5117 | if (direction > 0) |
| 5118 | result = stringlib_find_slice( |
| 5119 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5120 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5121 | start, end |
| 5122 | ); |
| 5123 | else |
| 5124 | result = stringlib_rfind_slice( |
| 5125 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5126 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5127 | start, end |
| 5128 | ); |
| 5129 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5130 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5131 | Py_DECREF(sub); |
| 5132 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5133 | return result; |
| 5134 | } |
| 5135 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5136 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5137 | int tailmatch(PyUnicodeObject *self, |
| 5138 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5139 | Py_ssize_t start, |
| 5140 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5141 | int direction) |
| 5142 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5143 | if (substring->length == 0) |
| 5144 | return 1; |
| 5145 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5146 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5147 | |
| 5148 | end -= substring->length; |
| 5149 | if (end < start) |
| 5150 | return 0; |
| 5151 | |
| 5152 | if (direction > 0) { |
| 5153 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 5154 | return 1; |
| 5155 | } else { |
| 5156 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 5157 | return 1; |
| 5158 | } |
| 5159 | |
| 5160 | return 0; |
| 5161 | } |
| 5162 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5163 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5164 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5165 | Py_ssize_t start, |
| 5166 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5167 | int direction) |
| 5168 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5169 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5170 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5171 | str = PyUnicode_FromObject(str); |
| 5172 | if (str == NULL) |
| 5173 | return -1; |
| 5174 | substr = PyUnicode_FromObject(substr); |
| 5175 | if (substr == NULL) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5176 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5177 | return -1; |
| 5178 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5179 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5180 | result = tailmatch((PyUnicodeObject *)str, |
| 5181 | (PyUnicodeObject *)substr, |
| 5182 | start, end, direction); |
| 5183 | Py_DECREF(str); |
| 5184 | Py_DECREF(substr); |
| 5185 | return result; |
| 5186 | } |
| 5187 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5188 | /* Apply fixfct filter to the Unicode object self and return a |
| 5189 | reference to the modified object */ |
| 5190 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5191 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5192 | PyObject *fixup(PyUnicodeObject *self, |
| 5193 | int (*fixfct)(PyUnicodeObject *s)) |
| 5194 | { |
| 5195 | |
| 5196 | PyUnicodeObject *u; |
| 5197 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5198 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5199 | if (u == NULL) |
| 5200 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5201 | |
| 5202 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5203 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5204 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5205 | /* fixfct should return TRUE if it modified the buffer. If |
| 5206 | FALSE, return a reference to the original buffer instead |
| 5207 | (to save space, not time) */ |
| 5208 | Py_INCREF(self); |
| 5209 | Py_DECREF(u); |
| 5210 | return (PyObject*) self; |
| 5211 | } |
| 5212 | return (PyObject*) u; |
| 5213 | } |
| 5214 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5215 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5216 | int fixupper(PyUnicodeObject *self) |
| 5217 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5218 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5219 | Py_UNICODE *s = self->str; |
| 5220 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5221 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5222 | while (len-- > 0) { |
| 5223 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5224 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5225 | ch = Py_UNICODE_TOUPPER(*s); |
| 5226 | if (ch != *s) { |
| 5227 | status = 1; |
| 5228 | *s = ch; |
| 5229 | } |
| 5230 | s++; |
| 5231 | } |
| 5232 | |
| 5233 | return status; |
| 5234 | } |
| 5235 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5236 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5237 | int fixlower(PyUnicodeObject *self) |
| 5238 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5239 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5240 | Py_UNICODE *s = self->str; |
| 5241 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5242 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5243 | while (len-- > 0) { |
| 5244 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5245 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5246 | ch = Py_UNICODE_TOLOWER(*s); |
| 5247 | if (ch != *s) { |
| 5248 | status = 1; |
| 5249 | *s = ch; |
| 5250 | } |
| 5251 | s++; |
| 5252 | } |
| 5253 | |
| 5254 | return status; |
| 5255 | } |
| 5256 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5257 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5258 | int fixswapcase(PyUnicodeObject *self) |
| 5259 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5260 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5261 | Py_UNICODE *s = self->str; |
| 5262 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5263 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5264 | while (len-- > 0) { |
| 5265 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5266 | *s = Py_UNICODE_TOLOWER(*s); |
| 5267 | status = 1; |
| 5268 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 5269 | *s = Py_UNICODE_TOUPPER(*s); |
| 5270 | status = 1; |
| 5271 | } |
| 5272 | s++; |
| 5273 | } |
| 5274 | |
| 5275 | return status; |
| 5276 | } |
| 5277 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5278 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5279 | int fixcapitalize(PyUnicodeObject *self) |
| 5280 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5281 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5282 | Py_UNICODE *s = self->str; |
| 5283 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5284 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5285 | if (len == 0) |
| 5286 | return 0; |
| 5287 | if (Py_UNICODE_ISLOWER(*s)) { |
| 5288 | *s = Py_UNICODE_TOUPPER(*s); |
| 5289 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5290 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5291 | s++; |
| 5292 | while (--len > 0) { |
| 5293 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5294 | *s = Py_UNICODE_TOLOWER(*s); |
| 5295 | status = 1; |
| 5296 | } |
| 5297 | s++; |
| 5298 | } |
| 5299 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5300 | } |
| 5301 | |
| 5302 | static |
| 5303 | int fixtitle(PyUnicodeObject *self) |
| 5304 | { |
| 5305 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5306 | register Py_UNICODE *e; |
| 5307 | int previous_is_cased; |
| 5308 | |
| 5309 | /* Shortcut for single character strings */ |
| 5310 | if (PyUnicode_GET_SIZE(self) == 1) { |
| 5311 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 5312 | if (*p != ch) { |
| 5313 | *p = ch; |
| 5314 | return 1; |
| 5315 | } |
| 5316 | else |
| 5317 | return 0; |
| 5318 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5319 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5320 | e = p + PyUnicode_GET_SIZE(self); |
| 5321 | previous_is_cased = 0; |
| 5322 | for (; p < e; p++) { |
| 5323 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5324 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5325 | if (previous_is_cased) |
| 5326 | *p = Py_UNICODE_TOLOWER(ch); |
| 5327 | else |
| 5328 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5329 | |
| 5330 | if (Py_UNICODE_ISLOWER(ch) || |
| 5331 | Py_UNICODE_ISUPPER(ch) || |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5332 | Py_UNICODE_ISTITLE(ch)) |
| 5333 | previous_is_cased = 1; |
| 5334 | else |
| 5335 | previous_is_cased = 0; |
| 5336 | } |
| 5337 | return 1; |
| 5338 | } |
| 5339 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5340 | PyObject * |
| 5341 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5342 | { |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5343 | PyObject *internal_separator = NULL; |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 5344 | const Py_UNICODE blank = ' '; |
| 5345 | const Py_UNICODE *sep = ␣ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5346 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5347 | PyUnicodeObject *res = NULL; /* the result */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5348 | Py_ssize_t res_alloc = 100; /* # allocated bytes for string in res */ |
| 5349 | Py_ssize_t res_used; /* # used bytes */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5350 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 5351 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5352 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5353 | PyObject *item; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5354 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5355 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5356 | fseq = PySequence_Fast(seq, ""); |
| 5357 | if (fseq == NULL) { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5358 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5359 | } |
| 5360 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5361 | /* Grrrr. A codec may be invoked to convert str objects to |
| 5362 | * Unicode, and so it's possible to call back into Python code |
| 5363 | * during PyUnicode_FromObject(), and so it's possible for a sick |
| 5364 | * codec to change the size of fseq (if seq is a list). Therefore |
| 5365 | * we have to keep refetching the size -- can't assume seqlen |
| 5366 | * is invariant. |
| 5367 | */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5368 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5369 | /* If empty sequence, return u"". */ |
| 5370 | if (seqlen == 0) { |
| 5371 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 5372 | goto Done; |
| 5373 | } |
| 5374 | /* If singleton sequence with an exact Unicode, return that. */ |
| 5375 | if (seqlen == 1) { |
| 5376 | item = PySequence_Fast_GET_ITEM(fseq, 0); |
| 5377 | if (PyUnicode_CheckExact(item)) { |
| 5378 | Py_INCREF(item); |
| 5379 | res = (PyUnicodeObject *)item; |
| 5380 | goto Done; |
| 5381 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5382 | } |
| 5383 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5384 | /* At least two items to join, or one that isn't exact Unicode. */ |
| 5385 | if (seqlen > 1) { |
| 5386 | /* Set up sep and seplen -- they're needed. */ |
| 5387 | if (separator == NULL) { |
| 5388 | sep = ␣ |
| 5389 | seplen = 1; |
| 5390 | } |
| 5391 | else { |
| 5392 | internal_separator = PyUnicode_FromObject(separator); |
| 5393 | if (internal_separator == NULL) |
| 5394 | goto onError; |
| 5395 | sep = PyUnicode_AS_UNICODE(internal_separator); |
| 5396 | seplen = PyUnicode_GET_SIZE(internal_separator); |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5397 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 5398 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5399 | } |
| 5400 | } |
| 5401 | |
| 5402 | /* Get space. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5403 | res = _PyUnicode_New(res_alloc); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5404 | if (res == NULL) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5405 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5406 | res_p = PyUnicode_AS_UNICODE(res); |
| 5407 | res_used = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5408 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5409 | for (i = 0; i < seqlen; ++i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5410 | Py_ssize_t itemlen; |
| 5411 | Py_ssize_t new_res_used; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5412 | |
| 5413 | item = PySequence_Fast_GET_ITEM(fseq, i); |
| 5414 | /* Convert item to Unicode. */ |
Guido van Rossum | f104429 | 2007-09-27 18:01:22 +0000 | [diff] [blame^] | 5415 | if (!PyString_Check(item) && !PyUnicode_Check(item)) |
| 5416 | { |
| 5417 | if (PyBytes_Check(item)) |
| 5418 | { |
| 5419 | PyErr_Format(PyExc_TypeError, |
| 5420 | "sequence item %d: join() will not operate on " |
| 5421 | "bytes objects", i); |
| 5422 | goto onError; |
| 5423 | } |
| 5424 | item = PyObject_Unicode(item); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5425 | } |
Guido van Rossum | f104429 | 2007-09-27 18:01:22 +0000 | [diff] [blame^] | 5426 | else |
| 5427 | item = PyUnicode_FromObject(item); |
| 5428 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5429 | if (item == NULL) |
| 5430 | goto onError; |
| 5431 | /* We own a reference to item from here on. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5432 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5433 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 5434 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5435 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5436 | /* 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] | 5437 | itemlen = PyUnicode_GET_SIZE(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5438 | new_res_used = res_used + itemlen; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5439 | if (new_res_used < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5440 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5441 | if (i < seqlen - 1) { |
| 5442 | new_res_used += seplen; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5443 | if (new_res_used < 0) |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5444 | goto Overflow; |
| 5445 | } |
| 5446 | if (new_res_used > res_alloc) { |
| 5447 | /* double allocated size until it's big enough */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5448 | do { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5449 | res_alloc += res_alloc; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5450 | if (res_alloc <= 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5451 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5452 | } while (new_res_used > res_alloc); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5453 | if (_PyUnicode_Resize(&res, res_alloc) < 0) { |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 5454 | Py_DECREF(item); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5455 | goto onError; |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 5456 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5457 | res_p = PyUnicode_AS_UNICODE(res) + res_used; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5458 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5459 | |
| 5460 | /* Copy item, and maybe the separator. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5461 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5462 | res_p += itemlen; |
| 5463 | if (i < seqlen - 1) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5464 | Py_UNICODE_COPY(res_p, sep, seplen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5465 | res_p += seplen; |
| 5466 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5467 | Py_DECREF(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5468 | res_used = new_res_used; |
| 5469 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5470 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5471 | /* Shrink res to match the used area; this probably can't fail, |
| 5472 | * but it's cheap to check. |
| 5473 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5474 | if (_PyUnicode_Resize(&res, res_used) < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5475 | goto onError; |
| 5476 | |
| 5477 | Done: |
| 5478 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5479 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5480 | return (PyObject *)res; |
| 5481 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5482 | Overflow: |
| 5483 | PyErr_SetString(PyExc_OverflowError, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5484 | "join() result is too long for a Python string"); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5485 | Py_DECREF(item); |
| 5486 | /* fall through */ |
| 5487 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5488 | onError: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5489 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5490 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5491 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5492 | return NULL; |
| 5493 | } |
| 5494 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5495 | static |
| 5496 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5497 | Py_ssize_t left, |
| 5498 | Py_ssize_t right, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5499 | Py_UNICODE fill) |
| 5500 | { |
| 5501 | PyUnicodeObject *u; |
| 5502 | |
| 5503 | if (left < 0) |
| 5504 | left = 0; |
| 5505 | if (right < 0) |
| 5506 | right = 0; |
| 5507 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5508 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5509 | Py_INCREF(self); |
| 5510 | return self; |
| 5511 | } |
| 5512 | |
| 5513 | u = _PyUnicode_New(left + self->length + right); |
| 5514 | if (u) { |
| 5515 | if (left) |
| 5516 | Py_UNICODE_FILL(u->str, fill, left); |
| 5517 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 5518 | if (right) |
| 5519 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 5520 | } |
| 5521 | |
| 5522 | return u; |
| 5523 | } |
| 5524 | |
| 5525 | #define SPLIT_APPEND(data, left, right) \ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5526 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5527 | if (!str) \ |
| 5528 | goto onError; \ |
| 5529 | if (PyList_Append(list, str)) { \ |
| 5530 | Py_DECREF(str); \ |
| 5531 | goto onError; \ |
| 5532 | } \ |
| 5533 | else \ |
| 5534 | Py_DECREF(str); |
| 5535 | |
| 5536 | static |
| 5537 | PyObject *split_whitespace(PyUnicodeObject *self, |
| 5538 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5539 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5540 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5541 | register Py_ssize_t i; |
| 5542 | register Py_ssize_t j; |
| 5543 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5544 | PyObject *str; |
| 5545 | |
| 5546 | for (i = j = 0; i < len; ) { |
| 5547 | /* find a token */ |
| 5548 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 5549 | i++; |
| 5550 | j = i; |
| 5551 | while (i < len && !Py_UNICODE_ISSPACE(self->str[i])) |
| 5552 | i++; |
| 5553 | if (j < i) { |
| 5554 | if (maxcount-- <= 0) |
| 5555 | break; |
| 5556 | SPLIT_APPEND(self->str, j, i); |
| 5557 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 5558 | i++; |
| 5559 | j = i; |
| 5560 | } |
| 5561 | } |
| 5562 | if (j < len) { |
| 5563 | SPLIT_APPEND(self->str, j, len); |
| 5564 | } |
| 5565 | return list; |
| 5566 | |
| 5567 | onError: |
| 5568 | Py_DECREF(list); |
| 5569 | return NULL; |
| 5570 | } |
| 5571 | |
| 5572 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5573 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5574 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5575 | register Py_ssize_t i; |
| 5576 | register Py_ssize_t j; |
| 5577 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5578 | PyObject *list; |
| 5579 | PyObject *str; |
| 5580 | Py_UNICODE *data; |
| 5581 | |
| 5582 | string = PyUnicode_FromObject(string); |
| 5583 | if (string == NULL) |
| 5584 | return NULL; |
| 5585 | data = PyUnicode_AS_UNICODE(string); |
| 5586 | len = PyUnicode_GET_SIZE(string); |
| 5587 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5588 | list = PyList_New(0); |
| 5589 | if (!list) |
| 5590 | goto onError; |
| 5591 | |
| 5592 | for (i = j = 0; i < len; ) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5593 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5594 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5595 | /* Find a line and append it */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5596 | while (i < len && !BLOOM_LINEBREAK(data[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5597 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5598 | |
| 5599 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5600 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5601 | if (i < len) { |
| 5602 | if (data[i] == '\r' && i + 1 < len && |
| 5603 | data[i+1] == '\n') |
| 5604 | i += 2; |
| 5605 | else |
| 5606 | i++; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5607 | if (keepends) |
| 5608 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5609 | } |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5610 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5611 | j = i; |
| 5612 | } |
| 5613 | if (j < len) { |
| 5614 | SPLIT_APPEND(data, j, len); |
| 5615 | } |
| 5616 | |
| 5617 | Py_DECREF(string); |
| 5618 | return list; |
| 5619 | |
| 5620 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5621 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5622 | Py_DECREF(string); |
| 5623 | return NULL; |
| 5624 | } |
| 5625 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5626 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5627 | PyObject *split_char(PyUnicodeObject *self, |
| 5628 | PyObject *list, |
| 5629 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5630 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5631 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5632 | register Py_ssize_t i; |
| 5633 | register Py_ssize_t j; |
| 5634 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5635 | PyObject *str; |
| 5636 | |
| 5637 | for (i = j = 0; i < len; ) { |
| 5638 | if (self->str[i] == ch) { |
| 5639 | if (maxcount-- <= 0) |
| 5640 | break; |
| 5641 | SPLIT_APPEND(self->str, j, i); |
| 5642 | i = j = i + 1; |
| 5643 | } else |
| 5644 | i++; |
| 5645 | } |
| 5646 | if (j <= len) { |
| 5647 | SPLIT_APPEND(self->str, j, len); |
| 5648 | } |
| 5649 | return list; |
| 5650 | |
| 5651 | onError: |
| 5652 | Py_DECREF(list); |
| 5653 | return NULL; |
| 5654 | } |
| 5655 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5656 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5657 | PyObject *split_substring(PyUnicodeObject *self, |
| 5658 | PyObject *list, |
| 5659 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5660 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5661 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5662 | register Py_ssize_t i; |
| 5663 | register Py_ssize_t j; |
| 5664 | Py_ssize_t len = self->length; |
| 5665 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5666 | PyObject *str; |
| 5667 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 5668 | for (i = j = 0; i <= len - sublen; ) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5669 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5670 | if (maxcount-- <= 0) |
| 5671 | break; |
| 5672 | SPLIT_APPEND(self->str, j, i); |
| 5673 | i = j = i + sublen; |
| 5674 | } else |
| 5675 | i++; |
| 5676 | } |
| 5677 | if (j <= len) { |
| 5678 | SPLIT_APPEND(self->str, j, len); |
| 5679 | } |
| 5680 | return list; |
| 5681 | |
| 5682 | onError: |
| 5683 | Py_DECREF(list); |
| 5684 | return NULL; |
| 5685 | } |
| 5686 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5687 | static |
| 5688 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
| 5689 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5690 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5691 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5692 | register Py_ssize_t i; |
| 5693 | register Py_ssize_t j; |
| 5694 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5695 | PyObject *str; |
| 5696 | |
| 5697 | for (i = j = len - 1; i >= 0; ) { |
| 5698 | /* find a token */ |
| 5699 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 5700 | i--; |
| 5701 | j = i; |
| 5702 | while (i >= 0 && !Py_UNICODE_ISSPACE(self->str[i])) |
| 5703 | i--; |
| 5704 | if (j > i) { |
| 5705 | if (maxcount-- <= 0) |
| 5706 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5707 | SPLIT_APPEND(self->str, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5708 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 5709 | i--; |
| 5710 | j = i; |
| 5711 | } |
| 5712 | } |
| 5713 | if (j >= 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5714 | SPLIT_APPEND(self->str, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5715 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5716 | if (PyList_Reverse(list) < 0) |
| 5717 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5718 | return list; |
| 5719 | |
| 5720 | onError: |
| 5721 | Py_DECREF(list); |
| 5722 | return NULL; |
| 5723 | } |
| 5724 | |
| 5725 | static |
| 5726 | PyObject *rsplit_char(PyUnicodeObject *self, |
| 5727 | PyObject *list, |
| 5728 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5729 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5730 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5731 | register Py_ssize_t i; |
| 5732 | register Py_ssize_t j; |
| 5733 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5734 | PyObject *str; |
| 5735 | |
| 5736 | for (i = j = len - 1; i >= 0; ) { |
| 5737 | if (self->str[i] == ch) { |
| 5738 | if (maxcount-- <= 0) |
| 5739 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5740 | SPLIT_APPEND(self->str, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5741 | j = i = i - 1; |
| 5742 | } else |
| 5743 | i--; |
| 5744 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 5745 | if (j >= -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5746 | SPLIT_APPEND(self->str, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5747 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5748 | if (PyList_Reverse(list) < 0) |
| 5749 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5750 | return list; |
| 5751 | |
| 5752 | onError: |
| 5753 | Py_DECREF(list); |
| 5754 | return NULL; |
| 5755 | } |
| 5756 | |
| 5757 | static |
| 5758 | PyObject *rsplit_substring(PyUnicodeObject *self, |
| 5759 | PyObject *list, |
| 5760 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5761 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5762 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5763 | register Py_ssize_t i; |
| 5764 | register Py_ssize_t j; |
| 5765 | Py_ssize_t len = self->length; |
| 5766 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5767 | PyObject *str; |
| 5768 | |
| 5769 | for (i = len - sublen, j = len; i >= 0; ) { |
| 5770 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5771 | if (maxcount-- <= 0) |
| 5772 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5773 | SPLIT_APPEND(self->str, i + sublen, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5774 | j = i; |
| 5775 | i -= sublen; |
| 5776 | } else |
| 5777 | i--; |
| 5778 | } |
| 5779 | if (j >= 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5780 | SPLIT_APPEND(self->str, 0, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5781 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5782 | if (PyList_Reverse(list) < 0) |
| 5783 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5784 | return list; |
| 5785 | |
| 5786 | onError: |
| 5787 | Py_DECREF(list); |
| 5788 | return NULL; |
| 5789 | } |
| 5790 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5791 | #undef SPLIT_APPEND |
| 5792 | |
| 5793 | static |
| 5794 | PyObject *split(PyUnicodeObject *self, |
| 5795 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5796 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5797 | { |
| 5798 | PyObject *list; |
| 5799 | |
| 5800 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5801 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5802 | |
| 5803 | list = PyList_New(0); |
| 5804 | if (!list) |
| 5805 | return NULL; |
| 5806 | |
| 5807 | if (substring == NULL) |
| 5808 | return split_whitespace(self,list,maxcount); |
| 5809 | |
| 5810 | else if (substring->length == 1) |
| 5811 | return split_char(self,list,substring->str[0],maxcount); |
| 5812 | |
| 5813 | else if (substring->length == 0) { |
| 5814 | Py_DECREF(list); |
| 5815 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5816 | return NULL; |
| 5817 | } |
| 5818 | else |
| 5819 | return split_substring(self,list,substring,maxcount); |
| 5820 | } |
| 5821 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5822 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5823 | PyObject *rsplit(PyUnicodeObject *self, |
| 5824 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5825 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5826 | { |
| 5827 | PyObject *list; |
| 5828 | |
| 5829 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5830 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5831 | |
| 5832 | list = PyList_New(0); |
| 5833 | if (!list) |
| 5834 | return NULL; |
| 5835 | |
| 5836 | if (substring == NULL) |
| 5837 | return rsplit_whitespace(self,list,maxcount); |
| 5838 | |
| 5839 | else if (substring->length == 1) |
| 5840 | return rsplit_char(self,list,substring->str[0],maxcount); |
| 5841 | |
| 5842 | else if (substring->length == 0) { |
| 5843 | Py_DECREF(list); |
| 5844 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5845 | return NULL; |
| 5846 | } |
| 5847 | else |
| 5848 | return rsplit_substring(self,list,substring,maxcount); |
| 5849 | } |
| 5850 | |
| 5851 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5852 | PyObject *replace(PyUnicodeObject *self, |
| 5853 | PyUnicodeObject *str1, |
| 5854 | PyUnicodeObject *str2, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5855 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5856 | { |
| 5857 | PyUnicodeObject *u; |
| 5858 | |
| 5859 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5860 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5861 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5862 | if (str1->length == str2->length) { |
| 5863 | /* same length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5864 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5865 | if (str1->length == 1) { |
| 5866 | /* replace characters */ |
| 5867 | Py_UNICODE u1, u2; |
| 5868 | if (!findchar(self->str, self->length, str1->str[0])) |
| 5869 | goto nothing; |
| 5870 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5871 | if (!u) |
| 5872 | return NULL; |
| 5873 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5874 | u1 = str1->str[0]; |
| 5875 | u2 = str2->str[0]; |
| 5876 | for (i = 0; i < u->length; i++) |
| 5877 | if (u->str[i] == u1) { |
| 5878 | if (--maxcount < 0) |
| 5879 | break; |
| 5880 | u->str[i] = u2; |
| 5881 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5882 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5883 | i = fastsearch( |
| 5884 | self->str, self->length, str1->str, str1->length, FAST_SEARCH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5885 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5886 | if (i < 0) |
| 5887 | goto nothing; |
| 5888 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5889 | if (!u) |
| 5890 | return NULL; |
| 5891 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5892 | while (i <= self->length - str1->length) |
| 5893 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 5894 | if (--maxcount < 0) |
| 5895 | break; |
| 5896 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 5897 | i += str1->length; |
| 5898 | } else |
| 5899 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5900 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5901 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5902 | |
| 5903 | Py_ssize_t n, i, j, e; |
| 5904 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5905 | Py_UNICODE *p; |
| 5906 | |
| 5907 | /* replace strings */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5908 | n = stringlib_count(self->str, self->length, str1->str, str1->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5909 | if (n > maxcount) |
| 5910 | n = maxcount; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5911 | if (n == 0) |
| 5912 | goto nothing; |
| 5913 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 5914 | delta = (str2->length - str1->length); |
| 5915 | if (delta == 0) { |
| 5916 | new_size = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5917 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5918 | product = n * (str2->length - str1->length); |
| 5919 | if ((product / (str2->length - str1->length)) != n) { |
| 5920 | PyErr_SetString(PyExc_OverflowError, |
| 5921 | "replace string is too long"); |
| 5922 | return NULL; |
| 5923 | } |
| 5924 | new_size = self->length + product; |
| 5925 | if (new_size < 0) { |
| 5926 | PyErr_SetString(PyExc_OverflowError, |
| 5927 | "replace string is too long"); |
| 5928 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5929 | } |
| 5930 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5931 | u = _PyUnicode_New(new_size); |
| 5932 | if (!u) |
| 5933 | return NULL; |
| 5934 | i = 0; |
| 5935 | p = u->str; |
| 5936 | e = self->length - str1->length; |
| 5937 | if (str1->length > 0) { |
| 5938 | while (n-- > 0) { |
| 5939 | /* look for next match */ |
| 5940 | j = i; |
| 5941 | while (j <= e) { |
| 5942 | if (Py_UNICODE_MATCH(self, j, str1)) |
| 5943 | break; |
| 5944 | j++; |
| 5945 | } |
| 5946 | if (j > i) { |
| 5947 | if (j > e) |
| 5948 | break; |
| 5949 | /* copy unchanged part [i:j] */ |
| 5950 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 5951 | p += j - i; |
| 5952 | } |
| 5953 | /* copy substitution string */ |
| 5954 | if (str2->length > 0) { |
| 5955 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 5956 | p += str2->length; |
| 5957 | } |
| 5958 | i = j + str1->length; |
| 5959 | } |
| 5960 | if (i < self->length) |
| 5961 | /* copy tail [i:] */ |
| 5962 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 5963 | } else { |
| 5964 | /* interleave */ |
| 5965 | while (n > 0) { |
| 5966 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 5967 | p += str2->length; |
| 5968 | if (--n <= 0) |
| 5969 | break; |
| 5970 | *p++ = self->str[i++]; |
| 5971 | } |
| 5972 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 5973 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5974 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5975 | return (PyObject *) u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5976 | |
| 5977 | nothing: |
| 5978 | /* nothing to replace; return original string (when possible) */ |
| 5979 | if (PyUnicode_CheckExact(self)) { |
| 5980 | Py_INCREF(self); |
| 5981 | return (PyObject *) self; |
| 5982 | } |
| 5983 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5984 | } |
| 5985 | |
| 5986 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 5987 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5988 | PyDoc_STRVAR(title__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5989 | "S.title() -> unicode\n\ |
| 5990 | \n\ |
| 5991 | 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] | 5992 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5993 | |
| 5994 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5995 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5996 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5997 | return fixup(self, fixtitle); |
| 5998 | } |
| 5999 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6000 | PyDoc_STRVAR(capitalize__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6001 | "S.capitalize() -> unicode\n\ |
| 6002 | \n\ |
| 6003 | 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] | 6004 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6005 | |
| 6006 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6007 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6008 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6009 | return fixup(self, fixcapitalize); |
| 6010 | } |
| 6011 | |
| 6012 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6013 | PyDoc_STRVAR(capwords__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6014 | "S.capwords() -> unicode\n\ |
| 6015 | \n\ |
| 6016 | 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] | 6017 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6018 | |
| 6019 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6020 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6021 | { |
| 6022 | PyObject *list; |
| 6023 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6024 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6025 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6026 | /* Split into words */ |
| 6027 | list = split(self, NULL, -1); |
| 6028 | if (!list) |
| 6029 | return NULL; |
| 6030 | |
| 6031 | /* Capitalize each word */ |
| 6032 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 6033 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 6034 | fixcapitalize); |
| 6035 | if (item == NULL) |
| 6036 | goto onError; |
| 6037 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 6038 | PyList_SET_ITEM(list, i, item); |
| 6039 | } |
| 6040 | |
| 6041 | /* Join the words to form a new string */ |
| 6042 | item = PyUnicode_Join(NULL, list); |
| 6043 | |
| 6044 | onError: |
| 6045 | Py_DECREF(list); |
| 6046 | return (PyObject *)item; |
| 6047 | } |
| 6048 | #endif |
| 6049 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6050 | /* Argument converter. Coerces to a single unicode character */ |
| 6051 | |
| 6052 | static int |
| 6053 | convert_uc(PyObject *obj, void *addr) |
| 6054 | { |
| 6055 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 6056 | PyObject *uniobj; |
| 6057 | Py_UNICODE *unistr; |
| 6058 | |
| 6059 | uniobj = PyUnicode_FromObject(obj); |
| 6060 | if (uniobj == NULL) { |
| 6061 | PyErr_SetString(PyExc_TypeError, |
| 6062 | "The fill character cannot be converted to Unicode"); |
| 6063 | return 0; |
| 6064 | } |
| 6065 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 6066 | PyErr_SetString(PyExc_TypeError, |
| 6067 | "The fill character must be exactly one character long"); |
| 6068 | Py_DECREF(uniobj); |
| 6069 | return 0; |
| 6070 | } |
| 6071 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 6072 | *fillcharloc = unistr[0]; |
| 6073 | Py_DECREF(uniobj); |
| 6074 | return 1; |
| 6075 | } |
| 6076 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6077 | PyDoc_STRVAR(center__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6078 | "S.center(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6079 | \n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6080 | Return S centered in a Unicode string of length width. Padding is\n\ |
| 6081 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6082 | |
| 6083 | static PyObject * |
| 6084 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 6085 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6086 | Py_ssize_t marg, left; |
| 6087 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6088 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6089 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6090 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6091 | return NULL; |
| 6092 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6093 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6094 | Py_INCREF(self); |
| 6095 | return (PyObject*) self; |
| 6096 | } |
| 6097 | |
| 6098 | marg = width - self->length; |
| 6099 | left = marg / 2 + (marg & width & 1); |
| 6100 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6101 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6102 | } |
| 6103 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6104 | #if 0 |
| 6105 | |
| 6106 | /* This code should go into some future Unicode collation support |
| 6107 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 6108 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6109 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6110 | /* speedy UTF-16 code point order comparison */ |
| 6111 | /* gleaned from: */ |
| 6112 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 6113 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6114 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6115 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6116 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6117 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 6118 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6119 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6120 | }; |
| 6121 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6122 | static int |
| 6123 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6124 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6125 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6126 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6127 | Py_UNICODE *s1 = str1->str; |
| 6128 | Py_UNICODE *s2 = str2->str; |
| 6129 | |
| 6130 | len1 = str1->length; |
| 6131 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6132 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6133 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6134 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6135 | |
| 6136 | c1 = *s1++; |
| 6137 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6138 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6139 | if (c1 > (1<<11) * 26) |
| 6140 | c1 += utf16Fixup[c1>>11]; |
| 6141 | if (c2 > (1<<11) * 26) |
| 6142 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6143 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6144 | |
| 6145 | if (c1 != c2) |
| 6146 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6147 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6148 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6149 | } |
| 6150 | |
| 6151 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6152 | } |
| 6153 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6154 | #else |
| 6155 | |
| 6156 | static int |
| 6157 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6158 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6159 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6160 | |
| 6161 | Py_UNICODE *s1 = str1->str; |
| 6162 | Py_UNICODE *s2 = str2->str; |
| 6163 | |
| 6164 | len1 = str1->length; |
| 6165 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6166 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6167 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6168 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6169 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6170 | c1 = *s1++; |
| 6171 | c2 = *s2++; |
| 6172 | |
| 6173 | if (c1 != c2) |
| 6174 | return (c1 < c2) ? -1 : 1; |
| 6175 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6176 | len1--; len2--; |
| 6177 | } |
| 6178 | |
| 6179 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6180 | } |
| 6181 | |
| 6182 | #endif |
| 6183 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6184 | int PyUnicode_Compare(PyObject *left, |
| 6185 | PyObject *right) |
| 6186 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6187 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) |
| 6188 | return unicode_compare((PyUnicodeObject *)left, |
| 6189 | (PyUnicodeObject *)right); |
| 6190 | if ((PyString_Check(left) && PyUnicode_Check(right)) || |
| 6191 | (PyUnicode_Check(left) && PyString_Check(right))) { |
| 6192 | if (PyUnicode_Check(left)) |
| 6193 | left = _PyUnicode_AsDefaultEncodedString(left, NULL); |
| 6194 | if (PyUnicode_Check(right)) |
| 6195 | right = _PyUnicode_AsDefaultEncodedString(right, NULL); |
| 6196 | assert(PyString_Check(left)); |
| 6197 | assert(PyString_Check(right)); |
| 6198 | return PyObject_Compare(left, right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6199 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6200 | PyErr_Format(PyExc_TypeError, |
| 6201 | "Can't compare %.100s and %.100s", |
| 6202 | left->ob_type->tp_name, |
| 6203 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6204 | return -1; |
| 6205 | } |
| 6206 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6207 | int |
| 6208 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 6209 | { |
| 6210 | int i; |
| 6211 | Py_UNICODE *id; |
| 6212 | assert(PyUnicode_Check(uni)); |
| 6213 | id = PyUnicode_AS_UNICODE(uni); |
| 6214 | /* Compare Unicode string and source character set string */ |
| 6215 | for (i = 0; id[i] && str[i]; i++) |
| 6216 | if (id[i] != str[i]) |
| 6217 | return ((int)id[i] < (int)str[i]) ? -1 : 1; |
| 6218 | if (id[i]) |
| 6219 | return 1; /* uni is longer */ |
| 6220 | if (str[i]) |
| 6221 | return -1; /* str is longer */ |
| 6222 | return 0; |
| 6223 | } |
| 6224 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6225 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 6226 | PyObject *right, |
| 6227 | int op) |
| 6228 | { |
| 6229 | int result; |
| 6230 | |
| 6231 | result = PyUnicode_Compare(left, right); |
| 6232 | if (result == -1 && PyErr_Occurred()) |
| 6233 | goto onError; |
| 6234 | |
| 6235 | /* Convert the return value to a Boolean */ |
| 6236 | switch (op) { |
| 6237 | case Py_EQ: |
| 6238 | result = (result == 0); |
| 6239 | break; |
| 6240 | case Py_NE: |
| 6241 | result = (result != 0); |
| 6242 | break; |
| 6243 | case Py_LE: |
| 6244 | result = (result <= 0); |
| 6245 | break; |
| 6246 | case Py_GE: |
| 6247 | result = (result >= 0); |
| 6248 | break; |
| 6249 | case Py_LT: |
| 6250 | result = (result == -1); |
| 6251 | break; |
| 6252 | case Py_GT: |
| 6253 | result = (result == 1); |
| 6254 | break; |
| 6255 | } |
| 6256 | return PyBool_FromLong(result); |
| 6257 | |
| 6258 | onError: |
| 6259 | |
| 6260 | /* Standard case |
| 6261 | |
| 6262 | Type errors mean that PyUnicode_FromObject() could not convert |
| 6263 | one of the arguments (usually the right hand side) to Unicode, |
| 6264 | ie. we can't handle the comparison request. However, it is |
| 6265 | possible that the other object knows a comparison method, which |
| 6266 | is why we return Py_NotImplemented to give the other object a |
| 6267 | chance. |
| 6268 | |
| 6269 | */ |
| 6270 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 6271 | PyErr_Clear(); |
| 6272 | Py_INCREF(Py_NotImplemented); |
| 6273 | return Py_NotImplemented; |
| 6274 | } |
| 6275 | if (op != Py_EQ && op != Py_NE) |
| 6276 | return NULL; |
| 6277 | |
| 6278 | /* Equality comparison. |
| 6279 | |
| 6280 | This is a special case: we silence any PyExc_UnicodeDecodeError |
| 6281 | and instead turn it into a PyErr_UnicodeWarning. |
| 6282 | |
| 6283 | */ |
| 6284 | if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) |
| 6285 | return NULL; |
| 6286 | PyErr_Clear(); |
Skip Montanaro | 46fc337 | 2007-08-12 11:44:53 +0000 | [diff] [blame] | 6287 | if (PyErr_WarnEx(PyExc_UnicodeWarning, |
| 6288 | (op == Py_EQ) ? |
| 6289 | "Unicode equal comparison " |
| 6290 | "failed to convert both arguments to Unicode - " |
| 6291 | "interpreting them as being unequal" |
| 6292 | : |
| 6293 | "Unicode unequal comparison " |
| 6294 | "failed to convert both arguments to Unicode - " |
| 6295 | "interpreting them as being unequal", |
| 6296 | 1) < 0) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6297 | return NULL; |
| 6298 | result = (op == Py_NE); |
| 6299 | return PyBool_FromLong(result); |
| 6300 | } |
| 6301 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6302 | int PyUnicode_Contains(PyObject *container, |
| 6303 | PyObject *element) |
| 6304 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6305 | PyObject *str, *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6306 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6307 | |
| 6308 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6309 | sub = PyUnicode_FromObject(element); |
| 6310 | if (!sub) { |
Walter Dörwald | 26e0f51 | 2007-06-12 16:51:31 +0000 | [diff] [blame] | 6311 | PyErr_Format(PyExc_TypeError, |
| 6312 | "'in <string>' requires string as left operand, not %s", |
| 6313 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6314 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6315 | } |
| 6316 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6317 | str = PyUnicode_FromObject(container); |
| 6318 | if (!str) { |
| 6319 | Py_DECREF(sub); |
| 6320 | return -1; |
| 6321 | } |
| 6322 | |
| 6323 | result = stringlib_contains_obj(str, sub); |
| 6324 | |
| 6325 | Py_DECREF(str); |
| 6326 | Py_DECREF(sub); |
| 6327 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6328 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6329 | } |
| 6330 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6331 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 6332 | |
| 6333 | PyObject *PyUnicode_Concat(PyObject *left, |
| 6334 | PyObject *right) |
| 6335 | { |
| 6336 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 6337 | |
Guido van Rossum | 84d79dd | 2007-04-13 02:23:57 +0000 | [diff] [blame] | 6338 | if (PyBytes_Check(left) || PyBytes_Check(right)) |
| 6339 | return PyBytes_Concat(left, right); |
| 6340 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6341 | /* Coerce the two arguments */ |
| 6342 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 6343 | if (u == NULL) |
| 6344 | goto onError; |
| 6345 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 6346 | if (v == NULL) |
| 6347 | goto onError; |
| 6348 | |
| 6349 | /* Shortcuts */ |
| 6350 | if (v == unicode_empty) { |
| 6351 | Py_DECREF(v); |
| 6352 | return (PyObject *)u; |
| 6353 | } |
| 6354 | if (u == unicode_empty) { |
| 6355 | Py_DECREF(u); |
| 6356 | return (PyObject *)v; |
| 6357 | } |
| 6358 | |
| 6359 | /* Concat the two Unicode strings */ |
| 6360 | w = _PyUnicode_New(u->length + v->length); |
| 6361 | if (w == NULL) |
| 6362 | goto onError; |
| 6363 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 6364 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 6365 | |
| 6366 | Py_DECREF(u); |
| 6367 | Py_DECREF(v); |
| 6368 | return (PyObject *)w; |
| 6369 | |
| 6370 | onError: |
| 6371 | Py_XDECREF(u); |
| 6372 | Py_XDECREF(v); |
| 6373 | return NULL; |
| 6374 | } |
| 6375 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6376 | void |
| 6377 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 6378 | { |
| 6379 | PyObject *new; |
| 6380 | if (*pleft == NULL) |
| 6381 | return; |
| 6382 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 6383 | Py_DECREF(*pleft); |
| 6384 | *pleft = NULL; |
| 6385 | return; |
| 6386 | } |
| 6387 | new = PyUnicode_Concat(*pleft, right); |
| 6388 | Py_DECREF(*pleft); |
| 6389 | *pleft = new; |
| 6390 | } |
| 6391 | |
| 6392 | void |
| 6393 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 6394 | { |
| 6395 | PyUnicode_Append(pleft, right); |
| 6396 | Py_XDECREF(right); |
| 6397 | } |
| 6398 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6399 | PyDoc_STRVAR(count__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6400 | "S.count(sub[, start[, end]]) -> int\n\ |
| 6401 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6402 | Return the number of non-overlapping occurrences of substring sub in\n\ |
| 6403 | 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] | 6404 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6405 | |
| 6406 | static PyObject * |
| 6407 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 6408 | { |
| 6409 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6410 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6411 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6412 | PyObject *result; |
| 6413 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6414 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 6415 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6416 | return NULL; |
| 6417 | |
| 6418 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6419 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6420 | if (substring == NULL) |
| 6421 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6422 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6423 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6424 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6425 | result = PyInt_FromSsize_t( |
| 6426 | stringlib_count(self->str + start, end - start, |
| 6427 | substring->str, substring->length) |
| 6428 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6429 | |
| 6430 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6431 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6432 | return result; |
| 6433 | } |
| 6434 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6435 | PyDoc_STRVAR(encode__doc__, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6436 | "S.encode([encoding[,errors]]) -> string or unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6437 | \n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6438 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 6439 | 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] | 6440 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6441 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 6442 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 6443 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6444 | |
| 6445 | static PyObject * |
| 6446 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 6447 | { |
| 6448 | char *encoding = NULL; |
| 6449 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6450 | PyObject *v; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 6451 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6452 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 6453 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6454 | v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6455 | if (v == NULL) |
| 6456 | goto onError; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 6457 | if (!PyBytes_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6458 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 6459 | "encoder did not return a bytes object " |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6460 | "(type=%.400s)", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 6461 | Py_Type(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6462 | Py_DECREF(v); |
| 6463 | return NULL; |
| 6464 | } |
| 6465 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6466 | |
| 6467 | onError: |
| 6468 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6469 | } |
| 6470 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6471 | PyDoc_STRVAR(expandtabs__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6472 | "S.expandtabs([tabsize]) -> unicode\n\ |
| 6473 | \n\ |
| 6474 | 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] | 6475 | 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] | 6476 | |
| 6477 | static PyObject* |
| 6478 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 6479 | { |
| 6480 | Py_UNICODE *e; |
| 6481 | Py_UNICODE *p; |
| 6482 | Py_UNICODE *q; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6483 | Py_ssize_t i, j, old_j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6484 | PyUnicodeObject *u; |
| 6485 | int tabsize = 8; |
| 6486 | |
| 6487 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 6488 | return NULL; |
| 6489 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6490 | /* First pass: determine size of output string */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6491 | i = j = old_j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6492 | e = self->str + self->length; |
| 6493 | for (p = self->str; p < e; p++) |
| 6494 | if (*p == '\t') { |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6495 | if (tabsize > 0) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6496 | j += tabsize - (j % tabsize); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6497 | if (old_j > j) { |
| 6498 | PyErr_SetString(PyExc_OverflowError, |
| 6499 | "new string is too long"); |
| 6500 | return NULL; |
| 6501 | } |
| 6502 | old_j = j; |
| 6503 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6504 | } |
| 6505 | else { |
| 6506 | j++; |
| 6507 | if (*p == '\n' || *p == '\r') { |
| 6508 | i += j; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6509 | old_j = j = 0; |
| 6510 | if (i < 0) { |
| 6511 | PyErr_SetString(PyExc_OverflowError, |
| 6512 | "new string is too long"); |
| 6513 | return NULL; |
| 6514 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6515 | } |
| 6516 | } |
| 6517 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6518 | if ((i + j) < 0) { |
| 6519 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 6520 | return NULL; |
| 6521 | } |
| 6522 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6523 | /* Second pass: create output string and fill it */ |
| 6524 | u = _PyUnicode_New(i + j); |
| 6525 | if (!u) |
| 6526 | return NULL; |
| 6527 | |
| 6528 | j = 0; |
| 6529 | q = u->str; |
| 6530 | |
| 6531 | for (p = self->str; p < e; p++) |
| 6532 | if (*p == '\t') { |
| 6533 | if (tabsize > 0) { |
| 6534 | i = tabsize - (j % tabsize); |
| 6535 | j += i; |
| 6536 | while (i--) |
| 6537 | *q++ = ' '; |
| 6538 | } |
| 6539 | } |
| 6540 | else { |
| 6541 | j++; |
| 6542 | *q++ = *p; |
| 6543 | if (*p == '\n' || *p == '\r') |
| 6544 | j = 0; |
| 6545 | } |
| 6546 | |
| 6547 | return (PyObject*) u; |
| 6548 | } |
| 6549 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6550 | PyDoc_STRVAR(find__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6551 | "S.find(sub [,start [,end]]) -> int\n\ |
| 6552 | \n\ |
| 6553 | Return the lowest index in S where substring sub is found,\n\ |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 6554 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6555 | arguments start and end are interpreted as in slice notation.\n\ |
| 6556 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6557 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6558 | |
| 6559 | static PyObject * |
| 6560 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 6561 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6562 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6563 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6564 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6565 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6566 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6567 | if (!PyArg_ParseTuple(args, "O|O&O&:find", &substring, |
| 6568 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6569 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6570 | substring = PyUnicode_FromObject(substring); |
| 6571 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6572 | return NULL; |
| 6573 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6574 | result = stringlib_find_slice( |
| 6575 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6576 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6577 | start, end |
| 6578 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6579 | |
| 6580 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6581 | |
| 6582 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6583 | } |
| 6584 | |
| 6585 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6586 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6587 | { |
| 6588 | if (index < 0 || index >= self->length) { |
| 6589 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 6590 | return NULL; |
| 6591 | } |
| 6592 | |
| 6593 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 6594 | } |
| 6595 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 6596 | /* Believe it or not, this produces the same value for ASCII strings |
| 6597 | as string_hash(). */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6598 | static long |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 6599 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6600 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 6601 | Py_ssize_t len; |
| 6602 | Py_UNICODE *p; |
| 6603 | long x; |
| 6604 | |
| 6605 | if (self->hash != -1) |
| 6606 | return self->hash; |
| 6607 | len = Py_Size(self); |
| 6608 | p = self->str; |
| 6609 | x = *p << 7; |
| 6610 | while (--len >= 0) |
| 6611 | x = (1000003*x) ^ *p++; |
| 6612 | x ^= Py_Size(self); |
| 6613 | if (x == -1) |
| 6614 | x = -2; |
| 6615 | self->hash = x; |
| 6616 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6617 | } |
| 6618 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6619 | PyDoc_STRVAR(index__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6620 | "S.index(sub [,start [,end]]) -> int\n\ |
| 6621 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6622 | 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] | 6623 | |
| 6624 | static PyObject * |
| 6625 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 6626 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6627 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6628 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6629 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6630 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6631 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6632 | if (!PyArg_ParseTuple(args, "O|O&O&:index", &substring, |
| 6633 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6634 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6635 | substring = PyUnicode_FromObject(substring); |
| 6636 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6637 | return NULL; |
| 6638 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6639 | result = stringlib_find_slice( |
| 6640 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6641 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6642 | start, end |
| 6643 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6644 | |
| 6645 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6646 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6647 | if (result < 0) { |
| 6648 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 6649 | return NULL; |
| 6650 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6651 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6652 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6653 | } |
| 6654 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6655 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6656 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6657 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6658 | 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] | 6659 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6660 | |
| 6661 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6662 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6663 | { |
| 6664 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6665 | register const Py_UNICODE *e; |
| 6666 | int cased; |
| 6667 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6668 | /* Shortcut for single character strings */ |
| 6669 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6670 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6671 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6672 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6673 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6674 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6675 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6676 | e = p + PyUnicode_GET_SIZE(self); |
| 6677 | cased = 0; |
| 6678 | for (; p < e; p++) { |
| 6679 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6680 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6681 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6682 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6683 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 6684 | cased = 1; |
| 6685 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6686 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6687 | } |
| 6688 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6689 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6690 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6691 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6692 | 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] | 6693 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6694 | |
| 6695 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6696 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6697 | { |
| 6698 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6699 | register const Py_UNICODE *e; |
| 6700 | int cased; |
| 6701 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6702 | /* Shortcut for single character strings */ |
| 6703 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6704 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6705 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6706 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6707 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6708 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6709 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6710 | e = p + PyUnicode_GET_SIZE(self); |
| 6711 | cased = 0; |
| 6712 | for (; p < e; p++) { |
| 6713 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6714 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6715 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6716 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6717 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 6718 | cased = 1; |
| 6719 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6720 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6721 | } |
| 6722 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6723 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6724 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6725 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6726 | Return True if S is a titlecased string and there is at least one\n\ |
| 6727 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 6728 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 6729 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6730 | |
| 6731 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6732 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6733 | { |
| 6734 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6735 | register const Py_UNICODE *e; |
| 6736 | int cased, previous_is_cased; |
| 6737 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6738 | /* Shortcut for single character strings */ |
| 6739 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6740 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 6741 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6742 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6743 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6744 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6745 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6746 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6747 | e = p + PyUnicode_GET_SIZE(self); |
| 6748 | cased = 0; |
| 6749 | previous_is_cased = 0; |
| 6750 | for (; p < e; p++) { |
| 6751 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6752 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6753 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 6754 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6755 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6756 | previous_is_cased = 1; |
| 6757 | cased = 1; |
| 6758 | } |
| 6759 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 6760 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6761 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6762 | previous_is_cased = 1; |
| 6763 | cased = 1; |
| 6764 | } |
| 6765 | else |
| 6766 | previous_is_cased = 0; |
| 6767 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6768 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6769 | } |
| 6770 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6771 | PyDoc_STRVAR(isspace__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6772 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6773 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6774 | Return True if all characters in S are whitespace\n\ |
| 6775 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6776 | |
| 6777 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6778 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6779 | { |
| 6780 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6781 | register const Py_UNICODE *e; |
| 6782 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6783 | /* Shortcut for single character strings */ |
| 6784 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6785 | Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6786 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6787 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6788 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6789 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6790 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6791 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6792 | e = p + PyUnicode_GET_SIZE(self); |
| 6793 | for (; p < e; p++) { |
| 6794 | if (!Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6795 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6796 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6797 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6798 | } |
| 6799 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6800 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6801 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6802 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6803 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6804 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6805 | |
| 6806 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6807 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6808 | { |
| 6809 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6810 | register const Py_UNICODE *e; |
| 6811 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6812 | /* Shortcut for single character strings */ |
| 6813 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6814 | Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6815 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6816 | |
| 6817 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6818 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6819 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6820 | |
| 6821 | e = p + PyUnicode_GET_SIZE(self); |
| 6822 | for (; p < e; p++) { |
| 6823 | if (!Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6824 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6825 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6826 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6827 | } |
| 6828 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6829 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6830 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6831 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6832 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6833 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6834 | |
| 6835 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6836 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6837 | { |
| 6838 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6839 | register const Py_UNICODE *e; |
| 6840 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6841 | /* Shortcut for single character strings */ |
| 6842 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6843 | Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6844 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6845 | |
| 6846 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6847 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6848 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6849 | |
| 6850 | e = p + PyUnicode_GET_SIZE(self); |
| 6851 | for (; p < e; p++) { |
| 6852 | if (!Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6853 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6854 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6855 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6856 | } |
| 6857 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6858 | PyDoc_STRVAR(isdecimal__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6859 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6860 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6861 | 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] | 6862 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6863 | |
| 6864 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6865 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6866 | { |
| 6867 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6868 | register const Py_UNICODE *e; |
| 6869 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6870 | /* Shortcut for single character strings */ |
| 6871 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6872 | Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6873 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6874 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6875 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6876 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6877 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6878 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6879 | e = p + PyUnicode_GET_SIZE(self); |
| 6880 | for (; p < e; p++) { |
| 6881 | if (!Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6882 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6883 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6884 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6885 | } |
| 6886 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6887 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6888 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6889 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6890 | Return True if all characters in S are digits\n\ |
| 6891 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6892 | |
| 6893 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6894 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6895 | { |
| 6896 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6897 | register const Py_UNICODE *e; |
| 6898 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6899 | /* Shortcut for single character strings */ |
| 6900 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6901 | Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6902 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6903 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6904 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6905 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6906 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6907 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6908 | e = p + PyUnicode_GET_SIZE(self); |
| 6909 | for (; p < e; p++) { |
| 6910 | if (!Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6911 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6912 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6913 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6914 | } |
| 6915 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6916 | PyDoc_STRVAR(isnumeric__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6917 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6918 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6919 | 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] | 6920 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6921 | |
| 6922 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6923 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6924 | { |
| 6925 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6926 | register const Py_UNICODE *e; |
| 6927 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6928 | /* Shortcut for single character strings */ |
| 6929 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6930 | Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6931 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6932 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6933 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6934 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6935 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6936 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6937 | e = p + PyUnicode_GET_SIZE(self); |
| 6938 | for (; p < e; p++) { |
| 6939 | if (!Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6940 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6941 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6942 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6943 | } |
| 6944 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 6945 | int |
| 6946 | PyUnicode_IsIdentifier(PyObject *self) |
| 6947 | { |
| 6948 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE((PyUnicodeObject*)self); |
| 6949 | register const Py_UNICODE *e; |
| 6950 | |
| 6951 | /* Special case for empty strings */ |
| 6952 | if (PyUnicode_GET_SIZE(self) == 0) |
| 6953 | return 0; |
| 6954 | |
| 6955 | /* PEP 3131 says that the first character must be in |
| 6956 | XID_Start and subsequent characters in XID_Continue, |
| 6957 | and for the ASCII range, the 2.x rules apply (i.e |
| 6958 | start with letters and underscore, continue with |
| 6959 | letters, digits, underscore). However, given the current |
| 6960 | definition of XID_Start and XID_Continue, it is sufficient |
| 6961 | to check just for these, except that _ must be allowed |
| 6962 | as starting an identifier. */ |
| 6963 | if (!_PyUnicode_IsXidStart(*p) && *p != 0x5F /* LOW LINE */) |
| 6964 | return 0; |
| 6965 | |
| 6966 | e = p + PyUnicode_GET_SIZE(self); |
| 6967 | for (p++; p < e; p++) { |
| 6968 | if (!_PyUnicode_IsXidContinue(*p)) |
| 6969 | return 0; |
| 6970 | } |
| 6971 | return 1; |
| 6972 | } |
| 6973 | |
| 6974 | PyDoc_STRVAR(isidentifier__doc__, |
| 6975 | "S.isidentifier() -> bool\n\ |
| 6976 | \n\ |
| 6977 | Return True if S is a valid identifier according\n\ |
| 6978 | to the language definition."); |
| 6979 | |
| 6980 | static PyObject* |
| 6981 | unicode_isidentifier(PyObject *self) |
| 6982 | { |
| 6983 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 6984 | } |
| 6985 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6986 | PyDoc_STRVAR(join__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6987 | "S.join(sequence) -> unicode\n\ |
| 6988 | \n\ |
| 6989 | 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] | 6990 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6991 | |
| 6992 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6993 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6994 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6995 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6996 | } |
| 6997 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6998 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6999 | unicode_length(PyUnicodeObject *self) |
| 7000 | { |
| 7001 | return self->length; |
| 7002 | } |
| 7003 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7004 | PyDoc_STRVAR(ljust__doc__, |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 7005 | "S.ljust(width[, fillchar]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7006 | \n\ |
| 7007 | 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] | 7008 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7009 | |
| 7010 | static PyObject * |
| 7011 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 7012 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7013 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7014 | Py_UNICODE fillchar = ' '; |
| 7015 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7016 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7017 | return NULL; |
| 7018 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7019 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7020 | Py_INCREF(self); |
| 7021 | return (PyObject*) self; |
| 7022 | } |
| 7023 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7024 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7025 | } |
| 7026 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7027 | PyDoc_STRVAR(lower__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7028 | "S.lower() -> unicode\n\ |
| 7029 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7030 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7031 | |
| 7032 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7033 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7034 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7035 | return fixup(self, fixlower); |
| 7036 | } |
| 7037 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7038 | #define LEFTSTRIP 0 |
| 7039 | #define RIGHTSTRIP 1 |
| 7040 | #define BOTHSTRIP 2 |
| 7041 | |
| 7042 | /* Arrays indexed by above */ |
| 7043 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 7044 | |
| 7045 | #define STRIPNAME(i) (stripformat[i]+3) |
| 7046 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7047 | /* externally visible for str.strip(unicode) */ |
| 7048 | PyObject * |
| 7049 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 7050 | { |
| 7051 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7052 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7053 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7054 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 7055 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7056 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7057 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
| 7058 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7059 | i = 0; |
| 7060 | if (striptype != RIGHTSTRIP) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7061 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7062 | i++; |
| 7063 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7064 | } |
| 7065 | |
| 7066 | j = len; |
| 7067 | if (striptype != LEFTSTRIP) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7068 | do { |
| 7069 | j--; |
| 7070 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 7071 | j++; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7072 | } |
| 7073 | |
| 7074 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7075 | Py_INCREF(self); |
| 7076 | return (PyObject*)self; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7077 | } |
| 7078 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7079 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7080 | } |
| 7081 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7082 | |
| 7083 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7084 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7085 | { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7086 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7087 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7088 | |
| 7089 | i = 0; |
| 7090 | if (striptype != RIGHTSTRIP) { |
| 7091 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 7092 | i++; |
| 7093 | } |
| 7094 | } |
| 7095 | |
| 7096 | j = len; |
| 7097 | if (striptype != LEFTSTRIP) { |
| 7098 | do { |
| 7099 | j--; |
| 7100 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 7101 | j++; |
| 7102 | } |
| 7103 | |
| 7104 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 7105 | Py_INCREF(self); |
| 7106 | return (PyObject*)self; |
| 7107 | } |
| 7108 | else |
| 7109 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7110 | } |
| 7111 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7112 | |
| 7113 | static PyObject * |
| 7114 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 7115 | { |
| 7116 | PyObject *sep = NULL; |
| 7117 | |
| 7118 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 7119 | return NULL; |
| 7120 | |
| 7121 | if (sep != NULL && sep != Py_None) { |
| 7122 | if (PyUnicode_Check(sep)) |
| 7123 | return _PyUnicode_XStrip(self, striptype, sep); |
| 7124 | else if (PyString_Check(sep)) { |
| 7125 | PyObject *res; |
| 7126 | sep = PyUnicode_FromObject(sep); |
| 7127 | if (sep==NULL) |
| 7128 | return NULL; |
| 7129 | res = _PyUnicode_XStrip(self, striptype, sep); |
| 7130 | Py_DECREF(sep); |
| 7131 | return res; |
| 7132 | } |
| 7133 | else { |
| 7134 | PyErr_Format(PyExc_TypeError, |
| 7135 | "%s arg must be None, unicode or str", |
| 7136 | STRIPNAME(striptype)); |
| 7137 | return NULL; |
| 7138 | } |
| 7139 | } |
| 7140 | |
| 7141 | return do_strip(self, striptype); |
| 7142 | } |
| 7143 | |
| 7144 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7145 | PyDoc_STRVAR(strip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7146 | "S.strip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7147 | \n\ |
| 7148 | Return a copy of the string S with leading and trailing\n\ |
| 7149 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7150 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7151 | 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] | 7152 | |
| 7153 | static PyObject * |
| 7154 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 7155 | { |
| 7156 | if (PyTuple_GET_SIZE(args) == 0) |
| 7157 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 7158 | else |
| 7159 | return do_argstrip(self, BOTHSTRIP, args); |
| 7160 | } |
| 7161 | |
| 7162 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7163 | PyDoc_STRVAR(lstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7164 | "S.lstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7165 | \n\ |
| 7166 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7167 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7168 | 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] | 7169 | |
| 7170 | static PyObject * |
| 7171 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 7172 | { |
| 7173 | if (PyTuple_GET_SIZE(args) == 0) |
| 7174 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 7175 | else |
| 7176 | return do_argstrip(self, LEFTSTRIP, args); |
| 7177 | } |
| 7178 | |
| 7179 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7180 | PyDoc_STRVAR(rstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7181 | "S.rstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7182 | \n\ |
| 7183 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7184 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7185 | 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] | 7186 | |
| 7187 | static PyObject * |
| 7188 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 7189 | { |
| 7190 | if (PyTuple_GET_SIZE(args) == 0) |
| 7191 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 7192 | else |
| 7193 | return do_argstrip(self, RIGHTSTRIP, args); |
| 7194 | } |
| 7195 | |
| 7196 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7197 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7198 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7199 | { |
| 7200 | PyUnicodeObject *u; |
| 7201 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7202 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7203 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7204 | |
| 7205 | if (len < 0) |
| 7206 | len = 0; |
| 7207 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7208 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7209 | /* no repeat, return original string */ |
| 7210 | Py_INCREF(str); |
| 7211 | return (PyObject*) str; |
| 7212 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7213 | |
| 7214 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 7215 | * needed doesn't overflow size_t |
| 7216 | */ |
| 7217 | nchars = len * str->length; |
| 7218 | if (len && nchars / len != str->length) { |
| 7219 | PyErr_SetString(PyExc_OverflowError, |
| 7220 | "repeated string is too long"); |
| 7221 | return NULL; |
| 7222 | } |
| 7223 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 7224 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 7225 | PyErr_SetString(PyExc_OverflowError, |
| 7226 | "repeated string is too long"); |
| 7227 | return NULL; |
| 7228 | } |
| 7229 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7230 | if (!u) |
| 7231 | return NULL; |
| 7232 | |
| 7233 | p = u->str; |
| 7234 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7235 | if (str->length == 1 && len > 0) { |
| 7236 | Py_UNICODE_FILL(p, str->str[0], len); |
| 7237 | } else { |
| 7238 | Py_ssize_t done = 0; /* number of characters copied this far */ |
| 7239 | if (done < nchars) { |
| 7240 | Py_UNICODE_COPY(p, str->str, str->length); |
| 7241 | done = str->length; |
| 7242 | } |
| 7243 | while (done < nchars) { |
| 7244 | int n = (done <= nchars-done) ? done : nchars-done; |
| 7245 | Py_UNICODE_COPY(p+done, p, n); |
| 7246 | done += n; |
| 7247 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7248 | } |
| 7249 | |
| 7250 | return (PyObject*) u; |
| 7251 | } |
| 7252 | |
| 7253 | PyObject *PyUnicode_Replace(PyObject *obj, |
| 7254 | PyObject *subobj, |
| 7255 | PyObject *replobj, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7256 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7257 | { |
| 7258 | PyObject *self; |
| 7259 | PyObject *str1; |
| 7260 | PyObject *str2; |
| 7261 | PyObject *result; |
| 7262 | |
| 7263 | self = PyUnicode_FromObject(obj); |
| 7264 | if (self == NULL) |
| 7265 | return NULL; |
| 7266 | str1 = PyUnicode_FromObject(subobj); |
| 7267 | if (str1 == NULL) { |
| 7268 | Py_DECREF(self); |
| 7269 | return NULL; |
| 7270 | } |
| 7271 | str2 = PyUnicode_FromObject(replobj); |
| 7272 | if (str2 == NULL) { |
| 7273 | Py_DECREF(self); |
| 7274 | Py_DECREF(str1); |
| 7275 | return NULL; |
| 7276 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7277 | result = replace((PyUnicodeObject *)self, |
| 7278 | (PyUnicodeObject *)str1, |
| 7279 | (PyUnicodeObject *)str2, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7280 | maxcount); |
| 7281 | Py_DECREF(self); |
| 7282 | Py_DECREF(str1); |
| 7283 | Py_DECREF(str2); |
| 7284 | return result; |
| 7285 | } |
| 7286 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7287 | PyDoc_STRVAR(replace__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7288 | "S.replace (old, new[, maxsplit]) -> unicode\n\ |
| 7289 | \n\ |
| 7290 | Return a copy of S with all occurrences of substring\n\ |
| 7291 | 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] | 7292 | given, only the first maxsplit occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7293 | |
| 7294 | static PyObject* |
| 7295 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 7296 | { |
| 7297 | PyUnicodeObject *str1; |
| 7298 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7299 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7300 | PyObject *result; |
| 7301 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7302 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7303 | return NULL; |
| 7304 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 7305 | if (str1 == NULL) |
| 7306 | return NULL; |
| 7307 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7308 | if (str2 == NULL) { |
| 7309 | Py_DECREF(str1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7310 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7311 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7312 | |
| 7313 | result = replace(self, str1, str2, maxcount); |
| 7314 | |
| 7315 | Py_DECREF(str1); |
| 7316 | Py_DECREF(str2); |
| 7317 | return result; |
| 7318 | } |
| 7319 | |
| 7320 | static |
| 7321 | PyObject *unicode_repr(PyObject *unicode) |
| 7322 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7323 | PyObject *repr; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7324 | Py_UNICODE *p; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7325 | Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); |
| 7326 | Py_ssize_t size = PyUnicode_GET_SIZE(unicode); |
| 7327 | |
| 7328 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 7329 | better to choose a different scheme. Perhaps scan the |
| 7330 | first N-chars of the string and allocate based on that size. |
| 7331 | */ |
| 7332 | /* Initial allocation is based on the longest-possible unichr |
| 7333 | escape. |
| 7334 | |
| 7335 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 7336 | unichr, so in this case it's the longest unichr escape. In |
| 7337 | narrow (UTF-16) builds this is five chars per source unichr |
| 7338 | since there are two unichrs in the surrogate pair, so in narrow |
| 7339 | (UTF-16) builds it's not the longest unichr escape. |
| 7340 | |
| 7341 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 7342 | so in the narrow (UTF-16) build case it's the longest unichr |
| 7343 | escape. |
| 7344 | */ |
| 7345 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7346 | repr = PyUnicode_FromUnicode(NULL, |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7347 | 2 /* quotes */ |
| 7348 | #ifdef Py_UNICODE_WIDE |
| 7349 | + 10*size |
| 7350 | #else |
| 7351 | + 6*size |
| 7352 | #endif |
| 7353 | + 1); |
| 7354 | if (repr == NULL) |
| 7355 | return NULL; |
| 7356 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7357 | p = PyUnicode_AS_UNICODE(repr); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7358 | |
| 7359 | /* Add quote */ |
| 7360 | *p++ = (findchar(s, size, '\'') && |
| 7361 | !findchar(s, size, '"')) ? '"' : '\''; |
| 7362 | while (size-- > 0) { |
| 7363 | Py_UNICODE ch = *s++; |
| 7364 | |
| 7365 | /* Escape quotes and backslashes */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7366 | if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7367 | *p++ = '\\'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7368 | *p++ = ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7369 | continue; |
| 7370 | } |
| 7371 | |
| 7372 | #ifdef Py_UNICODE_WIDE |
| 7373 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 7374 | else if (ch >= 0x10000) { |
| 7375 | *p++ = '\\'; |
| 7376 | *p++ = 'U'; |
| 7377 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 7378 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 7379 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 7380 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 7381 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 7382 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 7383 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 7384 | *p++ = hexdigits[ch & 0x0000000F]; |
| 7385 | continue; |
| 7386 | } |
| 7387 | #else |
| 7388 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 7389 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 7390 | Py_UNICODE ch2; |
| 7391 | Py_UCS4 ucs; |
| 7392 | |
| 7393 | ch2 = *s++; |
| 7394 | size--; |
| 7395 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 7396 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 7397 | *p++ = '\\'; |
| 7398 | *p++ = 'U'; |
| 7399 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 7400 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 7401 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 7402 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 7403 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 7404 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 7405 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 7406 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 7407 | continue; |
| 7408 | } |
| 7409 | /* Fall through: isolated surrogates are copied as-is */ |
| 7410 | s--; |
| 7411 | size++; |
| 7412 | } |
| 7413 | #endif |
| 7414 | |
| 7415 | /* Map 16-bit characters to '\uxxxx' */ |
| 7416 | if (ch >= 256) { |
| 7417 | *p++ = '\\'; |
| 7418 | *p++ = 'u'; |
| 7419 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 7420 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 7421 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7422 | *p++ = hexdigits[ch & 0x000F]; |
| 7423 | } |
| 7424 | |
| 7425 | /* Map special whitespace to '\t', \n', '\r' */ |
| 7426 | else if (ch == '\t') { |
| 7427 | *p++ = '\\'; |
| 7428 | *p++ = 't'; |
| 7429 | } |
| 7430 | else if (ch == '\n') { |
| 7431 | *p++ = '\\'; |
| 7432 | *p++ = 'n'; |
| 7433 | } |
| 7434 | else if (ch == '\r') { |
| 7435 | *p++ = '\\'; |
| 7436 | *p++ = 'r'; |
| 7437 | } |
| 7438 | |
| 7439 | /* Map non-printable US ASCII to '\xhh' */ |
| 7440 | else if (ch < ' ' || ch >= 0x7F) { |
| 7441 | *p++ = '\\'; |
| 7442 | *p++ = 'x'; |
| 7443 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7444 | *p++ = hexdigits[ch & 0x000F]; |
| 7445 | } |
| 7446 | |
| 7447 | /* Copy everything else as-is */ |
| 7448 | else |
| 7449 | *p++ = (char) ch; |
| 7450 | } |
| 7451 | /* Add quote */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7452 | *p++ = PyUnicode_AS_UNICODE(repr)[0]; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7453 | |
| 7454 | *p = '\0'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7455 | _PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7456 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7457 | } |
| 7458 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7459 | PyDoc_STRVAR(rfind__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7460 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 7461 | \n\ |
| 7462 | Return the highest index in S where substring sub is found,\n\ |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 7463 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7464 | arguments start and end are interpreted as in slice notation.\n\ |
| 7465 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7466 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7467 | |
| 7468 | static PyObject * |
| 7469 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 7470 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7471 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7472 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7473 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7474 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7475 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7476 | if (!PyArg_ParseTuple(args, "O|O&O&:rfind", &substring, |
| 7477 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7478 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7479 | substring = PyUnicode_FromObject(substring); |
| 7480 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7481 | return NULL; |
| 7482 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7483 | result = stringlib_rfind_slice( |
| 7484 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7485 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7486 | start, end |
| 7487 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7488 | |
| 7489 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7490 | |
| 7491 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7492 | } |
| 7493 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7494 | PyDoc_STRVAR(rindex__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7495 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 7496 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7497 | 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] | 7498 | |
| 7499 | static PyObject * |
| 7500 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 7501 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7502 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7503 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7504 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7505 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7506 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7507 | if (!PyArg_ParseTuple(args, "O|O&O&:rindex", &substring, |
| 7508 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7509 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7510 | substring = PyUnicode_FromObject(substring); |
| 7511 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7512 | return NULL; |
| 7513 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7514 | result = stringlib_rfind_slice( |
| 7515 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7516 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7517 | start, end |
| 7518 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7519 | |
| 7520 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7521 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7522 | if (result < 0) { |
| 7523 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7524 | return NULL; |
| 7525 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7526 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7527 | } |
| 7528 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7529 | PyDoc_STRVAR(rjust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7530 | "S.rjust(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7531 | \n\ |
| 7532 | 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] | 7533 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7534 | |
| 7535 | static PyObject * |
| 7536 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 7537 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7538 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7539 | Py_UNICODE fillchar = ' '; |
| 7540 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7541 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7542 | return NULL; |
| 7543 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7544 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7545 | Py_INCREF(self); |
| 7546 | return (PyObject*) self; |
| 7547 | } |
| 7548 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7549 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7550 | } |
| 7551 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7552 | PyObject *PyUnicode_Split(PyObject *s, |
| 7553 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7554 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7555 | { |
| 7556 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7557 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7558 | s = PyUnicode_FromObject(s); |
| 7559 | if (s == NULL) |
| 7560 | return NULL; |
| 7561 | if (sep != NULL) { |
| 7562 | sep = PyUnicode_FromObject(sep); |
| 7563 | if (sep == NULL) { |
| 7564 | Py_DECREF(s); |
| 7565 | return NULL; |
| 7566 | } |
| 7567 | } |
| 7568 | |
| 7569 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7570 | |
| 7571 | Py_DECREF(s); |
| 7572 | Py_XDECREF(sep); |
| 7573 | return result; |
| 7574 | } |
| 7575 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7576 | PyDoc_STRVAR(split__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7577 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 7578 | \n\ |
| 7579 | Return a list of the words in S, using sep as the\n\ |
| 7580 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Thomas Heller | ca0d2cb | 2004-09-15 11:41:32 +0000 | [diff] [blame] | 7581 | 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] | 7582 | any whitespace string is a separator."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7583 | |
| 7584 | static PyObject* |
| 7585 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 7586 | { |
| 7587 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7588 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7589 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7590 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7591 | return NULL; |
| 7592 | |
| 7593 | if (substring == Py_None) |
| 7594 | return split(self, NULL, maxcount); |
| 7595 | else if (PyUnicode_Check(substring)) |
| 7596 | return split(self, (PyUnicodeObject *)substring, maxcount); |
| 7597 | else |
| 7598 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
| 7599 | } |
| 7600 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7601 | PyObject * |
| 7602 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 7603 | { |
| 7604 | PyObject* str_obj; |
| 7605 | PyObject* sep_obj; |
| 7606 | PyObject* out; |
| 7607 | |
| 7608 | str_obj = PyUnicode_FromObject(str_in); |
| 7609 | if (!str_obj) |
| 7610 | return NULL; |
| 7611 | sep_obj = PyUnicode_FromObject(sep_in); |
| 7612 | if (!sep_obj) { |
| 7613 | Py_DECREF(str_obj); |
| 7614 | return NULL; |
| 7615 | } |
| 7616 | |
| 7617 | out = stringlib_partition( |
| 7618 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7619 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7620 | ); |
| 7621 | |
| 7622 | Py_DECREF(sep_obj); |
| 7623 | Py_DECREF(str_obj); |
| 7624 | |
| 7625 | return out; |
| 7626 | } |
| 7627 | |
| 7628 | |
| 7629 | PyObject * |
| 7630 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 7631 | { |
| 7632 | PyObject* str_obj; |
| 7633 | PyObject* sep_obj; |
| 7634 | PyObject* out; |
| 7635 | |
| 7636 | str_obj = PyUnicode_FromObject(str_in); |
| 7637 | if (!str_obj) |
| 7638 | return NULL; |
| 7639 | sep_obj = PyUnicode_FromObject(sep_in); |
| 7640 | if (!sep_obj) { |
| 7641 | Py_DECREF(str_obj); |
| 7642 | return NULL; |
| 7643 | } |
| 7644 | |
| 7645 | out = stringlib_rpartition( |
| 7646 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7647 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7648 | ); |
| 7649 | |
| 7650 | Py_DECREF(sep_obj); |
| 7651 | Py_DECREF(str_obj); |
| 7652 | |
| 7653 | return out; |
| 7654 | } |
| 7655 | |
| 7656 | PyDoc_STRVAR(partition__doc__, |
| 7657 | "S.partition(sep) -> (head, sep, tail)\n\ |
| 7658 | \n\ |
| 7659 | Searches for the separator sep in S, and returns the part before it,\n\ |
| 7660 | the separator itself, and the part after it. If the separator is not\n\ |
| 7661 | found, returns S and two empty strings."); |
| 7662 | |
| 7663 | static PyObject* |
| 7664 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 7665 | { |
| 7666 | return PyUnicode_Partition((PyObject *)self, separator); |
| 7667 | } |
| 7668 | |
| 7669 | PyDoc_STRVAR(rpartition__doc__, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7670 | "S.rpartition(sep) -> (tail, sep, head)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7671 | \n\ |
| 7672 | Searches for the separator sep in S, starting at the end of S, and returns\n\ |
| 7673 | 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] | 7674 | separator is not found, returns two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7675 | |
| 7676 | static PyObject* |
| 7677 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 7678 | { |
| 7679 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 7680 | } |
| 7681 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7682 | PyObject *PyUnicode_RSplit(PyObject *s, |
| 7683 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7684 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7685 | { |
| 7686 | PyObject *result; |
| 7687 | |
| 7688 | s = PyUnicode_FromObject(s); |
| 7689 | if (s == NULL) |
| 7690 | return NULL; |
| 7691 | if (sep != NULL) { |
| 7692 | sep = PyUnicode_FromObject(sep); |
| 7693 | if (sep == NULL) { |
| 7694 | Py_DECREF(s); |
| 7695 | return NULL; |
| 7696 | } |
| 7697 | } |
| 7698 | |
| 7699 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7700 | |
| 7701 | Py_DECREF(s); |
| 7702 | Py_XDECREF(sep); |
| 7703 | return result; |
| 7704 | } |
| 7705 | |
| 7706 | PyDoc_STRVAR(rsplit__doc__, |
| 7707 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
| 7708 | \n\ |
| 7709 | Return a list of the words in S, using sep as the\n\ |
| 7710 | delimiter string, starting at the end of the string and\n\ |
| 7711 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 7712 | splits are done. If sep is not specified, any whitespace string\n\ |
| 7713 | is a separator."); |
| 7714 | |
| 7715 | static PyObject* |
| 7716 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 7717 | { |
| 7718 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7719 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7720 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7721 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7722 | return NULL; |
| 7723 | |
| 7724 | if (substring == Py_None) |
| 7725 | return rsplit(self, NULL, maxcount); |
| 7726 | else if (PyUnicode_Check(substring)) |
| 7727 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
| 7728 | else |
| 7729 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
| 7730 | } |
| 7731 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7732 | PyDoc_STRVAR(splitlines__doc__, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7733 | "S.splitlines([keepends]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7734 | \n\ |
| 7735 | 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] | 7736 | 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] | 7737 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7738 | |
| 7739 | static PyObject* |
| 7740 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 7741 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7742 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7743 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7744 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7745 | return NULL; |
| 7746 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7747 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7748 | } |
| 7749 | |
| 7750 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 7751 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7752 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 7753 | if (PyUnicode_CheckExact(self)) { |
| 7754 | Py_INCREF(self); |
| 7755 | return self; |
| 7756 | } else |
| 7757 | /* Subtype -- return genuine unicode string with the same value. */ |
| 7758 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), |
| 7759 | PyUnicode_GET_SIZE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7760 | } |
| 7761 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7762 | PyDoc_STRVAR(swapcase__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7763 | "S.swapcase() -> unicode\n\ |
| 7764 | \n\ |
| 7765 | 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] | 7766 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7767 | |
| 7768 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7769 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7770 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7771 | return fixup(self, fixswapcase); |
| 7772 | } |
| 7773 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7774 | PyDoc_STRVAR(translate__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7775 | "S.translate(table) -> unicode\n\ |
| 7776 | \n\ |
| 7777 | Return a copy of the string S, where all characters have been mapped\n\ |
| 7778 | 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] | 7779 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ |
| 7780 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 7781 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7782 | |
| 7783 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7784 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7785 | { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7786 | return PyUnicode_TranslateCharmap(self->str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7787 | self->length, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7788 | table, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7789 | "ignore"); |
| 7790 | } |
| 7791 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7792 | PyDoc_STRVAR(upper__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7793 | "S.upper() -> unicode\n\ |
| 7794 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7795 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7796 | |
| 7797 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7798 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7799 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7800 | return fixup(self, fixupper); |
| 7801 | } |
| 7802 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7803 | PyDoc_STRVAR(zfill__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7804 | "S.zfill(width) -> unicode\n\ |
| 7805 | \n\ |
| 7806 | 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] | 7807 | of the specified width. The string x is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7808 | |
| 7809 | static PyObject * |
| 7810 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 7811 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7812 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7813 | PyUnicodeObject *u; |
| 7814 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7815 | Py_ssize_t width; |
| 7816 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7817 | return NULL; |
| 7818 | |
| 7819 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 7820 | if (PyUnicode_CheckExact(self)) { |
| 7821 | Py_INCREF(self); |
| 7822 | return (PyObject*) self; |
| 7823 | } |
| 7824 | else |
| 7825 | return PyUnicode_FromUnicode( |
| 7826 | PyUnicode_AS_UNICODE(self), |
| 7827 | PyUnicode_GET_SIZE(self) |
| 7828 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7829 | } |
| 7830 | |
| 7831 | fill = width - self->length; |
| 7832 | |
| 7833 | u = pad(self, fill, 0, '0'); |
| 7834 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 7835 | if (u == NULL) |
| 7836 | return NULL; |
| 7837 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7838 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 7839 | /* move sign to beginning of string */ |
| 7840 | u->str[0] = u->str[fill]; |
| 7841 | u->str[fill] = '0'; |
| 7842 | } |
| 7843 | |
| 7844 | return (PyObject*) u; |
| 7845 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7846 | |
| 7847 | #if 0 |
| 7848 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7849 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7850 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7851 | return PyInt_FromLong(unicode_freelist_size); |
| 7852 | } |
| 7853 | #endif |
| 7854 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7855 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7856 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7857 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7858 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 7859 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7860 | With optional end, stop comparing S at that position.\n\ |
| 7861 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7862 | |
| 7863 | static PyObject * |
| 7864 | unicode_startswith(PyUnicodeObject *self, |
| 7865 | PyObject *args) |
| 7866 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7867 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7868 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7869 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7870 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7871 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7872 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7873 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7874 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7875 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7876 | if (PyTuple_Check(subobj)) { |
| 7877 | Py_ssize_t i; |
| 7878 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 7879 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 7880 | PyTuple_GET_ITEM(subobj, i)); |
| 7881 | if (substring == NULL) |
| 7882 | return NULL; |
| 7883 | result = tailmatch(self, substring, start, end, -1); |
| 7884 | Py_DECREF(substring); |
| 7885 | if (result) { |
| 7886 | Py_RETURN_TRUE; |
| 7887 | } |
| 7888 | } |
| 7889 | /* nothing matched */ |
| 7890 | Py_RETURN_FALSE; |
| 7891 | } |
| 7892 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7893 | if (substring == NULL) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7894 | return NULL; |
| 7895 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7896 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7897 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7898 | } |
| 7899 | |
| 7900 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7901 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7902 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7903 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7904 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 7905 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7906 | With optional end, stop comparing S at that position.\n\ |
| 7907 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7908 | |
| 7909 | static PyObject * |
| 7910 | unicode_endswith(PyUnicodeObject *self, |
| 7911 | PyObject *args) |
| 7912 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7913 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7914 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7915 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7916 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7917 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7918 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7919 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 7920 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7921 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7922 | if (PyTuple_Check(subobj)) { |
| 7923 | Py_ssize_t i; |
| 7924 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 7925 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 7926 | PyTuple_GET_ITEM(subobj, i)); |
| 7927 | if (substring == NULL) |
| 7928 | return NULL; |
| 7929 | result = tailmatch(self, substring, start, end, +1); |
| 7930 | Py_DECREF(substring); |
| 7931 | if (result) { |
| 7932 | Py_RETURN_TRUE; |
| 7933 | } |
| 7934 | } |
| 7935 | Py_RETURN_FALSE; |
| 7936 | } |
| 7937 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7938 | if (substring == NULL) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7939 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7940 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7941 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7942 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7943 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7944 | } |
| 7945 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 7946 | #include "stringlib/string_format.h" |
| 7947 | |
| 7948 | PyDoc_STRVAR(format__doc__, |
| 7949 | "S.format(*args, **kwargs) -> unicode\n\ |
| 7950 | \n\ |
| 7951 | "); |
| 7952 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 7953 | PyDoc_STRVAR(p_format__doc__, |
| 7954 | "S.__format__(format_spec) -> unicode\n\ |
| 7955 | \n\ |
| 7956 | "); |
| 7957 | |
| 7958 | static PyObject * |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 7959 | unicode_getnewargs(PyUnicodeObject *v) |
| 7960 | { |
| 7961 | return Py_BuildValue("(u#)", v->str, v->length); |
| 7962 | } |
| 7963 | |
| 7964 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7965 | static PyMethodDef unicode_methods[] = { |
| 7966 | |
| 7967 | /* Order is according to common usage: often used methods should |
| 7968 | appear first, since lookup is done sequentially. */ |
| 7969 | |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7970 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
| 7971 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 7972 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7973 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7974 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 7975 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 7976 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 7977 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 7978 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 7979 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 7980 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7981 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7982 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 7983 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 7984 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7985 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7986 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 7987 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 7988 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7989 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7990 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7991 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7992 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7993 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 7994 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 7995 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 7996 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 7997 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 7998 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 7999 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 8000 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 8001 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 8002 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 8003 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 8004 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 8005 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 8006 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8007 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8008 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 8009 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
| 8010 | {"__format__", (PyCFunction) unicode_unicode__format__, METH_VARARGS, p_format__doc__}, |
Eric Smith | f6db409 | 2007-08-27 23:52:26 +0000 | [diff] [blame] | 8011 | {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
| 8012 | {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8013 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8014 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8015 | #endif |
| 8016 | |
| 8017 | #if 0 |
| 8018 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8019 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8020 | #endif |
| 8021 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8022 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8023 | {NULL, NULL} |
| 8024 | }; |
| 8025 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8026 | static PyObject * |
| 8027 | unicode_mod(PyObject *v, PyObject *w) |
| 8028 | { |
| 8029 | if (!PyUnicode_Check(v)) { |
| 8030 | Py_INCREF(Py_NotImplemented); |
| 8031 | return Py_NotImplemented; |
| 8032 | } |
| 8033 | return PyUnicode_Format(v, w); |
| 8034 | } |
| 8035 | |
| 8036 | static PyNumberMethods unicode_as_number = { |
| 8037 | 0, /*nb_add*/ |
| 8038 | 0, /*nb_subtract*/ |
| 8039 | 0, /*nb_multiply*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8040 | unicode_mod, /*nb_remainder*/ |
| 8041 | }; |
| 8042 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8043 | static PySequenceMethods unicode_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8044 | (lenfunc) unicode_length, /* sq_length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8045 | PyUnicode_Concat, /* sq_concat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8046 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 8047 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
Thomas Wouters | d2cf20e | 2007-08-30 22:57:53 +0000 | [diff] [blame] | 8048 | 0, /* sq_slice */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8049 | 0, /* sq_ass_item */ |
| 8050 | 0, /* sq_ass_slice */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8051 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8052 | }; |
| 8053 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8054 | static PyObject* |
| 8055 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 8056 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8057 | if (PyIndex_Check(item)) { |
| 8058 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8059 | if (i == -1 && PyErr_Occurred()) |
| 8060 | return NULL; |
| 8061 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8062 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8063 | return unicode_getitem(self, i); |
| 8064 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8065 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8066 | Py_UNICODE* source_buf; |
| 8067 | Py_UNICODE* result_buf; |
| 8068 | PyObject* result; |
| 8069 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8070 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8071 | &start, &stop, &step, &slicelength) < 0) { |
| 8072 | return NULL; |
| 8073 | } |
| 8074 | |
| 8075 | if (slicelength <= 0) { |
| 8076 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 8077 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 8078 | PyUnicode_CheckExact(self)) { |
| 8079 | Py_INCREF(self); |
| 8080 | return (PyObject *)self; |
| 8081 | } else if (step == 1) { |
| 8082 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8083 | } else { |
| 8084 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8085 | result_buf = (Py_UNICODE *)PyMem_MALLOC(slicelength* |
| 8086 | sizeof(Py_UNICODE)); |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8087 | |
| 8088 | if (result_buf == NULL) |
| 8089 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8090 | |
| 8091 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 8092 | result_buf[i] = source_buf[cur]; |
| 8093 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8094 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8095 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
| 8096 | PyMem_FREE(result_buf); |
| 8097 | return result; |
| 8098 | } |
| 8099 | } else { |
| 8100 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 8101 | return NULL; |
| 8102 | } |
| 8103 | } |
| 8104 | |
| 8105 | static PyMappingMethods unicode_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8106 | (lenfunc)unicode_length, /* mp_length */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8107 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 8108 | (objobjargproc)0, /* mp_ass_subscript */ |
| 8109 | }; |
| 8110 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8111 | |
| 8112 | static int |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 8113 | unicode_buffer_getbuffer(PyUnicodeObject *self, Py_buffer *view, int flags) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8114 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8115 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 8116 | if (flags & PyBUF_CHARACTER) { |
Guido van Rossum | a74184e | 2007-08-29 04:05:57 +0000 | [diff] [blame] | 8117 | PyErr_SetString(PyExc_SystemError, "can't use str as char buffer"); |
| 8118 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8119 | } |
Guido van Rossum | a74184e | 2007-08-29 04:05:57 +0000 | [diff] [blame] | 8120 | return PyBuffer_FillInfo(view, (void *)self->str, |
| 8121 | PyUnicode_GET_DATA_SIZE(self), 1, flags); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8122 | } |
| 8123 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 8124 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8125 | /* Helpers for PyUnicode_Format() */ |
| 8126 | |
| 8127 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8128 | 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] | 8129 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8130 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8131 | if (argidx < arglen) { |
| 8132 | (*p_argidx)++; |
| 8133 | if (arglen < 0) |
| 8134 | return args; |
| 8135 | else |
| 8136 | return PyTuple_GetItem(args, argidx); |
| 8137 | } |
| 8138 | PyErr_SetString(PyExc_TypeError, |
| 8139 | "not enough arguments for format string"); |
| 8140 | return NULL; |
| 8141 | } |
| 8142 | |
| 8143 | #define F_LJUST (1<<0) |
| 8144 | #define F_SIGN (1<<1) |
| 8145 | #define F_BLANK (1<<2) |
| 8146 | #define F_ALT (1<<3) |
| 8147 | #define F_ZERO (1<<4) |
| 8148 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8149 | static Py_ssize_t |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8150 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8151 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8152 | register Py_ssize_t i; |
| 8153 | Py_ssize_t len = strlen(charbuffer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8154 | for (i = len - 1; i >= 0; i--) |
| 8155 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
| 8156 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8157 | return len; |
| 8158 | } |
| 8159 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8160 | static int |
| 8161 | doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) |
| 8162 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8163 | Py_ssize_t result; |
| 8164 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8165 | PyOS_ascii_formatd((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8166 | result = strtounicode(buffer, (char *)buffer); |
| 8167 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8168 | } |
| 8169 | |
| 8170 | static int |
| 8171 | longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) |
| 8172 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8173 | Py_ssize_t result; |
| 8174 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8175 | PyOS_snprintf((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8176 | result = strtounicode(buffer, (char *)buffer); |
| 8177 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8178 | } |
| 8179 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8180 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 8181 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 8182 | formatting is done. */ |
| 8183 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8184 | static int |
| 8185 | formatfloat(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8186 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8187 | int flags, |
| 8188 | int prec, |
| 8189 | int type, |
| 8190 | PyObject *v) |
| 8191 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8192 | /* fmt = '%#.' + `prec` + `type` |
| 8193 | 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] | 8194 | char fmt[20]; |
| 8195 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8196 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8197 | x = PyFloat_AsDouble(v); |
| 8198 | if (x == -1.0 && PyErr_Occurred()) |
| 8199 | return -1; |
| 8200 | if (prec < 0) |
| 8201 | prec = 6; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8202 | if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
| 8203 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8204 | /* Worst case length calc to ensure no buffer overrun: |
| 8205 | |
| 8206 | 'g' formats: |
| 8207 | fmt = %#.<prec>g |
| 8208 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 8209 | for any double rep.) |
| 8210 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 8211 | |
| 8212 | 'f' formats: |
| 8213 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 8214 | len = 1 + 50 + 1 + prec = 52 + prec |
| 8215 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8216 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8217 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8218 | |
| 8219 | */ |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 8220 | if (((type == 'g' || type == 'G') && |
| 8221 | buflen <= (size_t)10 + (size_t)prec) || |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8222 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8223 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8224 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8225 | return -1; |
| 8226 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8227 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 8228 | (flags&F_ALT) ? "#" : "", |
| 8229 | prec, type); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8230 | return doubletounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8231 | } |
| 8232 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8233 | static PyObject* |
| 8234 | formatlong(PyObject *val, int flags, int prec, int type) |
| 8235 | { |
| 8236 | char *buf; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8237 | int len; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8238 | PyObject *str; /* temporary string object. */ |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8239 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8240 | |
| 8241 | str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
| 8242 | if (!str) |
| 8243 | return NULL; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8244 | result = PyUnicode_FromStringAndSize(buf, len); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8245 | Py_DECREF(str); |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8246 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8247 | } |
| 8248 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8249 | static int |
| 8250 | formatint(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8251 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8252 | int flags, |
| 8253 | int prec, |
| 8254 | int type, |
| 8255 | PyObject *v) |
| 8256 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8257 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8258 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 8259 | * + 1 + 1 |
| 8260 | * = 24 |
| 8261 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8262 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8263 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8264 | long x; |
| 8265 | |
| 8266 | x = PyInt_AsLong(v); |
| 8267 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8268 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8269 | if (x < 0 && type == 'u') { |
| 8270 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8271 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8272 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 8273 | sign = "-"; |
| 8274 | else |
| 8275 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8276 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8277 | prec = 1; |
| 8278 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8279 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 8280 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8281 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8282 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8283 | PyErr_SetString(PyExc_OverflowError, |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8284 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8285 | return -1; |
| 8286 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8287 | |
| 8288 | if ((flags & F_ALT) && |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8289 | (type == 'x' || type == 'X' || type == 'o')) { |
| 8290 | /* When converting under %#o, %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8291 | * of issues that cause pain: |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8292 | * - for %#o, we want a different base marker than C |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8293 | * - when 0 is being converted, the C standard leaves off |
| 8294 | * the '0x' or '0X', which is inconsistent with other |
| 8295 | * %#x/%#X conversions and inconsistent with Python's |
| 8296 | * hex() function |
| 8297 | * - there are platforms that violate the standard and |
| 8298 | * convert 0 with the '0x' or '0X' |
| 8299 | * (Metrowerks, Compaq Tru64) |
| 8300 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8301 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8302 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8303 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8304 | * We can achieve the desired consistency by inserting our |
| 8305 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 8306 | * of %#x/%#X. |
| 8307 | * |
| 8308 | * Note that this is the same approach as used in |
| 8309 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8310 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8311 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 8312 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8313 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8314 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8315 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 8316 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8317 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 8318 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8319 | if (sign[0]) |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8320 | return longtounicode(buf, buflen, fmt, -x); |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8321 | else |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8322 | return longtounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8323 | } |
| 8324 | |
| 8325 | static int |
| 8326 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8327 | size_t buflen, |
| 8328 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8329 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8330 | /* presume that the buffer is at least 2 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8331 | if (PyUnicode_Check(v)) { |
| 8332 | if (PyUnicode_GET_SIZE(v) != 1) |
| 8333 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8334 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8335 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8336 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8337 | else if (PyString_Check(v)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8338 | if (PyString_GET_SIZE(v) != 1) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8339 | goto onError; |
| 8340 | buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0]; |
| 8341 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8342 | |
| 8343 | else { |
| 8344 | /* Integer input truncated to a character */ |
| 8345 | long x; |
| 8346 | x = PyInt_AsLong(v); |
| 8347 | if (x == -1 && PyErr_Occurred()) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8348 | goto onError; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8349 | #ifdef Py_UNICODE_WIDE |
| 8350 | if (x < 0 || x > 0x10ffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 8351 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8352 | "%c arg not in range(0x110000) " |
| 8353 | "(wide Python build)"); |
| 8354 | return -1; |
| 8355 | } |
| 8356 | #else |
| 8357 | if (x < 0 || x > 0xffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 8358 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8359 | "%c arg not in range(0x10000) " |
| 8360 | "(narrow Python build)"); |
| 8361 | return -1; |
| 8362 | } |
| 8363 | #endif |
| 8364 | buf[0] = (Py_UNICODE) x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8365 | } |
| 8366 | buf[1] = '\0'; |
| 8367 | return 1; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8368 | |
| 8369 | onError: |
| 8370 | PyErr_SetString(PyExc_TypeError, |
| 8371 | "%c requires int or char"); |
| 8372 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8373 | } |
| 8374 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8375 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 8376 | |
| 8377 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 8378 | chars are formatted. XXX This is a magic number. Each formatting |
| 8379 | routine does bounds checking to ensure no overflow, but a better |
| 8380 | solution may be to malloc a buffer of appropriate size for each |
| 8381 | format. For now, the current solution is sufficient. |
| 8382 | */ |
| 8383 | #define FORMATBUFLEN (size_t)120 |
| 8384 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8385 | PyObject *PyUnicode_Format(PyObject *format, |
| 8386 | PyObject *args) |
| 8387 | { |
| 8388 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8389 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8390 | int args_owned = 0; |
| 8391 | PyUnicodeObject *result = NULL; |
| 8392 | PyObject *dict = NULL; |
| 8393 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8394 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8395 | if (format == NULL || args == NULL) { |
| 8396 | PyErr_BadInternalCall(); |
| 8397 | return NULL; |
| 8398 | } |
| 8399 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8400 | if (uformat == NULL) |
| 8401 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8402 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 8403 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 8404 | |
| 8405 | reslen = rescnt = fmtcnt + 100; |
| 8406 | result = _PyUnicode_New(reslen); |
| 8407 | if (result == NULL) |
| 8408 | goto onError; |
| 8409 | res = PyUnicode_AS_UNICODE(result); |
| 8410 | |
| 8411 | if (PyTuple_Check(args)) { |
| 8412 | arglen = PyTuple_Size(args); |
| 8413 | argidx = 0; |
| 8414 | } |
| 8415 | else { |
| 8416 | arglen = -1; |
| 8417 | argidx = -2; |
| 8418 | } |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 8419 | if (Py_Type(args)->tp_as_mapping && !PyTuple_Check(args) && |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 8420 | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8421 | dict = args; |
| 8422 | |
| 8423 | while (--fmtcnt >= 0) { |
| 8424 | if (*fmt != '%') { |
| 8425 | if (--rescnt < 0) { |
| 8426 | rescnt = fmtcnt + 100; |
| 8427 | reslen += rescnt; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8428 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8429 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8430 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 8431 | --rescnt; |
| 8432 | } |
| 8433 | *res++ = *fmt++; |
| 8434 | } |
| 8435 | else { |
| 8436 | /* Got a format specifier */ |
| 8437 | int flags = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8438 | Py_ssize_t width = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8439 | int prec = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8440 | Py_UNICODE c = '\0'; |
| 8441 | Py_UNICODE fill; |
| 8442 | PyObject *v = NULL; |
| 8443 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8444 | Py_UNICODE *pbuf; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8445 | Py_UNICODE sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8446 | Py_ssize_t len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8447 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8448 | |
| 8449 | fmt++; |
| 8450 | if (*fmt == '(') { |
| 8451 | Py_UNICODE *keystart; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8452 | Py_ssize_t keylen; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8453 | PyObject *key; |
| 8454 | int pcount = 1; |
| 8455 | |
| 8456 | if (dict == NULL) { |
| 8457 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8458 | "format requires a mapping"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8459 | goto onError; |
| 8460 | } |
| 8461 | ++fmt; |
| 8462 | --fmtcnt; |
| 8463 | keystart = fmt; |
| 8464 | /* Skip over balanced parentheses */ |
| 8465 | while (pcount > 0 && --fmtcnt >= 0) { |
| 8466 | if (*fmt == ')') |
| 8467 | --pcount; |
| 8468 | else if (*fmt == '(') |
| 8469 | ++pcount; |
| 8470 | fmt++; |
| 8471 | } |
| 8472 | keylen = fmt - keystart - 1; |
| 8473 | if (fmtcnt < 0 || pcount > 0) { |
| 8474 | PyErr_SetString(PyExc_ValueError, |
| 8475 | "incomplete format key"); |
| 8476 | goto onError; |
| 8477 | } |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 8478 | #if 0 |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8479 | /* keys are converted to strings using UTF-8 and |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8480 | then looked up since Python uses strings to hold |
| 8481 | variables names etc. in its namespaces and we |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8482 | wouldn't want to break common idioms. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8483 | key = PyUnicode_EncodeUTF8(keystart, |
| 8484 | keylen, |
| 8485 | NULL); |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 8486 | #else |
| 8487 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 8488 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8489 | if (key == NULL) |
| 8490 | goto onError; |
| 8491 | if (args_owned) { |
| 8492 | Py_DECREF(args); |
| 8493 | args_owned = 0; |
| 8494 | } |
| 8495 | args = PyObject_GetItem(dict, key); |
| 8496 | Py_DECREF(key); |
| 8497 | if (args == NULL) { |
| 8498 | goto onError; |
| 8499 | } |
| 8500 | args_owned = 1; |
| 8501 | arglen = -1; |
| 8502 | argidx = -2; |
| 8503 | } |
| 8504 | while (--fmtcnt >= 0) { |
| 8505 | switch (c = *fmt++) { |
| 8506 | case '-': flags |= F_LJUST; continue; |
| 8507 | case '+': flags |= F_SIGN; continue; |
| 8508 | case ' ': flags |= F_BLANK; continue; |
| 8509 | case '#': flags |= F_ALT; continue; |
| 8510 | case '0': flags |= F_ZERO; continue; |
| 8511 | } |
| 8512 | break; |
| 8513 | } |
| 8514 | if (c == '*') { |
| 8515 | v = getnextarg(args, arglen, &argidx); |
| 8516 | if (v == NULL) |
| 8517 | goto onError; |
| 8518 | if (!PyInt_Check(v)) { |
| 8519 | PyErr_SetString(PyExc_TypeError, |
| 8520 | "* wants int"); |
| 8521 | goto onError; |
| 8522 | } |
| 8523 | width = PyInt_AsLong(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 8524 | if (width == -1 && PyErr_Occurred()) |
| 8525 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8526 | if (width < 0) { |
| 8527 | flags |= F_LJUST; |
| 8528 | width = -width; |
| 8529 | } |
| 8530 | if (--fmtcnt >= 0) |
| 8531 | c = *fmt++; |
| 8532 | } |
| 8533 | else if (c >= '0' && c <= '9') { |
| 8534 | width = c - '0'; |
| 8535 | while (--fmtcnt >= 0) { |
| 8536 | c = *fmt++; |
| 8537 | if (c < '0' || c > '9') |
| 8538 | break; |
| 8539 | if ((width*10) / 10 != width) { |
| 8540 | PyErr_SetString(PyExc_ValueError, |
| 8541 | "width too big"); |
| 8542 | goto onError; |
| 8543 | } |
| 8544 | width = width*10 + (c - '0'); |
| 8545 | } |
| 8546 | } |
| 8547 | if (c == '.') { |
| 8548 | prec = 0; |
| 8549 | if (--fmtcnt >= 0) |
| 8550 | c = *fmt++; |
| 8551 | if (c == '*') { |
| 8552 | v = getnextarg(args, arglen, &argidx); |
| 8553 | if (v == NULL) |
| 8554 | goto onError; |
| 8555 | if (!PyInt_Check(v)) { |
| 8556 | PyErr_SetString(PyExc_TypeError, |
| 8557 | "* wants int"); |
| 8558 | goto onError; |
| 8559 | } |
| 8560 | prec = PyInt_AsLong(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 8561 | if (prec == -1 && PyErr_Occurred()) |
| 8562 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8563 | if (prec < 0) |
| 8564 | prec = 0; |
| 8565 | if (--fmtcnt >= 0) |
| 8566 | c = *fmt++; |
| 8567 | } |
| 8568 | else if (c >= '0' && c <= '9') { |
| 8569 | prec = c - '0'; |
| 8570 | while (--fmtcnt >= 0) { |
| 8571 | c = Py_CHARMASK(*fmt++); |
| 8572 | if (c < '0' || c > '9') |
| 8573 | break; |
| 8574 | if ((prec*10) / 10 != prec) { |
| 8575 | PyErr_SetString(PyExc_ValueError, |
| 8576 | "prec too big"); |
| 8577 | goto onError; |
| 8578 | } |
| 8579 | prec = prec*10 + (c - '0'); |
| 8580 | } |
| 8581 | } |
| 8582 | } /* prec */ |
| 8583 | if (fmtcnt >= 0) { |
| 8584 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8585 | if (--fmtcnt >= 0) |
| 8586 | c = *fmt++; |
| 8587 | } |
| 8588 | } |
| 8589 | if (fmtcnt < 0) { |
| 8590 | PyErr_SetString(PyExc_ValueError, |
| 8591 | "incomplete format"); |
| 8592 | goto onError; |
| 8593 | } |
| 8594 | if (c != '%') { |
| 8595 | v = getnextarg(args, arglen, &argidx); |
| 8596 | if (v == NULL) |
| 8597 | goto onError; |
| 8598 | } |
| 8599 | sign = 0; |
| 8600 | fill = ' '; |
| 8601 | switch (c) { |
| 8602 | |
| 8603 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8604 | pbuf = formatbuf; |
| 8605 | /* presume that buffer length is at least 1 */ |
| 8606 | pbuf[0] = '%'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8607 | len = 1; |
| 8608 | break; |
| 8609 | |
| 8610 | case 's': |
| 8611 | case 'r': |
| 8612 | if (PyUnicode_Check(v) && c == 's') { |
| 8613 | temp = v; |
| 8614 | Py_INCREF(temp); |
| 8615 | } |
| 8616 | else { |
| 8617 | PyObject *unicode; |
| 8618 | if (c == 's') |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8619 | temp = PyObject_Unicode(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8620 | else |
| 8621 | temp = PyObject_Repr(v); |
| 8622 | if (temp == NULL) |
| 8623 | goto onError; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8624 | if (PyUnicode_Check(temp)) |
| 8625 | /* nothing to do */; |
| 8626 | else if (PyString_Check(temp)) { |
| 8627 | /* convert to string to Unicode */ |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8628 | unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8629 | PyString_GET_SIZE(temp), |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8630 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8631 | "strict"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8632 | Py_DECREF(temp); |
| 8633 | temp = unicode; |
| 8634 | if (temp == NULL) |
| 8635 | goto onError; |
| 8636 | } |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8637 | else { |
| 8638 | Py_DECREF(temp); |
| 8639 | PyErr_SetString(PyExc_TypeError, |
| 8640 | "%s argument has non-string str()"); |
| 8641 | goto onError; |
| 8642 | } |
| 8643 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8644 | pbuf = PyUnicode_AS_UNICODE(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8645 | len = PyUnicode_GET_SIZE(temp); |
| 8646 | if (prec >= 0 && len > prec) |
| 8647 | len = prec; |
| 8648 | break; |
| 8649 | |
| 8650 | case 'i': |
| 8651 | case 'd': |
| 8652 | case 'u': |
| 8653 | case 'o': |
| 8654 | case 'x': |
| 8655 | case 'X': |
| 8656 | if (c == 'i') |
| 8657 | c = 'd'; |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 8658 | if (PyLong_Check(v)) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8659 | temp = formatlong(v, flags, prec, c); |
| 8660 | if (!temp) |
| 8661 | goto onError; |
| 8662 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 8663 | len = PyUnicode_GET_SIZE(temp); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8664 | sign = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8665 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8666 | else { |
| 8667 | pbuf = formatbuf; |
| 8668 | len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8669 | flags, prec, c, v); |
| 8670 | if (len < 0) |
| 8671 | goto onError; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8672 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8673 | } |
| 8674 | if (flags & F_ZERO) |
| 8675 | fill = '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8676 | break; |
| 8677 | |
| 8678 | case 'e': |
| 8679 | case 'E': |
| 8680 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 8681 | case 'F': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8682 | case 'g': |
| 8683 | case 'G': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 8684 | if (c == 'F') |
| 8685 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8686 | pbuf = formatbuf; |
| 8687 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8688 | flags, prec, c, v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8689 | if (len < 0) |
| 8690 | goto onError; |
| 8691 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8692 | if (flags & F_ZERO) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8693 | fill = '0'; |
| 8694 | break; |
| 8695 | |
| 8696 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8697 | pbuf = formatbuf; |
| 8698 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8699 | if (len < 0) |
| 8700 | goto onError; |
| 8701 | break; |
| 8702 | |
| 8703 | default: |
| 8704 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 8705 | "unsupported format character '%c' (0x%x) " |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 8706 | "at index %zd", |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8707 | (31<=c && c<=126) ? (char)c : '?', |
Marc-André Lemburg | 24e53b6 | 2002-09-24 09:32:14 +0000 | [diff] [blame] | 8708 | (int)c, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 8709 | (Py_ssize_t)(fmt - 1 - |
| 8710 | PyUnicode_AS_UNICODE(uformat))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8711 | goto onError; |
| 8712 | } |
| 8713 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8714 | if (*pbuf == '-' || *pbuf == '+') { |
| 8715 | sign = *pbuf++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8716 | len--; |
| 8717 | } |
| 8718 | else if (flags & F_SIGN) |
| 8719 | sign = '+'; |
| 8720 | else if (flags & F_BLANK) |
| 8721 | sign = ' '; |
| 8722 | else |
| 8723 | sign = 0; |
| 8724 | } |
| 8725 | if (width < len) |
| 8726 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8727 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8728 | reslen -= rescnt; |
| 8729 | rescnt = width + fmtcnt + 100; |
| 8730 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8731 | if (reslen < 0) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8732 | Py_XDECREF(temp); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8733 | PyErr_NoMemory(); |
| 8734 | goto onError; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8735 | } |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8736 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 8737 | Py_XDECREF(temp); |
| 8738 | goto onError; |
| 8739 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8740 | res = PyUnicode_AS_UNICODE(result) |
| 8741 | + reslen - rescnt; |
| 8742 | } |
| 8743 | if (sign) { |
| 8744 | if (fill != ' ') |
| 8745 | *res++ = sign; |
| 8746 | rescnt--; |
| 8747 | if (width > len) |
| 8748 | width--; |
| 8749 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8750 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8751 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8752 | assert(pbuf[1] == c); |
| 8753 | if (fill != ' ') { |
| 8754 | *res++ = *pbuf++; |
| 8755 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8756 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8757 | rescnt -= 2; |
| 8758 | width -= 2; |
| 8759 | if (width < 0) |
| 8760 | width = 0; |
| 8761 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8762 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8763 | if (width > len && !(flags & F_LJUST)) { |
| 8764 | do { |
| 8765 | --rescnt; |
| 8766 | *res++ = fill; |
| 8767 | } while (--width > len); |
| 8768 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8769 | if (fill == ' ') { |
| 8770 | if (sign) |
| 8771 | *res++ = sign; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8772 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8773 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8774 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8775 | *res++ = *pbuf++; |
| 8776 | *res++ = *pbuf++; |
| 8777 | } |
| 8778 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8779 | Py_UNICODE_COPY(res, pbuf, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8780 | res += len; |
| 8781 | rescnt -= len; |
| 8782 | while (--width >= len) { |
| 8783 | --rescnt; |
| 8784 | *res++ = ' '; |
| 8785 | } |
| 8786 | if (dict && (argidx < arglen) && c != '%') { |
| 8787 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 8788 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8789 | Py_XDECREF(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8790 | goto onError; |
| 8791 | } |
| 8792 | Py_XDECREF(temp); |
| 8793 | } /* '%' */ |
| 8794 | } /* until end */ |
| 8795 | if (argidx < arglen && !dict) { |
| 8796 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 8797 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8798 | goto onError; |
| 8799 | } |
| 8800 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8801 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
| 8802 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8803 | if (args_owned) { |
| 8804 | Py_DECREF(args); |
| 8805 | } |
| 8806 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8807 | return (PyObject *)result; |
| 8808 | |
| 8809 | onError: |
| 8810 | Py_XDECREF(result); |
| 8811 | Py_DECREF(uformat); |
| 8812 | if (args_owned) { |
| 8813 | Py_DECREF(args); |
| 8814 | } |
| 8815 | return NULL; |
| 8816 | } |
| 8817 | |
| 8818 | static PyBufferProcs unicode_as_buffer = { |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 8819 | (getbufferproc) unicode_buffer_getbuffer, |
| 8820 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8821 | }; |
| 8822 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 8823 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8824 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 8825 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8826 | static PyObject * |
| 8827 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8828 | { |
| 8829 | PyObject *x = NULL; |
Guido van Rossum | 55b4a7b | 2007-07-11 09:28:11 +0000 | [diff] [blame] | 8830 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8831 | char *encoding = NULL; |
| 8832 | char *errors = NULL; |
| 8833 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8834 | if (type != &PyUnicode_Type) |
| 8835 | return unicode_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8836 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
| 8837 | kwlist, &x, &encoding, &errors)) |
| 8838 | return NULL; |
| 8839 | if (x == NULL) |
| 8840 | return (PyObject *)_PyUnicode_New(0); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 8841 | if (encoding == NULL && errors == NULL) |
| 8842 | return PyObject_Unicode(x); |
| 8843 | else |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8844 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
| 8845 | } |
| 8846 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8847 | static PyObject * |
| 8848 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8849 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8850 | PyUnicodeObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8851 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8852 | |
| 8853 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 8854 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 8855 | if (tmp == NULL) |
| 8856 | return NULL; |
| 8857 | assert(PyUnicode_Check(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8858 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8859 | if (pnew == NULL) { |
| 8860 | Py_DECREF(tmp); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8861 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8862 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8863 | pnew->str = PyMem_NEW(Py_UNICODE, n+1); |
| 8864 | if (pnew->str == NULL) { |
| 8865 | _Py_ForgetReference((PyObject *)pnew); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8866 | PyObject_Del(pnew); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8867 | Py_DECREF(tmp); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 8868 | return PyErr_NoMemory(); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8869 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8870 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 8871 | pnew->length = n; |
| 8872 | pnew->hash = tmp->hash; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8873 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8874 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8875 | } |
| 8876 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8877 | PyDoc_STRVAR(unicode_doc, |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 8878 | "str(string [, encoding[, errors]]) -> object\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8879 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 8880 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 8881 | encoding defaults to the current default string encoding.\n\ |
| 8882 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8883 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8884 | static PyObject *unicode_iter(PyObject *seq); |
| 8885 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8886 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 8887 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 8888 | "str", /* tp_name */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8889 | sizeof(PyUnicodeObject), /* tp_size */ |
| 8890 | 0, /* tp_itemsize */ |
| 8891 | /* Slots */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 8892 | (destructor)unicode_dealloc, /* tp_dealloc */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8893 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8894 | 0, /* tp_getattr */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8895 | 0, /* tp_setattr */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8896 | 0, /* tp_compare */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8897 | unicode_repr, /* tp_repr */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8898 | &unicode_as_number, /* tp_as_number */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8899 | &unicode_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8900 | &unicode_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8901 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 8902 | 0, /* tp_call*/ |
| 8903 | (reprfunc) unicode_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8904 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 8905 | 0, /* tp_setattro */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8906 | &unicode_as_buffer, /* tp_as_buffer */ |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 8907 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 8908 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8909 | unicode_doc, /* tp_doc */ |
| 8910 | 0, /* tp_traverse */ |
| 8911 | 0, /* tp_clear */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8912 | PyUnicode_RichCompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8913 | 0, /* tp_weaklistoffset */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8914 | unicode_iter, /* tp_iter */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8915 | 0, /* tp_iternext */ |
| 8916 | unicode_methods, /* tp_methods */ |
| 8917 | 0, /* tp_members */ |
| 8918 | 0, /* tp_getset */ |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 8919 | &PyBaseString_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8920 | 0, /* tp_dict */ |
| 8921 | 0, /* tp_descr_get */ |
| 8922 | 0, /* tp_descr_set */ |
| 8923 | 0, /* tp_dictoffset */ |
| 8924 | 0, /* tp_init */ |
| 8925 | 0, /* tp_alloc */ |
| 8926 | unicode_new, /* tp_new */ |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8927 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8928 | }; |
| 8929 | |
| 8930 | /* Initialize the Unicode implementation */ |
| 8931 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 8932 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8933 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8934 | int i; |
| 8935 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8936 | /* XXX - move this array to unicodectype.c ? */ |
| 8937 | Py_UNICODE linebreak[] = { |
| 8938 | 0x000A, /* LINE FEED */ |
| 8939 | 0x000D, /* CARRIAGE RETURN */ |
| 8940 | 0x001C, /* FILE SEPARATOR */ |
| 8941 | 0x001D, /* GROUP SEPARATOR */ |
| 8942 | 0x001E, /* RECORD SEPARATOR */ |
| 8943 | 0x0085, /* NEXT LINE */ |
| 8944 | 0x2028, /* LINE SEPARATOR */ |
| 8945 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 8946 | }; |
| 8947 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8948 | /* Init the implementation */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8949 | unicode_freelist = NULL; |
| 8950 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8951 | unicode_empty = _PyUnicode_New(0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8952 | if (!unicode_empty) |
| 8953 | return; |
| 8954 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8955 | for (i = 0; i < 256; i++) |
| 8956 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 8957 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 8958 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8959 | |
| 8960 | /* initialize the linebreak bloom filter */ |
| 8961 | bloom_linebreak = make_bloom_mask( |
| 8962 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 8963 | ); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8964 | |
| 8965 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8966 | } |
| 8967 | |
| 8968 | /* Finalize the Unicode implementation */ |
| 8969 | |
| 8970 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 8971 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8972 | { |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8973 | PyUnicodeObject *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8974 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8975 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 8976 | Py_XDECREF(unicode_empty); |
| 8977 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8978 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8979 | for (i = 0; i < 256; i++) { |
| 8980 | if (unicode_latin1[i]) { |
| 8981 | Py_DECREF(unicode_latin1[i]); |
| 8982 | unicode_latin1[i] = NULL; |
| 8983 | } |
| 8984 | } |
| 8985 | |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8986 | for (u = unicode_freelist; u != NULL;) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8987 | PyUnicodeObject *v = u; |
| 8988 | u = *(PyUnicodeObject **)u; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 8989 | if (v->str) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 8990 | PyMem_DEL(v->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 8991 | Py_XDECREF(v->defenc); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8992 | PyObject_Del(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8993 | } |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8994 | unicode_freelist = NULL; |
| 8995 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8996 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 8997 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 8998 | void |
| 8999 | PyUnicode_InternInPlace(PyObject **p) |
| 9000 | { |
| 9001 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 9002 | PyObject *t; |
| 9003 | if (s == NULL || !PyUnicode_Check(s)) |
| 9004 | Py_FatalError( |
| 9005 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 9006 | /* If it's a subclass, we don't really know what putting |
| 9007 | it in the interned dict might do. */ |
| 9008 | if (!PyUnicode_CheckExact(s)) |
| 9009 | return; |
| 9010 | if (PyUnicode_CHECK_INTERNED(s)) |
| 9011 | return; |
| 9012 | if (interned == NULL) { |
| 9013 | interned = PyDict_New(); |
| 9014 | if (interned == NULL) { |
| 9015 | PyErr_Clear(); /* Don't leave an exception */ |
| 9016 | return; |
| 9017 | } |
| 9018 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9019 | /* It might be that the GetItem call fails even |
| 9020 | though the key is present in the dictionary, |
| 9021 | namely when this happens during a stack overflow. */ |
| 9022 | Py_ALLOW_RECURSION |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9023 | t = PyDict_GetItem(interned, (PyObject *)s); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9024 | Py_END_ALLOW_RECURSION |
| 9025 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9026 | if (t) { |
| 9027 | Py_INCREF(t); |
| 9028 | Py_DECREF(*p); |
| 9029 | *p = t; |
| 9030 | return; |
| 9031 | } |
| 9032 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9033 | PyThreadState_GET()->recursion_critical = 1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9034 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 9035 | PyErr_Clear(); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9036 | PyThreadState_GET()->recursion_critical = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9037 | return; |
| 9038 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9039 | PyThreadState_GET()->recursion_critical = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9040 | /* The two references in interned are not counted by refcnt. |
| 9041 | The deallocator will take care of this */ |
Martin v. Löwis | 5d7428b | 2007-07-21 18:47:48 +0000 | [diff] [blame] | 9042 | Py_Refcnt(s) -= 2; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9043 | PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
| 9044 | } |
| 9045 | |
| 9046 | void |
| 9047 | PyUnicode_InternImmortal(PyObject **p) |
| 9048 | { |
| 9049 | PyUnicode_InternInPlace(p); |
| 9050 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 9051 | PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 9052 | Py_INCREF(*p); |
| 9053 | } |
| 9054 | } |
| 9055 | |
| 9056 | PyObject * |
| 9057 | PyUnicode_InternFromString(const char *cp) |
| 9058 | { |
| 9059 | PyObject *s = PyUnicode_FromString(cp); |
| 9060 | if (s == NULL) |
| 9061 | return NULL; |
| 9062 | PyUnicode_InternInPlace(&s); |
| 9063 | return s; |
| 9064 | } |
| 9065 | |
| 9066 | void _Py_ReleaseInternedUnicodeStrings(void) |
| 9067 | { |
| 9068 | PyObject *keys; |
| 9069 | PyUnicodeObject *s; |
| 9070 | Py_ssize_t i, n; |
| 9071 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
| 9072 | |
| 9073 | if (interned == NULL || !PyDict_Check(interned)) |
| 9074 | return; |
| 9075 | keys = PyDict_Keys(interned); |
| 9076 | if (keys == NULL || !PyList_Check(keys)) { |
| 9077 | PyErr_Clear(); |
| 9078 | return; |
| 9079 | } |
| 9080 | |
| 9081 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 9082 | detector, interned unicode strings are not forcibly deallocated; |
| 9083 | rather, we give them their stolen references back, and then clear |
| 9084 | and DECREF the interned dict. */ |
| 9085 | |
| 9086 | n = PyList_GET_SIZE(keys); |
| 9087 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
| 9088 | n); |
| 9089 | for (i = 0; i < n; i++) { |
| 9090 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
| 9091 | switch (s->state) { |
| 9092 | case SSTATE_NOT_INTERNED: |
| 9093 | /* XXX Shouldn't happen */ |
| 9094 | break; |
| 9095 | case SSTATE_INTERNED_IMMORTAL: |
Martin v. Löwis | 5d7428b | 2007-07-21 18:47:48 +0000 | [diff] [blame] | 9096 | Py_Refcnt(s) += 1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9097 | immortal_size += s->length; |
| 9098 | break; |
| 9099 | case SSTATE_INTERNED_MORTAL: |
Martin v. Löwis | 5d7428b | 2007-07-21 18:47:48 +0000 | [diff] [blame] | 9100 | Py_Refcnt(s) += 2; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9101 | mortal_size += s->length; |
| 9102 | break; |
| 9103 | default: |
| 9104 | Py_FatalError("Inconsistent interned string state."); |
| 9105 | } |
| 9106 | s->state = SSTATE_NOT_INTERNED; |
| 9107 | } |
| 9108 | fprintf(stderr, "total size of all interned strings: " |
| 9109 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 9110 | "mortal/immortal\n", mortal_size, immortal_size); |
| 9111 | Py_DECREF(keys); |
| 9112 | PyDict_Clear(interned); |
| 9113 | Py_DECREF(interned); |
| 9114 | interned = NULL; |
| 9115 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9116 | |
| 9117 | |
| 9118 | /********************* Unicode Iterator **************************/ |
| 9119 | |
| 9120 | typedef struct { |
| 9121 | PyObject_HEAD |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9122 | Py_ssize_t it_index; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9123 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
| 9124 | } unicodeiterobject; |
| 9125 | |
| 9126 | static void |
| 9127 | unicodeiter_dealloc(unicodeiterobject *it) |
| 9128 | { |
| 9129 | _PyObject_GC_UNTRACK(it); |
| 9130 | Py_XDECREF(it->it_seq); |
| 9131 | PyObject_GC_Del(it); |
| 9132 | } |
| 9133 | |
| 9134 | static int |
| 9135 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 9136 | { |
| 9137 | Py_VISIT(it->it_seq); |
| 9138 | return 0; |
| 9139 | } |
| 9140 | |
| 9141 | static PyObject * |
| 9142 | unicodeiter_next(unicodeiterobject *it) |
| 9143 | { |
| 9144 | PyUnicodeObject *seq; |
| 9145 | PyObject *item; |
| 9146 | |
| 9147 | assert(it != NULL); |
| 9148 | seq = it->it_seq; |
| 9149 | if (seq == NULL) |
| 9150 | return NULL; |
| 9151 | assert(PyUnicode_Check(seq)); |
| 9152 | |
| 9153 | if (it->it_index < PyUnicode_GET_SIZE(seq)) { |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9154 | item = PyUnicode_FromUnicode( |
| 9155 | PyUnicode_AS_UNICODE(seq)+it->it_index, 1); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9156 | if (item != NULL) |
| 9157 | ++it->it_index; |
| 9158 | return item; |
| 9159 | } |
| 9160 | |
| 9161 | Py_DECREF(seq); |
| 9162 | it->it_seq = NULL; |
| 9163 | return NULL; |
| 9164 | } |
| 9165 | |
| 9166 | static PyObject * |
| 9167 | unicodeiter_len(unicodeiterobject *it) |
| 9168 | { |
| 9169 | Py_ssize_t len = 0; |
| 9170 | if (it->it_seq) |
| 9171 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 9172 | return PyInt_FromSsize_t(len); |
| 9173 | } |
| 9174 | |
| 9175 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 9176 | |
| 9177 | static PyMethodDef unicodeiter_methods[] = { |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9178 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
| 9179 | length_hint_doc}, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9180 | {NULL, NULL} /* sentinel */ |
| 9181 | }; |
| 9182 | |
| 9183 | PyTypeObject PyUnicodeIter_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9184 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9185 | "unicodeiterator", /* tp_name */ |
| 9186 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 9187 | 0, /* tp_itemsize */ |
| 9188 | /* methods */ |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9189 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9190 | 0, /* tp_print */ |
| 9191 | 0, /* tp_getattr */ |
| 9192 | 0, /* tp_setattr */ |
| 9193 | 0, /* tp_compare */ |
| 9194 | 0, /* tp_repr */ |
| 9195 | 0, /* tp_as_number */ |
| 9196 | 0, /* tp_as_sequence */ |
| 9197 | 0, /* tp_as_mapping */ |
| 9198 | 0, /* tp_hash */ |
| 9199 | 0, /* tp_call */ |
| 9200 | 0, /* tp_str */ |
| 9201 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9202 | 0, /* tp_setattro */ |
| 9203 | 0, /* tp_as_buffer */ |
| 9204 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 9205 | 0, /* tp_doc */ |
| 9206 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 9207 | 0, /* tp_clear */ |
| 9208 | 0, /* tp_richcompare */ |
| 9209 | 0, /* tp_weaklistoffset */ |
| 9210 | PyObject_SelfIter, /* tp_iter */ |
| 9211 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 9212 | unicodeiter_methods, /* tp_methods */ |
| 9213 | 0, |
| 9214 | }; |
| 9215 | |
| 9216 | static PyObject * |
| 9217 | unicode_iter(PyObject *seq) |
| 9218 | { |
| 9219 | unicodeiterobject *it; |
| 9220 | |
| 9221 | if (!PyUnicode_Check(seq)) { |
| 9222 | PyErr_BadInternalCall(); |
| 9223 | return NULL; |
| 9224 | } |
| 9225 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 9226 | if (it == NULL) |
| 9227 | return NULL; |
| 9228 | it->it_index = 0; |
| 9229 | Py_INCREF(seq); |
| 9230 | it->it_seq = (PyUnicodeObject *)seq; |
| 9231 | _PyObject_GC_TRACK(it); |
| 9232 | return (PyObject *)it; |
| 9233 | } |
| 9234 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9235 | size_t |
| 9236 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 9237 | { |
| 9238 | int res = 0; |
| 9239 | while(*u++) |
| 9240 | res++; |
| 9241 | return res; |
| 9242 | } |
| 9243 | |
| 9244 | Py_UNICODE* |
| 9245 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9246 | { |
| 9247 | Py_UNICODE *u = s1; |
| 9248 | while ((*u++ = *s2++)); |
| 9249 | return s1; |
| 9250 | } |
| 9251 | |
| 9252 | Py_UNICODE* |
| 9253 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 9254 | { |
| 9255 | Py_UNICODE *u = s1; |
| 9256 | while ((*u++ = *s2++)) |
| 9257 | if (n-- == 0) |
| 9258 | break; |
| 9259 | return s1; |
| 9260 | } |
| 9261 | |
| 9262 | int |
| 9263 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9264 | { |
| 9265 | while (*s1 && *s2 && *s1 == *s2) |
| 9266 | s1++, s2++; |
| 9267 | if (*s1 && *s2) |
| 9268 | return (*s1 < *s2) ? -1 : +1; |
| 9269 | if (*s1) |
| 9270 | return 1; |
| 9271 | if (*s2) |
| 9272 | return -1; |
| 9273 | return 0; |
| 9274 | } |
| 9275 | |
| 9276 | Py_UNICODE* |
| 9277 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 9278 | { |
| 9279 | const Py_UNICODE *p; |
| 9280 | for (p = s; *p; p++) |
| 9281 | if (*p == c) |
| 9282 | return (Py_UNICODE*)p; |
| 9283 | return NULL; |
| 9284 | } |
| 9285 | |
| 9286 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9287 | #ifdef __cplusplus |
| 9288 | } |
| 9289 | #endif |
| 9290 | |
| 9291 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9292 | /* |
| 9293 | Local variables: |
| 9294 | c-basic-offset: 4 |
| 9295 | indent-tabs-mode: nil |
| 9296 | End: |
| 9297 | */ |