Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1 | /* |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2 | |
| 3 | Unicode implementation based on original code by Fredrik Lundh, |
Fred Drake | 785d14f | 2000-05-09 19:54:43 +0000 | [diff] [blame] | 4 | modified by Marc-Andre Lemburg <mal@lemburg.com> according to the |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5 | Unicode Integration Proposal (see file Misc/unicode.txt). |
| 6 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7 | Major speed upgrades to the method implementations at the Reykjavik |
| 8 | NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke. |
| 9 | |
Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 10 | Copyright (c) Corporation for National Research Initiatives. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 12 | -------------------------------------------------------------------- |
| 13 | The original string type implementation is: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 15 | Copyright (c) 1999 by Secret Labs AB |
| 16 | Copyright (c) 1999 by Fredrik Lundh |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 17 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 18 | By obtaining, using, and/or copying this software and/or its |
| 19 | associated documentation, you agree that you have read, understood, |
| 20 | and will comply with the following terms and conditions: |
| 21 | |
| 22 | Permission to use, copy, modify, and distribute this software and its |
| 23 | associated documentation for any purpose and without fee is hereby |
| 24 | granted, provided that the above copyright notice appears in all |
| 25 | copies, and that both that copyright notice and this permission notice |
| 26 | appear in supporting documentation, and that the name of Secret Labs |
| 27 | AB or the author not be used in advertising or publicity pertaining to |
| 28 | distribution of the software without specific, written prior |
| 29 | permission. |
| 30 | |
| 31 | SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 32 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 33 | FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR |
| 34 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 35 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 36 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 37 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 38 | -------------------------------------------------------------------- |
| 39 | |
| 40 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 41 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 42 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 43 | #include "Python.h" |
| 44 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 45 | #include "unicodeobject.h" |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 46 | #include "ucnhash.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 47 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 48 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 49 | #include <windows.h> |
| 50 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 52 | /* Limit for the Unicode object free list */ |
| 53 | |
| 54 | #define MAX_UNICODE_FREELIST_SIZE 1024 |
| 55 | |
| 56 | /* Limit for the Unicode object free list stay alive optimization. |
| 57 | |
| 58 | The implementation will keep allocated Unicode memory intact for |
| 59 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 60 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 61 | |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 62 | At worst this will result in MAX_UNICODE_FREELIST_SIZE * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 63 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 64 | malloc()-overhead) bytes of unused garbage. |
| 65 | |
| 66 | Setting the limit to 0 effectively turns the feature off. |
| 67 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 68 | Note: This is an experimental feature ! If you get core dumps when |
| 69 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 70 | |
| 71 | */ |
| 72 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 73 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 74 | |
| 75 | /* Endianness switches; defaults to little endian */ |
| 76 | |
| 77 | #ifdef WORDS_BIGENDIAN |
| 78 | # define BYTEORDER_IS_BIG_ENDIAN |
| 79 | #else |
| 80 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 81 | #endif |
| 82 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 83 | /* --- Globals ------------------------------------------------------------ |
| 84 | |
| 85 | The globals are initialized by the _PyUnicode_Init() API and should |
| 86 | not be used before calling that API. |
| 87 | |
| 88 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 89 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 90 | |
| 91 | #ifdef __cplusplus |
| 92 | extern "C" { |
| 93 | #endif |
| 94 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 95 | /* This dictionary holds all interned unicode strings. Note that references |
| 96 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 97 | When the interned string reaches a refcnt of 0 the string deallocation |
| 98 | function will delete the reference from this dictionary. |
| 99 | |
| 100 | Another way to look at this is that to say that the actual reference |
| 101 | count of a string is: s->ob_refcnt + (s->ob_sstate?2:0) |
| 102 | */ |
| 103 | static PyObject *interned; |
| 104 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 105 | /* Free list for Unicode objects */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 106 | static PyUnicodeObject *unicode_freelist; |
| 107 | static int unicode_freelist_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 108 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 109 | /* The empty Unicode object is shared to improve performance. */ |
| 110 | static PyUnicodeObject *unicode_empty; |
| 111 | |
| 112 | /* Single character Unicode strings in the Latin-1 range are being |
| 113 | shared as well. */ |
| 114 | static PyUnicodeObject *unicode_latin1[256]; |
| 115 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 116 | /* 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] | 117 | parameter; it is fixed to "utf-8". Always use the |
| 118 | PyUnicode_GetDefaultEncoding() API to access this global. */ |
| 119 | static const char unicode_default_encoding[] = "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 120 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 121 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 122 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 123 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 124 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 125 | return 0x10FFFF; |
| 126 | #else |
| 127 | /* This is actually an illegal character, so it should |
| 128 | not be passed to unichr. */ |
| 129 | return 0xFFFF; |
| 130 | #endif |
| 131 | } |
| 132 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 133 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 134 | |
| 135 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 136 | to keep things simple, we use a single bitmask, using the least 5 |
| 137 | bits from each unicode characters as the bit index. */ |
| 138 | |
| 139 | /* the linebreak mask is set up by Unicode_Init below */ |
| 140 | |
| 141 | #define BLOOM_MASK unsigned long |
| 142 | |
| 143 | static BLOOM_MASK bloom_linebreak; |
| 144 | |
| 145 | #define BLOOM(mask, ch) ((mask & (1 << ((ch) & 0x1F)))) |
| 146 | |
| 147 | #define BLOOM_LINEBREAK(ch)\ |
| 148 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK((ch))) |
| 149 | |
| 150 | Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len) |
| 151 | { |
| 152 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 153 | |
| 154 | long mask; |
| 155 | Py_ssize_t i; |
| 156 | |
| 157 | mask = 0; |
| 158 | for (i = 0; i < len; i++) |
| 159 | mask |= (1 << (ptr[i] & 0x1F)); |
| 160 | |
| 161 | return mask; |
| 162 | } |
| 163 | |
| 164 | Py_LOCAL_INLINE(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen) |
| 165 | { |
| 166 | Py_ssize_t i; |
| 167 | |
| 168 | for (i = 0; i < setlen; i++) |
| 169 | if (set[i] == chr) |
| 170 | return 1; |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | #define BLOOM_MEMBER(mask, chr, set, setlen)\ |
| 176 | BLOOM(mask, chr) && unicode_member(chr, set, setlen) |
| 177 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 178 | /* --- Unicode Object ----------------------------------------------------- */ |
| 179 | |
| 180 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 181 | int unicode_resize(register PyUnicodeObject *unicode, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 182 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 183 | { |
| 184 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 185 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 186 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 187 | if (unicode->length == length) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 188 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 189 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 190 | /* Resizing shared object (unicode_empty or single character |
| 191 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 192 | instead ! */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 193 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 194 | if (unicode == unicode_empty || |
| 195 | (unicode->length == 1 && |
| 196 | unicode->str[0] < 256U && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 197 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 198 | PyErr_SetString(PyExc_SystemError, |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 199 | "can't resize shared unicode objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 200 | return -1; |
| 201 | } |
| 202 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 203 | /* We allocate one more byte to make sure the string is Ux0000 terminated. |
| 204 | The overallocation is also used by fastsearch, which assumes that it's |
| 205 | safe to look at str[length] (without making any assumptions about what |
| 206 | it contains). */ |
| 207 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 208 | oldstr = unicode->str; |
| 209 | PyMem_RESIZE(unicode->str, Py_UNICODE, length + 1); |
| 210 | if (!unicode->str) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 211 | unicode->str = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 212 | PyErr_NoMemory(); |
| 213 | return -1; |
| 214 | } |
| 215 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 216 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 217 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 218 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 219 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 220 | if (unicode->defenc) { |
| 221 | Py_DECREF(unicode->defenc); |
| 222 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 223 | } |
| 224 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 225 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | /* We allocate one more byte to make sure the string is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 230 | Ux0000 terminated -- XXX is this needed ? |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 231 | |
| 232 | XXX This allocator could further be enhanced by assuring that the |
| 233 | free list never reduces its size below 1. |
| 234 | |
| 235 | */ |
| 236 | |
| 237 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 238 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 239 | { |
| 240 | register PyUnicodeObject *unicode; |
| 241 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 242 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 243 | if (length == 0 && unicode_empty != NULL) { |
| 244 | Py_INCREF(unicode_empty); |
| 245 | return unicode_empty; |
| 246 | } |
| 247 | |
| 248 | /* Unicode freelist & memory allocation */ |
| 249 | if (unicode_freelist) { |
| 250 | unicode = unicode_freelist; |
Marc-André Lemburg | bea47e7 | 2000-06-17 20:31:17 +0000 | [diff] [blame] | 251 | unicode_freelist = *(PyUnicodeObject **)unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 252 | unicode_freelist_size--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 253 | if (unicode->str) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 254 | /* Keep-Alive optimization: we only upsize the buffer, |
| 255 | never downsize it. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 256 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 257 | unicode_resize(unicode, length) < 0) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 258 | PyMem_DEL(unicode->str); |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 259 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 260 | } |
| 261 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 262 | else { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 263 | unicode->str = PyMem_NEW(Py_UNICODE, length + 1); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 264 | } |
| 265 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 266 | } |
| 267 | else { |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 268 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 269 | if (unicode == NULL) |
| 270 | return NULL; |
| 271 | unicode->str = PyMem_NEW(Py_UNICODE, length + 1); |
| 272 | } |
| 273 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 274 | if (!unicode->str) { |
| 275 | PyErr_NoMemory(); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 276 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 277 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 278 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 279 | * the caller fails before initializing str -- unicode_resize() |
| 280 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 281 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 282 | * We don't want unicode_resize to read uninitialized memory in |
| 283 | * that case. |
| 284 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 285 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 286 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 287 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 288 | unicode->hash = -1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 289 | unicode->state = 0; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 290 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 291 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 292 | |
| 293 | onError: |
| 294 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 295 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 296 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 300 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 301 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 302 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
| 303 | case SSTATE_NOT_INTERNED: |
| 304 | break; |
| 305 | |
| 306 | case SSTATE_INTERNED_MORTAL: |
| 307 | /* revive dead object temporarily for DelItem */ |
| 308 | unicode->ob_refcnt = 3; |
| 309 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 310 | Py_FatalError( |
| 311 | "deletion of interned unicode string failed"); |
| 312 | break; |
| 313 | |
| 314 | case SSTATE_INTERNED_IMMORTAL: |
| 315 | Py_FatalError("Immortal interned unicode string died."); |
| 316 | |
| 317 | default: |
| 318 | Py_FatalError("Inconsistent interned unicode string state."); |
| 319 | } |
| 320 | |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 321 | if (PyUnicode_CheckExact(unicode) && |
| 322 | unicode_freelist_size < MAX_UNICODE_FREELIST_SIZE) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 323 | /* Keep-Alive optimization */ |
| 324 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 325 | PyMem_DEL(unicode->str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 326 | unicode->str = NULL; |
| 327 | unicode->length = 0; |
| 328 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 329 | if (unicode->defenc) { |
| 330 | Py_DECREF(unicode->defenc); |
| 331 | unicode->defenc = NULL; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 332 | } |
| 333 | /* Add to free list */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 334 | *(PyUnicodeObject **)unicode = unicode_freelist; |
| 335 | unicode_freelist = unicode; |
| 336 | unicode_freelist_size++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 337 | } |
| 338 | else { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 339 | PyMem_DEL(unicode->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 340 | Py_XDECREF(unicode->defenc); |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 341 | unicode->ob_type->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 345 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 346 | { |
| 347 | register PyUnicodeObject *v; |
| 348 | |
| 349 | /* Argument checks */ |
| 350 | if (unicode == NULL) { |
| 351 | PyErr_BadInternalCall(); |
| 352 | return -1; |
| 353 | } |
| 354 | v = (PyUnicodeObject *)*unicode; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 355 | if (v == NULL || !PyUnicode_Check(v) || v->ob_refcnt != 1 || length < 0) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 356 | PyErr_BadInternalCall(); |
| 357 | return -1; |
| 358 | } |
| 359 | |
| 360 | /* Resizing unicode_empty and single character objects is not |
| 361 | possible since these are being shared. We simply return a fresh |
| 362 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 363 | if (v->length != length && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 364 | (v == unicode_empty || v->length == 1)) { |
| 365 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 366 | if (w == NULL) |
| 367 | return -1; |
| 368 | Py_UNICODE_COPY(w->str, v->str, |
| 369 | length < v->length ? length : v->length); |
Raymond Hettinger | c8df578 | 2003-03-09 07:30:43 +0000 | [diff] [blame] | 370 | Py_DECREF(*unicode); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 371 | *unicode = (PyObject *)w; |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 376 | objects, since we can modify them in-place. */ |
| 377 | return unicode_resize(v, length); |
| 378 | } |
| 379 | |
| 380 | /* Internal API for use in unicodeobject.c only ! */ |
| 381 | #define _PyUnicode_Resize(unicodevar, length) \ |
| 382 | PyUnicode_Resize(((PyObject **)(unicodevar)), length) |
| 383 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 384 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 385 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 386 | { |
| 387 | PyUnicodeObject *unicode; |
| 388 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 389 | /* If the Unicode data is known at construction time, we can apply |
| 390 | some optimizations which share commonly used objects. */ |
| 391 | if (u != NULL) { |
| 392 | |
| 393 | /* Optimization for empty strings */ |
| 394 | if (size == 0 && unicode_empty != NULL) { |
| 395 | Py_INCREF(unicode_empty); |
| 396 | return (PyObject *)unicode_empty; |
| 397 | } |
| 398 | |
| 399 | /* Single character Unicode objects in the Latin-1 range are |
| 400 | shared when using this constructor */ |
| 401 | if (size == 1 && *u < 256) { |
| 402 | unicode = unicode_latin1[*u]; |
| 403 | if (!unicode) { |
| 404 | unicode = _PyUnicode_New(1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 405 | if (!unicode) |
| 406 | return NULL; |
Marc-André Lemburg | 8879a33 | 2001-06-07 12:26:56 +0000 | [diff] [blame] | 407 | unicode->str[0] = *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 408 | unicode_latin1[*u] = unicode; |
| 409 | } |
| 410 | Py_INCREF(unicode); |
| 411 | return (PyObject *)unicode; |
| 412 | } |
| 413 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 414 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 415 | unicode = _PyUnicode_New(size); |
| 416 | if (!unicode) |
| 417 | return NULL; |
| 418 | |
| 419 | /* Copy the Unicode data into the new object */ |
| 420 | if (u != NULL) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 421 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 422 | |
| 423 | return (PyObject *)unicode; |
| 424 | } |
| 425 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 426 | PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 427 | { |
| 428 | PyUnicodeObject *unicode; |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 429 | /* If the Unicode data is known at construction time, we can apply |
| 430 | some optimizations which share commonly used objects. */ |
| 431 | if (u != NULL) { |
| 432 | |
| 433 | /* Optimization for empty strings */ |
| 434 | if (size == 0 && unicode_empty != NULL) { |
| 435 | Py_INCREF(unicode_empty); |
| 436 | return (PyObject *)unicode_empty; |
| 437 | } |
| 438 | |
Walter Dörwald | 071b9da | 2007-05-05 14:21:20 +0000 | [diff] [blame] | 439 | /* Single characters are shared when using this constructor */ |
| 440 | if (size == 1) { |
Walter Dörwald | ce32db3 | 2007-05-05 14:26:59 +0000 | [diff] [blame] | 441 | unicode = unicode_latin1[(int)*u]; |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 442 | if (!unicode) { |
| 443 | unicode = _PyUnicode_New(1); |
| 444 | if (!unicode) |
| 445 | return NULL; |
| 446 | unicode->str[0] = *u; |
Walter Dörwald | ce32db3 | 2007-05-05 14:26:59 +0000 | [diff] [blame] | 447 | unicode_latin1[(int)*u] = unicode; |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 448 | } |
| 449 | Py_INCREF(unicode); |
| 450 | return (PyObject *)unicode; |
| 451 | } |
| 452 | } |
| 453 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 454 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 455 | if (!unicode) |
| 456 | return NULL; |
| 457 | |
| 458 | /* Copy the Unicode data into the new object */ |
| 459 | if (u != NULL) { |
Walter Dörwald | ce32db3 | 2007-05-05 14:26:59 +0000 | [diff] [blame] | 460 | Py_UNICODE *p = unicode->str; |
| 461 | while ((*p++ = *u++)) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 462 | ; |
| 463 | } |
| 464 | |
| 465 | return (PyObject *)unicode; |
| 466 | } |
| 467 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 468 | PyObject *PyUnicode_FromString(const char *u) |
| 469 | { |
| 470 | size_t size = strlen(u); |
| 471 | if (size > PY_SSIZE_T_MAX) { |
| 472 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 473 | return NULL; |
| 474 | } |
| 475 | |
| 476 | return PyUnicode_FromStringAndSize(u, size); |
| 477 | } |
| 478 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 479 | #ifdef HAVE_WCHAR_H |
| 480 | |
| 481 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 482 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 483 | { |
| 484 | PyUnicodeObject *unicode; |
| 485 | |
| 486 | if (w == NULL) { |
| 487 | PyErr_BadInternalCall(); |
| 488 | return NULL; |
| 489 | } |
| 490 | |
| 491 | unicode = _PyUnicode_New(size); |
| 492 | if (!unicode) |
| 493 | return NULL; |
| 494 | |
| 495 | /* Copy the wchar_t data into the new object */ |
| 496 | #ifdef HAVE_USABLE_WCHAR_T |
| 497 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 498 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 499 | { |
| 500 | register Py_UNICODE *u; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 501 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 502 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 503 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 504 | *u++ = *w++; |
| 505 | } |
| 506 | #endif |
| 507 | |
| 508 | return (PyObject *)unicode; |
| 509 | } |
| 510 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame^] | 511 | static void |
| 512 | makefmt(char *fmt, int longflag, int size_tflag, int zeropad, int width, int precision, char c) |
| 513 | { |
| 514 | *fmt++ = '%'; |
| 515 | if (width) { |
| 516 | if (zeropad) |
| 517 | *fmt++ = '0'; |
| 518 | fmt += sprintf(fmt, "%d", width); |
| 519 | } |
| 520 | if (precision) |
| 521 | fmt += sprintf(fmt, ".%d", precision); |
| 522 | if (longflag) |
| 523 | *fmt++ = 'l'; |
| 524 | else if (size_tflag) { |
| 525 | char *f = PY_FORMAT_SIZE_T; |
| 526 | while (*f) |
| 527 | *fmt++ = *f++; |
| 528 | } |
| 529 | *fmt++ = c; |
| 530 | *fmt = '\0'; |
| 531 | } |
| 532 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 533 | #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} |
| 534 | |
| 535 | PyObject * |
| 536 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 537 | { |
| 538 | va_list count; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 539 | Py_ssize_t callcount = 0; |
| 540 | PyObject **callresults = NULL; |
| 541 | PyObject **callresult; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 542 | Py_ssize_t n = 0; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame^] | 543 | int width = 0; |
| 544 | int precision = 0; |
| 545 | int zeropad; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 546 | const char* f; |
| 547 | Py_UNICODE *s; |
| 548 | PyObject *string; |
| 549 | /* used by sprintf */ |
| 550 | char buffer[21]; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame^] | 551 | /* use abuffer instead of buffer, if we need more space |
| 552 | * (which can happen if there's a format specifier with width). */ |
| 553 | char *abuffer = NULL; |
| 554 | char *realbuffer; |
| 555 | Py_ssize_t abuffersize = 0; |
| 556 | char fmt[60]; /* should be enough for %0width.precisionld */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 557 | const char *copy; |
| 558 | |
| 559 | #ifdef VA_LIST_IS_ARRAY |
| 560 | Py_MEMCPY(count, vargs, sizeof(va_list)); |
| 561 | #else |
| 562 | #ifdef __va_copy |
| 563 | __va_copy(count, vargs); |
| 564 | #else |
| 565 | count = vargs; |
| 566 | #endif |
| 567 | #endif |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 568 | /* step 1: count the number of %S/%R format specifications |
| 569 | * (we call PyObject_Unicode()/PyObject_Repr() for these objects |
| 570 | * once during step 3 and put the result in an array) */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 571 | for (f = format; *f; f++) { |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 572 | if (*f == '%' && (*(f+1)=='S' || *(f+1)=='R')) |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 573 | ++callcount; |
| 574 | } |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 575 | /* step 2: allocate memory for the results of |
| 576 | * PyObject_Unicode()/PyObject_Repr() calls */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 577 | if (callcount) { |
| 578 | callresults = PyMem_Malloc(sizeof(PyObject *)*callcount); |
| 579 | if (!callresults) { |
| 580 | PyErr_NoMemory(); |
| 581 | return NULL; |
| 582 | } |
| 583 | callresult = callresults; |
| 584 | } |
| 585 | /* step 3: figure out how large a buffer we need */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 586 | for (f = format; *f; f++) { |
| 587 | if (*f == '%') { |
| 588 | const char* p = f; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame^] | 589 | width = 0; |
| 590 | while (isdigit(Py_CHARMASK(*f))) |
| 591 | width = (width*10) + *f++ - '0'; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 592 | while (*++f && *f != '%' && !isalpha(Py_CHARMASK(*f))) |
| 593 | ; |
| 594 | |
| 595 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 596 | * they don't affect the amount of space we reserve. |
| 597 | */ |
| 598 | if ((*f == 'l' || *f == 'z') && |
| 599 | (f[1] == 'd' || f[1] == 'u')) |
| 600 | ++f; |
| 601 | |
| 602 | switch (*f) { |
| 603 | case 'c': |
| 604 | (void)va_arg(count, int); |
| 605 | /* fall through... */ |
| 606 | case '%': |
| 607 | n++; |
| 608 | break; |
| 609 | case 'd': case 'u': case 'i': case 'x': |
| 610 | (void) va_arg(count, int); |
| 611 | /* 20 bytes is enough to hold a 64-bit |
| 612 | integer. Decimal takes the most space. |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame^] | 613 | This isn't enough for octal. |
| 614 | If a width is specified we need more |
| 615 | (which we allocate later). */ |
| 616 | if (width < 20) |
| 617 | width = 20; |
| 618 | n += width; |
| 619 | if (abuffersize < width) |
| 620 | abuffersize = width; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 621 | break; |
| 622 | case 's': |
| 623 | n += strlen(va_arg(count, char*)); |
| 624 | break; |
| 625 | case 'U': |
| 626 | { |
| 627 | PyObject *obj = va_arg(count, PyObject *); |
| 628 | assert(obj && PyUnicode_Check(obj)); |
| 629 | n += PyUnicode_GET_SIZE(obj); |
| 630 | break; |
| 631 | } |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 632 | case 'S': |
| 633 | { |
| 634 | PyObject *obj = va_arg(count, PyObject *); |
| 635 | PyObject *str; |
| 636 | assert(obj); |
| 637 | str = PyObject_Unicode(obj); |
| 638 | if (!str) |
| 639 | goto fail; |
| 640 | n += PyUnicode_GET_SIZE(str); |
| 641 | /* Remember the str and switch to the next slot */ |
| 642 | *callresult++ = str; |
| 643 | break; |
| 644 | } |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 645 | case 'R': |
| 646 | { |
| 647 | PyObject *obj = va_arg(count, PyObject *); |
| 648 | PyObject *repr; |
| 649 | assert(obj); |
| 650 | repr = PyObject_Repr(obj); |
| 651 | if (!repr) |
| 652 | goto fail; |
| 653 | n += PyUnicode_GET_SIZE(repr); |
| 654 | /* Remember the repr and switch to the next slot */ |
| 655 | *callresult++ = repr; |
| 656 | break; |
| 657 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 658 | case 'p': |
| 659 | (void) va_arg(count, int); |
| 660 | /* maximum 64-bit pointer representation: |
| 661 | * 0xffffffffffffffff |
| 662 | * so 19 characters is enough. |
| 663 | * XXX I count 18 -- what's the extra for? |
| 664 | */ |
| 665 | n += 19; |
| 666 | break; |
| 667 | default: |
| 668 | /* if we stumble upon an unknown |
| 669 | formatting code, copy the rest of |
| 670 | the format string to the output |
| 671 | string. (we cannot just skip the |
| 672 | code, since there's no way to know |
| 673 | what's in the argument list) */ |
| 674 | n += strlen(p); |
| 675 | goto expand; |
| 676 | } |
| 677 | } else |
| 678 | n++; |
| 679 | } |
| 680 | expand: |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame^] | 681 | if (abuffersize > 20) { |
| 682 | abuffer = PyMem_Malloc(abuffersize); |
| 683 | if (!abuffer) { |
| 684 | PyErr_NoMemory(); |
| 685 | goto fail; |
| 686 | } |
| 687 | realbuffer = abuffer; |
| 688 | } |
| 689 | else |
| 690 | realbuffer = buffer; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 691 | /* step 4: fill the buffer */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 692 | /* 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] | 693 | we don't have to resize the string. |
| 694 | There can be no errors beyond this point. */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 695 | string = PyUnicode_FromUnicode(NULL, n); |
| 696 | if (!string) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame^] | 697 | goto fail; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 698 | |
| 699 | s = PyUnicode_AS_UNICODE(string); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 700 | callresult = callresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 701 | |
| 702 | for (f = format; *f; f++) { |
| 703 | if (*f == '%') { |
| 704 | const char* p = f++; |
| 705 | int longflag = 0; |
| 706 | int size_tflag = 0; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame^] | 707 | zeropad = (*f == '0'); |
| 708 | /* parse the width.precision part */ |
| 709 | width = 0; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 710 | while (isdigit(Py_CHARMASK(*f))) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame^] | 711 | width = (width*10) + *f++ - '0'; |
| 712 | precision = 0; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 713 | if (*f == '.') { |
| 714 | f++; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 715 | while (isdigit(Py_CHARMASK(*f))) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame^] | 716 | precision = (precision*10) + *f++ - '0'; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 717 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 718 | /* handle the long flag, but only for %ld and %lu. |
| 719 | others can be added when necessary. */ |
| 720 | if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
| 721 | longflag = 1; |
| 722 | ++f; |
| 723 | } |
| 724 | /* handle the size_t flag. */ |
| 725 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 726 | size_tflag = 1; |
| 727 | ++f; |
| 728 | } |
| 729 | |
| 730 | switch (*f) { |
| 731 | case 'c': |
| 732 | *s++ = va_arg(vargs, int); |
| 733 | break; |
| 734 | case 'd': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame^] | 735 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'd'); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 736 | if (longflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame^] | 737 | sprintf(realbuffer, fmt, va_arg(vargs, long)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 738 | else if (size_tflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame^] | 739 | sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 740 | else |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame^] | 741 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 742 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 743 | break; |
| 744 | case 'u': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame^] | 745 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'u'); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 746 | if (longflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame^] | 747 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 748 | else if (size_tflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame^] | 749 | sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 750 | else |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame^] | 751 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
| 752 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 753 | break; |
| 754 | case 'i': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame^] | 755 | makefmt(fmt, 0, 0, zeropad, width, precision, 'i'); |
| 756 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 757 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 758 | break; |
| 759 | case 'x': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame^] | 760 | makefmt(fmt, 0, 0, zeropad, width, precision, 'x'); |
| 761 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 762 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 763 | break; |
| 764 | case 's': |
| 765 | p = va_arg(vargs, char*); |
| 766 | appendstring(p); |
| 767 | break; |
| 768 | case 'U': |
| 769 | { |
| 770 | PyObject *obj = va_arg(vargs, PyObject *); |
Walter Dörwald | 5c2fab6 | 2007-05-24 19:51:02 +0000 | [diff] [blame] | 771 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 772 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 773 | s += size; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 774 | break; |
| 775 | } |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 776 | case 'S': |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 777 | case 'R': |
| 778 | { |
| 779 | /* unused, since we already have the result */ |
| 780 | (void) va_arg(vargs, PyObject *); |
| 781 | Py_UNICODE *ucopy = PyUnicode_AS_UNICODE(*callresult); |
| 782 | Py_ssize_t usize = PyUnicode_GET_SIZE(*callresult); |
| 783 | Py_ssize_t upos; |
| 784 | for (upos = 0; upos<usize;) |
| 785 | *s++ = ucopy[upos++]; |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 786 | /* We're done with the unicode()/repr() => forget it */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 787 | Py_DECREF(*callresult); |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 788 | /* switch to next unicode()/repr() result */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 789 | ++callresult; |
| 790 | break; |
| 791 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 792 | case 'p': |
| 793 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 794 | /* %p is ill-defined: ensure leading 0x. */ |
| 795 | if (buffer[1] == 'X') |
| 796 | buffer[1] = 'x'; |
| 797 | else if (buffer[1] != 'x') { |
| 798 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 799 | buffer[0] = '0'; |
| 800 | buffer[1] = 'x'; |
| 801 | } |
| 802 | appendstring(buffer); |
| 803 | break; |
| 804 | case '%': |
| 805 | *s++ = '%'; |
| 806 | break; |
| 807 | default: |
| 808 | appendstring(p); |
| 809 | goto end; |
| 810 | } |
| 811 | } else |
| 812 | *s++ = *f; |
| 813 | } |
| 814 | |
| 815 | end: |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 816 | if (callresults) |
| 817 | PyMem_Free(callresults); |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame^] | 818 | if (abuffer) |
| 819 | PyMem_Free(abuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 820 | _PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
| 821 | return string; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 822 | fail: |
| 823 | if (callresults) { |
| 824 | PyObject **callresult2 = callresults; |
| 825 | while (callresult2 <= callresult) { |
| 826 | Py_DECREF(*callresult2); |
| 827 | ++callresult2; |
| 828 | } |
| 829 | PyMem_Free(callresults); |
| 830 | } |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame^] | 831 | if (abuffer) |
| 832 | PyMem_Free(abuffer); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 833 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | #undef appendstring |
| 837 | |
| 838 | PyObject * |
| 839 | PyUnicode_FromFormat(const char *format, ...) |
| 840 | { |
| 841 | PyObject* ret; |
| 842 | va_list vargs; |
| 843 | |
| 844 | #ifdef HAVE_STDARG_PROTOTYPES |
| 845 | va_start(vargs, format); |
| 846 | #else |
| 847 | va_start(vargs); |
| 848 | #endif |
| 849 | ret = PyUnicode_FromFormatV(format, vargs); |
| 850 | va_end(vargs); |
| 851 | return ret; |
| 852 | } |
| 853 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 854 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 855 | wchar_t *w, |
| 856 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 857 | { |
| 858 | if (unicode == NULL) { |
| 859 | PyErr_BadInternalCall(); |
| 860 | return -1; |
| 861 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 862 | |
| 863 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 864 | if (size > PyUnicode_GET_SIZE(unicode)) |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 865 | size = PyUnicode_GET_SIZE(unicode) + 1; |
| 866 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 867 | #ifdef HAVE_USABLE_WCHAR_T |
| 868 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 869 | #else |
| 870 | { |
| 871 | register Py_UNICODE *u; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 872 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 873 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 874 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 875 | *w++ = *u++; |
| 876 | } |
| 877 | #endif |
| 878 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 879 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 880 | return PyUnicode_GET_SIZE(unicode); |
| 881 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 882 | return size; |
| 883 | } |
| 884 | |
| 885 | #endif |
| 886 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 887 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 888 | { |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 889 | Py_UNICODE s[1]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 890 | |
| 891 | #ifdef Py_UNICODE_WIDE |
| 892 | if (ordinal < 0 || ordinal > 0x10ffff) { |
| 893 | PyErr_SetString(PyExc_ValueError, |
| 894 | "unichr() arg not in range(0x110000) " |
| 895 | "(wide Python build)"); |
| 896 | return NULL; |
| 897 | } |
| 898 | #else |
| 899 | if (ordinal < 0 || ordinal > 0xffff) { |
| 900 | PyErr_SetString(PyExc_ValueError, |
| 901 | "unichr() arg not in range(0x10000) " |
| 902 | "(narrow Python build)"); |
| 903 | return NULL; |
| 904 | } |
| 905 | #endif |
| 906 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 907 | s[0] = (Py_UNICODE)ordinal; |
| 908 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 911 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 912 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 913 | /* XXX Perhaps we should make this API an alias of |
| 914 | PyObject_Unicode() instead ?! */ |
| 915 | if (PyUnicode_CheckExact(obj)) { |
| 916 | Py_INCREF(obj); |
| 917 | return obj; |
| 918 | } |
| 919 | if (PyUnicode_Check(obj)) { |
| 920 | /* For a Unicode subtype that's not a Unicode object, |
| 921 | return a true Unicode object with the same data. */ |
| 922 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 923 | PyUnicode_GET_SIZE(obj)); |
| 924 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 925 | return PyUnicode_FromEncodedObject(obj, NULL, "strict"); |
| 926 | } |
| 927 | |
| 928 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
| 929 | const char *encoding, |
| 930 | const char *errors) |
| 931 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 932 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 933 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 934 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 935 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 936 | if (obj == NULL) { |
| 937 | PyErr_BadInternalCall(); |
| 938 | return NULL; |
| 939 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 940 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 941 | #if 0 |
| 942 | /* For b/w compatibility we also accept Unicode objects provided |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 943 | that no encodings is given and then redirect to |
| 944 | PyObject_Unicode() which then applies the additional logic for |
| 945 | Unicode subclasses. |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 946 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 947 | NOTE: This API should really only be used for object which |
| 948 | represent *encoded* Unicode ! |
| 949 | |
| 950 | */ |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 951 | if (PyUnicode_Check(obj)) { |
| 952 | if (encoding) { |
| 953 | PyErr_SetString(PyExc_TypeError, |
| 954 | "decoding Unicode is not supported"); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 955 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 956 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 957 | return PyObject_Unicode(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 958 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 959 | #else |
| 960 | if (PyUnicode_Check(obj)) { |
| 961 | PyErr_SetString(PyExc_TypeError, |
| 962 | "decoding Unicode is not supported"); |
| 963 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 964 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 965 | #endif |
| 966 | |
| 967 | /* Coerce object */ |
| 968 | if (PyString_Check(obj)) { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 969 | s = PyString_AS_STRING(obj); |
| 970 | len = PyString_GET_SIZE(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 971 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 972 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
| 973 | /* Overwrite the error message with something more useful in |
| 974 | case of a TypeError. */ |
| 975 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 976 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 977 | "coercing to Unicode: need string or buffer, " |
| 978 | "%.80s found", |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 979 | obj->ob_type->tp_name); |
| 980 | goto onError; |
| 981 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 982 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 983 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 984 | if (len == 0) { |
| 985 | Py_INCREF(unicode_empty); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 986 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 987 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 988 | else |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 989 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 990 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 991 | return v; |
| 992 | |
| 993 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 994 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | PyObject *PyUnicode_Decode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 998 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 999 | const char *encoding, |
| 1000 | const char *errors) |
| 1001 | { |
| 1002 | PyObject *buffer = NULL, *unicode; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1003 | |
| 1004 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1005 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1006 | |
| 1007 | /* Shortcuts for common default encodings */ |
| 1008 | if (strcmp(encoding, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1009 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1010 | else if (strcmp(encoding, "latin-1") == 0) |
| 1011 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1012 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1013 | else if (strcmp(encoding, "mbcs") == 0) |
| 1014 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 1015 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1016 | else if (strcmp(encoding, "ascii") == 0) |
| 1017 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1018 | |
| 1019 | /* Decode via the codec registry */ |
| 1020 | buffer = PyBuffer_FromMemory((void *)s, size); |
| 1021 | if (buffer == NULL) |
| 1022 | goto onError; |
| 1023 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1024 | if (unicode == NULL) |
| 1025 | goto onError; |
| 1026 | if (!PyUnicode_Check(unicode)) { |
| 1027 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 1028 | "decoder did not return an unicode object (type=%.400s)", |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1029 | unicode->ob_type->tp_name); |
| 1030 | Py_DECREF(unicode); |
| 1031 | goto onError; |
| 1032 | } |
| 1033 | Py_DECREF(buffer); |
| 1034 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1035 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1036 | onError: |
| 1037 | Py_XDECREF(buffer); |
| 1038 | return NULL; |
| 1039 | } |
| 1040 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1041 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1042 | const char *encoding, |
| 1043 | const char *errors) |
| 1044 | { |
| 1045 | PyObject *v; |
| 1046 | |
| 1047 | if (!PyUnicode_Check(unicode)) { |
| 1048 | PyErr_BadArgument(); |
| 1049 | goto onError; |
| 1050 | } |
| 1051 | |
| 1052 | if (encoding == NULL) |
| 1053 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1054 | |
| 1055 | /* Decode via the codec registry */ |
| 1056 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1057 | if (v == NULL) |
| 1058 | goto onError; |
| 1059 | return v; |
| 1060 | |
| 1061 | onError: |
| 1062 | return NULL; |
| 1063 | } |
| 1064 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1065 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1066 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1067 | const char *encoding, |
| 1068 | const char *errors) |
| 1069 | { |
| 1070 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1071 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1072 | unicode = PyUnicode_FromUnicode(s, size); |
| 1073 | if (unicode == NULL) |
| 1074 | return NULL; |
| 1075 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1076 | Py_DECREF(unicode); |
| 1077 | return v; |
| 1078 | } |
| 1079 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1080 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1081 | const char *encoding, |
| 1082 | const char *errors) |
| 1083 | { |
| 1084 | PyObject *v; |
| 1085 | |
| 1086 | if (!PyUnicode_Check(unicode)) { |
| 1087 | PyErr_BadArgument(); |
| 1088 | goto onError; |
| 1089 | } |
| 1090 | |
| 1091 | if (encoding == NULL) |
| 1092 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1093 | |
| 1094 | /* Encode via the codec registry */ |
| 1095 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1096 | if (v == NULL) |
| 1097 | goto onError; |
| 1098 | return v; |
| 1099 | |
| 1100 | onError: |
| 1101 | return NULL; |
| 1102 | } |
| 1103 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1104 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1105 | const char *encoding, |
| 1106 | const char *errors) |
| 1107 | { |
| 1108 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1109 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1110 | if (!PyUnicode_Check(unicode)) { |
| 1111 | PyErr_BadArgument(); |
| 1112 | goto onError; |
| 1113 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1114 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1115 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1116 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1117 | |
| 1118 | /* Shortcuts for common default encodings */ |
| 1119 | if (errors == NULL) { |
| 1120 | if (strcmp(encoding, "utf-8") == 0) |
Jeremy Hylton | 9cea41c | 2001-05-29 17:13:15 +0000 | [diff] [blame] | 1121 | return PyUnicode_AsUTF8String(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1122 | else if (strcmp(encoding, "latin-1") == 0) |
| 1123 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1124 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1125 | else if (strcmp(encoding, "mbcs") == 0) |
| 1126 | return PyUnicode_AsMBCSString(unicode); |
| 1127 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1128 | else if (strcmp(encoding, "ascii") == 0) |
| 1129 | return PyUnicode_AsASCIIString(unicode); |
| 1130 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1131 | |
| 1132 | /* Encode via the codec registry */ |
| 1133 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1134 | if (v == NULL) |
| 1135 | goto onError; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1136 | if (!PyBytes_Check(v)) { |
| 1137 | if (PyString_Check(v)) { |
| 1138 | /* Old codec, turn it into bytes */ |
| 1139 | PyObject *b = PyBytes_FromObject(v); |
| 1140 | Py_DECREF(v); |
| 1141 | return b; |
| 1142 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1143 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1144 | "encoder did not return a bytes object " |
| 1145 | "(type=%.400s, encoding=%.20s, errors=%.20s)", |
| 1146 | v->ob_type->tp_name, |
| 1147 | encoding ? encoding : "NULL", |
| 1148 | errors ? errors : "NULL"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1149 | Py_DECREF(v); |
| 1150 | goto onError; |
| 1151 | } |
| 1152 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1153 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1154 | onError: |
| 1155 | return NULL; |
| 1156 | } |
| 1157 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1158 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 1159 | const char *errors) |
| 1160 | { |
| 1161 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1162 | PyObject *b; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1163 | if (v) |
| 1164 | return v; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1165 | if (errors != NULL) |
| 1166 | Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString"); |
| 1167 | if (errors == NULL) { |
| 1168 | b = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1169 | PyUnicode_GET_SIZE(unicode), |
| 1170 | NULL); |
| 1171 | } |
| 1172 | else { |
| 1173 | b = PyUnicode_AsEncodedString(unicode, NULL, errors); |
| 1174 | } |
| 1175 | if (!b) |
| 1176 | return NULL; |
| 1177 | v = PyString_FromStringAndSize(PyBytes_AsString(b), |
| 1178 | PyBytes_Size(b)); |
| 1179 | Py_DECREF(b); |
| 1180 | if (!errors) { |
| 1181 | Py_XINCREF(v); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1182 | ((PyUnicodeObject *)unicode)->defenc = v; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1183 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1184 | return v; |
| 1185 | } |
| 1186 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1187 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1188 | { |
| 1189 | if (!PyUnicode_Check(unicode)) { |
| 1190 | PyErr_BadArgument(); |
| 1191 | goto onError; |
| 1192 | } |
| 1193 | return PyUnicode_AS_UNICODE(unicode); |
| 1194 | |
| 1195 | onError: |
| 1196 | return NULL; |
| 1197 | } |
| 1198 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1199 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1200 | { |
| 1201 | if (!PyUnicode_Check(unicode)) { |
| 1202 | PyErr_BadArgument(); |
| 1203 | goto onError; |
| 1204 | } |
| 1205 | return PyUnicode_GET_SIZE(unicode); |
| 1206 | |
| 1207 | onError: |
| 1208 | return -1; |
| 1209 | } |
| 1210 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1211 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1212 | { |
| 1213 | return unicode_default_encoding; |
| 1214 | } |
| 1215 | |
| 1216 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 1217 | { |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1218 | if (strcmp(encoding, unicode_default_encoding) != 0) { |
| 1219 | PyErr_Format(PyExc_ValueError, |
| 1220 | "Can only set default encoding to %s", |
| 1221 | unicode_default_encoding); |
| 1222 | return -1; |
| 1223 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1224 | return 0; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1227 | /* error handling callback helper: |
| 1228 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1229 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1230 | and adjust various state variables. |
| 1231 | return 0 on success, -1 on error |
| 1232 | */ |
| 1233 | |
| 1234 | static |
| 1235 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 1236 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1237 | const char *input, Py_ssize_t insize, Py_ssize_t *startinpos, Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 1238 | PyObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1239 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1240 | 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] | 1241 | |
| 1242 | PyObject *restuple = NULL; |
| 1243 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1244 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
| 1245 | Py_ssize_t requiredsize; |
| 1246 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1247 | Py_UNICODE *repptr; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1248 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1249 | int res = -1; |
| 1250 | |
| 1251 | if (*errorHandler == NULL) { |
| 1252 | *errorHandler = PyCodec_LookupError(errors); |
| 1253 | if (*errorHandler == NULL) |
| 1254 | goto onError; |
| 1255 | } |
| 1256 | |
| 1257 | if (*exceptionObject == NULL) { |
| 1258 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 1259 | encoding, input, insize, *startinpos, *endinpos, reason); |
| 1260 | if (*exceptionObject == NULL) |
| 1261 | goto onError; |
| 1262 | } |
| 1263 | else { |
| 1264 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 1265 | goto onError; |
| 1266 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 1267 | goto onError; |
| 1268 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1269 | goto onError; |
| 1270 | } |
| 1271 | |
| 1272 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1273 | if (restuple == NULL) |
| 1274 | goto onError; |
| 1275 | if (!PyTuple_Check(restuple)) { |
| 1276 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 1277 | goto onError; |
| 1278 | } |
| 1279 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 1280 | goto onError; |
| 1281 | if (newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1282 | newpos = insize+newpos; |
| 1283 | if (newpos<0 || newpos>insize) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 1284 | 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] | 1285 | goto onError; |
| 1286 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1287 | |
| 1288 | /* need more space? (at least enough for what we |
| 1289 | have+the replacement+the rest of the string (starting |
| 1290 | at the new input position), so we won't have to check space |
| 1291 | when there are no errors in the rest of the string) */ |
| 1292 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 1293 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 1294 | requiredsize = *outpos + repsize + insize-newpos; |
| 1295 | if (requiredsize > outsize) { |
| 1296 | if (requiredsize<2*outsize) |
| 1297 | requiredsize = 2*outsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1298 | if (PyUnicode_Resize(output, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1299 | goto onError; |
| 1300 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
| 1301 | } |
| 1302 | *endinpos = newpos; |
| 1303 | *inptr = input + newpos; |
| 1304 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 1305 | *outptr += repsize; |
| 1306 | *outpos += repsize; |
| 1307 | /* we made it! */ |
| 1308 | res = 0; |
| 1309 | |
| 1310 | onError: |
| 1311 | Py_XDECREF(restuple); |
| 1312 | return res; |
| 1313 | } |
| 1314 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1315 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 1316 | |
| 1317 | /* see RFC2152 for details */ |
| 1318 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1319 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1320 | char utf7_special[128] = { |
| 1321 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 1322 | encoded: |
| 1323 | 0 - not special |
| 1324 | 1 - special |
| 1325 | 2 - whitespace (optional) |
| 1326 | 3 - RFC2152 Set O (optional) */ |
| 1327 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 1328 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1329 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 1330 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 1331 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1332 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 1333 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1334 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 1335 | |
| 1336 | }; |
| 1337 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1338 | /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
| 1339 | warnings about the comparison always being false; since |
| 1340 | utf7_special[0] is 1, we can safely make that one comparison |
| 1341 | true */ |
| 1342 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1343 | #define SPECIAL(c, encodeO, encodeWS) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1344 | ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1345 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1346 | (encodeO && (utf7_special[(c)] == 3))) |
| 1347 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1348 | #define B64(n) \ |
| 1349 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 1350 | #define B64CHAR(c) \ |
| 1351 | (isalnum(c) || (c) == '+' || (c) == '/') |
| 1352 | #define UB64(c) \ |
| 1353 | ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
| 1354 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1355 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1356 | #define ENCODE(out, ch, bits) \ |
| 1357 | while (bits >= 6) { \ |
| 1358 | *out++ = B64(ch >> (bits-6)); \ |
| 1359 | bits -= 6; \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1362 | #define DECODE(out, ch, bits, surrogate) \ |
| 1363 | while (bits >= 16) { \ |
| 1364 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 1365 | bits -= 16; \ |
| 1366 | if (surrogate) { \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1367 | /* We have already generated an error for the high surrogate \ |
| 1368 | 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] | 1369 | surrogate = 0; \ |
| 1370 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1371 | /* This is a surrogate pair. Unfortunately we can't represent \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1372 | it in a 16-bit character */ \ |
| 1373 | surrogate = 1; \ |
| 1374 | errmsg = "code pairs are not supported"; \ |
| 1375 | goto utf7Error; \ |
| 1376 | } else { \ |
| 1377 | *out++ = outCh; \ |
| 1378 | } \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1379 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1380 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1381 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1382 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1383 | const char *errors) |
| 1384 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1385 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1386 | Py_ssize_t startinpos; |
| 1387 | Py_ssize_t endinpos; |
| 1388 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1389 | const char *e; |
| 1390 | PyUnicodeObject *unicode; |
| 1391 | Py_UNICODE *p; |
| 1392 | const char *errmsg = ""; |
| 1393 | int inShift = 0; |
| 1394 | unsigned int bitsleft = 0; |
| 1395 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1396 | int surrogate = 0; |
| 1397 | PyObject *errorHandler = NULL; |
| 1398 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1399 | |
| 1400 | unicode = _PyUnicode_New(size); |
| 1401 | if (!unicode) |
| 1402 | return NULL; |
| 1403 | if (size == 0) |
| 1404 | return (PyObject *)unicode; |
| 1405 | |
| 1406 | p = unicode->str; |
| 1407 | e = s + size; |
| 1408 | |
| 1409 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1410 | Py_UNICODE ch; |
| 1411 | restart: |
| 1412 | ch = *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1413 | |
| 1414 | if (inShift) { |
| 1415 | if ((ch == '-') || !B64CHAR(ch)) { |
| 1416 | inShift = 0; |
| 1417 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1418 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1419 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1420 | if (bitsleft >= 6) { |
| 1421 | /* The shift sequence has a partial character in it. If |
| 1422 | bitsleft < 6 then we could just classify it as padding |
| 1423 | but that is not the case here */ |
| 1424 | |
| 1425 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1426 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1427 | } |
| 1428 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1429 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1430 | here so indicate the potential of a misencoded character. */ |
| 1431 | |
| 1432 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 1433 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 1434 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1435 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
| 1438 | if (ch == '-') { |
| 1439 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1440 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1441 | inShift = 1; |
| 1442 | } |
| 1443 | } else if (SPECIAL(ch,0,0)) { |
| 1444 | errmsg = "unexpected special character"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1445 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1446 | } else { |
| 1447 | *p++ = ch; |
| 1448 | } |
| 1449 | } else { |
| 1450 | charsleft = (charsleft << 6) | UB64(ch); |
| 1451 | bitsleft += 6; |
| 1452 | s++; |
| 1453 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1454 | } |
| 1455 | } |
| 1456 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1457 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1458 | s++; |
| 1459 | if (s < e && *s == '-') { |
| 1460 | s++; |
| 1461 | *p++ = '+'; |
| 1462 | } else |
| 1463 | { |
| 1464 | inShift = 1; |
| 1465 | bitsleft = 0; |
| 1466 | } |
| 1467 | } |
| 1468 | else if (SPECIAL(ch,0,0)) { |
| 1469 | errmsg = "unexpected special character"; |
| 1470 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1471 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1472 | } |
| 1473 | else { |
| 1474 | *p++ = ch; |
| 1475 | s++; |
| 1476 | } |
| 1477 | continue; |
| 1478 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1479 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1480 | endinpos = s-starts; |
| 1481 | if (unicode_decode_call_errorhandler( |
| 1482 | errors, &errorHandler, |
| 1483 | "utf7", errmsg, |
| 1484 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1485 | (PyObject **)&unicode, &outpos, &p)) |
| 1486 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | if (inShift) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1490 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1491 | endinpos = size; |
| 1492 | if (unicode_decode_call_errorhandler( |
| 1493 | errors, &errorHandler, |
| 1494 | "utf7", "unterminated shift sequence", |
| 1495 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1496 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1497 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1498 | if (s < e) |
| 1499 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1502 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1503 | goto onError; |
| 1504 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1505 | Py_XDECREF(errorHandler); |
| 1506 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1507 | return (PyObject *)unicode; |
| 1508 | |
| 1509 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1510 | Py_XDECREF(errorHandler); |
| 1511 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1512 | Py_DECREF(unicode); |
| 1513 | return NULL; |
| 1514 | } |
| 1515 | |
| 1516 | |
| 1517 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1518 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1519 | int encodeSetO, |
| 1520 | int encodeWhiteSpace, |
| 1521 | const char *errors) |
| 1522 | { |
| 1523 | PyObject *v; |
| 1524 | /* It might be possible to tighten this worst case */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1525 | Py_ssize_t cbAllocated = 5 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1526 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1527 | Py_ssize_t i = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1528 | unsigned int bitsleft = 0; |
| 1529 | unsigned long charsleft = 0; |
| 1530 | char * out; |
| 1531 | char * start; |
| 1532 | |
| 1533 | if (size == 0) |
Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1534 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1535 | |
Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1536 | v = PyBytes_FromStringAndSize(NULL, cbAllocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1537 | if (v == NULL) |
| 1538 | return NULL; |
| 1539 | |
Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1540 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1541 | for (;i < size; ++i) { |
| 1542 | Py_UNICODE ch = s[i]; |
| 1543 | |
| 1544 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1545 | if (ch == '+') { |
| 1546 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1547 | *out++ = '-'; |
| 1548 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1549 | charsleft = ch; |
| 1550 | bitsleft = 16; |
| 1551 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1552 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1553 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1554 | } else { |
| 1555 | *out++ = (char) ch; |
| 1556 | } |
| 1557 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1558 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1559 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1560 | charsleft = 0; |
| 1561 | bitsleft = 0; |
| 1562 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1563 | so no '-' is required, except if the character is itself a '-' */ |
| 1564 | if (B64CHAR(ch) || ch == '-') { |
| 1565 | *out++ = '-'; |
| 1566 | } |
| 1567 | inShift = 0; |
| 1568 | *out++ = (char) ch; |
| 1569 | } else { |
| 1570 | bitsleft += 16; |
| 1571 | charsleft = (charsleft << 16) | ch; |
| 1572 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1573 | |
| 1574 | /* If the next character is special then we dont' need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1575 | 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] | 1576 | or '-' then the shift sequence will be terminated implicitly and we |
| 1577 | don't have to insert a '-'. */ |
| 1578 | |
| 1579 | if (bitsleft == 0) { |
| 1580 | if (i + 1 < size) { |
| 1581 | Py_UNICODE ch2 = s[i+1]; |
| 1582 | |
| 1583 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1584 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1585 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1586 | *out++ = '-'; |
| 1587 | inShift = 0; |
| 1588 | } else { |
| 1589 | inShift = 0; |
| 1590 | } |
| 1591 | |
| 1592 | } |
| 1593 | else { |
| 1594 | *out++ = '-'; |
| 1595 | inShift = 0; |
| 1596 | } |
| 1597 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1598 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1599 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1600 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1601 | if (bitsleft) { |
| 1602 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1603 | *out++ = '-'; |
| 1604 | } |
| 1605 | |
Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1606 | if (PyBytes_Resize(v, out - start)) { |
| 1607 | Py_DECREF(v); |
| 1608 | return NULL; |
| 1609 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1610 | return v; |
| 1611 | } |
| 1612 | |
| 1613 | #undef SPECIAL |
| 1614 | #undef B64 |
| 1615 | #undef B64CHAR |
| 1616 | #undef UB64 |
| 1617 | #undef ENCODE |
| 1618 | #undef DECODE |
| 1619 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1620 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1621 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1622 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1623 | char utf8_code_length[256] = { |
| 1624 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1625 | illegal prefix. see RFC 2279 for details */ |
| 1626 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1627 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1628 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1629 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1630 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1631 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1632 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1633 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1634 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1635 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1636 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1637 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1638 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1639 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1640 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1641 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1642 | }; |
| 1643 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1644 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1645 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1646 | const char *errors) |
| 1647 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1648 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 1649 | } |
| 1650 | |
| 1651 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1652 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1653 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1654 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1655 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1656 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1657 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1658 | Py_ssize_t startinpos; |
| 1659 | Py_ssize_t endinpos; |
| 1660 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1661 | const char *e; |
| 1662 | PyUnicodeObject *unicode; |
| 1663 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1664 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1665 | PyObject *errorHandler = NULL; |
| 1666 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1667 | |
| 1668 | /* Note: size will always be longer than the resulting Unicode |
| 1669 | character count */ |
| 1670 | unicode = _PyUnicode_New(size); |
| 1671 | if (!unicode) |
| 1672 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1673 | if (size == 0) { |
| 1674 | if (consumed) |
| 1675 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1676 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1677 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1678 | |
| 1679 | /* Unpack UTF-8 encoded data */ |
| 1680 | p = unicode->str; |
| 1681 | e = s + size; |
| 1682 | |
| 1683 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1684 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1685 | |
| 1686 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1687 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1688 | s++; |
| 1689 | continue; |
| 1690 | } |
| 1691 | |
| 1692 | n = utf8_code_length[ch]; |
| 1693 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1694 | if (s + n > e) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1695 | if (consumed) |
| 1696 | break; |
| 1697 | else { |
| 1698 | errmsg = "unexpected end of data"; |
| 1699 | startinpos = s-starts; |
| 1700 | endinpos = size; |
| 1701 | goto utf8Error; |
| 1702 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1703 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1704 | |
| 1705 | switch (n) { |
| 1706 | |
| 1707 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1708 | errmsg = "unexpected code byte"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1709 | startinpos = s-starts; |
| 1710 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1711 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1712 | |
| 1713 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1714 | errmsg = "internal error"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1715 | startinpos = s-starts; |
| 1716 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1717 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1718 | |
| 1719 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1720 | if ((s[1] & 0xc0) != 0x80) { |
| 1721 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1722 | startinpos = s-starts; |
| 1723 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1724 | goto utf8Error; |
| 1725 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1726 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1727 | if (ch < 0x80) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1728 | startinpos = s-starts; |
| 1729 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1730 | errmsg = "illegal encoding"; |
| 1731 | goto utf8Error; |
| 1732 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1733 | else |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1734 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1735 | break; |
| 1736 | |
| 1737 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1738 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1739 | (s[2] & 0xc0) != 0x80) { |
| 1740 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1741 | startinpos = s-starts; |
| 1742 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1743 | goto utf8Error; |
| 1744 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1745 | 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] | 1746 | if (ch < 0x0800) { |
| 1747 | /* Note: UTF-8 encodings of surrogates are considered |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1748 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1749 | |
| 1750 | XXX For wide builds (UCS-4) we should probably try |
| 1751 | to recombine the surrogates into a single code |
| 1752 | unit. |
| 1753 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1754 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1755 | startinpos = s-starts; |
| 1756 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1757 | goto utf8Error; |
| 1758 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1759 | else |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1760 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1761 | break; |
| 1762 | |
| 1763 | case 4: |
| 1764 | if ((s[1] & 0xc0) != 0x80 || |
| 1765 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1766 | (s[3] & 0xc0) != 0x80) { |
| 1767 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1768 | startinpos = s-starts; |
| 1769 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1770 | goto utf8Error; |
| 1771 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1772 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 1773 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 1774 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1775 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1776 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1777 | || (ch > 0x10ffff)) /* maximum value allowed for |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1778 | UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1779 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1780 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1781 | startinpos = s-starts; |
| 1782 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1783 | goto utf8Error; |
| 1784 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1785 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1786 | *p++ = (Py_UNICODE)ch; |
| 1787 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1788 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1789 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1790 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 1791 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1792 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1793 | /* high surrogate = top 10 bits added to D800 */ |
| 1794 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1795 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1796 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1797 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1798 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1799 | break; |
| 1800 | |
| 1801 | default: |
| 1802 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1803 | errmsg = "unsupported Unicode code range"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1804 | startinpos = s-starts; |
| 1805 | endinpos = startinpos+n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1806 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1807 | } |
| 1808 | s += n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1809 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1810 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1811 | utf8Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1812 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1813 | if (unicode_decode_call_errorhandler( |
| 1814 | errors, &errorHandler, |
| 1815 | "utf8", errmsg, |
| 1816 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1817 | (PyObject **)&unicode, &outpos, &p)) |
| 1818 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1819 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1820 | if (consumed) |
| 1821 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1822 | |
| 1823 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1824 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1825 | goto onError; |
| 1826 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1827 | Py_XDECREF(errorHandler); |
| 1828 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1829 | return (PyObject *)unicode; |
| 1830 | |
| 1831 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1832 | Py_XDECREF(errorHandler); |
| 1833 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1834 | Py_DECREF(unicode); |
| 1835 | return NULL; |
| 1836 | } |
| 1837 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1838 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 1839 | and allocate exactly as much space needed at the end. Else allocate the |
| 1840 | maximum possible needed (4 result bytes per Unicode character), and return |
| 1841 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1842 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1843 | PyObject * |
| 1844 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1845 | Py_ssize_t size, |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1846 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1847 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1848 | #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] | 1849 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1850 | Py_ssize_t i; /* index into s of next input byte */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1851 | PyObject *v; /* result string object */ |
| 1852 | char *p; /* next free byte in output buffer */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1853 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1854 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1855 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1856 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1857 | assert(s != NULL); |
| 1858 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1859 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1860 | if (size <= MAX_SHORT_UNICHARS) { |
| 1861 | /* Write into the stack buffer; nallocated can't overflow. |
| 1862 | * At the end, we'll allocate exactly as much heap space as it |
| 1863 | * turns out we need. |
| 1864 | */ |
| 1865 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
| 1866 | v = NULL; /* will allocate after we're done */ |
| 1867 | p = stackbuf; |
| 1868 | } |
| 1869 | else { |
| 1870 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 1871 | nallocated = size * 4; |
| 1872 | if (nallocated / 4 != size) /* overflow! */ |
| 1873 | return PyErr_NoMemory(); |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1874 | v = PyBytes_FromStringAndSize(NULL, nallocated); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1875 | if (v == NULL) |
| 1876 | return NULL; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1877 | p = PyBytes_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1878 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1879 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1880 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1881 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1882 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1883 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1884 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1885 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1886 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1887 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1888 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 1889 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 1890 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1891 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1892 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1893 | /* Encode UCS2 Unicode ordinals */ |
| 1894 | if (ch < 0x10000) { |
| 1895 | /* Special case: check for high surrogate */ |
| 1896 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 1897 | Py_UCS4 ch2 = s[i]; |
| 1898 | /* Check for low surrogate and combine the two to |
| 1899 | form a UCS4 value */ |
| 1900 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1901 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1902 | i++; |
| 1903 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1904 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1905 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1906 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1907 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1908 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1909 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1910 | continue; |
| 1911 | } |
| 1912 | encodeUCS4: |
| 1913 | /* Encode UCS4 Unicode ordinals */ |
| 1914 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 1915 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 1916 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1917 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1918 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1919 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 1920 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1921 | if (v == NULL) { |
| 1922 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1923 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1924 | assert(nneeded <= nallocated); |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1925 | v = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1926 | } |
| 1927 | else { |
| 1928 | /* Cut back to size actually needed. */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1929 | nneeded = p - PyBytes_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1930 | assert(nneeded <= nallocated); |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1931 | PyBytes_Resize(v, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1932 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1933 | return v; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1934 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1935 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1938 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 1939 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1940 | if (!PyUnicode_Check(unicode)) { |
| 1941 | PyErr_BadArgument(); |
| 1942 | return NULL; |
| 1943 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 1944 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1945 | PyUnicode_GET_SIZE(unicode), |
| 1946 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1947 | } |
| 1948 | |
| 1949 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 1950 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1951 | PyObject * |
| 1952 | PyUnicode_DecodeUTF16(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1953 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1954 | const char *errors, |
| 1955 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1956 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1957 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 1958 | } |
| 1959 | |
| 1960 | PyObject * |
| 1961 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1962 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1963 | const char *errors, |
| 1964 | int *byteorder, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1965 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1966 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1967 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1968 | Py_ssize_t startinpos; |
| 1969 | Py_ssize_t endinpos; |
| 1970 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1971 | PyUnicodeObject *unicode; |
| 1972 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1973 | const unsigned char *q, *e; |
| 1974 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1975 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1976 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 1977 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1978 | int ihi = 1, ilo = 0; |
| 1979 | #else |
| 1980 | int ihi = 0, ilo = 1; |
| 1981 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1982 | PyObject *errorHandler = NULL; |
| 1983 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1984 | |
| 1985 | /* Note: size will always be longer than the resulting Unicode |
| 1986 | character count */ |
| 1987 | unicode = _PyUnicode_New(size); |
| 1988 | if (!unicode) |
| 1989 | return NULL; |
| 1990 | if (size == 0) |
| 1991 | return (PyObject *)unicode; |
| 1992 | |
| 1993 | /* Unpack UTF-16 encoded data */ |
| 1994 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1995 | q = (unsigned char *)s; |
| 1996 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1997 | |
| 1998 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1999 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2000 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2001 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2002 | byte order setting accordingly. In native mode, the leading BOM |
| 2003 | mark is skipped, in all other modes, it is copied to the output |
| 2004 | stream as-is (giving a ZWNBSP character). */ |
| 2005 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2006 | if (size >= 2) { |
| 2007 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2008 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2009 | if (bom == 0xFEFF) { |
| 2010 | q += 2; |
| 2011 | bo = -1; |
| 2012 | } |
| 2013 | else if (bom == 0xFFFE) { |
| 2014 | q += 2; |
| 2015 | bo = 1; |
| 2016 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2017 | #else |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2018 | if (bom == 0xFEFF) { |
| 2019 | q += 2; |
| 2020 | bo = 1; |
| 2021 | } |
| 2022 | else if (bom == 0xFFFE) { |
| 2023 | q += 2; |
| 2024 | bo = -1; |
| 2025 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2026 | #endif |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2027 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2028 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2029 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2030 | if (bo == -1) { |
| 2031 | /* force LE */ |
| 2032 | ihi = 1; |
| 2033 | ilo = 0; |
| 2034 | } |
| 2035 | else if (bo == 1) { |
| 2036 | /* force BE */ |
| 2037 | ihi = 0; |
| 2038 | ilo = 1; |
| 2039 | } |
| 2040 | |
| 2041 | while (q < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2042 | Py_UNICODE ch; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2043 | /* remaining bytes at the end? (size should be even) */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2044 | if (e-q<2) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2045 | if (consumed) |
| 2046 | break; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2047 | errmsg = "truncated data"; |
| 2048 | startinpos = ((const char *)q)-starts; |
| 2049 | endinpos = ((const char *)e)-starts; |
| 2050 | goto utf16Error; |
| 2051 | /* The remaining input chars are ignored if the callback |
| 2052 | chooses to skip the input */ |
| 2053 | } |
| 2054 | ch = (q[ihi] << 8) | q[ilo]; |
| 2055 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2056 | q += 2; |
| 2057 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2058 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 2059 | *p++ = ch; |
| 2060 | continue; |
| 2061 | } |
| 2062 | |
| 2063 | /* UTF-16 code pair: */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2064 | if (q >= e) { |
| 2065 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2066 | startinpos = (((const char *)q)-2)-starts; |
| 2067 | endinpos = ((const char *)e)-starts; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2068 | goto utf16Error; |
| 2069 | } |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2070 | if (0xD800 <= ch && ch <= 0xDBFF) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2071 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 2072 | q += 2; |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2073 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2074 | #ifndef Py_UNICODE_WIDE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2075 | *p++ = ch; |
| 2076 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2077 | #else |
| 2078 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2079 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2080 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2081 | } |
| 2082 | else { |
| 2083 | errmsg = "illegal UTF-16 surrogate"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2084 | startinpos = (((const char *)q)-4)-starts; |
| 2085 | endinpos = startinpos+2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2086 | goto utf16Error; |
| 2087 | } |
| 2088 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2089 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2090 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2091 | startinpos = (((const char *)q)-2)-starts; |
| 2092 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2093 | /* Fall through to report the error */ |
| 2094 | |
| 2095 | utf16Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2096 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2097 | if (unicode_decode_call_errorhandler( |
| 2098 | errors, &errorHandler, |
| 2099 | "utf16", errmsg, |
| 2100 | starts, size, &startinpos, &endinpos, &exc, (const char **)&q, |
| 2101 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2102 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2103 | } |
| 2104 | |
| 2105 | if (byteorder) |
| 2106 | *byteorder = bo; |
| 2107 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2108 | if (consumed) |
| 2109 | *consumed = (const char *)q-starts; |
| 2110 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2111 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2112 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2113 | goto onError; |
| 2114 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2115 | Py_XDECREF(errorHandler); |
| 2116 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2117 | return (PyObject *)unicode; |
| 2118 | |
| 2119 | onError: |
| 2120 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2121 | Py_XDECREF(errorHandler); |
| 2122 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2123 | return NULL; |
| 2124 | } |
| 2125 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2126 | PyObject * |
| 2127 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2128 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2129 | const char *errors, |
| 2130 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2131 | { |
| 2132 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2133 | unsigned char *p; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2134 | #ifdef Py_UNICODE_WIDE |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2135 | int i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2136 | #else |
| 2137 | const int pairs = 0; |
| 2138 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2139 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2140 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2141 | int ihi = 1, ilo = 0; |
| 2142 | #else |
| 2143 | int ihi = 0, ilo = 1; |
| 2144 | #endif |
| 2145 | |
| 2146 | #define STORECHAR(CH) \ |
| 2147 | do { \ |
| 2148 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 2149 | p[ilo] = (CH) & 0xff; \ |
| 2150 | p += 2; \ |
| 2151 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2152 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2153 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2154 | for (i = pairs = 0; i < size; i++) |
| 2155 | if (s[i] >= 0x10000) |
| 2156 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2157 | #endif |
Walter Dörwald | 3cc3452 | 2007-05-04 10:48:27 +0000 | [diff] [blame] | 2158 | v = PyBytes_FromStringAndSize(NULL, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2159 | 2 * (size + pairs + (byteorder == 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2160 | if (v == NULL) |
| 2161 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2162 | |
Walter Dörwald | 3cc3452 | 2007-05-04 10:48:27 +0000 | [diff] [blame] | 2163 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2164 | if (byteorder == 0) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2165 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2166 | if (size == 0) |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2167 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2168 | |
| 2169 | if (byteorder == -1) { |
| 2170 | /* force LE */ |
| 2171 | ihi = 1; |
| 2172 | ilo = 0; |
| 2173 | } |
| 2174 | else if (byteorder == 1) { |
| 2175 | /* force BE */ |
| 2176 | ihi = 0; |
| 2177 | ilo = 1; |
| 2178 | } |
| 2179 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2180 | while (size-- > 0) { |
| 2181 | Py_UNICODE ch = *s++; |
| 2182 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2183 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2184 | if (ch >= 0x10000) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2185 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2186 | ch = 0xD800 | ((ch-0x10000) >> 10); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2187 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2188 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2189 | STORECHAR(ch); |
| 2190 | if (ch2) |
| 2191 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2192 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2193 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2194 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2195 | } |
| 2196 | |
| 2197 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 2198 | { |
| 2199 | if (!PyUnicode_Check(unicode)) { |
| 2200 | PyErr_BadArgument(); |
| 2201 | return NULL; |
| 2202 | } |
| 2203 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 2204 | PyUnicode_GET_SIZE(unicode), |
| 2205 | NULL, |
| 2206 | 0); |
| 2207 | } |
| 2208 | |
| 2209 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 2210 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2211 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 2212 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2213 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2214 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2215 | const char *errors) |
| 2216 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2217 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2218 | Py_ssize_t startinpos; |
| 2219 | Py_ssize_t endinpos; |
| 2220 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2221 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2222 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2223 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2224 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2225 | char* message; |
| 2226 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2227 | PyObject *errorHandler = NULL; |
| 2228 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2229 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2230 | /* Escaped strings will always be longer than the resulting |
| 2231 | 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] | 2232 | length after conversion to the true value. |
| 2233 | (but if the error callback returns a long replacement string |
| 2234 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2235 | v = _PyUnicode_New(size); |
| 2236 | if (v == NULL) |
| 2237 | goto onError; |
| 2238 | if (size == 0) |
| 2239 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2240 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2241 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2242 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2243 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2244 | while (s < end) { |
| 2245 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2246 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2247 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2248 | |
| 2249 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2250 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2251 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2252 | continue; |
| 2253 | } |
| 2254 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2255 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2256 | /* \ - Escapes */ |
| 2257 | s++; |
| 2258 | switch (*s++) { |
| 2259 | |
| 2260 | /* \x escapes */ |
| 2261 | case '\n': break; |
| 2262 | case '\\': *p++ = '\\'; break; |
| 2263 | case '\'': *p++ = '\''; break; |
| 2264 | case '\"': *p++ = '\"'; break; |
| 2265 | case 'b': *p++ = '\b'; break; |
| 2266 | case 'f': *p++ = '\014'; break; /* FF */ |
| 2267 | case 't': *p++ = '\t'; break; |
| 2268 | case 'n': *p++ = '\n'; break; |
| 2269 | case 'r': *p++ = '\r'; break; |
| 2270 | case 'v': *p++ = '\013'; break; /* VT */ |
| 2271 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 2272 | |
| 2273 | /* \OOO (octal) escapes */ |
| 2274 | case '0': case '1': case '2': case '3': |
| 2275 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2276 | x = s[-1] - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2277 | if ('0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2278 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2279 | if ('0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2280 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2281 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2282 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2283 | break; |
| 2284 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2285 | /* hex escapes */ |
| 2286 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2287 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2288 | digits = 2; |
| 2289 | message = "truncated \\xXX escape"; |
| 2290 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2291 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2292 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2293 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2294 | digits = 4; |
| 2295 | message = "truncated \\uXXXX escape"; |
| 2296 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2297 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2298 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2299 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2300 | digits = 8; |
| 2301 | message = "truncated \\UXXXXXXXX escape"; |
| 2302 | hexescape: |
| 2303 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2304 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2305 | if (s+digits>end) { |
| 2306 | endinpos = size; |
| 2307 | if (unicode_decode_call_errorhandler( |
| 2308 | errors, &errorHandler, |
| 2309 | "unicodeescape", "end of string in escape sequence", |
| 2310 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2311 | (PyObject **)&v, &outpos, &p)) |
| 2312 | goto onError; |
| 2313 | goto nextByte; |
| 2314 | } |
| 2315 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2316 | c = (unsigned char) s[i]; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2317 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2318 | endinpos = (s+i+1)-starts; |
| 2319 | if (unicode_decode_call_errorhandler( |
| 2320 | errors, &errorHandler, |
| 2321 | "unicodeescape", message, |
| 2322 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2323 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2324 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2325 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2326 | } |
| 2327 | chr = (chr<<4) & ~0xF; |
| 2328 | if (c >= '0' && c <= '9') |
| 2329 | chr += c - '0'; |
| 2330 | else if (c >= 'a' && c <= 'f') |
| 2331 | chr += 10 + c - 'a'; |
| 2332 | else |
| 2333 | chr += 10 + c - 'A'; |
| 2334 | } |
| 2335 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 2336 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2337 | /* _decoding_error will have already written into the |
| 2338 | target buffer. */ |
| 2339 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2340 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2341 | /* when we get here, chr is a 32-bit unicode character */ |
| 2342 | if (chr <= 0xffff) |
| 2343 | /* UCS-2 character */ |
| 2344 | *p++ = (Py_UNICODE) chr; |
| 2345 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2346 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2347 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2348 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2349 | *p++ = chr; |
| 2350 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2351 | chr -= 0x10000L; |
| 2352 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2353 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2354 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2355 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2356 | endinpos = s-starts; |
| 2357 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2358 | if (unicode_decode_call_errorhandler( |
| 2359 | errors, &errorHandler, |
| 2360 | "unicodeescape", "illegal Unicode character", |
| 2361 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2362 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2363 | goto onError; |
| 2364 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2365 | break; |
| 2366 | |
| 2367 | /* \N{name} */ |
| 2368 | case 'N': |
| 2369 | message = "malformed \\N character escape"; |
| 2370 | if (ucnhash_CAPI == NULL) { |
| 2371 | /* load the unicode data module */ |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2372 | PyObject *m, *api; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2373 | m = PyImport_ImportModule("unicodedata"); |
| 2374 | if (m == NULL) |
| 2375 | goto ucnhashError; |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2376 | api = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2377 | Py_DECREF(m); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2378 | if (api == NULL) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2379 | goto ucnhashError; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2380 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2381 | Py_DECREF(api); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2382 | if (ucnhash_CAPI == NULL) |
| 2383 | goto ucnhashError; |
| 2384 | } |
| 2385 | if (*s == '{') { |
| 2386 | const char *start = s+1; |
| 2387 | /* look for the closing brace */ |
| 2388 | while (*s != '}' && s < end) |
| 2389 | s++; |
| 2390 | if (s > start && s < end && *s == '}') { |
| 2391 | /* found a name. look it up in the unicode database */ |
| 2392 | message = "unknown Unicode character name"; |
| 2393 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 2394 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2395 | goto store; |
| 2396 | } |
| 2397 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2398 | endinpos = s-starts; |
| 2399 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2400 | if (unicode_decode_call_errorhandler( |
| 2401 | errors, &errorHandler, |
| 2402 | "unicodeescape", message, |
| 2403 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2404 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2405 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2406 | break; |
| 2407 | |
| 2408 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2409 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2410 | message = "\\ at end of string"; |
| 2411 | s--; |
| 2412 | endinpos = s-starts; |
| 2413 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2414 | if (unicode_decode_call_errorhandler( |
| 2415 | errors, &errorHandler, |
| 2416 | "unicodeescape", message, |
| 2417 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2418 | (PyObject **)&v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2419 | goto onError; |
| 2420 | } |
| 2421 | else { |
| 2422 | *p++ = '\\'; |
| 2423 | *p++ = (unsigned char)s[-1]; |
| 2424 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2425 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2426 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2427 | nextByte: |
| 2428 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2429 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2430 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2431 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 2432 | Py_XDECREF(errorHandler); |
| 2433 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2434 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2435 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2436 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2437 | PyErr_SetString( |
| 2438 | PyExc_UnicodeError, |
| 2439 | "\\N escapes not supported (can't load unicodedata module)" |
| 2440 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2441 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2442 | Py_XDECREF(errorHandler); |
| 2443 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 2444 | return NULL; |
| 2445 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2446 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2447 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2448 | Py_XDECREF(errorHandler); |
| 2449 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2450 | return NULL; |
| 2451 | } |
| 2452 | |
| 2453 | /* Return a Unicode-Escape string version of the Unicode object. |
| 2454 | |
| 2455 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 2456 | appropriate. |
| 2457 | |
| 2458 | */ |
| 2459 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2460 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
| 2461 | Py_ssize_t size, |
| 2462 | Py_UNICODE ch) |
| 2463 | { |
| 2464 | /* like wcschr, but doesn't stop at NULL characters */ |
| 2465 | |
| 2466 | while (size-- > 0) { |
| 2467 | if (*s == ch) |
| 2468 | return s; |
| 2469 | s++; |
| 2470 | } |
| 2471 | |
| 2472 | return NULL; |
| 2473 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 2474 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2475 | static const char *hexdigits = "0123456789abcdef"; |
| 2476 | |
| 2477 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 2478 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2479 | { |
| 2480 | PyObject *repr; |
| 2481 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2482 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2483 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 2484 | better to choose a different scheme. Perhaps scan the |
| 2485 | first N-chars of the string and allocate based on that size. |
| 2486 | */ |
| 2487 | /* Initial allocation is based on the longest-possible unichr |
| 2488 | escape. |
| 2489 | |
| 2490 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 2491 | unichr, so in this case it's the longest unichr escape. In |
| 2492 | narrow (UTF-16) builds this is five chars per source unichr |
| 2493 | since there are two unichrs in the surrogate pair, so in narrow |
| 2494 | (UTF-16) builds it's not the longest unichr escape. |
| 2495 | |
| 2496 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 2497 | so in the narrow (UTF-16) build case it's the longest unichr |
| 2498 | escape. |
| 2499 | */ |
| 2500 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2501 | repr = PyBytes_FromStringAndSize(NULL, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2502 | #ifdef Py_UNICODE_WIDE |
| 2503 | + 10*size |
| 2504 | #else |
| 2505 | + 6*size |
| 2506 | #endif |
| 2507 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2508 | if (repr == NULL) |
| 2509 | return NULL; |
| 2510 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2511 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2512 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2513 | while (size-- > 0) { |
| 2514 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2515 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2516 | /* Escape backslashes */ |
| 2517 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2518 | *p++ = '\\'; |
| 2519 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2520 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2521 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2522 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 2523 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2524 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 2525 | else if (ch >= 0x10000) { |
| 2526 | *p++ = '\\'; |
| 2527 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2528 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 2529 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 2530 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 2531 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 2532 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 2533 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 2534 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 2535 | *p++ = hexdigits[ch & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2536 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2537 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2538 | #else |
| 2539 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2540 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 2541 | Py_UNICODE ch2; |
| 2542 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2543 | |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2544 | ch2 = *s++; |
| 2545 | size--; |
| 2546 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 2547 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 2548 | *p++ = '\\'; |
| 2549 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2550 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 2551 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 2552 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 2553 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 2554 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 2555 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 2556 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 2557 | *p++ = hexdigits[ucs & 0x0000000F]; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2558 | continue; |
| 2559 | } |
| 2560 | /* Fall through: isolated surrogates are copied as-is */ |
| 2561 | s--; |
| 2562 | size++; |
| 2563 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2564 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2565 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2566 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2567 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2568 | *p++ = '\\'; |
| 2569 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2570 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 2571 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 2572 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 2573 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2574 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2575 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2576 | /* Map special whitespace to '\t', \n', '\r' */ |
| 2577 | else if (ch == '\t') { |
| 2578 | *p++ = '\\'; |
| 2579 | *p++ = 't'; |
| 2580 | } |
| 2581 | else if (ch == '\n') { |
| 2582 | *p++ = '\\'; |
| 2583 | *p++ = 'n'; |
| 2584 | } |
| 2585 | else if (ch == '\r') { |
| 2586 | *p++ = '\\'; |
| 2587 | *p++ = 'r'; |
| 2588 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2589 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2590 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 2591 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2592 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2593 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2594 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 2595 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2596 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2597 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2598 | /* Copy everything else as-is */ |
| 2599 | else |
| 2600 | *p++ = (char) ch; |
| 2601 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2602 | |
| 2603 | *p = '\0'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2604 | if (PyBytes_Resize(repr, p - PyBytes_AS_STRING(repr))) { |
| 2605 | Py_DECREF(repr); |
| 2606 | return NULL; |
| 2607 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2608 | return repr; |
| 2609 | } |
| 2610 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2611 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 2612 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2613 | PyObject *s, *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2614 | if (!PyUnicode_Check(unicode)) { |
| 2615 | PyErr_BadArgument(); |
| 2616 | return NULL; |
| 2617 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2618 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2619 | PyUnicode_GET_SIZE(unicode)); |
| 2620 | |
| 2621 | if (!s) |
| 2622 | return NULL; |
| 2623 | result = PyString_FromStringAndSize(PyBytes_AS_STRING(s), |
| 2624 | PyBytes_GET_SIZE(s)); |
| 2625 | Py_DECREF(s); |
| 2626 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2627 | } |
| 2628 | |
| 2629 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 2630 | |
| 2631 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2632 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2633 | const char *errors) |
| 2634 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2635 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2636 | Py_ssize_t startinpos; |
| 2637 | Py_ssize_t endinpos; |
| 2638 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2639 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2640 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2641 | const char *end; |
| 2642 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2643 | PyObject *errorHandler = NULL; |
| 2644 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2645 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2646 | /* Escaped strings will always be longer than the resulting |
| 2647 | 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] | 2648 | length after conversion to the true value. (But decoding error |
| 2649 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2650 | v = _PyUnicode_New(size); |
| 2651 | if (v == NULL) |
| 2652 | goto onError; |
| 2653 | if (size == 0) |
| 2654 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2655 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2656 | end = s + size; |
| 2657 | while (s < end) { |
| 2658 | unsigned char c; |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 2659 | Py_UCS4 x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2660 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2661 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2662 | |
| 2663 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2664 | if (*s != '\\') { |
| 2665 | *p++ = (unsigned char)*s++; |
| 2666 | continue; |
| 2667 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2668 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2669 | |
| 2670 | /* \u-escapes are only interpreted iff the number of leading |
| 2671 | backslashes if odd */ |
| 2672 | bs = s; |
| 2673 | for (;s < end;) { |
| 2674 | if (*s != '\\') |
| 2675 | break; |
| 2676 | *p++ = (unsigned char)*s++; |
| 2677 | } |
| 2678 | if (((s - bs) & 1) == 0 || |
| 2679 | s >= end || |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2680 | (*s != 'u' && *s != 'U')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2681 | continue; |
| 2682 | } |
| 2683 | p--; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2684 | count = *s=='u' ? 4 : 8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2685 | s++; |
| 2686 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2687 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2688 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2689 | for (x = 0, i = 0; i < count; ++i, ++s) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2690 | c = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2691 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2692 | endinpos = s-starts; |
| 2693 | if (unicode_decode_call_errorhandler( |
| 2694 | errors, &errorHandler, |
| 2695 | "rawunicodeescape", "truncated \\uXXXX", |
| 2696 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2697 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2698 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2699 | goto nextByte; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2700 | } |
| 2701 | x = (x<<4) & ~0xF; |
| 2702 | if (c >= '0' && c <= '9') |
| 2703 | x += c - '0'; |
| 2704 | else if (c >= 'a' && c <= 'f') |
| 2705 | x += 10 + c - 'a'; |
| 2706 | else |
| 2707 | x += 10 + c - 'A'; |
| 2708 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2709 | #ifndef Py_UNICODE_WIDE |
| 2710 | if (x > 0x10000) { |
| 2711 | if (unicode_decode_call_errorhandler( |
| 2712 | errors, &errorHandler, |
| 2713 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
| 2714 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2715 | (PyObject **)&v, &outpos, &p)) |
| 2716 | goto onError; |
| 2717 | } |
| 2718 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2719 | *p++ = x; |
| 2720 | nextByte: |
| 2721 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2722 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2723 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2724 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2725 | Py_XDECREF(errorHandler); |
| 2726 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2727 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2728 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2729 | onError: |
| 2730 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2731 | Py_XDECREF(errorHandler); |
| 2732 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2733 | return NULL; |
| 2734 | } |
| 2735 | |
| 2736 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2737 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2738 | { |
| 2739 | PyObject *repr; |
| 2740 | char *p; |
| 2741 | char *q; |
| 2742 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2743 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 2744 | repr = PyBytes_FromStringAndSize(NULL, 10 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2745 | #else |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 2746 | repr = PyBytes_FromStringAndSize(NULL, 6 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2747 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2748 | if (repr == NULL) |
| 2749 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2750 | if (size == 0) |
| 2751 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2752 | |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 2753 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2754 | while (size-- > 0) { |
| 2755 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2756 | #ifdef Py_UNICODE_WIDE |
| 2757 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2758 | if (ch >= 0x10000) { |
| 2759 | *p++ = '\\'; |
| 2760 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 2761 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 2762 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 2763 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 2764 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 2765 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 2766 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 2767 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 2768 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2769 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2770 | else |
| 2771 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2772 | /* Map 16-bit characters to '\uxxxx' */ |
| 2773 | if (ch >= 256) { |
| 2774 | *p++ = '\\'; |
| 2775 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 2776 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 2777 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 2778 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 2779 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2780 | } |
| 2781 | /* Copy everything else as-is */ |
| 2782 | else |
| 2783 | *p++ = (char) ch; |
| 2784 | } |
| 2785 | *p = '\0'; |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 2786 | if (PyBytes_Resize(repr, p - q)) { |
| 2787 | Py_DECREF(repr); |
| 2788 | return NULL; |
| 2789 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2790 | return repr; |
| 2791 | } |
| 2792 | |
| 2793 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 2794 | { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 2795 | PyObject *s, *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2796 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 2797 | PyErr_BadArgument(); |
| 2798 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2799 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 2800 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2801 | PyUnicode_GET_SIZE(unicode)); |
| 2802 | |
| 2803 | if (!s) |
| 2804 | return NULL; |
| 2805 | result = PyString_FromStringAndSize(PyBytes_AS_STRING(s), |
| 2806 | PyBytes_GET_SIZE(s)); |
| 2807 | Py_DECREF(s); |
| 2808 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2809 | } |
| 2810 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2811 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 2812 | |
| 2813 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2814 | Py_ssize_t size, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2815 | const char *errors) |
| 2816 | { |
| 2817 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2818 | Py_ssize_t startinpos; |
| 2819 | Py_ssize_t endinpos; |
| 2820 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2821 | PyUnicodeObject *v; |
| 2822 | Py_UNICODE *p; |
| 2823 | const char *end; |
| 2824 | const char *reason; |
| 2825 | PyObject *errorHandler = NULL; |
| 2826 | PyObject *exc = NULL; |
| 2827 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 2828 | #ifdef Py_UNICODE_WIDE |
| 2829 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 2830 | #endif |
| 2831 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2832 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2833 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 2834 | if (v == NULL) |
| 2835 | goto onError; |
| 2836 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
| 2837 | return (PyObject *)v; |
| 2838 | p = PyUnicode_AS_UNICODE(v); |
| 2839 | end = s + size; |
| 2840 | |
| 2841 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2842 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2843 | /* We have to sanity check the raw data, otherwise doom looms for |
| 2844 | some malformed UCS-4 data. */ |
| 2845 | if ( |
| 2846 | #ifdef Py_UNICODE_WIDE |
| 2847 | *p > unimax || *p < 0 || |
| 2848 | #endif |
| 2849 | end-s < Py_UNICODE_SIZE |
| 2850 | ) |
| 2851 | { |
| 2852 | startinpos = s - starts; |
| 2853 | if (end-s < Py_UNICODE_SIZE) { |
| 2854 | endinpos = end-starts; |
| 2855 | reason = "truncated input"; |
| 2856 | } |
| 2857 | else { |
| 2858 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 2859 | reason = "illegal code point (> 0x10FFFF)"; |
| 2860 | } |
| 2861 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 2862 | if (unicode_decode_call_errorhandler( |
| 2863 | errors, &errorHandler, |
| 2864 | "unicode_internal", reason, |
| 2865 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2866 | (PyObject **)&v, &outpos, &p)) { |
| 2867 | goto onError; |
| 2868 | } |
| 2869 | } |
| 2870 | else { |
| 2871 | p++; |
| 2872 | s += Py_UNICODE_SIZE; |
| 2873 | } |
| 2874 | } |
| 2875 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2876 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2877 | goto onError; |
| 2878 | Py_XDECREF(errorHandler); |
| 2879 | Py_XDECREF(exc); |
| 2880 | return (PyObject *)v; |
| 2881 | |
| 2882 | onError: |
| 2883 | Py_XDECREF(v); |
| 2884 | Py_XDECREF(errorHandler); |
| 2885 | Py_XDECREF(exc); |
| 2886 | return NULL; |
| 2887 | } |
| 2888 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2889 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 2890 | |
| 2891 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2892 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2893 | const char *errors) |
| 2894 | { |
| 2895 | PyUnicodeObject *v; |
| 2896 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2897 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2898 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2899 | if (size == 1) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 2900 | Py_UNICODE r = *(unsigned char*)s; |
| 2901 | return PyUnicode_FromUnicode(&r, 1); |
| 2902 | } |
| 2903 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2904 | v = _PyUnicode_New(size); |
| 2905 | if (v == NULL) |
| 2906 | goto onError; |
| 2907 | if (size == 0) |
| 2908 | return (PyObject *)v; |
| 2909 | p = PyUnicode_AS_UNICODE(v); |
| 2910 | while (size-- > 0) |
| 2911 | *p++ = (unsigned char)*s++; |
| 2912 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2913 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2914 | onError: |
| 2915 | Py_XDECREF(v); |
| 2916 | return NULL; |
| 2917 | } |
| 2918 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2919 | /* create or adjust a UnicodeEncodeError */ |
| 2920 | static void make_encode_exception(PyObject **exceptionObject, |
| 2921 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2922 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 2923 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2924 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2925 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2926 | if (*exceptionObject == NULL) { |
| 2927 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 2928 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2929 | } |
| 2930 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2931 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 2932 | goto onError; |
| 2933 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 2934 | goto onError; |
| 2935 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 2936 | goto onError; |
| 2937 | return; |
| 2938 | onError: |
| 2939 | Py_DECREF(*exceptionObject); |
| 2940 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2941 | } |
| 2942 | } |
| 2943 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2944 | /* raises a UnicodeEncodeError */ |
| 2945 | static void raise_encode_exception(PyObject **exceptionObject, |
| 2946 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2947 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 2948 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2949 | const char *reason) |
| 2950 | { |
| 2951 | make_encode_exception(exceptionObject, |
| 2952 | encoding, unicode, size, startpos, endpos, reason); |
| 2953 | if (*exceptionObject != NULL) |
| 2954 | PyCodec_StrictErrors(*exceptionObject); |
| 2955 | } |
| 2956 | |
| 2957 | /* error handling callback helper: |
| 2958 | build arguments, call the callback and check the arguments, |
| 2959 | put the result into newpos and return the replacement string, which |
| 2960 | has to be freed by the caller */ |
| 2961 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 2962 | PyObject **errorHandler, |
| 2963 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2964 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 2965 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 2966 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2967 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2968 | 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] | 2969 | |
| 2970 | PyObject *restuple; |
| 2971 | PyObject *resunicode; |
| 2972 | |
| 2973 | if (*errorHandler == NULL) { |
| 2974 | *errorHandler = PyCodec_LookupError(errors); |
| 2975 | if (*errorHandler == NULL) |
| 2976 | return NULL; |
| 2977 | } |
| 2978 | |
| 2979 | make_encode_exception(exceptionObject, |
| 2980 | encoding, unicode, size, startpos, endpos, reason); |
| 2981 | if (*exceptionObject == NULL) |
| 2982 | return NULL; |
| 2983 | |
| 2984 | restuple = PyObject_CallFunctionObjArgs( |
| 2985 | *errorHandler, *exceptionObject, NULL); |
| 2986 | if (restuple == NULL) |
| 2987 | return NULL; |
| 2988 | if (!PyTuple_Check(restuple)) { |
| 2989 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 2990 | Py_DECREF(restuple); |
| 2991 | return NULL; |
| 2992 | } |
| 2993 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 2994 | &resunicode, newpos)) { |
| 2995 | Py_DECREF(restuple); |
| 2996 | return NULL; |
| 2997 | } |
| 2998 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2999 | *newpos = size+*newpos; |
| 3000 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 3001 | 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] | 3002 | Py_DECREF(restuple); |
| 3003 | return NULL; |
| 3004 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3005 | Py_INCREF(resunicode); |
| 3006 | Py_DECREF(restuple); |
| 3007 | return resunicode; |
| 3008 | } |
| 3009 | |
| 3010 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3011 | Py_ssize_t size, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3012 | const char *errors, |
| 3013 | int limit) |
| 3014 | { |
| 3015 | /* output object */ |
| 3016 | PyObject *res; |
| 3017 | /* pointers to the beginning and end+1 of input */ |
| 3018 | const Py_UNICODE *startp = p; |
| 3019 | const Py_UNICODE *endp = p + size; |
| 3020 | /* pointer to the beginning of the unencodable characters */ |
| 3021 | /* const Py_UNICODE *badp = NULL; */ |
| 3022 | /* pointer into the output */ |
| 3023 | char *str; |
| 3024 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3025 | Py_ssize_t respos = 0; |
| 3026 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3027 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 3028 | 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] | 3029 | PyObject *errorHandler = NULL; |
| 3030 | PyObject *exc = NULL; |
| 3031 | /* the following variable is used for caching string comparisons |
| 3032 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 3033 | int known_errorHandler = -1; |
| 3034 | |
| 3035 | /* allocate enough for a simple encoding without |
| 3036 | replacements, if we need more, we'll resize */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3037 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3038 | if (res == NULL) |
| 3039 | goto onError; |
| 3040 | if (size == 0) |
| 3041 | return res; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3042 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3043 | ressize = size; |
| 3044 | |
| 3045 | while (p<endp) { |
| 3046 | Py_UNICODE c = *p; |
| 3047 | |
| 3048 | /* can we encode this? */ |
| 3049 | if (c<limit) { |
| 3050 | /* no overflow check, because we know that the space is enough */ |
| 3051 | *str++ = (char)c; |
| 3052 | ++p; |
| 3053 | } |
| 3054 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3055 | Py_ssize_t unicodepos = p-startp; |
| 3056 | Py_ssize_t requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3057 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3058 | Py_ssize_t repsize; |
| 3059 | Py_ssize_t newpos; |
| 3060 | Py_ssize_t respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3061 | Py_UNICODE *uni2; |
| 3062 | /* startpos for collecting unencodable chars */ |
| 3063 | const Py_UNICODE *collstart = p; |
| 3064 | const Py_UNICODE *collend = p; |
| 3065 | /* find all unecodable characters */ |
| 3066 | while ((collend < endp) && ((*collend)>=limit)) |
| 3067 | ++collend; |
| 3068 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 3069 | if (known_errorHandler==-1) { |
| 3070 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3071 | known_errorHandler = 1; |
| 3072 | else if (!strcmp(errors, "replace")) |
| 3073 | known_errorHandler = 2; |
| 3074 | else if (!strcmp(errors, "ignore")) |
| 3075 | known_errorHandler = 3; |
| 3076 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3077 | known_errorHandler = 4; |
| 3078 | else |
| 3079 | known_errorHandler = 0; |
| 3080 | } |
| 3081 | switch (known_errorHandler) { |
| 3082 | case 1: /* strict */ |
| 3083 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 3084 | goto onError; |
| 3085 | case 2: /* replace */ |
| 3086 | while (collstart++<collend) |
| 3087 | *str++ = '?'; /* fall through */ |
| 3088 | case 3: /* ignore */ |
| 3089 | p = collend; |
| 3090 | break; |
| 3091 | case 4: /* xmlcharrefreplace */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3092 | respos = str - PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3093 | /* determine replacement size (temporarily (mis)uses p) */ |
| 3094 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 3095 | if (*p<10) |
| 3096 | repsize += 2+1+1; |
| 3097 | else if (*p<100) |
| 3098 | repsize += 2+2+1; |
| 3099 | else if (*p<1000) |
| 3100 | repsize += 2+3+1; |
| 3101 | else if (*p<10000) |
| 3102 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 3103 | #ifndef Py_UNICODE_WIDE |
| 3104 | else |
| 3105 | repsize += 2+5+1; |
| 3106 | #else |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3107 | else if (*p<100000) |
| 3108 | repsize += 2+5+1; |
| 3109 | else if (*p<1000000) |
| 3110 | repsize += 2+6+1; |
| 3111 | else |
| 3112 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3113 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3114 | } |
| 3115 | requiredsize = respos+repsize+(endp-collend); |
| 3116 | if (requiredsize > ressize) { |
| 3117 | if (requiredsize<2*ressize) |
| 3118 | requiredsize = 2*ressize; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3119 | if (PyBytes_Resize(res, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3120 | goto onError; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3121 | str = PyBytes_AS_STRING(res) + respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3122 | ressize = requiredsize; |
| 3123 | } |
| 3124 | /* generate replacement (temporarily (mis)uses p) */ |
| 3125 | for (p = collstart; p < collend; ++p) { |
| 3126 | str += sprintf(str, "&#%d;", (int)*p); |
| 3127 | } |
| 3128 | p = collend; |
| 3129 | break; |
| 3130 | default: |
| 3131 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 3132 | encoding, reason, startp, size, &exc, |
| 3133 | collstart-startp, collend-startp, &newpos); |
| 3134 | if (repunicode == NULL) |
| 3135 | goto onError; |
| 3136 | /* need more space? (at least enough for what we |
| 3137 | have+the replacement+the rest of the string, so |
| 3138 | we won't have to check space for encodable characters) */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3139 | respos = str - PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3140 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3141 | requiredsize = respos+repsize+(endp-collend); |
| 3142 | if (requiredsize > ressize) { |
| 3143 | if (requiredsize<2*ressize) |
| 3144 | requiredsize = 2*ressize; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3145 | if (PyBytes_Resize(res, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3146 | Py_DECREF(repunicode); |
| 3147 | goto onError; |
| 3148 | } |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3149 | str = PyBytes_AS_STRING(res) + respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3150 | ressize = requiredsize; |
| 3151 | } |
| 3152 | /* check if there is anything unencodable in the replacement |
| 3153 | and copy it to the output */ |
| 3154 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 3155 | c = *uni2; |
| 3156 | if (c >= limit) { |
| 3157 | raise_encode_exception(&exc, encoding, startp, size, |
| 3158 | unicodepos, unicodepos+1, reason); |
| 3159 | Py_DECREF(repunicode); |
| 3160 | goto onError; |
| 3161 | } |
| 3162 | *str = (char)c; |
| 3163 | } |
| 3164 | p = startp + newpos; |
| 3165 | Py_DECREF(repunicode); |
| 3166 | } |
| 3167 | } |
| 3168 | } |
| 3169 | /* Resize if we allocated to much */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3170 | respos = str - PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3171 | if (respos<ressize) |
| 3172 | /* If this falls res will be NULL */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3173 | PyBytes_Resize(res, respos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3174 | Py_XDECREF(errorHandler); |
| 3175 | Py_XDECREF(exc); |
| 3176 | return res; |
| 3177 | |
| 3178 | onError: |
| 3179 | Py_XDECREF(res); |
| 3180 | Py_XDECREF(errorHandler); |
| 3181 | Py_XDECREF(exc); |
| 3182 | return NULL; |
| 3183 | } |
| 3184 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3185 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3186 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3187 | const char *errors) |
| 3188 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3189 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3190 | } |
| 3191 | |
| 3192 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 3193 | { |
| 3194 | if (!PyUnicode_Check(unicode)) { |
| 3195 | PyErr_BadArgument(); |
| 3196 | return NULL; |
| 3197 | } |
| 3198 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 3199 | PyUnicode_GET_SIZE(unicode), |
| 3200 | NULL); |
| 3201 | } |
| 3202 | |
| 3203 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 3204 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3205 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3206 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3207 | const char *errors) |
| 3208 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3209 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3210 | PyUnicodeObject *v; |
| 3211 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3212 | Py_ssize_t startinpos; |
| 3213 | Py_ssize_t endinpos; |
| 3214 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3215 | const char *e; |
| 3216 | PyObject *errorHandler = NULL; |
| 3217 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3218 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3219 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3220 | if (size == 1 && *(unsigned char*)s < 128) { |
| 3221 | Py_UNICODE r = *(unsigned char*)s; |
| 3222 | return PyUnicode_FromUnicode(&r, 1); |
| 3223 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3224 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3225 | v = _PyUnicode_New(size); |
| 3226 | if (v == NULL) |
| 3227 | goto onError; |
| 3228 | if (size == 0) |
| 3229 | return (PyObject *)v; |
| 3230 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3231 | e = s + size; |
| 3232 | while (s < e) { |
| 3233 | register unsigned char c = (unsigned char)*s; |
| 3234 | if (c < 128) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3235 | *p++ = c; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3236 | ++s; |
| 3237 | } |
| 3238 | else { |
| 3239 | startinpos = s-starts; |
| 3240 | endinpos = startinpos + 1; |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 3241 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3242 | if (unicode_decode_call_errorhandler( |
| 3243 | errors, &errorHandler, |
| 3244 | "ascii", "ordinal not in range(128)", |
| 3245 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3246 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3247 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3248 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3249 | } |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3250 | if (p - PyUnicode_AS_UNICODE(v) < PyString_GET_SIZE(v)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3251 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3252 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3253 | Py_XDECREF(errorHandler); |
| 3254 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3255 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3256 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3257 | onError: |
| 3258 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3259 | Py_XDECREF(errorHandler); |
| 3260 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3261 | return NULL; |
| 3262 | } |
| 3263 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3264 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3265 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3266 | const char *errors) |
| 3267 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3268 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3269 | } |
| 3270 | |
| 3271 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 3272 | { |
| 3273 | if (!PyUnicode_Check(unicode)) { |
| 3274 | PyErr_BadArgument(); |
| 3275 | return NULL; |
| 3276 | } |
| 3277 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 3278 | PyUnicode_GET_SIZE(unicode), |
| 3279 | NULL); |
| 3280 | } |
| 3281 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3282 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3283 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3284 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3285 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3286 | #if SIZEOF_INT < SIZEOF_SSIZE_T |
| 3287 | #define NEED_RETRY |
| 3288 | #endif |
| 3289 | |
| 3290 | /* XXX This code is limited to "true" double-byte encodings, as |
| 3291 | a) it assumes an incomplete character consists of a single byte, and |
| 3292 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
| 3293 | encodings, see IsDBCSLeadByteEx documentation. */ |
| 3294 | |
| 3295 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 3296 | { |
| 3297 | const char *curr = s + offset; |
| 3298 | |
| 3299 | if (IsDBCSLeadByte(*curr)) { |
| 3300 | const char *prev = CharPrev(s, curr); |
| 3301 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
| 3302 | } |
| 3303 | return 0; |
| 3304 | } |
| 3305 | |
| 3306 | /* |
| 3307 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 3308 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 3309 | */ |
| 3310 | static int decode_mbcs(PyUnicodeObject **v, |
| 3311 | const char *s, /* MBCS string */ |
| 3312 | int size, /* sizeof MBCS string */ |
| 3313 | int final) |
| 3314 | { |
| 3315 | Py_UNICODE *p; |
| 3316 | Py_ssize_t n = 0; |
| 3317 | int usize = 0; |
| 3318 | |
| 3319 | assert(size >= 0); |
| 3320 | |
| 3321 | /* Skip trailing lead-byte unless 'final' is set */ |
| 3322 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
| 3323 | --size; |
| 3324 | |
| 3325 | /* First get the size of the result */ |
| 3326 | if (size > 0) { |
| 3327 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 3328 | if (usize == 0) { |
| 3329 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3330 | return -1; |
| 3331 | } |
| 3332 | } |
| 3333 | |
| 3334 | if (*v == NULL) { |
| 3335 | /* Create unicode object */ |
| 3336 | *v = _PyUnicode_New(usize); |
| 3337 | if (*v == NULL) |
| 3338 | return -1; |
| 3339 | } |
| 3340 | else { |
| 3341 | /* Extend unicode object */ |
| 3342 | n = PyUnicode_GET_SIZE(*v); |
| 3343 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 3344 | return -1; |
| 3345 | } |
| 3346 | |
| 3347 | /* Do the conversion */ |
| 3348 | if (size > 0) { |
| 3349 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 3350 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 3351 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3352 | return -1; |
| 3353 | } |
| 3354 | } |
| 3355 | |
| 3356 | return size; |
| 3357 | } |
| 3358 | |
| 3359 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
| 3360 | Py_ssize_t size, |
| 3361 | const char *errors, |
| 3362 | Py_ssize_t *consumed) |
| 3363 | { |
| 3364 | PyUnicodeObject *v = NULL; |
| 3365 | int done; |
| 3366 | |
| 3367 | if (consumed) |
| 3368 | *consumed = 0; |
| 3369 | |
| 3370 | #ifdef NEED_RETRY |
| 3371 | retry: |
| 3372 | if (size > INT_MAX) |
| 3373 | done = decode_mbcs(&v, s, INT_MAX, 0); |
| 3374 | else |
| 3375 | #endif |
| 3376 | done = decode_mbcs(&v, s, (int)size, !consumed); |
| 3377 | |
| 3378 | if (done < 0) { |
| 3379 | Py_XDECREF(v); |
| 3380 | return NULL; |
| 3381 | } |
| 3382 | |
| 3383 | if (consumed) |
| 3384 | *consumed += done; |
| 3385 | |
| 3386 | #ifdef NEED_RETRY |
| 3387 | if (size > INT_MAX) { |
| 3388 | s += done; |
| 3389 | size -= done; |
| 3390 | goto retry; |
| 3391 | } |
| 3392 | #endif |
| 3393 | |
| 3394 | return (PyObject *)v; |
| 3395 | } |
| 3396 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3397 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3398 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3399 | const char *errors) |
| 3400 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3401 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 3402 | } |
| 3403 | |
| 3404 | /* |
| 3405 | * Convert unicode into string object (MBCS). |
| 3406 | * Returns 0 if succeed, -1 otherwise. |
| 3407 | */ |
| 3408 | static int encode_mbcs(PyObject **repr, |
| 3409 | const Py_UNICODE *p, /* unicode */ |
| 3410 | int size) /* size of unicode */ |
| 3411 | { |
| 3412 | int mbcssize = 0; |
| 3413 | Py_ssize_t n = 0; |
| 3414 | |
| 3415 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3416 | |
| 3417 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3418 | if (size > 0) { |
| 3419 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 3420 | if (mbcssize == 0) { |
| 3421 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3422 | return -1; |
| 3423 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3424 | } |
| 3425 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3426 | if (*repr == NULL) { |
| 3427 | /* Create string object */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3428 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3429 | if (*repr == NULL) |
| 3430 | return -1; |
| 3431 | } |
| 3432 | else { |
| 3433 | /* Extend string object */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3434 | n = PyBytes_Size(*repr); |
| 3435 | if (PyBytes_Resize(*repr, n + mbcssize) < 0) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3436 | return -1; |
| 3437 | } |
| 3438 | |
| 3439 | /* Do the conversion */ |
| 3440 | if (size > 0) { |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3441 | char *s = PyBytes_AS_STRING(*repr) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3442 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 3443 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3444 | return -1; |
| 3445 | } |
| 3446 | } |
| 3447 | |
| 3448 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3449 | } |
| 3450 | |
| 3451 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3452 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3453 | const char *errors) |
| 3454 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3455 | PyObject *repr = NULL; |
| 3456 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 3457 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3458 | #ifdef NEED_RETRY |
| 3459 | retry: |
| 3460 | if (size > INT_MAX) |
| 3461 | ret = encode_mbcs(&repr, p, INT_MAX); |
| 3462 | else |
| 3463 | #endif |
| 3464 | ret = encode_mbcs(&repr, p, (int)size); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3465 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3466 | if (ret < 0) { |
| 3467 | Py_XDECREF(repr); |
| 3468 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3469 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3470 | |
| 3471 | #ifdef NEED_RETRY |
| 3472 | if (size > INT_MAX) { |
| 3473 | p += INT_MAX; |
| 3474 | size -= INT_MAX; |
| 3475 | goto retry; |
| 3476 | } |
| 3477 | #endif |
| 3478 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3479 | return repr; |
| 3480 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3481 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3482 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 3483 | { |
| 3484 | if (!PyUnicode_Check(unicode)) { |
| 3485 | PyErr_BadArgument(); |
| 3486 | return NULL; |
| 3487 | } |
| 3488 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 3489 | PyUnicode_GET_SIZE(unicode), |
| 3490 | NULL); |
| 3491 | } |
| 3492 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3493 | #undef NEED_RETRY |
| 3494 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3495 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3496 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3497 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 3498 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3499 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3500 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3501 | PyObject *mapping, |
| 3502 | const char *errors) |
| 3503 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3504 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3505 | Py_ssize_t startinpos; |
| 3506 | Py_ssize_t endinpos; |
| 3507 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3508 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3509 | PyUnicodeObject *v; |
| 3510 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3511 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3512 | PyObject *errorHandler = NULL; |
| 3513 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3514 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3515 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3516 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3517 | /* Default to Latin-1 */ |
| 3518 | if (mapping == NULL) |
| 3519 | return PyUnicode_DecodeLatin1(s, size, errors); |
| 3520 | |
| 3521 | v = _PyUnicode_New(size); |
| 3522 | if (v == NULL) |
| 3523 | goto onError; |
| 3524 | if (size == 0) |
| 3525 | return (PyObject *)v; |
| 3526 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3527 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3528 | if (PyUnicode_CheckExact(mapping)) { |
| 3529 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 3530 | maplen = PyUnicode_GET_SIZE(mapping); |
| 3531 | while (s < e) { |
| 3532 | unsigned char ch = *s; |
| 3533 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3534 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3535 | if (ch < maplen) |
| 3536 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3537 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3538 | if (x == 0xfffe) { |
| 3539 | /* undefined mapping */ |
| 3540 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3541 | startinpos = s-starts; |
| 3542 | endinpos = startinpos+1; |
| 3543 | if (unicode_decode_call_errorhandler( |
| 3544 | errors, &errorHandler, |
| 3545 | "charmap", "character maps to <undefined>", |
| 3546 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3547 | (PyObject **)&v, &outpos, &p)) { |
| 3548 | goto onError; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 3549 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3550 | continue; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 3551 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3552 | *p++ = x; |
| 3553 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3554 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3555 | } |
| 3556 | else { |
| 3557 | while (s < e) { |
| 3558 | unsigned char ch = *s; |
| 3559 | PyObject *w, *x; |
| 3560 | |
| 3561 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 3562 | w = PyInt_FromLong((long)ch); |
| 3563 | if (w == NULL) |
| 3564 | goto onError; |
| 3565 | x = PyObject_GetItem(mapping, w); |
| 3566 | Py_DECREF(w); |
| 3567 | if (x == NULL) { |
| 3568 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3569 | /* No mapping found means: mapping is undefined. */ |
| 3570 | PyErr_Clear(); |
| 3571 | x = Py_None; |
| 3572 | Py_INCREF(x); |
| 3573 | } else |
| 3574 | goto onError; |
| 3575 | } |
| 3576 | |
| 3577 | /* Apply mapping */ |
| 3578 | if (PyInt_Check(x)) { |
| 3579 | long value = PyInt_AS_LONG(x); |
| 3580 | if (value < 0 || value > 65535) { |
| 3581 | PyErr_SetString(PyExc_TypeError, |
| 3582 | "character mapping must be in range(65536)"); |
| 3583 | Py_DECREF(x); |
| 3584 | goto onError; |
| 3585 | } |
| 3586 | *p++ = (Py_UNICODE)value; |
| 3587 | } |
| 3588 | else if (x == Py_None) { |
| 3589 | /* undefined mapping */ |
| 3590 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3591 | startinpos = s-starts; |
| 3592 | endinpos = startinpos+1; |
| 3593 | if (unicode_decode_call_errorhandler( |
| 3594 | errors, &errorHandler, |
| 3595 | "charmap", "character maps to <undefined>", |
| 3596 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3597 | (PyObject **)&v, &outpos, &p)) { |
| 3598 | Py_DECREF(x); |
| 3599 | goto onError; |
| 3600 | } |
Walter Dörwald | d4fff17 | 2005-11-28 22:15:56 +0000 | [diff] [blame] | 3601 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3602 | continue; |
| 3603 | } |
| 3604 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3605 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3606 | |
| 3607 | if (targetsize == 1) |
| 3608 | /* 1-1 mapping */ |
| 3609 | *p++ = *PyUnicode_AS_UNICODE(x); |
| 3610 | |
| 3611 | else if (targetsize > 1) { |
| 3612 | /* 1-n mapping */ |
| 3613 | if (targetsize > extrachars) { |
| 3614 | /* resize first */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3615 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 3616 | Py_ssize_t needed = (targetsize - extrachars) + \ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3617 | (targetsize << 2); |
| 3618 | extrachars += needed; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3619 | /* XXX overflow detection missing */ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3620 | if (_PyUnicode_Resize(&v, |
| 3621 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 3622 | Py_DECREF(x); |
| 3623 | goto onError; |
| 3624 | } |
| 3625 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 3626 | } |
| 3627 | Py_UNICODE_COPY(p, |
| 3628 | PyUnicode_AS_UNICODE(x), |
| 3629 | targetsize); |
| 3630 | p += targetsize; |
| 3631 | extrachars -= targetsize; |
| 3632 | } |
| 3633 | /* 1-0 mapping: skip the character */ |
| 3634 | } |
| 3635 | else { |
| 3636 | /* wrong return value */ |
| 3637 | PyErr_SetString(PyExc_TypeError, |
| 3638 | "character mapping must return integer, None or unicode"); |
| 3639 | Py_DECREF(x); |
| 3640 | goto onError; |
| 3641 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3642 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3643 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3644 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3645 | } |
| 3646 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3647 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3648 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3649 | Py_XDECREF(errorHandler); |
| 3650 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3651 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3652 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3653 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3654 | Py_XDECREF(errorHandler); |
| 3655 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3656 | Py_XDECREF(v); |
| 3657 | return NULL; |
| 3658 | } |
| 3659 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3660 | /* Charmap encoding: the lookup table */ |
| 3661 | |
| 3662 | struct encoding_map{ |
| 3663 | PyObject_HEAD |
| 3664 | unsigned char level1[32]; |
| 3665 | int count2, count3; |
| 3666 | unsigned char level23[1]; |
| 3667 | }; |
| 3668 | |
| 3669 | static PyObject* |
| 3670 | encoding_map_size(PyObject *obj, PyObject* args) |
| 3671 | { |
| 3672 | struct encoding_map *map = (struct encoding_map*)obj; |
| 3673 | return PyInt_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
| 3674 | 128*map->count3); |
| 3675 | } |
| 3676 | |
| 3677 | static PyMethodDef encoding_map_methods[] = { |
| 3678 | {"size", encoding_map_size, METH_NOARGS, |
| 3679 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 3680 | { 0 } |
| 3681 | }; |
| 3682 | |
| 3683 | static void |
| 3684 | encoding_map_dealloc(PyObject* o) |
| 3685 | { |
| 3686 | PyObject_FREE(o); |
| 3687 | } |
| 3688 | |
| 3689 | static PyTypeObject EncodingMapType = { |
| 3690 | PyObject_HEAD_INIT(NULL) |
| 3691 | 0, /*ob_size*/ |
| 3692 | "EncodingMap", /*tp_name*/ |
| 3693 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 3694 | 0, /*tp_itemsize*/ |
| 3695 | /* methods */ |
| 3696 | encoding_map_dealloc, /*tp_dealloc*/ |
| 3697 | 0, /*tp_print*/ |
| 3698 | 0, /*tp_getattr*/ |
| 3699 | 0, /*tp_setattr*/ |
| 3700 | 0, /*tp_compare*/ |
| 3701 | 0, /*tp_repr*/ |
| 3702 | 0, /*tp_as_number*/ |
| 3703 | 0, /*tp_as_sequence*/ |
| 3704 | 0, /*tp_as_mapping*/ |
| 3705 | 0, /*tp_hash*/ |
| 3706 | 0, /*tp_call*/ |
| 3707 | 0, /*tp_str*/ |
| 3708 | 0, /*tp_getattro*/ |
| 3709 | 0, /*tp_setattro*/ |
| 3710 | 0, /*tp_as_buffer*/ |
| 3711 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 3712 | 0, /*tp_doc*/ |
| 3713 | 0, /*tp_traverse*/ |
| 3714 | 0, /*tp_clear*/ |
| 3715 | 0, /*tp_richcompare*/ |
| 3716 | 0, /*tp_weaklistoffset*/ |
| 3717 | 0, /*tp_iter*/ |
| 3718 | 0, /*tp_iternext*/ |
| 3719 | encoding_map_methods, /*tp_methods*/ |
| 3720 | 0, /*tp_members*/ |
| 3721 | 0, /*tp_getset*/ |
| 3722 | 0, /*tp_base*/ |
| 3723 | 0, /*tp_dict*/ |
| 3724 | 0, /*tp_descr_get*/ |
| 3725 | 0, /*tp_descr_set*/ |
| 3726 | 0, /*tp_dictoffset*/ |
| 3727 | 0, /*tp_init*/ |
| 3728 | 0, /*tp_alloc*/ |
| 3729 | 0, /*tp_new*/ |
| 3730 | 0, /*tp_free*/ |
| 3731 | 0, /*tp_is_gc*/ |
| 3732 | }; |
| 3733 | |
| 3734 | PyObject* |
| 3735 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 3736 | { |
| 3737 | Py_UNICODE *decode; |
| 3738 | PyObject *result; |
| 3739 | struct encoding_map *mresult; |
| 3740 | int i; |
| 3741 | int need_dict = 0; |
| 3742 | unsigned char level1[32]; |
| 3743 | unsigned char level2[512]; |
| 3744 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 3745 | int count2 = 0, count3 = 0; |
| 3746 | |
| 3747 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 3748 | PyErr_BadArgument(); |
| 3749 | return NULL; |
| 3750 | } |
| 3751 | decode = PyUnicode_AS_UNICODE(string); |
| 3752 | memset(level1, 0xFF, sizeof level1); |
| 3753 | memset(level2, 0xFF, sizeof level2); |
| 3754 | |
| 3755 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 3756 | or if there are non-BMP characters, we need to use |
| 3757 | a mapping dictionary. */ |
| 3758 | if (decode[0] != 0) |
| 3759 | need_dict = 1; |
| 3760 | for (i = 1; i < 256; i++) { |
| 3761 | int l1, l2; |
| 3762 | if (decode[i] == 0 |
| 3763 | #ifdef Py_UNICODE_WIDE |
| 3764 | || decode[i] > 0xFFFF |
| 3765 | #endif |
| 3766 | ) { |
| 3767 | need_dict = 1; |
| 3768 | break; |
| 3769 | } |
| 3770 | if (decode[i] == 0xFFFE) |
| 3771 | /* unmapped character */ |
| 3772 | continue; |
| 3773 | l1 = decode[i] >> 11; |
| 3774 | l2 = decode[i] >> 7; |
| 3775 | if (level1[l1] == 0xFF) |
| 3776 | level1[l1] = count2++; |
| 3777 | if (level2[l2] == 0xFF) |
| 3778 | level2[l2] = count3++; |
| 3779 | } |
| 3780 | |
| 3781 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 3782 | need_dict = 1; |
| 3783 | |
| 3784 | if (need_dict) { |
| 3785 | PyObject *result = PyDict_New(); |
| 3786 | PyObject *key, *value; |
| 3787 | if (!result) |
| 3788 | return NULL; |
| 3789 | for (i = 0; i < 256; i++) { |
| 3790 | key = value = NULL; |
| 3791 | key = PyInt_FromLong(decode[i]); |
| 3792 | value = PyInt_FromLong(i); |
| 3793 | if (!key || !value) |
| 3794 | goto failed1; |
| 3795 | if (PyDict_SetItem(result, key, value) == -1) |
| 3796 | goto failed1; |
| 3797 | Py_DECREF(key); |
| 3798 | Py_DECREF(value); |
| 3799 | } |
| 3800 | return result; |
| 3801 | failed1: |
| 3802 | Py_XDECREF(key); |
| 3803 | Py_XDECREF(value); |
| 3804 | Py_DECREF(result); |
| 3805 | return NULL; |
| 3806 | } |
| 3807 | |
| 3808 | /* Create a three-level trie */ |
| 3809 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 3810 | 16*count2 + 128*count3 - 1); |
| 3811 | if (!result) |
| 3812 | return PyErr_NoMemory(); |
| 3813 | PyObject_Init(result, &EncodingMapType); |
| 3814 | mresult = (struct encoding_map*)result; |
| 3815 | mresult->count2 = count2; |
| 3816 | mresult->count3 = count3; |
| 3817 | mlevel1 = mresult->level1; |
| 3818 | mlevel2 = mresult->level23; |
| 3819 | mlevel3 = mresult->level23 + 16*count2; |
| 3820 | memcpy(mlevel1, level1, 32); |
| 3821 | memset(mlevel2, 0xFF, 16*count2); |
| 3822 | memset(mlevel3, 0, 128*count3); |
| 3823 | count3 = 0; |
| 3824 | for (i = 1; i < 256; i++) { |
| 3825 | int o1, o2, o3, i2, i3; |
| 3826 | if (decode[i] == 0xFFFE) |
| 3827 | /* unmapped character */ |
| 3828 | continue; |
| 3829 | o1 = decode[i]>>11; |
| 3830 | o2 = (decode[i]>>7) & 0xF; |
| 3831 | i2 = 16*mlevel1[o1] + o2; |
| 3832 | if (mlevel2[i2] == 0xFF) |
| 3833 | mlevel2[i2] = count3++; |
| 3834 | o3 = decode[i] & 0x7F; |
| 3835 | i3 = 128*mlevel2[i2] + o3; |
| 3836 | mlevel3[i3] = i; |
| 3837 | } |
| 3838 | return result; |
| 3839 | } |
| 3840 | |
| 3841 | static int |
| 3842 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 3843 | { |
| 3844 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 3845 | int l1 = c>>11; |
| 3846 | int l2 = (c>>7) & 0xF; |
| 3847 | int l3 = c & 0x7F; |
| 3848 | int i; |
| 3849 | |
| 3850 | #ifdef Py_UNICODE_WIDE |
| 3851 | if (c > 0xFFFF) { |
| 3852 | return -1; |
| 3853 | } |
| 3854 | #endif |
| 3855 | if (c == 0) |
| 3856 | return 0; |
| 3857 | /* level 1*/ |
| 3858 | i = map->level1[l1]; |
| 3859 | if (i == 0xFF) { |
| 3860 | return -1; |
| 3861 | } |
| 3862 | /* level 2*/ |
| 3863 | i = map->level23[16*i+l2]; |
| 3864 | if (i == 0xFF) { |
| 3865 | return -1; |
| 3866 | } |
| 3867 | /* level 3 */ |
| 3868 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 3869 | if (i == 0) { |
| 3870 | return -1; |
| 3871 | } |
| 3872 | return i; |
| 3873 | } |
| 3874 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3875 | /* Lookup the character ch in the mapping. If the character |
| 3876 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3877 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3878 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3879 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3880 | PyObject *w = PyInt_FromLong((long)c); |
| 3881 | PyObject *x; |
| 3882 | |
| 3883 | if (w == NULL) |
| 3884 | return NULL; |
| 3885 | x = PyObject_GetItem(mapping, w); |
| 3886 | Py_DECREF(w); |
| 3887 | if (x == NULL) { |
| 3888 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3889 | /* No mapping found means: mapping is undefined. */ |
| 3890 | PyErr_Clear(); |
| 3891 | x = Py_None; |
| 3892 | Py_INCREF(x); |
| 3893 | return x; |
| 3894 | } else |
| 3895 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3896 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 3897 | else if (x == Py_None) |
| 3898 | return x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3899 | else if (PyInt_Check(x)) { |
| 3900 | long value = PyInt_AS_LONG(x); |
| 3901 | if (value < 0 || value > 255) { |
| 3902 | PyErr_SetString(PyExc_TypeError, |
| 3903 | "character mapping must be in range(256)"); |
| 3904 | Py_DECREF(x); |
| 3905 | return NULL; |
| 3906 | } |
| 3907 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3908 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3909 | else if (PyString_Check(x)) |
| 3910 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3911 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3912 | /* wrong return value */ |
Walter Dörwald | 580ceed | 2007-05-09 10:39:19 +0000 | [diff] [blame] | 3913 | PyErr_Format(PyExc_TypeError, |
| 3914 | "character mapping must return integer, None or str8, not %.400s", |
| 3915 | x->ob_type->tp_name); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3916 | Py_DECREF(x); |
| 3917 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3918 | } |
| 3919 | } |
| 3920 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3921 | static int |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3922 | charmapencode_resize(PyObject *outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3923 | { |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3924 | Py_ssize_t outsize = PyBytes_GET_SIZE( outobj); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3925 | /* exponentially overallocate to minimize reallocations */ |
| 3926 | if (requiredsize < 2*outsize) |
| 3927 | requiredsize = 2*outsize; |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3928 | if (PyBytes_Resize(outobj, requiredsize)) { |
| 3929 | Py_DECREF(outobj); |
| 3930 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3931 | } |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3932 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3933 | } |
| 3934 | |
| 3935 | typedef enum charmapencode_result { |
| 3936 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
| 3937 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3938 | /* 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] | 3939 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3940 | space is available. Return a new reference to the object that |
| 3941 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 3942 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 3943 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3944 | static |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3945 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3946 | PyObject *outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3947 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3948 | PyObject *rep; |
| 3949 | char *outstart; |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3950 | Py_ssize_t outsize = PyBytes_GET_SIZE(outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3951 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3952 | if (mapping->ob_type == &EncodingMapType) { |
| 3953 | int res = encoding_map_lookup(c, mapping); |
| 3954 | Py_ssize_t requiredsize = *outpos+1; |
| 3955 | if (res == -1) |
| 3956 | return enc_FAILED; |
| 3957 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3958 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3959 | return enc_EXCEPTION; |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3960 | outstart = PyBytes_AS_STRING(outobj); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3961 | outstart[(*outpos)++] = (char)res; |
| 3962 | return enc_SUCCESS; |
| 3963 | } |
| 3964 | |
| 3965 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3966 | if (rep==NULL) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3967 | return enc_EXCEPTION; |
| 3968 | else if (rep==Py_None) { |
| 3969 | Py_DECREF(rep); |
| 3970 | return enc_FAILED; |
| 3971 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3972 | if (PyInt_Check(rep)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3973 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3974 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3975 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3976 | Py_DECREF(rep); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3977 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3978 | } |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3979 | outstart = PyBytes_AS_STRING(outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3980 | outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); |
| 3981 | } |
| 3982 | else { |
| 3983 | const char *repchars = PyString_AS_STRING(rep); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3984 | Py_ssize_t repsize = PyString_GET_SIZE(rep); |
| 3985 | Py_ssize_t requiredsize = *outpos+repsize; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3986 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3987 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3988 | Py_DECREF(rep); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3989 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3990 | } |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3991 | outstart = PyBytes_AS_STRING(outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3992 | memcpy(outstart + *outpos, repchars, repsize); |
| 3993 | *outpos += repsize; |
| 3994 | } |
| 3995 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3996 | Py_DECREF(rep); |
| 3997 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3998 | } |
| 3999 | |
| 4000 | /* handle an error in PyUnicode_EncodeCharmap |
| 4001 | Return 0 on success, -1 on error */ |
| 4002 | static |
| 4003 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4004 | 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] | 4005 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4006 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4007 | PyObject *res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4008 | { |
| 4009 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4010 | Py_ssize_t repsize; |
| 4011 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4012 | Py_UNICODE *uni2; |
| 4013 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4014 | Py_ssize_t collstartpos = *inpos; |
| 4015 | Py_ssize_t collendpos = *inpos+1; |
| 4016 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4017 | char *encoding = "charmap"; |
| 4018 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4019 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4020 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4021 | /* find all unencodable characters */ |
| 4022 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4023 | PyObject *rep; |
| 4024 | if (mapping->ob_type == &EncodingMapType) { |
| 4025 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 4026 | if (res != -1) |
| 4027 | break; |
| 4028 | ++collendpos; |
| 4029 | continue; |
| 4030 | } |
| 4031 | |
| 4032 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 4033 | if (rep==NULL) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4034 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4035 | else if (rep!=Py_None) { |
| 4036 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4037 | break; |
| 4038 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4039 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4040 | ++collendpos; |
| 4041 | } |
| 4042 | /* cache callback name lookup |
| 4043 | * (if not done yet, i.e. it's the first error) */ |
| 4044 | if (*known_errorHandler==-1) { |
| 4045 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4046 | *known_errorHandler = 1; |
| 4047 | else if (!strcmp(errors, "replace")) |
| 4048 | *known_errorHandler = 2; |
| 4049 | else if (!strcmp(errors, "ignore")) |
| 4050 | *known_errorHandler = 3; |
| 4051 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4052 | *known_errorHandler = 4; |
| 4053 | else |
| 4054 | *known_errorHandler = 0; |
| 4055 | } |
| 4056 | switch (*known_errorHandler) { |
| 4057 | case 1: /* strict */ |
| 4058 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4059 | return -1; |
| 4060 | case 2: /* replace */ |
| 4061 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 4062 | x = charmapencode_output('?', mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4063 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4064 | return -1; |
| 4065 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4066 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4067 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4068 | return -1; |
| 4069 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4070 | } |
| 4071 | /* fall through */ |
| 4072 | case 3: /* ignore */ |
| 4073 | *inpos = collendpos; |
| 4074 | break; |
| 4075 | case 4: /* xmlcharrefreplace */ |
| 4076 | /* generate replacement (temporarily (mis)uses p) */ |
| 4077 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 4078 | char buffer[2+29+1+1]; |
| 4079 | char *cp; |
| 4080 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 4081 | for (cp = buffer; *cp; ++cp) { |
| 4082 | x = charmapencode_output(*cp, mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4083 | if (x==enc_EXCEPTION) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4084 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4085 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4086 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4087 | return -1; |
| 4088 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4089 | } |
| 4090 | } |
| 4091 | *inpos = collendpos; |
| 4092 | break; |
| 4093 | default: |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4094 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4095 | encoding, reason, p, size, exceptionObject, |
| 4096 | collstartpos, collendpos, &newpos); |
| 4097 | if (repunicode == NULL) |
| 4098 | return -1; |
| 4099 | /* generate replacement */ |
| 4100 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4101 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 4102 | x = charmapencode_output(*uni2, mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4103 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4104 | return -1; |
| 4105 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4106 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4107 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4108 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4109 | return -1; |
| 4110 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4111 | } |
| 4112 | *inpos = newpos; |
| 4113 | Py_DECREF(repunicode); |
| 4114 | } |
| 4115 | return 0; |
| 4116 | } |
| 4117 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4118 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4119 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4120 | PyObject *mapping, |
| 4121 | const char *errors) |
| 4122 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4123 | /* output object */ |
| 4124 | PyObject *res = NULL; |
| 4125 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4126 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4127 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4128 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4129 | PyObject *errorHandler = NULL; |
| 4130 | PyObject *exc = NULL; |
| 4131 | /* the following variable is used for caching string comparisons |
| 4132 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4133 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4134 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4135 | |
| 4136 | /* Default to Latin-1 */ |
| 4137 | if (mapping == NULL) |
| 4138 | return PyUnicode_EncodeLatin1(p, size, errors); |
| 4139 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4140 | /* allocate enough for a simple encoding without |
| 4141 | replacements, if we need more, we'll resize */ |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4142 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4143 | if (res == NULL) |
| 4144 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 4145 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4146 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4147 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4148 | while (inpos<size) { |
| 4149 | /* try to encode it */ |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4150 | charmapencode_result x = charmapencode_output(p[inpos], mapping, res, &respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4151 | if (x==enc_EXCEPTION) /* error */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4152 | goto onError; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4153 | if (x==enc_FAILED) { /* unencodable character */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4154 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 4155 | &exc, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4156 | &known_errorHandler, &errorHandler, errors, |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4157 | res, &respos)) { |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 4158 | goto onError; |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 4159 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4160 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4161 | else |
| 4162 | /* done with this character => adjust input position */ |
| 4163 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4164 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4165 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4166 | /* Resize if we allocated to much */ |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4167 | if (respos<PyBytes_GET_SIZE(res)) { |
| 4168 | if (PyBytes_Resize(res, respos)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4169 | goto onError; |
| 4170 | } |
| 4171 | Py_XDECREF(exc); |
| 4172 | Py_XDECREF(errorHandler); |
| 4173 | return res; |
| 4174 | |
| 4175 | onError: |
| 4176 | Py_XDECREF(res); |
| 4177 | Py_XDECREF(exc); |
| 4178 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4179 | return NULL; |
| 4180 | } |
| 4181 | |
| 4182 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 4183 | PyObject *mapping) |
| 4184 | { |
| 4185 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 4186 | PyErr_BadArgument(); |
| 4187 | return NULL; |
| 4188 | } |
| 4189 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 4190 | PyUnicode_GET_SIZE(unicode), |
| 4191 | mapping, |
| 4192 | NULL); |
| 4193 | } |
| 4194 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4195 | /* create or adjust a UnicodeTranslateError */ |
| 4196 | static void make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4197 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4198 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4199 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4200 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4201 | if (*exceptionObject == NULL) { |
| 4202 | *exceptionObject = PyUnicodeTranslateError_Create( |
| 4203 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4204 | } |
| 4205 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4206 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 4207 | goto onError; |
| 4208 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 4209 | goto onError; |
| 4210 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 4211 | goto onError; |
| 4212 | return; |
| 4213 | onError: |
| 4214 | Py_DECREF(*exceptionObject); |
| 4215 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4216 | } |
| 4217 | } |
| 4218 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4219 | /* raises a UnicodeTranslateError */ |
| 4220 | static void raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4221 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4222 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4223 | const char *reason) |
| 4224 | { |
| 4225 | make_translate_exception(exceptionObject, |
| 4226 | unicode, size, startpos, endpos, reason); |
| 4227 | if (*exceptionObject != NULL) |
| 4228 | PyCodec_StrictErrors(*exceptionObject); |
| 4229 | } |
| 4230 | |
| 4231 | /* error handling callback helper: |
| 4232 | build arguments, call the callback and check the arguments, |
| 4233 | put the result into newpos and return the replacement string, which |
| 4234 | has to be freed by the caller */ |
| 4235 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 4236 | PyObject **errorHandler, |
| 4237 | const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4238 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4239 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4240 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4241 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4242 | 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] | 4243 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4244 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4245 | PyObject *restuple; |
| 4246 | PyObject *resunicode; |
| 4247 | |
| 4248 | if (*errorHandler == NULL) { |
| 4249 | *errorHandler = PyCodec_LookupError(errors); |
| 4250 | if (*errorHandler == NULL) |
| 4251 | return NULL; |
| 4252 | } |
| 4253 | |
| 4254 | make_translate_exception(exceptionObject, |
| 4255 | unicode, size, startpos, endpos, reason); |
| 4256 | if (*exceptionObject == NULL) |
| 4257 | return NULL; |
| 4258 | |
| 4259 | restuple = PyObject_CallFunctionObjArgs( |
| 4260 | *errorHandler, *exceptionObject, NULL); |
| 4261 | if (restuple == NULL) |
| 4262 | return NULL; |
| 4263 | if (!PyTuple_Check(restuple)) { |
| 4264 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 4265 | Py_DECREF(restuple); |
| 4266 | return NULL; |
| 4267 | } |
| 4268 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4269 | &resunicode, &i_newpos)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4270 | Py_DECREF(restuple); |
| 4271 | return NULL; |
| 4272 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4273 | if (i_newpos<0) |
| 4274 | *newpos = size+i_newpos; |
| 4275 | else |
| 4276 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4277 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 4278 | 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] | 4279 | Py_DECREF(restuple); |
| 4280 | return NULL; |
| 4281 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4282 | Py_INCREF(resunicode); |
| 4283 | Py_DECREF(restuple); |
| 4284 | return resunicode; |
| 4285 | } |
| 4286 | |
| 4287 | /* Lookup the character ch in the mapping and put the result in result, |
| 4288 | which must be decrefed by the caller. |
| 4289 | Return 0 on success, -1 on error */ |
| 4290 | static |
| 4291 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 4292 | { |
| 4293 | PyObject *w = PyInt_FromLong((long)c); |
| 4294 | PyObject *x; |
| 4295 | |
| 4296 | if (w == NULL) |
| 4297 | return -1; |
| 4298 | x = PyObject_GetItem(mapping, w); |
| 4299 | Py_DECREF(w); |
| 4300 | if (x == NULL) { |
| 4301 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4302 | /* No mapping found means: use 1:1 mapping. */ |
| 4303 | PyErr_Clear(); |
| 4304 | *result = NULL; |
| 4305 | return 0; |
| 4306 | } else |
| 4307 | return -1; |
| 4308 | } |
| 4309 | else if (x == Py_None) { |
| 4310 | *result = x; |
| 4311 | return 0; |
| 4312 | } |
| 4313 | else if (PyInt_Check(x)) { |
| 4314 | long value = PyInt_AS_LONG(x); |
| 4315 | long max = PyUnicode_GetMax(); |
| 4316 | if (value < 0 || value > max) { |
| 4317 | PyErr_Format(PyExc_TypeError, |
| 4318 | "character mapping must be in range(0x%lx)", max+1); |
| 4319 | Py_DECREF(x); |
| 4320 | return -1; |
| 4321 | } |
| 4322 | *result = x; |
| 4323 | return 0; |
| 4324 | } |
| 4325 | else if (PyUnicode_Check(x)) { |
| 4326 | *result = x; |
| 4327 | return 0; |
| 4328 | } |
| 4329 | else { |
| 4330 | /* wrong return value */ |
| 4331 | PyErr_SetString(PyExc_TypeError, |
| 4332 | "character mapping must return integer, None or unicode"); |
Walter Dörwald | 150523e | 2003-08-15 16:52:19 +0000 | [diff] [blame] | 4333 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4334 | return -1; |
| 4335 | } |
| 4336 | } |
| 4337 | /* ensure that *outobj is at least requiredsize characters long, |
| 4338 | if not reallocate and adjust various state variables. |
| 4339 | Return 0 on success, -1 on error */ |
| 4340 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4341 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4342 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4343 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4344 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4345 | if (requiredsize > oldsize) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4346 | /* remember old output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4347 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4348 | /* exponentially overallocate to minimize reallocations */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4349 | if (requiredsize < 2 * oldsize) |
| 4350 | requiredsize = 2 * oldsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4351 | if (_PyUnicode_Resize(outobj, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4352 | return -1; |
| 4353 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4354 | } |
| 4355 | return 0; |
| 4356 | } |
| 4357 | /* lookup the character, put the result in the output string and adjust |
| 4358 | various state variables. Return a new reference to the object that |
| 4359 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 4360 | undefined (in which case no character was written). |
| 4361 | The called must decref result. |
| 4362 | Return 0 on success, -1 on error. */ |
| 4363 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4364 | 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] | 4365 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4366 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4367 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4368 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4369 | return -1; |
| 4370 | if (*res==NULL) { |
| 4371 | /* not found => default to 1:1 mapping */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4372 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4373 | } |
| 4374 | else if (*res==Py_None) |
| 4375 | ; |
| 4376 | else if (PyInt_Check(*res)) { |
| 4377 | /* no overflow check, because we know that the space is enough */ |
| 4378 | *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); |
| 4379 | } |
| 4380 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4381 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4382 | if (repsize==1) { |
| 4383 | /* no overflow check, because we know that the space is enough */ |
| 4384 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 4385 | } |
| 4386 | else if (repsize!=0) { |
| 4387 | /* more than one character */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4388 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
Walter Dörwald | cd736e7 | 2004-02-05 17:36:00 +0000 | [diff] [blame] | 4389 | (insize - (curinp-startinp)) + |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4390 | repsize - 1; |
| 4391 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4392 | return -1; |
| 4393 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 4394 | *outp += repsize; |
| 4395 | } |
| 4396 | } |
| 4397 | else |
| 4398 | return -1; |
| 4399 | return 0; |
| 4400 | } |
| 4401 | |
| 4402 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4403 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4404 | PyObject *mapping, |
| 4405 | const char *errors) |
| 4406 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4407 | /* output object */ |
| 4408 | PyObject *res = NULL; |
| 4409 | /* pointers to the beginning and end+1 of input */ |
| 4410 | const Py_UNICODE *startp = p; |
| 4411 | const Py_UNICODE *endp = p + size; |
| 4412 | /* pointer into the output */ |
| 4413 | Py_UNICODE *str; |
| 4414 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4415 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4416 | char *reason = "character maps to <undefined>"; |
| 4417 | PyObject *errorHandler = NULL; |
| 4418 | PyObject *exc = NULL; |
| 4419 | /* the following variable is used for caching string comparisons |
| 4420 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4421 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4422 | int known_errorHandler = -1; |
| 4423 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4424 | if (mapping == NULL) { |
| 4425 | PyErr_BadArgument(); |
| 4426 | return NULL; |
| 4427 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4428 | |
| 4429 | /* allocate enough for a simple 1:1 translation without |
| 4430 | replacements, if we need more, we'll resize */ |
| 4431 | res = PyUnicode_FromUnicode(NULL, size); |
| 4432 | if (res == NULL) |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4433 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4434 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4435 | return res; |
| 4436 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4437 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4438 | while (p<endp) { |
| 4439 | /* try to encode it */ |
| 4440 | PyObject *x = NULL; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4441 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4442 | Py_XDECREF(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4443 | goto onError; |
| 4444 | } |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 4445 | Py_XDECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4446 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 4447 | ++p; |
| 4448 | else { /* untranslatable character */ |
| 4449 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4450 | Py_ssize_t repsize; |
| 4451 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4452 | Py_UNICODE *uni2; |
| 4453 | /* startpos for collecting untranslatable chars */ |
| 4454 | const Py_UNICODE *collstart = p; |
| 4455 | const Py_UNICODE *collend = p+1; |
| 4456 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4457 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4458 | /* find all untranslatable characters */ |
| 4459 | while (collend < endp) { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4460 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4461 | goto onError; |
| 4462 | Py_XDECREF(x); |
| 4463 | if (x!=Py_None) |
| 4464 | break; |
| 4465 | ++collend; |
| 4466 | } |
| 4467 | /* cache callback name lookup |
| 4468 | * (if not done yet, i.e. it's the first error) */ |
| 4469 | if (known_errorHandler==-1) { |
| 4470 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4471 | known_errorHandler = 1; |
| 4472 | else if (!strcmp(errors, "replace")) |
| 4473 | known_errorHandler = 2; |
| 4474 | else if (!strcmp(errors, "ignore")) |
| 4475 | known_errorHandler = 3; |
| 4476 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4477 | known_errorHandler = 4; |
| 4478 | else |
| 4479 | known_errorHandler = 0; |
| 4480 | } |
| 4481 | switch (known_errorHandler) { |
| 4482 | case 1: /* strict */ |
| 4483 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
| 4484 | goto onError; |
| 4485 | case 2: /* replace */ |
| 4486 | /* No need to check for space, this is a 1:1 replacement */ |
| 4487 | for (coll = collstart; coll<collend; ++coll) |
| 4488 | *str++ = '?'; |
| 4489 | /* fall through */ |
| 4490 | case 3: /* ignore */ |
| 4491 | p = collend; |
| 4492 | break; |
| 4493 | case 4: /* xmlcharrefreplace */ |
| 4494 | /* generate replacement (temporarily (mis)uses p) */ |
| 4495 | for (p = collstart; p < collend; ++p) { |
| 4496 | char buffer[2+29+1+1]; |
| 4497 | char *cp; |
| 4498 | sprintf(buffer, "&#%d;", (int)*p); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4499 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4500 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 4501 | goto onError; |
| 4502 | for (cp = buffer; *cp; ++cp) |
| 4503 | *str++ = *cp; |
| 4504 | } |
| 4505 | p = collend; |
| 4506 | break; |
| 4507 | default: |
| 4508 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 4509 | reason, startp, size, &exc, |
| 4510 | collstart-startp, collend-startp, &newpos); |
| 4511 | if (repunicode == NULL) |
| 4512 | goto onError; |
| 4513 | /* generate replacement */ |
| 4514 | repsize = PyUnicode_GET_SIZE(repunicode); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4515 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4516 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 4517 | Py_DECREF(repunicode); |
| 4518 | goto onError; |
| 4519 | } |
| 4520 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 4521 | *str++ = *uni2; |
| 4522 | p = startp + newpos; |
| 4523 | Py_DECREF(repunicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4524 | } |
| 4525 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4526 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4527 | /* Resize if we allocated to much */ |
| 4528 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4529 | if (respos<PyUnicode_GET_SIZE(res)) { |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4530 | if (_PyUnicode_Resize(&res, respos) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 4531 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4532 | } |
| 4533 | Py_XDECREF(exc); |
| 4534 | Py_XDECREF(errorHandler); |
| 4535 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4536 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4537 | onError: |
| 4538 | Py_XDECREF(res); |
| 4539 | Py_XDECREF(exc); |
| 4540 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4541 | return NULL; |
| 4542 | } |
| 4543 | |
| 4544 | PyObject *PyUnicode_Translate(PyObject *str, |
| 4545 | PyObject *mapping, |
| 4546 | const char *errors) |
| 4547 | { |
| 4548 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4549 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4550 | str = PyUnicode_FromObject(str); |
| 4551 | if (str == NULL) |
| 4552 | goto onError; |
| 4553 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 4554 | PyUnicode_GET_SIZE(str), |
| 4555 | mapping, |
| 4556 | errors); |
| 4557 | Py_DECREF(str); |
| 4558 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4559 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4560 | onError: |
| 4561 | Py_XDECREF(str); |
| 4562 | return NULL; |
| 4563 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4564 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4565 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 4566 | |
| 4567 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4568 | Py_ssize_t length, |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4569 | char *output, |
| 4570 | const char *errors) |
| 4571 | { |
| 4572 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4573 | PyObject *errorHandler = NULL; |
| 4574 | PyObject *exc = NULL; |
| 4575 | const char *encoding = "decimal"; |
| 4576 | const char *reason = "invalid decimal Unicode string"; |
| 4577 | /* the following variable is used for caching string comparisons |
| 4578 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 4579 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4580 | |
| 4581 | if (output == NULL) { |
| 4582 | PyErr_BadArgument(); |
| 4583 | return -1; |
| 4584 | } |
| 4585 | |
| 4586 | p = s; |
| 4587 | end = s + length; |
| 4588 | while (p < end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4589 | register Py_UNICODE ch = *p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4590 | int decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4591 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4592 | Py_ssize_t repsize; |
| 4593 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4594 | Py_UNICODE *uni2; |
| 4595 | Py_UNICODE *collstart; |
| 4596 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4597 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4598 | if (Py_UNICODE_ISSPACE(ch)) { |
| 4599 | *output++ = ' '; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4600 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4601 | continue; |
| 4602 | } |
| 4603 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 4604 | if (decimal >= 0) { |
| 4605 | *output++ = '0' + decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4606 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4607 | continue; |
| 4608 | } |
Guido van Rossum | ba47704 | 2000-04-06 18:18:10 +0000 | [diff] [blame] | 4609 | if (0 < ch && ch < 256) { |
Guido van Rossum | 42c29aa | 2000-05-03 23:58:29 +0000 | [diff] [blame] | 4610 | *output++ = (char)ch; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4611 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4612 | continue; |
| 4613 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4614 | /* All other characters are considered unencodable */ |
| 4615 | collstart = p; |
| 4616 | collend = p+1; |
| 4617 | while (collend < end) { |
| 4618 | if ((0 < *collend && *collend < 256) || |
| 4619 | !Py_UNICODE_ISSPACE(*collend) || |
| 4620 | Py_UNICODE_TODECIMAL(*collend)) |
| 4621 | break; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4622 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4623 | /* cache callback name lookup |
| 4624 | * (if not done yet, i.e. it's the first error) */ |
| 4625 | if (known_errorHandler==-1) { |
| 4626 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4627 | known_errorHandler = 1; |
| 4628 | else if (!strcmp(errors, "replace")) |
| 4629 | known_errorHandler = 2; |
| 4630 | else if (!strcmp(errors, "ignore")) |
| 4631 | known_errorHandler = 3; |
| 4632 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4633 | known_errorHandler = 4; |
| 4634 | else |
| 4635 | known_errorHandler = 0; |
| 4636 | } |
| 4637 | switch (known_errorHandler) { |
| 4638 | case 1: /* strict */ |
| 4639 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 4640 | goto onError; |
| 4641 | case 2: /* replace */ |
| 4642 | for (p = collstart; p < collend; ++p) |
| 4643 | *output++ = '?'; |
| 4644 | /* fall through */ |
| 4645 | case 3: /* ignore */ |
| 4646 | p = collend; |
| 4647 | break; |
| 4648 | case 4: /* xmlcharrefreplace */ |
| 4649 | /* generate replacement (temporarily (mis)uses p) */ |
| 4650 | for (p = collstart; p < collend; ++p) |
| 4651 | output += sprintf(output, "&#%d;", (int)*p); |
| 4652 | p = collend; |
| 4653 | break; |
| 4654 | default: |
| 4655 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 4656 | encoding, reason, s, length, &exc, |
| 4657 | collstart-s, collend-s, &newpos); |
| 4658 | if (repunicode == NULL) |
| 4659 | goto onError; |
| 4660 | /* generate replacement */ |
| 4661 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4662 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 4663 | Py_UNICODE ch = *uni2; |
| 4664 | if (Py_UNICODE_ISSPACE(ch)) |
| 4665 | *output++ = ' '; |
| 4666 | else { |
| 4667 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 4668 | if (decimal >= 0) |
| 4669 | *output++ = '0' + decimal; |
| 4670 | else if (0 < ch && ch < 256) |
| 4671 | *output++ = (char)ch; |
| 4672 | else { |
| 4673 | Py_DECREF(repunicode); |
| 4674 | raise_encode_exception(&exc, encoding, |
| 4675 | s, length, collstart-s, collend-s, reason); |
| 4676 | goto onError; |
| 4677 | } |
| 4678 | } |
| 4679 | } |
| 4680 | p = s + newpos; |
| 4681 | Py_DECREF(repunicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4682 | } |
| 4683 | } |
| 4684 | /* 0-terminate the output string */ |
| 4685 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4686 | Py_XDECREF(exc); |
| 4687 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4688 | return 0; |
| 4689 | |
| 4690 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4691 | Py_XDECREF(exc); |
| 4692 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4693 | return -1; |
| 4694 | } |
| 4695 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4696 | /* --- Helpers ------------------------------------------------------------ */ |
| 4697 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4698 | #define STRINGLIB_CHAR Py_UNICODE |
| 4699 | |
| 4700 | #define STRINGLIB_LEN PyUnicode_GET_SIZE |
| 4701 | #define STRINGLIB_NEW PyUnicode_FromUnicode |
| 4702 | #define STRINGLIB_STR PyUnicode_AS_UNICODE |
| 4703 | |
| 4704 | Py_LOCAL_INLINE(int) |
| 4705 | STRINGLIB_CMP(const Py_UNICODE* str, const Py_UNICODE* other, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4706 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4707 | if (str[0] != other[0]) |
| 4708 | return 1; |
| 4709 | return memcmp((void*) str, (void*) other, len * sizeof(Py_UNICODE)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4710 | } |
| 4711 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4712 | #define STRINGLIB_EMPTY unicode_empty |
| 4713 | |
| 4714 | #include "stringlib/fastsearch.h" |
| 4715 | |
| 4716 | #include "stringlib/count.h" |
| 4717 | #include "stringlib/find.h" |
| 4718 | #include "stringlib/partition.h" |
| 4719 | |
| 4720 | /* helper macro to fixup start/end slice values */ |
| 4721 | #define FIX_START_END(obj) \ |
| 4722 | if (start < 0) \ |
| 4723 | start += (obj)->length; \ |
| 4724 | if (start < 0) \ |
| 4725 | start = 0; \ |
| 4726 | if (end > (obj)->length) \ |
| 4727 | end = (obj)->length; \ |
| 4728 | if (end < 0) \ |
| 4729 | end += (obj)->length; \ |
| 4730 | if (end < 0) \ |
| 4731 | end = 0; |
| 4732 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4733 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4734 | PyObject *substr, |
| 4735 | Py_ssize_t start, |
| 4736 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4737 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4738 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4739 | PyUnicodeObject* str_obj; |
| 4740 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4741 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4742 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 4743 | if (!str_obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4744 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4745 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 4746 | if (!sub_obj) { |
| 4747 | Py_DECREF(str_obj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4748 | return -1; |
| 4749 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4750 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4751 | FIX_START_END(str_obj); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4752 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4753 | result = stringlib_count( |
| 4754 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
| 4755 | ); |
| 4756 | |
| 4757 | Py_DECREF(sub_obj); |
| 4758 | Py_DECREF(str_obj); |
| 4759 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4760 | return result; |
| 4761 | } |
| 4762 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4763 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4764 | PyObject *sub, |
| 4765 | Py_ssize_t start, |
| 4766 | Py_ssize_t end, |
| 4767 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4768 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4769 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4770 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4771 | str = PyUnicode_FromObject(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4772 | if (!str) |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 4773 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4774 | sub = PyUnicode_FromObject(sub); |
| 4775 | if (!sub) { |
Marc-André Lemburg | 4164439 | 2002-05-29 13:46:29 +0000 | [diff] [blame] | 4776 | Py_DECREF(str); |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 4777 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4778 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4779 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4780 | if (direction > 0) |
| 4781 | result = stringlib_find_slice( |
| 4782 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 4783 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 4784 | start, end |
| 4785 | ); |
| 4786 | else |
| 4787 | result = stringlib_rfind_slice( |
| 4788 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 4789 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 4790 | start, end |
| 4791 | ); |
| 4792 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4793 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4794 | Py_DECREF(sub); |
| 4795 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4796 | return result; |
| 4797 | } |
| 4798 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4799 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4800 | int tailmatch(PyUnicodeObject *self, |
| 4801 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4802 | Py_ssize_t start, |
| 4803 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4804 | int direction) |
| 4805 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4806 | if (substring->length == 0) |
| 4807 | return 1; |
| 4808 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4809 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4810 | |
| 4811 | end -= substring->length; |
| 4812 | if (end < start) |
| 4813 | return 0; |
| 4814 | |
| 4815 | if (direction > 0) { |
| 4816 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 4817 | return 1; |
| 4818 | } else { |
| 4819 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 4820 | return 1; |
| 4821 | } |
| 4822 | |
| 4823 | return 0; |
| 4824 | } |
| 4825 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4826 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4827 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4828 | Py_ssize_t start, |
| 4829 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4830 | int direction) |
| 4831 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4832 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4833 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4834 | str = PyUnicode_FromObject(str); |
| 4835 | if (str == NULL) |
| 4836 | return -1; |
| 4837 | substr = PyUnicode_FromObject(substr); |
| 4838 | if (substr == NULL) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 4839 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4840 | return -1; |
| 4841 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4842 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4843 | result = tailmatch((PyUnicodeObject *)str, |
| 4844 | (PyUnicodeObject *)substr, |
| 4845 | start, end, direction); |
| 4846 | Py_DECREF(str); |
| 4847 | Py_DECREF(substr); |
| 4848 | return result; |
| 4849 | } |
| 4850 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4851 | /* Apply fixfct filter to the Unicode object self and return a |
| 4852 | reference to the modified object */ |
| 4853 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4854 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4855 | PyObject *fixup(PyUnicodeObject *self, |
| 4856 | int (*fixfct)(PyUnicodeObject *s)) |
| 4857 | { |
| 4858 | |
| 4859 | PyUnicodeObject *u; |
| 4860 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4861 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4862 | if (u == NULL) |
| 4863 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4864 | |
| 4865 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 4866 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4867 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4868 | /* fixfct should return TRUE if it modified the buffer. If |
| 4869 | FALSE, return a reference to the original buffer instead |
| 4870 | (to save space, not time) */ |
| 4871 | Py_INCREF(self); |
| 4872 | Py_DECREF(u); |
| 4873 | return (PyObject*) self; |
| 4874 | } |
| 4875 | return (PyObject*) u; |
| 4876 | } |
| 4877 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4878 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4879 | int fixupper(PyUnicodeObject *self) |
| 4880 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4881 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4882 | Py_UNICODE *s = self->str; |
| 4883 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4884 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4885 | while (len-- > 0) { |
| 4886 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4887 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4888 | ch = Py_UNICODE_TOUPPER(*s); |
| 4889 | if (ch != *s) { |
| 4890 | status = 1; |
| 4891 | *s = ch; |
| 4892 | } |
| 4893 | s++; |
| 4894 | } |
| 4895 | |
| 4896 | return status; |
| 4897 | } |
| 4898 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4899 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4900 | int fixlower(PyUnicodeObject *self) |
| 4901 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4902 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4903 | Py_UNICODE *s = self->str; |
| 4904 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4905 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4906 | while (len-- > 0) { |
| 4907 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4908 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4909 | ch = Py_UNICODE_TOLOWER(*s); |
| 4910 | if (ch != *s) { |
| 4911 | status = 1; |
| 4912 | *s = ch; |
| 4913 | } |
| 4914 | s++; |
| 4915 | } |
| 4916 | |
| 4917 | return status; |
| 4918 | } |
| 4919 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4920 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4921 | int fixswapcase(PyUnicodeObject *self) |
| 4922 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4923 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4924 | Py_UNICODE *s = self->str; |
| 4925 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4926 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4927 | while (len-- > 0) { |
| 4928 | if (Py_UNICODE_ISUPPER(*s)) { |
| 4929 | *s = Py_UNICODE_TOLOWER(*s); |
| 4930 | status = 1; |
| 4931 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 4932 | *s = Py_UNICODE_TOUPPER(*s); |
| 4933 | status = 1; |
| 4934 | } |
| 4935 | s++; |
| 4936 | } |
| 4937 | |
| 4938 | return status; |
| 4939 | } |
| 4940 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4941 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4942 | int fixcapitalize(PyUnicodeObject *self) |
| 4943 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4944 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4945 | Py_UNICODE *s = self->str; |
| 4946 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4947 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4948 | if (len == 0) |
| 4949 | return 0; |
| 4950 | if (Py_UNICODE_ISLOWER(*s)) { |
| 4951 | *s = Py_UNICODE_TOUPPER(*s); |
| 4952 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4953 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4954 | s++; |
| 4955 | while (--len > 0) { |
| 4956 | if (Py_UNICODE_ISUPPER(*s)) { |
| 4957 | *s = Py_UNICODE_TOLOWER(*s); |
| 4958 | status = 1; |
| 4959 | } |
| 4960 | s++; |
| 4961 | } |
| 4962 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4963 | } |
| 4964 | |
| 4965 | static |
| 4966 | int fixtitle(PyUnicodeObject *self) |
| 4967 | { |
| 4968 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 4969 | register Py_UNICODE *e; |
| 4970 | int previous_is_cased; |
| 4971 | |
| 4972 | /* Shortcut for single character strings */ |
| 4973 | if (PyUnicode_GET_SIZE(self) == 1) { |
| 4974 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 4975 | if (*p != ch) { |
| 4976 | *p = ch; |
| 4977 | return 1; |
| 4978 | } |
| 4979 | else |
| 4980 | return 0; |
| 4981 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4982 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4983 | e = p + PyUnicode_GET_SIZE(self); |
| 4984 | previous_is_cased = 0; |
| 4985 | for (; p < e; p++) { |
| 4986 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4987 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4988 | if (previous_is_cased) |
| 4989 | *p = Py_UNICODE_TOLOWER(ch); |
| 4990 | else |
| 4991 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4992 | |
| 4993 | if (Py_UNICODE_ISLOWER(ch) || |
| 4994 | Py_UNICODE_ISUPPER(ch) || |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4995 | Py_UNICODE_ISTITLE(ch)) |
| 4996 | previous_is_cased = 1; |
| 4997 | else |
| 4998 | previous_is_cased = 0; |
| 4999 | } |
| 5000 | return 1; |
| 5001 | } |
| 5002 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5003 | PyObject * |
| 5004 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5005 | { |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5006 | PyObject *internal_separator = NULL; |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 5007 | const Py_UNICODE blank = ' '; |
| 5008 | const Py_UNICODE *sep = ␣ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5009 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5010 | PyUnicodeObject *res = NULL; /* the result */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5011 | Py_ssize_t res_alloc = 100; /* # allocated bytes for string in res */ |
| 5012 | Py_ssize_t res_used; /* # used bytes */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5013 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 5014 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5015 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5016 | PyObject *item; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5017 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5018 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5019 | fseq = PySequence_Fast(seq, ""); |
| 5020 | if (fseq == NULL) { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5021 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5022 | } |
| 5023 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5024 | /* Grrrr. A codec may be invoked to convert str objects to |
| 5025 | * Unicode, and so it's possible to call back into Python code |
| 5026 | * during PyUnicode_FromObject(), and so it's possible for a sick |
| 5027 | * codec to change the size of fseq (if seq is a list). Therefore |
| 5028 | * we have to keep refetching the size -- can't assume seqlen |
| 5029 | * is invariant. |
| 5030 | */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5031 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5032 | /* If empty sequence, return u"". */ |
| 5033 | if (seqlen == 0) { |
| 5034 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 5035 | goto Done; |
| 5036 | } |
| 5037 | /* If singleton sequence with an exact Unicode, return that. */ |
| 5038 | if (seqlen == 1) { |
| 5039 | item = PySequence_Fast_GET_ITEM(fseq, 0); |
| 5040 | if (PyUnicode_CheckExact(item)) { |
| 5041 | Py_INCREF(item); |
| 5042 | res = (PyUnicodeObject *)item; |
| 5043 | goto Done; |
| 5044 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5045 | } |
| 5046 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5047 | /* At least two items to join, or one that isn't exact Unicode. */ |
| 5048 | if (seqlen > 1) { |
| 5049 | /* Set up sep and seplen -- they're needed. */ |
| 5050 | if (separator == NULL) { |
| 5051 | sep = ␣ |
| 5052 | seplen = 1; |
| 5053 | } |
| 5054 | else { |
| 5055 | internal_separator = PyUnicode_FromObject(separator); |
| 5056 | if (internal_separator == NULL) |
| 5057 | goto onError; |
| 5058 | sep = PyUnicode_AS_UNICODE(internal_separator); |
| 5059 | seplen = PyUnicode_GET_SIZE(internal_separator); |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5060 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 5061 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5062 | } |
| 5063 | } |
| 5064 | |
| 5065 | /* Get space. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5066 | res = _PyUnicode_New(res_alloc); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5067 | if (res == NULL) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5068 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5069 | res_p = PyUnicode_AS_UNICODE(res); |
| 5070 | res_used = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5071 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5072 | for (i = 0; i < seqlen; ++i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5073 | Py_ssize_t itemlen; |
| 5074 | Py_ssize_t new_res_used; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5075 | |
| 5076 | item = PySequence_Fast_GET_ITEM(fseq, i); |
| 5077 | /* Convert item to Unicode. */ |
| 5078 | if (! PyUnicode_Check(item) && ! PyString_Check(item)) { |
| 5079 | PyErr_Format(PyExc_TypeError, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5080 | "sequence item %zd: expected string or Unicode," |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5081 | " %.80s found", |
| 5082 | i, item->ob_type->tp_name); |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 5083 | goto onError; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5084 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5085 | item = PyUnicode_FromObject(item); |
| 5086 | if (item == NULL) |
| 5087 | goto onError; |
| 5088 | /* We own a reference to item from here on. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5089 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5090 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 5091 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5092 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5093 | /* 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] | 5094 | itemlen = PyUnicode_GET_SIZE(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5095 | new_res_used = res_used + itemlen; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5096 | if (new_res_used < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5097 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5098 | if (i < seqlen - 1) { |
| 5099 | new_res_used += seplen; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5100 | if (new_res_used < 0) |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5101 | goto Overflow; |
| 5102 | } |
| 5103 | if (new_res_used > res_alloc) { |
| 5104 | /* double allocated size until it's big enough */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5105 | do { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5106 | res_alloc += res_alloc; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5107 | if (res_alloc <= 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5108 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5109 | } while (new_res_used > res_alloc); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5110 | if (_PyUnicode_Resize(&res, res_alloc) < 0) { |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 5111 | Py_DECREF(item); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5112 | goto onError; |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 5113 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5114 | res_p = PyUnicode_AS_UNICODE(res) + res_used; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5115 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5116 | |
| 5117 | /* Copy item, and maybe the separator. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5118 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5119 | res_p += itemlen; |
| 5120 | if (i < seqlen - 1) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5121 | Py_UNICODE_COPY(res_p, sep, seplen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5122 | res_p += seplen; |
| 5123 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5124 | Py_DECREF(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5125 | res_used = new_res_used; |
| 5126 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5127 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5128 | /* Shrink res to match the used area; this probably can't fail, |
| 5129 | * but it's cheap to check. |
| 5130 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5131 | if (_PyUnicode_Resize(&res, res_used) < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5132 | goto onError; |
| 5133 | |
| 5134 | Done: |
| 5135 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5136 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5137 | return (PyObject *)res; |
| 5138 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5139 | Overflow: |
| 5140 | PyErr_SetString(PyExc_OverflowError, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5141 | "join() result is too long for a Python string"); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5142 | Py_DECREF(item); |
| 5143 | /* fall through */ |
| 5144 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5145 | onError: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5146 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5147 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5148 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5149 | return NULL; |
| 5150 | } |
| 5151 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5152 | static |
| 5153 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5154 | Py_ssize_t left, |
| 5155 | Py_ssize_t right, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5156 | Py_UNICODE fill) |
| 5157 | { |
| 5158 | PyUnicodeObject *u; |
| 5159 | |
| 5160 | if (left < 0) |
| 5161 | left = 0; |
| 5162 | if (right < 0) |
| 5163 | right = 0; |
| 5164 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5165 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5166 | Py_INCREF(self); |
| 5167 | return self; |
| 5168 | } |
| 5169 | |
| 5170 | u = _PyUnicode_New(left + self->length + right); |
| 5171 | if (u) { |
| 5172 | if (left) |
| 5173 | Py_UNICODE_FILL(u->str, fill, left); |
| 5174 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 5175 | if (right) |
| 5176 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 5177 | } |
| 5178 | |
| 5179 | return u; |
| 5180 | } |
| 5181 | |
| 5182 | #define SPLIT_APPEND(data, left, right) \ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5183 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5184 | if (!str) \ |
| 5185 | goto onError; \ |
| 5186 | if (PyList_Append(list, str)) { \ |
| 5187 | Py_DECREF(str); \ |
| 5188 | goto onError; \ |
| 5189 | } \ |
| 5190 | else \ |
| 5191 | Py_DECREF(str); |
| 5192 | |
| 5193 | static |
| 5194 | PyObject *split_whitespace(PyUnicodeObject *self, |
| 5195 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5196 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5197 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5198 | register Py_ssize_t i; |
| 5199 | register Py_ssize_t j; |
| 5200 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5201 | PyObject *str; |
| 5202 | |
| 5203 | for (i = j = 0; i < len; ) { |
| 5204 | /* find a token */ |
| 5205 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 5206 | i++; |
| 5207 | j = i; |
| 5208 | while (i < len && !Py_UNICODE_ISSPACE(self->str[i])) |
| 5209 | i++; |
| 5210 | if (j < i) { |
| 5211 | if (maxcount-- <= 0) |
| 5212 | break; |
| 5213 | SPLIT_APPEND(self->str, j, i); |
| 5214 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 5215 | i++; |
| 5216 | j = i; |
| 5217 | } |
| 5218 | } |
| 5219 | if (j < len) { |
| 5220 | SPLIT_APPEND(self->str, j, len); |
| 5221 | } |
| 5222 | return list; |
| 5223 | |
| 5224 | onError: |
| 5225 | Py_DECREF(list); |
| 5226 | return NULL; |
| 5227 | } |
| 5228 | |
| 5229 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5230 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5231 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5232 | register Py_ssize_t i; |
| 5233 | register Py_ssize_t j; |
| 5234 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5235 | PyObject *list; |
| 5236 | PyObject *str; |
| 5237 | Py_UNICODE *data; |
| 5238 | |
| 5239 | string = PyUnicode_FromObject(string); |
| 5240 | if (string == NULL) |
| 5241 | return NULL; |
| 5242 | data = PyUnicode_AS_UNICODE(string); |
| 5243 | len = PyUnicode_GET_SIZE(string); |
| 5244 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5245 | list = PyList_New(0); |
| 5246 | if (!list) |
| 5247 | goto onError; |
| 5248 | |
| 5249 | for (i = j = 0; i < len; ) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5250 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5251 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5252 | /* Find a line and append it */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5253 | while (i < len && !BLOOM_LINEBREAK(data[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5254 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5255 | |
| 5256 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5257 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5258 | if (i < len) { |
| 5259 | if (data[i] == '\r' && i + 1 < len && |
| 5260 | data[i+1] == '\n') |
| 5261 | i += 2; |
| 5262 | else |
| 5263 | i++; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5264 | if (keepends) |
| 5265 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5266 | } |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5267 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5268 | j = i; |
| 5269 | } |
| 5270 | if (j < len) { |
| 5271 | SPLIT_APPEND(data, j, len); |
| 5272 | } |
| 5273 | |
| 5274 | Py_DECREF(string); |
| 5275 | return list; |
| 5276 | |
| 5277 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5278 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5279 | Py_DECREF(string); |
| 5280 | return NULL; |
| 5281 | } |
| 5282 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5283 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5284 | PyObject *split_char(PyUnicodeObject *self, |
| 5285 | PyObject *list, |
| 5286 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5287 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5288 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5289 | register Py_ssize_t i; |
| 5290 | register Py_ssize_t j; |
| 5291 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5292 | PyObject *str; |
| 5293 | |
| 5294 | for (i = j = 0; i < len; ) { |
| 5295 | if (self->str[i] == ch) { |
| 5296 | if (maxcount-- <= 0) |
| 5297 | break; |
| 5298 | SPLIT_APPEND(self->str, j, i); |
| 5299 | i = j = i + 1; |
| 5300 | } else |
| 5301 | i++; |
| 5302 | } |
| 5303 | if (j <= len) { |
| 5304 | SPLIT_APPEND(self->str, j, len); |
| 5305 | } |
| 5306 | return list; |
| 5307 | |
| 5308 | onError: |
| 5309 | Py_DECREF(list); |
| 5310 | return NULL; |
| 5311 | } |
| 5312 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5313 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5314 | PyObject *split_substring(PyUnicodeObject *self, |
| 5315 | PyObject *list, |
| 5316 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5317 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5318 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5319 | register Py_ssize_t i; |
| 5320 | register Py_ssize_t j; |
| 5321 | Py_ssize_t len = self->length; |
| 5322 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5323 | PyObject *str; |
| 5324 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 5325 | for (i = j = 0; i <= len - sublen; ) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5326 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5327 | if (maxcount-- <= 0) |
| 5328 | break; |
| 5329 | SPLIT_APPEND(self->str, j, i); |
| 5330 | i = j = i + sublen; |
| 5331 | } else |
| 5332 | i++; |
| 5333 | } |
| 5334 | if (j <= len) { |
| 5335 | SPLIT_APPEND(self->str, j, len); |
| 5336 | } |
| 5337 | return list; |
| 5338 | |
| 5339 | onError: |
| 5340 | Py_DECREF(list); |
| 5341 | return NULL; |
| 5342 | } |
| 5343 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5344 | static |
| 5345 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
| 5346 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5347 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5348 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5349 | register Py_ssize_t i; |
| 5350 | register Py_ssize_t j; |
| 5351 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5352 | PyObject *str; |
| 5353 | |
| 5354 | for (i = j = len - 1; i >= 0; ) { |
| 5355 | /* find a token */ |
| 5356 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 5357 | i--; |
| 5358 | j = i; |
| 5359 | while (i >= 0 && !Py_UNICODE_ISSPACE(self->str[i])) |
| 5360 | i--; |
| 5361 | if (j > i) { |
| 5362 | if (maxcount-- <= 0) |
| 5363 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5364 | SPLIT_APPEND(self->str, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5365 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 5366 | i--; |
| 5367 | j = i; |
| 5368 | } |
| 5369 | } |
| 5370 | if (j >= 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5371 | SPLIT_APPEND(self->str, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5372 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5373 | if (PyList_Reverse(list) < 0) |
| 5374 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5375 | return list; |
| 5376 | |
| 5377 | onError: |
| 5378 | Py_DECREF(list); |
| 5379 | return NULL; |
| 5380 | } |
| 5381 | |
| 5382 | static |
| 5383 | PyObject *rsplit_char(PyUnicodeObject *self, |
| 5384 | PyObject *list, |
| 5385 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5386 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5387 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5388 | register Py_ssize_t i; |
| 5389 | register Py_ssize_t j; |
| 5390 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5391 | PyObject *str; |
| 5392 | |
| 5393 | for (i = j = len - 1; i >= 0; ) { |
| 5394 | if (self->str[i] == ch) { |
| 5395 | if (maxcount-- <= 0) |
| 5396 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5397 | SPLIT_APPEND(self->str, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5398 | j = i = i - 1; |
| 5399 | } else |
| 5400 | i--; |
| 5401 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 5402 | if (j >= -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5403 | SPLIT_APPEND(self->str, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5404 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5405 | if (PyList_Reverse(list) < 0) |
| 5406 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5407 | return list; |
| 5408 | |
| 5409 | onError: |
| 5410 | Py_DECREF(list); |
| 5411 | return NULL; |
| 5412 | } |
| 5413 | |
| 5414 | static |
| 5415 | PyObject *rsplit_substring(PyUnicodeObject *self, |
| 5416 | PyObject *list, |
| 5417 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5418 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5419 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5420 | register Py_ssize_t i; |
| 5421 | register Py_ssize_t j; |
| 5422 | Py_ssize_t len = self->length; |
| 5423 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5424 | PyObject *str; |
| 5425 | |
| 5426 | for (i = len - sublen, j = len; i >= 0; ) { |
| 5427 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5428 | if (maxcount-- <= 0) |
| 5429 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5430 | SPLIT_APPEND(self->str, i + sublen, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5431 | j = i; |
| 5432 | i -= sublen; |
| 5433 | } else |
| 5434 | i--; |
| 5435 | } |
| 5436 | if (j >= 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5437 | SPLIT_APPEND(self->str, 0, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5438 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5439 | if (PyList_Reverse(list) < 0) |
| 5440 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5441 | return list; |
| 5442 | |
| 5443 | onError: |
| 5444 | Py_DECREF(list); |
| 5445 | return NULL; |
| 5446 | } |
| 5447 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5448 | #undef SPLIT_APPEND |
| 5449 | |
| 5450 | static |
| 5451 | PyObject *split(PyUnicodeObject *self, |
| 5452 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5453 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5454 | { |
| 5455 | PyObject *list; |
| 5456 | |
| 5457 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5458 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5459 | |
| 5460 | list = PyList_New(0); |
| 5461 | if (!list) |
| 5462 | return NULL; |
| 5463 | |
| 5464 | if (substring == NULL) |
| 5465 | return split_whitespace(self,list,maxcount); |
| 5466 | |
| 5467 | else if (substring->length == 1) |
| 5468 | return split_char(self,list,substring->str[0],maxcount); |
| 5469 | |
| 5470 | else if (substring->length == 0) { |
| 5471 | Py_DECREF(list); |
| 5472 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5473 | return NULL; |
| 5474 | } |
| 5475 | else |
| 5476 | return split_substring(self,list,substring,maxcount); |
| 5477 | } |
| 5478 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5479 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5480 | PyObject *rsplit(PyUnicodeObject *self, |
| 5481 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5482 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5483 | { |
| 5484 | PyObject *list; |
| 5485 | |
| 5486 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5487 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5488 | |
| 5489 | list = PyList_New(0); |
| 5490 | if (!list) |
| 5491 | return NULL; |
| 5492 | |
| 5493 | if (substring == NULL) |
| 5494 | return rsplit_whitespace(self,list,maxcount); |
| 5495 | |
| 5496 | else if (substring->length == 1) |
| 5497 | return rsplit_char(self,list,substring->str[0],maxcount); |
| 5498 | |
| 5499 | else if (substring->length == 0) { |
| 5500 | Py_DECREF(list); |
| 5501 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5502 | return NULL; |
| 5503 | } |
| 5504 | else |
| 5505 | return rsplit_substring(self,list,substring,maxcount); |
| 5506 | } |
| 5507 | |
| 5508 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5509 | PyObject *replace(PyUnicodeObject *self, |
| 5510 | PyUnicodeObject *str1, |
| 5511 | PyUnicodeObject *str2, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5512 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5513 | { |
| 5514 | PyUnicodeObject *u; |
| 5515 | |
| 5516 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5517 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5518 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5519 | if (str1->length == str2->length) { |
| 5520 | /* same length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5521 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5522 | if (str1->length == 1) { |
| 5523 | /* replace characters */ |
| 5524 | Py_UNICODE u1, u2; |
| 5525 | if (!findchar(self->str, self->length, str1->str[0])) |
| 5526 | goto nothing; |
| 5527 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5528 | if (!u) |
| 5529 | return NULL; |
| 5530 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5531 | u1 = str1->str[0]; |
| 5532 | u2 = str2->str[0]; |
| 5533 | for (i = 0; i < u->length; i++) |
| 5534 | if (u->str[i] == u1) { |
| 5535 | if (--maxcount < 0) |
| 5536 | break; |
| 5537 | u->str[i] = u2; |
| 5538 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5539 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5540 | i = fastsearch( |
| 5541 | self->str, self->length, str1->str, str1->length, FAST_SEARCH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5542 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5543 | if (i < 0) |
| 5544 | goto nothing; |
| 5545 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5546 | if (!u) |
| 5547 | return NULL; |
| 5548 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5549 | while (i <= self->length - str1->length) |
| 5550 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 5551 | if (--maxcount < 0) |
| 5552 | break; |
| 5553 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 5554 | i += str1->length; |
| 5555 | } else |
| 5556 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5557 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5558 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5559 | |
| 5560 | Py_ssize_t n, i, j, e; |
| 5561 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5562 | Py_UNICODE *p; |
| 5563 | |
| 5564 | /* replace strings */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5565 | n = stringlib_count(self->str, self->length, str1->str, str1->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5566 | if (n > maxcount) |
| 5567 | n = maxcount; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5568 | if (n == 0) |
| 5569 | goto nothing; |
| 5570 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 5571 | delta = (str2->length - str1->length); |
| 5572 | if (delta == 0) { |
| 5573 | new_size = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5574 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5575 | product = n * (str2->length - str1->length); |
| 5576 | if ((product / (str2->length - str1->length)) != n) { |
| 5577 | PyErr_SetString(PyExc_OverflowError, |
| 5578 | "replace string is too long"); |
| 5579 | return NULL; |
| 5580 | } |
| 5581 | new_size = self->length + product; |
| 5582 | if (new_size < 0) { |
| 5583 | PyErr_SetString(PyExc_OverflowError, |
| 5584 | "replace string is too long"); |
| 5585 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5586 | } |
| 5587 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5588 | u = _PyUnicode_New(new_size); |
| 5589 | if (!u) |
| 5590 | return NULL; |
| 5591 | i = 0; |
| 5592 | p = u->str; |
| 5593 | e = self->length - str1->length; |
| 5594 | if (str1->length > 0) { |
| 5595 | while (n-- > 0) { |
| 5596 | /* look for next match */ |
| 5597 | j = i; |
| 5598 | while (j <= e) { |
| 5599 | if (Py_UNICODE_MATCH(self, j, str1)) |
| 5600 | break; |
| 5601 | j++; |
| 5602 | } |
| 5603 | if (j > i) { |
| 5604 | if (j > e) |
| 5605 | break; |
| 5606 | /* copy unchanged part [i:j] */ |
| 5607 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 5608 | p += j - i; |
| 5609 | } |
| 5610 | /* copy substitution string */ |
| 5611 | if (str2->length > 0) { |
| 5612 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 5613 | p += str2->length; |
| 5614 | } |
| 5615 | i = j + str1->length; |
| 5616 | } |
| 5617 | if (i < self->length) |
| 5618 | /* copy tail [i:] */ |
| 5619 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 5620 | } else { |
| 5621 | /* interleave */ |
| 5622 | while (n > 0) { |
| 5623 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 5624 | p += str2->length; |
| 5625 | if (--n <= 0) |
| 5626 | break; |
| 5627 | *p++ = self->str[i++]; |
| 5628 | } |
| 5629 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 5630 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5631 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5632 | return (PyObject *) u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5633 | |
| 5634 | nothing: |
| 5635 | /* nothing to replace; return original string (when possible) */ |
| 5636 | if (PyUnicode_CheckExact(self)) { |
| 5637 | Py_INCREF(self); |
| 5638 | return (PyObject *) self; |
| 5639 | } |
| 5640 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5641 | } |
| 5642 | |
| 5643 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 5644 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5645 | PyDoc_STRVAR(title__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5646 | "S.title() -> unicode\n\ |
| 5647 | \n\ |
| 5648 | 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] | 5649 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5650 | |
| 5651 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5652 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5653 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5654 | return fixup(self, fixtitle); |
| 5655 | } |
| 5656 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5657 | PyDoc_STRVAR(capitalize__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5658 | "S.capitalize() -> unicode\n\ |
| 5659 | \n\ |
| 5660 | 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] | 5661 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5662 | |
| 5663 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5664 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5665 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5666 | return fixup(self, fixcapitalize); |
| 5667 | } |
| 5668 | |
| 5669 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5670 | PyDoc_STRVAR(capwords__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5671 | "S.capwords() -> unicode\n\ |
| 5672 | \n\ |
| 5673 | 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] | 5674 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5675 | |
| 5676 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5677 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5678 | { |
| 5679 | PyObject *list; |
| 5680 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5681 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5682 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5683 | /* Split into words */ |
| 5684 | list = split(self, NULL, -1); |
| 5685 | if (!list) |
| 5686 | return NULL; |
| 5687 | |
| 5688 | /* Capitalize each word */ |
| 5689 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 5690 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 5691 | fixcapitalize); |
| 5692 | if (item == NULL) |
| 5693 | goto onError; |
| 5694 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 5695 | PyList_SET_ITEM(list, i, item); |
| 5696 | } |
| 5697 | |
| 5698 | /* Join the words to form a new string */ |
| 5699 | item = PyUnicode_Join(NULL, list); |
| 5700 | |
| 5701 | onError: |
| 5702 | Py_DECREF(list); |
| 5703 | return (PyObject *)item; |
| 5704 | } |
| 5705 | #endif |
| 5706 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5707 | /* Argument converter. Coerces to a single unicode character */ |
| 5708 | |
| 5709 | static int |
| 5710 | convert_uc(PyObject *obj, void *addr) |
| 5711 | { |
| 5712 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 5713 | PyObject *uniobj; |
| 5714 | Py_UNICODE *unistr; |
| 5715 | |
| 5716 | uniobj = PyUnicode_FromObject(obj); |
| 5717 | if (uniobj == NULL) { |
| 5718 | PyErr_SetString(PyExc_TypeError, |
| 5719 | "The fill character cannot be converted to Unicode"); |
| 5720 | return 0; |
| 5721 | } |
| 5722 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 5723 | PyErr_SetString(PyExc_TypeError, |
| 5724 | "The fill character must be exactly one character long"); |
| 5725 | Py_DECREF(uniobj); |
| 5726 | return 0; |
| 5727 | } |
| 5728 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 5729 | *fillcharloc = unistr[0]; |
| 5730 | Py_DECREF(uniobj); |
| 5731 | return 1; |
| 5732 | } |
| 5733 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5734 | PyDoc_STRVAR(center__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5735 | "S.center(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5736 | \n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5737 | Return S centered in a Unicode string of length width. Padding is\n\ |
| 5738 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5739 | |
| 5740 | static PyObject * |
| 5741 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 5742 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5743 | Py_ssize_t marg, left; |
| 5744 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5745 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5746 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 5747 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5748 | return NULL; |
| 5749 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5750 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5751 | Py_INCREF(self); |
| 5752 | return (PyObject*) self; |
| 5753 | } |
| 5754 | |
| 5755 | marg = width - self->length; |
| 5756 | left = marg / 2 + (marg & width & 1); |
| 5757 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5758 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5759 | } |
| 5760 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5761 | #if 0 |
| 5762 | |
| 5763 | /* This code should go into some future Unicode collation support |
| 5764 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 5765 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5766 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5767 | /* speedy UTF-16 code point order comparison */ |
| 5768 | /* gleaned from: */ |
| 5769 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 5770 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 5771 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5772 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5773 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5774 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 5775 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 5776 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5777 | }; |
| 5778 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5779 | static int |
| 5780 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 5781 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5782 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5783 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5784 | Py_UNICODE *s1 = str1->str; |
| 5785 | Py_UNICODE *s2 = str2->str; |
| 5786 | |
| 5787 | len1 = str1->length; |
| 5788 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5789 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5790 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5791 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5792 | |
| 5793 | c1 = *s1++; |
| 5794 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 5795 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5796 | if (c1 > (1<<11) * 26) |
| 5797 | c1 += utf16Fixup[c1>>11]; |
| 5798 | if (c2 > (1<<11) * 26) |
| 5799 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5800 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 5801 | |
| 5802 | if (c1 != c2) |
| 5803 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5804 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5805 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5806 | } |
| 5807 | |
| 5808 | return (len1 < len2) ? -1 : (len1 != len2); |
| 5809 | } |
| 5810 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5811 | #else |
| 5812 | |
| 5813 | static int |
| 5814 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 5815 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5816 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5817 | |
| 5818 | Py_UNICODE *s1 = str1->str; |
| 5819 | Py_UNICODE *s2 = str2->str; |
| 5820 | |
| 5821 | len1 = str1->length; |
| 5822 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5823 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5824 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5825 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5826 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 5827 | c1 = *s1++; |
| 5828 | c2 = *s2++; |
| 5829 | |
| 5830 | if (c1 != c2) |
| 5831 | return (c1 < c2) ? -1 : 1; |
| 5832 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5833 | len1--; len2--; |
| 5834 | } |
| 5835 | |
| 5836 | return (len1 < len2) ? -1 : (len1 != len2); |
| 5837 | } |
| 5838 | |
| 5839 | #endif |
| 5840 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5841 | int PyUnicode_Compare(PyObject *left, |
| 5842 | PyObject *right) |
| 5843 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 5844 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) |
| 5845 | return unicode_compare((PyUnicodeObject *)left, |
| 5846 | (PyUnicodeObject *)right); |
| 5847 | if ((PyString_Check(left) && PyUnicode_Check(right)) || |
| 5848 | (PyUnicode_Check(left) && PyString_Check(right))) { |
| 5849 | if (PyUnicode_Check(left)) |
| 5850 | left = _PyUnicode_AsDefaultEncodedString(left, NULL); |
| 5851 | if (PyUnicode_Check(right)) |
| 5852 | right = _PyUnicode_AsDefaultEncodedString(right, NULL); |
| 5853 | assert(PyString_Check(left)); |
| 5854 | assert(PyString_Check(right)); |
| 5855 | return PyObject_Compare(left, right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5856 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 5857 | PyErr_Format(PyExc_TypeError, |
| 5858 | "Can't compare %.100s and %.100s", |
| 5859 | left->ob_type->tp_name, |
| 5860 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5861 | return -1; |
| 5862 | } |
| 5863 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 5864 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 5865 | PyObject *right, |
| 5866 | int op) |
| 5867 | { |
| 5868 | int result; |
| 5869 | |
| 5870 | result = PyUnicode_Compare(left, right); |
| 5871 | if (result == -1 && PyErr_Occurred()) |
| 5872 | goto onError; |
| 5873 | |
| 5874 | /* Convert the return value to a Boolean */ |
| 5875 | switch (op) { |
| 5876 | case Py_EQ: |
| 5877 | result = (result == 0); |
| 5878 | break; |
| 5879 | case Py_NE: |
| 5880 | result = (result != 0); |
| 5881 | break; |
| 5882 | case Py_LE: |
| 5883 | result = (result <= 0); |
| 5884 | break; |
| 5885 | case Py_GE: |
| 5886 | result = (result >= 0); |
| 5887 | break; |
| 5888 | case Py_LT: |
| 5889 | result = (result == -1); |
| 5890 | break; |
| 5891 | case Py_GT: |
| 5892 | result = (result == 1); |
| 5893 | break; |
| 5894 | } |
| 5895 | return PyBool_FromLong(result); |
| 5896 | |
| 5897 | onError: |
| 5898 | |
| 5899 | /* Standard case |
| 5900 | |
| 5901 | Type errors mean that PyUnicode_FromObject() could not convert |
| 5902 | one of the arguments (usually the right hand side) to Unicode, |
| 5903 | ie. we can't handle the comparison request. However, it is |
| 5904 | possible that the other object knows a comparison method, which |
| 5905 | is why we return Py_NotImplemented to give the other object a |
| 5906 | chance. |
| 5907 | |
| 5908 | */ |
| 5909 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 5910 | PyErr_Clear(); |
| 5911 | Py_INCREF(Py_NotImplemented); |
| 5912 | return Py_NotImplemented; |
| 5913 | } |
| 5914 | if (op != Py_EQ && op != Py_NE) |
| 5915 | return NULL; |
| 5916 | |
| 5917 | /* Equality comparison. |
| 5918 | |
| 5919 | This is a special case: we silence any PyExc_UnicodeDecodeError |
| 5920 | and instead turn it into a PyErr_UnicodeWarning. |
| 5921 | |
| 5922 | */ |
| 5923 | if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) |
| 5924 | return NULL; |
| 5925 | PyErr_Clear(); |
| 5926 | if (PyErr_Warn(PyExc_UnicodeWarning, |
| 5927 | (op == Py_EQ) ? |
| 5928 | "Unicode equal comparison " |
| 5929 | "failed to convert both arguments to Unicode - " |
| 5930 | "interpreting them as being unequal" : |
| 5931 | "Unicode unequal comparison " |
| 5932 | "failed to convert both arguments to Unicode - " |
| 5933 | "interpreting them as being unequal" |
| 5934 | ) < 0) |
| 5935 | return NULL; |
| 5936 | result = (op == Py_NE); |
| 5937 | return PyBool_FromLong(result); |
| 5938 | } |
| 5939 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5940 | int PyUnicode_Contains(PyObject *container, |
| 5941 | PyObject *element) |
| 5942 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5943 | PyObject *str, *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5944 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5945 | |
| 5946 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5947 | sub = PyUnicode_FromObject(element); |
| 5948 | if (!sub) { |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 5949 | PyErr_SetString(PyExc_TypeError, |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 5950 | "'in <string>' requires string as left operand"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5951 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5952 | } |
| 5953 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5954 | str = PyUnicode_FromObject(container); |
| 5955 | if (!str) { |
| 5956 | Py_DECREF(sub); |
| 5957 | return -1; |
| 5958 | } |
| 5959 | |
| 5960 | result = stringlib_contains_obj(str, sub); |
| 5961 | |
| 5962 | Py_DECREF(str); |
| 5963 | Py_DECREF(sub); |
| 5964 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5965 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5966 | } |
| 5967 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5968 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 5969 | |
| 5970 | PyObject *PyUnicode_Concat(PyObject *left, |
| 5971 | PyObject *right) |
| 5972 | { |
| 5973 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 5974 | |
Guido van Rossum | 84d79dd | 2007-04-13 02:23:57 +0000 | [diff] [blame] | 5975 | if (PyBytes_Check(left) || PyBytes_Check(right)) |
| 5976 | return PyBytes_Concat(left, right); |
| 5977 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5978 | /* Coerce the two arguments */ |
| 5979 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 5980 | if (u == NULL) |
| 5981 | goto onError; |
| 5982 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 5983 | if (v == NULL) |
| 5984 | goto onError; |
| 5985 | |
| 5986 | /* Shortcuts */ |
| 5987 | if (v == unicode_empty) { |
| 5988 | Py_DECREF(v); |
| 5989 | return (PyObject *)u; |
| 5990 | } |
| 5991 | if (u == unicode_empty) { |
| 5992 | Py_DECREF(u); |
| 5993 | return (PyObject *)v; |
| 5994 | } |
| 5995 | |
| 5996 | /* Concat the two Unicode strings */ |
| 5997 | w = _PyUnicode_New(u->length + v->length); |
| 5998 | if (w == NULL) |
| 5999 | goto onError; |
| 6000 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 6001 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 6002 | |
| 6003 | Py_DECREF(u); |
| 6004 | Py_DECREF(v); |
| 6005 | return (PyObject *)w; |
| 6006 | |
| 6007 | onError: |
| 6008 | Py_XDECREF(u); |
| 6009 | Py_XDECREF(v); |
| 6010 | return NULL; |
| 6011 | } |
| 6012 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6013 | void |
| 6014 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 6015 | { |
| 6016 | PyObject *new; |
| 6017 | if (*pleft == NULL) |
| 6018 | return; |
| 6019 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 6020 | Py_DECREF(*pleft); |
| 6021 | *pleft = NULL; |
| 6022 | return; |
| 6023 | } |
| 6024 | new = PyUnicode_Concat(*pleft, right); |
| 6025 | Py_DECREF(*pleft); |
| 6026 | *pleft = new; |
| 6027 | } |
| 6028 | |
| 6029 | void |
| 6030 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 6031 | { |
| 6032 | PyUnicode_Append(pleft, right); |
| 6033 | Py_XDECREF(right); |
| 6034 | } |
| 6035 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6036 | PyDoc_STRVAR(count__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6037 | "S.count(sub[, start[, end]]) -> int\n\ |
| 6038 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6039 | Return the number of non-overlapping occurrences of substring sub in\n\ |
| 6040 | 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] | 6041 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6042 | |
| 6043 | static PyObject * |
| 6044 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 6045 | { |
| 6046 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6047 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6048 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6049 | PyObject *result; |
| 6050 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6051 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 6052 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6053 | return NULL; |
| 6054 | |
| 6055 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6056 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6057 | if (substring == NULL) |
| 6058 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6059 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6060 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6061 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6062 | result = PyInt_FromSsize_t( |
| 6063 | stringlib_count(self->str + start, end - start, |
| 6064 | substring->str, substring->length) |
| 6065 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6066 | |
| 6067 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6068 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6069 | return result; |
| 6070 | } |
| 6071 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6072 | PyDoc_STRVAR(encode__doc__, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6073 | "S.encode([encoding[,errors]]) -> string or unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6074 | \n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6075 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 6076 | 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] | 6077 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6078 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 6079 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 6080 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6081 | |
| 6082 | static PyObject * |
| 6083 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 6084 | { |
| 6085 | char *encoding = NULL; |
| 6086 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6087 | PyObject *v; |
| 6088 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6089 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 6090 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6091 | v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6092 | if (v == NULL) |
| 6093 | goto onError; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 6094 | if (!PyBytes_Check(v)) { |
Guido van Rossum | 4355a47 | 2007-05-04 05:00:04 +0000 | [diff] [blame] | 6095 | if (PyString_Check(v)) { |
| 6096 | /* Old codec, turn it into bytes */ |
| 6097 | PyObject *b = PyBytes_FromObject(v); |
| 6098 | Py_DECREF(v); |
| 6099 | return b; |
| 6100 | } |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6101 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 6102 | "encoder did not return a bytes object " |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6103 | "(type=%.400s)", |
| 6104 | v->ob_type->tp_name); |
| 6105 | Py_DECREF(v); |
| 6106 | return NULL; |
| 6107 | } |
| 6108 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6109 | |
| 6110 | onError: |
| 6111 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6112 | } |
| 6113 | |
| 6114 | PyDoc_STRVAR(decode__doc__, |
| 6115 | "S.decode([encoding[,errors]]) -> string or unicode\n\ |
| 6116 | \n\ |
| 6117 | Decodes S using the codec registered for encoding. encoding defaults\n\ |
| 6118 | to the default encoding. errors may be given to set a different error\n\ |
| 6119 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 6120 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 6121 | as well as any other name registerd with codecs.register_error that is\n\ |
| 6122 | able to handle UnicodeDecodeErrors."); |
| 6123 | |
| 6124 | static PyObject * |
Marc-André Lemburg | 126b44c | 2004-07-10 12:04:20 +0000 | [diff] [blame] | 6125 | unicode_decode(PyUnicodeObject *self, PyObject *args) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6126 | { |
| 6127 | char *encoding = NULL; |
| 6128 | char *errors = NULL; |
| 6129 | PyObject *v; |
| 6130 | |
| 6131 | if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
| 6132 | return NULL; |
| 6133 | v = PyUnicode_AsDecodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6134 | if (v == NULL) |
| 6135 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6136 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 6137 | PyErr_Format(PyExc_TypeError, |
| 6138 | "decoder did not return a string/unicode object " |
| 6139 | "(type=%.400s)", |
| 6140 | v->ob_type->tp_name); |
| 6141 | Py_DECREF(v); |
| 6142 | return NULL; |
| 6143 | } |
| 6144 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6145 | |
| 6146 | onError: |
| 6147 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6148 | } |
| 6149 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6150 | PyDoc_STRVAR(expandtabs__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6151 | "S.expandtabs([tabsize]) -> unicode\n\ |
| 6152 | \n\ |
| 6153 | 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] | 6154 | 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] | 6155 | |
| 6156 | static PyObject* |
| 6157 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 6158 | { |
| 6159 | Py_UNICODE *e; |
| 6160 | Py_UNICODE *p; |
| 6161 | Py_UNICODE *q; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6162 | Py_ssize_t i, j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6163 | PyUnicodeObject *u; |
| 6164 | int tabsize = 8; |
| 6165 | |
| 6166 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 6167 | return NULL; |
| 6168 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6169 | /* First pass: determine size of output string */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6170 | i = j = 0; |
| 6171 | e = self->str + self->length; |
| 6172 | for (p = self->str; p < e; p++) |
| 6173 | if (*p == '\t') { |
| 6174 | if (tabsize > 0) |
| 6175 | j += tabsize - (j % tabsize); |
| 6176 | } |
| 6177 | else { |
| 6178 | j++; |
| 6179 | if (*p == '\n' || *p == '\r') { |
| 6180 | i += j; |
| 6181 | j = 0; |
| 6182 | } |
| 6183 | } |
| 6184 | |
| 6185 | /* Second pass: create output string and fill it */ |
| 6186 | u = _PyUnicode_New(i + j); |
| 6187 | if (!u) |
| 6188 | return NULL; |
| 6189 | |
| 6190 | j = 0; |
| 6191 | q = u->str; |
| 6192 | |
| 6193 | for (p = self->str; p < e; p++) |
| 6194 | if (*p == '\t') { |
| 6195 | if (tabsize > 0) { |
| 6196 | i = tabsize - (j % tabsize); |
| 6197 | j += i; |
| 6198 | while (i--) |
| 6199 | *q++ = ' '; |
| 6200 | } |
| 6201 | } |
| 6202 | else { |
| 6203 | j++; |
| 6204 | *q++ = *p; |
| 6205 | if (*p == '\n' || *p == '\r') |
| 6206 | j = 0; |
| 6207 | } |
| 6208 | |
| 6209 | return (PyObject*) u; |
| 6210 | } |
| 6211 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6212 | PyDoc_STRVAR(find__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6213 | "S.find(sub [,start [,end]]) -> int\n\ |
| 6214 | \n\ |
| 6215 | Return the lowest index in S where substring sub is found,\n\ |
| 6216 | such that sub is contained within s[start,end]. Optional\n\ |
| 6217 | arguments start and end are interpreted as in slice notation.\n\ |
| 6218 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6219 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6220 | |
| 6221 | static PyObject * |
| 6222 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 6223 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6224 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6225 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6226 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6227 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6228 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6229 | if (!PyArg_ParseTuple(args, "O|O&O&:find", &substring, |
| 6230 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6231 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6232 | substring = PyUnicode_FromObject(substring); |
| 6233 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6234 | return NULL; |
| 6235 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6236 | result = stringlib_find_slice( |
| 6237 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6238 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6239 | start, end |
| 6240 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6241 | |
| 6242 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6243 | |
| 6244 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6245 | } |
| 6246 | |
| 6247 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6248 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6249 | { |
| 6250 | if (index < 0 || index >= self->length) { |
| 6251 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 6252 | return NULL; |
| 6253 | } |
| 6254 | |
| 6255 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 6256 | } |
| 6257 | |
| 6258 | static long |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6259 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6260 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6261 | /* Since Unicode objects compare equal to their UTF-8 string |
| 6262 | counterparts, we hash the UTF-8 string. */ |
| 6263 | PyObject *v = _PyUnicode_AsDefaultEncodedString(self, NULL); |
| 6264 | return PyObject_Hash(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6265 | } |
| 6266 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6267 | PyDoc_STRVAR(index__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6268 | "S.index(sub [,start [,end]]) -> int\n\ |
| 6269 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6270 | 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] | 6271 | |
| 6272 | static PyObject * |
| 6273 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 6274 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6275 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6276 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6277 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6278 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6279 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6280 | if (!PyArg_ParseTuple(args, "O|O&O&:index", &substring, |
| 6281 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6282 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6283 | substring = PyUnicode_FromObject(substring); |
| 6284 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6285 | return NULL; |
| 6286 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6287 | result = stringlib_find_slice( |
| 6288 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6289 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6290 | start, end |
| 6291 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6292 | |
| 6293 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6294 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6295 | if (result < 0) { |
| 6296 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 6297 | return NULL; |
| 6298 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6299 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6300 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6301 | } |
| 6302 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6303 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6304 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6305 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6306 | 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] | 6307 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6308 | |
| 6309 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6310 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6311 | { |
| 6312 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6313 | register const Py_UNICODE *e; |
| 6314 | int cased; |
| 6315 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6316 | /* Shortcut for single character strings */ |
| 6317 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6318 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6319 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6320 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6321 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6322 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6323 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6324 | e = p + PyUnicode_GET_SIZE(self); |
| 6325 | cased = 0; |
| 6326 | for (; p < e; p++) { |
| 6327 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6328 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6329 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6330 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6331 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 6332 | cased = 1; |
| 6333 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6334 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6335 | } |
| 6336 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6337 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6338 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6339 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6340 | 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] | 6341 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6342 | |
| 6343 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6344 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6345 | { |
| 6346 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6347 | register const Py_UNICODE *e; |
| 6348 | int cased; |
| 6349 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6350 | /* Shortcut for single character strings */ |
| 6351 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6352 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6353 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6354 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6355 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6356 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6357 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6358 | e = p + PyUnicode_GET_SIZE(self); |
| 6359 | cased = 0; |
| 6360 | for (; p < e; p++) { |
| 6361 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6362 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6363 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6364 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6365 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 6366 | cased = 1; |
| 6367 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6368 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6369 | } |
| 6370 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6371 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6372 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6373 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6374 | Return True if S is a titlecased string and there is at least one\n\ |
| 6375 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 6376 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 6377 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6378 | |
| 6379 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6380 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6381 | { |
| 6382 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6383 | register const Py_UNICODE *e; |
| 6384 | int cased, previous_is_cased; |
| 6385 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6386 | /* Shortcut for single character strings */ |
| 6387 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6388 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 6389 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6390 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6391 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6392 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6393 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6394 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6395 | e = p + PyUnicode_GET_SIZE(self); |
| 6396 | cased = 0; |
| 6397 | previous_is_cased = 0; |
| 6398 | for (; p < e; p++) { |
| 6399 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6400 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6401 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 6402 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6403 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6404 | previous_is_cased = 1; |
| 6405 | cased = 1; |
| 6406 | } |
| 6407 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 6408 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6409 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6410 | previous_is_cased = 1; |
| 6411 | cased = 1; |
| 6412 | } |
| 6413 | else |
| 6414 | previous_is_cased = 0; |
| 6415 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6416 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6417 | } |
| 6418 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6419 | PyDoc_STRVAR(isspace__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6420 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6421 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6422 | Return True if all characters in S are whitespace\n\ |
| 6423 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6424 | |
| 6425 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6426 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6427 | { |
| 6428 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6429 | register const Py_UNICODE *e; |
| 6430 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6431 | /* Shortcut for single character strings */ |
| 6432 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6433 | Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6434 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6435 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6436 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6437 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6438 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6439 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6440 | e = p + PyUnicode_GET_SIZE(self); |
| 6441 | for (; p < e; p++) { |
| 6442 | if (!Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6443 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6444 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6445 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6446 | } |
| 6447 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6448 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6449 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6450 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6451 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6452 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6453 | |
| 6454 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6455 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6456 | { |
| 6457 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6458 | register const Py_UNICODE *e; |
| 6459 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6460 | /* Shortcut for single character strings */ |
| 6461 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6462 | Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6463 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6464 | |
| 6465 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6466 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6467 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6468 | |
| 6469 | e = p + PyUnicode_GET_SIZE(self); |
| 6470 | for (; p < e; p++) { |
| 6471 | if (!Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6472 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6473 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6474 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6475 | } |
| 6476 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6477 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6478 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6479 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6480 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6481 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6482 | |
| 6483 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6484 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6485 | { |
| 6486 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6487 | register const Py_UNICODE *e; |
| 6488 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6489 | /* Shortcut for single character strings */ |
| 6490 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6491 | Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6492 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6493 | |
| 6494 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6495 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6496 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6497 | |
| 6498 | e = p + PyUnicode_GET_SIZE(self); |
| 6499 | for (; p < e; p++) { |
| 6500 | if (!Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6501 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6502 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6503 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6504 | } |
| 6505 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6506 | PyDoc_STRVAR(isdecimal__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6507 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6508 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6509 | 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] | 6510 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6511 | |
| 6512 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6513 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6514 | { |
| 6515 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6516 | register const Py_UNICODE *e; |
| 6517 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6518 | /* Shortcut for single character strings */ |
| 6519 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6520 | Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6521 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6522 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6523 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6524 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6525 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6526 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6527 | e = p + PyUnicode_GET_SIZE(self); |
| 6528 | for (; p < e; p++) { |
| 6529 | if (!Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6530 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6531 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6532 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6533 | } |
| 6534 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6535 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6536 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6537 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6538 | Return True if all characters in S are digits\n\ |
| 6539 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6540 | |
| 6541 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6542 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6543 | { |
| 6544 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6545 | register const Py_UNICODE *e; |
| 6546 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6547 | /* Shortcut for single character strings */ |
| 6548 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6549 | Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6550 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6551 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6552 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6553 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6554 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6555 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6556 | e = p + PyUnicode_GET_SIZE(self); |
| 6557 | for (; p < e; p++) { |
| 6558 | if (!Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6559 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6560 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6561 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6562 | } |
| 6563 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6564 | PyDoc_STRVAR(isnumeric__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6565 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6566 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6567 | 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] | 6568 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6569 | |
| 6570 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6571 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6572 | { |
| 6573 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6574 | register const Py_UNICODE *e; |
| 6575 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6576 | /* Shortcut for single character strings */ |
| 6577 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6578 | Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6579 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6580 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6581 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6582 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6583 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6584 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6585 | e = p + PyUnicode_GET_SIZE(self); |
| 6586 | for (; p < e; p++) { |
| 6587 | if (!Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6588 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6589 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6590 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6591 | } |
| 6592 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6593 | PyDoc_STRVAR(join__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6594 | "S.join(sequence) -> unicode\n\ |
| 6595 | \n\ |
| 6596 | 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] | 6597 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6598 | |
| 6599 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6600 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6601 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6602 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6603 | } |
| 6604 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6605 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6606 | unicode_length(PyUnicodeObject *self) |
| 6607 | { |
| 6608 | return self->length; |
| 6609 | } |
| 6610 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6611 | PyDoc_STRVAR(ljust__doc__, |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 6612 | "S.ljust(width[, fillchar]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6613 | \n\ |
| 6614 | 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] | 6615 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6616 | |
| 6617 | static PyObject * |
| 6618 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 6619 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6620 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6621 | Py_UNICODE fillchar = ' '; |
| 6622 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6623 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6624 | return NULL; |
| 6625 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6626 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6627 | Py_INCREF(self); |
| 6628 | return (PyObject*) self; |
| 6629 | } |
| 6630 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6631 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6632 | } |
| 6633 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6634 | PyDoc_STRVAR(lower__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6635 | "S.lower() -> unicode\n\ |
| 6636 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6637 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6638 | |
| 6639 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6640 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6641 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6642 | return fixup(self, fixlower); |
| 6643 | } |
| 6644 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6645 | #define LEFTSTRIP 0 |
| 6646 | #define RIGHTSTRIP 1 |
| 6647 | #define BOTHSTRIP 2 |
| 6648 | |
| 6649 | /* Arrays indexed by above */ |
| 6650 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 6651 | |
| 6652 | #define STRIPNAME(i) (stripformat[i]+3) |
| 6653 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6654 | /* externally visible for str.strip(unicode) */ |
| 6655 | PyObject * |
| 6656 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 6657 | { |
| 6658 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6659 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6660 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6661 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 6662 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6663 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6664 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
| 6665 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6666 | i = 0; |
| 6667 | if (striptype != RIGHTSTRIP) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6668 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 6669 | i++; |
| 6670 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6671 | } |
| 6672 | |
| 6673 | j = len; |
| 6674 | if (striptype != LEFTSTRIP) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6675 | do { |
| 6676 | j--; |
| 6677 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 6678 | j++; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6679 | } |
| 6680 | |
| 6681 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6682 | Py_INCREF(self); |
| 6683 | return (PyObject*)self; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6684 | } |
| 6685 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6686 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6687 | } |
| 6688 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6689 | |
| 6690 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6691 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6692 | { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6693 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6694 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6695 | |
| 6696 | i = 0; |
| 6697 | if (striptype != RIGHTSTRIP) { |
| 6698 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 6699 | i++; |
| 6700 | } |
| 6701 | } |
| 6702 | |
| 6703 | j = len; |
| 6704 | if (striptype != LEFTSTRIP) { |
| 6705 | do { |
| 6706 | j--; |
| 6707 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 6708 | j++; |
| 6709 | } |
| 6710 | |
| 6711 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 6712 | Py_INCREF(self); |
| 6713 | return (PyObject*)self; |
| 6714 | } |
| 6715 | else |
| 6716 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6717 | } |
| 6718 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6719 | |
| 6720 | static PyObject * |
| 6721 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 6722 | { |
| 6723 | PyObject *sep = NULL; |
| 6724 | |
| 6725 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 6726 | return NULL; |
| 6727 | |
| 6728 | if (sep != NULL && sep != Py_None) { |
| 6729 | if (PyUnicode_Check(sep)) |
| 6730 | return _PyUnicode_XStrip(self, striptype, sep); |
| 6731 | else if (PyString_Check(sep)) { |
| 6732 | PyObject *res; |
| 6733 | sep = PyUnicode_FromObject(sep); |
| 6734 | if (sep==NULL) |
| 6735 | return NULL; |
| 6736 | res = _PyUnicode_XStrip(self, striptype, sep); |
| 6737 | Py_DECREF(sep); |
| 6738 | return res; |
| 6739 | } |
| 6740 | else { |
| 6741 | PyErr_Format(PyExc_TypeError, |
| 6742 | "%s arg must be None, unicode or str", |
| 6743 | STRIPNAME(striptype)); |
| 6744 | return NULL; |
| 6745 | } |
| 6746 | } |
| 6747 | |
| 6748 | return do_strip(self, striptype); |
| 6749 | } |
| 6750 | |
| 6751 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6752 | PyDoc_STRVAR(strip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6753 | "S.strip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6754 | \n\ |
| 6755 | Return a copy of the string S with leading and trailing\n\ |
| 6756 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6757 | If chars is given and not None, remove characters in chars instead.\n\ |
| 6758 | 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] | 6759 | |
| 6760 | static PyObject * |
| 6761 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 6762 | { |
| 6763 | if (PyTuple_GET_SIZE(args) == 0) |
| 6764 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 6765 | else |
| 6766 | return do_argstrip(self, BOTHSTRIP, args); |
| 6767 | } |
| 6768 | |
| 6769 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6770 | PyDoc_STRVAR(lstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6771 | "S.lstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6772 | \n\ |
| 6773 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6774 | If chars is given and not None, remove characters in chars instead.\n\ |
| 6775 | 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] | 6776 | |
| 6777 | static PyObject * |
| 6778 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 6779 | { |
| 6780 | if (PyTuple_GET_SIZE(args) == 0) |
| 6781 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 6782 | else |
| 6783 | return do_argstrip(self, LEFTSTRIP, args); |
| 6784 | } |
| 6785 | |
| 6786 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6787 | PyDoc_STRVAR(rstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6788 | "S.rstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6789 | \n\ |
| 6790 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6791 | If chars is given and not None, remove characters in chars instead.\n\ |
| 6792 | 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] | 6793 | |
| 6794 | static PyObject * |
| 6795 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 6796 | { |
| 6797 | if (PyTuple_GET_SIZE(args) == 0) |
| 6798 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 6799 | else |
| 6800 | return do_argstrip(self, RIGHTSTRIP, args); |
| 6801 | } |
| 6802 | |
| 6803 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6804 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6805 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6806 | { |
| 6807 | PyUnicodeObject *u; |
| 6808 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6809 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 6810 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6811 | |
| 6812 | if (len < 0) |
| 6813 | len = 0; |
| 6814 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6815 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6816 | /* no repeat, return original string */ |
| 6817 | Py_INCREF(str); |
| 6818 | return (PyObject*) str; |
| 6819 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 6820 | |
| 6821 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 6822 | * needed doesn't overflow size_t |
| 6823 | */ |
| 6824 | nchars = len * str->length; |
| 6825 | if (len && nchars / len != str->length) { |
| 6826 | PyErr_SetString(PyExc_OverflowError, |
| 6827 | "repeated string is too long"); |
| 6828 | return NULL; |
| 6829 | } |
| 6830 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 6831 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 6832 | PyErr_SetString(PyExc_OverflowError, |
| 6833 | "repeated string is too long"); |
| 6834 | return NULL; |
| 6835 | } |
| 6836 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6837 | if (!u) |
| 6838 | return NULL; |
| 6839 | |
| 6840 | p = u->str; |
| 6841 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6842 | if (str->length == 1 && len > 0) { |
| 6843 | Py_UNICODE_FILL(p, str->str[0], len); |
| 6844 | } else { |
| 6845 | Py_ssize_t done = 0; /* number of characters copied this far */ |
| 6846 | if (done < nchars) { |
| 6847 | Py_UNICODE_COPY(p, str->str, str->length); |
| 6848 | done = str->length; |
| 6849 | } |
| 6850 | while (done < nchars) { |
| 6851 | int n = (done <= nchars-done) ? done : nchars-done; |
| 6852 | Py_UNICODE_COPY(p+done, p, n); |
| 6853 | done += n; |
| 6854 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6855 | } |
| 6856 | |
| 6857 | return (PyObject*) u; |
| 6858 | } |
| 6859 | |
| 6860 | PyObject *PyUnicode_Replace(PyObject *obj, |
| 6861 | PyObject *subobj, |
| 6862 | PyObject *replobj, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6863 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6864 | { |
| 6865 | PyObject *self; |
| 6866 | PyObject *str1; |
| 6867 | PyObject *str2; |
| 6868 | PyObject *result; |
| 6869 | |
| 6870 | self = PyUnicode_FromObject(obj); |
| 6871 | if (self == NULL) |
| 6872 | return NULL; |
| 6873 | str1 = PyUnicode_FromObject(subobj); |
| 6874 | if (str1 == NULL) { |
| 6875 | Py_DECREF(self); |
| 6876 | return NULL; |
| 6877 | } |
| 6878 | str2 = PyUnicode_FromObject(replobj); |
| 6879 | if (str2 == NULL) { |
| 6880 | Py_DECREF(self); |
| 6881 | Py_DECREF(str1); |
| 6882 | return NULL; |
| 6883 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6884 | result = replace((PyUnicodeObject *)self, |
| 6885 | (PyUnicodeObject *)str1, |
| 6886 | (PyUnicodeObject *)str2, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6887 | maxcount); |
| 6888 | Py_DECREF(self); |
| 6889 | Py_DECREF(str1); |
| 6890 | Py_DECREF(str2); |
| 6891 | return result; |
| 6892 | } |
| 6893 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6894 | PyDoc_STRVAR(replace__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6895 | "S.replace (old, new[, maxsplit]) -> unicode\n\ |
| 6896 | \n\ |
| 6897 | Return a copy of S with all occurrences of substring\n\ |
| 6898 | 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] | 6899 | given, only the first maxsplit occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6900 | |
| 6901 | static PyObject* |
| 6902 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 6903 | { |
| 6904 | PyUnicodeObject *str1; |
| 6905 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6906 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6907 | PyObject *result; |
| 6908 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6909 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6910 | return NULL; |
| 6911 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 6912 | if (str1 == NULL) |
| 6913 | return NULL; |
| 6914 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 6915 | if (str2 == NULL) { |
| 6916 | Py_DECREF(str1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6917 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 6918 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6919 | |
| 6920 | result = replace(self, str1, str2, maxcount); |
| 6921 | |
| 6922 | Py_DECREF(str1); |
| 6923 | Py_DECREF(str2); |
| 6924 | return result; |
| 6925 | } |
| 6926 | |
| 6927 | static |
| 6928 | PyObject *unicode_repr(PyObject *unicode) |
| 6929 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6930 | PyObject *repr; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6931 | Py_UNICODE *p; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6932 | Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); |
| 6933 | Py_ssize_t size = PyUnicode_GET_SIZE(unicode); |
| 6934 | |
| 6935 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 6936 | better to choose a different scheme. Perhaps scan the |
| 6937 | first N-chars of the string and allocate based on that size. |
| 6938 | */ |
| 6939 | /* Initial allocation is based on the longest-possible unichr |
| 6940 | escape. |
| 6941 | |
| 6942 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 6943 | unichr, so in this case it's the longest unichr escape. In |
| 6944 | narrow (UTF-16) builds this is five chars per source unichr |
| 6945 | since there are two unichrs in the surrogate pair, so in narrow |
| 6946 | (UTF-16) builds it's not the longest unichr escape. |
| 6947 | |
| 6948 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 6949 | so in the narrow (UTF-16) build case it's the longest unichr |
| 6950 | escape. |
| 6951 | */ |
| 6952 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6953 | repr = PyUnicode_FromUnicode(NULL, |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6954 | 2 /* quotes */ |
| 6955 | #ifdef Py_UNICODE_WIDE |
| 6956 | + 10*size |
| 6957 | #else |
| 6958 | + 6*size |
| 6959 | #endif |
| 6960 | + 1); |
| 6961 | if (repr == NULL) |
| 6962 | return NULL; |
| 6963 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6964 | p = PyUnicode_AS_UNICODE(repr); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6965 | |
| 6966 | /* Add quote */ |
| 6967 | *p++ = (findchar(s, size, '\'') && |
| 6968 | !findchar(s, size, '"')) ? '"' : '\''; |
| 6969 | while (size-- > 0) { |
| 6970 | Py_UNICODE ch = *s++; |
| 6971 | |
| 6972 | /* Escape quotes and backslashes */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6973 | if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6974 | *p++ = '\\'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6975 | *p++ = ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6976 | continue; |
| 6977 | } |
| 6978 | |
| 6979 | #ifdef Py_UNICODE_WIDE |
| 6980 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 6981 | else if (ch >= 0x10000) { |
| 6982 | *p++ = '\\'; |
| 6983 | *p++ = 'U'; |
| 6984 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 6985 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 6986 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 6987 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 6988 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 6989 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 6990 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 6991 | *p++ = hexdigits[ch & 0x0000000F]; |
| 6992 | continue; |
| 6993 | } |
| 6994 | #else |
| 6995 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 6996 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 6997 | Py_UNICODE ch2; |
| 6998 | Py_UCS4 ucs; |
| 6999 | |
| 7000 | ch2 = *s++; |
| 7001 | size--; |
| 7002 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 7003 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 7004 | *p++ = '\\'; |
| 7005 | *p++ = 'U'; |
| 7006 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 7007 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 7008 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 7009 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 7010 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 7011 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 7012 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 7013 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 7014 | continue; |
| 7015 | } |
| 7016 | /* Fall through: isolated surrogates are copied as-is */ |
| 7017 | s--; |
| 7018 | size++; |
| 7019 | } |
| 7020 | #endif |
| 7021 | |
| 7022 | /* Map 16-bit characters to '\uxxxx' */ |
| 7023 | if (ch >= 256) { |
| 7024 | *p++ = '\\'; |
| 7025 | *p++ = 'u'; |
| 7026 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 7027 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 7028 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7029 | *p++ = hexdigits[ch & 0x000F]; |
| 7030 | } |
| 7031 | |
| 7032 | /* Map special whitespace to '\t', \n', '\r' */ |
| 7033 | else if (ch == '\t') { |
| 7034 | *p++ = '\\'; |
| 7035 | *p++ = 't'; |
| 7036 | } |
| 7037 | else if (ch == '\n') { |
| 7038 | *p++ = '\\'; |
| 7039 | *p++ = 'n'; |
| 7040 | } |
| 7041 | else if (ch == '\r') { |
| 7042 | *p++ = '\\'; |
| 7043 | *p++ = 'r'; |
| 7044 | } |
| 7045 | |
| 7046 | /* Map non-printable US ASCII to '\xhh' */ |
| 7047 | else if (ch < ' ' || ch >= 0x7F) { |
| 7048 | *p++ = '\\'; |
| 7049 | *p++ = 'x'; |
| 7050 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7051 | *p++ = hexdigits[ch & 0x000F]; |
| 7052 | } |
| 7053 | |
| 7054 | /* Copy everything else as-is */ |
| 7055 | else |
| 7056 | *p++ = (char) ch; |
| 7057 | } |
| 7058 | /* Add quote */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7059 | *p++ = PyUnicode_AS_UNICODE(repr)[0]; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7060 | |
| 7061 | *p = '\0'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7062 | _PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7063 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7064 | } |
| 7065 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7066 | PyDoc_STRVAR(rfind__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7067 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 7068 | \n\ |
| 7069 | Return the highest index in S where substring sub is found,\n\ |
| 7070 | such that sub is contained within s[start,end]. Optional\n\ |
| 7071 | arguments start and end are interpreted as in slice notation.\n\ |
| 7072 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7073 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7074 | |
| 7075 | static PyObject * |
| 7076 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 7077 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7078 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7079 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7080 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7081 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7082 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7083 | if (!PyArg_ParseTuple(args, "O|O&O&:rfind", &substring, |
| 7084 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7085 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7086 | substring = PyUnicode_FromObject(substring); |
| 7087 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7088 | return NULL; |
| 7089 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7090 | result = stringlib_rfind_slice( |
| 7091 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7092 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7093 | start, end |
| 7094 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7095 | |
| 7096 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7097 | |
| 7098 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7099 | } |
| 7100 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7101 | PyDoc_STRVAR(rindex__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7102 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 7103 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7104 | 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] | 7105 | |
| 7106 | static PyObject * |
| 7107 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 7108 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7109 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7110 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7111 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7112 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7113 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7114 | if (!PyArg_ParseTuple(args, "O|O&O&:rindex", &substring, |
| 7115 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7116 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7117 | substring = PyUnicode_FromObject(substring); |
| 7118 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7119 | return NULL; |
| 7120 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7121 | result = stringlib_rfind_slice( |
| 7122 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7123 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7124 | start, end |
| 7125 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7126 | |
| 7127 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7128 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7129 | if (result < 0) { |
| 7130 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7131 | return NULL; |
| 7132 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7133 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7134 | } |
| 7135 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7136 | PyDoc_STRVAR(rjust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7137 | "S.rjust(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7138 | \n\ |
| 7139 | 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] | 7140 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7141 | |
| 7142 | static PyObject * |
| 7143 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 7144 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7145 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7146 | Py_UNICODE fillchar = ' '; |
| 7147 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7148 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7149 | return NULL; |
| 7150 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7151 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7152 | Py_INCREF(self); |
| 7153 | return (PyObject*) self; |
| 7154 | } |
| 7155 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7156 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7157 | } |
| 7158 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7159 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7160 | 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] | 7161 | { |
| 7162 | /* standard clamping */ |
| 7163 | if (start < 0) |
| 7164 | start = 0; |
| 7165 | if (end < 0) |
| 7166 | end = 0; |
| 7167 | if (end > self->length) |
| 7168 | end = self->length; |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7169 | if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7170 | /* full slice, return original string */ |
| 7171 | Py_INCREF(self); |
| 7172 | return (PyObject*) self; |
| 7173 | } |
| 7174 | if (start > end) |
| 7175 | start = end; |
| 7176 | /* copy slice */ |
| 7177 | return (PyObject*) PyUnicode_FromUnicode(self->str + start, |
| 7178 | end - start); |
| 7179 | } |
| 7180 | |
| 7181 | PyObject *PyUnicode_Split(PyObject *s, |
| 7182 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7183 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7184 | { |
| 7185 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7186 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7187 | s = PyUnicode_FromObject(s); |
| 7188 | if (s == NULL) |
| 7189 | return NULL; |
| 7190 | if (sep != NULL) { |
| 7191 | sep = PyUnicode_FromObject(sep); |
| 7192 | if (sep == NULL) { |
| 7193 | Py_DECREF(s); |
| 7194 | return NULL; |
| 7195 | } |
| 7196 | } |
| 7197 | |
| 7198 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7199 | |
| 7200 | Py_DECREF(s); |
| 7201 | Py_XDECREF(sep); |
| 7202 | return result; |
| 7203 | } |
| 7204 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7205 | PyDoc_STRVAR(split__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7206 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 7207 | \n\ |
| 7208 | Return a list of the words in S, using sep as the\n\ |
| 7209 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Thomas Heller | ca0d2cb | 2004-09-15 11:41:32 +0000 | [diff] [blame] | 7210 | 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] | 7211 | any whitespace string is a separator."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7212 | |
| 7213 | static PyObject* |
| 7214 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 7215 | { |
| 7216 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7217 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7218 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7219 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7220 | return NULL; |
| 7221 | |
| 7222 | if (substring == Py_None) |
| 7223 | return split(self, NULL, maxcount); |
| 7224 | else if (PyUnicode_Check(substring)) |
| 7225 | return split(self, (PyUnicodeObject *)substring, maxcount); |
| 7226 | else |
| 7227 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
| 7228 | } |
| 7229 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7230 | PyObject * |
| 7231 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 7232 | { |
| 7233 | PyObject* str_obj; |
| 7234 | PyObject* sep_obj; |
| 7235 | PyObject* out; |
| 7236 | |
| 7237 | str_obj = PyUnicode_FromObject(str_in); |
| 7238 | if (!str_obj) |
| 7239 | return NULL; |
| 7240 | sep_obj = PyUnicode_FromObject(sep_in); |
| 7241 | if (!sep_obj) { |
| 7242 | Py_DECREF(str_obj); |
| 7243 | return NULL; |
| 7244 | } |
| 7245 | |
| 7246 | out = stringlib_partition( |
| 7247 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7248 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7249 | ); |
| 7250 | |
| 7251 | Py_DECREF(sep_obj); |
| 7252 | Py_DECREF(str_obj); |
| 7253 | |
| 7254 | return out; |
| 7255 | } |
| 7256 | |
| 7257 | |
| 7258 | PyObject * |
| 7259 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 7260 | { |
| 7261 | PyObject* str_obj; |
| 7262 | PyObject* sep_obj; |
| 7263 | PyObject* out; |
| 7264 | |
| 7265 | str_obj = PyUnicode_FromObject(str_in); |
| 7266 | if (!str_obj) |
| 7267 | return NULL; |
| 7268 | sep_obj = PyUnicode_FromObject(sep_in); |
| 7269 | if (!sep_obj) { |
| 7270 | Py_DECREF(str_obj); |
| 7271 | return NULL; |
| 7272 | } |
| 7273 | |
| 7274 | out = stringlib_rpartition( |
| 7275 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7276 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7277 | ); |
| 7278 | |
| 7279 | Py_DECREF(sep_obj); |
| 7280 | Py_DECREF(str_obj); |
| 7281 | |
| 7282 | return out; |
| 7283 | } |
| 7284 | |
| 7285 | PyDoc_STRVAR(partition__doc__, |
| 7286 | "S.partition(sep) -> (head, sep, tail)\n\ |
| 7287 | \n\ |
| 7288 | Searches for the separator sep in S, and returns the part before it,\n\ |
| 7289 | the separator itself, and the part after it. If the separator is not\n\ |
| 7290 | found, returns S and two empty strings."); |
| 7291 | |
| 7292 | static PyObject* |
| 7293 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 7294 | { |
| 7295 | return PyUnicode_Partition((PyObject *)self, separator); |
| 7296 | } |
| 7297 | |
| 7298 | PyDoc_STRVAR(rpartition__doc__, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7299 | "S.rpartition(sep) -> (tail, sep, head)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7300 | \n\ |
| 7301 | Searches for the separator sep in S, starting at the end of S, and returns\n\ |
| 7302 | 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] | 7303 | separator is not found, returns two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7304 | |
| 7305 | static PyObject* |
| 7306 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 7307 | { |
| 7308 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 7309 | } |
| 7310 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7311 | PyObject *PyUnicode_RSplit(PyObject *s, |
| 7312 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7313 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7314 | { |
| 7315 | PyObject *result; |
| 7316 | |
| 7317 | s = PyUnicode_FromObject(s); |
| 7318 | if (s == NULL) |
| 7319 | return NULL; |
| 7320 | if (sep != NULL) { |
| 7321 | sep = PyUnicode_FromObject(sep); |
| 7322 | if (sep == NULL) { |
| 7323 | Py_DECREF(s); |
| 7324 | return NULL; |
| 7325 | } |
| 7326 | } |
| 7327 | |
| 7328 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7329 | |
| 7330 | Py_DECREF(s); |
| 7331 | Py_XDECREF(sep); |
| 7332 | return result; |
| 7333 | } |
| 7334 | |
| 7335 | PyDoc_STRVAR(rsplit__doc__, |
| 7336 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
| 7337 | \n\ |
| 7338 | Return a list of the words in S, using sep as the\n\ |
| 7339 | delimiter string, starting at the end of the string and\n\ |
| 7340 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 7341 | splits are done. If sep is not specified, any whitespace string\n\ |
| 7342 | is a separator."); |
| 7343 | |
| 7344 | static PyObject* |
| 7345 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 7346 | { |
| 7347 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7348 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7349 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7350 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7351 | return NULL; |
| 7352 | |
| 7353 | if (substring == Py_None) |
| 7354 | return rsplit(self, NULL, maxcount); |
| 7355 | else if (PyUnicode_Check(substring)) |
| 7356 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
| 7357 | else |
| 7358 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
| 7359 | } |
| 7360 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7361 | PyDoc_STRVAR(splitlines__doc__, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7362 | "S.splitlines([keepends]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7363 | \n\ |
| 7364 | 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] | 7365 | 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] | 7366 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7367 | |
| 7368 | static PyObject* |
| 7369 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 7370 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7371 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7372 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7373 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7374 | return NULL; |
| 7375 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7376 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7377 | } |
| 7378 | |
| 7379 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 7380 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7381 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame^] | 7382 | if (PyUnicode_CheckExact(self)) { |
| 7383 | Py_INCREF(self); |
| 7384 | return self; |
| 7385 | } else |
| 7386 | /* Subtype -- return genuine unicode string with the same value. */ |
| 7387 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), |
| 7388 | PyUnicode_GET_SIZE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7389 | } |
| 7390 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7391 | PyDoc_STRVAR(swapcase__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7392 | "S.swapcase() -> unicode\n\ |
| 7393 | \n\ |
| 7394 | 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] | 7395 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7396 | |
| 7397 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7398 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7399 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7400 | return fixup(self, fixswapcase); |
| 7401 | } |
| 7402 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7403 | PyDoc_STRVAR(translate__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7404 | "S.translate(table) -> unicode\n\ |
| 7405 | \n\ |
| 7406 | Return a copy of the string S, where all characters have been mapped\n\ |
| 7407 | 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] | 7408 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ |
| 7409 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 7410 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7411 | |
| 7412 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7413 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7414 | { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7415 | return PyUnicode_TranslateCharmap(self->str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7416 | self->length, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7417 | table, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7418 | "ignore"); |
| 7419 | } |
| 7420 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7421 | PyDoc_STRVAR(upper__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7422 | "S.upper() -> unicode\n\ |
| 7423 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7424 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7425 | |
| 7426 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7427 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7428 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7429 | return fixup(self, fixupper); |
| 7430 | } |
| 7431 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7432 | PyDoc_STRVAR(zfill__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7433 | "S.zfill(width) -> unicode\n\ |
| 7434 | \n\ |
| 7435 | 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] | 7436 | of the specified width. The string x is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7437 | |
| 7438 | static PyObject * |
| 7439 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 7440 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7441 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7442 | PyUnicodeObject *u; |
| 7443 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7444 | Py_ssize_t width; |
| 7445 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7446 | return NULL; |
| 7447 | |
| 7448 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 7449 | if (PyUnicode_CheckExact(self)) { |
| 7450 | Py_INCREF(self); |
| 7451 | return (PyObject*) self; |
| 7452 | } |
| 7453 | else |
| 7454 | return PyUnicode_FromUnicode( |
| 7455 | PyUnicode_AS_UNICODE(self), |
| 7456 | PyUnicode_GET_SIZE(self) |
| 7457 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7458 | } |
| 7459 | |
| 7460 | fill = width - self->length; |
| 7461 | |
| 7462 | u = pad(self, fill, 0, '0'); |
| 7463 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 7464 | if (u == NULL) |
| 7465 | return NULL; |
| 7466 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7467 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 7468 | /* move sign to beginning of string */ |
| 7469 | u->str[0] = u->str[fill]; |
| 7470 | u->str[fill] = '0'; |
| 7471 | } |
| 7472 | |
| 7473 | return (PyObject*) u; |
| 7474 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7475 | |
| 7476 | #if 0 |
| 7477 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7478 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7479 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7480 | return PyInt_FromLong(unicode_freelist_size); |
| 7481 | } |
| 7482 | #endif |
| 7483 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7484 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7485 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7486 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7487 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 7488 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7489 | With optional end, stop comparing S at that position.\n\ |
| 7490 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7491 | |
| 7492 | static PyObject * |
| 7493 | unicode_startswith(PyUnicodeObject *self, |
| 7494 | PyObject *args) |
| 7495 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7496 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7497 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7498 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7499 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7500 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7501 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7502 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7503 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7504 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7505 | if (PyTuple_Check(subobj)) { |
| 7506 | Py_ssize_t i; |
| 7507 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 7508 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 7509 | PyTuple_GET_ITEM(subobj, i)); |
| 7510 | if (substring == NULL) |
| 7511 | return NULL; |
| 7512 | result = tailmatch(self, substring, start, end, -1); |
| 7513 | Py_DECREF(substring); |
| 7514 | if (result) { |
| 7515 | Py_RETURN_TRUE; |
| 7516 | } |
| 7517 | } |
| 7518 | /* nothing matched */ |
| 7519 | Py_RETURN_FALSE; |
| 7520 | } |
| 7521 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7522 | if (substring == NULL) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7523 | return NULL; |
| 7524 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7525 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7526 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7527 | } |
| 7528 | |
| 7529 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7530 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7531 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7532 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7533 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 7534 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7535 | With optional end, stop comparing S at that position.\n\ |
| 7536 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7537 | |
| 7538 | static PyObject * |
| 7539 | unicode_endswith(PyUnicodeObject *self, |
| 7540 | PyObject *args) |
| 7541 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7542 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7543 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7544 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7545 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7546 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7547 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7548 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 7549 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7550 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7551 | if (PyTuple_Check(subobj)) { |
| 7552 | Py_ssize_t i; |
| 7553 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 7554 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 7555 | PyTuple_GET_ITEM(subobj, i)); |
| 7556 | if (substring == NULL) |
| 7557 | return NULL; |
| 7558 | result = tailmatch(self, substring, start, end, +1); |
| 7559 | Py_DECREF(substring); |
| 7560 | if (result) { |
| 7561 | Py_RETURN_TRUE; |
| 7562 | } |
| 7563 | } |
| 7564 | Py_RETURN_FALSE; |
| 7565 | } |
| 7566 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7567 | if (substring == NULL) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7568 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7569 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7570 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7571 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7572 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7573 | } |
| 7574 | |
| 7575 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 7576 | |
| 7577 | static PyObject * |
| 7578 | unicode_getnewargs(PyUnicodeObject *v) |
| 7579 | { |
| 7580 | return Py_BuildValue("(u#)", v->str, v->length); |
| 7581 | } |
| 7582 | |
| 7583 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7584 | static PyMethodDef unicode_methods[] = { |
| 7585 | |
| 7586 | /* Order is according to common usage: often used methods should |
| 7587 | appear first, since lookup is done sequentially. */ |
| 7588 | |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7589 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
| 7590 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 7591 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7592 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7593 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 7594 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 7595 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 7596 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 7597 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 7598 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 7599 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7600 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7601 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 7602 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 7603 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7604 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7605 | {"decode", (PyCFunction) unicode_decode, METH_VARARGS, decode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7606 | /* {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */ |
| 7607 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 7608 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 7609 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7610 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7611 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7612 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7613 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7614 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 7615 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 7616 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 7617 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 7618 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 7619 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 7620 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 7621 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 7622 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 7623 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 7624 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 7625 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 7626 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 7627 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7628 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 7629 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7630 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7631 | #endif |
| 7632 | |
| 7633 | #if 0 |
| 7634 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7635 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7636 | #endif |
| 7637 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 7638 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7639 | {NULL, NULL} |
| 7640 | }; |
| 7641 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7642 | static PyObject * |
| 7643 | unicode_mod(PyObject *v, PyObject *w) |
| 7644 | { |
| 7645 | if (!PyUnicode_Check(v)) { |
| 7646 | Py_INCREF(Py_NotImplemented); |
| 7647 | return Py_NotImplemented; |
| 7648 | } |
| 7649 | return PyUnicode_Format(v, w); |
| 7650 | } |
| 7651 | |
| 7652 | static PyNumberMethods unicode_as_number = { |
| 7653 | 0, /*nb_add*/ |
| 7654 | 0, /*nb_subtract*/ |
| 7655 | 0, /*nb_multiply*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7656 | unicode_mod, /*nb_remainder*/ |
| 7657 | }; |
| 7658 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7659 | static PySequenceMethods unicode_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7660 | (lenfunc) unicode_length, /* sq_length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7661 | PyUnicode_Concat, /* sq_concat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7662 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 7663 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 7664 | (ssizessizeargfunc) unicode_slice, /* sq_slice */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7665 | 0, /* sq_ass_item */ |
| 7666 | 0, /* sq_ass_slice */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7667 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7668 | }; |
| 7669 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7670 | static PyObject* |
| 7671 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 7672 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7673 | if (PyIndex_Check(item)) { |
| 7674 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7675 | if (i == -1 && PyErr_Occurred()) |
| 7676 | return NULL; |
| 7677 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7678 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7679 | return unicode_getitem(self, i); |
| 7680 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7681 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7682 | Py_UNICODE* source_buf; |
| 7683 | Py_UNICODE* result_buf; |
| 7684 | PyObject* result; |
| 7685 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7686 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7687 | &start, &stop, &step, &slicelength) < 0) { |
| 7688 | return NULL; |
| 7689 | } |
| 7690 | |
| 7691 | if (slicelength <= 0) { |
| 7692 | return PyUnicode_FromUnicode(NULL, 0); |
| 7693 | } else { |
| 7694 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7695 | result_buf = (Py_UNICODE *)PyMem_MALLOC(slicelength* |
| 7696 | sizeof(Py_UNICODE)); |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7697 | |
| 7698 | if (result_buf == NULL) |
| 7699 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7700 | |
| 7701 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 7702 | result_buf[i] = source_buf[cur]; |
| 7703 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7704 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7705 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
| 7706 | PyMem_FREE(result_buf); |
| 7707 | return result; |
| 7708 | } |
| 7709 | } else { |
| 7710 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 7711 | return NULL; |
| 7712 | } |
| 7713 | } |
| 7714 | |
| 7715 | static PyMappingMethods unicode_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7716 | (lenfunc)unicode_length, /* mp_length */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7717 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 7718 | (objobjargproc)0, /* mp_ass_subscript */ |
| 7719 | }; |
| 7720 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7721 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7722 | unicode_buffer_getreadbuf(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7723 | Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7724 | const void **ptr) |
| 7725 | { |
| 7726 | if (index != 0) { |
| 7727 | PyErr_SetString(PyExc_SystemError, |
| 7728 | "accessing non-existent unicode segment"); |
| 7729 | return -1; |
| 7730 | } |
| 7731 | *ptr = (void *) self->str; |
| 7732 | return PyUnicode_GET_DATA_SIZE(self); |
| 7733 | } |
| 7734 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7735 | static Py_ssize_t |
| 7736 | unicode_buffer_getwritebuf(PyUnicodeObject *self, Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7737 | const void **ptr) |
| 7738 | { |
| 7739 | PyErr_SetString(PyExc_TypeError, |
Neal Norwitz | 20e7213 | 2002-06-13 21:25:17 +0000 | [diff] [blame] | 7740 | "cannot use unicode as modifiable buffer"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7741 | return -1; |
| 7742 | } |
| 7743 | |
| 7744 | static int |
| 7745 | unicode_buffer_getsegcount(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7746 | Py_ssize_t *lenp) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7747 | { |
| 7748 | if (lenp) |
| 7749 | *lenp = PyUnicode_GET_DATA_SIZE(self); |
| 7750 | return 1; |
| 7751 | } |
| 7752 | |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 7753 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7754 | unicode_buffer_getcharbuf(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7755 | Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7756 | const void **ptr) |
| 7757 | { |
| 7758 | PyObject *str; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7759 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7760 | if (index != 0) { |
| 7761 | PyErr_SetString(PyExc_SystemError, |
| 7762 | "accessing non-existent unicode segment"); |
| 7763 | return -1; |
| 7764 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 7765 | str = _PyUnicode_AsDefaultEncodedString((PyObject *)self, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7766 | if (str == NULL) |
| 7767 | return -1; |
| 7768 | *ptr = (void *) PyString_AS_STRING(str); |
| 7769 | return PyString_GET_SIZE(str); |
| 7770 | } |
| 7771 | |
| 7772 | /* Helpers for PyUnicode_Format() */ |
| 7773 | |
| 7774 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7775 | 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] | 7776 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7777 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7778 | if (argidx < arglen) { |
| 7779 | (*p_argidx)++; |
| 7780 | if (arglen < 0) |
| 7781 | return args; |
| 7782 | else |
| 7783 | return PyTuple_GetItem(args, argidx); |
| 7784 | } |
| 7785 | PyErr_SetString(PyExc_TypeError, |
| 7786 | "not enough arguments for format string"); |
| 7787 | return NULL; |
| 7788 | } |
| 7789 | |
| 7790 | #define F_LJUST (1<<0) |
| 7791 | #define F_SIGN (1<<1) |
| 7792 | #define F_BLANK (1<<2) |
| 7793 | #define F_ALT (1<<3) |
| 7794 | #define F_ZERO (1<<4) |
| 7795 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7796 | static Py_ssize_t |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7797 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7798 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7799 | register Py_ssize_t i; |
| 7800 | Py_ssize_t len = strlen(charbuffer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7801 | for (i = len - 1; i >= 0; i--) |
| 7802 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
| 7803 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7804 | return len; |
| 7805 | } |
| 7806 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7807 | static int |
| 7808 | doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) |
| 7809 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 7810 | Py_ssize_t result; |
| 7811 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7812 | PyOS_ascii_formatd((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 7813 | result = strtounicode(buffer, (char *)buffer); |
| 7814 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7815 | } |
| 7816 | |
| 7817 | static int |
| 7818 | longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) |
| 7819 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 7820 | Py_ssize_t result; |
| 7821 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7822 | PyOS_snprintf((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 7823 | result = strtounicode(buffer, (char *)buffer); |
| 7824 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7825 | } |
| 7826 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 7827 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 7828 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 7829 | formatting is done. */ |
| 7830 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7831 | static int |
| 7832 | formatfloat(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7833 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7834 | int flags, |
| 7835 | int prec, |
| 7836 | int type, |
| 7837 | PyObject *v) |
| 7838 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7839 | /* fmt = '%#.' + `prec` + `type` |
| 7840 | 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] | 7841 | char fmt[20]; |
| 7842 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7843 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7844 | x = PyFloat_AsDouble(v); |
| 7845 | if (x == -1.0 && PyErr_Occurred()) |
| 7846 | return -1; |
| 7847 | if (prec < 0) |
| 7848 | prec = 6; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7849 | if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
| 7850 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7851 | /* Worst case length calc to ensure no buffer overrun: |
| 7852 | |
| 7853 | 'g' formats: |
| 7854 | fmt = %#.<prec>g |
| 7855 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 7856 | for any double rep.) |
| 7857 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 7858 | |
| 7859 | 'f' formats: |
| 7860 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 7861 | len = 1 + 50 + 1 + prec = 52 + prec |
| 7862 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7863 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7864 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7865 | |
| 7866 | */ |
| 7867 | if ((type == 'g' && buflen <= (size_t)10 + (size_t)prec) || |
| 7868 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7869 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7870 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7871 | return -1; |
| 7872 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7873 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 7874 | (flags&F_ALT) ? "#" : "", |
| 7875 | prec, type); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7876 | return doubletounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7877 | } |
| 7878 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7879 | static PyObject* |
| 7880 | formatlong(PyObject *val, int flags, int prec, int type) |
| 7881 | { |
| 7882 | char *buf; |
| 7883 | int i, len; |
| 7884 | PyObject *str; /* temporary string object. */ |
| 7885 | PyUnicodeObject *result; |
| 7886 | |
| 7887 | str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
| 7888 | if (!str) |
| 7889 | return NULL; |
| 7890 | result = _PyUnicode_New(len); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 7891 | if (!result) { |
| 7892 | Py_DECREF(str); |
| 7893 | return NULL; |
| 7894 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7895 | for (i = 0; i < len; i++) |
| 7896 | result->str[i] = buf[i]; |
| 7897 | result->str[len] = 0; |
| 7898 | Py_DECREF(str); |
| 7899 | return (PyObject*)result; |
| 7900 | } |
| 7901 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7902 | static int |
| 7903 | formatint(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7904 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7905 | int flags, |
| 7906 | int prec, |
| 7907 | int type, |
| 7908 | PyObject *v) |
| 7909 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7910 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7911 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 7912 | * + 1 + 1 |
| 7913 | * = 24 |
| 7914 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7915 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7916 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7917 | long x; |
| 7918 | |
| 7919 | x = PyInt_AsLong(v); |
| 7920 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7921 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7922 | if (x < 0 && type == 'u') { |
| 7923 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 7924 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7925 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 7926 | sign = "-"; |
| 7927 | else |
| 7928 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7929 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7930 | prec = 1; |
| 7931 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7932 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 7933 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7934 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7935 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7936 | PyErr_SetString(PyExc_OverflowError, |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7937 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7938 | return -1; |
| 7939 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7940 | |
| 7941 | if ((flags & F_ALT) && |
| 7942 | (type == 'x' || type == 'X')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7943 | /* When converting under %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7944 | * of issues that cause pain: |
| 7945 | * - when 0 is being converted, the C standard leaves off |
| 7946 | * the '0x' or '0X', which is inconsistent with other |
| 7947 | * %#x/%#X conversions and inconsistent with Python's |
| 7948 | * hex() function |
| 7949 | * - there are platforms that violate the standard and |
| 7950 | * convert 0 with the '0x' or '0X' |
| 7951 | * (Metrowerks, Compaq Tru64) |
| 7952 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7953 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7954 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7955 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7956 | * We can achieve the desired consistency by inserting our |
| 7957 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 7958 | * of %#x/%#X. |
| 7959 | * |
| 7960 | * Note that this is the same approach as used in |
| 7961 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 7962 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7963 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 7964 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 7965 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7966 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7967 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 7968 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7969 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 7970 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7971 | if (sign[0]) |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7972 | return longtounicode(buf, buflen, fmt, -x); |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7973 | else |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7974 | return longtounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7975 | } |
| 7976 | |
| 7977 | static int |
| 7978 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7979 | size_t buflen, |
| 7980 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7981 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7982 | /* presume that the buffer is at least 2 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7983 | if (PyUnicode_Check(v)) { |
| 7984 | if (PyUnicode_GET_SIZE(v) != 1) |
| 7985 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7986 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7987 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7988 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7989 | else if (PyString_Check(v)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7990 | if (PyString_GET_SIZE(v) != 1) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7991 | goto onError; |
| 7992 | buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0]; |
| 7993 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7994 | |
| 7995 | else { |
| 7996 | /* Integer input truncated to a character */ |
| 7997 | long x; |
| 7998 | x = PyInt_AsLong(v); |
| 7999 | if (x == -1 && PyErr_Occurred()) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8000 | goto onError; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8001 | #ifdef Py_UNICODE_WIDE |
| 8002 | if (x < 0 || x > 0x10ffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 8003 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8004 | "%c arg not in range(0x110000) " |
| 8005 | "(wide Python build)"); |
| 8006 | return -1; |
| 8007 | } |
| 8008 | #else |
| 8009 | if (x < 0 || x > 0xffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 8010 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8011 | "%c arg not in range(0x10000) " |
| 8012 | "(narrow Python build)"); |
| 8013 | return -1; |
| 8014 | } |
| 8015 | #endif |
| 8016 | buf[0] = (Py_UNICODE) x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8017 | } |
| 8018 | buf[1] = '\0'; |
| 8019 | return 1; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8020 | |
| 8021 | onError: |
| 8022 | PyErr_SetString(PyExc_TypeError, |
| 8023 | "%c requires int or char"); |
| 8024 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8025 | } |
| 8026 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8027 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 8028 | |
| 8029 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 8030 | chars are formatted. XXX This is a magic number. Each formatting |
| 8031 | routine does bounds checking to ensure no overflow, but a better |
| 8032 | solution may be to malloc a buffer of appropriate size for each |
| 8033 | format. For now, the current solution is sufficient. |
| 8034 | */ |
| 8035 | #define FORMATBUFLEN (size_t)120 |
| 8036 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8037 | PyObject *PyUnicode_Format(PyObject *format, |
| 8038 | PyObject *args) |
| 8039 | { |
| 8040 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8041 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8042 | int args_owned = 0; |
| 8043 | PyUnicodeObject *result = NULL; |
| 8044 | PyObject *dict = NULL; |
| 8045 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8046 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8047 | if (format == NULL || args == NULL) { |
| 8048 | PyErr_BadInternalCall(); |
| 8049 | return NULL; |
| 8050 | } |
| 8051 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8052 | if (uformat == NULL) |
| 8053 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8054 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 8055 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 8056 | |
| 8057 | reslen = rescnt = fmtcnt + 100; |
| 8058 | result = _PyUnicode_New(reslen); |
| 8059 | if (result == NULL) |
| 8060 | goto onError; |
| 8061 | res = PyUnicode_AS_UNICODE(result); |
| 8062 | |
| 8063 | if (PyTuple_Check(args)) { |
| 8064 | arglen = PyTuple_Size(args); |
| 8065 | argidx = 0; |
| 8066 | } |
| 8067 | else { |
| 8068 | arglen = -1; |
| 8069 | argidx = -2; |
| 8070 | } |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 8071 | if (args->ob_type->tp_as_mapping && !PyTuple_Check(args) && |
| 8072 | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8073 | dict = args; |
| 8074 | |
| 8075 | while (--fmtcnt >= 0) { |
| 8076 | if (*fmt != '%') { |
| 8077 | if (--rescnt < 0) { |
| 8078 | rescnt = fmtcnt + 100; |
| 8079 | reslen += rescnt; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8080 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8081 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8082 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 8083 | --rescnt; |
| 8084 | } |
| 8085 | *res++ = *fmt++; |
| 8086 | } |
| 8087 | else { |
| 8088 | /* Got a format specifier */ |
| 8089 | int flags = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8090 | Py_ssize_t width = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8091 | int prec = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8092 | Py_UNICODE c = '\0'; |
| 8093 | Py_UNICODE fill; |
| 8094 | PyObject *v = NULL; |
| 8095 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8096 | Py_UNICODE *pbuf; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8097 | Py_UNICODE sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8098 | Py_ssize_t len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8099 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8100 | |
| 8101 | fmt++; |
| 8102 | if (*fmt == '(') { |
| 8103 | Py_UNICODE *keystart; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8104 | Py_ssize_t keylen; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8105 | PyObject *key; |
| 8106 | int pcount = 1; |
| 8107 | |
| 8108 | if (dict == NULL) { |
| 8109 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8110 | "format requires a mapping"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8111 | goto onError; |
| 8112 | } |
| 8113 | ++fmt; |
| 8114 | --fmtcnt; |
| 8115 | keystart = fmt; |
| 8116 | /* Skip over balanced parentheses */ |
| 8117 | while (pcount > 0 && --fmtcnt >= 0) { |
| 8118 | if (*fmt == ')') |
| 8119 | --pcount; |
| 8120 | else if (*fmt == '(') |
| 8121 | ++pcount; |
| 8122 | fmt++; |
| 8123 | } |
| 8124 | keylen = fmt - keystart - 1; |
| 8125 | if (fmtcnt < 0 || pcount > 0) { |
| 8126 | PyErr_SetString(PyExc_ValueError, |
| 8127 | "incomplete format key"); |
| 8128 | goto onError; |
| 8129 | } |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 8130 | #if 0 |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8131 | /* keys are converted to strings using UTF-8 and |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8132 | then looked up since Python uses strings to hold |
| 8133 | variables names etc. in its namespaces and we |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8134 | wouldn't want to break common idioms. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8135 | key = PyUnicode_EncodeUTF8(keystart, |
| 8136 | keylen, |
| 8137 | NULL); |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 8138 | #else |
| 8139 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 8140 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8141 | if (key == NULL) |
| 8142 | goto onError; |
| 8143 | if (args_owned) { |
| 8144 | Py_DECREF(args); |
| 8145 | args_owned = 0; |
| 8146 | } |
| 8147 | args = PyObject_GetItem(dict, key); |
| 8148 | Py_DECREF(key); |
| 8149 | if (args == NULL) { |
| 8150 | goto onError; |
| 8151 | } |
| 8152 | args_owned = 1; |
| 8153 | arglen = -1; |
| 8154 | argidx = -2; |
| 8155 | } |
| 8156 | while (--fmtcnt >= 0) { |
| 8157 | switch (c = *fmt++) { |
| 8158 | case '-': flags |= F_LJUST; continue; |
| 8159 | case '+': flags |= F_SIGN; continue; |
| 8160 | case ' ': flags |= F_BLANK; continue; |
| 8161 | case '#': flags |= F_ALT; continue; |
| 8162 | case '0': flags |= F_ZERO; continue; |
| 8163 | } |
| 8164 | break; |
| 8165 | } |
| 8166 | if (c == '*') { |
| 8167 | v = getnextarg(args, arglen, &argidx); |
| 8168 | if (v == NULL) |
| 8169 | goto onError; |
| 8170 | if (!PyInt_Check(v)) { |
| 8171 | PyErr_SetString(PyExc_TypeError, |
| 8172 | "* wants int"); |
| 8173 | goto onError; |
| 8174 | } |
| 8175 | width = PyInt_AsLong(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 8176 | if (width == -1 && PyErr_Occurred()) |
| 8177 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8178 | if (width < 0) { |
| 8179 | flags |= F_LJUST; |
| 8180 | width = -width; |
| 8181 | } |
| 8182 | if (--fmtcnt >= 0) |
| 8183 | c = *fmt++; |
| 8184 | } |
| 8185 | else if (c >= '0' && c <= '9') { |
| 8186 | width = c - '0'; |
| 8187 | while (--fmtcnt >= 0) { |
| 8188 | c = *fmt++; |
| 8189 | if (c < '0' || c > '9') |
| 8190 | break; |
| 8191 | if ((width*10) / 10 != width) { |
| 8192 | PyErr_SetString(PyExc_ValueError, |
| 8193 | "width too big"); |
| 8194 | goto onError; |
| 8195 | } |
| 8196 | width = width*10 + (c - '0'); |
| 8197 | } |
| 8198 | } |
| 8199 | if (c == '.') { |
| 8200 | prec = 0; |
| 8201 | if (--fmtcnt >= 0) |
| 8202 | c = *fmt++; |
| 8203 | if (c == '*') { |
| 8204 | v = getnextarg(args, arglen, &argidx); |
| 8205 | if (v == NULL) |
| 8206 | goto onError; |
| 8207 | if (!PyInt_Check(v)) { |
| 8208 | PyErr_SetString(PyExc_TypeError, |
| 8209 | "* wants int"); |
| 8210 | goto onError; |
| 8211 | } |
| 8212 | prec = PyInt_AsLong(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 8213 | if (prec == -1 && PyErr_Occurred()) |
| 8214 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8215 | if (prec < 0) |
| 8216 | prec = 0; |
| 8217 | if (--fmtcnt >= 0) |
| 8218 | c = *fmt++; |
| 8219 | } |
| 8220 | else if (c >= '0' && c <= '9') { |
| 8221 | prec = c - '0'; |
| 8222 | while (--fmtcnt >= 0) { |
| 8223 | c = Py_CHARMASK(*fmt++); |
| 8224 | if (c < '0' || c > '9') |
| 8225 | break; |
| 8226 | if ((prec*10) / 10 != prec) { |
| 8227 | PyErr_SetString(PyExc_ValueError, |
| 8228 | "prec too big"); |
| 8229 | goto onError; |
| 8230 | } |
| 8231 | prec = prec*10 + (c - '0'); |
| 8232 | } |
| 8233 | } |
| 8234 | } /* prec */ |
| 8235 | if (fmtcnt >= 0) { |
| 8236 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8237 | if (--fmtcnt >= 0) |
| 8238 | c = *fmt++; |
| 8239 | } |
| 8240 | } |
| 8241 | if (fmtcnt < 0) { |
| 8242 | PyErr_SetString(PyExc_ValueError, |
| 8243 | "incomplete format"); |
| 8244 | goto onError; |
| 8245 | } |
| 8246 | if (c != '%') { |
| 8247 | v = getnextarg(args, arglen, &argidx); |
| 8248 | if (v == NULL) |
| 8249 | goto onError; |
| 8250 | } |
| 8251 | sign = 0; |
| 8252 | fill = ' '; |
| 8253 | switch (c) { |
| 8254 | |
| 8255 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8256 | pbuf = formatbuf; |
| 8257 | /* presume that buffer length is at least 1 */ |
| 8258 | pbuf[0] = '%'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8259 | len = 1; |
| 8260 | break; |
| 8261 | |
| 8262 | case 's': |
| 8263 | case 'r': |
| 8264 | if (PyUnicode_Check(v) && c == 's') { |
| 8265 | temp = v; |
| 8266 | Py_INCREF(temp); |
| 8267 | } |
| 8268 | else { |
| 8269 | PyObject *unicode; |
| 8270 | if (c == 's') |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8271 | temp = PyObject_Unicode(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8272 | else |
| 8273 | temp = PyObject_Repr(v); |
| 8274 | if (temp == NULL) |
| 8275 | goto onError; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8276 | if (PyUnicode_Check(temp)) |
| 8277 | /* nothing to do */; |
| 8278 | else if (PyString_Check(temp)) { |
| 8279 | /* convert to string to Unicode */ |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8280 | unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8281 | PyString_GET_SIZE(temp), |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8282 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8283 | "strict"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8284 | Py_DECREF(temp); |
| 8285 | temp = unicode; |
| 8286 | if (temp == NULL) |
| 8287 | goto onError; |
| 8288 | } |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8289 | else { |
| 8290 | Py_DECREF(temp); |
| 8291 | PyErr_SetString(PyExc_TypeError, |
| 8292 | "%s argument has non-string str()"); |
| 8293 | goto onError; |
| 8294 | } |
| 8295 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8296 | pbuf = PyUnicode_AS_UNICODE(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8297 | len = PyUnicode_GET_SIZE(temp); |
| 8298 | if (prec >= 0 && len > prec) |
| 8299 | len = prec; |
| 8300 | break; |
| 8301 | |
| 8302 | case 'i': |
| 8303 | case 'd': |
| 8304 | case 'u': |
| 8305 | case 'o': |
| 8306 | case 'x': |
| 8307 | case 'X': |
| 8308 | if (c == 'i') |
| 8309 | c = 'd'; |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 8310 | if (PyLong_Check(v)) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8311 | temp = formatlong(v, flags, prec, c); |
| 8312 | if (!temp) |
| 8313 | goto onError; |
| 8314 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 8315 | len = PyUnicode_GET_SIZE(temp); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8316 | sign = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8317 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8318 | else { |
| 8319 | pbuf = formatbuf; |
| 8320 | len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8321 | flags, prec, c, v); |
| 8322 | if (len < 0) |
| 8323 | goto onError; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8324 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8325 | } |
| 8326 | if (flags & F_ZERO) |
| 8327 | fill = '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8328 | break; |
| 8329 | |
| 8330 | case 'e': |
| 8331 | case 'E': |
| 8332 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 8333 | case 'F': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8334 | case 'g': |
| 8335 | case 'G': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 8336 | if (c == 'F') |
| 8337 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8338 | pbuf = formatbuf; |
| 8339 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8340 | flags, prec, c, v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8341 | if (len < 0) |
| 8342 | goto onError; |
| 8343 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8344 | if (flags & F_ZERO) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8345 | fill = '0'; |
| 8346 | break; |
| 8347 | |
| 8348 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8349 | pbuf = formatbuf; |
| 8350 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8351 | if (len < 0) |
| 8352 | goto onError; |
| 8353 | break; |
| 8354 | |
| 8355 | default: |
| 8356 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 8357 | "unsupported format character '%c' (0x%x) " |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 8358 | "at index %zd", |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8359 | (31<=c && c<=126) ? (char)c : '?', |
Marc-André Lemburg | 24e53b6 | 2002-09-24 09:32:14 +0000 | [diff] [blame] | 8360 | (int)c, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 8361 | (Py_ssize_t)(fmt - 1 - |
| 8362 | PyUnicode_AS_UNICODE(uformat))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8363 | goto onError; |
| 8364 | } |
| 8365 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8366 | if (*pbuf == '-' || *pbuf == '+') { |
| 8367 | sign = *pbuf++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8368 | len--; |
| 8369 | } |
| 8370 | else if (flags & F_SIGN) |
| 8371 | sign = '+'; |
| 8372 | else if (flags & F_BLANK) |
| 8373 | sign = ' '; |
| 8374 | else |
| 8375 | sign = 0; |
| 8376 | } |
| 8377 | if (width < len) |
| 8378 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8379 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8380 | reslen -= rescnt; |
| 8381 | rescnt = width + fmtcnt + 100; |
| 8382 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8383 | if (reslen < 0) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8384 | Py_XDECREF(temp); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8385 | PyErr_NoMemory(); |
| 8386 | goto onError; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8387 | } |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8388 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 8389 | Py_XDECREF(temp); |
| 8390 | goto onError; |
| 8391 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8392 | res = PyUnicode_AS_UNICODE(result) |
| 8393 | + reslen - rescnt; |
| 8394 | } |
| 8395 | if (sign) { |
| 8396 | if (fill != ' ') |
| 8397 | *res++ = sign; |
| 8398 | rescnt--; |
| 8399 | if (width > len) |
| 8400 | width--; |
| 8401 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8402 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 8403 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8404 | assert(pbuf[1] == c); |
| 8405 | if (fill != ' ') { |
| 8406 | *res++ = *pbuf++; |
| 8407 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8408 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8409 | rescnt -= 2; |
| 8410 | width -= 2; |
| 8411 | if (width < 0) |
| 8412 | width = 0; |
| 8413 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8414 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8415 | if (width > len && !(flags & F_LJUST)) { |
| 8416 | do { |
| 8417 | --rescnt; |
| 8418 | *res++ = fill; |
| 8419 | } while (--width > len); |
| 8420 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8421 | if (fill == ' ') { |
| 8422 | if (sign) |
| 8423 | *res++ = sign; |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8424 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8425 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8426 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8427 | *res++ = *pbuf++; |
| 8428 | *res++ = *pbuf++; |
| 8429 | } |
| 8430 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8431 | Py_UNICODE_COPY(res, pbuf, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8432 | res += len; |
| 8433 | rescnt -= len; |
| 8434 | while (--width >= len) { |
| 8435 | --rescnt; |
| 8436 | *res++ = ' '; |
| 8437 | } |
| 8438 | if (dict && (argidx < arglen) && c != '%') { |
| 8439 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 8440 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8441 | Py_XDECREF(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8442 | goto onError; |
| 8443 | } |
| 8444 | Py_XDECREF(temp); |
| 8445 | } /* '%' */ |
| 8446 | } /* until end */ |
| 8447 | if (argidx < arglen && !dict) { |
| 8448 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 8449 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8450 | goto onError; |
| 8451 | } |
| 8452 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8453 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
| 8454 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8455 | if (args_owned) { |
| 8456 | Py_DECREF(args); |
| 8457 | } |
| 8458 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8459 | return (PyObject *)result; |
| 8460 | |
| 8461 | onError: |
| 8462 | Py_XDECREF(result); |
| 8463 | Py_DECREF(uformat); |
| 8464 | if (args_owned) { |
| 8465 | Py_DECREF(args); |
| 8466 | } |
| 8467 | return NULL; |
| 8468 | } |
| 8469 | |
| 8470 | static PyBufferProcs unicode_as_buffer = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8471 | (readbufferproc) unicode_buffer_getreadbuf, |
| 8472 | (writebufferproc) unicode_buffer_getwritebuf, |
| 8473 | (segcountproc) unicode_buffer_getsegcount, |
| 8474 | (charbufferproc) unicode_buffer_getcharbuf, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8475 | }; |
| 8476 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 8477 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8478 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 8479 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8480 | static PyObject * |
| 8481 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8482 | { |
| 8483 | PyObject *x = NULL; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 8484 | static char *kwlist[] = {"string", "encoding", "errors", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8485 | char *encoding = NULL; |
| 8486 | char *errors = NULL; |
| 8487 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8488 | if (type != &PyUnicode_Type) |
| 8489 | return unicode_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8490 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
| 8491 | kwlist, &x, &encoding, &errors)) |
| 8492 | return NULL; |
| 8493 | if (x == NULL) |
| 8494 | return (PyObject *)_PyUnicode_New(0); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 8495 | if (encoding == NULL && errors == NULL) |
| 8496 | return PyObject_Unicode(x); |
| 8497 | else |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8498 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
| 8499 | } |
| 8500 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8501 | static PyObject * |
| 8502 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8503 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8504 | PyUnicodeObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8505 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8506 | |
| 8507 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 8508 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 8509 | if (tmp == NULL) |
| 8510 | return NULL; |
| 8511 | assert(PyUnicode_Check(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8512 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8513 | if (pnew == NULL) { |
| 8514 | Py_DECREF(tmp); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8515 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8516 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8517 | pnew->str = PyMem_NEW(Py_UNICODE, n+1); |
| 8518 | if (pnew->str == NULL) { |
| 8519 | _Py_ForgetReference((PyObject *)pnew); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8520 | PyObject_Del(pnew); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8521 | Py_DECREF(tmp); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 8522 | return PyErr_NoMemory(); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8523 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8524 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 8525 | pnew->length = n; |
| 8526 | pnew->hash = tmp->hash; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8527 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8528 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8529 | } |
| 8530 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8531 | PyDoc_STRVAR(unicode_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8532 | "unicode(string [, encoding[, errors]]) -> object\n\ |
| 8533 | \n\ |
| 8534 | Create a new Unicode object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 8535 | encoding defaults to the current default string encoding.\n\ |
| 8536 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8537 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8538 | static PyObject *unicode_iter(PyObject *seq); |
| 8539 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8540 | PyTypeObject PyUnicode_Type = { |
| 8541 | PyObject_HEAD_INIT(&PyType_Type) |
| 8542 | 0, /* ob_size */ |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 8543 | "str", /* tp_name */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8544 | sizeof(PyUnicodeObject), /* tp_size */ |
| 8545 | 0, /* tp_itemsize */ |
| 8546 | /* Slots */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 8547 | (destructor)unicode_dealloc, /* tp_dealloc */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8548 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8549 | 0, /* tp_getattr */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8550 | 0, /* tp_setattr */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8551 | 0, /* tp_compare */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8552 | unicode_repr, /* tp_repr */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8553 | &unicode_as_number, /* tp_as_number */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8554 | &unicode_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8555 | &unicode_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8556 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 8557 | 0, /* tp_call*/ |
| 8558 | (reprfunc) unicode_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8559 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 8560 | 0, /* tp_setattro */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8561 | &unicode_as_buffer, /* tp_as_buffer */ |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 8562 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 8563 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8564 | unicode_doc, /* tp_doc */ |
| 8565 | 0, /* tp_traverse */ |
| 8566 | 0, /* tp_clear */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8567 | PyUnicode_RichCompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8568 | 0, /* tp_weaklistoffset */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8569 | unicode_iter, /* tp_iter */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8570 | 0, /* tp_iternext */ |
| 8571 | unicode_methods, /* tp_methods */ |
| 8572 | 0, /* tp_members */ |
| 8573 | 0, /* tp_getset */ |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 8574 | &PyBaseString_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8575 | 0, /* tp_dict */ |
| 8576 | 0, /* tp_descr_get */ |
| 8577 | 0, /* tp_descr_set */ |
| 8578 | 0, /* tp_dictoffset */ |
| 8579 | 0, /* tp_init */ |
| 8580 | 0, /* tp_alloc */ |
| 8581 | unicode_new, /* tp_new */ |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8582 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8583 | }; |
| 8584 | |
| 8585 | /* Initialize the Unicode implementation */ |
| 8586 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 8587 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8588 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8589 | int i; |
| 8590 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8591 | /* XXX - move this array to unicodectype.c ? */ |
| 8592 | Py_UNICODE linebreak[] = { |
| 8593 | 0x000A, /* LINE FEED */ |
| 8594 | 0x000D, /* CARRIAGE RETURN */ |
| 8595 | 0x001C, /* FILE SEPARATOR */ |
| 8596 | 0x001D, /* GROUP SEPARATOR */ |
| 8597 | 0x001E, /* RECORD SEPARATOR */ |
| 8598 | 0x0085, /* NEXT LINE */ |
| 8599 | 0x2028, /* LINE SEPARATOR */ |
| 8600 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 8601 | }; |
| 8602 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8603 | /* Init the implementation */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8604 | unicode_freelist = NULL; |
| 8605 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8606 | unicode_empty = _PyUnicode_New(0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8607 | if (!unicode_empty) |
| 8608 | return; |
| 8609 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8610 | for (i = 0; i < 256; i++) |
| 8611 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 8612 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 8613 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8614 | |
| 8615 | /* initialize the linebreak bloom filter */ |
| 8616 | bloom_linebreak = make_bloom_mask( |
| 8617 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 8618 | ); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8619 | |
| 8620 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8621 | } |
| 8622 | |
| 8623 | /* Finalize the Unicode implementation */ |
| 8624 | |
| 8625 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 8626 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8627 | { |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8628 | PyUnicodeObject *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8629 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8630 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 8631 | Py_XDECREF(unicode_empty); |
| 8632 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8633 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8634 | for (i = 0; i < 256; i++) { |
| 8635 | if (unicode_latin1[i]) { |
| 8636 | Py_DECREF(unicode_latin1[i]); |
| 8637 | unicode_latin1[i] = NULL; |
| 8638 | } |
| 8639 | } |
| 8640 | |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8641 | for (u = unicode_freelist; u != NULL;) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8642 | PyUnicodeObject *v = u; |
| 8643 | u = *(PyUnicodeObject **)u; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 8644 | if (v->str) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 8645 | PyMem_DEL(v->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 8646 | Py_XDECREF(v->defenc); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8647 | PyObject_Del(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8648 | } |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8649 | unicode_freelist = NULL; |
| 8650 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8651 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 8652 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 8653 | void |
| 8654 | PyUnicode_InternInPlace(PyObject **p) |
| 8655 | { |
| 8656 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 8657 | PyObject *t; |
| 8658 | if (s == NULL || !PyUnicode_Check(s)) |
| 8659 | Py_FatalError( |
| 8660 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 8661 | /* If it's a subclass, we don't really know what putting |
| 8662 | it in the interned dict might do. */ |
| 8663 | if (!PyUnicode_CheckExact(s)) |
| 8664 | return; |
| 8665 | if (PyUnicode_CHECK_INTERNED(s)) |
| 8666 | return; |
| 8667 | if (interned == NULL) { |
| 8668 | interned = PyDict_New(); |
| 8669 | if (interned == NULL) { |
| 8670 | PyErr_Clear(); /* Don't leave an exception */ |
| 8671 | return; |
| 8672 | } |
| 8673 | } |
| 8674 | t = PyDict_GetItem(interned, (PyObject *)s); |
| 8675 | if (t) { |
| 8676 | Py_INCREF(t); |
| 8677 | Py_DECREF(*p); |
| 8678 | *p = t; |
| 8679 | return; |
| 8680 | } |
| 8681 | |
| 8682 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 8683 | PyErr_Clear(); |
| 8684 | return; |
| 8685 | } |
| 8686 | /* The two references in interned are not counted by refcnt. |
| 8687 | The deallocator will take care of this */ |
| 8688 | s->ob_refcnt -= 2; |
| 8689 | PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
| 8690 | } |
| 8691 | |
| 8692 | void |
| 8693 | PyUnicode_InternImmortal(PyObject **p) |
| 8694 | { |
| 8695 | PyUnicode_InternInPlace(p); |
| 8696 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 8697 | PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 8698 | Py_INCREF(*p); |
| 8699 | } |
| 8700 | } |
| 8701 | |
| 8702 | PyObject * |
| 8703 | PyUnicode_InternFromString(const char *cp) |
| 8704 | { |
| 8705 | PyObject *s = PyUnicode_FromString(cp); |
| 8706 | if (s == NULL) |
| 8707 | return NULL; |
| 8708 | PyUnicode_InternInPlace(&s); |
| 8709 | return s; |
| 8710 | } |
| 8711 | |
| 8712 | void _Py_ReleaseInternedUnicodeStrings(void) |
| 8713 | { |
| 8714 | PyObject *keys; |
| 8715 | PyUnicodeObject *s; |
| 8716 | Py_ssize_t i, n; |
| 8717 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
| 8718 | |
| 8719 | if (interned == NULL || !PyDict_Check(interned)) |
| 8720 | return; |
| 8721 | keys = PyDict_Keys(interned); |
| 8722 | if (keys == NULL || !PyList_Check(keys)) { |
| 8723 | PyErr_Clear(); |
| 8724 | return; |
| 8725 | } |
| 8726 | |
| 8727 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 8728 | detector, interned unicode strings are not forcibly deallocated; |
| 8729 | rather, we give them their stolen references back, and then clear |
| 8730 | and DECREF the interned dict. */ |
| 8731 | |
| 8732 | n = PyList_GET_SIZE(keys); |
| 8733 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
| 8734 | n); |
| 8735 | for (i = 0; i < n; i++) { |
| 8736 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
| 8737 | switch (s->state) { |
| 8738 | case SSTATE_NOT_INTERNED: |
| 8739 | /* XXX Shouldn't happen */ |
| 8740 | break; |
| 8741 | case SSTATE_INTERNED_IMMORTAL: |
| 8742 | s->ob_refcnt += 1; |
| 8743 | immortal_size += s->length; |
| 8744 | break; |
| 8745 | case SSTATE_INTERNED_MORTAL: |
| 8746 | s->ob_refcnt += 2; |
| 8747 | mortal_size += s->length; |
| 8748 | break; |
| 8749 | default: |
| 8750 | Py_FatalError("Inconsistent interned string state."); |
| 8751 | } |
| 8752 | s->state = SSTATE_NOT_INTERNED; |
| 8753 | } |
| 8754 | fprintf(stderr, "total size of all interned strings: " |
| 8755 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 8756 | "mortal/immortal\n", mortal_size, immortal_size); |
| 8757 | Py_DECREF(keys); |
| 8758 | PyDict_Clear(interned); |
| 8759 | Py_DECREF(interned); |
| 8760 | interned = NULL; |
| 8761 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8762 | |
| 8763 | |
| 8764 | /********************* Unicode Iterator **************************/ |
| 8765 | |
| 8766 | typedef struct { |
| 8767 | PyObject_HEAD |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 8768 | Py_ssize_t it_index; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8769 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
| 8770 | } unicodeiterobject; |
| 8771 | |
| 8772 | static void |
| 8773 | unicodeiter_dealloc(unicodeiterobject *it) |
| 8774 | { |
| 8775 | _PyObject_GC_UNTRACK(it); |
| 8776 | Py_XDECREF(it->it_seq); |
| 8777 | PyObject_GC_Del(it); |
| 8778 | } |
| 8779 | |
| 8780 | static int |
| 8781 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 8782 | { |
| 8783 | Py_VISIT(it->it_seq); |
| 8784 | return 0; |
| 8785 | } |
| 8786 | |
| 8787 | static PyObject * |
| 8788 | unicodeiter_next(unicodeiterobject *it) |
| 8789 | { |
| 8790 | PyUnicodeObject *seq; |
| 8791 | PyObject *item; |
| 8792 | |
| 8793 | assert(it != NULL); |
| 8794 | seq = it->it_seq; |
| 8795 | if (seq == NULL) |
| 8796 | return NULL; |
| 8797 | assert(PyUnicode_Check(seq)); |
| 8798 | |
| 8799 | if (it->it_index < PyUnicode_GET_SIZE(seq)) { |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 8800 | item = PyUnicode_FromUnicode( |
| 8801 | PyUnicode_AS_UNICODE(seq)+it->it_index, 1); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8802 | if (item != NULL) |
| 8803 | ++it->it_index; |
| 8804 | return item; |
| 8805 | } |
| 8806 | |
| 8807 | Py_DECREF(seq); |
| 8808 | it->it_seq = NULL; |
| 8809 | return NULL; |
| 8810 | } |
| 8811 | |
| 8812 | static PyObject * |
| 8813 | unicodeiter_len(unicodeiterobject *it) |
| 8814 | { |
| 8815 | Py_ssize_t len = 0; |
| 8816 | if (it->it_seq) |
| 8817 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 8818 | return PyInt_FromSsize_t(len); |
| 8819 | } |
| 8820 | |
| 8821 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 8822 | |
| 8823 | static PyMethodDef unicodeiter_methods[] = { |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 8824 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
| 8825 | length_hint_doc}, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8826 | {NULL, NULL} /* sentinel */ |
| 8827 | }; |
| 8828 | |
| 8829 | PyTypeObject PyUnicodeIter_Type = { |
| 8830 | PyObject_HEAD_INIT(&PyType_Type) |
| 8831 | 0, /* ob_size */ |
| 8832 | "unicodeiterator", /* tp_name */ |
| 8833 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 8834 | 0, /* tp_itemsize */ |
| 8835 | /* methods */ |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 8836 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8837 | 0, /* tp_print */ |
| 8838 | 0, /* tp_getattr */ |
| 8839 | 0, /* tp_setattr */ |
| 8840 | 0, /* tp_compare */ |
| 8841 | 0, /* tp_repr */ |
| 8842 | 0, /* tp_as_number */ |
| 8843 | 0, /* tp_as_sequence */ |
| 8844 | 0, /* tp_as_mapping */ |
| 8845 | 0, /* tp_hash */ |
| 8846 | 0, /* tp_call */ |
| 8847 | 0, /* tp_str */ |
| 8848 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 8849 | 0, /* tp_setattro */ |
| 8850 | 0, /* tp_as_buffer */ |
| 8851 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 8852 | 0, /* tp_doc */ |
| 8853 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 8854 | 0, /* tp_clear */ |
| 8855 | 0, /* tp_richcompare */ |
| 8856 | 0, /* tp_weaklistoffset */ |
| 8857 | PyObject_SelfIter, /* tp_iter */ |
| 8858 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 8859 | unicodeiter_methods, /* tp_methods */ |
| 8860 | 0, |
| 8861 | }; |
| 8862 | |
| 8863 | static PyObject * |
| 8864 | unicode_iter(PyObject *seq) |
| 8865 | { |
| 8866 | unicodeiterobject *it; |
| 8867 | |
| 8868 | if (!PyUnicode_Check(seq)) { |
| 8869 | PyErr_BadInternalCall(); |
| 8870 | return NULL; |
| 8871 | } |
| 8872 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 8873 | if (it == NULL) |
| 8874 | return NULL; |
| 8875 | it->it_index = 0; |
| 8876 | Py_INCREF(seq); |
| 8877 | it->it_seq = (PyUnicodeObject *)seq; |
| 8878 | _PyObject_GC_TRACK(it); |
| 8879 | return (PyObject *)it; |
| 8880 | } |
| 8881 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8882 | #ifdef __cplusplus |
| 8883 | } |
| 8884 | #endif |
| 8885 | |
| 8886 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 8887 | /* |
| 8888 | Local variables: |
| 8889 | c-basic-offset: 4 |
| 8890 | indent-tabs-mode: nil |
| 8891 | End: |
| 8892 | */ |