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': |
| 624 | n += strlen(va_arg(count, char*)); |
| 625 | break; |
| 626 | case 'U': |
| 627 | { |
| 628 | PyObject *obj = va_arg(count, PyObject *); |
| 629 | assert(obj && PyUnicode_Check(obj)); |
| 630 | n += PyUnicode_GET_SIZE(obj); |
| 631 | break; |
| 632 | } |
Walter Dörwald | d7fb764 | 2007-06-11 16:36:59 +0000 | [diff] [blame] | 633 | case 'V': |
| 634 | { |
| 635 | PyObject *obj = va_arg(count, PyObject *); |
| 636 | const char *str = va_arg(count, const char *); |
| 637 | assert(obj || str); |
| 638 | assert(!obj || PyUnicode_Check(obj)); |
| 639 | if (obj) |
| 640 | n += PyUnicode_GET_SIZE(obj); |
| 641 | else |
| 642 | n += strlen(str); |
| 643 | break; |
| 644 | } |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 645 | case 'S': |
| 646 | { |
| 647 | PyObject *obj = va_arg(count, PyObject *); |
| 648 | PyObject *str; |
| 649 | assert(obj); |
| 650 | str = PyObject_Unicode(obj); |
| 651 | if (!str) |
| 652 | goto fail; |
| 653 | n += PyUnicode_GET_SIZE(str); |
| 654 | /* Remember the str and switch to the next slot */ |
| 655 | *callresult++ = str; |
| 656 | break; |
| 657 | } |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 658 | case 'R': |
| 659 | { |
| 660 | PyObject *obj = va_arg(count, PyObject *); |
| 661 | PyObject *repr; |
| 662 | assert(obj); |
| 663 | repr = PyObject_Repr(obj); |
| 664 | if (!repr) |
| 665 | goto fail; |
| 666 | n += PyUnicode_GET_SIZE(repr); |
| 667 | /* Remember the repr and switch to the next slot */ |
| 668 | *callresult++ = repr; |
| 669 | break; |
| 670 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 671 | case 'p': |
| 672 | (void) va_arg(count, int); |
| 673 | /* maximum 64-bit pointer representation: |
| 674 | * 0xffffffffffffffff |
| 675 | * so 19 characters is enough. |
| 676 | * XXX I count 18 -- what's the extra for? |
| 677 | */ |
| 678 | n += 19; |
| 679 | break; |
| 680 | default: |
| 681 | /* if we stumble upon an unknown |
| 682 | formatting code, copy the rest of |
| 683 | the format string to the output |
| 684 | string. (we cannot just skip the |
| 685 | code, since there's no way to know |
| 686 | what's in the argument list) */ |
| 687 | n += strlen(p); |
| 688 | goto expand; |
| 689 | } |
| 690 | } else |
| 691 | n++; |
| 692 | } |
| 693 | expand: |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 694 | if (abuffersize > 20) { |
| 695 | abuffer = PyMem_Malloc(abuffersize); |
| 696 | if (!abuffer) { |
| 697 | PyErr_NoMemory(); |
| 698 | goto fail; |
| 699 | } |
| 700 | realbuffer = abuffer; |
| 701 | } |
| 702 | else |
| 703 | realbuffer = buffer; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 704 | /* step 4: fill the buffer */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 705 | /* 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] | 706 | we don't have to resize the string. |
| 707 | There can be no errors beyond this point. */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 708 | string = PyUnicode_FromUnicode(NULL, n); |
| 709 | if (!string) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 710 | goto fail; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 711 | |
| 712 | s = PyUnicode_AS_UNICODE(string); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 713 | callresult = callresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 714 | |
| 715 | for (f = format; *f; f++) { |
| 716 | if (*f == '%') { |
| 717 | const char* p = f++; |
| 718 | int longflag = 0; |
| 719 | int size_tflag = 0; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 720 | zeropad = (*f == '0'); |
| 721 | /* parse the width.precision part */ |
| 722 | width = 0; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 723 | while (isdigit(Py_CHARMASK(*f))) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 724 | width = (width*10) + *f++ - '0'; |
| 725 | precision = 0; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 726 | if (*f == '.') { |
| 727 | f++; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 728 | while (isdigit(Py_CHARMASK(*f))) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 729 | precision = (precision*10) + *f++ - '0'; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 730 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 731 | /* handle the long flag, but only for %ld and %lu. |
| 732 | others can be added when necessary. */ |
| 733 | if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
| 734 | longflag = 1; |
| 735 | ++f; |
| 736 | } |
| 737 | /* handle the size_t flag. */ |
| 738 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 739 | size_tflag = 1; |
| 740 | ++f; |
| 741 | } |
| 742 | |
| 743 | switch (*f) { |
| 744 | case 'c': |
| 745 | *s++ = va_arg(vargs, int); |
| 746 | break; |
| 747 | case 'd': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 748 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'd'); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 749 | if (longflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 750 | sprintf(realbuffer, fmt, va_arg(vargs, long)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 751 | else if (size_tflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 752 | sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 753 | else |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 754 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 755 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 756 | break; |
| 757 | case 'u': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 758 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'u'); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 759 | if (longflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 760 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 761 | else if (size_tflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 762 | sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 763 | else |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 764 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
| 765 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 766 | break; |
| 767 | case 'i': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 768 | makefmt(fmt, 0, 0, zeropad, width, precision, 'i'); |
| 769 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 770 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 771 | break; |
| 772 | case 'x': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 773 | makefmt(fmt, 0, 0, zeropad, width, precision, 'x'); |
| 774 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 775 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 776 | break; |
| 777 | case 's': |
| 778 | p = va_arg(vargs, char*); |
| 779 | appendstring(p); |
| 780 | break; |
| 781 | case 'U': |
| 782 | { |
| 783 | PyObject *obj = va_arg(vargs, PyObject *); |
Walter Dörwald | 5c2fab6 | 2007-05-24 19:51:02 +0000 | [diff] [blame] | 784 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 785 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 786 | s += size; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 787 | break; |
| 788 | } |
Walter Dörwald | d7fb764 | 2007-06-11 16:36:59 +0000 | [diff] [blame] | 789 | case 'V': |
| 790 | { |
| 791 | PyObject *obj = va_arg(vargs, PyObject *); |
| 792 | const char *str = va_arg(vargs, const char *); |
| 793 | if (obj) { |
| 794 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 795 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 796 | s += size; |
| 797 | } else { |
| 798 | appendstring(str); |
| 799 | } |
| 800 | break; |
| 801 | } |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 802 | case 'S': |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 803 | case 'R': |
| 804 | { |
Guido van Rossum | 755114a | 2007-06-13 01:04:27 +0000 | [diff] [blame] | 805 | Py_UNICODE *ucopy; |
| 806 | Py_ssize_t usize; |
| 807 | Py_ssize_t upos; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 808 | /* unused, since we already have the result */ |
| 809 | (void) va_arg(vargs, PyObject *); |
Guido van Rossum | 755114a | 2007-06-13 01:04:27 +0000 | [diff] [blame] | 810 | ucopy = PyUnicode_AS_UNICODE(*callresult); |
| 811 | usize = PyUnicode_GET_SIZE(*callresult); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 812 | for (upos = 0; upos<usize;) |
| 813 | *s++ = ucopy[upos++]; |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 814 | /* We're done with the unicode()/repr() => forget it */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 815 | Py_DECREF(*callresult); |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 816 | /* switch to next unicode()/repr() result */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 817 | ++callresult; |
| 818 | break; |
| 819 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 820 | case 'p': |
| 821 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 822 | /* %p is ill-defined: ensure leading 0x. */ |
| 823 | if (buffer[1] == 'X') |
| 824 | buffer[1] = 'x'; |
| 825 | else if (buffer[1] != 'x') { |
| 826 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 827 | buffer[0] = '0'; |
| 828 | buffer[1] = 'x'; |
| 829 | } |
| 830 | appendstring(buffer); |
| 831 | break; |
| 832 | case '%': |
| 833 | *s++ = '%'; |
| 834 | break; |
| 835 | default: |
| 836 | appendstring(p); |
| 837 | goto end; |
| 838 | } |
| 839 | } else |
| 840 | *s++ = *f; |
| 841 | } |
| 842 | |
| 843 | end: |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 844 | if (callresults) |
| 845 | PyMem_Free(callresults); |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 846 | if (abuffer) |
| 847 | PyMem_Free(abuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 848 | _PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
| 849 | return string; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 850 | fail: |
| 851 | if (callresults) { |
| 852 | PyObject **callresult2 = callresults; |
Guido van Rossum | 307fa8c | 2007-07-16 20:46:27 +0000 | [diff] [blame] | 853 | while (callresult2 < callresult) { |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 854 | Py_DECREF(*callresult2); |
| 855 | ++callresult2; |
| 856 | } |
| 857 | PyMem_Free(callresults); |
| 858 | } |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 859 | if (abuffer) |
| 860 | PyMem_Free(abuffer); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 861 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | #undef appendstring |
| 865 | |
| 866 | PyObject * |
| 867 | PyUnicode_FromFormat(const char *format, ...) |
| 868 | { |
| 869 | PyObject* ret; |
| 870 | va_list vargs; |
| 871 | |
| 872 | #ifdef HAVE_STDARG_PROTOTYPES |
| 873 | va_start(vargs, format); |
| 874 | #else |
| 875 | va_start(vargs); |
| 876 | #endif |
| 877 | ret = PyUnicode_FromFormatV(format, vargs); |
| 878 | va_end(vargs); |
| 879 | return ret; |
| 880 | } |
| 881 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 882 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 883 | wchar_t *w, |
| 884 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 885 | { |
| 886 | if (unicode == NULL) { |
| 887 | PyErr_BadInternalCall(); |
| 888 | return -1; |
| 889 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 890 | |
| 891 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 892 | if (size > PyUnicode_GET_SIZE(unicode)) |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 893 | size = PyUnicode_GET_SIZE(unicode) + 1; |
| 894 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 895 | #ifdef HAVE_USABLE_WCHAR_T |
| 896 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 897 | #else |
| 898 | { |
| 899 | register Py_UNICODE *u; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 900 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 901 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 902 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 903 | *w++ = *u++; |
| 904 | } |
| 905 | #endif |
| 906 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 907 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 908 | return PyUnicode_GET_SIZE(unicode); |
| 909 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 910 | return size; |
| 911 | } |
| 912 | |
| 913 | #endif |
| 914 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 915 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 916 | { |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 917 | Py_UNICODE s[2]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 918 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 919 | if (ordinal < 0 || ordinal > 0x10ffff) { |
| 920 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 921 | "chr() arg not in range(0x110000)"); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 922 | return NULL; |
| 923 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 924 | |
| 925 | #ifndef Py_UNICODE_WIDE |
| 926 | if (ordinal > 0xffff) { |
| 927 | ordinal -= 0x10000; |
| 928 | s[0] = 0xD800 | (ordinal >> 10); |
| 929 | s[1] = 0xDC00 | (ordinal & 0x3FF); |
| 930 | return PyUnicode_FromUnicode(s, 2); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 931 | } |
| 932 | #endif |
| 933 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 934 | s[0] = (Py_UNICODE)ordinal; |
| 935 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 938 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 939 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 940 | /* XXX Perhaps we should make this API an alias of |
| 941 | PyObject_Unicode() instead ?! */ |
| 942 | if (PyUnicode_CheckExact(obj)) { |
| 943 | Py_INCREF(obj); |
| 944 | return obj; |
| 945 | } |
| 946 | if (PyUnicode_Check(obj)) { |
| 947 | /* For a Unicode subtype that's not a Unicode object, |
| 948 | return a true Unicode object with the same data. */ |
| 949 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 950 | PyUnicode_GET_SIZE(obj)); |
| 951 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 952 | return PyUnicode_FromEncodedObject(obj, NULL, "strict"); |
| 953 | } |
| 954 | |
| 955 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
| 956 | const char *encoding, |
| 957 | const char *errors) |
| 958 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 959 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 960 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 961 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 962 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 963 | if (obj == NULL) { |
| 964 | PyErr_BadInternalCall(); |
| 965 | return NULL; |
| 966 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 967 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 968 | if (PyUnicode_Check(obj)) { |
| 969 | PyErr_SetString(PyExc_TypeError, |
| 970 | "decoding Unicode is not supported"); |
| 971 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 972 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 973 | |
| 974 | /* Coerce object */ |
| 975 | if (PyString_Check(obj)) { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 976 | s = PyString_AS_STRING(obj); |
| 977 | len = PyString_GET_SIZE(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 978 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 979 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
| 980 | /* Overwrite the error message with something more useful in |
| 981 | case of a TypeError. */ |
| 982 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 983 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 984 | "coercing to Unicode: need string or buffer, " |
| 985 | "%.80s found", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 986 | Py_Type(obj)->tp_name); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 987 | goto onError; |
| 988 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 989 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 990 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 991 | if (len == 0) { |
| 992 | Py_INCREF(unicode_empty); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 993 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 994 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 995 | else |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 996 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 997 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 998 | return v; |
| 999 | |
| 1000 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1001 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | PyObject *PyUnicode_Decode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1005 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1006 | const char *encoding, |
| 1007 | const char *errors) |
| 1008 | { |
| 1009 | PyObject *buffer = NULL, *unicode; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1010 | |
| 1011 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1012 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1013 | |
| 1014 | /* Shortcuts for common default encodings */ |
| 1015 | if (strcmp(encoding, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1016 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1017 | else if (strcmp(encoding, "latin-1") == 0) |
| 1018 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1019 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1020 | else if (strcmp(encoding, "mbcs") == 0) |
| 1021 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 1022 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1023 | else if (strcmp(encoding, "ascii") == 0) |
| 1024 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1025 | |
| 1026 | /* Decode via the codec registry */ |
| 1027 | buffer = PyBuffer_FromMemory((void *)s, size); |
| 1028 | if (buffer == NULL) |
| 1029 | goto onError; |
| 1030 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1031 | if (unicode == NULL) |
| 1032 | goto onError; |
| 1033 | if (!PyUnicode_Check(unicode)) { |
| 1034 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 1035 | "decoder did not return an unicode object (type=%.400s)", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1036 | Py_Type(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1037 | Py_DECREF(unicode); |
| 1038 | goto onError; |
| 1039 | } |
| 1040 | Py_DECREF(buffer); |
| 1041 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1042 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1043 | onError: |
| 1044 | Py_XDECREF(buffer); |
| 1045 | return NULL; |
| 1046 | } |
| 1047 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1048 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1049 | const char *encoding, |
| 1050 | const char *errors) |
| 1051 | { |
| 1052 | PyObject *v; |
| 1053 | |
| 1054 | if (!PyUnicode_Check(unicode)) { |
| 1055 | PyErr_BadArgument(); |
| 1056 | goto onError; |
| 1057 | } |
| 1058 | |
| 1059 | if (encoding == NULL) |
| 1060 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1061 | |
| 1062 | /* Decode via the codec registry */ |
| 1063 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1064 | if (v == NULL) |
| 1065 | goto onError; |
| 1066 | return v; |
| 1067 | |
| 1068 | onError: |
| 1069 | return NULL; |
| 1070 | } |
| 1071 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1072 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1073 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1074 | const char *encoding, |
| 1075 | const char *errors) |
| 1076 | { |
| 1077 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1078 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1079 | unicode = PyUnicode_FromUnicode(s, size); |
| 1080 | if (unicode == NULL) |
| 1081 | return NULL; |
| 1082 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1083 | Py_DECREF(unicode); |
| 1084 | return v; |
| 1085 | } |
| 1086 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1087 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1088 | const char *encoding, |
| 1089 | const char *errors) |
| 1090 | { |
| 1091 | PyObject *v; |
| 1092 | |
| 1093 | if (!PyUnicode_Check(unicode)) { |
| 1094 | PyErr_BadArgument(); |
| 1095 | goto onError; |
| 1096 | } |
| 1097 | |
| 1098 | if (encoding == NULL) |
| 1099 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1100 | |
| 1101 | /* Encode via the codec registry */ |
| 1102 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1103 | if (v == NULL) |
| 1104 | goto onError; |
| 1105 | return v; |
| 1106 | |
| 1107 | onError: |
| 1108 | return NULL; |
| 1109 | } |
| 1110 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1111 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1112 | const char *encoding, |
| 1113 | const char *errors) |
| 1114 | { |
| 1115 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1116 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1117 | if (!PyUnicode_Check(unicode)) { |
| 1118 | PyErr_BadArgument(); |
| 1119 | goto onError; |
| 1120 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1121 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1122 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1123 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1124 | |
| 1125 | /* Shortcuts for common default encodings */ |
| 1126 | if (errors == NULL) { |
| 1127 | if (strcmp(encoding, "utf-8") == 0) |
Jeremy Hylton | 9cea41c | 2001-05-29 17:13:15 +0000 | [diff] [blame] | 1128 | return PyUnicode_AsUTF8String(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1129 | else if (strcmp(encoding, "latin-1") == 0) |
| 1130 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1131 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1132 | else if (strcmp(encoding, "mbcs") == 0) |
| 1133 | return PyUnicode_AsMBCSString(unicode); |
| 1134 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1135 | else if (strcmp(encoding, "ascii") == 0) |
| 1136 | return PyUnicode_AsASCIIString(unicode); |
| 1137 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1138 | |
| 1139 | /* Encode via the codec registry */ |
| 1140 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1141 | if (v == NULL) |
| 1142 | goto onError; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1143 | if (!PyBytes_Check(v)) { |
| 1144 | if (PyString_Check(v)) { |
| 1145 | /* Old codec, turn it into bytes */ |
| 1146 | PyObject *b = PyBytes_FromObject(v); |
| 1147 | Py_DECREF(v); |
| 1148 | return b; |
| 1149 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1150 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1151 | "encoder did not return a bytes object " |
| 1152 | "(type=%.400s, encoding=%.20s, errors=%.20s)", |
| 1153 | v->ob_type->tp_name, |
| 1154 | encoding ? encoding : "NULL", |
| 1155 | errors ? errors : "NULL"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1156 | Py_DECREF(v); |
| 1157 | goto onError; |
| 1158 | } |
| 1159 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1160 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1161 | onError: |
| 1162 | return NULL; |
| 1163 | } |
| 1164 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1165 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 1166 | const char *errors) |
| 1167 | { |
| 1168 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1169 | PyObject *b; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1170 | if (v) |
| 1171 | return v; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1172 | if (errors != NULL) |
| 1173 | Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString"); |
Guido van Rossum | 0661009 | 2007-08-16 21:02:22 +0000 | [diff] [blame] | 1174 | b = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1175 | PyUnicode_GET_SIZE(unicode), |
| 1176 | NULL); |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1177 | if (!b) |
| 1178 | return NULL; |
| 1179 | v = PyString_FromStringAndSize(PyBytes_AsString(b), |
| 1180 | PyBytes_Size(b)); |
| 1181 | Py_DECREF(b); |
Guido van Rossum | e7a0d39 | 2007-07-12 07:53:00 +0000 | [diff] [blame] | 1182 | ((PyUnicodeObject *)unicode)->defenc = v; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1183 | return v; |
| 1184 | } |
| 1185 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1186 | char* |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1187 | PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1188 | { |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1189 | PyObject *str8; |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 1190 | if (!PyUnicode_Check(unicode)) { |
| 1191 | PyErr_BadArgument(); |
| 1192 | return NULL; |
| 1193 | } |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1194 | str8 = _PyUnicode_AsDefaultEncodedString(unicode, NULL); |
| 1195 | if (str8 == NULL) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1196 | return NULL; |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1197 | if (psize != NULL) |
| 1198 | *psize = PyString_GET_SIZE(str8); |
| 1199 | return PyString_AS_STRING(str8); |
| 1200 | } |
| 1201 | |
| 1202 | char* |
| 1203 | PyUnicode_AsString(PyObject *unicode) |
| 1204 | { |
| 1205 | return PyUnicode_AsStringAndSize(unicode, NULL); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1208 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1209 | { |
| 1210 | if (!PyUnicode_Check(unicode)) { |
| 1211 | PyErr_BadArgument(); |
| 1212 | goto onError; |
| 1213 | } |
| 1214 | return PyUnicode_AS_UNICODE(unicode); |
| 1215 | |
| 1216 | onError: |
| 1217 | return NULL; |
| 1218 | } |
| 1219 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1220 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1221 | { |
| 1222 | if (!PyUnicode_Check(unicode)) { |
| 1223 | PyErr_BadArgument(); |
| 1224 | goto onError; |
| 1225 | } |
| 1226 | return PyUnicode_GET_SIZE(unicode); |
| 1227 | |
| 1228 | onError: |
| 1229 | return -1; |
| 1230 | } |
| 1231 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1232 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1233 | { |
| 1234 | return unicode_default_encoding; |
| 1235 | } |
| 1236 | |
| 1237 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 1238 | { |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1239 | if (strcmp(encoding, unicode_default_encoding) != 0) { |
| 1240 | PyErr_Format(PyExc_ValueError, |
| 1241 | "Can only set default encoding to %s", |
| 1242 | unicode_default_encoding); |
| 1243 | return -1; |
| 1244 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1245 | return 0; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1248 | /* error handling callback helper: |
| 1249 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1250 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1251 | and adjust various state variables. |
| 1252 | return 0 on success, -1 on error |
| 1253 | */ |
| 1254 | |
| 1255 | static |
| 1256 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 1257 | const char *encoding, const char *reason, |
Walter Dörwald | a651d3d | 2007-08-30 15:29:21 +0000 | [diff] [blame^] | 1258 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 1259 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1260 | PyObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1261 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1262 | 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] | 1263 | |
| 1264 | PyObject *restuple = NULL; |
| 1265 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1266 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1267 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1268 | Py_ssize_t requiredsize; |
| 1269 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1270 | Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1271 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1272 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1273 | int res = -1; |
| 1274 | |
| 1275 | if (*errorHandler == NULL) { |
| 1276 | *errorHandler = PyCodec_LookupError(errors); |
| 1277 | if (*errorHandler == NULL) |
| 1278 | goto onError; |
| 1279 | } |
| 1280 | |
| 1281 | if (*exceptionObject == NULL) { |
| 1282 | *exceptionObject = PyUnicodeDecodeError_Create( |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1283 | encoding, *input, *inend-*input, *startinpos, *endinpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1284 | if (*exceptionObject == NULL) |
| 1285 | goto onError; |
| 1286 | } |
| 1287 | else { |
| 1288 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 1289 | goto onError; |
| 1290 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 1291 | goto onError; |
| 1292 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1293 | goto onError; |
| 1294 | } |
| 1295 | |
| 1296 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1297 | if (restuple == NULL) |
| 1298 | goto onError; |
| 1299 | if (!PyTuple_Check(restuple)) { |
| 1300 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 1301 | goto onError; |
| 1302 | } |
| 1303 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 1304 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1305 | |
| 1306 | /* Copy back the bytes variables, which might have been modified by the |
| 1307 | callback */ |
| 1308 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 1309 | if (!inputobj) |
| 1310 | goto onError; |
| 1311 | if (!PyBytes_Check(inputobj)) { |
| 1312 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
| 1313 | } |
| 1314 | *input = PyBytes_AS_STRING(inputobj); |
| 1315 | insize = PyBytes_GET_SIZE(inputobj); |
| 1316 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 1317 | /* we can DECREF safely, as the exception has another reference, |
| 1318 | so the object won't go away. */ |
| 1319 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1320 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1321 | if (newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1322 | newpos = insize+newpos; |
| 1323 | if (newpos<0 || newpos>insize) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 1324 | 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] | 1325 | goto onError; |
| 1326 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1327 | |
| 1328 | /* need more space? (at least enough for what we |
| 1329 | have+the replacement+the rest of the string (starting |
| 1330 | at the new input position), so we won't have to check space |
| 1331 | when there are no errors in the rest of the string) */ |
| 1332 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 1333 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 1334 | requiredsize = *outpos + repsize + insize-newpos; |
| 1335 | if (requiredsize > outsize) { |
| 1336 | if (requiredsize<2*outsize) |
| 1337 | requiredsize = 2*outsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1338 | if (PyUnicode_Resize(output, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1339 | goto onError; |
| 1340 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
| 1341 | } |
| 1342 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1343 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1344 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 1345 | *outptr += repsize; |
| 1346 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1347 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1348 | /* we made it! */ |
| 1349 | res = 0; |
| 1350 | |
| 1351 | onError: |
| 1352 | Py_XDECREF(restuple); |
| 1353 | return res; |
| 1354 | } |
| 1355 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1356 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 1357 | |
| 1358 | /* see RFC2152 for details */ |
| 1359 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1360 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1361 | char utf7_special[128] = { |
| 1362 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 1363 | encoded: |
| 1364 | 0 - not special |
| 1365 | 1 - special |
| 1366 | 2 - whitespace (optional) |
| 1367 | 3 - RFC2152 Set O (optional) */ |
| 1368 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 1369 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1370 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 1371 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 1372 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1373 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 1374 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1375 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 1376 | |
| 1377 | }; |
| 1378 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1379 | /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
| 1380 | warnings about the comparison always being false; since |
| 1381 | utf7_special[0] is 1, we can safely make that one comparison |
| 1382 | true */ |
| 1383 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1384 | #define SPECIAL(c, encodeO, encodeWS) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1385 | ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1386 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1387 | (encodeO && (utf7_special[(c)] == 3))) |
| 1388 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1389 | #define B64(n) \ |
| 1390 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 1391 | #define B64CHAR(c) \ |
| 1392 | (isalnum(c) || (c) == '+' || (c) == '/') |
| 1393 | #define UB64(c) \ |
| 1394 | ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
| 1395 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1396 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1397 | #define ENCODE(out, ch, bits) \ |
| 1398 | while (bits >= 6) { \ |
| 1399 | *out++ = B64(ch >> (bits-6)); \ |
| 1400 | bits -= 6; \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1403 | #define DECODE(out, ch, bits, surrogate) \ |
| 1404 | while (bits >= 16) { \ |
| 1405 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 1406 | bits -= 16; \ |
| 1407 | if (surrogate) { \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1408 | /* We have already generated an error for the high surrogate \ |
| 1409 | 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] | 1410 | surrogate = 0; \ |
| 1411 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1412 | /* This is a surrogate pair. Unfortunately we can't represent \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1413 | it in a 16-bit character */ \ |
| 1414 | surrogate = 1; \ |
| 1415 | errmsg = "code pairs are not supported"; \ |
| 1416 | goto utf7Error; \ |
| 1417 | } else { \ |
| 1418 | *out++ = outCh; \ |
| 1419 | } \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1420 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1421 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1422 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1423 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1424 | const char *errors) |
| 1425 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1426 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1427 | Py_ssize_t startinpos; |
| 1428 | Py_ssize_t endinpos; |
| 1429 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1430 | const char *e; |
| 1431 | PyUnicodeObject *unicode; |
| 1432 | Py_UNICODE *p; |
| 1433 | const char *errmsg = ""; |
| 1434 | int inShift = 0; |
| 1435 | unsigned int bitsleft = 0; |
| 1436 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1437 | int surrogate = 0; |
| 1438 | PyObject *errorHandler = NULL; |
| 1439 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1440 | |
| 1441 | unicode = _PyUnicode_New(size); |
| 1442 | if (!unicode) |
| 1443 | return NULL; |
| 1444 | if (size == 0) |
| 1445 | return (PyObject *)unicode; |
| 1446 | |
| 1447 | p = unicode->str; |
| 1448 | e = s + size; |
| 1449 | |
| 1450 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1451 | Py_UNICODE ch; |
| 1452 | restart: |
| 1453 | ch = *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1454 | |
| 1455 | if (inShift) { |
| 1456 | if ((ch == '-') || !B64CHAR(ch)) { |
| 1457 | inShift = 0; |
| 1458 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1459 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1460 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1461 | if (bitsleft >= 6) { |
| 1462 | /* The shift sequence has a partial character in it. If |
| 1463 | bitsleft < 6 then we could just classify it as padding |
| 1464 | but that is not the case here */ |
| 1465 | |
| 1466 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1467 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1468 | } |
| 1469 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1470 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1471 | here so indicate the potential of a misencoded character. */ |
| 1472 | |
| 1473 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 1474 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 1475 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1476 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | if (ch == '-') { |
| 1480 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1481 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1482 | inShift = 1; |
| 1483 | } |
| 1484 | } else if (SPECIAL(ch,0,0)) { |
| 1485 | errmsg = "unexpected special character"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1486 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1487 | } else { |
| 1488 | *p++ = ch; |
| 1489 | } |
| 1490 | } else { |
| 1491 | charsleft = (charsleft << 6) | UB64(ch); |
| 1492 | bitsleft += 6; |
| 1493 | s++; |
| 1494 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1495 | } |
| 1496 | } |
| 1497 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1498 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1499 | s++; |
| 1500 | if (s < e && *s == '-') { |
| 1501 | s++; |
| 1502 | *p++ = '+'; |
| 1503 | } else |
| 1504 | { |
| 1505 | inShift = 1; |
| 1506 | bitsleft = 0; |
| 1507 | } |
| 1508 | } |
| 1509 | else if (SPECIAL(ch,0,0)) { |
| 1510 | errmsg = "unexpected special character"; |
| 1511 | s++; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1512 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1513 | } |
| 1514 | else { |
| 1515 | *p++ = ch; |
| 1516 | s++; |
| 1517 | } |
| 1518 | continue; |
| 1519 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1520 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1521 | endinpos = s-starts; |
| 1522 | if (unicode_decode_call_errorhandler( |
| 1523 | errors, &errorHandler, |
| 1524 | "utf7", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1525 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1526 | (PyObject **)&unicode, &outpos, &p)) |
| 1527 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | if (inShift) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1531 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1532 | endinpos = size; |
| 1533 | if (unicode_decode_call_errorhandler( |
| 1534 | errors, &errorHandler, |
| 1535 | "utf7", "unterminated shift sequence", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1536 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1537 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1538 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1539 | if (s < e) |
| 1540 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1543 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1544 | goto onError; |
| 1545 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1546 | Py_XDECREF(errorHandler); |
| 1547 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1548 | return (PyObject *)unicode; |
| 1549 | |
| 1550 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1551 | Py_XDECREF(errorHandler); |
| 1552 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1553 | Py_DECREF(unicode); |
| 1554 | return NULL; |
| 1555 | } |
| 1556 | |
| 1557 | |
| 1558 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1559 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1560 | int encodeSetO, |
| 1561 | int encodeWhiteSpace, |
| 1562 | const char *errors) |
| 1563 | { |
| 1564 | PyObject *v; |
| 1565 | /* It might be possible to tighten this worst case */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1566 | Py_ssize_t cbAllocated = 5 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1567 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1568 | Py_ssize_t i = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1569 | unsigned int bitsleft = 0; |
| 1570 | unsigned long charsleft = 0; |
| 1571 | char * out; |
| 1572 | char * start; |
| 1573 | |
| 1574 | if (size == 0) |
Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1575 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1576 | |
Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1577 | v = PyBytes_FromStringAndSize(NULL, cbAllocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1578 | if (v == NULL) |
| 1579 | return NULL; |
| 1580 | |
Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1581 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1582 | for (;i < size; ++i) { |
| 1583 | Py_UNICODE ch = s[i]; |
| 1584 | |
| 1585 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1586 | if (ch == '+') { |
| 1587 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1588 | *out++ = '-'; |
| 1589 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1590 | charsleft = ch; |
| 1591 | bitsleft = 16; |
| 1592 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1593 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1594 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1595 | } else { |
| 1596 | *out++ = (char) ch; |
| 1597 | } |
| 1598 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1599 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1600 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1601 | charsleft = 0; |
| 1602 | bitsleft = 0; |
| 1603 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1604 | so no '-' is required, except if the character is itself a '-' */ |
| 1605 | if (B64CHAR(ch) || ch == '-') { |
| 1606 | *out++ = '-'; |
| 1607 | } |
| 1608 | inShift = 0; |
| 1609 | *out++ = (char) ch; |
| 1610 | } else { |
| 1611 | bitsleft += 16; |
| 1612 | charsleft = (charsleft << 16) | ch; |
| 1613 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1614 | |
| 1615 | /* If the next character is special then we dont' need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1616 | 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] | 1617 | or '-' then the shift sequence will be terminated implicitly and we |
| 1618 | don't have to insert a '-'. */ |
| 1619 | |
| 1620 | if (bitsleft == 0) { |
| 1621 | if (i + 1 < size) { |
| 1622 | Py_UNICODE ch2 = s[i+1]; |
| 1623 | |
| 1624 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1625 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1626 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1627 | *out++ = '-'; |
| 1628 | inShift = 0; |
| 1629 | } else { |
| 1630 | inShift = 0; |
| 1631 | } |
| 1632 | |
| 1633 | } |
| 1634 | else { |
| 1635 | *out++ = '-'; |
| 1636 | inShift = 0; |
| 1637 | } |
| 1638 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1639 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1640 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1641 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1642 | if (bitsleft) { |
| 1643 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1644 | *out++ = '-'; |
| 1645 | } |
| 1646 | |
Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1647 | if (PyBytes_Resize(v, out - start)) { |
| 1648 | Py_DECREF(v); |
| 1649 | return NULL; |
| 1650 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1651 | return v; |
| 1652 | } |
| 1653 | |
| 1654 | #undef SPECIAL |
| 1655 | #undef B64 |
| 1656 | #undef B64CHAR |
| 1657 | #undef UB64 |
| 1658 | #undef ENCODE |
| 1659 | #undef DECODE |
| 1660 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1661 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1662 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1663 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1664 | char utf8_code_length[256] = { |
| 1665 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1666 | illegal prefix. see RFC 2279 for details */ |
| 1667 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1668 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1669 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1670 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1671 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1672 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1673 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1674 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1675 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1676 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1677 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1678 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1679 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1680 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1681 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1682 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1683 | }; |
| 1684 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1685 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1686 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1687 | const char *errors) |
| 1688 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1689 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 1690 | } |
| 1691 | |
| 1692 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1693 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1694 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1695 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1696 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1697 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1698 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1699 | Py_ssize_t startinpos; |
| 1700 | Py_ssize_t endinpos; |
| 1701 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1702 | const char *e; |
| 1703 | PyUnicodeObject *unicode; |
| 1704 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1705 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1706 | PyObject *errorHandler = NULL; |
| 1707 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1708 | |
| 1709 | /* Note: size will always be longer than the resulting Unicode |
| 1710 | character count */ |
| 1711 | unicode = _PyUnicode_New(size); |
| 1712 | if (!unicode) |
| 1713 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1714 | if (size == 0) { |
| 1715 | if (consumed) |
| 1716 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1717 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1718 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1719 | |
| 1720 | /* Unpack UTF-8 encoded data */ |
| 1721 | p = unicode->str; |
| 1722 | e = s + size; |
| 1723 | |
| 1724 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1725 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1726 | |
| 1727 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1728 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1729 | s++; |
| 1730 | continue; |
| 1731 | } |
| 1732 | |
| 1733 | n = utf8_code_length[ch]; |
| 1734 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1735 | if (s + n > e) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1736 | if (consumed) |
| 1737 | break; |
| 1738 | else { |
| 1739 | errmsg = "unexpected end of data"; |
| 1740 | startinpos = s-starts; |
| 1741 | endinpos = size; |
| 1742 | goto utf8Error; |
| 1743 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1744 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1745 | |
| 1746 | switch (n) { |
| 1747 | |
| 1748 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1749 | errmsg = "unexpected code byte"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1750 | startinpos = s-starts; |
| 1751 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1752 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1753 | |
| 1754 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1755 | errmsg = "internal error"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1756 | startinpos = s-starts; |
| 1757 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1758 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1759 | |
| 1760 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1761 | if ((s[1] & 0xc0) != 0x80) { |
| 1762 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1763 | startinpos = s-starts; |
| 1764 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1765 | goto utf8Error; |
| 1766 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1767 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1768 | if (ch < 0x80) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1769 | startinpos = s-starts; |
| 1770 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1771 | errmsg = "illegal encoding"; |
| 1772 | goto utf8Error; |
| 1773 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1774 | else |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1775 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1776 | break; |
| 1777 | |
| 1778 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1779 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1780 | (s[2] & 0xc0) != 0x80) { |
| 1781 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1782 | startinpos = s-starts; |
| 1783 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1784 | goto utf8Error; |
| 1785 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1786 | 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] | 1787 | if (ch < 0x0800) { |
| 1788 | /* Note: UTF-8 encodings of surrogates are considered |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1789 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1790 | |
| 1791 | XXX For wide builds (UCS-4) we should probably try |
| 1792 | to recombine the surrogates into a single code |
| 1793 | unit. |
| 1794 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1795 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1796 | startinpos = s-starts; |
| 1797 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1798 | goto utf8Error; |
| 1799 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1800 | else |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1801 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1802 | break; |
| 1803 | |
| 1804 | case 4: |
| 1805 | if ((s[1] & 0xc0) != 0x80 || |
| 1806 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1807 | (s[3] & 0xc0) != 0x80) { |
| 1808 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1809 | startinpos = s-starts; |
| 1810 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1811 | goto utf8Error; |
| 1812 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1813 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 1814 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 1815 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1816 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1817 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1818 | || (ch > 0x10ffff)) /* maximum value allowed for |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1819 | UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1820 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1821 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1822 | startinpos = s-starts; |
| 1823 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1824 | goto utf8Error; |
| 1825 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1826 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1827 | *p++ = (Py_UNICODE)ch; |
| 1828 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1829 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1830 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1831 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 1832 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1833 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1834 | /* high surrogate = top 10 bits added to D800 */ |
| 1835 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1836 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1837 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1838 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1839 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1840 | break; |
| 1841 | |
| 1842 | default: |
| 1843 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1844 | errmsg = "unsupported Unicode code range"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1845 | startinpos = s-starts; |
| 1846 | endinpos = startinpos+n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1847 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1848 | } |
| 1849 | s += n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1850 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1851 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1852 | utf8Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1853 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1854 | if (unicode_decode_call_errorhandler( |
| 1855 | errors, &errorHandler, |
| 1856 | "utf8", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1857 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1858 | (PyObject **)&unicode, &outpos, &p)) |
| 1859 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1860 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1861 | if (consumed) |
| 1862 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1863 | |
| 1864 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1865 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1866 | goto onError; |
| 1867 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1868 | Py_XDECREF(errorHandler); |
| 1869 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1870 | return (PyObject *)unicode; |
| 1871 | |
| 1872 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1873 | Py_XDECREF(errorHandler); |
| 1874 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1875 | Py_DECREF(unicode); |
| 1876 | return NULL; |
| 1877 | } |
| 1878 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1879 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 1880 | and allocate exactly as much space needed at the end. Else allocate the |
| 1881 | maximum possible needed (4 result bytes per Unicode character), and return |
| 1882 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1883 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1884 | PyObject * |
| 1885 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1886 | Py_ssize_t size, |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1887 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1888 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1889 | #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] | 1890 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1891 | Py_ssize_t i; /* index into s of next input byte */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1892 | PyObject *v; /* result string object */ |
| 1893 | char *p; /* next free byte in output buffer */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1894 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1895 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1896 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1897 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1898 | assert(s != NULL); |
| 1899 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1900 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1901 | if (size <= MAX_SHORT_UNICHARS) { |
| 1902 | /* Write into the stack buffer; nallocated can't overflow. |
| 1903 | * At the end, we'll allocate exactly as much heap space as it |
| 1904 | * turns out we need. |
| 1905 | */ |
| 1906 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
| 1907 | v = NULL; /* will allocate after we're done */ |
| 1908 | p = stackbuf; |
| 1909 | } |
| 1910 | else { |
| 1911 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 1912 | nallocated = size * 4; |
| 1913 | if (nallocated / 4 != size) /* overflow! */ |
| 1914 | return PyErr_NoMemory(); |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1915 | v = PyBytes_FromStringAndSize(NULL, nallocated); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1916 | if (v == NULL) |
| 1917 | return NULL; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1918 | p = PyBytes_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1919 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1920 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1921 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1922 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1923 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1924 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1925 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1926 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1927 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1928 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1929 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 1930 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 1931 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1932 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1933 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1934 | /* Encode UCS2 Unicode ordinals */ |
| 1935 | if (ch < 0x10000) { |
| 1936 | /* Special case: check for high surrogate */ |
| 1937 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 1938 | Py_UCS4 ch2 = s[i]; |
| 1939 | /* Check for low surrogate and combine the two to |
| 1940 | form a UCS4 value */ |
| 1941 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1942 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1943 | i++; |
| 1944 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1945 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1946 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1947 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1948 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1949 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1950 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1951 | continue; |
| 1952 | } |
| 1953 | encodeUCS4: |
| 1954 | /* Encode UCS4 Unicode ordinals */ |
| 1955 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 1956 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 1957 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1958 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1959 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1960 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 1961 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1962 | if (v == NULL) { |
| 1963 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1964 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1965 | assert(nneeded <= nallocated); |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1966 | v = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1967 | } |
| 1968 | else { |
| 1969 | /* Cut back to size actually needed. */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1970 | nneeded = p - PyBytes_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1971 | assert(nneeded <= nallocated); |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1972 | PyBytes_Resize(v, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1973 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1974 | return v; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1975 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1976 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1977 | } |
| 1978 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1979 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 1980 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1981 | if (!PyUnicode_Check(unicode)) { |
| 1982 | PyErr_BadArgument(); |
| 1983 | return NULL; |
| 1984 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 1985 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1986 | PyUnicode_GET_SIZE(unicode), |
| 1987 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1988 | } |
| 1989 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 1990 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 1991 | |
| 1992 | PyObject * |
| 1993 | PyUnicode_DecodeUTF32(const char *s, |
| 1994 | Py_ssize_t size, |
| 1995 | const char *errors, |
| 1996 | int *byteorder) |
| 1997 | { |
| 1998 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 1999 | } |
| 2000 | |
| 2001 | PyObject * |
| 2002 | PyUnicode_DecodeUTF32Stateful(const char *s, |
| 2003 | Py_ssize_t size, |
| 2004 | const char *errors, |
| 2005 | int *byteorder, |
| 2006 | Py_ssize_t *consumed) |
| 2007 | { |
| 2008 | const char *starts = s; |
| 2009 | Py_ssize_t startinpos; |
| 2010 | Py_ssize_t endinpos; |
| 2011 | Py_ssize_t outpos; |
| 2012 | PyUnicodeObject *unicode; |
| 2013 | Py_UNICODE *p; |
| 2014 | #ifndef Py_UNICODE_WIDE |
| 2015 | int i, pairs; |
| 2016 | #else |
| 2017 | const int pairs = 0; |
| 2018 | #endif |
| 2019 | const unsigned char *q, *e; |
| 2020 | int bo = 0; /* assume native ordering by default */ |
| 2021 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2022 | /* Offsets from q for retrieving bytes in the right order. */ |
| 2023 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2024 | int iorder[] = {0, 1, 2, 3}; |
| 2025 | #else |
| 2026 | int iorder[] = {3, 2, 1, 0}; |
| 2027 | #endif |
| 2028 | PyObject *errorHandler = NULL; |
| 2029 | PyObject *exc = NULL; |
Guido van Rossum | 8d991ed | 2007-08-17 15:41:00 +0000 | [diff] [blame] | 2030 | /* On narrow builds we split characters outside the BMP into two |
| 2031 | codepoints => count how much extra space we need. */ |
| 2032 | #ifndef Py_UNICODE_WIDE |
| 2033 | for (i = pairs = 0; i < size/4; i++) |
| 2034 | if (((Py_UCS4 *)s)[i] >= 0x10000) |
| 2035 | pairs++; |
| 2036 | #endif |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2037 | |
| 2038 | /* This might be one to much, because of a BOM */ |
| 2039 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 2040 | if (!unicode) |
| 2041 | return NULL; |
| 2042 | if (size == 0) |
| 2043 | return (PyObject *)unicode; |
| 2044 | |
| 2045 | /* Unpack UTF-32 encoded data */ |
| 2046 | p = unicode->str; |
| 2047 | q = (unsigned char *)s; |
| 2048 | e = q + size; |
| 2049 | |
| 2050 | if (byteorder) |
| 2051 | bo = *byteorder; |
| 2052 | |
| 2053 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2054 | byte order setting accordingly. In native mode, the leading BOM |
| 2055 | mark is skipped, in all other modes, it is copied to the output |
| 2056 | stream as-is (giving a ZWNBSP character). */ |
| 2057 | if (bo == 0) { |
| 2058 | if (size >= 4) { |
| 2059 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2060 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 2061 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2062 | if (bom == 0x0000FEFF) { |
| 2063 | q += 4; |
| 2064 | bo = -1; |
| 2065 | } |
| 2066 | else if (bom == 0xFFFE0000) { |
| 2067 | q += 4; |
| 2068 | bo = 1; |
| 2069 | } |
| 2070 | #else |
| 2071 | if (bom == 0x0000FEFF) { |
| 2072 | q += 4; |
| 2073 | bo = 1; |
| 2074 | } |
| 2075 | else if (bom == 0xFFFE0000) { |
| 2076 | q += 4; |
| 2077 | bo = -1; |
| 2078 | } |
| 2079 | #endif |
| 2080 | } |
| 2081 | } |
| 2082 | |
| 2083 | if (bo == -1) { |
| 2084 | /* force LE */ |
| 2085 | iorder[0] = 0; |
| 2086 | iorder[1] = 1; |
| 2087 | iorder[2] = 2; |
| 2088 | iorder[3] = 3; |
| 2089 | } |
| 2090 | else if (bo == 1) { |
| 2091 | /* force BE */ |
| 2092 | iorder[0] = 3; |
| 2093 | iorder[1] = 2; |
| 2094 | iorder[2] = 1; |
| 2095 | iorder[3] = 0; |
| 2096 | } |
| 2097 | |
| 2098 | while (q < e) { |
| 2099 | Py_UCS4 ch; |
| 2100 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 2101 | if (e-q<4) { |
| 2102 | if (consumed) |
| 2103 | break; |
| 2104 | errmsg = "truncated data"; |
| 2105 | startinpos = ((const char *)q)-starts; |
| 2106 | endinpos = ((const char *)e)-starts; |
| 2107 | goto utf32Error; |
| 2108 | /* The remaining input chars are ignored if the callback |
| 2109 | chooses to skip the input */ |
| 2110 | } |
| 2111 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2112 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 2113 | |
| 2114 | if (ch >= 0x110000) |
| 2115 | { |
| 2116 | errmsg = "codepoint not in range(0x110000)"; |
| 2117 | startinpos = ((const char *)q)-starts; |
| 2118 | endinpos = startinpos+4; |
| 2119 | goto utf32Error; |
| 2120 | } |
| 2121 | #ifndef Py_UNICODE_WIDE |
| 2122 | if (ch >= 0x10000) |
| 2123 | { |
| 2124 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 2125 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2126 | } |
| 2127 | else |
| 2128 | #endif |
| 2129 | *p++ = ch; |
| 2130 | q += 4; |
| 2131 | continue; |
| 2132 | utf32Error: |
| 2133 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2134 | if (unicode_decode_call_errorhandler( |
| 2135 | errors, &errorHandler, |
| 2136 | "utf32", errmsg, |
| 2137 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 2138 | (PyObject **)&unicode, &outpos, &p)) |
| 2139 | goto onError; |
| 2140 | } |
| 2141 | |
| 2142 | if (byteorder) |
| 2143 | *byteorder = bo; |
| 2144 | |
| 2145 | if (consumed) |
| 2146 | *consumed = (const char *)q-starts; |
| 2147 | |
| 2148 | /* Adjust length */ |
| 2149 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 2150 | goto onError; |
| 2151 | |
| 2152 | Py_XDECREF(errorHandler); |
| 2153 | Py_XDECREF(exc); |
| 2154 | return (PyObject *)unicode; |
| 2155 | |
| 2156 | onError: |
| 2157 | Py_DECREF(unicode); |
| 2158 | Py_XDECREF(errorHandler); |
| 2159 | Py_XDECREF(exc); |
| 2160 | return NULL; |
| 2161 | } |
| 2162 | |
| 2163 | PyObject * |
| 2164 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 2165 | Py_ssize_t size, |
| 2166 | const char *errors, |
| 2167 | int byteorder) |
| 2168 | { |
| 2169 | PyObject *v; |
| 2170 | unsigned char *p; |
| 2171 | #ifndef Py_UNICODE_WIDE |
| 2172 | int i, pairs; |
| 2173 | #else |
| 2174 | const int pairs = 0; |
| 2175 | #endif |
| 2176 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2177 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2178 | int iorder[] = {0, 1, 2, 3}; |
| 2179 | #else |
| 2180 | int iorder[] = {3, 2, 1, 0}; |
| 2181 | #endif |
| 2182 | |
| 2183 | #define STORECHAR(CH) \ |
| 2184 | do { \ |
| 2185 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 2186 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 2187 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 2188 | p[iorder[0]] = (CH) & 0xff; \ |
| 2189 | p += 4; \ |
| 2190 | } while(0) |
| 2191 | |
| 2192 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 2193 | so we need less space. */ |
| 2194 | #ifndef Py_UNICODE_WIDE |
| 2195 | for (i = pairs = 0; i < size-1; i++) |
| 2196 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2197 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 2198 | pairs++; |
| 2199 | #endif |
| 2200 | v = PyBytes_FromStringAndSize(NULL, |
| 2201 | 4 * (size - pairs + (byteorder == 0))); |
| 2202 | if (v == NULL) |
| 2203 | return NULL; |
| 2204 | |
| 2205 | p = (unsigned char *)PyBytes_AS_STRING(v); |
| 2206 | if (byteorder == 0) |
| 2207 | STORECHAR(0xFEFF); |
| 2208 | if (size == 0) |
| 2209 | return v; |
| 2210 | |
| 2211 | if (byteorder == -1) { |
| 2212 | /* force LE */ |
| 2213 | iorder[0] = 0; |
| 2214 | iorder[1] = 1; |
| 2215 | iorder[2] = 2; |
| 2216 | iorder[3] = 3; |
| 2217 | } |
| 2218 | else if (byteorder == 1) { |
| 2219 | /* force BE */ |
| 2220 | iorder[0] = 3; |
| 2221 | iorder[1] = 2; |
| 2222 | iorder[2] = 1; |
| 2223 | iorder[3] = 0; |
| 2224 | } |
| 2225 | |
| 2226 | while (size-- > 0) { |
| 2227 | Py_UCS4 ch = *s++; |
| 2228 | #ifndef Py_UNICODE_WIDE |
| 2229 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 2230 | Py_UCS4 ch2 = *s; |
| 2231 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 2232 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 2233 | s++; |
| 2234 | size--; |
| 2235 | } |
| 2236 | } |
| 2237 | #endif |
| 2238 | STORECHAR(ch); |
| 2239 | } |
| 2240 | return v; |
| 2241 | #undef STORECHAR |
| 2242 | } |
| 2243 | |
| 2244 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 2245 | { |
| 2246 | if (!PyUnicode_Check(unicode)) { |
| 2247 | PyErr_BadArgument(); |
| 2248 | return NULL; |
| 2249 | } |
| 2250 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
| 2251 | PyUnicode_GET_SIZE(unicode), |
| 2252 | NULL, |
| 2253 | 0); |
| 2254 | } |
| 2255 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2256 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 2257 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2258 | PyObject * |
| 2259 | PyUnicode_DecodeUTF16(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2260 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2261 | const char *errors, |
| 2262 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2263 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2264 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 2265 | } |
| 2266 | |
| 2267 | PyObject * |
| 2268 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2269 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2270 | const char *errors, |
| 2271 | int *byteorder, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2272 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2273 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2274 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2275 | Py_ssize_t startinpos; |
| 2276 | Py_ssize_t endinpos; |
| 2277 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2278 | PyUnicodeObject *unicode; |
| 2279 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2280 | const unsigned char *q, *e; |
| 2281 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2282 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2283 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 2284 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2285 | int ihi = 1, ilo = 0; |
| 2286 | #else |
| 2287 | int ihi = 0, ilo = 1; |
| 2288 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2289 | PyObject *errorHandler = NULL; |
| 2290 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2291 | |
| 2292 | /* Note: size will always be longer than the resulting Unicode |
| 2293 | character count */ |
| 2294 | unicode = _PyUnicode_New(size); |
| 2295 | if (!unicode) |
| 2296 | return NULL; |
| 2297 | if (size == 0) |
| 2298 | return (PyObject *)unicode; |
| 2299 | |
| 2300 | /* Unpack UTF-16 encoded data */ |
| 2301 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2302 | q = (unsigned char *)s; |
| 2303 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2304 | |
| 2305 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2306 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2307 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2308 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2309 | byte order setting accordingly. In native mode, the leading BOM |
| 2310 | mark is skipped, in all other modes, it is copied to the output |
| 2311 | stream as-is (giving a ZWNBSP character). */ |
| 2312 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2313 | if (size >= 2) { |
| 2314 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2315 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2316 | if (bom == 0xFEFF) { |
| 2317 | q += 2; |
| 2318 | bo = -1; |
| 2319 | } |
| 2320 | else if (bom == 0xFFFE) { |
| 2321 | q += 2; |
| 2322 | bo = 1; |
| 2323 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2324 | #else |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2325 | if (bom == 0xFEFF) { |
| 2326 | q += 2; |
| 2327 | bo = 1; |
| 2328 | } |
| 2329 | else if (bom == 0xFFFE) { |
| 2330 | q += 2; |
| 2331 | bo = -1; |
| 2332 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2333 | #endif |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2334 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2335 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2336 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2337 | if (bo == -1) { |
| 2338 | /* force LE */ |
| 2339 | ihi = 1; |
| 2340 | ilo = 0; |
| 2341 | } |
| 2342 | else if (bo == 1) { |
| 2343 | /* force BE */ |
| 2344 | ihi = 0; |
| 2345 | ilo = 1; |
| 2346 | } |
| 2347 | |
| 2348 | while (q < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2349 | Py_UNICODE ch; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2350 | /* remaining bytes at the end? (size should be even) */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2351 | if (e-q<2) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2352 | if (consumed) |
| 2353 | break; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2354 | errmsg = "truncated data"; |
| 2355 | startinpos = ((const char *)q)-starts; |
| 2356 | endinpos = ((const char *)e)-starts; |
| 2357 | goto utf16Error; |
| 2358 | /* The remaining input chars are ignored if the callback |
| 2359 | chooses to skip the input */ |
| 2360 | } |
| 2361 | ch = (q[ihi] << 8) | q[ilo]; |
| 2362 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2363 | q += 2; |
| 2364 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2365 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 2366 | *p++ = ch; |
| 2367 | continue; |
| 2368 | } |
| 2369 | |
| 2370 | /* UTF-16 code pair: */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2371 | if (q >= e) { |
| 2372 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2373 | startinpos = (((const char *)q)-2)-starts; |
| 2374 | endinpos = ((const char *)e)-starts; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2375 | goto utf16Error; |
| 2376 | } |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2377 | if (0xD800 <= ch && ch <= 0xDBFF) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2378 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 2379 | q += 2; |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2380 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2381 | #ifndef Py_UNICODE_WIDE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2382 | *p++ = ch; |
| 2383 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2384 | #else |
| 2385 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2386 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2387 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2388 | } |
| 2389 | else { |
| 2390 | errmsg = "illegal UTF-16 surrogate"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2391 | startinpos = (((const char *)q)-4)-starts; |
| 2392 | endinpos = startinpos+2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2393 | goto utf16Error; |
| 2394 | } |
| 2395 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2396 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2397 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2398 | startinpos = (((const char *)q)-2)-starts; |
| 2399 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2400 | /* Fall through to report the error */ |
| 2401 | |
| 2402 | utf16Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2403 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2404 | if (unicode_decode_call_errorhandler( |
| 2405 | errors, &errorHandler, |
| 2406 | "utf16", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2407 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2408 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2409 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2410 | } |
| 2411 | |
| 2412 | if (byteorder) |
| 2413 | *byteorder = bo; |
| 2414 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2415 | if (consumed) |
| 2416 | *consumed = (const char *)q-starts; |
| 2417 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2418 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2419 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2420 | goto onError; |
| 2421 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2422 | Py_XDECREF(errorHandler); |
| 2423 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2424 | return (PyObject *)unicode; |
| 2425 | |
| 2426 | onError: |
| 2427 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2428 | Py_XDECREF(errorHandler); |
| 2429 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2430 | return NULL; |
| 2431 | } |
| 2432 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2433 | PyObject * |
| 2434 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2435 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2436 | const char *errors, |
| 2437 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2438 | { |
| 2439 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2440 | unsigned char *p; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2441 | #ifdef Py_UNICODE_WIDE |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2442 | int i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2443 | #else |
| 2444 | const int pairs = 0; |
| 2445 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2446 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2447 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2448 | int ihi = 1, ilo = 0; |
| 2449 | #else |
| 2450 | int ihi = 0, ilo = 1; |
| 2451 | #endif |
| 2452 | |
| 2453 | #define STORECHAR(CH) \ |
| 2454 | do { \ |
| 2455 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 2456 | p[ilo] = (CH) & 0xff; \ |
| 2457 | p += 2; \ |
| 2458 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2459 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2460 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2461 | for (i = pairs = 0; i < size; i++) |
| 2462 | if (s[i] >= 0x10000) |
| 2463 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2464 | #endif |
Walter Dörwald | 3cc3452 | 2007-05-04 10:48:27 +0000 | [diff] [blame] | 2465 | v = PyBytes_FromStringAndSize(NULL, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2466 | 2 * (size + pairs + (byteorder == 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2467 | if (v == NULL) |
| 2468 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2469 | |
Walter Dörwald | 3cc3452 | 2007-05-04 10:48:27 +0000 | [diff] [blame] | 2470 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2471 | if (byteorder == 0) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2472 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2473 | if (size == 0) |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2474 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2475 | |
| 2476 | if (byteorder == -1) { |
| 2477 | /* force LE */ |
| 2478 | ihi = 1; |
| 2479 | ilo = 0; |
| 2480 | } |
| 2481 | else if (byteorder == 1) { |
| 2482 | /* force BE */ |
| 2483 | ihi = 0; |
| 2484 | ilo = 1; |
| 2485 | } |
| 2486 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2487 | while (size-- > 0) { |
| 2488 | Py_UNICODE ch = *s++; |
| 2489 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2490 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2491 | if (ch >= 0x10000) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2492 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2493 | ch = 0xD800 | ((ch-0x10000) >> 10); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2494 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2495 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2496 | STORECHAR(ch); |
| 2497 | if (ch2) |
| 2498 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2499 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2500 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2501 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2502 | } |
| 2503 | |
| 2504 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 2505 | { |
| 2506 | if (!PyUnicode_Check(unicode)) { |
| 2507 | PyErr_BadArgument(); |
| 2508 | return NULL; |
| 2509 | } |
| 2510 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 2511 | PyUnicode_GET_SIZE(unicode), |
| 2512 | NULL, |
| 2513 | 0); |
| 2514 | } |
| 2515 | |
| 2516 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 2517 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2518 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 2519 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2520 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2521 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2522 | const char *errors) |
| 2523 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2524 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2525 | Py_ssize_t startinpos; |
| 2526 | Py_ssize_t endinpos; |
| 2527 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2528 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2529 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2530 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2531 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2532 | char* message; |
| 2533 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2534 | PyObject *errorHandler = NULL; |
| 2535 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2536 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2537 | /* Escaped strings will always be longer than the resulting |
| 2538 | 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] | 2539 | length after conversion to the true value. |
| 2540 | (but if the error callback returns a long replacement string |
| 2541 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2542 | v = _PyUnicode_New(size); |
| 2543 | if (v == NULL) |
| 2544 | goto onError; |
| 2545 | if (size == 0) |
| 2546 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2547 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2548 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2549 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2550 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2551 | while (s < end) { |
| 2552 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2553 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2554 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2555 | |
| 2556 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2557 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2558 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2559 | continue; |
| 2560 | } |
| 2561 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2562 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2563 | /* \ - Escapes */ |
| 2564 | s++; |
| 2565 | switch (*s++) { |
| 2566 | |
| 2567 | /* \x escapes */ |
| 2568 | case '\n': break; |
| 2569 | case '\\': *p++ = '\\'; break; |
| 2570 | case '\'': *p++ = '\''; break; |
| 2571 | case '\"': *p++ = '\"'; break; |
| 2572 | case 'b': *p++ = '\b'; break; |
| 2573 | case 'f': *p++ = '\014'; break; /* FF */ |
| 2574 | case 't': *p++ = '\t'; break; |
| 2575 | case 'n': *p++ = '\n'; break; |
| 2576 | case 'r': *p++ = '\r'; break; |
| 2577 | case 'v': *p++ = '\013'; break; /* VT */ |
| 2578 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 2579 | |
| 2580 | /* \OOO (octal) escapes */ |
| 2581 | case '0': case '1': case '2': case '3': |
| 2582 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2583 | x = s[-1] - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2584 | if ('0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2585 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2586 | if ('0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2587 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2588 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2589 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2590 | break; |
| 2591 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2592 | /* hex escapes */ |
| 2593 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2594 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2595 | digits = 2; |
| 2596 | message = "truncated \\xXX escape"; |
| 2597 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2598 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2599 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2600 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2601 | digits = 4; |
| 2602 | message = "truncated \\uXXXX escape"; |
| 2603 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2604 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2605 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2606 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2607 | digits = 8; |
| 2608 | message = "truncated \\UXXXXXXXX escape"; |
| 2609 | hexescape: |
| 2610 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2611 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2612 | if (s+digits>end) { |
| 2613 | endinpos = size; |
| 2614 | if (unicode_decode_call_errorhandler( |
| 2615 | errors, &errorHandler, |
| 2616 | "unicodeescape", "end of string in escape sequence", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2617 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2618 | (PyObject **)&v, &outpos, &p)) |
| 2619 | goto onError; |
| 2620 | goto nextByte; |
| 2621 | } |
| 2622 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2623 | c = (unsigned char) s[i]; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2624 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2625 | endinpos = (s+i+1)-starts; |
| 2626 | if (unicode_decode_call_errorhandler( |
| 2627 | errors, &errorHandler, |
| 2628 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2629 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2630 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2631 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2632 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2633 | } |
| 2634 | chr = (chr<<4) & ~0xF; |
| 2635 | if (c >= '0' && c <= '9') |
| 2636 | chr += c - '0'; |
| 2637 | else if (c >= 'a' && c <= 'f') |
| 2638 | chr += 10 + c - 'a'; |
| 2639 | else |
| 2640 | chr += 10 + c - 'A'; |
| 2641 | } |
| 2642 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 2643 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2644 | /* _decoding_error will have already written into the |
| 2645 | target buffer. */ |
| 2646 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2647 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2648 | /* when we get here, chr is a 32-bit unicode character */ |
| 2649 | if (chr <= 0xffff) |
| 2650 | /* UCS-2 character */ |
| 2651 | *p++ = (Py_UNICODE) chr; |
| 2652 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2653 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2654 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2655 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2656 | *p++ = chr; |
| 2657 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2658 | chr -= 0x10000L; |
| 2659 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2660 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2661 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2662 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2663 | endinpos = s-starts; |
| 2664 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2665 | if (unicode_decode_call_errorhandler( |
| 2666 | errors, &errorHandler, |
| 2667 | "unicodeescape", "illegal Unicode character", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2668 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2669 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2670 | goto onError; |
| 2671 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2672 | break; |
| 2673 | |
| 2674 | /* \N{name} */ |
| 2675 | case 'N': |
| 2676 | message = "malformed \\N character escape"; |
| 2677 | if (ucnhash_CAPI == NULL) { |
| 2678 | /* load the unicode data module */ |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2679 | PyObject *m, *api; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2680 | m = PyImport_ImportModule("unicodedata"); |
| 2681 | if (m == NULL) |
| 2682 | goto ucnhashError; |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2683 | api = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2684 | Py_DECREF(m); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2685 | if (api == NULL) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2686 | goto ucnhashError; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2687 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2688 | Py_DECREF(api); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2689 | if (ucnhash_CAPI == NULL) |
| 2690 | goto ucnhashError; |
| 2691 | } |
| 2692 | if (*s == '{') { |
| 2693 | const char *start = s+1; |
| 2694 | /* look for the closing brace */ |
| 2695 | while (*s != '}' && s < end) |
| 2696 | s++; |
| 2697 | if (s > start && s < end && *s == '}') { |
| 2698 | /* found a name. look it up in the unicode database */ |
| 2699 | message = "unknown Unicode character name"; |
| 2700 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 2701 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2702 | goto store; |
| 2703 | } |
| 2704 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2705 | endinpos = s-starts; |
| 2706 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2707 | if (unicode_decode_call_errorhandler( |
| 2708 | errors, &errorHandler, |
| 2709 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2710 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2711 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2712 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2713 | break; |
| 2714 | |
| 2715 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2716 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2717 | message = "\\ at end of string"; |
| 2718 | s--; |
| 2719 | endinpos = s-starts; |
| 2720 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2721 | if (unicode_decode_call_errorhandler( |
| 2722 | errors, &errorHandler, |
| 2723 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2724 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2725 | (PyObject **)&v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2726 | goto onError; |
| 2727 | } |
| 2728 | else { |
| 2729 | *p++ = '\\'; |
| 2730 | *p++ = (unsigned char)s[-1]; |
| 2731 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2732 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2733 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2734 | nextByte: |
| 2735 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2736 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2737 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2738 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 2739 | Py_XDECREF(errorHandler); |
| 2740 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2741 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2742 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2743 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2744 | PyErr_SetString( |
| 2745 | PyExc_UnicodeError, |
| 2746 | "\\N escapes not supported (can't load unicodedata module)" |
| 2747 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2748 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2749 | Py_XDECREF(errorHandler); |
| 2750 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 2751 | return NULL; |
| 2752 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2753 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2754 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2755 | Py_XDECREF(errorHandler); |
| 2756 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2757 | return NULL; |
| 2758 | } |
| 2759 | |
| 2760 | /* Return a Unicode-Escape string version of the Unicode object. |
| 2761 | |
| 2762 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 2763 | appropriate. |
| 2764 | |
| 2765 | */ |
| 2766 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2767 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
| 2768 | Py_ssize_t size, |
| 2769 | Py_UNICODE ch) |
| 2770 | { |
| 2771 | /* like wcschr, but doesn't stop at NULL characters */ |
| 2772 | |
| 2773 | while (size-- > 0) { |
| 2774 | if (*s == ch) |
| 2775 | return s; |
| 2776 | s++; |
| 2777 | } |
| 2778 | |
| 2779 | return NULL; |
| 2780 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 2781 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2782 | static const char *hexdigits = "0123456789abcdef"; |
| 2783 | |
| 2784 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 2785 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2786 | { |
| 2787 | PyObject *repr; |
| 2788 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2789 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2790 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 2791 | better to choose a different scheme. Perhaps scan the |
| 2792 | first N-chars of the string and allocate based on that size. |
| 2793 | */ |
| 2794 | /* Initial allocation is based on the longest-possible unichr |
| 2795 | escape. |
| 2796 | |
| 2797 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 2798 | unichr, so in this case it's the longest unichr escape. In |
| 2799 | narrow (UTF-16) builds this is five chars per source unichr |
| 2800 | since there are two unichrs in the surrogate pair, so in narrow |
| 2801 | (UTF-16) builds it's not the longest unichr escape. |
| 2802 | |
| 2803 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 2804 | so in the narrow (UTF-16) build case it's the longest unichr |
| 2805 | escape. |
| 2806 | */ |
| 2807 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2808 | repr = PyBytes_FromStringAndSize(NULL, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2809 | #ifdef Py_UNICODE_WIDE |
| 2810 | + 10*size |
| 2811 | #else |
| 2812 | + 6*size |
| 2813 | #endif |
| 2814 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2815 | if (repr == NULL) |
| 2816 | return NULL; |
| 2817 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2818 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2819 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2820 | while (size-- > 0) { |
| 2821 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2822 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2823 | /* Escape backslashes */ |
| 2824 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2825 | *p++ = '\\'; |
| 2826 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2827 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2828 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2829 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 2830 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2831 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 2832 | else if (ch >= 0x10000) { |
| 2833 | *p++ = '\\'; |
| 2834 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2835 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 2836 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 2837 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 2838 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 2839 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 2840 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 2841 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 2842 | *p++ = hexdigits[ch & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2843 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2844 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2845 | #else |
| 2846 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2847 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 2848 | Py_UNICODE ch2; |
| 2849 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2850 | |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2851 | ch2 = *s++; |
| 2852 | size--; |
| 2853 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 2854 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 2855 | *p++ = '\\'; |
| 2856 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2857 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 2858 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 2859 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 2860 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 2861 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 2862 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 2863 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 2864 | *p++ = hexdigits[ucs & 0x0000000F]; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2865 | continue; |
| 2866 | } |
| 2867 | /* Fall through: isolated surrogates are copied as-is */ |
| 2868 | s--; |
| 2869 | size++; |
| 2870 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2871 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2872 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2873 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2874 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2875 | *p++ = '\\'; |
| 2876 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2877 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 2878 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 2879 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 2880 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2881 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2882 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2883 | /* Map special whitespace to '\t', \n', '\r' */ |
| 2884 | else if (ch == '\t') { |
| 2885 | *p++ = '\\'; |
| 2886 | *p++ = 't'; |
| 2887 | } |
| 2888 | else if (ch == '\n') { |
| 2889 | *p++ = '\\'; |
| 2890 | *p++ = 'n'; |
| 2891 | } |
| 2892 | else if (ch == '\r') { |
| 2893 | *p++ = '\\'; |
| 2894 | *p++ = 'r'; |
| 2895 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2896 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2897 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 2898 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2899 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2900 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2901 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 2902 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2903 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2904 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2905 | /* Copy everything else as-is */ |
| 2906 | else |
| 2907 | *p++ = (char) ch; |
| 2908 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2909 | |
| 2910 | *p = '\0'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2911 | if (PyBytes_Resize(repr, p - PyBytes_AS_STRING(repr))) { |
| 2912 | Py_DECREF(repr); |
| 2913 | return NULL; |
| 2914 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2915 | return repr; |
| 2916 | } |
| 2917 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2918 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 2919 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2920 | PyObject *s, *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2921 | if (!PyUnicode_Check(unicode)) { |
| 2922 | PyErr_BadArgument(); |
| 2923 | return NULL; |
| 2924 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2925 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2926 | PyUnicode_GET_SIZE(unicode)); |
| 2927 | |
| 2928 | if (!s) |
| 2929 | return NULL; |
| 2930 | result = PyString_FromStringAndSize(PyBytes_AS_STRING(s), |
| 2931 | PyBytes_GET_SIZE(s)); |
| 2932 | Py_DECREF(s); |
| 2933 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2934 | } |
| 2935 | |
| 2936 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 2937 | |
| 2938 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2939 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2940 | const char *errors) |
| 2941 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2942 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2943 | Py_ssize_t startinpos; |
| 2944 | Py_ssize_t endinpos; |
| 2945 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2946 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2947 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2948 | const char *end; |
| 2949 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2950 | PyObject *errorHandler = NULL; |
| 2951 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2952 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2953 | /* Escaped strings will always be longer than the resulting |
| 2954 | 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] | 2955 | length after conversion to the true value. (But decoding error |
| 2956 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2957 | v = _PyUnicode_New(size); |
| 2958 | if (v == NULL) |
| 2959 | goto onError; |
| 2960 | if (size == 0) |
| 2961 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2962 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2963 | end = s + size; |
| 2964 | while (s < end) { |
| 2965 | unsigned char c; |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 2966 | Py_UCS4 x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2967 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2968 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2969 | |
| 2970 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2971 | if (*s != '\\') { |
| 2972 | *p++ = (unsigned char)*s++; |
| 2973 | continue; |
| 2974 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2975 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2976 | |
| 2977 | /* \u-escapes are only interpreted iff the number of leading |
| 2978 | backslashes if odd */ |
| 2979 | bs = s; |
| 2980 | for (;s < end;) { |
| 2981 | if (*s != '\\') |
| 2982 | break; |
| 2983 | *p++ = (unsigned char)*s++; |
| 2984 | } |
| 2985 | if (((s - bs) & 1) == 0 || |
| 2986 | s >= end || |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2987 | (*s != 'u' && *s != 'U')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2988 | continue; |
| 2989 | } |
| 2990 | p--; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2991 | count = *s=='u' ? 4 : 8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2992 | s++; |
| 2993 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2994 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2995 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2996 | for (x = 0, i = 0; i < count; ++i, ++s) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2997 | c = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2998 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2999 | endinpos = s-starts; |
| 3000 | if (unicode_decode_call_errorhandler( |
| 3001 | errors, &errorHandler, |
| 3002 | "rawunicodeescape", "truncated \\uXXXX", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3003 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3004 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3005 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3006 | goto nextByte; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3007 | } |
| 3008 | x = (x<<4) & ~0xF; |
| 3009 | if (c >= '0' && c <= '9') |
| 3010 | x += c - '0'; |
| 3011 | else if (c >= 'a' && c <= 'f') |
| 3012 | x += 10 + c - 'a'; |
| 3013 | else |
| 3014 | x += 10 + c - 'A'; |
| 3015 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3016 | #ifndef Py_UNICODE_WIDE |
| 3017 | if (x > 0x10000) { |
| 3018 | if (unicode_decode_call_errorhandler( |
| 3019 | errors, &errorHandler, |
| 3020 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3021 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3022 | (PyObject **)&v, &outpos, &p)) |
| 3023 | goto onError; |
| 3024 | } |
| 3025 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3026 | *p++ = x; |
| 3027 | nextByte: |
| 3028 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3029 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3030 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3031 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3032 | Py_XDECREF(errorHandler); |
| 3033 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3034 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3035 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3036 | onError: |
| 3037 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3038 | Py_XDECREF(errorHandler); |
| 3039 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3040 | return NULL; |
| 3041 | } |
| 3042 | |
| 3043 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3044 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3045 | { |
| 3046 | PyObject *repr; |
| 3047 | char *p; |
| 3048 | char *q; |
| 3049 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3050 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3051 | repr = PyBytes_FromStringAndSize(NULL, 10 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3052 | #else |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3053 | repr = PyBytes_FromStringAndSize(NULL, 6 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3054 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3055 | if (repr == NULL) |
| 3056 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3057 | if (size == 0) |
| 3058 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3059 | |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3060 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3061 | while (size-- > 0) { |
| 3062 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3063 | #ifdef Py_UNICODE_WIDE |
| 3064 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 3065 | if (ch >= 0x10000) { |
| 3066 | *p++ = '\\'; |
| 3067 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3068 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 3069 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 3070 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 3071 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 3072 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3073 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3074 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3075 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3076 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3077 | else |
| 3078 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3079 | /* Map 16-bit characters to '\uxxxx' */ |
| 3080 | if (ch >= 256) { |
| 3081 | *p++ = '\\'; |
| 3082 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3083 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3084 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3085 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3086 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3087 | } |
| 3088 | /* Copy everything else as-is */ |
| 3089 | else |
| 3090 | *p++ = (char) ch; |
| 3091 | } |
| 3092 | *p = '\0'; |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3093 | if (PyBytes_Resize(repr, p - q)) { |
| 3094 | Py_DECREF(repr); |
| 3095 | return NULL; |
| 3096 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3097 | return repr; |
| 3098 | } |
| 3099 | |
| 3100 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 3101 | { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3102 | PyObject *s, *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3103 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3104 | PyErr_BadArgument(); |
| 3105 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3106 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3107 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3108 | PyUnicode_GET_SIZE(unicode)); |
| 3109 | |
| 3110 | if (!s) |
| 3111 | return NULL; |
| 3112 | result = PyString_FromStringAndSize(PyBytes_AS_STRING(s), |
| 3113 | PyBytes_GET_SIZE(s)); |
| 3114 | Py_DECREF(s); |
| 3115 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3116 | } |
| 3117 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3118 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 3119 | |
| 3120 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3121 | Py_ssize_t size, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3122 | const char *errors) |
| 3123 | { |
| 3124 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3125 | Py_ssize_t startinpos; |
| 3126 | Py_ssize_t endinpos; |
| 3127 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3128 | PyUnicodeObject *v; |
| 3129 | Py_UNICODE *p; |
| 3130 | const char *end; |
| 3131 | const char *reason; |
| 3132 | PyObject *errorHandler = NULL; |
| 3133 | PyObject *exc = NULL; |
| 3134 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 3135 | #ifdef Py_UNICODE_WIDE |
| 3136 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 3137 | #endif |
| 3138 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3139 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3140 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 3141 | if (v == NULL) |
| 3142 | goto onError; |
| 3143 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
| 3144 | return (PyObject *)v; |
| 3145 | p = PyUnicode_AS_UNICODE(v); |
| 3146 | end = s + size; |
| 3147 | |
| 3148 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3149 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3150 | /* We have to sanity check the raw data, otherwise doom looms for |
| 3151 | some malformed UCS-4 data. */ |
| 3152 | if ( |
| 3153 | #ifdef Py_UNICODE_WIDE |
| 3154 | *p > unimax || *p < 0 || |
| 3155 | #endif |
| 3156 | end-s < Py_UNICODE_SIZE |
| 3157 | ) |
| 3158 | { |
| 3159 | startinpos = s - starts; |
| 3160 | if (end-s < Py_UNICODE_SIZE) { |
| 3161 | endinpos = end-starts; |
| 3162 | reason = "truncated input"; |
| 3163 | } |
| 3164 | else { |
| 3165 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 3166 | reason = "illegal code point (> 0x10FFFF)"; |
| 3167 | } |
| 3168 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 3169 | if (unicode_decode_call_errorhandler( |
| 3170 | errors, &errorHandler, |
| 3171 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3172 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3173 | (PyObject **)&v, &outpos, &p)) { |
| 3174 | goto onError; |
| 3175 | } |
| 3176 | } |
| 3177 | else { |
| 3178 | p++; |
| 3179 | s += Py_UNICODE_SIZE; |
| 3180 | } |
| 3181 | } |
| 3182 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3183 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3184 | goto onError; |
| 3185 | Py_XDECREF(errorHandler); |
| 3186 | Py_XDECREF(exc); |
| 3187 | return (PyObject *)v; |
| 3188 | |
| 3189 | onError: |
| 3190 | Py_XDECREF(v); |
| 3191 | Py_XDECREF(errorHandler); |
| 3192 | Py_XDECREF(exc); |
| 3193 | return NULL; |
| 3194 | } |
| 3195 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3196 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 3197 | |
| 3198 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3199 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3200 | const char *errors) |
| 3201 | { |
| 3202 | PyUnicodeObject *v; |
| 3203 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3204 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3205 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3206 | if (size == 1) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3207 | Py_UNICODE r = *(unsigned char*)s; |
| 3208 | return PyUnicode_FromUnicode(&r, 1); |
| 3209 | } |
| 3210 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3211 | v = _PyUnicode_New(size); |
| 3212 | if (v == NULL) |
| 3213 | goto onError; |
| 3214 | if (size == 0) |
| 3215 | return (PyObject *)v; |
| 3216 | p = PyUnicode_AS_UNICODE(v); |
| 3217 | while (size-- > 0) |
| 3218 | *p++ = (unsigned char)*s++; |
| 3219 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3220 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3221 | onError: |
| 3222 | Py_XDECREF(v); |
| 3223 | return NULL; |
| 3224 | } |
| 3225 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3226 | /* create or adjust a UnicodeEncodeError */ |
| 3227 | static void make_encode_exception(PyObject **exceptionObject, |
| 3228 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3229 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3230 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3231 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3232 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3233 | if (*exceptionObject == NULL) { |
| 3234 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 3235 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3236 | } |
| 3237 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3238 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 3239 | goto onError; |
| 3240 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 3241 | goto onError; |
| 3242 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 3243 | goto onError; |
| 3244 | return; |
| 3245 | onError: |
| 3246 | Py_DECREF(*exceptionObject); |
| 3247 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3248 | } |
| 3249 | } |
| 3250 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3251 | /* raises a UnicodeEncodeError */ |
| 3252 | static void raise_encode_exception(PyObject **exceptionObject, |
| 3253 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3254 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3255 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3256 | const char *reason) |
| 3257 | { |
| 3258 | make_encode_exception(exceptionObject, |
| 3259 | encoding, unicode, size, startpos, endpos, reason); |
| 3260 | if (*exceptionObject != NULL) |
| 3261 | PyCodec_StrictErrors(*exceptionObject); |
| 3262 | } |
| 3263 | |
| 3264 | /* error handling callback helper: |
| 3265 | build arguments, call the callback and check the arguments, |
| 3266 | put the result into newpos and return the replacement string, which |
| 3267 | has to be freed by the caller */ |
| 3268 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 3269 | PyObject **errorHandler, |
| 3270 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3271 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 3272 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3273 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3274 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3275 | 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] | 3276 | |
| 3277 | PyObject *restuple; |
| 3278 | PyObject *resunicode; |
| 3279 | |
| 3280 | if (*errorHandler == NULL) { |
| 3281 | *errorHandler = PyCodec_LookupError(errors); |
| 3282 | if (*errorHandler == NULL) |
| 3283 | return NULL; |
| 3284 | } |
| 3285 | |
| 3286 | make_encode_exception(exceptionObject, |
| 3287 | encoding, unicode, size, startpos, endpos, reason); |
| 3288 | if (*exceptionObject == NULL) |
| 3289 | return NULL; |
| 3290 | |
| 3291 | restuple = PyObject_CallFunctionObjArgs( |
| 3292 | *errorHandler, *exceptionObject, NULL); |
| 3293 | if (restuple == NULL) |
| 3294 | return NULL; |
| 3295 | if (!PyTuple_Check(restuple)) { |
| 3296 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 3297 | Py_DECREF(restuple); |
| 3298 | return NULL; |
| 3299 | } |
| 3300 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 3301 | &resunicode, newpos)) { |
| 3302 | Py_DECREF(restuple); |
| 3303 | return NULL; |
| 3304 | } |
| 3305 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3306 | *newpos = size+*newpos; |
| 3307 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 3308 | 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] | 3309 | Py_DECREF(restuple); |
| 3310 | return NULL; |
| 3311 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3312 | Py_INCREF(resunicode); |
| 3313 | Py_DECREF(restuple); |
| 3314 | return resunicode; |
| 3315 | } |
| 3316 | |
| 3317 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3318 | Py_ssize_t size, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3319 | const char *errors, |
| 3320 | int limit) |
| 3321 | { |
| 3322 | /* output object */ |
| 3323 | PyObject *res; |
| 3324 | /* pointers to the beginning and end+1 of input */ |
| 3325 | const Py_UNICODE *startp = p; |
| 3326 | const Py_UNICODE *endp = p + size; |
| 3327 | /* pointer to the beginning of the unencodable characters */ |
| 3328 | /* const Py_UNICODE *badp = NULL; */ |
| 3329 | /* pointer into the output */ |
| 3330 | char *str; |
| 3331 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3332 | Py_ssize_t respos = 0; |
| 3333 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3334 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 3335 | 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] | 3336 | PyObject *errorHandler = NULL; |
| 3337 | PyObject *exc = NULL; |
| 3338 | /* the following variable is used for caching string comparisons |
| 3339 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 3340 | int known_errorHandler = -1; |
| 3341 | |
| 3342 | /* allocate enough for a simple encoding without |
| 3343 | replacements, if we need more, we'll resize */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3344 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3345 | if (res == NULL) |
| 3346 | goto onError; |
| 3347 | if (size == 0) |
| 3348 | return res; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3349 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3350 | ressize = size; |
| 3351 | |
| 3352 | while (p<endp) { |
| 3353 | Py_UNICODE c = *p; |
| 3354 | |
| 3355 | /* can we encode this? */ |
| 3356 | if (c<limit) { |
| 3357 | /* no overflow check, because we know that the space is enough */ |
| 3358 | *str++ = (char)c; |
| 3359 | ++p; |
| 3360 | } |
| 3361 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3362 | Py_ssize_t unicodepos = p-startp; |
| 3363 | Py_ssize_t requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3364 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3365 | Py_ssize_t repsize; |
| 3366 | Py_ssize_t newpos; |
| 3367 | Py_ssize_t respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3368 | Py_UNICODE *uni2; |
| 3369 | /* startpos for collecting unencodable chars */ |
| 3370 | const Py_UNICODE *collstart = p; |
| 3371 | const Py_UNICODE *collend = p; |
| 3372 | /* find all unecodable characters */ |
| 3373 | while ((collend < endp) && ((*collend)>=limit)) |
| 3374 | ++collend; |
| 3375 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 3376 | if (known_errorHandler==-1) { |
| 3377 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3378 | known_errorHandler = 1; |
| 3379 | else if (!strcmp(errors, "replace")) |
| 3380 | known_errorHandler = 2; |
| 3381 | else if (!strcmp(errors, "ignore")) |
| 3382 | known_errorHandler = 3; |
| 3383 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3384 | known_errorHandler = 4; |
| 3385 | else |
| 3386 | known_errorHandler = 0; |
| 3387 | } |
| 3388 | switch (known_errorHandler) { |
| 3389 | case 1: /* strict */ |
| 3390 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 3391 | goto onError; |
| 3392 | case 2: /* replace */ |
| 3393 | while (collstart++<collend) |
| 3394 | *str++ = '?'; /* fall through */ |
| 3395 | case 3: /* ignore */ |
| 3396 | p = collend; |
| 3397 | break; |
| 3398 | case 4: /* xmlcharrefreplace */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3399 | respos = str - PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3400 | /* determine replacement size (temporarily (mis)uses p) */ |
| 3401 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 3402 | if (*p<10) |
| 3403 | repsize += 2+1+1; |
| 3404 | else if (*p<100) |
| 3405 | repsize += 2+2+1; |
| 3406 | else if (*p<1000) |
| 3407 | repsize += 2+3+1; |
| 3408 | else if (*p<10000) |
| 3409 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 3410 | #ifndef Py_UNICODE_WIDE |
| 3411 | else |
| 3412 | repsize += 2+5+1; |
| 3413 | #else |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3414 | else if (*p<100000) |
| 3415 | repsize += 2+5+1; |
| 3416 | else if (*p<1000000) |
| 3417 | repsize += 2+6+1; |
| 3418 | else |
| 3419 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3420 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3421 | } |
| 3422 | requiredsize = respos+repsize+(endp-collend); |
| 3423 | if (requiredsize > ressize) { |
| 3424 | if (requiredsize<2*ressize) |
| 3425 | requiredsize = 2*ressize; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3426 | if (PyBytes_Resize(res, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3427 | goto onError; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3428 | str = PyBytes_AS_STRING(res) + respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3429 | ressize = requiredsize; |
| 3430 | } |
| 3431 | /* generate replacement (temporarily (mis)uses p) */ |
| 3432 | for (p = collstart; p < collend; ++p) { |
| 3433 | str += sprintf(str, "&#%d;", (int)*p); |
| 3434 | } |
| 3435 | p = collend; |
| 3436 | break; |
| 3437 | default: |
| 3438 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 3439 | encoding, reason, startp, size, &exc, |
| 3440 | collstart-startp, collend-startp, &newpos); |
| 3441 | if (repunicode == NULL) |
| 3442 | goto onError; |
| 3443 | /* need more space? (at least enough for what we |
| 3444 | have+the replacement+the rest of the string, so |
| 3445 | we won't have to check space for encodable characters) */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3446 | respos = str - PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3447 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3448 | requiredsize = respos+repsize+(endp-collend); |
| 3449 | if (requiredsize > ressize) { |
| 3450 | if (requiredsize<2*ressize) |
| 3451 | requiredsize = 2*ressize; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3452 | if (PyBytes_Resize(res, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3453 | Py_DECREF(repunicode); |
| 3454 | goto onError; |
| 3455 | } |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3456 | str = PyBytes_AS_STRING(res) + respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3457 | ressize = requiredsize; |
| 3458 | } |
| 3459 | /* check if there is anything unencodable in the replacement |
| 3460 | and copy it to the output */ |
| 3461 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 3462 | c = *uni2; |
| 3463 | if (c >= limit) { |
| 3464 | raise_encode_exception(&exc, encoding, startp, size, |
| 3465 | unicodepos, unicodepos+1, reason); |
| 3466 | Py_DECREF(repunicode); |
| 3467 | goto onError; |
| 3468 | } |
| 3469 | *str = (char)c; |
| 3470 | } |
| 3471 | p = startp + newpos; |
| 3472 | Py_DECREF(repunicode); |
| 3473 | } |
| 3474 | } |
| 3475 | } |
| 3476 | /* Resize if we allocated to much */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3477 | respos = str - PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3478 | if (respos<ressize) |
| 3479 | /* If this falls res will be NULL */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3480 | PyBytes_Resize(res, respos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3481 | Py_XDECREF(errorHandler); |
| 3482 | Py_XDECREF(exc); |
| 3483 | return res; |
| 3484 | |
| 3485 | onError: |
| 3486 | Py_XDECREF(res); |
| 3487 | Py_XDECREF(errorHandler); |
| 3488 | Py_XDECREF(exc); |
| 3489 | return NULL; |
| 3490 | } |
| 3491 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3492 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3493 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3494 | const char *errors) |
| 3495 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3496 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3497 | } |
| 3498 | |
| 3499 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 3500 | { |
| 3501 | if (!PyUnicode_Check(unicode)) { |
| 3502 | PyErr_BadArgument(); |
| 3503 | return NULL; |
| 3504 | } |
| 3505 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 3506 | PyUnicode_GET_SIZE(unicode), |
| 3507 | NULL); |
| 3508 | } |
| 3509 | |
| 3510 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 3511 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3512 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3513 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3514 | const char *errors) |
| 3515 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3516 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3517 | PyUnicodeObject *v; |
| 3518 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3519 | Py_ssize_t startinpos; |
| 3520 | Py_ssize_t endinpos; |
| 3521 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3522 | const char *e; |
| 3523 | PyObject *errorHandler = NULL; |
| 3524 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3525 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3526 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3527 | if (size == 1 && *(unsigned char*)s < 128) { |
| 3528 | Py_UNICODE r = *(unsigned char*)s; |
| 3529 | return PyUnicode_FromUnicode(&r, 1); |
| 3530 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3531 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3532 | v = _PyUnicode_New(size); |
| 3533 | if (v == NULL) |
| 3534 | goto onError; |
| 3535 | if (size == 0) |
| 3536 | return (PyObject *)v; |
| 3537 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3538 | e = s + size; |
| 3539 | while (s < e) { |
| 3540 | register unsigned char c = (unsigned char)*s; |
| 3541 | if (c < 128) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3542 | *p++ = c; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3543 | ++s; |
| 3544 | } |
| 3545 | else { |
| 3546 | startinpos = s-starts; |
| 3547 | endinpos = startinpos + 1; |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 3548 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3549 | if (unicode_decode_call_errorhandler( |
| 3550 | errors, &errorHandler, |
| 3551 | "ascii", "ordinal not in range(128)", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3552 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3553 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3554 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3555 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3556 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3557 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3558 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3559 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3560 | Py_XDECREF(errorHandler); |
| 3561 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3562 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3563 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3564 | onError: |
| 3565 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3566 | Py_XDECREF(errorHandler); |
| 3567 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3568 | return NULL; |
| 3569 | } |
| 3570 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3571 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3572 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3573 | const char *errors) |
| 3574 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3575 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3576 | } |
| 3577 | |
| 3578 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 3579 | { |
| 3580 | if (!PyUnicode_Check(unicode)) { |
| 3581 | PyErr_BadArgument(); |
| 3582 | return NULL; |
| 3583 | } |
| 3584 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 3585 | PyUnicode_GET_SIZE(unicode), |
| 3586 | NULL); |
| 3587 | } |
| 3588 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3589 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3590 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3591 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3592 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3593 | #if SIZEOF_INT < SIZEOF_SSIZE_T |
| 3594 | #define NEED_RETRY |
| 3595 | #endif |
| 3596 | |
| 3597 | /* XXX This code is limited to "true" double-byte encodings, as |
| 3598 | a) it assumes an incomplete character consists of a single byte, and |
| 3599 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
| 3600 | encodings, see IsDBCSLeadByteEx documentation. */ |
| 3601 | |
| 3602 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 3603 | { |
| 3604 | const char *curr = s + offset; |
| 3605 | |
| 3606 | if (IsDBCSLeadByte(*curr)) { |
| 3607 | const char *prev = CharPrev(s, curr); |
| 3608 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
| 3609 | } |
| 3610 | return 0; |
| 3611 | } |
| 3612 | |
| 3613 | /* |
| 3614 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 3615 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 3616 | */ |
| 3617 | static int decode_mbcs(PyUnicodeObject **v, |
| 3618 | const char *s, /* MBCS string */ |
| 3619 | int size, /* sizeof MBCS string */ |
| 3620 | int final) |
| 3621 | { |
| 3622 | Py_UNICODE *p; |
| 3623 | Py_ssize_t n = 0; |
| 3624 | int usize = 0; |
| 3625 | |
| 3626 | assert(size >= 0); |
| 3627 | |
| 3628 | /* Skip trailing lead-byte unless 'final' is set */ |
| 3629 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
| 3630 | --size; |
| 3631 | |
| 3632 | /* First get the size of the result */ |
| 3633 | if (size > 0) { |
| 3634 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 3635 | if (usize == 0) { |
| 3636 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3637 | return -1; |
| 3638 | } |
| 3639 | } |
| 3640 | |
| 3641 | if (*v == NULL) { |
| 3642 | /* Create unicode object */ |
| 3643 | *v = _PyUnicode_New(usize); |
| 3644 | if (*v == NULL) |
| 3645 | return -1; |
| 3646 | } |
| 3647 | else { |
| 3648 | /* Extend unicode object */ |
| 3649 | n = PyUnicode_GET_SIZE(*v); |
| 3650 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 3651 | return -1; |
| 3652 | } |
| 3653 | |
| 3654 | /* Do the conversion */ |
| 3655 | if (size > 0) { |
| 3656 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 3657 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 3658 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3659 | return -1; |
| 3660 | } |
| 3661 | } |
| 3662 | |
| 3663 | return size; |
| 3664 | } |
| 3665 | |
| 3666 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
| 3667 | Py_ssize_t size, |
| 3668 | const char *errors, |
| 3669 | Py_ssize_t *consumed) |
| 3670 | { |
| 3671 | PyUnicodeObject *v = NULL; |
| 3672 | int done; |
| 3673 | |
| 3674 | if (consumed) |
| 3675 | *consumed = 0; |
| 3676 | |
| 3677 | #ifdef NEED_RETRY |
| 3678 | retry: |
| 3679 | if (size > INT_MAX) |
| 3680 | done = decode_mbcs(&v, s, INT_MAX, 0); |
| 3681 | else |
| 3682 | #endif |
| 3683 | done = decode_mbcs(&v, s, (int)size, !consumed); |
| 3684 | |
| 3685 | if (done < 0) { |
| 3686 | Py_XDECREF(v); |
| 3687 | return NULL; |
| 3688 | } |
| 3689 | |
| 3690 | if (consumed) |
| 3691 | *consumed += done; |
| 3692 | |
| 3693 | #ifdef NEED_RETRY |
| 3694 | if (size > INT_MAX) { |
| 3695 | s += done; |
| 3696 | size -= done; |
| 3697 | goto retry; |
| 3698 | } |
| 3699 | #endif |
| 3700 | |
| 3701 | return (PyObject *)v; |
| 3702 | } |
| 3703 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3704 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3705 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3706 | const char *errors) |
| 3707 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3708 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 3709 | } |
| 3710 | |
| 3711 | /* |
| 3712 | * Convert unicode into string object (MBCS). |
| 3713 | * Returns 0 if succeed, -1 otherwise. |
| 3714 | */ |
| 3715 | static int encode_mbcs(PyObject **repr, |
| 3716 | const Py_UNICODE *p, /* unicode */ |
| 3717 | int size) /* size of unicode */ |
| 3718 | { |
| 3719 | int mbcssize = 0; |
| 3720 | Py_ssize_t n = 0; |
| 3721 | |
| 3722 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3723 | |
| 3724 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3725 | if (size > 0) { |
| 3726 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 3727 | if (mbcssize == 0) { |
| 3728 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3729 | return -1; |
| 3730 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3731 | } |
| 3732 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3733 | if (*repr == NULL) { |
| 3734 | /* Create string object */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3735 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3736 | if (*repr == NULL) |
| 3737 | return -1; |
| 3738 | } |
| 3739 | else { |
| 3740 | /* Extend string object */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3741 | n = PyBytes_Size(*repr); |
| 3742 | if (PyBytes_Resize(*repr, n + mbcssize) < 0) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3743 | return -1; |
| 3744 | } |
| 3745 | |
| 3746 | /* Do the conversion */ |
| 3747 | if (size > 0) { |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3748 | char *s = PyBytes_AS_STRING(*repr) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3749 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 3750 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3751 | return -1; |
| 3752 | } |
| 3753 | } |
| 3754 | |
| 3755 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3756 | } |
| 3757 | |
| 3758 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3759 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3760 | const char *errors) |
| 3761 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3762 | PyObject *repr = NULL; |
| 3763 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 3764 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3765 | #ifdef NEED_RETRY |
| 3766 | retry: |
| 3767 | if (size > INT_MAX) |
| 3768 | ret = encode_mbcs(&repr, p, INT_MAX); |
| 3769 | else |
| 3770 | #endif |
| 3771 | ret = encode_mbcs(&repr, p, (int)size); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3772 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3773 | if (ret < 0) { |
| 3774 | Py_XDECREF(repr); |
| 3775 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3776 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3777 | |
| 3778 | #ifdef NEED_RETRY |
| 3779 | if (size > INT_MAX) { |
| 3780 | p += INT_MAX; |
| 3781 | size -= INT_MAX; |
| 3782 | goto retry; |
| 3783 | } |
| 3784 | #endif |
| 3785 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3786 | return repr; |
| 3787 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3788 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3789 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 3790 | { |
| 3791 | if (!PyUnicode_Check(unicode)) { |
| 3792 | PyErr_BadArgument(); |
| 3793 | return NULL; |
| 3794 | } |
| 3795 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 3796 | PyUnicode_GET_SIZE(unicode), |
| 3797 | NULL); |
| 3798 | } |
| 3799 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3800 | #undef NEED_RETRY |
| 3801 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3802 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3803 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3804 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 3805 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3806 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3807 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3808 | PyObject *mapping, |
| 3809 | const char *errors) |
| 3810 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3811 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3812 | Py_ssize_t startinpos; |
| 3813 | Py_ssize_t endinpos; |
| 3814 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3815 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3816 | PyUnicodeObject *v; |
| 3817 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3818 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3819 | PyObject *errorHandler = NULL; |
| 3820 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3821 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3822 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3823 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3824 | /* Default to Latin-1 */ |
| 3825 | if (mapping == NULL) |
| 3826 | return PyUnicode_DecodeLatin1(s, size, errors); |
| 3827 | |
| 3828 | v = _PyUnicode_New(size); |
| 3829 | if (v == NULL) |
| 3830 | goto onError; |
| 3831 | if (size == 0) |
| 3832 | return (PyObject *)v; |
| 3833 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3834 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3835 | if (PyUnicode_CheckExact(mapping)) { |
| 3836 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 3837 | maplen = PyUnicode_GET_SIZE(mapping); |
| 3838 | while (s < e) { |
| 3839 | unsigned char ch = *s; |
| 3840 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3841 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3842 | if (ch < maplen) |
| 3843 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3844 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3845 | if (x == 0xfffe) { |
| 3846 | /* undefined mapping */ |
| 3847 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3848 | startinpos = s-starts; |
| 3849 | endinpos = startinpos+1; |
| 3850 | if (unicode_decode_call_errorhandler( |
| 3851 | errors, &errorHandler, |
| 3852 | "charmap", "character maps to <undefined>", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3853 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3854 | (PyObject **)&v, &outpos, &p)) { |
| 3855 | goto onError; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 3856 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3857 | continue; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 3858 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3859 | *p++ = x; |
| 3860 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3861 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3862 | } |
| 3863 | else { |
| 3864 | while (s < e) { |
| 3865 | unsigned char ch = *s; |
| 3866 | PyObject *w, *x; |
| 3867 | |
| 3868 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 3869 | w = PyInt_FromLong((long)ch); |
| 3870 | if (w == NULL) |
| 3871 | goto onError; |
| 3872 | x = PyObject_GetItem(mapping, w); |
| 3873 | Py_DECREF(w); |
| 3874 | if (x == NULL) { |
| 3875 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3876 | /* No mapping found means: mapping is undefined. */ |
| 3877 | PyErr_Clear(); |
| 3878 | x = Py_None; |
| 3879 | Py_INCREF(x); |
| 3880 | } else |
| 3881 | goto onError; |
| 3882 | } |
| 3883 | |
| 3884 | /* Apply mapping */ |
| 3885 | if (PyInt_Check(x)) { |
| 3886 | long value = PyInt_AS_LONG(x); |
| 3887 | if (value < 0 || value > 65535) { |
| 3888 | PyErr_SetString(PyExc_TypeError, |
| 3889 | "character mapping must be in range(65536)"); |
| 3890 | Py_DECREF(x); |
| 3891 | goto onError; |
| 3892 | } |
| 3893 | *p++ = (Py_UNICODE)value; |
| 3894 | } |
| 3895 | else if (x == Py_None) { |
| 3896 | /* undefined mapping */ |
| 3897 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3898 | startinpos = s-starts; |
| 3899 | endinpos = startinpos+1; |
| 3900 | if (unicode_decode_call_errorhandler( |
| 3901 | errors, &errorHandler, |
| 3902 | "charmap", "character maps to <undefined>", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3903 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3904 | (PyObject **)&v, &outpos, &p)) { |
| 3905 | Py_DECREF(x); |
| 3906 | goto onError; |
| 3907 | } |
Walter Dörwald | d4fff17 | 2005-11-28 22:15:56 +0000 | [diff] [blame] | 3908 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3909 | continue; |
| 3910 | } |
| 3911 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3912 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3913 | |
| 3914 | if (targetsize == 1) |
| 3915 | /* 1-1 mapping */ |
| 3916 | *p++ = *PyUnicode_AS_UNICODE(x); |
| 3917 | |
| 3918 | else if (targetsize > 1) { |
| 3919 | /* 1-n mapping */ |
| 3920 | if (targetsize > extrachars) { |
| 3921 | /* resize first */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3922 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 3923 | Py_ssize_t needed = (targetsize - extrachars) + \ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3924 | (targetsize << 2); |
| 3925 | extrachars += needed; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3926 | /* XXX overflow detection missing */ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3927 | if (_PyUnicode_Resize(&v, |
| 3928 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 3929 | Py_DECREF(x); |
| 3930 | goto onError; |
| 3931 | } |
| 3932 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 3933 | } |
| 3934 | Py_UNICODE_COPY(p, |
| 3935 | PyUnicode_AS_UNICODE(x), |
| 3936 | targetsize); |
| 3937 | p += targetsize; |
| 3938 | extrachars -= targetsize; |
| 3939 | } |
| 3940 | /* 1-0 mapping: skip the character */ |
| 3941 | } |
| 3942 | else { |
| 3943 | /* wrong return value */ |
| 3944 | PyErr_SetString(PyExc_TypeError, |
| 3945 | "character mapping must return integer, None or unicode"); |
| 3946 | Py_DECREF(x); |
| 3947 | goto onError; |
| 3948 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3949 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3950 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3951 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3952 | } |
| 3953 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3954 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3955 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3956 | Py_XDECREF(errorHandler); |
| 3957 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3958 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3959 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3960 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3961 | Py_XDECREF(errorHandler); |
| 3962 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3963 | Py_XDECREF(v); |
| 3964 | return NULL; |
| 3965 | } |
| 3966 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3967 | /* Charmap encoding: the lookup table */ |
| 3968 | |
| 3969 | struct encoding_map{ |
| 3970 | PyObject_HEAD |
| 3971 | unsigned char level1[32]; |
| 3972 | int count2, count3; |
| 3973 | unsigned char level23[1]; |
| 3974 | }; |
| 3975 | |
| 3976 | static PyObject* |
| 3977 | encoding_map_size(PyObject *obj, PyObject* args) |
| 3978 | { |
| 3979 | struct encoding_map *map = (struct encoding_map*)obj; |
| 3980 | return PyInt_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
| 3981 | 128*map->count3); |
| 3982 | } |
| 3983 | |
| 3984 | static PyMethodDef encoding_map_methods[] = { |
| 3985 | {"size", encoding_map_size, METH_NOARGS, |
| 3986 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 3987 | { 0 } |
| 3988 | }; |
| 3989 | |
| 3990 | static void |
| 3991 | encoding_map_dealloc(PyObject* o) |
| 3992 | { |
| 3993 | PyObject_FREE(o); |
| 3994 | } |
| 3995 | |
| 3996 | static PyTypeObject EncodingMapType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3997 | PyVarObject_HEAD_INIT(NULL, 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3998 | "EncodingMap", /*tp_name*/ |
| 3999 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 4000 | 0, /*tp_itemsize*/ |
| 4001 | /* methods */ |
| 4002 | encoding_map_dealloc, /*tp_dealloc*/ |
| 4003 | 0, /*tp_print*/ |
| 4004 | 0, /*tp_getattr*/ |
| 4005 | 0, /*tp_setattr*/ |
| 4006 | 0, /*tp_compare*/ |
| 4007 | 0, /*tp_repr*/ |
| 4008 | 0, /*tp_as_number*/ |
| 4009 | 0, /*tp_as_sequence*/ |
| 4010 | 0, /*tp_as_mapping*/ |
| 4011 | 0, /*tp_hash*/ |
| 4012 | 0, /*tp_call*/ |
| 4013 | 0, /*tp_str*/ |
| 4014 | 0, /*tp_getattro*/ |
| 4015 | 0, /*tp_setattro*/ |
| 4016 | 0, /*tp_as_buffer*/ |
| 4017 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 4018 | 0, /*tp_doc*/ |
| 4019 | 0, /*tp_traverse*/ |
| 4020 | 0, /*tp_clear*/ |
| 4021 | 0, /*tp_richcompare*/ |
| 4022 | 0, /*tp_weaklistoffset*/ |
| 4023 | 0, /*tp_iter*/ |
| 4024 | 0, /*tp_iternext*/ |
| 4025 | encoding_map_methods, /*tp_methods*/ |
| 4026 | 0, /*tp_members*/ |
| 4027 | 0, /*tp_getset*/ |
| 4028 | 0, /*tp_base*/ |
| 4029 | 0, /*tp_dict*/ |
| 4030 | 0, /*tp_descr_get*/ |
| 4031 | 0, /*tp_descr_set*/ |
| 4032 | 0, /*tp_dictoffset*/ |
| 4033 | 0, /*tp_init*/ |
| 4034 | 0, /*tp_alloc*/ |
| 4035 | 0, /*tp_new*/ |
| 4036 | 0, /*tp_free*/ |
| 4037 | 0, /*tp_is_gc*/ |
| 4038 | }; |
| 4039 | |
| 4040 | PyObject* |
| 4041 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 4042 | { |
| 4043 | Py_UNICODE *decode; |
| 4044 | PyObject *result; |
| 4045 | struct encoding_map *mresult; |
| 4046 | int i; |
| 4047 | int need_dict = 0; |
| 4048 | unsigned char level1[32]; |
| 4049 | unsigned char level2[512]; |
| 4050 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 4051 | int count2 = 0, count3 = 0; |
| 4052 | |
| 4053 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 4054 | PyErr_BadArgument(); |
| 4055 | return NULL; |
| 4056 | } |
| 4057 | decode = PyUnicode_AS_UNICODE(string); |
| 4058 | memset(level1, 0xFF, sizeof level1); |
| 4059 | memset(level2, 0xFF, sizeof level2); |
| 4060 | |
| 4061 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 4062 | or if there are non-BMP characters, we need to use |
| 4063 | a mapping dictionary. */ |
| 4064 | if (decode[0] != 0) |
| 4065 | need_dict = 1; |
| 4066 | for (i = 1; i < 256; i++) { |
| 4067 | int l1, l2; |
| 4068 | if (decode[i] == 0 |
| 4069 | #ifdef Py_UNICODE_WIDE |
| 4070 | || decode[i] > 0xFFFF |
| 4071 | #endif |
| 4072 | ) { |
| 4073 | need_dict = 1; |
| 4074 | break; |
| 4075 | } |
| 4076 | if (decode[i] == 0xFFFE) |
| 4077 | /* unmapped character */ |
| 4078 | continue; |
| 4079 | l1 = decode[i] >> 11; |
| 4080 | l2 = decode[i] >> 7; |
| 4081 | if (level1[l1] == 0xFF) |
| 4082 | level1[l1] = count2++; |
| 4083 | if (level2[l2] == 0xFF) |
| 4084 | level2[l2] = count3++; |
| 4085 | } |
| 4086 | |
| 4087 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 4088 | need_dict = 1; |
| 4089 | |
| 4090 | if (need_dict) { |
| 4091 | PyObject *result = PyDict_New(); |
| 4092 | PyObject *key, *value; |
| 4093 | if (!result) |
| 4094 | return NULL; |
| 4095 | for (i = 0; i < 256; i++) { |
| 4096 | key = value = NULL; |
| 4097 | key = PyInt_FromLong(decode[i]); |
| 4098 | value = PyInt_FromLong(i); |
| 4099 | if (!key || !value) |
| 4100 | goto failed1; |
| 4101 | if (PyDict_SetItem(result, key, value) == -1) |
| 4102 | goto failed1; |
| 4103 | Py_DECREF(key); |
| 4104 | Py_DECREF(value); |
| 4105 | } |
| 4106 | return result; |
| 4107 | failed1: |
| 4108 | Py_XDECREF(key); |
| 4109 | Py_XDECREF(value); |
| 4110 | Py_DECREF(result); |
| 4111 | return NULL; |
| 4112 | } |
| 4113 | |
| 4114 | /* Create a three-level trie */ |
| 4115 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 4116 | 16*count2 + 128*count3 - 1); |
| 4117 | if (!result) |
| 4118 | return PyErr_NoMemory(); |
| 4119 | PyObject_Init(result, &EncodingMapType); |
| 4120 | mresult = (struct encoding_map*)result; |
| 4121 | mresult->count2 = count2; |
| 4122 | mresult->count3 = count3; |
| 4123 | mlevel1 = mresult->level1; |
| 4124 | mlevel2 = mresult->level23; |
| 4125 | mlevel3 = mresult->level23 + 16*count2; |
| 4126 | memcpy(mlevel1, level1, 32); |
| 4127 | memset(mlevel2, 0xFF, 16*count2); |
| 4128 | memset(mlevel3, 0, 128*count3); |
| 4129 | count3 = 0; |
| 4130 | for (i = 1; i < 256; i++) { |
| 4131 | int o1, o2, o3, i2, i3; |
| 4132 | if (decode[i] == 0xFFFE) |
| 4133 | /* unmapped character */ |
| 4134 | continue; |
| 4135 | o1 = decode[i]>>11; |
| 4136 | o2 = (decode[i]>>7) & 0xF; |
| 4137 | i2 = 16*mlevel1[o1] + o2; |
| 4138 | if (mlevel2[i2] == 0xFF) |
| 4139 | mlevel2[i2] = count3++; |
| 4140 | o3 = decode[i] & 0x7F; |
| 4141 | i3 = 128*mlevel2[i2] + o3; |
| 4142 | mlevel3[i3] = i; |
| 4143 | } |
| 4144 | return result; |
| 4145 | } |
| 4146 | |
| 4147 | static int |
| 4148 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 4149 | { |
| 4150 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 4151 | int l1 = c>>11; |
| 4152 | int l2 = (c>>7) & 0xF; |
| 4153 | int l3 = c & 0x7F; |
| 4154 | int i; |
| 4155 | |
| 4156 | #ifdef Py_UNICODE_WIDE |
| 4157 | if (c > 0xFFFF) { |
| 4158 | return -1; |
| 4159 | } |
| 4160 | #endif |
| 4161 | if (c == 0) |
| 4162 | return 0; |
| 4163 | /* level 1*/ |
| 4164 | i = map->level1[l1]; |
| 4165 | if (i == 0xFF) { |
| 4166 | return -1; |
| 4167 | } |
| 4168 | /* level 2*/ |
| 4169 | i = map->level23[16*i+l2]; |
| 4170 | if (i == 0xFF) { |
| 4171 | return -1; |
| 4172 | } |
| 4173 | /* level 3 */ |
| 4174 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 4175 | if (i == 0) { |
| 4176 | return -1; |
| 4177 | } |
| 4178 | return i; |
| 4179 | } |
| 4180 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4181 | /* Lookup the character ch in the mapping. If the character |
| 4182 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4183 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4184 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4185 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4186 | PyObject *w = PyInt_FromLong((long)c); |
| 4187 | PyObject *x; |
| 4188 | |
| 4189 | if (w == NULL) |
| 4190 | return NULL; |
| 4191 | x = PyObject_GetItem(mapping, w); |
| 4192 | Py_DECREF(w); |
| 4193 | if (x == NULL) { |
| 4194 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4195 | /* No mapping found means: mapping is undefined. */ |
| 4196 | PyErr_Clear(); |
| 4197 | x = Py_None; |
| 4198 | Py_INCREF(x); |
| 4199 | return x; |
| 4200 | } else |
| 4201 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4202 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 4203 | else if (x == Py_None) |
| 4204 | return x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4205 | else if (PyInt_Check(x)) { |
| 4206 | long value = PyInt_AS_LONG(x); |
| 4207 | if (value < 0 || value > 255) { |
| 4208 | PyErr_SetString(PyExc_TypeError, |
| 4209 | "character mapping must be in range(256)"); |
| 4210 | Py_DECREF(x); |
| 4211 | return NULL; |
| 4212 | } |
| 4213 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4214 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4215 | else if (PyString_Check(x)) |
| 4216 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4217 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4218 | /* wrong return value */ |
Walter Dörwald | 580ceed | 2007-05-09 10:39:19 +0000 | [diff] [blame] | 4219 | PyErr_Format(PyExc_TypeError, |
| 4220 | "character mapping must return integer, None or str8, not %.400s", |
| 4221 | x->ob_type->tp_name); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4222 | Py_DECREF(x); |
| 4223 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4224 | } |
| 4225 | } |
| 4226 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4227 | static int |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4228 | charmapencode_resize(PyObject *outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4229 | { |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4230 | Py_ssize_t outsize = PyBytes_GET_SIZE( outobj); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4231 | /* exponentially overallocate to minimize reallocations */ |
| 4232 | if (requiredsize < 2*outsize) |
| 4233 | requiredsize = 2*outsize; |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4234 | if (PyBytes_Resize(outobj, requiredsize)) { |
| 4235 | Py_DECREF(outobj); |
| 4236 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4237 | } |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4238 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4239 | } |
| 4240 | |
| 4241 | typedef enum charmapencode_result { |
| 4242 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
| 4243 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4244 | /* 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] | 4245 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4246 | space is available. Return a new reference to the object that |
| 4247 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 4248 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 4249 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4250 | static |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4251 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4252 | PyObject *outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4253 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4254 | PyObject *rep; |
| 4255 | char *outstart; |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4256 | Py_ssize_t outsize = PyBytes_GET_SIZE(outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4257 | |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 4258 | if (Py_Type(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4259 | int res = encoding_map_lookup(c, mapping); |
| 4260 | Py_ssize_t requiredsize = *outpos+1; |
| 4261 | if (res == -1) |
| 4262 | return enc_FAILED; |
| 4263 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4264 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4265 | return enc_EXCEPTION; |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4266 | outstart = PyBytes_AS_STRING(outobj); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4267 | outstart[(*outpos)++] = (char)res; |
| 4268 | return enc_SUCCESS; |
| 4269 | } |
| 4270 | |
| 4271 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4272 | if (rep==NULL) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4273 | return enc_EXCEPTION; |
| 4274 | else if (rep==Py_None) { |
| 4275 | Py_DECREF(rep); |
| 4276 | return enc_FAILED; |
| 4277 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4278 | if (PyInt_Check(rep)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4279 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4280 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4281 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4282 | Py_DECREF(rep); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4283 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4284 | } |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4285 | outstart = PyBytes_AS_STRING(outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4286 | outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); |
| 4287 | } |
| 4288 | else { |
| 4289 | const char *repchars = PyString_AS_STRING(rep); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4290 | Py_ssize_t repsize = PyString_GET_SIZE(rep); |
| 4291 | Py_ssize_t requiredsize = *outpos+repsize; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4292 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4293 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4294 | Py_DECREF(rep); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4295 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4296 | } |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4297 | outstart = PyBytes_AS_STRING(outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4298 | memcpy(outstart + *outpos, repchars, repsize); |
| 4299 | *outpos += repsize; |
| 4300 | } |
| 4301 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4302 | Py_DECREF(rep); |
| 4303 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4304 | } |
| 4305 | |
| 4306 | /* handle an error in PyUnicode_EncodeCharmap |
| 4307 | Return 0 on success, -1 on error */ |
| 4308 | static |
| 4309 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4310 | 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] | 4311 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4312 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4313 | PyObject *res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4314 | { |
| 4315 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4316 | Py_ssize_t repsize; |
| 4317 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4318 | Py_UNICODE *uni2; |
| 4319 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4320 | Py_ssize_t collstartpos = *inpos; |
| 4321 | Py_ssize_t collendpos = *inpos+1; |
| 4322 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4323 | char *encoding = "charmap"; |
| 4324 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4325 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4326 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4327 | /* find all unencodable characters */ |
| 4328 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4329 | PyObject *rep; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 4330 | if (Py_Type(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4331 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 4332 | if (res != -1) |
| 4333 | break; |
| 4334 | ++collendpos; |
| 4335 | continue; |
| 4336 | } |
| 4337 | |
| 4338 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 4339 | if (rep==NULL) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4340 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4341 | else if (rep!=Py_None) { |
| 4342 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4343 | break; |
| 4344 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4345 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4346 | ++collendpos; |
| 4347 | } |
| 4348 | /* cache callback name lookup |
| 4349 | * (if not done yet, i.e. it's the first error) */ |
| 4350 | if (*known_errorHandler==-1) { |
| 4351 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4352 | *known_errorHandler = 1; |
| 4353 | else if (!strcmp(errors, "replace")) |
| 4354 | *known_errorHandler = 2; |
| 4355 | else if (!strcmp(errors, "ignore")) |
| 4356 | *known_errorHandler = 3; |
| 4357 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4358 | *known_errorHandler = 4; |
| 4359 | else |
| 4360 | *known_errorHandler = 0; |
| 4361 | } |
| 4362 | switch (*known_errorHandler) { |
| 4363 | case 1: /* strict */ |
| 4364 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4365 | return -1; |
| 4366 | case 2: /* replace */ |
| 4367 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 4368 | x = charmapencode_output('?', mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4369 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4370 | return -1; |
| 4371 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4372 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4373 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4374 | return -1; |
| 4375 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4376 | } |
| 4377 | /* fall through */ |
| 4378 | case 3: /* ignore */ |
| 4379 | *inpos = collendpos; |
| 4380 | break; |
| 4381 | case 4: /* xmlcharrefreplace */ |
| 4382 | /* generate replacement (temporarily (mis)uses p) */ |
| 4383 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 4384 | char buffer[2+29+1+1]; |
| 4385 | char *cp; |
| 4386 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 4387 | for (cp = buffer; *cp; ++cp) { |
| 4388 | x = charmapencode_output(*cp, mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4389 | if (x==enc_EXCEPTION) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4390 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4391 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4392 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4393 | return -1; |
| 4394 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4395 | } |
| 4396 | } |
| 4397 | *inpos = collendpos; |
| 4398 | break; |
| 4399 | default: |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4400 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4401 | encoding, reason, p, size, exceptionObject, |
| 4402 | collstartpos, collendpos, &newpos); |
| 4403 | if (repunicode == NULL) |
| 4404 | return -1; |
| 4405 | /* generate replacement */ |
| 4406 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4407 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 4408 | x = charmapencode_output(*uni2, mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4409 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4410 | return -1; |
| 4411 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4412 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4413 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4414 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4415 | return -1; |
| 4416 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4417 | } |
| 4418 | *inpos = newpos; |
| 4419 | Py_DECREF(repunicode); |
| 4420 | } |
| 4421 | return 0; |
| 4422 | } |
| 4423 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4424 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4425 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4426 | PyObject *mapping, |
| 4427 | const char *errors) |
| 4428 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4429 | /* output object */ |
| 4430 | PyObject *res = NULL; |
| 4431 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4432 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4433 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4434 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4435 | PyObject *errorHandler = NULL; |
| 4436 | PyObject *exc = NULL; |
| 4437 | /* the following variable is used for caching string comparisons |
| 4438 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4439 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4440 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4441 | |
| 4442 | /* Default to Latin-1 */ |
| 4443 | if (mapping == NULL) |
| 4444 | return PyUnicode_EncodeLatin1(p, size, errors); |
| 4445 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4446 | /* allocate enough for a simple encoding without |
| 4447 | replacements, if we need more, we'll resize */ |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4448 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4449 | if (res == NULL) |
| 4450 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 4451 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4452 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4453 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4454 | while (inpos<size) { |
| 4455 | /* try to encode it */ |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4456 | charmapencode_result x = charmapencode_output(p[inpos], mapping, res, &respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4457 | if (x==enc_EXCEPTION) /* error */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4458 | goto onError; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4459 | if (x==enc_FAILED) { /* unencodable character */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4460 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 4461 | &exc, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4462 | &known_errorHandler, &errorHandler, errors, |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4463 | res, &respos)) { |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 4464 | goto onError; |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 4465 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4466 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4467 | else |
| 4468 | /* done with this character => adjust input position */ |
| 4469 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4470 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4471 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4472 | /* Resize if we allocated to much */ |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4473 | if (respos<PyBytes_GET_SIZE(res)) { |
| 4474 | if (PyBytes_Resize(res, respos)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4475 | goto onError; |
| 4476 | } |
| 4477 | Py_XDECREF(exc); |
| 4478 | Py_XDECREF(errorHandler); |
| 4479 | return res; |
| 4480 | |
| 4481 | onError: |
| 4482 | Py_XDECREF(res); |
| 4483 | Py_XDECREF(exc); |
| 4484 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4485 | return NULL; |
| 4486 | } |
| 4487 | |
| 4488 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 4489 | PyObject *mapping) |
| 4490 | { |
| 4491 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 4492 | PyErr_BadArgument(); |
| 4493 | return NULL; |
| 4494 | } |
| 4495 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 4496 | PyUnicode_GET_SIZE(unicode), |
| 4497 | mapping, |
| 4498 | NULL); |
| 4499 | } |
| 4500 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4501 | /* create or adjust a UnicodeTranslateError */ |
| 4502 | static void make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4503 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4504 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4505 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4506 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4507 | if (*exceptionObject == NULL) { |
| 4508 | *exceptionObject = PyUnicodeTranslateError_Create( |
| 4509 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4510 | } |
| 4511 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4512 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 4513 | goto onError; |
| 4514 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 4515 | goto onError; |
| 4516 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 4517 | goto onError; |
| 4518 | return; |
| 4519 | onError: |
| 4520 | Py_DECREF(*exceptionObject); |
| 4521 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4522 | } |
| 4523 | } |
| 4524 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4525 | /* raises a UnicodeTranslateError */ |
| 4526 | static void raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4527 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4528 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4529 | const char *reason) |
| 4530 | { |
| 4531 | make_translate_exception(exceptionObject, |
| 4532 | unicode, size, startpos, endpos, reason); |
| 4533 | if (*exceptionObject != NULL) |
| 4534 | PyCodec_StrictErrors(*exceptionObject); |
| 4535 | } |
| 4536 | |
| 4537 | /* error handling callback helper: |
| 4538 | build arguments, call the callback and check the arguments, |
| 4539 | put the result into newpos and return the replacement string, which |
| 4540 | has to be freed by the caller */ |
| 4541 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 4542 | PyObject **errorHandler, |
| 4543 | const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4544 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4545 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4546 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4547 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4548 | 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] | 4549 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4550 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4551 | PyObject *restuple; |
| 4552 | PyObject *resunicode; |
| 4553 | |
| 4554 | if (*errorHandler == NULL) { |
| 4555 | *errorHandler = PyCodec_LookupError(errors); |
| 4556 | if (*errorHandler == NULL) |
| 4557 | return NULL; |
| 4558 | } |
| 4559 | |
| 4560 | make_translate_exception(exceptionObject, |
| 4561 | unicode, size, startpos, endpos, reason); |
| 4562 | if (*exceptionObject == NULL) |
| 4563 | return NULL; |
| 4564 | |
| 4565 | restuple = PyObject_CallFunctionObjArgs( |
| 4566 | *errorHandler, *exceptionObject, NULL); |
| 4567 | if (restuple == NULL) |
| 4568 | return NULL; |
| 4569 | if (!PyTuple_Check(restuple)) { |
| 4570 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 4571 | Py_DECREF(restuple); |
| 4572 | return NULL; |
| 4573 | } |
| 4574 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4575 | &resunicode, &i_newpos)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4576 | Py_DECREF(restuple); |
| 4577 | return NULL; |
| 4578 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4579 | if (i_newpos<0) |
| 4580 | *newpos = size+i_newpos; |
| 4581 | else |
| 4582 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4583 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 4584 | 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] | 4585 | Py_DECREF(restuple); |
| 4586 | return NULL; |
| 4587 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4588 | Py_INCREF(resunicode); |
| 4589 | Py_DECREF(restuple); |
| 4590 | return resunicode; |
| 4591 | } |
| 4592 | |
| 4593 | /* Lookup the character ch in the mapping and put the result in result, |
| 4594 | which must be decrefed by the caller. |
| 4595 | Return 0 on success, -1 on error */ |
| 4596 | static |
| 4597 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 4598 | { |
| 4599 | PyObject *w = PyInt_FromLong((long)c); |
| 4600 | PyObject *x; |
| 4601 | |
| 4602 | if (w == NULL) |
| 4603 | return -1; |
| 4604 | x = PyObject_GetItem(mapping, w); |
| 4605 | Py_DECREF(w); |
| 4606 | if (x == NULL) { |
| 4607 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4608 | /* No mapping found means: use 1:1 mapping. */ |
| 4609 | PyErr_Clear(); |
| 4610 | *result = NULL; |
| 4611 | return 0; |
| 4612 | } else |
| 4613 | return -1; |
| 4614 | } |
| 4615 | else if (x == Py_None) { |
| 4616 | *result = x; |
| 4617 | return 0; |
| 4618 | } |
| 4619 | else if (PyInt_Check(x)) { |
| 4620 | long value = PyInt_AS_LONG(x); |
| 4621 | long max = PyUnicode_GetMax(); |
| 4622 | if (value < 0 || value > max) { |
| 4623 | PyErr_Format(PyExc_TypeError, |
| 4624 | "character mapping must be in range(0x%lx)", max+1); |
| 4625 | Py_DECREF(x); |
| 4626 | return -1; |
| 4627 | } |
| 4628 | *result = x; |
| 4629 | return 0; |
| 4630 | } |
| 4631 | else if (PyUnicode_Check(x)) { |
| 4632 | *result = x; |
| 4633 | return 0; |
| 4634 | } |
| 4635 | else { |
| 4636 | /* wrong return value */ |
| 4637 | PyErr_SetString(PyExc_TypeError, |
| 4638 | "character mapping must return integer, None or unicode"); |
Walter Dörwald | 150523e | 2003-08-15 16:52:19 +0000 | [diff] [blame] | 4639 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4640 | return -1; |
| 4641 | } |
| 4642 | } |
| 4643 | /* ensure that *outobj is at least requiredsize characters long, |
| 4644 | if not reallocate and adjust various state variables. |
| 4645 | Return 0 on success, -1 on error */ |
| 4646 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4647 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4648 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4649 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4650 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4651 | if (requiredsize > oldsize) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4652 | /* remember old output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4653 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4654 | /* exponentially overallocate to minimize reallocations */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4655 | if (requiredsize < 2 * oldsize) |
| 4656 | requiredsize = 2 * oldsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4657 | if (_PyUnicode_Resize(outobj, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4658 | return -1; |
| 4659 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4660 | } |
| 4661 | return 0; |
| 4662 | } |
| 4663 | /* lookup the character, put the result in the output string and adjust |
| 4664 | various state variables. Return a new reference to the object that |
| 4665 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 4666 | undefined (in which case no character was written). |
| 4667 | The called must decref result. |
| 4668 | Return 0 on success, -1 on error. */ |
| 4669 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4670 | 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] | 4671 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4672 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4673 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4674 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4675 | return -1; |
| 4676 | if (*res==NULL) { |
| 4677 | /* not found => default to 1:1 mapping */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4678 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4679 | } |
| 4680 | else if (*res==Py_None) |
| 4681 | ; |
| 4682 | else if (PyInt_Check(*res)) { |
| 4683 | /* no overflow check, because we know that the space is enough */ |
| 4684 | *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); |
| 4685 | } |
| 4686 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4687 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4688 | if (repsize==1) { |
| 4689 | /* no overflow check, because we know that the space is enough */ |
| 4690 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 4691 | } |
| 4692 | else if (repsize!=0) { |
| 4693 | /* more than one character */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4694 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
Walter Dörwald | cd736e7 | 2004-02-05 17:36:00 +0000 | [diff] [blame] | 4695 | (insize - (curinp-startinp)) + |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4696 | repsize - 1; |
| 4697 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4698 | return -1; |
| 4699 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 4700 | *outp += repsize; |
| 4701 | } |
| 4702 | } |
| 4703 | else |
| 4704 | return -1; |
| 4705 | return 0; |
| 4706 | } |
| 4707 | |
| 4708 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4709 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4710 | PyObject *mapping, |
| 4711 | const char *errors) |
| 4712 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4713 | /* output object */ |
| 4714 | PyObject *res = NULL; |
| 4715 | /* pointers to the beginning and end+1 of input */ |
| 4716 | const Py_UNICODE *startp = p; |
| 4717 | const Py_UNICODE *endp = p + size; |
| 4718 | /* pointer into the output */ |
| 4719 | Py_UNICODE *str; |
| 4720 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4721 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4722 | char *reason = "character maps to <undefined>"; |
| 4723 | PyObject *errorHandler = NULL; |
| 4724 | PyObject *exc = NULL; |
| 4725 | /* the following variable is used for caching string comparisons |
| 4726 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4727 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4728 | int known_errorHandler = -1; |
| 4729 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4730 | if (mapping == NULL) { |
| 4731 | PyErr_BadArgument(); |
| 4732 | return NULL; |
| 4733 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4734 | |
| 4735 | /* allocate enough for a simple 1:1 translation without |
| 4736 | replacements, if we need more, we'll resize */ |
| 4737 | res = PyUnicode_FromUnicode(NULL, size); |
| 4738 | if (res == NULL) |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4739 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4740 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4741 | return res; |
| 4742 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4743 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4744 | while (p<endp) { |
| 4745 | /* try to encode it */ |
| 4746 | PyObject *x = NULL; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4747 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4748 | Py_XDECREF(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4749 | goto onError; |
| 4750 | } |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 4751 | Py_XDECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4752 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 4753 | ++p; |
| 4754 | else { /* untranslatable character */ |
| 4755 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4756 | Py_ssize_t repsize; |
| 4757 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4758 | Py_UNICODE *uni2; |
| 4759 | /* startpos for collecting untranslatable chars */ |
| 4760 | const Py_UNICODE *collstart = p; |
| 4761 | const Py_UNICODE *collend = p+1; |
| 4762 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4763 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4764 | /* find all untranslatable characters */ |
| 4765 | while (collend < endp) { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4766 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4767 | goto onError; |
| 4768 | Py_XDECREF(x); |
| 4769 | if (x!=Py_None) |
| 4770 | break; |
| 4771 | ++collend; |
| 4772 | } |
| 4773 | /* cache callback name lookup |
| 4774 | * (if not done yet, i.e. it's the first error) */ |
| 4775 | if (known_errorHandler==-1) { |
| 4776 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4777 | known_errorHandler = 1; |
| 4778 | else if (!strcmp(errors, "replace")) |
| 4779 | known_errorHandler = 2; |
| 4780 | else if (!strcmp(errors, "ignore")) |
| 4781 | known_errorHandler = 3; |
| 4782 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4783 | known_errorHandler = 4; |
| 4784 | else |
| 4785 | known_errorHandler = 0; |
| 4786 | } |
| 4787 | switch (known_errorHandler) { |
| 4788 | case 1: /* strict */ |
| 4789 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
| 4790 | goto onError; |
| 4791 | case 2: /* replace */ |
| 4792 | /* No need to check for space, this is a 1:1 replacement */ |
| 4793 | for (coll = collstart; coll<collend; ++coll) |
| 4794 | *str++ = '?'; |
| 4795 | /* fall through */ |
| 4796 | case 3: /* ignore */ |
| 4797 | p = collend; |
| 4798 | break; |
| 4799 | case 4: /* xmlcharrefreplace */ |
| 4800 | /* generate replacement (temporarily (mis)uses p) */ |
| 4801 | for (p = collstart; p < collend; ++p) { |
| 4802 | char buffer[2+29+1+1]; |
| 4803 | char *cp; |
| 4804 | sprintf(buffer, "&#%d;", (int)*p); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4805 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4806 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 4807 | goto onError; |
| 4808 | for (cp = buffer; *cp; ++cp) |
| 4809 | *str++ = *cp; |
| 4810 | } |
| 4811 | p = collend; |
| 4812 | break; |
| 4813 | default: |
| 4814 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 4815 | reason, startp, size, &exc, |
| 4816 | collstart-startp, collend-startp, &newpos); |
| 4817 | if (repunicode == NULL) |
| 4818 | goto onError; |
| 4819 | /* generate replacement */ |
| 4820 | repsize = PyUnicode_GET_SIZE(repunicode); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4821 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4822 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 4823 | Py_DECREF(repunicode); |
| 4824 | goto onError; |
| 4825 | } |
| 4826 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 4827 | *str++ = *uni2; |
| 4828 | p = startp + newpos; |
| 4829 | Py_DECREF(repunicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4830 | } |
| 4831 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4832 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4833 | /* Resize if we allocated to much */ |
| 4834 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4835 | if (respos<PyUnicode_GET_SIZE(res)) { |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4836 | if (_PyUnicode_Resize(&res, respos) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 4837 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4838 | } |
| 4839 | Py_XDECREF(exc); |
| 4840 | Py_XDECREF(errorHandler); |
| 4841 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4842 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4843 | onError: |
| 4844 | Py_XDECREF(res); |
| 4845 | Py_XDECREF(exc); |
| 4846 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4847 | return NULL; |
| 4848 | } |
| 4849 | |
| 4850 | PyObject *PyUnicode_Translate(PyObject *str, |
| 4851 | PyObject *mapping, |
| 4852 | const char *errors) |
| 4853 | { |
| 4854 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4855 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4856 | str = PyUnicode_FromObject(str); |
| 4857 | if (str == NULL) |
| 4858 | goto onError; |
| 4859 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 4860 | PyUnicode_GET_SIZE(str), |
| 4861 | mapping, |
| 4862 | errors); |
| 4863 | Py_DECREF(str); |
| 4864 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4865 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4866 | onError: |
| 4867 | Py_XDECREF(str); |
| 4868 | return NULL; |
| 4869 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4870 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4871 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 4872 | |
| 4873 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4874 | Py_ssize_t length, |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4875 | char *output, |
| 4876 | const char *errors) |
| 4877 | { |
| 4878 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4879 | PyObject *errorHandler = NULL; |
| 4880 | PyObject *exc = NULL; |
| 4881 | const char *encoding = "decimal"; |
| 4882 | const char *reason = "invalid decimal Unicode string"; |
| 4883 | /* the following variable is used for caching string comparisons |
| 4884 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 4885 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4886 | |
| 4887 | if (output == NULL) { |
| 4888 | PyErr_BadArgument(); |
| 4889 | return -1; |
| 4890 | } |
| 4891 | |
| 4892 | p = s; |
| 4893 | end = s + length; |
| 4894 | while (p < end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4895 | register Py_UNICODE ch = *p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4896 | int decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4897 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4898 | Py_ssize_t repsize; |
| 4899 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4900 | Py_UNICODE *uni2; |
| 4901 | Py_UNICODE *collstart; |
| 4902 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4903 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4904 | if (Py_UNICODE_ISSPACE(ch)) { |
| 4905 | *output++ = ' '; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4906 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4907 | continue; |
| 4908 | } |
| 4909 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 4910 | if (decimal >= 0) { |
| 4911 | *output++ = '0' + decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4912 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4913 | continue; |
| 4914 | } |
Guido van Rossum | ba47704 | 2000-04-06 18:18:10 +0000 | [diff] [blame] | 4915 | if (0 < ch && ch < 256) { |
Guido van Rossum | 42c29aa | 2000-05-03 23:58:29 +0000 | [diff] [blame] | 4916 | *output++ = (char)ch; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4917 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4918 | continue; |
| 4919 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4920 | /* All other characters are considered unencodable */ |
| 4921 | collstart = p; |
| 4922 | collend = p+1; |
| 4923 | while (collend < end) { |
| 4924 | if ((0 < *collend && *collend < 256) || |
| 4925 | !Py_UNICODE_ISSPACE(*collend) || |
| 4926 | Py_UNICODE_TODECIMAL(*collend)) |
| 4927 | break; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4928 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4929 | /* cache callback name lookup |
| 4930 | * (if not done yet, i.e. it's the first error) */ |
| 4931 | if (known_errorHandler==-1) { |
| 4932 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4933 | known_errorHandler = 1; |
| 4934 | else if (!strcmp(errors, "replace")) |
| 4935 | known_errorHandler = 2; |
| 4936 | else if (!strcmp(errors, "ignore")) |
| 4937 | known_errorHandler = 3; |
| 4938 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4939 | known_errorHandler = 4; |
| 4940 | else |
| 4941 | known_errorHandler = 0; |
| 4942 | } |
| 4943 | switch (known_errorHandler) { |
| 4944 | case 1: /* strict */ |
| 4945 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 4946 | goto onError; |
| 4947 | case 2: /* replace */ |
| 4948 | for (p = collstart; p < collend; ++p) |
| 4949 | *output++ = '?'; |
| 4950 | /* fall through */ |
| 4951 | case 3: /* ignore */ |
| 4952 | p = collend; |
| 4953 | break; |
| 4954 | case 4: /* xmlcharrefreplace */ |
| 4955 | /* generate replacement (temporarily (mis)uses p) */ |
| 4956 | for (p = collstart; p < collend; ++p) |
| 4957 | output += sprintf(output, "&#%d;", (int)*p); |
| 4958 | p = collend; |
| 4959 | break; |
| 4960 | default: |
| 4961 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 4962 | encoding, reason, s, length, &exc, |
| 4963 | collstart-s, collend-s, &newpos); |
| 4964 | if (repunicode == NULL) |
| 4965 | goto onError; |
| 4966 | /* generate replacement */ |
| 4967 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4968 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 4969 | Py_UNICODE ch = *uni2; |
| 4970 | if (Py_UNICODE_ISSPACE(ch)) |
| 4971 | *output++ = ' '; |
| 4972 | else { |
| 4973 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 4974 | if (decimal >= 0) |
| 4975 | *output++ = '0' + decimal; |
| 4976 | else if (0 < ch && ch < 256) |
| 4977 | *output++ = (char)ch; |
| 4978 | else { |
| 4979 | Py_DECREF(repunicode); |
| 4980 | raise_encode_exception(&exc, encoding, |
| 4981 | s, length, collstart-s, collend-s, reason); |
| 4982 | goto onError; |
| 4983 | } |
| 4984 | } |
| 4985 | } |
| 4986 | p = s + newpos; |
| 4987 | Py_DECREF(repunicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4988 | } |
| 4989 | } |
| 4990 | /* 0-terminate the output string */ |
| 4991 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4992 | Py_XDECREF(exc); |
| 4993 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4994 | return 0; |
| 4995 | |
| 4996 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4997 | Py_XDECREF(exc); |
| 4998 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4999 | return -1; |
| 5000 | } |
| 5001 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5002 | /* --- Helpers ------------------------------------------------------------ */ |
| 5003 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 5004 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5005 | |
| 5006 | #include "stringlib/fastsearch.h" |
| 5007 | |
| 5008 | #include "stringlib/count.h" |
| 5009 | #include "stringlib/find.h" |
| 5010 | #include "stringlib/partition.h" |
| 5011 | |
| 5012 | /* helper macro to fixup start/end slice values */ |
| 5013 | #define FIX_START_END(obj) \ |
| 5014 | if (start < 0) \ |
| 5015 | start += (obj)->length; \ |
| 5016 | if (start < 0) \ |
| 5017 | start = 0; \ |
| 5018 | if (end > (obj)->length) \ |
| 5019 | end = (obj)->length; \ |
| 5020 | if (end < 0) \ |
| 5021 | end += (obj)->length; \ |
| 5022 | if (end < 0) \ |
| 5023 | end = 0; |
| 5024 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5025 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5026 | PyObject *substr, |
| 5027 | Py_ssize_t start, |
| 5028 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5029 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5030 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5031 | PyUnicodeObject* str_obj; |
| 5032 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5033 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5034 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 5035 | if (!str_obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5036 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5037 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 5038 | if (!sub_obj) { |
| 5039 | Py_DECREF(str_obj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5040 | return -1; |
| 5041 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5042 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5043 | FIX_START_END(str_obj); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5044 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5045 | result = stringlib_count( |
| 5046 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
| 5047 | ); |
| 5048 | |
| 5049 | Py_DECREF(sub_obj); |
| 5050 | Py_DECREF(str_obj); |
| 5051 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5052 | return result; |
| 5053 | } |
| 5054 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5055 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5056 | PyObject *sub, |
| 5057 | Py_ssize_t start, |
| 5058 | Py_ssize_t end, |
| 5059 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5060 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5061 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5062 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5063 | str = PyUnicode_FromObject(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5064 | if (!str) |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 5065 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5066 | sub = PyUnicode_FromObject(sub); |
| 5067 | if (!sub) { |
Marc-André Lemburg | 4164439 | 2002-05-29 13:46:29 +0000 | [diff] [blame] | 5068 | Py_DECREF(str); |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 5069 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5070 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5071 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5072 | if (direction > 0) |
| 5073 | result = stringlib_find_slice( |
| 5074 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5075 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5076 | start, end |
| 5077 | ); |
| 5078 | else |
| 5079 | result = stringlib_rfind_slice( |
| 5080 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5081 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5082 | start, end |
| 5083 | ); |
| 5084 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5085 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5086 | Py_DECREF(sub); |
| 5087 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5088 | return result; |
| 5089 | } |
| 5090 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5091 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5092 | int tailmatch(PyUnicodeObject *self, |
| 5093 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5094 | Py_ssize_t start, |
| 5095 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5096 | int direction) |
| 5097 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5098 | if (substring->length == 0) |
| 5099 | return 1; |
| 5100 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5101 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5102 | |
| 5103 | end -= substring->length; |
| 5104 | if (end < start) |
| 5105 | return 0; |
| 5106 | |
| 5107 | if (direction > 0) { |
| 5108 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 5109 | return 1; |
| 5110 | } else { |
| 5111 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 5112 | return 1; |
| 5113 | } |
| 5114 | |
| 5115 | return 0; |
| 5116 | } |
| 5117 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5118 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5119 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5120 | Py_ssize_t start, |
| 5121 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5122 | int direction) |
| 5123 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5124 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5125 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5126 | str = PyUnicode_FromObject(str); |
| 5127 | if (str == NULL) |
| 5128 | return -1; |
| 5129 | substr = PyUnicode_FromObject(substr); |
| 5130 | if (substr == NULL) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5131 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5132 | return -1; |
| 5133 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5134 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5135 | result = tailmatch((PyUnicodeObject *)str, |
| 5136 | (PyUnicodeObject *)substr, |
| 5137 | start, end, direction); |
| 5138 | Py_DECREF(str); |
| 5139 | Py_DECREF(substr); |
| 5140 | return result; |
| 5141 | } |
| 5142 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5143 | /* Apply fixfct filter to the Unicode object self and return a |
| 5144 | reference to the modified object */ |
| 5145 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5146 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5147 | PyObject *fixup(PyUnicodeObject *self, |
| 5148 | int (*fixfct)(PyUnicodeObject *s)) |
| 5149 | { |
| 5150 | |
| 5151 | PyUnicodeObject *u; |
| 5152 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5153 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5154 | if (u == NULL) |
| 5155 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5156 | |
| 5157 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5158 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5159 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5160 | /* fixfct should return TRUE if it modified the buffer. If |
| 5161 | FALSE, return a reference to the original buffer instead |
| 5162 | (to save space, not time) */ |
| 5163 | Py_INCREF(self); |
| 5164 | Py_DECREF(u); |
| 5165 | return (PyObject*) self; |
| 5166 | } |
| 5167 | return (PyObject*) u; |
| 5168 | } |
| 5169 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5170 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5171 | int fixupper(PyUnicodeObject *self) |
| 5172 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5173 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5174 | Py_UNICODE *s = self->str; |
| 5175 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5176 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5177 | while (len-- > 0) { |
| 5178 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5179 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5180 | ch = Py_UNICODE_TOUPPER(*s); |
| 5181 | if (ch != *s) { |
| 5182 | status = 1; |
| 5183 | *s = ch; |
| 5184 | } |
| 5185 | s++; |
| 5186 | } |
| 5187 | |
| 5188 | return status; |
| 5189 | } |
| 5190 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5191 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5192 | int fixlower(PyUnicodeObject *self) |
| 5193 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5194 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5195 | Py_UNICODE *s = self->str; |
| 5196 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5197 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5198 | while (len-- > 0) { |
| 5199 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5200 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5201 | ch = Py_UNICODE_TOLOWER(*s); |
| 5202 | if (ch != *s) { |
| 5203 | status = 1; |
| 5204 | *s = ch; |
| 5205 | } |
| 5206 | s++; |
| 5207 | } |
| 5208 | |
| 5209 | return status; |
| 5210 | } |
| 5211 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5212 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5213 | int fixswapcase(PyUnicodeObject *self) |
| 5214 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5215 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5216 | Py_UNICODE *s = self->str; |
| 5217 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5218 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5219 | while (len-- > 0) { |
| 5220 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5221 | *s = Py_UNICODE_TOLOWER(*s); |
| 5222 | status = 1; |
| 5223 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 5224 | *s = Py_UNICODE_TOUPPER(*s); |
| 5225 | status = 1; |
| 5226 | } |
| 5227 | s++; |
| 5228 | } |
| 5229 | |
| 5230 | return status; |
| 5231 | } |
| 5232 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5233 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5234 | int fixcapitalize(PyUnicodeObject *self) |
| 5235 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5236 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5237 | Py_UNICODE *s = self->str; |
| 5238 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5239 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5240 | if (len == 0) |
| 5241 | return 0; |
| 5242 | if (Py_UNICODE_ISLOWER(*s)) { |
| 5243 | *s = Py_UNICODE_TOUPPER(*s); |
| 5244 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5245 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5246 | s++; |
| 5247 | while (--len > 0) { |
| 5248 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5249 | *s = Py_UNICODE_TOLOWER(*s); |
| 5250 | status = 1; |
| 5251 | } |
| 5252 | s++; |
| 5253 | } |
| 5254 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5255 | } |
| 5256 | |
| 5257 | static |
| 5258 | int fixtitle(PyUnicodeObject *self) |
| 5259 | { |
| 5260 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5261 | register Py_UNICODE *e; |
| 5262 | int previous_is_cased; |
| 5263 | |
| 5264 | /* Shortcut for single character strings */ |
| 5265 | if (PyUnicode_GET_SIZE(self) == 1) { |
| 5266 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 5267 | if (*p != ch) { |
| 5268 | *p = ch; |
| 5269 | return 1; |
| 5270 | } |
| 5271 | else |
| 5272 | return 0; |
| 5273 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5274 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5275 | e = p + PyUnicode_GET_SIZE(self); |
| 5276 | previous_is_cased = 0; |
| 5277 | for (; p < e; p++) { |
| 5278 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5279 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5280 | if (previous_is_cased) |
| 5281 | *p = Py_UNICODE_TOLOWER(ch); |
| 5282 | else |
| 5283 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5284 | |
| 5285 | if (Py_UNICODE_ISLOWER(ch) || |
| 5286 | Py_UNICODE_ISUPPER(ch) || |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5287 | Py_UNICODE_ISTITLE(ch)) |
| 5288 | previous_is_cased = 1; |
| 5289 | else |
| 5290 | previous_is_cased = 0; |
| 5291 | } |
| 5292 | return 1; |
| 5293 | } |
| 5294 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5295 | PyObject * |
| 5296 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5297 | { |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5298 | PyObject *internal_separator = NULL; |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 5299 | const Py_UNICODE blank = ' '; |
| 5300 | const Py_UNICODE *sep = ␣ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5301 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5302 | PyUnicodeObject *res = NULL; /* the result */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5303 | Py_ssize_t res_alloc = 100; /* # allocated bytes for string in res */ |
| 5304 | Py_ssize_t res_used; /* # used bytes */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5305 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 5306 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5307 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5308 | PyObject *item; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5309 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5310 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5311 | fseq = PySequence_Fast(seq, ""); |
| 5312 | if (fseq == NULL) { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5313 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5314 | } |
| 5315 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5316 | /* Grrrr. A codec may be invoked to convert str objects to |
| 5317 | * Unicode, and so it's possible to call back into Python code |
| 5318 | * during PyUnicode_FromObject(), and so it's possible for a sick |
| 5319 | * codec to change the size of fseq (if seq is a list). Therefore |
| 5320 | * we have to keep refetching the size -- can't assume seqlen |
| 5321 | * is invariant. |
| 5322 | */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5323 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5324 | /* If empty sequence, return u"". */ |
| 5325 | if (seqlen == 0) { |
| 5326 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 5327 | goto Done; |
| 5328 | } |
| 5329 | /* If singleton sequence with an exact Unicode, return that. */ |
| 5330 | if (seqlen == 1) { |
| 5331 | item = PySequence_Fast_GET_ITEM(fseq, 0); |
| 5332 | if (PyUnicode_CheckExact(item)) { |
| 5333 | Py_INCREF(item); |
| 5334 | res = (PyUnicodeObject *)item; |
| 5335 | goto Done; |
| 5336 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5337 | } |
| 5338 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5339 | /* At least two items to join, or one that isn't exact Unicode. */ |
| 5340 | if (seqlen > 1) { |
| 5341 | /* Set up sep and seplen -- they're needed. */ |
| 5342 | if (separator == NULL) { |
| 5343 | sep = ␣ |
| 5344 | seplen = 1; |
| 5345 | } |
| 5346 | else { |
| 5347 | internal_separator = PyUnicode_FromObject(separator); |
| 5348 | if (internal_separator == NULL) |
| 5349 | goto onError; |
| 5350 | sep = PyUnicode_AS_UNICODE(internal_separator); |
| 5351 | seplen = PyUnicode_GET_SIZE(internal_separator); |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5352 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 5353 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5354 | } |
| 5355 | } |
| 5356 | |
| 5357 | /* Get space. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5358 | res = _PyUnicode_New(res_alloc); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5359 | if (res == NULL) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5360 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5361 | res_p = PyUnicode_AS_UNICODE(res); |
| 5362 | res_used = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5363 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5364 | for (i = 0; i < seqlen; ++i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5365 | Py_ssize_t itemlen; |
| 5366 | Py_ssize_t new_res_used; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5367 | |
| 5368 | item = PySequence_Fast_GET_ITEM(fseq, i); |
| 5369 | /* Convert item to Unicode. */ |
| 5370 | if (! PyUnicode_Check(item) && ! PyString_Check(item)) { |
| 5371 | PyErr_Format(PyExc_TypeError, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5372 | "sequence item %zd: expected string or Unicode," |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5373 | " %.80s found", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 5374 | i, Py_Type(item)->tp_name); |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 5375 | goto onError; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5376 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5377 | item = PyUnicode_FromObject(item); |
| 5378 | if (item == NULL) |
| 5379 | goto onError; |
| 5380 | /* We own a reference to item from here on. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5381 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5382 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 5383 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5384 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5385 | /* 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] | 5386 | itemlen = PyUnicode_GET_SIZE(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5387 | new_res_used = res_used + itemlen; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5388 | if (new_res_used < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5389 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5390 | if (i < seqlen - 1) { |
| 5391 | new_res_used += seplen; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5392 | if (new_res_used < 0) |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5393 | goto Overflow; |
| 5394 | } |
| 5395 | if (new_res_used > res_alloc) { |
| 5396 | /* double allocated size until it's big enough */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5397 | do { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5398 | res_alloc += res_alloc; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5399 | if (res_alloc <= 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5400 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5401 | } while (new_res_used > res_alloc); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5402 | if (_PyUnicode_Resize(&res, res_alloc) < 0) { |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 5403 | Py_DECREF(item); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5404 | goto onError; |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 5405 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5406 | res_p = PyUnicode_AS_UNICODE(res) + res_used; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5407 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5408 | |
| 5409 | /* Copy item, and maybe the separator. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5410 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5411 | res_p += itemlen; |
| 5412 | if (i < seqlen - 1) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5413 | Py_UNICODE_COPY(res_p, sep, seplen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5414 | res_p += seplen; |
| 5415 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5416 | Py_DECREF(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5417 | res_used = new_res_used; |
| 5418 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5419 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5420 | /* Shrink res to match the used area; this probably can't fail, |
| 5421 | * but it's cheap to check. |
| 5422 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5423 | if (_PyUnicode_Resize(&res, res_used) < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5424 | goto onError; |
| 5425 | |
| 5426 | Done: |
| 5427 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5428 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5429 | return (PyObject *)res; |
| 5430 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5431 | Overflow: |
| 5432 | PyErr_SetString(PyExc_OverflowError, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5433 | "join() result is too long for a Python string"); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5434 | Py_DECREF(item); |
| 5435 | /* fall through */ |
| 5436 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5437 | onError: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5438 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5439 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5440 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5441 | return NULL; |
| 5442 | } |
| 5443 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5444 | static |
| 5445 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5446 | Py_ssize_t left, |
| 5447 | Py_ssize_t right, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5448 | Py_UNICODE fill) |
| 5449 | { |
| 5450 | PyUnicodeObject *u; |
| 5451 | |
| 5452 | if (left < 0) |
| 5453 | left = 0; |
| 5454 | if (right < 0) |
| 5455 | right = 0; |
| 5456 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5457 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5458 | Py_INCREF(self); |
| 5459 | return self; |
| 5460 | } |
| 5461 | |
| 5462 | u = _PyUnicode_New(left + self->length + right); |
| 5463 | if (u) { |
| 5464 | if (left) |
| 5465 | Py_UNICODE_FILL(u->str, fill, left); |
| 5466 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 5467 | if (right) |
| 5468 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 5469 | } |
| 5470 | |
| 5471 | return u; |
| 5472 | } |
| 5473 | |
| 5474 | #define SPLIT_APPEND(data, left, right) \ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5475 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5476 | if (!str) \ |
| 5477 | goto onError; \ |
| 5478 | if (PyList_Append(list, str)) { \ |
| 5479 | Py_DECREF(str); \ |
| 5480 | goto onError; \ |
| 5481 | } \ |
| 5482 | else \ |
| 5483 | Py_DECREF(str); |
| 5484 | |
| 5485 | static |
| 5486 | PyObject *split_whitespace(PyUnicodeObject *self, |
| 5487 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5488 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5489 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5490 | register Py_ssize_t i; |
| 5491 | register Py_ssize_t j; |
| 5492 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5493 | PyObject *str; |
| 5494 | |
| 5495 | for (i = j = 0; i < len; ) { |
| 5496 | /* find a token */ |
| 5497 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 5498 | i++; |
| 5499 | j = i; |
| 5500 | while (i < len && !Py_UNICODE_ISSPACE(self->str[i])) |
| 5501 | i++; |
| 5502 | if (j < i) { |
| 5503 | if (maxcount-- <= 0) |
| 5504 | break; |
| 5505 | SPLIT_APPEND(self->str, j, i); |
| 5506 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 5507 | i++; |
| 5508 | j = i; |
| 5509 | } |
| 5510 | } |
| 5511 | if (j < len) { |
| 5512 | SPLIT_APPEND(self->str, j, len); |
| 5513 | } |
| 5514 | return list; |
| 5515 | |
| 5516 | onError: |
| 5517 | Py_DECREF(list); |
| 5518 | return NULL; |
| 5519 | } |
| 5520 | |
| 5521 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5522 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5523 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5524 | register Py_ssize_t i; |
| 5525 | register Py_ssize_t j; |
| 5526 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5527 | PyObject *list; |
| 5528 | PyObject *str; |
| 5529 | Py_UNICODE *data; |
| 5530 | |
| 5531 | string = PyUnicode_FromObject(string); |
| 5532 | if (string == NULL) |
| 5533 | return NULL; |
| 5534 | data = PyUnicode_AS_UNICODE(string); |
| 5535 | len = PyUnicode_GET_SIZE(string); |
| 5536 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5537 | list = PyList_New(0); |
| 5538 | if (!list) |
| 5539 | goto onError; |
| 5540 | |
| 5541 | for (i = j = 0; i < len; ) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5542 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5543 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5544 | /* Find a line and append it */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5545 | while (i < len && !BLOOM_LINEBREAK(data[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5546 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5547 | |
| 5548 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5549 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5550 | if (i < len) { |
| 5551 | if (data[i] == '\r' && i + 1 < len && |
| 5552 | data[i+1] == '\n') |
| 5553 | i += 2; |
| 5554 | else |
| 5555 | i++; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5556 | if (keepends) |
| 5557 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5558 | } |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5559 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5560 | j = i; |
| 5561 | } |
| 5562 | if (j < len) { |
| 5563 | SPLIT_APPEND(data, j, len); |
| 5564 | } |
| 5565 | |
| 5566 | Py_DECREF(string); |
| 5567 | return list; |
| 5568 | |
| 5569 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5570 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5571 | Py_DECREF(string); |
| 5572 | return NULL; |
| 5573 | } |
| 5574 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5575 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5576 | PyObject *split_char(PyUnicodeObject *self, |
| 5577 | PyObject *list, |
| 5578 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5579 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5580 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5581 | register Py_ssize_t i; |
| 5582 | register Py_ssize_t j; |
| 5583 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5584 | PyObject *str; |
| 5585 | |
| 5586 | for (i = j = 0; i < len; ) { |
| 5587 | if (self->str[i] == ch) { |
| 5588 | if (maxcount-- <= 0) |
| 5589 | break; |
| 5590 | SPLIT_APPEND(self->str, j, i); |
| 5591 | i = j = i + 1; |
| 5592 | } else |
| 5593 | i++; |
| 5594 | } |
| 5595 | if (j <= len) { |
| 5596 | SPLIT_APPEND(self->str, j, len); |
| 5597 | } |
| 5598 | return list; |
| 5599 | |
| 5600 | onError: |
| 5601 | Py_DECREF(list); |
| 5602 | return NULL; |
| 5603 | } |
| 5604 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5605 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5606 | PyObject *split_substring(PyUnicodeObject *self, |
| 5607 | PyObject *list, |
| 5608 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5609 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5610 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5611 | register Py_ssize_t i; |
| 5612 | register Py_ssize_t j; |
| 5613 | Py_ssize_t len = self->length; |
| 5614 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5615 | PyObject *str; |
| 5616 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 5617 | for (i = j = 0; i <= len - sublen; ) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5618 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5619 | if (maxcount-- <= 0) |
| 5620 | break; |
| 5621 | SPLIT_APPEND(self->str, j, i); |
| 5622 | i = j = i + sublen; |
| 5623 | } else |
| 5624 | i++; |
| 5625 | } |
| 5626 | if (j <= len) { |
| 5627 | SPLIT_APPEND(self->str, j, len); |
| 5628 | } |
| 5629 | return list; |
| 5630 | |
| 5631 | onError: |
| 5632 | Py_DECREF(list); |
| 5633 | return NULL; |
| 5634 | } |
| 5635 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5636 | static |
| 5637 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
| 5638 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5639 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5640 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5641 | register Py_ssize_t i; |
| 5642 | register Py_ssize_t j; |
| 5643 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5644 | PyObject *str; |
| 5645 | |
| 5646 | for (i = j = len - 1; i >= 0; ) { |
| 5647 | /* find a token */ |
| 5648 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 5649 | i--; |
| 5650 | j = i; |
| 5651 | while (i >= 0 && !Py_UNICODE_ISSPACE(self->str[i])) |
| 5652 | i--; |
| 5653 | if (j > i) { |
| 5654 | if (maxcount-- <= 0) |
| 5655 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5656 | SPLIT_APPEND(self->str, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5657 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 5658 | i--; |
| 5659 | j = i; |
| 5660 | } |
| 5661 | } |
| 5662 | if (j >= 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5663 | SPLIT_APPEND(self->str, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5664 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5665 | if (PyList_Reverse(list) < 0) |
| 5666 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5667 | return list; |
| 5668 | |
| 5669 | onError: |
| 5670 | Py_DECREF(list); |
| 5671 | return NULL; |
| 5672 | } |
| 5673 | |
| 5674 | static |
| 5675 | PyObject *rsplit_char(PyUnicodeObject *self, |
| 5676 | PyObject *list, |
| 5677 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5678 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5679 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5680 | register Py_ssize_t i; |
| 5681 | register Py_ssize_t j; |
| 5682 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5683 | PyObject *str; |
| 5684 | |
| 5685 | for (i = j = len - 1; i >= 0; ) { |
| 5686 | if (self->str[i] == ch) { |
| 5687 | if (maxcount-- <= 0) |
| 5688 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5689 | SPLIT_APPEND(self->str, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5690 | j = i = i - 1; |
| 5691 | } else |
| 5692 | i--; |
| 5693 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 5694 | if (j >= -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5695 | SPLIT_APPEND(self->str, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5696 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5697 | if (PyList_Reverse(list) < 0) |
| 5698 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5699 | return list; |
| 5700 | |
| 5701 | onError: |
| 5702 | Py_DECREF(list); |
| 5703 | return NULL; |
| 5704 | } |
| 5705 | |
| 5706 | static |
| 5707 | PyObject *rsplit_substring(PyUnicodeObject *self, |
| 5708 | PyObject *list, |
| 5709 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5710 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5711 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5712 | register Py_ssize_t i; |
| 5713 | register Py_ssize_t j; |
| 5714 | Py_ssize_t len = self->length; |
| 5715 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5716 | PyObject *str; |
| 5717 | |
| 5718 | for (i = len - sublen, j = len; i >= 0; ) { |
| 5719 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5720 | if (maxcount-- <= 0) |
| 5721 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5722 | SPLIT_APPEND(self->str, i + sublen, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5723 | j = i; |
| 5724 | i -= sublen; |
| 5725 | } else |
| 5726 | i--; |
| 5727 | } |
| 5728 | if (j >= 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5729 | SPLIT_APPEND(self->str, 0, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5730 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5731 | if (PyList_Reverse(list) < 0) |
| 5732 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5733 | return list; |
| 5734 | |
| 5735 | onError: |
| 5736 | Py_DECREF(list); |
| 5737 | return NULL; |
| 5738 | } |
| 5739 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5740 | #undef SPLIT_APPEND |
| 5741 | |
| 5742 | static |
| 5743 | PyObject *split(PyUnicodeObject *self, |
| 5744 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5745 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5746 | { |
| 5747 | PyObject *list; |
| 5748 | |
| 5749 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5750 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5751 | |
| 5752 | list = PyList_New(0); |
| 5753 | if (!list) |
| 5754 | return NULL; |
| 5755 | |
| 5756 | if (substring == NULL) |
| 5757 | return split_whitespace(self,list,maxcount); |
| 5758 | |
| 5759 | else if (substring->length == 1) |
| 5760 | return split_char(self,list,substring->str[0],maxcount); |
| 5761 | |
| 5762 | else if (substring->length == 0) { |
| 5763 | Py_DECREF(list); |
| 5764 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5765 | return NULL; |
| 5766 | } |
| 5767 | else |
| 5768 | return split_substring(self,list,substring,maxcount); |
| 5769 | } |
| 5770 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5771 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5772 | PyObject *rsplit(PyUnicodeObject *self, |
| 5773 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5774 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5775 | { |
| 5776 | PyObject *list; |
| 5777 | |
| 5778 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5779 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5780 | |
| 5781 | list = PyList_New(0); |
| 5782 | if (!list) |
| 5783 | return NULL; |
| 5784 | |
| 5785 | if (substring == NULL) |
| 5786 | return rsplit_whitespace(self,list,maxcount); |
| 5787 | |
| 5788 | else if (substring->length == 1) |
| 5789 | return rsplit_char(self,list,substring->str[0],maxcount); |
| 5790 | |
| 5791 | else if (substring->length == 0) { |
| 5792 | Py_DECREF(list); |
| 5793 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5794 | return NULL; |
| 5795 | } |
| 5796 | else |
| 5797 | return rsplit_substring(self,list,substring,maxcount); |
| 5798 | } |
| 5799 | |
| 5800 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5801 | PyObject *replace(PyUnicodeObject *self, |
| 5802 | PyUnicodeObject *str1, |
| 5803 | PyUnicodeObject *str2, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5804 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5805 | { |
| 5806 | PyUnicodeObject *u; |
| 5807 | |
| 5808 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5809 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5810 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5811 | if (str1->length == str2->length) { |
| 5812 | /* same length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5813 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5814 | if (str1->length == 1) { |
| 5815 | /* replace characters */ |
| 5816 | Py_UNICODE u1, u2; |
| 5817 | if (!findchar(self->str, self->length, str1->str[0])) |
| 5818 | goto nothing; |
| 5819 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5820 | if (!u) |
| 5821 | return NULL; |
| 5822 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5823 | u1 = str1->str[0]; |
| 5824 | u2 = str2->str[0]; |
| 5825 | for (i = 0; i < u->length; i++) |
| 5826 | if (u->str[i] == u1) { |
| 5827 | if (--maxcount < 0) |
| 5828 | break; |
| 5829 | u->str[i] = u2; |
| 5830 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5831 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5832 | i = fastsearch( |
| 5833 | self->str, self->length, str1->str, str1->length, FAST_SEARCH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5834 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5835 | if (i < 0) |
| 5836 | goto nothing; |
| 5837 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5838 | if (!u) |
| 5839 | return NULL; |
| 5840 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5841 | while (i <= self->length - str1->length) |
| 5842 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 5843 | if (--maxcount < 0) |
| 5844 | break; |
| 5845 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 5846 | i += str1->length; |
| 5847 | } else |
| 5848 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5849 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5850 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5851 | |
| 5852 | Py_ssize_t n, i, j, e; |
| 5853 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5854 | Py_UNICODE *p; |
| 5855 | |
| 5856 | /* replace strings */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5857 | n = stringlib_count(self->str, self->length, str1->str, str1->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5858 | if (n > maxcount) |
| 5859 | n = maxcount; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5860 | if (n == 0) |
| 5861 | goto nothing; |
| 5862 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 5863 | delta = (str2->length - str1->length); |
| 5864 | if (delta == 0) { |
| 5865 | new_size = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5866 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5867 | product = n * (str2->length - str1->length); |
| 5868 | if ((product / (str2->length - str1->length)) != n) { |
| 5869 | PyErr_SetString(PyExc_OverflowError, |
| 5870 | "replace string is too long"); |
| 5871 | return NULL; |
| 5872 | } |
| 5873 | new_size = self->length + product; |
| 5874 | if (new_size < 0) { |
| 5875 | PyErr_SetString(PyExc_OverflowError, |
| 5876 | "replace string is too long"); |
| 5877 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5878 | } |
| 5879 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5880 | u = _PyUnicode_New(new_size); |
| 5881 | if (!u) |
| 5882 | return NULL; |
| 5883 | i = 0; |
| 5884 | p = u->str; |
| 5885 | e = self->length - str1->length; |
| 5886 | if (str1->length > 0) { |
| 5887 | while (n-- > 0) { |
| 5888 | /* look for next match */ |
| 5889 | j = i; |
| 5890 | while (j <= e) { |
| 5891 | if (Py_UNICODE_MATCH(self, j, str1)) |
| 5892 | break; |
| 5893 | j++; |
| 5894 | } |
| 5895 | if (j > i) { |
| 5896 | if (j > e) |
| 5897 | break; |
| 5898 | /* copy unchanged part [i:j] */ |
| 5899 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 5900 | p += j - i; |
| 5901 | } |
| 5902 | /* copy substitution string */ |
| 5903 | if (str2->length > 0) { |
| 5904 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 5905 | p += str2->length; |
| 5906 | } |
| 5907 | i = j + str1->length; |
| 5908 | } |
| 5909 | if (i < self->length) |
| 5910 | /* copy tail [i:] */ |
| 5911 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 5912 | } else { |
| 5913 | /* interleave */ |
| 5914 | while (n > 0) { |
| 5915 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 5916 | p += str2->length; |
| 5917 | if (--n <= 0) |
| 5918 | break; |
| 5919 | *p++ = self->str[i++]; |
| 5920 | } |
| 5921 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 5922 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5923 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5924 | return (PyObject *) u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5925 | |
| 5926 | nothing: |
| 5927 | /* nothing to replace; return original string (when possible) */ |
| 5928 | if (PyUnicode_CheckExact(self)) { |
| 5929 | Py_INCREF(self); |
| 5930 | return (PyObject *) self; |
| 5931 | } |
| 5932 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5933 | } |
| 5934 | |
| 5935 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 5936 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5937 | PyDoc_STRVAR(title__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5938 | "S.title() -> unicode\n\ |
| 5939 | \n\ |
| 5940 | 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] | 5941 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5942 | |
| 5943 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5944 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5945 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5946 | return fixup(self, fixtitle); |
| 5947 | } |
| 5948 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5949 | PyDoc_STRVAR(capitalize__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5950 | "S.capitalize() -> unicode\n\ |
| 5951 | \n\ |
| 5952 | 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] | 5953 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5954 | |
| 5955 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5956 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5957 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5958 | return fixup(self, fixcapitalize); |
| 5959 | } |
| 5960 | |
| 5961 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5962 | PyDoc_STRVAR(capwords__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5963 | "S.capwords() -> unicode\n\ |
| 5964 | \n\ |
| 5965 | 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] | 5966 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5967 | |
| 5968 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5969 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5970 | { |
| 5971 | PyObject *list; |
| 5972 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5973 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5974 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5975 | /* Split into words */ |
| 5976 | list = split(self, NULL, -1); |
| 5977 | if (!list) |
| 5978 | return NULL; |
| 5979 | |
| 5980 | /* Capitalize each word */ |
| 5981 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 5982 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 5983 | fixcapitalize); |
| 5984 | if (item == NULL) |
| 5985 | goto onError; |
| 5986 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 5987 | PyList_SET_ITEM(list, i, item); |
| 5988 | } |
| 5989 | |
| 5990 | /* Join the words to form a new string */ |
| 5991 | item = PyUnicode_Join(NULL, list); |
| 5992 | |
| 5993 | onError: |
| 5994 | Py_DECREF(list); |
| 5995 | return (PyObject *)item; |
| 5996 | } |
| 5997 | #endif |
| 5998 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5999 | /* Argument converter. Coerces to a single unicode character */ |
| 6000 | |
| 6001 | static int |
| 6002 | convert_uc(PyObject *obj, void *addr) |
| 6003 | { |
| 6004 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 6005 | PyObject *uniobj; |
| 6006 | Py_UNICODE *unistr; |
| 6007 | |
| 6008 | uniobj = PyUnicode_FromObject(obj); |
| 6009 | if (uniobj == NULL) { |
| 6010 | PyErr_SetString(PyExc_TypeError, |
| 6011 | "The fill character cannot be converted to Unicode"); |
| 6012 | return 0; |
| 6013 | } |
| 6014 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 6015 | PyErr_SetString(PyExc_TypeError, |
| 6016 | "The fill character must be exactly one character long"); |
| 6017 | Py_DECREF(uniobj); |
| 6018 | return 0; |
| 6019 | } |
| 6020 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 6021 | *fillcharloc = unistr[0]; |
| 6022 | Py_DECREF(uniobj); |
| 6023 | return 1; |
| 6024 | } |
| 6025 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6026 | PyDoc_STRVAR(center__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6027 | "S.center(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6028 | \n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6029 | Return S centered in a Unicode string of length width. Padding is\n\ |
| 6030 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6031 | |
| 6032 | static PyObject * |
| 6033 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 6034 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6035 | Py_ssize_t marg, left; |
| 6036 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6037 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6038 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6039 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6040 | return NULL; |
| 6041 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6042 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6043 | Py_INCREF(self); |
| 6044 | return (PyObject*) self; |
| 6045 | } |
| 6046 | |
| 6047 | marg = width - self->length; |
| 6048 | left = marg / 2 + (marg & width & 1); |
| 6049 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6050 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6051 | } |
| 6052 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6053 | #if 0 |
| 6054 | |
| 6055 | /* This code should go into some future Unicode collation support |
| 6056 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 6057 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6058 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6059 | /* speedy UTF-16 code point order comparison */ |
| 6060 | /* gleaned from: */ |
| 6061 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 6062 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6063 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6064 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6065 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6066 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 6067 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6068 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6069 | }; |
| 6070 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6071 | static int |
| 6072 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6073 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6074 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6075 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6076 | Py_UNICODE *s1 = str1->str; |
| 6077 | Py_UNICODE *s2 = str2->str; |
| 6078 | |
| 6079 | len1 = str1->length; |
| 6080 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6081 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6082 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6083 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6084 | |
| 6085 | c1 = *s1++; |
| 6086 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6087 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6088 | if (c1 > (1<<11) * 26) |
| 6089 | c1 += utf16Fixup[c1>>11]; |
| 6090 | if (c2 > (1<<11) * 26) |
| 6091 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6092 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6093 | |
| 6094 | if (c1 != c2) |
| 6095 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6096 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6097 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6098 | } |
| 6099 | |
| 6100 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6101 | } |
| 6102 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6103 | #else |
| 6104 | |
| 6105 | static int |
| 6106 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6107 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6108 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6109 | |
| 6110 | Py_UNICODE *s1 = str1->str; |
| 6111 | Py_UNICODE *s2 = str2->str; |
| 6112 | |
| 6113 | len1 = str1->length; |
| 6114 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6115 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6116 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6117 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6118 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6119 | c1 = *s1++; |
| 6120 | c2 = *s2++; |
| 6121 | |
| 6122 | if (c1 != c2) |
| 6123 | return (c1 < c2) ? -1 : 1; |
| 6124 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6125 | len1--; len2--; |
| 6126 | } |
| 6127 | |
| 6128 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6129 | } |
| 6130 | |
| 6131 | #endif |
| 6132 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6133 | int PyUnicode_Compare(PyObject *left, |
| 6134 | PyObject *right) |
| 6135 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6136 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) |
| 6137 | return unicode_compare((PyUnicodeObject *)left, |
| 6138 | (PyUnicodeObject *)right); |
| 6139 | if ((PyString_Check(left) && PyUnicode_Check(right)) || |
| 6140 | (PyUnicode_Check(left) && PyString_Check(right))) { |
| 6141 | if (PyUnicode_Check(left)) |
| 6142 | left = _PyUnicode_AsDefaultEncodedString(left, NULL); |
| 6143 | if (PyUnicode_Check(right)) |
| 6144 | right = _PyUnicode_AsDefaultEncodedString(right, NULL); |
| 6145 | assert(PyString_Check(left)); |
| 6146 | assert(PyString_Check(right)); |
| 6147 | return PyObject_Compare(left, right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6148 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6149 | PyErr_Format(PyExc_TypeError, |
| 6150 | "Can't compare %.100s and %.100s", |
| 6151 | left->ob_type->tp_name, |
| 6152 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6153 | return -1; |
| 6154 | } |
| 6155 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6156 | int |
| 6157 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 6158 | { |
| 6159 | int i; |
| 6160 | Py_UNICODE *id; |
| 6161 | assert(PyUnicode_Check(uni)); |
| 6162 | id = PyUnicode_AS_UNICODE(uni); |
| 6163 | /* Compare Unicode string and source character set string */ |
| 6164 | for (i = 0; id[i] && str[i]; i++) |
| 6165 | if (id[i] != str[i]) |
| 6166 | return ((int)id[i] < (int)str[i]) ? -1 : 1; |
| 6167 | if (id[i]) |
| 6168 | return 1; /* uni is longer */ |
| 6169 | if (str[i]) |
| 6170 | return -1; /* str is longer */ |
| 6171 | return 0; |
| 6172 | } |
| 6173 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6174 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 6175 | PyObject *right, |
| 6176 | int op) |
| 6177 | { |
| 6178 | int result; |
| 6179 | |
| 6180 | result = PyUnicode_Compare(left, right); |
| 6181 | if (result == -1 && PyErr_Occurred()) |
| 6182 | goto onError; |
| 6183 | |
| 6184 | /* Convert the return value to a Boolean */ |
| 6185 | switch (op) { |
| 6186 | case Py_EQ: |
| 6187 | result = (result == 0); |
| 6188 | break; |
| 6189 | case Py_NE: |
| 6190 | result = (result != 0); |
| 6191 | break; |
| 6192 | case Py_LE: |
| 6193 | result = (result <= 0); |
| 6194 | break; |
| 6195 | case Py_GE: |
| 6196 | result = (result >= 0); |
| 6197 | break; |
| 6198 | case Py_LT: |
| 6199 | result = (result == -1); |
| 6200 | break; |
| 6201 | case Py_GT: |
| 6202 | result = (result == 1); |
| 6203 | break; |
| 6204 | } |
| 6205 | return PyBool_FromLong(result); |
| 6206 | |
| 6207 | onError: |
| 6208 | |
| 6209 | /* Standard case |
| 6210 | |
| 6211 | Type errors mean that PyUnicode_FromObject() could not convert |
| 6212 | one of the arguments (usually the right hand side) to Unicode, |
| 6213 | ie. we can't handle the comparison request. However, it is |
| 6214 | possible that the other object knows a comparison method, which |
| 6215 | is why we return Py_NotImplemented to give the other object a |
| 6216 | chance. |
| 6217 | |
| 6218 | */ |
| 6219 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 6220 | PyErr_Clear(); |
| 6221 | Py_INCREF(Py_NotImplemented); |
| 6222 | return Py_NotImplemented; |
| 6223 | } |
| 6224 | if (op != Py_EQ && op != Py_NE) |
| 6225 | return NULL; |
| 6226 | |
| 6227 | /* Equality comparison. |
| 6228 | |
| 6229 | This is a special case: we silence any PyExc_UnicodeDecodeError |
| 6230 | and instead turn it into a PyErr_UnicodeWarning. |
| 6231 | |
| 6232 | */ |
| 6233 | if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) |
| 6234 | return NULL; |
| 6235 | PyErr_Clear(); |
Skip Montanaro | 46fc337 | 2007-08-12 11:44:53 +0000 | [diff] [blame] | 6236 | if (PyErr_WarnEx(PyExc_UnicodeWarning, |
| 6237 | (op == Py_EQ) ? |
| 6238 | "Unicode equal comparison " |
| 6239 | "failed to convert both arguments to Unicode - " |
| 6240 | "interpreting them as being unequal" |
| 6241 | : |
| 6242 | "Unicode unequal comparison " |
| 6243 | "failed to convert both arguments to Unicode - " |
| 6244 | "interpreting them as being unequal", |
| 6245 | 1) < 0) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6246 | return NULL; |
| 6247 | result = (op == Py_NE); |
| 6248 | return PyBool_FromLong(result); |
| 6249 | } |
| 6250 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6251 | int PyUnicode_Contains(PyObject *container, |
| 6252 | PyObject *element) |
| 6253 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6254 | PyObject *str, *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6255 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6256 | |
| 6257 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6258 | sub = PyUnicode_FromObject(element); |
| 6259 | if (!sub) { |
Walter Dörwald | 26e0f51 | 2007-06-12 16:51:31 +0000 | [diff] [blame] | 6260 | PyErr_Format(PyExc_TypeError, |
| 6261 | "'in <string>' requires string as left operand, not %s", |
| 6262 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6263 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6264 | } |
| 6265 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6266 | str = PyUnicode_FromObject(container); |
| 6267 | if (!str) { |
| 6268 | Py_DECREF(sub); |
| 6269 | return -1; |
| 6270 | } |
| 6271 | |
| 6272 | result = stringlib_contains_obj(str, sub); |
| 6273 | |
| 6274 | Py_DECREF(str); |
| 6275 | Py_DECREF(sub); |
| 6276 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6277 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6278 | } |
| 6279 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6280 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 6281 | |
| 6282 | PyObject *PyUnicode_Concat(PyObject *left, |
| 6283 | PyObject *right) |
| 6284 | { |
| 6285 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 6286 | |
Guido van Rossum | 84d79dd | 2007-04-13 02:23:57 +0000 | [diff] [blame] | 6287 | if (PyBytes_Check(left) || PyBytes_Check(right)) |
| 6288 | return PyBytes_Concat(left, right); |
| 6289 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6290 | /* Coerce the two arguments */ |
| 6291 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 6292 | if (u == NULL) |
| 6293 | goto onError; |
| 6294 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 6295 | if (v == NULL) |
| 6296 | goto onError; |
| 6297 | |
| 6298 | /* Shortcuts */ |
| 6299 | if (v == unicode_empty) { |
| 6300 | Py_DECREF(v); |
| 6301 | return (PyObject *)u; |
| 6302 | } |
| 6303 | if (u == unicode_empty) { |
| 6304 | Py_DECREF(u); |
| 6305 | return (PyObject *)v; |
| 6306 | } |
| 6307 | |
| 6308 | /* Concat the two Unicode strings */ |
| 6309 | w = _PyUnicode_New(u->length + v->length); |
| 6310 | if (w == NULL) |
| 6311 | goto onError; |
| 6312 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 6313 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 6314 | |
| 6315 | Py_DECREF(u); |
| 6316 | Py_DECREF(v); |
| 6317 | return (PyObject *)w; |
| 6318 | |
| 6319 | onError: |
| 6320 | Py_XDECREF(u); |
| 6321 | Py_XDECREF(v); |
| 6322 | return NULL; |
| 6323 | } |
| 6324 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6325 | void |
| 6326 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 6327 | { |
| 6328 | PyObject *new; |
| 6329 | if (*pleft == NULL) |
| 6330 | return; |
| 6331 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 6332 | Py_DECREF(*pleft); |
| 6333 | *pleft = NULL; |
| 6334 | return; |
| 6335 | } |
| 6336 | new = PyUnicode_Concat(*pleft, right); |
| 6337 | Py_DECREF(*pleft); |
| 6338 | *pleft = new; |
| 6339 | } |
| 6340 | |
| 6341 | void |
| 6342 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 6343 | { |
| 6344 | PyUnicode_Append(pleft, right); |
| 6345 | Py_XDECREF(right); |
| 6346 | } |
| 6347 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6348 | PyDoc_STRVAR(count__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6349 | "S.count(sub[, start[, end]]) -> int\n\ |
| 6350 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6351 | Return the number of non-overlapping occurrences of substring sub in\n\ |
| 6352 | 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] | 6353 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6354 | |
| 6355 | static PyObject * |
| 6356 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 6357 | { |
| 6358 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6359 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6360 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6361 | PyObject *result; |
| 6362 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6363 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 6364 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6365 | return NULL; |
| 6366 | |
| 6367 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6368 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6369 | if (substring == NULL) |
| 6370 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6371 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6372 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6373 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6374 | result = PyInt_FromSsize_t( |
| 6375 | stringlib_count(self->str + start, end - start, |
| 6376 | substring->str, substring->length) |
| 6377 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6378 | |
| 6379 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6380 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6381 | return result; |
| 6382 | } |
| 6383 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6384 | PyDoc_STRVAR(encode__doc__, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6385 | "S.encode([encoding[,errors]]) -> string or unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6386 | \n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6387 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 6388 | 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] | 6389 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6390 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 6391 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 6392 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6393 | |
| 6394 | static PyObject * |
| 6395 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 6396 | { |
| 6397 | char *encoding = NULL; |
| 6398 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6399 | PyObject *v; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 6400 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6401 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 6402 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6403 | v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6404 | if (v == NULL) |
| 6405 | goto onError; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 6406 | if (!PyBytes_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6407 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 6408 | "encoder did not return a bytes object " |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6409 | "(type=%.400s)", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 6410 | Py_Type(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6411 | Py_DECREF(v); |
| 6412 | return NULL; |
| 6413 | } |
| 6414 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6415 | |
| 6416 | onError: |
| 6417 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6418 | } |
| 6419 | |
| 6420 | PyDoc_STRVAR(decode__doc__, |
| 6421 | "S.decode([encoding[,errors]]) -> string or unicode\n\ |
| 6422 | \n\ |
| 6423 | Decodes S using the codec registered for encoding. encoding defaults\n\ |
| 6424 | to the default encoding. errors may be given to set a different error\n\ |
| 6425 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 6426 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 6427 | as well as any other name registerd with codecs.register_error that is\n\ |
| 6428 | able to handle UnicodeDecodeErrors."); |
| 6429 | |
| 6430 | static PyObject * |
Marc-André Lemburg | 126b44c | 2004-07-10 12:04:20 +0000 | [diff] [blame] | 6431 | unicode_decode(PyUnicodeObject *self, PyObject *args) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6432 | { |
Guido van Rossum | a74184e | 2007-08-29 04:05:57 +0000 | [diff] [blame] | 6433 | PyErr_Format(PyExc_TypeError, "decoding str is not supported"); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6434 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6435 | } |
| 6436 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6437 | PyDoc_STRVAR(expandtabs__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6438 | "S.expandtabs([tabsize]) -> unicode\n\ |
| 6439 | \n\ |
| 6440 | 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] | 6441 | 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] | 6442 | |
| 6443 | static PyObject* |
| 6444 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 6445 | { |
| 6446 | Py_UNICODE *e; |
| 6447 | Py_UNICODE *p; |
| 6448 | Py_UNICODE *q; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6449 | Py_ssize_t i, j, old_j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6450 | PyUnicodeObject *u; |
| 6451 | int tabsize = 8; |
| 6452 | |
| 6453 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 6454 | return NULL; |
| 6455 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6456 | /* First pass: determine size of output string */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6457 | i = j = old_j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6458 | e = self->str + self->length; |
| 6459 | for (p = self->str; p < e; p++) |
| 6460 | if (*p == '\t') { |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6461 | if (tabsize > 0) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6462 | j += tabsize - (j % tabsize); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6463 | if (old_j > j) { |
| 6464 | PyErr_SetString(PyExc_OverflowError, |
| 6465 | "new string is too long"); |
| 6466 | return NULL; |
| 6467 | } |
| 6468 | old_j = j; |
| 6469 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6470 | } |
| 6471 | else { |
| 6472 | j++; |
| 6473 | if (*p == '\n' || *p == '\r') { |
| 6474 | i += j; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6475 | old_j = j = 0; |
| 6476 | if (i < 0) { |
| 6477 | PyErr_SetString(PyExc_OverflowError, |
| 6478 | "new string is too long"); |
| 6479 | return NULL; |
| 6480 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6481 | } |
| 6482 | } |
| 6483 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6484 | if ((i + j) < 0) { |
| 6485 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 6486 | return NULL; |
| 6487 | } |
| 6488 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6489 | /* Second pass: create output string and fill it */ |
| 6490 | u = _PyUnicode_New(i + j); |
| 6491 | if (!u) |
| 6492 | return NULL; |
| 6493 | |
| 6494 | j = 0; |
| 6495 | q = u->str; |
| 6496 | |
| 6497 | for (p = self->str; p < e; p++) |
| 6498 | if (*p == '\t') { |
| 6499 | if (tabsize > 0) { |
| 6500 | i = tabsize - (j % tabsize); |
| 6501 | j += i; |
| 6502 | while (i--) |
| 6503 | *q++ = ' '; |
| 6504 | } |
| 6505 | } |
| 6506 | else { |
| 6507 | j++; |
| 6508 | *q++ = *p; |
| 6509 | if (*p == '\n' || *p == '\r') |
| 6510 | j = 0; |
| 6511 | } |
| 6512 | |
| 6513 | return (PyObject*) u; |
| 6514 | } |
| 6515 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6516 | PyDoc_STRVAR(find__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6517 | "S.find(sub [,start [,end]]) -> int\n\ |
| 6518 | \n\ |
| 6519 | 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] | 6520 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6521 | arguments start and end are interpreted as in slice notation.\n\ |
| 6522 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6523 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6524 | |
| 6525 | static PyObject * |
| 6526 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 6527 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6528 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6529 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6530 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6531 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6532 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6533 | if (!PyArg_ParseTuple(args, "O|O&O&:find", &substring, |
| 6534 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6535 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6536 | substring = PyUnicode_FromObject(substring); |
| 6537 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6538 | return NULL; |
| 6539 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6540 | result = stringlib_find_slice( |
| 6541 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6542 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6543 | start, end |
| 6544 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6545 | |
| 6546 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6547 | |
| 6548 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6549 | } |
| 6550 | |
| 6551 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6552 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6553 | { |
| 6554 | if (index < 0 || index >= self->length) { |
| 6555 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 6556 | return NULL; |
| 6557 | } |
| 6558 | |
| 6559 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 6560 | } |
| 6561 | |
| 6562 | static long |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6563 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6564 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6565 | /* Since Unicode objects compare equal to their UTF-8 string |
| 6566 | counterparts, we hash the UTF-8 string. */ |
| 6567 | PyObject *v = _PyUnicode_AsDefaultEncodedString(self, NULL); |
| 6568 | return PyObject_Hash(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6569 | } |
| 6570 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6571 | PyDoc_STRVAR(index__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6572 | "S.index(sub [,start [,end]]) -> int\n\ |
| 6573 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6574 | 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] | 6575 | |
| 6576 | static PyObject * |
| 6577 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 6578 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6579 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6580 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6581 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6582 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6583 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6584 | if (!PyArg_ParseTuple(args, "O|O&O&:index", &substring, |
| 6585 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6586 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6587 | substring = PyUnicode_FromObject(substring); |
| 6588 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6589 | return NULL; |
| 6590 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6591 | result = stringlib_find_slice( |
| 6592 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6593 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6594 | start, end |
| 6595 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6596 | |
| 6597 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6598 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6599 | if (result < 0) { |
| 6600 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 6601 | return NULL; |
| 6602 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6603 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6604 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6605 | } |
| 6606 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6607 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6608 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6609 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6610 | 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] | 6611 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6612 | |
| 6613 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6614 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6615 | { |
| 6616 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6617 | register const Py_UNICODE *e; |
| 6618 | int cased; |
| 6619 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6620 | /* Shortcut for single character strings */ |
| 6621 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6622 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6623 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6624 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6625 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6626 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6627 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6628 | e = p + PyUnicode_GET_SIZE(self); |
| 6629 | cased = 0; |
| 6630 | for (; p < e; p++) { |
| 6631 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6632 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6633 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6634 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6635 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 6636 | cased = 1; |
| 6637 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6638 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6639 | } |
| 6640 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6641 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6642 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6643 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6644 | 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] | 6645 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6646 | |
| 6647 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6648 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6649 | { |
| 6650 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6651 | register const Py_UNICODE *e; |
| 6652 | int cased; |
| 6653 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6654 | /* Shortcut for single character strings */ |
| 6655 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6656 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6657 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6658 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6659 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6660 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6661 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6662 | e = p + PyUnicode_GET_SIZE(self); |
| 6663 | cased = 0; |
| 6664 | for (; p < e; p++) { |
| 6665 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6666 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6667 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6668 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6669 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 6670 | cased = 1; |
| 6671 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6672 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6673 | } |
| 6674 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6675 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6676 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6677 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6678 | Return True if S is a titlecased string and there is at least one\n\ |
| 6679 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 6680 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 6681 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6682 | |
| 6683 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6684 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6685 | { |
| 6686 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6687 | register const Py_UNICODE *e; |
| 6688 | int cased, previous_is_cased; |
| 6689 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6690 | /* Shortcut for single character strings */ |
| 6691 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6692 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 6693 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6694 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6695 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6696 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6697 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6698 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6699 | e = p + PyUnicode_GET_SIZE(self); |
| 6700 | cased = 0; |
| 6701 | previous_is_cased = 0; |
| 6702 | for (; p < e; p++) { |
| 6703 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6704 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6705 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 6706 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6707 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6708 | previous_is_cased = 1; |
| 6709 | cased = 1; |
| 6710 | } |
| 6711 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 6712 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6713 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6714 | previous_is_cased = 1; |
| 6715 | cased = 1; |
| 6716 | } |
| 6717 | else |
| 6718 | previous_is_cased = 0; |
| 6719 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6720 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6721 | } |
| 6722 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6723 | PyDoc_STRVAR(isspace__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6724 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6725 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6726 | Return True if all characters in S are whitespace\n\ |
| 6727 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6728 | |
| 6729 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6730 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6731 | { |
| 6732 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6733 | register const Py_UNICODE *e; |
| 6734 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6735 | /* Shortcut for single character strings */ |
| 6736 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6737 | Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6738 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6739 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6740 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6741 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6742 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6743 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6744 | e = p + PyUnicode_GET_SIZE(self); |
| 6745 | for (; p < e; p++) { |
| 6746 | if (!Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6747 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6748 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6749 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6750 | } |
| 6751 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6752 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6753 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6754 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6755 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6756 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6757 | |
| 6758 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6759 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6760 | { |
| 6761 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6762 | register const Py_UNICODE *e; |
| 6763 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6764 | /* Shortcut for single character strings */ |
| 6765 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6766 | Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6767 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6768 | |
| 6769 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6770 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6771 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6772 | |
| 6773 | e = p + PyUnicode_GET_SIZE(self); |
| 6774 | for (; p < e; p++) { |
| 6775 | if (!Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6776 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6777 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6778 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6779 | } |
| 6780 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6781 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6782 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6783 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6784 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6785 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6786 | |
| 6787 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6788 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6789 | { |
| 6790 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6791 | register const Py_UNICODE *e; |
| 6792 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6793 | /* Shortcut for single character strings */ |
| 6794 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6795 | Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6796 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6797 | |
| 6798 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6799 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6800 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6801 | |
| 6802 | e = p + PyUnicode_GET_SIZE(self); |
| 6803 | for (; p < e; p++) { |
| 6804 | if (!Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6805 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6806 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6807 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6808 | } |
| 6809 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6810 | PyDoc_STRVAR(isdecimal__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6811 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6812 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6813 | 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] | 6814 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6815 | |
| 6816 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6817 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6818 | { |
| 6819 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6820 | register const Py_UNICODE *e; |
| 6821 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6822 | /* Shortcut for single character strings */ |
| 6823 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6824 | Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6825 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6826 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6827 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6828 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6829 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6830 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6831 | e = p + PyUnicode_GET_SIZE(self); |
| 6832 | for (; p < e; p++) { |
| 6833 | if (!Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6834 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6835 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6836 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6837 | } |
| 6838 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6839 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6840 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6841 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6842 | Return True if all characters in S are digits\n\ |
| 6843 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6844 | |
| 6845 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6846 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6847 | { |
| 6848 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6849 | register const Py_UNICODE *e; |
| 6850 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6851 | /* Shortcut for single character strings */ |
| 6852 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6853 | Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6854 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6855 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6856 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6857 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6858 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6859 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6860 | e = p + PyUnicode_GET_SIZE(self); |
| 6861 | for (; p < e; p++) { |
| 6862 | if (!Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6863 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6864 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6865 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6866 | } |
| 6867 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6868 | PyDoc_STRVAR(isnumeric__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6869 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6870 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6871 | 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] | 6872 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6873 | |
| 6874 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6875 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6876 | { |
| 6877 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6878 | register const Py_UNICODE *e; |
| 6879 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6880 | /* Shortcut for single character strings */ |
| 6881 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6882 | Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6883 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6884 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6885 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6886 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6887 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6888 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6889 | e = p + PyUnicode_GET_SIZE(self); |
| 6890 | for (; p < e; p++) { |
| 6891 | if (!Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6892 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6893 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6894 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6895 | } |
| 6896 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 6897 | int |
| 6898 | PyUnicode_IsIdentifier(PyObject *self) |
| 6899 | { |
| 6900 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE((PyUnicodeObject*)self); |
| 6901 | register const Py_UNICODE *e; |
| 6902 | |
| 6903 | /* Special case for empty strings */ |
| 6904 | if (PyUnicode_GET_SIZE(self) == 0) |
| 6905 | return 0; |
| 6906 | |
| 6907 | /* PEP 3131 says that the first character must be in |
| 6908 | XID_Start and subsequent characters in XID_Continue, |
| 6909 | and for the ASCII range, the 2.x rules apply (i.e |
| 6910 | start with letters and underscore, continue with |
| 6911 | letters, digits, underscore). However, given the current |
| 6912 | definition of XID_Start and XID_Continue, it is sufficient |
| 6913 | to check just for these, except that _ must be allowed |
| 6914 | as starting an identifier. */ |
| 6915 | if (!_PyUnicode_IsXidStart(*p) && *p != 0x5F /* LOW LINE */) |
| 6916 | return 0; |
| 6917 | |
| 6918 | e = p + PyUnicode_GET_SIZE(self); |
| 6919 | for (p++; p < e; p++) { |
| 6920 | if (!_PyUnicode_IsXidContinue(*p)) |
| 6921 | return 0; |
| 6922 | } |
| 6923 | return 1; |
| 6924 | } |
| 6925 | |
| 6926 | PyDoc_STRVAR(isidentifier__doc__, |
| 6927 | "S.isidentifier() -> bool\n\ |
| 6928 | \n\ |
| 6929 | Return True if S is a valid identifier according\n\ |
| 6930 | to the language definition."); |
| 6931 | |
| 6932 | static PyObject* |
| 6933 | unicode_isidentifier(PyObject *self) |
| 6934 | { |
| 6935 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 6936 | } |
| 6937 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6938 | PyDoc_STRVAR(join__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6939 | "S.join(sequence) -> unicode\n\ |
| 6940 | \n\ |
| 6941 | 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] | 6942 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6943 | |
| 6944 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6945 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6946 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6947 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6948 | } |
| 6949 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6950 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6951 | unicode_length(PyUnicodeObject *self) |
| 6952 | { |
| 6953 | return self->length; |
| 6954 | } |
| 6955 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6956 | PyDoc_STRVAR(ljust__doc__, |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 6957 | "S.ljust(width[, fillchar]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6958 | \n\ |
| 6959 | 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] | 6960 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6961 | |
| 6962 | static PyObject * |
| 6963 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 6964 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6965 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6966 | Py_UNICODE fillchar = ' '; |
| 6967 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6968 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6969 | return NULL; |
| 6970 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6971 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6972 | Py_INCREF(self); |
| 6973 | return (PyObject*) self; |
| 6974 | } |
| 6975 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6976 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6977 | } |
| 6978 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6979 | PyDoc_STRVAR(lower__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6980 | "S.lower() -> unicode\n\ |
| 6981 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6982 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6983 | |
| 6984 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6985 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6986 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6987 | return fixup(self, fixlower); |
| 6988 | } |
| 6989 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6990 | #define LEFTSTRIP 0 |
| 6991 | #define RIGHTSTRIP 1 |
| 6992 | #define BOTHSTRIP 2 |
| 6993 | |
| 6994 | /* Arrays indexed by above */ |
| 6995 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 6996 | |
| 6997 | #define STRIPNAME(i) (stripformat[i]+3) |
| 6998 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6999 | /* externally visible for str.strip(unicode) */ |
| 7000 | PyObject * |
| 7001 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 7002 | { |
| 7003 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7004 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7005 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7006 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 7007 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7008 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7009 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
| 7010 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7011 | i = 0; |
| 7012 | if (striptype != RIGHTSTRIP) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7013 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7014 | i++; |
| 7015 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7016 | } |
| 7017 | |
| 7018 | j = len; |
| 7019 | if (striptype != LEFTSTRIP) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7020 | do { |
| 7021 | j--; |
| 7022 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 7023 | j++; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7024 | } |
| 7025 | |
| 7026 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7027 | Py_INCREF(self); |
| 7028 | return (PyObject*)self; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7029 | } |
| 7030 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7031 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7032 | } |
| 7033 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7034 | |
| 7035 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7036 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7037 | { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7038 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7039 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7040 | |
| 7041 | i = 0; |
| 7042 | if (striptype != RIGHTSTRIP) { |
| 7043 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 7044 | i++; |
| 7045 | } |
| 7046 | } |
| 7047 | |
| 7048 | j = len; |
| 7049 | if (striptype != LEFTSTRIP) { |
| 7050 | do { |
| 7051 | j--; |
| 7052 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 7053 | j++; |
| 7054 | } |
| 7055 | |
| 7056 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 7057 | Py_INCREF(self); |
| 7058 | return (PyObject*)self; |
| 7059 | } |
| 7060 | else |
| 7061 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7062 | } |
| 7063 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7064 | |
| 7065 | static PyObject * |
| 7066 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 7067 | { |
| 7068 | PyObject *sep = NULL; |
| 7069 | |
| 7070 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 7071 | return NULL; |
| 7072 | |
| 7073 | if (sep != NULL && sep != Py_None) { |
| 7074 | if (PyUnicode_Check(sep)) |
| 7075 | return _PyUnicode_XStrip(self, striptype, sep); |
| 7076 | else if (PyString_Check(sep)) { |
| 7077 | PyObject *res; |
| 7078 | sep = PyUnicode_FromObject(sep); |
| 7079 | if (sep==NULL) |
| 7080 | return NULL; |
| 7081 | res = _PyUnicode_XStrip(self, striptype, sep); |
| 7082 | Py_DECREF(sep); |
| 7083 | return res; |
| 7084 | } |
| 7085 | else { |
| 7086 | PyErr_Format(PyExc_TypeError, |
| 7087 | "%s arg must be None, unicode or str", |
| 7088 | STRIPNAME(striptype)); |
| 7089 | return NULL; |
| 7090 | } |
| 7091 | } |
| 7092 | |
| 7093 | return do_strip(self, striptype); |
| 7094 | } |
| 7095 | |
| 7096 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7097 | PyDoc_STRVAR(strip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7098 | "S.strip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7099 | \n\ |
| 7100 | Return a copy of the string S with leading and trailing\n\ |
| 7101 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7102 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7103 | 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] | 7104 | |
| 7105 | static PyObject * |
| 7106 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 7107 | { |
| 7108 | if (PyTuple_GET_SIZE(args) == 0) |
| 7109 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 7110 | else |
| 7111 | return do_argstrip(self, BOTHSTRIP, args); |
| 7112 | } |
| 7113 | |
| 7114 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7115 | PyDoc_STRVAR(lstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7116 | "S.lstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7117 | \n\ |
| 7118 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7119 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7120 | 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] | 7121 | |
| 7122 | static PyObject * |
| 7123 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 7124 | { |
| 7125 | if (PyTuple_GET_SIZE(args) == 0) |
| 7126 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 7127 | else |
| 7128 | return do_argstrip(self, LEFTSTRIP, args); |
| 7129 | } |
| 7130 | |
| 7131 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7132 | PyDoc_STRVAR(rstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7133 | "S.rstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7134 | \n\ |
| 7135 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7136 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7137 | 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] | 7138 | |
| 7139 | static PyObject * |
| 7140 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 7141 | { |
| 7142 | if (PyTuple_GET_SIZE(args) == 0) |
| 7143 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 7144 | else |
| 7145 | return do_argstrip(self, RIGHTSTRIP, args); |
| 7146 | } |
| 7147 | |
| 7148 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7149 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7150 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7151 | { |
| 7152 | PyUnicodeObject *u; |
| 7153 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7154 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7155 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7156 | |
| 7157 | if (len < 0) |
| 7158 | len = 0; |
| 7159 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7160 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7161 | /* no repeat, return original string */ |
| 7162 | Py_INCREF(str); |
| 7163 | return (PyObject*) str; |
| 7164 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7165 | |
| 7166 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 7167 | * needed doesn't overflow size_t |
| 7168 | */ |
| 7169 | nchars = len * str->length; |
| 7170 | if (len && nchars / len != str->length) { |
| 7171 | PyErr_SetString(PyExc_OverflowError, |
| 7172 | "repeated string is too long"); |
| 7173 | return NULL; |
| 7174 | } |
| 7175 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 7176 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 7177 | PyErr_SetString(PyExc_OverflowError, |
| 7178 | "repeated string is too long"); |
| 7179 | return NULL; |
| 7180 | } |
| 7181 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7182 | if (!u) |
| 7183 | return NULL; |
| 7184 | |
| 7185 | p = u->str; |
| 7186 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7187 | if (str->length == 1 && len > 0) { |
| 7188 | Py_UNICODE_FILL(p, str->str[0], len); |
| 7189 | } else { |
| 7190 | Py_ssize_t done = 0; /* number of characters copied this far */ |
| 7191 | if (done < nchars) { |
| 7192 | Py_UNICODE_COPY(p, str->str, str->length); |
| 7193 | done = str->length; |
| 7194 | } |
| 7195 | while (done < nchars) { |
| 7196 | int n = (done <= nchars-done) ? done : nchars-done; |
| 7197 | Py_UNICODE_COPY(p+done, p, n); |
| 7198 | done += n; |
| 7199 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7200 | } |
| 7201 | |
| 7202 | return (PyObject*) u; |
| 7203 | } |
| 7204 | |
| 7205 | PyObject *PyUnicode_Replace(PyObject *obj, |
| 7206 | PyObject *subobj, |
| 7207 | PyObject *replobj, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7208 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7209 | { |
| 7210 | PyObject *self; |
| 7211 | PyObject *str1; |
| 7212 | PyObject *str2; |
| 7213 | PyObject *result; |
| 7214 | |
| 7215 | self = PyUnicode_FromObject(obj); |
| 7216 | if (self == NULL) |
| 7217 | return NULL; |
| 7218 | str1 = PyUnicode_FromObject(subobj); |
| 7219 | if (str1 == NULL) { |
| 7220 | Py_DECREF(self); |
| 7221 | return NULL; |
| 7222 | } |
| 7223 | str2 = PyUnicode_FromObject(replobj); |
| 7224 | if (str2 == NULL) { |
| 7225 | Py_DECREF(self); |
| 7226 | Py_DECREF(str1); |
| 7227 | return NULL; |
| 7228 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7229 | result = replace((PyUnicodeObject *)self, |
| 7230 | (PyUnicodeObject *)str1, |
| 7231 | (PyUnicodeObject *)str2, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7232 | maxcount); |
| 7233 | Py_DECREF(self); |
| 7234 | Py_DECREF(str1); |
| 7235 | Py_DECREF(str2); |
| 7236 | return result; |
| 7237 | } |
| 7238 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7239 | PyDoc_STRVAR(replace__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7240 | "S.replace (old, new[, maxsplit]) -> unicode\n\ |
| 7241 | \n\ |
| 7242 | Return a copy of S with all occurrences of substring\n\ |
| 7243 | 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] | 7244 | given, only the first maxsplit occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7245 | |
| 7246 | static PyObject* |
| 7247 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 7248 | { |
| 7249 | PyUnicodeObject *str1; |
| 7250 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7251 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7252 | PyObject *result; |
| 7253 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7254 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7255 | return NULL; |
| 7256 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 7257 | if (str1 == NULL) |
| 7258 | return NULL; |
| 7259 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7260 | if (str2 == NULL) { |
| 7261 | Py_DECREF(str1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7262 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7263 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7264 | |
| 7265 | result = replace(self, str1, str2, maxcount); |
| 7266 | |
| 7267 | Py_DECREF(str1); |
| 7268 | Py_DECREF(str2); |
| 7269 | return result; |
| 7270 | } |
| 7271 | |
| 7272 | static |
| 7273 | PyObject *unicode_repr(PyObject *unicode) |
| 7274 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7275 | PyObject *repr; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7276 | Py_UNICODE *p; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7277 | Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); |
| 7278 | Py_ssize_t size = PyUnicode_GET_SIZE(unicode); |
| 7279 | |
| 7280 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 7281 | better to choose a different scheme. Perhaps scan the |
| 7282 | first N-chars of the string and allocate based on that size. |
| 7283 | */ |
| 7284 | /* Initial allocation is based on the longest-possible unichr |
| 7285 | escape. |
| 7286 | |
| 7287 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 7288 | unichr, so in this case it's the longest unichr escape. In |
| 7289 | narrow (UTF-16) builds this is five chars per source unichr |
| 7290 | since there are two unichrs in the surrogate pair, so in narrow |
| 7291 | (UTF-16) builds it's not the longest unichr escape. |
| 7292 | |
| 7293 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 7294 | so in the narrow (UTF-16) build case it's the longest unichr |
| 7295 | escape. |
| 7296 | */ |
| 7297 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7298 | repr = PyUnicode_FromUnicode(NULL, |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7299 | 2 /* quotes */ |
| 7300 | #ifdef Py_UNICODE_WIDE |
| 7301 | + 10*size |
| 7302 | #else |
| 7303 | + 6*size |
| 7304 | #endif |
| 7305 | + 1); |
| 7306 | if (repr == NULL) |
| 7307 | return NULL; |
| 7308 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7309 | p = PyUnicode_AS_UNICODE(repr); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7310 | |
| 7311 | /* Add quote */ |
| 7312 | *p++ = (findchar(s, size, '\'') && |
| 7313 | !findchar(s, size, '"')) ? '"' : '\''; |
| 7314 | while (size-- > 0) { |
| 7315 | Py_UNICODE ch = *s++; |
| 7316 | |
| 7317 | /* Escape quotes and backslashes */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7318 | if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7319 | *p++ = '\\'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7320 | *p++ = ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7321 | continue; |
| 7322 | } |
| 7323 | |
| 7324 | #ifdef Py_UNICODE_WIDE |
| 7325 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 7326 | else if (ch >= 0x10000) { |
| 7327 | *p++ = '\\'; |
| 7328 | *p++ = 'U'; |
| 7329 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 7330 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 7331 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 7332 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 7333 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 7334 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 7335 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 7336 | *p++ = hexdigits[ch & 0x0000000F]; |
| 7337 | continue; |
| 7338 | } |
| 7339 | #else |
| 7340 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 7341 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 7342 | Py_UNICODE ch2; |
| 7343 | Py_UCS4 ucs; |
| 7344 | |
| 7345 | ch2 = *s++; |
| 7346 | size--; |
| 7347 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 7348 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 7349 | *p++ = '\\'; |
| 7350 | *p++ = 'U'; |
| 7351 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 7352 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 7353 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 7354 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 7355 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 7356 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 7357 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 7358 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 7359 | continue; |
| 7360 | } |
| 7361 | /* Fall through: isolated surrogates are copied as-is */ |
| 7362 | s--; |
| 7363 | size++; |
| 7364 | } |
| 7365 | #endif |
| 7366 | |
| 7367 | /* Map 16-bit characters to '\uxxxx' */ |
| 7368 | if (ch >= 256) { |
| 7369 | *p++ = '\\'; |
| 7370 | *p++ = 'u'; |
| 7371 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 7372 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 7373 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7374 | *p++ = hexdigits[ch & 0x000F]; |
| 7375 | } |
| 7376 | |
| 7377 | /* Map special whitespace to '\t', \n', '\r' */ |
| 7378 | else if (ch == '\t') { |
| 7379 | *p++ = '\\'; |
| 7380 | *p++ = 't'; |
| 7381 | } |
| 7382 | else if (ch == '\n') { |
| 7383 | *p++ = '\\'; |
| 7384 | *p++ = 'n'; |
| 7385 | } |
| 7386 | else if (ch == '\r') { |
| 7387 | *p++ = '\\'; |
| 7388 | *p++ = 'r'; |
| 7389 | } |
| 7390 | |
| 7391 | /* Map non-printable US ASCII to '\xhh' */ |
| 7392 | else if (ch < ' ' || ch >= 0x7F) { |
| 7393 | *p++ = '\\'; |
| 7394 | *p++ = 'x'; |
| 7395 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7396 | *p++ = hexdigits[ch & 0x000F]; |
| 7397 | } |
| 7398 | |
| 7399 | /* Copy everything else as-is */ |
| 7400 | else |
| 7401 | *p++ = (char) ch; |
| 7402 | } |
| 7403 | /* Add quote */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7404 | *p++ = PyUnicode_AS_UNICODE(repr)[0]; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7405 | |
| 7406 | *p = '\0'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7407 | _PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7408 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7409 | } |
| 7410 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7411 | PyDoc_STRVAR(rfind__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7412 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 7413 | \n\ |
| 7414 | 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] | 7415 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7416 | arguments start and end are interpreted as in slice notation.\n\ |
| 7417 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7418 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7419 | |
| 7420 | static PyObject * |
| 7421 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 7422 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7423 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7424 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7425 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7426 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7427 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7428 | if (!PyArg_ParseTuple(args, "O|O&O&:rfind", &substring, |
| 7429 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7430 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7431 | substring = PyUnicode_FromObject(substring); |
| 7432 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7433 | return NULL; |
| 7434 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7435 | result = stringlib_rfind_slice( |
| 7436 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7437 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7438 | start, end |
| 7439 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7440 | |
| 7441 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7442 | |
| 7443 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7444 | } |
| 7445 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7446 | PyDoc_STRVAR(rindex__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7447 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 7448 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7449 | 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] | 7450 | |
| 7451 | static PyObject * |
| 7452 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 7453 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7454 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7455 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7456 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7457 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7458 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7459 | if (!PyArg_ParseTuple(args, "O|O&O&:rindex", &substring, |
| 7460 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7461 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7462 | substring = PyUnicode_FromObject(substring); |
| 7463 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7464 | return NULL; |
| 7465 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7466 | result = stringlib_rfind_slice( |
| 7467 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7468 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7469 | start, end |
| 7470 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7471 | |
| 7472 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7473 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7474 | if (result < 0) { |
| 7475 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7476 | return NULL; |
| 7477 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7478 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7479 | } |
| 7480 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7481 | PyDoc_STRVAR(rjust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7482 | "S.rjust(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7483 | \n\ |
| 7484 | 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] | 7485 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7486 | |
| 7487 | static PyObject * |
| 7488 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 7489 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7490 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7491 | Py_UNICODE fillchar = ' '; |
| 7492 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7493 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7494 | return NULL; |
| 7495 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7496 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7497 | Py_INCREF(self); |
| 7498 | return (PyObject*) self; |
| 7499 | } |
| 7500 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7501 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7502 | } |
| 7503 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7504 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7505 | unicode_slice(PyUnicodeObject *self, Py_ssize_t start, Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7506 | { |
| 7507 | /* standard clamping */ |
| 7508 | if (start < 0) |
| 7509 | start = 0; |
| 7510 | if (end < 0) |
| 7511 | end = 0; |
| 7512 | if (end > self->length) |
| 7513 | end = self->length; |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7514 | if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7515 | /* full slice, return original string */ |
| 7516 | Py_INCREF(self); |
| 7517 | return (PyObject*) self; |
| 7518 | } |
| 7519 | if (start > end) |
| 7520 | start = end; |
| 7521 | /* copy slice */ |
| 7522 | return (PyObject*) PyUnicode_FromUnicode(self->str + start, |
| 7523 | end - start); |
| 7524 | } |
| 7525 | |
| 7526 | PyObject *PyUnicode_Split(PyObject *s, |
| 7527 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7528 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7529 | { |
| 7530 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7531 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7532 | s = PyUnicode_FromObject(s); |
| 7533 | if (s == NULL) |
| 7534 | return NULL; |
| 7535 | if (sep != NULL) { |
| 7536 | sep = PyUnicode_FromObject(sep); |
| 7537 | if (sep == NULL) { |
| 7538 | Py_DECREF(s); |
| 7539 | return NULL; |
| 7540 | } |
| 7541 | } |
| 7542 | |
| 7543 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7544 | |
| 7545 | Py_DECREF(s); |
| 7546 | Py_XDECREF(sep); |
| 7547 | return result; |
| 7548 | } |
| 7549 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7550 | PyDoc_STRVAR(split__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7551 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 7552 | \n\ |
| 7553 | Return a list of the words in S, using sep as the\n\ |
| 7554 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Thomas Heller | ca0d2cb | 2004-09-15 11:41:32 +0000 | [diff] [blame] | 7555 | 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] | 7556 | any whitespace string is a separator."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7557 | |
| 7558 | static PyObject* |
| 7559 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 7560 | { |
| 7561 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7562 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7563 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7564 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7565 | return NULL; |
| 7566 | |
| 7567 | if (substring == Py_None) |
| 7568 | return split(self, NULL, maxcount); |
| 7569 | else if (PyUnicode_Check(substring)) |
| 7570 | return split(self, (PyUnicodeObject *)substring, maxcount); |
| 7571 | else |
| 7572 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
| 7573 | } |
| 7574 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7575 | PyObject * |
| 7576 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 7577 | { |
| 7578 | PyObject* str_obj; |
| 7579 | PyObject* sep_obj; |
| 7580 | PyObject* out; |
| 7581 | |
| 7582 | str_obj = PyUnicode_FromObject(str_in); |
| 7583 | if (!str_obj) |
| 7584 | return NULL; |
| 7585 | sep_obj = PyUnicode_FromObject(sep_in); |
| 7586 | if (!sep_obj) { |
| 7587 | Py_DECREF(str_obj); |
| 7588 | return NULL; |
| 7589 | } |
| 7590 | |
| 7591 | out = stringlib_partition( |
| 7592 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7593 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7594 | ); |
| 7595 | |
| 7596 | Py_DECREF(sep_obj); |
| 7597 | Py_DECREF(str_obj); |
| 7598 | |
| 7599 | return out; |
| 7600 | } |
| 7601 | |
| 7602 | |
| 7603 | PyObject * |
| 7604 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 7605 | { |
| 7606 | PyObject* str_obj; |
| 7607 | PyObject* sep_obj; |
| 7608 | PyObject* out; |
| 7609 | |
| 7610 | str_obj = PyUnicode_FromObject(str_in); |
| 7611 | if (!str_obj) |
| 7612 | return NULL; |
| 7613 | sep_obj = PyUnicode_FromObject(sep_in); |
| 7614 | if (!sep_obj) { |
| 7615 | Py_DECREF(str_obj); |
| 7616 | return NULL; |
| 7617 | } |
| 7618 | |
| 7619 | out = stringlib_rpartition( |
| 7620 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7621 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7622 | ); |
| 7623 | |
| 7624 | Py_DECREF(sep_obj); |
| 7625 | Py_DECREF(str_obj); |
| 7626 | |
| 7627 | return out; |
| 7628 | } |
| 7629 | |
| 7630 | PyDoc_STRVAR(partition__doc__, |
| 7631 | "S.partition(sep) -> (head, sep, tail)\n\ |
| 7632 | \n\ |
| 7633 | Searches for the separator sep in S, and returns the part before it,\n\ |
| 7634 | the separator itself, and the part after it. If the separator is not\n\ |
| 7635 | found, returns S and two empty strings."); |
| 7636 | |
| 7637 | static PyObject* |
| 7638 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 7639 | { |
| 7640 | return PyUnicode_Partition((PyObject *)self, separator); |
| 7641 | } |
| 7642 | |
| 7643 | PyDoc_STRVAR(rpartition__doc__, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7644 | "S.rpartition(sep) -> (tail, sep, head)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7645 | \n\ |
| 7646 | Searches for the separator sep in S, starting at the end of S, and returns\n\ |
| 7647 | 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] | 7648 | separator is not found, returns two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7649 | |
| 7650 | static PyObject* |
| 7651 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 7652 | { |
| 7653 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 7654 | } |
| 7655 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7656 | PyObject *PyUnicode_RSplit(PyObject *s, |
| 7657 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7658 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7659 | { |
| 7660 | PyObject *result; |
| 7661 | |
| 7662 | s = PyUnicode_FromObject(s); |
| 7663 | if (s == NULL) |
| 7664 | return NULL; |
| 7665 | if (sep != NULL) { |
| 7666 | sep = PyUnicode_FromObject(sep); |
| 7667 | if (sep == NULL) { |
| 7668 | Py_DECREF(s); |
| 7669 | return NULL; |
| 7670 | } |
| 7671 | } |
| 7672 | |
| 7673 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7674 | |
| 7675 | Py_DECREF(s); |
| 7676 | Py_XDECREF(sep); |
| 7677 | return result; |
| 7678 | } |
| 7679 | |
| 7680 | PyDoc_STRVAR(rsplit__doc__, |
| 7681 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
| 7682 | \n\ |
| 7683 | Return a list of the words in S, using sep as the\n\ |
| 7684 | delimiter string, starting at the end of the string and\n\ |
| 7685 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 7686 | splits are done. If sep is not specified, any whitespace string\n\ |
| 7687 | is a separator."); |
| 7688 | |
| 7689 | static PyObject* |
| 7690 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 7691 | { |
| 7692 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7693 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7694 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7695 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7696 | return NULL; |
| 7697 | |
| 7698 | if (substring == Py_None) |
| 7699 | return rsplit(self, NULL, maxcount); |
| 7700 | else if (PyUnicode_Check(substring)) |
| 7701 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
| 7702 | else |
| 7703 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
| 7704 | } |
| 7705 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7706 | PyDoc_STRVAR(splitlines__doc__, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7707 | "S.splitlines([keepends]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7708 | \n\ |
| 7709 | 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] | 7710 | 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] | 7711 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7712 | |
| 7713 | static PyObject* |
| 7714 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 7715 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7716 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7717 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7718 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7719 | return NULL; |
| 7720 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7721 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7722 | } |
| 7723 | |
| 7724 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 7725 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7726 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 7727 | if (PyUnicode_CheckExact(self)) { |
| 7728 | Py_INCREF(self); |
| 7729 | return self; |
| 7730 | } else |
| 7731 | /* Subtype -- return genuine unicode string with the same value. */ |
| 7732 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), |
| 7733 | PyUnicode_GET_SIZE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7734 | } |
| 7735 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7736 | PyDoc_STRVAR(swapcase__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7737 | "S.swapcase() -> unicode\n\ |
| 7738 | \n\ |
| 7739 | 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] | 7740 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7741 | |
| 7742 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7743 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7744 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7745 | return fixup(self, fixswapcase); |
| 7746 | } |
| 7747 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7748 | PyDoc_STRVAR(translate__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7749 | "S.translate(table) -> unicode\n\ |
| 7750 | \n\ |
| 7751 | Return a copy of the string S, where all characters have been mapped\n\ |
| 7752 | 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] | 7753 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ |
| 7754 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 7755 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7756 | |
| 7757 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7758 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7759 | { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7760 | return PyUnicode_TranslateCharmap(self->str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7761 | self->length, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7762 | table, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7763 | "ignore"); |
| 7764 | } |
| 7765 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7766 | PyDoc_STRVAR(upper__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7767 | "S.upper() -> unicode\n\ |
| 7768 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7769 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7770 | |
| 7771 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7772 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7773 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7774 | return fixup(self, fixupper); |
| 7775 | } |
| 7776 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7777 | PyDoc_STRVAR(zfill__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7778 | "S.zfill(width) -> unicode\n\ |
| 7779 | \n\ |
| 7780 | 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] | 7781 | of the specified width. The string x is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7782 | |
| 7783 | static PyObject * |
| 7784 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 7785 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7786 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7787 | PyUnicodeObject *u; |
| 7788 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7789 | Py_ssize_t width; |
| 7790 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7791 | return NULL; |
| 7792 | |
| 7793 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 7794 | if (PyUnicode_CheckExact(self)) { |
| 7795 | Py_INCREF(self); |
| 7796 | return (PyObject*) self; |
| 7797 | } |
| 7798 | else |
| 7799 | return PyUnicode_FromUnicode( |
| 7800 | PyUnicode_AS_UNICODE(self), |
| 7801 | PyUnicode_GET_SIZE(self) |
| 7802 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7803 | } |
| 7804 | |
| 7805 | fill = width - self->length; |
| 7806 | |
| 7807 | u = pad(self, fill, 0, '0'); |
| 7808 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 7809 | if (u == NULL) |
| 7810 | return NULL; |
| 7811 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7812 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 7813 | /* move sign to beginning of string */ |
| 7814 | u->str[0] = u->str[fill]; |
| 7815 | u->str[fill] = '0'; |
| 7816 | } |
| 7817 | |
| 7818 | return (PyObject*) u; |
| 7819 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7820 | |
| 7821 | #if 0 |
| 7822 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7823 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7824 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7825 | return PyInt_FromLong(unicode_freelist_size); |
| 7826 | } |
| 7827 | #endif |
| 7828 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7829 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7830 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7831 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7832 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 7833 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7834 | With optional end, stop comparing S at that position.\n\ |
| 7835 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7836 | |
| 7837 | static PyObject * |
| 7838 | unicode_startswith(PyUnicodeObject *self, |
| 7839 | PyObject *args) |
| 7840 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7841 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7842 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7843 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7844 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7845 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7846 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7847 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7848 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7849 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7850 | if (PyTuple_Check(subobj)) { |
| 7851 | Py_ssize_t i; |
| 7852 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 7853 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 7854 | PyTuple_GET_ITEM(subobj, i)); |
| 7855 | if (substring == NULL) |
| 7856 | return NULL; |
| 7857 | result = tailmatch(self, substring, start, end, -1); |
| 7858 | Py_DECREF(substring); |
| 7859 | if (result) { |
| 7860 | Py_RETURN_TRUE; |
| 7861 | } |
| 7862 | } |
| 7863 | /* nothing matched */ |
| 7864 | Py_RETURN_FALSE; |
| 7865 | } |
| 7866 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7867 | if (substring == NULL) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7868 | return NULL; |
| 7869 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7870 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7871 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7872 | } |
| 7873 | |
| 7874 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7875 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7876 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7877 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7878 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 7879 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7880 | With optional end, stop comparing S at that position.\n\ |
| 7881 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7882 | |
| 7883 | static PyObject * |
| 7884 | unicode_endswith(PyUnicodeObject *self, |
| 7885 | PyObject *args) |
| 7886 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7887 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7888 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7889 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7890 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7891 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7892 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7893 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 7894 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7895 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7896 | if (PyTuple_Check(subobj)) { |
| 7897 | Py_ssize_t i; |
| 7898 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 7899 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 7900 | PyTuple_GET_ITEM(subobj, i)); |
| 7901 | if (substring == NULL) |
| 7902 | return NULL; |
| 7903 | result = tailmatch(self, substring, start, end, +1); |
| 7904 | Py_DECREF(substring); |
| 7905 | if (result) { |
| 7906 | Py_RETURN_TRUE; |
| 7907 | } |
| 7908 | } |
| 7909 | Py_RETURN_FALSE; |
| 7910 | } |
| 7911 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7912 | if (substring == NULL) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7913 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7914 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7915 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7916 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7917 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7918 | } |
| 7919 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 7920 | #include "stringlib/string_format.h" |
| 7921 | |
| 7922 | PyDoc_STRVAR(format__doc__, |
| 7923 | "S.format(*args, **kwargs) -> unicode\n\ |
| 7924 | \n\ |
| 7925 | "); |
| 7926 | |
| 7927 | static PyObject * |
| 7928 | unicode_format(PyObject *self, PyObject *args, PyObject *kwds) |
| 7929 | { |
| 7930 | /* this calls into stringlib/string_format.h because it can be |
| 7931 | included for either string or unicode. this is needed for |
| 7932 | python 2.6. */ |
| 7933 | return do_string_format(self, args, kwds); |
| 7934 | } |
| 7935 | |
| 7936 | |
| 7937 | PyDoc_STRVAR(p_format__doc__, |
| 7938 | "S.__format__(format_spec) -> unicode\n\ |
| 7939 | \n\ |
| 7940 | "); |
| 7941 | |
| 7942 | static PyObject * |
| 7943 | unicode__format__(PyObject *self, PyObject *args) |
| 7944 | { |
| 7945 | return unicode_unicode__format__(self, args); |
| 7946 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7947 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 7948 | |
| 7949 | static PyObject * |
| 7950 | unicode_getnewargs(PyUnicodeObject *v) |
| 7951 | { |
| 7952 | return Py_BuildValue("(u#)", v->str, v->length); |
| 7953 | } |
| 7954 | |
| 7955 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7956 | static PyMethodDef unicode_methods[] = { |
| 7957 | |
| 7958 | /* Order is according to common usage: often used methods should |
| 7959 | appear first, since lookup is done sequentially. */ |
| 7960 | |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7961 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
| 7962 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 7963 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7964 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7965 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 7966 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 7967 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 7968 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 7969 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 7970 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 7971 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7972 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7973 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 7974 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 7975 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7976 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7977 | {"decode", (PyCFunction) unicode_decode, METH_VARARGS, decode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7978 | /* {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */ |
| 7979 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 7980 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 7981 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7982 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7983 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7984 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7985 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7986 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 7987 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 7988 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 7989 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 7990 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 7991 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 7992 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 7993 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 7994 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 7995 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 7996 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 7997 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 7998 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 7999 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8000 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8001 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8002 | {"format", (PyCFunction) unicode_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
| 8003 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Eric Smith | f6db409 | 2007-08-27 23:52:26 +0000 | [diff] [blame] | 8004 | {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
| 8005 | {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8006 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8007 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8008 | #endif |
| 8009 | |
| 8010 | #if 0 |
| 8011 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8012 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8013 | #endif |
| 8014 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8015 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8016 | {NULL, NULL} |
| 8017 | }; |
| 8018 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8019 | static PyObject * |
| 8020 | unicode_mod(PyObject *v, PyObject *w) |
| 8021 | { |
| 8022 | if (!PyUnicode_Check(v)) { |
| 8023 | Py_INCREF(Py_NotImplemented); |
| 8024 | return Py_NotImplemented; |
| 8025 | } |
| 8026 | return PyUnicode_Format(v, w); |
| 8027 | } |
| 8028 | |
| 8029 | static PyNumberMethods unicode_as_number = { |
| 8030 | 0, /*nb_add*/ |
| 8031 | 0, /*nb_subtract*/ |
| 8032 | 0, /*nb_multiply*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8033 | unicode_mod, /*nb_remainder*/ |
| 8034 | }; |
| 8035 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8036 | static PySequenceMethods unicode_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8037 | (lenfunc) unicode_length, /* sq_length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8038 | PyUnicode_Concat, /* sq_concat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8039 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 8040 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 8041 | (ssizessizeargfunc) unicode_slice, /* sq_slice */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8042 | 0, /* sq_ass_item */ |
| 8043 | 0, /* sq_ass_slice */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8044 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8045 | }; |
| 8046 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8047 | static PyObject* |
| 8048 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 8049 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8050 | if (PyIndex_Check(item)) { |
| 8051 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8052 | if (i == -1 && PyErr_Occurred()) |
| 8053 | return NULL; |
| 8054 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8055 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8056 | return unicode_getitem(self, i); |
| 8057 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8058 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8059 | Py_UNICODE* source_buf; |
| 8060 | Py_UNICODE* result_buf; |
| 8061 | PyObject* result; |
| 8062 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8063 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8064 | &start, &stop, &step, &slicelength) < 0) { |
| 8065 | return NULL; |
| 8066 | } |
| 8067 | |
| 8068 | if (slicelength <= 0) { |
| 8069 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 8070 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 8071 | PyUnicode_CheckExact(self)) { |
| 8072 | Py_INCREF(self); |
| 8073 | return (PyObject *)self; |
| 8074 | } else if (step == 1) { |
| 8075 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8076 | } else { |
| 8077 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8078 | result_buf = (Py_UNICODE *)PyMem_MALLOC(slicelength* |
| 8079 | sizeof(Py_UNICODE)); |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8080 | |
| 8081 | if (result_buf == NULL) |
| 8082 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8083 | |
| 8084 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 8085 | result_buf[i] = source_buf[cur]; |
| 8086 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8087 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8088 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
| 8089 | PyMem_FREE(result_buf); |
| 8090 | return result; |
| 8091 | } |
| 8092 | } else { |
| 8093 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 8094 | return NULL; |
| 8095 | } |
| 8096 | } |
| 8097 | |
| 8098 | static PyMappingMethods unicode_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8099 | (lenfunc)unicode_length, /* mp_length */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8100 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 8101 | (objobjargproc)0, /* mp_ass_subscript */ |
| 8102 | }; |
| 8103 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8104 | |
| 8105 | static int |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 8106 | unicode_buffer_getbuffer(PyUnicodeObject *self, PyBuffer *view, int flags) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8107 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8108 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 8109 | if (flags & PyBUF_CHARACTER) { |
Guido van Rossum | a74184e | 2007-08-29 04:05:57 +0000 | [diff] [blame] | 8110 | PyErr_SetString(PyExc_SystemError, "can't use str as char buffer"); |
| 8111 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8112 | } |
Guido van Rossum | a74184e | 2007-08-29 04:05:57 +0000 | [diff] [blame] | 8113 | return PyBuffer_FillInfo(view, (void *)self->str, |
| 8114 | PyUnicode_GET_DATA_SIZE(self), 1, flags); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8115 | } |
| 8116 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 8117 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8118 | /* Helpers for PyUnicode_Format() */ |
| 8119 | |
| 8120 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8121 | 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] | 8122 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8123 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8124 | if (argidx < arglen) { |
| 8125 | (*p_argidx)++; |
| 8126 | if (arglen < 0) |
| 8127 | return args; |
| 8128 | else |
| 8129 | return PyTuple_GetItem(args, argidx); |
| 8130 | } |
| 8131 | PyErr_SetString(PyExc_TypeError, |
| 8132 | "not enough arguments for format string"); |
| 8133 | return NULL; |
| 8134 | } |
| 8135 | |
| 8136 | #define F_LJUST (1<<0) |
| 8137 | #define F_SIGN (1<<1) |
| 8138 | #define F_BLANK (1<<2) |
| 8139 | #define F_ALT (1<<3) |
| 8140 | #define F_ZERO (1<<4) |
| 8141 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8142 | static Py_ssize_t |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8143 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8144 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8145 | register Py_ssize_t i; |
| 8146 | Py_ssize_t len = strlen(charbuffer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8147 | for (i = len - 1; i >= 0; i--) |
| 8148 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
| 8149 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8150 | return len; |
| 8151 | } |
| 8152 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8153 | static int |
| 8154 | doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) |
| 8155 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8156 | Py_ssize_t result; |
| 8157 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8158 | PyOS_ascii_formatd((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8159 | result = strtounicode(buffer, (char *)buffer); |
| 8160 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8161 | } |
| 8162 | |
| 8163 | static int |
| 8164 | longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) |
| 8165 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8166 | Py_ssize_t result; |
| 8167 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8168 | PyOS_snprintf((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8169 | result = strtounicode(buffer, (char *)buffer); |
| 8170 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8171 | } |
| 8172 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8173 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 8174 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 8175 | formatting is done. */ |
| 8176 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8177 | static int |
| 8178 | formatfloat(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8179 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8180 | int flags, |
| 8181 | int prec, |
| 8182 | int type, |
| 8183 | PyObject *v) |
| 8184 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8185 | /* fmt = '%#.' + `prec` + `type` |
| 8186 | 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] | 8187 | char fmt[20]; |
| 8188 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8189 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8190 | x = PyFloat_AsDouble(v); |
| 8191 | if (x == -1.0 && PyErr_Occurred()) |
| 8192 | return -1; |
| 8193 | if (prec < 0) |
| 8194 | prec = 6; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8195 | if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
| 8196 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8197 | /* Worst case length calc to ensure no buffer overrun: |
| 8198 | |
| 8199 | 'g' formats: |
| 8200 | fmt = %#.<prec>g |
| 8201 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 8202 | for any double rep.) |
| 8203 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 8204 | |
| 8205 | 'f' formats: |
| 8206 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 8207 | len = 1 + 50 + 1 + prec = 52 + prec |
| 8208 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8209 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8210 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8211 | |
| 8212 | */ |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 8213 | if (((type == 'g' || type == 'G') && |
| 8214 | buflen <= (size_t)10 + (size_t)prec) || |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8215 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8216 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8217 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8218 | return -1; |
| 8219 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8220 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 8221 | (flags&F_ALT) ? "#" : "", |
| 8222 | prec, type); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8223 | return doubletounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8224 | } |
| 8225 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8226 | static PyObject* |
| 8227 | formatlong(PyObject *val, int flags, int prec, int type) |
| 8228 | { |
| 8229 | char *buf; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8230 | int len; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8231 | PyObject *str; /* temporary string object. */ |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8232 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8233 | |
| 8234 | str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
| 8235 | if (!str) |
| 8236 | return NULL; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8237 | result = PyUnicode_FromStringAndSize(buf, len); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8238 | Py_DECREF(str); |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8239 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8240 | } |
| 8241 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8242 | static int |
| 8243 | formatint(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8244 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8245 | int flags, |
| 8246 | int prec, |
| 8247 | int type, |
| 8248 | PyObject *v) |
| 8249 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8250 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8251 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 8252 | * + 1 + 1 |
| 8253 | * = 24 |
| 8254 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8255 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8256 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8257 | long x; |
| 8258 | |
| 8259 | x = PyInt_AsLong(v); |
| 8260 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8261 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8262 | if (x < 0 && type == 'u') { |
| 8263 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8264 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8265 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 8266 | sign = "-"; |
| 8267 | else |
| 8268 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8269 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8270 | prec = 1; |
| 8271 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8272 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 8273 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8274 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8275 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8276 | PyErr_SetString(PyExc_OverflowError, |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8277 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8278 | return -1; |
| 8279 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8280 | |
| 8281 | if ((flags & F_ALT) && |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8282 | (type == 'x' || type == 'X' || type == 'o')) { |
| 8283 | /* When converting under %#o, %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8284 | * of issues that cause pain: |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8285 | * - for %#o, we want a different base marker than C |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8286 | * - when 0 is being converted, the C standard leaves off |
| 8287 | * the '0x' or '0X', which is inconsistent with other |
| 8288 | * %#x/%#X conversions and inconsistent with Python's |
| 8289 | * hex() function |
| 8290 | * - there are platforms that violate the standard and |
| 8291 | * convert 0 with the '0x' or '0X' |
| 8292 | * (Metrowerks, Compaq Tru64) |
| 8293 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8294 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8295 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8296 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8297 | * We can achieve the desired consistency by inserting our |
| 8298 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 8299 | * of %#x/%#X. |
| 8300 | * |
| 8301 | * Note that this is the same approach as used in |
| 8302 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8303 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8304 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 8305 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8306 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8307 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8308 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 8309 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8310 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 8311 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8312 | if (sign[0]) |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8313 | return longtounicode(buf, buflen, fmt, -x); |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8314 | else |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8315 | return longtounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8316 | } |
| 8317 | |
| 8318 | static int |
| 8319 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8320 | size_t buflen, |
| 8321 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8322 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8323 | /* presume that the buffer is at least 2 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8324 | if (PyUnicode_Check(v)) { |
| 8325 | if (PyUnicode_GET_SIZE(v) != 1) |
| 8326 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8327 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8328 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8329 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8330 | else if (PyString_Check(v)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8331 | if (PyString_GET_SIZE(v) != 1) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8332 | goto onError; |
| 8333 | buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0]; |
| 8334 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8335 | |
| 8336 | else { |
| 8337 | /* Integer input truncated to a character */ |
| 8338 | long x; |
| 8339 | x = PyInt_AsLong(v); |
| 8340 | if (x == -1 && PyErr_Occurred()) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8341 | goto onError; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8342 | #ifdef Py_UNICODE_WIDE |
| 8343 | if (x < 0 || x > 0x10ffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 8344 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8345 | "%c arg not in range(0x110000) " |
| 8346 | "(wide Python build)"); |
| 8347 | return -1; |
| 8348 | } |
| 8349 | #else |
| 8350 | if (x < 0 || x > 0xffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 8351 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8352 | "%c arg not in range(0x10000) " |
| 8353 | "(narrow Python build)"); |
| 8354 | return -1; |
| 8355 | } |
| 8356 | #endif |
| 8357 | buf[0] = (Py_UNICODE) x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8358 | } |
| 8359 | buf[1] = '\0'; |
| 8360 | return 1; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8361 | |
| 8362 | onError: |
| 8363 | PyErr_SetString(PyExc_TypeError, |
| 8364 | "%c requires int or char"); |
| 8365 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8366 | } |
| 8367 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8368 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 8369 | |
| 8370 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 8371 | chars are formatted. XXX This is a magic number. Each formatting |
| 8372 | routine does bounds checking to ensure no overflow, but a better |
| 8373 | solution may be to malloc a buffer of appropriate size for each |
| 8374 | format. For now, the current solution is sufficient. |
| 8375 | */ |
| 8376 | #define FORMATBUFLEN (size_t)120 |
| 8377 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8378 | PyObject *PyUnicode_Format(PyObject *format, |
| 8379 | PyObject *args) |
| 8380 | { |
| 8381 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8382 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8383 | int args_owned = 0; |
| 8384 | PyUnicodeObject *result = NULL; |
| 8385 | PyObject *dict = NULL; |
| 8386 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8387 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8388 | if (format == NULL || args == NULL) { |
| 8389 | PyErr_BadInternalCall(); |
| 8390 | return NULL; |
| 8391 | } |
| 8392 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8393 | if (uformat == NULL) |
| 8394 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8395 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 8396 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 8397 | |
| 8398 | reslen = rescnt = fmtcnt + 100; |
| 8399 | result = _PyUnicode_New(reslen); |
| 8400 | if (result == NULL) |
| 8401 | goto onError; |
| 8402 | res = PyUnicode_AS_UNICODE(result); |
| 8403 | |
| 8404 | if (PyTuple_Check(args)) { |
| 8405 | arglen = PyTuple_Size(args); |
| 8406 | argidx = 0; |
| 8407 | } |
| 8408 | else { |
| 8409 | arglen = -1; |
| 8410 | argidx = -2; |
| 8411 | } |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 8412 | if (Py_Type(args)->tp_as_mapping && !PyTuple_Check(args) && |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 8413 | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8414 | dict = args; |
| 8415 | |
| 8416 | while (--fmtcnt >= 0) { |
| 8417 | if (*fmt != '%') { |
| 8418 | if (--rescnt < 0) { |
| 8419 | rescnt = fmtcnt + 100; |
| 8420 | reslen += rescnt; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8421 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8422 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8423 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 8424 | --rescnt; |
| 8425 | } |
| 8426 | *res++ = *fmt++; |
| 8427 | } |
| 8428 | else { |
| 8429 | /* Got a format specifier */ |
| 8430 | int flags = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8431 | Py_ssize_t width = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8432 | int prec = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8433 | Py_UNICODE c = '\0'; |
| 8434 | Py_UNICODE fill; |
| 8435 | PyObject *v = NULL; |
| 8436 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8437 | Py_UNICODE *pbuf; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8438 | Py_UNICODE sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8439 | Py_ssize_t len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8440 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8441 | |
| 8442 | fmt++; |
| 8443 | if (*fmt == '(') { |
| 8444 | Py_UNICODE *keystart; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8445 | Py_ssize_t keylen; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8446 | PyObject *key; |
| 8447 | int pcount = 1; |
| 8448 | |
| 8449 | if (dict == NULL) { |
| 8450 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8451 | "format requires a mapping"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8452 | goto onError; |
| 8453 | } |
| 8454 | ++fmt; |
| 8455 | --fmtcnt; |
| 8456 | keystart = fmt; |
| 8457 | /* Skip over balanced parentheses */ |
| 8458 | while (pcount > 0 && --fmtcnt >= 0) { |
| 8459 | if (*fmt == ')') |
| 8460 | --pcount; |
| 8461 | else if (*fmt == '(') |
| 8462 | ++pcount; |
| 8463 | fmt++; |
| 8464 | } |
| 8465 | keylen = fmt - keystart - 1; |
| 8466 | if (fmtcnt < 0 || pcount > 0) { |
| 8467 | PyErr_SetString(PyExc_ValueError, |
| 8468 | "incomplete format key"); |
| 8469 | goto onError; |
| 8470 | } |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 8471 | #if 0 |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8472 | /* keys are converted to strings using UTF-8 and |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8473 | then looked up since Python uses strings to hold |
| 8474 | variables names etc. in its namespaces and we |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8475 | wouldn't want to break common idioms. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8476 | key = PyUnicode_EncodeUTF8(keystart, |
| 8477 | keylen, |
| 8478 | NULL); |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 8479 | #else |
| 8480 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 8481 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8482 | if (key == NULL) |
| 8483 | goto onError; |
| 8484 | if (args_owned) { |
| 8485 | Py_DECREF(args); |
| 8486 | args_owned = 0; |
| 8487 | } |
| 8488 | args = PyObject_GetItem(dict, key); |
| 8489 | Py_DECREF(key); |
| 8490 | if (args == NULL) { |
| 8491 | goto onError; |
| 8492 | } |
| 8493 | args_owned = 1; |
| 8494 | arglen = -1; |
| 8495 | argidx = -2; |
| 8496 | } |
| 8497 | while (--fmtcnt >= 0) { |
| 8498 | switch (c = *fmt++) { |
| 8499 | case '-': flags |= F_LJUST; continue; |
| 8500 | case '+': flags |= F_SIGN; continue; |
| 8501 | case ' ': flags |= F_BLANK; continue; |
| 8502 | case '#': flags |= F_ALT; continue; |
| 8503 | case '0': flags |= F_ZERO; continue; |
| 8504 | } |
| 8505 | break; |
| 8506 | } |
| 8507 | if (c == '*') { |
| 8508 | v = getnextarg(args, arglen, &argidx); |
| 8509 | if (v == NULL) |
| 8510 | goto onError; |
| 8511 | if (!PyInt_Check(v)) { |
| 8512 | PyErr_SetString(PyExc_TypeError, |
| 8513 | "* wants int"); |
| 8514 | goto onError; |
| 8515 | } |
| 8516 | width = PyInt_AsLong(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 8517 | if (width == -1 && PyErr_Occurred()) |
| 8518 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8519 | if (width < 0) { |
| 8520 | flags |= F_LJUST; |
| 8521 | width = -width; |
| 8522 | } |
| 8523 | if (--fmtcnt >= 0) |
| 8524 | c = *fmt++; |
| 8525 | } |
| 8526 | else if (c >= '0' && c <= '9') { |
| 8527 | width = c - '0'; |
| 8528 | while (--fmtcnt >= 0) { |
| 8529 | c = *fmt++; |
| 8530 | if (c < '0' || c > '9') |
| 8531 | break; |
| 8532 | if ((width*10) / 10 != width) { |
| 8533 | PyErr_SetString(PyExc_ValueError, |
| 8534 | "width too big"); |
| 8535 | goto onError; |
| 8536 | } |
| 8537 | width = width*10 + (c - '0'); |
| 8538 | } |
| 8539 | } |
| 8540 | if (c == '.') { |
| 8541 | prec = 0; |
| 8542 | if (--fmtcnt >= 0) |
| 8543 | c = *fmt++; |
| 8544 | if (c == '*') { |
| 8545 | v = getnextarg(args, arglen, &argidx); |
| 8546 | if (v == NULL) |
| 8547 | goto onError; |
| 8548 | if (!PyInt_Check(v)) { |
| 8549 | PyErr_SetString(PyExc_TypeError, |
| 8550 | "* wants int"); |
| 8551 | goto onError; |
| 8552 | } |
| 8553 | prec = PyInt_AsLong(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 8554 | if (prec == -1 && PyErr_Occurred()) |
| 8555 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8556 | if (prec < 0) |
| 8557 | prec = 0; |
| 8558 | if (--fmtcnt >= 0) |
| 8559 | c = *fmt++; |
| 8560 | } |
| 8561 | else if (c >= '0' && c <= '9') { |
| 8562 | prec = c - '0'; |
| 8563 | while (--fmtcnt >= 0) { |
| 8564 | c = Py_CHARMASK(*fmt++); |
| 8565 | if (c < '0' || c > '9') |
| 8566 | break; |
| 8567 | if ((prec*10) / 10 != prec) { |
| 8568 | PyErr_SetString(PyExc_ValueError, |
| 8569 | "prec too big"); |
| 8570 | goto onError; |
| 8571 | } |
| 8572 | prec = prec*10 + (c - '0'); |
| 8573 | } |
| 8574 | } |
| 8575 | } /* prec */ |
| 8576 | if (fmtcnt >= 0) { |
| 8577 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8578 | if (--fmtcnt >= 0) |
| 8579 | c = *fmt++; |
| 8580 | } |
| 8581 | } |
| 8582 | if (fmtcnt < 0) { |
| 8583 | PyErr_SetString(PyExc_ValueError, |
| 8584 | "incomplete format"); |
| 8585 | goto onError; |
| 8586 | } |
| 8587 | if (c != '%') { |
| 8588 | v = getnextarg(args, arglen, &argidx); |
| 8589 | if (v == NULL) |
| 8590 | goto onError; |
| 8591 | } |
| 8592 | sign = 0; |
| 8593 | fill = ' '; |
| 8594 | switch (c) { |
| 8595 | |
| 8596 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8597 | pbuf = formatbuf; |
| 8598 | /* presume that buffer length is at least 1 */ |
| 8599 | pbuf[0] = '%'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8600 | len = 1; |
| 8601 | break; |
| 8602 | |
| 8603 | case 's': |
| 8604 | case 'r': |
| 8605 | if (PyUnicode_Check(v) && c == 's') { |
| 8606 | temp = v; |
| 8607 | Py_INCREF(temp); |
| 8608 | } |
| 8609 | else { |
| 8610 | PyObject *unicode; |
| 8611 | if (c == 's') |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8612 | temp = PyObject_Unicode(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8613 | else |
| 8614 | temp = PyObject_Repr(v); |
| 8615 | if (temp == NULL) |
| 8616 | goto onError; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8617 | if (PyUnicode_Check(temp)) |
| 8618 | /* nothing to do */; |
| 8619 | else if (PyString_Check(temp)) { |
| 8620 | /* convert to string to Unicode */ |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8621 | unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8622 | PyString_GET_SIZE(temp), |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8623 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8624 | "strict"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8625 | Py_DECREF(temp); |
| 8626 | temp = unicode; |
| 8627 | if (temp == NULL) |
| 8628 | goto onError; |
| 8629 | } |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8630 | else { |
| 8631 | Py_DECREF(temp); |
| 8632 | PyErr_SetString(PyExc_TypeError, |
| 8633 | "%s argument has non-string str()"); |
| 8634 | goto onError; |
| 8635 | } |
| 8636 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8637 | pbuf = PyUnicode_AS_UNICODE(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8638 | len = PyUnicode_GET_SIZE(temp); |
| 8639 | if (prec >= 0 && len > prec) |
| 8640 | len = prec; |
| 8641 | break; |
| 8642 | |
| 8643 | case 'i': |
| 8644 | case 'd': |
| 8645 | case 'u': |
| 8646 | case 'o': |
| 8647 | case 'x': |
| 8648 | case 'X': |
| 8649 | if (c == 'i') |
| 8650 | c = 'd'; |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 8651 | if (PyLong_Check(v)) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8652 | temp = formatlong(v, flags, prec, c); |
| 8653 | if (!temp) |
| 8654 | goto onError; |
| 8655 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 8656 | len = PyUnicode_GET_SIZE(temp); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8657 | sign = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8658 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8659 | else { |
| 8660 | pbuf = formatbuf; |
| 8661 | len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8662 | flags, prec, c, v); |
| 8663 | if (len < 0) |
| 8664 | goto onError; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8665 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8666 | } |
| 8667 | if (flags & F_ZERO) |
| 8668 | fill = '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8669 | break; |
| 8670 | |
| 8671 | case 'e': |
| 8672 | case 'E': |
| 8673 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 8674 | case 'F': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8675 | case 'g': |
| 8676 | case 'G': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 8677 | if (c == 'F') |
| 8678 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8679 | pbuf = formatbuf; |
| 8680 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8681 | flags, prec, c, v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8682 | if (len < 0) |
| 8683 | goto onError; |
| 8684 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8685 | if (flags & F_ZERO) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8686 | fill = '0'; |
| 8687 | break; |
| 8688 | |
| 8689 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8690 | pbuf = formatbuf; |
| 8691 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8692 | if (len < 0) |
| 8693 | goto onError; |
| 8694 | break; |
| 8695 | |
| 8696 | default: |
| 8697 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 8698 | "unsupported format character '%c' (0x%x) " |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 8699 | "at index %zd", |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8700 | (31<=c && c<=126) ? (char)c : '?', |
Marc-André Lemburg | 24e53b6 | 2002-09-24 09:32:14 +0000 | [diff] [blame] | 8701 | (int)c, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 8702 | (Py_ssize_t)(fmt - 1 - |
| 8703 | PyUnicode_AS_UNICODE(uformat))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8704 | goto onError; |
| 8705 | } |
| 8706 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8707 | if (*pbuf == '-' || *pbuf == '+') { |
| 8708 | sign = *pbuf++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8709 | len--; |
| 8710 | } |
| 8711 | else if (flags & F_SIGN) |
| 8712 | sign = '+'; |
| 8713 | else if (flags & F_BLANK) |
| 8714 | sign = ' '; |
| 8715 | else |
| 8716 | sign = 0; |
| 8717 | } |
| 8718 | if (width < len) |
| 8719 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8720 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8721 | reslen -= rescnt; |
| 8722 | rescnt = width + fmtcnt + 100; |
| 8723 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8724 | if (reslen < 0) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8725 | Py_XDECREF(temp); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8726 | PyErr_NoMemory(); |
| 8727 | goto onError; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8728 | } |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8729 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 8730 | Py_XDECREF(temp); |
| 8731 | goto onError; |
| 8732 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8733 | res = PyUnicode_AS_UNICODE(result) |
| 8734 | + reslen - rescnt; |
| 8735 | } |
| 8736 | if (sign) { |
| 8737 | if (fill != ' ') |
| 8738 | *res++ = sign; |
| 8739 | rescnt--; |
| 8740 | if (width > len) |
| 8741 | width--; |
| 8742 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8743 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8744 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8745 | assert(pbuf[1] == c); |
| 8746 | if (fill != ' ') { |
| 8747 | *res++ = *pbuf++; |
| 8748 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8749 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8750 | rescnt -= 2; |
| 8751 | width -= 2; |
| 8752 | if (width < 0) |
| 8753 | width = 0; |
| 8754 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8755 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8756 | if (width > len && !(flags & F_LJUST)) { |
| 8757 | do { |
| 8758 | --rescnt; |
| 8759 | *res++ = fill; |
| 8760 | } while (--width > len); |
| 8761 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8762 | if (fill == ' ') { |
| 8763 | if (sign) |
| 8764 | *res++ = sign; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8765 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8766 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8767 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8768 | *res++ = *pbuf++; |
| 8769 | *res++ = *pbuf++; |
| 8770 | } |
| 8771 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8772 | Py_UNICODE_COPY(res, pbuf, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8773 | res += len; |
| 8774 | rescnt -= len; |
| 8775 | while (--width >= len) { |
| 8776 | --rescnt; |
| 8777 | *res++ = ' '; |
| 8778 | } |
| 8779 | if (dict && (argidx < arglen) && c != '%') { |
| 8780 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 8781 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8782 | Py_XDECREF(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8783 | goto onError; |
| 8784 | } |
| 8785 | Py_XDECREF(temp); |
| 8786 | } /* '%' */ |
| 8787 | } /* until end */ |
| 8788 | if (argidx < arglen && !dict) { |
| 8789 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 8790 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8791 | goto onError; |
| 8792 | } |
| 8793 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8794 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
| 8795 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8796 | if (args_owned) { |
| 8797 | Py_DECREF(args); |
| 8798 | } |
| 8799 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8800 | return (PyObject *)result; |
| 8801 | |
| 8802 | onError: |
| 8803 | Py_XDECREF(result); |
| 8804 | Py_DECREF(uformat); |
| 8805 | if (args_owned) { |
| 8806 | Py_DECREF(args); |
| 8807 | } |
| 8808 | return NULL; |
| 8809 | } |
| 8810 | |
| 8811 | static PyBufferProcs unicode_as_buffer = { |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 8812 | (getbufferproc) unicode_buffer_getbuffer, |
| 8813 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8814 | }; |
| 8815 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 8816 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8817 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 8818 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8819 | static PyObject * |
| 8820 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8821 | { |
| 8822 | PyObject *x = NULL; |
Guido van Rossum | 55b4a7b | 2007-07-11 09:28:11 +0000 | [diff] [blame] | 8823 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8824 | char *encoding = NULL; |
| 8825 | char *errors = NULL; |
| 8826 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8827 | if (type != &PyUnicode_Type) |
| 8828 | return unicode_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8829 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
| 8830 | kwlist, &x, &encoding, &errors)) |
| 8831 | return NULL; |
| 8832 | if (x == NULL) |
| 8833 | return (PyObject *)_PyUnicode_New(0); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 8834 | if (encoding == NULL && errors == NULL) |
| 8835 | return PyObject_Unicode(x); |
| 8836 | else |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8837 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
| 8838 | } |
| 8839 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8840 | static PyObject * |
| 8841 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8842 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8843 | PyUnicodeObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8844 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8845 | |
| 8846 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 8847 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 8848 | if (tmp == NULL) |
| 8849 | return NULL; |
| 8850 | assert(PyUnicode_Check(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8851 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8852 | if (pnew == NULL) { |
| 8853 | Py_DECREF(tmp); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8854 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8855 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8856 | pnew->str = PyMem_NEW(Py_UNICODE, n+1); |
| 8857 | if (pnew->str == NULL) { |
| 8858 | _Py_ForgetReference((PyObject *)pnew); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8859 | PyObject_Del(pnew); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8860 | Py_DECREF(tmp); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 8861 | return PyErr_NoMemory(); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8862 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8863 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 8864 | pnew->length = n; |
| 8865 | pnew->hash = tmp->hash; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8866 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8867 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8868 | } |
| 8869 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8870 | PyDoc_STRVAR(unicode_doc, |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 8871 | "str(string [, encoding[, errors]]) -> object\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8872 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 8873 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 8874 | encoding defaults to the current default string encoding.\n\ |
| 8875 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8876 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8877 | static PyObject *unicode_iter(PyObject *seq); |
| 8878 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8879 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 8880 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 8881 | "str", /* tp_name */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8882 | sizeof(PyUnicodeObject), /* tp_size */ |
| 8883 | 0, /* tp_itemsize */ |
| 8884 | /* Slots */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 8885 | (destructor)unicode_dealloc, /* tp_dealloc */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8886 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8887 | 0, /* tp_getattr */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8888 | 0, /* tp_setattr */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8889 | 0, /* tp_compare */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8890 | unicode_repr, /* tp_repr */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8891 | &unicode_as_number, /* tp_as_number */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8892 | &unicode_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8893 | &unicode_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8894 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 8895 | 0, /* tp_call*/ |
| 8896 | (reprfunc) unicode_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8897 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 8898 | 0, /* tp_setattro */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8899 | &unicode_as_buffer, /* tp_as_buffer */ |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 8900 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 8901 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8902 | unicode_doc, /* tp_doc */ |
| 8903 | 0, /* tp_traverse */ |
| 8904 | 0, /* tp_clear */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8905 | PyUnicode_RichCompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8906 | 0, /* tp_weaklistoffset */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8907 | unicode_iter, /* tp_iter */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8908 | 0, /* tp_iternext */ |
| 8909 | unicode_methods, /* tp_methods */ |
| 8910 | 0, /* tp_members */ |
| 8911 | 0, /* tp_getset */ |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 8912 | &PyBaseString_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8913 | 0, /* tp_dict */ |
| 8914 | 0, /* tp_descr_get */ |
| 8915 | 0, /* tp_descr_set */ |
| 8916 | 0, /* tp_dictoffset */ |
| 8917 | 0, /* tp_init */ |
| 8918 | 0, /* tp_alloc */ |
| 8919 | unicode_new, /* tp_new */ |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8920 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8921 | }; |
| 8922 | |
| 8923 | /* Initialize the Unicode implementation */ |
| 8924 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 8925 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8926 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8927 | int i; |
| 8928 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8929 | /* XXX - move this array to unicodectype.c ? */ |
| 8930 | Py_UNICODE linebreak[] = { |
| 8931 | 0x000A, /* LINE FEED */ |
| 8932 | 0x000D, /* CARRIAGE RETURN */ |
| 8933 | 0x001C, /* FILE SEPARATOR */ |
| 8934 | 0x001D, /* GROUP SEPARATOR */ |
| 8935 | 0x001E, /* RECORD SEPARATOR */ |
| 8936 | 0x0085, /* NEXT LINE */ |
| 8937 | 0x2028, /* LINE SEPARATOR */ |
| 8938 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 8939 | }; |
| 8940 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8941 | /* Init the implementation */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8942 | unicode_freelist = NULL; |
| 8943 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8944 | unicode_empty = _PyUnicode_New(0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8945 | if (!unicode_empty) |
| 8946 | return; |
| 8947 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8948 | for (i = 0; i < 256; i++) |
| 8949 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 8950 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 8951 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8952 | |
| 8953 | /* initialize the linebreak bloom filter */ |
| 8954 | bloom_linebreak = make_bloom_mask( |
| 8955 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 8956 | ); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8957 | |
| 8958 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8959 | } |
| 8960 | |
| 8961 | /* Finalize the Unicode implementation */ |
| 8962 | |
| 8963 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 8964 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8965 | { |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8966 | PyUnicodeObject *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8967 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8968 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 8969 | Py_XDECREF(unicode_empty); |
| 8970 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8971 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8972 | for (i = 0; i < 256; i++) { |
| 8973 | if (unicode_latin1[i]) { |
| 8974 | Py_DECREF(unicode_latin1[i]); |
| 8975 | unicode_latin1[i] = NULL; |
| 8976 | } |
| 8977 | } |
| 8978 | |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8979 | for (u = unicode_freelist; u != NULL;) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8980 | PyUnicodeObject *v = u; |
| 8981 | u = *(PyUnicodeObject **)u; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 8982 | if (v->str) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 8983 | PyMem_DEL(v->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 8984 | Py_XDECREF(v->defenc); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8985 | PyObject_Del(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8986 | } |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8987 | unicode_freelist = NULL; |
| 8988 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8989 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 8990 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 8991 | void |
| 8992 | PyUnicode_InternInPlace(PyObject **p) |
| 8993 | { |
| 8994 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 8995 | PyObject *t; |
| 8996 | if (s == NULL || !PyUnicode_Check(s)) |
| 8997 | Py_FatalError( |
| 8998 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 8999 | /* If it's a subclass, we don't really know what putting |
| 9000 | it in the interned dict might do. */ |
| 9001 | if (!PyUnicode_CheckExact(s)) |
| 9002 | return; |
| 9003 | if (PyUnicode_CHECK_INTERNED(s)) |
| 9004 | return; |
| 9005 | if (interned == NULL) { |
| 9006 | interned = PyDict_New(); |
| 9007 | if (interned == NULL) { |
| 9008 | PyErr_Clear(); /* Don't leave an exception */ |
| 9009 | return; |
| 9010 | } |
| 9011 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9012 | /* It might be that the GetItem call fails even |
| 9013 | though the key is present in the dictionary, |
| 9014 | namely when this happens during a stack overflow. */ |
| 9015 | Py_ALLOW_RECURSION |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9016 | t = PyDict_GetItem(interned, (PyObject *)s); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9017 | Py_END_ALLOW_RECURSION |
| 9018 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9019 | if (t) { |
| 9020 | Py_INCREF(t); |
| 9021 | Py_DECREF(*p); |
| 9022 | *p = t; |
| 9023 | return; |
| 9024 | } |
| 9025 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9026 | PyThreadState_GET()->recursion_critical = 1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9027 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 9028 | PyErr_Clear(); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9029 | PyThreadState_GET()->recursion_critical = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9030 | return; |
| 9031 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9032 | PyThreadState_GET()->recursion_critical = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9033 | /* The two references in interned are not counted by refcnt. |
| 9034 | The deallocator will take care of this */ |
Martin v. Löwis | 5d7428b | 2007-07-21 18:47:48 +0000 | [diff] [blame] | 9035 | Py_Refcnt(s) -= 2; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9036 | PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
| 9037 | } |
| 9038 | |
| 9039 | void |
| 9040 | PyUnicode_InternImmortal(PyObject **p) |
| 9041 | { |
| 9042 | PyUnicode_InternInPlace(p); |
| 9043 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 9044 | PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 9045 | Py_INCREF(*p); |
| 9046 | } |
| 9047 | } |
| 9048 | |
| 9049 | PyObject * |
| 9050 | PyUnicode_InternFromString(const char *cp) |
| 9051 | { |
| 9052 | PyObject *s = PyUnicode_FromString(cp); |
| 9053 | if (s == NULL) |
| 9054 | return NULL; |
| 9055 | PyUnicode_InternInPlace(&s); |
| 9056 | return s; |
| 9057 | } |
| 9058 | |
| 9059 | void _Py_ReleaseInternedUnicodeStrings(void) |
| 9060 | { |
| 9061 | PyObject *keys; |
| 9062 | PyUnicodeObject *s; |
| 9063 | Py_ssize_t i, n; |
| 9064 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
| 9065 | |
| 9066 | if (interned == NULL || !PyDict_Check(interned)) |
| 9067 | return; |
| 9068 | keys = PyDict_Keys(interned); |
| 9069 | if (keys == NULL || !PyList_Check(keys)) { |
| 9070 | PyErr_Clear(); |
| 9071 | return; |
| 9072 | } |
| 9073 | |
| 9074 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 9075 | detector, interned unicode strings are not forcibly deallocated; |
| 9076 | rather, we give them their stolen references back, and then clear |
| 9077 | and DECREF the interned dict. */ |
| 9078 | |
| 9079 | n = PyList_GET_SIZE(keys); |
| 9080 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
| 9081 | n); |
| 9082 | for (i = 0; i < n; i++) { |
| 9083 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
| 9084 | switch (s->state) { |
| 9085 | case SSTATE_NOT_INTERNED: |
| 9086 | /* XXX Shouldn't happen */ |
| 9087 | break; |
| 9088 | case SSTATE_INTERNED_IMMORTAL: |
Martin v. Löwis | 5d7428b | 2007-07-21 18:47:48 +0000 | [diff] [blame] | 9089 | Py_Refcnt(s) += 1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9090 | immortal_size += s->length; |
| 9091 | break; |
| 9092 | case SSTATE_INTERNED_MORTAL: |
Martin v. Löwis | 5d7428b | 2007-07-21 18:47:48 +0000 | [diff] [blame] | 9093 | Py_Refcnt(s) += 2; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9094 | mortal_size += s->length; |
| 9095 | break; |
| 9096 | default: |
| 9097 | Py_FatalError("Inconsistent interned string state."); |
| 9098 | } |
| 9099 | s->state = SSTATE_NOT_INTERNED; |
| 9100 | } |
| 9101 | fprintf(stderr, "total size of all interned strings: " |
| 9102 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 9103 | "mortal/immortal\n", mortal_size, immortal_size); |
| 9104 | Py_DECREF(keys); |
| 9105 | PyDict_Clear(interned); |
| 9106 | Py_DECREF(interned); |
| 9107 | interned = NULL; |
| 9108 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9109 | |
| 9110 | |
| 9111 | /********************* Unicode Iterator **************************/ |
| 9112 | |
| 9113 | typedef struct { |
| 9114 | PyObject_HEAD |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9115 | Py_ssize_t it_index; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9116 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
| 9117 | } unicodeiterobject; |
| 9118 | |
| 9119 | static void |
| 9120 | unicodeiter_dealloc(unicodeiterobject *it) |
| 9121 | { |
| 9122 | _PyObject_GC_UNTRACK(it); |
| 9123 | Py_XDECREF(it->it_seq); |
| 9124 | PyObject_GC_Del(it); |
| 9125 | } |
| 9126 | |
| 9127 | static int |
| 9128 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 9129 | { |
| 9130 | Py_VISIT(it->it_seq); |
| 9131 | return 0; |
| 9132 | } |
| 9133 | |
| 9134 | static PyObject * |
| 9135 | unicodeiter_next(unicodeiterobject *it) |
| 9136 | { |
| 9137 | PyUnicodeObject *seq; |
| 9138 | PyObject *item; |
| 9139 | |
| 9140 | assert(it != NULL); |
| 9141 | seq = it->it_seq; |
| 9142 | if (seq == NULL) |
| 9143 | return NULL; |
| 9144 | assert(PyUnicode_Check(seq)); |
| 9145 | |
| 9146 | if (it->it_index < PyUnicode_GET_SIZE(seq)) { |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9147 | item = PyUnicode_FromUnicode( |
| 9148 | PyUnicode_AS_UNICODE(seq)+it->it_index, 1); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9149 | if (item != NULL) |
| 9150 | ++it->it_index; |
| 9151 | return item; |
| 9152 | } |
| 9153 | |
| 9154 | Py_DECREF(seq); |
| 9155 | it->it_seq = NULL; |
| 9156 | return NULL; |
| 9157 | } |
| 9158 | |
| 9159 | static PyObject * |
| 9160 | unicodeiter_len(unicodeiterobject *it) |
| 9161 | { |
| 9162 | Py_ssize_t len = 0; |
| 9163 | if (it->it_seq) |
| 9164 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 9165 | return PyInt_FromSsize_t(len); |
| 9166 | } |
| 9167 | |
| 9168 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 9169 | |
| 9170 | static PyMethodDef unicodeiter_methods[] = { |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9171 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
| 9172 | length_hint_doc}, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9173 | {NULL, NULL} /* sentinel */ |
| 9174 | }; |
| 9175 | |
| 9176 | PyTypeObject PyUnicodeIter_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9177 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9178 | "unicodeiterator", /* tp_name */ |
| 9179 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 9180 | 0, /* tp_itemsize */ |
| 9181 | /* methods */ |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9182 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9183 | 0, /* tp_print */ |
| 9184 | 0, /* tp_getattr */ |
| 9185 | 0, /* tp_setattr */ |
| 9186 | 0, /* tp_compare */ |
| 9187 | 0, /* tp_repr */ |
| 9188 | 0, /* tp_as_number */ |
| 9189 | 0, /* tp_as_sequence */ |
| 9190 | 0, /* tp_as_mapping */ |
| 9191 | 0, /* tp_hash */ |
| 9192 | 0, /* tp_call */ |
| 9193 | 0, /* tp_str */ |
| 9194 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9195 | 0, /* tp_setattro */ |
| 9196 | 0, /* tp_as_buffer */ |
| 9197 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 9198 | 0, /* tp_doc */ |
| 9199 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 9200 | 0, /* tp_clear */ |
| 9201 | 0, /* tp_richcompare */ |
| 9202 | 0, /* tp_weaklistoffset */ |
| 9203 | PyObject_SelfIter, /* tp_iter */ |
| 9204 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 9205 | unicodeiter_methods, /* tp_methods */ |
| 9206 | 0, |
| 9207 | }; |
| 9208 | |
| 9209 | static PyObject * |
| 9210 | unicode_iter(PyObject *seq) |
| 9211 | { |
| 9212 | unicodeiterobject *it; |
| 9213 | |
| 9214 | if (!PyUnicode_Check(seq)) { |
| 9215 | PyErr_BadInternalCall(); |
| 9216 | return NULL; |
| 9217 | } |
| 9218 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 9219 | if (it == NULL) |
| 9220 | return NULL; |
| 9221 | it->it_index = 0; |
| 9222 | Py_INCREF(seq); |
| 9223 | it->it_seq = (PyUnicodeObject *)seq; |
| 9224 | _PyObject_GC_TRACK(it); |
| 9225 | return (PyObject *)it; |
| 9226 | } |
| 9227 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9228 | size_t |
| 9229 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 9230 | { |
| 9231 | int res = 0; |
| 9232 | while(*u++) |
| 9233 | res++; |
| 9234 | return res; |
| 9235 | } |
| 9236 | |
| 9237 | Py_UNICODE* |
| 9238 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9239 | { |
| 9240 | Py_UNICODE *u = s1; |
| 9241 | while ((*u++ = *s2++)); |
| 9242 | return s1; |
| 9243 | } |
| 9244 | |
| 9245 | Py_UNICODE* |
| 9246 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 9247 | { |
| 9248 | Py_UNICODE *u = s1; |
| 9249 | while ((*u++ = *s2++)) |
| 9250 | if (n-- == 0) |
| 9251 | break; |
| 9252 | return s1; |
| 9253 | } |
| 9254 | |
| 9255 | int |
| 9256 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9257 | { |
| 9258 | while (*s1 && *s2 && *s1 == *s2) |
| 9259 | s1++, s2++; |
| 9260 | if (*s1 && *s2) |
| 9261 | return (*s1 < *s2) ? -1 : +1; |
| 9262 | if (*s1) |
| 9263 | return 1; |
| 9264 | if (*s2) |
| 9265 | return -1; |
| 9266 | return 0; |
| 9267 | } |
| 9268 | |
| 9269 | Py_UNICODE* |
| 9270 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 9271 | { |
| 9272 | const Py_UNICODE *p; |
| 9273 | for (p = s; *p; p++) |
| 9274 | if (*p == c) |
| 9275 | return (Py_UNICODE*)p; |
| 9276 | return NULL; |
| 9277 | } |
| 9278 | |
| 9279 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9280 | #ifdef __cplusplus |
| 9281 | } |
| 9282 | #endif |
| 9283 | |
| 9284 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9285 | /* |
| 9286 | Local variables: |
| 9287 | c-basic-offset: 4 |
| 9288 | indent-tabs-mode: nil |
| 9289 | End: |
| 9290 | */ |