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; |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1054 | Py_buffer info; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1055 | |
| 1056 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1057 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1058 | |
| 1059 | /* Shortcuts for common default encodings */ |
| 1060 | if (strcmp(encoding, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1061 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1062 | else if (strcmp(encoding, "latin-1") == 0) |
| 1063 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1064 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1065 | else if (strcmp(encoding, "mbcs") == 0) |
| 1066 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 1067 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1068 | else if (strcmp(encoding, "ascii") == 0) |
| 1069 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1070 | |
| 1071 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1072 | buffer = NULL; |
| 1073 | if (PyBuffer_FillInfo(&info, (void *)s, size, 1, PyBUF_SIMPLE) < 0) |
| 1074 | goto onError; |
| 1075 | buffer = PyMemoryView_FromMemory(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1076 | if (buffer == NULL) |
| 1077 | goto onError; |
| 1078 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1079 | if (unicode == NULL) |
| 1080 | goto onError; |
| 1081 | if (!PyUnicode_Check(unicode)) { |
| 1082 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 1083 | "decoder did not return an unicode object (type=%.400s)", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1084 | Py_Type(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1085 | Py_DECREF(unicode); |
| 1086 | goto onError; |
| 1087 | } |
| 1088 | Py_DECREF(buffer); |
| 1089 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1090 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1091 | onError: |
| 1092 | Py_XDECREF(buffer); |
| 1093 | return NULL; |
| 1094 | } |
| 1095 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1096 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1097 | const char *encoding, |
| 1098 | const char *errors) |
| 1099 | { |
| 1100 | PyObject *v; |
| 1101 | |
| 1102 | if (!PyUnicode_Check(unicode)) { |
| 1103 | PyErr_BadArgument(); |
| 1104 | goto onError; |
| 1105 | } |
| 1106 | |
| 1107 | if (encoding == NULL) |
| 1108 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1109 | |
| 1110 | /* Decode via the codec registry */ |
| 1111 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1112 | if (v == NULL) |
| 1113 | goto onError; |
| 1114 | return v; |
| 1115 | |
| 1116 | onError: |
| 1117 | return NULL; |
| 1118 | } |
| 1119 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1120 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1121 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1122 | const char *encoding, |
| 1123 | const char *errors) |
| 1124 | { |
| 1125 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1126 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1127 | unicode = PyUnicode_FromUnicode(s, size); |
| 1128 | if (unicode == NULL) |
| 1129 | return NULL; |
| 1130 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1131 | Py_DECREF(unicode); |
| 1132 | return v; |
| 1133 | } |
| 1134 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1135 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1136 | const char *encoding, |
| 1137 | const char *errors) |
| 1138 | { |
| 1139 | PyObject *v; |
| 1140 | |
| 1141 | if (!PyUnicode_Check(unicode)) { |
| 1142 | PyErr_BadArgument(); |
| 1143 | goto onError; |
| 1144 | } |
| 1145 | |
| 1146 | if (encoding == NULL) |
| 1147 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1148 | |
| 1149 | /* Encode via the codec registry */ |
| 1150 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1151 | if (v == NULL) |
| 1152 | goto onError; |
| 1153 | return v; |
| 1154 | |
| 1155 | onError: |
| 1156 | return NULL; |
| 1157 | } |
| 1158 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1159 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1160 | const char *encoding, |
| 1161 | const char *errors) |
| 1162 | { |
| 1163 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1164 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1165 | if (!PyUnicode_Check(unicode)) { |
| 1166 | PyErr_BadArgument(); |
| 1167 | goto onError; |
| 1168 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1169 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1170 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1171 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1172 | |
| 1173 | /* Shortcuts for common default encodings */ |
| 1174 | if (errors == NULL) { |
| 1175 | if (strcmp(encoding, "utf-8") == 0) |
Jeremy Hylton | 9cea41c | 2001-05-29 17:13:15 +0000 | [diff] [blame] | 1176 | return PyUnicode_AsUTF8String(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1177 | else if (strcmp(encoding, "latin-1") == 0) |
| 1178 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1179 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1180 | else if (strcmp(encoding, "mbcs") == 0) |
| 1181 | return PyUnicode_AsMBCSString(unicode); |
| 1182 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1183 | else if (strcmp(encoding, "ascii") == 0) |
| 1184 | return PyUnicode_AsASCIIString(unicode); |
| 1185 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1186 | |
| 1187 | /* Encode via the codec registry */ |
| 1188 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1189 | if (v == NULL) |
| 1190 | goto onError; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1191 | if (!PyBytes_Check(v)) { |
| 1192 | if (PyString_Check(v)) { |
| 1193 | /* Old codec, turn it into bytes */ |
| 1194 | PyObject *b = PyBytes_FromObject(v); |
| 1195 | Py_DECREF(v); |
| 1196 | return b; |
| 1197 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1198 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1199 | "encoder did not return a bytes object " |
| 1200 | "(type=%.400s, encoding=%.20s, errors=%.20s)", |
| 1201 | v->ob_type->tp_name, |
| 1202 | encoding ? encoding : "NULL", |
| 1203 | errors ? errors : "NULL"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1204 | Py_DECREF(v); |
| 1205 | goto onError; |
| 1206 | } |
| 1207 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1208 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1209 | onError: |
| 1210 | return NULL; |
| 1211 | } |
| 1212 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1213 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 1214 | const char *errors) |
| 1215 | { |
| 1216 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1217 | PyObject *b; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1218 | if (v) |
| 1219 | return v; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1220 | if (errors != NULL) |
| 1221 | Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString"); |
Guido van Rossum | 0661009 | 2007-08-16 21:02:22 +0000 | [diff] [blame] | 1222 | b = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1223 | PyUnicode_GET_SIZE(unicode), |
| 1224 | NULL); |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1225 | if (!b) |
| 1226 | return NULL; |
| 1227 | v = PyString_FromStringAndSize(PyBytes_AsString(b), |
| 1228 | PyBytes_Size(b)); |
| 1229 | Py_DECREF(b); |
Guido van Rossum | e7a0d39 | 2007-07-12 07:53:00 +0000 | [diff] [blame] | 1230 | ((PyUnicodeObject *)unicode)->defenc = v; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1231 | return v; |
| 1232 | } |
| 1233 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1234 | char* |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1235 | PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1236 | { |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1237 | PyObject *str8; |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 1238 | if (!PyUnicode_Check(unicode)) { |
| 1239 | PyErr_BadArgument(); |
| 1240 | return NULL; |
| 1241 | } |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1242 | str8 = _PyUnicode_AsDefaultEncodedString(unicode, NULL); |
| 1243 | if (str8 == NULL) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1244 | return NULL; |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1245 | if (psize != NULL) |
| 1246 | *psize = PyString_GET_SIZE(str8); |
| 1247 | return PyString_AS_STRING(str8); |
| 1248 | } |
| 1249 | |
| 1250 | char* |
| 1251 | PyUnicode_AsString(PyObject *unicode) |
| 1252 | { |
| 1253 | return PyUnicode_AsStringAndSize(unicode, NULL); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1256 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1257 | { |
| 1258 | if (!PyUnicode_Check(unicode)) { |
| 1259 | PyErr_BadArgument(); |
| 1260 | goto onError; |
| 1261 | } |
| 1262 | return PyUnicode_AS_UNICODE(unicode); |
| 1263 | |
| 1264 | onError: |
| 1265 | return NULL; |
| 1266 | } |
| 1267 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1268 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1269 | { |
| 1270 | if (!PyUnicode_Check(unicode)) { |
| 1271 | PyErr_BadArgument(); |
| 1272 | goto onError; |
| 1273 | } |
| 1274 | return PyUnicode_GET_SIZE(unicode); |
| 1275 | |
| 1276 | onError: |
| 1277 | return -1; |
| 1278 | } |
| 1279 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1280 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1281 | { |
| 1282 | return unicode_default_encoding; |
| 1283 | } |
| 1284 | |
| 1285 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 1286 | { |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1287 | if (strcmp(encoding, unicode_default_encoding) != 0) { |
| 1288 | PyErr_Format(PyExc_ValueError, |
| 1289 | "Can only set default encoding to %s", |
| 1290 | unicode_default_encoding); |
| 1291 | return -1; |
| 1292 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1293 | return 0; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1296 | /* error handling callback helper: |
| 1297 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1298 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1299 | and adjust various state variables. |
| 1300 | return 0 on success, -1 on error |
| 1301 | */ |
| 1302 | |
| 1303 | static |
| 1304 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 1305 | const char *encoding, const char *reason, |
Walter Dörwald | a651d3d | 2007-08-30 15:29:21 +0000 | [diff] [blame] | 1306 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 1307 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1308 | PyObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1309 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1310 | 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] | 1311 | |
| 1312 | PyObject *restuple = NULL; |
| 1313 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1314 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1315 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1316 | Py_ssize_t requiredsize; |
| 1317 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1318 | Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1319 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1320 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1321 | int res = -1; |
| 1322 | |
| 1323 | if (*errorHandler == NULL) { |
| 1324 | *errorHandler = PyCodec_LookupError(errors); |
| 1325 | if (*errorHandler == NULL) |
| 1326 | goto onError; |
| 1327 | } |
| 1328 | |
| 1329 | if (*exceptionObject == NULL) { |
| 1330 | *exceptionObject = PyUnicodeDecodeError_Create( |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1331 | encoding, *input, *inend-*input, *startinpos, *endinpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1332 | if (*exceptionObject == NULL) |
| 1333 | goto onError; |
| 1334 | } |
| 1335 | else { |
| 1336 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 1337 | goto onError; |
| 1338 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 1339 | goto onError; |
| 1340 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1341 | goto onError; |
| 1342 | } |
| 1343 | |
| 1344 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1345 | if (restuple == NULL) |
| 1346 | goto onError; |
| 1347 | if (!PyTuple_Check(restuple)) { |
| 1348 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 1349 | goto onError; |
| 1350 | } |
| 1351 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 1352 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1353 | |
| 1354 | /* Copy back the bytes variables, which might have been modified by the |
| 1355 | callback */ |
| 1356 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 1357 | if (!inputobj) |
| 1358 | goto onError; |
| 1359 | if (!PyBytes_Check(inputobj)) { |
| 1360 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
| 1361 | } |
| 1362 | *input = PyBytes_AS_STRING(inputobj); |
| 1363 | insize = PyBytes_GET_SIZE(inputobj); |
| 1364 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 1365 | /* we can DECREF safely, as the exception has another reference, |
| 1366 | so the object won't go away. */ |
| 1367 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1368 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1369 | if (newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1370 | newpos = insize+newpos; |
| 1371 | if (newpos<0 || newpos>insize) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 1372 | 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] | 1373 | goto onError; |
| 1374 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1375 | |
| 1376 | /* need more space? (at least enough for what we |
| 1377 | have+the replacement+the rest of the string (starting |
| 1378 | at the new input position), so we won't have to check space |
| 1379 | when there are no errors in the rest of the string) */ |
| 1380 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 1381 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 1382 | requiredsize = *outpos + repsize + insize-newpos; |
| 1383 | if (requiredsize > outsize) { |
| 1384 | if (requiredsize<2*outsize) |
| 1385 | requiredsize = 2*outsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1386 | if (PyUnicode_Resize(output, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1387 | goto onError; |
| 1388 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
| 1389 | } |
| 1390 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1391 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1392 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 1393 | *outptr += repsize; |
| 1394 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1395 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1396 | /* we made it! */ |
| 1397 | res = 0; |
| 1398 | |
| 1399 | onError: |
| 1400 | Py_XDECREF(restuple); |
| 1401 | return res; |
| 1402 | } |
| 1403 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1404 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 1405 | |
| 1406 | /* see RFC2152 for details */ |
| 1407 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1408 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1409 | char utf7_special[128] = { |
| 1410 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 1411 | encoded: |
| 1412 | 0 - not special |
| 1413 | 1 - special |
| 1414 | 2 - whitespace (optional) |
| 1415 | 3 - RFC2152 Set O (optional) */ |
| 1416 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 1417 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1418 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 1419 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 1420 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1421 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 1422 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1423 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 1424 | |
| 1425 | }; |
| 1426 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1427 | /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
| 1428 | warnings about the comparison always being false; since |
| 1429 | utf7_special[0] is 1, we can safely make that one comparison |
| 1430 | true */ |
| 1431 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1432 | #define SPECIAL(c, encodeO, encodeWS) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1433 | ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1434 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1435 | (encodeO && (utf7_special[(c)] == 3))) |
| 1436 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1437 | #define B64(n) \ |
| 1438 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 1439 | #define B64CHAR(c) \ |
| 1440 | (isalnum(c) || (c) == '+' || (c) == '/') |
| 1441 | #define UB64(c) \ |
| 1442 | ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
| 1443 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1444 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1445 | #define ENCODE(out, ch, bits) \ |
| 1446 | while (bits >= 6) { \ |
| 1447 | *out++ = B64(ch >> (bits-6)); \ |
| 1448 | bits -= 6; \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1451 | #define DECODE(out, ch, bits, surrogate) \ |
| 1452 | while (bits >= 16) { \ |
| 1453 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 1454 | bits -= 16; \ |
| 1455 | if (surrogate) { \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1456 | /* We have already generated an error for the high surrogate \ |
| 1457 | 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] | 1458 | surrogate = 0; \ |
| 1459 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1460 | /* This is a surrogate pair. Unfortunately we can't represent \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1461 | it in a 16-bit character */ \ |
| 1462 | surrogate = 1; \ |
| 1463 | errmsg = "code pairs are not supported"; \ |
| 1464 | goto utf7Error; \ |
| 1465 | } else { \ |
| 1466 | *out++ = outCh; \ |
| 1467 | } \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1468 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1469 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1470 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1471 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1472 | const char *errors) |
| 1473 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1474 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1475 | Py_ssize_t startinpos; |
| 1476 | Py_ssize_t endinpos; |
| 1477 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1478 | const char *e; |
| 1479 | PyUnicodeObject *unicode; |
| 1480 | Py_UNICODE *p; |
| 1481 | const char *errmsg = ""; |
| 1482 | int inShift = 0; |
| 1483 | unsigned int bitsleft = 0; |
| 1484 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1485 | int surrogate = 0; |
| 1486 | PyObject *errorHandler = NULL; |
| 1487 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1488 | |
| 1489 | unicode = _PyUnicode_New(size); |
| 1490 | if (!unicode) |
| 1491 | return NULL; |
| 1492 | if (size == 0) |
| 1493 | return (PyObject *)unicode; |
| 1494 | |
| 1495 | p = unicode->str; |
| 1496 | e = s + size; |
| 1497 | |
| 1498 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1499 | Py_UNICODE ch; |
| 1500 | restart: |
| 1501 | ch = *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1502 | |
| 1503 | if (inShift) { |
| 1504 | if ((ch == '-') || !B64CHAR(ch)) { |
| 1505 | inShift = 0; |
| 1506 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1507 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1508 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1509 | if (bitsleft >= 6) { |
| 1510 | /* The shift sequence has a partial character in it. If |
| 1511 | bitsleft < 6 then we could just classify it as padding |
| 1512 | but that is not the case here */ |
| 1513 | |
| 1514 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1515 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1516 | } |
| 1517 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1518 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1519 | here so indicate the potential of a misencoded character. */ |
| 1520 | |
| 1521 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 1522 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 1523 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1524 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | if (ch == '-') { |
| 1528 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1529 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1530 | inShift = 1; |
| 1531 | } |
| 1532 | } else if (SPECIAL(ch,0,0)) { |
| 1533 | errmsg = "unexpected special character"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1534 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1535 | } else { |
| 1536 | *p++ = ch; |
| 1537 | } |
| 1538 | } else { |
| 1539 | charsleft = (charsleft << 6) | UB64(ch); |
| 1540 | bitsleft += 6; |
| 1541 | s++; |
| 1542 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1543 | } |
| 1544 | } |
| 1545 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1546 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1547 | s++; |
| 1548 | if (s < e && *s == '-') { |
| 1549 | s++; |
| 1550 | *p++ = '+'; |
| 1551 | } else |
| 1552 | { |
| 1553 | inShift = 1; |
| 1554 | bitsleft = 0; |
| 1555 | } |
| 1556 | } |
| 1557 | else if (SPECIAL(ch,0,0)) { |
Walter Dörwald | 2b65c75 | 2007-08-30 15:35:26 +0000 | [diff] [blame] | 1558 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1559 | errmsg = "unexpected special character"; |
| 1560 | s++; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1561 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1562 | } |
| 1563 | else { |
| 1564 | *p++ = ch; |
| 1565 | s++; |
| 1566 | } |
| 1567 | continue; |
| 1568 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1569 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1570 | endinpos = s-starts; |
| 1571 | if (unicode_decode_call_errorhandler( |
| 1572 | errors, &errorHandler, |
| 1573 | "utf7", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1574 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1575 | (PyObject **)&unicode, &outpos, &p)) |
| 1576 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | if (inShift) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1580 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1581 | endinpos = size; |
| 1582 | if (unicode_decode_call_errorhandler( |
| 1583 | errors, &errorHandler, |
| 1584 | "utf7", "unterminated shift sequence", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1585 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1586 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1587 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1588 | if (s < e) |
| 1589 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1592 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1593 | goto onError; |
| 1594 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1595 | Py_XDECREF(errorHandler); |
| 1596 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1597 | return (PyObject *)unicode; |
| 1598 | |
| 1599 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1600 | Py_XDECREF(errorHandler); |
| 1601 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1602 | Py_DECREF(unicode); |
| 1603 | return NULL; |
| 1604 | } |
| 1605 | |
| 1606 | |
| 1607 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1608 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1609 | int encodeSetO, |
| 1610 | int encodeWhiteSpace, |
| 1611 | const char *errors) |
| 1612 | { |
| 1613 | PyObject *v; |
| 1614 | /* It might be possible to tighten this worst case */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1615 | Py_ssize_t cbAllocated = 5 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1616 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1617 | Py_ssize_t i = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1618 | unsigned int bitsleft = 0; |
| 1619 | unsigned long charsleft = 0; |
| 1620 | char * out; |
| 1621 | char * start; |
| 1622 | |
| 1623 | if (size == 0) |
Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1624 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1625 | |
Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1626 | v = PyBytes_FromStringAndSize(NULL, cbAllocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1627 | if (v == NULL) |
| 1628 | return NULL; |
| 1629 | |
Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1630 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1631 | for (;i < size; ++i) { |
| 1632 | Py_UNICODE ch = s[i]; |
| 1633 | |
| 1634 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1635 | if (ch == '+') { |
| 1636 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1637 | *out++ = '-'; |
| 1638 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1639 | charsleft = ch; |
| 1640 | bitsleft = 16; |
| 1641 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1642 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1643 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1644 | } else { |
| 1645 | *out++ = (char) ch; |
| 1646 | } |
| 1647 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1648 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1649 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1650 | charsleft = 0; |
| 1651 | bitsleft = 0; |
| 1652 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1653 | so no '-' is required, except if the character is itself a '-' */ |
| 1654 | if (B64CHAR(ch) || ch == '-') { |
| 1655 | *out++ = '-'; |
| 1656 | } |
| 1657 | inShift = 0; |
| 1658 | *out++ = (char) ch; |
| 1659 | } else { |
| 1660 | bitsleft += 16; |
| 1661 | charsleft = (charsleft << 16) | ch; |
| 1662 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1663 | |
| 1664 | /* If the next character is special then we dont' need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1665 | 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] | 1666 | or '-' then the shift sequence will be terminated implicitly and we |
| 1667 | don't have to insert a '-'. */ |
| 1668 | |
| 1669 | if (bitsleft == 0) { |
| 1670 | if (i + 1 < size) { |
| 1671 | Py_UNICODE ch2 = s[i+1]; |
| 1672 | |
| 1673 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1674 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1675 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1676 | *out++ = '-'; |
| 1677 | inShift = 0; |
| 1678 | } else { |
| 1679 | inShift = 0; |
| 1680 | } |
| 1681 | |
| 1682 | } |
| 1683 | else { |
| 1684 | *out++ = '-'; |
| 1685 | inShift = 0; |
| 1686 | } |
| 1687 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1688 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1689 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1690 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1691 | if (bitsleft) { |
| 1692 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1693 | *out++ = '-'; |
| 1694 | } |
| 1695 | |
Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1696 | if (PyBytes_Resize(v, out - start)) { |
| 1697 | Py_DECREF(v); |
| 1698 | return NULL; |
| 1699 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1700 | return v; |
| 1701 | } |
| 1702 | |
| 1703 | #undef SPECIAL |
| 1704 | #undef B64 |
| 1705 | #undef B64CHAR |
| 1706 | #undef UB64 |
| 1707 | #undef ENCODE |
| 1708 | #undef DECODE |
| 1709 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1710 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1711 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1712 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1713 | char utf8_code_length[256] = { |
| 1714 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1715 | illegal prefix. see RFC 2279 for details */ |
| 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 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1721 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1722 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1723 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1724 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1725 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1726 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1727 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1728 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1729 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1730 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1731 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1732 | }; |
| 1733 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1734 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1735 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1736 | const char *errors) |
| 1737 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1738 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 1739 | } |
| 1740 | |
| 1741 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1742 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1743 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1744 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1745 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1746 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1747 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1748 | Py_ssize_t startinpos; |
| 1749 | Py_ssize_t endinpos; |
| 1750 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1751 | const char *e; |
| 1752 | PyUnicodeObject *unicode; |
| 1753 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1754 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1755 | PyObject *errorHandler = NULL; |
| 1756 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1757 | |
| 1758 | /* Note: size will always be longer than the resulting Unicode |
| 1759 | character count */ |
| 1760 | unicode = _PyUnicode_New(size); |
| 1761 | if (!unicode) |
| 1762 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1763 | if (size == 0) { |
| 1764 | if (consumed) |
| 1765 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1766 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1767 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1768 | |
| 1769 | /* Unpack UTF-8 encoded data */ |
| 1770 | p = unicode->str; |
| 1771 | e = s + size; |
| 1772 | |
| 1773 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1774 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1775 | |
| 1776 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1777 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1778 | s++; |
| 1779 | continue; |
| 1780 | } |
| 1781 | |
| 1782 | n = utf8_code_length[ch]; |
| 1783 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1784 | if (s + n > e) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1785 | if (consumed) |
| 1786 | break; |
| 1787 | else { |
| 1788 | errmsg = "unexpected end of data"; |
| 1789 | startinpos = s-starts; |
| 1790 | endinpos = size; |
| 1791 | goto utf8Error; |
| 1792 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1793 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1794 | |
| 1795 | switch (n) { |
| 1796 | |
| 1797 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1798 | errmsg = "unexpected code byte"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1799 | startinpos = s-starts; |
| 1800 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1801 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1802 | |
| 1803 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1804 | errmsg = "internal error"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1805 | startinpos = s-starts; |
| 1806 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1807 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1808 | |
| 1809 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1810 | if ((s[1] & 0xc0) != 0x80) { |
| 1811 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1812 | startinpos = s-starts; |
| 1813 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1814 | goto utf8Error; |
| 1815 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1816 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1817 | if (ch < 0x80) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1818 | startinpos = s-starts; |
| 1819 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1820 | errmsg = "illegal encoding"; |
| 1821 | goto utf8Error; |
| 1822 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1823 | else |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1824 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1825 | break; |
| 1826 | |
| 1827 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1828 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1829 | (s[2] & 0xc0) != 0x80) { |
| 1830 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1831 | startinpos = s-starts; |
| 1832 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1833 | goto utf8Error; |
| 1834 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1835 | 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] | 1836 | if (ch < 0x0800) { |
| 1837 | /* Note: UTF-8 encodings of surrogates are considered |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1838 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1839 | |
| 1840 | XXX For wide builds (UCS-4) we should probably try |
| 1841 | to recombine the surrogates into a single code |
| 1842 | unit. |
| 1843 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1844 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1845 | startinpos = s-starts; |
| 1846 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1847 | goto utf8Error; |
| 1848 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1849 | else |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1850 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1851 | break; |
| 1852 | |
| 1853 | case 4: |
| 1854 | if ((s[1] & 0xc0) != 0x80 || |
| 1855 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1856 | (s[3] & 0xc0) != 0x80) { |
| 1857 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1858 | startinpos = s-starts; |
| 1859 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1860 | goto utf8Error; |
| 1861 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1862 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 1863 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 1864 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1865 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1866 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1867 | || (ch > 0x10ffff)) /* maximum value allowed for |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1868 | UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1869 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1870 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1871 | startinpos = s-starts; |
| 1872 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1873 | goto utf8Error; |
| 1874 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1875 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1876 | *p++ = (Py_UNICODE)ch; |
| 1877 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1878 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1879 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1880 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 1881 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1882 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1883 | /* high surrogate = top 10 bits added to D800 */ |
| 1884 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1885 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1886 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1887 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1888 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1889 | break; |
| 1890 | |
| 1891 | default: |
| 1892 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1893 | errmsg = "unsupported Unicode code range"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1894 | startinpos = s-starts; |
| 1895 | endinpos = startinpos+n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1896 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1897 | } |
| 1898 | s += n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1899 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1900 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1901 | utf8Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1902 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1903 | if (unicode_decode_call_errorhandler( |
| 1904 | errors, &errorHandler, |
| 1905 | "utf8", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1906 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1907 | (PyObject **)&unicode, &outpos, &p)) |
| 1908 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1909 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1910 | if (consumed) |
| 1911 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1912 | |
| 1913 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1914 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1915 | goto onError; |
| 1916 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1917 | Py_XDECREF(errorHandler); |
| 1918 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1919 | return (PyObject *)unicode; |
| 1920 | |
| 1921 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1922 | Py_XDECREF(errorHandler); |
| 1923 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1924 | Py_DECREF(unicode); |
| 1925 | return NULL; |
| 1926 | } |
| 1927 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1928 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 1929 | and allocate exactly as much space needed at the end. Else allocate the |
| 1930 | maximum possible needed (4 result bytes per Unicode character), and return |
| 1931 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1932 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1933 | PyObject * |
| 1934 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1935 | Py_ssize_t size, |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1936 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1937 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1938 | #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] | 1939 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1940 | Py_ssize_t i; /* index into s of next input byte */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1941 | PyObject *v; /* result string object */ |
| 1942 | char *p; /* next free byte in output buffer */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1943 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1944 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1945 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1946 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1947 | assert(s != NULL); |
| 1948 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1949 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1950 | if (size <= MAX_SHORT_UNICHARS) { |
| 1951 | /* Write into the stack buffer; nallocated can't overflow. |
| 1952 | * At the end, we'll allocate exactly as much heap space as it |
| 1953 | * turns out we need. |
| 1954 | */ |
| 1955 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
| 1956 | v = NULL; /* will allocate after we're done */ |
| 1957 | p = stackbuf; |
| 1958 | } |
| 1959 | else { |
| 1960 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 1961 | nallocated = size * 4; |
| 1962 | if (nallocated / 4 != size) /* overflow! */ |
| 1963 | return PyErr_NoMemory(); |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1964 | v = PyBytes_FromStringAndSize(NULL, nallocated); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1965 | if (v == NULL) |
| 1966 | return NULL; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1967 | p = PyBytes_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1968 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1969 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1970 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1971 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1972 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1973 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1974 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1975 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1976 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1977 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1978 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 1979 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 1980 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1981 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1982 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1983 | /* Encode UCS2 Unicode ordinals */ |
| 1984 | if (ch < 0x10000) { |
| 1985 | /* Special case: check for high surrogate */ |
| 1986 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 1987 | Py_UCS4 ch2 = s[i]; |
| 1988 | /* Check for low surrogate and combine the two to |
| 1989 | form a UCS4 value */ |
| 1990 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1991 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1992 | i++; |
| 1993 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1994 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1995 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1996 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1997 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1998 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1999 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2000 | continue; |
| 2001 | } |
| 2002 | encodeUCS4: |
| 2003 | /* Encode UCS4 Unicode ordinals */ |
| 2004 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2005 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 2006 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2007 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2008 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2009 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2010 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2011 | if (v == NULL) { |
| 2012 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2013 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2014 | assert(nneeded <= nallocated); |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 2015 | v = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2016 | } |
| 2017 | else { |
| 2018 | /* Cut back to size actually needed. */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 2019 | nneeded = p - PyBytes_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2020 | assert(nneeded <= nallocated); |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 2021 | PyBytes_Resize(v, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2022 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2023 | return v; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2024 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2025 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2028 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 2029 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2030 | if (!PyUnicode_Check(unicode)) { |
| 2031 | PyErr_BadArgument(); |
| 2032 | return NULL; |
| 2033 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 2034 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 2035 | PyUnicode_GET_SIZE(unicode), |
| 2036 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2037 | } |
| 2038 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2039 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 2040 | |
| 2041 | PyObject * |
| 2042 | PyUnicode_DecodeUTF32(const char *s, |
| 2043 | Py_ssize_t size, |
| 2044 | const char *errors, |
| 2045 | int *byteorder) |
| 2046 | { |
| 2047 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 2048 | } |
| 2049 | |
| 2050 | PyObject * |
| 2051 | PyUnicode_DecodeUTF32Stateful(const char *s, |
| 2052 | Py_ssize_t size, |
| 2053 | const char *errors, |
| 2054 | int *byteorder, |
| 2055 | Py_ssize_t *consumed) |
| 2056 | { |
| 2057 | const char *starts = s; |
| 2058 | Py_ssize_t startinpos; |
| 2059 | Py_ssize_t endinpos; |
| 2060 | Py_ssize_t outpos; |
| 2061 | PyUnicodeObject *unicode; |
| 2062 | Py_UNICODE *p; |
| 2063 | #ifndef Py_UNICODE_WIDE |
| 2064 | int i, pairs; |
| 2065 | #else |
| 2066 | const int pairs = 0; |
| 2067 | #endif |
| 2068 | const unsigned char *q, *e; |
| 2069 | int bo = 0; /* assume native ordering by default */ |
| 2070 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2071 | /* Offsets from q for retrieving bytes in the right order. */ |
| 2072 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2073 | int iorder[] = {0, 1, 2, 3}; |
| 2074 | #else |
| 2075 | int iorder[] = {3, 2, 1, 0}; |
| 2076 | #endif |
| 2077 | PyObject *errorHandler = NULL; |
| 2078 | PyObject *exc = NULL; |
Guido van Rossum | 8d991ed | 2007-08-17 15:41:00 +0000 | [diff] [blame] | 2079 | /* On narrow builds we split characters outside the BMP into two |
| 2080 | codepoints => count how much extra space we need. */ |
| 2081 | #ifndef Py_UNICODE_WIDE |
| 2082 | for (i = pairs = 0; i < size/4; i++) |
| 2083 | if (((Py_UCS4 *)s)[i] >= 0x10000) |
| 2084 | pairs++; |
| 2085 | #endif |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2086 | |
| 2087 | /* This might be one to much, because of a BOM */ |
| 2088 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 2089 | if (!unicode) |
| 2090 | return NULL; |
| 2091 | if (size == 0) |
| 2092 | return (PyObject *)unicode; |
| 2093 | |
| 2094 | /* Unpack UTF-32 encoded data */ |
| 2095 | p = unicode->str; |
| 2096 | q = (unsigned char *)s; |
| 2097 | e = q + size; |
| 2098 | |
| 2099 | if (byteorder) |
| 2100 | bo = *byteorder; |
| 2101 | |
| 2102 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2103 | byte order setting accordingly. In native mode, the leading BOM |
| 2104 | mark is skipped, in all other modes, it is copied to the output |
| 2105 | stream as-is (giving a ZWNBSP character). */ |
| 2106 | if (bo == 0) { |
| 2107 | if (size >= 4) { |
| 2108 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2109 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 2110 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2111 | if (bom == 0x0000FEFF) { |
| 2112 | q += 4; |
| 2113 | bo = -1; |
| 2114 | } |
| 2115 | else if (bom == 0xFFFE0000) { |
| 2116 | q += 4; |
| 2117 | bo = 1; |
| 2118 | } |
| 2119 | #else |
| 2120 | if (bom == 0x0000FEFF) { |
| 2121 | q += 4; |
| 2122 | bo = 1; |
| 2123 | } |
| 2124 | else if (bom == 0xFFFE0000) { |
| 2125 | q += 4; |
| 2126 | bo = -1; |
| 2127 | } |
| 2128 | #endif |
| 2129 | } |
| 2130 | } |
| 2131 | |
| 2132 | if (bo == -1) { |
| 2133 | /* force LE */ |
| 2134 | iorder[0] = 0; |
| 2135 | iorder[1] = 1; |
| 2136 | iorder[2] = 2; |
| 2137 | iorder[3] = 3; |
| 2138 | } |
| 2139 | else if (bo == 1) { |
| 2140 | /* force BE */ |
| 2141 | iorder[0] = 3; |
| 2142 | iorder[1] = 2; |
| 2143 | iorder[2] = 1; |
| 2144 | iorder[3] = 0; |
| 2145 | } |
| 2146 | |
| 2147 | while (q < e) { |
| 2148 | Py_UCS4 ch; |
| 2149 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 2150 | if (e-q<4) { |
| 2151 | if (consumed) |
| 2152 | break; |
| 2153 | errmsg = "truncated data"; |
| 2154 | startinpos = ((const char *)q)-starts; |
| 2155 | endinpos = ((const char *)e)-starts; |
| 2156 | goto utf32Error; |
| 2157 | /* The remaining input chars are ignored if the callback |
| 2158 | chooses to skip the input */ |
| 2159 | } |
| 2160 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2161 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 2162 | |
| 2163 | if (ch >= 0x110000) |
| 2164 | { |
| 2165 | errmsg = "codepoint not in range(0x110000)"; |
| 2166 | startinpos = ((const char *)q)-starts; |
| 2167 | endinpos = startinpos+4; |
| 2168 | goto utf32Error; |
| 2169 | } |
| 2170 | #ifndef Py_UNICODE_WIDE |
| 2171 | if (ch >= 0x10000) |
| 2172 | { |
| 2173 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 2174 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2175 | } |
| 2176 | else |
| 2177 | #endif |
| 2178 | *p++ = ch; |
| 2179 | q += 4; |
| 2180 | continue; |
| 2181 | utf32Error: |
| 2182 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2183 | if (unicode_decode_call_errorhandler( |
| 2184 | errors, &errorHandler, |
| 2185 | "utf32", errmsg, |
| 2186 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 2187 | (PyObject **)&unicode, &outpos, &p)) |
| 2188 | goto onError; |
| 2189 | } |
| 2190 | |
| 2191 | if (byteorder) |
| 2192 | *byteorder = bo; |
| 2193 | |
| 2194 | if (consumed) |
| 2195 | *consumed = (const char *)q-starts; |
| 2196 | |
| 2197 | /* Adjust length */ |
| 2198 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 2199 | goto onError; |
| 2200 | |
| 2201 | Py_XDECREF(errorHandler); |
| 2202 | Py_XDECREF(exc); |
| 2203 | return (PyObject *)unicode; |
| 2204 | |
| 2205 | onError: |
| 2206 | Py_DECREF(unicode); |
| 2207 | Py_XDECREF(errorHandler); |
| 2208 | Py_XDECREF(exc); |
| 2209 | return NULL; |
| 2210 | } |
| 2211 | |
| 2212 | PyObject * |
| 2213 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 2214 | Py_ssize_t size, |
| 2215 | const char *errors, |
| 2216 | int byteorder) |
| 2217 | { |
| 2218 | PyObject *v; |
| 2219 | unsigned char *p; |
| 2220 | #ifndef Py_UNICODE_WIDE |
| 2221 | int i, pairs; |
| 2222 | #else |
| 2223 | const int pairs = 0; |
| 2224 | #endif |
| 2225 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2226 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2227 | int iorder[] = {0, 1, 2, 3}; |
| 2228 | #else |
| 2229 | int iorder[] = {3, 2, 1, 0}; |
| 2230 | #endif |
| 2231 | |
| 2232 | #define STORECHAR(CH) \ |
| 2233 | do { \ |
| 2234 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 2235 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 2236 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 2237 | p[iorder[0]] = (CH) & 0xff; \ |
| 2238 | p += 4; \ |
| 2239 | } while(0) |
| 2240 | |
| 2241 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 2242 | so we need less space. */ |
| 2243 | #ifndef Py_UNICODE_WIDE |
| 2244 | for (i = pairs = 0; i < size-1; i++) |
| 2245 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2246 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 2247 | pairs++; |
| 2248 | #endif |
| 2249 | v = PyBytes_FromStringAndSize(NULL, |
| 2250 | 4 * (size - pairs + (byteorder == 0))); |
| 2251 | if (v == NULL) |
| 2252 | return NULL; |
| 2253 | |
| 2254 | p = (unsigned char *)PyBytes_AS_STRING(v); |
| 2255 | if (byteorder == 0) |
| 2256 | STORECHAR(0xFEFF); |
| 2257 | if (size == 0) |
| 2258 | return v; |
| 2259 | |
| 2260 | if (byteorder == -1) { |
| 2261 | /* force LE */ |
| 2262 | iorder[0] = 0; |
| 2263 | iorder[1] = 1; |
| 2264 | iorder[2] = 2; |
| 2265 | iorder[3] = 3; |
| 2266 | } |
| 2267 | else if (byteorder == 1) { |
| 2268 | /* force BE */ |
| 2269 | iorder[0] = 3; |
| 2270 | iorder[1] = 2; |
| 2271 | iorder[2] = 1; |
| 2272 | iorder[3] = 0; |
| 2273 | } |
| 2274 | |
| 2275 | while (size-- > 0) { |
| 2276 | Py_UCS4 ch = *s++; |
| 2277 | #ifndef Py_UNICODE_WIDE |
| 2278 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 2279 | Py_UCS4 ch2 = *s; |
| 2280 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 2281 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 2282 | s++; |
| 2283 | size--; |
| 2284 | } |
| 2285 | } |
| 2286 | #endif |
| 2287 | STORECHAR(ch); |
| 2288 | } |
| 2289 | return v; |
| 2290 | #undef STORECHAR |
| 2291 | } |
| 2292 | |
| 2293 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 2294 | { |
| 2295 | if (!PyUnicode_Check(unicode)) { |
| 2296 | PyErr_BadArgument(); |
| 2297 | return NULL; |
| 2298 | } |
| 2299 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
| 2300 | PyUnicode_GET_SIZE(unicode), |
| 2301 | NULL, |
| 2302 | 0); |
| 2303 | } |
| 2304 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2305 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 2306 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2307 | PyObject * |
| 2308 | PyUnicode_DecodeUTF16(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2309 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2310 | const char *errors, |
| 2311 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2312 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2313 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 2314 | } |
| 2315 | |
| 2316 | PyObject * |
| 2317 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2318 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2319 | const char *errors, |
| 2320 | int *byteorder, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2321 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2322 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2323 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2324 | Py_ssize_t startinpos; |
| 2325 | Py_ssize_t endinpos; |
| 2326 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2327 | PyUnicodeObject *unicode; |
| 2328 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2329 | const unsigned char *q, *e; |
| 2330 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2331 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2332 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 2333 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2334 | int ihi = 1, ilo = 0; |
| 2335 | #else |
| 2336 | int ihi = 0, ilo = 1; |
| 2337 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2338 | PyObject *errorHandler = NULL; |
| 2339 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2340 | |
| 2341 | /* Note: size will always be longer than the resulting Unicode |
| 2342 | character count */ |
| 2343 | unicode = _PyUnicode_New(size); |
| 2344 | if (!unicode) |
| 2345 | return NULL; |
| 2346 | if (size == 0) |
| 2347 | return (PyObject *)unicode; |
| 2348 | |
| 2349 | /* Unpack UTF-16 encoded data */ |
| 2350 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2351 | q = (unsigned char *)s; |
| 2352 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2353 | |
| 2354 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2355 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2356 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2357 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2358 | byte order setting accordingly. In native mode, the leading BOM |
| 2359 | mark is skipped, in all other modes, it is copied to the output |
| 2360 | stream as-is (giving a ZWNBSP character). */ |
| 2361 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2362 | if (size >= 2) { |
| 2363 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2364 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2365 | if (bom == 0xFEFF) { |
| 2366 | q += 2; |
| 2367 | bo = -1; |
| 2368 | } |
| 2369 | else if (bom == 0xFFFE) { |
| 2370 | q += 2; |
| 2371 | bo = 1; |
| 2372 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2373 | #else |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2374 | if (bom == 0xFEFF) { |
| 2375 | q += 2; |
| 2376 | bo = 1; |
| 2377 | } |
| 2378 | else if (bom == 0xFFFE) { |
| 2379 | q += 2; |
| 2380 | bo = -1; |
| 2381 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2382 | #endif |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2383 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2384 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2385 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2386 | if (bo == -1) { |
| 2387 | /* force LE */ |
| 2388 | ihi = 1; |
| 2389 | ilo = 0; |
| 2390 | } |
| 2391 | else if (bo == 1) { |
| 2392 | /* force BE */ |
| 2393 | ihi = 0; |
| 2394 | ilo = 1; |
| 2395 | } |
| 2396 | |
| 2397 | while (q < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2398 | Py_UNICODE ch; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2399 | /* remaining bytes at the end? (size should be even) */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2400 | if (e-q<2) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2401 | if (consumed) |
| 2402 | break; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2403 | errmsg = "truncated data"; |
| 2404 | startinpos = ((const char *)q)-starts; |
| 2405 | endinpos = ((const char *)e)-starts; |
| 2406 | goto utf16Error; |
| 2407 | /* The remaining input chars are ignored if the callback |
| 2408 | chooses to skip the input */ |
| 2409 | } |
| 2410 | ch = (q[ihi] << 8) | q[ilo]; |
| 2411 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2412 | q += 2; |
| 2413 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2414 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 2415 | *p++ = ch; |
| 2416 | continue; |
| 2417 | } |
| 2418 | |
| 2419 | /* UTF-16 code pair: */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2420 | if (q >= e) { |
| 2421 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2422 | startinpos = (((const char *)q)-2)-starts; |
| 2423 | endinpos = ((const char *)e)-starts; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2424 | goto utf16Error; |
| 2425 | } |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2426 | if (0xD800 <= ch && ch <= 0xDBFF) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2427 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 2428 | q += 2; |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2429 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2430 | #ifndef Py_UNICODE_WIDE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2431 | *p++ = ch; |
| 2432 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2433 | #else |
| 2434 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2435 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2436 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2437 | } |
| 2438 | else { |
| 2439 | errmsg = "illegal UTF-16 surrogate"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2440 | startinpos = (((const char *)q)-4)-starts; |
| 2441 | endinpos = startinpos+2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2442 | goto utf16Error; |
| 2443 | } |
| 2444 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2445 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2446 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2447 | startinpos = (((const char *)q)-2)-starts; |
| 2448 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2449 | /* Fall through to report the error */ |
| 2450 | |
| 2451 | utf16Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2452 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2453 | if (unicode_decode_call_errorhandler( |
| 2454 | errors, &errorHandler, |
| 2455 | "utf16", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2456 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2457 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2458 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2459 | } |
| 2460 | |
| 2461 | if (byteorder) |
| 2462 | *byteorder = bo; |
| 2463 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2464 | if (consumed) |
| 2465 | *consumed = (const char *)q-starts; |
| 2466 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2467 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2468 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2469 | goto onError; |
| 2470 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2471 | Py_XDECREF(errorHandler); |
| 2472 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2473 | return (PyObject *)unicode; |
| 2474 | |
| 2475 | onError: |
| 2476 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2477 | Py_XDECREF(errorHandler); |
| 2478 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2479 | return NULL; |
| 2480 | } |
| 2481 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2482 | PyObject * |
| 2483 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2484 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2485 | const char *errors, |
| 2486 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2487 | { |
| 2488 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2489 | unsigned char *p; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2490 | #ifdef Py_UNICODE_WIDE |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2491 | int i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2492 | #else |
| 2493 | const int pairs = 0; |
| 2494 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2495 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2496 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2497 | int ihi = 1, ilo = 0; |
| 2498 | #else |
| 2499 | int ihi = 0, ilo = 1; |
| 2500 | #endif |
| 2501 | |
| 2502 | #define STORECHAR(CH) \ |
| 2503 | do { \ |
| 2504 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 2505 | p[ilo] = (CH) & 0xff; \ |
| 2506 | p += 2; \ |
| 2507 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2508 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2509 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2510 | for (i = pairs = 0; i < size; i++) |
| 2511 | if (s[i] >= 0x10000) |
| 2512 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2513 | #endif |
Walter Dörwald | 3cc3452 | 2007-05-04 10:48:27 +0000 | [diff] [blame] | 2514 | v = PyBytes_FromStringAndSize(NULL, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2515 | 2 * (size + pairs + (byteorder == 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2516 | if (v == NULL) |
| 2517 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2518 | |
Walter Dörwald | 3cc3452 | 2007-05-04 10:48:27 +0000 | [diff] [blame] | 2519 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2520 | if (byteorder == 0) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2521 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2522 | if (size == 0) |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2523 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2524 | |
| 2525 | if (byteorder == -1) { |
| 2526 | /* force LE */ |
| 2527 | ihi = 1; |
| 2528 | ilo = 0; |
| 2529 | } |
| 2530 | else if (byteorder == 1) { |
| 2531 | /* force BE */ |
| 2532 | ihi = 0; |
| 2533 | ilo = 1; |
| 2534 | } |
| 2535 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2536 | while (size-- > 0) { |
| 2537 | Py_UNICODE ch = *s++; |
| 2538 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2539 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2540 | if (ch >= 0x10000) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2541 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2542 | ch = 0xD800 | ((ch-0x10000) >> 10); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2543 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2544 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2545 | STORECHAR(ch); |
| 2546 | if (ch2) |
| 2547 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2548 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2549 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2550 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2551 | } |
| 2552 | |
| 2553 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 2554 | { |
| 2555 | if (!PyUnicode_Check(unicode)) { |
| 2556 | PyErr_BadArgument(); |
| 2557 | return NULL; |
| 2558 | } |
| 2559 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 2560 | PyUnicode_GET_SIZE(unicode), |
| 2561 | NULL, |
| 2562 | 0); |
| 2563 | } |
| 2564 | |
| 2565 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 2566 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2567 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 2568 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2569 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2570 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2571 | const char *errors) |
| 2572 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2573 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2574 | Py_ssize_t startinpos; |
| 2575 | Py_ssize_t endinpos; |
| 2576 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2577 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2578 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2579 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2580 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2581 | char* message; |
| 2582 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2583 | PyObject *errorHandler = NULL; |
| 2584 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2585 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2586 | /* Escaped strings will always be longer than the resulting |
| 2587 | 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] | 2588 | length after conversion to the true value. |
| 2589 | (but if the error callback returns a long replacement string |
| 2590 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2591 | v = _PyUnicode_New(size); |
| 2592 | if (v == NULL) |
| 2593 | goto onError; |
| 2594 | if (size == 0) |
| 2595 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2596 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2597 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2598 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2599 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2600 | while (s < end) { |
| 2601 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2602 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2603 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2604 | |
| 2605 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2606 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2607 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2608 | continue; |
| 2609 | } |
| 2610 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2611 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2612 | /* \ - Escapes */ |
| 2613 | s++; |
| 2614 | switch (*s++) { |
| 2615 | |
| 2616 | /* \x escapes */ |
| 2617 | case '\n': break; |
| 2618 | case '\\': *p++ = '\\'; break; |
| 2619 | case '\'': *p++ = '\''; break; |
| 2620 | case '\"': *p++ = '\"'; break; |
| 2621 | case 'b': *p++ = '\b'; break; |
| 2622 | case 'f': *p++ = '\014'; break; /* FF */ |
| 2623 | case 't': *p++ = '\t'; break; |
| 2624 | case 'n': *p++ = '\n'; break; |
| 2625 | case 'r': *p++ = '\r'; break; |
| 2626 | case 'v': *p++ = '\013'; break; /* VT */ |
| 2627 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 2628 | |
| 2629 | /* \OOO (octal) escapes */ |
| 2630 | case '0': case '1': case '2': case '3': |
| 2631 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2632 | x = s[-1] - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2633 | if ('0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2634 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2635 | if ('0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2636 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2637 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2638 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2639 | break; |
| 2640 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2641 | /* hex escapes */ |
| 2642 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2643 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2644 | digits = 2; |
| 2645 | message = "truncated \\xXX escape"; |
| 2646 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2647 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2648 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2649 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2650 | digits = 4; |
| 2651 | message = "truncated \\uXXXX escape"; |
| 2652 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2653 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2654 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2655 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2656 | digits = 8; |
| 2657 | message = "truncated \\UXXXXXXXX escape"; |
| 2658 | hexescape: |
| 2659 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2660 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2661 | if (s+digits>end) { |
| 2662 | endinpos = size; |
| 2663 | if (unicode_decode_call_errorhandler( |
| 2664 | errors, &errorHandler, |
| 2665 | "unicodeescape", "end of string in escape sequence", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2666 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2667 | (PyObject **)&v, &outpos, &p)) |
| 2668 | goto onError; |
| 2669 | goto nextByte; |
| 2670 | } |
| 2671 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2672 | c = (unsigned char) s[i]; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2673 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2674 | endinpos = (s+i+1)-starts; |
| 2675 | if (unicode_decode_call_errorhandler( |
| 2676 | errors, &errorHandler, |
| 2677 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2678 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2679 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2680 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2681 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2682 | } |
| 2683 | chr = (chr<<4) & ~0xF; |
| 2684 | if (c >= '0' && c <= '9') |
| 2685 | chr += c - '0'; |
| 2686 | else if (c >= 'a' && c <= 'f') |
| 2687 | chr += 10 + c - 'a'; |
| 2688 | else |
| 2689 | chr += 10 + c - 'A'; |
| 2690 | } |
| 2691 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 2692 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2693 | /* _decoding_error will have already written into the |
| 2694 | target buffer. */ |
| 2695 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2696 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2697 | /* when we get here, chr is a 32-bit unicode character */ |
| 2698 | if (chr <= 0xffff) |
| 2699 | /* UCS-2 character */ |
| 2700 | *p++ = (Py_UNICODE) chr; |
| 2701 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2702 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2703 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2704 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2705 | *p++ = chr; |
| 2706 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2707 | chr -= 0x10000L; |
| 2708 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2709 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2710 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2711 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2712 | endinpos = s-starts; |
| 2713 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2714 | if (unicode_decode_call_errorhandler( |
| 2715 | errors, &errorHandler, |
| 2716 | "unicodeescape", "illegal Unicode character", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2717 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2718 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2719 | goto onError; |
| 2720 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2721 | break; |
| 2722 | |
| 2723 | /* \N{name} */ |
| 2724 | case 'N': |
| 2725 | message = "malformed \\N character escape"; |
| 2726 | if (ucnhash_CAPI == NULL) { |
| 2727 | /* load the unicode data module */ |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2728 | PyObject *m, *api; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2729 | m = PyImport_ImportModule("unicodedata"); |
| 2730 | if (m == NULL) |
| 2731 | goto ucnhashError; |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2732 | api = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2733 | Py_DECREF(m); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2734 | if (api == NULL) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2735 | goto ucnhashError; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2736 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2737 | Py_DECREF(api); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2738 | if (ucnhash_CAPI == NULL) |
| 2739 | goto ucnhashError; |
| 2740 | } |
| 2741 | if (*s == '{') { |
| 2742 | const char *start = s+1; |
| 2743 | /* look for the closing brace */ |
| 2744 | while (*s != '}' && s < end) |
| 2745 | s++; |
| 2746 | if (s > start && s < end && *s == '}') { |
| 2747 | /* found a name. look it up in the unicode database */ |
| 2748 | message = "unknown Unicode character name"; |
| 2749 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 2750 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2751 | goto store; |
| 2752 | } |
| 2753 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2754 | endinpos = s-starts; |
| 2755 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2756 | if (unicode_decode_call_errorhandler( |
| 2757 | errors, &errorHandler, |
| 2758 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2759 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2760 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2761 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2762 | break; |
| 2763 | |
| 2764 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2765 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2766 | message = "\\ at end of string"; |
| 2767 | s--; |
| 2768 | endinpos = s-starts; |
| 2769 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2770 | if (unicode_decode_call_errorhandler( |
| 2771 | errors, &errorHandler, |
| 2772 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2773 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2774 | (PyObject **)&v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2775 | goto onError; |
| 2776 | } |
| 2777 | else { |
| 2778 | *p++ = '\\'; |
| 2779 | *p++ = (unsigned char)s[-1]; |
| 2780 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2781 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2782 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2783 | nextByte: |
| 2784 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2785 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2786 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2787 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 2788 | Py_XDECREF(errorHandler); |
| 2789 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2790 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2791 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2792 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2793 | PyErr_SetString( |
| 2794 | PyExc_UnicodeError, |
| 2795 | "\\N escapes not supported (can't load unicodedata module)" |
| 2796 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2797 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2798 | Py_XDECREF(errorHandler); |
| 2799 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 2800 | return NULL; |
| 2801 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2802 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2803 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2804 | Py_XDECREF(errorHandler); |
| 2805 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2806 | return NULL; |
| 2807 | } |
| 2808 | |
| 2809 | /* Return a Unicode-Escape string version of the Unicode object. |
| 2810 | |
| 2811 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 2812 | appropriate. |
| 2813 | |
| 2814 | */ |
| 2815 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2816 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
| 2817 | Py_ssize_t size, |
| 2818 | Py_UNICODE ch) |
| 2819 | { |
| 2820 | /* like wcschr, but doesn't stop at NULL characters */ |
| 2821 | |
| 2822 | while (size-- > 0) { |
| 2823 | if (*s == ch) |
| 2824 | return s; |
| 2825 | s++; |
| 2826 | } |
| 2827 | |
| 2828 | return NULL; |
| 2829 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 2830 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2831 | static const char *hexdigits = "0123456789abcdef"; |
| 2832 | |
| 2833 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 2834 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2835 | { |
| 2836 | PyObject *repr; |
| 2837 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2838 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2839 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 2840 | better to choose a different scheme. Perhaps scan the |
| 2841 | first N-chars of the string and allocate based on that size. |
| 2842 | */ |
| 2843 | /* Initial allocation is based on the longest-possible unichr |
| 2844 | escape. |
| 2845 | |
| 2846 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 2847 | unichr, so in this case it's the longest unichr escape. In |
| 2848 | narrow (UTF-16) builds this is five chars per source unichr |
| 2849 | since there are two unichrs in the surrogate pair, so in narrow |
| 2850 | (UTF-16) builds it's not the longest unichr escape. |
| 2851 | |
| 2852 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 2853 | so in the narrow (UTF-16) build case it's the longest unichr |
| 2854 | escape. |
| 2855 | */ |
| 2856 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2857 | repr = PyBytes_FromStringAndSize(NULL, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2858 | #ifdef Py_UNICODE_WIDE |
| 2859 | + 10*size |
| 2860 | #else |
| 2861 | + 6*size |
| 2862 | #endif |
| 2863 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2864 | if (repr == NULL) |
| 2865 | return NULL; |
| 2866 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2867 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2868 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2869 | while (size-- > 0) { |
| 2870 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2871 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2872 | /* Escape backslashes */ |
| 2873 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2874 | *p++ = '\\'; |
| 2875 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2876 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2877 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2878 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 2879 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2880 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 2881 | else if (ch >= 0x10000) { |
| 2882 | *p++ = '\\'; |
| 2883 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2884 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 2885 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 2886 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 2887 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 2888 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 2889 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 2890 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 2891 | *p++ = hexdigits[ch & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2892 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2893 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2894 | #else |
| 2895 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2896 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 2897 | Py_UNICODE ch2; |
| 2898 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2899 | |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2900 | ch2 = *s++; |
| 2901 | size--; |
| 2902 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 2903 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 2904 | *p++ = '\\'; |
| 2905 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2906 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 2907 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 2908 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 2909 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 2910 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 2911 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 2912 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 2913 | *p++ = hexdigits[ucs & 0x0000000F]; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2914 | continue; |
| 2915 | } |
| 2916 | /* Fall through: isolated surrogates are copied as-is */ |
| 2917 | s--; |
| 2918 | size++; |
| 2919 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2920 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2921 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2922 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2923 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2924 | *p++ = '\\'; |
| 2925 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2926 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 2927 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 2928 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 2929 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2930 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2931 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2932 | /* Map special whitespace to '\t', \n', '\r' */ |
| 2933 | else if (ch == '\t') { |
| 2934 | *p++ = '\\'; |
| 2935 | *p++ = 't'; |
| 2936 | } |
| 2937 | else if (ch == '\n') { |
| 2938 | *p++ = '\\'; |
| 2939 | *p++ = 'n'; |
| 2940 | } |
| 2941 | else if (ch == '\r') { |
| 2942 | *p++ = '\\'; |
| 2943 | *p++ = 'r'; |
| 2944 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2945 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2946 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 2947 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2948 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2949 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2950 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 2951 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2952 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2953 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2954 | /* Copy everything else as-is */ |
| 2955 | else |
| 2956 | *p++ = (char) ch; |
| 2957 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2958 | |
| 2959 | *p = '\0'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2960 | if (PyBytes_Resize(repr, p - PyBytes_AS_STRING(repr))) { |
| 2961 | Py_DECREF(repr); |
| 2962 | return NULL; |
| 2963 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2964 | return repr; |
| 2965 | } |
| 2966 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2967 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 2968 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2969 | PyObject *s, *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2970 | if (!PyUnicode_Check(unicode)) { |
| 2971 | PyErr_BadArgument(); |
| 2972 | return NULL; |
| 2973 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2974 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2975 | PyUnicode_GET_SIZE(unicode)); |
| 2976 | |
| 2977 | if (!s) |
| 2978 | return NULL; |
| 2979 | result = PyString_FromStringAndSize(PyBytes_AS_STRING(s), |
| 2980 | PyBytes_GET_SIZE(s)); |
| 2981 | Py_DECREF(s); |
| 2982 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2983 | } |
| 2984 | |
| 2985 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 2986 | |
| 2987 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2988 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2989 | const char *errors) |
| 2990 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2991 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2992 | Py_ssize_t startinpos; |
| 2993 | Py_ssize_t endinpos; |
| 2994 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2995 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2996 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2997 | const char *end; |
| 2998 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2999 | PyObject *errorHandler = NULL; |
| 3000 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3001 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3002 | /* Escaped strings will always be longer than the resulting |
| 3003 | 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] | 3004 | length after conversion to the true value. (But decoding error |
| 3005 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3006 | v = _PyUnicode_New(size); |
| 3007 | if (v == NULL) |
| 3008 | goto onError; |
| 3009 | if (size == 0) |
| 3010 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3011 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3012 | end = s + size; |
| 3013 | while (s < end) { |
| 3014 | unsigned char c; |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 3015 | Py_UCS4 x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3016 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3017 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3018 | |
| 3019 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3020 | if (*s != '\\') { |
| 3021 | *p++ = (unsigned char)*s++; |
| 3022 | continue; |
| 3023 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3024 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3025 | |
| 3026 | /* \u-escapes are only interpreted iff the number of leading |
| 3027 | backslashes if odd */ |
| 3028 | bs = s; |
| 3029 | for (;s < end;) { |
| 3030 | if (*s != '\\') |
| 3031 | break; |
| 3032 | *p++ = (unsigned char)*s++; |
| 3033 | } |
| 3034 | if (((s - bs) & 1) == 0 || |
| 3035 | s >= end || |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3036 | (*s != 'u' && *s != 'U')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3037 | continue; |
| 3038 | } |
| 3039 | p--; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3040 | count = *s=='u' ? 4 : 8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3041 | s++; |
| 3042 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3043 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3044 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3045 | for (x = 0, i = 0; i < count; ++i, ++s) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3046 | c = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3047 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3048 | endinpos = s-starts; |
| 3049 | if (unicode_decode_call_errorhandler( |
| 3050 | errors, &errorHandler, |
| 3051 | "rawunicodeescape", "truncated \\uXXXX", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3052 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3053 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3054 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3055 | goto nextByte; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3056 | } |
| 3057 | x = (x<<4) & ~0xF; |
| 3058 | if (c >= '0' && c <= '9') |
| 3059 | x += c - '0'; |
| 3060 | else if (c >= 'a' && c <= 'f') |
| 3061 | x += 10 + c - 'a'; |
| 3062 | else |
| 3063 | x += 10 + c - 'A'; |
| 3064 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3065 | #ifndef Py_UNICODE_WIDE |
| 3066 | if (x > 0x10000) { |
| 3067 | if (unicode_decode_call_errorhandler( |
| 3068 | errors, &errorHandler, |
| 3069 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3070 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3071 | (PyObject **)&v, &outpos, &p)) |
| 3072 | goto onError; |
| 3073 | } |
| 3074 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3075 | *p++ = x; |
| 3076 | nextByte: |
| 3077 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3078 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3079 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3080 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3081 | Py_XDECREF(errorHandler); |
| 3082 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3083 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3084 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3085 | onError: |
| 3086 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3087 | Py_XDECREF(errorHandler); |
| 3088 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3089 | return NULL; |
| 3090 | } |
| 3091 | |
| 3092 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3093 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3094 | { |
| 3095 | PyObject *repr; |
| 3096 | char *p; |
| 3097 | char *q; |
| 3098 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3099 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3100 | repr = PyBytes_FromStringAndSize(NULL, 10 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3101 | #else |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3102 | repr = PyBytes_FromStringAndSize(NULL, 6 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3103 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3104 | if (repr == NULL) |
| 3105 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3106 | if (size == 0) |
| 3107 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3108 | |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3109 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3110 | while (size-- > 0) { |
| 3111 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3112 | #ifdef Py_UNICODE_WIDE |
| 3113 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 3114 | if (ch >= 0x10000) { |
| 3115 | *p++ = '\\'; |
| 3116 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3117 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 3118 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 3119 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 3120 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 3121 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3122 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3123 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3124 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3125 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3126 | else |
| 3127 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3128 | /* Map 16-bit characters to '\uxxxx' */ |
| 3129 | if (ch >= 256) { |
| 3130 | *p++ = '\\'; |
| 3131 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3132 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3133 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3134 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3135 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3136 | } |
| 3137 | /* Copy everything else as-is */ |
| 3138 | else |
| 3139 | *p++ = (char) ch; |
| 3140 | } |
| 3141 | *p = '\0'; |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3142 | if (PyBytes_Resize(repr, p - q)) { |
| 3143 | Py_DECREF(repr); |
| 3144 | return NULL; |
| 3145 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3146 | return repr; |
| 3147 | } |
| 3148 | |
| 3149 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 3150 | { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3151 | PyObject *s, *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3152 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3153 | PyErr_BadArgument(); |
| 3154 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3155 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3156 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3157 | PyUnicode_GET_SIZE(unicode)); |
| 3158 | |
| 3159 | if (!s) |
| 3160 | return NULL; |
| 3161 | result = PyString_FromStringAndSize(PyBytes_AS_STRING(s), |
| 3162 | PyBytes_GET_SIZE(s)); |
| 3163 | Py_DECREF(s); |
| 3164 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3165 | } |
| 3166 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3167 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 3168 | |
| 3169 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3170 | Py_ssize_t size, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3171 | const char *errors) |
| 3172 | { |
| 3173 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3174 | Py_ssize_t startinpos; |
| 3175 | Py_ssize_t endinpos; |
| 3176 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3177 | PyUnicodeObject *v; |
| 3178 | Py_UNICODE *p; |
| 3179 | const char *end; |
| 3180 | const char *reason; |
| 3181 | PyObject *errorHandler = NULL; |
| 3182 | PyObject *exc = NULL; |
| 3183 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 3184 | #ifdef Py_UNICODE_WIDE |
| 3185 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 3186 | #endif |
| 3187 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3188 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3189 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 3190 | if (v == NULL) |
| 3191 | goto onError; |
| 3192 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
| 3193 | return (PyObject *)v; |
| 3194 | p = PyUnicode_AS_UNICODE(v); |
| 3195 | end = s + size; |
| 3196 | |
| 3197 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3198 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3199 | /* We have to sanity check the raw data, otherwise doom looms for |
| 3200 | some malformed UCS-4 data. */ |
| 3201 | if ( |
| 3202 | #ifdef Py_UNICODE_WIDE |
| 3203 | *p > unimax || *p < 0 || |
| 3204 | #endif |
| 3205 | end-s < Py_UNICODE_SIZE |
| 3206 | ) |
| 3207 | { |
| 3208 | startinpos = s - starts; |
| 3209 | if (end-s < Py_UNICODE_SIZE) { |
| 3210 | endinpos = end-starts; |
| 3211 | reason = "truncated input"; |
| 3212 | } |
| 3213 | else { |
| 3214 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 3215 | reason = "illegal code point (> 0x10FFFF)"; |
| 3216 | } |
| 3217 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 3218 | if (unicode_decode_call_errorhandler( |
| 3219 | errors, &errorHandler, |
| 3220 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3221 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3222 | (PyObject **)&v, &outpos, &p)) { |
| 3223 | goto onError; |
| 3224 | } |
| 3225 | } |
| 3226 | else { |
| 3227 | p++; |
| 3228 | s += Py_UNICODE_SIZE; |
| 3229 | } |
| 3230 | } |
| 3231 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3232 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3233 | goto onError; |
| 3234 | Py_XDECREF(errorHandler); |
| 3235 | Py_XDECREF(exc); |
| 3236 | return (PyObject *)v; |
| 3237 | |
| 3238 | onError: |
| 3239 | Py_XDECREF(v); |
| 3240 | Py_XDECREF(errorHandler); |
| 3241 | Py_XDECREF(exc); |
| 3242 | return NULL; |
| 3243 | } |
| 3244 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3245 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 3246 | |
| 3247 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3248 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3249 | const char *errors) |
| 3250 | { |
| 3251 | PyUnicodeObject *v; |
| 3252 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3253 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3254 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3255 | if (size == 1) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3256 | Py_UNICODE r = *(unsigned char*)s; |
| 3257 | return PyUnicode_FromUnicode(&r, 1); |
| 3258 | } |
| 3259 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3260 | v = _PyUnicode_New(size); |
| 3261 | if (v == NULL) |
| 3262 | goto onError; |
| 3263 | if (size == 0) |
| 3264 | return (PyObject *)v; |
| 3265 | p = PyUnicode_AS_UNICODE(v); |
| 3266 | while (size-- > 0) |
| 3267 | *p++ = (unsigned char)*s++; |
| 3268 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3269 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3270 | onError: |
| 3271 | Py_XDECREF(v); |
| 3272 | return NULL; |
| 3273 | } |
| 3274 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3275 | /* create or adjust a UnicodeEncodeError */ |
| 3276 | static void make_encode_exception(PyObject **exceptionObject, |
| 3277 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3278 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3279 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3280 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3281 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3282 | if (*exceptionObject == NULL) { |
| 3283 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 3284 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3285 | } |
| 3286 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3287 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 3288 | goto onError; |
| 3289 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 3290 | goto onError; |
| 3291 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 3292 | goto onError; |
| 3293 | return; |
| 3294 | onError: |
| 3295 | Py_DECREF(*exceptionObject); |
| 3296 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3297 | } |
| 3298 | } |
| 3299 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3300 | /* raises a UnicodeEncodeError */ |
| 3301 | static void raise_encode_exception(PyObject **exceptionObject, |
| 3302 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3303 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3304 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3305 | const char *reason) |
| 3306 | { |
| 3307 | make_encode_exception(exceptionObject, |
| 3308 | encoding, unicode, size, startpos, endpos, reason); |
| 3309 | if (*exceptionObject != NULL) |
| 3310 | PyCodec_StrictErrors(*exceptionObject); |
| 3311 | } |
| 3312 | |
| 3313 | /* error handling callback helper: |
| 3314 | build arguments, call the callback and check the arguments, |
| 3315 | put the result into newpos and return the replacement string, which |
| 3316 | has to be freed by the caller */ |
| 3317 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 3318 | PyObject **errorHandler, |
| 3319 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3320 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 3321 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3322 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3323 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3324 | 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] | 3325 | |
| 3326 | PyObject *restuple; |
| 3327 | PyObject *resunicode; |
| 3328 | |
| 3329 | if (*errorHandler == NULL) { |
| 3330 | *errorHandler = PyCodec_LookupError(errors); |
| 3331 | if (*errorHandler == NULL) |
| 3332 | return NULL; |
| 3333 | } |
| 3334 | |
| 3335 | make_encode_exception(exceptionObject, |
| 3336 | encoding, unicode, size, startpos, endpos, reason); |
| 3337 | if (*exceptionObject == NULL) |
| 3338 | return NULL; |
| 3339 | |
| 3340 | restuple = PyObject_CallFunctionObjArgs( |
| 3341 | *errorHandler, *exceptionObject, NULL); |
| 3342 | if (restuple == NULL) |
| 3343 | return NULL; |
| 3344 | if (!PyTuple_Check(restuple)) { |
| 3345 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 3346 | Py_DECREF(restuple); |
| 3347 | return NULL; |
| 3348 | } |
| 3349 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 3350 | &resunicode, newpos)) { |
| 3351 | Py_DECREF(restuple); |
| 3352 | return NULL; |
| 3353 | } |
| 3354 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3355 | *newpos = size+*newpos; |
| 3356 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 3357 | 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] | 3358 | Py_DECREF(restuple); |
| 3359 | return NULL; |
| 3360 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3361 | Py_INCREF(resunicode); |
| 3362 | Py_DECREF(restuple); |
| 3363 | return resunicode; |
| 3364 | } |
| 3365 | |
| 3366 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3367 | Py_ssize_t size, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3368 | const char *errors, |
| 3369 | int limit) |
| 3370 | { |
| 3371 | /* output object */ |
| 3372 | PyObject *res; |
| 3373 | /* pointers to the beginning and end+1 of input */ |
| 3374 | const Py_UNICODE *startp = p; |
| 3375 | const Py_UNICODE *endp = p + size; |
| 3376 | /* pointer to the beginning of the unencodable characters */ |
| 3377 | /* const Py_UNICODE *badp = NULL; */ |
| 3378 | /* pointer into the output */ |
| 3379 | char *str; |
| 3380 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3381 | Py_ssize_t respos = 0; |
| 3382 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3383 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 3384 | 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] | 3385 | PyObject *errorHandler = NULL; |
| 3386 | PyObject *exc = NULL; |
| 3387 | /* the following variable is used for caching string comparisons |
| 3388 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 3389 | int known_errorHandler = -1; |
| 3390 | |
| 3391 | /* allocate enough for a simple encoding without |
| 3392 | replacements, if we need more, we'll resize */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3393 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3394 | if (res == NULL) |
| 3395 | goto onError; |
| 3396 | if (size == 0) |
| 3397 | return res; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3398 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3399 | ressize = size; |
| 3400 | |
| 3401 | while (p<endp) { |
| 3402 | Py_UNICODE c = *p; |
| 3403 | |
| 3404 | /* can we encode this? */ |
| 3405 | if (c<limit) { |
| 3406 | /* no overflow check, because we know that the space is enough */ |
| 3407 | *str++ = (char)c; |
| 3408 | ++p; |
| 3409 | } |
| 3410 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3411 | Py_ssize_t unicodepos = p-startp; |
| 3412 | Py_ssize_t requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3413 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3414 | Py_ssize_t repsize; |
| 3415 | Py_ssize_t newpos; |
| 3416 | Py_ssize_t respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3417 | Py_UNICODE *uni2; |
| 3418 | /* startpos for collecting unencodable chars */ |
| 3419 | const Py_UNICODE *collstart = p; |
| 3420 | const Py_UNICODE *collend = p; |
| 3421 | /* find all unecodable characters */ |
| 3422 | while ((collend < endp) && ((*collend)>=limit)) |
| 3423 | ++collend; |
| 3424 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 3425 | if (known_errorHandler==-1) { |
| 3426 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3427 | known_errorHandler = 1; |
| 3428 | else if (!strcmp(errors, "replace")) |
| 3429 | known_errorHandler = 2; |
| 3430 | else if (!strcmp(errors, "ignore")) |
| 3431 | known_errorHandler = 3; |
| 3432 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3433 | known_errorHandler = 4; |
| 3434 | else |
| 3435 | known_errorHandler = 0; |
| 3436 | } |
| 3437 | switch (known_errorHandler) { |
| 3438 | case 1: /* strict */ |
| 3439 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 3440 | goto onError; |
| 3441 | case 2: /* replace */ |
| 3442 | while (collstart++<collend) |
| 3443 | *str++ = '?'; /* fall through */ |
| 3444 | case 3: /* ignore */ |
| 3445 | p = collend; |
| 3446 | break; |
| 3447 | case 4: /* xmlcharrefreplace */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3448 | respos = str - PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3449 | /* determine replacement size (temporarily (mis)uses p) */ |
| 3450 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 3451 | if (*p<10) |
| 3452 | repsize += 2+1+1; |
| 3453 | else if (*p<100) |
| 3454 | repsize += 2+2+1; |
| 3455 | else if (*p<1000) |
| 3456 | repsize += 2+3+1; |
| 3457 | else if (*p<10000) |
| 3458 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 3459 | #ifndef Py_UNICODE_WIDE |
| 3460 | else |
| 3461 | repsize += 2+5+1; |
| 3462 | #else |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3463 | else if (*p<100000) |
| 3464 | repsize += 2+5+1; |
| 3465 | else if (*p<1000000) |
| 3466 | repsize += 2+6+1; |
| 3467 | else |
| 3468 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3469 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3470 | } |
| 3471 | requiredsize = respos+repsize+(endp-collend); |
| 3472 | if (requiredsize > ressize) { |
| 3473 | if (requiredsize<2*ressize) |
| 3474 | requiredsize = 2*ressize; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3475 | if (PyBytes_Resize(res, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3476 | goto onError; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3477 | str = PyBytes_AS_STRING(res) + respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3478 | ressize = requiredsize; |
| 3479 | } |
| 3480 | /* generate replacement (temporarily (mis)uses p) */ |
| 3481 | for (p = collstart; p < collend; ++p) { |
| 3482 | str += sprintf(str, "&#%d;", (int)*p); |
| 3483 | } |
| 3484 | p = collend; |
| 3485 | break; |
| 3486 | default: |
| 3487 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 3488 | encoding, reason, startp, size, &exc, |
| 3489 | collstart-startp, collend-startp, &newpos); |
| 3490 | if (repunicode == NULL) |
| 3491 | goto onError; |
| 3492 | /* need more space? (at least enough for what we |
| 3493 | have+the replacement+the rest of the string, so |
| 3494 | we won't have to check space for encodable characters) */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3495 | respos = str - PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3496 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3497 | requiredsize = respos+repsize+(endp-collend); |
| 3498 | if (requiredsize > ressize) { |
| 3499 | if (requiredsize<2*ressize) |
| 3500 | requiredsize = 2*ressize; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3501 | if (PyBytes_Resize(res, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3502 | Py_DECREF(repunicode); |
| 3503 | goto onError; |
| 3504 | } |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3505 | str = PyBytes_AS_STRING(res) + respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3506 | ressize = requiredsize; |
| 3507 | } |
| 3508 | /* check if there is anything unencodable in the replacement |
| 3509 | and copy it to the output */ |
| 3510 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 3511 | c = *uni2; |
| 3512 | if (c >= limit) { |
| 3513 | raise_encode_exception(&exc, encoding, startp, size, |
| 3514 | unicodepos, unicodepos+1, reason); |
| 3515 | Py_DECREF(repunicode); |
| 3516 | goto onError; |
| 3517 | } |
| 3518 | *str = (char)c; |
| 3519 | } |
| 3520 | p = startp + newpos; |
| 3521 | Py_DECREF(repunicode); |
| 3522 | } |
| 3523 | } |
| 3524 | } |
| 3525 | /* Resize if we allocated to much */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3526 | respos = str - PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3527 | if (respos<ressize) |
| 3528 | /* If this falls res will be NULL */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3529 | PyBytes_Resize(res, respos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3530 | Py_XDECREF(errorHandler); |
| 3531 | Py_XDECREF(exc); |
| 3532 | return res; |
| 3533 | |
| 3534 | onError: |
| 3535 | Py_XDECREF(res); |
| 3536 | Py_XDECREF(errorHandler); |
| 3537 | Py_XDECREF(exc); |
| 3538 | return NULL; |
| 3539 | } |
| 3540 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3541 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3542 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3543 | const char *errors) |
| 3544 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3545 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3546 | } |
| 3547 | |
| 3548 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 3549 | { |
| 3550 | if (!PyUnicode_Check(unicode)) { |
| 3551 | PyErr_BadArgument(); |
| 3552 | return NULL; |
| 3553 | } |
| 3554 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 3555 | PyUnicode_GET_SIZE(unicode), |
| 3556 | NULL); |
| 3557 | } |
| 3558 | |
| 3559 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 3560 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3561 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3562 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3563 | const char *errors) |
| 3564 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3565 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3566 | PyUnicodeObject *v; |
| 3567 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3568 | Py_ssize_t startinpos; |
| 3569 | Py_ssize_t endinpos; |
| 3570 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3571 | const char *e; |
| 3572 | PyObject *errorHandler = NULL; |
| 3573 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3574 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3575 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3576 | if (size == 1 && *(unsigned char*)s < 128) { |
| 3577 | Py_UNICODE r = *(unsigned char*)s; |
| 3578 | return PyUnicode_FromUnicode(&r, 1); |
| 3579 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3580 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3581 | v = _PyUnicode_New(size); |
| 3582 | if (v == NULL) |
| 3583 | goto onError; |
| 3584 | if (size == 0) |
| 3585 | return (PyObject *)v; |
| 3586 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3587 | e = s + size; |
| 3588 | while (s < e) { |
| 3589 | register unsigned char c = (unsigned char)*s; |
| 3590 | if (c < 128) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3591 | *p++ = c; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3592 | ++s; |
| 3593 | } |
| 3594 | else { |
| 3595 | startinpos = s-starts; |
| 3596 | endinpos = startinpos + 1; |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 3597 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3598 | if (unicode_decode_call_errorhandler( |
| 3599 | errors, &errorHandler, |
| 3600 | "ascii", "ordinal not in range(128)", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3601 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3602 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3603 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3604 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3605 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3606 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3607 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3608 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3609 | Py_XDECREF(errorHandler); |
| 3610 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3611 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3612 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3613 | onError: |
| 3614 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3615 | Py_XDECREF(errorHandler); |
| 3616 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3617 | return NULL; |
| 3618 | } |
| 3619 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3620 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3621 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3622 | const char *errors) |
| 3623 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3624 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3625 | } |
| 3626 | |
| 3627 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 3628 | { |
| 3629 | if (!PyUnicode_Check(unicode)) { |
| 3630 | PyErr_BadArgument(); |
| 3631 | return NULL; |
| 3632 | } |
| 3633 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 3634 | PyUnicode_GET_SIZE(unicode), |
| 3635 | NULL); |
| 3636 | } |
| 3637 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3638 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3639 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3640 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3641 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3642 | #if SIZEOF_INT < SIZEOF_SSIZE_T |
| 3643 | #define NEED_RETRY |
| 3644 | #endif |
| 3645 | |
| 3646 | /* XXX This code is limited to "true" double-byte encodings, as |
| 3647 | a) it assumes an incomplete character consists of a single byte, and |
| 3648 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
| 3649 | encodings, see IsDBCSLeadByteEx documentation. */ |
| 3650 | |
| 3651 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 3652 | { |
| 3653 | const char *curr = s + offset; |
| 3654 | |
| 3655 | if (IsDBCSLeadByte(*curr)) { |
| 3656 | const char *prev = CharPrev(s, curr); |
| 3657 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
| 3658 | } |
| 3659 | return 0; |
| 3660 | } |
| 3661 | |
| 3662 | /* |
| 3663 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 3664 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 3665 | */ |
| 3666 | static int decode_mbcs(PyUnicodeObject **v, |
| 3667 | const char *s, /* MBCS string */ |
| 3668 | int size, /* sizeof MBCS string */ |
| 3669 | int final) |
| 3670 | { |
| 3671 | Py_UNICODE *p; |
| 3672 | Py_ssize_t n = 0; |
| 3673 | int usize = 0; |
| 3674 | |
| 3675 | assert(size >= 0); |
| 3676 | |
| 3677 | /* Skip trailing lead-byte unless 'final' is set */ |
| 3678 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
| 3679 | --size; |
| 3680 | |
| 3681 | /* First get the size of the result */ |
| 3682 | if (size > 0) { |
| 3683 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 3684 | if (usize == 0) { |
| 3685 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3686 | return -1; |
| 3687 | } |
| 3688 | } |
| 3689 | |
| 3690 | if (*v == NULL) { |
| 3691 | /* Create unicode object */ |
| 3692 | *v = _PyUnicode_New(usize); |
| 3693 | if (*v == NULL) |
| 3694 | return -1; |
| 3695 | } |
| 3696 | else { |
| 3697 | /* Extend unicode object */ |
| 3698 | n = PyUnicode_GET_SIZE(*v); |
| 3699 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 3700 | return -1; |
| 3701 | } |
| 3702 | |
| 3703 | /* Do the conversion */ |
| 3704 | if (size > 0) { |
| 3705 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 3706 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 3707 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3708 | return -1; |
| 3709 | } |
| 3710 | } |
| 3711 | |
| 3712 | return size; |
| 3713 | } |
| 3714 | |
| 3715 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
| 3716 | Py_ssize_t size, |
| 3717 | const char *errors, |
| 3718 | Py_ssize_t *consumed) |
| 3719 | { |
| 3720 | PyUnicodeObject *v = NULL; |
| 3721 | int done; |
| 3722 | |
| 3723 | if (consumed) |
| 3724 | *consumed = 0; |
| 3725 | |
| 3726 | #ifdef NEED_RETRY |
| 3727 | retry: |
| 3728 | if (size > INT_MAX) |
| 3729 | done = decode_mbcs(&v, s, INT_MAX, 0); |
| 3730 | else |
| 3731 | #endif |
| 3732 | done = decode_mbcs(&v, s, (int)size, !consumed); |
| 3733 | |
| 3734 | if (done < 0) { |
| 3735 | Py_XDECREF(v); |
| 3736 | return NULL; |
| 3737 | } |
| 3738 | |
| 3739 | if (consumed) |
| 3740 | *consumed += done; |
| 3741 | |
| 3742 | #ifdef NEED_RETRY |
| 3743 | if (size > INT_MAX) { |
| 3744 | s += done; |
| 3745 | size -= done; |
| 3746 | goto retry; |
| 3747 | } |
| 3748 | #endif |
| 3749 | |
| 3750 | return (PyObject *)v; |
| 3751 | } |
| 3752 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3753 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3754 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3755 | const char *errors) |
| 3756 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3757 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 3758 | } |
| 3759 | |
| 3760 | /* |
| 3761 | * Convert unicode into string object (MBCS). |
| 3762 | * Returns 0 if succeed, -1 otherwise. |
| 3763 | */ |
| 3764 | static int encode_mbcs(PyObject **repr, |
| 3765 | const Py_UNICODE *p, /* unicode */ |
| 3766 | int size) /* size of unicode */ |
| 3767 | { |
| 3768 | int mbcssize = 0; |
| 3769 | Py_ssize_t n = 0; |
| 3770 | |
| 3771 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3772 | |
| 3773 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3774 | if (size > 0) { |
| 3775 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 3776 | if (mbcssize == 0) { |
| 3777 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3778 | return -1; |
| 3779 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3780 | } |
| 3781 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3782 | if (*repr == NULL) { |
| 3783 | /* Create string object */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3784 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3785 | if (*repr == NULL) |
| 3786 | return -1; |
| 3787 | } |
| 3788 | else { |
| 3789 | /* Extend string object */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3790 | n = PyBytes_Size(*repr); |
| 3791 | if (PyBytes_Resize(*repr, n + mbcssize) < 0) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3792 | return -1; |
| 3793 | } |
| 3794 | |
| 3795 | /* Do the conversion */ |
| 3796 | if (size > 0) { |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3797 | char *s = PyBytes_AS_STRING(*repr) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3798 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 3799 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3800 | return -1; |
| 3801 | } |
| 3802 | } |
| 3803 | |
| 3804 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3805 | } |
| 3806 | |
| 3807 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3808 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3809 | const char *errors) |
| 3810 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3811 | PyObject *repr = NULL; |
| 3812 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 3813 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3814 | #ifdef NEED_RETRY |
| 3815 | retry: |
| 3816 | if (size > INT_MAX) |
| 3817 | ret = encode_mbcs(&repr, p, INT_MAX); |
| 3818 | else |
| 3819 | #endif |
| 3820 | ret = encode_mbcs(&repr, p, (int)size); |
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 | if (ret < 0) { |
| 3823 | Py_XDECREF(repr); |
| 3824 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3825 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3826 | |
| 3827 | #ifdef NEED_RETRY |
| 3828 | if (size > INT_MAX) { |
| 3829 | p += INT_MAX; |
| 3830 | size -= INT_MAX; |
| 3831 | goto retry; |
| 3832 | } |
| 3833 | #endif |
| 3834 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3835 | return repr; |
| 3836 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3837 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3838 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 3839 | { |
| 3840 | if (!PyUnicode_Check(unicode)) { |
| 3841 | PyErr_BadArgument(); |
| 3842 | return NULL; |
| 3843 | } |
| 3844 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 3845 | PyUnicode_GET_SIZE(unicode), |
| 3846 | NULL); |
| 3847 | } |
| 3848 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3849 | #undef NEED_RETRY |
| 3850 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3851 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3852 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3853 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 3854 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3855 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3856 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3857 | PyObject *mapping, |
| 3858 | const char *errors) |
| 3859 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3860 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3861 | Py_ssize_t startinpos; |
| 3862 | Py_ssize_t endinpos; |
| 3863 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3864 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3865 | PyUnicodeObject *v; |
| 3866 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3867 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3868 | PyObject *errorHandler = NULL; |
| 3869 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3870 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3871 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3872 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3873 | /* Default to Latin-1 */ |
| 3874 | if (mapping == NULL) |
| 3875 | return PyUnicode_DecodeLatin1(s, size, errors); |
| 3876 | |
| 3877 | v = _PyUnicode_New(size); |
| 3878 | if (v == NULL) |
| 3879 | goto onError; |
| 3880 | if (size == 0) |
| 3881 | return (PyObject *)v; |
| 3882 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3883 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3884 | if (PyUnicode_CheckExact(mapping)) { |
| 3885 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 3886 | maplen = PyUnicode_GET_SIZE(mapping); |
| 3887 | while (s < e) { |
| 3888 | unsigned char ch = *s; |
| 3889 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3890 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3891 | if (ch < maplen) |
| 3892 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3893 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3894 | if (x == 0xfffe) { |
| 3895 | /* undefined mapping */ |
| 3896 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3897 | startinpos = s-starts; |
| 3898 | endinpos = startinpos+1; |
| 3899 | if (unicode_decode_call_errorhandler( |
| 3900 | errors, &errorHandler, |
| 3901 | "charmap", "character maps to <undefined>", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3902 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3903 | (PyObject **)&v, &outpos, &p)) { |
| 3904 | goto onError; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 3905 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3906 | continue; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 3907 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3908 | *p++ = x; |
| 3909 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3910 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3911 | } |
| 3912 | else { |
| 3913 | while (s < e) { |
| 3914 | unsigned char ch = *s; |
| 3915 | PyObject *w, *x; |
| 3916 | |
| 3917 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 3918 | w = PyInt_FromLong((long)ch); |
| 3919 | if (w == NULL) |
| 3920 | goto onError; |
| 3921 | x = PyObject_GetItem(mapping, w); |
| 3922 | Py_DECREF(w); |
| 3923 | if (x == NULL) { |
| 3924 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3925 | /* No mapping found means: mapping is undefined. */ |
| 3926 | PyErr_Clear(); |
| 3927 | x = Py_None; |
| 3928 | Py_INCREF(x); |
| 3929 | } else |
| 3930 | goto onError; |
| 3931 | } |
| 3932 | |
| 3933 | /* Apply mapping */ |
| 3934 | if (PyInt_Check(x)) { |
| 3935 | long value = PyInt_AS_LONG(x); |
| 3936 | if (value < 0 || value > 65535) { |
| 3937 | PyErr_SetString(PyExc_TypeError, |
| 3938 | "character mapping must be in range(65536)"); |
| 3939 | Py_DECREF(x); |
| 3940 | goto onError; |
| 3941 | } |
| 3942 | *p++ = (Py_UNICODE)value; |
| 3943 | } |
| 3944 | else if (x == Py_None) { |
| 3945 | /* undefined mapping */ |
| 3946 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3947 | startinpos = s-starts; |
| 3948 | endinpos = startinpos+1; |
| 3949 | if (unicode_decode_call_errorhandler( |
| 3950 | errors, &errorHandler, |
| 3951 | "charmap", "character maps to <undefined>", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3952 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3953 | (PyObject **)&v, &outpos, &p)) { |
| 3954 | Py_DECREF(x); |
| 3955 | goto onError; |
| 3956 | } |
Walter Dörwald | d4fff17 | 2005-11-28 22:15:56 +0000 | [diff] [blame] | 3957 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3958 | continue; |
| 3959 | } |
| 3960 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3961 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3962 | |
| 3963 | if (targetsize == 1) |
| 3964 | /* 1-1 mapping */ |
| 3965 | *p++ = *PyUnicode_AS_UNICODE(x); |
| 3966 | |
| 3967 | else if (targetsize > 1) { |
| 3968 | /* 1-n mapping */ |
| 3969 | if (targetsize > extrachars) { |
| 3970 | /* resize first */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3971 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 3972 | Py_ssize_t needed = (targetsize - extrachars) + \ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3973 | (targetsize << 2); |
| 3974 | extrachars += needed; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3975 | /* XXX overflow detection missing */ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3976 | if (_PyUnicode_Resize(&v, |
| 3977 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 3978 | Py_DECREF(x); |
| 3979 | goto onError; |
| 3980 | } |
| 3981 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 3982 | } |
| 3983 | Py_UNICODE_COPY(p, |
| 3984 | PyUnicode_AS_UNICODE(x), |
| 3985 | targetsize); |
| 3986 | p += targetsize; |
| 3987 | extrachars -= targetsize; |
| 3988 | } |
| 3989 | /* 1-0 mapping: skip the character */ |
| 3990 | } |
| 3991 | else { |
| 3992 | /* wrong return value */ |
| 3993 | PyErr_SetString(PyExc_TypeError, |
| 3994 | "character mapping must return integer, None or unicode"); |
| 3995 | Py_DECREF(x); |
| 3996 | goto onError; |
| 3997 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3998 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3999 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4000 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4001 | } |
| 4002 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4003 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4004 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4005 | Py_XDECREF(errorHandler); |
| 4006 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4007 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4008 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4009 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4010 | Py_XDECREF(errorHandler); |
| 4011 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4012 | Py_XDECREF(v); |
| 4013 | return NULL; |
| 4014 | } |
| 4015 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4016 | /* Charmap encoding: the lookup table */ |
| 4017 | |
| 4018 | struct encoding_map{ |
| 4019 | PyObject_HEAD |
| 4020 | unsigned char level1[32]; |
| 4021 | int count2, count3; |
| 4022 | unsigned char level23[1]; |
| 4023 | }; |
| 4024 | |
| 4025 | static PyObject* |
| 4026 | encoding_map_size(PyObject *obj, PyObject* args) |
| 4027 | { |
| 4028 | struct encoding_map *map = (struct encoding_map*)obj; |
| 4029 | return PyInt_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
| 4030 | 128*map->count3); |
| 4031 | } |
| 4032 | |
| 4033 | static PyMethodDef encoding_map_methods[] = { |
| 4034 | {"size", encoding_map_size, METH_NOARGS, |
| 4035 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 4036 | { 0 } |
| 4037 | }; |
| 4038 | |
| 4039 | static void |
| 4040 | encoding_map_dealloc(PyObject* o) |
| 4041 | { |
| 4042 | PyObject_FREE(o); |
| 4043 | } |
| 4044 | |
| 4045 | static PyTypeObject EncodingMapType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 4046 | PyVarObject_HEAD_INIT(NULL, 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4047 | "EncodingMap", /*tp_name*/ |
| 4048 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 4049 | 0, /*tp_itemsize*/ |
| 4050 | /* methods */ |
| 4051 | encoding_map_dealloc, /*tp_dealloc*/ |
| 4052 | 0, /*tp_print*/ |
| 4053 | 0, /*tp_getattr*/ |
| 4054 | 0, /*tp_setattr*/ |
| 4055 | 0, /*tp_compare*/ |
| 4056 | 0, /*tp_repr*/ |
| 4057 | 0, /*tp_as_number*/ |
| 4058 | 0, /*tp_as_sequence*/ |
| 4059 | 0, /*tp_as_mapping*/ |
| 4060 | 0, /*tp_hash*/ |
| 4061 | 0, /*tp_call*/ |
| 4062 | 0, /*tp_str*/ |
| 4063 | 0, /*tp_getattro*/ |
| 4064 | 0, /*tp_setattro*/ |
| 4065 | 0, /*tp_as_buffer*/ |
| 4066 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 4067 | 0, /*tp_doc*/ |
| 4068 | 0, /*tp_traverse*/ |
| 4069 | 0, /*tp_clear*/ |
| 4070 | 0, /*tp_richcompare*/ |
| 4071 | 0, /*tp_weaklistoffset*/ |
| 4072 | 0, /*tp_iter*/ |
| 4073 | 0, /*tp_iternext*/ |
| 4074 | encoding_map_methods, /*tp_methods*/ |
| 4075 | 0, /*tp_members*/ |
| 4076 | 0, /*tp_getset*/ |
| 4077 | 0, /*tp_base*/ |
| 4078 | 0, /*tp_dict*/ |
| 4079 | 0, /*tp_descr_get*/ |
| 4080 | 0, /*tp_descr_set*/ |
| 4081 | 0, /*tp_dictoffset*/ |
| 4082 | 0, /*tp_init*/ |
| 4083 | 0, /*tp_alloc*/ |
| 4084 | 0, /*tp_new*/ |
| 4085 | 0, /*tp_free*/ |
| 4086 | 0, /*tp_is_gc*/ |
| 4087 | }; |
| 4088 | |
| 4089 | PyObject* |
| 4090 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 4091 | { |
| 4092 | Py_UNICODE *decode; |
| 4093 | PyObject *result; |
| 4094 | struct encoding_map *mresult; |
| 4095 | int i; |
| 4096 | int need_dict = 0; |
| 4097 | unsigned char level1[32]; |
| 4098 | unsigned char level2[512]; |
| 4099 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 4100 | int count2 = 0, count3 = 0; |
| 4101 | |
| 4102 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 4103 | PyErr_BadArgument(); |
| 4104 | return NULL; |
| 4105 | } |
| 4106 | decode = PyUnicode_AS_UNICODE(string); |
| 4107 | memset(level1, 0xFF, sizeof level1); |
| 4108 | memset(level2, 0xFF, sizeof level2); |
| 4109 | |
| 4110 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 4111 | or if there are non-BMP characters, we need to use |
| 4112 | a mapping dictionary. */ |
| 4113 | if (decode[0] != 0) |
| 4114 | need_dict = 1; |
| 4115 | for (i = 1; i < 256; i++) { |
| 4116 | int l1, l2; |
| 4117 | if (decode[i] == 0 |
| 4118 | #ifdef Py_UNICODE_WIDE |
| 4119 | || decode[i] > 0xFFFF |
| 4120 | #endif |
| 4121 | ) { |
| 4122 | need_dict = 1; |
| 4123 | break; |
| 4124 | } |
| 4125 | if (decode[i] == 0xFFFE) |
| 4126 | /* unmapped character */ |
| 4127 | continue; |
| 4128 | l1 = decode[i] >> 11; |
| 4129 | l2 = decode[i] >> 7; |
| 4130 | if (level1[l1] == 0xFF) |
| 4131 | level1[l1] = count2++; |
| 4132 | if (level2[l2] == 0xFF) |
| 4133 | level2[l2] = count3++; |
| 4134 | } |
| 4135 | |
| 4136 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 4137 | need_dict = 1; |
| 4138 | |
| 4139 | if (need_dict) { |
| 4140 | PyObject *result = PyDict_New(); |
| 4141 | PyObject *key, *value; |
| 4142 | if (!result) |
| 4143 | return NULL; |
| 4144 | for (i = 0; i < 256; i++) { |
| 4145 | key = value = NULL; |
| 4146 | key = PyInt_FromLong(decode[i]); |
| 4147 | value = PyInt_FromLong(i); |
| 4148 | if (!key || !value) |
| 4149 | goto failed1; |
| 4150 | if (PyDict_SetItem(result, key, value) == -1) |
| 4151 | goto failed1; |
| 4152 | Py_DECREF(key); |
| 4153 | Py_DECREF(value); |
| 4154 | } |
| 4155 | return result; |
| 4156 | failed1: |
| 4157 | Py_XDECREF(key); |
| 4158 | Py_XDECREF(value); |
| 4159 | Py_DECREF(result); |
| 4160 | return NULL; |
| 4161 | } |
| 4162 | |
| 4163 | /* Create a three-level trie */ |
| 4164 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 4165 | 16*count2 + 128*count3 - 1); |
| 4166 | if (!result) |
| 4167 | return PyErr_NoMemory(); |
| 4168 | PyObject_Init(result, &EncodingMapType); |
| 4169 | mresult = (struct encoding_map*)result; |
| 4170 | mresult->count2 = count2; |
| 4171 | mresult->count3 = count3; |
| 4172 | mlevel1 = mresult->level1; |
| 4173 | mlevel2 = mresult->level23; |
| 4174 | mlevel3 = mresult->level23 + 16*count2; |
| 4175 | memcpy(mlevel1, level1, 32); |
| 4176 | memset(mlevel2, 0xFF, 16*count2); |
| 4177 | memset(mlevel3, 0, 128*count3); |
| 4178 | count3 = 0; |
| 4179 | for (i = 1; i < 256; i++) { |
| 4180 | int o1, o2, o3, i2, i3; |
| 4181 | if (decode[i] == 0xFFFE) |
| 4182 | /* unmapped character */ |
| 4183 | continue; |
| 4184 | o1 = decode[i]>>11; |
| 4185 | o2 = (decode[i]>>7) & 0xF; |
| 4186 | i2 = 16*mlevel1[o1] + o2; |
| 4187 | if (mlevel2[i2] == 0xFF) |
| 4188 | mlevel2[i2] = count3++; |
| 4189 | o3 = decode[i] & 0x7F; |
| 4190 | i3 = 128*mlevel2[i2] + o3; |
| 4191 | mlevel3[i3] = i; |
| 4192 | } |
| 4193 | return result; |
| 4194 | } |
| 4195 | |
| 4196 | static int |
| 4197 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 4198 | { |
| 4199 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 4200 | int l1 = c>>11; |
| 4201 | int l2 = (c>>7) & 0xF; |
| 4202 | int l3 = c & 0x7F; |
| 4203 | int i; |
| 4204 | |
| 4205 | #ifdef Py_UNICODE_WIDE |
| 4206 | if (c > 0xFFFF) { |
| 4207 | return -1; |
| 4208 | } |
| 4209 | #endif |
| 4210 | if (c == 0) |
| 4211 | return 0; |
| 4212 | /* level 1*/ |
| 4213 | i = map->level1[l1]; |
| 4214 | if (i == 0xFF) { |
| 4215 | return -1; |
| 4216 | } |
| 4217 | /* level 2*/ |
| 4218 | i = map->level23[16*i+l2]; |
| 4219 | if (i == 0xFF) { |
| 4220 | return -1; |
| 4221 | } |
| 4222 | /* level 3 */ |
| 4223 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 4224 | if (i == 0) { |
| 4225 | return -1; |
| 4226 | } |
| 4227 | return i; |
| 4228 | } |
| 4229 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4230 | /* Lookup the character ch in the mapping. If the character |
| 4231 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4232 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4233 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4234 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4235 | PyObject *w = PyInt_FromLong((long)c); |
| 4236 | PyObject *x; |
| 4237 | |
| 4238 | if (w == NULL) |
| 4239 | return NULL; |
| 4240 | x = PyObject_GetItem(mapping, w); |
| 4241 | Py_DECREF(w); |
| 4242 | if (x == NULL) { |
| 4243 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4244 | /* No mapping found means: mapping is undefined. */ |
| 4245 | PyErr_Clear(); |
| 4246 | x = Py_None; |
| 4247 | Py_INCREF(x); |
| 4248 | return x; |
| 4249 | } else |
| 4250 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4251 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 4252 | else if (x == Py_None) |
| 4253 | return x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4254 | else if (PyInt_Check(x)) { |
| 4255 | long value = PyInt_AS_LONG(x); |
| 4256 | if (value < 0 || value > 255) { |
| 4257 | PyErr_SetString(PyExc_TypeError, |
| 4258 | "character mapping must be in range(256)"); |
| 4259 | Py_DECREF(x); |
| 4260 | return NULL; |
| 4261 | } |
| 4262 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4263 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4264 | else if (PyString_Check(x)) |
| 4265 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4266 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4267 | /* wrong return value */ |
Walter Dörwald | 580ceed | 2007-05-09 10:39:19 +0000 | [diff] [blame] | 4268 | PyErr_Format(PyExc_TypeError, |
| 4269 | "character mapping must return integer, None or str8, not %.400s", |
| 4270 | x->ob_type->tp_name); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4271 | Py_DECREF(x); |
| 4272 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4273 | } |
| 4274 | } |
| 4275 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4276 | static int |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4277 | charmapencode_resize(PyObject *outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4278 | { |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4279 | Py_ssize_t outsize = PyBytes_GET_SIZE( outobj); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4280 | /* exponentially overallocate to minimize reallocations */ |
| 4281 | if (requiredsize < 2*outsize) |
| 4282 | requiredsize = 2*outsize; |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4283 | if (PyBytes_Resize(outobj, requiredsize)) { |
| 4284 | Py_DECREF(outobj); |
| 4285 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4286 | } |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4287 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4288 | } |
| 4289 | |
| 4290 | typedef enum charmapencode_result { |
| 4291 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
| 4292 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4293 | /* 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] | 4294 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4295 | space is available. Return a new reference to the object that |
| 4296 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 4297 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 4298 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4299 | static |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4300 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4301 | PyObject *outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4302 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4303 | PyObject *rep; |
| 4304 | char *outstart; |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4305 | Py_ssize_t outsize = PyBytes_GET_SIZE(outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4306 | |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 4307 | if (Py_Type(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4308 | int res = encoding_map_lookup(c, mapping); |
| 4309 | Py_ssize_t requiredsize = *outpos+1; |
| 4310 | if (res == -1) |
| 4311 | return enc_FAILED; |
| 4312 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4313 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4314 | return enc_EXCEPTION; |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4315 | outstart = PyBytes_AS_STRING(outobj); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4316 | outstart[(*outpos)++] = (char)res; |
| 4317 | return enc_SUCCESS; |
| 4318 | } |
| 4319 | |
| 4320 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4321 | if (rep==NULL) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4322 | return enc_EXCEPTION; |
| 4323 | else if (rep==Py_None) { |
| 4324 | Py_DECREF(rep); |
| 4325 | return enc_FAILED; |
| 4326 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4327 | if (PyInt_Check(rep)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4328 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4329 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4330 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4331 | Py_DECREF(rep); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4332 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4333 | } |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4334 | outstart = PyBytes_AS_STRING(outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4335 | outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); |
| 4336 | } |
| 4337 | else { |
| 4338 | const char *repchars = PyString_AS_STRING(rep); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4339 | Py_ssize_t repsize = PyString_GET_SIZE(rep); |
| 4340 | Py_ssize_t requiredsize = *outpos+repsize; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4341 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4342 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4343 | Py_DECREF(rep); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4344 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4345 | } |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4346 | outstart = PyBytes_AS_STRING(outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4347 | memcpy(outstart + *outpos, repchars, repsize); |
| 4348 | *outpos += repsize; |
| 4349 | } |
| 4350 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4351 | Py_DECREF(rep); |
| 4352 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4353 | } |
| 4354 | |
| 4355 | /* handle an error in PyUnicode_EncodeCharmap |
| 4356 | Return 0 on success, -1 on error */ |
| 4357 | static |
| 4358 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4359 | 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] | 4360 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4361 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4362 | PyObject *res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4363 | { |
| 4364 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4365 | Py_ssize_t repsize; |
| 4366 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4367 | Py_UNICODE *uni2; |
| 4368 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4369 | Py_ssize_t collstartpos = *inpos; |
| 4370 | Py_ssize_t collendpos = *inpos+1; |
| 4371 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4372 | char *encoding = "charmap"; |
| 4373 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4374 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4375 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4376 | /* find all unencodable characters */ |
| 4377 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4378 | PyObject *rep; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 4379 | if (Py_Type(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4380 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 4381 | if (res != -1) |
| 4382 | break; |
| 4383 | ++collendpos; |
| 4384 | continue; |
| 4385 | } |
| 4386 | |
| 4387 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 4388 | if (rep==NULL) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4389 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4390 | else if (rep!=Py_None) { |
| 4391 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4392 | break; |
| 4393 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4394 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4395 | ++collendpos; |
| 4396 | } |
| 4397 | /* cache callback name lookup |
| 4398 | * (if not done yet, i.e. it's the first error) */ |
| 4399 | if (*known_errorHandler==-1) { |
| 4400 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4401 | *known_errorHandler = 1; |
| 4402 | else if (!strcmp(errors, "replace")) |
| 4403 | *known_errorHandler = 2; |
| 4404 | else if (!strcmp(errors, "ignore")) |
| 4405 | *known_errorHandler = 3; |
| 4406 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4407 | *known_errorHandler = 4; |
| 4408 | else |
| 4409 | *known_errorHandler = 0; |
| 4410 | } |
| 4411 | switch (*known_errorHandler) { |
| 4412 | case 1: /* strict */ |
| 4413 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4414 | return -1; |
| 4415 | case 2: /* replace */ |
| 4416 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 4417 | x = charmapencode_output('?', mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4418 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4419 | return -1; |
| 4420 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4421 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4422 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4423 | return -1; |
| 4424 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4425 | } |
| 4426 | /* fall through */ |
| 4427 | case 3: /* ignore */ |
| 4428 | *inpos = collendpos; |
| 4429 | break; |
| 4430 | case 4: /* xmlcharrefreplace */ |
| 4431 | /* generate replacement (temporarily (mis)uses p) */ |
| 4432 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 4433 | char buffer[2+29+1+1]; |
| 4434 | char *cp; |
| 4435 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 4436 | for (cp = buffer; *cp; ++cp) { |
| 4437 | x = charmapencode_output(*cp, mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4438 | if (x==enc_EXCEPTION) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4439 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4440 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4441 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4442 | return -1; |
| 4443 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4444 | } |
| 4445 | } |
| 4446 | *inpos = collendpos; |
| 4447 | break; |
| 4448 | default: |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4449 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4450 | encoding, reason, p, size, exceptionObject, |
| 4451 | collstartpos, collendpos, &newpos); |
| 4452 | if (repunicode == NULL) |
| 4453 | return -1; |
| 4454 | /* generate replacement */ |
| 4455 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4456 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 4457 | x = charmapencode_output(*uni2, mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4458 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4459 | return -1; |
| 4460 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4461 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4462 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4463 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4464 | return -1; |
| 4465 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4466 | } |
| 4467 | *inpos = newpos; |
| 4468 | Py_DECREF(repunicode); |
| 4469 | } |
| 4470 | return 0; |
| 4471 | } |
| 4472 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4473 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4474 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4475 | PyObject *mapping, |
| 4476 | const char *errors) |
| 4477 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4478 | /* output object */ |
| 4479 | PyObject *res = NULL; |
| 4480 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4481 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4482 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4483 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4484 | PyObject *errorHandler = NULL; |
| 4485 | PyObject *exc = NULL; |
| 4486 | /* the following variable is used for caching string comparisons |
| 4487 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4488 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4489 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4490 | |
| 4491 | /* Default to Latin-1 */ |
| 4492 | if (mapping == NULL) |
| 4493 | return PyUnicode_EncodeLatin1(p, size, errors); |
| 4494 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4495 | /* allocate enough for a simple encoding without |
| 4496 | replacements, if we need more, we'll resize */ |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4497 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4498 | if (res == NULL) |
| 4499 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 4500 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4501 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4502 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4503 | while (inpos<size) { |
| 4504 | /* try to encode it */ |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4505 | charmapencode_result x = charmapencode_output(p[inpos], mapping, res, &respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4506 | if (x==enc_EXCEPTION) /* error */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4507 | goto onError; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4508 | if (x==enc_FAILED) { /* unencodable character */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4509 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 4510 | &exc, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4511 | &known_errorHandler, &errorHandler, errors, |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4512 | res, &respos)) { |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 4513 | goto onError; |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 4514 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4515 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4516 | else |
| 4517 | /* done with this character => adjust input position */ |
| 4518 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4519 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4520 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4521 | /* Resize if we allocated to much */ |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4522 | if (respos<PyBytes_GET_SIZE(res)) { |
| 4523 | if (PyBytes_Resize(res, respos)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4524 | goto onError; |
| 4525 | } |
| 4526 | Py_XDECREF(exc); |
| 4527 | Py_XDECREF(errorHandler); |
| 4528 | return res; |
| 4529 | |
| 4530 | onError: |
| 4531 | Py_XDECREF(res); |
| 4532 | Py_XDECREF(exc); |
| 4533 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4534 | return NULL; |
| 4535 | } |
| 4536 | |
| 4537 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 4538 | PyObject *mapping) |
| 4539 | { |
| 4540 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 4541 | PyErr_BadArgument(); |
| 4542 | return NULL; |
| 4543 | } |
| 4544 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 4545 | PyUnicode_GET_SIZE(unicode), |
| 4546 | mapping, |
| 4547 | NULL); |
| 4548 | } |
| 4549 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4550 | /* create or adjust a UnicodeTranslateError */ |
| 4551 | static void make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4552 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4553 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4554 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4555 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4556 | if (*exceptionObject == NULL) { |
| 4557 | *exceptionObject = PyUnicodeTranslateError_Create( |
| 4558 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4559 | } |
| 4560 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4561 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 4562 | goto onError; |
| 4563 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 4564 | goto onError; |
| 4565 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 4566 | goto onError; |
| 4567 | return; |
| 4568 | onError: |
| 4569 | Py_DECREF(*exceptionObject); |
| 4570 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4571 | } |
| 4572 | } |
| 4573 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4574 | /* raises a UnicodeTranslateError */ |
| 4575 | static void raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4576 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4577 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4578 | const char *reason) |
| 4579 | { |
| 4580 | make_translate_exception(exceptionObject, |
| 4581 | unicode, size, startpos, endpos, reason); |
| 4582 | if (*exceptionObject != NULL) |
| 4583 | PyCodec_StrictErrors(*exceptionObject); |
| 4584 | } |
| 4585 | |
| 4586 | /* error handling callback helper: |
| 4587 | build arguments, call the callback and check the arguments, |
| 4588 | put the result into newpos and return the replacement string, which |
| 4589 | has to be freed by the caller */ |
| 4590 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 4591 | PyObject **errorHandler, |
| 4592 | const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4593 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4594 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4595 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4596 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4597 | 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] | 4598 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4599 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4600 | PyObject *restuple; |
| 4601 | PyObject *resunicode; |
| 4602 | |
| 4603 | if (*errorHandler == NULL) { |
| 4604 | *errorHandler = PyCodec_LookupError(errors); |
| 4605 | if (*errorHandler == NULL) |
| 4606 | return NULL; |
| 4607 | } |
| 4608 | |
| 4609 | make_translate_exception(exceptionObject, |
| 4610 | unicode, size, startpos, endpos, reason); |
| 4611 | if (*exceptionObject == NULL) |
| 4612 | return NULL; |
| 4613 | |
| 4614 | restuple = PyObject_CallFunctionObjArgs( |
| 4615 | *errorHandler, *exceptionObject, NULL); |
| 4616 | if (restuple == NULL) |
| 4617 | return NULL; |
| 4618 | if (!PyTuple_Check(restuple)) { |
| 4619 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 4620 | Py_DECREF(restuple); |
| 4621 | return NULL; |
| 4622 | } |
| 4623 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4624 | &resunicode, &i_newpos)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4625 | Py_DECREF(restuple); |
| 4626 | return NULL; |
| 4627 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4628 | if (i_newpos<0) |
| 4629 | *newpos = size+i_newpos; |
| 4630 | else |
| 4631 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4632 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 4633 | 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] | 4634 | Py_DECREF(restuple); |
| 4635 | return NULL; |
| 4636 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4637 | Py_INCREF(resunicode); |
| 4638 | Py_DECREF(restuple); |
| 4639 | return resunicode; |
| 4640 | } |
| 4641 | |
| 4642 | /* Lookup the character ch in the mapping and put the result in result, |
| 4643 | which must be decrefed by the caller. |
| 4644 | Return 0 on success, -1 on error */ |
| 4645 | static |
| 4646 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 4647 | { |
| 4648 | PyObject *w = PyInt_FromLong((long)c); |
| 4649 | PyObject *x; |
| 4650 | |
| 4651 | if (w == NULL) |
| 4652 | return -1; |
| 4653 | x = PyObject_GetItem(mapping, w); |
| 4654 | Py_DECREF(w); |
| 4655 | if (x == NULL) { |
| 4656 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4657 | /* No mapping found means: use 1:1 mapping. */ |
| 4658 | PyErr_Clear(); |
| 4659 | *result = NULL; |
| 4660 | return 0; |
| 4661 | } else |
| 4662 | return -1; |
| 4663 | } |
| 4664 | else if (x == Py_None) { |
| 4665 | *result = x; |
| 4666 | return 0; |
| 4667 | } |
| 4668 | else if (PyInt_Check(x)) { |
| 4669 | long value = PyInt_AS_LONG(x); |
| 4670 | long max = PyUnicode_GetMax(); |
| 4671 | if (value < 0 || value > max) { |
| 4672 | PyErr_Format(PyExc_TypeError, |
| 4673 | "character mapping must be in range(0x%lx)", max+1); |
| 4674 | Py_DECREF(x); |
| 4675 | return -1; |
| 4676 | } |
| 4677 | *result = x; |
| 4678 | return 0; |
| 4679 | } |
| 4680 | else if (PyUnicode_Check(x)) { |
| 4681 | *result = x; |
| 4682 | return 0; |
| 4683 | } |
| 4684 | else { |
| 4685 | /* wrong return value */ |
| 4686 | PyErr_SetString(PyExc_TypeError, |
| 4687 | "character mapping must return integer, None or unicode"); |
Walter Dörwald | 150523e | 2003-08-15 16:52:19 +0000 | [diff] [blame] | 4688 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4689 | return -1; |
| 4690 | } |
| 4691 | } |
| 4692 | /* ensure that *outobj is at least requiredsize characters long, |
| 4693 | if not reallocate and adjust various state variables. |
| 4694 | Return 0 on success, -1 on error */ |
| 4695 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4696 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4697 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4698 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4699 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4700 | if (requiredsize > oldsize) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4701 | /* remember old output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4702 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4703 | /* exponentially overallocate to minimize reallocations */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4704 | if (requiredsize < 2 * oldsize) |
| 4705 | requiredsize = 2 * oldsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4706 | if (_PyUnicode_Resize(outobj, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4707 | return -1; |
| 4708 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4709 | } |
| 4710 | return 0; |
| 4711 | } |
| 4712 | /* lookup the character, put the result in the output string and adjust |
| 4713 | various state variables. Return a new reference to the object that |
| 4714 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 4715 | undefined (in which case no character was written). |
| 4716 | The called must decref result. |
| 4717 | Return 0 on success, -1 on error. */ |
| 4718 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4719 | 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] | 4720 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4721 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4722 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4723 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4724 | return -1; |
| 4725 | if (*res==NULL) { |
| 4726 | /* not found => default to 1:1 mapping */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4727 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4728 | } |
| 4729 | else if (*res==Py_None) |
| 4730 | ; |
| 4731 | else if (PyInt_Check(*res)) { |
| 4732 | /* no overflow check, because we know that the space is enough */ |
| 4733 | *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); |
| 4734 | } |
| 4735 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4736 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4737 | if (repsize==1) { |
| 4738 | /* no overflow check, because we know that the space is enough */ |
| 4739 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 4740 | } |
| 4741 | else if (repsize!=0) { |
| 4742 | /* more than one character */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4743 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
Walter Dörwald | cd736e7 | 2004-02-05 17:36:00 +0000 | [diff] [blame] | 4744 | (insize - (curinp-startinp)) + |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4745 | repsize - 1; |
| 4746 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4747 | return -1; |
| 4748 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 4749 | *outp += repsize; |
| 4750 | } |
| 4751 | } |
| 4752 | else |
| 4753 | return -1; |
| 4754 | return 0; |
| 4755 | } |
| 4756 | |
| 4757 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4758 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4759 | PyObject *mapping, |
| 4760 | const char *errors) |
| 4761 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4762 | /* output object */ |
| 4763 | PyObject *res = NULL; |
| 4764 | /* pointers to the beginning and end+1 of input */ |
| 4765 | const Py_UNICODE *startp = p; |
| 4766 | const Py_UNICODE *endp = p + size; |
| 4767 | /* pointer into the output */ |
| 4768 | Py_UNICODE *str; |
| 4769 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4770 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4771 | char *reason = "character maps to <undefined>"; |
| 4772 | PyObject *errorHandler = NULL; |
| 4773 | PyObject *exc = NULL; |
| 4774 | /* the following variable is used for caching string comparisons |
| 4775 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4776 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4777 | int known_errorHandler = -1; |
| 4778 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4779 | if (mapping == NULL) { |
| 4780 | PyErr_BadArgument(); |
| 4781 | return NULL; |
| 4782 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4783 | |
| 4784 | /* allocate enough for a simple 1:1 translation without |
| 4785 | replacements, if we need more, we'll resize */ |
| 4786 | res = PyUnicode_FromUnicode(NULL, size); |
| 4787 | if (res == NULL) |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4788 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4789 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4790 | return res; |
| 4791 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4792 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4793 | while (p<endp) { |
| 4794 | /* try to encode it */ |
| 4795 | PyObject *x = NULL; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4796 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4797 | Py_XDECREF(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4798 | goto onError; |
| 4799 | } |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 4800 | Py_XDECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4801 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 4802 | ++p; |
| 4803 | else { /* untranslatable character */ |
| 4804 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4805 | Py_ssize_t repsize; |
| 4806 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4807 | Py_UNICODE *uni2; |
| 4808 | /* startpos for collecting untranslatable chars */ |
| 4809 | const Py_UNICODE *collstart = p; |
| 4810 | const Py_UNICODE *collend = p+1; |
| 4811 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4812 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4813 | /* find all untranslatable characters */ |
| 4814 | while (collend < endp) { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4815 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4816 | goto onError; |
| 4817 | Py_XDECREF(x); |
| 4818 | if (x!=Py_None) |
| 4819 | break; |
| 4820 | ++collend; |
| 4821 | } |
| 4822 | /* cache callback name lookup |
| 4823 | * (if not done yet, i.e. it's the first error) */ |
| 4824 | if (known_errorHandler==-1) { |
| 4825 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4826 | known_errorHandler = 1; |
| 4827 | else if (!strcmp(errors, "replace")) |
| 4828 | known_errorHandler = 2; |
| 4829 | else if (!strcmp(errors, "ignore")) |
| 4830 | known_errorHandler = 3; |
| 4831 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4832 | known_errorHandler = 4; |
| 4833 | else |
| 4834 | known_errorHandler = 0; |
| 4835 | } |
| 4836 | switch (known_errorHandler) { |
| 4837 | case 1: /* strict */ |
| 4838 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
| 4839 | goto onError; |
| 4840 | case 2: /* replace */ |
| 4841 | /* No need to check for space, this is a 1:1 replacement */ |
| 4842 | for (coll = collstart; coll<collend; ++coll) |
| 4843 | *str++ = '?'; |
| 4844 | /* fall through */ |
| 4845 | case 3: /* ignore */ |
| 4846 | p = collend; |
| 4847 | break; |
| 4848 | case 4: /* xmlcharrefreplace */ |
| 4849 | /* generate replacement (temporarily (mis)uses p) */ |
| 4850 | for (p = collstart; p < collend; ++p) { |
| 4851 | char buffer[2+29+1+1]; |
| 4852 | char *cp; |
| 4853 | sprintf(buffer, "&#%d;", (int)*p); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4854 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4855 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 4856 | goto onError; |
| 4857 | for (cp = buffer; *cp; ++cp) |
| 4858 | *str++ = *cp; |
| 4859 | } |
| 4860 | p = collend; |
| 4861 | break; |
| 4862 | default: |
| 4863 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 4864 | reason, startp, size, &exc, |
| 4865 | collstart-startp, collend-startp, &newpos); |
| 4866 | if (repunicode == NULL) |
| 4867 | goto onError; |
| 4868 | /* generate replacement */ |
| 4869 | repsize = PyUnicode_GET_SIZE(repunicode); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4870 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4871 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 4872 | Py_DECREF(repunicode); |
| 4873 | goto onError; |
| 4874 | } |
| 4875 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 4876 | *str++ = *uni2; |
| 4877 | p = startp + newpos; |
| 4878 | Py_DECREF(repunicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4879 | } |
| 4880 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4881 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4882 | /* Resize if we allocated to much */ |
| 4883 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4884 | if (respos<PyUnicode_GET_SIZE(res)) { |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4885 | if (_PyUnicode_Resize(&res, respos) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 4886 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4887 | } |
| 4888 | Py_XDECREF(exc); |
| 4889 | Py_XDECREF(errorHandler); |
| 4890 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4891 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4892 | onError: |
| 4893 | Py_XDECREF(res); |
| 4894 | Py_XDECREF(exc); |
| 4895 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4896 | return NULL; |
| 4897 | } |
| 4898 | |
| 4899 | PyObject *PyUnicode_Translate(PyObject *str, |
| 4900 | PyObject *mapping, |
| 4901 | const char *errors) |
| 4902 | { |
| 4903 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4904 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4905 | str = PyUnicode_FromObject(str); |
| 4906 | if (str == NULL) |
| 4907 | goto onError; |
| 4908 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 4909 | PyUnicode_GET_SIZE(str), |
| 4910 | mapping, |
| 4911 | errors); |
| 4912 | Py_DECREF(str); |
| 4913 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4914 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4915 | onError: |
| 4916 | Py_XDECREF(str); |
| 4917 | return NULL; |
| 4918 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4919 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4920 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 4921 | |
| 4922 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4923 | Py_ssize_t length, |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4924 | char *output, |
| 4925 | const char *errors) |
| 4926 | { |
| 4927 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4928 | PyObject *errorHandler = NULL; |
| 4929 | PyObject *exc = NULL; |
| 4930 | const char *encoding = "decimal"; |
| 4931 | const char *reason = "invalid decimal Unicode string"; |
| 4932 | /* the following variable is used for caching string comparisons |
| 4933 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 4934 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4935 | |
| 4936 | if (output == NULL) { |
| 4937 | PyErr_BadArgument(); |
| 4938 | return -1; |
| 4939 | } |
| 4940 | |
| 4941 | p = s; |
| 4942 | end = s + length; |
| 4943 | while (p < end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4944 | register Py_UNICODE ch = *p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4945 | int decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4946 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4947 | Py_ssize_t repsize; |
| 4948 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4949 | Py_UNICODE *uni2; |
| 4950 | Py_UNICODE *collstart; |
| 4951 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4952 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4953 | if (Py_UNICODE_ISSPACE(ch)) { |
| 4954 | *output++ = ' '; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4955 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4956 | continue; |
| 4957 | } |
| 4958 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 4959 | if (decimal >= 0) { |
| 4960 | *output++ = '0' + decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4961 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4962 | continue; |
| 4963 | } |
Guido van Rossum | ba47704 | 2000-04-06 18:18:10 +0000 | [diff] [blame] | 4964 | if (0 < ch && ch < 256) { |
Guido van Rossum | 42c29aa | 2000-05-03 23:58:29 +0000 | [diff] [blame] | 4965 | *output++ = (char)ch; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4966 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4967 | continue; |
| 4968 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4969 | /* All other characters are considered unencodable */ |
| 4970 | collstart = p; |
| 4971 | collend = p+1; |
| 4972 | while (collend < end) { |
| 4973 | if ((0 < *collend && *collend < 256) || |
| 4974 | !Py_UNICODE_ISSPACE(*collend) || |
| 4975 | Py_UNICODE_TODECIMAL(*collend)) |
| 4976 | break; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4977 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4978 | /* cache callback name lookup |
| 4979 | * (if not done yet, i.e. it's the first error) */ |
| 4980 | if (known_errorHandler==-1) { |
| 4981 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4982 | known_errorHandler = 1; |
| 4983 | else if (!strcmp(errors, "replace")) |
| 4984 | known_errorHandler = 2; |
| 4985 | else if (!strcmp(errors, "ignore")) |
| 4986 | known_errorHandler = 3; |
| 4987 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4988 | known_errorHandler = 4; |
| 4989 | else |
| 4990 | known_errorHandler = 0; |
| 4991 | } |
| 4992 | switch (known_errorHandler) { |
| 4993 | case 1: /* strict */ |
| 4994 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 4995 | goto onError; |
| 4996 | case 2: /* replace */ |
| 4997 | for (p = collstart; p < collend; ++p) |
| 4998 | *output++ = '?'; |
| 4999 | /* fall through */ |
| 5000 | case 3: /* ignore */ |
| 5001 | p = collend; |
| 5002 | break; |
| 5003 | case 4: /* xmlcharrefreplace */ |
| 5004 | /* generate replacement (temporarily (mis)uses p) */ |
| 5005 | for (p = collstart; p < collend; ++p) |
| 5006 | output += sprintf(output, "&#%d;", (int)*p); |
| 5007 | p = collend; |
| 5008 | break; |
| 5009 | default: |
| 5010 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 5011 | encoding, reason, s, length, &exc, |
| 5012 | collstart-s, collend-s, &newpos); |
| 5013 | if (repunicode == NULL) |
| 5014 | goto onError; |
| 5015 | /* generate replacement */ |
| 5016 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5017 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 5018 | Py_UNICODE ch = *uni2; |
| 5019 | if (Py_UNICODE_ISSPACE(ch)) |
| 5020 | *output++ = ' '; |
| 5021 | else { |
| 5022 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5023 | if (decimal >= 0) |
| 5024 | *output++ = '0' + decimal; |
| 5025 | else if (0 < ch && ch < 256) |
| 5026 | *output++ = (char)ch; |
| 5027 | else { |
| 5028 | Py_DECREF(repunicode); |
| 5029 | raise_encode_exception(&exc, encoding, |
| 5030 | s, length, collstart-s, collend-s, reason); |
| 5031 | goto onError; |
| 5032 | } |
| 5033 | } |
| 5034 | } |
| 5035 | p = s + newpos; |
| 5036 | Py_DECREF(repunicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5037 | } |
| 5038 | } |
| 5039 | /* 0-terminate the output string */ |
| 5040 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5041 | Py_XDECREF(exc); |
| 5042 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5043 | return 0; |
| 5044 | |
| 5045 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5046 | Py_XDECREF(exc); |
| 5047 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5048 | return -1; |
| 5049 | } |
| 5050 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5051 | /* --- Helpers ------------------------------------------------------------ */ |
| 5052 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 5053 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5054 | |
| 5055 | #include "stringlib/fastsearch.h" |
| 5056 | |
| 5057 | #include "stringlib/count.h" |
| 5058 | #include "stringlib/find.h" |
| 5059 | #include "stringlib/partition.h" |
| 5060 | |
| 5061 | /* helper macro to fixup start/end slice values */ |
| 5062 | #define FIX_START_END(obj) \ |
| 5063 | if (start < 0) \ |
| 5064 | start += (obj)->length; \ |
| 5065 | if (start < 0) \ |
| 5066 | start = 0; \ |
| 5067 | if (end > (obj)->length) \ |
| 5068 | end = (obj)->length; \ |
| 5069 | if (end < 0) \ |
| 5070 | end += (obj)->length; \ |
| 5071 | if (end < 0) \ |
| 5072 | end = 0; |
| 5073 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5074 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5075 | PyObject *substr, |
| 5076 | Py_ssize_t start, |
| 5077 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5078 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5079 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5080 | PyUnicodeObject* str_obj; |
| 5081 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5082 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5083 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 5084 | if (!str_obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5085 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5086 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 5087 | if (!sub_obj) { |
| 5088 | Py_DECREF(str_obj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5089 | return -1; |
| 5090 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5091 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5092 | FIX_START_END(str_obj); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5093 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5094 | result = stringlib_count( |
| 5095 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
| 5096 | ); |
| 5097 | |
| 5098 | Py_DECREF(sub_obj); |
| 5099 | Py_DECREF(str_obj); |
| 5100 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5101 | return result; |
| 5102 | } |
| 5103 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5104 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5105 | PyObject *sub, |
| 5106 | Py_ssize_t start, |
| 5107 | Py_ssize_t end, |
| 5108 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5109 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5110 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5111 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5112 | str = PyUnicode_FromObject(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5113 | if (!str) |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 5114 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5115 | sub = PyUnicode_FromObject(sub); |
| 5116 | if (!sub) { |
Marc-André Lemburg | 4164439 | 2002-05-29 13:46:29 +0000 | [diff] [blame] | 5117 | Py_DECREF(str); |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 5118 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5119 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5120 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5121 | if (direction > 0) |
| 5122 | result = stringlib_find_slice( |
| 5123 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5124 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5125 | start, end |
| 5126 | ); |
| 5127 | else |
| 5128 | result = stringlib_rfind_slice( |
| 5129 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5130 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5131 | start, end |
| 5132 | ); |
| 5133 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5134 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5135 | Py_DECREF(sub); |
| 5136 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5137 | return result; |
| 5138 | } |
| 5139 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5140 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5141 | int tailmatch(PyUnicodeObject *self, |
| 5142 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5143 | Py_ssize_t start, |
| 5144 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5145 | int direction) |
| 5146 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5147 | if (substring->length == 0) |
| 5148 | return 1; |
| 5149 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5150 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5151 | |
| 5152 | end -= substring->length; |
| 5153 | if (end < start) |
| 5154 | return 0; |
| 5155 | |
| 5156 | if (direction > 0) { |
| 5157 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 5158 | return 1; |
| 5159 | } else { |
| 5160 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 5161 | return 1; |
| 5162 | } |
| 5163 | |
| 5164 | return 0; |
| 5165 | } |
| 5166 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5167 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5168 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5169 | Py_ssize_t start, |
| 5170 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5171 | int direction) |
| 5172 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5173 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5174 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5175 | str = PyUnicode_FromObject(str); |
| 5176 | if (str == NULL) |
| 5177 | return -1; |
| 5178 | substr = PyUnicode_FromObject(substr); |
| 5179 | if (substr == NULL) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5180 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5181 | return -1; |
| 5182 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5183 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5184 | result = tailmatch((PyUnicodeObject *)str, |
| 5185 | (PyUnicodeObject *)substr, |
| 5186 | start, end, direction); |
| 5187 | Py_DECREF(str); |
| 5188 | Py_DECREF(substr); |
| 5189 | return result; |
| 5190 | } |
| 5191 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5192 | /* Apply fixfct filter to the Unicode object self and return a |
| 5193 | reference to the modified object */ |
| 5194 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5195 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5196 | PyObject *fixup(PyUnicodeObject *self, |
| 5197 | int (*fixfct)(PyUnicodeObject *s)) |
| 5198 | { |
| 5199 | |
| 5200 | PyUnicodeObject *u; |
| 5201 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5202 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5203 | if (u == NULL) |
| 5204 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5205 | |
| 5206 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5207 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5208 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5209 | /* fixfct should return TRUE if it modified the buffer. If |
| 5210 | FALSE, return a reference to the original buffer instead |
| 5211 | (to save space, not time) */ |
| 5212 | Py_INCREF(self); |
| 5213 | Py_DECREF(u); |
| 5214 | return (PyObject*) self; |
| 5215 | } |
| 5216 | return (PyObject*) u; |
| 5217 | } |
| 5218 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5219 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5220 | int fixupper(PyUnicodeObject *self) |
| 5221 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5222 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5223 | Py_UNICODE *s = self->str; |
| 5224 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5225 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5226 | while (len-- > 0) { |
| 5227 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5228 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5229 | ch = Py_UNICODE_TOUPPER(*s); |
| 5230 | if (ch != *s) { |
| 5231 | status = 1; |
| 5232 | *s = ch; |
| 5233 | } |
| 5234 | s++; |
| 5235 | } |
| 5236 | |
| 5237 | return status; |
| 5238 | } |
| 5239 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5240 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5241 | int fixlower(PyUnicodeObject *self) |
| 5242 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5243 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5244 | Py_UNICODE *s = self->str; |
| 5245 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5246 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5247 | while (len-- > 0) { |
| 5248 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5249 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5250 | ch = Py_UNICODE_TOLOWER(*s); |
| 5251 | if (ch != *s) { |
| 5252 | status = 1; |
| 5253 | *s = ch; |
| 5254 | } |
| 5255 | s++; |
| 5256 | } |
| 5257 | |
| 5258 | return status; |
| 5259 | } |
| 5260 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5261 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5262 | int fixswapcase(PyUnicodeObject *self) |
| 5263 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5264 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5265 | Py_UNICODE *s = self->str; |
| 5266 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5267 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5268 | while (len-- > 0) { |
| 5269 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5270 | *s = Py_UNICODE_TOLOWER(*s); |
| 5271 | status = 1; |
| 5272 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 5273 | *s = Py_UNICODE_TOUPPER(*s); |
| 5274 | status = 1; |
| 5275 | } |
| 5276 | s++; |
| 5277 | } |
| 5278 | |
| 5279 | return status; |
| 5280 | } |
| 5281 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5282 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5283 | int fixcapitalize(PyUnicodeObject *self) |
| 5284 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5285 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5286 | Py_UNICODE *s = self->str; |
| 5287 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5288 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5289 | if (len == 0) |
| 5290 | return 0; |
| 5291 | if (Py_UNICODE_ISLOWER(*s)) { |
| 5292 | *s = Py_UNICODE_TOUPPER(*s); |
| 5293 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5294 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5295 | s++; |
| 5296 | while (--len > 0) { |
| 5297 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5298 | *s = Py_UNICODE_TOLOWER(*s); |
| 5299 | status = 1; |
| 5300 | } |
| 5301 | s++; |
| 5302 | } |
| 5303 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5304 | } |
| 5305 | |
| 5306 | static |
| 5307 | int fixtitle(PyUnicodeObject *self) |
| 5308 | { |
| 5309 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5310 | register Py_UNICODE *e; |
| 5311 | int previous_is_cased; |
| 5312 | |
| 5313 | /* Shortcut for single character strings */ |
| 5314 | if (PyUnicode_GET_SIZE(self) == 1) { |
| 5315 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 5316 | if (*p != ch) { |
| 5317 | *p = ch; |
| 5318 | return 1; |
| 5319 | } |
| 5320 | else |
| 5321 | return 0; |
| 5322 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5323 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5324 | e = p + PyUnicode_GET_SIZE(self); |
| 5325 | previous_is_cased = 0; |
| 5326 | for (; p < e; p++) { |
| 5327 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5328 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5329 | if (previous_is_cased) |
| 5330 | *p = Py_UNICODE_TOLOWER(ch); |
| 5331 | else |
| 5332 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5333 | |
| 5334 | if (Py_UNICODE_ISLOWER(ch) || |
| 5335 | Py_UNICODE_ISUPPER(ch) || |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5336 | Py_UNICODE_ISTITLE(ch)) |
| 5337 | previous_is_cased = 1; |
| 5338 | else |
| 5339 | previous_is_cased = 0; |
| 5340 | } |
| 5341 | return 1; |
| 5342 | } |
| 5343 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5344 | PyObject * |
| 5345 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5346 | { |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5347 | PyObject *internal_separator = NULL; |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 5348 | const Py_UNICODE blank = ' '; |
| 5349 | const Py_UNICODE *sep = ␣ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5350 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5351 | PyUnicodeObject *res = NULL; /* the result */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5352 | Py_ssize_t res_alloc = 100; /* # allocated bytes for string in res */ |
| 5353 | Py_ssize_t res_used; /* # used bytes */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5354 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 5355 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5356 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5357 | PyObject *item; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5358 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5359 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5360 | fseq = PySequence_Fast(seq, ""); |
| 5361 | if (fseq == NULL) { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5362 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5363 | } |
| 5364 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5365 | /* Grrrr. A codec may be invoked to convert str objects to |
| 5366 | * Unicode, and so it's possible to call back into Python code |
| 5367 | * during PyUnicode_FromObject(), and so it's possible for a sick |
| 5368 | * codec to change the size of fseq (if seq is a list). Therefore |
| 5369 | * we have to keep refetching the size -- can't assume seqlen |
| 5370 | * is invariant. |
| 5371 | */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5372 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5373 | /* If empty sequence, return u"". */ |
| 5374 | if (seqlen == 0) { |
| 5375 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 5376 | goto Done; |
| 5377 | } |
| 5378 | /* If singleton sequence with an exact Unicode, return that. */ |
| 5379 | if (seqlen == 1) { |
| 5380 | item = PySequence_Fast_GET_ITEM(fseq, 0); |
| 5381 | if (PyUnicode_CheckExact(item)) { |
| 5382 | Py_INCREF(item); |
| 5383 | res = (PyUnicodeObject *)item; |
| 5384 | goto Done; |
| 5385 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5386 | } |
| 5387 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5388 | /* At least two items to join, or one that isn't exact Unicode. */ |
| 5389 | if (seqlen > 1) { |
| 5390 | /* Set up sep and seplen -- they're needed. */ |
| 5391 | if (separator == NULL) { |
| 5392 | sep = ␣ |
| 5393 | seplen = 1; |
| 5394 | } |
| 5395 | else { |
| 5396 | internal_separator = PyUnicode_FromObject(separator); |
| 5397 | if (internal_separator == NULL) |
| 5398 | goto onError; |
| 5399 | sep = PyUnicode_AS_UNICODE(internal_separator); |
| 5400 | seplen = PyUnicode_GET_SIZE(internal_separator); |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5401 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 5402 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5403 | } |
| 5404 | } |
| 5405 | |
| 5406 | /* Get space. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5407 | res = _PyUnicode_New(res_alloc); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5408 | if (res == NULL) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5409 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5410 | res_p = PyUnicode_AS_UNICODE(res); |
| 5411 | res_used = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5412 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5413 | for (i = 0; i < seqlen; ++i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5414 | Py_ssize_t itemlen; |
| 5415 | Py_ssize_t new_res_used; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5416 | |
| 5417 | item = PySequence_Fast_GET_ITEM(fseq, i); |
| 5418 | /* Convert item to Unicode. */ |
Guido van Rossum | f104429 | 2007-09-27 18:01:22 +0000 | [diff] [blame] | 5419 | if (!PyString_Check(item) && !PyUnicode_Check(item)) |
| 5420 | { |
| 5421 | if (PyBytes_Check(item)) |
| 5422 | { |
| 5423 | PyErr_Format(PyExc_TypeError, |
| 5424 | "sequence item %d: join() will not operate on " |
| 5425 | "bytes objects", i); |
| 5426 | goto onError; |
| 5427 | } |
| 5428 | item = PyObject_Unicode(item); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5429 | } |
Guido van Rossum | f104429 | 2007-09-27 18:01:22 +0000 | [diff] [blame] | 5430 | else |
| 5431 | item = PyUnicode_FromObject(item); |
| 5432 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5433 | if (item == NULL) |
| 5434 | goto onError; |
| 5435 | /* We own a reference to item from here on. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5436 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5437 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 5438 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5439 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5440 | /* 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] | 5441 | itemlen = PyUnicode_GET_SIZE(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5442 | new_res_used = res_used + itemlen; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5443 | if (new_res_used < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5444 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5445 | if (i < seqlen - 1) { |
| 5446 | new_res_used += seplen; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5447 | if (new_res_used < 0) |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5448 | goto Overflow; |
| 5449 | } |
| 5450 | if (new_res_used > res_alloc) { |
| 5451 | /* double allocated size until it's big enough */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5452 | do { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5453 | res_alloc += res_alloc; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5454 | if (res_alloc <= 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5455 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5456 | } while (new_res_used > res_alloc); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5457 | if (_PyUnicode_Resize(&res, res_alloc) < 0) { |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 5458 | Py_DECREF(item); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5459 | goto onError; |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 5460 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5461 | res_p = PyUnicode_AS_UNICODE(res) + res_used; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5462 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5463 | |
| 5464 | /* Copy item, and maybe the separator. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5465 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5466 | res_p += itemlen; |
| 5467 | if (i < seqlen - 1) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5468 | Py_UNICODE_COPY(res_p, sep, seplen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5469 | res_p += seplen; |
| 5470 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5471 | Py_DECREF(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5472 | res_used = new_res_used; |
| 5473 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5474 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5475 | /* Shrink res to match the used area; this probably can't fail, |
| 5476 | * but it's cheap to check. |
| 5477 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5478 | if (_PyUnicode_Resize(&res, res_used) < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5479 | goto onError; |
| 5480 | |
| 5481 | Done: |
| 5482 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5483 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5484 | return (PyObject *)res; |
| 5485 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5486 | Overflow: |
| 5487 | PyErr_SetString(PyExc_OverflowError, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5488 | "join() result is too long for a Python string"); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5489 | Py_DECREF(item); |
| 5490 | /* fall through */ |
| 5491 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5492 | onError: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5493 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5494 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5495 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5496 | return NULL; |
| 5497 | } |
| 5498 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5499 | static |
| 5500 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5501 | Py_ssize_t left, |
| 5502 | Py_ssize_t right, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5503 | Py_UNICODE fill) |
| 5504 | { |
| 5505 | PyUnicodeObject *u; |
| 5506 | |
| 5507 | if (left < 0) |
| 5508 | left = 0; |
| 5509 | if (right < 0) |
| 5510 | right = 0; |
| 5511 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5512 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5513 | Py_INCREF(self); |
| 5514 | return self; |
| 5515 | } |
| 5516 | |
| 5517 | u = _PyUnicode_New(left + self->length + right); |
| 5518 | if (u) { |
| 5519 | if (left) |
| 5520 | Py_UNICODE_FILL(u->str, fill, left); |
| 5521 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 5522 | if (right) |
| 5523 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 5524 | } |
| 5525 | |
| 5526 | return u; |
| 5527 | } |
| 5528 | |
| 5529 | #define SPLIT_APPEND(data, left, right) \ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5530 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5531 | if (!str) \ |
| 5532 | goto onError; \ |
| 5533 | if (PyList_Append(list, str)) { \ |
| 5534 | Py_DECREF(str); \ |
| 5535 | goto onError; \ |
| 5536 | } \ |
| 5537 | else \ |
| 5538 | Py_DECREF(str); |
| 5539 | |
| 5540 | static |
| 5541 | PyObject *split_whitespace(PyUnicodeObject *self, |
| 5542 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5543 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5544 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5545 | register Py_ssize_t i; |
| 5546 | register Py_ssize_t j; |
| 5547 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5548 | PyObject *str; |
| 5549 | |
| 5550 | for (i = j = 0; i < len; ) { |
| 5551 | /* find a token */ |
| 5552 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 5553 | i++; |
| 5554 | j = i; |
| 5555 | while (i < len && !Py_UNICODE_ISSPACE(self->str[i])) |
| 5556 | i++; |
| 5557 | if (j < i) { |
| 5558 | if (maxcount-- <= 0) |
| 5559 | break; |
| 5560 | SPLIT_APPEND(self->str, j, i); |
| 5561 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 5562 | i++; |
| 5563 | j = i; |
| 5564 | } |
| 5565 | } |
| 5566 | if (j < len) { |
| 5567 | SPLIT_APPEND(self->str, j, len); |
| 5568 | } |
| 5569 | return list; |
| 5570 | |
| 5571 | onError: |
| 5572 | Py_DECREF(list); |
| 5573 | return NULL; |
| 5574 | } |
| 5575 | |
| 5576 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5577 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5578 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5579 | register Py_ssize_t i; |
| 5580 | register Py_ssize_t j; |
| 5581 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5582 | PyObject *list; |
| 5583 | PyObject *str; |
| 5584 | Py_UNICODE *data; |
| 5585 | |
| 5586 | string = PyUnicode_FromObject(string); |
| 5587 | if (string == NULL) |
| 5588 | return NULL; |
| 5589 | data = PyUnicode_AS_UNICODE(string); |
| 5590 | len = PyUnicode_GET_SIZE(string); |
| 5591 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5592 | list = PyList_New(0); |
| 5593 | if (!list) |
| 5594 | goto onError; |
| 5595 | |
| 5596 | for (i = j = 0; i < len; ) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5597 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5598 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5599 | /* Find a line and append it */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5600 | while (i < len && !BLOOM_LINEBREAK(data[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5601 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5602 | |
| 5603 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5604 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5605 | if (i < len) { |
| 5606 | if (data[i] == '\r' && i + 1 < len && |
| 5607 | data[i+1] == '\n') |
| 5608 | i += 2; |
| 5609 | else |
| 5610 | i++; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5611 | if (keepends) |
| 5612 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5613 | } |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5614 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5615 | j = i; |
| 5616 | } |
| 5617 | if (j < len) { |
| 5618 | SPLIT_APPEND(data, j, len); |
| 5619 | } |
| 5620 | |
| 5621 | Py_DECREF(string); |
| 5622 | return list; |
| 5623 | |
| 5624 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5625 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5626 | Py_DECREF(string); |
| 5627 | return NULL; |
| 5628 | } |
| 5629 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5630 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5631 | PyObject *split_char(PyUnicodeObject *self, |
| 5632 | PyObject *list, |
| 5633 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5634 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5635 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5636 | register Py_ssize_t i; |
| 5637 | register Py_ssize_t j; |
| 5638 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5639 | PyObject *str; |
| 5640 | |
| 5641 | for (i = j = 0; i < len; ) { |
| 5642 | if (self->str[i] == ch) { |
| 5643 | if (maxcount-- <= 0) |
| 5644 | break; |
| 5645 | SPLIT_APPEND(self->str, j, i); |
| 5646 | i = j = i + 1; |
| 5647 | } else |
| 5648 | i++; |
| 5649 | } |
| 5650 | if (j <= len) { |
| 5651 | SPLIT_APPEND(self->str, j, len); |
| 5652 | } |
| 5653 | return list; |
| 5654 | |
| 5655 | onError: |
| 5656 | Py_DECREF(list); |
| 5657 | return NULL; |
| 5658 | } |
| 5659 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5660 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5661 | PyObject *split_substring(PyUnicodeObject *self, |
| 5662 | PyObject *list, |
| 5663 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5664 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5665 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5666 | register Py_ssize_t i; |
| 5667 | register Py_ssize_t j; |
| 5668 | Py_ssize_t len = self->length; |
| 5669 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5670 | PyObject *str; |
| 5671 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 5672 | for (i = j = 0; i <= len - sublen; ) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5673 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5674 | if (maxcount-- <= 0) |
| 5675 | break; |
| 5676 | SPLIT_APPEND(self->str, j, i); |
| 5677 | i = j = i + sublen; |
| 5678 | } else |
| 5679 | i++; |
| 5680 | } |
| 5681 | if (j <= len) { |
| 5682 | SPLIT_APPEND(self->str, j, len); |
| 5683 | } |
| 5684 | return list; |
| 5685 | |
| 5686 | onError: |
| 5687 | Py_DECREF(list); |
| 5688 | return NULL; |
| 5689 | } |
| 5690 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5691 | static |
| 5692 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
| 5693 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5694 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5695 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5696 | register Py_ssize_t i; |
| 5697 | register Py_ssize_t j; |
| 5698 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5699 | PyObject *str; |
| 5700 | |
| 5701 | for (i = j = len - 1; i >= 0; ) { |
| 5702 | /* find a token */ |
| 5703 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 5704 | i--; |
| 5705 | j = i; |
| 5706 | while (i >= 0 && !Py_UNICODE_ISSPACE(self->str[i])) |
| 5707 | i--; |
| 5708 | if (j > i) { |
| 5709 | if (maxcount-- <= 0) |
| 5710 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5711 | SPLIT_APPEND(self->str, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5712 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 5713 | i--; |
| 5714 | j = i; |
| 5715 | } |
| 5716 | } |
| 5717 | if (j >= 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5718 | SPLIT_APPEND(self->str, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5719 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5720 | if (PyList_Reverse(list) < 0) |
| 5721 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5722 | return list; |
| 5723 | |
| 5724 | onError: |
| 5725 | Py_DECREF(list); |
| 5726 | return NULL; |
| 5727 | } |
| 5728 | |
| 5729 | static |
| 5730 | PyObject *rsplit_char(PyUnicodeObject *self, |
| 5731 | PyObject *list, |
| 5732 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5733 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5734 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5735 | register Py_ssize_t i; |
| 5736 | register Py_ssize_t j; |
| 5737 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5738 | PyObject *str; |
| 5739 | |
| 5740 | for (i = j = len - 1; i >= 0; ) { |
| 5741 | if (self->str[i] == ch) { |
| 5742 | if (maxcount-- <= 0) |
| 5743 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5744 | SPLIT_APPEND(self->str, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5745 | j = i = i - 1; |
| 5746 | } else |
| 5747 | i--; |
| 5748 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 5749 | if (j >= -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5750 | SPLIT_APPEND(self->str, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5751 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5752 | if (PyList_Reverse(list) < 0) |
| 5753 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5754 | return list; |
| 5755 | |
| 5756 | onError: |
| 5757 | Py_DECREF(list); |
| 5758 | return NULL; |
| 5759 | } |
| 5760 | |
| 5761 | static |
| 5762 | PyObject *rsplit_substring(PyUnicodeObject *self, |
| 5763 | PyObject *list, |
| 5764 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5765 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5766 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5767 | register Py_ssize_t i; |
| 5768 | register Py_ssize_t j; |
| 5769 | Py_ssize_t len = self->length; |
| 5770 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5771 | PyObject *str; |
| 5772 | |
| 5773 | for (i = len - sublen, j = len; i >= 0; ) { |
| 5774 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5775 | if (maxcount-- <= 0) |
| 5776 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5777 | SPLIT_APPEND(self->str, i + sublen, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5778 | j = i; |
| 5779 | i -= sublen; |
| 5780 | } else |
| 5781 | i--; |
| 5782 | } |
| 5783 | if (j >= 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5784 | SPLIT_APPEND(self->str, 0, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5785 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5786 | if (PyList_Reverse(list) < 0) |
| 5787 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5788 | return list; |
| 5789 | |
| 5790 | onError: |
| 5791 | Py_DECREF(list); |
| 5792 | return NULL; |
| 5793 | } |
| 5794 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5795 | #undef SPLIT_APPEND |
| 5796 | |
| 5797 | static |
| 5798 | PyObject *split(PyUnicodeObject *self, |
| 5799 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5800 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5801 | { |
| 5802 | PyObject *list; |
| 5803 | |
| 5804 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5805 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5806 | |
| 5807 | list = PyList_New(0); |
| 5808 | if (!list) |
| 5809 | return NULL; |
| 5810 | |
| 5811 | if (substring == NULL) |
| 5812 | return split_whitespace(self,list,maxcount); |
| 5813 | |
| 5814 | else if (substring->length == 1) |
| 5815 | return split_char(self,list,substring->str[0],maxcount); |
| 5816 | |
| 5817 | else if (substring->length == 0) { |
| 5818 | Py_DECREF(list); |
| 5819 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5820 | return NULL; |
| 5821 | } |
| 5822 | else |
| 5823 | return split_substring(self,list,substring,maxcount); |
| 5824 | } |
| 5825 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5826 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5827 | PyObject *rsplit(PyUnicodeObject *self, |
| 5828 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5829 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5830 | { |
| 5831 | PyObject *list; |
| 5832 | |
| 5833 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5834 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5835 | |
| 5836 | list = PyList_New(0); |
| 5837 | if (!list) |
| 5838 | return NULL; |
| 5839 | |
| 5840 | if (substring == NULL) |
| 5841 | return rsplit_whitespace(self,list,maxcount); |
| 5842 | |
| 5843 | else if (substring->length == 1) |
| 5844 | return rsplit_char(self,list,substring->str[0],maxcount); |
| 5845 | |
| 5846 | else if (substring->length == 0) { |
| 5847 | Py_DECREF(list); |
| 5848 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5849 | return NULL; |
| 5850 | } |
| 5851 | else |
| 5852 | return rsplit_substring(self,list,substring,maxcount); |
| 5853 | } |
| 5854 | |
| 5855 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5856 | PyObject *replace(PyUnicodeObject *self, |
| 5857 | PyUnicodeObject *str1, |
| 5858 | PyUnicodeObject *str2, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5859 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5860 | { |
| 5861 | PyUnicodeObject *u; |
| 5862 | |
| 5863 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5864 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5865 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5866 | if (str1->length == str2->length) { |
| 5867 | /* same length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5868 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5869 | if (str1->length == 1) { |
| 5870 | /* replace characters */ |
| 5871 | Py_UNICODE u1, u2; |
| 5872 | if (!findchar(self->str, self->length, str1->str[0])) |
| 5873 | goto nothing; |
| 5874 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5875 | if (!u) |
| 5876 | return NULL; |
| 5877 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5878 | u1 = str1->str[0]; |
| 5879 | u2 = str2->str[0]; |
| 5880 | for (i = 0; i < u->length; i++) |
| 5881 | if (u->str[i] == u1) { |
| 5882 | if (--maxcount < 0) |
| 5883 | break; |
| 5884 | u->str[i] = u2; |
| 5885 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5886 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5887 | i = fastsearch( |
| 5888 | self->str, self->length, str1->str, str1->length, FAST_SEARCH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5889 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5890 | if (i < 0) |
| 5891 | goto nothing; |
| 5892 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5893 | if (!u) |
| 5894 | return NULL; |
| 5895 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5896 | while (i <= self->length - str1->length) |
| 5897 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 5898 | if (--maxcount < 0) |
| 5899 | break; |
| 5900 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 5901 | i += str1->length; |
| 5902 | } else |
| 5903 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5904 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5905 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5906 | |
| 5907 | Py_ssize_t n, i, j, e; |
| 5908 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5909 | Py_UNICODE *p; |
| 5910 | |
| 5911 | /* replace strings */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5912 | n = stringlib_count(self->str, self->length, str1->str, str1->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5913 | if (n > maxcount) |
| 5914 | n = maxcount; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5915 | if (n == 0) |
| 5916 | goto nothing; |
| 5917 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 5918 | delta = (str2->length - str1->length); |
| 5919 | if (delta == 0) { |
| 5920 | new_size = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5921 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5922 | product = n * (str2->length - str1->length); |
| 5923 | if ((product / (str2->length - str1->length)) != n) { |
| 5924 | PyErr_SetString(PyExc_OverflowError, |
| 5925 | "replace string is too long"); |
| 5926 | return NULL; |
| 5927 | } |
| 5928 | new_size = self->length + product; |
| 5929 | if (new_size < 0) { |
| 5930 | PyErr_SetString(PyExc_OverflowError, |
| 5931 | "replace string is too long"); |
| 5932 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5933 | } |
| 5934 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5935 | u = _PyUnicode_New(new_size); |
| 5936 | if (!u) |
| 5937 | return NULL; |
| 5938 | i = 0; |
| 5939 | p = u->str; |
| 5940 | e = self->length - str1->length; |
| 5941 | if (str1->length > 0) { |
| 5942 | while (n-- > 0) { |
| 5943 | /* look for next match */ |
| 5944 | j = i; |
| 5945 | while (j <= e) { |
| 5946 | if (Py_UNICODE_MATCH(self, j, str1)) |
| 5947 | break; |
| 5948 | j++; |
| 5949 | } |
| 5950 | if (j > i) { |
| 5951 | if (j > e) |
| 5952 | break; |
| 5953 | /* copy unchanged part [i:j] */ |
| 5954 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 5955 | p += j - i; |
| 5956 | } |
| 5957 | /* copy substitution string */ |
| 5958 | if (str2->length > 0) { |
| 5959 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 5960 | p += str2->length; |
| 5961 | } |
| 5962 | i = j + str1->length; |
| 5963 | } |
| 5964 | if (i < self->length) |
| 5965 | /* copy tail [i:] */ |
| 5966 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 5967 | } else { |
| 5968 | /* interleave */ |
| 5969 | while (n > 0) { |
| 5970 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 5971 | p += str2->length; |
| 5972 | if (--n <= 0) |
| 5973 | break; |
| 5974 | *p++ = self->str[i++]; |
| 5975 | } |
| 5976 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 5977 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5978 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5979 | return (PyObject *) u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5980 | |
| 5981 | nothing: |
| 5982 | /* nothing to replace; return original string (when possible) */ |
| 5983 | if (PyUnicode_CheckExact(self)) { |
| 5984 | Py_INCREF(self); |
| 5985 | return (PyObject *) self; |
| 5986 | } |
| 5987 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5988 | } |
| 5989 | |
| 5990 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 5991 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5992 | PyDoc_STRVAR(title__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5993 | "S.title() -> unicode\n\ |
| 5994 | \n\ |
| 5995 | 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] | 5996 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5997 | |
| 5998 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5999 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6000 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6001 | return fixup(self, fixtitle); |
| 6002 | } |
| 6003 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6004 | PyDoc_STRVAR(capitalize__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6005 | "S.capitalize() -> unicode\n\ |
| 6006 | \n\ |
| 6007 | 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] | 6008 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6009 | |
| 6010 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6011 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6012 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6013 | return fixup(self, fixcapitalize); |
| 6014 | } |
| 6015 | |
| 6016 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6017 | PyDoc_STRVAR(capwords__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6018 | "S.capwords() -> unicode\n\ |
| 6019 | \n\ |
| 6020 | 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] | 6021 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6022 | |
| 6023 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6024 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6025 | { |
| 6026 | PyObject *list; |
| 6027 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6028 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6029 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6030 | /* Split into words */ |
| 6031 | list = split(self, NULL, -1); |
| 6032 | if (!list) |
| 6033 | return NULL; |
| 6034 | |
| 6035 | /* Capitalize each word */ |
| 6036 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 6037 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 6038 | fixcapitalize); |
| 6039 | if (item == NULL) |
| 6040 | goto onError; |
| 6041 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 6042 | PyList_SET_ITEM(list, i, item); |
| 6043 | } |
| 6044 | |
| 6045 | /* Join the words to form a new string */ |
| 6046 | item = PyUnicode_Join(NULL, list); |
| 6047 | |
| 6048 | onError: |
| 6049 | Py_DECREF(list); |
| 6050 | return (PyObject *)item; |
| 6051 | } |
| 6052 | #endif |
| 6053 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6054 | /* Argument converter. Coerces to a single unicode character */ |
| 6055 | |
| 6056 | static int |
| 6057 | convert_uc(PyObject *obj, void *addr) |
| 6058 | { |
| 6059 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 6060 | PyObject *uniobj; |
| 6061 | Py_UNICODE *unistr; |
| 6062 | |
| 6063 | uniobj = PyUnicode_FromObject(obj); |
| 6064 | if (uniobj == NULL) { |
| 6065 | PyErr_SetString(PyExc_TypeError, |
| 6066 | "The fill character cannot be converted to Unicode"); |
| 6067 | return 0; |
| 6068 | } |
| 6069 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 6070 | PyErr_SetString(PyExc_TypeError, |
| 6071 | "The fill character must be exactly one character long"); |
| 6072 | Py_DECREF(uniobj); |
| 6073 | return 0; |
| 6074 | } |
| 6075 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 6076 | *fillcharloc = unistr[0]; |
| 6077 | Py_DECREF(uniobj); |
| 6078 | return 1; |
| 6079 | } |
| 6080 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6081 | PyDoc_STRVAR(center__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6082 | "S.center(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6083 | \n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6084 | Return S centered in a Unicode string of length width. Padding is\n\ |
| 6085 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6086 | |
| 6087 | static PyObject * |
| 6088 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 6089 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6090 | Py_ssize_t marg, left; |
| 6091 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6092 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6093 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6094 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6095 | return NULL; |
| 6096 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6097 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6098 | Py_INCREF(self); |
| 6099 | return (PyObject*) self; |
| 6100 | } |
| 6101 | |
| 6102 | marg = width - self->length; |
| 6103 | left = marg / 2 + (marg & width & 1); |
| 6104 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6105 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6106 | } |
| 6107 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6108 | #if 0 |
| 6109 | |
| 6110 | /* This code should go into some future Unicode collation support |
| 6111 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 6112 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6113 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6114 | /* speedy UTF-16 code point order comparison */ |
| 6115 | /* gleaned from: */ |
| 6116 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 6117 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6118 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6119 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6120 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6121 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 6122 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6123 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6124 | }; |
| 6125 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6126 | static int |
| 6127 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6128 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6129 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6130 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6131 | Py_UNICODE *s1 = str1->str; |
| 6132 | Py_UNICODE *s2 = str2->str; |
| 6133 | |
| 6134 | len1 = str1->length; |
| 6135 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6136 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6137 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6138 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6139 | |
| 6140 | c1 = *s1++; |
| 6141 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6142 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6143 | if (c1 > (1<<11) * 26) |
| 6144 | c1 += utf16Fixup[c1>>11]; |
| 6145 | if (c2 > (1<<11) * 26) |
| 6146 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6147 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6148 | |
| 6149 | if (c1 != c2) |
| 6150 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6151 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6152 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6153 | } |
| 6154 | |
| 6155 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6156 | } |
| 6157 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6158 | #else |
| 6159 | |
| 6160 | static int |
| 6161 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6162 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6163 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6164 | |
| 6165 | Py_UNICODE *s1 = str1->str; |
| 6166 | Py_UNICODE *s2 = str2->str; |
| 6167 | |
| 6168 | len1 = str1->length; |
| 6169 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6170 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6171 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6172 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6173 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6174 | c1 = *s1++; |
| 6175 | c2 = *s2++; |
| 6176 | |
| 6177 | if (c1 != c2) |
| 6178 | return (c1 < c2) ? -1 : 1; |
| 6179 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6180 | len1--; len2--; |
| 6181 | } |
| 6182 | |
| 6183 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6184 | } |
| 6185 | |
| 6186 | #endif |
| 6187 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6188 | int PyUnicode_Compare(PyObject *left, |
| 6189 | PyObject *right) |
| 6190 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6191 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) |
| 6192 | return unicode_compare((PyUnicodeObject *)left, |
| 6193 | (PyUnicodeObject *)right); |
| 6194 | if ((PyString_Check(left) && PyUnicode_Check(right)) || |
| 6195 | (PyUnicode_Check(left) && PyString_Check(right))) { |
| 6196 | if (PyUnicode_Check(left)) |
| 6197 | left = _PyUnicode_AsDefaultEncodedString(left, NULL); |
| 6198 | if (PyUnicode_Check(right)) |
| 6199 | right = _PyUnicode_AsDefaultEncodedString(right, NULL); |
| 6200 | assert(PyString_Check(left)); |
| 6201 | assert(PyString_Check(right)); |
| 6202 | return PyObject_Compare(left, right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6203 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6204 | PyErr_Format(PyExc_TypeError, |
| 6205 | "Can't compare %.100s and %.100s", |
| 6206 | left->ob_type->tp_name, |
| 6207 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6208 | return -1; |
| 6209 | } |
| 6210 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6211 | int |
| 6212 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 6213 | { |
| 6214 | int i; |
| 6215 | Py_UNICODE *id; |
| 6216 | assert(PyUnicode_Check(uni)); |
| 6217 | id = PyUnicode_AS_UNICODE(uni); |
| 6218 | /* Compare Unicode string and source character set string */ |
| 6219 | for (i = 0; id[i] && str[i]; i++) |
| 6220 | if (id[i] != str[i]) |
| 6221 | return ((int)id[i] < (int)str[i]) ? -1 : 1; |
| 6222 | if (id[i]) |
| 6223 | return 1; /* uni is longer */ |
| 6224 | if (str[i]) |
| 6225 | return -1; /* str is longer */ |
| 6226 | return 0; |
| 6227 | } |
| 6228 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6229 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 6230 | PyObject *right, |
| 6231 | int op) |
| 6232 | { |
| 6233 | int result; |
| 6234 | |
| 6235 | result = PyUnicode_Compare(left, right); |
| 6236 | if (result == -1 && PyErr_Occurred()) |
| 6237 | goto onError; |
| 6238 | |
| 6239 | /* Convert the return value to a Boolean */ |
| 6240 | switch (op) { |
| 6241 | case Py_EQ: |
| 6242 | result = (result == 0); |
| 6243 | break; |
| 6244 | case Py_NE: |
| 6245 | result = (result != 0); |
| 6246 | break; |
| 6247 | case Py_LE: |
| 6248 | result = (result <= 0); |
| 6249 | break; |
| 6250 | case Py_GE: |
| 6251 | result = (result >= 0); |
| 6252 | break; |
| 6253 | case Py_LT: |
| 6254 | result = (result == -1); |
| 6255 | break; |
| 6256 | case Py_GT: |
| 6257 | result = (result == 1); |
| 6258 | break; |
| 6259 | } |
| 6260 | return PyBool_FromLong(result); |
| 6261 | |
| 6262 | onError: |
| 6263 | |
| 6264 | /* Standard case |
| 6265 | |
| 6266 | Type errors mean that PyUnicode_FromObject() could not convert |
| 6267 | one of the arguments (usually the right hand side) to Unicode, |
| 6268 | ie. we can't handle the comparison request. However, it is |
| 6269 | possible that the other object knows a comparison method, which |
| 6270 | is why we return Py_NotImplemented to give the other object a |
| 6271 | chance. |
| 6272 | |
| 6273 | */ |
| 6274 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 6275 | PyErr_Clear(); |
| 6276 | Py_INCREF(Py_NotImplemented); |
| 6277 | return Py_NotImplemented; |
| 6278 | } |
| 6279 | if (op != Py_EQ && op != Py_NE) |
| 6280 | return NULL; |
| 6281 | |
| 6282 | /* Equality comparison. |
| 6283 | |
| 6284 | This is a special case: we silence any PyExc_UnicodeDecodeError |
| 6285 | and instead turn it into a PyErr_UnicodeWarning. |
| 6286 | |
| 6287 | */ |
| 6288 | if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) |
| 6289 | return NULL; |
| 6290 | PyErr_Clear(); |
Skip Montanaro | 46fc337 | 2007-08-12 11:44:53 +0000 | [diff] [blame] | 6291 | if (PyErr_WarnEx(PyExc_UnicodeWarning, |
| 6292 | (op == Py_EQ) ? |
| 6293 | "Unicode equal comparison " |
| 6294 | "failed to convert both arguments to Unicode - " |
| 6295 | "interpreting them as being unequal" |
| 6296 | : |
| 6297 | "Unicode unequal comparison " |
| 6298 | "failed to convert both arguments to Unicode - " |
| 6299 | "interpreting them as being unequal", |
| 6300 | 1) < 0) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6301 | return NULL; |
| 6302 | result = (op == Py_NE); |
| 6303 | return PyBool_FromLong(result); |
| 6304 | } |
| 6305 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6306 | int PyUnicode_Contains(PyObject *container, |
| 6307 | PyObject *element) |
| 6308 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6309 | PyObject *str, *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6310 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6311 | |
| 6312 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6313 | sub = PyUnicode_FromObject(element); |
| 6314 | if (!sub) { |
Walter Dörwald | 26e0f51 | 2007-06-12 16:51:31 +0000 | [diff] [blame] | 6315 | PyErr_Format(PyExc_TypeError, |
| 6316 | "'in <string>' requires string as left operand, not %s", |
| 6317 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6318 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6319 | } |
| 6320 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6321 | str = PyUnicode_FromObject(container); |
| 6322 | if (!str) { |
| 6323 | Py_DECREF(sub); |
| 6324 | return -1; |
| 6325 | } |
| 6326 | |
| 6327 | result = stringlib_contains_obj(str, sub); |
| 6328 | |
| 6329 | Py_DECREF(str); |
| 6330 | Py_DECREF(sub); |
| 6331 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6332 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6333 | } |
| 6334 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6335 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 6336 | |
| 6337 | PyObject *PyUnicode_Concat(PyObject *left, |
| 6338 | PyObject *right) |
| 6339 | { |
| 6340 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 6341 | |
Guido van Rossum | 84d79dd | 2007-04-13 02:23:57 +0000 | [diff] [blame] | 6342 | if (PyBytes_Check(left) || PyBytes_Check(right)) |
| 6343 | return PyBytes_Concat(left, right); |
| 6344 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6345 | /* Coerce the two arguments */ |
| 6346 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 6347 | if (u == NULL) |
| 6348 | goto onError; |
| 6349 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 6350 | if (v == NULL) |
| 6351 | goto onError; |
| 6352 | |
| 6353 | /* Shortcuts */ |
| 6354 | if (v == unicode_empty) { |
| 6355 | Py_DECREF(v); |
| 6356 | return (PyObject *)u; |
| 6357 | } |
| 6358 | if (u == unicode_empty) { |
| 6359 | Py_DECREF(u); |
| 6360 | return (PyObject *)v; |
| 6361 | } |
| 6362 | |
| 6363 | /* Concat the two Unicode strings */ |
| 6364 | w = _PyUnicode_New(u->length + v->length); |
| 6365 | if (w == NULL) |
| 6366 | goto onError; |
| 6367 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 6368 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 6369 | |
| 6370 | Py_DECREF(u); |
| 6371 | Py_DECREF(v); |
| 6372 | return (PyObject *)w; |
| 6373 | |
| 6374 | onError: |
| 6375 | Py_XDECREF(u); |
| 6376 | Py_XDECREF(v); |
| 6377 | return NULL; |
| 6378 | } |
| 6379 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6380 | void |
| 6381 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 6382 | { |
| 6383 | PyObject *new; |
| 6384 | if (*pleft == NULL) |
| 6385 | return; |
| 6386 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 6387 | Py_DECREF(*pleft); |
| 6388 | *pleft = NULL; |
| 6389 | return; |
| 6390 | } |
| 6391 | new = PyUnicode_Concat(*pleft, right); |
| 6392 | Py_DECREF(*pleft); |
| 6393 | *pleft = new; |
| 6394 | } |
| 6395 | |
| 6396 | void |
| 6397 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 6398 | { |
| 6399 | PyUnicode_Append(pleft, right); |
| 6400 | Py_XDECREF(right); |
| 6401 | } |
| 6402 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6403 | PyDoc_STRVAR(count__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6404 | "S.count(sub[, start[, end]]) -> int\n\ |
| 6405 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6406 | Return the number of non-overlapping occurrences of substring sub in\n\ |
| 6407 | 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] | 6408 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6409 | |
| 6410 | static PyObject * |
| 6411 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 6412 | { |
| 6413 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6414 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6415 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6416 | PyObject *result; |
| 6417 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6418 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 6419 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6420 | return NULL; |
| 6421 | |
| 6422 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6423 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6424 | if (substring == NULL) |
| 6425 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6426 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6427 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6428 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6429 | result = PyInt_FromSsize_t( |
| 6430 | stringlib_count(self->str + start, end - start, |
| 6431 | substring->str, substring->length) |
| 6432 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6433 | |
| 6434 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6435 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6436 | return result; |
| 6437 | } |
| 6438 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6439 | PyDoc_STRVAR(encode__doc__, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6440 | "S.encode([encoding[,errors]]) -> string or unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6441 | \n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6442 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 6443 | 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] | 6444 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6445 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 6446 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 6447 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6448 | |
| 6449 | static PyObject * |
| 6450 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 6451 | { |
| 6452 | char *encoding = NULL; |
| 6453 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6454 | PyObject *v; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 6455 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6456 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 6457 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6458 | v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6459 | if (v == NULL) |
| 6460 | goto onError; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 6461 | if (!PyBytes_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6462 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 6463 | "encoder did not return a bytes object " |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6464 | "(type=%.400s)", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 6465 | Py_Type(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6466 | Py_DECREF(v); |
| 6467 | return NULL; |
| 6468 | } |
| 6469 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6470 | |
| 6471 | onError: |
| 6472 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6473 | } |
| 6474 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6475 | PyDoc_STRVAR(expandtabs__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6476 | "S.expandtabs([tabsize]) -> unicode\n\ |
| 6477 | \n\ |
| 6478 | 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] | 6479 | 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] | 6480 | |
| 6481 | static PyObject* |
| 6482 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 6483 | { |
| 6484 | Py_UNICODE *e; |
| 6485 | Py_UNICODE *p; |
| 6486 | Py_UNICODE *q; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6487 | Py_ssize_t i, j, old_j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6488 | PyUnicodeObject *u; |
| 6489 | int tabsize = 8; |
| 6490 | |
| 6491 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 6492 | return NULL; |
| 6493 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6494 | /* First pass: determine size of output string */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6495 | i = j = old_j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6496 | e = self->str + self->length; |
| 6497 | for (p = self->str; p < e; p++) |
| 6498 | if (*p == '\t') { |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6499 | if (tabsize > 0) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6500 | j += tabsize - (j % tabsize); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6501 | if (old_j > j) { |
| 6502 | PyErr_SetString(PyExc_OverflowError, |
| 6503 | "new string is too long"); |
| 6504 | return NULL; |
| 6505 | } |
| 6506 | old_j = j; |
| 6507 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6508 | } |
| 6509 | else { |
| 6510 | j++; |
| 6511 | if (*p == '\n' || *p == '\r') { |
| 6512 | i += j; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6513 | old_j = j = 0; |
| 6514 | if (i < 0) { |
| 6515 | PyErr_SetString(PyExc_OverflowError, |
| 6516 | "new string is too long"); |
| 6517 | return NULL; |
| 6518 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6519 | } |
| 6520 | } |
| 6521 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6522 | if ((i + j) < 0) { |
| 6523 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 6524 | return NULL; |
| 6525 | } |
| 6526 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6527 | /* Second pass: create output string and fill it */ |
| 6528 | u = _PyUnicode_New(i + j); |
| 6529 | if (!u) |
| 6530 | return NULL; |
| 6531 | |
| 6532 | j = 0; |
| 6533 | q = u->str; |
| 6534 | |
| 6535 | for (p = self->str; p < e; p++) |
| 6536 | if (*p == '\t') { |
| 6537 | if (tabsize > 0) { |
| 6538 | i = tabsize - (j % tabsize); |
| 6539 | j += i; |
| 6540 | while (i--) |
| 6541 | *q++ = ' '; |
| 6542 | } |
| 6543 | } |
| 6544 | else { |
| 6545 | j++; |
| 6546 | *q++ = *p; |
| 6547 | if (*p == '\n' || *p == '\r') |
| 6548 | j = 0; |
| 6549 | } |
| 6550 | |
| 6551 | return (PyObject*) u; |
| 6552 | } |
| 6553 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6554 | PyDoc_STRVAR(find__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6555 | "S.find(sub [,start [,end]]) -> int\n\ |
| 6556 | \n\ |
| 6557 | 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] | 6558 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6559 | arguments start and end are interpreted as in slice notation.\n\ |
| 6560 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6561 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6562 | |
| 6563 | static PyObject * |
| 6564 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 6565 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6566 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6567 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6568 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6569 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6570 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6571 | if (!PyArg_ParseTuple(args, "O|O&O&:find", &substring, |
| 6572 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6573 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6574 | substring = PyUnicode_FromObject(substring); |
| 6575 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6576 | return NULL; |
| 6577 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6578 | result = stringlib_find_slice( |
| 6579 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6580 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6581 | start, end |
| 6582 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6583 | |
| 6584 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6585 | |
| 6586 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6587 | } |
| 6588 | |
| 6589 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6590 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6591 | { |
| 6592 | if (index < 0 || index >= self->length) { |
| 6593 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 6594 | return NULL; |
| 6595 | } |
| 6596 | |
| 6597 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 6598 | } |
| 6599 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 6600 | /* Believe it or not, this produces the same value for ASCII strings |
| 6601 | as string_hash(). */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6602 | static long |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 6603 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6604 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 6605 | Py_ssize_t len; |
| 6606 | Py_UNICODE *p; |
| 6607 | long x; |
| 6608 | |
| 6609 | if (self->hash != -1) |
| 6610 | return self->hash; |
| 6611 | len = Py_Size(self); |
| 6612 | p = self->str; |
| 6613 | x = *p << 7; |
| 6614 | while (--len >= 0) |
| 6615 | x = (1000003*x) ^ *p++; |
| 6616 | x ^= Py_Size(self); |
| 6617 | if (x == -1) |
| 6618 | x = -2; |
| 6619 | self->hash = x; |
| 6620 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6621 | } |
| 6622 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6623 | PyDoc_STRVAR(index__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6624 | "S.index(sub [,start [,end]]) -> int\n\ |
| 6625 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6626 | 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] | 6627 | |
| 6628 | static PyObject * |
| 6629 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 6630 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6631 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6632 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6633 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6634 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6635 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6636 | if (!PyArg_ParseTuple(args, "O|O&O&:index", &substring, |
| 6637 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6638 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6639 | substring = PyUnicode_FromObject(substring); |
| 6640 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6641 | return NULL; |
| 6642 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6643 | result = stringlib_find_slice( |
| 6644 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6645 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6646 | start, end |
| 6647 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6648 | |
| 6649 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6650 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6651 | if (result < 0) { |
| 6652 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 6653 | return NULL; |
| 6654 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6655 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6656 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6657 | } |
| 6658 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6659 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6660 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6661 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6662 | 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] | 6663 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6664 | |
| 6665 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6666 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6667 | { |
| 6668 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6669 | register const Py_UNICODE *e; |
| 6670 | int cased; |
| 6671 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6672 | /* Shortcut for single character strings */ |
| 6673 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6674 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6675 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6676 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6677 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6678 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6679 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6680 | e = p + PyUnicode_GET_SIZE(self); |
| 6681 | cased = 0; |
| 6682 | for (; p < e; p++) { |
| 6683 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6684 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6685 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6686 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6687 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 6688 | cased = 1; |
| 6689 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6690 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6691 | } |
| 6692 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6693 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6694 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6695 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6696 | 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] | 6697 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6698 | |
| 6699 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6700 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6701 | { |
| 6702 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6703 | register const Py_UNICODE *e; |
| 6704 | int cased; |
| 6705 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6706 | /* Shortcut for single character strings */ |
| 6707 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6708 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6709 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6710 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6711 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6712 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6713 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6714 | e = p + PyUnicode_GET_SIZE(self); |
| 6715 | cased = 0; |
| 6716 | for (; p < e; p++) { |
| 6717 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6718 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6719 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6720 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6721 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 6722 | cased = 1; |
| 6723 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6724 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6725 | } |
| 6726 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6727 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6728 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6729 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6730 | Return True if S is a titlecased string and there is at least one\n\ |
| 6731 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 6732 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 6733 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6734 | |
| 6735 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6736 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6737 | { |
| 6738 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6739 | register const Py_UNICODE *e; |
| 6740 | int cased, previous_is_cased; |
| 6741 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6742 | /* Shortcut for single character strings */ |
| 6743 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6744 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 6745 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6746 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6747 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6748 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6749 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6750 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6751 | e = p + PyUnicode_GET_SIZE(self); |
| 6752 | cased = 0; |
| 6753 | previous_is_cased = 0; |
| 6754 | for (; p < e; p++) { |
| 6755 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6756 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6757 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 6758 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6759 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6760 | previous_is_cased = 1; |
| 6761 | cased = 1; |
| 6762 | } |
| 6763 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 6764 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6765 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6766 | previous_is_cased = 1; |
| 6767 | cased = 1; |
| 6768 | } |
| 6769 | else |
| 6770 | previous_is_cased = 0; |
| 6771 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6772 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6773 | } |
| 6774 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6775 | PyDoc_STRVAR(isspace__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6776 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6777 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6778 | Return True if all characters in S are whitespace\n\ |
| 6779 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6780 | |
| 6781 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6782 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6783 | { |
| 6784 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6785 | register const Py_UNICODE *e; |
| 6786 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6787 | /* Shortcut for single character strings */ |
| 6788 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6789 | Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6790 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6791 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6792 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6793 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6794 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6795 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6796 | e = p + PyUnicode_GET_SIZE(self); |
| 6797 | for (; p < e; p++) { |
| 6798 | if (!Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6799 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6800 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6801 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6802 | } |
| 6803 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6804 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6805 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6806 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6807 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6808 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6809 | |
| 6810 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6811 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6812 | { |
| 6813 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6814 | register const Py_UNICODE *e; |
| 6815 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6816 | /* Shortcut for single character strings */ |
| 6817 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6818 | Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6819 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6820 | |
| 6821 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6822 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6823 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6824 | |
| 6825 | e = p + PyUnicode_GET_SIZE(self); |
| 6826 | for (; p < e; p++) { |
| 6827 | if (!Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6828 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6829 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6830 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6831 | } |
| 6832 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6833 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6834 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6835 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6836 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6837 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6838 | |
| 6839 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6840 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6841 | { |
| 6842 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6843 | register const Py_UNICODE *e; |
| 6844 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6845 | /* Shortcut for single character strings */ |
| 6846 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6847 | Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6848 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6849 | |
| 6850 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6851 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6852 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6853 | |
| 6854 | e = p + PyUnicode_GET_SIZE(self); |
| 6855 | for (; p < e; p++) { |
| 6856 | if (!Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6857 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6858 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6859 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6860 | } |
| 6861 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6862 | PyDoc_STRVAR(isdecimal__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6863 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6864 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6865 | 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] | 6866 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6867 | |
| 6868 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6869 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6870 | { |
| 6871 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6872 | register const Py_UNICODE *e; |
| 6873 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6874 | /* Shortcut for single character strings */ |
| 6875 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6876 | Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6877 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6878 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6879 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6880 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6881 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6882 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6883 | e = p + PyUnicode_GET_SIZE(self); |
| 6884 | for (; p < e; p++) { |
| 6885 | if (!Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6886 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6887 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6888 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6889 | } |
| 6890 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6891 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6892 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6893 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6894 | Return True if all characters in S are digits\n\ |
| 6895 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6896 | |
| 6897 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6898 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6899 | { |
| 6900 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6901 | register const Py_UNICODE *e; |
| 6902 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6903 | /* Shortcut for single character strings */ |
| 6904 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6905 | Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6906 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6907 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6908 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6909 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6910 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6911 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6912 | e = p + PyUnicode_GET_SIZE(self); |
| 6913 | for (; p < e; p++) { |
| 6914 | if (!Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6915 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6916 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6917 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6918 | } |
| 6919 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6920 | PyDoc_STRVAR(isnumeric__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6921 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6922 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6923 | 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] | 6924 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6925 | |
| 6926 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6927 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6928 | { |
| 6929 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6930 | register const Py_UNICODE *e; |
| 6931 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6932 | /* Shortcut for single character strings */ |
| 6933 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6934 | Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6935 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6936 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6937 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6938 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6939 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6940 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6941 | e = p + PyUnicode_GET_SIZE(self); |
| 6942 | for (; p < e; p++) { |
| 6943 | if (!Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6944 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6945 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6946 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6947 | } |
| 6948 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 6949 | int |
| 6950 | PyUnicode_IsIdentifier(PyObject *self) |
| 6951 | { |
| 6952 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE((PyUnicodeObject*)self); |
| 6953 | register const Py_UNICODE *e; |
| 6954 | |
| 6955 | /* Special case for empty strings */ |
| 6956 | if (PyUnicode_GET_SIZE(self) == 0) |
| 6957 | return 0; |
| 6958 | |
| 6959 | /* PEP 3131 says that the first character must be in |
| 6960 | XID_Start and subsequent characters in XID_Continue, |
| 6961 | and for the ASCII range, the 2.x rules apply (i.e |
| 6962 | start with letters and underscore, continue with |
| 6963 | letters, digits, underscore). However, given the current |
| 6964 | definition of XID_Start and XID_Continue, it is sufficient |
| 6965 | to check just for these, except that _ must be allowed |
| 6966 | as starting an identifier. */ |
| 6967 | if (!_PyUnicode_IsXidStart(*p) && *p != 0x5F /* LOW LINE */) |
| 6968 | return 0; |
| 6969 | |
| 6970 | e = p + PyUnicode_GET_SIZE(self); |
| 6971 | for (p++; p < e; p++) { |
| 6972 | if (!_PyUnicode_IsXidContinue(*p)) |
| 6973 | return 0; |
| 6974 | } |
| 6975 | return 1; |
| 6976 | } |
| 6977 | |
| 6978 | PyDoc_STRVAR(isidentifier__doc__, |
| 6979 | "S.isidentifier() -> bool\n\ |
| 6980 | \n\ |
| 6981 | Return True if S is a valid identifier according\n\ |
| 6982 | to the language definition."); |
| 6983 | |
| 6984 | static PyObject* |
| 6985 | unicode_isidentifier(PyObject *self) |
| 6986 | { |
| 6987 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 6988 | } |
| 6989 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6990 | PyDoc_STRVAR(join__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6991 | "S.join(sequence) -> unicode\n\ |
| 6992 | \n\ |
| 6993 | 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] | 6994 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6995 | |
| 6996 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6997 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6998 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6999 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7000 | } |
| 7001 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7002 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7003 | unicode_length(PyUnicodeObject *self) |
| 7004 | { |
| 7005 | return self->length; |
| 7006 | } |
| 7007 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7008 | PyDoc_STRVAR(ljust__doc__, |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 7009 | "S.ljust(width[, fillchar]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7010 | \n\ |
| 7011 | 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] | 7012 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7013 | |
| 7014 | static PyObject * |
| 7015 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 7016 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7017 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7018 | Py_UNICODE fillchar = ' '; |
| 7019 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7020 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7021 | return NULL; |
| 7022 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7023 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7024 | Py_INCREF(self); |
| 7025 | return (PyObject*) self; |
| 7026 | } |
| 7027 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7028 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7029 | } |
| 7030 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7031 | PyDoc_STRVAR(lower__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7032 | "S.lower() -> unicode\n\ |
| 7033 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7034 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7035 | |
| 7036 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7037 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7038 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7039 | return fixup(self, fixlower); |
| 7040 | } |
| 7041 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7042 | #define LEFTSTRIP 0 |
| 7043 | #define RIGHTSTRIP 1 |
| 7044 | #define BOTHSTRIP 2 |
| 7045 | |
| 7046 | /* Arrays indexed by above */ |
| 7047 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 7048 | |
| 7049 | #define STRIPNAME(i) (stripformat[i]+3) |
| 7050 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7051 | /* externally visible for str.strip(unicode) */ |
| 7052 | PyObject * |
| 7053 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 7054 | { |
| 7055 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7056 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7057 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7058 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 7059 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7060 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7061 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
| 7062 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7063 | i = 0; |
| 7064 | if (striptype != RIGHTSTRIP) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7065 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7066 | i++; |
| 7067 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7068 | } |
| 7069 | |
| 7070 | j = len; |
| 7071 | if (striptype != LEFTSTRIP) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7072 | do { |
| 7073 | j--; |
| 7074 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 7075 | j++; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7076 | } |
| 7077 | |
| 7078 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7079 | Py_INCREF(self); |
| 7080 | return (PyObject*)self; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7081 | } |
| 7082 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7083 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7084 | } |
| 7085 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7086 | |
| 7087 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7088 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7089 | { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7090 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7091 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7092 | |
| 7093 | i = 0; |
| 7094 | if (striptype != RIGHTSTRIP) { |
| 7095 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 7096 | i++; |
| 7097 | } |
| 7098 | } |
| 7099 | |
| 7100 | j = len; |
| 7101 | if (striptype != LEFTSTRIP) { |
| 7102 | do { |
| 7103 | j--; |
| 7104 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 7105 | j++; |
| 7106 | } |
| 7107 | |
| 7108 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 7109 | Py_INCREF(self); |
| 7110 | return (PyObject*)self; |
| 7111 | } |
| 7112 | else |
| 7113 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7114 | } |
| 7115 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7116 | |
| 7117 | static PyObject * |
| 7118 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 7119 | { |
| 7120 | PyObject *sep = NULL; |
| 7121 | |
| 7122 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 7123 | return NULL; |
| 7124 | |
| 7125 | if (sep != NULL && sep != Py_None) { |
| 7126 | if (PyUnicode_Check(sep)) |
| 7127 | return _PyUnicode_XStrip(self, striptype, sep); |
| 7128 | else if (PyString_Check(sep)) { |
| 7129 | PyObject *res; |
| 7130 | sep = PyUnicode_FromObject(sep); |
| 7131 | if (sep==NULL) |
| 7132 | return NULL; |
| 7133 | res = _PyUnicode_XStrip(self, striptype, sep); |
| 7134 | Py_DECREF(sep); |
| 7135 | return res; |
| 7136 | } |
| 7137 | else { |
| 7138 | PyErr_Format(PyExc_TypeError, |
| 7139 | "%s arg must be None, unicode or str", |
| 7140 | STRIPNAME(striptype)); |
| 7141 | return NULL; |
| 7142 | } |
| 7143 | } |
| 7144 | |
| 7145 | return do_strip(self, striptype); |
| 7146 | } |
| 7147 | |
| 7148 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7149 | PyDoc_STRVAR(strip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7150 | "S.strip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7151 | \n\ |
| 7152 | Return a copy of the string S with leading and trailing\n\ |
| 7153 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7154 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7155 | 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] | 7156 | |
| 7157 | static PyObject * |
| 7158 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 7159 | { |
| 7160 | if (PyTuple_GET_SIZE(args) == 0) |
| 7161 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 7162 | else |
| 7163 | return do_argstrip(self, BOTHSTRIP, args); |
| 7164 | } |
| 7165 | |
| 7166 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7167 | PyDoc_STRVAR(lstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7168 | "S.lstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7169 | \n\ |
| 7170 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7171 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7172 | 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] | 7173 | |
| 7174 | static PyObject * |
| 7175 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 7176 | { |
| 7177 | if (PyTuple_GET_SIZE(args) == 0) |
| 7178 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 7179 | else |
| 7180 | return do_argstrip(self, LEFTSTRIP, args); |
| 7181 | } |
| 7182 | |
| 7183 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7184 | PyDoc_STRVAR(rstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7185 | "S.rstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7186 | \n\ |
| 7187 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7188 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7189 | 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] | 7190 | |
| 7191 | static PyObject * |
| 7192 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 7193 | { |
| 7194 | if (PyTuple_GET_SIZE(args) == 0) |
| 7195 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 7196 | else |
| 7197 | return do_argstrip(self, RIGHTSTRIP, args); |
| 7198 | } |
| 7199 | |
| 7200 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7201 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7202 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7203 | { |
| 7204 | PyUnicodeObject *u; |
| 7205 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7206 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7207 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7208 | |
| 7209 | if (len < 0) |
| 7210 | len = 0; |
| 7211 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7212 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7213 | /* no repeat, return original string */ |
| 7214 | Py_INCREF(str); |
| 7215 | return (PyObject*) str; |
| 7216 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7217 | |
| 7218 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 7219 | * needed doesn't overflow size_t |
| 7220 | */ |
| 7221 | nchars = len * str->length; |
| 7222 | if (len && nchars / len != str->length) { |
| 7223 | PyErr_SetString(PyExc_OverflowError, |
| 7224 | "repeated string is too long"); |
| 7225 | return NULL; |
| 7226 | } |
| 7227 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 7228 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 7229 | PyErr_SetString(PyExc_OverflowError, |
| 7230 | "repeated string is too long"); |
| 7231 | return NULL; |
| 7232 | } |
| 7233 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7234 | if (!u) |
| 7235 | return NULL; |
| 7236 | |
| 7237 | p = u->str; |
| 7238 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7239 | if (str->length == 1 && len > 0) { |
| 7240 | Py_UNICODE_FILL(p, str->str[0], len); |
| 7241 | } else { |
| 7242 | Py_ssize_t done = 0; /* number of characters copied this far */ |
| 7243 | if (done < nchars) { |
| 7244 | Py_UNICODE_COPY(p, str->str, str->length); |
| 7245 | done = str->length; |
| 7246 | } |
| 7247 | while (done < nchars) { |
| 7248 | int n = (done <= nchars-done) ? done : nchars-done; |
| 7249 | Py_UNICODE_COPY(p+done, p, n); |
| 7250 | done += n; |
| 7251 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7252 | } |
| 7253 | |
| 7254 | return (PyObject*) u; |
| 7255 | } |
| 7256 | |
| 7257 | PyObject *PyUnicode_Replace(PyObject *obj, |
| 7258 | PyObject *subobj, |
| 7259 | PyObject *replobj, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7260 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7261 | { |
| 7262 | PyObject *self; |
| 7263 | PyObject *str1; |
| 7264 | PyObject *str2; |
| 7265 | PyObject *result; |
| 7266 | |
| 7267 | self = PyUnicode_FromObject(obj); |
| 7268 | if (self == NULL) |
| 7269 | return NULL; |
| 7270 | str1 = PyUnicode_FromObject(subobj); |
| 7271 | if (str1 == NULL) { |
| 7272 | Py_DECREF(self); |
| 7273 | return NULL; |
| 7274 | } |
| 7275 | str2 = PyUnicode_FromObject(replobj); |
| 7276 | if (str2 == NULL) { |
| 7277 | Py_DECREF(self); |
| 7278 | Py_DECREF(str1); |
| 7279 | return NULL; |
| 7280 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7281 | result = replace((PyUnicodeObject *)self, |
| 7282 | (PyUnicodeObject *)str1, |
| 7283 | (PyUnicodeObject *)str2, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7284 | maxcount); |
| 7285 | Py_DECREF(self); |
| 7286 | Py_DECREF(str1); |
| 7287 | Py_DECREF(str2); |
| 7288 | return result; |
| 7289 | } |
| 7290 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7291 | PyDoc_STRVAR(replace__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7292 | "S.replace (old, new[, maxsplit]) -> unicode\n\ |
| 7293 | \n\ |
| 7294 | Return a copy of S with all occurrences of substring\n\ |
| 7295 | 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] | 7296 | given, only the first maxsplit occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7297 | |
| 7298 | static PyObject* |
| 7299 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 7300 | { |
| 7301 | PyUnicodeObject *str1; |
| 7302 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7303 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7304 | PyObject *result; |
| 7305 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7306 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7307 | return NULL; |
| 7308 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 7309 | if (str1 == NULL) |
| 7310 | return NULL; |
| 7311 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7312 | if (str2 == NULL) { |
| 7313 | Py_DECREF(str1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7314 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7315 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7316 | |
| 7317 | result = replace(self, str1, str2, maxcount); |
| 7318 | |
| 7319 | Py_DECREF(str1); |
| 7320 | Py_DECREF(str2); |
| 7321 | return result; |
| 7322 | } |
| 7323 | |
| 7324 | static |
| 7325 | PyObject *unicode_repr(PyObject *unicode) |
| 7326 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7327 | PyObject *repr; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7328 | Py_UNICODE *p; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7329 | Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); |
| 7330 | Py_ssize_t size = PyUnicode_GET_SIZE(unicode); |
| 7331 | |
| 7332 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 7333 | better to choose a different scheme. Perhaps scan the |
| 7334 | first N-chars of the string and allocate based on that size. |
| 7335 | */ |
| 7336 | /* Initial allocation is based on the longest-possible unichr |
| 7337 | escape. |
| 7338 | |
| 7339 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 7340 | unichr, so in this case it's the longest unichr escape. In |
| 7341 | narrow (UTF-16) builds this is five chars per source unichr |
| 7342 | since there are two unichrs in the surrogate pair, so in narrow |
| 7343 | (UTF-16) builds it's not the longest unichr escape. |
| 7344 | |
| 7345 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 7346 | so in the narrow (UTF-16) build case it's the longest unichr |
| 7347 | escape. |
| 7348 | */ |
| 7349 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7350 | repr = PyUnicode_FromUnicode(NULL, |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7351 | 2 /* quotes */ |
| 7352 | #ifdef Py_UNICODE_WIDE |
| 7353 | + 10*size |
| 7354 | #else |
| 7355 | + 6*size |
| 7356 | #endif |
| 7357 | + 1); |
| 7358 | if (repr == NULL) |
| 7359 | return NULL; |
| 7360 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7361 | p = PyUnicode_AS_UNICODE(repr); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7362 | |
| 7363 | /* Add quote */ |
| 7364 | *p++ = (findchar(s, size, '\'') && |
| 7365 | !findchar(s, size, '"')) ? '"' : '\''; |
| 7366 | while (size-- > 0) { |
| 7367 | Py_UNICODE ch = *s++; |
| 7368 | |
| 7369 | /* Escape quotes and backslashes */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7370 | if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7371 | *p++ = '\\'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7372 | *p++ = ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7373 | continue; |
| 7374 | } |
| 7375 | |
| 7376 | #ifdef Py_UNICODE_WIDE |
| 7377 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 7378 | else if (ch >= 0x10000) { |
| 7379 | *p++ = '\\'; |
| 7380 | *p++ = 'U'; |
| 7381 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 7382 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 7383 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 7384 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 7385 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 7386 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 7387 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 7388 | *p++ = hexdigits[ch & 0x0000000F]; |
| 7389 | continue; |
| 7390 | } |
| 7391 | #else |
| 7392 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 7393 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 7394 | Py_UNICODE ch2; |
| 7395 | Py_UCS4 ucs; |
| 7396 | |
| 7397 | ch2 = *s++; |
| 7398 | size--; |
| 7399 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 7400 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 7401 | *p++ = '\\'; |
| 7402 | *p++ = 'U'; |
| 7403 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 7404 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 7405 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 7406 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 7407 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 7408 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 7409 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 7410 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 7411 | continue; |
| 7412 | } |
| 7413 | /* Fall through: isolated surrogates are copied as-is */ |
| 7414 | s--; |
| 7415 | size++; |
| 7416 | } |
| 7417 | #endif |
| 7418 | |
| 7419 | /* Map 16-bit characters to '\uxxxx' */ |
| 7420 | if (ch >= 256) { |
| 7421 | *p++ = '\\'; |
| 7422 | *p++ = 'u'; |
| 7423 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 7424 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 7425 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7426 | *p++ = hexdigits[ch & 0x000F]; |
| 7427 | } |
| 7428 | |
| 7429 | /* Map special whitespace to '\t', \n', '\r' */ |
| 7430 | else if (ch == '\t') { |
| 7431 | *p++ = '\\'; |
| 7432 | *p++ = 't'; |
| 7433 | } |
| 7434 | else if (ch == '\n') { |
| 7435 | *p++ = '\\'; |
| 7436 | *p++ = 'n'; |
| 7437 | } |
| 7438 | else if (ch == '\r') { |
| 7439 | *p++ = '\\'; |
| 7440 | *p++ = 'r'; |
| 7441 | } |
| 7442 | |
| 7443 | /* Map non-printable US ASCII to '\xhh' */ |
| 7444 | else if (ch < ' ' || ch >= 0x7F) { |
| 7445 | *p++ = '\\'; |
| 7446 | *p++ = 'x'; |
| 7447 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7448 | *p++ = hexdigits[ch & 0x000F]; |
| 7449 | } |
| 7450 | |
| 7451 | /* Copy everything else as-is */ |
| 7452 | else |
| 7453 | *p++ = (char) ch; |
| 7454 | } |
| 7455 | /* Add quote */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7456 | *p++ = PyUnicode_AS_UNICODE(repr)[0]; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7457 | |
| 7458 | *p = '\0'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7459 | _PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7460 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7461 | } |
| 7462 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7463 | PyDoc_STRVAR(rfind__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7464 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 7465 | \n\ |
| 7466 | 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] | 7467 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7468 | arguments start and end are interpreted as in slice notation.\n\ |
| 7469 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7470 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7471 | |
| 7472 | static PyObject * |
| 7473 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 7474 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7475 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7476 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7477 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7478 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7479 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7480 | if (!PyArg_ParseTuple(args, "O|O&O&:rfind", &substring, |
| 7481 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7482 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7483 | substring = PyUnicode_FromObject(substring); |
| 7484 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7485 | return NULL; |
| 7486 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7487 | result = stringlib_rfind_slice( |
| 7488 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7489 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7490 | start, end |
| 7491 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7492 | |
| 7493 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7494 | |
| 7495 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7496 | } |
| 7497 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7498 | PyDoc_STRVAR(rindex__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7499 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 7500 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7501 | 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] | 7502 | |
| 7503 | static PyObject * |
| 7504 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 7505 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7506 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7507 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7508 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7509 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7510 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7511 | if (!PyArg_ParseTuple(args, "O|O&O&:rindex", &substring, |
| 7512 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7513 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7514 | substring = PyUnicode_FromObject(substring); |
| 7515 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7516 | return NULL; |
| 7517 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7518 | result = stringlib_rfind_slice( |
| 7519 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7520 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7521 | start, end |
| 7522 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7523 | |
| 7524 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7525 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7526 | if (result < 0) { |
| 7527 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7528 | return NULL; |
| 7529 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7530 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7531 | } |
| 7532 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7533 | PyDoc_STRVAR(rjust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7534 | "S.rjust(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7535 | \n\ |
| 7536 | 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] | 7537 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7538 | |
| 7539 | static PyObject * |
| 7540 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 7541 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7542 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7543 | Py_UNICODE fillchar = ' '; |
| 7544 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7545 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7546 | return NULL; |
| 7547 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7548 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7549 | Py_INCREF(self); |
| 7550 | return (PyObject*) self; |
| 7551 | } |
| 7552 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7553 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7554 | } |
| 7555 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7556 | PyObject *PyUnicode_Split(PyObject *s, |
| 7557 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7558 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7559 | { |
| 7560 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7561 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7562 | s = PyUnicode_FromObject(s); |
| 7563 | if (s == NULL) |
| 7564 | return NULL; |
| 7565 | if (sep != NULL) { |
| 7566 | sep = PyUnicode_FromObject(sep); |
| 7567 | if (sep == NULL) { |
| 7568 | Py_DECREF(s); |
| 7569 | return NULL; |
| 7570 | } |
| 7571 | } |
| 7572 | |
| 7573 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7574 | |
| 7575 | Py_DECREF(s); |
| 7576 | Py_XDECREF(sep); |
| 7577 | return result; |
| 7578 | } |
| 7579 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7580 | PyDoc_STRVAR(split__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7581 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 7582 | \n\ |
| 7583 | Return a list of the words in S, using sep as the\n\ |
| 7584 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Thomas Heller | ca0d2cb | 2004-09-15 11:41:32 +0000 | [diff] [blame] | 7585 | 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] | 7586 | any whitespace string is a separator."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7587 | |
| 7588 | static PyObject* |
| 7589 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 7590 | { |
| 7591 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7592 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7593 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7594 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7595 | return NULL; |
| 7596 | |
| 7597 | if (substring == Py_None) |
| 7598 | return split(self, NULL, maxcount); |
| 7599 | else if (PyUnicode_Check(substring)) |
| 7600 | return split(self, (PyUnicodeObject *)substring, maxcount); |
| 7601 | else |
| 7602 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
| 7603 | } |
| 7604 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7605 | PyObject * |
| 7606 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 7607 | { |
| 7608 | PyObject* str_obj; |
| 7609 | PyObject* sep_obj; |
| 7610 | PyObject* out; |
| 7611 | |
| 7612 | str_obj = PyUnicode_FromObject(str_in); |
| 7613 | if (!str_obj) |
| 7614 | return NULL; |
| 7615 | sep_obj = PyUnicode_FromObject(sep_in); |
| 7616 | if (!sep_obj) { |
| 7617 | Py_DECREF(str_obj); |
| 7618 | return NULL; |
| 7619 | } |
| 7620 | |
| 7621 | out = stringlib_partition( |
| 7622 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7623 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7624 | ); |
| 7625 | |
| 7626 | Py_DECREF(sep_obj); |
| 7627 | Py_DECREF(str_obj); |
| 7628 | |
| 7629 | return out; |
| 7630 | } |
| 7631 | |
| 7632 | |
| 7633 | PyObject * |
| 7634 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 7635 | { |
| 7636 | PyObject* str_obj; |
| 7637 | PyObject* sep_obj; |
| 7638 | PyObject* out; |
| 7639 | |
| 7640 | str_obj = PyUnicode_FromObject(str_in); |
| 7641 | if (!str_obj) |
| 7642 | return NULL; |
| 7643 | sep_obj = PyUnicode_FromObject(sep_in); |
| 7644 | if (!sep_obj) { |
| 7645 | Py_DECREF(str_obj); |
| 7646 | return NULL; |
| 7647 | } |
| 7648 | |
| 7649 | out = stringlib_rpartition( |
| 7650 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7651 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7652 | ); |
| 7653 | |
| 7654 | Py_DECREF(sep_obj); |
| 7655 | Py_DECREF(str_obj); |
| 7656 | |
| 7657 | return out; |
| 7658 | } |
| 7659 | |
| 7660 | PyDoc_STRVAR(partition__doc__, |
| 7661 | "S.partition(sep) -> (head, sep, tail)\n\ |
| 7662 | \n\ |
| 7663 | Searches for the separator sep in S, and returns the part before it,\n\ |
| 7664 | the separator itself, and the part after it. If the separator is not\n\ |
| 7665 | found, returns S and two empty strings."); |
| 7666 | |
| 7667 | static PyObject* |
| 7668 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 7669 | { |
| 7670 | return PyUnicode_Partition((PyObject *)self, separator); |
| 7671 | } |
| 7672 | |
| 7673 | PyDoc_STRVAR(rpartition__doc__, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7674 | "S.rpartition(sep) -> (tail, sep, head)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7675 | \n\ |
| 7676 | Searches for the separator sep in S, starting at the end of S, and returns\n\ |
| 7677 | 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] | 7678 | separator is not found, returns two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7679 | |
| 7680 | static PyObject* |
| 7681 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 7682 | { |
| 7683 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 7684 | } |
| 7685 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7686 | PyObject *PyUnicode_RSplit(PyObject *s, |
| 7687 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7688 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7689 | { |
| 7690 | PyObject *result; |
| 7691 | |
| 7692 | s = PyUnicode_FromObject(s); |
| 7693 | if (s == NULL) |
| 7694 | return NULL; |
| 7695 | if (sep != NULL) { |
| 7696 | sep = PyUnicode_FromObject(sep); |
| 7697 | if (sep == NULL) { |
| 7698 | Py_DECREF(s); |
| 7699 | return NULL; |
| 7700 | } |
| 7701 | } |
| 7702 | |
| 7703 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7704 | |
| 7705 | Py_DECREF(s); |
| 7706 | Py_XDECREF(sep); |
| 7707 | return result; |
| 7708 | } |
| 7709 | |
| 7710 | PyDoc_STRVAR(rsplit__doc__, |
| 7711 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
| 7712 | \n\ |
| 7713 | Return a list of the words in S, using sep as the\n\ |
| 7714 | delimiter string, starting at the end of the string and\n\ |
| 7715 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 7716 | splits are done. If sep is not specified, any whitespace string\n\ |
| 7717 | is a separator."); |
| 7718 | |
| 7719 | static PyObject* |
| 7720 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 7721 | { |
| 7722 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7723 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7724 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7725 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7726 | return NULL; |
| 7727 | |
| 7728 | if (substring == Py_None) |
| 7729 | return rsplit(self, NULL, maxcount); |
| 7730 | else if (PyUnicode_Check(substring)) |
| 7731 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
| 7732 | else |
| 7733 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
| 7734 | } |
| 7735 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7736 | PyDoc_STRVAR(splitlines__doc__, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7737 | "S.splitlines([keepends]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7738 | \n\ |
| 7739 | 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] | 7740 | 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] | 7741 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7742 | |
| 7743 | static PyObject* |
| 7744 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 7745 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7746 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7747 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7748 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7749 | return NULL; |
| 7750 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7751 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7752 | } |
| 7753 | |
| 7754 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 7755 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7756 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 7757 | if (PyUnicode_CheckExact(self)) { |
| 7758 | Py_INCREF(self); |
| 7759 | return self; |
| 7760 | } else |
| 7761 | /* Subtype -- return genuine unicode string with the same value. */ |
| 7762 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), |
| 7763 | PyUnicode_GET_SIZE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7764 | } |
| 7765 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7766 | PyDoc_STRVAR(swapcase__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7767 | "S.swapcase() -> unicode\n\ |
| 7768 | \n\ |
| 7769 | 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] | 7770 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7771 | |
| 7772 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7773 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7774 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7775 | return fixup(self, fixswapcase); |
| 7776 | } |
| 7777 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7778 | PyDoc_STRVAR(translate__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7779 | "S.translate(table) -> unicode\n\ |
| 7780 | \n\ |
| 7781 | Return a copy of the string S, where all characters have been mapped\n\ |
| 7782 | 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] | 7783 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ |
| 7784 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 7785 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7786 | |
| 7787 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7788 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7789 | { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7790 | return PyUnicode_TranslateCharmap(self->str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7791 | self->length, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7792 | table, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7793 | "ignore"); |
| 7794 | } |
| 7795 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7796 | PyDoc_STRVAR(upper__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7797 | "S.upper() -> unicode\n\ |
| 7798 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7799 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7800 | |
| 7801 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7802 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7803 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7804 | return fixup(self, fixupper); |
| 7805 | } |
| 7806 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7807 | PyDoc_STRVAR(zfill__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7808 | "S.zfill(width) -> unicode\n\ |
| 7809 | \n\ |
| 7810 | 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] | 7811 | of the specified width. The string x is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7812 | |
| 7813 | static PyObject * |
| 7814 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 7815 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7816 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7817 | PyUnicodeObject *u; |
| 7818 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7819 | Py_ssize_t width; |
| 7820 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7821 | return NULL; |
| 7822 | |
| 7823 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 7824 | if (PyUnicode_CheckExact(self)) { |
| 7825 | Py_INCREF(self); |
| 7826 | return (PyObject*) self; |
| 7827 | } |
| 7828 | else |
| 7829 | return PyUnicode_FromUnicode( |
| 7830 | PyUnicode_AS_UNICODE(self), |
| 7831 | PyUnicode_GET_SIZE(self) |
| 7832 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7833 | } |
| 7834 | |
| 7835 | fill = width - self->length; |
| 7836 | |
| 7837 | u = pad(self, fill, 0, '0'); |
| 7838 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 7839 | if (u == NULL) |
| 7840 | return NULL; |
| 7841 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7842 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 7843 | /* move sign to beginning of string */ |
| 7844 | u->str[0] = u->str[fill]; |
| 7845 | u->str[fill] = '0'; |
| 7846 | } |
| 7847 | |
| 7848 | return (PyObject*) u; |
| 7849 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7850 | |
| 7851 | #if 0 |
| 7852 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7853 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7854 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7855 | return PyInt_FromLong(unicode_freelist_size); |
| 7856 | } |
| 7857 | #endif |
| 7858 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7859 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7860 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7861 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7862 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 7863 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7864 | With optional end, stop comparing S at that position.\n\ |
| 7865 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7866 | |
| 7867 | static PyObject * |
| 7868 | unicode_startswith(PyUnicodeObject *self, |
| 7869 | PyObject *args) |
| 7870 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7871 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7872 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7873 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7874 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7875 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7876 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7877 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7878 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7879 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7880 | if (PyTuple_Check(subobj)) { |
| 7881 | Py_ssize_t i; |
| 7882 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 7883 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 7884 | PyTuple_GET_ITEM(subobj, i)); |
| 7885 | if (substring == NULL) |
| 7886 | return NULL; |
| 7887 | result = tailmatch(self, substring, start, end, -1); |
| 7888 | Py_DECREF(substring); |
| 7889 | if (result) { |
| 7890 | Py_RETURN_TRUE; |
| 7891 | } |
| 7892 | } |
| 7893 | /* nothing matched */ |
| 7894 | Py_RETURN_FALSE; |
| 7895 | } |
| 7896 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7897 | if (substring == NULL) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7898 | return NULL; |
| 7899 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7900 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7901 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7902 | } |
| 7903 | |
| 7904 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7905 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7906 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7907 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7908 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 7909 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7910 | With optional end, stop comparing S at that position.\n\ |
| 7911 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7912 | |
| 7913 | static PyObject * |
| 7914 | unicode_endswith(PyUnicodeObject *self, |
| 7915 | PyObject *args) |
| 7916 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7917 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7918 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7919 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7920 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7921 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7922 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7923 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 7924 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7925 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7926 | if (PyTuple_Check(subobj)) { |
| 7927 | Py_ssize_t i; |
| 7928 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 7929 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 7930 | PyTuple_GET_ITEM(subobj, i)); |
| 7931 | if (substring == NULL) |
| 7932 | return NULL; |
| 7933 | result = tailmatch(self, substring, start, end, +1); |
| 7934 | Py_DECREF(substring); |
| 7935 | if (result) { |
| 7936 | Py_RETURN_TRUE; |
| 7937 | } |
| 7938 | } |
| 7939 | Py_RETURN_FALSE; |
| 7940 | } |
| 7941 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7942 | if (substring == NULL) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7943 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7944 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7945 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7946 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7947 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7948 | } |
| 7949 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 7950 | #include "stringlib/string_format.h" |
| 7951 | |
| 7952 | PyDoc_STRVAR(format__doc__, |
| 7953 | "S.format(*args, **kwargs) -> unicode\n\ |
| 7954 | \n\ |
| 7955 | "); |
| 7956 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 7957 | PyDoc_STRVAR(p_format__doc__, |
| 7958 | "S.__format__(format_spec) -> unicode\n\ |
| 7959 | \n\ |
| 7960 | "); |
| 7961 | |
| 7962 | static PyObject * |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 7963 | unicode_getnewargs(PyUnicodeObject *v) |
| 7964 | { |
| 7965 | return Py_BuildValue("(u#)", v->str, v->length); |
| 7966 | } |
| 7967 | |
| 7968 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7969 | static PyMethodDef unicode_methods[] = { |
| 7970 | |
| 7971 | /* Order is according to common usage: often used methods should |
| 7972 | appear first, since lookup is done sequentially. */ |
| 7973 | |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7974 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
| 7975 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 7976 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7977 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7978 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 7979 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 7980 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 7981 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 7982 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 7983 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 7984 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7985 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7986 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 7987 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 7988 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7989 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7990 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 7991 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 7992 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7993 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7994 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7995 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7996 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7997 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 7998 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 7999 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 8000 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 8001 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 8002 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 8003 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 8004 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 8005 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 8006 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 8007 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 8008 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 8009 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 8010 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8011 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8012 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 8013 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
| 8014 | {"__format__", (PyCFunction) unicode_unicode__format__, METH_VARARGS, p_format__doc__}, |
Eric Smith | f6db409 | 2007-08-27 23:52:26 +0000 | [diff] [blame] | 8015 | {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
| 8016 | {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8017 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8018 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8019 | #endif |
| 8020 | |
| 8021 | #if 0 |
| 8022 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8023 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8024 | #endif |
| 8025 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8026 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8027 | {NULL, NULL} |
| 8028 | }; |
| 8029 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8030 | static PyObject * |
| 8031 | unicode_mod(PyObject *v, PyObject *w) |
| 8032 | { |
| 8033 | if (!PyUnicode_Check(v)) { |
| 8034 | Py_INCREF(Py_NotImplemented); |
| 8035 | return Py_NotImplemented; |
| 8036 | } |
| 8037 | return PyUnicode_Format(v, w); |
| 8038 | } |
| 8039 | |
| 8040 | static PyNumberMethods unicode_as_number = { |
| 8041 | 0, /*nb_add*/ |
| 8042 | 0, /*nb_subtract*/ |
| 8043 | 0, /*nb_multiply*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8044 | unicode_mod, /*nb_remainder*/ |
| 8045 | }; |
| 8046 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8047 | static PySequenceMethods unicode_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8048 | (lenfunc) unicode_length, /* sq_length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8049 | PyUnicode_Concat, /* sq_concat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8050 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 8051 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
Thomas Wouters | d2cf20e | 2007-08-30 22:57:53 +0000 | [diff] [blame] | 8052 | 0, /* sq_slice */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8053 | 0, /* sq_ass_item */ |
| 8054 | 0, /* sq_ass_slice */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8055 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8056 | }; |
| 8057 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8058 | static PyObject* |
| 8059 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 8060 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8061 | if (PyIndex_Check(item)) { |
| 8062 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8063 | if (i == -1 && PyErr_Occurred()) |
| 8064 | return NULL; |
| 8065 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8066 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8067 | return unicode_getitem(self, i); |
| 8068 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8069 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8070 | Py_UNICODE* source_buf; |
| 8071 | Py_UNICODE* result_buf; |
| 8072 | PyObject* result; |
| 8073 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8074 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8075 | &start, &stop, &step, &slicelength) < 0) { |
| 8076 | return NULL; |
| 8077 | } |
| 8078 | |
| 8079 | if (slicelength <= 0) { |
| 8080 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 8081 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 8082 | PyUnicode_CheckExact(self)) { |
| 8083 | Py_INCREF(self); |
| 8084 | return (PyObject *)self; |
| 8085 | } else if (step == 1) { |
| 8086 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8087 | } else { |
| 8088 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8089 | result_buf = (Py_UNICODE *)PyMem_MALLOC(slicelength* |
| 8090 | sizeof(Py_UNICODE)); |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8091 | |
| 8092 | if (result_buf == NULL) |
| 8093 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8094 | |
| 8095 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 8096 | result_buf[i] = source_buf[cur]; |
| 8097 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8098 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8099 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
| 8100 | PyMem_FREE(result_buf); |
| 8101 | return result; |
| 8102 | } |
| 8103 | } else { |
| 8104 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 8105 | return NULL; |
| 8106 | } |
| 8107 | } |
| 8108 | |
| 8109 | static PyMappingMethods unicode_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8110 | (lenfunc)unicode_length, /* mp_length */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8111 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 8112 | (objobjargproc)0, /* mp_ass_subscript */ |
| 8113 | }; |
| 8114 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8115 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8116 | /* Helpers for PyUnicode_Format() */ |
| 8117 | |
| 8118 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8119 | 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] | 8120 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8121 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8122 | if (argidx < arglen) { |
| 8123 | (*p_argidx)++; |
| 8124 | if (arglen < 0) |
| 8125 | return args; |
| 8126 | else |
| 8127 | return PyTuple_GetItem(args, argidx); |
| 8128 | } |
| 8129 | PyErr_SetString(PyExc_TypeError, |
| 8130 | "not enough arguments for format string"); |
| 8131 | return NULL; |
| 8132 | } |
| 8133 | |
| 8134 | #define F_LJUST (1<<0) |
| 8135 | #define F_SIGN (1<<1) |
| 8136 | #define F_BLANK (1<<2) |
| 8137 | #define F_ALT (1<<3) |
| 8138 | #define F_ZERO (1<<4) |
| 8139 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8140 | static Py_ssize_t |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8141 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8142 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8143 | register Py_ssize_t i; |
| 8144 | Py_ssize_t len = strlen(charbuffer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8145 | for (i = len - 1; i >= 0; i--) |
| 8146 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
| 8147 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8148 | return len; |
| 8149 | } |
| 8150 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8151 | static int |
| 8152 | doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) |
| 8153 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8154 | Py_ssize_t result; |
| 8155 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8156 | PyOS_ascii_formatd((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8157 | result = strtounicode(buffer, (char *)buffer); |
| 8158 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8159 | } |
| 8160 | |
| 8161 | static int |
| 8162 | longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) |
| 8163 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8164 | Py_ssize_t result; |
| 8165 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8166 | PyOS_snprintf((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8167 | result = strtounicode(buffer, (char *)buffer); |
| 8168 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8169 | } |
| 8170 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8171 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 8172 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 8173 | formatting is done. */ |
| 8174 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8175 | static int |
| 8176 | formatfloat(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8177 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8178 | int flags, |
| 8179 | int prec, |
| 8180 | int type, |
| 8181 | PyObject *v) |
| 8182 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8183 | /* fmt = '%#.' + `prec` + `type` |
| 8184 | 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] | 8185 | char fmt[20]; |
| 8186 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8187 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8188 | x = PyFloat_AsDouble(v); |
| 8189 | if (x == -1.0 && PyErr_Occurred()) |
| 8190 | return -1; |
| 8191 | if (prec < 0) |
| 8192 | prec = 6; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8193 | if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
| 8194 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8195 | /* Worst case length calc to ensure no buffer overrun: |
| 8196 | |
| 8197 | 'g' formats: |
| 8198 | fmt = %#.<prec>g |
| 8199 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 8200 | for any double rep.) |
| 8201 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 8202 | |
| 8203 | 'f' formats: |
| 8204 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 8205 | len = 1 + 50 + 1 + prec = 52 + prec |
| 8206 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8207 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8208 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8209 | |
| 8210 | */ |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 8211 | if (((type == 'g' || type == 'G') && |
| 8212 | buflen <= (size_t)10 + (size_t)prec) || |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8213 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8214 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8215 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8216 | return -1; |
| 8217 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8218 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 8219 | (flags&F_ALT) ? "#" : "", |
| 8220 | prec, type); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8221 | return doubletounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8222 | } |
| 8223 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8224 | static PyObject* |
| 8225 | formatlong(PyObject *val, int flags, int prec, int type) |
| 8226 | { |
| 8227 | char *buf; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8228 | int len; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8229 | PyObject *str; /* temporary string object. */ |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8230 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8231 | |
| 8232 | str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
| 8233 | if (!str) |
| 8234 | return NULL; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8235 | result = PyUnicode_FromStringAndSize(buf, len); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8236 | Py_DECREF(str); |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8237 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8238 | } |
| 8239 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8240 | static int |
| 8241 | formatint(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8242 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8243 | int flags, |
| 8244 | int prec, |
| 8245 | int type, |
| 8246 | PyObject *v) |
| 8247 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8248 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8249 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 8250 | * + 1 + 1 |
| 8251 | * = 24 |
| 8252 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8253 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8254 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8255 | long x; |
| 8256 | |
| 8257 | x = PyInt_AsLong(v); |
| 8258 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8259 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8260 | if (x < 0 && type == 'u') { |
| 8261 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8262 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8263 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 8264 | sign = "-"; |
| 8265 | else |
| 8266 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8267 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8268 | prec = 1; |
| 8269 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8270 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 8271 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8272 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8273 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8274 | PyErr_SetString(PyExc_OverflowError, |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8275 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8276 | return -1; |
| 8277 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8278 | |
| 8279 | if ((flags & F_ALT) && |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8280 | (type == 'x' || type == 'X' || type == 'o')) { |
| 8281 | /* When converting under %#o, %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8282 | * of issues that cause pain: |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8283 | * - for %#o, we want a different base marker than C |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8284 | * - when 0 is being converted, the C standard leaves off |
| 8285 | * the '0x' or '0X', which is inconsistent with other |
| 8286 | * %#x/%#X conversions and inconsistent with Python's |
| 8287 | * hex() function |
| 8288 | * - there are platforms that violate the standard and |
| 8289 | * convert 0 with the '0x' or '0X' |
| 8290 | * (Metrowerks, Compaq Tru64) |
| 8291 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8292 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8293 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8294 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8295 | * We can achieve the desired consistency by inserting our |
| 8296 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 8297 | * of %#x/%#X. |
| 8298 | * |
| 8299 | * Note that this is the same approach as used in |
| 8300 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8301 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8302 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 8303 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8304 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8305 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8306 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 8307 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8308 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 8309 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8310 | if (sign[0]) |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8311 | return longtounicode(buf, buflen, fmt, -x); |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8312 | else |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8313 | return longtounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8314 | } |
| 8315 | |
| 8316 | static int |
| 8317 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8318 | size_t buflen, |
| 8319 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8320 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8321 | /* presume that the buffer is at least 2 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8322 | if (PyUnicode_Check(v)) { |
| 8323 | if (PyUnicode_GET_SIZE(v) != 1) |
| 8324 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8325 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8326 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8327 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8328 | else if (PyString_Check(v)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8329 | if (PyString_GET_SIZE(v) != 1) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8330 | goto onError; |
| 8331 | buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0]; |
| 8332 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8333 | |
| 8334 | else { |
| 8335 | /* Integer input truncated to a character */ |
| 8336 | long x; |
| 8337 | x = PyInt_AsLong(v); |
| 8338 | if (x == -1 && PyErr_Occurred()) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8339 | goto onError; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8340 | #ifdef Py_UNICODE_WIDE |
| 8341 | if (x < 0 || x > 0x10ffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 8342 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8343 | "%c arg not in range(0x110000) " |
| 8344 | "(wide Python build)"); |
| 8345 | return -1; |
| 8346 | } |
| 8347 | #else |
| 8348 | if (x < 0 || x > 0xffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 8349 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8350 | "%c arg not in range(0x10000) " |
| 8351 | "(narrow Python build)"); |
| 8352 | return -1; |
| 8353 | } |
| 8354 | #endif |
| 8355 | buf[0] = (Py_UNICODE) x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8356 | } |
| 8357 | buf[1] = '\0'; |
| 8358 | return 1; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8359 | |
| 8360 | onError: |
| 8361 | PyErr_SetString(PyExc_TypeError, |
| 8362 | "%c requires int or char"); |
| 8363 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8364 | } |
| 8365 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8366 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 8367 | |
| 8368 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 8369 | chars are formatted. XXX This is a magic number. Each formatting |
| 8370 | routine does bounds checking to ensure no overflow, but a better |
| 8371 | solution may be to malloc a buffer of appropriate size for each |
| 8372 | format. For now, the current solution is sufficient. |
| 8373 | */ |
| 8374 | #define FORMATBUFLEN (size_t)120 |
| 8375 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8376 | PyObject *PyUnicode_Format(PyObject *format, |
| 8377 | PyObject *args) |
| 8378 | { |
| 8379 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8380 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8381 | int args_owned = 0; |
| 8382 | PyUnicodeObject *result = NULL; |
| 8383 | PyObject *dict = NULL; |
| 8384 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8385 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8386 | if (format == NULL || args == NULL) { |
| 8387 | PyErr_BadInternalCall(); |
| 8388 | return NULL; |
| 8389 | } |
| 8390 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8391 | if (uformat == NULL) |
| 8392 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8393 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 8394 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 8395 | |
| 8396 | reslen = rescnt = fmtcnt + 100; |
| 8397 | result = _PyUnicode_New(reslen); |
| 8398 | if (result == NULL) |
| 8399 | goto onError; |
| 8400 | res = PyUnicode_AS_UNICODE(result); |
| 8401 | |
| 8402 | if (PyTuple_Check(args)) { |
| 8403 | arglen = PyTuple_Size(args); |
| 8404 | argidx = 0; |
| 8405 | } |
| 8406 | else { |
| 8407 | arglen = -1; |
| 8408 | argidx = -2; |
| 8409 | } |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 8410 | if (Py_Type(args)->tp_as_mapping && !PyTuple_Check(args) && |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 8411 | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8412 | dict = args; |
| 8413 | |
| 8414 | while (--fmtcnt >= 0) { |
| 8415 | if (*fmt != '%') { |
| 8416 | if (--rescnt < 0) { |
| 8417 | rescnt = fmtcnt + 100; |
| 8418 | reslen += rescnt; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8419 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8420 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8421 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 8422 | --rescnt; |
| 8423 | } |
| 8424 | *res++ = *fmt++; |
| 8425 | } |
| 8426 | else { |
| 8427 | /* Got a format specifier */ |
| 8428 | int flags = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8429 | Py_ssize_t width = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8430 | int prec = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8431 | Py_UNICODE c = '\0'; |
| 8432 | Py_UNICODE fill; |
| 8433 | PyObject *v = NULL; |
| 8434 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8435 | Py_UNICODE *pbuf; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8436 | Py_UNICODE sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8437 | Py_ssize_t len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8438 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8439 | |
| 8440 | fmt++; |
| 8441 | if (*fmt == '(') { |
| 8442 | Py_UNICODE *keystart; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8443 | Py_ssize_t keylen; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8444 | PyObject *key; |
| 8445 | int pcount = 1; |
| 8446 | |
| 8447 | if (dict == NULL) { |
| 8448 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8449 | "format requires a mapping"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8450 | goto onError; |
| 8451 | } |
| 8452 | ++fmt; |
| 8453 | --fmtcnt; |
| 8454 | keystart = fmt; |
| 8455 | /* Skip over balanced parentheses */ |
| 8456 | while (pcount > 0 && --fmtcnt >= 0) { |
| 8457 | if (*fmt == ')') |
| 8458 | --pcount; |
| 8459 | else if (*fmt == '(') |
| 8460 | ++pcount; |
| 8461 | fmt++; |
| 8462 | } |
| 8463 | keylen = fmt - keystart - 1; |
| 8464 | if (fmtcnt < 0 || pcount > 0) { |
| 8465 | PyErr_SetString(PyExc_ValueError, |
| 8466 | "incomplete format key"); |
| 8467 | goto onError; |
| 8468 | } |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 8469 | #if 0 |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8470 | /* keys are converted to strings using UTF-8 and |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8471 | then looked up since Python uses strings to hold |
| 8472 | variables names etc. in its namespaces and we |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8473 | wouldn't want to break common idioms. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8474 | key = PyUnicode_EncodeUTF8(keystart, |
| 8475 | keylen, |
| 8476 | NULL); |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 8477 | #else |
| 8478 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 8479 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8480 | if (key == NULL) |
| 8481 | goto onError; |
| 8482 | if (args_owned) { |
| 8483 | Py_DECREF(args); |
| 8484 | args_owned = 0; |
| 8485 | } |
| 8486 | args = PyObject_GetItem(dict, key); |
| 8487 | Py_DECREF(key); |
| 8488 | if (args == NULL) { |
| 8489 | goto onError; |
| 8490 | } |
| 8491 | args_owned = 1; |
| 8492 | arglen = -1; |
| 8493 | argidx = -2; |
| 8494 | } |
| 8495 | while (--fmtcnt >= 0) { |
| 8496 | switch (c = *fmt++) { |
| 8497 | case '-': flags |= F_LJUST; continue; |
| 8498 | case '+': flags |= F_SIGN; continue; |
| 8499 | case ' ': flags |= F_BLANK; continue; |
| 8500 | case '#': flags |= F_ALT; continue; |
| 8501 | case '0': flags |= F_ZERO; continue; |
| 8502 | } |
| 8503 | break; |
| 8504 | } |
| 8505 | if (c == '*') { |
| 8506 | v = getnextarg(args, arglen, &argidx); |
| 8507 | if (v == NULL) |
| 8508 | goto onError; |
| 8509 | if (!PyInt_Check(v)) { |
| 8510 | PyErr_SetString(PyExc_TypeError, |
| 8511 | "* wants int"); |
| 8512 | goto onError; |
| 8513 | } |
| 8514 | width = PyInt_AsLong(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 8515 | if (width == -1 && PyErr_Occurred()) |
| 8516 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8517 | if (width < 0) { |
| 8518 | flags |= F_LJUST; |
| 8519 | width = -width; |
| 8520 | } |
| 8521 | if (--fmtcnt >= 0) |
| 8522 | c = *fmt++; |
| 8523 | } |
| 8524 | else if (c >= '0' && c <= '9') { |
| 8525 | width = c - '0'; |
| 8526 | while (--fmtcnt >= 0) { |
| 8527 | c = *fmt++; |
| 8528 | if (c < '0' || c > '9') |
| 8529 | break; |
| 8530 | if ((width*10) / 10 != width) { |
| 8531 | PyErr_SetString(PyExc_ValueError, |
| 8532 | "width too big"); |
| 8533 | goto onError; |
| 8534 | } |
| 8535 | width = width*10 + (c - '0'); |
| 8536 | } |
| 8537 | } |
| 8538 | if (c == '.') { |
| 8539 | prec = 0; |
| 8540 | if (--fmtcnt >= 0) |
| 8541 | c = *fmt++; |
| 8542 | if (c == '*') { |
| 8543 | v = getnextarg(args, arglen, &argidx); |
| 8544 | if (v == NULL) |
| 8545 | goto onError; |
| 8546 | if (!PyInt_Check(v)) { |
| 8547 | PyErr_SetString(PyExc_TypeError, |
| 8548 | "* wants int"); |
| 8549 | goto onError; |
| 8550 | } |
| 8551 | prec = PyInt_AsLong(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 8552 | if (prec == -1 && PyErr_Occurred()) |
| 8553 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8554 | if (prec < 0) |
| 8555 | prec = 0; |
| 8556 | if (--fmtcnt >= 0) |
| 8557 | c = *fmt++; |
| 8558 | } |
| 8559 | else if (c >= '0' && c <= '9') { |
| 8560 | prec = c - '0'; |
| 8561 | while (--fmtcnt >= 0) { |
| 8562 | c = Py_CHARMASK(*fmt++); |
| 8563 | if (c < '0' || c > '9') |
| 8564 | break; |
| 8565 | if ((prec*10) / 10 != prec) { |
| 8566 | PyErr_SetString(PyExc_ValueError, |
| 8567 | "prec too big"); |
| 8568 | goto onError; |
| 8569 | } |
| 8570 | prec = prec*10 + (c - '0'); |
| 8571 | } |
| 8572 | } |
| 8573 | } /* prec */ |
| 8574 | if (fmtcnt >= 0) { |
| 8575 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8576 | if (--fmtcnt >= 0) |
| 8577 | c = *fmt++; |
| 8578 | } |
| 8579 | } |
| 8580 | if (fmtcnt < 0) { |
| 8581 | PyErr_SetString(PyExc_ValueError, |
| 8582 | "incomplete format"); |
| 8583 | goto onError; |
| 8584 | } |
| 8585 | if (c != '%') { |
| 8586 | v = getnextarg(args, arglen, &argidx); |
| 8587 | if (v == NULL) |
| 8588 | goto onError; |
| 8589 | } |
| 8590 | sign = 0; |
| 8591 | fill = ' '; |
| 8592 | switch (c) { |
| 8593 | |
| 8594 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8595 | pbuf = formatbuf; |
| 8596 | /* presume that buffer length is at least 1 */ |
| 8597 | pbuf[0] = '%'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8598 | len = 1; |
| 8599 | break; |
| 8600 | |
| 8601 | case 's': |
| 8602 | case 'r': |
| 8603 | if (PyUnicode_Check(v) && c == 's') { |
| 8604 | temp = v; |
| 8605 | Py_INCREF(temp); |
| 8606 | } |
| 8607 | else { |
| 8608 | PyObject *unicode; |
| 8609 | if (c == 's') |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8610 | temp = PyObject_Unicode(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8611 | else |
| 8612 | temp = PyObject_Repr(v); |
| 8613 | if (temp == NULL) |
| 8614 | goto onError; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8615 | if (PyUnicode_Check(temp)) |
| 8616 | /* nothing to do */; |
| 8617 | else if (PyString_Check(temp)) { |
| 8618 | /* convert to string to Unicode */ |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8619 | unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8620 | PyString_GET_SIZE(temp), |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8621 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8622 | "strict"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8623 | Py_DECREF(temp); |
| 8624 | temp = unicode; |
| 8625 | if (temp == NULL) |
| 8626 | goto onError; |
| 8627 | } |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8628 | else { |
| 8629 | Py_DECREF(temp); |
| 8630 | PyErr_SetString(PyExc_TypeError, |
| 8631 | "%s argument has non-string str()"); |
| 8632 | goto onError; |
| 8633 | } |
| 8634 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8635 | pbuf = PyUnicode_AS_UNICODE(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8636 | len = PyUnicode_GET_SIZE(temp); |
| 8637 | if (prec >= 0 && len > prec) |
| 8638 | len = prec; |
| 8639 | break; |
| 8640 | |
| 8641 | case 'i': |
| 8642 | case 'd': |
| 8643 | case 'u': |
| 8644 | case 'o': |
| 8645 | case 'x': |
| 8646 | case 'X': |
| 8647 | if (c == 'i') |
| 8648 | c = 'd'; |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 8649 | if (PyLong_Check(v)) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8650 | temp = formatlong(v, flags, prec, c); |
| 8651 | if (!temp) |
| 8652 | goto onError; |
| 8653 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 8654 | len = PyUnicode_GET_SIZE(temp); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8655 | sign = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8656 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8657 | else { |
| 8658 | pbuf = formatbuf; |
| 8659 | len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8660 | flags, prec, c, v); |
| 8661 | if (len < 0) |
| 8662 | goto onError; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8663 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8664 | } |
| 8665 | if (flags & F_ZERO) |
| 8666 | fill = '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8667 | break; |
| 8668 | |
| 8669 | case 'e': |
| 8670 | case 'E': |
| 8671 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 8672 | case 'F': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8673 | case 'g': |
| 8674 | case 'G': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 8675 | if (c == 'F') |
| 8676 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8677 | pbuf = formatbuf; |
| 8678 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8679 | flags, prec, c, v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8680 | if (len < 0) |
| 8681 | goto onError; |
| 8682 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8683 | if (flags & F_ZERO) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8684 | fill = '0'; |
| 8685 | break; |
| 8686 | |
| 8687 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8688 | pbuf = formatbuf; |
| 8689 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8690 | if (len < 0) |
| 8691 | goto onError; |
| 8692 | break; |
| 8693 | |
| 8694 | default: |
| 8695 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 8696 | "unsupported format character '%c' (0x%x) " |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 8697 | "at index %zd", |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8698 | (31<=c && c<=126) ? (char)c : '?', |
Marc-André Lemburg | 24e53b6 | 2002-09-24 09:32:14 +0000 | [diff] [blame] | 8699 | (int)c, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 8700 | (Py_ssize_t)(fmt - 1 - |
| 8701 | PyUnicode_AS_UNICODE(uformat))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8702 | goto onError; |
| 8703 | } |
| 8704 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8705 | if (*pbuf == '-' || *pbuf == '+') { |
| 8706 | sign = *pbuf++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8707 | len--; |
| 8708 | } |
| 8709 | else if (flags & F_SIGN) |
| 8710 | sign = '+'; |
| 8711 | else if (flags & F_BLANK) |
| 8712 | sign = ' '; |
| 8713 | else |
| 8714 | sign = 0; |
| 8715 | } |
| 8716 | if (width < len) |
| 8717 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8718 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8719 | reslen -= rescnt; |
| 8720 | rescnt = width + fmtcnt + 100; |
| 8721 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8722 | if (reslen < 0) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8723 | Py_XDECREF(temp); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8724 | PyErr_NoMemory(); |
| 8725 | goto onError; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8726 | } |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8727 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 8728 | Py_XDECREF(temp); |
| 8729 | goto onError; |
| 8730 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8731 | res = PyUnicode_AS_UNICODE(result) |
| 8732 | + reslen - rescnt; |
| 8733 | } |
| 8734 | if (sign) { |
| 8735 | if (fill != ' ') |
| 8736 | *res++ = sign; |
| 8737 | rescnt--; |
| 8738 | if (width > len) |
| 8739 | width--; |
| 8740 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8741 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8742 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8743 | assert(pbuf[1] == c); |
| 8744 | if (fill != ' ') { |
| 8745 | *res++ = *pbuf++; |
| 8746 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8747 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8748 | rescnt -= 2; |
| 8749 | width -= 2; |
| 8750 | if (width < 0) |
| 8751 | width = 0; |
| 8752 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8753 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8754 | if (width > len && !(flags & F_LJUST)) { |
| 8755 | do { |
| 8756 | --rescnt; |
| 8757 | *res++ = fill; |
| 8758 | } while (--width > len); |
| 8759 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8760 | if (fill == ' ') { |
| 8761 | if (sign) |
| 8762 | *res++ = sign; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8763 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8764 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8765 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8766 | *res++ = *pbuf++; |
| 8767 | *res++ = *pbuf++; |
| 8768 | } |
| 8769 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8770 | Py_UNICODE_COPY(res, pbuf, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8771 | res += len; |
| 8772 | rescnt -= len; |
| 8773 | while (--width >= len) { |
| 8774 | --rescnt; |
| 8775 | *res++ = ' '; |
| 8776 | } |
| 8777 | if (dict && (argidx < arglen) && c != '%') { |
| 8778 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 8779 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8780 | Py_XDECREF(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8781 | goto onError; |
| 8782 | } |
| 8783 | Py_XDECREF(temp); |
| 8784 | } /* '%' */ |
| 8785 | } /* until end */ |
| 8786 | if (argidx < arglen && !dict) { |
| 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"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8789 | goto onError; |
| 8790 | } |
| 8791 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8792 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
| 8793 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8794 | if (args_owned) { |
| 8795 | Py_DECREF(args); |
| 8796 | } |
| 8797 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8798 | return (PyObject *)result; |
| 8799 | |
| 8800 | onError: |
| 8801 | Py_XDECREF(result); |
| 8802 | Py_DECREF(uformat); |
| 8803 | if (args_owned) { |
| 8804 | Py_DECREF(args); |
| 8805 | } |
| 8806 | return NULL; |
| 8807 | } |
| 8808 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 8809 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8810 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 8811 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8812 | static PyObject * |
| 8813 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8814 | { |
| 8815 | PyObject *x = NULL; |
Guido van Rossum | 55b4a7b | 2007-07-11 09:28:11 +0000 | [diff] [blame] | 8816 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8817 | char *encoding = NULL; |
| 8818 | char *errors = NULL; |
| 8819 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8820 | if (type != &PyUnicode_Type) |
| 8821 | return unicode_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8822 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
| 8823 | kwlist, &x, &encoding, &errors)) |
| 8824 | return NULL; |
| 8825 | if (x == NULL) |
| 8826 | return (PyObject *)_PyUnicode_New(0); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 8827 | if (encoding == NULL && errors == NULL) |
| 8828 | return PyObject_Unicode(x); |
| 8829 | else |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8830 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
| 8831 | } |
| 8832 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8833 | static PyObject * |
| 8834 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8835 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8836 | PyUnicodeObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8837 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8838 | |
| 8839 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 8840 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 8841 | if (tmp == NULL) |
| 8842 | return NULL; |
| 8843 | assert(PyUnicode_Check(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8844 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8845 | if (pnew == NULL) { |
| 8846 | Py_DECREF(tmp); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8847 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8848 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8849 | pnew->str = PyMem_NEW(Py_UNICODE, n+1); |
| 8850 | if (pnew->str == NULL) { |
| 8851 | _Py_ForgetReference((PyObject *)pnew); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8852 | PyObject_Del(pnew); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8853 | Py_DECREF(tmp); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 8854 | return PyErr_NoMemory(); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8855 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8856 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 8857 | pnew->length = n; |
| 8858 | pnew->hash = tmp->hash; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8859 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8860 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8861 | } |
| 8862 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8863 | PyDoc_STRVAR(unicode_doc, |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 8864 | "str(string [, encoding[, errors]]) -> object\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8865 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 8866 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 8867 | encoding defaults to the current default string encoding.\n\ |
| 8868 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8869 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8870 | static PyObject *unicode_iter(PyObject *seq); |
| 8871 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8872 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 8873 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 8874 | "str", /* tp_name */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8875 | sizeof(PyUnicodeObject), /* tp_size */ |
| 8876 | 0, /* tp_itemsize */ |
| 8877 | /* Slots */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 8878 | (destructor)unicode_dealloc, /* tp_dealloc */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8879 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8880 | 0, /* tp_getattr */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8881 | 0, /* tp_setattr */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8882 | 0, /* tp_compare */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8883 | unicode_repr, /* tp_repr */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8884 | &unicode_as_number, /* tp_as_number */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8885 | &unicode_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8886 | &unicode_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8887 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 8888 | 0, /* tp_call*/ |
| 8889 | (reprfunc) unicode_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8890 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 8891 | 0, /* tp_setattro */ |
Alexandre Vassalotti | 70a2371 | 2007-10-14 02:05:51 +0000 | [diff] [blame] | 8892 | 0, /* tp_as_buffer */ |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 8893 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 8894 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8895 | unicode_doc, /* tp_doc */ |
| 8896 | 0, /* tp_traverse */ |
| 8897 | 0, /* tp_clear */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8898 | PyUnicode_RichCompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8899 | 0, /* tp_weaklistoffset */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8900 | unicode_iter, /* tp_iter */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8901 | 0, /* tp_iternext */ |
| 8902 | unicode_methods, /* tp_methods */ |
| 8903 | 0, /* tp_members */ |
| 8904 | 0, /* tp_getset */ |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 8905 | &PyBaseString_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8906 | 0, /* tp_dict */ |
| 8907 | 0, /* tp_descr_get */ |
| 8908 | 0, /* tp_descr_set */ |
| 8909 | 0, /* tp_dictoffset */ |
| 8910 | 0, /* tp_init */ |
| 8911 | 0, /* tp_alloc */ |
| 8912 | unicode_new, /* tp_new */ |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8913 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8914 | }; |
| 8915 | |
| 8916 | /* Initialize the Unicode implementation */ |
| 8917 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 8918 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8919 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8920 | int i; |
| 8921 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8922 | /* XXX - move this array to unicodectype.c ? */ |
| 8923 | Py_UNICODE linebreak[] = { |
| 8924 | 0x000A, /* LINE FEED */ |
| 8925 | 0x000D, /* CARRIAGE RETURN */ |
| 8926 | 0x001C, /* FILE SEPARATOR */ |
| 8927 | 0x001D, /* GROUP SEPARATOR */ |
| 8928 | 0x001E, /* RECORD SEPARATOR */ |
| 8929 | 0x0085, /* NEXT LINE */ |
| 8930 | 0x2028, /* LINE SEPARATOR */ |
| 8931 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 8932 | }; |
| 8933 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8934 | /* Init the implementation */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8935 | unicode_freelist = NULL; |
| 8936 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8937 | unicode_empty = _PyUnicode_New(0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8938 | if (!unicode_empty) |
| 8939 | return; |
| 8940 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8941 | for (i = 0; i < 256; i++) |
| 8942 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 8943 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 8944 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8945 | |
| 8946 | /* initialize the linebreak bloom filter */ |
| 8947 | bloom_linebreak = make_bloom_mask( |
| 8948 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 8949 | ); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8950 | |
| 8951 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8952 | } |
| 8953 | |
| 8954 | /* Finalize the Unicode implementation */ |
| 8955 | |
| 8956 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 8957 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8958 | { |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8959 | PyUnicodeObject *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8960 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8961 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 8962 | Py_XDECREF(unicode_empty); |
| 8963 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8964 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8965 | for (i = 0; i < 256; i++) { |
| 8966 | if (unicode_latin1[i]) { |
| 8967 | Py_DECREF(unicode_latin1[i]); |
| 8968 | unicode_latin1[i] = NULL; |
| 8969 | } |
| 8970 | } |
| 8971 | |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8972 | for (u = unicode_freelist; u != NULL;) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8973 | PyUnicodeObject *v = u; |
| 8974 | u = *(PyUnicodeObject **)u; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 8975 | if (v->str) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 8976 | PyMem_DEL(v->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 8977 | Py_XDECREF(v->defenc); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8978 | PyObject_Del(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8979 | } |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8980 | unicode_freelist = NULL; |
| 8981 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8982 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 8983 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 8984 | void |
| 8985 | PyUnicode_InternInPlace(PyObject **p) |
| 8986 | { |
| 8987 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 8988 | PyObject *t; |
| 8989 | if (s == NULL || !PyUnicode_Check(s)) |
| 8990 | Py_FatalError( |
| 8991 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 8992 | /* If it's a subclass, we don't really know what putting |
| 8993 | it in the interned dict might do. */ |
| 8994 | if (!PyUnicode_CheckExact(s)) |
| 8995 | return; |
| 8996 | if (PyUnicode_CHECK_INTERNED(s)) |
| 8997 | return; |
| 8998 | if (interned == NULL) { |
| 8999 | interned = PyDict_New(); |
| 9000 | if (interned == NULL) { |
| 9001 | PyErr_Clear(); /* Don't leave an exception */ |
| 9002 | return; |
| 9003 | } |
| 9004 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9005 | /* It might be that the GetItem call fails even |
| 9006 | though the key is present in the dictionary, |
| 9007 | namely when this happens during a stack overflow. */ |
| 9008 | Py_ALLOW_RECURSION |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9009 | t = PyDict_GetItem(interned, (PyObject *)s); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9010 | Py_END_ALLOW_RECURSION |
| 9011 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9012 | if (t) { |
| 9013 | Py_INCREF(t); |
| 9014 | Py_DECREF(*p); |
| 9015 | *p = t; |
| 9016 | return; |
| 9017 | } |
| 9018 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9019 | PyThreadState_GET()->recursion_critical = 1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9020 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 9021 | PyErr_Clear(); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9022 | PyThreadState_GET()->recursion_critical = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9023 | return; |
| 9024 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9025 | PyThreadState_GET()->recursion_critical = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9026 | /* The two references in interned are not counted by refcnt. |
| 9027 | The deallocator will take care of this */ |
Martin v. Löwis | 5d7428b | 2007-07-21 18:47:48 +0000 | [diff] [blame] | 9028 | Py_Refcnt(s) -= 2; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9029 | PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
| 9030 | } |
| 9031 | |
| 9032 | void |
| 9033 | PyUnicode_InternImmortal(PyObject **p) |
| 9034 | { |
| 9035 | PyUnicode_InternInPlace(p); |
| 9036 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 9037 | PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 9038 | Py_INCREF(*p); |
| 9039 | } |
| 9040 | } |
| 9041 | |
| 9042 | PyObject * |
| 9043 | PyUnicode_InternFromString(const char *cp) |
| 9044 | { |
| 9045 | PyObject *s = PyUnicode_FromString(cp); |
| 9046 | if (s == NULL) |
| 9047 | return NULL; |
| 9048 | PyUnicode_InternInPlace(&s); |
| 9049 | return s; |
| 9050 | } |
| 9051 | |
| 9052 | void _Py_ReleaseInternedUnicodeStrings(void) |
| 9053 | { |
| 9054 | PyObject *keys; |
| 9055 | PyUnicodeObject *s; |
| 9056 | Py_ssize_t i, n; |
| 9057 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
| 9058 | |
| 9059 | if (interned == NULL || !PyDict_Check(interned)) |
| 9060 | return; |
| 9061 | keys = PyDict_Keys(interned); |
| 9062 | if (keys == NULL || !PyList_Check(keys)) { |
| 9063 | PyErr_Clear(); |
| 9064 | return; |
| 9065 | } |
| 9066 | |
| 9067 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 9068 | detector, interned unicode strings are not forcibly deallocated; |
| 9069 | rather, we give them their stolen references back, and then clear |
| 9070 | and DECREF the interned dict. */ |
| 9071 | |
| 9072 | n = PyList_GET_SIZE(keys); |
| 9073 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
| 9074 | n); |
| 9075 | for (i = 0; i < n; i++) { |
| 9076 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
| 9077 | switch (s->state) { |
| 9078 | case SSTATE_NOT_INTERNED: |
| 9079 | /* XXX Shouldn't happen */ |
| 9080 | break; |
| 9081 | case SSTATE_INTERNED_IMMORTAL: |
Martin v. Löwis | 5d7428b | 2007-07-21 18:47:48 +0000 | [diff] [blame] | 9082 | Py_Refcnt(s) += 1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9083 | immortal_size += s->length; |
| 9084 | break; |
| 9085 | case SSTATE_INTERNED_MORTAL: |
Martin v. Löwis | 5d7428b | 2007-07-21 18:47:48 +0000 | [diff] [blame] | 9086 | Py_Refcnt(s) += 2; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9087 | mortal_size += s->length; |
| 9088 | break; |
| 9089 | default: |
| 9090 | Py_FatalError("Inconsistent interned string state."); |
| 9091 | } |
| 9092 | s->state = SSTATE_NOT_INTERNED; |
| 9093 | } |
| 9094 | fprintf(stderr, "total size of all interned strings: " |
| 9095 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 9096 | "mortal/immortal\n", mortal_size, immortal_size); |
| 9097 | Py_DECREF(keys); |
| 9098 | PyDict_Clear(interned); |
| 9099 | Py_DECREF(interned); |
| 9100 | interned = NULL; |
| 9101 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9102 | |
| 9103 | |
| 9104 | /********************* Unicode Iterator **************************/ |
| 9105 | |
| 9106 | typedef struct { |
| 9107 | PyObject_HEAD |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9108 | Py_ssize_t it_index; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9109 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
| 9110 | } unicodeiterobject; |
| 9111 | |
| 9112 | static void |
| 9113 | unicodeiter_dealloc(unicodeiterobject *it) |
| 9114 | { |
| 9115 | _PyObject_GC_UNTRACK(it); |
| 9116 | Py_XDECREF(it->it_seq); |
| 9117 | PyObject_GC_Del(it); |
| 9118 | } |
| 9119 | |
| 9120 | static int |
| 9121 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 9122 | { |
| 9123 | Py_VISIT(it->it_seq); |
| 9124 | return 0; |
| 9125 | } |
| 9126 | |
| 9127 | static PyObject * |
| 9128 | unicodeiter_next(unicodeiterobject *it) |
| 9129 | { |
| 9130 | PyUnicodeObject *seq; |
| 9131 | PyObject *item; |
| 9132 | |
| 9133 | assert(it != NULL); |
| 9134 | seq = it->it_seq; |
| 9135 | if (seq == NULL) |
| 9136 | return NULL; |
| 9137 | assert(PyUnicode_Check(seq)); |
| 9138 | |
| 9139 | if (it->it_index < PyUnicode_GET_SIZE(seq)) { |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9140 | item = PyUnicode_FromUnicode( |
| 9141 | PyUnicode_AS_UNICODE(seq)+it->it_index, 1); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9142 | if (item != NULL) |
| 9143 | ++it->it_index; |
| 9144 | return item; |
| 9145 | } |
| 9146 | |
| 9147 | Py_DECREF(seq); |
| 9148 | it->it_seq = NULL; |
| 9149 | return NULL; |
| 9150 | } |
| 9151 | |
| 9152 | static PyObject * |
| 9153 | unicodeiter_len(unicodeiterobject *it) |
| 9154 | { |
| 9155 | Py_ssize_t len = 0; |
| 9156 | if (it->it_seq) |
| 9157 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 9158 | return PyInt_FromSsize_t(len); |
| 9159 | } |
| 9160 | |
| 9161 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 9162 | |
| 9163 | static PyMethodDef unicodeiter_methods[] = { |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9164 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
| 9165 | length_hint_doc}, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9166 | {NULL, NULL} /* sentinel */ |
| 9167 | }; |
| 9168 | |
| 9169 | PyTypeObject PyUnicodeIter_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9170 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9171 | "unicodeiterator", /* tp_name */ |
| 9172 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 9173 | 0, /* tp_itemsize */ |
| 9174 | /* methods */ |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9175 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9176 | 0, /* tp_print */ |
| 9177 | 0, /* tp_getattr */ |
| 9178 | 0, /* tp_setattr */ |
| 9179 | 0, /* tp_compare */ |
| 9180 | 0, /* tp_repr */ |
| 9181 | 0, /* tp_as_number */ |
| 9182 | 0, /* tp_as_sequence */ |
| 9183 | 0, /* tp_as_mapping */ |
| 9184 | 0, /* tp_hash */ |
| 9185 | 0, /* tp_call */ |
| 9186 | 0, /* tp_str */ |
| 9187 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9188 | 0, /* tp_setattro */ |
| 9189 | 0, /* tp_as_buffer */ |
| 9190 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 9191 | 0, /* tp_doc */ |
| 9192 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 9193 | 0, /* tp_clear */ |
| 9194 | 0, /* tp_richcompare */ |
| 9195 | 0, /* tp_weaklistoffset */ |
| 9196 | PyObject_SelfIter, /* tp_iter */ |
| 9197 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 9198 | unicodeiter_methods, /* tp_methods */ |
| 9199 | 0, |
| 9200 | }; |
| 9201 | |
| 9202 | static PyObject * |
| 9203 | unicode_iter(PyObject *seq) |
| 9204 | { |
| 9205 | unicodeiterobject *it; |
| 9206 | |
| 9207 | if (!PyUnicode_Check(seq)) { |
| 9208 | PyErr_BadInternalCall(); |
| 9209 | return NULL; |
| 9210 | } |
| 9211 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 9212 | if (it == NULL) |
| 9213 | return NULL; |
| 9214 | it->it_index = 0; |
| 9215 | Py_INCREF(seq); |
| 9216 | it->it_seq = (PyUnicodeObject *)seq; |
| 9217 | _PyObject_GC_TRACK(it); |
| 9218 | return (PyObject *)it; |
| 9219 | } |
| 9220 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9221 | size_t |
| 9222 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 9223 | { |
| 9224 | int res = 0; |
| 9225 | while(*u++) |
| 9226 | res++; |
| 9227 | return res; |
| 9228 | } |
| 9229 | |
| 9230 | Py_UNICODE* |
| 9231 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9232 | { |
| 9233 | Py_UNICODE *u = s1; |
| 9234 | while ((*u++ = *s2++)); |
| 9235 | return s1; |
| 9236 | } |
| 9237 | |
| 9238 | Py_UNICODE* |
| 9239 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 9240 | { |
| 9241 | Py_UNICODE *u = s1; |
| 9242 | while ((*u++ = *s2++)) |
| 9243 | if (n-- == 0) |
| 9244 | break; |
| 9245 | return s1; |
| 9246 | } |
| 9247 | |
| 9248 | int |
| 9249 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9250 | { |
| 9251 | while (*s1 && *s2 && *s1 == *s2) |
| 9252 | s1++, s2++; |
| 9253 | if (*s1 && *s2) |
| 9254 | return (*s1 < *s2) ? -1 : +1; |
| 9255 | if (*s1) |
| 9256 | return 1; |
| 9257 | if (*s2) |
| 9258 | return -1; |
| 9259 | return 0; |
| 9260 | } |
| 9261 | |
| 9262 | Py_UNICODE* |
| 9263 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 9264 | { |
| 9265 | const Py_UNICODE *p; |
| 9266 | for (p = s; *p; p++) |
| 9267 | if (*p == c) |
| 9268 | return (Py_UNICODE*)p; |
| 9269 | return NULL; |
| 9270 | } |
| 9271 | |
| 9272 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9273 | #ifdef __cplusplus |
| 9274 | } |
| 9275 | #endif |
| 9276 | |
| 9277 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9278 | /* |
| 9279 | Local variables: |
| 9280 | c-basic-offset: 4 |
| 9281 | indent-tabs-mode: nil |
| 9282 | End: |
| 9283 | */ |