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 */ |
Martin v. Löwis | 5d7428b | 2007-07-21 18:47:48 +0000 | [diff] [blame] | 308 | Py_Refcnt(unicode) = 3; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 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); |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 341 | Py_Type(unicode)->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; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 355 | if (v == NULL || !PyUnicode_Check(v) || Py_Refcnt(v) != 1 || length < 0) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 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 |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 430 | some optimizations which share commonly used objects. |
| 431 | Also, this means the input must be UTF-8, so fall back to the |
| 432 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 433 | if (u != NULL) { |
| 434 | |
| 435 | /* Optimization for empty strings */ |
| 436 | if (size == 0 && unicode_empty != NULL) { |
| 437 | Py_INCREF(unicode_empty); |
| 438 | return (PyObject *)unicode_empty; |
| 439 | } |
| 440 | |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 441 | /* Single characters are shared when using this constructor. |
| 442 | Restrict to ASCII, since the input must be UTF-8. */ |
| 443 | if (size == 1 && Py_CHARMASK(*u) < 128) { |
Guido van Rossum | 00058aa | 2007-07-19 18:21:28 +0000 | [diff] [blame] | 444 | unicode = unicode_latin1[Py_CHARMASK(*u)]; |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 445 | if (!unicode) { |
| 446 | unicode = _PyUnicode_New(1); |
| 447 | if (!unicode) |
| 448 | return NULL; |
Guido van Rossum | 00058aa | 2007-07-19 18:21:28 +0000 | [diff] [blame] | 449 | unicode->str[0] = Py_CHARMASK(*u); |
| 450 | unicode_latin1[Py_CHARMASK(*u)] = unicode; |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 451 | } |
| 452 | Py_INCREF(unicode); |
| 453 | return (PyObject *)unicode; |
| 454 | } |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 455 | |
| 456 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 459 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 460 | if (!unicode) |
| 461 | return NULL; |
| 462 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 463 | return (PyObject *)unicode; |
| 464 | } |
| 465 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 466 | PyObject *PyUnicode_FromString(const char *u) |
| 467 | { |
| 468 | size_t size = strlen(u); |
| 469 | if (size > PY_SSIZE_T_MAX) { |
| 470 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 471 | return NULL; |
| 472 | } |
| 473 | |
| 474 | return PyUnicode_FromStringAndSize(u, size); |
| 475 | } |
| 476 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 477 | #ifdef HAVE_WCHAR_H |
| 478 | |
| 479 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 480 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 481 | { |
| 482 | PyUnicodeObject *unicode; |
| 483 | |
| 484 | if (w == NULL) { |
| 485 | PyErr_BadInternalCall(); |
| 486 | return NULL; |
| 487 | } |
| 488 | |
| 489 | unicode = _PyUnicode_New(size); |
| 490 | if (!unicode) |
| 491 | return NULL; |
| 492 | |
| 493 | /* Copy the wchar_t data into the new object */ |
| 494 | #ifdef HAVE_USABLE_WCHAR_T |
| 495 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 496 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 497 | { |
| 498 | register Py_UNICODE *u; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 499 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 500 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 501 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 502 | *u++ = *w++; |
| 503 | } |
| 504 | #endif |
| 505 | |
| 506 | return (PyObject *)unicode; |
| 507 | } |
| 508 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 509 | static void |
| 510 | makefmt(char *fmt, int longflag, int size_tflag, int zeropad, int width, int precision, char c) |
| 511 | { |
| 512 | *fmt++ = '%'; |
| 513 | if (width) { |
| 514 | if (zeropad) |
| 515 | *fmt++ = '0'; |
| 516 | fmt += sprintf(fmt, "%d", width); |
| 517 | } |
| 518 | if (precision) |
| 519 | fmt += sprintf(fmt, ".%d", precision); |
| 520 | if (longflag) |
| 521 | *fmt++ = 'l'; |
| 522 | else if (size_tflag) { |
| 523 | char *f = PY_FORMAT_SIZE_T; |
| 524 | while (*f) |
| 525 | *fmt++ = *f++; |
| 526 | } |
| 527 | *fmt++ = c; |
| 528 | *fmt = '\0'; |
| 529 | } |
| 530 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 531 | #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} |
| 532 | |
| 533 | PyObject * |
| 534 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 535 | { |
| 536 | va_list count; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 537 | Py_ssize_t callcount = 0; |
| 538 | PyObject **callresults = NULL; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 539 | PyObject **callresult = NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 540 | Py_ssize_t n = 0; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 541 | int width = 0; |
| 542 | int precision = 0; |
| 543 | int zeropad; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 544 | const char* f; |
| 545 | Py_UNICODE *s; |
| 546 | PyObject *string; |
| 547 | /* used by sprintf */ |
| 548 | char buffer[21]; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 549 | /* use abuffer instead of buffer, if we need more space |
| 550 | * (which can happen if there's a format specifier with width). */ |
| 551 | char *abuffer = NULL; |
| 552 | char *realbuffer; |
| 553 | Py_ssize_t abuffersize = 0; |
| 554 | char fmt[60]; /* should be enough for %0width.precisionld */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 555 | const char *copy; |
| 556 | |
| 557 | #ifdef VA_LIST_IS_ARRAY |
| 558 | Py_MEMCPY(count, vargs, sizeof(va_list)); |
| 559 | #else |
| 560 | #ifdef __va_copy |
| 561 | __va_copy(count, vargs); |
| 562 | #else |
| 563 | count = vargs; |
| 564 | #endif |
| 565 | #endif |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 566 | /* step 1: count the number of %S/%R format specifications |
| 567 | * (we call PyObject_Unicode()/PyObject_Repr() for these objects |
| 568 | * once during step 3 and put the result in an array) */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 569 | for (f = format; *f; f++) { |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 570 | if (*f == '%' && (*(f+1)=='S' || *(f+1)=='R')) |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 571 | ++callcount; |
| 572 | } |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 573 | /* step 2: allocate memory for the results of |
| 574 | * PyObject_Unicode()/PyObject_Repr() calls */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 575 | if (callcount) { |
| 576 | callresults = PyMem_Malloc(sizeof(PyObject *)*callcount); |
| 577 | if (!callresults) { |
| 578 | PyErr_NoMemory(); |
| 579 | return NULL; |
| 580 | } |
| 581 | callresult = callresults; |
| 582 | } |
| 583 | /* step 3: figure out how large a buffer we need */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 584 | for (f = format; *f; f++) { |
| 585 | if (*f == '%') { |
| 586 | const char* p = f; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 587 | width = 0; |
| 588 | while (isdigit(Py_CHARMASK(*f))) |
| 589 | width = (width*10) + *f++ - '0'; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 590 | while (*++f && *f != '%' && !isalpha(Py_CHARMASK(*f))) |
| 591 | ; |
| 592 | |
| 593 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 594 | * they don't affect the amount of space we reserve. |
| 595 | */ |
| 596 | if ((*f == 'l' || *f == 'z') && |
| 597 | (f[1] == 'd' || f[1] == 'u')) |
| 598 | ++f; |
| 599 | |
| 600 | switch (*f) { |
| 601 | case 'c': |
| 602 | (void)va_arg(count, int); |
| 603 | /* fall through... */ |
| 604 | case '%': |
| 605 | n++; |
| 606 | break; |
| 607 | case 'd': case 'u': case 'i': case 'x': |
| 608 | (void) va_arg(count, int); |
| 609 | /* 20 bytes is enough to hold a 64-bit |
| 610 | integer. Decimal takes the most space. |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 611 | This isn't enough for octal. |
| 612 | If a width is specified we need more |
| 613 | (which we allocate later). */ |
| 614 | if (width < 20) |
| 615 | width = 20; |
| 616 | n += width; |
| 617 | if (abuffersize < width) |
| 618 | abuffersize = width; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 619 | break; |
| 620 | case 's': |
| 621 | n += strlen(va_arg(count, char*)); |
| 622 | break; |
| 623 | case 'U': |
| 624 | { |
| 625 | PyObject *obj = va_arg(count, PyObject *); |
| 626 | assert(obj && PyUnicode_Check(obj)); |
| 627 | n += PyUnicode_GET_SIZE(obj); |
| 628 | break; |
| 629 | } |
Walter Dörwald | d7fb764 | 2007-06-11 16:36:59 +0000 | [diff] [blame] | 630 | case 'V': |
| 631 | { |
| 632 | PyObject *obj = va_arg(count, PyObject *); |
| 633 | const char *str = va_arg(count, const char *); |
| 634 | assert(obj || str); |
| 635 | assert(!obj || PyUnicode_Check(obj)); |
| 636 | if (obj) |
| 637 | n += PyUnicode_GET_SIZE(obj); |
| 638 | else |
| 639 | n += strlen(str); |
| 640 | break; |
| 641 | } |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 642 | case 'S': |
| 643 | { |
| 644 | PyObject *obj = va_arg(count, PyObject *); |
| 645 | PyObject *str; |
| 646 | assert(obj); |
| 647 | str = PyObject_Unicode(obj); |
| 648 | if (!str) |
| 649 | goto fail; |
| 650 | n += PyUnicode_GET_SIZE(str); |
| 651 | /* Remember the str and switch to the next slot */ |
| 652 | *callresult++ = str; |
| 653 | break; |
| 654 | } |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 655 | case 'R': |
| 656 | { |
| 657 | PyObject *obj = va_arg(count, PyObject *); |
| 658 | PyObject *repr; |
| 659 | assert(obj); |
| 660 | repr = PyObject_Repr(obj); |
| 661 | if (!repr) |
| 662 | goto fail; |
| 663 | n += PyUnicode_GET_SIZE(repr); |
| 664 | /* Remember the repr and switch to the next slot */ |
| 665 | *callresult++ = repr; |
| 666 | break; |
| 667 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 668 | case 'p': |
| 669 | (void) va_arg(count, int); |
| 670 | /* maximum 64-bit pointer representation: |
| 671 | * 0xffffffffffffffff |
| 672 | * so 19 characters is enough. |
| 673 | * XXX I count 18 -- what's the extra for? |
| 674 | */ |
| 675 | n += 19; |
| 676 | break; |
| 677 | default: |
| 678 | /* if we stumble upon an unknown |
| 679 | formatting code, copy the rest of |
| 680 | the format string to the output |
| 681 | string. (we cannot just skip the |
| 682 | code, since there's no way to know |
| 683 | what's in the argument list) */ |
| 684 | n += strlen(p); |
| 685 | goto expand; |
| 686 | } |
| 687 | } else |
| 688 | n++; |
| 689 | } |
| 690 | expand: |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 691 | if (abuffersize > 20) { |
| 692 | abuffer = PyMem_Malloc(abuffersize); |
| 693 | if (!abuffer) { |
| 694 | PyErr_NoMemory(); |
| 695 | goto fail; |
| 696 | } |
| 697 | realbuffer = abuffer; |
| 698 | } |
| 699 | else |
| 700 | realbuffer = buffer; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 701 | /* step 4: fill the buffer */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 702 | /* 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] | 703 | we don't have to resize the string. |
| 704 | There can be no errors beyond this point. */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 705 | string = PyUnicode_FromUnicode(NULL, n); |
| 706 | if (!string) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 707 | goto fail; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 708 | |
| 709 | s = PyUnicode_AS_UNICODE(string); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 710 | callresult = callresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 711 | |
| 712 | for (f = format; *f; f++) { |
| 713 | if (*f == '%') { |
| 714 | const char* p = f++; |
| 715 | int longflag = 0; |
| 716 | int size_tflag = 0; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 717 | zeropad = (*f == '0'); |
| 718 | /* parse the width.precision part */ |
| 719 | width = 0; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 720 | while (isdigit(Py_CHARMASK(*f))) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 721 | width = (width*10) + *f++ - '0'; |
| 722 | precision = 0; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 723 | if (*f == '.') { |
| 724 | f++; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 725 | while (isdigit(Py_CHARMASK(*f))) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 726 | precision = (precision*10) + *f++ - '0'; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 727 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 728 | /* handle the long flag, but only for %ld and %lu. |
| 729 | others can be added when necessary. */ |
| 730 | if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
| 731 | longflag = 1; |
| 732 | ++f; |
| 733 | } |
| 734 | /* handle the size_t flag. */ |
| 735 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 736 | size_tflag = 1; |
| 737 | ++f; |
| 738 | } |
| 739 | |
| 740 | switch (*f) { |
| 741 | case 'c': |
| 742 | *s++ = va_arg(vargs, int); |
| 743 | break; |
| 744 | case 'd': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 745 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'd'); |
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, 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, Py_ssize_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, int)); |
| 752 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 753 | break; |
| 754 | case 'u': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 755 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'u'); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 756 | if (longflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 757 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 758 | else if (size_tflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 759 | sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 760 | else |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 761 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
| 762 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 763 | break; |
| 764 | case 'i': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 765 | makefmt(fmt, 0, 0, zeropad, width, precision, 'i'); |
| 766 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 767 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 768 | break; |
| 769 | case 'x': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 770 | makefmt(fmt, 0, 0, zeropad, width, precision, 'x'); |
| 771 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 772 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 773 | break; |
| 774 | case 's': |
| 775 | p = va_arg(vargs, char*); |
| 776 | appendstring(p); |
| 777 | break; |
| 778 | case 'U': |
| 779 | { |
| 780 | PyObject *obj = va_arg(vargs, PyObject *); |
Walter Dörwald | 5c2fab6 | 2007-05-24 19:51:02 +0000 | [diff] [blame] | 781 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 782 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 783 | s += size; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 784 | break; |
| 785 | } |
Walter Dörwald | d7fb764 | 2007-06-11 16:36:59 +0000 | [diff] [blame] | 786 | case 'V': |
| 787 | { |
| 788 | PyObject *obj = va_arg(vargs, PyObject *); |
| 789 | const char *str = va_arg(vargs, const char *); |
| 790 | if (obj) { |
| 791 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 792 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 793 | s += size; |
| 794 | } else { |
| 795 | appendstring(str); |
| 796 | } |
| 797 | break; |
| 798 | } |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 799 | case 'S': |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 800 | case 'R': |
| 801 | { |
Guido van Rossum | 755114a | 2007-06-13 01:04:27 +0000 | [diff] [blame] | 802 | Py_UNICODE *ucopy; |
| 803 | Py_ssize_t usize; |
| 804 | Py_ssize_t upos; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 805 | /* unused, since we already have the result */ |
| 806 | (void) va_arg(vargs, PyObject *); |
Guido van Rossum | 755114a | 2007-06-13 01:04:27 +0000 | [diff] [blame] | 807 | ucopy = PyUnicode_AS_UNICODE(*callresult); |
| 808 | usize = PyUnicode_GET_SIZE(*callresult); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 809 | for (upos = 0; upos<usize;) |
| 810 | *s++ = ucopy[upos++]; |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 811 | /* We're done with the unicode()/repr() => forget it */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 812 | Py_DECREF(*callresult); |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 813 | /* switch to next unicode()/repr() result */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 814 | ++callresult; |
| 815 | break; |
| 816 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 817 | case 'p': |
| 818 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 819 | /* %p is ill-defined: ensure leading 0x. */ |
| 820 | if (buffer[1] == 'X') |
| 821 | buffer[1] = 'x'; |
| 822 | else if (buffer[1] != 'x') { |
| 823 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 824 | buffer[0] = '0'; |
| 825 | buffer[1] = 'x'; |
| 826 | } |
| 827 | appendstring(buffer); |
| 828 | break; |
| 829 | case '%': |
| 830 | *s++ = '%'; |
| 831 | break; |
| 832 | default: |
| 833 | appendstring(p); |
| 834 | goto end; |
| 835 | } |
| 836 | } else |
| 837 | *s++ = *f; |
| 838 | } |
| 839 | |
| 840 | end: |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 841 | if (callresults) |
| 842 | PyMem_Free(callresults); |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 843 | if (abuffer) |
| 844 | PyMem_Free(abuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 845 | _PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
| 846 | return string; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 847 | fail: |
| 848 | if (callresults) { |
| 849 | PyObject **callresult2 = callresults; |
Guido van Rossum | 307fa8c | 2007-07-16 20:46:27 +0000 | [diff] [blame] | 850 | while (callresult2 < callresult) { |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 851 | Py_DECREF(*callresult2); |
| 852 | ++callresult2; |
| 853 | } |
| 854 | PyMem_Free(callresults); |
| 855 | } |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 856 | if (abuffer) |
| 857 | PyMem_Free(abuffer); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 858 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | #undef appendstring |
| 862 | |
| 863 | PyObject * |
| 864 | PyUnicode_FromFormat(const char *format, ...) |
| 865 | { |
| 866 | PyObject* ret; |
| 867 | va_list vargs; |
| 868 | |
| 869 | #ifdef HAVE_STDARG_PROTOTYPES |
| 870 | va_start(vargs, format); |
| 871 | #else |
| 872 | va_start(vargs); |
| 873 | #endif |
| 874 | ret = PyUnicode_FromFormatV(format, vargs); |
| 875 | va_end(vargs); |
| 876 | return ret; |
| 877 | } |
| 878 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 879 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 880 | wchar_t *w, |
| 881 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 882 | { |
| 883 | if (unicode == NULL) { |
| 884 | PyErr_BadInternalCall(); |
| 885 | return -1; |
| 886 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 887 | |
| 888 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 889 | if (size > PyUnicode_GET_SIZE(unicode)) |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 890 | size = PyUnicode_GET_SIZE(unicode) + 1; |
| 891 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 892 | #ifdef HAVE_USABLE_WCHAR_T |
| 893 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 894 | #else |
| 895 | { |
| 896 | register Py_UNICODE *u; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 897 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 898 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 899 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 900 | *w++ = *u++; |
| 901 | } |
| 902 | #endif |
| 903 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 904 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 905 | return PyUnicode_GET_SIZE(unicode); |
| 906 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 907 | return size; |
| 908 | } |
| 909 | |
| 910 | #endif |
| 911 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 912 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 913 | { |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 914 | Py_UNICODE s[2]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 915 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 916 | if (ordinal < 0 || ordinal > 0x10ffff) { |
| 917 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 918 | "chr() arg not in range(0x110000)"); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 919 | return NULL; |
| 920 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 921 | |
| 922 | #ifndef Py_UNICODE_WIDE |
| 923 | if (ordinal > 0xffff) { |
| 924 | ordinal -= 0x10000; |
| 925 | s[0] = 0xD800 | (ordinal >> 10); |
| 926 | s[1] = 0xDC00 | (ordinal & 0x3FF); |
| 927 | return PyUnicode_FromUnicode(s, 2); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 928 | } |
| 929 | #endif |
| 930 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 931 | s[0] = (Py_UNICODE)ordinal; |
| 932 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 935 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 936 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 937 | /* XXX Perhaps we should make this API an alias of |
| 938 | PyObject_Unicode() instead ?! */ |
| 939 | if (PyUnicode_CheckExact(obj)) { |
| 940 | Py_INCREF(obj); |
| 941 | return obj; |
| 942 | } |
| 943 | if (PyUnicode_Check(obj)) { |
| 944 | /* For a Unicode subtype that's not a Unicode object, |
| 945 | return a true Unicode object with the same data. */ |
| 946 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 947 | PyUnicode_GET_SIZE(obj)); |
| 948 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 949 | return PyUnicode_FromEncodedObject(obj, NULL, "strict"); |
| 950 | } |
| 951 | |
| 952 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
| 953 | const char *encoding, |
| 954 | const char *errors) |
| 955 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 956 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 957 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 958 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 959 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 960 | if (obj == NULL) { |
| 961 | PyErr_BadInternalCall(); |
| 962 | return NULL; |
| 963 | } |
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 | #if 0 |
| 966 | /* For b/w compatibility we also accept Unicode objects provided |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 967 | that no encodings is given and then redirect to |
| 968 | PyObject_Unicode() which then applies the additional logic for |
| 969 | Unicode subclasses. |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 970 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 971 | NOTE: This API should really only be used for object which |
| 972 | represent *encoded* Unicode ! |
| 973 | |
| 974 | */ |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 975 | if (PyUnicode_Check(obj)) { |
| 976 | if (encoding) { |
| 977 | PyErr_SetString(PyExc_TypeError, |
| 978 | "decoding Unicode is not supported"); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 979 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 980 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 981 | return PyObject_Unicode(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 982 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 983 | #else |
| 984 | if (PyUnicode_Check(obj)) { |
| 985 | PyErr_SetString(PyExc_TypeError, |
| 986 | "decoding Unicode is not supported"); |
| 987 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 988 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 989 | #endif |
| 990 | |
| 991 | /* Coerce object */ |
| 992 | if (PyString_Check(obj)) { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 993 | s = PyString_AS_STRING(obj); |
| 994 | len = PyString_GET_SIZE(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 995 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 996 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
| 997 | /* Overwrite the error message with something more useful in |
| 998 | case of a TypeError. */ |
| 999 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1000 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1001 | "coercing to Unicode: need string or buffer, " |
| 1002 | "%.80s found", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1003 | Py_Type(obj)->tp_name); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1004 | goto onError; |
| 1005 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1006 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1007 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1008 | if (len == 0) { |
| 1009 | Py_INCREF(unicode_empty); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1010 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1011 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1012 | else |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1013 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1014 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1015 | return v; |
| 1016 | |
| 1017 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1018 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | PyObject *PyUnicode_Decode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1022 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1023 | const char *encoding, |
| 1024 | const char *errors) |
| 1025 | { |
| 1026 | PyObject *buffer = NULL, *unicode; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1027 | |
| 1028 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1029 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1030 | |
| 1031 | /* Shortcuts for common default encodings */ |
| 1032 | if (strcmp(encoding, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1033 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1034 | else if (strcmp(encoding, "latin-1") == 0) |
| 1035 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1036 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1037 | else if (strcmp(encoding, "mbcs") == 0) |
| 1038 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 1039 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1040 | else if (strcmp(encoding, "ascii") == 0) |
| 1041 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1042 | |
| 1043 | /* Decode via the codec registry */ |
| 1044 | buffer = PyBuffer_FromMemory((void *)s, size); |
| 1045 | if (buffer == NULL) |
| 1046 | goto onError; |
| 1047 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1048 | if (unicode == NULL) |
| 1049 | goto onError; |
| 1050 | if (!PyUnicode_Check(unicode)) { |
| 1051 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 1052 | "decoder did not return an unicode object (type=%.400s)", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1053 | Py_Type(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1054 | Py_DECREF(unicode); |
| 1055 | goto onError; |
| 1056 | } |
| 1057 | Py_DECREF(buffer); |
| 1058 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1059 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1060 | onError: |
| 1061 | Py_XDECREF(buffer); |
| 1062 | return NULL; |
| 1063 | } |
| 1064 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1065 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1066 | const char *encoding, |
| 1067 | const char *errors) |
| 1068 | { |
| 1069 | PyObject *v; |
| 1070 | |
| 1071 | if (!PyUnicode_Check(unicode)) { |
| 1072 | PyErr_BadArgument(); |
| 1073 | goto onError; |
| 1074 | } |
| 1075 | |
| 1076 | if (encoding == NULL) |
| 1077 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1078 | |
| 1079 | /* Decode via the codec registry */ |
| 1080 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1081 | if (v == NULL) |
| 1082 | goto onError; |
| 1083 | return v; |
| 1084 | |
| 1085 | onError: |
| 1086 | return NULL; |
| 1087 | } |
| 1088 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1089 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1090 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1091 | const char *encoding, |
| 1092 | const char *errors) |
| 1093 | { |
| 1094 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1095 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1096 | unicode = PyUnicode_FromUnicode(s, size); |
| 1097 | if (unicode == NULL) |
| 1098 | return NULL; |
| 1099 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1100 | Py_DECREF(unicode); |
| 1101 | return v; |
| 1102 | } |
| 1103 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1104 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1105 | const char *encoding, |
| 1106 | const char *errors) |
| 1107 | { |
| 1108 | PyObject *v; |
| 1109 | |
| 1110 | if (!PyUnicode_Check(unicode)) { |
| 1111 | PyErr_BadArgument(); |
| 1112 | goto onError; |
| 1113 | } |
| 1114 | |
| 1115 | if (encoding == NULL) |
| 1116 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1117 | |
| 1118 | /* Encode via the codec registry */ |
| 1119 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1120 | if (v == NULL) |
| 1121 | goto onError; |
| 1122 | return v; |
| 1123 | |
| 1124 | onError: |
| 1125 | return NULL; |
| 1126 | } |
| 1127 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1128 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1129 | const char *encoding, |
| 1130 | const char *errors) |
| 1131 | { |
| 1132 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1133 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1134 | if (!PyUnicode_Check(unicode)) { |
| 1135 | PyErr_BadArgument(); |
| 1136 | goto onError; |
| 1137 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1138 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1139 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1140 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1141 | |
| 1142 | /* Shortcuts for common default encodings */ |
| 1143 | if (errors == NULL) { |
| 1144 | if (strcmp(encoding, "utf-8") == 0) |
Jeremy Hylton | 9cea41c | 2001-05-29 17:13:15 +0000 | [diff] [blame] | 1145 | return PyUnicode_AsUTF8String(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1146 | else if (strcmp(encoding, "latin-1") == 0) |
| 1147 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1148 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1149 | else if (strcmp(encoding, "mbcs") == 0) |
| 1150 | return PyUnicode_AsMBCSString(unicode); |
| 1151 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1152 | else if (strcmp(encoding, "ascii") == 0) |
| 1153 | return PyUnicode_AsASCIIString(unicode); |
| 1154 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1155 | |
| 1156 | /* Encode via the codec registry */ |
| 1157 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1158 | if (v == NULL) |
| 1159 | goto onError; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1160 | if (!PyBytes_Check(v)) { |
| 1161 | if (PyString_Check(v)) { |
| 1162 | /* Old codec, turn it into bytes */ |
| 1163 | PyObject *b = PyBytes_FromObject(v); |
| 1164 | Py_DECREF(v); |
| 1165 | return b; |
| 1166 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1167 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1168 | "encoder did not return a bytes object " |
| 1169 | "(type=%.400s, encoding=%.20s, errors=%.20s)", |
| 1170 | v->ob_type->tp_name, |
| 1171 | encoding ? encoding : "NULL", |
| 1172 | errors ? errors : "NULL"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1173 | Py_DECREF(v); |
| 1174 | goto onError; |
| 1175 | } |
| 1176 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1177 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1178 | onError: |
| 1179 | return NULL; |
| 1180 | } |
| 1181 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1182 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 1183 | const char *errors) |
| 1184 | { |
| 1185 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1186 | PyObject *b; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1187 | if (v) |
| 1188 | return v; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1189 | if (errors != NULL) |
| 1190 | Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString"); |
Neal Norwitz | ab40b30 | 2007-08-12 17:21:38 +0000 | [diff] [blame^] | 1191 | /* XXX(nnorwitz): errors will always be NULL due to the check above. |
| 1192 | Should this check and the else be removed since it's dead code? |
| 1193 | */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1194 | if (errors == NULL) { |
| 1195 | b = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1196 | PyUnicode_GET_SIZE(unicode), |
| 1197 | NULL); |
| 1198 | } |
| 1199 | else { |
| 1200 | b = PyUnicode_AsEncodedString(unicode, NULL, errors); |
| 1201 | } |
| 1202 | if (!b) |
| 1203 | return NULL; |
| 1204 | v = PyString_FromStringAndSize(PyBytes_AsString(b), |
| 1205 | PyBytes_Size(b)); |
| 1206 | Py_DECREF(b); |
Guido van Rossum | e7a0d39 | 2007-07-12 07:53:00 +0000 | [diff] [blame] | 1207 | ((PyUnicodeObject *)unicode)->defenc = v; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1208 | return v; |
| 1209 | } |
| 1210 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1211 | char* |
| 1212 | PyUnicode_AsString(PyObject *unicode) |
| 1213 | { |
| 1214 | assert(PyUnicode_Check(unicode)); |
| 1215 | unicode = _PyUnicode_AsDefaultEncodedString(unicode, NULL); |
| 1216 | if (!unicode) |
| 1217 | return NULL; |
| 1218 | return PyString_AsString(unicode); |
| 1219 | } |
| 1220 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1221 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1222 | { |
| 1223 | if (!PyUnicode_Check(unicode)) { |
| 1224 | PyErr_BadArgument(); |
| 1225 | goto onError; |
| 1226 | } |
| 1227 | return PyUnicode_AS_UNICODE(unicode); |
| 1228 | |
| 1229 | onError: |
| 1230 | return NULL; |
| 1231 | } |
| 1232 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1233 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1234 | { |
| 1235 | if (!PyUnicode_Check(unicode)) { |
| 1236 | PyErr_BadArgument(); |
| 1237 | goto onError; |
| 1238 | } |
| 1239 | return PyUnicode_GET_SIZE(unicode); |
| 1240 | |
| 1241 | onError: |
| 1242 | return -1; |
| 1243 | } |
| 1244 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1245 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1246 | { |
| 1247 | return unicode_default_encoding; |
| 1248 | } |
| 1249 | |
| 1250 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 1251 | { |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1252 | if (strcmp(encoding, unicode_default_encoding) != 0) { |
| 1253 | PyErr_Format(PyExc_ValueError, |
| 1254 | "Can only set default encoding to %s", |
| 1255 | unicode_default_encoding); |
| 1256 | return -1; |
| 1257 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1258 | return 0; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1261 | /* error handling callback helper: |
| 1262 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1263 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1264 | and adjust various state variables. |
| 1265 | return 0 on success, -1 on error |
| 1266 | */ |
| 1267 | |
| 1268 | static |
| 1269 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 1270 | const char *encoding, const char *reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1271 | const char **input, const char **inend, Py_ssize_t *startinpos, Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1272 | PyObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1273 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1274 | 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] | 1275 | |
| 1276 | PyObject *restuple = NULL; |
| 1277 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1278 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1279 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1280 | Py_ssize_t requiredsize; |
| 1281 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1282 | Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1283 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1284 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1285 | int res = -1; |
| 1286 | |
| 1287 | if (*errorHandler == NULL) { |
| 1288 | *errorHandler = PyCodec_LookupError(errors); |
| 1289 | if (*errorHandler == NULL) |
| 1290 | goto onError; |
| 1291 | } |
| 1292 | |
| 1293 | if (*exceptionObject == NULL) { |
| 1294 | *exceptionObject = PyUnicodeDecodeError_Create( |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1295 | encoding, *input, *inend-*input, *startinpos, *endinpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1296 | if (*exceptionObject == NULL) |
| 1297 | goto onError; |
| 1298 | } |
| 1299 | else { |
| 1300 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 1301 | goto onError; |
| 1302 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 1303 | goto onError; |
| 1304 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1305 | goto onError; |
| 1306 | } |
| 1307 | |
| 1308 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1309 | if (restuple == NULL) |
| 1310 | goto onError; |
| 1311 | if (!PyTuple_Check(restuple)) { |
| 1312 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 1313 | goto onError; |
| 1314 | } |
| 1315 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 1316 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1317 | |
| 1318 | /* Copy back the bytes variables, which might have been modified by the |
| 1319 | callback */ |
| 1320 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 1321 | if (!inputobj) |
| 1322 | goto onError; |
| 1323 | if (!PyBytes_Check(inputobj)) { |
| 1324 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
| 1325 | } |
| 1326 | *input = PyBytes_AS_STRING(inputobj); |
| 1327 | insize = PyBytes_GET_SIZE(inputobj); |
| 1328 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 1329 | /* we can DECREF safely, as the exception has another reference, |
| 1330 | so the object won't go away. */ |
| 1331 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1332 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1333 | if (newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1334 | newpos = insize+newpos; |
| 1335 | if (newpos<0 || newpos>insize) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 1336 | 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] | 1337 | goto onError; |
| 1338 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1339 | |
| 1340 | /* need more space? (at least enough for what we |
| 1341 | have+the replacement+the rest of the string (starting |
| 1342 | at the new input position), so we won't have to check space |
| 1343 | when there are no errors in the rest of the string) */ |
| 1344 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 1345 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 1346 | requiredsize = *outpos + repsize + insize-newpos; |
| 1347 | if (requiredsize > outsize) { |
| 1348 | if (requiredsize<2*outsize) |
| 1349 | requiredsize = 2*outsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1350 | if (PyUnicode_Resize(output, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1351 | goto onError; |
| 1352 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
| 1353 | } |
| 1354 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1355 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1356 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 1357 | *outptr += repsize; |
| 1358 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1359 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1360 | /* we made it! */ |
| 1361 | res = 0; |
| 1362 | |
| 1363 | onError: |
| 1364 | Py_XDECREF(restuple); |
| 1365 | return res; |
| 1366 | } |
| 1367 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1368 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 1369 | |
| 1370 | /* see RFC2152 for details */ |
| 1371 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1372 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1373 | char utf7_special[128] = { |
| 1374 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 1375 | encoded: |
| 1376 | 0 - not special |
| 1377 | 1 - special |
| 1378 | 2 - whitespace (optional) |
| 1379 | 3 - RFC2152 Set O (optional) */ |
| 1380 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 1381 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1382 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 1383 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 1384 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1385 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 1386 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1387 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 1388 | |
| 1389 | }; |
| 1390 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1391 | /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
| 1392 | warnings about the comparison always being false; since |
| 1393 | utf7_special[0] is 1, we can safely make that one comparison |
| 1394 | true */ |
| 1395 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1396 | #define SPECIAL(c, encodeO, encodeWS) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1397 | ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1398 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1399 | (encodeO && (utf7_special[(c)] == 3))) |
| 1400 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1401 | #define B64(n) \ |
| 1402 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 1403 | #define B64CHAR(c) \ |
| 1404 | (isalnum(c) || (c) == '+' || (c) == '/') |
| 1405 | #define UB64(c) \ |
| 1406 | ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
| 1407 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1408 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1409 | #define ENCODE(out, ch, bits) \ |
| 1410 | while (bits >= 6) { \ |
| 1411 | *out++ = B64(ch >> (bits-6)); \ |
| 1412 | bits -= 6; \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1415 | #define DECODE(out, ch, bits, surrogate) \ |
| 1416 | while (bits >= 16) { \ |
| 1417 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 1418 | bits -= 16; \ |
| 1419 | if (surrogate) { \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1420 | /* We have already generated an error for the high surrogate \ |
| 1421 | 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] | 1422 | surrogate = 0; \ |
| 1423 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1424 | /* This is a surrogate pair. Unfortunately we can't represent \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1425 | it in a 16-bit character */ \ |
| 1426 | surrogate = 1; \ |
| 1427 | errmsg = "code pairs are not supported"; \ |
| 1428 | goto utf7Error; \ |
| 1429 | } else { \ |
| 1430 | *out++ = outCh; \ |
| 1431 | } \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1432 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1433 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1434 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1435 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1436 | const char *errors) |
| 1437 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1438 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1439 | Py_ssize_t startinpos; |
| 1440 | Py_ssize_t endinpos; |
| 1441 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1442 | const char *e; |
| 1443 | PyUnicodeObject *unicode; |
| 1444 | Py_UNICODE *p; |
| 1445 | const char *errmsg = ""; |
| 1446 | int inShift = 0; |
| 1447 | unsigned int bitsleft = 0; |
| 1448 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1449 | int surrogate = 0; |
| 1450 | PyObject *errorHandler = NULL; |
| 1451 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1452 | |
| 1453 | unicode = _PyUnicode_New(size); |
| 1454 | if (!unicode) |
| 1455 | return NULL; |
| 1456 | if (size == 0) |
| 1457 | return (PyObject *)unicode; |
| 1458 | |
| 1459 | p = unicode->str; |
| 1460 | e = s + size; |
| 1461 | |
| 1462 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1463 | Py_UNICODE ch; |
| 1464 | restart: |
| 1465 | ch = *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1466 | |
| 1467 | if (inShift) { |
| 1468 | if ((ch == '-') || !B64CHAR(ch)) { |
| 1469 | inShift = 0; |
| 1470 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1471 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1472 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1473 | if (bitsleft >= 6) { |
| 1474 | /* The shift sequence has a partial character in it. If |
| 1475 | bitsleft < 6 then we could just classify it as padding |
| 1476 | but that is not the case here */ |
| 1477 | |
| 1478 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1479 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1480 | } |
| 1481 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1482 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1483 | here so indicate the potential of a misencoded character. */ |
| 1484 | |
| 1485 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 1486 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 1487 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1488 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | if (ch == '-') { |
| 1492 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1493 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1494 | inShift = 1; |
| 1495 | } |
| 1496 | } else if (SPECIAL(ch,0,0)) { |
| 1497 | errmsg = "unexpected special character"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1498 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1499 | } else { |
| 1500 | *p++ = ch; |
| 1501 | } |
| 1502 | } else { |
| 1503 | charsleft = (charsleft << 6) | UB64(ch); |
| 1504 | bitsleft += 6; |
| 1505 | s++; |
| 1506 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1507 | } |
| 1508 | } |
| 1509 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1510 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1511 | s++; |
| 1512 | if (s < e && *s == '-') { |
| 1513 | s++; |
| 1514 | *p++ = '+'; |
| 1515 | } else |
| 1516 | { |
| 1517 | inShift = 1; |
| 1518 | bitsleft = 0; |
| 1519 | } |
| 1520 | } |
| 1521 | else if (SPECIAL(ch,0,0)) { |
| 1522 | errmsg = "unexpected special character"; |
| 1523 | s++; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1524 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1525 | } |
| 1526 | else { |
| 1527 | *p++ = ch; |
| 1528 | s++; |
| 1529 | } |
| 1530 | continue; |
| 1531 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1532 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1533 | endinpos = s-starts; |
| 1534 | if (unicode_decode_call_errorhandler( |
| 1535 | errors, &errorHandler, |
| 1536 | "utf7", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1537 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1538 | (PyObject **)&unicode, &outpos, &p)) |
| 1539 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | if (inShift) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1543 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1544 | endinpos = size; |
| 1545 | if (unicode_decode_call_errorhandler( |
| 1546 | errors, &errorHandler, |
| 1547 | "utf7", "unterminated shift sequence", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1548 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1549 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1550 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1551 | if (s < e) |
| 1552 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1555 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1556 | goto onError; |
| 1557 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1558 | Py_XDECREF(errorHandler); |
| 1559 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1560 | return (PyObject *)unicode; |
| 1561 | |
| 1562 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1563 | Py_XDECREF(errorHandler); |
| 1564 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1565 | Py_DECREF(unicode); |
| 1566 | return NULL; |
| 1567 | } |
| 1568 | |
| 1569 | |
| 1570 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1571 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1572 | int encodeSetO, |
| 1573 | int encodeWhiteSpace, |
| 1574 | const char *errors) |
| 1575 | { |
| 1576 | PyObject *v; |
| 1577 | /* It might be possible to tighten this worst case */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1578 | Py_ssize_t cbAllocated = 5 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1579 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1580 | Py_ssize_t i = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1581 | unsigned int bitsleft = 0; |
| 1582 | unsigned long charsleft = 0; |
| 1583 | char * out; |
| 1584 | char * start; |
| 1585 | |
| 1586 | if (size == 0) |
Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1587 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1588 | |
Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1589 | v = PyBytes_FromStringAndSize(NULL, cbAllocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1590 | if (v == NULL) |
| 1591 | return NULL; |
| 1592 | |
Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1593 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1594 | for (;i < size; ++i) { |
| 1595 | Py_UNICODE ch = s[i]; |
| 1596 | |
| 1597 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1598 | if (ch == '+') { |
| 1599 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1600 | *out++ = '-'; |
| 1601 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1602 | charsleft = ch; |
| 1603 | bitsleft = 16; |
| 1604 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1605 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1606 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1607 | } else { |
| 1608 | *out++ = (char) ch; |
| 1609 | } |
| 1610 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1611 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1612 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1613 | charsleft = 0; |
| 1614 | bitsleft = 0; |
| 1615 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1616 | so no '-' is required, except if the character is itself a '-' */ |
| 1617 | if (B64CHAR(ch) || ch == '-') { |
| 1618 | *out++ = '-'; |
| 1619 | } |
| 1620 | inShift = 0; |
| 1621 | *out++ = (char) ch; |
| 1622 | } else { |
| 1623 | bitsleft += 16; |
| 1624 | charsleft = (charsleft << 16) | ch; |
| 1625 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1626 | |
| 1627 | /* If the next character is special then we dont' need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1628 | 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] | 1629 | or '-' then the shift sequence will be terminated implicitly and we |
| 1630 | don't have to insert a '-'. */ |
| 1631 | |
| 1632 | if (bitsleft == 0) { |
| 1633 | if (i + 1 < size) { |
| 1634 | Py_UNICODE ch2 = s[i+1]; |
| 1635 | |
| 1636 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1637 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1638 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1639 | *out++ = '-'; |
| 1640 | inShift = 0; |
| 1641 | } else { |
| 1642 | inShift = 0; |
| 1643 | } |
| 1644 | |
| 1645 | } |
| 1646 | else { |
| 1647 | *out++ = '-'; |
| 1648 | inShift = 0; |
| 1649 | } |
| 1650 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1651 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1652 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1653 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1654 | if (bitsleft) { |
| 1655 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1656 | *out++ = '-'; |
| 1657 | } |
| 1658 | |
Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1659 | if (PyBytes_Resize(v, out - start)) { |
| 1660 | Py_DECREF(v); |
| 1661 | return NULL; |
| 1662 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1663 | return v; |
| 1664 | } |
| 1665 | |
| 1666 | #undef SPECIAL |
| 1667 | #undef B64 |
| 1668 | #undef B64CHAR |
| 1669 | #undef UB64 |
| 1670 | #undef ENCODE |
| 1671 | #undef DECODE |
| 1672 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1673 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1674 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1675 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1676 | char utf8_code_length[256] = { |
| 1677 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1678 | illegal prefix. see RFC 2279 for details */ |
| 1679 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1680 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1681 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1682 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1683 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1684 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1685 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1686 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1687 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1688 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1689 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1690 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1691 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1692 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1693 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1694 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1695 | }; |
| 1696 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1697 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1698 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1699 | const char *errors) |
| 1700 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1701 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 1702 | } |
| 1703 | |
| 1704 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1705 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1706 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1707 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1708 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1709 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1710 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1711 | Py_ssize_t startinpos; |
| 1712 | Py_ssize_t endinpos; |
| 1713 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1714 | const char *e; |
| 1715 | PyUnicodeObject *unicode; |
| 1716 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1717 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1718 | PyObject *errorHandler = NULL; |
| 1719 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1720 | |
| 1721 | /* Note: size will always be longer than the resulting Unicode |
| 1722 | character count */ |
| 1723 | unicode = _PyUnicode_New(size); |
| 1724 | if (!unicode) |
| 1725 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1726 | if (size == 0) { |
| 1727 | if (consumed) |
| 1728 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1729 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1730 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1731 | |
| 1732 | /* Unpack UTF-8 encoded data */ |
| 1733 | p = unicode->str; |
| 1734 | e = s + size; |
| 1735 | |
| 1736 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1737 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1738 | |
| 1739 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1740 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1741 | s++; |
| 1742 | continue; |
| 1743 | } |
| 1744 | |
| 1745 | n = utf8_code_length[ch]; |
| 1746 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1747 | if (s + n > e) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1748 | if (consumed) |
| 1749 | break; |
| 1750 | else { |
| 1751 | errmsg = "unexpected end of data"; |
| 1752 | startinpos = s-starts; |
| 1753 | endinpos = size; |
| 1754 | goto utf8Error; |
| 1755 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1756 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1757 | |
| 1758 | switch (n) { |
| 1759 | |
| 1760 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1761 | errmsg = "unexpected code byte"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1762 | startinpos = s-starts; |
| 1763 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1764 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1765 | |
| 1766 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1767 | errmsg = "internal error"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1768 | startinpos = s-starts; |
| 1769 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1770 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1771 | |
| 1772 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1773 | if ((s[1] & 0xc0) != 0x80) { |
| 1774 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1775 | startinpos = s-starts; |
| 1776 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1777 | goto utf8Error; |
| 1778 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1779 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1780 | if (ch < 0x80) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1781 | startinpos = s-starts; |
| 1782 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1783 | errmsg = "illegal encoding"; |
| 1784 | goto utf8Error; |
| 1785 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1786 | else |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1787 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1788 | break; |
| 1789 | |
| 1790 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1791 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1792 | (s[2] & 0xc0) != 0x80) { |
| 1793 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1794 | startinpos = s-starts; |
| 1795 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1796 | goto utf8Error; |
| 1797 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1798 | 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] | 1799 | if (ch < 0x0800) { |
| 1800 | /* Note: UTF-8 encodings of surrogates are considered |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1801 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1802 | |
| 1803 | XXX For wide builds (UCS-4) we should probably try |
| 1804 | to recombine the surrogates into a single code |
| 1805 | unit. |
| 1806 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1807 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1808 | startinpos = s-starts; |
| 1809 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1810 | goto utf8Error; |
| 1811 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1812 | else |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1813 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1814 | break; |
| 1815 | |
| 1816 | case 4: |
| 1817 | if ((s[1] & 0xc0) != 0x80 || |
| 1818 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1819 | (s[3] & 0xc0) != 0x80) { |
| 1820 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1821 | startinpos = s-starts; |
| 1822 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1823 | goto utf8Error; |
| 1824 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1825 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 1826 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 1827 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1828 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1829 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1830 | || (ch > 0x10ffff)) /* maximum value allowed for |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1831 | UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1832 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1833 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1834 | startinpos = s-starts; |
| 1835 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1836 | goto utf8Error; |
| 1837 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1838 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1839 | *p++ = (Py_UNICODE)ch; |
| 1840 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1841 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1842 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1843 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 1844 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1845 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1846 | /* high surrogate = top 10 bits added to D800 */ |
| 1847 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1848 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1849 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1850 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1851 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1852 | break; |
| 1853 | |
| 1854 | default: |
| 1855 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1856 | errmsg = "unsupported Unicode code range"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1857 | startinpos = s-starts; |
| 1858 | endinpos = startinpos+n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1859 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1860 | } |
| 1861 | s += n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1862 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1863 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1864 | utf8Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1865 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1866 | if (unicode_decode_call_errorhandler( |
| 1867 | errors, &errorHandler, |
| 1868 | "utf8", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1869 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1870 | (PyObject **)&unicode, &outpos, &p)) |
| 1871 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1872 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1873 | if (consumed) |
| 1874 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1875 | |
| 1876 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1877 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1878 | goto onError; |
| 1879 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1880 | Py_XDECREF(errorHandler); |
| 1881 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1882 | return (PyObject *)unicode; |
| 1883 | |
| 1884 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1885 | Py_XDECREF(errorHandler); |
| 1886 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1887 | Py_DECREF(unicode); |
| 1888 | return NULL; |
| 1889 | } |
| 1890 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1891 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 1892 | and allocate exactly as much space needed at the end. Else allocate the |
| 1893 | maximum possible needed (4 result bytes per Unicode character), and return |
| 1894 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1895 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1896 | PyObject * |
| 1897 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1898 | Py_ssize_t size, |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1899 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1900 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1901 | #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] | 1902 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1903 | Py_ssize_t i; /* index into s of next input byte */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1904 | PyObject *v; /* result string object */ |
| 1905 | char *p; /* next free byte in output buffer */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1906 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1907 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1908 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1909 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1910 | assert(s != NULL); |
| 1911 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1912 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1913 | if (size <= MAX_SHORT_UNICHARS) { |
| 1914 | /* Write into the stack buffer; nallocated can't overflow. |
| 1915 | * At the end, we'll allocate exactly as much heap space as it |
| 1916 | * turns out we need. |
| 1917 | */ |
| 1918 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
| 1919 | v = NULL; /* will allocate after we're done */ |
| 1920 | p = stackbuf; |
| 1921 | } |
| 1922 | else { |
| 1923 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 1924 | nallocated = size * 4; |
| 1925 | if (nallocated / 4 != size) /* overflow! */ |
| 1926 | return PyErr_NoMemory(); |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1927 | v = PyBytes_FromStringAndSize(NULL, nallocated); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1928 | if (v == NULL) |
| 1929 | return NULL; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1930 | p = PyBytes_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1931 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1932 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1933 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1934 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1935 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1936 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1937 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1938 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1939 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1940 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1941 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 1942 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 1943 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1944 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1945 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1946 | /* Encode UCS2 Unicode ordinals */ |
| 1947 | if (ch < 0x10000) { |
| 1948 | /* Special case: check for high surrogate */ |
| 1949 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 1950 | Py_UCS4 ch2 = s[i]; |
| 1951 | /* Check for low surrogate and combine the two to |
| 1952 | form a UCS4 value */ |
| 1953 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1954 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1955 | i++; |
| 1956 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1957 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1958 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1959 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1960 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1961 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1962 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1963 | continue; |
| 1964 | } |
| 1965 | encodeUCS4: |
| 1966 | /* Encode UCS4 Unicode ordinals */ |
| 1967 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 1968 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 1969 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1970 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1971 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1972 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 1973 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1974 | if (v == NULL) { |
| 1975 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1976 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1977 | assert(nneeded <= nallocated); |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1978 | v = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1979 | } |
| 1980 | else { |
| 1981 | /* Cut back to size actually needed. */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1982 | nneeded = p - PyBytes_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1983 | assert(nneeded <= nallocated); |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1984 | PyBytes_Resize(v, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1985 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1986 | return v; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1987 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1988 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1991 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 1992 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1993 | if (!PyUnicode_Check(unicode)) { |
| 1994 | PyErr_BadArgument(); |
| 1995 | return NULL; |
| 1996 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 1997 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1998 | PyUnicode_GET_SIZE(unicode), |
| 1999 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2000 | } |
| 2001 | |
| 2002 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 2003 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2004 | PyObject * |
| 2005 | PyUnicode_DecodeUTF16(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2006 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2007 | const char *errors, |
| 2008 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2009 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2010 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 2011 | } |
| 2012 | |
| 2013 | PyObject * |
| 2014 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2015 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2016 | const char *errors, |
| 2017 | int *byteorder, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2018 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2019 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2020 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2021 | Py_ssize_t startinpos; |
| 2022 | Py_ssize_t endinpos; |
| 2023 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2024 | PyUnicodeObject *unicode; |
| 2025 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2026 | const unsigned char *q, *e; |
| 2027 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2028 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2029 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 2030 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2031 | int ihi = 1, ilo = 0; |
| 2032 | #else |
| 2033 | int ihi = 0, ilo = 1; |
| 2034 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2035 | PyObject *errorHandler = NULL; |
| 2036 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2037 | |
| 2038 | /* Note: size will always be longer than the resulting Unicode |
| 2039 | character count */ |
| 2040 | unicode = _PyUnicode_New(size); |
| 2041 | if (!unicode) |
| 2042 | return NULL; |
| 2043 | if (size == 0) |
| 2044 | return (PyObject *)unicode; |
| 2045 | |
| 2046 | /* Unpack UTF-16 encoded data */ |
| 2047 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2048 | q = (unsigned char *)s; |
| 2049 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2050 | |
| 2051 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2052 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2053 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2054 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2055 | byte order setting accordingly. In native mode, the leading BOM |
| 2056 | mark is skipped, in all other modes, it is copied to the output |
| 2057 | stream as-is (giving a ZWNBSP character). */ |
| 2058 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2059 | if (size >= 2) { |
| 2060 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2061 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2062 | if (bom == 0xFEFF) { |
| 2063 | q += 2; |
| 2064 | bo = -1; |
| 2065 | } |
| 2066 | else if (bom == 0xFFFE) { |
| 2067 | q += 2; |
| 2068 | bo = 1; |
| 2069 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2070 | #else |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2071 | if (bom == 0xFEFF) { |
| 2072 | q += 2; |
| 2073 | bo = 1; |
| 2074 | } |
| 2075 | else if (bom == 0xFFFE) { |
| 2076 | q += 2; |
| 2077 | bo = -1; |
| 2078 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2079 | #endif |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2080 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2081 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2082 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2083 | if (bo == -1) { |
| 2084 | /* force LE */ |
| 2085 | ihi = 1; |
| 2086 | ilo = 0; |
| 2087 | } |
| 2088 | else if (bo == 1) { |
| 2089 | /* force BE */ |
| 2090 | ihi = 0; |
| 2091 | ilo = 1; |
| 2092 | } |
| 2093 | |
| 2094 | while (q < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2095 | Py_UNICODE ch; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2096 | /* remaining bytes at the end? (size should be even) */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2097 | if (e-q<2) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2098 | if (consumed) |
| 2099 | break; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2100 | errmsg = "truncated data"; |
| 2101 | startinpos = ((const char *)q)-starts; |
| 2102 | endinpos = ((const char *)e)-starts; |
| 2103 | goto utf16Error; |
| 2104 | /* The remaining input chars are ignored if the callback |
| 2105 | chooses to skip the input */ |
| 2106 | } |
| 2107 | ch = (q[ihi] << 8) | q[ilo]; |
| 2108 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2109 | q += 2; |
| 2110 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2111 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 2112 | *p++ = ch; |
| 2113 | continue; |
| 2114 | } |
| 2115 | |
| 2116 | /* UTF-16 code pair: */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2117 | if (q >= e) { |
| 2118 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2119 | startinpos = (((const char *)q)-2)-starts; |
| 2120 | endinpos = ((const char *)e)-starts; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2121 | goto utf16Error; |
| 2122 | } |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2123 | if (0xD800 <= ch && ch <= 0xDBFF) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2124 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 2125 | q += 2; |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2126 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2127 | #ifndef Py_UNICODE_WIDE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2128 | *p++ = ch; |
| 2129 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2130 | #else |
| 2131 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2132 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2133 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2134 | } |
| 2135 | else { |
| 2136 | errmsg = "illegal UTF-16 surrogate"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2137 | startinpos = (((const char *)q)-4)-starts; |
| 2138 | endinpos = startinpos+2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2139 | goto utf16Error; |
| 2140 | } |
| 2141 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2142 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2143 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2144 | startinpos = (((const char *)q)-2)-starts; |
| 2145 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2146 | /* Fall through to report the error */ |
| 2147 | |
| 2148 | utf16Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2149 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2150 | if (unicode_decode_call_errorhandler( |
| 2151 | errors, &errorHandler, |
| 2152 | "utf16", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2153 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2154 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2155 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2156 | } |
| 2157 | |
| 2158 | if (byteorder) |
| 2159 | *byteorder = bo; |
| 2160 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2161 | if (consumed) |
| 2162 | *consumed = (const char *)q-starts; |
| 2163 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2164 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2165 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2166 | goto onError; |
| 2167 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2168 | Py_XDECREF(errorHandler); |
| 2169 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2170 | return (PyObject *)unicode; |
| 2171 | |
| 2172 | onError: |
| 2173 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2174 | Py_XDECREF(errorHandler); |
| 2175 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2176 | return NULL; |
| 2177 | } |
| 2178 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2179 | PyObject * |
| 2180 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2181 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2182 | const char *errors, |
| 2183 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2184 | { |
| 2185 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2186 | unsigned char *p; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2187 | #ifdef Py_UNICODE_WIDE |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2188 | int i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2189 | #else |
| 2190 | const int pairs = 0; |
| 2191 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2192 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2193 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2194 | int ihi = 1, ilo = 0; |
| 2195 | #else |
| 2196 | int ihi = 0, ilo = 1; |
| 2197 | #endif |
| 2198 | |
| 2199 | #define STORECHAR(CH) \ |
| 2200 | do { \ |
| 2201 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 2202 | p[ilo] = (CH) & 0xff; \ |
| 2203 | p += 2; \ |
| 2204 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2205 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2206 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2207 | for (i = pairs = 0; i < size; i++) |
| 2208 | if (s[i] >= 0x10000) |
| 2209 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2210 | #endif |
Walter Dörwald | 3cc3452 | 2007-05-04 10:48:27 +0000 | [diff] [blame] | 2211 | v = PyBytes_FromStringAndSize(NULL, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2212 | 2 * (size + pairs + (byteorder == 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2213 | if (v == NULL) |
| 2214 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2215 | |
Walter Dörwald | 3cc3452 | 2007-05-04 10:48:27 +0000 | [diff] [blame] | 2216 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2217 | if (byteorder == 0) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2218 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2219 | if (size == 0) |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2220 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2221 | |
| 2222 | if (byteorder == -1) { |
| 2223 | /* force LE */ |
| 2224 | ihi = 1; |
| 2225 | ilo = 0; |
| 2226 | } |
| 2227 | else if (byteorder == 1) { |
| 2228 | /* force BE */ |
| 2229 | ihi = 0; |
| 2230 | ilo = 1; |
| 2231 | } |
| 2232 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2233 | while (size-- > 0) { |
| 2234 | Py_UNICODE ch = *s++; |
| 2235 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2236 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2237 | if (ch >= 0x10000) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2238 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2239 | ch = 0xD800 | ((ch-0x10000) >> 10); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2240 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2241 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2242 | STORECHAR(ch); |
| 2243 | if (ch2) |
| 2244 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2245 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2246 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2247 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 2251 | { |
| 2252 | if (!PyUnicode_Check(unicode)) { |
| 2253 | PyErr_BadArgument(); |
| 2254 | return NULL; |
| 2255 | } |
| 2256 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 2257 | PyUnicode_GET_SIZE(unicode), |
| 2258 | NULL, |
| 2259 | 0); |
| 2260 | } |
| 2261 | |
| 2262 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 2263 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2264 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 2265 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2266 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2267 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2268 | const char *errors) |
| 2269 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2270 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2271 | Py_ssize_t startinpos; |
| 2272 | Py_ssize_t endinpos; |
| 2273 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2274 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2275 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2276 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2277 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2278 | char* message; |
| 2279 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2280 | PyObject *errorHandler = NULL; |
| 2281 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2282 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2283 | /* Escaped strings will always be longer than the resulting |
| 2284 | 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] | 2285 | length after conversion to the true value. |
| 2286 | (but if the error callback returns a long replacement string |
| 2287 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2288 | v = _PyUnicode_New(size); |
| 2289 | if (v == NULL) |
| 2290 | goto onError; |
| 2291 | if (size == 0) |
| 2292 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2293 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2294 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2295 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2296 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2297 | while (s < end) { |
| 2298 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2299 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2300 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2301 | |
| 2302 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2303 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2304 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2305 | continue; |
| 2306 | } |
| 2307 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2308 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2309 | /* \ - Escapes */ |
| 2310 | s++; |
| 2311 | switch (*s++) { |
| 2312 | |
| 2313 | /* \x escapes */ |
| 2314 | case '\n': break; |
| 2315 | case '\\': *p++ = '\\'; break; |
| 2316 | case '\'': *p++ = '\''; break; |
| 2317 | case '\"': *p++ = '\"'; break; |
| 2318 | case 'b': *p++ = '\b'; break; |
| 2319 | case 'f': *p++ = '\014'; break; /* FF */ |
| 2320 | case 't': *p++ = '\t'; break; |
| 2321 | case 'n': *p++ = '\n'; break; |
| 2322 | case 'r': *p++ = '\r'; break; |
| 2323 | case 'v': *p++ = '\013'; break; /* VT */ |
| 2324 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 2325 | |
| 2326 | /* \OOO (octal) escapes */ |
| 2327 | case '0': case '1': case '2': case '3': |
| 2328 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2329 | x = s[-1] - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2330 | if ('0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2331 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2332 | if ('0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2333 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2334 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2335 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2336 | break; |
| 2337 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2338 | /* hex escapes */ |
| 2339 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2340 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2341 | digits = 2; |
| 2342 | message = "truncated \\xXX escape"; |
| 2343 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2344 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2345 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2346 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2347 | digits = 4; |
| 2348 | message = "truncated \\uXXXX escape"; |
| 2349 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2350 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2351 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2352 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2353 | digits = 8; |
| 2354 | message = "truncated \\UXXXXXXXX escape"; |
| 2355 | hexescape: |
| 2356 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2357 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2358 | if (s+digits>end) { |
| 2359 | endinpos = size; |
| 2360 | if (unicode_decode_call_errorhandler( |
| 2361 | errors, &errorHandler, |
| 2362 | "unicodeescape", "end of string in escape sequence", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2363 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2364 | (PyObject **)&v, &outpos, &p)) |
| 2365 | goto onError; |
| 2366 | goto nextByte; |
| 2367 | } |
| 2368 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2369 | c = (unsigned char) s[i]; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2370 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2371 | endinpos = (s+i+1)-starts; |
| 2372 | if (unicode_decode_call_errorhandler( |
| 2373 | errors, &errorHandler, |
| 2374 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2375 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2376 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2377 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2378 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2379 | } |
| 2380 | chr = (chr<<4) & ~0xF; |
| 2381 | if (c >= '0' && c <= '9') |
| 2382 | chr += c - '0'; |
| 2383 | else if (c >= 'a' && c <= 'f') |
| 2384 | chr += 10 + c - 'a'; |
| 2385 | else |
| 2386 | chr += 10 + c - 'A'; |
| 2387 | } |
| 2388 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 2389 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2390 | /* _decoding_error will have already written into the |
| 2391 | target buffer. */ |
| 2392 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2393 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2394 | /* when we get here, chr is a 32-bit unicode character */ |
| 2395 | if (chr <= 0xffff) |
| 2396 | /* UCS-2 character */ |
| 2397 | *p++ = (Py_UNICODE) chr; |
| 2398 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2399 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2400 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2401 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2402 | *p++ = chr; |
| 2403 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2404 | chr -= 0x10000L; |
| 2405 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2406 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2407 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2408 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2409 | endinpos = s-starts; |
| 2410 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2411 | if (unicode_decode_call_errorhandler( |
| 2412 | errors, &errorHandler, |
| 2413 | "unicodeescape", "illegal Unicode character", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2414 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2415 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2416 | goto onError; |
| 2417 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2418 | break; |
| 2419 | |
| 2420 | /* \N{name} */ |
| 2421 | case 'N': |
| 2422 | message = "malformed \\N character escape"; |
| 2423 | if (ucnhash_CAPI == NULL) { |
| 2424 | /* load the unicode data module */ |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2425 | PyObject *m, *api; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2426 | m = PyImport_ImportModule("unicodedata"); |
| 2427 | if (m == NULL) |
| 2428 | goto ucnhashError; |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2429 | api = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2430 | Py_DECREF(m); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2431 | if (api == NULL) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2432 | goto ucnhashError; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2433 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2434 | Py_DECREF(api); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2435 | if (ucnhash_CAPI == NULL) |
| 2436 | goto ucnhashError; |
| 2437 | } |
| 2438 | if (*s == '{') { |
| 2439 | const char *start = s+1; |
| 2440 | /* look for the closing brace */ |
| 2441 | while (*s != '}' && s < end) |
| 2442 | s++; |
| 2443 | if (s > start && s < end && *s == '}') { |
| 2444 | /* found a name. look it up in the unicode database */ |
| 2445 | message = "unknown Unicode character name"; |
| 2446 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 2447 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2448 | goto store; |
| 2449 | } |
| 2450 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2451 | endinpos = s-starts; |
| 2452 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2453 | if (unicode_decode_call_errorhandler( |
| 2454 | errors, &errorHandler, |
| 2455 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2456 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2457 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2458 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2459 | break; |
| 2460 | |
| 2461 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2462 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2463 | message = "\\ at end of string"; |
| 2464 | s--; |
| 2465 | endinpos = s-starts; |
| 2466 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2467 | if (unicode_decode_call_errorhandler( |
| 2468 | errors, &errorHandler, |
| 2469 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2470 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2471 | (PyObject **)&v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2472 | goto onError; |
| 2473 | } |
| 2474 | else { |
| 2475 | *p++ = '\\'; |
| 2476 | *p++ = (unsigned char)s[-1]; |
| 2477 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2478 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2479 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2480 | nextByte: |
| 2481 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2482 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2483 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2484 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 2485 | Py_XDECREF(errorHandler); |
| 2486 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2487 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2488 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2489 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2490 | PyErr_SetString( |
| 2491 | PyExc_UnicodeError, |
| 2492 | "\\N escapes not supported (can't load unicodedata module)" |
| 2493 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2494 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2495 | Py_XDECREF(errorHandler); |
| 2496 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 2497 | return NULL; |
| 2498 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2499 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2500 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2501 | Py_XDECREF(errorHandler); |
| 2502 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2503 | return NULL; |
| 2504 | } |
| 2505 | |
| 2506 | /* Return a Unicode-Escape string version of the Unicode object. |
| 2507 | |
| 2508 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 2509 | appropriate. |
| 2510 | |
| 2511 | */ |
| 2512 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2513 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
| 2514 | Py_ssize_t size, |
| 2515 | Py_UNICODE ch) |
| 2516 | { |
| 2517 | /* like wcschr, but doesn't stop at NULL characters */ |
| 2518 | |
| 2519 | while (size-- > 0) { |
| 2520 | if (*s == ch) |
| 2521 | return s; |
| 2522 | s++; |
| 2523 | } |
| 2524 | |
| 2525 | return NULL; |
| 2526 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 2527 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2528 | static const char *hexdigits = "0123456789abcdef"; |
| 2529 | |
| 2530 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 2531 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2532 | { |
| 2533 | PyObject *repr; |
| 2534 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2535 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2536 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 2537 | better to choose a different scheme. Perhaps scan the |
| 2538 | first N-chars of the string and allocate based on that size. |
| 2539 | */ |
| 2540 | /* Initial allocation is based on the longest-possible unichr |
| 2541 | escape. |
| 2542 | |
| 2543 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 2544 | unichr, so in this case it's the longest unichr escape. In |
| 2545 | narrow (UTF-16) builds this is five chars per source unichr |
| 2546 | since there are two unichrs in the surrogate pair, so in narrow |
| 2547 | (UTF-16) builds it's not the longest unichr escape. |
| 2548 | |
| 2549 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 2550 | so in the narrow (UTF-16) build case it's the longest unichr |
| 2551 | escape. |
| 2552 | */ |
| 2553 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2554 | repr = PyBytes_FromStringAndSize(NULL, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2555 | #ifdef Py_UNICODE_WIDE |
| 2556 | + 10*size |
| 2557 | #else |
| 2558 | + 6*size |
| 2559 | #endif |
| 2560 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2561 | if (repr == NULL) |
| 2562 | return NULL; |
| 2563 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2564 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2565 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2566 | while (size-- > 0) { |
| 2567 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2568 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2569 | /* Escape backslashes */ |
| 2570 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2571 | *p++ = '\\'; |
| 2572 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2573 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2574 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2575 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 2576 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2577 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 2578 | else if (ch >= 0x10000) { |
| 2579 | *p++ = '\\'; |
| 2580 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2581 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 2582 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 2583 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 2584 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 2585 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 2586 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 2587 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 2588 | *p++ = hexdigits[ch & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2589 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2590 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2591 | #else |
| 2592 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2593 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 2594 | Py_UNICODE ch2; |
| 2595 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2596 | |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2597 | ch2 = *s++; |
| 2598 | size--; |
| 2599 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 2600 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 2601 | *p++ = '\\'; |
| 2602 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2603 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 2604 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 2605 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 2606 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 2607 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 2608 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 2609 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 2610 | *p++ = hexdigits[ucs & 0x0000000F]; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2611 | continue; |
| 2612 | } |
| 2613 | /* Fall through: isolated surrogates are copied as-is */ |
| 2614 | s--; |
| 2615 | size++; |
| 2616 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2617 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2618 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2619 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2620 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2621 | *p++ = '\\'; |
| 2622 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2623 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 2624 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 2625 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 2626 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2627 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2628 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2629 | /* Map special whitespace to '\t', \n', '\r' */ |
| 2630 | else if (ch == '\t') { |
| 2631 | *p++ = '\\'; |
| 2632 | *p++ = 't'; |
| 2633 | } |
| 2634 | else if (ch == '\n') { |
| 2635 | *p++ = '\\'; |
| 2636 | *p++ = 'n'; |
| 2637 | } |
| 2638 | else if (ch == '\r') { |
| 2639 | *p++ = '\\'; |
| 2640 | *p++ = 'r'; |
| 2641 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2642 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2643 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 2644 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2645 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2646 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2647 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 2648 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2649 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2650 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2651 | /* Copy everything else as-is */ |
| 2652 | else |
| 2653 | *p++ = (char) ch; |
| 2654 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2655 | |
| 2656 | *p = '\0'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2657 | if (PyBytes_Resize(repr, p - PyBytes_AS_STRING(repr))) { |
| 2658 | Py_DECREF(repr); |
| 2659 | return NULL; |
| 2660 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2661 | return repr; |
| 2662 | } |
| 2663 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2664 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 2665 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2666 | PyObject *s, *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2667 | if (!PyUnicode_Check(unicode)) { |
| 2668 | PyErr_BadArgument(); |
| 2669 | return NULL; |
| 2670 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2671 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2672 | PyUnicode_GET_SIZE(unicode)); |
| 2673 | |
| 2674 | if (!s) |
| 2675 | return NULL; |
| 2676 | result = PyString_FromStringAndSize(PyBytes_AS_STRING(s), |
| 2677 | PyBytes_GET_SIZE(s)); |
| 2678 | Py_DECREF(s); |
| 2679 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2680 | } |
| 2681 | |
| 2682 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 2683 | |
| 2684 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2685 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2686 | const char *errors) |
| 2687 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2688 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2689 | Py_ssize_t startinpos; |
| 2690 | Py_ssize_t endinpos; |
| 2691 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2692 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2693 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2694 | const char *end; |
| 2695 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2696 | PyObject *errorHandler = NULL; |
| 2697 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2698 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2699 | /* Escaped strings will always be longer than the resulting |
| 2700 | 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] | 2701 | length after conversion to the true value. (But decoding error |
| 2702 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2703 | v = _PyUnicode_New(size); |
| 2704 | if (v == NULL) |
| 2705 | goto onError; |
| 2706 | if (size == 0) |
| 2707 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2708 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2709 | end = s + size; |
| 2710 | while (s < end) { |
| 2711 | unsigned char c; |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 2712 | Py_UCS4 x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2713 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2714 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2715 | |
| 2716 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2717 | if (*s != '\\') { |
| 2718 | *p++ = (unsigned char)*s++; |
| 2719 | continue; |
| 2720 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2721 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2722 | |
| 2723 | /* \u-escapes are only interpreted iff the number of leading |
| 2724 | backslashes if odd */ |
| 2725 | bs = s; |
| 2726 | for (;s < end;) { |
| 2727 | if (*s != '\\') |
| 2728 | break; |
| 2729 | *p++ = (unsigned char)*s++; |
| 2730 | } |
| 2731 | if (((s - bs) & 1) == 0 || |
| 2732 | s >= end || |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2733 | (*s != 'u' && *s != 'U')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2734 | continue; |
| 2735 | } |
| 2736 | p--; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2737 | count = *s=='u' ? 4 : 8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2738 | s++; |
| 2739 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2740 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2741 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2742 | for (x = 0, i = 0; i < count; ++i, ++s) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2743 | c = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2744 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2745 | endinpos = s-starts; |
| 2746 | if (unicode_decode_call_errorhandler( |
| 2747 | errors, &errorHandler, |
| 2748 | "rawunicodeescape", "truncated \\uXXXX", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2749 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2750 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2751 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2752 | goto nextByte; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2753 | } |
| 2754 | x = (x<<4) & ~0xF; |
| 2755 | if (c >= '0' && c <= '9') |
| 2756 | x += c - '0'; |
| 2757 | else if (c >= 'a' && c <= 'f') |
| 2758 | x += 10 + c - 'a'; |
| 2759 | else |
| 2760 | x += 10 + c - 'A'; |
| 2761 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2762 | #ifndef Py_UNICODE_WIDE |
| 2763 | if (x > 0x10000) { |
| 2764 | if (unicode_decode_call_errorhandler( |
| 2765 | errors, &errorHandler, |
| 2766 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2767 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2768 | (PyObject **)&v, &outpos, &p)) |
| 2769 | goto onError; |
| 2770 | } |
| 2771 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2772 | *p++ = x; |
| 2773 | nextByte: |
| 2774 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2775 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2776 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2777 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2778 | Py_XDECREF(errorHandler); |
| 2779 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2780 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2781 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2782 | onError: |
| 2783 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2784 | Py_XDECREF(errorHandler); |
| 2785 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2786 | return NULL; |
| 2787 | } |
| 2788 | |
| 2789 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2790 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2791 | { |
| 2792 | PyObject *repr; |
| 2793 | char *p; |
| 2794 | char *q; |
| 2795 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2796 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 2797 | repr = PyBytes_FromStringAndSize(NULL, 10 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2798 | #else |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 2799 | repr = PyBytes_FromStringAndSize(NULL, 6 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2800 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2801 | if (repr == NULL) |
| 2802 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2803 | if (size == 0) |
| 2804 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2805 | |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 2806 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2807 | while (size-- > 0) { |
| 2808 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2809 | #ifdef Py_UNICODE_WIDE |
| 2810 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2811 | if (ch >= 0x10000) { |
| 2812 | *p++ = '\\'; |
| 2813 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 2814 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 2815 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 2816 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 2817 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 2818 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 2819 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 2820 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 2821 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2822 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2823 | else |
| 2824 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2825 | /* Map 16-bit characters to '\uxxxx' */ |
| 2826 | if (ch >= 256) { |
| 2827 | *p++ = '\\'; |
| 2828 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 2829 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 2830 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 2831 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 2832 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2833 | } |
| 2834 | /* Copy everything else as-is */ |
| 2835 | else |
| 2836 | *p++ = (char) ch; |
| 2837 | } |
| 2838 | *p = '\0'; |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 2839 | if (PyBytes_Resize(repr, p - q)) { |
| 2840 | Py_DECREF(repr); |
| 2841 | return NULL; |
| 2842 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2843 | return repr; |
| 2844 | } |
| 2845 | |
| 2846 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 2847 | { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 2848 | PyObject *s, *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2849 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 2850 | PyErr_BadArgument(); |
| 2851 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2852 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 2853 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2854 | PyUnicode_GET_SIZE(unicode)); |
| 2855 | |
| 2856 | if (!s) |
| 2857 | return NULL; |
| 2858 | result = PyString_FromStringAndSize(PyBytes_AS_STRING(s), |
| 2859 | PyBytes_GET_SIZE(s)); |
| 2860 | Py_DECREF(s); |
| 2861 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2862 | } |
| 2863 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2864 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 2865 | |
| 2866 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2867 | Py_ssize_t size, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2868 | const char *errors) |
| 2869 | { |
| 2870 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2871 | Py_ssize_t startinpos; |
| 2872 | Py_ssize_t endinpos; |
| 2873 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2874 | PyUnicodeObject *v; |
| 2875 | Py_UNICODE *p; |
| 2876 | const char *end; |
| 2877 | const char *reason; |
| 2878 | PyObject *errorHandler = NULL; |
| 2879 | PyObject *exc = NULL; |
| 2880 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 2881 | #ifdef Py_UNICODE_WIDE |
| 2882 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 2883 | #endif |
| 2884 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2885 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2886 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 2887 | if (v == NULL) |
| 2888 | goto onError; |
| 2889 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
| 2890 | return (PyObject *)v; |
| 2891 | p = PyUnicode_AS_UNICODE(v); |
| 2892 | end = s + size; |
| 2893 | |
| 2894 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2895 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2896 | /* We have to sanity check the raw data, otherwise doom looms for |
| 2897 | some malformed UCS-4 data. */ |
| 2898 | if ( |
| 2899 | #ifdef Py_UNICODE_WIDE |
| 2900 | *p > unimax || *p < 0 || |
| 2901 | #endif |
| 2902 | end-s < Py_UNICODE_SIZE |
| 2903 | ) |
| 2904 | { |
| 2905 | startinpos = s - starts; |
| 2906 | if (end-s < Py_UNICODE_SIZE) { |
| 2907 | endinpos = end-starts; |
| 2908 | reason = "truncated input"; |
| 2909 | } |
| 2910 | else { |
| 2911 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 2912 | reason = "illegal code point (> 0x10FFFF)"; |
| 2913 | } |
| 2914 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 2915 | if (unicode_decode_call_errorhandler( |
| 2916 | errors, &errorHandler, |
| 2917 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2918 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2919 | (PyObject **)&v, &outpos, &p)) { |
| 2920 | goto onError; |
| 2921 | } |
| 2922 | } |
| 2923 | else { |
| 2924 | p++; |
| 2925 | s += Py_UNICODE_SIZE; |
| 2926 | } |
| 2927 | } |
| 2928 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2929 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2930 | goto onError; |
| 2931 | Py_XDECREF(errorHandler); |
| 2932 | Py_XDECREF(exc); |
| 2933 | return (PyObject *)v; |
| 2934 | |
| 2935 | onError: |
| 2936 | Py_XDECREF(v); |
| 2937 | Py_XDECREF(errorHandler); |
| 2938 | Py_XDECREF(exc); |
| 2939 | return NULL; |
| 2940 | } |
| 2941 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2942 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 2943 | |
| 2944 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2945 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2946 | const char *errors) |
| 2947 | { |
| 2948 | PyUnicodeObject *v; |
| 2949 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2950 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2951 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2952 | if (size == 1) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 2953 | Py_UNICODE r = *(unsigned char*)s; |
| 2954 | return PyUnicode_FromUnicode(&r, 1); |
| 2955 | } |
| 2956 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2957 | v = _PyUnicode_New(size); |
| 2958 | if (v == NULL) |
| 2959 | goto onError; |
| 2960 | if (size == 0) |
| 2961 | return (PyObject *)v; |
| 2962 | p = PyUnicode_AS_UNICODE(v); |
| 2963 | while (size-- > 0) |
| 2964 | *p++ = (unsigned char)*s++; |
| 2965 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2966 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2967 | onError: |
| 2968 | Py_XDECREF(v); |
| 2969 | return NULL; |
| 2970 | } |
| 2971 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2972 | /* create or adjust a UnicodeEncodeError */ |
| 2973 | static void make_encode_exception(PyObject **exceptionObject, |
| 2974 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2975 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 2976 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2977 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2978 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2979 | if (*exceptionObject == NULL) { |
| 2980 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 2981 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2982 | } |
| 2983 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2984 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 2985 | goto onError; |
| 2986 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 2987 | goto onError; |
| 2988 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 2989 | goto onError; |
| 2990 | return; |
| 2991 | onError: |
| 2992 | Py_DECREF(*exceptionObject); |
| 2993 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2994 | } |
| 2995 | } |
| 2996 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2997 | /* raises a UnicodeEncodeError */ |
| 2998 | static void raise_encode_exception(PyObject **exceptionObject, |
| 2999 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3000 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3001 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3002 | const char *reason) |
| 3003 | { |
| 3004 | make_encode_exception(exceptionObject, |
| 3005 | encoding, unicode, size, startpos, endpos, reason); |
| 3006 | if (*exceptionObject != NULL) |
| 3007 | PyCodec_StrictErrors(*exceptionObject); |
| 3008 | } |
| 3009 | |
| 3010 | /* error handling callback helper: |
| 3011 | build arguments, call the callback and check the arguments, |
| 3012 | put the result into newpos and return the replacement string, which |
| 3013 | has to be freed by the caller */ |
| 3014 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 3015 | PyObject **errorHandler, |
| 3016 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3017 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 3018 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3019 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3020 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3021 | 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] | 3022 | |
| 3023 | PyObject *restuple; |
| 3024 | PyObject *resunicode; |
| 3025 | |
| 3026 | if (*errorHandler == NULL) { |
| 3027 | *errorHandler = PyCodec_LookupError(errors); |
| 3028 | if (*errorHandler == NULL) |
| 3029 | return NULL; |
| 3030 | } |
| 3031 | |
| 3032 | make_encode_exception(exceptionObject, |
| 3033 | encoding, unicode, size, startpos, endpos, reason); |
| 3034 | if (*exceptionObject == NULL) |
| 3035 | return NULL; |
| 3036 | |
| 3037 | restuple = PyObject_CallFunctionObjArgs( |
| 3038 | *errorHandler, *exceptionObject, NULL); |
| 3039 | if (restuple == NULL) |
| 3040 | return NULL; |
| 3041 | if (!PyTuple_Check(restuple)) { |
| 3042 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 3043 | Py_DECREF(restuple); |
| 3044 | return NULL; |
| 3045 | } |
| 3046 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 3047 | &resunicode, newpos)) { |
| 3048 | Py_DECREF(restuple); |
| 3049 | return NULL; |
| 3050 | } |
| 3051 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3052 | *newpos = size+*newpos; |
| 3053 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 3054 | 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] | 3055 | Py_DECREF(restuple); |
| 3056 | return NULL; |
| 3057 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3058 | Py_INCREF(resunicode); |
| 3059 | Py_DECREF(restuple); |
| 3060 | return resunicode; |
| 3061 | } |
| 3062 | |
| 3063 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3064 | Py_ssize_t size, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3065 | const char *errors, |
| 3066 | int limit) |
| 3067 | { |
| 3068 | /* output object */ |
| 3069 | PyObject *res; |
| 3070 | /* pointers to the beginning and end+1 of input */ |
| 3071 | const Py_UNICODE *startp = p; |
| 3072 | const Py_UNICODE *endp = p + size; |
| 3073 | /* pointer to the beginning of the unencodable characters */ |
| 3074 | /* const Py_UNICODE *badp = NULL; */ |
| 3075 | /* pointer into the output */ |
| 3076 | char *str; |
| 3077 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3078 | Py_ssize_t respos = 0; |
| 3079 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3080 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 3081 | 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] | 3082 | PyObject *errorHandler = NULL; |
| 3083 | PyObject *exc = NULL; |
| 3084 | /* the following variable is used for caching string comparisons |
| 3085 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 3086 | int known_errorHandler = -1; |
| 3087 | |
| 3088 | /* allocate enough for a simple encoding without |
| 3089 | replacements, if we need more, we'll resize */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3090 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3091 | if (res == NULL) |
| 3092 | goto onError; |
| 3093 | if (size == 0) |
| 3094 | return res; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3095 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3096 | ressize = size; |
| 3097 | |
| 3098 | while (p<endp) { |
| 3099 | Py_UNICODE c = *p; |
| 3100 | |
| 3101 | /* can we encode this? */ |
| 3102 | if (c<limit) { |
| 3103 | /* no overflow check, because we know that the space is enough */ |
| 3104 | *str++ = (char)c; |
| 3105 | ++p; |
| 3106 | } |
| 3107 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3108 | Py_ssize_t unicodepos = p-startp; |
| 3109 | Py_ssize_t requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3110 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3111 | Py_ssize_t repsize; |
| 3112 | Py_ssize_t newpos; |
| 3113 | Py_ssize_t respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3114 | Py_UNICODE *uni2; |
| 3115 | /* startpos for collecting unencodable chars */ |
| 3116 | const Py_UNICODE *collstart = p; |
| 3117 | const Py_UNICODE *collend = p; |
| 3118 | /* find all unecodable characters */ |
| 3119 | while ((collend < endp) && ((*collend)>=limit)) |
| 3120 | ++collend; |
| 3121 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 3122 | if (known_errorHandler==-1) { |
| 3123 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3124 | known_errorHandler = 1; |
| 3125 | else if (!strcmp(errors, "replace")) |
| 3126 | known_errorHandler = 2; |
| 3127 | else if (!strcmp(errors, "ignore")) |
| 3128 | known_errorHandler = 3; |
| 3129 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3130 | known_errorHandler = 4; |
| 3131 | else |
| 3132 | known_errorHandler = 0; |
| 3133 | } |
| 3134 | switch (known_errorHandler) { |
| 3135 | case 1: /* strict */ |
| 3136 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 3137 | goto onError; |
| 3138 | case 2: /* replace */ |
| 3139 | while (collstart++<collend) |
| 3140 | *str++ = '?'; /* fall through */ |
| 3141 | case 3: /* ignore */ |
| 3142 | p = collend; |
| 3143 | break; |
| 3144 | case 4: /* xmlcharrefreplace */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3145 | respos = str - PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3146 | /* determine replacement size (temporarily (mis)uses p) */ |
| 3147 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 3148 | if (*p<10) |
| 3149 | repsize += 2+1+1; |
| 3150 | else if (*p<100) |
| 3151 | repsize += 2+2+1; |
| 3152 | else if (*p<1000) |
| 3153 | repsize += 2+3+1; |
| 3154 | else if (*p<10000) |
| 3155 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 3156 | #ifndef Py_UNICODE_WIDE |
| 3157 | else |
| 3158 | repsize += 2+5+1; |
| 3159 | #else |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3160 | else if (*p<100000) |
| 3161 | repsize += 2+5+1; |
| 3162 | else if (*p<1000000) |
| 3163 | repsize += 2+6+1; |
| 3164 | else |
| 3165 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3166 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3167 | } |
| 3168 | requiredsize = respos+repsize+(endp-collend); |
| 3169 | if (requiredsize > ressize) { |
| 3170 | if (requiredsize<2*ressize) |
| 3171 | requiredsize = 2*ressize; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3172 | if (PyBytes_Resize(res, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3173 | goto onError; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3174 | str = PyBytes_AS_STRING(res) + respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3175 | ressize = requiredsize; |
| 3176 | } |
| 3177 | /* generate replacement (temporarily (mis)uses p) */ |
| 3178 | for (p = collstart; p < collend; ++p) { |
| 3179 | str += sprintf(str, "&#%d;", (int)*p); |
| 3180 | } |
| 3181 | p = collend; |
| 3182 | break; |
| 3183 | default: |
| 3184 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 3185 | encoding, reason, startp, size, &exc, |
| 3186 | collstart-startp, collend-startp, &newpos); |
| 3187 | if (repunicode == NULL) |
| 3188 | goto onError; |
| 3189 | /* need more space? (at least enough for what we |
| 3190 | have+the replacement+the rest of the string, so |
| 3191 | we won't have to check space for encodable characters) */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3192 | respos = str - PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3193 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3194 | requiredsize = respos+repsize+(endp-collend); |
| 3195 | if (requiredsize > ressize) { |
| 3196 | if (requiredsize<2*ressize) |
| 3197 | requiredsize = 2*ressize; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3198 | if (PyBytes_Resize(res, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3199 | Py_DECREF(repunicode); |
| 3200 | goto onError; |
| 3201 | } |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3202 | str = PyBytes_AS_STRING(res) + respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3203 | ressize = requiredsize; |
| 3204 | } |
| 3205 | /* check if there is anything unencodable in the replacement |
| 3206 | and copy it to the output */ |
| 3207 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 3208 | c = *uni2; |
| 3209 | if (c >= limit) { |
| 3210 | raise_encode_exception(&exc, encoding, startp, size, |
| 3211 | unicodepos, unicodepos+1, reason); |
| 3212 | Py_DECREF(repunicode); |
| 3213 | goto onError; |
| 3214 | } |
| 3215 | *str = (char)c; |
| 3216 | } |
| 3217 | p = startp + newpos; |
| 3218 | Py_DECREF(repunicode); |
| 3219 | } |
| 3220 | } |
| 3221 | } |
| 3222 | /* Resize if we allocated to much */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3223 | respos = str - PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3224 | if (respos<ressize) |
| 3225 | /* If this falls res will be NULL */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3226 | PyBytes_Resize(res, respos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3227 | Py_XDECREF(errorHandler); |
| 3228 | Py_XDECREF(exc); |
| 3229 | return res; |
| 3230 | |
| 3231 | onError: |
| 3232 | Py_XDECREF(res); |
| 3233 | Py_XDECREF(errorHandler); |
| 3234 | Py_XDECREF(exc); |
| 3235 | return NULL; |
| 3236 | } |
| 3237 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3238 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3239 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3240 | const char *errors) |
| 3241 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3242 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3243 | } |
| 3244 | |
| 3245 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 3246 | { |
| 3247 | if (!PyUnicode_Check(unicode)) { |
| 3248 | PyErr_BadArgument(); |
| 3249 | return NULL; |
| 3250 | } |
| 3251 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 3252 | PyUnicode_GET_SIZE(unicode), |
| 3253 | NULL); |
| 3254 | } |
| 3255 | |
| 3256 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 3257 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3258 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3259 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3260 | const char *errors) |
| 3261 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3262 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3263 | PyUnicodeObject *v; |
| 3264 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3265 | Py_ssize_t startinpos; |
| 3266 | Py_ssize_t endinpos; |
| 3267 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3268 | const char *e; |
| 3269 | PyObject *errorHandler = NULL; |
| 3270 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3271 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3272 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3273 | if (size == 1 && *(unsigned char*)s < 128) { |
| 3274 | Py_UNICODE r = *(unsigned char*)s; |
| 3275 | return PyUnicode_FromUnicode(&r, 1); |
| 3276 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3277 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3278 | v = _PyUnicode_New(size); |
| 3279 | if (v == NULL) |
| 3280 | goto onError; |
| 3281 | if (size == 0) |
| 3282 | return (PyObject *)v; |
| 3283 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3284 | e = s + size; |
| 3285 | while (s < e) { |
| 3286 | register unsigned char c = (unsigned char)*s; |
| 3287 | if (c < 128) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3288 | *p++ = c; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3289 | ++s; |
| 3290 | } |
| 3291 | else { |
| 3292 | startinpos = s-starts; |
| 3293 | endinpos = startinpos + 1; |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 3294 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3295 | if (unicode_decode_call_errorhandler( |
| 3296 | errors, &errorHandler, |
| 3297 | "ascii", "ordinal not in range(128)", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3298 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3299 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3300 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3301 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3302 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3303 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3304 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3305 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3306 | Py_XDECREF(errorHandler); |
| 3307 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3308 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3309 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3310 | onError: |
| 3311 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3312 | Py_XDECREF(errorHandler); |
| 3313 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3314 | return NULL; |
| 3315 | } |
| 3316 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3317 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3318 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3319 | const char *errors) |
| 3320 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3321 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3322 | } |
| 3323 | |
| 3324 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 3325 | { |
| 3326 | if (!PyUnicode_Check(unicode)) { |
| 3327 | PyErr_BadArgument(); |
| 3328 | return NULL; |
| 3329 | } |
| 3330 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 3331 | PyUnicode_GET_SIZE(unicode), |
| 3332 | NULL); |
| 3333 | } |
| 3334 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3335 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3336 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3337 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3338 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3339 | #if SIZEOF_INT < SIZEOF_SSIZE_T |
| 3340 | #define NEED_RETRY |
| 3341 | #endif |
| 3342 | |
| 3343 | /* XXX This code is limited to "true" double-byte encodings, as |
| 3344 | a) it assumes an incomplete character consists of a single byte, and |
| 3345 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
| 3346 | encodings, see IsDBCSLeadByteEx documentation. */ |
| 3347 | |
| 3348 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 3349 | { |
| 3350 | const char *curr = s + offset; |
| 3351 | |
| 3352 | if (IsDBCSLeadByte(*curr)) { |
| 3353 | const char *prev = CharPrev(s, curr); |
| 3354 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
| 3355 | } |
| 3356 | return 0; |
| 3357 | } |
| 3358 | |
| 3359 | /* |
| 3360 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 3361 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 3362 | */ |
| 3363 | static int decode_mbcs(PyUnicodeObject **v, |
| 3364 | const char *s, /* MBCS string */ |
| 3365 | int size, /* sizeof MBCS string */ |
| 3366 | int final) |
| 3367 | { |
| 3368 | Py_UNICODE *p; |
| 3369 | Py_ssize_t n = 0; |
| 3370 | int usize = 0; |
| 3371 | |
| 3372 | assert(size >= 0); |
| 3373 | |
| 3374 | /* Skip trailing lead-byte unless 'final' is set */ |
| 3375 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
| 3376 | --size; |
| 3377 | |
| 3378 | /* First get the size of the result */ |
| 3379 | if (size > 0) { |
| 3380 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 3381 | if (usize == 0) { |
| 3382 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3383 | return -1; |
| 3384 | } |
| 3385 | } |
| 3386 | |
| 3387 | if (*v == NULL) { |
| 3388 | /* Create unicode object */ |
| 3389 | *v = _PyUnicode_New(usize); |
| 3390 | if (*v == NULL) |
| 3391 | return -1; |
| 3392 | } |
| 3393 | else { |
| 3394 | /* Extend unicode object */ |
| 3395 | n = PyUnicode_GET_SIZE(*v); |
| 3396 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 3397 | return -1; |
| 3398 | } |
| 3399 | |
| 3400 | /* Do the conversion */ |
| 3401 | if (size > 0) { |
| 3402 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 3403 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 3404 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3405 | return -1; |
| 3406 | } |
| 3407 | } |
| 3408 | |
| 3409 | return size; |
| 3410 | } |
| 3411 | |
| 3412 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
| 3413 | Py_ssize_t size, |
| 3414 | const char *errors, |
| 3415 | Py_ssize_t *consumed) |
| 3416 | { |
| 3417 | PyUnicodeObject *v = NULL; |
| 3418 | int done; |
| 3419 | |
| 3420 | if (consumed) |
| 3421 | *consumed = 0; |
| 3422 | |
| 3423 | #ifdef NEED_RETRY |
| 3424 | retry: |
| 3425 | if (size > INT_MAX) |
| 3426 | done = decode_mbcs(&v, s, INT_MAX, 0); |
| 3427 | else |
| 3428 | #endif |
| 3429 | done = decode_mbcs(&v, s, (int)size, !consumed); |
| 3430 | |
| 3431 | if (done < 0) { |
| 3432 | Py_XDECREF(v); |
| 3433 | return NULL; |
| 3434 | } |
| 3435 | |
| 3436 | if (consumed) |
| 3437 | *consumed += done; |
| 3438 | |
| 3439 | #ifdef NEED_RETRY |
| 3440 | if (size > INT_MAX) { |
| 3441 | s += done; |
| 3442 | size -= done; |
| 3443 | goto retry; |
| 3444 | } |
| 3445 | #endif |
| 3446 | |
| 3447 | return (PyObject *)v; |
| 3448 | } |
| 3449 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3450 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3451 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3452 | const char *errors) |
| 3453 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3454 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 3455 | } |
| 3456 | |
| 3457 | /* |
| 3458 | * Convert unicode into string object (MBCS). |
| 3459 | * Returns 0 if succeed, -1 otherwise. |
| 3460 | */ |
| 3461 | static int encode_mbcs(PyObject **repr, |
| 3462 | const Py_UNICODE *p, /* unicode */ |
| 3463 | int size) /* size of unicode */ |
| 3464 | { |
| 3465 | int mbcssize = 0; |
| 3466 | Py_ssize_t n = 0; |
| 3467 | |
| 3468 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3469 | |
| 3470 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3471 | if (size > 0) { |
| 3472 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 3473 | if (mbcssize == 0) { |
| 3474 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3475 | return -1; |
| 3476 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3477 | } |
| 3478 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3479 | if (*repr == NULL) { |
| 3480 | /* Create string object */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3481 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3482 | if (*repr == NULL) |
| 3483 | return -1; |
| 3484 | } |
| 3485 | else { |
| 3486 | /* Extend string object */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3487 | n = PyBytes_Size(*repr); |
| 3488 | if (PyBytes_Resize(*repr, n + mbcssize) < 0) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3489 | return -1; |
| 3490 | } |
| 3491 | |
| 3492 | /* Do the conversion */ |
| 3493 | if (size > 0) { |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3494 | char *s = PyBytes_AS_STRING(*repr) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3495 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 3496 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3497 | return -1; |
| 3498 | } |
| 3499 | } |
| 3500 | |
| 3501 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3502 | } |
| 3503 | |
| 3504 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3505 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3506 | const char *errors) |
| 3507 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3508 | PyObject *repr = NULL; |
| 3509 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 3510 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3511 | #ifdef NEED_RETRY |
| 3512 | retry: |
| 3513 | if (size > INT_MAX) |
| 3514 | ret = encode_mbcs(&repr, p, INT_MAX); |
| 3515 | else |
| 3516 | #endif |
| 3517 | ret = encode_mbcs(&repr, p, (int)size); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3518 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3519 | if (ret < 0) { |
| 3520 | Py_XDECREF(repr); |
| 3521 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3522 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3523 | |
| 3524 | #ifdef NEED_RETRY |
| 3525 | if (size > INT_MAX) { |
| 3526 | p += INT_MAX; |
| 3527 | size -= INT_MAX; |
| 3528 | goto retry; |
| 3529 | } |
| 3530 | #endif |
| 3531 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3532 | return repr; |
| 3533 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3534 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3535 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 3536 | { |
| 3537 | if (!PyUnicode_Check(unicode)) { |
| 3538 | PyErr_BadArgument(); |
| 3539 | return NULL; |
| 3540 | } |
| 3541 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 3542 | PyUnicode_GET_SIZE(unicode), |
| 3543 | NULL); |
| 3544 | } |
| 3545 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3546 | #undef NEED_RETRY |
| 3547 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3548 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3549 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3550 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 3551 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3552 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3553 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3554 | PyObject *mapping, |
| 3555 | const char *errors) |
| 3556 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3557 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3558 | Py_ssize_t startinpos; |
| 3559 | Py_ssize_t endinpos; |
| 3560 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3561 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3562 | PyUnicodeObject *v; |
| 3563 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3564 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3565 | PyObject *errorHandler = NULL; |
| 3566 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3567 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3568 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3569 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3570 | /* Default to Latin-1 */ |
| 3571 | if (mapping == NULL) |
| 3572 | return PyUnicode_DecodeLatin1(s, size, errors); |
| 3573 | |
| 3574 | v = _PyUnicode_New(size); |
| 3575 | if (v == NULL) |
| 3576 | goto onError; |
| 3577 | if (size == 0) |
| 3578 | return (PyObject *)v; |
| 3579 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3580 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3581 | if (PyUnicode_CheckExact(mapping)) { |
| 3582 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 3583 | maplen = PyUnicode_GET_SIZE(mapping); |
| 3584 | while (s < e) { |
| 3585 | unsigned char ch = *s; |
| 3586 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3587 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3588 | if (ch < maplen) |
| 3589 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3590 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3591 | if (x == 0xfffe) { |
| 3592 | /* undefined mapping */ |
| 3593 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3594 | startinpos = s-starts; |
| 3595 | endinpos = startinpos+1; |
| 3596 | if (unicode_decode_call_errorhandler( |
| 3597 | errors, &errorHandler, |
| 3598 | "charmap", "character maps to <undefined>", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3599 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3600 | (PyObject **)&v, &outpos, &p)) { |
| 3601 | goto onError; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 3602 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3603 | continue; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 3604 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3605 | *p++ = x; |
| 3606 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3607 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3608 | } |
| 3609 | else { |
| 3610 | while (s < e) { |
| 3611 | unsigned char ch = *s; |
| 3612 | PyObject *w, *x; |
| 3613 | |
| 3614 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 3615 | w = PyInt_FromLong((long)ch); |
| 3616 | if (w == NULL) |
| 3617 | goto onError; |
| 3618 | x = PyObject_GetItem(mapping, w); |
| 3619 | Py_DECREF(w); |
| 3620 | if (x == NULL) { |
| 3621 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3622 | /* No mapping found means: mapping is undefined. */ |
| 3623 | PyErr_Clear(); |
| 3624 | x = Py_None; |
| 3625 | Py_INCREF(x); |
| 3626 | } else |
| 3627 | goto onError; |
| 3628 | } |
| 3629 | |
| 3630 | /* Apply mapping */ |
| 3631 | if (PyInt_Check(x)) { |
| 3632 | long value = PyInt_AS_LONG(x); |
| 3633 | if (value < 0 || value > 65535) { |
| 3634 | PyErr_SetString(PyExc_TypeError, |
| 3635 | "character mapping must be in range(65536)"); |
| 3636 | Py_DECREF(x); |
| 3637 | goto onError; |
| 3638 | } |
| 3639 | *p++ = (Py_UNICODE)value; |
| 3640 | } |
| 3641 | else if (x == Py_None) { |
| 3642 | /* undefined mapping */ |
| 3643 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3644 | startinpos = s-starts; |
| 3645 | endinpos = startinpos+1; |
| 3646 | if (unicode_decode_call_errorhandler( |
| 3647 | errors, &errorHandler, |
| 3648 | "charmap", "character maps to <undefined>", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3649 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3650 | (PyObject **)&v, &outpos, &p)) { |
| 3651 | Py_DECREF(x); |
| 3652 | goto onError; |
| 3653 | } |
Walter Dörwald | d4fff17 | 2005-11-28 22:15:56 +0000 | [diff] [blame] | 3654 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3655 | continue; |
| 3656 | } |
| 3657 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3658 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3659 | |
| 3660 | if (targetsize == 1) |
| 3661 | /* 1-1 mapping */ |
| 3662 | *p++ = *PyUnicode_AS_UNICODE(x); |
| 3663 | |
| 3664 | else if (targetsize > 1) { |
| 3665 | /* 1-n mapping */ |
| 3666 | if (targetsize > extrachars) { |
| 3667 | /* resize first */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3668 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 3669 | Py_ssize_t needed = (targetsize - extrachars) + \ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3670 | (targetsize << 2); |
| 3671 | extrachars += needed; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3672 | /* XXX overflow detection missing */ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3673 | if (_PyUnicode_Resize(&v, |
| 3674 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 3675 | Py_DECREF(x); |
| 3676 | goto onError; |
| 3677 | } |
| 3678 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 3679 | } |
| 3680 | Py_UNICODE_COPY(p, |
| 3681 | PyUnicode_AS_UNICODE(x), |
| 3682 | targetsize); |
| 3683 | p += targetsize; |
| 3684 | extrachars -= targetsize; |
| 3685 | } |
| 3686 | /* 1-0 mapping: skip the character */ |
| 3687 | } |
| 3688 | else { |
| 3689 | /* wrong return value */ |
| 3690 | PyErr_SetString(PyExc_TypeError, |
| 3691 | "character mapping must return integer, None or unicode"); |
| 3692 | Py_DECREF(x); |
| 3693 | goto onError; |
| 3694 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3695 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3696 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3697 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3698 | } |
| 3699 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3700 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3701 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3702 | Py_XDECREF(errorHandler); |
| 3703 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3704 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3705 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3706 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3707 | Py_XDECREF(errorHandler); |
| 3708 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3709 | Py_XDECREF(v); |
| 3710 | return NULL; |
| 3711 | } |
| 3712 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3713 | /* Charmap encoding: the lookup table */ |
| 3714 | |
| 3715 | struct encoding_map{ |
| 3716 | PyObject_HEAD |
| 3717 | unsigned char level1[32]; |
| 3718 | int count2, count3; |
| 3719 | unsigned char level23[1]; |
| 3720 | }; |
| 3721 | |
| 3722 | static PyObject* |
| 3723 | encoding_map_size(PyObject *obj, PyObject* args) |
| 3724 | { |
| 3725 | struct encoding_map *map = (struct encoding_map*)obj; |
| 3726 | return PyInt_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
| 3727 | 128*map->count3); |
| 3728 | } |
| 3729 | |
| 3730 | static PyMethodDef encoding_map_methods[] = { |
| 3731 | {"size", encoding_map_size, METH_NOARGS, |
| 3732 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 3733 | { 0 } |
| 3734 | }; |
| 3735 | |
| 3736 | static void |
| 3737 | encoding_map_dealloc(PyObject* o) |
| 3738 | { |
| 3739 | PyObject_FREE(o); |
| 3740 | } |
| 3741 | |
| 3742 | static PyTypeObject EncodingMapType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3743 | PyVarObject_HEAD_INIT(NULL, 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3744 | "EncodingMap", /*tp_name*/ |
| 3745 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 3746 | 0, /*tp_itemsize*/ |
| 3747 | /* methods */ |
| 3748 | encoding_map_dealloc, /*tp_dealloc*/ |
| 3749 | 0, /*tp_print*/ |
| 3750 | 0, /*tp_getattr*/ |
| 3751 | 0, /*tp_setattr*/ |
| 3752 | 0, /*tp_compare*/ |
| 3753 | 0, /*tp_repr*/ |
| 3754 | 0, /*tp_as_number*/ |
| 3755 | 0, /*tp_as_sequence*/ |
| 3756 | 0, /*tp_as_mapping*/ |
| 3757 | 0, /*tp_hash*/ |
| 3758 | 0, /*tp_call*/ |
| 3759 | 0, /*tp_str*/ |
| 3760 | 0, /*tp_getattro*/ |
| 3761 | 0, /*tp_setattro*/ |
| 3762 | 0, /*tp_as_buffer*/ |
| 3763 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 3764 | 0, /*tp_doc*/ |
| 3765 | 0, /*tp_traverse*/ |
| 3766 | 0, /*tp_clear*/ |
| 3767 | 0, /*tp_richcompare*/ |
| 3768 | 0, /*tp_weaklistoffset*/ |
| 3769 | 0, /*tp_iter*/ |
| 3770 | 0, /*tp_iternext*/ |
| 3771 | encoding_map_methods, /*tp_methods*/ |
| 3772 | 0, /*tp_members*/ |
| 3773 | 0, /*tp_getset*/ |
| 3774 | 0, /*tp_base*/ |
| 3775 | 0, /*tp_dict*/ |
| 3776 | 0, /*tp_descr_get*/ |
| 3777 | 0, /*tp_descr_set*/ |
| 3778 | 0, /*tp_dictoffset*/ |
| 3779 | 0, /*tp_init*/ |
| 3780 | 0, /*tp_alloc*/ |
| 3781 | 0, /*tp_new*/ |
| 3782 | 0, /*tp_free*/ |
| 3783 | 0, /*tp_is_gc*/ |
| 3784 | }; |
| 3785 | |
| 3786 | PyObject* |
| 3787 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 3788 | { |
| 3789 | Py_UNICODE *decode; |
| 3790 | PyObject *result; |
| 3791 | struct encoding_map *mresult; |
| 3792 | int i; |
| 3793 | int need_dict = 0; |
| 3794 | unsigned char level1[32]; |
| 3795 | unsigned char level2[512]; |
| 3796 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 3797 | int count2 = 0, count3 = 0; |
| 3798 | |
| 3799 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 3800 | PyErr_BadArgument(); |
| 3801 | return NULL; |
| 3802 | } |
| 3803 | decode = PyUnicode_AS_UNICODE(string); |
| 3804 | memset(level1, 0xFF, sizeof level1); |
| 3805 | memset(level2, 0xFF, sizeof level2); |
| 3806 | |
| 3807 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 3808 | or if there are non-BMP characters, we need to use |
| 3809 | a mapping dictionary. */ |
| 3810 | if (decode[0] != 0) |
| 3811 | need_dict = 1; |
| 3812 | for (i = 1; i < 256; i++) { |
| 3813 | int l1, l2; |
| 3814 | if (decode[i] == 0 |
| 3815 | #ifdef Py_UNICODE_WIDE |
| 3816 | || decode[i] > 0xFFFF |
| 3817 | #endif |
| 3818 | ) { |
| 3819 | need_dict = 1; |
| 3820 | break; |
| 3821 | } |
| 3822 | if (decode[i] == 0xFFFE) |
| 3823 | /* unmapped character */ |
| 3824 | continue; |
| 3825 | l1 = decode[i] >> 11; |
| 3826 | l2 = decode[i] >> 7; |
| 3827 | if (level1[l1] == 0xFF) |
| 3828 | level1[l1] = count2++; |
| 3829 | if (level2[l2] == 0xFF) |
| 3830 | level2[l2] = count3++; |
| 3831 | } |
| 3832 | |
| 3833 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 3834 | need_dict = 1; |
| 3835 | |
| 3836 | if (need_dict) { |
| 3837 | PyObject *result = PyDict_New(); |
| 3838 | PyObject *key, *value; |
| 3839 | if (!result) |
| 3840 | return NULL; |
| 3841 | for (i = 0; i < 256; i++) { |
| 3842 | key = value = NULL; |
| 3843 | key = PyInt_FromLong(decode[i]); |
| 3844 | value = PyInt_FromLong(i); |
| 3845 | if (!key || !value) |
| 3846 | goto failed1; |
| 3847 | if (PyDict_SetItem(result, key, value) == -1) |
| 3848 | goto failed1; |
| 3849 | Py_DECREF(key); |
| 3850 | Py_DECREF(value); |
| 3851 | } |
| 3852 | return result; |
| 3853 | failed1: |
| 3854 | Py_XDECREF(key); |
| 3855 | Py_XDECREF(value); |
| 3856 | Py_DECREF(result); |
| 3857 | return NULL; |
| 3858 | } |
| 3859 | |
| 3860 | /* Create a three-level trie */ |
| 3861 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 3862 | 16*count2 + 128*count3 - 1); |
| 3863 | if (!result) |
| 3864 | return PyErr_NoMemory(); |
| 3865 | PyObject_Init(result, &EncodingMapType); |
| 3866 | mresult = (struct encoding_map*)result; |
| 3867 | mresult->count2 = count2; |
| 3868 | mresult->count3 = count3; |
| 3869 | mlevel1 = mresult->level1; |
| 3870 | mlevel2 = mresult->level23; |
| 3871 | mlevel3 = mresult->level23 + 16*count2; |
| 3872 | memcpy(mlevel1, level1, 32); |
| 3873 | memset(mlevel2, 0xFF, 16*count2); |
| 3874 | memset(mlevel3, 0, 128*count3); |
| 3875 | count3 = 0; |
| 3876 | for (i = 1; i < 256; i++) { |
| 3877 | int o1, o2, o3, i2, i3; |
| 3878 | if (decode[i] == 0xFFFE) |
| 3879 | /* unmapped character */ |
| 3880 | continue; |
| 3881 | o1 = decode[i]>>11; |
| 3882 | o2 = (decode[i]>>7) & 0xF; |
| 3883 | i2 = 16*mlevel1[o1] + o2; |
| 3884 | if (mlevel2[i2] == 0xFF) |
| 3885 | mlevel2[i2] = count3++; |
| 3886 | o3 = decode[i] & 0x7F; |
| 3887 | i3 = 128*mlevel2[i2] + o3; |
| 3888 | mlevel3[i3] = i; |
| 3889 | } |
| 3890 | return result; |
| 3891 | } |
| 3892 | |
| 3893 | static int |
| 3894 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 3895 | { |
| 3896 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 3897 | int l1 = c>>11; |
| 3898 | int l2 = (c>>7) & 0xF; |
| 3899 | int l3 = c & 0x7F; |
| 3900 | int i; |
| 3901 | |
| 3902 | #ifdef Py_UNICODE_WIDE |
| 3903 | if (c > 0xFFFF) { |
| 3904 | return -1; |
| 3905 | } |
| 3906 | #endif |
| 3907 | if (c == 0) |
| 3908 | return 0; |
| 3909 | /* level 1*/ |
| 3910 | i = map->level1[l1]; |
| 3911 | if (i == 0xFF) { |
| 3912 | return -1; |
| 3913 | } |
| 3914 | /* level 2*/ |
| 3915 | i = map->level23[16*i+l2]; |
| 3916 | if (i == 0xFF) { |
| 3917 | return -1; |
| 3918 | } |
| 3919 | /* level 3 */ |
| 3920 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 3921 | if (i == 0) { |
| 3922 | return -1; |
| 3923 | } |
| 3924 | return i; |
| 3925 | } |
| 3926 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3927 | /* Lookup the character ch in the mapping. If the character |
| 3928 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3929 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3930 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3931 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3932 | PyObject *w = PyInt_FromLong((long)c); |
| 3933 | PyObject *x; |
| 3934 | |
| 3935 | if (w == NULL) |
| 3936 | return NULL; |
| 3937 | x = PyObject_GetItem(mapping, w); |
| 3938 | Py_DECREF(w); |
| 3939 | if (x == NULL) { |
| 3940 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3941 | /* No mapping found means: mapping is undefined. */ |
| 3942 | PyErr_Clear(); |
| 3943 | x = Py_None; |
| 3944 | Py_INCREF(x); |
| 3945 | return x; |
| 3946 | } else |
| 3947 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3948 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 3949 | else if (x == Py_None) |
| 3950 | return x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3951 | else if (PyInt_Check(x)) { |
| 3952 | long value = PyInt_AS_LONG(x); |
| 3953 | if (value < 0 || value > 255) { |
| 3954 | PyErr_SetString(PyExc_TypeError, |
| 3955 | "character mapping must be in range(256)"); |
| 3956 | Py_DECREF(x); |
| 3957 | return NULL; |
| 3958 | } |
| 3959 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3960 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3961 | else if (PyString_Check(x)) |
| 3962 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3963 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3964 | /* wrong return value */ |
Walter Dörwald | 580ceed | 2007-05-09 10:39:19 +0000 | [diff] [blame] | 3965 | PyErr_Format(PyExc_TypeError, |
| 3966 | "character mapping must return integer, None or str8, not %.400s", |
| 3967 | x->ob_type->tp_name); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3968 | Py_DECREF(x); |
| 3969 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3970 | } |
| 3971 | } |
| 3972 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3973 | static int |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3974 | charmapencode_resize(PyObject *outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3975 | { |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3976 | Py_ssize_t outsize = PyBytes_GET_SIZE( outobj); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3977 | /* exponentially overallocate to minimize reallocations */ |
| 3978 | if (requiredsize < 2*outsize) |
| 3979 | requiredsize = 2*outsize; |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3980 | if (PyBytes_Resize(outobj, requiredsize)) { |
| 3981 | Py_DECREF(outobj); |
| 3982 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3983 | } |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3984 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3985 | } |
| 3986 | |
| 3987 | typedef enum charmapencode_result { |
| 3988 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
| 3989 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3990 | /* 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] | 3991 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3992 | space is available. Return a new reference to the object that |
| 3993 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 3994 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 3995 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3996 | static |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3997 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3998 | PyObject *outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3999 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4000 | PyObject *rep; |
| 4001 | char *outstart; |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4002 | Py_ssize_t outsize = PyBytes_GET_SIZE(outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4003 | |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 4004 | if (Py_Type(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4005 | int res = encoding_map_lookup(c, mapping); |
| 4006 | Py_ssize_t requiredsize = *outpos+1; |
| 4007 | if (res == -1) |
| 4008 | return enc_FAILED; |
| 4009 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4010 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4011 | return enc_EXCEPTION; |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4012 | outstart = PyBytes_AS_STRING(outobj); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4013 | outstart[(*outpos)++] = (char)res; |
| 4014 | return enc_SUCCESS; |
| 4015 | } |
| 4016 | |
| 4017 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4018 | if (rep==NULL) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4019 | return enc_EXCEPTION; |
| 4020 | else if (rep==Py_None) { |
| 4021 | Py_DECREF(rep); |
| 4022 | return enc_FAILED; |
| 4023 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4024 | if (PyInt_Check(rep)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4025 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4026 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4027 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4028 | Py_DECREF(rep); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4029 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4030 | } |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4031 | outstart = PyBytes_AS_STRING(outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4032 | outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); |
| 4033 | } |
| 4034 | else { |
| 4035 | const char *repchars = PyString_AS_STRING(rep); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4036 | Py_ssize_t repsize = PyString_GET_SIZE(rep); |
| 4037 | Py_ssize_t requiredsize = *outpos+repsize; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4038 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4039 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4040 | Py_DECREF(rep); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4041 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4042 | } |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4043 | outstart = PyBytes_AS_STRING(outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4044 | memcpy(outstart + *outpos, repchars, repsize); |
| 4045 | *outpos += repsize; |
| 4046 | } |
| 4047 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4048 | Py_DECREF(rep); |
| 4049 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4050 | } |
| 4051 | |
| 4052 | /* handle an error in PyUnicode_EncodeCharmap |
| 4053 | Return 0 on success, -1 on error */ |
| 4054 | static |
| 4055 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4056 | 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] | 4057 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4058 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4059 | PyObject *res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4060 | { |
| 4061 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4062 | Py_ssize_t repsize; |
| 4063 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4064 | Py_UNICODE *uni2; |
| 4065 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4066 | Py_ssize_t collstartpos = *inpos; |
| 4067 | Py_ssize_t collendpos = *inpos+1; |
| 4068 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4069 | char *encoding = "charmap"; |
| 4070 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4071 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4072 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4073 | /* find all unencodable characters */ |
| 4074 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4075 | PyObject *rep; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 4076 | if (Py_Type(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4077 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 4078 | if (res != -1) |
| 4079 | break; |
| 4080 | ++collendpos; |
| 4081 | continue; |
| 4082 | } |
| 4083 | |
| 4084 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 4085 | if (rep==NULL) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4086 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4087 | else if (rep!=Py_None) { |
| 4088 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4089 | break; |
| 4090 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4091 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4092 | ++collendpos; |
| 4093 | } |
| 4094 | /* cache callback name lookup |
| 4095 | * (if not done yet, i.e. it's the first error) */ |
| 4096 | if (*known_errorHandler==-1) { |
| 4097 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4098 | *known_errorHandler = 1; |
| 4099 | else if (!strcmp(errors, "replace")) |
| 4100 | *known_errorHandler = 2; |
| 4101 | else if (!strcmp(errors, "ignore")) |
| 4102 | *known_errorHandler = 3; |
| 4103 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4104 | *known_errorHandler = 4; |
| 4105 | else |
| 4106 | *known_errorHandler = 0; |
| 4107 | } |
| 4108 | switch (*known_errorHandler) { |
| 4109 | case 1: /* strict */ |
| 4110 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4111 | return -1; |
| 4112 | case 2: /* replace */ |
| 4113 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 4114 | x = charmapencode_output('?', mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4115 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4116 | return -1; |
| 4117 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4118 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4119 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4120 | return -1; |
| 4121 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4122 | } |
| 4123 | /* fall through */ |
| 4124 | case 3: /* ignore */ |
| 4125 | *inpos = collendpos; |
| 4126 | break; |
| 4127 | case 4: /* xmlcharrefreplace */ |
| 4128 | /* generate replacement (temporarily (mis)uses p) */ |
| 4129 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 4130 | char buffer[2+29+1+1]; |
| 4131 | char *cp; |
| 4132 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 4133 | for (cp = buffer; *cp; ++cp) { |
| 4134 | x = charmapencode_output(*cp, mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4135 | if (x==enc_EXCEPTION) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4136 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4137 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4138 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4139 | return -1; |
| 4140 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4141 | } |
| 4142 | } |
| 4143 | *inpos = collendpos; |
| 4144 | break; |
| 4145 | default: |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4146 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4147 | encoding, reason, p, size, exceptionObject, |
| 4148 | collstartpos, collendpos, &newpos); |
| 4149 | if (repunicode == NULL) |
| 4150 | return -1; |
| 4151 | /* generate replacement */ |
| 4152 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4153 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 4154 | x = charmapencode_output(*uni2, mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4155 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4156 | return -1; |
| 4157 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4158 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4159 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4160 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4161 | return -1; |
| 4162 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4163 | } |
| 4164 | *inpos = newpos; |
| 4165 | Py_DECREF(repunicode); |
| 4166 | } |
| 4167 | return 0; |
| 4168 | } |
| 4169 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4170 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4171 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4172 | PyObject *mapping, |
| 4173 | const char *errors) |
| 4174 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4175 | /* output object */ |
| 4176 | PyObject *res = NULL; |
| 4177 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4178 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4179 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4180 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4181 | PyObject *errorHandler = NULL; |
| 4182 | PyObject *exc = NULL; |
| 4183 | /* the following variable is used for caching string comparisons |
| 4184 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4185 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4186 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4187 | |
| 4188 | /* Default to Latin-1 */ |
| 4189 | if (mapping == NULL) |
| 4190 | return PyUnicode_EncodeLatin1(p, size, errors); |
| 4191 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4192 | /* allocate enough for a simple encoding without |
| 4193 | replacements, if we need more, we'll resize */ |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4194 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4195 | if (res == NULL) |
| 4196 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 4197 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4198 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4199 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4200 | while (inpos<size) { |
| 4201 | /* try to encode it */ |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4202 | charmapencode_result x = charmapencode_output(p[inpos], mapping, res, &respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4203 | if (x==enc_EXCEPTION) /* error */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4204 | goto onError; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4205 | if (x==enc_FAILED) { /* unencodable character */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4206 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 4207 | &exc, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4208 | &known_errorHandler, &errorHandler, errors, |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4209 | res, &respos)) { |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 4210 | goto onError; |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 4211 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4212 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4213 | else |
| 4214 | /* done with this character => adjust input position */ |
| 4215 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4216 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4217 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4218 | /* Resize if we allocated to much */ |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4219 | if (respos<PyBytes_GET_SIZE(res)) { |
| 4220 | if (PyBytes_Resize(res, respos)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4221 | goto onError; |
| 4222 | } |
| 4223 | Py_XDECREF(exc); |
| 4224 | Py_XDECREF(errorHandler); |
| 4225 | return res; |
| 4226 | |
| 4227 | onError: |
| 4228 | Py_XDECREF(res); |
| 4229 | Py_XDECREF(exc); |
| 4230 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4231 | return NULL; |
| 4232 | } |
| 4233 | |
| 4234 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 4235 | PyObject *mapping) |
| 4236 | { |
| 4237 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 4238 | PyErr_BadArgument(); |
| 4239 | return NULL; |
| 4240 | } |
| 4241 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 4242 | PyUnicode_GET_SIZE(unicode), |
| 4243 | mapping, |
| 4244 | NULL); |
| 4245 | } |
| 4246 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4247 | /* create or adjust a UnicodeTranslateError */ |
| 4248 | static void make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4249 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4250 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4251 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4252 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4253 | if (*exceptionObject == NULL) { |
| 4254 | *exceptionObject = PyUnicodeTranslateError_Create( |
| 4255 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4256 | } |
| 4257 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4258 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 4259 | goto onError; |
| 4260 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 4261 | goto onError; |
| 4262 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 4263 | goto onError; |
| 4264 | return; |
| 4265 | onError: |
| 4266 | Py_DECREF(*exceptionObject); |
| 4267 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4268 | } |
| 4269 | } |
| 4270 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4271 | /* raises a UnicodeTranslateError */ |
| 4272 | static void raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4273 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4274 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4275 | const char *reason) |
| 4276 | { |
| 4277 | make_translate_exception(exceptionObject, |
| 4278 | unicode, size, startpos, endpos, reason); |
| 4279 | if (*exceptionObject != NULL) |
| 4280 | PyCodec_StrictErrors(*exceptionObject); |
| 4281 | } |
| 4282 | |
| 4283 | /* error handling callback helper: |
| 4284 | build arguments, call the callback and check the arguments, |
| 4285 | put the result into newpos and return the replacement string, which |
| 4286 | has to be freed by the caller */ |
| 4287 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 4288 | PyObject **errorHandler, |
| 4289 | const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4290 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4291 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4292 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4293 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4294 | 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] | 4295 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4296 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4297 | PyObject *restuple; |
| 4298 | PyObject *resunicode; |
| 4299 | |
| 4300 | if (*errorHandler == NULL) { |
| 4301 | *errorHandler = PyCodec_LookupError(errors); |
| 4302 | if (*errorHandler == NULL) |
| 4303 | return NULL; |
| 4304 | } |
| 4305 | |
| 4306 | make_translate_exception(exceptionObject, |
| 4307 | unicode, size, startpos, endpos, reason); |
| 4308 | if (*exceptionObject == NULL) |
| 4309 | return NULL; |
| 4310 | |
| 4311 | restuple = PyObject_CallFunctionObjArgs( |
| 4312 | *errorHandler, *exceptionObject, NULL); |
| 4313 | if (restuple == NULL) |
| 4314 | return NULL; |
| 4315 | if (!PyTuple_Check(restuple)) { |
| 4316 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 4317 | Py_DECREF(restuple); |
| 4318 | return NULL; |
| 4319 | } |
| 4320 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4321 | &resunicode, &i_newpos)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4322 | Py_DECREF(restuple); |
| 4323 | return NULL; |
| 4324 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4325 | if (i_newpos<0) |
| 4326 | *newpos = size+i_newpos; |
| 4327 | else |
| 4328 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4329 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 4330 | 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] | 4331 | Py_DECREF(restuple); |
| 4332 | return NULL; |
| 4333 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4334 | Py_INCREF(resunicode); |
| 4335 | Py_DECREF(restuple); |
| 4336 | return resunicode; |
| 4337 | } |
| 4338 | |
| 4339 | /* Lookup the character ch in the mapping and put the result in result, |
| 4340 | which must be decrefed by the caller. |
| 4341 | Return 0 on success, -1 on error */ |
| 4342 | static |
| 4343 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 4344 | { |
| 4345 | PyObject *w = PyInt_FromLong((long)c); |
| 4346 | PyObject *x; |
| 4347 | |
| 4348 | if (w == NULL) |
| 4349 | return -1; |
| 4350 | x = PyObject_GetItem(mapping, w); |
| 4351 | Py_DECREF(w); |
| 4352 | if (x == NULL) { |
| 4353 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4354 | /* No mapping found means: use 1:1 mapping. */ |
| 4355 | PyErr_Clear(); |
| 4356 | *result = NULL; |
| 4357 | return 0; |
| 4358 | } else |
| 4359 | return -1; |
| 4360 | } |
| 4361 | else if (x == Py_None) { |
| 4362 | *result = x; |
| 4363 | return 0; |
| 4364 | } |
| 4365 | else if (PyInt_Check(x)) { |
| 4366 | long value = PyInt_AS_LONG(x); |
| 4367 | long max = PyUnicode_GetMax(); |
| 4368 | if (value < 0 || value > max) { |
| 4369 | PyErr_Format(PyExc_TypeError, |
| 4370 | "character mapping must be in range(0x%lx)", max+1); |
| 4371 | Py_DECREF(x); |
| 4372 | return -1; |
| 4373 | } |
| 4374 | *result = x; |
| 4375 | return 0; |
| 4376 | } |
| 4377 | else if (PyUnicode_Check(x)) { |
| 4378 | *result = x; |
| 4379 | return 0; |
| 4380 | } |
| 4381 | else { |
| 4382 | /* wrong return value */ |
| 4383 | PyErr_SetString(PyExc_TypeError, |
| 4384 | "character mapping must return integer, None or unicode"); |
Walter Dörwald | 150523e | 2003-08-15 16:52:19 +0000 | [diff] [blame] | 4385 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4386 | return -1; |
| 4387 | } |
| 4388 | } |
| 4389 | /* ensure that *outobj is at least requiredsize characters long, |
| 4390 | if not reallocate and adjust various state variables. |
| 4391 | Return 0 on success, -1 on error */ |
| 4392 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4393 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4394 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4395 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4396 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4397 | if (requiredsize > oldsize) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4398 | /* remember old output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4399 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4400 | /* exponentially overallocate to minimize reallocations */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4401 | if (requiredsize < 2 * oldsize) |
| 4402 | requiredsize = 2 * oldsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4403 | if (_PyUnicode_Resize(outobj, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4404 | return -1; |
| 4405 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4406 | } |
| 4407 | return 0; |
| 4408 | } |
| 4409 | /* lookup the character, put the result in the output string and adjust |
| 4410 | various state variables. Return a new reference to the object that |
| 4411 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 4412 | undefined (in which case no character was written). |
| 4413 | The called must decref result. |
| 4414 | Return 0 on success, -1 on error. */ |
| 4415 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4416 | 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] | 4417 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4418 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4419 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4420 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4421 | return -1; |
| 4422 | if (*res==NULL) { |
| 4423 | /* not found => default to 1:1 mapping */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4424 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4425 | } |
| 4426 | else if (*res==Py_None) |
| 4427 | ; |
| 4428 | else if (PyInt_Check(*res)) { |
| 4429 | /* no overflow check, because we know that the space is enough */ |
| 4430 | *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); |
| 4431 | } |
| 4432 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4433 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4434 | if (repsize==1) { |
| 4435 | /* no overflow check, because we know that the space is enough */ |
| 4436 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 4437 | } |
| 4438 | else if (repsize!=0) { |
| 4439 | /* more than one character */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4440 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
Walter Dörwald | cd736e7 | 2004-02-05 17:36:00 +0000 | [diff] [blame] | 4441 | (insize - (curinp-startinp)) + |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4442 | repsize - 1; |
| 4443 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4444 | return -1; |
| 4445 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 4446 | *outp += repsize; |
| 4447 | } |
| 4448 | } |
| 4449 | else |
| 4450 | return -1; |
| 4451 | return 0; |
| 4452 | } |
| 4453 | |
| 4454 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4455 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4456 | PyObject *mapping, |
| 4457 | const char *errors) |
| 4458 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4459 | /* output object */ |
| 4460 | PyObject *res = NULL; |
| 4461 | /* pointers to the beginning and end+1 of input */ |
| 4462 | const Py_UNICODE *startp = p; |
| 4463 | const Py_UNICODE *endp = p + size; |
| 4464 | /* pointer into the output */ |
| 4465 | Py_UNICODE *str; |
| 4466 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4467 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4468 | char *reason = "character maps to <undefined>"; |
| 4469 | PyObject *errorHandler = NULL; |
| 4470 | PyObject *exc = NULL; |
| 4471 | /* the following variable is used for caching string comparisons |
| 4472 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4473 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4474 | int known_errorHandler = -1; |
| 4475 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4476 | if (mapping == NULL) { |
| 4477 | PyErr_BadArgument(); |
| 4478 | return NULL; |
| 4479 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4480 | |
| 4481 | /* allocate enough for a simple 1:1 translation without |
| 4482 | replacements, if we need more, we'll resize */ |
| 4483 | res = PyUnicode_FromUnicode(NULL, size); |
| 4484 | if (res == NULL) |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4485 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4486 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4487 | return res; |
| 4488 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4489 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4490 | while (p<endp) { |
| 4491 | /* try to encode it */ |
| 4492 | PyObject *x = NULL; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4493 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4494 | Py_XDECREF(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4495 | goto onError; |
| 4496 | } |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 4497 | Py_XDECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4498 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 4499 | ++p; |
| 4500 | else { /* untranslatable character */ |
| 4501 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4502 | Py_ssize_t repsize; |
| 4503 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4504 | Py_UNICODE *uni2; |
| 4505 | /* startpos for collecting untranslatable chars */ |
| 4506 | const Py_UNICODE *collstart = p; |
| 4507 | const Py_UNICODE *collend = p+1; |
| 4508 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4509 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4510 | /* find all untranslatable characters */ |
| 4511 | while (collend < endp) { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4512 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4513 | goto onError; |
| 4514 | Py_XDECREF(x); |
| 4515 | if (x!=Py_None) |
| 4516 | break; |
| 4517 | ++collend; |
| 4518 | } |
| 4519 | /* cache callback name lookup |
| 4520 | * (if not done yet, i.e. it's the first error) */ |
| 4521 | if (known_errorHandler==-1) { |
| 4522 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4523 | known_errorHandler = 1; |
| 4524 | else if (!strcmp(errors, "replace")) |
| 4525 | known_errorHandler = 2; |
| 4526 | else if (!strcmp(errors, "ignore")) |
| 4527 | known_errorHandler = 3; |
| 4528 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4529 | known_errorHandler = 4; |
| 4530 | else |
| 4531 | known_errorHandler = 0; |
| 4532 | } |
| 4533 | switch (known_errorHandler) { |
| 4534 | case 1: /* strict */ |
| 4535 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
| 4536 | goto onError; |
| 4537 | case 2: /* replace */ |
| 4538 | /* No need to check for space, this is a 1:1 replacement */ |
| 4539 | for (coll = collstart; coll<collend; ++coll) |
| 4540 | *str++ = '?'; |
| 4541 | /* fall through */ |
| 4542 | case 3: /* ignore */ |
| 4543 | p = collend; |
| 4544 | break; |
| 4545 | case 4: /* xmlcharrefreplace */ |
| 4546 | /* generate replacement (temporarily (mis)uses p) */ |
| 4547 | for (p = collstart; p < collend; ++p) { |
| 4548 | char buffer[2+29+1+1]; |
| 4549 | char *cp; |
| 4550 | sprintf(buffer, "&#%d;", (int)*p); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4551 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4552 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 4553 | goto onError; |
| 4554 | for (cp = buffer; *cp; ++cp) |
| 4555 | *str++ = *cp; |
| 4556 | } |
| 4557 | p = collend; |
| 4558 | break; |
| 4559 | default: |
| 4560 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 4561 | reason, startp, size, &exc, |
| 4562 | collstart-startp, collend-startp, &newpos); |
| 4563 | if (repunicode == NULL) |
| 4564 | goto onError; |
| 4565 | /* generate replacement */ |
| 4566 | repsize = PyUnicode_GET_SIZE(repunicode); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4567 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4568 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 4569 | Py_DECREF(repunicode); |
| 4570 | goto onError; |
| 4571 | } |
| 4572 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 4573 | *str++ = *uni2; |
| 4574 | p = startp + newpos; |
| 4575 | Py_DECREF(repunicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4576 | } |
| 4577 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4578 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4579 | /* Resize if we allocated to much */ |
| 4580 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4581 | if (respos<PyUnicode_GET_SIZE(res)) { |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4582 | if (_PyUnicode_Resize(&res, respos) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 4583 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4584 | } |
| 4585 | Py_XDECREF(exc); |
| 4586 | Py_XDECREF(errorHandler); |
| 4587 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4588 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4589 | onError: |
| 4590 | Py_XDECREF(res); |
| 4591 | Py_XDECREF(exc); |
| 4592 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4593 | return NULL; |
| 4594 | } |
| 4595 | |
| 4596 | PyObject *PyUnicode_Translate(PyObject *str, |
| 4597 | PyObject *mapping, |
| 4598 | const char *errors) |
| 4599 | { |
| 4600 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4601 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4602 | str = PyUnicode_FromObject(str); |
| 4603 | if (str == NULL) |
| 4604 | goto onError; |
| 4605 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 4606 | PyUnicode_GET_SIZE(str), |
| 4607 | mapping, |
| 4608 | errors); |
| 4609 | Py_DECREF(str); |
| 4610 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4611 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4612 | onError: |
| 4613 | Py_XDECREF(str); |
| 4614 | return NULL; |
| 4615 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4616 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4617 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 4618 | |
| 4619 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4620 | Py_ssize_t length, |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4621 | char *output, |
| 4622 | const char *errors) |
| 4623 | { |
| 4624 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4625 | PyObject *errorHandler = NULL; |
| 4626 | PyObject *exc = NULL; |
| 4627 | const char *encoding = "decimal"; |
| 4628 | const char *reason = "invalid decimal Unicode string"; |
| 4629 | /* the following variable is used for caching string comparisons |
| 4630 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 4631 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4632 | |
| 4633 | if (output == NULL) { |
| 4634 | PyErr_BadArgument(); |
| 4635 | return -1; |
| 4636 | } |
| 4637 | |
| 4638 | p = s; |
| 4639 | end = s + length; |
| 4640 | while (p < end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4641 | register Py_UNICODE ch = *p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4642 | int decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4643 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4644 | Py_ssize_t repsize; |
| 4645 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4646 | Py_UNICODE *uni2; |
| 4647 | Py_UNICODE *collstart; |
| 4648 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4649 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4650 | if (Py_UNICODE_ISSPACE(ch)) { |
| 4651 | *output++ = ' '; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4652 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4653 | continue; |
| 4654 | } |
| 4655 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 4656 | if (decimal >= 0) { |
| 4657 | *output++ = '0' + decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4658 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4659 | continue; |
| 4660 | } |
Guido van Rossum | ba47704 | 2000-04-06 18:18:10 +0000 | [diff] [blame] | 4661 | if (0 < ch && ch < 256) { |
Guido van Rossum | 42c29aa | 2000-05-03 23:58:29 +0000 | [diff] [blame] | 4662 | *output++ = (char)ch; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4663 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4664 | continue; |
| 4665 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4666 | /* All other characters are considered unencodable */ |
| 4667 | collstart = p; |
| 4668 | collend = p+1; |
| 4669 | while (collend < end) { |
| 4670 | if ((0 < *collend && *collend < 256) || |
| 4671 | !Py_UNICODE_ISSPACE(*collend) || |
| 4672 | Py_UNICODE_TODECIMAL(*collend)) |
| 4673 | break; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4674 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4675 | /* cache callback name lookup |
| 4676 | * (if not done yet, i.e. it's the first error) */ |
| 4677 | if (known_errorHandler==-1) { |
| 4678 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4679 | known_errorHandler = 1; |
| 4680 | else if (!strcmp(errors, "replace")) |
| 4681 | known_errorHandler = 2; |
| 4682 | else if (!strcmp(errors, "ignore")) |
| 4683 | known_errorHandler = 3; |
| 4684 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4685 | known_errorHandler = 4; |
| 4686 | else |
| 4687 | known_errorHandler = 0; |
| 4688 | } |
| 4689 | switch (known_errorHandler) { |
| 4690 | case 1: /* strict */ |
| 4691 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 4692 | goto onError; |
| 4693 | case 2: /* replace */ |
| 4694 | for (p = collstart; p < collend; ++p) |
| 4695 | *output++ = '?'; |
| 4696 | /* fall through */ |
| 4697 | case 3: /* ignore */ |
| 4698 | p = collend; |
| 4699 | break; |
| 4700 | case 4: /* xmlcharrefreplace */ |
| 4701 | /* generate replacement (temporarily (mis)uses p) */ |
| 4702 | for (p = collstart; p < collend; ++p) |
| 4703 | output += sprintf(output, "&#%d;", (int)*p); |
| 4704 | p = collend; |
| 4705 | break; |
| 4706 | default: |
| 4707 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 4708 | encoding, reason, s, length, &exc, |
| 4709 | collstart-s, collend-s, &newpos); |
| 4710 | if (repunicode == NULL) |
| 4711 | goto onError; |
| 4712 | /* generate replacement */ |
| 4713 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4714 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 4715 | Py_UNICODE ch = *uni2; |
| 4716 | if (Py_UNICODE_ISSPACE(ch)) |
| 4717 | *output++ = ' '; |
| 4718 | else { |
| 4719 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 4720 | if (decimal >= 0) |
| 4721 | *output++ = '0' + decimal; |
| 4722 | else if (0 < ch && ch < 256) |
| 4723 | *output++ = (char)ch; |
| 4724 | else { |
| 4725 | Py_DECREF(repunicode); |
| 4726 | raise_encode_exception(&exc, encoding, |
| 4727 | s, length, collstart-s, collend-s, reason); |
| 4728 | goto onError; |
| 4729 | } |
| 4730 | } |
| 4731 | } |
| 4732 | p = s + newpos; |
| 4733 | Py_DECREF(repunicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4734 | } |
| 4735 | } |
| 4736 | /* 0-terminate the output string */ |
| 4737 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4738 | Py_XDECREF(exc); |
| 4739 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4740 | return 0; |
| 4741 | |
| 4742 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4743 | Py_XDECREF(exc); |
| 4744 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4745 | return -1; |
| 4746 | } |
| 4747 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4748 | /* --- Helpers ------------------------------------------------------------ */ |
| 4749 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4750 | #define STRINGLIB_CHAR Py_UNICODE |
| 4751 | |
| 4752 | #define STRINGLIB_LEN PyUnicode_GET_SIZE |
| 4753 | #define STRINGLIB_NEW PyUnicode_FromUnicode |
| 4754 | #define STRINGLIB_STR PyUnicode_AS_UNICODE |
| 4755 | |
| 4756 | Py_LOCAL_INLINE(int) |
| 4757 | 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] | 4758 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4759 | if (str[0] != other[0]) |
| 4760 | return 1; |
| 4761 | return memcmp((void*) str, (void*) other, len * sizeof(Py_UNICODE)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4762 | } |
| 4763 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4764 | #define STRINGLIB_EMPTY unicode_empty |
| 4765 | |
| 4766 | #include "stringlib/fastsearch.h" |
| 4767 | |
| 4768 | #include "stringlib/count.h" |
| 4769 | #include "stringlib/find.h" |
| 4770 | #include "stringlib/partition.h" |
| 4771 | |
| 4772 | /* helper macro to fixup start/end slice values */ |
| 4773 | #define FIX_START_END(obj) \ |
| 4774 | if (start < 0) \ |
| 4775 | start += (obj)->length; \ |
| 4776 | if (start < 0) \ |
| 4777 | start = 0; \ |
| 4778 | if (end > (obj)->length) \ |
| 4779 | end = (obj)->length; \ |
| 4780 | if (end < 0) \ |
| 4781 | end += (obj)->length; \ |
| 4782 | if (end < 0) \ |
| 4783 | end = 0; |
| 4784 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4785 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4786 | PyObject *substr, |
| 4787 | Py_ssize_t start, |
| 4788 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4789 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4790 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4791 | PyUnicodeObject* str_obj; |
| 4792 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4793 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4794 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 4795 | if (!str_obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4796 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4797 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 4798 | if (!sub_obj) { |
| 4799 | Py_DECREF(str_obj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4800 | return -1; |
| 4801 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4802 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4803 | FIX_START_END(str_obj); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4804 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4805 | result = stringlib_count( |
| 4806 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
| 4807 | ); |
| 4808 | |
| 4809 | Py_DECREF(sub_obj); |
| 4810 | Py_DECREF(str_obj); |
| 4811 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4812 | return result; |
| 4813 | } |
| 4814 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4815 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4816 | PyObject *sub, |
| 4817 | Py_ssize_t start, |
| 4818 | Py_ssize_t end, |
| 4819 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4820 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4821 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4822 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4823 | str = PyUnicode_FromObject(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4824 | if (!str) |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 4825 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4826 | sub = PyUnicode_FromObject(sub); |
| 4827 | if (!sub) { |
Marc-André Lemburg | 4164439 | 2002-05-29 13:46:29 +0000 | [diff] [blame] | 4828 | Py_DECREF(str); |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 4829 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4830 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4831 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4832 | if (direction > 0) |
| 4833 | result = stringlib_find_slice( |
| 4834 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 4835 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 4836 | start, end |
| 4837 | ); |
| 4838 | else |
| 4839 | result = stringlib_rfind_slice( |
| 4840 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 4841 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 4842 | start, end |
| 4843 | ); |
| 4844 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4845 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4846 | Py_DECREF(sub); |
| 4847 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4848 | return result; |
| 4849 | } |
| 4850 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4851 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4852 | int tailmatch(PyUnicodeObject *self, |
| 4853 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4854 | Py_ssize_t start, |
| 4855 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4856 | int direction) |
| 4857 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4858 | if (substring->length == 0) |
| 4859 | return 1; |
| 4860 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4861 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4862 | |
| 4863 | end -= substring->length; |
| 4864 | if (end < start) |
| 4865 | return 0; |
| 4866 | |
| 4867 | if (direction > 0) { |
| 4868 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 4869 | return 1; |
| 4870 | } else { |
| 4871 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 4872 | return 1; |
| 4873 | } |
| 4874 | |
| 4875 | return 0; |
| 4876 | } |
| 4877 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4878 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4879 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4880 | Py_ssize_t start, |
| 4881 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4882 | int direction) |
| 4883 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4884 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4885 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4886 | str = PyUnicode_FromObject(str); |
| 4887 | if (str == NULL) |
| 4888 | return -1; |
| 4889 | substr = PyUnicode_FromObject(substr); |
| 4890 | if (substr == NULL) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 4891 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4892 | return -1; |
| 4893 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4894 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4895 | result = tailmatch((PyUnicodeObject *)str, |
| 4896 | (PyUnicodeObject *)substr, |
| 4897 | start, end, direction); |
| 4898 | Py_DECREF(str); |
| 4899 | Py_DECREF(substr); |
| 4900 | return result; |
| 4901 | } |
| 4902 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4903 | /* Apply fixfct filter to the Unicode object self and return a |
| 4904 | reference to the modified object */ |
| 4905 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4906 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4907 | PyObject *fixup(PyUnicodeObject *self, |
| 4908 | int (*fixfct)(PyUnicodeObject *s)) |
| 4909 | { |
| 4910 | |
| 4911 | PyUnicodeObject *u; |
| 4912 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4913 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4914 | if (u == NULL) |
| 4915 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4916 | |
| 4917 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 4918 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4919 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4920 | /* fixfct should return TRUE if it modified the buffer. If |
| 4921 | FALSE, return a reference to the original buffer instead |
| 4922 | (to save space, not time) */ |
| 4923 | Py_INCREF(self); |
| 4924 | Py_DECREF(u); |
| 4925 | return (PyObject*) self; |
| 4926 | } |
| 4927 | return (PyObject*) u; |
| 4928 | } |
| 4929 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4930 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4931 | int fixupper(PyUnicodeObject *self) |
| 4932 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4933 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4934 | Py_UNICODE *s = self->str; |
| 4935 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4936 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4937 | while (len-- > 0) { |
| 4938 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4939 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4940 | ch = Py_UNICODE_TOUPPER(*s); |
| 4941 | if (ch != *s) { |
| 4942 | status = 1; |
| 4943 | *s = ch; |
| 4944 | } |
| 4945 | s++; |
| 4946 | } |
| 4947 | |
| 4948 | return status; |
| 4949 | } |
| 4950 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4951 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4952 | int fixlower(PyUnicodeObject *self) |
| 4953 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4954 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4955 | Py_UNICODE *s = self->str; |
| 4956 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4957 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4958 | while (len-- > 0) { |
| 4959 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4960 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4961 | ch = Py_UNICODE_TOLOWER(*s); |
| 4962 | if (ch != *s) { |
| 4963 | status = 1; |
| 4964 | *s = ch; |
| 4965 | } |
| 4966 | s++; |
| 4967 | } |
| 4968 | |
| 4969 | return status; |
| 4970 | } |
| 4971 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4972 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4973 | int fixswapcase(PyUnicodeObject *self) |
| 4974 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4975 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4976 | Py_UNICODE *s = self->str; |
| 4977 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4978 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4979 | while (len-- > 0) { |
| 4980 | if (Py_UNICODE_ISUPPER(*s)) { |
| 4981 | *s = Py_UNICODE_TOLOWER(*s); |
| 4982 | status = 1; |
| 4983 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 4984 | *s = Py_UNICODE_TOUPPER(*s); |
| 4985 | status = 1; |
| 4986 | } |
| 4987 | s++; |
| 4988 | } |
| 4989 | |
| 4990 | return status; |
| 4991 | } |
| 4992 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4993 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4994 | int fixcapitalize(PyUnicodeObject *self) |
| 4995 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4996 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4997 | Py_UNICODE *s = self->str; |
| 4998 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4999 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5000 | if (len == 0) |
| 5001 | return 0; |
| 5002 | if (Py_UNICODE_ISLOWER(*s)) { |
| 5003 | *s = Py_UNICODE_TOUPPER(*s); |
| 5004 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5005 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5006 | s++; |
| 5007 | while (--len > 0) { |
| 5008 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5009 | *s = Py_UNICODE_TOLOWER(*s); |
| 5010 | status = 1; |
| 5011 | } |
| 5012 | s++; |
| 5013 | } |
| 5014 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5015 | } |
| 5016 | |
| 5017 | static |
| 5018 | int fixtitle(PyUnicodeObject *self) |
| 5019 | { |
| 5020 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5021 | register Py_UNICODE *e; |
| 5022 | int previous_is_cased; |
| 5023 | |
| 5024 | /* Shortcut for single character strings */ |
| 5025 | if (PyUnicode_GET_SIZE(self) == 1) { |
| 5026 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 5027 | if (*p != ch) { |
| 5028 | *p = ch; |
| 5029 | return 1; |
| 5030 | } |
| 5031 | else |
| 5032 | return 0; |
| 5033 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5034 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5035 | e = p + PyUnicode_GET_SIZE(self); |
| 5036 | previous_is_cased = 0; |
| 5037 | for (; p < e; p++) { |
| 5038 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5039 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5040 | if (previous_is_cased) |
| 5041 | *p = Py_UNICODE_TOLOWER(ch); |
| 5042 | else |
| 5043 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5044 | |
| 5045 | if (Py_UNICODE_ISLOWER(ch) || |
| 5046 | Py_UNICODE_ISUPPER(ch) || |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5047 | Py_UNICODE_ISTITLE(ch)) |
| 5048 | previous_is_cased = 1; |
| 5049 | else |
| 5050 | previous_is_cased = 0; |
| 5051 | } |
| 5052 | return 1; |
| 5053 | } |
| 5054 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5055 | PyObject * |
| 5056 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5057 | { |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5058 | PyObject *internal_separator = NULL; |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 5059 | const Py_UNICODE blank = ' '; |
| 5060 | const Py_UNICODE *sep = ␣ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5061 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5062 | PyUnicodeObject *res = NULL; /* the result */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5063 | Py_ssize_t res_alloc = 100; /* # allocated bytes for string in res */ |
| 5064 | Py_ssize_t res_used; /* # used bytes */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5065 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 5066 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5067 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5068 | PyObject *item; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5069 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5070 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5071 | fseq = PySequence_Fast(seq, ""); |
| 5072 | if (fseq == NULL) { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5073 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5074 | } |
| 5075 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5076 | /* Grrrr. A codec may be invoked to convert str objects to |
| 5077 | * Unicode, and so it's possible to call back into Python code |
| 5078 | * during PyUnicode_FromObject(), and so it's possible for a sick |
| 5079 | * codec to change the size of fseq (if seq is a list). Therefore |
| 5080 | * we have to keep refetching the size -- can't assume seqlen |
| 5081 | * is invariant. |
| 5082 | */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5083 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5084 | /* If empty sequence, return u"". */ |
| 5085 | if (seqlen == 0) { |
| 5086 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 5087 | goto Done; |
| 5088 | } |
| 5089 | /* If singleton sequence with an exact Unicode, return that. */ |
| 5090 | if (seqlen == 1) { |
| 5091 | item = PySequence_Fast_GET_ITEM(fseq, 0); |
| 5092 | if (PyUnicode_CheckExact(item)) { |
| 5093 | Py_INCREF(item); |
| 5094 | res = (PyUnicodeObject *)item; |
| 5095 | goto Done; |
| 5096 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5097 | } |
| 5098 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5099 | /* At least two items to join, or one that isn't exact Unicode. */ |
| 5100 | if (seqlen > 1) { |
| 5101 | /* Set up sep and seplen -- they're needed. */ |
| 5102 | if (separator == NULL) { |
| 5103 | sep = ␣ |
| 5104 | seplen = 1; |
| 5105 | } |
| 5106 | else { |
| 5107 | internal_separator = PyUnicode_FromObject(separator); |
| 5108 | if (internal_separator == NULL) |
| 5109 | goto onError; |
| 5110 | sep = PyUnicode_AS_UNICODE(internal_separator); |
| 5111 | seplen = PyUnicode_GET_SIZE(internal_separator); |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5112 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 5113 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5114 | } |
| 5115 | } |
| 5116 | |
| 5117 | /* Get space. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5118 | res = _PyUnicode_New(res_alloc); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5119 | if (res == NULL) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5120 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5121 | res_p = PyUnicode_AS_UNICODE(res); |
| 5122 | res_used = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5123 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5124 | for (i = 0; i < seqlen; ++i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5125 | Py_ssize_t itemlen; |
| 5126 | Py_ssize_t new_res_used; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5127 | |
| 5128 | item = PySequence_Fast_GET_ITEM(fseq, i); |
| 5129 | /* Convert item to Unicode. */ |
| 5130 | if (! PyUnicode_Check(item) && ! PyString_Check(item)) { |
| 5131 | PyErr_Format(PyExc_TypeError, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5132 | "sequence item %zd: expected string or Unicode," |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5133 | " %.80s found", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 5134 | i, Py_Type(item)->tp_name); |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 5135 | goto onError; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5136 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5137 | item = PyUnicode_FromObject(item); |
| 5138 | if (item == NULL) |
| 5139 | goto onError; |
| 5140 | /* We own a reference to item from here on. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5141 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5142 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 5143 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5144 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5145 | /* 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] | 5146 | itemlen = PyUnicode_GET_SIZE(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5147 | new_res_used = res_used + itemlen; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5148 | if (new_res_used < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5149 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5150 | if (i < seqlen - 1) { |
| 5151 | new_res_used += seplen; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5152 | if (new_res_used < 0) |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5153 | goto Overflow; |
| 5154 | } |
| 5155 | if (new_res_used > res_alloc) { |
| 5156 | /* double allocated size until it's big enough */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5157 | do { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5158 | res_alloc += res_alloc; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5159 | if (res_alloc <= 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5160 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5161 | } while (new_res_used > res_alloc); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5162 | if (_PyUnicode_Resize(&res, res_alloc) < 0) { |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 5163 | Py_DECREF(item); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5164 | goto onError; |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 5165 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5166 | res_p = PyUnicode_AS_UNICODE(res) + res_used; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5167 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5168 | |
| 5169 | /* Copy item, and maybe the separator. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5170 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5171 | res_p += itemlen; |
| 5172 | if (i < seqlen - 1) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5173 | Py_UNICODE_COPY(res_p, sep, seplen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5174 | res_p += seplen; |
| 5175 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5176 | Py_DECREF(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5177 | res_used = new_res_used; |
| 5178 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5179 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5180 | /* Shrink res to match the used area; this probably can't fail, |
| 5181 | * but it's cheap to check. |
| 5182 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5183 | if (_PyUnicode_Resize(&res, res_used) < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5184 | goto onError; |
| 5185 | |
| 5186 | Done: |
| 5187 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5188 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5189 | return (PyObject *)res; |
| 5190 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5191 | Overflow: |
| 5192 | PyErr_SetString(PyExc_OverflowError, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5193 | "join() result is too long for a Python string"); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5194 | Py_DECREF(item); |
| 5195 | /* fall through */ |
| 5196 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5197 | onError: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5198 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5199 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5200 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5201 | return NULL; |
| 5202 | } |
| 5203 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5204 | static |
| 5205 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5206 | Py_ssize_t left, |
| 5207 | Py_ssize_t right, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5208 | Py_UNICODE fill) |
| 5209 | { |
| 5210 | PyUnicodeObject *u; |
| 5211 | |
| 5212 | if (left < 0) |
| 5213 | left = 0; |
| 5214 | if (right < 0) |
| 5215 | right = 0; |
| 5216 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5217 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5218 | Py_INCREF(self); |
| 5219 | return self; |
| 5220 | } |
| 5221 | |
| 5222 | u = _PyUnicode_New(left + self->length + right); |
| 5223 | if (u) { |
| 5224 | if (left) |
| 5225 | Py_UNICODE_FILL(u->str, fill, left); |
| 5226 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 5227 | if (right) |
| 5228 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 5229 | } |
| 5230 | |
| 5231 | return u; |
| 5232 | } |
| 5233 | |
| 5234 | #define SPLIT_APPEND(data, left, right) \ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5235 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5236 | if (!str) \ |
| 5237 | goto onError; \ |
| 5238 | if (PyList_Append(list, str)) { \ |
| 5239 | Py_DECREF(str); \ |
| 5240 | goto onError; \ |
| 5241 | } \ |
| 5242 | else \ |
| 5243 | Py_DECREF(str); |
| 5244 | |
| 5245 | static |
| 5246 | PyObject *split_whitespace(PyUnicodeObject *self, |
| 5247 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5248 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5249 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5250 | register Py_ssize_t i; |
| 5251 | register Py_ssize_t j; |
| 5252 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5253 | PyObject *str; |
| 5254 | |
| 5255 | for (i = j = 0; i < len; ) { |
| 5256 | /* find a token */ |
| 5257 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 5258 | i++; |
| 5259 | j = i; |
| 5260 | while (i < len && !Py_UNICODE_ISSPACE(self->str[i])) |
| 5261 | i++; |
| 5262 | if (j < i) { |
| 5263 | if (maxcount-- <= 0) |
| 5264 | break; |
| 5265 | SPLIT_APPEND(self->str, j, i); |
| 5266 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 5267 | i++; |
| 5268 | j = i; |
| 5269 | } |
| 5270 | } |
| 5271 | if (j < len) { |
| 5272 | SPLIT_APPEND(self->str, j, len); |
| 5273 | } |
| 5274 | return list; |
| 5275 | |
| 5276 | onError: |
| 5277 | Py_DECREF(list); |
| 5278 | return NULL; |
| 5279 | } |
| 5280 | |
| 5281 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5282 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5283 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5284 | register Py_ssize_t i; |
| 5285 | register Py_ssize_t j; |
| 5286 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5287 | PyObject *list; |
| 5288 | PyObject *str; |
| 5289 | Py_UNICODE *data; |
| 5290 | |
| 5291 | string = PyUnicode_FromObject(string); |
| 5292 | if (string == NULL) |
| 5293 | return NULL; |
| 5294 | data = PyUnicode_AS_UNICODE(string); |
| 5295 | len = PyUnicode_GET_SIZE(string); |
| 5296 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5297 | list = PyList_New(0); |
| 5298 | if (!list) |
| 5299 | goto onError; |
| 5300 | |
| 5301 | for (i = j = 0; i < len; ) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5302 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5303 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5304 | /* Find a line and append it */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5305 | while (i < len && !BLOOM_LINEBREAK(data[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5306 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5307 | |
| 5308 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5309 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5310 | if (i < len) { |
| 5311 | if (data[i] == '\r' && i + 1 < len && |
| 5312 | data[i+1] == '\n') |
| 5313 | i += 2; |
| 5314 | else |
| 5315 | i++; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5316 | if (keepends) |
| 5317 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5318 | } |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5319 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5320 | j = i; |
| 5321 | } |
| 5322 | if (j < len) { |
| 5323 | SPLIT_APPEND(data, j, len); |
| 5324 | } |
| 5325 | |
| 5326 | Py_DECREF(string); |
| 5327 | return list; |
| 5328 | |
| 5329 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5330 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5331 | Py_DECREF(string); |
| 5332 | return NULL; |
| 5333 | } |
| 5334 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5335 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5336 | PyObject *split_char(PyUnicodeObject *self, |
| 5337 | PyObject *list, |
| 5338 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5339 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5340 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5341 | register Py_ssize_t i; |
| 5342 | register Py_ssize_t j; |
| 5343 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5344 | PyObject *str; |
| 5345 | |
| 5346 | for (i = j = 0; i < len; ) { |
| 5347 | if (self->str[i] == ch) { |
| 5348 | if (maxcount-- <= 0) |
| 5349 | break; |
| 5350 | SPLIT_APPEND(self->str, j, i); |
| 5351 | i = j = i + 1; |
| 5352 | } else |
| 5353 | i++; |
| 5354 | } |
| 5355 | if (j <= len) { |
| 5356 | SPLIT_APPEND(self->str, j, len); |
| 5357 | } |
| 5358 | return list; |
| 5359 | |
| 5360 | onError: |
| 5361 | Py_DECREF(list); |
| 5362 | return NULL; |
| 5363 | } |
| 5364 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5365 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5366 | PyObject *split_substring(PyUnicodeObject *self, |
| 5367 | PyObject *list, |
| 5368 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5369 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5370 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5371 | register Py_ssize_t i; |
| 5372 | register Py_ssize_t j; |
| 5373 | Py_ssize_t len = self->length; |
| 5374 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5375 | PyObject *str; |
| 5376 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 5377 | for (i = j = 0; i <= len - sublen; ) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5378 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5379 | if (maxcount-- <= 0) |
| 5380 | break; |
| 5381 | SPLIT_APPEND(self->str, j, i); |
| 5382 | i = j = i + sublen; |
| 5383 | } else |
| 5384 | i++; |
| 5385 | } |
| 5386 | if (j <= len) { |
| 5387 | SPLIT_APPEND(self->str, j, len); |
| 5388 | } |
| 5389 | return list; |
| 5390 | |
| 5391 | onError: |
| 5392 | Py_DECREF(list); |
| 5393 | return NULL; |
| 5394 | } |
| 5395 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5396 | static |
| 5397 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
| 5398 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5399 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5400 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5401 | register Py_ssize_t i; |
| 5402 | register Py_ssize_t j; |
| 5403 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5404 | PyObject *str; |
| 5405 | |
| 5406 | for (i = j = len - 1; i >= 0; ) { |
| 5407 | /* find a token */ |
| 5408 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 5409 | i--; |
| 5410 | j = i; |
| 5411 | while (i >= 0 && !Py_UNICODE_ISSPACE(self->str[i])) |
| 5412 | i--; |
| 5413 | if (j > i) { |
| 5414 | if (maxcount-- <= 0) |
| 5415 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5416 | SPLIT_APPEND(self->str, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5417 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 5418 | i--; |
| 5419 | j = i; |
| 5420 | } |
| 5421 | } |
| 5422 | if (j >= 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5423 | SPLIT_APPEND(self->str, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5424 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5425 | if (PyList_Reverse(list) < 0) |
| 5426 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5427 | return list; |
| 5428 | |
| 5429 | onError: |
| 5430 | Py_DECREF(list); |
| 5431 | return NULL; |
| 5432 | } |
| 5433 | |
| 5434 | static |
| 5435 | PyObject *rsplit_char(PyUnicodeObject *self, |
| 5436 | PyObject *list, |
| 5437 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5438 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5439 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5440 | register Py_ssize_t i; |
| 5441 | register Py_ssize_t j; |
| 5442 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5443 | PyObject *str; |
| 5444 | |
| 5445 | for (i = j = len - 1; i >= 0; ) { |
| 5446 | if (self->str[i] == ch) { |
| 5447 | if (maxcount-- <= 0) |
| 5448 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5449 | SPLIT_APPEND(self->str, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5450 | j = i = i - 1; |
| 5451 | } else |
| 5452 | i--; |
| 5453 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 5454 | if (j >= -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5455 | SPLIT_APPEND(self->str, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5456 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5457 | if (PyList_Reverse(list) < 0) |
| 5458 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5459 | return list; |
| 5460 | |
| 5461 | onError: |
| 5462 | Py_DECREF(list); |
| 5463 | return NULL; |
| 5464 | } |
| 5465 | |
| 5466 | static |
| 5467 | PyObject *rsplit_substring(PyUnicodeObject *self, |
| 5468 | PyObject *list, |
| 5469 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5470 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5471 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5472 | register Py_ssize_t i; |
| 5473 | register Py_ssize_t j; |
| 5474 | Py_ssize_t len = self->length; |
| 5475 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5476 | PyObject *str; |
| 5477 | |
| 5478 | for (i = len - sublen, j = len; i >= 0; ) { |
| 5479 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5480 | if (maxcount-- <= 0) |
| 5481 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5482 | SPLIT_APPEND(self->str, i + sublen, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5483 | j = i; |
| 5484 | i -= sublen; |
| 5485 | } else |
| 5486 | i--; |
| 5487 | } |
| 5488 | if (j >= 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5489 | SPLIT_APPEND(self->str, 0, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5490 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5491 | if (PyList_Reverse(list) < 0) |
| 5492 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5493 | return list; |
| 5494 | |
| 5495 | onError: |
| 5496 | Py_DECREF(list); |
| 5497 | return NULL; |
| 5498 | } |
| 5499 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5500 | #undef SPLIT_APPEND |
| 5501 | |
| 5502 | static |
| 5503 | PyObject *split(PyUnicodeObject *self, |
| 5504 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5505 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5506 | { |
| 5507 | PyObject *list; |
| 5508 | |
| 5509 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5510 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5511 | |
| 5512 | list = PyList_New(0); |
| 5513 | if (!list) |
| 5514 | return NULL; |
| 5515 | |
| 5516 | if (substring == NULL) |
| 5517 | return split_whitespace(self,list,maxcount); |
| 5518 | |
| 5519 | else if (substring->length == 1) |
| 5520 | return split_char(self,list,substring->str[0],maxcount); |
| 5521 | |
| 5522 | else if (substring->length == 0) { |
| 5523 | Py_DECREF(list); |
| 5524 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5525 | return NULL; |
| 5526 | } |
| 5527 | else |
| 5528 | return split_substring(self,list,substring,maxcount); |
| 5529 | } |
| 5530 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5531 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5532 | PyObject *rsplit(PyUnicodeObject *self, |
| 5533 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5534 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5535 | { |
| 5536 | PyObject *list; |
| 5537 | |
| 5538 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5539 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5540 | |
| 5541 | list = PyList_New(0); |
| 5542 | if (!list) |
| 5543 | return NULL; |
| 5544 | |
| 5545 | if (substring == NULL) |
| 5546 | return rsplit_whitespace(self,list,maxcount); |
| 5547 | |
| 5548 | else if (substring->length == 1) |
| 5549 | return rsplit_char(self,list,substring->str[0],maxcount); |
| 5550 | |
| 5551 | else if (substring->length == 0) { |
| 5552 | Py_DECREF(list); |
| 5553 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5554 | return NULL; |
| 5555 | } |
| 5556 | else |
| 5557 | return rsplit_substring(self,list,substring,maxcount); |
| 5558 | } |
| 5559 | |
| 5560 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5561 | PyObject *replace(PyUnicodeObject *self, |
| 5562 | PyUnicodeObject *str1, |
| 5563 | PyUnicodeObject *str2, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5564 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5565 | { |
| 5566 | PyUnicodeObject *u; |
| 5567 | |
| 5568 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5569 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5570 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5571 | if (str1->length == str2->length) { |
| 5572 | /* same length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5573 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5574 | if (str1->length == 1) { |
| 5575 | /* replace characters */ |
| 5576 | Py_UNICODE u1, u2; |
| 5577 | if (!findchar(self->str, self->length, str1->str[0])) |
| 5578 | goto nothing; |
| 5579 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5580 | if (!u) |
| 5581 | return NULL; |
| 5582 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5583 | u1 = str1->str[0]; |
| 5584 | u2 = str2->str[0]; |
| 5585 | for (i = 0; i < u->length; i++) |
| 5586 | if (u->str[i] == u1) { |
| 5587 | if (--maxcount < 0) |
| 5588 | break; |
| 5589 | u->str[i] = u2; |
| 5590 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5591 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5592 | i = fastsearch( |
| 5593 | self->str, self->length, str1->str, str1->length, FAST_SEARCH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5594 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5595 | if (i < 0) |
| 5596 | goto nothing; |
| 5597 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5598 | if (!u) |
| 5599 | return NULL; |
| 5600 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5601 | while (i <= self->length - str1->length) |
| 5602 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 5603 | if (--maxcount < 0) |
| 5604 | break; |
| 5605 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 5606 | i += str1->length; |
| 5607 | } else |
| 5608 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5609 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5610 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5611 | |
| 5612 | Py_ssize_t n, i, j, e; |
| 5613 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5614 | Py_UNICODE *p; |
| 5615 | |
| 5616 | /* replace strings */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5617 | n = stringlib_count(self->str, self->length, str1->str, str1->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5618 | if (n > maxcount) |
| 5619 | n = maxcount; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5620 | if (n == 0) |
| 5621 | goto nothing; |
| 5622 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 5623 | delta = (str2->length - str1->length); |
| 5624 | if (delta == 0) { |
| 5625 | new_size = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5626 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5627 | product = n * (str2->length - str1->length); |
| 5628 | if ((product / (str2->length - str1->length)) != n) { |
| 5629 | PyErr_SetString(PyExc_OverflowError, |
| 5630 | "replace string is too long"); |
| 5631 | return NULL; |
| 5632 | } |
| 5633 | new_size = self->length + product; |
| 5634 | if (new_size < 0) { |
| 5635 | PyErr_SetString(PyExc_OverflowError, |
| 5636 | "replace string is too long"); |
| 5637 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5638 | } |
| 5639 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5640 | u = _PyUnicode_New(new_size); |
| 5641 | if (!u) |
| 5642 | return NULL; |
| 5643 | i = 0; |
| 5644 | p = u->str; |
| 5645 | e = self->length - str1->length; |
| 5646 | if (str1->length > 0) { |
| 5647 | while (n-- > 0) { |
| 5648 | /* look for next match */ |
| 5649 | j = i; |
| 5650 | while (j <= e) { |
| 5651 | if (Py_UNICODE_MATCH(self, j, str1)) |
| 5652 | break; |
| 5653 | j++; |
| 5654 | } |
| 5655 | if (j > i) { |
| 5656 | if (j > e) |
| 5657 | break; |
| 5658 | /* copy unchanged part [i:j] */ |
| 5659 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 5660 | p += j - i; |
| 5661 | } |
| 5662 | /* copy substitution string */ |
| 5663 | if (str2->length > 0) { |
| 5664 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 5665 | p += str2->length; |
| 5666 | } |
| 5667 | i = j + str1->length; |
| 5668 | } |
| 5669 | if (i < self->length) |
| 5670 | /* copy tail [i:] */ |
| 5671 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 5672 | } else { |
| 5673 | /* interleave */ |
| 5674 | while (n > 0) { |
| 5675 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 5676 | p += str2->length; |
| 5677 | if (--n <= 0) |
| 5678 | break; |
| 5679 | *p++ = self->str[i++]; |
| 5680 | } |
| 5681 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 5682 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5683 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5684 | return (PyObject *) u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5685 | |
| 5686 | nothing: |
| 5687 | /* nothing to replace; return original string (when possible) */ |
| 5688 | if (PyUnicode_CheckExact(self)) { |
| 5689 | Py_INCREF(self); |
| 5690 | return (PyObject *) self; |
| 5691 | } |
| 5692 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5693 | } |
| 5694 | |
| 5695 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 5696 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5697 | PyDoc_STRVAR(title__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5698 | "S.title() -> unicode\n\ |
| 5699 | \n\ |
| 5700 | 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] | 5701 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5702 | |
| 5703 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5704 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5705 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5706 | return fixup(self, fixtitle); |
| 5707 | } |
| 5708 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5709 | PyDoc_STRVAR(capitalize__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5710 | "S.capitalize() -> unicode\n\ |
| 5711 | \n\ |
| 5712 | 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] | 5713 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5714 | |
| 5715 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5716 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5717 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5718 | return fixup(self, fixcapitalize); |
| 5719 | } |
| 5720 | |
| 5721 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5722 | PyDoc_STRVAR(capwords__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5723 | "S.capwords() -> unicode\n\ |
| 5724 | \n\ |
| 5725 | 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] | 5726 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5727 | |
| 5728 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5729 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5730 | { |
| 5731 | PyObject *list; |
| 5732 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5733 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5734 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5735 | /* Split into words */ |
| 5736 | list = split(self, NULL, -1); |
| 5737 | if (!list) |
| 5738 | return NULL; |
| 5739 | |
| 5740 | /* Capitalize each word */ |
| 5741 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 5742 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 5743 | fixcapitalize); |
| 5744 | if (item == NULL) |
| 5745 | goto onError; |
| 5746 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 5747 | PyList_SET_ITEM(list, i, item); |
| 5748 | } |
| 5749 | |
| 5750 | /* Join the words to form a new string */ |
| 5751 | item = PyUnicode_Join(NULL, list); |
| 5752 | |
| 5753 | onError: |
| 5754 | Py_DECREF(list); |
| 5755 | return (PyObject *)item; |
| 5756 | } |
| 5757 | #endif |
| 5758 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5759 | /* Argument converter. Coerces to a single unicode character */ |
| 5760 | |
| 5761 | static int |
| 5762 | convert_uc(PyObject *obj, void *addr) |
| 5763 | { |
| 5764 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 5765 | PyObject *uniobj; |
| 5766 | Py_UNICODE *unistr; |
| 5767 | |
| 5768 | uniobj = PyUnicode_FromObject(obj); |
| 5769 | if (uniobj == NULL) { |
| 5770 | PyErr_SetString(PyExc_TypeError, |
| 5771 | "The fill character cannot be converted to Unicode"); |
| 5772 | return 0; |
| 5773 | } |
| 5774 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 5775 | PyErr_SetString(PyExc_TypeError, |
| 5776 | "The fill character must be exactly one character long"); |
| 5777 | Py_DECREF(uniobj); |
| 5778 | return 0; |
| 5779 | } |
| 5780 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 5781 | *fillcharloc = unistr[0]; |
| 5782 | Py_DECREF(uniobj); |
| 5783 | return 1; |
| 5784 | } |
| 5785 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5786 | PyDoc_STRVAR(center__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5787 | "S.center(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5788 | \n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5789 | Return S centered in a Unicode string of length width. Padding is\n\ |
| 5790 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5791 | |
| 5792 | static PyObject * |
| 5793 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 5794 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5795 | Py_ssize_t marg, left; |
| 5796 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5797 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5798 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 5799 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5800 | return NULL; |
| 5801 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5802 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5803 | Py_INCREF(self); |
| 5804 | return (PyObject*) self; |
| 5805 | } |
| 5806 | |
| 5807 | marg = width - self->length; |
| 5808 | left = marg / 2 + (marg & width & 1); |
| 5809 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5810 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5811 | } |
| 5812 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5813 | #if 0 |
| 5814 | |
| 5815 | /* This code should go into some future Unicode collation support |
| 5816 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 5817 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5818 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5819 | /* speedy UTF-16 code point order comparison */ |
| 5820 | /* gleaned from: */ |
| 5821 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 5822 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 5823 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5824 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5825 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5826 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 5827 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 5828 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5829 | }; |
| 5830 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5831 | static int |
| 5832 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 5833 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5834 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5835 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5836 | Py_UNICODE *s1 = str1->str; |
| 5837 | Py_UNICODE *s2 = str2->str; |
| 5838 | |
| 5839 | len1 = str1->length; |
| 5840 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5841 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5842 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5843 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5844 | |
| 5845 | c1 = *s1++; |
| 5846 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 5847 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5848 | if (c1 > (1<<11) * 26) |
| 5849 | c1 += utf16Fixup[c1>>11]; |
| 5850 | if (c2 > (1<<11) * 26) |
| 5851 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5852 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 5853 | |
| 5854 | if (c1 != c2) |
| 5855 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5856 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5857 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5858 | } |
| 5859 | |
| 5860 | return (len1 < len2) ? -1 : (len1 != len2); |
| 5861 | } |
| 5862 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5863 | #else |
| 5864 | |
| 5865 | static int |
| 5866 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 5867 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5868 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5869 | |
| 5870 | Py_UNICODE *s1 = str1->str; |
| 5871 | Py_UNICODE *s2 = str2->str; |
| 5872 | |
| 5873 | len1 = str1->length; |
| 5874 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5875 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5876 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5877 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5878 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 5879 | c1 = *s1++; |
| 5880 | c2 = *s2++; |
| 5881 | |
| 5882 | if (c1 != c2) |
| 5883 | return (c1 < c2) ? -1 : 1; |
| 5884 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5885 | len1--; len2--; |
| 5886 | } |
| 5887 | |
| 5888 | return (len1 < len2) ? -1 : (len1 != len2); |
| 5889 | } |
| 5890 | |
| 5891 | #endif |
| 5892 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5893 | int PyUnicode_Compare(PyObject *left, |
| 5894 | PyObject *right) |
| 5895 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 5896 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) |
| 5897 | return unicode_compare((PyUnicodeObject *)left, |
| 5898 | (PyUnicodeObject *)right); |
| 5899 | if ((PyString_Check(left) && PyUnicode_Check(right)) || |
| 5900 | (PyUnicode_Check(left) && PyString_Check(right))) { |
| 5901 | if (PyUnicode_Check(left)) |
| 5902 | left = _PyUnicode_AsDefaultEncodedString(left, NULL); |
| 5903 | if (PyUnicode_Check(right)) |
| 5904 | right = _PyUnicode_AsDefaultEncodedString(right, NULL); |
| 5905 | assert(PyString_Check(left)); |
| 5906 | assert(PyString_Check(right)); |
| 5907 | return PyObject_Compare(left, right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5908 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 5909 | PyErr_Format(PyExc_TypeError, |
| 5910 | "Can't compare %.100s and %.100s", |
| 5911 | left->ob_type->tp_name, |
| 5912 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5913 | return -1; |
| 5914 | } |
| 5915 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 5916 | int |
| 5917 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 5918 | { |
| 5919 | int i; |
| 5920 | Py_UNICODE *id; |
| 5921 | assert(PyUnicode_Check(uni)); |
| 5922 | id = PyUnicode_AS_UNICODE(uni); |
| 5923 | /* Compare Unicode string and source character set string */ |
| 5924 | for (i = 0; id[i] && str[i]; i++) |
| 5925 | if (id[i] != str[i]) |
| 5926 | return ((int)id[i] < (int)str[i]) ? -1 : 1; |
| 5927 | if (id[i]) |
| 5928 | return 1; /* uni is longer */ |
| 5929 | if (str[i]) |
| 5930 | return -1; /* str is longer */ |
| 5931 | return 0; |
| 5932 | } |
| 5933 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 5934 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 5935 | PyObject *right, |
| 5936 | int op) |
| 5937 | { |
| 5938 | int result; |
| 5939 | |
| 5940 | result = PyUnicode_Compare(left, right); |
| 5941 | if (result == -1 && PyErr_Occurred()) |
| 5942 | goto onError; |
| 5943 | |
| 5944 | /* Convert the return value to a Boolean */ |
| 5945 | switch (op) { |
| 5946 | case Py_EQ: |
| 5947 | result = (result == 0); |
| 5948 | break; |
| 5949 | case Py_NE: |
| 5950 | result = (result != 0); |
| 5951 | break; |
| 5952 | case Py_LE: |
| 5953 | result = (result <= 0); |
| 5954 | break; |
| 5955 | case Py_GE: |
| 5956 | result = (result >= 0); |
| 5957 | break; |
| 5958 | case Py_LT: |
| 5959 | result = (result == -1); |
| 5960 | break; |
| 5961 | case Py_GT: |
| 5962 | result = (result == 1); |
| 5963 | break; |
| 5964 | } |
| 5965 | return PyBool_FromLong(result); |
| 5966 | |
| 5967 | onError: |
| 5968 | |
| 5969 | /* Standard case |
| 5970 | |
| 5971 | Type errors mean that PyUnicode_FromObject() could not convert |
| 5972 | one of the arguments (usually the right hand side) to Unicode, |
| 5973 | ie. we can't handle the comparison request. However, it is |
| 5974 | possible that the other object knows a comparison method, which |
| 5975 | is why we return Py_NotImplemented to give the other object a |
| 5976 | chance. |
| 5977 | |
| 5978 | */ |
| 5979 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 5980 | PyErr_Clear(); |
| 5981 | Py_INCREF(Py_NotImplemented); |
| 5982 | return Py_NotImplemented; |
| 5983 | } |
| 5984 | if (op != Py_EQ && op != Py_NE) |
| 5985 | return NULL; |
| 5986 | |
| 5987 | /* Equality comparison. |
| 5988 | |
| 5989 | This is a special case: we silence any PyExc_UnicodeDecodeError |
| 5990 | and instead turn it into a PyErr_UnicodeWarning. |
| 5991 | |
| 5992 | */ |
| 5993 | if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) |
| 5994 | return NULL; |
| 5995 | PyErr_Clear(); |
Skip Montanaro | 46fc337 | 2007-08-12 11:44:53 +0000 | [diff] [blame] | 5996 | if (PyErr_WarnEx(PyExc_UnicodeWarning, |
| 5997 | (op == Py_EQ) ? |
| 5998 | "Unicode equal comparison " |
| 5999 | "failed to convert both arguments to Unicode - " |
| 6000 | "interpreting them as being unequal" |
| 6001 | : |
| 6002 | "Unicode unequal comparison " |
| 6003 | "failed to convert both arguments to Unicode - " |
| 6004 | "interpreting them as being unequal", |
| 6005 | 1) < 0) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6006 | return NULL; |
| 6007 | result = (op == Py_NE); |
| 6008 | return PyBool_FromLong(result); |
| 6009 | } |
| 6010 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6011 | int PyUnicode_Contains(PyObject *container, |
| 6012 | PyObject *element) |
| 6013 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6014 | PyObject *str, *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6015 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6016 | |
| 6017 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6018 | sub = PyUnicode_FromObject(element); |
| 6019 | if (!sub) { |
Walter Dörwald | 26e0f51 | 2007-06-12 16:51:31 +0000 | [diff] [blame] | 6020 | PyErr_Format(PyExc_TypeError, |
| 6021 | "'in <string>' requires string as left operand, not %s", |
| 6022 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6023 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6024 | } |
| 6025 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6026 | str = PyUnicode_FromObject(container); |
| 6027 | if (!str) { |
| 6028 | Py_DECREF(sub); |
| 6029 | return -1; |
| 6030 | } |
| 6031 | |
| 6032 | result = stringlib_contains_obj(str, sub); |
| 6033 | |
| 6034 | Py_DECREF(str); |
| 6035 | Py_DECREF(sub); |
| 6036 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6037 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6038 | } |
| 6039 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6040 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 6041 | |
| 6042 | PyObject *PyUnicode_Concat(PyObject *left, |
| 6043 | PyObject *right) |
| 6044 | { |
| 6045 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 6046 | |
Guido van Rossum | 84d79dd | 2007-04-13 02:23:57 +0000 | [diff] [blame] | 6047 | if (PyBytes_Check(left) || PyBytes_Check(right)) |
| 6048 | return PyBytes_Concat(left, right); |
| 6049 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6050 | /* Coerce the two arguments */ |
| 6051 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 6052 | if (u == NULL) |
| 6053 | goto onError; |
| 6054 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 6055 | if (v == NULL) |
| 6056 | goto onError; |
| 6057 | |
| 6058 | /* Shortcuts */ |
| 6059 | if (v == unicode_empty) { |
| 6060 | Py_DECREF(v); |
| 6061 | return (PyObject *)u; |
| 6062 | } |
| 6063 | if (u == unicode_empty) { |
| 6064 | Py_DECREF(u); |
| 6065 | return (PyObject *)v; |
| 6066 | } |
| 6067 | |
| 6068 | /* Concat the two Unicode strings */ |
| 6069 | w = _PyUnicode_New(u->length + v->length); |
| 6070 | if (w == NULL) |
| 6071 | goto onError; |
| 6072 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 6073 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 6074 | |
| 6075 | Py_DECREF(u); |
| 6076 | Py_DECREF(v); |
| 6077 | return (PyObject *)w; |
| 6078 | |
| 6079 | onError: |
| 6080 | Py_XDECREF(u); |
| 6081 | Py_XDECREF(v); |
| 6082 | return NULL; |
| 6083 | } |
| 6084 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6085 | void |
| 6086 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 6087 | { |
| 6088 | PyObject *new; |
| 6089 | if (*pleft == NULL) |
| 6090 | return; |
| 6091 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 6092 | Py_DECREF(*pleft); |
| 6093 | *pleft = NULL; |
| 6094 | return; |
| 6095 | } |
| 6096 | new = PyUnicode_Concat(*pleft, right); |
| 6097 | Py_DECREF(*pleft); |
| 6098 | *pleft = new; |
| 6099 | } |
| 6100 | |
| 6101 | void |
| 6102 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 6103 | { |
| 6104 | PyUnicode_Append(pleft, right); |
| 6105 | Py_XDECREF(right); |
| 6106 | } |
| 6107 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6108 | PyDoc_STRVAR(count__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6109 | "S.count(sub[, start[, end]]) -> int\n\ |
| 6110 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6111 | Return the number of non-overlapping occurrences of substring sub in\n\ |
| 6112 | 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] | 6113 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6114 | |
| 6115 | static PyObject * |
| 6116 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 6117 | { |
| 6118 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6119 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6120 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6121 | PyObject *result; |
| 6122 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6123 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 6124 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6125 | return NULL; |
| 6126 | |
| 6127 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6128 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6129 | if (substring == NULL) |
| 6130 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6131 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6132 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6133 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6134 | result = PyInt_FromSsize_t( |
| 6135 | stringlib_count(self->str + start, end - start, |
| 6136 | substring->str, substring->length) |
| 6137 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6138 | |
| 6139 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6140 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6141 | return result; |
| 6142 | } |
| 6143 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6144 | PyDoc_STRVAR(encode__doc__, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6145 | "S.encode([encoding[,errors]]) -> string or unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6146 | \n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6147 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 6148 | 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] | 6149 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6150 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 6151 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 6152 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6153 | |
| 6154 | static PyObject * |
| 6155 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 6156 | { |
| 6157 | char *encoding = NULL; |
| 6158 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6159 | PyObject *v; |
| 6160 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6161 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 6162 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6163 | v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6164 | if (v == NULL) |
| 6165 | goto onError; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 6166 | if (!PyBytes_Check(v)) { |
Guido van Rossum | 4355a47 | 2007-05-04 05:00:04 +0000 | [diff] [blame] | 6167 | if (PyString_Check(v)) { |
| 6168 | /* Old codec, turn it into bytes */ |
| 6169 | PyObject *b = PyBytes_FromObject(v); |
| 6170 | Py_DECREF(v); |
| 6171 | return b; |
| 6172 | } |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6173 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 6174 | "encoder did not return a bytes object " |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6175 | "(type=%.400s)", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 6176 | Py_Type(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6177 | Py_DECREF(v); |
| 6178 | return NULL; |
| 6179 | } |
| 6180 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6181 | |
| 6182 | onError: |
| 6183 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6184 | } |
| 6185 | |
| 6186 | PyDoc_STRVAR(decode__doc__, |
| 6187 | "S.decode([encoding[,errors]]) -> string or unicode\n\ |
| 6188 | \n\ |
| 6189 | Decodes S using the codec registered for encoding. encoding defaults\n\ |
| 6190 | to the default encoding. errors may be given to set a different error\n\ |
| 6191 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 6192 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 6193 | as well as any other name registerd with codecs.register_error that is\n\ |
| 6194 | able to handle UnicodeDecodeErrors."); |
| 6195 | |
| 6196 | static PyObject * |
Marc-André Lemburg | 126b44c | 2004-07-10 12:04:20 +0000 | [diff] [blame] | 6197 | unicode_decode(PyUnicodeObject *self, PyObject *args) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6198 | { |
| 6199 | char *encoding = NULL; |
| 6200 | char *errors = NULL; |
| 6201 | PyObject *v; |
| 6202 | |
| 6203 | if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
| 6204 | return NULL; |
| 6205 | v = PyUnicode_AsDecodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6206 | if (v == NULL) |
| 6207 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6208 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 6209 | PyErr_Format(PyExc_TypeError, |
| 6210 | "decoder did not return a string/unicode object " |
| 6211 | "(type=%.400s)", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 6212 | Py_Type(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6213 | Py_DECREF(v); |
| 6214 | return NULL; |
| 6215 | } |
| 6216 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6217 | |
| 6218 | onError: |
| 6219 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6220 | } |
| 6221 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6222 | PyDoc_STRVAR(expandtabs__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6223 | "S.expandtabs([tabsize]) -> unicode\n\ |
| 6224 | \n\ |
| 6225 | 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] | 6226 | 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] | 6227 | |
| 6228 | static PyObject* |
| 6229 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 6230 | { |
| 6231 | Py_UNICODE *e; |
| 6232 | Py_UNICODE *p; |
| 6233 | Py_UNICODE *q; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6234 | Py_ssize_t i, j, old_j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6235 | PyUnicodeObject *u; |
| 6236 | int tabsize = 8; |
| 6237 | |
| 6238 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 6239 | return NULL; |
| 6240 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6241 | /* First pass: determine size of output string */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6242 | i = j = old_j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6243 | e = self->str + self->length; |
| 6244 | for (p = self->str; p < e; p++) |
| 6245 | if (*p == '\t') { |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6246 | if (tabsize > 0) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6247 | j += tabsize - (j % tabsize); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6248 | if (old_j > j) { |
| 6249 | PyErr_SetString(PyExc_OverflowError, |
| 6250 | "new string is too long"); |
| 6251 | return NULL; |
| 6252 | } |
| 6253 | old_j = j; |
| 6254 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6255 | } |
| 6256 | else { |
| 6257 | j++; |
| 6258 | if (*p == '\n' || *p == '\r') { |
| 6259 | i += j; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6260 | old_j = j = 0; |
| 6261 | if (i < 0) { |
| 6262 | PyErr_SetString(PyExc_OverflowError, |
| 6263 | "new string is too long"); |
| 6264 | return NULL; |
| 6265 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6266 | } |
| 6267 | } |
| 6268 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6269 | if ((i + j) < 0) { |
| 6270 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 6271 | return NULL; |
| 6272 | } |
| 6273 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6274 | /* Second pass: create output string and fill it */ |
| 6275 | u = _PyUnicode_New(i + j); |
| 6276 | if (!u) |
| 6277 | return NULL; |
| 6278 | |
| 6279 | j = 0; |
| 6280 | q = u->str; |
| 6281 | |
| 6282 | for (p = self->str; p < e; p++) |
| 6283 | if (*p == '\t') { |
| 6284 | if (tabsize > 0) { |
| 6285 | i = tabsize - (j % tabsize); |
| 6286 | j += i; |
| 6287 | while (i--) |
| 6288 | *q++ = ' '; |
| 6289 | } |
| 6290 | } |
| 6291 | else { |
| 6292 | j++; |
| 6293 | *q++ = *p; |
| 6294 | if (*p == '\n' || *p == '\r') |
| 6295 | j = 0; |
| 6296 | } |
| 6297 | |
| 6298 | return (PyObject*) u; |
| 6299 | } |
| 6300 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6301 | PyDoc_STRVAR(find__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6302 | "S.find(sub [,start [,end]]) -> int\n\ |
| 6303 | \n\ |
| 6304 | Return the lowest index in S where substring sub is found,\n\ |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 6305 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6306 | arguments start and end are interpreted as in slice notation.\n\ |
| 6307 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6308 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6309 | |
| 6310 | static PyObject * |
| 6311 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 6312 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6313 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6314 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6315 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6316 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6317 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6318 | if (!PyArg_ParseTuple(args, "O|O&O&:find", &substring, |
| 6319 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6320 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6321 | substring = PyUnicode_FromObject(substring); |
| 6322 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6323 | return NULL; |
| 6324 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6325 | result = stringlib_find_slice( |
| 6326 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6327 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6328 | start, end |
| 6329 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6330 | |
| 6331 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6332 | |
| 6333 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6334 | } |
| 6335 | |
| 6336 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6337 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6338 | { |
| 6339 | if (index < 0 || index >= self->length) { |
| 6340 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 6341 | return NULL; |
| 6342 | } |
| 6343 | |
| 6344 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 6345 | } |
| 6346 | |
| 6347 | static long |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6348 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6349 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6350 | /* Since Unicode objects compare equal to their UTF-8 string |
| 6351 | counterparts, we hash the UTF-8 string. */ |
| 6352 | PyObject *v = _PyUnicode_AsDefaultEncodedString(self, NULL); |
| 6353 | return PyObject_Hash(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6354 | } |
| 6355 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6356 | PyDoc_STRVAR(index__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6357 | "S.index(sub [,start [,end]]) -> int\n\ |
| 6358 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6359 | 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] | 6360 | |
| 6361 | static PyObject * |
| 6362 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 6363 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6364 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6365 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6366 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6367 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6368 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6369 | if (!PyArg_ParseTuple(args, "O|O&O&:index", &substring, |
| 6370 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6371 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6372 | substring = PyUnicode_FromObject(substring); |
| 6373 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6374 | return NULL; |
| 6375 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6376 | result = stringlib_find_slice( |
| 6377 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6378 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6379 | start, end |
| 6380 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6381 | |
| 6382 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6383 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6384 | if (result < 0) { |
| 6385 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 6386 | return NULL; |
| 6387 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6388 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6389 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6390 | } |
| 6391 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6392 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6393 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6394 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6395 | 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] | 6396 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6397 | |
| 6398 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6399 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6400 | { |
| 6401 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6402 | register const Py_UNICODE *e; |
| 6403 | int cased; |
| 6404 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6405 | /* Shortcut for single character strings */ |
| 6406 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6407 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6408 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6409 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6410 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6411 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6412 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6413 | e = p + PyUnicode_GET_SIZE(self); |
| 6414 | cased = 0; |
| 6415 | for (; p < e; p++) { |
| 6416 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6417 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6418 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6419 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6420 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 6421 | cased = 1; |
| 6422 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6423 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6424 | } |
| 6425 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6426 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6427 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6428 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6429 | 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] | 6430 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6431 | |
| 6432 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6433 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6434 | { |
| 6435 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6436 | register const Py_UNICODE *e; |
| 6437 | int cased; |
| 6438 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6439 | /* Shortcut for single character strings */ |
| 6440 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6441 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6442 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6443 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6444 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6445 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6446 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6447 | e = p + PyUnicode_GET_SIZE(self); |
| 6448 | cased = 0; |
| 6449 | for (; p < e; p++) { |
| 6450 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6451 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6452 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6453 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6454 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 6455 | cased = 1; |
| 6456 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6457 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6458 | } |
| 6459 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6460 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6461 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6462 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6463 | Return True if S is a titlecased string and there is at least one\n\ |
| 6464 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 6465 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 6466 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6467 | |
| 6468 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6469 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6470 | { |
| 6471 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6472 | register const Py_UNICODE *e; |
| 6473 | int cased, previous_is_cased; |
| 6474 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6475 | /* Shortcut for single character strings */ |
| 6476 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6477 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 6478 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6479 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6480 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6481 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6482 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6483 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6484 | e = p + PyUnicode_GET_SIZE(self); |
| 6485 | cased = 0; |
| 6486 | previous_is_cased = 0; |
| 6487 | for (; p < e; p++) { |
| 6488 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6489 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6490 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 6491 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6492 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6493 | previous_is_cased = 1; |
| 6494 | cased = 1; |
| 6495 | } |
| 6496 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 6497 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6498 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6499 | previous_is_cased = 1; |
| 6500 | cased = 1; |
| 6501 | } |
| 6502 | else |
| 6503 | previous_is_cased = 0; |
| 6504 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6505 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6506 | } |
| 6507 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6508 | PyDoc_STRVAR(isspace__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6509 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6510 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6511 | Return True if all characters in S are whitespace\n\ |
| 6512 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6513 | |
| 6514 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6515 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6516 | { |
| 6517 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6518 | register const Py_UNICODE *e; |
| 6519 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6520 | /* Shortcut for single character strings */ |
| 6521 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6522 | Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6523 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6524 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6525 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6526 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6527 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6528 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6529 | e = p + PyUnicode_GET_SIZE(self); |
| 6530 | for (; p < e; p++) { |
| 6531 | if (!Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6532 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6533 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6534 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6535 | } |
| 6536 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6537 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6538 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6539 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6540 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6541 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6542 | |
| 6543 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6544 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6545 | { |
| 6546 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6547 | register const Py_UNICODE *e; |
| 6548 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6549 | /* Shortcut for single character strings */ |
| 6550 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6551 | Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6552 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6553 | |
| 6554 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6555 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6556 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6557 | |
| 6558 | e = p + PyUnicode_GET_SIZE(self); |
| 6559 | for (; p < e; p++) { |
| 6560 | if (!Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6561 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6562 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6563 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6564 | } |
| 6565 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6566 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6567 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6568 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6569 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6570 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6571 | |
| 6572 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6573 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6574 | { |
| 6575 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6576 | register const Py_UNICODE *e; |
| 6577 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6578 | /* Shortcut for single character strings */ |
| 6579 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6580 | Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6581 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6582 | |
| 6583 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6584 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6585 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6586 | |
| 6587 | e = p + PyUnicode_GET_SIZE(self); |
| 6588 | for (; p < e; p++) { |
| 6589 | if (!Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6590 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6591 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6592 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6593 | } |
| 6594 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6595 | PyDoc_STRVAR(isdecimal__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6596 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6597 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6598 | 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] | 6599 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6600 | |
| 6601 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6602 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6603 | { |
| 6604 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6605 | register const Py_UNICODE *e; |
| 6606 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6607 | /* Shortcut for single character strings */ |
| 6608 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6609 | Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6610 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6611 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6612 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6613 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6614 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6615 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6616 | e = p + PyUnicode_GET_SIZE(self); |
| 6617 | for (; p < e; p++) { |
| 6618 | if (!Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6619 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6620 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6621 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6622 | } |
| 6623 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6624 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6625 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6626 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6627 | Return True if all characters in S are digits\n\ |
| 6628 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6629 | |
| 6630 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6631 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6632 | { |
| 6633 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6634 | register const Py_UNICODE *e; |
| 6635 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6636 | /* Shortcut for single character strings */ |
| 6637 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6638 | Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6639 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6640 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6641 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6642 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6643 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6644 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6645 | e = p + PyUnicode_GET_SIZE(self); |
| 6646 | for (; p < e; p++) { |
| 6647 | if (!Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6648 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6649 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6650 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6651 | } |
| 6652 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6653 | PyDoc_STRVAR(isnumeric__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6654 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6655 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6656 | 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] | 6657 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6658 | |
| 6659 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6660 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6661 | { |
| 6662 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6663 | register const Py_UNICODE *e; |
| 6664 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6665 | /* Shortcut for single character strings */ |
| 6666 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6667 | Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6668 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6669 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6670 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6671 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6672 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6673 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6674 | e = p + PyUnicode_GET_SIZE(self); |
| 6675 | for (; p < e; p++) { |
| 6676 | if (!Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6677 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6678 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6679 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6680 | } |
| 6681 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6682 | PyDoc_STRVAR(join__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6683 | "S.join(sequence) -> unicode\n\ |
| 6684 | \n\ |
| 6685 | 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] | 6686 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6687 | |
| 6688 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6689 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6690 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6691 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6692 | } |
| 6693 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6694 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6695 | unicode_length(PyUnicodeObject *self) |
| 6696 | { |
| 6697 | return self->length; |
| 6698 | } |
| 6699 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6700 | PyDoc_STRVAR(ljust__doc__, |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 6701 | "S.ljust(width[, fillchar]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6702 | \n\ |
| 6703 | 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] | 6704 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6705 | |
| 6706 | static PyObject * |
| 6707 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 6708 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6709 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6710 | Py_UNICODE fillchar = ' '; |
| 6711 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6712 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6713 | return NULL; |
| 6714 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6715 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6716 | Py_INCREF(self); |
| 6717 | return (PyObject*) self; |
| 6718 | } |
| 6719 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6720 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6721 | } |
| 6722 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6723 | PyDoc_STRVAR(lower__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6724 | "S.lower() -> unicode\n\ |
| 6725 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6726 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6727 | |
| 6728 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6729 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6730 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6731 | return fixup(self, fixlower); |
| 6732 | } |
| 6733 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6734 | #define LEFTSTRIP 0 |
| 6735 | #define RIGHTSTRIP 1 |
| 6736 | #define BOTHSTRIP 2 |
| 6737 | |
| 6738 | /* Arrays indexed by above */ |
| 6739 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 6740 | |
| 6741 | #define STRIPNAME(i) (stripformat[i]+3) |
| 6742 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6743 | /* externally visible for str.strip(unicode) */ |
| 6744 | PyObject * |
| 6745 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 6746 | { |
| 6747 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6748 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6749 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6750 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 6751 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6752 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6753 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
| 6754 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6755 | i = 0; |
| 6756 | if (striptype != RIGHTSTRIP) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6757 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 6758 | i++; |
| 6759 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6760 | } |
| 6761 | |
| 6762 | j = len; |
| 6763 | if (striptype != LEFTSTRIP) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6764 | do { |
| 6765 | j--; |
| 6766 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 6767 | j++; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6768 | } |
| 6769 | |
| 6770 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6771 | Py_INCREF(self); |
| 6772 | return (PyObject*)self; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6773 | } |
| 6774 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6775 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6776 | } |
| 6777 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6778 | |
| 6779 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6780 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6781 | { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6782 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6783 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6784 | |
| 6785 | i = 0; |
| 6786 | if (striptype != RIGHTSTRIP) { |
| 6787 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 6788 | i++; |
| 6789 | } |
| 6790 | } |
| 6791 | |
| 6792 | j = len; |
| 6793 | if (striptype != LEFTSTRIP) { |
| 6794 | do { |
| 6795 | j--; |
| 6796 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 6797 | j++; |
| 6798 | } |
| 6799 | |
| 6800 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 6801 | Py_INCREF(self); |
| 6802 | return (PyObject*)self; |
| 6803 | } |
| 6804 | else |
| 6805 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6806 | } |
| 6807 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6808 | |
| 6809 | static PyObject * |
| 6810 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 6811 | { |
| 6812 | PyObject *sep = NULL; |
| 6813 | |
| 6814 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 6815 | return NULL; |
| 6816 | |
| 6817 | if (sep != NULL && sep != Py_None) { |
| 6818 | if (PyUnicode_Check(sep)) |
| 6819 | return _PyUnicode_XStrip(self, striptype, sep); |
| 6820 | else if (PyString_Check(sep)) { |
| 6821 | PyObject *res; |
| 6822 | sep = PyUnicode_FromObject(sep); |
| 6823 | if (sep==NULL) |
| 6824 | return NULL; |
| 6825 | res = _PyUnicode_XStrip(self, striptype, sep); |
| 6826 | Py_DECREF(sep); |
| 6827 | return res; |
| 6828 | } |
| 6829 | else { |
| 6830 | PyErr_Format(PyExc_TypeError, |
| 6831 | "%s arg must be None, unicode or str", |
| 6832 | STRIPNAME(striptype)); |
| 6833 | return NULL; |
| 6834 | } |
| 6835 | } |
| 6836 | |
| 6837 | return do_strip(self, striptype); |
| 6838 | } |
| 6839 | |
| 6840 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6841 | PyDoc_STRVAR(strip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6842 | "S.strip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6843 | \n\ |
| 6844 | Return a copy of the string S with leading and trailing\n\ |
| 6845 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6846 | If chars is given and not None, remove characters in chars instead.\n\ |
| 6847 | 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] | 6848 | |
| 6849 | static PyObject * |
| 6850 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 6851 | { |
| 6852 | if (PyTuple_GET_SIZE(args) == 0) |
| 6853 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 6854 | else |
| 6855 | return do_argstrip(self, BOTHSTRIP, args); |
| 6856 | } |
| 6857 | |
| 6858 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6859 | PyDoc_STRVAR(lstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6860 | "S.lstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6861 | \n\ |
| 6862 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6863 | If chars is given and not None, remove characters in chars instead.\n\ |
| 6864 | 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] | 6865 | |
| 6866 | static PyObject * |
| 6867 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 6868 | { |
| 6869 | if (PyTuple_GET_SIZE(args) == 0) |
| 6870 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 6871 | else |
| 6872 | return do_argstrip(self, LEFTSTRIP, args); |
| 6873 | } |
| 6874 | |
| 6875 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6876 | PyDoc_STRVAR(rstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6877 | "S.rstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6878 | \n\ |
| 6879 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6880 | If chars is given and not None, remove characters in chars instead.\n\ |
| 6881 | 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] | 6882 | |
| 6883 | static PyObject * |
| 6884 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 6885 | { |
| 6886 | if (PyTuple_GET_SIZE(args) == 0) |
| 6887 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 6888 | else |
| 6889 | return do_argstrip(self, RIGHTSTRIP, args); |
| 6890 | } |
| 6891 | |
| 6892 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6893 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6894 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6895 | { |
| 6896 | PyUnicodeObject *u; |
| 6897 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6898 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 6899 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6900 | |
| 6901 | if (len < 0) |
| 6902 | len = 0; |
| 6903 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6904 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6905 | /* no repeat, return original string */ |
| 6906 | Py_INCREF(str); |
| 6907 | return (PyObject*) str; |
| 6908 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 6909 | |
| 6910 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 6911 | * needed doesn't overflow size_t |
| 6912 | */ |
| 6913 | nchars = len * str->length; |
| 6914 | if (len && nchars / len != str->length) { |
| 6915 | PyErr_SetString(PyExc_OverflowError, |
| 6916 | "repeated string is too long"); |
| 6917 | return NULL; |
| 6918 | } |
| 6919 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 6920 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 6921 | PyErr_SetString(PyExc_OverflowError, |
| 6922 | "repeated string is too long"); |
| 6923 | return NULL; |
| 6924 | } |
| 6925 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6926 | if (!u) |
| 6927 | return NULL; |
| 6928 | |
| 6929 | p = u->str; |
| 6930 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6931 | if (str->length == 1 && len > 0) { |
| 6932 | Py_UNICODE_FILL(p, str->str[0], len); |
| 6933 | } else { |
| 6934 | Py_ssize_t done = 0; /* number of characters copied this far */ |
| 6935 | if (done < nchars) { |
| 6936 | Py_UNICODE_COPY(p, str->str, str->length); |
| 6937 | done = str->length; |
| 6938 | } |
| 6939 | while (done < nchars) { |
| 6940 | int n = (done <= nchars-done) ? done : nchars-done; |
| 6941 | Py_UNICODE_COPY(p+done, p, n); |
| 6942 | done += n; |
| 6943 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6944 | } |
| 6945 | |
| 6946 | return (PyObject*) u; |
| 6947 | } |
| 6948 | |
| 6949 | PyObject *PyUnicode_Replace(PyObject *obj, |
| 6950 | PyObject *subobj, |
| 6951 | PyObject *replobj, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6952 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6953 | { |
| 6954 | PyObject *self; |
| 6955 | PyObject *str1; |
| 6956 | PyObject *str2; |
| 6957 | PyObject *result; |
| 6958 | |
| 6959 | self = PyUnicode_FromObject(obj); |
| 6960 | if (self == NULL) |
| 6961 | return NULL; |
| 6962 | str1 = PyUnicode_FromObject(subobj); |
| 6963 | if (str1 == NULL) { |
| 6964 | Py_DECREF(self); |
| 6965 | return NULL; |
| 6966 | } |
| 6967 | str2 = PyUnicode_FromObject(replobj); |
| 6968 | if (str2 == NULL) { |
| 6969 | Py_DECREF(self); |
| 6970 | Py_DECREF(str1); |
| 6971 | return NULL; |
| 6972 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6973 | result = replace((PyUnicodeObject *)self, |
| 6974 | (PyUnicodeObject *)str1, |
| 6975 | (PyUnicodeObject *)str2, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6976 | maxcount); |
| 6977 | Py_DECREF(self); |
| 6978 | Py_DECREF(str1); |
| 6979 | Py_DECREF(str2); |
| 6980 | return result; |
| 6981 | } |
| 6982 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6983 | PyDoc_STRVAR(replace__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6984 | "S.replace (old, new[, maxsplit]) -> unicode\n\ |
| 6985 | \n\ |
| 6986 | Return a copy of S with all occurrences of substring\n\ |
| 6987 | 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] | 6988 | given, only the first maxsplit occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6989 | |
| 6990 | static PyObject* |
| 6991 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 6992 | { |
| 6993 | PyUnicodeObject *str1; |
| 6994 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6995 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6996 | PyObject *result; |
| 6997 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6998 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6999 | return NULL; |
| 7000 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 7001 | if (str1 == NULL) |
| 7002 | return NULL; |
| 7003 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7004 | if (str2 == NULL) { |
| 7005 | Py_DECREF(str1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7006 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7007 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7008 | |
| 7009 | result = replace(self, str1, str2, maxcount); |
| 7010 | |
| 7011 | Py_DECREF(str1); |
| 7012 | Py_DECREF(str2); |
| 7013 | return result; |
| 7014 | } |
| 7015 | |
| 7016 | static |
| 7017 | PyObject *unicode_repr(PyObject *unicode) |
| 7018 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7019 | PyObject *repr; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7020 | Py_UNICODE *p; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7021 | Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); |
| 7022 | Py_ssize_t size = PyUnicode_GET_SIZE(unicode); |
| 7023 | |
| 7024 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 7025 | better to choose a different scheme. Perhaps scan the |
| 7026 | first N-chars of the string and allocate based on that size. |
| 7027 | */ |
| 7028 | /* Initial allocation is based on the longest-possible unichr |
| 7029 | escape. |
| 7030 | |
| 7031 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 7032 | unichr, so in this case it's the longest unichr escape. In |
| 7033 | narrow (UTF-16) builds this is five chars per source unichr |
| 7034 | since there are two unichrs in the surrogate pair, so in narrow |
| 7035 | (UTF-16) builds it's not the longest unichr escape. |
| 7036 | |
| 7037 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 7038 | so in the narrow (UTF-16) build case it's the longest unichr |
| 7039 | escape. |
| 7040 | */ |
| 7041 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7042 | repr = PyUnicode_FromUnicode(NULL, |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7043 | 2 /* quotes */ |
| 7044 | #ifdef Py_UNICODE_WIDE |
| 7045 | + 10*size |
| 7046 | #else |
| 7047 | + 6*size |
| 7048 | #endif |
| 7049 | + 1); |
| 7050 | if (repr == NULL) |
| 7051 | return NULL; |
| 7052 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7053 | p = PyUnicode_AS_UNICODE(repr); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7054 | |
| 7055 | /* Add quote */ |
| 7056 | *p++ = (findchar(s, size, '\'') && |
| 7057 | !findchar(s, size, '"')) ? '"' : '\''; |
| 7058 | while (size-- > 0) { |
| 7059 | Py_UNICODE ch = *s++; |
| 7060 | |
| 7061 | /* Escape quotes and backslashes */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7062 | if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7063 | *p++ = '\\'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7064 | *p++ = ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7065 | continue; |
| 7066 | } |
| 7067 | |
| 7068 | #ifdef Py_UNICODE_WIDE |
| 7069 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 7070 | else if (ch >= 0x10000) { |
| 7071 | *p++ = '\\'; |
| 7072 | *p++ = 'U'; |
| 7073 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 7074 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 7075 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 7076 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 7077 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 7078 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 7079 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 7080 | *p++ = hexdigits[ch & 0x0000000F]; |
| 7081 | continue; |
| 7082 | } |
| 7083 | #else |
| 7084 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 7085 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 7086 | Py_UNICODE ch2; |
| 7087 | Py_UCS4 ucs; |
| 7088 | |
| 7089 | ch2 = *s++; |
| 7090 | size--; |
| 7091 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 7092 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 7093 | *p++ = '\\'; |
| 7094 | *p++ = 'U'; |
| 7095 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 7096 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 7097 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 7098 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 7099 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 7100 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 7101 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 7102 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 7103 | continue; |
| 7104 | } |
| 7105 | /* Fall through: isolated surrogates are copied as-is */ |
| 7106 | s--; |
| 7107 | size++; |
| 7108 | } |
| 7109 | #endif |
| 7110 | |
| 7111 | /* Map 16-bit characters to '\uxxxx' */ |
| 7112 | if (ch >= 256) { |
| 7113 | *p++ = '\\'; |
| 7114 | *p++ = 'u'; |
| 7115 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 7116 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 7117 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7118 | *p++ = hexdigits[ch & 0x000F]; |
| 7119 | } |
| 7120 | |
| 7121 | /* Map special whitespace to '\t', \n', '\r' */ |
| 7122 | else if (ch == '\t') { |
| 7123 | *p++ = '\\'; |
| 7124 | *p++ = 't'; |
| 7125 | } |
| 7126 | else if (ch == '\n') { |
| 7127 | *p++ = '\\'; |
| 7128 | *p++ = 'n'; |
| 7129 | } |
| 7130 | else if (ch == '\r') { |
| 7131 | *p++ = '\\'; |
| 7132 | *p++ = 'r'; |
| 7133 | } |
| 7134 | |
| 7135 | /* Map non-printable US ASCII to '\xhh' */ |
| 7136 | else if (ch < ' ' || ch >= 0x7F) { |
| 7137 | *p++ = '\\'; |
| 7138 | *p++ = 'x'; |
| 7139 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7140 | *p++ = hexdigits[ch & 0x000F]; |
| 7141 | } |
| 7142 | |
| 7143 | /* Copy everything else as-is */ |
| 7144 | else |
| 7145 | *p++ = (char) ch; |
| 7146 | } |
| 7147 | /* Add quote */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7148 | *p++ = PyUnicode_AS_UNICODE(repr)[0]; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7149 | |
| 7150 | *p = '\0'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7151 | _PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7152 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7153 | } |
| 7154 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7155 | PyDoc_STRVAR(rfind__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7156 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 7157 | \n\ |
| 7158 | Return the highest index in S where substring sub is found,\n\ |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 7159 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7160 | arguments start and end are interpreted as in slice notation.\n\ |
| 7161 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7162 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7163 | |
| 7164 | static PyObject * |
| 7165 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 7166 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7167 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7168 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7169 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7170 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7171 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7172 | if (!PyArg_ParseTuple(args, "O|O&O&:rfind", &substring, |
| 7173 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7174 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7175 | substring = PyUnicode_FromObject(substring); |
| 7176 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7177 | return NULL; |
| 7178 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7179 | result = stringlib_rfind_slice( |
| 7180 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7181 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7182 | start, end |
| 7183 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7184 | |
| 7185 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7186 | |
| 7187 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7188 | } |
| 7189 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7190 | PyDoc_STRVAR(rindex__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7191 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 7192 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7193 | 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] | 7194 | |
| 7195 | static PyObject * |
| 7196 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 7197 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7198 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7199 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7200 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7201 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7202 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7203 | if (!PyArg_ParseTuple(args, "O|O&O&:rindex", &substring, |
| 7204 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7205 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7206 | substring = PyUnicode_FromObject(substring); |
| 7207 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7208 | return NULL; |
| 7209 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7210 | result = stringlib_rfind_slice( |
| 7211 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7212 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7213 | start, end |
| 7214 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7215 | |
| 7216 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7217 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7218 | if (result < 0) { |
| 7219 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7220 | return NULL; |
| 7221 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7222 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7223 | } |
| 7224 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7225 | PyDoc_STRVAR(rjust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7226 | "S.rjust(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7227 | \n\ |
| 7228 | 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] | 7229 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7230 | |
| 7231 | static PyObject * |
| 7232 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 7233 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7234 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7235 | Py_UNICODE fillchar = ' '; |
| 7236 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7237 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7238 | return NULL; |
| 7239 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7240 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7241 | Py_INCREF(self); |
| 7242 | return (PyObject*) self; |
| 7243 | } |
| 7244 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7245 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7246 | } |
| 7247 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7248 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7249 | 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] | 7250 | { |
| 7251 | /* standard clamping */ |
| 7252 | if (start < 0) |
| 7253 | start = 0; |
| 7254 | if (end < 0) |
| 7255 | end = 0; |
| 7256 | if (end > self->length) |
| 7257 | end = self->length; |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7258 | if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7259 | /* full slice, return original string */ |
| 7260 | Py_INCREF(self); |
| 7261 | return (PyObject*) self; |
| 7262 | } |
| 7263 | if (start > end) |
| 7264 | start = end; |
| 7265 | /* copy slice */ |
| 7266 | return (PyObject*) PyUnicode_FromUnicode(self->str + start, |
| 7267 | end - start); |
| 7268 | } |
| 7269 | |
| 7270 | PyObject *PyUnicode_Split(PyObject *s, |
| 7271 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7272 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7273 | { |
| 7274 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7275 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7276 | s = PyUnicode_FromObject(s); |
| 7277 | if (s == NULL) |
| 7278 | return NULL; |
| 7279 | if (sep != NULL) { |
| 7280 | sep = PyUnicode_FromObject(sep); |
| 7281 | if (sep == NULL) { |
| 7282 | Py_DECREF(s); |
| 7283 | return NULL; |
| 7284 | } |
| 7285 | } |
| 7286 | |
| 7287 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7288 | |
| 7289 | Py_DECREF(s); |
| 7290 | Py_XDECREF(sep); |
| 7291 | return result; |
| 7292 | } |
| 7293 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7294 | PyDoc_STRVAR(split__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7295 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 7296 | \n\ |
| 7297 | Return a list of the words in S, using sep as the\n\ |
| 7298 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Thomas Heller | ca0d2cb | 2004-09-15 11:41:32 +0000 | [diff] [blame] | 7299 | 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] | 7300 | any whitespace string is a separator."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7301 | |
| 7302 | static PyObject* |
| 7303 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 7304 | { |
| 7305 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7306 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7307 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7308 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7309 | return NULL; |
| 7310 | |
| 7311 | if (substring == Py_None) |
| 7312 | return split(self, NULL, maxcount); |
| 7313 | else if (PyUnicode_Check(substring)) |
| 7314 | return split(self, (PyUnicodeObject *)substring, maxcount); |
| 7315 | else |
| 7316 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
| 7317 | } |
| 7318 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7319 | PyObject * |
| 7320 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 7321 | { |
| 7322 | PyObject* str_obj; |
| 7323 | PyObject* sep_obj; |
| 7324 | PyObject* out; |
| 7325 | |
| 7326 | str_obj = PyUnicode_FromObject(str_in); |
| 7327 | if (!str_obj) |
| 7328 | return NULL; |
| 7329 | sep_obj = PyUnicode_FromObject(sep_in); |
| 7330 | if (!sep_obj) { |
| 7331 | Py_DECREF(str_obj); |
| 7332 | return NULL; |
| 7333 | } |
| 7334 | |
| 7335 | out = stringlib_partition( |
| 7336 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7337 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7338 | ); |
| 7339 | |
| 7340 | Py_DECREF(sep_obj); |
| 7341 | Py_DECREF(str_obj); |
| 7342 | |
| 7343 | return out; |
| 7344 | } |
| 7345 | |
| 7346 | |
| 7347 | PyObject * |
| 7348 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 7349 | { |
| 7350 | PyObject* str_obj; |
| 7351 | PyObject* sep_obj; |
| 7352 | PyObject* out; |
| 7353 | |
| 7354 | str_obj = PyUnicode_FromObject(str_in); |
| 7355 | if (!str_obj) |
| 7356 | return NULL; |
| 7357 | sep_obj = PyUnicode_FromObject(sep_in); |
| 7358 | if (!sep_obj) { |
| 7359 | Py_DECREF(str_obj); |
| 7360 | return NULL; |
| 7361 | } |
| 7362 | |
| 7363 | out = stringlib_rpartition( |
| 7364 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7365 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7366 | ); |
| 7367 | |
| 7368 | Py_DECREF(sep_obj); |
| 7369 | Py_DECREF(str_obj); |
| 7370 | |
| 7371 | return out; |
| 7372 | } |
| 7373 | |
| 7374 | PyDoc_STRVAR(partition__doc__, |
| 7375 | "S.partition(sep) -> (head, sep, tail)\n\ |
| 7376 | \n\ |
| 7377 | Searches for the separator sep in S, and returns the part before it,\n\ |
| 7378 | the separator itself, and the part after it. If the separator is not\n\ |
| 7379 | found, returns S and two empty strings."); |
| 7380 | |
| 7381 | static PyObject* |
| 7382 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 7383 | { |
| 7384 | return PyUnicode_Partition((PyObject *)self, separator); |
| 7385 | } |
| 7386 | |
| 7387 | PyDoc_STRVAR(rpartition__doc__, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7388 | "S.rpartition(sep) -> (tail, sep, head)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7389 | \n\ |
| 7390 | Searches for the separator sep in S, starting at the end of S, and returns\n\ |
| 7391 | 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] | 7392 | separator is not found, returns two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7393 | |
| 7394 | static PyObject* |
| 7395 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 7396 | { |
| 7397 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 7398 | } |
| 7399 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7400 | PyObject *PyUnicode_RSplit(PyObject *s, |
| 7401 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7402 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7403 | { |
| 7404 | PyObject *result; |
| 7405 | |
| 7406 | s = PyUnicode_FromObject(s); |
| 7407 | if (s == NULL) |
| 7408 | return NULL; |
| 7409 | if (sep != NULL) { |
| 7410 | sep = PyUnicode_FromObject(sep); |
| 7411 | if (sep == NULL) { |
| 7412 | Py_DECREF(s); |
| 7413 | return NULL; |
| 7414 | } |
| 7415 | } |
| 7416 | |
| 7417 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7418 | |
| 7419 | Py_DECREF(s); |
| 7420 | Py_XDECREF(sep); |
| 7421 | return result; |
| 7422 | } |
| 7423 | |
| 7424 | PyDoc_STRVAR(rsplit__doc__, |
| 7425 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
| 7426 | \n\ |
| 7427 | Return a list of the words in S, using sep as the\n\ |
| 7428 | delimiter string, starting at the end of the string and\n\ |
| 7429 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 7430 | splits are done. If sep is not specified, any whitespace string\n\ |
| 7431 | is a separator."); |
| 7432 | |
| 7433 | static PyObject* |
| 7434 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 7435 | { |
| 7436 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7437 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7438 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7439 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7440 | return NULL; |
| 7441 | |
| 7442 | if (substring == Py_None) |
| 7443 | return rsplit(self, NULL, maxcount); |
| 7444 | else if (PyUnicode_Check(substring)) |
| 7445 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
| 7446 | else |
| 7447 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
| 7448 | } |
| 7449 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7450 | PyDoc_STRVAR(splitlines__doc__, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7451 | "S.splitlines([keepends]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7452 | \n\ |
| 7453 | 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] | 7454 | 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] | 7455 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7456 | |
| 7457 | static PyObject* |
| 7458 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 7459 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7460 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7461 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7462 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7463 | return NULL; |
| 7464 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7465 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7466 | } |
| 7467 | |
| 7468 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 7469 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7470 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 7471 | if (PyUnicode_CheckExact(self)) { |
| 7472 | Py_INCREF(self); |
| 7473 | return self; |
| 7474 | } else |
| 7475 | /* Subtype -- return genuine unicode string with the same value. */ |
| 7476 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), |
| 7477 | PyUnicode_GET_SIZE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7478 | } |
| 7479 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7480 | PyDoc_STRVAR(swapcase__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7481 | "S.swapcase() -> unicode\n\ |
| 7482 | \n\ |
| 7483 | 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] | 7484 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7485 | |
| 7486 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7487 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7488 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7489 | return fixup(self, fixswapcase); |
| 7490 | } |
| 7491 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7492 | PyDoc_STRVAR(translate__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7493 | "S.translate(table) -> unicode\n\ |
| 7494 | \n\ |
| 7495 | Return a copy of the string S, where all characters have been mapped\n\ |
| 7496 | 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] | 7497 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ |
| 7498 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 7499 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7500 | |
| 7501 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7502 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7503 | { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7504 | return PyUnicode_TranslateCharmap(self->str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7505 | self->length, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7506 | table, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7507 | "ignore"); |
| 7508 | } |
| 7509 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7510 | PyDoc_STRVAR(upper__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7511 | "S.upper() -> unicode\n\ |
| 7512 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7513 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7514 | |
| 7515 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7516 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7517 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7518 | return fixup(self, fixupper); |
| 7519 | } |
| 7520 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7521 | PyDoc_STRVAR(zfill__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7522 | "S.zfill(width) -> unicode\n\ |
| 7523 | \n\ |
| 7524 | 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] | 7525 | of the specified width. The string x is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7526 | |
| 7527 | static PyObject * |
| 7528 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 7529 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7530 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7531 | PyUnicodeObject *u; |
| 7532 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7533 | Py_ssize_t width; |
| 7534 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7535 | return NULL; |
| 7536 | |
| 7537 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 7538 | if (PyUnicode_CheckExact(self)) { |
| 7539 | Py_INCREF(self); |
| 7540 | return (PyObject*) self; |
| 7541 | } |
| 7542 | else |
| 7543 | return PyUnicode_FromUnicode( |
| 7544 | PyUnicode_AS_UNICODE(self), |
| 7545 | PyUnicode_GET_SIZE(self) |
| 7546 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7547 | } |
| 7548 | |
| 7549 | fill = width - self->length; |
| 7550 | |
| 7551 | u = pad(self, fill, 0, '0'); |
| 7552 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 7553 | if (u == NULL) |
| 7554 | return NULL; |
| 7555 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7556 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 7557 | /* move sign to beginning of string */ |
| 7558 | u->str[0] = u->str[fill]; |
| 7559 | u->str[fill] = '0'; |
| 7560 | } |
| 7561 | |
| 7562 | return (PyObject*) u; |
| 7563 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7564 | |
| 7565 | #if 0 |
| 7566 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7567 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7568 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7569 | return PyInt_FromLong(unicode_freelist_size); |
| 7570 | } |
| 7571 | #endif |
| 7572 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7573 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7574 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7575 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7576 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 7577 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7578 | With optional end, stop comparing S at that position.\n\ |
| 7579 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7580 | |
| 7581 | static PyObject * |
| 7582 | unicode_startswith(PyUnicodeObject *self, |
| 7583 | PyObject *args) |
| 7584 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7585 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7586 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7587 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7588 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7589 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7590 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7591 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7592 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7593 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7594 | if (PyTuple_Check(subobj)) { |
| 7595 | Py_ssize_t i; |
| 7596 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 7597 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 7598 | PyTuple_GET_ITEM(subobj, i)); |
| 7599 | if (substring == NULL) |
| 7600 | return NULL; |
| 7601 | result = tailmatch(self, substring, start, end, -1); |
| 7602 | Py_DECREF(substring); |
| 7603 | if (result) { |
| 7604 | Py_RETURN_TRUE; |
| 7605 | } |
| 7606 | } |
| 7607 | /* nothing matched */ |
| 7608 | Py_RETURN_FALSE; |
| 7609 | } |
| 7610 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7611 | if (substring == NULL) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7612 | return NULL; |
| 7613 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7614 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7615 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7616 | } |
| 7617 | |
| 7618 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7619 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7620 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7621 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7622 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 7623 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7624 | With optional end, stop comparing S at that position.\n\ |
| 7625 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7626 | |
| 7627 | static PyObject * |
| 7628 | unicode_endswith(PyUnicodeObject *self, |
| 7629 | PyObject *args) |
| 7630 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7631 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7632 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7633 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7634 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7635 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7636 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7637 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 7638 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7639 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7640 | if (PyTuple_Check(subobj)) { |
| 7641 | Py_ssize_t i; |
| 7642 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 7643 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 7644 | PyTuple_GET_ITEM(subobj, i)); |
| 7645 | if (substring == NULL) |
| 7646 | return NULL; |
| 7647 | result = tailmatch(self, substring, start, end, +1); |
| 7648 | Py_DECREF(substring); |
| 7649 | if (result) { |
| 7650 | Py_RETURN_TRUE; |
| 7651 | } |
| 7652 | } |
| 7653 | Py_RETURN_FALSE; |
| 7654 | } |
| 7655 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7656 | if (substring == NULL) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7657 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7658 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7659 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7660 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7661 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7662 | } |
| 7663 | |
| 7664 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 7665 | |
| 7666 | static PyObject * |
| 7667 | unicode_getnewargs(PyUnicodeObject *v) |
| 7668 | { |
| 7669 | return Py_BuildValue("(u#)", v->str, v->length); |
| 7670 | } |
| 7671 | |
| 7672 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7673 | static PyMethodDef unicode_methods[] = { |
| 7674 | |
| 7675 | /* Order is according to common usage: often used methods should |
| 7676 | appear first, since lookup is done sequentially. */ |
| 7677 | |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7678 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
| 7679 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 7680 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7681 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7682 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 7683 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 7684 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 7685 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 7686 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 7687 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 7688 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7689 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7690 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 7691 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 7692 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7693 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7694 | {"decode", (PyCFunction) unicode_decode, METH_VARARGS, decode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7695 | /* {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */ |
| 7696 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 7697 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 7698 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7699 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7700 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7701 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7702 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7703 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 7704 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 7705 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 7706 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 7707 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 7708 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 7709 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 7710 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 7711 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 7712 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 7713 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 7714 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 7715 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 7716 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7717 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 7718 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7719 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7720 | #endif |
| 7721 | |
| 7722 | #if 0 |
| 7723 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7724 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7725 | #endif |
| 7726 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 7727 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7728 | {NULL, NULL} |
| 7729 | }; |
| 7730 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7731 | static PyObject * |
| 7732 | unicode_mod(PyObject *v, PyObject *w) |
| 7733 | { |
| 7734 | if (!PyUnicode_Check(v)) { |
| 7735 | Py_INCREF(Py_NotImplemented); |
| 7736 | return Py_NotImplemented; |
| 7737 | } |
| 7738 | return PyUnicode_Format(v, w); |
| 7739 | } |
| 7740 | |
| 7741 | static PyNumberMethods unicode_as_number = { |
| 7742 | 0, /*nb_add*/ |
| 7743 | 0, /*nb_subtract*/ |
| 7744 | 0, /*nb_multiply*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7745 | unicode_mod, /*nb_remainder*/ |
| 7746 | }; |
| 7747 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7748 | static PySequenceMethods unicode_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7749 | (lenfunc) unicode_length, /* sq_length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7750 | PyUnicode_Concat, /* sq_concat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7751 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 7752 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 7753 | (ssizessizeargfunc) unicode_slice, /* sq_slice */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7754 | 0, /* sq_ass_item */ |
| 7755 | 0, /* sq_ass_slice */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7756 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7757 | }; |
| 7758 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7759 | static PyObject* |
| 7760 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 7761 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7762 | if (PyIndex_Check(item)) { |
| 7763 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7764 | if (i == -1 && PyErr_Occurred()) |
| 7765 | return NULL; |
| 7766 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7767 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7768 | return unicode_getitem(self, i); |
| 7769 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7770 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7771 | Py_UNICODE* source_buf; |
| 7772 | Py_UNICODE* result_buf; |
| 7773 | PyObject* result; |
| 7774 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7775 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7776 | &start, &stop, &step, &slicelength) < 0) { |
| 7777 | return NULL; |
| 7778 | } |
| 7779 | |
| 7780 | if (slicelength <= 0) { |
| 7781 | return PyUnicode_FromUnicode(NULL, 0); |
| 7782 | } else { |
| 7783 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7784 | result_buf = (Py_UNICODE *)PyMem_MALLOC(slicelength* |
| 7785 | sizeof(Py_UNICODE)); |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7786 | |
| 7787 | if (result_buf == NULL) |
| 7788 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7789 | |
| 7790 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 7791 | result_buf[i] = source_buf[cur]; |
| 7792 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7793 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7794 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
| 7795 | PyMem_FREE(result_buf); |
| 7796 | return result; |
| 7797 | } |
| 7798 | } else { |
| 7799 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 7800 | return NULL; |
| 7801 | } |
| 7802 | } |
| 7803 | |
| 7804 | static PyMappingMethods unicode_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7805 | (lenfunc)unicode_length, /* mp_length */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7806 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 7807 | (objobjargproc)0, /* mp_ass_subscript */ |
| 7808 | }; |
| 7809 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7810 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7811 | unicode_buffer_getreadbuf(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7812 | Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7813 | const void **ptr) |
| 7814 | { |
| 7815 | if (index != 0) { |
| 7816 | PyErr_SetString(PyExc_SystemError, |
| 7817 | "accessing non-existent unicode segment"); |
| 7818 | return -1; |
| 7819 | } |
| 7820 | *ptr = (void *) self->str; |
| 7821 | return PyUnicode_GET_DATA_SIZE(self); |
| 7822 | } |
| 7823 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7824 | static Py_ssize_t |
| 7825 | unicode_buffer_getwritebuf(PyUnicodeObject *self, Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7826 | const void **ptr) |
| 7827 | { |
| 7828 | PyErr_SetString(PyExc_TypeError, |
Neal Norwitz | 20e7213 | 2002-06-13 21:25:17 +0000 | [diff] [blame] | 7829 | "cannot use unicode as modifiable buffer"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7830 | return -1; |
| 7831 | } |
| 7832 | |
| 7833 | static int |
| 7834 | unicode_buffer_getsegcount(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7835 | Py_ssize_t *lenp) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7836 | { |
| 7837 | if (lenp) |
| 7838 | *lenp = PyUnicode_GET_DATA_SIZE(self); |
| 7839 | return 1; |
| 7840 | } |
| 7841 | |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 7842 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7843 | unicode_buffer_getcharbuf(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7844 | Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7845 | const void **ptr) |
| 7846 | { |
| 7847 | PyObject *str; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7848 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7849 | if (index != 0) { |
| 7850 | PyErr_SetString(PyExc_SystemError, |
| 7851 | "accessing non-existent unicode segment"); |
| 7852 | return -1; |
| 7853 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 7854 | str = _PyUnicode_AsDefaultEncodedString((PyObject *)self, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7855 | if (str == NULL) |
| 7856 | return -1; |
| 7857 | *ptr = (void *) PyString_AS_STRING(str); |
| 7858 | return PyString_GET_SIZE(str); |
| 7859 | } |
| 7860 | |
| 7861 | /* Helpers for PyUnicode_Format() */ |
| 7862 | |
| 7863 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7864 | 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] | 7865 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7866 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7867 | if (argidx < arglen) { |
| 7868 | (*p_argidx)++; |
| 7869 | if (arglen < 0) |
| 7870 | return args; |
| 7871 | else |
| 7872 | return PyTuple_GetItem(args, argidx); |
| 7873 | } |
| 7874 | PyErr_SetString(PyExc_TypeError, |
| 7875 | "not enough arguments for format string"); |
| 7876 | return NULL; |
| 7877 | } |
| 7878 | |
| 7879 | #define F_LJUST (1<<0) |
| 7880 | #define F_SIGN (1<<1) |
| 7881 | #define F_BLANK (1<<2) |
| 7882 | #define F_ALT (1<<3) |
| 7883 | #define F_ZERO (1<<4) |
| 7884 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7885 | static Py_ssize_t |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7886 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7887 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7888 | register Py_ssize_t i; |
| 7889 | Py_ssize_t len = strlen(charbuffer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7890 | for (i = len - 1; i >= 0; i--) |
| 7891 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
| 7892 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7893 | return len; |
| 7894 | } |
| 7895 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7896 | static int |
| 7897 | doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) |
| 7898 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 7899 | Py_ssize_t result; |
| 7900 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7901 | PyOS_ascii_formatd((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 7902 | result = strtounicode(buffer, (char *)buffer); |
| 7903 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7904 | } |
| 7905 | |
| 7906 | static int |
| 7907 | longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) |
| 7908 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 7909 | Py_ssize_t result; |
| 7910 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7911 | PyOS_snprintf((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 7912 | result = strtounicode(buffer, (char *)buffer); |
| 7913 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7914 | } |
| 7915 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 7916 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 7917 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 7918 | formatting is done. */ |
| 7919 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7920 | static int |
| 7921 | formatfloat(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7922 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7923 | int flags, |
| 7924 | int prec, |
| 7925 | int type, |
| 7926 | PyObject *v) |
| 7927 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7928 | /* fmt = '%#.' + `prec` + `type` |
| 7929 | 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] | 7930 | char fmt[20]; |
| 7931 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7932 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7933 | x = PyFloat_AsDouble(v); |
| 7934 | if (x == -1.0 && PyErr_Occurred()) |
| 7935 | return -1; |
| 7936 | if (prec < 0) |
| 7937 | prec = 6; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7938 | if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
| 7939 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7940 | /* Worst case length calc to ensure no buffer overrun: |
| 7941 | |
| 7942 | 'g' formats: |
| 7943 | fmt = %#.<prec>g |
| 7944 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 7945 | for any double rep.) |
| 7946 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 7947 | |
| 7948 | 'f' formats: |
| 7949 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 7950 | len = 1 + 50 + 1 + prec = 52 + prec |
| 7951 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7952 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7953 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7954 | |
| 7955 | */ |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 7956 | if (((type == 'g' || type == 'G') && |
| 7957 | buflen <= (size_t)10 + (size_t)prec) || |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7958 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7959 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7960 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7961 | return -1; |
| 7962 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7963 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 7964 | (flags&F_ALT) ? "#" : "", |
| 7965 | prec, type); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7966 | return doubletounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7967 | } |
| 7968 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7969 | static PyObject* |
| 7970 | formatlong(PyObject *val, int flags, int prec, int type) |
| 7971 | { |
| 7972 | char *buf; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 7973 | int len; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7974 | PyObject *str; /* temporary string object. */ |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 7975 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7976 | |
| 7977 | str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
| 7978 | if (!str) |
| 7979 | return NULL; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 7980 | result = PyUnicode_FromStringAndSize(buf, len); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7981 | Py_DECREF(str); |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 7982 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7983 | } |
| 7984 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7985 | static int |
| 7986 | formatint(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7987 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7988 | int flags, |
| 7989 | int prec, |
| 7990 | int type, |
| 7991 | PyObject *v) |
| 7992 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7993 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7994 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 7995 | * + 1 + 1 |
| 7996 | * = 24 |
| 7997 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7998 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7999 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8000 | long x; |
| 8001 | |
| 8002 | x = PyInt_AsLong(v); |
| 8003 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8004 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8005 | if (x < 0 && type == 'u') { |
| 8006 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8007 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8008 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 8009 | sign = "-"; |
| 8010 | else |
| 8011 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8012 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8013 | prec = 1; |
| 8014 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8015 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 8016 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8017 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8018 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8019 | PyErr_SetString(PyExc_OverflowError, |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8020 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8021 | return -1; |
| 8022 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8023 | |
| 8024 | if ((flags & F_ALT) && |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8025 | (type == 'x' || type == 'X' || type == 'o')) { |
| 8026 | /* When converting under %#o, %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8027 | * of issues that cause pain: |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8028 | * - for %#o, we want a different base marker than C |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8029 | * - when 0 is being converted, the C standard leaves off |
| 8030 | * the '0x' or '0X', which is inconsistent with other |
| 8031 | * %#x/%#X conversions and inconsistent with Python's |
| 8032 | * hex() function |
| 8033 | * - there are platforms that violate the standard and |
| 8034 | * convert 0 with the '0x' or '0X' |
| 8035 | * (Metrowerks, Compaq Tru64) |
| 8036 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8037 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8038 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8039 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8040 | * We can achieve the desired consistency by inserting our |
| 8041 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 8042 | * of %#x/%#X. |
| 8043 | * |
| 8044 | * Note that this is the same approach as used in |
| 8045 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8046 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8047 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 8048 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8049 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8050 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8051 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 8052 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8053 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 8054 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8055 | if (sign[0]) |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8056 | return longtounicode(buf, buflen, fmt, -x); |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8057 | else |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8058 | return longtounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8059 | } |
| 8060 | |
| 8061 | static int |
| 8062 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8063 | size_t buflen, |
| 8064 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8065 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8066 | /* presume that the buffer is at least 2 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8067 | if (PyUnicode_Check(v)) { |
| 8068 | if (PyUnicode_GET_SIZE(v) != 1) |
| 8069 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8070 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8071 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8072 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8073 | else if (PyString_Check(v)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8074 | if (PyString_GET_SIZE(v) != 1) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8075 | goto onError; |
| 8076 | buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0]; |
| 8077 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8078 | |
| 8079 | else { |
| 8080 | /* Integer input truncated to a character */ |
| 8081 | long x; |
| 8082 | x = PyInt_AsLong(v); |
| 8083 | if (x == -1 && PyErr_Occurred()) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8084 | goto onError; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8085 | #ifdef Py_UNICODE_WIDE |
| 8086 | if (x < 0 || x > 0x10ffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 8087 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8088 | "%c arg not in range(0x110000) " |
| 8089 | "(wide Python build)"); |
| 8090 | return -1; |
| 8091 | } |
| 8092 | #else |
| 8093 | if (x < 0 || x > 0xffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 8094 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8095 | "%c arg not in range(0x10000) " |
| 8096 | "(narrow Python build)"); |
| 8097 | return -1; |
| 8098 | } |
| 8099 | #endif |
| 8100 | buf[0] = (Py_UNICODE) x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8101 | } |
| 8102 | buf[1] = '\0'; |
| 8103 | return 1; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8104 | |
| 8105 | onError: |
| 8106 | PyErr_SetString(PyExc_TypeError, |
| 8107 | "%c requires int or char"); |
| 8108 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8109 | } |
| 8110 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8111 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 8112 | |
| 8113 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 8114 | chars are formatted. XXX This is a magic number. Each formatting |
| 8115 | routine does bounds checking to ensure no overflow, but a better |
| 8116 | solution may be to malloc a buffer of appropriate size for each |
| 8117 | format. For now, the current solution is sufficient. |
| 8118 | */ |
| 8119 | #define FORMATBUFLEN (size_t)120 |
| 8120 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8121 | PyObject *PyUnicode_Format(PyObject *format, |
| 8122 | PyObject *args) |
| 8123 | { |
| 8124 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8125 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8126 | int args_owned = 0; |
| 8127 | PyUnicodeObject *result = NULL; |
| 8128 | PyObject *dict = NULL; |
| 8129 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8130 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8131 | if (format == NULL || args == NULL) { |
| 8132 | PyErr_BadInternalCall(); |
| 8133 | return NULL; |
| 8134 | } |
| 8135 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8136 | if (uformat == NULL) |
| 8137 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8138 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 8139 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 8140 | |
| 8141 | reslen = rescnt = fmtcnt + 100; |
| 8142 | result = _PyUnicode_New(reslen); |
| 8143 | if (result == NULL) |
| 8144 | goto onError; |
| 8145 | res = PyUnicode_AS_UNICODE(result); |
| 8146 | |
| 8147 | if (PyTuple_Check(args)) { |
| 8148 | arglen = PyTuple_Size(args); |
| 8149 | argidx = 0; |
| 8150 | } |
| 8151 | else { |
| 8152 | arglen = -1; |
| 8153 | argidx = -2; |
| 8154 | } |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 8155 | if (Py_Type(args)->tp_as_mapping && !PyTuple_Check(args) && |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 8156 | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8157 | dict = args; |
| 8158 | |
| 8159 | while (--fmtcnt >= 0) { |
| 8160 | if (*fmt != '%') { |
| 8161 | if (--rescnt < 0) { |
| 8162 | rescnt = fmtcnt + 100; |
| 8163 | reslen += rescnt; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8164 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8165 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8166 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 8167 | --rescnt; |
| 8168 | } |
| 8169 | *res++ = *fmt++; |
| 8170 | } |
| 8171 | else { |
| 8172 | /* Got a format specifier */ |
| 8173 | int flags = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8174 | Py_ssize_t width = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8175 | int prec = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8176 | Py_UNICODE c = '\0'; |
| 8177 | Py_UNICODE fill; |
| 8178 | PyObject *v = NULL; |
| 8179 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8180 | Py_UNICODE *pbuf; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8181 | Py_UNICODE sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8182 | Py_ssize_t len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8183 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8184 | |
| 8185 | fmt++; |
| 8186 | if (*fmt == '(') { |
| 8187 | Py_UNICODE *keystart; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8188 | Py_ssize_t keylen; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8189 | PyObject *key; |
| 8190 | int pcount = 1; |
| 8191 | |
| 8192 | if (dict == NULL) { |
| 8193 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8194 | "format requires a mapping"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8195 | goto onError; |
| 8196 | } |
| 8197 | ++fmt; |
| 8198 | --fmtcnt; |
| 8199 | keystart = fmt; |
| 8200 | /* Skip over balanced parentheses */ |
| 8201 | while (pcount > 0 && --fmtcnt >= 0) { |
| 8202 | if (*fmt == ')') |
| 8203 | --pcount; |
| 8204 | else if (*fmt == '(') |
| 8205 | ++pcount; |
| 8206 | fmt++; |
| 8207 | } |
| 8208 | keylen = fmt - keystart - 1; |
| 8209 | if (fmtcnt < 0 || pcount > 0) { |
| 8210 | PyErr_SetString(PyExc_ValueError, |
| 8211 | "incomplete format key"); |
| 8212 | goto onError; |
| 8213 | } |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 8214 | #if 0 |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8215 | /* keys are converted to strings using UTF-8 and |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8216 | then looked up since Python uses strings to hold |
| 8217 | variables names etc. in its namespaces and we |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8218 | wouldn't want to break common idioms. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8219 | key = PyUnicode_EncodeUTF8(keystart, |
| 8220 | keylen, |
| 8221 | NULL); |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 8222 | #else |
| 8223 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 8224 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8225 | if (key == NULL) |
| 8226 | goto onError; |
| 8227 | if (args_owned) { |
| 8228 | Py_DECREF(args); |
| 8229 | args_owned = 0; |
| 8230 | } |
| 8231 | args = PyObject_GetItem(dict, key); |
| 8232 | Py_DECREF(key); |
| 8233 | if (args == NULL) { |
| 8234 | goto onError; |
| 8235 | } |
| 8236 | args_owned = 1; |
| 8237 | arglen = -1; |
| 8238 | argidx = -2; |
| 8239 | } |
| 8240 | while (--fmtcnt >= 0) { |
| 8241 | switch (c = *fmt++) { |
| 8242 | case '-': flags |= F_LJUST; continue; |
| 8243 | case '+': flags |= F_SIGN; continue; |
| 8244 | case ' ': flags |= F_BLANK; continue; |
| 8245 | case '#': flags |= F_ALT; continue; |
| 8246 | case '0': flags |= F_ZERO; continue; |
| 8247 | } |
| 8248 | break; |
| 8249 | } |
| 8250 | if (c == '*') { |
| 8251 | v = getnextarg(args, arglen, &argidx); |
| 8252 | if (v == NULL) |
| 8253 | goto onError; |
| 8254 | if (!PyInt_Check(v)) { |
| 8255 | PyErr_SetString(PyExc_TypeError, |
| 8256 | "* wants int"); |
| 8257 | goto onError; |
| 8258 | } |
| 8259 | width = PyInt_AsLong(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 8260 | if (width == -1 && PyErr_Occurred()) |
| 8261 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8262 | if (width < 0) { |
| 8263 | flags |= F_LJUST; |
| 8264 | width = -width; |
| 8265 | } |
| 8266 | if (--fmtcnt >= 0) |
| 8267 | c = *fmt++; |
| 8268 | } |
| 8269 | else if (c >= '0' && c <= '9') { |
| 8270 | width = c - '0'; |
| 8271 | while (--fmtcnt >= 0) { |
| 8272 | c = *fmt++; |
| 8273 | if (c < '0' || c > '9') |
| 8274 | break; |
| 8275 | if ((width*10) / 10 != width) { |
| 8276 | PyErr_SetString(PyExc_ValueError, |
| 8277 | "width too big"); |
| 8278 | goto onError; |
| 8279 | } |
| 8280 | width = width*10 + (c - '0'); |
| 8281 | } |
| 8282 | } |
| 8283 | if (c == '.') { |
| 8284 | prec = 0; |
| 8285 | if (--fmtcnt >= 0) |
| 8286 | c = *fmt++; |
| 8287 | if (c == '*') { |
| 8288 | v = getnextarg(args, arglen, &argidx); |
| 8289 | if (v == NULL) |
| 8290 | goto onError; |
| 8291 | if (!PyInt_Check(v)) { |
| 8292 | PyErr_SetString(PyExc_TypeError, |
| 8293 | "* wants int"); |
| 8294 | goto onError; |
| 8295 | } |
| 8296 | prec = PyInt_AsLong(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 8297 | if (prec == -1 && PyErr_Occurred()) |
| 8298 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8299 | if (prec < 0) |
| 8300 | prec = 0; |
| 8301 | if (--fmtcnt >= 0) |
| 8302 | c = *fmt++; |
| 8303 | } |
| 8304 | else if (c >= '0' && c <= '9') { |
| 8305 | prec = c - '0'; |
| 8306 | while (--fmtcnt >= 0) { |
| 8307 | c = Py_CHARMASK(*fmt++); |
| 8308 | if (c < '0' || c > '9') |
| 8309 | break; |
| 8310 | if ((prec*10) / 10 != prec) { |
| 8311 | PyErr_SetString(PyExc_ValueError, |
| 8312 | "prec too big"); |
| 8313 | goto onError; |
| 8314 | } |
| 8315 | prec = prec*10 + (c - '0'); |
| 8316 | } |
| 8317 | } |
| 8318 | } /* prec */ |
| 8319 | if (fmtcnt >= 0) { |
| 8320 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8321 | if (--fmtcnt >= 0) |
| 8322 | c = *fmt++; |
| 8323 | } |
| 8324 | } |
| 8325 | if (fmtcnt < 0) { |
| 8326 | PyErr_SetString(PyExc_ValueError, |
| 8327 | "incomplete format"); |
| 8328 | goto onError; |
| 8329 | } |
| 8330 | if (c != '%') { |
| 8331 | v = getnextarg(args, arglen, &argidx); |
| 8332 | if (v == NULL) |
| 8333 | goto onError; |
| 8334 | } |
| 8335 | sign = 0; |
| 8336 | fill = ' '; |
| 8337 | switch (c) { |
| 8338 | |
| 8339 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8340 | pbuf = formatbuf; |
| 8341 | /* presume that buffer length is at least 1 */ |
| 8342 | pbuf[0] = '%'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8343 | len = 1; |
| 8344 | break; |
| 8345 | |
| 8346 | case 's': |
| 8347 | case 'r': |
| 8348 | if (PyUnicode_Check(v) && c == 's') { |
| 8349 | temp = v; |
| 8350 | Py_INCREF(temp); |
| 8351 | } |
| 8352 | else { |
| 8353 | PyObject *unicode; |
| 8354 | if (c == 's') |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8355 | temp = PyObject_Unicode(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8356 | else |
| 8357 | temp = PyObject_Repr(v); |
| 8358 | if (temp == NULL) |
| 8359 | goto onError; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8360 | if (PyUnicode_Check(temp)) |
| 8361 | /* nothing to do */; |
| 8362 | else if (PyString_Check(temp)) { |
| 8363 | /* convert to string to Unicode */ |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8364 | unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8365 | PyString_GET_SIZE(temp), |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8366 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8367 | "strict"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8368 | Py_DECREF(temp); |
| 8369 | temp = unicode; |
| 8370 | if (temp == NULL) |
| 8371 | goto onError; |
| 8372 | } |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8373 | else { |
| 8374 | Py_DECREF(temp); |
| 8375 | PyErr_SetString(PyExc_TypeError, |
| 8376 | "%s argument has non-string str()"); |
| 8377 | goto onError; |
| 8378 | } |
| 8379 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8380 | pbuf = PyUnicode_AS_UNICODE(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8381 | len = PyUnicode_GET_SIZE(temp); |
| 8382 | if (prec >= 0 && len > prec) |
| 8383 | len = prec; |
| 8384 | break; |
| 8385 | |
| 8386 | case 'i': |
| 8387 | case 'd': |
| 8388 | case 'u': |
| 8389 | case 'o': |
| 8390 | case 'x': |
| 8391 | case 'X': |
| 8392 | if (c == 'i') |
| 8393 | c = 'd'; |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 8394 | if (PyLong_Check(v)) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8395 | temp = formatlong(v, flags, prec, c); |
| 8396 | if (!temp) |
| 8397 | goto onError; |
| 8398 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 8399 | len = PyUnicode_GET_SIZE(temp); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8400 | sign = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8401 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8402 | else { |
| 8403 | pbuf = formatbuf; |
| 8404 | len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8405 | flags, prec, c, v); |
| 8406 | if (len < 0) |
| 8407 | goto onError; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8408 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8409 | } |
| 8410 | if (flags & F_ZERO) |
| 8411 | fill = '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8412 | break; |
| 8413 | |
| 8414 | case 'e': |
| 8415 | case 'E': |
| 8416 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 8417 | case 'F': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8418 | case 'g': |
| 8419 | case 'G': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 8420 | if (c == 'F') |
| 8421 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8422 | pbuf = formatbuf; |
| 8423 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8424 | flags, prec, c, v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8425 | if (len < 0) |
| 8426 | goto onError; |
| 8427 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8428 | if (flags & F_ZERO) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8429 | fill = '0'; |
| 8430 | break; |
| 8431 | |
| 8432 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8433 | pbuf = formatbuf; |
| 8434 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8435 | if (len < 0) |
| 8436 | goto onError; |
| 8437 | break; |
| 8438 | |
| 8439 | default: |
| 8440 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 8441 | "unsupported format character '%c' (0x%x) " |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 8442 | "at index %zd", |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8443 | (31<=c && c<=126) ? (char)c : '?', |
Marc-André Lemburg | 24e53b6 | 2002-09-24 09:32:14 +0000 | [diff] [blame] | 8444 | (int)c, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 8445 | (Py_ssize_t)(fmt - 1 - |
| 8446 | PyUnicode_AS_UNICODE(uformat))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8447 | goto onError; |
| 8448 | } |
| 8449 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8450 | if (*pbuf == '-' || *pbuf == '+') { |
| 8451 | sign = *pbuf++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8452 | len--; |
| 8453 | } |
| 8454 | else if (flags & F_SIGN) |
| 8455 | sign = '+'; |
| 8456 | else if (flags & F_BLANK) |
| 8457 | sign = ' '; |
| 8458 | else |
| 8459 | sign = 0; |
| 8460 | } |
| 8461 | if (width < len) |
| 8462 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8463 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8464 | reslen -= rescnt; |
| 8465 | rescnt = width + fmtcnt + 100; |
| 8466 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8467 | if (reslen < 0) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8468 | Py_XDECREF(temp); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8469 | PyErr_NoMemory(); |
| 8470 | goto onError; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8471 | } |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8472 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 8473 | Py_XDECREF(temp); |
| 8474 | goto onError; |
| 8475 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8476 | res = PyUnicode_AS_UNICODE(result) |
| 8477 | + reslen - rescnt; |
| 8478 | } |
| 8479 | if (sign) { |
| 8480 | if (fill != ' ') |
| 8481 | *res++ = sign; |
| 8482 | rescnt--; |
| 8483 | if (width > len) |
| 8484 | width--; |
| 8485 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8486 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8487 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8488 | assert(pbuf[1] == c); |
| 8489 | if (fill != ' ') { |
| 8490 | *res++ = *pbuf++; |
| 8491 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8492 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8493 | rescnt -= 2; |
| 8494 | width -= 2; |
| 8495 | if (width < 0) |
| 8496 | width = 0; |
| 8497 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8498 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8499 | if (width > len && !(flags & F_LJUST)) { |
| 8500 | do { |
| 8501 | --rescnt; |
| 8502 | *res++ = fill; |
| 8503 | } while (--width > len); |
| 8504 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8505 | if (fill == ' ') { |
| 8506 | if (sign) |
| 8507 | *res++ = sign; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8508 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8509 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8510 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8511 | *res++ = *pbuf++; |
| 8512 | *res++ = *pbuf++; |
| 8513 | } |
| 8514 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8515 | Py_UNICODE_COPY(res, pbuf, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8516 | res += len; |
| 8517 | rescnt -= len; |
| 8518 | while (--width >= len) { |
| 8519 | --rescnt; |
| 8520 | *res++ = ' '; |
| 8521 | } |
| 8522 | if (dict && (argidx < arglen) && c != '%') { |
| 8523 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 8524 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8525 | Py_XDECREF(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8526 | goto onError; |
| 8527 | } |
| 8528 | Py_XDECREF(temp); |
| 8529 | } /* '%' */ |
| 8530 | } /* until end */ |
| 8531 | if (argidx < arglen && !dict) { |
| 8532 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 8533 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8534 | goto onError; |
| 8535 | } |
| 8536 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8537 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
| 8538 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8539 | if (args_owned) { |
| 8540 | Py_DECREF(args); |
| 8541 | } |
| 8542 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8543 | return (PyObject *)result; |
| 8544 | |
| 8545 | onError: |
| 8546 | Py_XDECREF(result); |
| 8547 | Py_DECREF(uformat); |
| 8548 | if (args_owned) { |
| 8549 | Py_DECREF(args); |
| 8550 | } |
| 8551 | return NULL; |
| 8552 | } |
| 8553 | |
| 8554 | static PyBufferProcs unicode_as_buffer = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8555 | (readbufferproc) unicode_buffer_getreadbuf, |
| 8556 | (writebufferproc) unicode_buffer_getwritebuf, |
| 8557 | (segcountproc) unicode_buffer_getsegcount, |
| 8558 | (charbufferproc) unicode_buffer_getcharbuf, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8559 | }; |
| 8560 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 8561 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8562 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 8563 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8564 | static PyObject * |
| 8565 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8566 | { |
| 8567 | PyObject *x = NULL; |
Guido van Rossum | 55b4a7b | 2007-07-11 09:28:11 +0000 | [diff] [blame] | 8568 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8569 | char *encoding = NULL; |
| 8570 | char *errors = NULL; |
| 8571 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8572 | if (type != &PyUnicode_Type) |
| 8573 | return unicode_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8574 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
| 8575 | kwlist, &x, &encoding, &errors)) |
| 8576 | return NULL; |
| 8577 | if (x == NULL) |
| 8578 | return (PyObject *)_PyUnicode_New(0); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 8579 | if (encoding == NULL && errors == NULL) |
| 8580 | return PyObject_Unicode(x); |
| 8581 | else |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8582 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
| 8583 | } |
| 8584 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8585 | static PyObject * |
| 8586 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8587 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8588 | PyUnicodeObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8589 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8590 | |
| 8591 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 8592 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 8593 | if (tmp == NULL) |
| 8594 | return NULL; |
| 8595 | assert(PyUnicode_Check(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8596 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8597 | if (pnew == NULL) { |
| 8598 | Py_DECREF(tmp); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8599 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8600 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8601 | pnew->str = PyMem_NEW(Py_UNICODE, n+1); |
| 8602 | if (pnew->str == NULL) { |
| 8603 | _Py_ForgetReference((PyObject *)pnew); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8604 | PyObject_Del(pnew); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8605 | Py_DECREF(tmp); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 8606 | return PyErr_NoMemory(); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8607 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8608 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 8609 | pnew->length = n; |
| 8610 | pnew->hash = tmp->hash; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8611 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8612 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8613 | } |
| 8614 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8615 | PyDoc_STRVAR(unicode_doc, |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 8616 | "str(string [, encoding[, errors]]) -> object\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8617 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 8618 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 8619 | encoding defaults to the current default string encoding.\n\ |
| 8620 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8621 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8622 | static PyObject *unicode_iter(PyObject *seq); |
| 8623 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8624 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 8625 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 8626 | "str", /* tp_name */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8627 | sizeof(PyUnicodeObject), /* tp_size */ |
| 8628 | 0, /* tp_itemsize */ |
| 8629 | /* Slots */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 8630 | (destructor)unicode_dealloc, /* tp_dealloc */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8631 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8632 | 0, /* tp_getattr */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8633 | 0, /* tp_setattr */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8634 | 0, /* tp_compare */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8635 | unicode_repr, /* tp_repr */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8636 | &unicode_as_number, /* tp_as_number */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8637 | &unicode_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8638 | &unicode_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8639 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 8640 | 0, /* tp_call*/ |
| 8641 | (reprfunc) unicode_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8642 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 8643 | 0, /* tp_setattro */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8644 | &unicode_as_buffer, /* tp_as_buffer */ |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 8645 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 8646 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8647 | unicode_doc, /* tp_doc */ |
| 8648 | 0, /* tp_traverse */ |
| 8649 | 0, /* tp_clear */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8650 | PyUnicode_RichCompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8651 | 0, /* tp_weaklistoffset */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8652 | unicode_iter, /* tp_iter */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8653 | 0, /* tp_iternext */ |
| 8654 | unicode_methods, /* tp_methods */ |
| 8655 | 0, /* tp_members */ |
| 8656 | 0, /* tp_getset */ |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 8657 | &PyBaseString_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8658 | 0, /* tp_dict */ |
| 8659 | 0, /* tp_descr_get */ |
| 8660 | 0, /* tp_descr_set */ |
| 8661 | 0, /* tp_dictoffset */ |
| 8662 | 0, /* tp_init */ |
| 8663 | 0, /* tp_alloc */ |
| 8664 | unicode_new, /* tp_new */ |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8665 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8666 | }; |
| 8667 | |
| 8668 | /* Initialize the Unicode implementation */ |
| 8669 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 8670 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8671 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8672 | int i; |
| 8673 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8674 | /* XXX - move this array to unicodectype.c ? */ |
| 8675 | Py_UNICODE linebreak[] = { |
| 8676 | 0x000A, /* LINE FEED */ |
| 8677 | 0x000D, /* CARRIAGE RETURN */ |
| 8678 | 0x001C, /* FILE SEPARATOR */ |
| 8679 | 0x001D, /* GROUP SEPARATOR */ |
| 8680 | 0x001E, /* RECORD SEPARATOR */ |
| 8681 | 0x0085, /* NEXT LINE */ |
| 8682 | 0x2028, /* LINE SEPARATOR */ |
| 8683 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 8684 | }; |
| 8685 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8686 | /* Init the implementation */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8687 | unicode_freelist = NULL; |
| 8688 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8689 | unicode_empty = _PyUnicode_New(0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8690 | if (!unicode_empty) |
| 8691 | return; |
| 8692 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8693 | for (i = 0; i < 256; i++) |
| 8694 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 8695 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 8696 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8697 | |
| 8698 | /* initialize the linebreak bloom filter */ |
| 8699 | bloom_linebreak = make_bloom_mask( |
| 8700 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 8701 | ); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8702 | |
| 8703 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8704 | } |
| 8705 | |
| 8706 | /* Finalize the Unicode implementation */ |
| 8707 | |
| 8708 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 8709 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8710 | { |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8711 | PyUnicodeObject *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8712 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8713 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 8714 | Py_XDECREF(unicode_empty); |
| 8715 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8716 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8717 | for (i = 0; i < 256; i++) { |
| 8718 | if (unicode_latin1[i]) { |
| 8719 | Py_DECREF(unicode_latin1[i]); |
| 8720 | unicode_latin1[i] = NULL; |
| 8721 | } |
| 8722 | } |
| 8723 | |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8724 | for (u = unicode_freelist; u != NULL;) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8725 | PyUnicodeObject *v = u; |
| 8726 | u = *(PyUnicodeObject **)u; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 8727 | if (v->str) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 8728 | PyMem_DEL(v->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 8729 | Py_XDECREF(v->defenc); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8730 | PyObject_Del(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8731 | } |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8732 | unicode_freelist = NULL; |
| 8733 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8734 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 8735 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 8736 | void |
| 8737 | PyUnicode_InternInPlace(PyObject **p) |
| 8738 | { |
| 8739 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 8740 | PyObject *t; |
| 8741 | if (s == NULL || !PyUnicode_Check(s)) |
| 8742 | Py_FatalError( |
| 8743 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 8744 | /* If it's a subclass, we don't really know what putting |
| 8745 | it in the interned dict might do. */ |
| 8746 | if (!PyUnicode_CheckExact(s)) |
| 8747 | return; |
| 8748 | if (PyUnicode_CHECK_INTERNED(s)) |
| 8749 | return; |
| 8750 | if (interned == NULL) { |
| 8751 | interned = PyDict_New(); |
| 8752 | if (interned == NULL) { |
| 8753 | PyErr_Clear(); /* Don't leave an exception */ |
| 8754 | return; |
| 8755 | } |
| 8756 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 8757 | /* It might be that the GetItem call fails even |
| 8758 | though the key is present in the dictionary, |
| 8759 | namely when this happens during a stack overflow. */ |
| 8760 | Py_ALLOW_RECURSION |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 8761 | t = PyDict_GetItem(interned, (PyObject *)s); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 8762 | Py_END_ALLOW_RECURSION |
| 8763 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 8764 | if (t) { |
| 8765 | Py_INCREF(t); |
| 8766 | Py_DECREF(*p); |
| 8767 | *p = t; |
| 8768 | return; |
| 8769 | } |
| 8770 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 8771 | PyThreadState_GET()->recursion_critical = 1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 8772 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 8773 | PyErr_Clear(); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 8774 | PyThreadState_GET()->recursion_critical = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 8775 | return; |
| 8776 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 8777 | PyThreadState_GET()->recursion_critical = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 8778 | /* The two references in interned are not counted by refcnt. |
| 8779 | The deallocator will take care of this */ |
Martin v. Löwis | 5d7428b | 2007-07-21 18:47:48 +0000 | [diff] [blame] | 8780 | Py_Refcnt(s) -= 2; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 8781 | PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
| 8782 | } |
| 8783 | |
| 8784 | void |
| 8785 | PyUnicode_InternImmortal(PyObject **p) |
| 8786 | { |
| 8787 | PyUnicode_InternInPlace(p); |
| 8788 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 8789 | PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 8790 | Py_INCREF(*p); |
| 8791 | } |
| 8792 | } |
| 8793 | |
| 8794 | PyObject * |
| 8795 | PyUnicode_InternFromString(const char *cp) |
| 8796 | { |
| 8797 | PyObject *s = PyUnicode_FromString(cp); |
| 8798 | if (s == NULL) |
| 8799 | return NULL; |
| 8800 | PyUnicode_InternInPlace(&s); |
| 8801 | return s; |
| 8802 | } |
| 8803 | |
| 8804 | void _Py_ReleaseInternedUnicodeStrings(void) |
| 8805 | { |
| 8806 | PyObject *keys; |
| 8807 | PyUnicodeObject *s; |
| 8808 | Py_ssize_t i, n; |
| 8809 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
| 8810 | |
| 8811 | if (interned == NULL || !PyDict_Check(interned)) |
| 8812 | return; |
| 8813 | keys = PyDict_Keys(interned); |
| 8814 | if (keys == NULL || !PyList_Check(keys)) { |
| 8815 | PyErr_Clear(); |
| 8816 | return; |
| 8817 | } |
| 8818 | |
| 8819 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 8820 | detector, interned unicode strings are not forcibly deallocated; |
| 8821 | rather, we give them their stolen references back, and then clear |
| 8822 | and DECREF the interned dict. */ |
| 8823 | |
| 8824 | n = PyList_GET_SIZE(keys); |
| 8825 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
| 8826 | n); |
| 8827 | for (i = 0; i < n; i++) { |
| 8828 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
| 8829 | switch (s->state) { |
| 8830 | case SSTATE_NOT_INTERNED: |
| 8831 | /* XXX Shouldn't happen */ |
| 8832 | break; |
| 8833 | case SSTATE_INTERNED_IMMORTAL: |
Martin v. Löwis | 5d7428b | 2007-07-21 18:47:48 +0000 | [diff] [blame] | 8834 | Py_Refcnt(s) += 1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 8835 | immortal_size += s->length; |
| 8836 | break; |
| 8837 | case SSTATE_INTERNED_MORTAL: |
Martin v. Löwis | 5d7428b | 2007-07-21 18:47:48 +0000 | [diff] [blame] | 8838 | Py_Refcnt(s) += 2; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 8839 | mortal_size += s->length; |
| 8840 | break; |
| 8841 | default: |
| 8842 | Py_FatalError("Inconsistent interned string state."); |
| 8843 | } |
| 8844 | s->state = SSTATE_NOT_INTERNED; |
| 8845 | } |
| 8846 | fprintf(stderr, "total size of all interned strings: " |
| 8847 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 8848 | "mortal/immortal\n", mortal_size, immortal_size); |
| 8849 | Py_DECREF(keys); |
| 8850 | PyDict_Clear(interned); |
| 8851 | Py_DECREF(interned); |
| 8852 | interned = NULL; |
| 8853 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8854 | |
| 8855 | |
| 8856 | /********************* Unicode Iterator **************************/ |
| 8857 | |
| 8858 | typedef struct { |
| 8859 | PyObject_HEAD |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 8860 | Py_ssize_t it_index; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8861 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
| 8862 | } unicodeiterobject; |
| 8863 | |
| 8864 | static void |
| 8865 | unicodeiter_dealloc(unicodeiterobject *it) |
| 8866 | { |
| 8867 | _PyObject_GC_UNTRACK(it); |
| 8868 | Py_XDECREF(it->it_seq); |
| 8869 | PyObject_GC_Del(it); |
| 8870 | } |
| 8871 | |
| 8872 | static int |
| 8873 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 8874 | { |
| 8875 | Py_VISIT(it->it_seq); |
| 8876 | return 0; |
| 8877 | } |
| 8878 | |
| 8879 | static PyObject * |
| 8880 | unicodeiter_next(unicodeiterobject *it) |
| 8881 | { |
| 8882 | PyUnicodeObject *seq; |
| 8883 | PyObject *item; |
| 8884 | |
| 8885 | assert(it != NULL); |
| 8886 | seq = it->it_seq; |
| 8887 | if (seq == NULL) |
| 8888 | return NULL; |
| 8889 | assert(PyUnicode_Check(seq)); |
| 8890 | |
| 8891 | if (it->it_index < PyUnicode_GET_SIZE(seq)) { |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 8892 | item = PyUnicode_FromUnicode( |
| 8893 | PyUnicode_AS_UNICODE(seq)+it->it_index, 1); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8894 | if (item != NULL) |
| 8895 | ++it->it_index; |
| 8896 | return item; |
| 8897 | } |
| 8898 | |
| 8899 | Py_DECREF(seq); |
| 8900 | it->it_seq = NULL; |
| 8901 | return NULL; |
| 8902 | } |
| 8903 | |
| 8904 | static PyObject * |
| 8905 | unicodeiter_len(unicodeiterobject *it) |
| 8906 | { |
| 8907 | Py_ssize_t len = 0; |
| 8908 | if (it->it_seq) |
| 8909 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 8910 | return PyInt_FromSsize_t(len); |
| 8911 | } |
| 8912 | |
| 8913 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 8914 | |
| 8915 | static PyMethodDef unicodeiter_methods[] = { |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 8916 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
| 8917 | length_hint_doc}, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8918 | {NULL, NULL} /* sentinel */ |
| 8919 | }; |
| 8920 | |
| 8921 | PyTypeObject PyUnicodeIter_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 8922 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8923 | "unicodeiterator", /* tp_name */ |
| 8924 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 8925 | 0, /* tp_itemsize */ |
| 8926 | /* methods */ |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 8927 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8928 | 0, /* tp_print */ |
| 8929 | 0, /* tp_getattr */ |
| 8930 | 0, /* tp_setattr */ |
| 8931 | 0, /* tp_compare */ |
| 8932 | 0, /* tp_repr */ |
| 8933 | 0, /* tp_as_number */ |
| 8934 | 0, /* tp_as_sequence */ |
| 8935 | 0, /* tp_as_mapping */ |
| 8936 | 0, /* tp_hash */ |
| 8937 | 0, /* tp_call */ |
| 8938 | 0, /* tp_str */ |
| 8939 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 8940 | 0, /* tp_setattro */ |
| 8941 | 0, /* tp_as_buffer */ |
| 8942 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 8943 | 0, /* tp_doc */ |
| 8944 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 8945 | 0, /* tp_clear */ |
| 8946 | 0, /* tp_richcompare */ |
| 8947 | 0, /* tp_weaklistoffset */ |
| 8948 | PyObject_SelfIter, /* tp_iter */ |
| 8949 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 8950 | unicodeiter_methods, /* tp_methods */ |
| 8951 | 0, |
| 8952 | }; |
| 8953 | |
| 8954 | static PyObject * |
| 8955 | unicode_iter(PyObject *seq) |
| 8956 | { |
| 8957 | unicodeiterobject *it; |
| 8958 | |
| 8959 | if (!PyUnicode_Check(seq)) { |
| 8960 | PyErr_BadInternalCall(); |
| 8961 | return NULL; |
| 8962 | } |
| 8963 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 8964 | if (it == NULL) |
| 8965 | return NULL; |
| 8966 | it->it_index = 0; |
| 8967 | Py_INCREF(seq); |
| 8968 | it->it_seq = (PyUnicodeObject *)seq; |
| 8969 | _PyObject_GC_TRACK(it); |
| 8970 | return (PyObject *)it; |
| 8971 | } |
| 8972 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 8973 | size_t |
| 8974 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 8975 | { |
| 8976 | int res = 0; |
| 8977 | while(*u++) |
| 8978 | res++; |
| 8979 | return res; |
| 8980 | } |
| 8981 | |
| 8982 | Py_UNICODE* |
| 8983 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 8984 | { |
| 8985 | Py_UNICODE *u = s1; |
| 8986 | while ((*u++ = *s2++)); |
| 8987 | return s1; |
| 8988 | } |
| 8989 | |
| 8990 | Py_UNICODE* |
| 8991 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 8992 | { |
| 8993 | Py_UNICODE *u = s1; |
| 8994 | while ((*u++ = *s2++)) |
| 8995 | if (n-- == 0) |
| 8996 | break; |
| 8997 | return s1; |
| 8998 | } |
| 8999 | |
| 9000 | int |
| 9001 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9002 | { |
| 9003 | while (*s1 && *s2 && *s1 == *s2) |
| 9004 | s1++, s2++; |
| 9005 | if (*s1 && *s2) |
| 9006 | return (*s1 < *s2) ? -1 : +1; |
| 9007 | if (*s1) |
| 9008 | return 1; |
| 9009 | if (*s2) |
| 9010 | return -1; |
| 9011 | return 0; |
| 9012 | } |
| 9013 | |
| 9014 | Py_UNICODE* |
| 9015 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 9016 | { |
| 9017 | const Py_UNICODE *p; |
| 9018 | for (p = s; *p; p++) |
| 9019 | if (*p == c) |
| 9020 | return (Py_UNICODE*)p; |
| 9021 | return NULL; |
| 9022 | } |
| 9023 | |
| 9024 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9025 | #ifdef __cplusplus |
| 9026 | } |
| 9027 | #endif |
| 9028 | |
| 9029 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9030 | /* |
| 9031 | Local variables: |
| 9032 | c-basic-offset: 4 |
| 9033 | indent-tabs-mode: nil |
| 9034 | End: |
| 9035 | */ |