| 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 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 95 | /* Free list for Unicode objects */ | 
| Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 96 | static PyUnicodeObject *unicode_freelist; | 
 | 97 | static int unicode_freelist_size; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 98 |  | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 99 | /* The empty Unicode object is shared to improve performance. */ | 
 | 100 | static PyUnicodeObject *unicode_empty; | 
 | 101 |  | 
 | 102 | /* Single character Unicode strings in the Latin-1 range are being | 
 | 103 |    shared as well. */ | 
 | 104 | static PyUnicodeObject *unicode_latin1[256]; | 
 | 105 |  | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 106 | /* 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] | 107 |    parameter; it is fixed to "utf-8".  Always use the | 
 | 108 |    PyUnicode_GetDefaultEncoding() API to access this global. */ | 
 | 109 | static const char unicode_default_encoding[] = "utf-8"; | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 110 |  | 
| Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 111 | Py_UNICODE | 
| Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 112 | PyUnicode_GetMax(void) | 
| Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 113 | { | 
| Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 114 | #ifdef Py_UNICODE_WIDE | 
| Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 115 | 	return 0x10FFFF; | 
 | 116 | #else | 
 | 117 | 	/* This is actually an illegal character, so it should | 
 | 118 | 	   not be passed to unichr. */ | 
 | 119 | 	return 0xFFFF; | 
 | 120 | #endif | 
 | 121 | } | 
 | 122 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 123 | /* --- Bloom Filters ----------------------------------------------------- */ | 
 | 124 |  | 
 | 125 | /* stuff to implement simple "bloom filters" for Unicode characters. | 
 | 126 |    to keep things simple, we use a single bitmask, using the least 5 | 
 | 127 |    bits from each unicode characters as the bit index. */ | 
 | 128 |  | 
 | 129 | /* the linebreak mask is set up by Unicode_Init below */ | 
 | 130 |  | 
 | 131 | #define BLOOM_MASK unsigned long | 
 | 132 |  | 
 | 133 | static BLOOM_MASK bloom_linebreak; | 
 | 134 |  | 
 | 135 | #define BLOOM(mask, ch) ((mask & (1 << ((ch) & 0x1F)))) | 
 | 136 |  | 
 | 137 | #define BLOOM_LINEBREAK(ch)\ | 
 | 138 |     (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK((ch))) | 
 | 139 |  | 
 | 140 | Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len) | 
 | 141 | { | 
 | 142 |     /* calculate simple bloom-style bitmask for a given unicode string */ | 
 | 143 |  | 
 | 144 |     long mask; | 
 | 145 |     Py_ssize_t i; | 
 | 146 |  | 
 | 147 |     mask = 0; | 
 | 148 |     for (i = 0; i < len; i++) | 
 | 149 |         mask |= (1 << (ptr[i] & 0x1F)); | 
 | 150 |  | 
 | 151 |     return mask; | 
 | 152 | } | 
 | 153 |  | 
 | 154 | Py_LOCAL_INLINE(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen) | 
 | 155 | { | 
 | 156 |     Py_ssize_t i; | 
 | 157 |  | 
 | 158 |     for (i = 0; i < setlen; i++) | 
 | 159 |         if (set[i] == chr) | 
 | 160 |             return 1; | 
 | 161 |  | 
 | 162 |     return 0; | 
 | 163 | } | 
 | 164 |  | 
 | 165 | #define BLOOM_MEMBER(mask, chr, set, setlen)\ | 
 | 166 |     BLOOM(mask, chr) && unicode_member(chr, set, setlen) | 
 | 167 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 168 | /* --- Unicode Object ----------------------------------------------------- */ | 
 | 169 |  | 
 | 170 | static | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 171 | int unicode_resize(register PyUnicodeObject *unicode, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 172 |                       Py_ssize_t length) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 173 | { | 
 | 174 |     void *oldstr; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 175 |  | 
| Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 176 |     /* Shortcut if there's nothing much to do. */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 177 |     if (unicode->length == length) | 
| Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 178 | 	goto reset; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 179 |  | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 180 |     /* Resizing shared object (unicode_empty or single character | 
 | 181 |        objects) in-place is not allowed. Use PyUnicode_Resize() | 
 | 182 |        instead ! */ | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 183 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 184 |     if (unicode == unicode_empty ||  | 
 | 185 | 	(unicode->length == 1 &&  | 
 | 186 | 	 unicode->str[0] < 256U && | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 187 | 	 unicode_latin1[unicode->str[0]] == unicode)) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 188 |         PyErr_SetString(PyExc_SystemError, | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 189 |                         "can't resize shared unicode objects"); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 190 |         return -1; | 
 | 191 |     } | 
 | 192 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 193 |     /* We allocate one more byte to make sure the string is Ux0000 terminated. | 
 | 194 |        The overallocation is also used by fastsearch, which assumes that it's | 
 | 195 |        safe to look at str[length] (without making any assumptions about what | 
 | 196 |        it contains). */ | 
 | 197 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 198 |     oldstr = unicode->str; | 
 | 199 |     PyMem_RESIZE(unicode->str, Py_UNICODE, length + 1); | 
 | 200 |     if (!unicode->str) { | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 201 | 	unicode->str = (Py_UNICODE *)oldstr; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 202 |         PyErr_NoMemory(); | 
 | 203 |         return -1; | 
 | 204 |     } | 
 | 205 |     unicode->str[length] = 0; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 206 |     unicode->length = length; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 207 |  | 
| Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 208 |  reset: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 209 |     /* Reset the object caches */ | 
| Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 210 |     if (unicode->defenc) { | 
 | 211 |         Py_DECREF(unicode->defenc); | 
 | 212 |         unicode->defenc = NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 213 |     } | 
 | 214 |     unicode->hash = -1; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 215 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 216 |     return 0; | 
 | 217 | } | 
 | 218 |  | 
 | 219 | /* We allocate one more byte to make sure the string is | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 220 |    Ux0000 terminated -- XXX is this needed ? | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 221 |  | 
 | 222 |    XXX This allocator could further be enhanced by assuring that the | 
 | 223 |        free list never reduces its size below 1. | 
 | 224 |  | 
 | 225 | */ | 
 | 226 |  | 
 | 227 | static | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 228 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 229 | { | 
 | 230 |     register PyUnicodeObject *unicode; | 
 | 231 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 232 |     /* Optimization for empty strings */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 233 |     if (length == 0 && unicode_empty != NULL) { | 
 | 234 |         Py_INCREF(unicode_empty); | 
 | 235 |         return unicode_empty; | 
 | 236 |     } | 
 | 237 |  | 
 | 238 |     /* Unicode freelist & memory allocation */ | 
 | 239 |     if (unicode_freelist) { | 
 | 240 |         unicode = unicode_freelist; | 
| Marc-André Lemburg | bea47e7 | 2000-06-17 20:31:17 +0000 | [diff] [blame] | 241 |         unicode_freelist = *(PyUnicodeObject **)unicode; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 242 |         unicode_freelist_size--; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 243 | 	if (unicode->str) { | 
| Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 244 | 	    /* Keep-Alive optimization: we only upsize the buffer, | 
 | 245 | 	       never downsize it. */ | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 246 | 	    if ((unicode->length < length) && | 
| Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 247 |                 unicode_resize(unicode, length) < 0) { | 
| Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 248 | 		PyMem_DEL(unicode->str); | 
| Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 249 | 		goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 250 | 	    } | 
 | 251 | 	} | 
| Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 252 |         else { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 253 | 	    unicode->str = PyMem_NEW(Py_UNICODE, length + 1); | 
| Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 254 |         } | 
 | 255 |         PyObject_INIT(unicode, &PyUnicode_Type); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 256 |     } | 
 | 257 |     else { | 
| Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 258 |         unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 259 |         if (unicode == NULL) | 
 | 260 |             return NULL; | 
 | 261 | 	unicode->str = PyMem_NEW(Py_UNICODE, length + 1); | 
 | 262 |     } | 
 | 263 |  | 
| Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 264 |     if (!unicode->str) { | 
 | 265 | 	PyErr_NoMemory(); | 
| Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 266 | 	goto onError; | 
| Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 267 |     } | 
| Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 268 |     /* Initialize the first element to guard against cases where | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 269 |      * the caller fails before initializing str -- unicode_resize() | 
 | 270 |      * reads str[0], and the Keep-Alive optimization can keep memory | 
 | 271 |      * allocated for str alive across a call to unicode_dealloc(unicode). | 
 | 272 |      * We don't want unicode_resize to read uninitialized memory in | 
 | 273 |      * that case. | 
 | 274 |      */ | 
| Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 275 |     unicode->str[0] = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 276 |     unicode->str[length] = 0; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 277 |     unicode->length = length; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 278 |     unicode->hash = -1; | 
| Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 279 |     unicode->defenc = NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 280 |     return unicode; | 
| Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 281 |  | 
 | 282 |  onError: | 
 | 283 |     _Py_ForgetReference((PyObject *)unicode); | 
| Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 284 |     PyObject_Del(unicode); | 
| Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 285 |     return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 286 | } | 
 | 287 |  | 
 | 288 | static | 
| Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 289 | void unicode_dealloc(register PyUnicodeObject *unicode) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 290 | { | 
| Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 291 |     if (PyUnicode_CheckExact(unicode) && | 
 | 292 | 	unicode_freelist_size < MAX_UNICODE_FREELIST_SIZE) { | 
| Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 293 |         /* Keep-Alive optimization */ | 
 | 294 | 	if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { | 
| Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 295 | 	    PyMem_DEL(unicode->str); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 296 | 	    unicode->str = NULL; | 
 | 297 | 	    unicode->length = 0; | 
 | 298 | 	} | 
| Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 299 | 	if (unicode->defenc) { | 
 | 300 | 	    Py_DECREF(unicode->defenc); | 
 | 301 | 	    unicode->defenc = NULL; | 
| Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 302 | 	} | 
 | 303 | 	/* Add to free list */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 304 |         *(PyUnicodeObject **)unicode = unicode_freelist; | 
 | 305 |         unicode_freelist = unicode; | 
 | 306 |         unicode_freelist_size++; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 307 |     } | 
 | 308 |     else { | 
| Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 309 | 	PyMem_DEL(unicode->str); | 
| Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 310 | 	Py_XDECREF(unicode->defenc); | 
| Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 311 | 	unicode->ob_type->tp_free((PyObject *)unicode); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 312 |     } | 
 | 313 | } | 
 | 314 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 315 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 316 | { | 
 | 317 |     register PyUnicodeObject *v; | 
 | 318 |  | 
 | 319 |     /* Argument checks */ | 
 | 320 |     if (unicode == NULL) { | 
 | 321 | 	PyErr_BadInternalCall(); | 
 | 322 | 	return -1; | 
 | 323 |     } | 
 | 324 |     v = (PyUnicodeObject *)*unicode; | 
| Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 325 |     if (v == NULL || !PyUnicode_Check(v) || v->ob_refcnt != 1 || length < 0) { | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 326 | 	PyErr_BadInternalCall(); | 
 | 327 | 	return -1; | 
 | 328 |     } | 
 | 329 |  | 
 | 330 |     /* Resizing unicode_empty and single character objects is not | 
 | 331 |        possible since these are being shared. We simply return a fresh | 
 | 332 |        copy with the same Unicode content. */ | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 333 |     if (v->length != length && | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 334 | 	(v == unicode_empty || v->length == 1)) { | 
 | 335 | 	PyUnicodeObject *w = _PyUnicode_New(length); | 
 | 336 | 	if (w == NULL) | 
 | 337 | 	    return -1; | 
 | 338 | 	Py_UNICODE_COPY(w->str, v->str, | 
 | 339 | 			length < v->length ? length : v->length); | 
| Raymond Hettinger | c8df578 | 2003-03-09 07:30:43 +0000 | [diff] [blame] | 340 | 	Py_DECREF(*unicode); | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 341 | 	*unicode = (PyObject *)w; | 
 | 342 | 	return 0; | 
 | 343 |     } | 
 | 344 |  | 
 | 345 |     /* Note that we don't have to modify *unicode for unshared Unicode | 
 | 346 |        objects, since we can modify them in-place. */ | 
 | 347 |     return unicode_resize(v, length); | 
 | 348 | } | 
 | 349 |  | 
 | 350 | /* Internal API for use in unicodeobject.c only ! */ | 
 | 351 | #define _PyUnicode_Resize(unicodevar, length) \ | 
 | 352 |         PyUnicode_Resize(((PyObject **)(unicodevar)), length) | 
 | 353 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 354 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 355 | 				Py_ssize_t size) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 356 | { | 
 | 357 |     PyUnicodeObject *unicode; | 
 | 358 |  | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 359 |     /* If the Unicode data is known at construction time, we can apply | 
 | 360 |        some optimizations which share commonly used objects. */ | 
 | 361 |     if (u != NULL) { | 
 | 362 |  | 
 | 363 | 	/* Optimization for empty strings */ | 
 | 364 | 	if (size == 0 && unicode_empty != NULL) { | 
 | 365 | 	    Py_INCREF(unicode_empty); | 
 | 366 | 	    return (PyObject *)unicode_empty; | 
 | 367 | 	} | 
 | 368 |  | 
 | 369 | 	/* Single character Unicode objects in the Latin-1 range are | 
 | 370 | 	   shared when using this constructor */ | 
 | 371 | 	if (size == 1 && *u < 256) { | 
 | 372 | 	    unicode = unicode_latin1[*u]; | 
 | 373 | 	    if (!unicode) { | 
 | 374 | 		unicode = _PyUnicode_New(1); | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 375 | 		if (!unicode) | 
 | 376 | 		    return NULL; | 
| Marc-André Lemburg | 8879a33 | 2001-06-07 12:26:56 +0000 | [diff] [blame] | 377 | 		unicode->str[0] = *u; | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 378 | 		unicode_latin1[*u] = unicode; | 
 | 379 | 	    } | 
 | 380 | 	    Py_INCREF(unicode); | 
 | 381 | 	    return (PyObject *)unicode; | 
 | 382 | 	} | 
 | 383 |     } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 384 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 385 |     unicode = _PyUnicode_New(size); | 
 | 386 |     if (!unicode) | 
 | 387 |         return NULL; | 
 | 388 |  | 
 | 389 |     /* Copy the Unicode data into the new object */ | 
 | 390 |     if (u != NULL) | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 391 | 	Py_UNICODE_COPY(unicode->str, u, size); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 392 |  | 
 | 393 |     return (PyObject *)unicode; | 
 | 394 | } | 
 | 395 |  | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 396 | PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) | 
| Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 397 | { | 
 | 398 |     PyUnicodeObject *unicode; | 
| Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 399 |     /* If the Unicode data is known at construction time, we can apply | 
 | 400 |        some optimizations which share commonly used objects. */ | 
 | 401 |     if (u != NULL) { | 
 | 402 |  | 
 | 403 | 	/* Optimization for empty strings */ | 
 | 404 | 	if (size == 0 && unicode_empty != NULL) { | 
 | 405 | 	    Py_INCREF(unicode_empty); | 
 | 406 | 	    return (PyObject *)unicode_empty; | 
 | 407 | 	} | 
 | 408 |  | 
| Walter Dörwald | 071b9da | 2007-05-05 14:21:20 +0000 | [diff] [blame] | 409 | 	/* Single characters are shared when using this constructor */ | 
 | 410 | 	if (size == 1) { | 
| Walter Dörwald | ce32db3 | 2007-05-05 14:26:59 +0000 | [diff] [blame] | 411 | 	    unicode = unicode_latin1[(int)*u]; | 
| Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 412 | 	    if (!unicode) { | 
 | 413 | 		unicode = _PyUnicode_New(1); | 
 | 414 | 		if (!unicode) | 
 | 415 | 		    return NULL; | 
 | 416 | 		unicode->str[0] = *u; | 
| Walter Dörwald | ce32db3 | 2007-05-05 14:26:59 +0000 | [diff] [blame] | 417 | 		unicode_latin1[(int)*u] = unicode; | 
| Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 418 | 	    } | 
 | 419 | 	    Py_INCREF(unicode); | 
 | 420 | 	    return (PyObject *)unicode; | 
 | 421 | 	} | 
 | 422 |     } | 
 | 423 |  | 
| Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 424 |     unicode = _PyUnicode_New(size); | 
| Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 425 |     if (!unicode) | 
 | 426 |         return NULL; | 
 | 427 |  | 
 | 428 |     /* Copy the Unicode data into the new object */ | 
 | 429 |     if (u != NULL) { | 
| Walter Dörwald | ce32db3 | 2007-05-05 14:26:59 +0000 | [diff] [blame] | 430 |         Py_UNICODE *p = unicode->str; | 
 | 431 |         while ((*p++ = *u++)) | 
| Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 432 |             ; | 
 | 433 |     } | 
 | 434 |  | 
 | 435 |     return (PyObject *)unicode; | 
 | 436 | } | 
 | 437 |  | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 438 | PyObject *PyUnicode_FromString(const char *u) | 
 | 439 | { | 
 | 440 |     size_t size = strlen(u); | 
 | 441 |     if (size > PY_SSIZE_T_MAX) { | 
 | 442 |         PyErr_SetString(PyExc_OverflowError, "input too long"); | 
 | 443 |         return NULL; | 
 | 444 |     } | 
 | 445 |  | 
 | 446 |     return PyUnicode_FromStringAndSize(u, size); | 
 | 447 | } | 
 | 448 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 449 | #ifdef HAVE_WCHAR_H | 
 | 450 |  | 
 | 451 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 452 | 				 Py_ssize_t size) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 453 | { | 
 | 454 |     PyUnicodeObject *unicode; | 
 | 455 |  | 
 | 456 |     if (w == NULL) { | 
 | 457 | 	PyErr_BadInternalCall(); | 
 | 458 | 	return NULL; | 
 | 459 |     } | 
 | 460 |  | 
 | 461 |     unicode = _PyUnicode_New(size); | 
 | 462 |     if (!unicode) | 
 | 463 |         return NULL; | 
 | 464 |  | 
 | 465 |     /* Copy the wchar_t data into the new object */ | 
 | 466 | #ifdef HAVE_USABLE_WCHAR_T | 
 | 467 |     memcpy(unicode->str, w, size * sizeof(wchar_t)); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 468 | #else | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 469 |     { | 
 | 470 | 	register Py_UNICODE *u; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 471 | 	register Py_ssize_t i; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 472 | 	u = PyUnicode_AS_UNICODE(unicode); | 
| Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 473 | 	for (i = size; i > 0; i--) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 474 | 	    *u++ = *w++; | 
 | 475 |     } | 
 | 476 | #endif | 
 | 477 |  | 
 | 478 |     return (PyObject *)unicode; | 
 | 479 | } | 
 | 480 |  | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 481 | #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} | 
 | 482 |  | 
 | 483 | PyObject * | 
 | 484 | PyUnicode_FromFormatV(const char *format, va_list vargs) | 
 | 485 | { | 
 | 486 | 	va_list count; | 
 | 487 | 	Py_ssize_t n = 0; | 
 | 488 | 	const char* f; | 
 | 489 | 	Py_UNICODE *s; | 
 | 490 | 	PyObject *string; | 
 | 491 | 	/* used by sprintf */ | 
 | 492 | 	char buffer[21]; | 
 | 493 | 	const char *copy; | 
 | 494 |  | 
 | 495 | #ifdef VA_LIST_IS_ARRAY | 
 | 496 | 	Py_MEMCPY(count, vargs, sizeof(va_list)); | 
 | 497 | #else | 
 | 498 | #ifdef  __va_copy | 
 | 499 | 	__va_copy(count, vargs); | 
 | 500 | #else | 
 | 501 | 	count = vargs; | 
 | 502 | #endif | 
 | 503 | #endif | 
 | 504 | 	/* step 1: figure out how large a buffer we need */ | 
 | 505 | 	for (f = format; *f; f++) { | 
 | 506 | 		if (*f == '%') { | 
 | 507 | 			const char* p = f; | 
 | 508 | 			while (*++f && *f != '%' && !isalpha(Py_CHARMASK(*f))) | 
 | 509 | 				; | 
 | 510 |  | 
 | 511 | 			/* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since | 
 | 512 | 			 * they don't affect the amount of space we reserve. | 
 | 513 | 			 */ | 
 | 514 | 			if ((*f == 'l' || *f == 'z') && | 
 | 515 | 					(f[1] == 'd' || f[1] == 'u')) | 
 | 516 | 				++f; | 
 | 517 |  | 
 | 518 | 			switch (*f) { | 
 | 519 | 			case 'c': | 
 | 520 | 				(void)va_arg(count, int); | 
 | 521 | 				/* fall through... */ | 
 | 522 | 			case '%': | 
 | 523 | 				n++; | 
 | 524 | 				break; | 
 | 525 | 			case 'd': case 'u': case 'i': case 'x': | 
 | 526 | 				(void) va_arg(count, int); | 
 | 527 | 				/* 20 bytes is enough to hold a 64-bit | 
 | 528 | 				   integer.  Decimal takes the most space. | 
 | 529 | 				   This isn't enough for octal. */ | 
 | 530 | 				n += 20; | 
 | 531 | 				break; | 
 | 532 | 			case 's': | 
 | 533 | 				n += strlen(va_arg(count, char*)); | 
 | 534 | 				break; | 
 | 535 | 			case 'U': | 
 | 536 | 			{ | 
 | 537 | 				PyObject *obj = va_arg(count, PyObject *); | 
 | 538 | 				assert(obj && PyUnicode_Check(obj)); | 
 | 539 | 				n += PyUnicode_GET_SIZE(obj); | 
 | 540 | 				break; | 
 | 541 | 			} | 
 | 542 | 			case 'p': | 
 | 543 | 				(void) va_arg(count, int); | 
 | 544 | 				/* maximum 64-bit pointer representation: | 
 | 545 | 				 * 0xffffffffffffffff | 
 | 546 | 				 * so 19 characters is enough. | 
 | 547 | 				 * XXX I count 18 -- what's the extra for? | 
 | 548 | 				 */ | 
 | 549 | 				n += 19; | 
 | 550 | 				break; | 
 | 551 | 			default: | 
 | 552 | 				/* if we stumble upon an unknown | 
 | 553 | 				   formatting code, copy the rest of | 
 | 554 | 				   the format string to the output | 
 | 555 | 				   string. (we cannot just skip the | 
 | 556 | 				   code, since there's no way to know | 
 | 557 | 				   what's in the argument list) */ | 
 | 558 | 				n += strlen(p); | 
 | 559 | 				goto expand; | 
 | 560 | 			} | 
 | 561 | 		} else | 
 | 562 | 			n++; | 
 | 563 | 	} | 
 | 564 |  expand: | 
 | 565 | 	/* step 2: fill the buffer */ | 
 | 566 | 	/* Since we've analyzed how much space we need for the worst case, | 
 | 567 | 	   we don't have to resize the string. */ | 
 | 568 | 	string = PyUnicode_FromUnicode(NULL, n); | 
 | 569 | 	if (!string) | 
 | 570 | 		return NULL; | 
 | 571 |  | 
 | 572 | 	s = PyUnicode_AS_UNICODE(string); | 
 | 573 |  | 
 | 574 | 	for (f = format; *f; f++) { | 
 | 575 | 		if (*f == '%') { | 
 | 576 | 			const char* p = f++; | 
 | 577 | 			int longflag = 0; | 
 | 578 | 			int size_tflag = 0; | 
 | 579 | 			/* parse the width.precision part (we're only | 
 | 580 | 			   interested in the precision value, if any) */ | 
 | 581 | 			n = 0; | 
 | 582 | 			while (isdigit(Py_CHARMASK(*f))) | 
 | 583 | 				n = (n*10) + *f++ - '0'; | 
 | 584 | 			if (*f == '.') { | 
 | 585 | 				f++; | 
 | 586 | 				n = 0; | 
 | 587 | 				while (isdigit(Py_CHARMASK(*f))) | 
 | 588 | 					n = (n*10) + *f++ - '0'; | 
 | 589 | 			} | 
 | 590 | 			while (*f && *f != '%' && !isalpha(Py_CHARMASK(*f))) | 
 | 591 | 				f++; | 
 | 592 | 			/* handle the long flag, but only for %ld and %lu. | 
 | 593 | 			   others can be added when necessary. */ | 
 | 594 | 			if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { | 
 | 595 | 				longflag = 1; | 
 | 596 | 				++f; | 
 | 597 | 			} | 
 | 598 | 			/* handle the size_t flag. */ | 
 | 599 | 			if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { | 
 | 600 | 				size_tflag = 1; | 
 | 601 | 				++f; | 
 | 602 | 			} | 
 | 603 |  | 
 | 604 | 			switch (*f) { | 
 | 605 | 			case 'c': | 
 | 606 | 				*s++ = va_arg(vargs, int); | 
 | 607 | 				break; | 
 | 608 | 			case 'd': | 
 | 609 | 				if (longflag) | 
 | 610 | 					sprintf(buffer, "%ld", va_arg(vargs, long)); | 
 | 611 | 				else if (size_tflag) | 
 | 612 | 					sprintf(buffer, "%" PY_FORMAT_SIZE_T "d", | 
 | 613 | 					        va_arg(vargs, Py_ssize_t)); | 
 | 614 | 				else | 
 | 615 | 					sprintf(buffer, "%d", va_arg(vargs, int)); | 
 | 616 | 				appendstring(buffer); | 
 | 617 | 				break; | 
 | 618 | 			case 'u': | 
 | 619 | 				if (longflag) | 
 | 620 | 					sprintf(buffer, "%lu", | 
 | 621 | 						va_arg(vargs, unsigned long)); | 
 | 622 | 				else if (size_tflag) | 
 | 623 | 					sprintf(buffer, "%" PY_FORMAT_SIZE_T "u", | 
 | 624 | 					        va_arg(vargs, size_t)); | 
 | 625 | 				else | 
 | 626 | 					sprintf(buffer, "%u", | 
 | 627 | 						va_arg(vargs, unsigned int)); | 
 | 628 | 				appendstring(buffer); | 
 | 629 | 				break; | 
 | 630 | 			case 'i': | 
 | 631 | 				sprintf(buffer, "%i", va_arg(vargs, int)); | 
 | 632 | 				appendstring(buffer); | 
 | 633 | 				break; | 
 | 634 | 			case 'x': | 
 | 635 | 				sprintf(buffer, "%x", va_arg(vargs, int)); | 
 | 636 | 				appendstring(buffer); | 
 | 637 | 				break; | 
 | 638 | 			case 's': | 
 | 639 | 				p = va_arg(vargs, char*); | 
 | 640 | 				appendstring(p); | 
 | 641 | 				break; | 
 | 642 | 			case 'U': | 
 | 643 | 			{ | 
 | 644 | 				PyObject *obj = va_arg(vargs, PyObject *); | 
 | 645 | 				Py_UNICODE *ucopy = PyUnicode_AS_UNICODE(obj); | 
 | 646 | 				Py_ssize_t usize = PyUnicode_GET_SIZE(obj); | 
 | 647 | 				Py_ssize_t upos; | 
 | 648 | 				for (upos = 0; upos<usize;) | 
 | 649 | 					*s++ = ucopy[upos++]; | 
 | 650 | 				break; | 
 | 651 | 			} | 
 | 652 | 			case 'p': | 
 | 653 | 				sprintf(buffer, "%p", va_arg(vargs, void*)); | 
 | 654 | 				/* %p is ill-defined:  ensure leading 0x. */ | 
 | 655 | 				if (buffer[1] == 'X') | 
 | 656 | 					buffer[1] = 'x'; | 
 | 657 | 				else if (buffer[1] != 'x') { | 
 | 658 | 					memmove(buffer+2, buffer, strlen(buffer)+1); | 
 | 659 | 					buffer[0] = '0'; | 
 | 660 | 					buffer[1] = 'x'; | 
 | 661 | 				} | 
 | 662 | 				appendstring(buffer); | 
 | 663 | 				break; | 
 | 664 | 			case '%': | 
 | 665 | 				*s++ = '%'; | 
 | 666 | 				break; | 
 | 667 | 			default: | 
 | 668 | 				appendstring(p); | 
 | 669 | 				goto end; | 
 | 670 | 			} | 
 | 671 | 		} else | 
 | 672 | 			*s++ = *f; | 
 | 673 | 	} | 
 | 674 |  | 
 | 675 |  end: | 
 | 676 | 	_PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); | 
 | 677 | 	return string; | 
 | 678 | } | 
 | 679 |  | 
 | 680 | #undef appendstring | 
 | 681 |  | 
 | 682 | PyObject * | 
 | 683 | PyUnicode_FromFormat(const char *format, ...) | 
 | 684 | { | 
 | 685 | 	PyObject* ret; | 
 | 686 | 	va_list vargs; | 
 | 687 |  | 
 | 688 | #ifdef HAVE_STDARG_PROTOTYPES | 
 | 689 | 	va_start(vargs, format); | 
 | 690 | #else | 
 | 691 | 	va_start(vargs); | 
 | 692 | #endif | 
 | 693 | 	ret = PyUnicode_FromFormatV(format, vargs); | 
 | 694 | 	va_end(vargs); | 
 | 695 | 	return ret; | 
 | 696 | } | 
 | 697 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 698 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, | 
 | 699 | 				wchar_t *w, | 
 | 700 | 				Py_ssize_t size) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 701 | { | 
 | 702 |     if (unicode == NULL) { | 
 | 703 | 	PyErr_BadInternalCall(); | 
 | 704 | 	return -1; | 
 | 705 |     } | 
| Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 706 |  | 
 | 707 |     /* If possible, try to copy the 0-termination as well */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 708 |     if (size > PyUnicode_GET_SIZE(unicode)) | 
| Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 709 | 	size = PyUnicode_GET_SIZE(unicode) + 1; | 
 | 710 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 711 | #ifdef HAVE_USABLE_WCHAR_T | 
 | 712 |     memcpy(w, unicode->str, size * sizeof(wchar_t)); | 
 | 713 | #else | 
 | 714 |     { | 
 | 715 | 	register Py_UNICODE *u; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 716 | 	register Py_ssize_t i; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 717 | 	u = PyUnicode_AS_UNICODE(unicode); | 
| Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 718 | 	for (i = size; i > 0; i--) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 719 | 	    *w++ = *u++; | 
 | 720 |     } | 
 | 721 | #endif | 
 | 722 |  | 
| Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 723 |     if (size > PyUnicode_GET_SIZE(unicode)) | 
 | 724 |         return PyUnicode_GET_SIZE(unicode); | 
 | 725 |     else | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 726 |     return size; | 
 | 727 | } | 
 | 728 |  | 
 | 729 | #endif | 
 | 730 |  | 
| Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 731 | PyObject *PyUnicode_FromOrdinal(int ordinal) | 
 | 732 | { | 
| Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 733 |     Py_UNICODE s[1]; | 
| Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 734 |  | 
 | 735 | #ifdef Py_UNICODE_WIDE | 
 | 736 |     if (ordinal < 0 || ordinal > 0x10ffff) { | 
 | 737 | 	PyErr_SetString(PyExc_ValueError, | 
 | 738 | 			"unichr() arg not in range(0x110000) " | 
 | 739 | 			"(wide Python build)"); | 
 | 740 | 	return NULL; | 
 | 741 |     } | 
 | 742 | #else | 
 | 743 |     if (ordinal < 0 || ordinal > 0xffff) { | 
 | 744 | 	PyErr_SetString(PyExc_ValueError, | 
 | 745 | 			"unichr() arg not in range(0x10000) " | 
 | 746 | 			"(narrow Python build)"); | 
 | 747 | 	return NULL; | 
 | 748 |     } | 
 | 749 | #endif | 
 | 750 |  | 
| Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 751 |     s[0] = (Py_UNICODE)ordinal; | 
 | 752 |     return PyUnicode_FromUnicode(s, 1); | 
| Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 753 | } | 
 | 754 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 755 | PyObject *PyUnicode_FromObject(register PyObject *obj) | 
 | 756 | { | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 757 |     /* XXX Perhaps we should make this API an alias of | 
 | 758 |            PyObject_Unicode() instead ?! */ | 
 | 759 |     if (PyUnicode_CheckExact(obj)) { | 
 | 760 | 	Py_INCREF(obj); | 
 | 761 | 	return obj; | 
 | 762 |     } | 
 | 763 |     if (PyUnicode_Check(obj)) { | 
 | 764 | 	/* For a Unicode subtype that's not a Unicode object, | 
 | 765 | 	   return a true Unicode object with the same data. */ | 
 | 766 | 	return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), | 
 | 767 | 				     PyUnicode_GET_SIZE(obj)); | 
 | 768 |     } | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 769 |     return PyUnicode_FromEncodedObject(obj, NULL, "strict"); | 
 | 770 | } | 
 | 771 |  | 
 | 772 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, | 
 | 773 | 				      const char *encoding, | 
 | 774 | 				      const char *errors) | 
 | 775 | { | 
| Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 776 |     const char *s = NULL; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 777 |     Py_ssize_t len; | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 778 |     PyObject *v; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 779 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 780 |     if (obj == NULL) { | 
 | 781 | 	PyErr_BadInternalCall(); | 
 | 782 | 	return NULL; | 
 | 783 |     } | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 784 |  | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 785 | #if 0 | 
 | 786 |     /* For b/w compatibility we also accept Unicode objects provided | 
| Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 787 |        that no encodings is given and then redirect to | 
 | 788 |        PyObject_Unicode() which then applies the additional logic for | 
 | 789 |        Unicode subclasses. | 
| Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 790 |  | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 791 |        NOTE: This API should really only be used for object which | 
 | 792 |              represent *encoded* Unicode ! | 
 | 793 |  | 
 | 794 |     */ | 
| Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 795 | 	if (PyUnicode_Check(obj)) { | 
 | 796 | 	    if (encoding) { | 
 | 797 | 		PyErr_SetString(PyExc_TypeError, | 
 | 798 | 				"decoding Unicode is not supported"); | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 799 | 	    return NULL; | 
| Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 800 | 	    } | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 801 | 	return PyObject_Unicode(obj); | 
| Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 802 | 	    } | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 803 | #else | 
 | 804 |     if (PyUnicode_Check(obj)) { | 
 | 805 | 	PyErr_SetString(PyExc_TypeError, | 
 | 806 | 			"decoding Unicode is not supported"); | 
 | 807 | 	return NULL; | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 808 | 	} | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 809 | #endif | 
 | 810 |  | 
 | 811 |     /* Coerce object */ | 
 | 812 |     if (PyString_Check(obj)) { | 
| Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 813 | 	    s = PyString_AS_STRING(obj); | 
 | 814 | 	    len = PyString_GET_SIZE(obj); | 
| Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 815 | 	    } | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 816 |     else if (PyObject_AsCharBuffer(obj, &s, &len)) { | 
 | 817 | 	/* Overwrite the error message with something more useful in | 
 | 818 | 	   case of a TypeError. */ | 
 | 819 | 	if (PyErr_ExceptionMatches(PyExc_TypeError)) | 
| Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 820 | 	PyErr_Format(PyExc_TypeError, | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 821 | 			 "coercing to Unicode: need string or buffer, " | 
 | 822 | 			 "%.80s found", | 
| Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 823 | 		     obj->ob_type->tp_name); | 
 | 824 | 	goto onError; | 
 | 825 |     } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 826 |  | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 827 |     /* Convert to Unicode */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 828 |     if (len == 0) { | 
 | 829 | 	Py_INCREF(unicode_empty); | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 830 | 	v = (PyObject *)unicode_empty; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 831 |     } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 832 |     else | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 833 | 	v = PyUnicode_Decode(s, len, encoding, errors); | 
| Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 834 |  | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 835 |     return v; | 
 | 836 |  | 
 | 837 |  onError: | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 838 |     return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 839 | } | 
 | 840 |  | 
 | 841 | PyObject *PyUnicode_Decode(const char *s, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 842 | 			   Py_ssize_t size, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 843 | 			   const char *encoding, | 
 | 844 | 			   const char *errors) | 
 | 845 | { | 
 | 846 |     PyObject *buffer = NULL, *unicode; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 847 |  | 
 | 848 |     if (encoding == NULL) | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 849 | 	encoding = PyUnicode_GetDefaultEncoding(); | 
 | 850 |  | 
 | 851 |     /* Shortcuts for common default encodings */ | 
 | 852 |     if (strcmp(encoding, "utf-8") == 0) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 853 |         return PyUnicode_DecodeUTF8(s, size, errors); | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 854 |     else if (strcmp(encoding, "latin-1") == 0) | 
 | 855 |         return PyUnicode_DecodeLatin1(s, size, errors); | 
| Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 856 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) | 
 | 857 |     else if (strcmp(encoding, "mbcs") == 0) | 
 | 858 |         return PyUnicode_DecodeMBCS(s, size, errors); | 
 | 859 | #endif | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 860 |     else if (strcmp(encoding, "ascii") == 0) | 
 | 861 |         return PyUnicode_DecodeASCII(s, size, errors); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 862 |  | 
 | 863 |     /* Decode via the codec registry */ | 
 | 864 |     buffer = PyBuffer_FromMemory((void *)s, size); | 
 | 865 |     if (buffer == NULL) | 
 | 866 |         goto onError; | 
 | 867 |     unicode = PyCodec_Decode(buffer, encoding, errors); | 
 | 868 |     if (unicode == NULL) | 
 | 869 |         goto onError; | 
 | 870 |     if (!PyUnicode_Check(unicode)) { | 
 | 871 |         PyErr_Format(PyExc_TypeError, | 
| Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 872 |                      "decoder did not return an unicode object (type=%.400s)", | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 873 |                      unicode->ob_type->tp_name); | 
 | 874 |         Py_DECREF(unicode); | 
 | 875 |         goto onError; | 
 | 876 |     } | 
 | 877 |     Py_DECREF(buffer); | 
 | 878 |     return unicode; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 879 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 880 |  onError: | 
 | 881 |     Py_XDECREF(buffer); | 
 | 882 |     return NULL; | 
 | 883 | } | 
 | 884 |  | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 885 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, | 
 | 886 |                                     const char *encoding, | 
 | 887 |                                     const char *errors) | 
 | 888 | { | 
 | 889 |     PyObject *v; | 
 | 890 |  | 
 | 891 |     if (!PyUnicode_Check(unicode)) { | 
 | 892 |         PyErr_BadArgument(); | 
 | 893 |         goto onError; | 
 | 894 |     } | 
 | 895 |  | 
 | 896 |     if (encoding == NULL) | 
 | 897 | 	encoding = PyUnicode_GetDefaultEncoding(); | 
 | 898 |  | 
 | 899 |     /* Decode via the codec registry */ | 
 | 900 |     v = PyCodec_Decode(unicode, encoding, errors); | 
 | 901 |     if (v == NULL) | 
 | 902 |         goto onError; | 
 | 903 |     return v; | 
 | 904 |  | 
 | 905 |  onError: | 
 | 906 |     return NULL; | 
 | 907 | } | 
 | 908 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 909 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 910 | 			   Py_ssize_t size, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 911 | 			   const char *encoding, | 
 | 912 | 			   const char *errors) | 
 | 913 | { | 
 | 914 |     PyObject *v, *unicode; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 915 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 916 |     unicode = PyUnicode_FromUnicode(s, size); | 
 | 917 |     if (unicode == NULL) | 
 | 918 | 	return NULL; | 
 | 919 |     v = PyUnicode_AsEncodedString(unicode, encoding, errors); | 
 | 920 |     Py_DECREF(unicode); | 
 | 921 |     return v; | 
 | 922 | } | 
 | 923 |  | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 924 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, | 
 | 925 |                                     const char *encoding, | 
 | 926 |                                     const char *errors) | 
 | 927 | { | 
 | 928 |     PyObject *v; | 
 | 929 |  | 
 | 930 |     if (!PyUnicode_Check(unicode)) { | 
 | 931 |         PyErr_BadArgument(); | 
 | 932 |         goto onError; | 
 | 933 |     } | 
 | 934 |  | 
 | 935 |     if (encoding == NULL) | 
 | 936 | 	encoding = PyUnicode_GetDefaultEncoding(); | 
 | 937 |  | 
 | 938 |     /* Encode via the codec registry */ | 
 | 939 |     v = PyCodec_Encode(unicode, encoding, errors); | 
 | 940 |     if (v == NULL) | 
 | 941 |         goto onError; | 
 | 942 |     return v; | 
 | 943 |  | 
 | 944 |  onError: | 
 | 945 |     return NULL; | 
 | 946 | } | 
 | 947 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 948 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, | 
 | 949 |                                     const char *encoding, | 
 | 950 |                                     const char *errors) | 
 | 951 | { | 
 | 952 |     PyObject *v; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 953 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 954 |     if (!PyUnicode_Check(unicode)) { | 
 | 955 |         PyErr_BadArgument(); | 
 | 956 |         goto onError; | 
 | 957 |     } | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 958 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 959 |     if (encoding == NULL) | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 960 | 	encoding = PyUnicode_GetDefaultEncoding(); | 
 | 961 |  | 
 | 962 |     /* Shortcuts for common default encodings */ | 
 | 963 |     if (errors == NULL) { | 
 | 964 | 	if (strcmp(encoding, "utf-8") == 0) | 
| Jeremy Hylton | 9cea41c | 2001-05-29 17:13:15 +0000 | [diff] [blame] | 965 | 	    return PyUnicode_AsUTF8String(unicode); | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 966 | 	else if (strcmp(encoding, "latin-1") == 0) | 
 | 967 | 	    return PyUnicode_AsLatin1String(unicode); | 
| Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 968 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) | 
 | 969 | 	else if (strcmp(encoding, "mbcs") == 0) | 
 | 970 | 	    return PyUnicode_AsMBCSString(unicode); | 
 | 971 | #endif | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 972 | 	else if (strcmp(encoding, "ascii") == 0) | 
 | 973 | 	    return PyUnicode_AsASCIIString(unicode); | 
 | 974 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 975 |  | 
 | 976 |     /* Encode via the codec registry */ | 
 | 977 |     v = PyCodec_Encode(unicode, encoding, errors); | 
 | 978 |     if (v == NULL) | 
 | 979 |         goto onError; | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 980 |     if (!PyBytes_Check(v)) { | 
 | 981 |         if (PyString_Check(v)) { | 
 | 982 |             /* Old codec, turn it into bytes */ | 
 | 983 |             PyObject *b = PyBytes_FromObject(v); | 
 | 984 |             Py_DECREF(v); | 
 | 985 |             return b; | 
 | 986 |         } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 987 |         PyErr_Format(PyExc_TypeError, | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 988 |                      "encoder did not return a bytes object " | 
 | 989 |                      "(type=%.400s, encoding=%.20s, errors=%.20s)", | 
 | 990 |                      v->ob_type->tp_name, | 
 | 991 |                      encoding ? encoding : "NULL", | 
 | 992 |                      errors ? errors : "NULL"); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 993 |         Py_DECREF(v); | 
 | 994 |         goto onError; | 
 | 995 |     } | 
 | 996 |     return v; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 997 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 998 |  onError: | 
 | 999 |     return NULL; | 
 | 1000 | } | 
 | 1001 |  | 
| Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1002 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, | 
 | 1003 | 					    const char *errors) | 
 | 1004 | { | 
 | 1005 |     PyObject *v = ((PyUnicodeObject *)unicode)->defenc; | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1006 |     PyObject *b; | 
| Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1007 |     if (v) | 
 | 1008 |         return v; | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1009 |     if (errors != NULL) | 
 | 1010 |         Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString"); | 
 | 1011 |     if (errors == NULL) { | 
 | 1012 |         b = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), | 
 | 1013 |                                  PyUnicode_GET_SIZE(unicode), | 
 | 1014 |                                  NULL); | 
 | 1015 |     } | 
 | 1016 |     else { | 
 | 1017 |         b = PyUnicode_AsEncodedString(unicode, NULL, errors); | 
 | 1018 |     } | 
 | 1019 |     if (!b) | 
 | 1020 |         return NULL; | 
 | 1021 |     v = PyString_FromStringAndSize(PyBytes_AsString(b), | 
 | 1022 |                                    PyBytes_Size(b)); | 
 | 1023 |     Py_DECREF(b); | 
 | 1024 |     if (!errors) { | 
 | 1025 |         Py_XINCREF(v); | 
| Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1026 |         ((PyUnicodeObject *)unicode)->defenc = v; | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1027 |     } | 
| Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1028 |     return v; | 
 | 1029 | } | 
 | 1030 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1031 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) | 
 | 1032 | { | 
 | 1033 |     if (!PyUnicode_Check(unicode)) { | 
 | 1034 |         PyErr_BadArgument(); | 
 | 1035 |         goto onError; | 
 | 1036 |     } | 
 | 1037 |     return PyUnicode_AS_UNICODE(unicode); | 
 | 1038 |  | 
 | 1039 |  onError: | 
 | 1040 |     return NULL; | 
 | 1041 | } | 
 | 1042 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1043 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1044 | { | 
 | 1045 |     if (!PyUnicode_Check(unicode)) { | 
 | 1046 |         PyErr_BadArgument(); | 
 | 1047 |         goto onError; | 
 | 1048 |     } | 
 | 1049 |     return PyUnicode_GET_SIZE(unicode); | 
 | 1050 |  | 
 | 1051 |  onError: | 
 | 1052 |     return -1; | 
 | 1053 | } | 
 | 1054 |  | 
| Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1055 | const char *PyUnicode_GetDefaultEncoding(void) | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1056 | { | 
 | 1057 |     return unicode_default_encoding; | 
 | 1058 | } | 
 | 1059 |  | 
 | 1060 | int PyUnicode_SetDefaultEncoding(const char *encoding) | 
 | 1061 | { | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1062 |     if (strcmp(encoding, unicode_default_encoding) != 0) { | 
 | 1063 |         PyErr_Format(PyExc_ValueError, | 
 | 1064 |                      "Can only set default encoding to %s", | 
 | 1065 |                      unicode_default_encoding); | 
 | 1066 |         return -1; | 
 | 1067 |     } | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1068 |     return 0; | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1069 | } | 
 | 1070 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1071 | /* error handling callback helper: | 
 | 1072 |    build arguments, call the callback and check the arguments, | 
| Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1073 |    if no exception occurred, copy the replacement to the output | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1074 |    and adjust various state variables. | 
 | 1075 |    return 0 on success, -1 on error | 
 | 1076 | */ | 
 | 1077 |  | 
 | 1078 | static | 
 | 1079 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, | 
 | 1080 |                  const char *encoding, const char *reason, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1081 |                  const char *input, Py_ssize_t insize, Py_ssize_t *startinpos, Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, | 
 | 1082 |                  PyObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1083 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1084 |     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] | 1085 |  | 
 | 1086 |     PyObject *restuple = NULL; | 
 | 1087 |     PyObject *repunicode = NULL; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1088 |     Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); | 
 | 1089 |     Py_ssize_t requiredsize; | 
 | 1090 |     Py_ssize_t newpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1091 |     Py_UNICODE *repptr; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1092 |     Py_ssize_t repsize; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1093 |     int res = -1; | 
 | 1094 |  | 
 | 1095 |     if (*errorHandler == NULL) { | 
 | 1096 | 	*errorHandler = PyCodec_LookupError(errors); | 
 | 1097 | 	if (*errorHandler == NULL) | 
 | 1098 | 	   goto onError; | 
 | 1099 |     } | 
 | 1100 |  | 
 | 1101 |     if (*exceptionObject == NULL) { | 
 | 1102 |     	*exceptionObject = PyUnicodeDecodeError_Create( | 
 | 1103 | 	    encoding, input, insize, *startinpos, *endinpos, reason); | 
 | 1104 | 	if (*exceptionObject == NULL) | 
 | 1105 | 	   goto onError; | 
 | 1106 |     } | 
 | 1107 |     else { | 
 | 1108 | 	if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) | 
 | 1109 | 	    goto onError; | 
 | 1110 | 	if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) | 
 | 1111 | 	    goto onError; | 
 | 1112 | 	if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) | 
 | 1113 | 	    goto onError; | 
 | 1114 |     } | 
 | 1115 |  | 
 | 1116 |     restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); | 
 | 1117 |     if (restuple == NULL) | 
 | 1118 | 	goto onError; | 
 | 1119 |     if (!PyTuple_Check(restuple)) { | 
 | 1120 | 	PyErr_Format(PyExc_TypeError, &argparse[4]); | 
 | 1121 | 	goto onError; | 
 | 1122 |     } | 
 | 1123 |     if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) | 
 | 1124 | 	goto onError; | 
 | 1125 |     if (newpos<0) | 
| Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1126 | 	newpos = insize+newpos; | 
 | 1127 |     if (newpos<0 || newpos>insize) { | 
| Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 1128 | 	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] | 1129 | 	goto onError; | 
 | 1130 |     } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1131 |  | 
 | 1132 |     /* need more space? (at least enough for what we | 
 | 1133 |        have+the replacement+the rest of the string (starting | 
 | 1134 |        at the new input position), so we won't have to check space | 
 | 1135 |        when there are no errors in the rest of the string) */ | 
 | 1136 |     repptr = PyUnicode_AS_UNICODE(repunicode); | 
 | 1137 |     repsize = PyUnicode_GET_SIZE(repunicode); | 
 | 1138 |     requiredsize = *outpos + repsize + insize-newpos; | 
 | 1139 |     if (requiredsize > outsize) { | 
 | 1140 | 	if (requiredsize<2*outsize) | 
 | 1141 | 	    requiredsize = 2*outsize; | 
| Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1142 | 	if (PyUnicode_Resize(output, requiredsize) < 0) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1143 | 	    goto onError; | 
 | 1144 | 	*outptr = PyUnicode_AS_UNICODE(*output) + *outpos; | 
 | 1145 |     } | 
 | 1146 |     *endinpos = newpos; | 
 | 1147 |     *inptr = input + newpos; | 
 | 1148 |     Py_UNICODE_COPY(*outptr, repptr, repsize); | 
 | 1149 |     *outptr += repsize; | 
 | 1150 |     *outpos += repsize; | 
 | 1151 |     /* we made it! */ | 
 | 1152 |     res = 0; | 
 | 1153 |  | 
 | 1154 |     onError: | 
 | 1155 |     Py_XDECREF(restuple); | 
 | 1156 |     return res; | 
 | 1157 | } | 
 | 1158 |  | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1159 | /* --- UTF-7 Codec -------------------------------------------------------- */ | 
 | 1160 |  | 
 | 1161 | /* see RFC2152 for details */ | 
 | 1162 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1163 | static | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1164 | char utf7_special[128] = { | 
 | 1165 |     /* indicate whether a UTF-7 character is special i.e. cannot be directly | 
 | 1166 |        encoded: | 
 | 1167 | 	   0 - not special | 
 | 1168 | 	   1 - special | 
 | 1169 | 	   2 - whitespace (optional) | 
 | 1170 | 	   3 - RFC2152 Set O (optional) */ | 
 | 1171 |     1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, | 
 | 1172 |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 
 | 1173 |     2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, | 
 | 1174 |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, | 
 | 1175 |     3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 
 | 1176 |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, | 
 | 1177 |     3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 
 | 1178 |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, | 
 | 1179 |  | 
 | 1180 | }; | 
 | 1181 |  | 
| Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1182 | /* Note: The comparison (c) <= 0 is a trick to work-around gcc | 
 | 1183 |    warnings about the comparison always being false; since | 
 | 1184 |    utf7_special[0] is 1, we can safely make that one comparison | 
 | 1185 |    true  */ | 
 | 1186 |  | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1187 | #define SPECIAL(c, encodeO, encodeWS) \ | 
| Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1188 |     ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ | 
| Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1189 |      (encodeWS && (utf7_special[(c)] == 2)) || \ | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1190 |      (encodeO && (utf7_special[(c)] == 3))) | 
 | 1191 |  | 
| Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1192 | #define B64(n)  \ | 
 | 1193 |     ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) | 
 | 1194 | #define B64CHAR(c) \ | 
 | 1195 |     (isalnum(c) || (c) == '+' || (c) == '/') | 
 | 1196 | #define UB64(c) \ | 
 | 1197 |     ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ?                   \ | 
 | 1198 |      (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1199 |  | 
| Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1200 | #define ENCODE(out, ch, bits)                   \ | 
 | 1201 |     while (bits >= 6) {                         \ | 
 | 1202 |         *out++ = B64(ch >> (bits-6));           \ | 
 | 1203 |         bits -= 6;                              \ | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1204 |     } | 
 | 1205 |  | 
| Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1206 | #define DECODE(out, ch, bits, surrogate)                                \ | 
 | 1207 |     while (bits >= 16) {                                                \ | 
 | 1208 |         Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff);   \ | 
 | 1209 |         bits -= 16;                                                     \ | 
 | 1210 |         if (surrogate) {                                                \ | 
| Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1211 |             /* We have already generated an error for the high surrogate \ | 
 | 1212 |                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] | 1213 |             surrogate = 0;                                              \ | 
 | 1214 |         } else if (0xDC00 <= outCh && outCh <= 0xDFFF) {                \ | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1215 |             /* This is a surrogate pair. Unfortunately we can't represent \ | 
| Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1216 |                it in a 16-bit character */                              \ | 
 | 1217 |             surrogate = 1;                                              \ | 
 | 1218 |             errmsg = "code pairs are not supported";                    \ | 
 | 1219 |             goto utf7Error;                                             \ | 
 | 1220 |         } else {                                                        \ | 
 | 1221 |             *out++ = outCh;                                             \ | 
 | 1222 |         }                                                               \ | 
| Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1223 |     } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1224 |  | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1225 | PyObject *PyUnicode_DecodeUTF7(const char *s, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1226 | 			       Py_ssize_t size, | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1227 | 			       const char *errors) | 
 | 1228 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1229 |     const char *starts = s; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1230 |     Py_ssize_t startinpos; | 
 | 1231 |     Py_ssize_t endinpos; | 
 | 1232 |     Py_ssize_t outpos; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1233 |     const char *e; | 
 | 1234 |     PyUnicodeObject *unicode; | 
 | 1235 |     Py_UNICODE *p; | 
 | 1236 |     const char *errmsg = ""; | 
 | 1237 |     int inShift = 0; | 
 | 1238 |     unsigned int bitsleft = 0; | 
 | 1239 |     unsigned long charsleft = 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1240 |     int surrogate = 0; | 
 | 1241 |     PyObject *errorHandler = NULL; | 
 | 1242 |     PyObject *exc = NULL; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1243 |  | 
 | 1244 |     unicode = _PyUnicode_New(size); | 
 | 1245 |     if (!unicode) | 
 | 1246 |         return NULL; | 
 | 1247 |     if (size == 0) | 
 | 1248 |         return (PyObject *)unicode; | 
 | 1249 |  | 
 | 1250 |     p = unicode->str; | 
 | 1251 |     e = s + size; | 
 | 1252 |  | 
 | 1253 |     while (s < e) { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1254 |         Py_UNICODE ch; | 
 | 1255 |         restart: | 
 | 1256 |         ch = *s; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1257 |  | 
 | 1258 |         if (inShift) { | 
 | 1259 |             if ((ch == '-') || !B64CHAR(ch)) { | 
 | 1260 |                 inShift = 0; | 
 | 1261 |                 s++; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1262 |  | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1263 |                 /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); | 
 | 1264 |                 if (bitsleft >= 6) { | 
 | 1265 |                     /* The shift sequence has a partial character in it. If | 
 | 1266 |                        bitsleft < 6 then we could just classify it as padding | 
 | 1267 |                        but that is not the case here */ | 
 | 1268 |  | 
 | 1269 |                     errmsg = "partial character in shift sequence"; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1270 |                     goto utf7Error; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1271 |                 } | 
 | 1272 |                 /* According to RFC2152 the remaining bits should be zero. We | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1273 |                    choose to signal an error/insert a replacement character | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1274 |                    here so indicate the potential of a misencoded character. */ | 
 | 1275 |  | 
 | 1276 |                 /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ | 
 | 1277 |                 if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { | 
 | 1278 |                     errmsg = "non-zero padding bits in shift sequence"; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1279 |                     goto utf7Error; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1280 |                 } | 
 | 1281 |  | 
 | 1282 |                 if (ch == '-') { | 
 | 1283 |                     if ((s < e) && (*(s) == '-')) { | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1284 |                         *p++ = '-'; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1285 |                         inShift = 1; | 
 | 1286 |                     } | 
 | 1287 |                 } else if (SPECIAL(ch,0,0)) { | 
 | 1288 |                     errmsg = "unexpected special character"; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1289 | 	                goto utf7Error; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1290 |                 } else  { | 
 | 1291 |                     *p++ = ch; | 
 | 1292 |                 } | 
 | 1293 |             } else { | 
 | 1294 |                 charsleft = (charsleft << 6) | UB64(ch); | 
 | 1295 |                 bitsleft += 6; | 
 | 1296 |                 s++; | 
 | 1297 |                 /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); | 
 | 1298 |             } | 
 | 1299 |         } | 
 | 1300 |         else if ( ch == '+' ) { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1301 |             startinpos = s-starts; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1302 |             s++; | 
 | 1303 |             if (s < e && *s == '-') { | 
 | 1304 |                 s++; | 
 | 1305 |                 *p++ = '+'; | 
 | 1306 |             } else | 
 | 1307 |             { | 
 | 1308 |                 inShift = 1; | 
 | 1309 |                 bitsleft = 0; | 
 | 1310 |             } | 
 | 1311 |         } | 
 | 1312 |         else if (SPECIAL(ch,0,0)) { | 
 | 1313 |             errmsg = "unexpected special character"; | 
 | 1314 |             s++; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1315 | 	        goto utf7Error; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1316 |         } | 
 | 1317 |         else { | 
 | 1318 |             *p++ = ch; | 
 | 1319 |             s++; | 
 | 1320 |         } | 
 | 1321 |         continue; | 
 | 1322 |     utf7Error: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1323 |         outpos = p-PyUnicode_AS_UNICODE(unicode); | 
 | 1324 |         endinpos = s-starts; | 
 | 1325 |         if (unicode_decode_call_errorhandler( | 
 | 1326 |              errors, &errorHandler, | 
 | 1327 |              "utf7", errmsg, | 
 | 1328 |              starts, size, &startinpos, &endinpos, &exc, &s, | 
 | 1329 |              (PyObject **)&unicode, &outpos, &p)) | 
 | 1330 |         goto onError; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1331 |     } | 
 | 1332 |  | 
 | 1333 |     if (inShift) { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1334 |         outpos = p-PyUnicode_AS_UNICODE(unicode); | 
 | 1335 |         endinpos = size; | 
 | 1336 |         if (unicode_decode_call_errorhandler( | 
 | 1337 |              errors, &errorHandler, | 
 | 1338 |              "utf7", "unterminated shift sequence", | 
 | 1339 |              starts, size, &startinpos, &endinpos, &exc, &s, | 
 | 1340 |              (PyObject **)&unicode, &outpos, &p)) | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1341 |             goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1342 |         if (s < e) | 
 | 1343 |            goto restart; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1344 |     } | 
 | 1345 |  | 
| Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1346 |     if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1347 |         goto onError; | 
 | 1348 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1349 |     Py_XDECREF(errorHandler); | 
 | 1350 |     Py_XDECREF(exc); | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1351 |     return (PyObject *)unicode; | 
 | 1352 |  | 
 | 1353 | onError: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1354 |     Py_XDECREF(errorHandler); | 
 | 1355 |     Py_XDECREF(exc); | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1356 |     Py_DECREF(unicode); | 
 | 1357 |     return NULL; | 
 | 1358 | } | 
 | 1359 |  | 
 | 1360 |  | 
 | 1361 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1362 |                    Py_ssize_t size, | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1363 |                    int encodeSetO, | 
 | 1364 |                    int encodeWhiteSpace, | 
 | 1365 |                    const char *errors) | 
 | 1366 | { | 
 | 1367 |     PyObject *v; | 
 | 1368 |     /* It might be possible to tighten this worst case */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1369 |     Py_ssize_t cbAllocated = 5 * size; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1370 |     int inShift = 0; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1371 |     Py_ssize_t i = 0; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1372 |     unsigned int bitsleft = 0; | 
 | 1373 |     unsigned long charsleft = 0; | 
 | 1374 |     char * out; | 
 | 1375 |     char * start; | 
 | 1376 |  | 
 | 1377 |     if (size == 0) | 
| Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1378 | 	return PyBytes_FromStringAndSize(NULL, 0); | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1379 |  | 
| Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1380 |     v = PyBytes_FromStringAndSize(NULL, cbAllocated); | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1381 |     if (v == NULL) | 
 | 1382 |         return NULL; | 
 | 1383 |  | 
| Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1384 |     start = out = PyBytes_AS_STRING(v); | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1385 |     for (;i < size; ++i) { | 
 | 1386 |         Py_UNICODE ch = s[i]; | 
 | 1387 |  | 
 | 1388 |         if (!inShift) { | 
| Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1389 |             if (ch == '+') { | 
 | 1390 |                 *out++ = '+'; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1391 |                 *out++ = '-'; | 
 | 1392 |             } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { | 
 | 1393 |                 charsleft = ch; | 
 | 1394 |                 bitsleft = 16; | 
 | 1395 |                 *out++ = '+'; | 
| Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1396 |                 /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1397 |                 inShift = bitsleft > 0; | 
| Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1398 |             } else { | 
 | 1399 |                 *out++ = (char) ch; | 
 | 1400 |             } | 
 | 1401 |         } else { | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1402 |             if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { | 
 | 1403 |                 *out++ = B64(charsleft << (6-bitsleft)); | 
 | 1404 |                 charsleft = 0; | 
 | 1405 |                 bitsleft = 0; | 
 | 1406 |                 /* Characters not in the BASE64 set implicitly unshift the sequence | 
 | 1407 |                    so no '-' is required, except if the character is itself a '-' */ | 
 | 1408 |                 if (B64CHAR(ch) || ch == '-') { | 
 | 1409 |                     *out++ = '-'; | 
 | 1410 |                 } | 
 | 1411 |                 inShift = 0; | 
 | 1412 |                 *out++ = (char) ch; | 
 | 1413 |             } else { | 
 | 1414 |                 bitsleft += 16; | 
 | 1415 |                 charsleft = (charsleft << 16) | ch; | 
 | 1416 |                 /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); | 
 | 1417 |  | 
 | 1418 |                 /* If the next character is special then we dont' need to terminate | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1419 |                    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] | 1420 |                    or '-' then the shift sequence will be terminated implicitly and we | 
 | 1421 |                    don't have to insert a '-'. */ | 
 | 1422 |  | 
 | 1423 |                 if (bitsleft == 0) { | 
 | 1424 |                     if (i + 1 < size) { | 
 | 1425 |                         Py_UNICODE ch2 = s[i+1]; | 
 | 1426 |  | 
 | 1427 |                         if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1428 |  | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1429 |                         } else if (B64CHAR(ch2) || ch2 == '-') { | 
 | 1430 |                             *out++ = '-'; | 
 | 1431 |                             inShift = 0; | 
 | 1432 |                         } else { | 
 | 1433 |                             inShift = 0; | 
 | 1434 |                         } | 
 | 1435 |  | 
 | 1436 |                     } | 
 | 1437 |                     else { | 
 | 1438 |                         *out++ = '-'; | 
 | 1439 |                         inShift = 0; | 
 | 1440 |                     } | 
 | 1441 |                 } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1442 |             } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1443 |         } | 
| Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1444 |     } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1445 |     if (bitsleft) { | 
 | 1446 |         *out++= B64(charsleft << (6-bitsleft) ); | 
 | 1447 |         *out++ = '-'; | 
 | 1448 |     } | 
 | 1449 |  | 
| Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1450 |     if (PyBytes_Resize(v, out - start)) { | 
 | 1451 |         Py_DECREF(v); | 
 | 1452 |         return NULL; | 
 | 1453 |     } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1454 |     return v; | 
 | 1455 | } | 
 | 1456 |  | 
 | 1457 | #undef SPECIAL | 
 | 1458 | #undef B64 | 
 | 1459 | #undef B64CHAR | 
 | 1460 | #undef UB64 | 
 | 1461 | #undef ENCODE | 
 | 1462 | #undef DECODE | 
 | 1463 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1464 | /* --- UTF-8 Codec -------------------------------------------------------- */ | 
 | 1465 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1466 | static | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1467 | char utf8_code_length[256] = { | 
 | 1468 |     /* Map UTF-8 encoded prefix byte to sequence length.  zero means | 
 | 1469 |        illegal prefix.  see RFC 2279 for details */ | 
 | 1470 |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 
 | 1471 |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 
 | 1472 |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 
 | 1473 |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 
 | 1474 |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 
 | 1475 |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 
 | 1476 |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 
 | 1477 |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 
 | 1478 |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 
 | 1479 |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 
 | 1480 |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 
 | 1481 |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 
 | 1482 |     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | 
 | 1483 |     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | 
 | 1484 |     3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, | 
 | 1485 |     4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 | 
 | 1486 | }; | 
 | 1487 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1488 | PyObject *PyUnicode_DecodeUTF8(const char *s, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1489 | 			       Py_ssize_t size, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1490 | 			       const char *errors) | 
 | 1491 | { | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1492 |     return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); | 
 | 1493 | } | 
 | 1494 |  | 
 | 1495 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1496 | 			                Py_ssize_t size, | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1497 | 			                const char *errors, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1498 | 			                Py_ssize_t *consumed) | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1499 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1500 |     const char *starts = s; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1501 |     int n; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1502 |     Py_ssize_t startinpos; | 
 | 1503 |     Py_ssize_t endinpos; | 
 | 1504 |     Py_ssize_t outpos; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1505 |     const char *e; | 
 | 1506 |     PyUnicodeObject *unicode; | 
 | 1507 |     Py_UNICODE *p; | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1508 |     const char *errmsg = ""; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1509 |     PyObject *errorHandler = NULL; | 
 | 1510 |     PyObject *exc = NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1511 |  | 
 | 1512 |     /* Note: size will always be longer than the resulting Unicode | 
 | 1513 |        character count */ | 
 | 1514 |     unicode = _PyUnicode_New(size); | 
 | 1515 |     if (!unicode) | 
 | 1516 |         return NULL; | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1517 |     if (size == 0) { | 
 | 1518 |         if (consumed) | 
 | 1519 |             *consumed = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1520 |         return (PyObject *)unicode; | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1521 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1522 |  | 
 | 1523 |     /* Unpack UTF-8 encoded data */ | 
 | 1524 |     p = unicode->str; | 
 | 1525 |     e = s + size; | 
 | 1526 |  | 
 | 1527 |     while (s < e) { | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1528 |         Py_UCS4 ch = (unsigned char)*s; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1529 |  | 
 | 1530 |         if (ch < 0x80) { | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1531 |             *p++ = (Py_UNICODE)ch; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1532 |             s++; | 
 | 1533 |             continue; | 
 | 1534 |         } | 
 | 1535 |  | 
 | 1536 |         n = utf8_code_length[ch]; | 
 | 1537 |  | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1538 |         if (s + n > e) { | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1539 | 	    if (consumed) | 
 | 1540 | 		break; | 
 | 1541 | 	    else { | 
 | 1542 | 		errmsg = "unexpected end of data"; | 
 | 1543 | 		startinpos = s-starts; | 
 | 1544 | 		endinpos = size; | 
 | 1545 | 		goto utf8Error; | 
 | 1546 | 	    } | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1547 | 	} | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1548 |  | 
 | 1549 |         switch (n) { | 
 | 1550 |  | 
 | 1551 |         case 0: | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1552 |             errmsg = "unexpected code byte"; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1553 | 	    startinpos = s-starts; | 
 | 1554 | 	    endinpos = startinpos+1; | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1555 | 	    goto utf8Error; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1556 |  | 
 | 1557 |         case 1: | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1558 |             errmsg = "internal error"; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1559 | 	    startinpos = s-starts; | 
 | 1560 | 	    endinpos = startinpos+1; | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1561 | 	    goto utf8Error; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1562 |  | 
 | 1563 |         case 2: | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1564 |             if ((s[1] & 0xc0) != 0x80) { | 
 | 1565 |                 errmsg = "invalid data"; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1566 | 		startinpos = s-starts; | 
 | 1567 | 		endinpos = startinpos+2; | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1568 | 		goto utf8Error; | 
 | 1569 | 	    } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1570 |             ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1571 |             if (ch < 0x80) { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1572 | 		startinpos = s-starts; | 
 | 1573 | 		endinpos = startinpos+2; | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1574 |                 errmsg = "illegal encoding"; | 
 | 1575 | 		goto utf8Error; | 
 | 1576 | 	    } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1577 | 	    else | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1578 | 		*p++ = (Py_UNICODE)ch; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1579 |             break; | 
 | 1580 |  | 
 | 1581 |         case 3: | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1582 |             if ((s[1] & 0xc0) != 0x80 || | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1583 |                 (s[2] & 0xc0) != 0x80) { | 
 | 1584 |                 errmsg = "invalid data"; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1585 | 		startinpos = s-starts; | 
 | 1586 | 		endinpos = startinpos+3; | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1587 | 		goto utf8Error; | 
 | 1588 | 	    } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1589 |             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] | 1590 |             if (ch < 0x0800) { | 
 | 1591 | 		/* Note: UTF-8 encodings of surrogates are considered | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1592 | 		   legal UTF-8 sequences; | 
| Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1593 |  | 
 | 1594 | 		   XXX For wide builds (UCS-4) we should probably try | 
 | 1595 | 		       to recombine the surrogates into a single code | 
 | 1596 | 		       unit. | 
 | 1597 | 		*/ | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1598 |                 errmsg = "illegal encoding"; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1599 | 		startinpos = s-starts; | 
 | 1600 | 		endinpos = startinpos+3; | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1601 | 		goto utf8Error; | 
 | 1602 | 	    } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1603 | 	    else | 
| Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1604 | 		*p++ = (Py_UNICODE)ch; | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1605 |             break; | 
 | 1606 |  | 
 | 1607 |         case 4: | 
 | 1608 |             if ((s[1] & 0xc0) != 0x80 || | 
 | 1609 |                 (s[2] & 0xc0) != 0x80 || | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1610 |                 (s[3] & 0xc0) != 0x80) { | 
 | 1611 |                 errmsg = "invalid data"; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1612 | 		startinpos = s-starts; | 
 | 1613 | 		endinpos = startinpos+4; | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1614 | 		goto utf8Error; | 
 | 1615 | 	    } | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1616 |             ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + | 
 | 1617 |                  ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); | 
 | 1618 |             /* validate and convert to UTF-16 */ | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1619 |             if ((ch < 0x10000)        /* minimum value allowed for 4 | 
| Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1620 | 					 byte encoding */ | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1621 |                 || (ch > 0x10ffff))   /* maximum value allowed for | 
| Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1622 | 					 UTF-16 */ | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1623 | 	    { | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1624 |                 errmsg = "illegal encoding"; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1625 | 		startinpos = s-starts; | 
 | 1626 | 		endinpos = startinpos+4; | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1627 | 		goto utf8Error; | 
 | 1628 | 	    } | 
| Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1629 | #ifdef Py_UNICODE_WIDE | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1630 | 	    *p++ = (Py_UNICODE)ch; | 
 | 1631 | #else | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1632 |             /*  compute and append the two surrogates: */ | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1633 |  | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1634 |             /*  translate from 10000..10FFFF to 0..FFFF */ | 
 | 1635 |             ch -= 0x10000; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1636 |  | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1637 |             /*  high surrogate = top 10 bits added to D800 */ | 
 | 1638 |             *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1639 |  | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1640 |             /*  low surrogate = bottom 10 bits added to DC00 */ | 
| Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1641 |             *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1642 | #endif | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1643 |             break; | 
 | 1644 |  | 
 | 1645 |         default: | 
 | 1646 |             /* Other sizes are only needed for UCS-4 */ | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1647 |             errmsg = "unsupported Unicode code range"; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1648 | 	    startinpos = s-starts; | 
 | 1649 | 	    endinpos = startinpos+n; | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1650 | 	    goto utf8Error; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1651 |         } | 
 | 1652 |         s += n; | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1653 | 	continue; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1654 |  | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1655 |     utf8Error: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1656 |     outpos = p-PyUnicode_AS_UNICODE(unicode); | 
 | 1657 |     if (unicode_decode_call_errorhandler( | 
 | 1658 | 	     errors, &errorHandler, | 
 | 1659 | 	     "utf8", errmsg, | 
 | 1660 | 	     starts, size, &startinpos, &endinpos, &exc, &s, | 
 | 1661 | 	     (PyObject **)&unicode, &outpos, &p)) | 
 | 1662 | 	goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1663 |     } | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1664 |     if (consumed) | 
 | 1665 | 	*consumed = s-starts; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1666 |  | 
 | 1667 |     /* Adjust length */ | 
| Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1668 |     if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1669 |         goto onError; | 
 | 1670 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1671 |     Py_XDECREF(errorHandler); | 
 | 1672 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1673 |     return (PyObject *)unicode; | 
 | 1674 |  | 
 | 1675 | onError: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1676 |     Py_XDECREF(errorHandler); | 
 | 1677 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1678 |     Py_DECREF(unicode); | 
 | 1679 |     return NULL; | 
 | 1680 | } | 
 | 1681 |  | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1682 | /* Allocation strategy:  if the string is short, convert into a stack buffer | 
 | 1683 |    and allocate exactly as much space needed at the end.  Else allocate the | 
 | 1684 |    maximum possible needed (4 result bytes per Unicode character), and return | 
 | 1685 |    the excess memory at the end. | 
| Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1686 | */ | 
| Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1687 | PyObject * | 
 | 1688 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1689 | 		     Py_ssize_t size, | 
| Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1690 | 		     const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1691 | { | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1692 | #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] | 1693 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1694 |     Py_ssize_t i;           /* index into s of next input byte */ | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1695 |     PyObject *v;        /* result string object */ | 
 | 1696 |     char *p;            /* next free byte in output buffer */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1697 |     Py_ssize_t nallocated;  /* number of result bytes allocated */ | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1698 |     Py_ssize_t nneeded;        /* number of result bytes needed */ | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1699 |     char stackbuf[MAX_SHORT_UNICHARS * 4]; | 
| Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1700 |  | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1701 |     assert(s != NULL); | 
 | 1702 |     assert(size >= 0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1703 |  | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1704 |     if (size <= MAX_SHORT_UNICHARS) { | 
 | 1705 |         /* Write into the stack buffer; nallocated can't overflow. | 
 | 1706 |          * At the end, we'll allocate exactly as much heap space as it | 
 | 1707 |          * turns out we need. | 
 | 1708 |          */ | 
 | 1709 |         nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); | 
 | 1710 |         v = NULL;   /* will allocate after we're done */ | 
 | 1711 |         p = stackbuf; | 
 | 1712 |     } | 
 | 1713 |     else { | 
 | 1714 |         /* Overallocate on the heap, and give the excess back at the end. */ | 
 | 1715 |         nallocated = size * 4; | 
 | 1716 |         if (nallocated / 4 != size)  /* overflow! */ | 
 | 1717 |             return PyErr_NoMemory(); | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1718 |         v = PyBytes_FromStringAndSize(NULL, nallocated); | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1719 |         if (v == NULL) | 
 | 1720 |             return NULL; | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1721 |         p = PyBytes_AS_STRING(v); | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1722 |     } | 
| Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1723 |  | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1724 |     for (i = 0; i < size;) { | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1725 |         Py_UCS4 ch = s[i++]; | 
| Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1726 |  | 
| Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1727 |         if (ch < 0x80) | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1728 |             /* Encode ASCII */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1729 |             *p++ = (char) ch; | 
| Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1730 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1731 |         else if (ch < 0x0800) { | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1732 |             /* Encode Latin-1 */ | 
| Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 1733 |             *p++ = (char)(0xc0 | (ch >> 6)); | 
 | 1734 |             *p++ = (char)(0x80 | (ch & 0x3f)); | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1735 |         } | 
| Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1736 |         else { | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1737 |             /* Encode UCS2 Unicode ordinals */ | 
 | 1738 |             if (ch < 0x10000) { | 
 | 1739 |                 /* Special case: check for high surrogate */ | 
 | 1740 |                 if (0xD800 <= ch && ch <= 0xDBFF && i != size) { | 
 | 1741 |                     Py_UCS4 ch2 = s[i]; | 
 | 1742 |                     /* Check for low surrogate and combine the two to | 
 | 1743 |                        form a UCS4 value */ | 
 | 1744 |                     if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { | 
| Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1745 |                         ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1746 |                         i++; | 
 | 1747 |                         goto encodeUCS4; | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1748 |                     } | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1749 |                     /* Fall through: handles isolated high surrogates */ | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1750 |                 } | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1751 |                 *p++ = (char)(0xe0 | (ch >> 12)); | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1752 |                 *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); | 
 | 1753 |                 *p++ = (char)(0x80 | (ch & 0x3f)); | 
 | 1754 |                 continue; | 
 | 1755 |     	    } | 
 | 1756 | encodeUCS4: | 
 | 1757 |             /* Encode UCS4 Unicode ordinals */ | 
 | 1758 |             *p++ = (char)(0xf0 | (ch >> 18)); | 
 | 1759 |             *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); | 
 | 1760 |             *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); | 
 | 1761 |             *p++ = (char)(0x80 | (ch & 0x3f)); | 
 | 1762 |         } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1763 |     } | 
| Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 1764 |  | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1765 |     if (v == NULL) { | 
 | 1766 |         /* This was stack allocated. */ | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1767 |         nneeded = p - stackbuf; | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1768 |         assert(nneeded <= nallocated); | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1769 |         v = PyBytes_FromStringAndSize(stackbuf, nneeded); | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1770 |     } | 
 | 1771 |     else { | 
 | 1772 |     	/* Cut back to size actually needed. */ | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1773 |         nneeded = p - PyBytes_AS_STRING(v); | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1774 |         assert(nneeded <= nallocated); | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1775 |         PyBytes_Resize(v, nneeded); | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1776 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1777 |     return v; | 
| Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1778 |  | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1779 | #undef MAX_SHORT_UNICHARS | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1780 | } | 
 | 1781 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1782 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) | 
 | 1783 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1784 |     if (!PyUnicode_Check(unicode)) { | 
 | 1785 |         PyErr_BadArgument(); | 
 | 1786 |         return NULL; | 
 | 1787 |     } | 
| Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 1788 |     return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), | 
 | 1789 | 				PyUnicode_GET_SIZE(unicode), | 
 | 1790 | 				NULL); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1791 | } | 
 | 1792 |  | 
 | 1793 | /* --- UTF-16 Codec ------------------------------------------------------- */ | 
 | 1794 |  | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1795 | PyObject * | 
 | 1796 | PyUnicode_DecodeUTF16(const char *s, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1797 | 		      Py_ssize_t size, | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1798 | 		      const char *errors, | 
 | 1799 | 		      int *byteorder) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1800 | { | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1801 |     return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); | 
 | 1802 | } | 
 | 1803 |  | 
 | 1804 | PyObject * | 
 | 1805 | PyUnicode_DecodeUTF16Stateful(const char *s, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1806 | 			      Py_ssize_t size, | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1807 | 			      const char *errors, | 
 | 1808 | 			      int *byteorder, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1809 | 			      Py_ssize_t *consumed) | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1810 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1811 |     const char *starts = s; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1812 |     Py_ssize_t startinpos; | 
 | 1813 |     Py_ssize_t endinpos; | 
 | 1814 |     Py_ssize_t outpos; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1815 |     PyUnicodeObject *unicode; | 
 | 1816 |     Py_UNICODE *p; | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1817 |     const unsigned char *q, *e; | 
 | 1818 |     int bo = 0;       /* assume native ordering by default */ | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1819 |     const char *errmsg = ""; | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1820 |     /* Offsets from q for retrieving byte pairs in the right order. */ | 
 | 1821 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN | 
 | 1822 |     int ihi = 1, ilo = 0; | 
 | 1823 | #else | 
 | 1824 |     int ihi = 0, ilo = 1; | 
 | 1825 | #endif | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1826 |     PyObject *errorHandler = NULL; | 
 | 1827 |     PyObject *exc = NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1828 |  | 
 | 1829 |     /* Note: size will always be longer than the resulting Unicode | 
 | 1830 |        character count */ | 
 | 1831 |     unicode = _PyUnicode_New(size); | 
 | 1832 |     if (!unicode) | 
 | 1833 |         return NULL; | 
 | 1834 |     if (size == 0) | 
 | 1835 |         return (PyObject *)unicode; | 
 | 1836 |  | 
 | 1837 |     /* Unpack UTF-16 encoded data */ | 
 | 1838 |     p = unicode->str; | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1839 |     q = (unsigned char *)s; | 
 | 1840 |     e = q + size; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1841 |  | 
 | 1842 |     if (byteorder) | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1843 |         bo = *byteorder; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1844 |  | 
| Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1845 |     /* Check for BOM marks (U+FEFF) in the input and adjust current | 
 | 1846 |        byte order setting accordingly. In native mode, the leading BOM | 
 | 1847 |        mark is skipped, in all other modes, it is copied to the output | 
 | 1848 |        stream as-is (giving a ZWNBSP character). */ | 
 | 1849 |     if (bo == 0) { | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1850 |         if (size >= 2) { | 
 | 1851 |             const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; | 
| Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1852 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1853 | 	    if (bom == 0xFEFF) { | 
 | 1854 | 		q += 2; | 
 | 1855 | 		bo = -1; | 
 | 1856 | 	    } | 
 | 1857 | 	    else if (bom == 0xFFFE) { | 
 | 1858 | 		q += 2; | 
 | 1859 | 		bo = 1; | 
 | 1860 | 	    } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1861 | #else | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1862 | 	    if (bom == 0xFEFF) { | 
 | 1863 | 		q += 2; | 
 | 1864 | 		bo = 1; | 
 | 1865 | 	    } | 
 | 1866 | 	    else if (bom == 0xFFFE) { | 
 | 1867 | 		q += 2; | 
 | 1868 | 		bo = -1; | 
 | 1869 | 	    } | 
| Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1870 | #endif | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1871 | 	} | 
| Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1872 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1873 |  | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1874 |     if (bo == -1) { | 
 | 1875 |         /* force LE */ | 
 | 1876 |         ihi = 1; | 
 | 1877 |         ilo = 0; | 
 | 1878 |     } | 
 | 1879 |     else if (bo == 1) { | 
 | 1880 |         /* force BE */ | 
 | 1881 |         ihi = 0; | 
 | 1882 |         ilo = 1; | 
 | 1883 |     } | 
 | 1884 |  | 
 | 1885 |     while (q < e) { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1886 | 	Py_UNICODE ch; | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1887 | 	/* remaining bytes at the end? (size should be even) */ | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1888 | 	if (e-q<2) { | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1889 | 	    if (consumed) | 
 | 1890 | 		break; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1891 | 	    errmsg = "truncated data"; | 
 | 1892 | 	    startinpos = ((const char *)q)-starts; | 
 | 1893 | 	    endinpos = ((const char *)e)-starts; | 
 | 1894 | 	    goto utf16Error; | 
 | 1895 | 	    /* The remaining input chars are ignored if the callback | 
 | 1896 | 	       chooses to skip the input */ | 
 | 1897 | 	} | 
 | 1898 | 	ch = (q[ihi] << 8) | q[ilo]; | 
 | 1899 |  | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1900 | 	q += 2; | 
 | 1901 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1902 | 	if (ch < 0xD800 || ch > 0xDFFF) { | 
 | 1903 | 	    *p++ = ch; | 
 | 1904 | 	    continue; | 
 | 1905 | 	} | 
 | 1906 |  | 
 | 1907 | 	/* UTF-16 code pair: */ | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1908 | 	if (q >= e) { | 
 | 1909 | 	    errmsg = "unexpected end of data"; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1910 | 	    startinpos = (((const char *)q)-2)-starts; | 
 | 1911 | 	    endinpos = ((const char *)e)-starts; | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1912 | 	    goto utf16Error; | 
 | 1913 | 	} | 
| Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 1914 | 	if (0xD800 <= ch && ch <= 0xDBFF) { | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1915 | 	    Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; | 
 | 1916 | 	    q += 2; | 
| Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 1917 | 	    if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { | 
| Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1918 | #ifndef Py_UNICODE_WIDE | 
| Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1919 | 		*p++ = ch; | 
 | 1920 | 		*p++ = ch2; | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1921 | #else | 
 | 1922 | 		*p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1923 | #endif | 
| Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1924 | 		continue; | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1925 | 	    } | 
 | 1926 | 	    else { | 
 | 1927 |                 errmsg = "illegal UTF-16 surrogate"; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1928 | 		startinpos = (((const char *)q)-4)-starts; | 
 | 1929 | 		endinpos = startinpos+2; | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1930 | 		goto utf16Error; | 
 | 1931 | 	    } | 
 | 1932 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1933 | 	} | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1934 | 	errmsg = "illegal encoding"; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1935 | 	startinpos = (((const char *)q)-2)-starts; | 
 | 1936 | 	endinpos = startinpos+2; | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1937 | 	/* Fall through to report the error */ | 
 | 1938 |  | 
 | 1939 |     utf16Error: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1940 | 	outpos = p-PyUnicode_AS_UNICODE(unicode); | 
 | 1941 | 	if (unicode_decode_call_errorhandler( | 
 | 1942 | 	         errors, &errorHandler, | 
 | 1943 | 	         "utf16", errmsg, | 
 | 1944 | 	         starts, size, &startinpos, &endinpos, &exc, (const char **)&q, | 
 | 1945 | 	         (PyObject **)&unicode, &outpos, &p)) | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1946 | 	    goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1947 |     } | 
 | 1948 |  | 
 | 1949 |     if (byteorder) | 
 | 1950 |         *byteorder = bo; | 
 | 1951 |  | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1952 |     if (consumed) | 
 | 1953 | 	*consumed = (const char *)q-starts; | 
 | 1954 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1955 |     /* Adjust length */ | 
| Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1956 |     if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1957 |         goto onError; | 
 | 1958 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1959 |     Py_XDECREF(errorHandler); | 
 | 1960 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1961 |     return (PyObject *)unicode; | 
 | 1962 |  | 
 | 1963 | onError: | 
 | 1964 |     Py_DECREF(unicode); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1965 |     Py_XDECREF(errorHandler); | 
 | 1966 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1967 |     return NULL; | 
 | 1968 | } | 
 | 1969 |  | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1970 | PyObject * | 
 | 1971 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1972 | 		      Py_ssize_t size, | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1973 | 		      const char *errors, | 
 | 1974 | 		      int byteorder) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1975 | { | 
 | 1976 |     PyObject *v; | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1977 |     unsigned char *p; | 
| Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1978 | #ifdef Py_UNICODE_WIDE | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1979 |     int i, pairs; | 
| Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1980 | #else | 
 | 1981 |     const int pairs = 0; | 
 | 1982 | #endif | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1983 |     /* Offsets from p for storing byte pairs in the right order. */ | 
 | 1984 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN | 
 | 1985 |     int ihi = 1, ilo = 0; | 
 | 1986 | #else | 
 | 1987 |     int ihi = 0, ilo = 1; | 
 | 1988 | #endif | 
 | 1989 |  | 
 | 1990 | #define STORECHAR(CH)                   \ | 
 | 1991 |     do {                                \ | 
 | 1992 |         p[ihi] = ((CH) >> 8) & 0xff;    \ | 
 | 1993 |         p[ilo] = (CH) & 0xff;           \ | 
 | 1994 |         p += 2;                         \ | 
 | 1995 |     } while(0) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1996 |  | 
| Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1997 | #ifdef Py_UNICODE_WIDE | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1998 |     for (i = pairs = 0; i < size; i++) | 
 | 1999 | 	if (s[i] >= 0x10000) | 
 | 2000 | 	    pairs++; | 
| Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2001 | #endif | 
| Walter Dörwald | 3cc3452 | 2007-05-04 10:48:27 +0000 | [diff] [blame] | 2002 |     v = PyBytes_FromStringAndSize(NULL, | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2003 | 		  2 * (size + pairs + (byteorder == 0))); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2004 |     if (v == NULL) | 
 | 2005 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2006 |  | 
| Walter Dörwald | 3cc3452 | 2007-05-04 10:48:27 +0000 | [diff] [blame] | 2007 |     p = (unsigned char *)PyBytes_AS_STRING(v); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2008 |     if (byteorder == 0) | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2009 | 	STORECHAR(0xFEFF); | 
| Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2010 |     if (size == 0) | 
| Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2011 |         return v; | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2012 |  | 
 | 2013 |     if (byteorder == -1) { | 
 | 2014 |         /* force LE */ | 
 | 2015 |         ihi = 1; | 
 | 2016 |         ilo = 0; | 
 | 2017 |     } | 
 | 2018 |     else if (byteorder == 1) { | 
 | 2019 |         /* force BE */ | 
 | 2020 |         ihi = 0; | 
 | 2021 |         ilo = 1; | 
 | 2022 |     } | 
 | 2023 |  | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2024 |     while (size-- > 0) { | 
 | 2025 | 	Py_UNICODE ch = *s++; | 
 | 2026 | 	Py_UNICODE ch2 = 0; | 
| Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2027 | #ifdef Py_UNICODE_WIDE | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2028 | 	if (ch >= 0x10000) { | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2029 | 	    ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); | 
 | 2030 | 	    ch  = 0xD800 | ((ch-0x10000) >> 10); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2031 | 	} | 
| Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2032 | #endif | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2033 |         STORECHAR(ch); | 
 | 2034 |         if (ch2) | 
 | 2035 |             STORECHAR(ch2); | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2036 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2037 |     return v; | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2038 | #undef STORECHAR | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2039 | } | 
 | 2040 |  | 
 | 2041 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) | 
 | 2042 | { | 
 | 2043 |     if (!PyUnicode_Check(unicode)) { | 
 | 2044 |         PyErr_BadArgument(); | 
 | 2045 |         return NULL; | 
 | 2046 |     } | 
 | 2047 |     return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), | 
 | 2048 | 				 PyUnicode_GET_SIZE(unicode), | 
 | 2049 | 				 NULL, | 
 | 2050 | 				 0); | 
 | 2051 | } | 
 | 2052 |  | 
 | 2053 | /* --- Unicode Escape Codec ----------------------------------------------- */ | 
 | 2054 |  | 
| Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2055 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; | 
| Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 2056 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2057 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2058 | 					Py_ssize_t size, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2059 | 					const char *errors) | 
 | 2060 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2061 |     const char *starts = s; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2062 |     Py_ssize_t startinpos; | 
 | 2063 |     Py_ssize_t endinpos; | 
 | 2064 |     Py_ssize_t outpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2065 |     int i; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2066 |     PyUnicodeObject *v; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2067 |     Py_UNICODE *p; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2068 |     const char *end; | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2069 |     char* message; | 
 | 2070 |     Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2071 |     PyObject *errorHandler = NULL; | 
 | 2072 |     PyObject *exc = NULL; | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2073 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2074 |     /* Escaped strings will always be longer than the resulting | 
 | 2075 |        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] | 2076 |        length after conversion to the true value. | 
 | 2077 |        (but if the error callback returns a long replacement string | 
 | 2078 |        we'll have to allocate more space) */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2079 |     v = _PyUnicode_New(size); | 
 | 2080 |     if (v == NULL) | 
 | 2081 |         goto onError; | 
 | 2082 |     if (size == 0) | 
 | 2083 |         return (PyObject *)v; | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2084 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2085 |     p = PyUnicode_AS_UNICODE(v); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2086 |     end = s + size; | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2087 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2088 |     while (s < end) { | 
 | 2089 |         unsigned char c; | 
| Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2090 |         Py_UNICODE x; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2091 |         int digits; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2092 |  | 
 | 2093 |         /* Non-escape characters are interpreted as Unicode ordinals */ | 
 | 2094 |         if (*s != '\\') { | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2095 |             *p++ = (unsigned char) *s++; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2096 |             continue; | 
 | 2097 |         } | 
 | 2098 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2099 |         startinpos = s-starts; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2100 |         /* \ - Escapes */ | 
 | 2101 |         s++; | 
 | 2102 |         switch (*s++) { | 
 | 2103 |  | 
 | 2104 |         /* \x escapes */ | 
 | 2105 |         case '\n': break; | 
 | 2106 |         case '\\': *p++ = '\\'; break; | 
 | 2107 |         case '\'': *p++ = '\''; break; | 
 | 2108 |         case '\"': *p++ = '\"'; break; | 
 | 2109 |         case 'b': *p++ = '\b'; break; | 
 | 2110 |         case 'f': *p++ = '\014'; break; /* FF */ | 
 | 2111 |         case 't': *p++ = '\t'; break; | 
 | 2112 |         case 'n': *p++ = '\n'; break; | 
 | 2113 |         case 'r': *p++ = '\r'; break; | 
 | 2114 |         case 'v': *p++ = '\013'; break; /* VT */ | 
 | 2115 |         case 'a': *p++ = '\007'; break; /* BEL, not classic C */ | 
 | 2116 |  | 
 | 2117 |         /* \OOO (octal) escapes */ | 
 | 2118 |         case '0': case '1': case '2': case '3': | 
 | 2119 |         case '4': case '5': case '6': case '7': | 
| Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2120 |             x = s[-1] - '0'; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2121 |             if ('0' <= *s && *s <= '7') { | 
| Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2122 |                 x = (x<<3) + *s++ - '0'; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2123 |                 if ('0' <= *s && *s <= '7') | 
| Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2124 |                     x = (x<<3) + *s++ - '0'; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2125 |             } | 
| Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2126 |             *p++ = x; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2127 |             break; | 
 | 2128 |  | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2129 |         /* hex escapes */ | 
 | 2130 |         /* \xXX */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2131 |         case 'x': | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2132 |             digits = 2; | 
 | 2133 |             message = "truncated \\xXX escape"; | 
 | 2134 |             goto hexescape; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2135 |  | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2136 |         /* \uXXXX */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2137 |         case 'u': | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2138 |             digits = 4; | 
 | 2139 |             message = "truncated \\uXXXX escape"; | 
 | 2140 |             goto hexescape; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2141 |  | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2142 |         /* \UXXXXXXXX */ | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2143 |         case 'U': | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2144 |             digits = 8; | 
 | 2145 |             message = "truncated \\UXXXXXXXX escape"; | 
 | 2146 |         hexescape: | 
 | 2147 |             chr = 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2148 |             outpos = p-PyUnicode_AS_UNICODE(v); | 
 | 2149 |             if (s+digits>end) { | 
 | 2150 |                 endinpos = size; | 
 | 2151 |                 if (unicode_decode_call_errorhandler( | 
 | 2152 |                     errors, &errorHandler, | 
 | 2153 |                     "unicodeescape", "end of string in escape sequence", | 
 | 2154 |                     starts, size, &startinpos, &endinpos, &exc, &s, | 
 | 2155 |                     (PyObject **)&v, &outpos, &p)) | 
 | 2156 |                     goto onError; | 
 | 2157 |                 goto nextByte; | 
 | 2158 |             } | 
 | 2159 |             for (i = 0; i < digits; ++i) { | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2160 |                 c = (unsigned char) s[i]; | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2161 |                 if (!isxdigit(c)) { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2162 |                     endinpos = (s+i+1)-starts; | 
 | 2163 |                     if (unicode_decode_call_errorhandler( | 
 | 2164 |                         errors, &errorHandler, | 
 | 2165 |                         "unicodeescape", message, | 
 | 2166 |                         starts, size, &startinpos, &endinpos, &exc, &s, | 
 | 2167 |                         (PyObject **)&v, &outpos, &p)) | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2168 |                         goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2169 |                     goto nextByte; | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2170 |                 } | 
 | 2171 |                 chr = (chr<<4) & ~0xF; | 
 | 2172 |                 if (c >= '0' && c <= '9') | 
 | 2173 |                     chr += c - '0'; | 
 | 2174 |                 else if (c >= 'a' && c <= 'f') | 
 | 2175 |                     chr += 10 + c - 'a'; | 
 | 2176 |                 else | 
 | 2177 |                     chr += 10 + c - 'A'; | 
 | 2178 |             } | 
 | 2179 |             s += i; | 
| Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 2180 |             if (chr == 0xffffffff && PyErr_Occurred()) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2181 |                 /* _decoding_error will have already written into the | 
 | 2182 |                    target buffer. */ | 
 | 2183 |                 break; | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2184 |         store: | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2185 |             /* when we get here, chr is a 32-bit unicode character */ | 
 | 2186 |             if (chr <= 0xffff) | 
 | 2187 |                 /* UCS-2 character */ | 
 | 2188 |                 *p++ = (Py_UNICODE) chr; | 
 | 2189 |             else if (chr <= 0x10ffff) { | 
| Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2190 |                 /* UCS-4 character. Either store directly, or as | 
| Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2191 |                    surrogate pair. */ | 
| Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2192 | #ifdef Py_UNICODE_WIDE | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2193 |                 *p++ = chr; | 
 | 2194 | #else | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2195 |                 chr -= 0x10000L; | 
 | 2196 |                 *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); | 
| Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2197 |                 *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2198 | #endif | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2199 |             } else { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2200 |                 endinpos = s-starts; | 
 | 2201 |                 outpos = p-PyUnicode_AS_UNICODE(v); | 
 | 2202 |                 if (unicode_decode_call_errorhandler( | 
 | 2203 |                     errors, &errorHandler, | 
 | 2204 |                     "unicodeescape", "illegal Unicode character", | 
 | 2205 |                     starts, size, &startinpos, &endinpos, &exc, &s, | 
 | 2206 |                     (PyObject **)&v, &outpos, &p)) | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2207 |                     goto onError; | 
 | 2208 |             } | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2209 |             break; | 
 | 2210 |  | 
 | 2211 |         /* \N{name} */ | 
 | 2212 |         case 'N': | 
 | 2213 |             message = "malformed \\N character escape"; | 
 | 2214 |             if (ucnhash_CAPI == NULL) { | 
 | 2215 |                 /* load the unicode data module */ | 
| Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2216 |                 PyObject *m, *api; | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2217 |                 m = PyImport_ImportModule("unicodedata"); | 
 | 2218 |                 if (m == NULL) | 
 | 2219 |                     goto ucnhashError; | 
| Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2220 |                 api = PyObject_GetAttrString(m, "ucnhash_CAPI"); | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2221 |                 Py_DECREF(m); | 
| Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2222 |                 if (api == NULL) | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2223 |                     goto ucnhashError; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2224 |                 ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); | 
| Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2225 |                 Py_DECREF(api); | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2226 |                 if (ucnhash_CAPI == NULL) | 
 | 2227 |                     goto ucnhashError; | 
 | 2228 |             } | 
 | 2229 |             if (*s == '{') { | 
 | 2230 |                 const char *start = s+1; | 
 | 2231 |                 /* look for the closing brace */ | 
 | 2232 |                 while (*s != '}' && s < end) | 
 | 2233 |                     s++; | 
 | 2234 |                 if (s > start && s < end && *s == '}') { | 
 | 2235 |                     /* found a name.  look it up in the unicode database */ | 
 | 2236 |                     message = "unknown Unicode character name"; | 
 | 2237 |                     s++; | 
| Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 2238 |                     if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2239 |                         goto store; | 
 | 2240 |                 } | 
 | 2241 |             } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2242 |             endinpos = s-starts; | 
 | 2243 |             outpos = p-PyUnicode_AS_UNICODE(v); | 
 | 2244 |             if (unicode_decode_call_errorhandler( | 
 | 2245 |                 errors, &errorHandler, | 
 | 2246 |                 "unicodeescape", message, | 
 | 2247 |                 starts, size, &startinpos, &endinpos, &exc, &s, | 
 | 2248 |                 (PyObject **)&v, &outpos, &p)) | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2249 |                 goto onError; | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2250 |             break; | 
 | 2251 |  | 
 | 2252 |         default: | 
| Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2253 |             if (s > end) { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2254 |                 message = "\\ at end of string"; | 
 | 2255 |                 s--; | 
 | 2256 |                 endinpos = s-starts; | 
 | 2257 |                 outpos = p-PyUnicode_AS_UNICODE(v); | 
 | 2258 |                 if (unicode_decode_call_errorhandler( | 
 | 2259 |                     errors, &errorHandler, | 
 | 2260 |                     "unicodeescape", message, | 
 | 2261 |                     starts, size, &startinpos, &endinpos, &exc, &s, | 
 | 2262 |                     (PyObject **)&v, &outpos, &p)) | 
| Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2263 |                     goto onError; | 
 | 2264 |             } | 
 | 2265 |             else { | 
 | 2266 |                 *p++ = '\\'; | 
 | 2267 |                 *p++ = (unsigned char)s[-1]; | 
 | 2268 |             } | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2269 |             break; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2270 |         } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2271 |         nextByte: | 
 | 2272 |         ; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2273 |     } | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2274 |     if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2275 |         goto onError; | 
| Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 2276 |     Py_XDECREF(errorHandler); | 
 | 2277 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2278 |     return (PyObject *)v; | 
| Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2279 |  | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2280 | ucnhashError: | 
| Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2281 |     PyErr_SetString( | 
 | 2282 |         PyExc_UnicodeError, | 
 | 2283 |         "\\N escapes not supported (can't load unicodedata module)" | 
 | 2284 |         ); | 
| Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2285 |     Py_XDECREF(v); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2286 |     Py_XDECREF(errorHandler); | 
 | 2287 |     Py_XDECREF(exc); | 
| Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 2288 |     return NULL; | 
 | 2289 |  | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2290 | onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2291 |     Py_XDECREF(v); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2292 |     Py_XDECREF(errorHandler); | 
 | 2293 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2294 |     return NULL; | 
 | 2295 | } | 
 | 2296 |  | 
 | 2297 | /* Return a Unicode-Escape string version of the Unicode object. | 
 | 2298 |  | 
 | 2299 |    If quotes is true, the string is enclosed in u"" or u'' quotes as | 
 | 2300 |    appropriate. | 
 | 2301 |  | 
 | 2302 | */ | 
 | 2303 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2304 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, | 
 | 2305 |                                       Py_ssize_t size, | 
 | 2306 |                                       Py_UNICODE ch) | 
 | 2307 | { | 
 | 2308 |     /* like wcschr, but doesn't stop at NULL characters */ | 
 | 2309 |  | 
 | 2310 |     while (size-- > 0) { | 
 | 2311 |         if (*s == ch) | 
 | 2312 |             return s; | 
 | 2313 |         s++; | 
 | 2314 |     } | 
 | 2315 |  | 
 | 2316 |     return NULL; | 
 | 2317 | } | 
| Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 2318 |  | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2319 | static const char *hexdigits = "0123456789abcdef"; | 
 | 2320 |  | 
 | 2321 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, | 
 | 2322 | 					Py_ssize_t size) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2323 | { | 
 | 2324 |     PyObject *repr; | 
 | 2325 |     char *p; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2326 |  | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2327 |     /* XXX(nnorwitz): rather than over-allocating, it would be | 
 | 2328 |        better to choose a different scheme.  Perhaps scan the | 
 | 2329 |        first N-chars of the string and allocate based on that size. | 
 | 2330 |     */ | 
 | 2331 |     /* Initial allocation is based on the longest-possible unichr | 
 | 2332 |        escape. | 
 | 2333 |  | 
 | 2334 |        In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source | 
 | 2335 |        unichr, so in this case it's the longest unichr escape. In | 
 | 2336 |        narrow (UTF-16) builds this is five chars per source unichr | 
 | 2337 |        since there are two unichrs in the surrogate pair, so in narrow | 
 | 2338 |        (UTF-16) builds it's not the longest unichr escape. | 
 | 2339 |  | 
 | 2340 |        In wide or narrow builds '\uxxxx' is 6 chars per source unichr, | 
 | 2341 |        so in the narrow (UTF-16) build case it's the longest unichr | 
 | 2342 |        escape. | 
 | 2343 |     */ | 
 | 2344 |  | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2345 |     repr = PyBytes_FromStringAndSize(NULL, | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2346 | #ifdef Py_UNICODE_WIDE | 
 | 2347 |         + 10*size | 
 | 2348 | #else | 
 | 2349 |         + 6*size | 
 | 2350 | #endif | 
 | 2351 |         + 1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2352 |     if (repr == NULL) | 
 | 2353 |         return NULL; | 
 | 2354 |  | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2355 |     p = PyBytes_AS_STRING(repr); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2356 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2357 |     while (size-- > 0) { | 
 | 2358 |         Py_UNICODE ch = *s++; | 
| Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2359 |  | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2360 |         /* Escape backslashes */ | 
 | 2361 |         if (ch == '\\') { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2362 |             *p++ = '\\'; | 
 | 2363 |             *p++ = (char) ch; | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2364 |             continue; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2365 |         } | 
| Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2366 |  | 
| Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 2367 | #ifdef Py_UNICODE_WIDE | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2368 |         /* Map 21-bit characters to '\U00xxxxxx' */ | 
 | 2369 |         else if (ch >= 0x10000) { | 
 | 2370 |             *p++ = '\\'; | 
 | 2371 |             *p++ = 'U'; | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2372 |             *p++ = hexdigits[(ch >> 28) & 0x0000000F]; | 
 | 2373 |             *p++ = hexdigits[(ch >> 24) & 0x0000000F]; | 
 | 2374 |             *p++ = hexdigits[(ch >> 20) & 0x0000000F]; | 
 | 2375 |             *p++ = hexdigits[(ch >> 16) & 0x0000000F]; | 
 | 2376 |             *p++ = hexdigits[(ch >> 12) & 0x0000000F]; | 
 | 2377 |             *p++ = hexdigits[(ch >> 8) & 0x0000000F]; | 
 | 2378 |             *p++ = hexdigits[(ch >> 4) & 0x0000000F]; | 
 | 2379 |             *p++ = hexdigits[ch & 0x0000000F]; | 
| Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2380 | 	    continue; | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2381 |         } | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2382 | #else | 
 | 2383 | 	/* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ | 
| Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2384 | 	else if (ch >= 0xD800 && ch < 0xDC00) { | 
 | 2385 | 	    Py_UNICODE ch2; | 
 | 2386 | 	    Py_UCS4 ucs; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2387 |  | 
| Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2388 | 	    ch2 = *s++; | 
 | 2389 | 	    size--; | 
 | 2390 | 	    if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { | 
 | 2391 | 		ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; | 
 | 2392 | 		*p++ = '\\'; | 
 | 2393 | 		*p++ = 'U'; | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2394 | 		*p++ = hexdigits[(ucs >> 28) & 0x0000000F]; | 
 | 2395 | 		*p++ = hexdigits[(ucs >> 24) & 0x0000000F]; | 
 | 2396 | 		*p++ = hexdigits[(ucs >> 20) & 0x0000000F]; | 
 | 2397 | 		*p++ = hexdigits[(ucs >> 16) & 0x0000000F]; | 
 | 2398 | 		*p++ = hexdigits[(ucs >> 12) & 0x0000000F]; | 
 | 2399 | 		*p++ = hexdigits[(ucs >> 8) & 0x0000000F]; | 
 | 2400 | 		*p++ = hexdigits[(ucs >> 4) & 0x0000000F]; | 
 | 2401 | 		*p++ = hexdigits[ucs & 0x0000000F]; | 
| Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2402 | 		continue; | 
 | 2403 | 	    } | 
 | 2404 | 	    /* Fall through: isolated surrogates are copied as-is */ | 
 | 2405 | 	    s--; | 
 | 2406 | 	    size++; | 
 | 2407 | 	} | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2408 | #endif | 
| Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2409 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2410 |         /* Map 16-bit characters to '\uxxxx' */ | 
| Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2411 |         if (ch >= 256) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2412 |             *p++ = '\\'; | 
 | 2413 |             *p++ = 'u'; | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2414 |             *p++ = hexdigits[(ch >> 12) & 0x000F]; | 
 | 2415 |             *p++ = hexdigits[(ch >> 8) & 0x000F]; | 
 | 2416 |             *p++ = hexdigits[(ch >> 4) & 0x000F]; | 
 | 2417 |             *p++ = hexdigits[ch & 0x000F]; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2418 |         } | 
| Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2419 |  | 
| Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2420 |         /* Map special whitespace to '\t', \n', '\r' */ | 
 | 2421 |         else if (ch == '\t') { | 
 | 2422 |             *p++ = '\\'; | 
 | 2423 |             *p++ = 't'; | 
 | 2424 |         } | 
 | 2425 |         else if (ch == '\n') { | 
 | 2426 |             *p++ = '\\'; | 
 | 2427 |             *p++ = 'n'; | 
 | 2428 |         } | 
 | 2429 |         else if (ch == '\r') { | 
 | 2430 |             *p++ = '\\'; | 
 | 2431 |             *p++ = 'r'; | 
 | 2432 |         } | 
| Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2433 |  | 
| Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2434 |         /* Map non-printable US ASCII to '\xhh' */ | 
| Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 2435 |         else if (ch < ' ' || ch >= 0x7F) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2436 |             *p++ = '\\'; | 
| Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2437 |             *p++ = 'x'; | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2438 |             *p++ = hexdigits[(ch >> 4) & 0x000F]; | 
 | 2439 |             *p++ = hexdigits[ch & 0x000F]; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2440 |         } | 
| Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2441 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2442 |         /* Copy everything else as-is */ | 
 | 2443 |         else | 
 | 2444 |             *p++ = (char) ch; | 
 | 2445 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2446 |  | 
 | 2447 |     *p = '\0'; | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2448 |     if (PyBytes_Resize(repr, p - PyBytes_AS_STRING(repr))) { | 
 | 2449 |         Py_DECREF(repr); | 
 | 2450 |         return NULL; | 
 | 2451 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2452 |     return repr; | 
 | 2453 | } | 
 | 2454 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2455 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) | 
 | 2456 | { | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2457 |     PyObject *s, *result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2458 |     if (!PyUnicode_Check(unicode)) { | 
 | 2459 |         PyErr_BadArgument(); | 
 | 2460 |         return NULL; | 
 | 2461 |     } | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2462 |     s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), | 
 | 2463 |                                       PyUnicode_GET_SIZE(unicode)); | 
 | 2464 |  | 
 | 2465 |     if (!s) | 
 | 2466 |         return NULL; | 
 | 2467 |     result = PyString_FromStringAndSize(PyBytes_AS_STRING(s), | 
 | 2468 |                                         PyBytes_GET_SIZE(s)); | 
 | 2469 |     Py_DECREF(s); | 
 | 2470 |     return result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2471 | } | 
 | 2472 |  | 
 | 2473 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ | 
 | 2474 |  | 
 | 2475 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2476 | 					   Py_ssize_t size, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2477 | 					   const char *errors) | 
 | 2478 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2479 |     const char *starts = s; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2480 |     Py_ssize_t startinpos; | 
 | 2481 |     Py_ssize_t endinpos; | 
 | 2482 |     Py_ssize_t outpos; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2483 |     PyUnicodeObject *v; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2484 |     Py_UNICODE *p; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2485 |     const char *end; | 
 | 2486 |     const char *bs; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2487 |     PyObject *errorHandler = NULL; | 
 | 2488 |     PyObject *exc = NULL; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2489 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2490 |     /* Escaped strings will always be longer than the resulting | 
 | 2491 |        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] | 2492 |        length after conversion to the true value. (But decoding error | 
 | 2493 |        handler might have to resize the string) */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2494 |     v = _PyUnicode_New(size); | 
 | 2495 |     if (v == NULL) | 
 | 2496 | 	goto onError; | 
 | 2497 |     if (size == 0) | 
 | 2498 | 	return (PyObject *)v; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2499 |     p = PyUnicode_AS_UNICODE(v); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2500 |     end = s + size; | 
 | 2501 |     while (s < end) { | 
 | 2502 | 	unsigned char c; | 
| Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 2503 | 	Py_UCS4 x; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2504 | 	int i; | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2505 |         int count; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2506 |  | 
 | 2507 | 	/* Non-escape characters are interpreted as Unicode ordinals */ | 
 | 2508 | 	if (*s != '\\') { | 
 | 2509 | 	    *p++ = (unsigned char)*s++; | 
 | 2510 | 	    continue; | 
 | 2511 | 	} | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2512 | 	startinpos = s-starts; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2513 |  | 
 | 2514 | 	/* \u-escapes are only interpreted iff the number of leading | 
 | 2515 | 	   backslashes if odd */ | 
 | 2516 | 	bs = s; | 
 | 2517 | 	for (;s < end;) { | 
 | 2518 | 	    if (*s != '\\') | 
 | 2519 | 		break; | 
 | 2520 | 	    *p++ = (unsigned char)*s++; | 
 | 2521 | 	} | 
 | 2522 | 	if (((s - bs) & 1) == 0 || | 
 | 2523 | 	    s >= end || | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2524 | 	    (*s != 'u' && *s != 'U')) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2525 | 	    continue; | 
 | 2526 | 	} | 
 | 2527 | 	p--; | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2528 |         count = *s=='u' ? 4 : 8; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2529 | 	s++; | 
 | 2530 |  | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2531 | 	/* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2532 | 	outpos = p-PyUnicode_AS_UNICODE(v); | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2533 | 	for (x = 0, i = 0; i < count; ++i, ++s) { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2534 | 	    c = (unsigned char)*s; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2535 | 	    if (!isxdigit(c)) { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2536 | 		endinpos = s-starts; | 
 | 2537 | 		if (unicode_decode_call_errorhandler( | 
 | 2538 | 		    errors, &errorHandler, | 
 | 2539 | 		    "rawunicodeescape", "truncated \\uXXXX", | 
 | 2540 | 		    starts, size, &startinpos, &endinpos, &exc, &s, | 
 | 2541 | 		    (PyObject **)&v, &outpos, &p)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2542 | 		    goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2543 | 		goto nextByte; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2544 | 	    } | 
 | 2545 | 	    x = (x<<4) & ~0xF; | 
 | 2546 | 	    if (c >= '0' && c <= '9') | 
 | 2547 | 		x += c - '0'; | 
 | 2548 | 	    else if (c >= 'a' && c <= 'f') | 
 | 2549 | 		x += 10 + c - 'a'; | 
 | 2550 | 	    else | 
 | 2551 | 		x += 10 + c - 'A'; | 
 | 2552 | 	} | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2553 | #ifndef Py_UNICODE_WIDE | 
 | 2554 |         if (x > 0x10000) { | 
 | 2555 |             if (unicode_decode_call_errorhandler( | 
 | 2556 |                     errors, &errorHandler, | 
 | 2557 |                     "rawunicodeescape", "\\Uxxxxxxxx out of range", | 
 | 2558 | 		    starts, size, &startinpos, &endinpos, &exc, &s, | 
 | 2559 | 		    (PyObject **)&v, &outpos, &p)) | 
 | 2560 | 		    goto onError; | 
 | 2561 |         } | 
 | 2562 | #endif | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2563 | 	*p++ = x; | 
 | 2564 | 	nextByte: | 
 | 2565 | 	; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2566 |     } | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2567 |     if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) | 
| Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2568 | 	goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2569 |     Py_XDECREF(errorHandler); | 
 | 2570 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2571 |     return (PyObject *)v; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2572 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2573 |  onError: | 
 | 2574 |     Py_XDECREF(v); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2575 |     Py_XDECREF(errorHandler); | 
 | 2576 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2577 |     return NULL; | 
 | 2578 | } | 
 | 2579 |  | 
 | 2580 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2581 | 					   Py_ssize_t size) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2582 | { | 
 | 2583 |     PyObject *repr; | 
 | 2584 |     char *p; | 
 | 2585 |     char *q; | 
 | 2586 |  | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2587 | #ifdef Py_UNICODE_WIDE | 
| Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 2588 |     repr = PyBytes_FromStringAndSize(NULL, 10 * size); | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2589 | #else | 
| Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 2590 |     repr = PyBytes_FromStringAndSize(NULL, 6 * size); | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2591 | #endif | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2592 |     if (repr == NULL) | 
 | 2593 |         return NULL; | 
| Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2594 |     if (size == 0) | 
 | 2595 | 	return repr; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2596 |  | 
| Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 2597 |     p = q = PyBytes_AS_STRING(repr); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2598 |     while (size-- > 0) { | 
 | 2599 |         Py_UNICODE ch = *s++; | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2600 | #ifdef Py_UNICODE_WIDE | 
 | 2601 | 	/* Map 32-bit characters to '\Uxxxxxxxx' */ | 
 | 2602 | 	if (ch >= 0x10000) { | 
 | 2603 |             *p++ = '\\'; | 
 | 2604 |             *p++ = 'U'; | 
| Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 2605 |             *p++ = hexdigits[(ch >> 28) & 0xf]; | 
 | 2606 |             *p++ = hexdigits[(ch >> 24) & 0xf]; | 
 | 2607 |             *p++ = hexdigits[(ch >> 20) & 0xf]; | 
 | 2608 |             *p++ = hexdigits[(ch >> 16) & 0xf]; | 
 | 2609 |             *p++ = hexdigits[(ch >> 12) & 0xf]; | 
 | 2610 |             *p++ = hexdigits[(ch >> 8) & 0xf]; | 
 | 2611 |             *p++ = hexdigits[(ch >> 4) & 0xf]; | 
 | 2612 |             *p++ = hexdigits[ch & 15]; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2613 |         } | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2614 |         else | 
 | 2615 | #endif | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2616 | 	/* Map 16-bit characters to '\uxxxx' */ | 
 | 2617 | 	if (ch >= 256) { | 
 | 2618 |             *p++ = '\\'; | 
 | 2619 |             *p++ = 'u'; | 
| Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 2620 |             *p++ = hexdigits[(ch >> 12) & 0xf]; | 
 | 2621 |             *p++ = hexdigits[(ch >> 8) & 0xf]; | 
 | 2622 |             *p++ = hexdigits[(ch >> 4) & 0xf]; | 
 | 2623 |             *p++ = hexdigits[ch & 15]; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2624 |         } | 
 | 2625 | 	/* Copy everything else as-is */ | 
 | 2626 | 	else | 
 | 2627 |             *p++ = (char) ch; | 
 | 2628 |     } | 
 | 2629 |     *p = '\0'; | 
| Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 2630 |     if (PyBytes_Resize(repr, p - q)) { | 
 | 2631 |         Py_DECREF(repr); | 
 | 2632 |         return NULL; | 
 | 2633 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2634 |     return repr; | 
 | 2635 | } | 
 | 2636 |  | 
 | 2637 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) | 
 | 2638 | { | 
| Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 2639 |     PyObject *s, *result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2640 |     if (!PyUnicode_Check(unicode)) { | 
| Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 2641 |         PyErr_BadArgument(); | 
 | 2642 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2643 |     } | 
| Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 2644 |     s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), | 
 | 2645 |                                          PyUnicode_GET_SIZE(unicode)); | 
 | 2646 |  | 
 | 2647 |     if (!s) | 
 | 2648 |         return NULL; | 
 | 2649 |     result = PyString_FromStringAndSize(PyBytes_AS_STRING(s), | 
 | 2650 |                                         PyBytes_GET_SIZE(s)); | 
 | 2651 |     Py_DECREF(s); | 
 | 2652 |     return result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2653 | } | 
 | 2654 |  | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2655 | /* --- Unicode Internal Codec ------------------------------------------- */ | 
 | 2656 |  | 
 | 2657 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2658 | 					   Py_ssize_t size, | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2659 | 					   const char *errors) | 
 | 2660 | { | 
 | 2661 |     const char *starts = s; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2662 |     Py_ssize_t startinpos; | 
 | 2663 |     Py_ssize_t endinpos; | 
 | 2664 |     Py_ssize_t outpos; | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2665 |     PyUnicodeObject *v; | 
 | 2666 |     Py_UNICODE *p; | 
 | 2667 |     const char *end; | 
 | 2668 |     const char *reason; | 
 | 2669 |     PyObject *errorHandler = NULL; | 
 | 2670 |     PyObject *exc = NULL; | 
 | 2671 |  | 
| Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 2672 | #ifdef Py_UNICODE_WIDE | 
 | 2673 |     Py_UNICODE unimax = PyUnicode_GetMax(); | 
 | 2674 | #endif | 
 | 2675 |  | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2676 |     /* XXX overflow detection missing */ | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2677 |     v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); | 
 | 2678 |     if (v == NULL) | 
 | 2679 | 	goto onError; | 
 | 2680 |     if (PyUnicode_GetSize((PyObject *)v) == 0) | 
 | 2681 | 	return (PyObject *)v; | 
 | 2682 |     p = PyUnicode_AS_UNICODE(v); | 
 | 2683 |     end = s + size; | 
 | 2684 |  | 
 | 2685 |     while (s < end) { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2686 |         memcpy(p, s, sizeof(Py_UNICODE)); | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2687 |         /* We have to sanity check the raw data, otherwise doom looms for | 
 | 2688 |            some malformed UCS-4 data. */ | 
 | 2689 |         if ( | 
 | 2690 |             #ifdef Py_UNICODE_WIDE | 
 | 2691 |             *p > unimax || *p < 0 || | 
 | 2692 |             #endif | 
 | 2693 |             end-s < Py_UNICODE_SIZE | 
 | 2694 |             ) | 
 | 2695 |             { | 
 | 2696 |             startinpos = s - starts; | 
 | 2697 |             if (end-s < Py_UNICODE_SIZE) { | 
 | 2698 |                 endinpos = end-starts; | 
 | 2699 |                 reason = "truncated input"; | 
 | 2700 |             } | 
 | 2701 |             else { | 
 | 2702 |                 endinpos = s - starts + Py_UNICODE_SIZE; | 
 | 2703 |                 reason = "illegal code point (> 0x10FFFF)"; | 
 | 2704 |             } | 
 | 2705 |             outpos = p - PyUnicode_AS_UNICODE(v); | 
 | 2706 |             if (unicode_decode_call_errorhandler( | 
 | 2707 |                     errors, &errorHandler, | 
 | 2708 |                     "unicode_internal", reason, | 
 | 2709 |                     starts, size, &startinpos, &endinpos, &exc, &s, | 
 | 2710 |                     (PyObject **)&v, &outpos, &p)) { | 
 | 2711 |                 goto onError; | 
 | 2712 |             } | 
 | 2713 |         } | 
 | 2714 |         else { | 
 | 2715 |             p++; | 
 | 2716 |             s += Py_UNICODE_SIZE; | 
 | 2717 |         } | 
 | 2718 |     } | 
 | 2719 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2720 |     if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2721 |         goto onError; | 
 | 2722 |     Py_XDECREF(errorHandler); | 
 | 2723 |     Py_XDECREF(exc); | 
 | 2724 |     return (PyObject *)v; | 
 | 2725 |  | 
 | 2726 |  onError: | 
 | 2727 |     Py_XDECREF(v); | 
 | 2728 |     Py_XDECREF(errorHandler); | 
 | 2729 |     Py_XDECREF(exc); | 
 | 2730 |     return NULL; | 
 | 2731 | } | 
 | 2732 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2733 | /* --- Latin-1 Codec ------------------------------------------------------ */ | 
 | 2734 |  | 
 | 2735 | PyObject *PyUnicode_DecodeLatin1(const char *s, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2736 | 				 Py_ssize_t size, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2737 | 				 const char *errors) | 
 | 2738 | { | 
 | 2739 |     PyUnicodeObject *v; | 
 | 2740 |     Py_UNICODE *p; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2741 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2742 |     /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ | 
| Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2743 |     if (size == 1) { | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 2744 | 	Py_UNICODE r = *(unsigned char*)s; | 
 | 2745 | 	return PyUnicode_FromUnicode(&r, 1); | 
 | 2746 |     } | 
 | 2747 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2748 |     v = _PyUnicode_New(size); | 
 | 2749 |     if (v == NULL) | 
 | 2750 | 	goto onError; | 
 | 2751 |     if (size == 0) | 
 | 2752 | 	return (PyObject *)v; | 
 | 2753 |     p = PyUnicode_AS_UNICODE(v); | 
 | 2754 |     while (size-- > 0) | 
 | 2755 | 	*p++ = (unsigned char)*s++; | 
 | 2756 |     return (PyObject *)v; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2757 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2758 |  onError: | 
 | 2759 |     Py_XDECREF(v); | 
 | 2760 |     return NULL; | 
 | 2761 | } | 
 | 2762 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2763 | /* create or adjust a UnicodeEncodeError */ | 
 | 2764 | static void make_encode_exception(PyObject **exceptionObject, | 
 | 2765 |     const char *encoding, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2766 |     const Py_UNICODE *unicode, Py_ssize_t size, | 
 | 2767 |     Py_ssize_t startpos, Py_ssize_t endpos, | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2768 |     const char *reason) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2769 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2770 |     if (*exceptionObject == NULL) { | 
 | 2771 | 	*exceptionObject = PyUnicodeEncodeError_Create( | 
 | 2772 | 	    encoding, unicode, size, startpos, endpos, reason); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2773 |     } | 
 | 2774 |     else { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2775 | 	if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) | 
 | 2776 | 	    goto onError; | 
 | 2777 | 	if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) | 
 | 2778 | 	    goto onError; | 
 | 2779 | 	if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) | 
 | 2780 | 	    goto onError; | 
 | 2781 | 	return; | 
 | 2782 | 	onError: | 
 | 2783 | 	Py_DECREF(*exceptionObject); | 
 | 2784 | 	*exceptionObject = NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2785 |     } | 
 | 2786 | } | 
 | 2787 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2788 | /* raises a UnicodeEncodeError */ | 
 | 2789 | static void raise_encode_exception(PyObject **exceptionObject, | 
 | 2790 |     const char *encoding, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2791 |     const Py_UNICODE *unicode, Py_ssize_t size, | 
 | 2792 |     Py_ssize_t startpos, Py_ssize_t endpos, | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2793 |     const char *reason) | 
 | 2794 | { | 
 | 2795 |     make_encode_exception(exceptionObject, | 
 | 2796 | 	encoding, unicode, size, startpos, endpos, reason); | 
 | 2797 |     if (*exceptionObject != NULL) | 
 | 2798 | 	PyCodec_StrictErrors(*exceptionObject); | 
 | 2799 | } | 
 | 2800 |  | 
 | 2801 | /* error handling callback helper: | 
 | 2802 |    build arguments, call the callback and check the arguments, | 
 | 2803 |    put the result into newpos and return the replacement string, which | 
 | 2804 |    has to be freed by the caller */ | 
 | 2805 | static PyObject *unicode_encode_call_errorhandler(const char *errors, | 
 | 2806 |     PyObject **errorHandler, | 
 | 2807 |     const char *encoding, const char *reason, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2808 |     const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, | 
 | 2809 |     Py_ssize_t startpos, Py_ssize_t endpos, | 
 | 2810 |     Py_ssize_t *newpos) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2811 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2812 |     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] | 2813 |  | 
 | 2814 |     PyObject *restuple; | 
 | 2815 |     PyObject *resunicode; | 
 | 2816 |  | 
 | 2817 |     if (*errorHandler == NULL) { | 
 | 2818 | 	*errorHandler = PyCodec_LookupError(errors); | 
 | 2819 |         if (*errorHandler == NULL) | 
 | 2820 | 	    return NULL; | 
 | 2821 |     } | 
 | 2822 |  | 
 | 2823 |     make_encode_exception(exceptionObject, | 
 | 2824 | 	encoding, unicode, size, startpos, endpos, reason); | 
 | 2825 |     if (*exceptionObject == NULL) | 
 | 2826 | 	return NULL; | 
 | 2827 |  | 
 | 2828 |     restuple = PyObject_CallFunctionObjArgs( | 
 | 2829 | 	*errorHandler, *exceptionObject, NULL); | 
 | 2830 |     if (restuple == NULL) | 
 | 2831 | 	return NULL; | 
 | 2832 |     if (!PyTuple_Check(restuple)) { | 
 | 2833 | 	PyErr_Format(PyExc_TypeError, &argparse[4]); | 
 | 2834 | 	Py_DECREF(restuple); | 
 | 2835 | 	return NULL; | 
 | 2836 |     } | 
 | 2837 |     if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, | 
 | 2838 | 	&resunicode, newpos)) { | 
 | 2839 | 	Py_DECREF(restuple); | 
 | 2840 | 	return NULL; | 
 | 2841 |     } | 
 | 2842 |     if (*newpos<0) | 
| Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2843 | 	*newpos = size+*newpos; | 
 | 2844 |     if (*newpos<0 || *newpos>size) { | 
| Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 2845 | 	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] | 2846 | 	Py_DECREF(restuple); | 
 | 2847 | 	return NULL; | 
 | 2848 |     } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2849 |     Py_INCREF(resunicode); | 
 | 2850 |     Py_DECREF(restuple); | 
 | 2851 |     return resunicode; | 
 | 2852 | } | 
 | 2853 |  | 
 | 2854 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2855 | 				 Py_ssize_t size, | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2856 | 				 const char *errors, | 
 | 2857 | 				 int limit) | 
 | 2858 | { | 
 | 2859 |     /* output object */ | 
 | 2860 |     PyObject *res; | 
 | 2861 |     /* pointers to the beginning and end+1 of input */ | 
 | 2862 |     const Py_UNICODE *startp = p; | 
 | 2863 |     const Py_UNICODE *endp = p + size; | 
 | 2864 |     /* pointer to the beginning of the unencodable characters */ | 
 | 2865 |     /* const Py_UNICODE *badp = NULL; */ | 
 | 2866 |     /* pointer into the output */ | 
 | 2867 |     char *str; | 
 | 2868 |     /* current output position */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2869 |     Py_ssize_t respos = 0; | 
 | 2870 |     Py_ssize_t ressize; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2871 |     const char *encoding = (limit == 256) ? "latin-1" : "ascii"; | 
 | 2872 |     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] | 2873 |     PyObject *errorHandler = NULL; | 
 | 2874 |     PyObject *exc = NULL; | 
 | 2875 |     /* the following variable is used for caching string comparisons | 
 | 2876 |      * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ | 
 | 2877 |     int known_errorHandler = -1; | 
 | 2878 |  | 
 | 2879 |     /* allocate enough for a simple encoding without | 
 | 2880 |        replacements, if we need more, we'll resize */ | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 2881 |     res = PyBytes_FromStringAndSize(NULL, size); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2882 |     if (res == NULL) | 
 | 2883 |         goto onError; | 
 | 2884 |     if (size == 0) | 
 | 2885 | 	return res; | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 2886 |     str = PyBytes_AS_STRING(res); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2887 |     ressize = size; | 
 | 2888 |  | 
 | 2889 |     while (p<endp) { | 
 | 2890 | 	Py_UNICODE c = *p; | 
 | 2891 |  | 
 | 2892 | 	/* can we encode this? */ | 
 | 2893 | 	if (c<limit) { | 
 | 2894 | 	    /* no overflow check, because we know that the space is enough */ | 
 | 2895 | 	    *str++ = (char)c; | 
 | 2896 | 	    ++p; | 
 | 2897 | 	} | 
 | 2898 | 	else { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2899 | 	    Py_ssize_t unicodepos = p-startp; | 
 | 2900 | 	    Py_ssize_t requiredsize; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2901 | 	    PyObject *repunicode; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2902 | 	    Py_ssize_t repsize; | 
 | 2903 | 	    Py_ssize_t newpos; | 
 | 2904 | 	    Py_ssize_t respos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2905 | 	    Py_UNICODE *uni2; | 
 | 2906 | 	    /* startpos for collecting unencodable chars */ | 
 | 2907 | 	    const Py_UNICODE *collstart = p; | 
 | 2908 | 	    const Py_UNICODE *collend = p; | 
 | 2909 | 	    /* find all unecodable characters */ | 
 | 2910 | 	    while ((collend < endp) && ((*collend)>=limit)) | 
 | 2911 | 		++collend; | 
 | 2912 | 	    /* cache callback name lookup (if not done yet, i.e. it's the first error) */ | 
 | 2913 | 	    if (known_errorHandler==-1) { | 
 | 2914 | 		if ((errors==NULL) || (!strcmp(errors, "strict"))) | 
 | 2915 | 		    known_errorHandler = 1; | 
 | 2916 | 		else if (!strcmp(errors, "replace")) | 
 | 2917 | 		    known_errorHandler = 2; | 
 | 2918 | 		else if (!strcmp(errors, "ignore")) | 
 | 2919 | 		    known_errorHandler = 3; | 
 | 2920 | 		else if (!strcmp(errors, "xmlcharrefreplace")) | 
 | 2921 | 		    known_errorHandler = 4; | 
 | 2922 | 		else | 
 | 2923 | 		    known_errorHandler = 0; | 
 | 2924 | 	    } | 
 | 2925 | 	    switch (known_errorHandler) { | 
 | 2926 | 		case 1: /* strict */ | 
 | 2927 | 		    raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); | 
 | 2928 | 		    goto onError; | 
 | 2929 | 		case 2: /* replace */ | 
 | 2930 | 		    while (collstart++<collend) | 
 | 2931 | 			*str++ = '?'; /* fall through */ | 
 | 2932 | 		case 3: /* ignore */ | 
 | 2933 | 		    p = collend; | 
 | 2934 | 		    break; | 
 | 2935 | 		case 4: /* xmlcharrefreplace */ | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 2936 | 		    respos = str - PyBytes_AS_STRING(res); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2937 | 		    /* determine replacement size (temporarily (mis)uses p) */ | 
 | 2938 | 		    for (p = collstart, repsize = 0; p < collend; ++p) { | 
 | 2939 | 			if (*p<10) | 
 | 2940 | 			    repsize += 2+1+1; | 
 | 2941 | 			else if (*p<100) | 
 | 2942 | 			    repsize += 2+2+1; | 
 | 2943 | 			else if (*p<1000) | 
 | 2944 | 			    repsize += 2+3+1; | 
 | 2945 | 			else if (*p<10000) | 
 | 2946 | 			    repsize += 2+4+1; | 
| Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 2947 | #ifndef Py_UNICODE_WIDE | 
 | 2948 | 			else | 
 | 2949 | 			    repsize += 2+5+1; | 
 | 2950 | #else | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2951 | 			else if (*p<100000) | 
 | 2952 | 			    repsize += 2+5+1; | 
 | 2953 | 			else if (*p<1000000) | 
 | 2954 | 			    repsize += 2+6+1; | 
 | 2955 | 			else | 
 | 2956 | 			    repsize += 2+7+1; | 
| Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2957 | #endif | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2958 | 		    } | 
 | 2959 | 		    requiredsize = respos+repsize+(endp-collend); | 
 | 2960 | 		    if (requiredsize > ressize) { | 
 | 2961 | 			if (requiredsize<2*ressize) | 
 | 2962 | 			    requiredsize = 2*ressize; | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 2963 | 			if (PyBytes_Resize(res, requiredsize)) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2964 | 			    goto onError; | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 2965 | 			str = PyBytes_AS_STRING(res) + respos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2966 | 			ressize = requiredsize; | 
 | 2967 | 		    } | 
 | 2968 | 		    /* generate replacement (temporarily (mis)uses p) */ | 
 | 2969 | 		    for (p = collstart; p < collend; ++p) { | 
 | 2970 | 			str += sprintf(str, "&#%d;", (int)*p); | 
 | 2971 | 		    } | 
 | 2972 | 		    p = collend; | 
 | 2973 | 		    break; | 
 | 2974 | 		default: | 
 | 2975 | 		    repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, | 
 | 2976 | 			encoding, reason, startp, size, &exc, | 
 | 2977 | 			collstart-startp, collend-startp, &newpos); | 
 | 2978 | 		    if (repunicode == NULL) | 
 | 2979 | 			goto onError; | 
 | 2980 | 		    /* need more space? (at least enough for what we | 
 | 2981 | 		       have+the replacement+the rest of the string, so | 
 | 2982 | 		       we won't have to check space for encodable characters) */ | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 2983 | 		    respos = str - PyBytes_AS_STRING(res); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2984 | 		    repsize = PyUnicode_GET_SIZE(repunicode); | 
 | 2985 | 		    requiredsize = respos+repsize+(endp-collend); | 
 | 2986 | 		    if (requiredsize > ressize) { | 
 | 2987 | 			if (requiredsize<2*ressize) | 
 | 2988 | 			    requiredsize = 2*ressize; | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 2989 | 			if (PyBytes_Resize(res, requiredsize)) { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2990 | 			    Py_DECREF(repunicode); | 
 | 2991 | 			    goto onError; | 
 | 2992 | 			} | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 2993 | 			str = PyBytes_AS_STRING(res) + respos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2994 | 			ressize = requiredsize; | 
 | 2995 | 		    } | 
 | 2996 | 		    /* check if there is anything unencodable in the replacement | 
 | 2997 | 		       and copy it to the output */ | 
 | 2998 | 		    for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { | 
 | 2999 | 			c = *uni2; | 
 | 3000 | 			if (c >= limit) { | 
 | 3001 | 			    raise_encode_exception(&exc, encoding, startp, size, | 
 | 3002 | 				unicodepos, unicodepos+1, reason); | 
 | 3003 | 			    Py_DECREF(repunicode); | 
 | 3004 | 			    goto onError; | 
 | 3005 | 			} | 
 | 3006 | 			*str = (char)c; | 
 | 3007 | 		    } | 
 | 3008 | 		    p = startp + newpos; | 
 | 3009 | 		    Py_DECREF(repunicode); | 
 | 3010 | 	    } | 
 | 3011 | 	} | 
 | 3012 |     } | 
 | 3013 |     /* Resize if we allocated to much */ | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3014 |     respos = str - PyBytes_AS_STRING(res); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3015 |     if (respos<ressize) | 
 | 3016 |        /* If this falls res will be NULL */ | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3017 | 	PyBytes_Resize(res, respos); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3018 |     Py_XDECREF(errorHandler); | 
 | 3019 |     Py_XDECREF(exc); | 
 | 3020 |     return res; | 
 | 3021 |  | 
 | 3022 |     onError: | 
 | 3023 |     Py_XDECREF(res); | 
 | 3024 |     Py_XDECREF(errorHandler); | 
 | 3025 |     Py_XDECREF(exc); | 
 | 3026 |     return NULL; | 
 | 3027 | } | 
 | 3028 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3029 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3030 | 				 Py_ssize_t size, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3031 | 				 const char *errors) | 
 | 3032 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3033 |     return unicode_encode_ucs1(p, size, errors, 256); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3034 | } | 
 | 3035 |  | 
 | 3036 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) | 
 | 3037 | { | 
 | 3038 |     if (!PyUnicode_Check(unicode)) { | 
 | 3039 | 	PyErr_BadArgument(); | 
 | 3040 | 	return NULL; | 
 | 3041 |     } | 
 | 3042 |     return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), | 
 | 3043 | 				  PyUnicode_GET_SIZE(unicode), | 
 | 3044 | 				  NULL); | 
 | 3045 | } | 
 | 3046 |  | 
 | 3047 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ | 
 | 3048 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3049 | PyObject *PyUnicode_DecodeASCII(const char *s, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3050 | 				Py_ssize_t size, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3051 | 				const char *errors) | 
 | 3052 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3053 |     const char *starts = s; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3054 |     PyUnicodeObject *v; | 
 | 3055 |     Py_UNICODE *p; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3056 |     Py_ssize_t startinpos; | 
 | 3057 |     Py_ssize_t endinpos; | 
 | 3058 |     Py_ssize_t outpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3059 |     const char *e; | 
 | 3060 |     PyObject *errorHandler = NULL; | 
 | 3061 |     PyObject *exc = NULL; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3062 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3063 |     /* ASCII is equivalent to the first 128 ordinals in Unicode. */ | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3064 |     if (size == 1 && *(unsigned char*)s < 128) { | 
 | 3065 | 	Py_UNICODE r = *(unsigned char*)s; | 
 | 3066 | 	return PyUnicode_FromUnicode(&r, 1); | 
 | 3067 |     } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3068 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3069 |     v = _PyUnicode_New(size); | 
 | 3070 |     if (v == NULL) | 
 | 3071 | 	goto onError; | 
 | 3072 |     if (size == 0) | 
 | 3073 | 	return (PyObject *)v; | 
 | 3074 |     p = PyUnicode_AS_UNICODE(v); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3075 |     e = s + size; | 
 | 3076 |     while (s < e) { | 
 | 3077 | 	register unsigned char c = (unsigned char)*s; | 
 | 3078 | 	if (c < 128) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3079 | 	    *p++ = c; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3080 | 	    ++s; | 
 | 3081 | 	} | 
 | 3082 | 	else { | 
 | 3083 | 	    startinpos = s-starts; | 
 | 3084 | 	    endinpos = startinpos + 1; | 
| Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 3085 | 	    outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3086 | 	    if (unicode_decode_call_errorhandler( | 
 | 3087 | 		 errors, &errorHandler, | 
 | 3088 | 		 "ascii", "ordinal not in range(128)", | 
 | 3089 | 		 starts, size, &startinpos, &endinpos, &exc, &s, | 
 | 3090 | 		 (PyObject **)&v, &outpos, &p)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3091 | 		goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3092 | 	} | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3093 |     } | 
| Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3094 |     if (p - PyUnicode_AS_UNICODE(v) < PyString_GET_SIZE(v)) | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3095 | 	if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) | 
| Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3096 | 	    goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3097 |     Py_XDECREF(errorHandler); | 
 | 3098 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3099 |     return (PyObject *)v; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3100 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3101 |  onError: | 
 | 3102 |     Py_XDECREF(v); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3103 |     Py_XDECREF(errorHandler); | 
 | 3104 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3105 |     return NULL; | 
 | 3106 | } | 
 | 3107 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3108 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3109 | 				Py_ssize_t size, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3110 | 				const char *errors) | 
 | 3111 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3112 |     return unicode_encode_ucs1(p, size, errors, 128); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3113 | } | 
 | 3114 |  | 
 | 3115 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) | 
 | 3116 | { | 
 | 3117 |     if (!PyUnicode_Check(unicode)) { | 
 | 3118 | 	PyErr_BadArgument(); | 
 | 3119 | 	return NULL; | 
 | 3120 |     } | 
 | 3121 |     return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), | 
 | 3122 | 				 PyUnicode_GET_SIZE(unicode), | 
 | 3123 | 				 NULL); | 
 | 3124 | } | 
 | 3125 |  | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3126 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) | 
| Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3127 |  | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3128 | /* --- MBCS codecs for Windows -------------------------------------------- */ | 
| Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3129 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3130 | #if SIZEOF_INT < SIZEOF_SSIZE_T | 
 | 3131 | #define NEED_RETRY | 
 | 3132 | #endif | 
 | 3133 |  | 
 | 3134 | /* XXX This code is limited to "true" double-byte encodings, as | 
 | 3135 |    a) it assumes an incomplete character consists of a single byte, and | 
 | 3136 |    b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte | 
 | 3137 |       encodings, see IsDBCSLeadByteEx documentation. */ | 
 | 3138 |  | 
 | 3139 | static int is_dbcs_lead_byte(const char *s, int offset) | 
 | 3140 | { | 
 | 3141 |     const char *curr = s + offset; | 
 | 3142 |  | 
 | 3143 |     if (IsDBCSLeadByte(*curr)) { | 
 | 3144 | 	const char *prev = CharPrev(s, curr); | 
 | 3145 | 	return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); | 
 | 3146 |     } | 
 | 3147 |     return 0; | 
 | 3148 | } | 
 | 3149 |  | 
 | 3150 | /* | 
 | 3151 |  * Decode MBCS string into unicode object. If 'final' is set, converts | 
 | 3152 |  * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. | 
 | 3153 |  */ | 
 | 3154 | static int decode_mbcs(PyUnicodeObject **v, | 
 | 3155 | 			const char *s, /* MBCS string */ | 
 | 3156 | 			int size, /* sizeof MBCS string */ | 
 | 3157 | 			int final) | 
 | 3158 | { | 
 | 3159 |     Py_UNICODE *p; | 
 | 3160 |     Py_ssize_t n = 0; | 
 | 3161 |     int usize = 0; | 
 | 3162 |  | 
 | 3163 |     assert(size >= 0); | 
 | 3164 |  | 
 | 3165 |     /* Skip trailing lead-byte unless 'final' is set */ | 
 | 3166 |     if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) | 
 | 3167 | 	--size; | 
 | 3168 |  | 
 | 3169 |     /* First get the size of the result */ | 
 | 3170 |     if (size > 0) { | 
 | 3171 | 	usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); | 
 | 3172 | 	if (usize == 0) { | 
 | 3173 | 	    PyErr_SetFromWindowsErrWithFilename(0, NULL); | 
 | 3174 | 	    return -1; | 
 | 3175 | 	} | 
 | 3176 |     } | 
 | 3177 |  | 
 | 3178 |     if (*v == NULL) { | 
 | 3179 | 	/* Create unicode object */ | 
 | 3180 | 	*v = _PyUnicode_New(usize); | 
 | 3181 | 	if (*v == NULL) | 
 | 3182 | 	    return -1; | 
 | 3183 |     } | 
 | 3184 |     else { | 
 | 3185 | 	/* Extend unicode object */ | 
 | 3186 | 	n = PyUnicode_GET_SIZE(*v); | 
 | 3187 | 	if (_PyUnicode_Resize(v, n + usize) < 0) | 
 | 3188 | 	    return -1; | 
 | 3189 |     } | 
 | 3190 |  | 
 | 3191 |     /* Do the conversion */ | 
 | 3192 |     if (size > 0) { | 
 | 3193 | 	p = PyUnicode_AS_UNICODE(*v) + n; | 
 | 3194 | 	if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { | 
 | 3195 | 	    PyErr_SetFromWindowsErrWithFilename(0, NULL); | 
 | 3196 | 	    return -1; | 
 | 3197 | 	} | 
 | 3198 |     } | 
 | 3199 |  | 
 | 3200 |     return size; | 
 | 3201 | } | 
 | 3202 |  | 
 | 3203 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, | 
 | 3204 | 					Py_ssize_t size, | 
 | 3205 | 					const char *errors, | 
 | 3206 | 					Py_ssize_t *consumed) | 
 | 3207 | { | 
 | 3208 |     PyUnicodeObject *v = NULL; | 
 | 3209 |     int done; | 
 | 3210 |  | 
 | 3211 |     if (consumed) | 
 | 3212 | 	*consumed = 0; | 
 | 3213 |  | 
 | 3214 | #ifdef NEED_RETRY | 
 | 3215 |   retry: | 
 | 3216 |     if (size > INT_MAX) | 
 | 3217 | 	done = decode_mbcs(&v, s, INT_MAX, 0); | 
 | 3218 |     else | 
 | 3219 | #endif | 
 | 3220 | 	done = decode_mbcs(&v, s, (int)size, !consumed); | 
 | 3221 |  | 
 | 3222 |     if (done < 0) { | 
 | 3223 |         Py_XDECREF(v); | 
 | 3224 | 	return NULL; | 
 | 3225 |     } | 
 | 3226 |  | 
 | 3227 |     if (consumed) | 
 | 3228 | 	*consumed += done; | 
 | 3229 |  | 
 | 3230 | #ifdef NEED_RETRY | 
 | 3231 |     if (size > INT_MAX) { | 
 | 3232 | 	s += done; | 
 | 3233 | 	size -= done; | 
 | 3234 | 	goto retry; | 
 | 3235 |     } | 
 | 3236 | #endif | 
 | 3237 |  | 
 | 3238 |     return (PyObject *)v; | 
 | 3239 | } | 
 | 3240 |  | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3241 | PyObject *PyUnicode_DecodeMBCS(const char *s, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3242 | 				Py_ssize_t size, | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3243 | 				const char *errors) | 
 | 3244 | { | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3245 |     return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); | 
 | 3246 | } | 
 | 3247 |  | 
 | 3248 | /* | 
 | 3249 |  * Convert unicode into string object (MBCS). | 
 | 3250 |  * Returns 0 if succeed, -1 otherwise. | 
 | 3251 |  */ | 
 | 3252 | static int encode_mbcs(PyObject **repr, | 
 | 3253 | 			const Py_UNICODE *p, /* unicode */ | 
 | 3254 | 			int size) /* size of unicode */ | 
 | 3255 | { | 
 | 3256 |     int mbcssize = 0; | 
 | 3257 |     Py_ssize_t n = 0; | 
 | 3258 |  | 
 | 3259 |     assert(size >= 0); | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3260 |  | 
 | 3261 |     /* First get the size of the result */ | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3262 |     if (size > 0) { | 
 | 3263 | 	mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); | 
 | 3264 | 	if (mbcssize == 0) { | 
 | 3265 | 	    PyErr_SetFromWindowsErrWithFilename(0, NULL); | 
 | 3266 | 	    return -1; | 
 | 3267 | 	} | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3268 |     } | 
 | 3269 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3270 |     if (*repr == NULL) { | 
 | 3271 | 	/* Create string object */ | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3272 | 	*repr = PyBytes_FromStringAndSize(NULL, mbcssize); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3273 | 	if (*repr == NULL) | 
 | 3274 | 	    return -1; | 
 | 3275 |     } | 
 | 3276 |     else { | 
 | 3277 | 	/* Extend string object */ | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3278 | 	n = PyBytes_Size(*repr); | 
 | 3279 | 	if (PyBytes_Resize(*repr, n + mbcssize) < 0) | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3280 | 	    return -1; | 
 | 3281 |     } | 
 | 3282 |  | 
 | 3283 |     /* Do the conversion */ | 
 | 3284 |     if (size > 0) { | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3285 | 	char *s = PyBytes_AS_STRING(*repr) + n; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3286 | 	if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { | 
 | 3287 | 	    PyErr_SetFromWindowsErrWithFilename(0, NULL); | 
 | 3288 | 	    return -1; | 
 | 3289 | 	} | 
 | 3290 |     } | 
 | 3291 |  | 
 | 3292 |     return 0; | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3293 | } | 
 | 3294 |  | 
 | 3295 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3296 | 				Py_ssize_t size, | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3297 | 				const char *errors) | 
 | 3298 | { | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3299 |     PyObject *repr = NULL; | 
 | 3300 |     int ret; | 
| Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 3301 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3302 | #ifdef NEED_RETRY | 
 | 3303 |  retry: | 
 | 3304 |     if (size > INT_MAX) | 
 | 3305 | 	ret = encode_mbcs(&repr, p, INT_MAX); | 
 | 3306 |     else | 
 | 3307 | #endif | 
 | 3308 | 	ret = encode_mbcs(&repr, p, (int)size); | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3309 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3310 |     if (ret < 0) { | 
 | 3311 | 	Py_XDECREF(repr); | 
 | 3312 | 	return NULL; | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3313 |     } | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3314 |  | 
 | 3315 | #ifdef NEED_RETRY | 
 | 3316 |     if (size > INT_MAX) { | 
 | 3317 | 	p += INT_MAX; | 
 | 3318 | 	size -= INT_MAX; | 
 | 3319 | 	goto retry; | 
 | 3320 |     } | 
 | 3321 | #endif | 
 | 3322 |  | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3323 |     return repr; | 
 | 3324 | } | 
| Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3325 |  | 
| Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3326 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) | 
 | 3327 | { | 
 | 3328 |     if (!PyUnicode_Check(unicode)) { | 
 | 3329 |         PyErr_BadArgument(); | 
 | 3330 |         return NULL; | 
 | 3331 |     } | 
 | 3332 |     return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), | 
 | 3333 | 				PyUnicode_GET_SIZE(unicode), | 
 | 3334 | 				NULL); | 
 | 3335 | } | 
 | 3336 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3337 | #undef NEED_RETRY | 
 | 3338 |  | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3339 | #endif /* MS_WINDOWS */ | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3340 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3341 | /* --- Character Mapping Codec -------------------------------------------- */ | 
 | 3342 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3343 | PyObject *PyUnicode_DecodeCharmap(const char *s, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3344 | 				  Py_ssize_t size, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3345 | 				  PyObject *mapping, | 
 | 3346 | 				  const char *errors) | 
 | 3347 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3348 |     const char *starts = s; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3349 |     Py_ssize_t startinpos; | 
 | 3350 |     Py_ssize_t endinpos; | 
 | 3351 |     Py_ssize_t outpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3352 |     const char *e; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3353 |     PyUnicodeObject *v; | 
 | 3354 |     Py_UNICODE *p; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3355 |     Py_ssize_t extrachars = 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3356 |     PyObject *errorHandler = NULL; | 
 | 3357 |     PyObject *exc = NULL; | 
| Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3358 |     Py_UNICODE *mapstring = NULL; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3359 |     Py_ssize_t maplen = 0; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3360 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3361 |     /* Default to Latin-1 */ | 
 | 3362 |     if (mapping == NULL) | 
 | 3363 | 	return PyUnicode_DecodeLatin1(s, size, errors); | 
 | 3364 |  | 
 | 3365 |     v = _PyUnicode_New(size); | 
 | 3366 |     if (v == NULL) | 
 | 3367 | 	goto onError; | 
 | 3368 |     if (size == 0) | 
 | 3369 | 	return (PyObject *)v; | 
 | 3370 |     p = PyUnicode_AS_UNICODE(v); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3371 |     e = s + size; | 
| Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3372 |     if (PyUnicode_CheckExact(mapping)) { | 
 | 3373 | 	mapstring = PyUnicode_AS_UNICODE(mapping); | 
 | 3374 | 	maplen = PyUnicode_GET_SIZE(mapping); | 
 | 3375 | 	while (s < e) { | 
 | 3376 | 	    unsigned char ch = *s; | 
 | 3377 | 	    Py_UNICODE x = 0xfffe; /* illegal value */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3378 |  | 
| Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3379 | 	    if (ch < maplen) | 
 | 3380 | 		x = mapstring[ch]; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3381 |  | 
| Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3382 | 	    if (x == 0xfffe) { | 
 | 3383 | 		/* undefined mapping */ | 
 | 3384 | 		outpos = p-PyUnicode_AS_UNICODE(v); | 
 | 3385 | 		startinpos = s-starts; | 
 | 3386 | 		endinpos = startinpos+1; | 
 | 3387 | 		if (unicode_decode_call_errorhandler( | 
 | 3388 | 		     errors, &errorHandler, | 
 | 3389 | 		     "charmap", "character maps to <undefined>", | 
 | 3390 | 		     starts, size, &startinpos, &endinpos, &exc, &s, | 
 | 3391 | 		     (PyObject **)&v, &outpos, &p)) { | 
 | 3392 | 		    goto onError; | 
| Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 3393 | 		} | 
| Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3394 | 		continue; | 
| Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 3395 | 	    } | 
| Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3396 | 	    *p++ = x; | 
 | 3397 | 	    ++s; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3398 | 	} | 
| Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3399 |     } | 
 | 3400 |     else { | 
 | 3401 | 	while (s < e) { | 
 | 3402 | 	    unsigned char ch = *s; | 
 | 3403 | 	    PyObject *w, *x; | 
 | 3404 |  | 
 | 3405 | 	    /* Get mapping (char ordinal -> integer, Unicode char or None) */ | 
 | 3406 | 	    w = PyInt_FromLong((long)ch); | 
 | 3407 | 	    if (w == NULL) | 
 | 3408 | 		goto onError; | 
 | 3409 | 	    x = PyObject_GetItem(mapping, w); | 
 | 3410 | 	    Py_DECREF(w); | 
 | 3411 | 	    if (x == NULL) { | 
 | 3412 | 		if (PyErr_ExceptionMatches(PyExc_LookupError)) { | 
 | 3413 | 		    /* No mapping found means: mapping is undefined. */ | 
 | 3414 | 		    PyErr_Clear(); | 
 | 3415 | 		    x = Py_None; | 
 | 3416 | 		    Py_INCREF(x); | 
 | 3417 | 		} else | 
 | 3418 | 		    goto onError; | 
 | 3419 | 	    } | 
 | 3420 |      | 
 | 3421 | 	    /* Apply mapping */ | 
 | 3422 | 	    if (PyInt_Check(x)) { | 
 | 3423 | 		long value = PyInt_AS_LONG(x); | 
 | 3424 | 		if (value < 0 || value > 65535) { | 
 | 3425 | 		    PyErr_SetString(PyExc_TypeError, | 
 | 3426 | 				    "character mapping must be in range(65536)"); | 
 | 3427 | 		    Py_DECREF(x); | 
 | 3428 | 		    goto onError; | 
 | 3429 | 		} | 
 | 3430 | 		*p++ = (Py_UNICODE)value; | 
 | 3431 | 	    } | 
 | 3432 | 	    else if (x == Py_None) { | 
 | 3433 | 		/* undefined mapping */ | 
 | 3434 | 		outpos = p-PyUnicode_AS_UNICODE(v); | 
 | 3435 | 		startinpos = s-starts; | 
 | 3436 | 		endinpos = startinpos+1; | 
 | 3437 | 		if (unicode_decode_call_errorhandler( | 
 | 3438 | 		     errors, &errorHandler, | 
 | 3439 | 		     "charmap", "character maps to <undefined>", | 
 | 3440 | 		     starts, size, &startinpos, &endinpos, &exc, &s, | 
 | 3441 | 		     (PyObject **)&v, &outpos, &p)) { | 
 | 3442 | 		    Py_DECREF(x); | 
 | 3443 | 		    goto onError; | 
 | 3444 | 		} | 
| Walter Dörwald | d4fff17 | 2005-11-28 22:15:56 +0000 | [diff] [blame] | 3445 | 		Py_DECREF(x); | 
| Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3446 | 		continue; | 
 | 3447 | 	    } | 
 | 3448 | 	    else if (PyUnicode_Check(x)) { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3449 | 		Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); | 
| Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3450 |      | 
 | 3451 | 		if (targetsize == 1) | 
 | 3452 | 		    /* 1-1 mapping */ | 
 | 3453 | 		    *p++ = *PyUnicode_AS_UNICODE(x); | 
 | 3454 |      | 
 | 3455 | 		else if (targetsize > 1) { | 
 | 3456 | 		    /* 1-n mapping */ | 
 | 3457 | 		    if (targetsize > extrachars) { | 
 | 3458 | 			/* resize first */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3459 | 			Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); | 
 | 3460 | 			Py_ssize_t needed = (targetsize - extrachars) + \ | 
| Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3461 | 				     (targetsize << 2); | 
 | 3462 | 			extrachars += needed; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3463 | 			/* XXX overflow detection missing */ | 
| Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3464 | 			if (_PyUnicode_Resize(&v, | 
 | 3465 | 					     PyUnicode_GET_SIZE(v) + needed) < 0) { | 
 | 3466 | 			    Py_DECREF(x); | 
 | 3467 | 			    goto onError; | 
 | 3468 | 			} | 
 | 3469 | 			p = PyUnicode_AS_UNICODE(v) + oldpos; | 
 | 3470 | 		    } | 
 | 3471 | 		    Py_UNICODE_COPY(p, | 
 | 3472 | 				    PyUnicode_AS_UNICODE(x), | 
 | 3473 | 				    targetsize); | 
 | 3474 | 		    p += targetsize; | 
 | 3475 | 		    extrachars -= targetsize; | 
 | 3476 | 		} | 
 | 3477 | 		/* 1-0 mapping: skip the character */ | 
 | 3478 | 	    } | 
 | 3479 | 	    else { | 
 | 3480 | 		/* wrong return value */ | 
 | 3481 | 		PyErr_SetString(PyExc_TypeError, | 
 | 3482 | 		      "character mapping must return integer, None or unicode"); | 
 | 3483 | 		Py_DECREF(x); | 
 | 3484 | 		goto onError; | 
 | 3485 | 	    } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3486 | 	    Py_DECREF(x); | 
| Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3487 | 	    ++s; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3488 | 	} | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3489 |     } | 
 | 3490 |     if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3491 | 	if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3492 | 	    goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3493 |     Py_XDECREF(errorHandler); | 
 | 3494 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3495 |     return (PyObject *)v; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3496 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3497 |  onError: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3498 |     Py_XDECREF(errorHandler); | 
 | 3499 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3500 |     Py_XDECREF(v); | 
 | 3501 |     return NULL; | 
 | 3502 | } | 
 | 3503 |  | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3504 | /* Charmap encoding: the lookup table */ | 
 | 3505 |  | 
 | 3506 | struct encoding_map{ | 
 | 3507 |   PyObject_HEAD | 
 | 3508 |   unsigned char level1[32]; | 
 | 3509 |   int count2, count3; | 
 | 3510 |   unsigned char level23[1]; | 
 | 3511 | }; | 
 | 3512 |  | 
 | 3513 | static PyObject* | 
 | 3514 | encoding_map_size(PyObject *obj, PyObject* args) | 
 | 3515 | { | 
 | 3516 |     struct encoding_map *map = (struct encoding_map*)obj; | 
 | 3517 |     return PyInt_FromLong(sizeof(*map) - 1 + 16*map->count2 +  | 
 | 3518 |                           128*map->count3); | 
 | 3519 | } | 
 | 3520 |  | 
 | 3521 | static PyMethodDef encoding_map_methods[] = { | 
 | 3522 | 	{"size", encoding_map_size, METH_NOARGS,  | 
 | 3523 |          PyDoc_STR("Return the size (in bytes) of this object") }, | 
 | 3524 |         { 0 } | 
 | 3525 | }; | 
 | 3526 |  | 
 | 3527 | static void | 
 | 3528 | encoding_map_dealloc(PyObject* o) | 
 | 3529 | { | 
 | 3530 | 	PyObject_FREE(o); | 
 | 3531 | } | 
 | 3532 |  | 
 | 3533 | static PyTypeObject EncodingMapType = { | 
 | 3534 | 	PyObject_HEAD_INIT(NULL) | 
 | 3535 |         0,                      /*ob_size*/ | 
 | 3536 |         "EncodingMap",          /*tp_name*/ | 
 | 3537 |         sizeof(struct encoding_map),   /*tp_basicsize*/ | 
 | 3538 |         0,                      /*tp_itemsize*/ | 
 | 3539 |         /* methods */ | 
 | 3540 |         encoding_map_dealloc,   /*tp_dealloc*/ | 
 | 3541 |         0,                      /*tp_print*/ | 
 | 3542 |         0,                      /*tp_getattr*/ | 
 | 3543 |         0,                      /*tp_setattr*/ | 
 | 3544 |         0,                      /*tp_compare*/ | 
 | 3545 |         0,                      /*tp_repr*/ | 
 | 3546 |         0,                      /*tp_as_number*/ | 
 | 3547 |         0,                      /*tp_as_sequence*/ | 
 | 3548 |         0,                      /*tp_as_mapping*/ | 
 | 3549 |         0,                      /*tp_hash*/ | 
 | 3550 |         0,                      /*tp_call*/ | 
 | 3551 |         0,                      /*tp_str*/ | 
 | 3552 |         0,                      /*tp_getattro*/ | 
 | 3553 |         0,                      /*tp_setattro*/ | 
 | 3554 |         0,                      /*tp_as_buffer*/ | 
 | 3555 |         Py_TPFLAGS_DEFAULT,     /*tp_flags*/ | 
 | 3556 |         0,                      /*tp_doc*/ | 
 | 3557 |         0,                      /*tp_traverse*/ | 
 | 3558 |         0,                      /*tp_clear*/ | 
 | 3559 |         0,                      /*tp_richcompare*/ | 
 | 3560 |         0,                      /*tp_weaklistoffset*/ | 
 | 3561 |         0,                      /*tp_iter*/ | 
 | 3562 |         0,                      /*tp_iternext*/ | 
 | 3563 |         encoding_map_methods,   /*tp_methods*/ | 
 | 3564 |         0,                      /*tp_members*/ | 
 | 3565 |         0,                      /*tp_getset*/ | 
 | 3566 |         0,                      /*tp_base*/ | 
 | 3567 |         0,                      /*tp_dict*/ | 
 | 3568 |         0,                      /*tp_descr_get*/ | 
 | 3569 |         0,                      /*tp_descr_set*/ | 
 | 3570 |         0,                      /*tp_dictoffset*/ | 
 | 3571 |         0,                      /*tp_init*/ | 
 | 3572 |         0,                      /*tp_alloc*/ | 
 | 3573 |         0,                      /*tp_new*/ | 
 | 3574 |         0,                      /*tp_free*/ | 
 | 3575 |         0,                      /*tp_is_gc*/ | 
 | 3576 | }; | 
 | 3577 |  | 
 | 3578 | PyObject* | 
 | 3579 | PyUnicode_BuildEncodingMap(PyObject* string) | 
 | 3580 | { | 
 | 3581 |     Py_UNICODE *decode; | 
 | 3582 |     PyObject *result; | 
 | 3583 |     struct encoding_map *mresult; | 
 | 3584 |     int i; | 
 | 3585 |     int need_dict = 0; | 
 | 3586 |     unsigned char level1[32]; | 
 | 3587 |     unsigned char level2[512]; | 
 | 3588 |     unsigned char *mlevel1, *mlevel2, *mlevel3; | 
 | 3589 |     int count2 = 0, count3 = 0; | 
 | 3590 |  | 
 | 3591 |     if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { | 
 | 3592 |         PyErr_BadArgument(); | 
 | 3593 |         return NULL; | 
 | 3594 |     } | 
 | 3595 |     decode = PyUnicode_AS_UNICODE(string); | 
 | 3596 |     memset(level1, 0xFF, sizeof level1); | 
 | 3597 |     memset(level2, 0xFF, sizeof level2); | 
 | 3598 |  | 
 | 3599 |     /* If there isn't a one-to-one mapping of NULL to \0, | 
 | 3600 |        or if there are non-BMP characters, we need to use | 
 | 3601 |        a mapping dictionary. */ | 
 | 3602 |     if (decode[0] != 0) | 
 | 3603 |         need_dict = 1; | 
 | 3604 |     for (i = 1; i < 256; i++) { | 
 | 3605 |         int l1, l2; | 
 | 3606 |         if (decode[i] == 0 | 
 | 3607 |             #ifdef Py_UNICODE_WIDE | 
 | 3608 |             || decode[i] > 0xFFFF | 
 | 3609 |             #endif | 
 | 3610 |         ) { | 
 | 3611 |             need_dict = 1; | 
 | 3612 |             break; | 
 | 3613 |         } | 
 | 3614 |         if (decode[i] == 0xFFFE) | 
 | 3615 |             /* unmapped character */ | 
 | 3616 |             continue; | 
 | 3617 |         l1 = decode[i] >> 11; | 
 | 3618 |         l2 = decode[i] >> 7; | 
 | 3619 |         if (level1[l1] == 0xFF) | 
 | 3620 |             level1[l1] = count2++; | 
 | 3621 |         if (level2[l2] == 0xFF) | 
 | 3622 |             level2[l2] = count3++;  | 
 | 3623 |     } | 
 | 3624 |  | 
 | 3625 |     if (count2 >= 0xFF || count3 >= 0xFF) | 
 | 3626 |         need_dict = 1; | 
 | 3627 |  | 
 | 3628 |     if (need_dict) { | 
 | 3629 |         PyObject *result = PyDict_New(); | 
 | 3630 |         PyObject *key, *value; | 
 | 3631 |         if (!result) | 
 | 3632 |             return NULL; | 
 | 3633 |         for (i = 0; i < 256; i++) { | 
 | 3634 |             key = value = NULL; | 
 | 3635 |             key = PyInt_FromLong(decode[i]); | 
 | 3636 |             value = PyInt_FromLong(i); | 
 | 3637 |             if (!key || !value) | 
 | 3638 |                 goto failed1; | 
 | 3639 |             if (PyDict_SetItem(result, key, value) == -1) | 
 | 3640 |                 goto failed1; | 
 | 3641 |             Py_DECREF(key); | 
 | 3642 |             Py_DECREF(value); | 
 | 3643 |         } | 
 | 3644 |         return result; | 
 | 3645 |       failed1: | 
 | 3646 |         Py_XDECREF(key); | 
 | 3647 |         Py_XDECREF(value); | 
 | 3648 |         Py_DECREF(result); | 
 | 3649 |         return NULL; | 
 | 3650 |     } | 
 | 3651 |  | 
 | 3652 |     /* Create a three-level trie */ | 
 | 3653 |     result = PyObject_MALLOC(sizeof(struct encoding_map) + | 
 | 3654 |                              16*count2 + 128*count3 - 1); | 
 | 3655 |     if (!result) | 
 | 3656 |         return PyErr_NoMemory(); | 
 | 3657 |     PyObject_Init(result, &EncodingMapType); | 
 | 3658 |     mresult = (struct encoding_map*)result; | 
 | 3659 |     mresult->count2 = count2; | 
 | 3660 |     mresult->count3 = count3; | 
 | 3661 |     mlevel1 = mresult->level1; | 
 | 3662 |     mlevel2 = mresult->level23; | 
 | 3663 |     mlevel3 = mresult->level23 + 16*count2; | 
 | 3664 |     memcpy(mlevel1, level1, 32); | 
 | 3665 |     memset(mlevel2, 0xFF, 16*count2); | 
 | 3666 |     memset(mlevel3, 0, 128*count3); | 
 | 3667 |     count3 = 0; | 
 | 3668 |     for (i = 1; i < 256; i++) { | 
 | 3669 |         int o1, o2, o3, i2, i3; | 
 | 3670 |         if (decode[i] == 0xFFFE) | 
 | 3671 |             /* unmapped character */ | 
 | 3672 |             continue; | 
 | 3673 |         o1 = decode[i]>>11; | 
 | 3674 |         o2 = (decode[i]>>7) & 0xF; | 
 | 3675 |         i2 = 16*mlevel1[o1] + o2; | 
 | 3676 |         if (mlevel2[i2] == 0xFF) | 
 | 3677 |             mlevel2[i2] = count3++; | 
 | 3678 |         o3 = decode[i] & 0x7F; | 
 | 3679 |         i3 = 128*mlevel2[i2] + o3; | 
 | 3680 |         mlevel3[i3] = i; | 
 | 3681 |     } | 
 | 3682 |     return result; | 
 | 3683 | } | 
 | 3684 |  | 
 | 3685 | static int | 
 | 3686 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) | 
 | 3687 | { | 
 | 3688 |     struct encoding_map *map = (struct encoding_map*)mapping; | 
 | 3689 |     int l1 = c>>11; | 
 | 3690 |     int l2 = (c>>7) & 0xF; | 
 | 3691 |     int l3 = c & 0x7F; | 
 | 3692 |     int i; | 
 | 3693 |  | 
 | 3694 | #ifdef Py_UNICODE_WIDE | 
 | 3695 |     if (c > 0xFFFF) { | 
 | 3696 | 	return -1; | 
 | 3697 |     } | 
 | 3698 | #endif | 
 | 3699 |     if (c == 0) | 
 | 3700 |         return 0; | 
 | 3701 |     /* level 1*/ | 
 | 3702 |     i = map->level1[l1]; | 
 | 3703 |     if (i == 0xFF) { | 
 | 3704 |         return -1; | 
 | 3705 |     } | 
 | 3706 |     /* level 2*/ | 
 | 3707 |     i = map->level23[16*i+l2]; | 
 | 3708 |     if (i == 0xFF) { | 
 | 3709 |         return -1; | 
 | 3710 |     } | 
 | 3711 |     /* level 3 */ | 
 | 3712 |     i = map->level23[16*map->count2 + 128*i + l3]; | 
 | 3713 |     if (i == 0) { | 
 | 3714 |         return -1; | 
 | 3715 |     } | 
 | 3716 |     return i; | 
 | 3717 | } | 
 | 3718 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3719 | /* Lookup the character ch in the mapping. If the character | 
 | 3720 |    can't be found, Py_None is returned (or NULL, if another | 
| Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3721 |    error occurred). */ | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3722 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3723 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3724 |     PyObject *w = PyInt_FromLong((long)c); | 
 | 3725 |     PyObject *x; | 
 | 3726 |  | 
 | 3727 |     if (w == NULL) | 
 | 3728 | 	 return NULL; | 
 | 3729 |     x = PyObject_GetItem(mapping, w); | 
 | 3730 |     Py_DECREF(w); | 
 | 3731 |     if (x == NULL) { | 
 | 3732 | 	if (PyErr_ExceptionMatches(PyExc_LookupError)) { | 
 | 3733 | 	    /* No mapping found means: mapping is undefined. */ | 
 | 3734 | 	    PyErr_Clear(); | 
 | 3735 | 	    x = Py_None; | 
 | 3736 | 	    Py_INCREF(x); | 
 | 3737 | 	    return x; | 
 | 3738 | 	} else | 
 | 3739 | 	    return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3740 |     } | 
| Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 3741 |     else if (x == Py_None) | 
 | 3742 | 	return x; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3743 |     else if (PyInt_Check(x)) { | 
 | 3744 | 	long value = PyInt_AS_LONG(x); | 
 | 3745 | 	if (value < 0 || value > 255) { | 
 | 3746 | 	    PyErr_SetString(PyExc_TypeError, | 
 | 3747 | 			     "character mapping must be in range(256)"); | 
 | 3748 | 	    Py_DECREF(x); | 
 | 3749 | 	    return NULL; | 
 | 3750 | 	} | 
 | 3751 | 	return x; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3752 |     } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3753 |     else if (PyString_Check(x)) | 
 | 3754 | 	return x; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3755 |     else { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3756 | 	/* wrong return value */ | 
| Walter Dörwald | 580ceed | 2007-05-09 10:39:19 +0000 | [diff] [blame] | 3757 | 	PyErr_Format(PyExc_TypeError, | 
 | 3758 |                 "character mapping must return integer, None or str8, not %.400s", | 
 | 3759 |                 x->ob_type->tp_name); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3760 | 	Py_DECREF(x); | 
 | 3761 | 	return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3762 |     } | 
 | 3763 | } | 
 | 3764 |  | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3765 | static int | 
| Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3766 | charmapencode_resize(PyObject *outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3767 | { | 
| Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3768 | 	Py_ssize_t outsize = PyBytes_GET_SIZE(  outobj); | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3769 | 	/* exponentially overallocate to minimize reallocations */ | 
 | 3770 | 	if (requiredsize < 2*outsize) | 
 | 3771 | 	    requiredsize = 2*outsize; | 
| Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3772 | 	if (PyBytes_Resize(outobj, requiredsize)) { | 
 | 3773 | 	    Py_DECREF(outobj); | 
 | 3774 | 	    return -1; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3775 | 	} | 
| Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3776 | 	return 0; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3777 | } | 
 | 3778 |  | 
 | 3779 | typedef enum charmapencode_result {  | 
 | 3780 |   enc_SUCCESS, enc_FAILED, enc_EXCEPTION  | 
 | 3781 | }charmapencode_result; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3782 | /* 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] | 3783 |    various state variables. Resize the output bytes object if not enough | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3784 |    space is available. Return a new reference to the object that | 
 | 3785 |    was put in the output buffer, or Py_None, if the mapping was undefined | 
 | 3786 |    (in which case no character was written) or NULL, if a | 
| Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 3787 |    reallocation error occurred. The caller must decref the result */ | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3788 | static | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3789 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, | 
| Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3790 |     PyObject *outobj, Py_ssize_t *outpos) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3791 | { | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3792 |     PyObject *rep; | 
 | 3793 |     char *outstart; | 
| Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3794 |     Py_ssize_t outsize = PyBytes_GET_SIZE(outobj); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3795 |  | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3796 |     if (mapping->ob_type == &EncodingMapType) { | 
 | 3797 |         int res = encoding_map_lookup(c, mapping); | 
 | 3798 | 	Py_ssize_t requiredsize = *outpos+1; | 
 | 3799 |         if (res == -1) | 
 | 3800 |             return enc_FAILED; | 
 | 3801 | 	if (outsize<requiredsize)  | 
| Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3802 | 	    if (charmapencode_resize(outobj, outpos, requiredsize)) | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3803 | 		return enc_EXCEPTION; | 
| Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3804 |         outstart = PyBytes_AS_STRING(outobj); | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3805 | 	outstart[(*outpos)++] = (char)res; | 
 | 3806 | 	return enc_SUCCESS; | 
 | 3807 |     } | 
 | 3808 |  | 
 | 3809 |     rep = charmapencode_lookup(c, mapping); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3810 |     if (rep==NULL) | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3811 | 	return enc_EXCEPTION; | 
 | 3812 |     else if (rep==Py_None) { | 
 | 3813 | 	Py_DECREF(rep); | 
 | 3814 | 	return enc_FAILED; | 
 | 3815 |     } else { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3816 | 	if (PyInt_Check(rep)) { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3817 | 	    Py_ssize_t requiredsize = *outpos+1; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3818 | 	    if (outsize<requiredsize) | 
| Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3819 | 		if (charmapencode_resize(outobj, outpos, requiredsize)) { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3820 | 		    Py_DECREF(rep); | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3821 | 		    return enc_EXCEPTION; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3822 | 		} | 
| Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3823 |             outstart = PyBytes_AS_STRING(outobj); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3824 | 	    outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); | 
 | 3825 | 	} | 
 | 3826 | 	else { | 
 | 3827 | 	    const char *repchars = PyString_AS_STRING(rep); | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3828 | 	    Py_ssize_t repsize = PyString_GET_SIZE(rep); | 
 | 3829 | 	    Py_ssize_t requiredsize = *outpos+repsize; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3830 | 	    if (outsize<requiredsize) | 
| Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3831 | 		if (charmapencode_resize(outobj, outpos, requiredsize)) { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3832 | 		    Py_DECREF(rep); | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3833 | 		    return enc_EXCEPTION; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3834 | 		} | 
| Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3835 |             outstart = PyBytes_AS_STRING(outobj); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3836 | 	    memcpy(outstart + *outpos, repchars, repsize); | 
 | 3837 | 	    *outpos += repsize; | 
 | 3838 | 	} | 
 | 3839 |     } | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3840 |     Py_DECREF(rep); | 
 | 3841 |     return enc_SUCCESS; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3842 | } | 
 | 3843 |  | 
 | 3844 | /* handle an error in PyUnicode_EncodeCharmap | 
 | 3845 |    Return 0 on success, -1 on error */ | 
 | 3846 | static | 
 | 3847 | int charmap_encoding_error( | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3848 |     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] | 3849 |     PyObject **exceptionObject, | 
| Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3850 |     int *known_errorHandler, PyObject **errorHandler, const char *errors, | 
| Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3851 |     PyObject *res, Py_ssize_t *respos) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3852 | { | 
 | 3853 |     PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3854 |     Py_ssize_t repsize; | 
 | 3855 |     Py_ssize_t newpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3856 |     Py_UNICODE *uni2; | 
 | 3857 |     /* startpos for collecting unencodable chars */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3858 |     Py_ssize_t collstartpos = *inpos; | 
 | 3859 |     Py_ssize_t collendpos = *inpos+1; | 
 | 3860 |     Py_ssize_t collpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3861 |     char *encoding = "charmap"; | 
 | 3862 |     char *reason = "character maps to <undefined>"; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3863 |     charmapencode_result x; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3864 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3865 |     /* find all unencodable characters */ | 
 | 3866 |     while (collendpos < size) { | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3867 |         PyObject *rep; | 
 | 3868 |         if (mapping->ob_type == &EncodingMapType) { | 
 | 3869 | 	    int res = encoding_map_lookup(p[collendpos], mapping); | 
 | 3870 | 	    if (res != -1) | 
 | 3871 | 		break; | 
 | 3872 | 	    ++collendpos; | 
 | 3873 | 	    continue; | 
 | 3874 | 	} | 
 | 3875 |              | 
 | 3876 | 	rep = charmapencode_lookup(p[collendpos], mapping); | 
 | 3877 | 	if (rep==NULL) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3878 | 	    return -1; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3879 | 	else if (rep!=Py_None) { | 
 | 3880 | 	    Py_DECREF(rep); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3881 | 	    break; | 
 | 3882 | 	} | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3883 | 	Py_DECREF(rep); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3884 | 	++collendpos; | 
 | 3885 |     } | 
 | 3886 |     /* cache callback name lookup | 
 | 3887 |      * (if not done yet, i.e. it's the first error) */ | 
 | 3888 |     if (*known_errorHandler==-1) { | 
 | 3889 | 	if ((errors==NULL) || (!strcmp(errors, "strict"))) | 
 | 3890 | 	    *known_errorHandler = 1; | 
 | 3891 | 	else if (!strcmp(errors, "replace")) | 
 | 3892 | 	    *known_errorHandler = 2; | 
 | 3893 | 	else if (!strcmp(errors, "ignore")) | 
 | 3894 | 	    *known_errorHandler = 3; | 
 | 3895 | 	else if (!strcmp(errors, "xmlcharrefreplace")) | 
 | 3896 | 	    *known_errorHandler = 4; | 
 | 3897 | 	else | 
 | 3898 | 	    *known_errorHandler = 0; | 
 | 3899 |     } | 
 | 3900 |     switch (*known_errorHandler) { | 
 | 3901 | 	case 1: /* strict */ | 
 | 3902 | 	    raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); | 
 | 3903 | 	    return -1; | 
 | 3904 | 	case 2: /* replace */ | 
 | 3905 | 	    for (collpos = collstartpos; collpos<collendpos; ++collpos) { | 
 | 3906 | 		x = charmapencode_output('?', mapping, res, respos); | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3907 | 		if (x==enc_EXCEPTION) { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3908 | 		    return -1; | 
 | 3909 | 		} | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3910 | 		else if (x==enc_FAILED) { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3911 | 		    raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); | 
 | 3912 | 		    return -1; | 
 | 3913 | 		} | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3914 | 	    } | 
 | 3915 | 	    /* fall through */ | 
 | 3916 | 	case 3: /* ignore */ | 
 | 3917 | 	    *inpos = collendpos; | 
 | 3918 | 	    break; | 
 | 3919 | 	case 4: /* xmlcharrefreplace */ | 
 | 3920 | 	    /* generate replacement (temporarily (mis)uses p) */ | 
 | 3921 | 	    for (collpos = collstartpos; collpos < collendpos; ++collpos) { | 
 | 3922 | 		char buffer[2+29+1+1]; | 
 | 3923 | 		char *cp; | 
 | 3924 | 		sprintf(buffer, "&#%d;", (int)p[collpos]); | 
 | 3925 | 		for (cp = buffer; *cp; ++cp) { | 
 | 3926 | 		    x = charmapencode_output(*cp, mapping, res, respos); | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3927 | 		    if (x==enc_EXCEPTION) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3928 | 			return -1; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3929 | 		    else if (x==enc_FAILED) { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3930 | 			raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); | 
 | 3931 | 			return -1; | 
 | 3932 | 		    } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3933 | 		} | 
 | 3934 | 	    } | 
 | 3935 | 	    *inpos = collendpos; | 
 | 3936 | 	    break; | 
 | 3937 | 	default: | 
| Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3938 | 	    repunicode = unicode_encode_call_errorhandler(errors, errorHandler, | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3939 | 		encoding, reason, p, size, exceptionObject, | 
 | 3940 | 		collstartpos, collendpos, &newpos); | 
 | 3941 | 	    if (repunicode == NULL) | 
 | 3942 | 		return -1; | 
 | 3943 | 	    /* generate replacement  */ | 
 | 3944 | 	    repsize = PyUnicode_GET_SIZE(repunicode); | 
 | 3945 | 	    for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { | 
 | 3946 | 		x = charmapencode_output(*uni2, mapping, res, respos); | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3947 | 		if (x==enc_EXCEPTION) { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3948 | 		    return -1; | 
 | 3949 | 		} | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3950 | 		else if (x==enc_FAILED) { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3951 | 		    Py_DECREF(repunicode); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3952 | 		    raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); | 
 | 3953 | 		    return -1; | 
 | 3954 | 		} | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3955 | 	    } | 
 | 3956 | 	    *inpos = newpos; | 
 | 3957 | 	    Py_DECREF(repunicode); | 
 | 3958 |     } | 
 | 3959 |     return 0; | 
 | 3960 | } | 
 | 3961 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3962 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3963 | 				  Py_ssize_t size, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3964 | 				  PyObject *mapping, | 
 | 3965 | 				  const char *errors) | 
 | 3966 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3967 |     /* output object */ | 
 | 3968 |     PyObject *res = NULL; | 
 | 3969 |     /* current input position */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3970 |     Py_ssize_t inpos = 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3971 |     /* current output position */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3972 |     Py_ssize_t respos = 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3973 |     PyObject *errorHandler = NULL; | 
 | 3974 |     PyObject *exc = NULL; | 
 | 3975 |     /* the following variable is used for caching string comparisons | 
 | 3976 |      * -1=not initialized, 0=unknown, 1=strict, 2=replace, | 
 | 3977 |      * 3=ignore, 4=xmlcharrefreplace */ | 
 | 3978 |     int known_errorHandler = -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3979 |  | 
 | 3980 |     /* Default to Latin-1 */ | 
 | 3981 |     if (mapping == NULL) | 
 | 3982 | 	return PyUnicode_EncodeLatin1(p, size, errors); | 
 | 3983 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3984 |     /* allocate enough for a simple encoding without | 
 | 3985 |        replacements, if we need more, we'll resize */ | 
| Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3986 |     res = PyBytes_FromStringAndSize(NULL, size); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3987 |     if (res == NULL) | 
 | 3988 |         goto onError; | 
| Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3989 |     if (size == 0) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3990 | 	return res; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3991 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3992 |     while (inpos<size) { | 
 | 3993 | 	/* try to encode it */ | 
| Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 3994 | 	charmapencode_result x = charmapencode_output(p[inpos], mapping, res, &respos); | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3995 | 	if (x==enc_EXCEPTION) /* error */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3996 | 	    goto onError; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3997 | 	if (x==enc_FAILED) { /* unencodable character */ | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3998 | 	    if (charmap_encoding_error(p, size, &inpos, mapping, | 
 | 3999 | 		&exc, | 
| Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4000 | 		&known_errorHandler, &errorHandler, errors, | 
| Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4001 | 		res, &respos)) { | 
| Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 4002 | 		goto onError; | 
| Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 4003 | 	    } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4004 | 	} | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4005 | 	else | 
 | 4006 | 	    /* done with this character => adjust input position */ | 
 | 4007 | 	    ++inpos; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4008 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4009 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4010 |     /* Resize if we allocated to much */ | 
| Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4011 |     if (respos<PyBytes_GET_SIZE(res)) { | 
 | 4012 | 	if (PyBytes_Resize(res, respos)) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4013 | 	    goto onError; | 
 | 4014 |     } | 
 | 4015 |     Py_XDECREF(exc); | 
 | 4016 |     Py_XDECREF(errorHandler); | 
 | 4017 |     return res; | 
 | 4018 |  | 
 | 4019 |     onError: | 
 | 4020 |     Py_XDECREF(res); | 
 | 4021 |     Py_XDECREF(exc); | 
 | 4022 |     Py_XDECREF(errorHandler); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4023 |     return NULL; | 
 | 4024 | } | 
 | 4025 |  | 
 | 4026 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, | 
 | 4027 | 				    PyObject *mapping) | 
 | 4028 | { | 
 | 4029 |     if (!PyUnicode_Check(unicode) || mapping == NULL) { | 
 | 4030 | 	PyErr_BadArgument(); | 
 | 4031 | 	return NULL; | 
 | 4032 |     } | 
 | 4033 |     return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), | 
 | 4034 | 				   PyUnicode_GET_SIZE(unicode), | 
 | 4035 | 				   mapping, | 
 | 4036 | 				   NULL); | 
 | 4037 | } | 
 | 4038 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4039 | /* create or adjust a UnicodeTranslateError */ | 
 | 4040 | static void make_translate_exception(PyObject **exceptionObject, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4041 |     const Py_UNICODE *unicode, Py_ssize_t size, | 
 | 4042 |     Py_ssize_t startpos, Py_ssize_t endpos, | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4043 |     const char *reason) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4044 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4045 |     if (*exceptionObject == NULL) { | 
 | 4046 |     	*exceptionObject = PyUnicodeTranslateError_Create( | 
 | 4047 | 	    unicode, size, startpos, endpos, reason); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4048 |     } | 
 | 4049 |     else { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4050 | 	if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) | 
 | 4051 | 	    goto onError; | 
 | 4052 | 	if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) | 
 | 4053 | 	    goto onError; | 
 | 4054 | 	if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) | 
 | 4055 | 	    goto onError; | 
 | 4056 | 	return; | 
 | 4057 | 	onError: | 
 | 4058 | 	Py_DECREF(*exceptionObject); | 
 | 4059 | 	*exceptionObject = NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4060 |     } | 
 | 4061 | } | 
 | 4062 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4063 | /* raises a UnicodeTranslateError */ | 
 | 4064 | static void raise_translate_exception(PyObject **exceptionObject, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4065 |     const Py_UNICODE *unicode, Py_ssize_t size, | 
 | 4066 |     Py_ssize_t startpos, Py_ssize_t endpos, | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4067 |     const char *reason) | 
 | 4068 | { | 
 | 4069 |     make_translate_exception(exceptionObject, | 
 | 4070 | 	unicode, size, startpos, endpos, reason); | 
 | 4071 |     if (*exceptionObject != NULL) | 
 | 4072 | 	PyCodec_StrictErrors(*exceptionObject); | 
 | 4073 | } | 
 | 4074 |  | 
 | 4075 | /* error handling callback helper: | 
 | 4076 |    build arguments, call the callback and check the arguments, | 
 | 4077 |    put the result into newpos and return the replacement string, which | 
 | 4078 |    has to be freed by the caller */ | 
 | 4079 | static PyObject *unicode_translate_call_errorhandler(const char *errors, | 
 | 4080 |     PyObject **errorHandler, | 
 | 4081 |     const char *reason, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4082 |     const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, | 
 | 4083 |     Py_ssize_t startpos, Py_ssize_t endpos, | 
 | 4084 |     Py_ssize_t *newpos) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4085 | { | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4086 |     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] | 4087 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4088 |     Py_ssize_t i_newpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4089 |     PyObject *restuple; | 
 | 4090 |     PyObject *resunicode; | 
 | 4091 |  | 
 | 4092 |     if (*errorHandler == NULL) { | 
 | 4093 | 	*errorHandler = PyCodec_LookupError(errors); | 
 | 4094 |         if (*errorHandler == NULL) | 
 | 4095 | 	    return NULL; | 
 | 4096 |     } | 
 | 4097 |  | 
 | 4098 |     make_translate_exception(exceptionObject, | 
 | 4099 | 	unicode, size, startpos, endpos, reason); | 
 | 4100 |     if (*exceptionObject == NULL) | 
 | 4101 | 	return NULL; | 
 | 4102 |  | 
 | 4103 |     restuple = PyObject_CallFunctionObjArgs( | 
 | 4104 | 	*errorHandler, *exceptionObject, NULL); | 
 | 4105 |     if (restuple == NULL) | 
 | 4106 | 	return NULL; | 
 | 4107 |     if (!PyTuple_Check(restuple)) { | 
 | 4108 | 	PyErr_Format(PyExc_TypeError, &argparse[4]); | 
 | 4109 | 	Py_DECREF(restuple); | 
 | 4110 | 	return NULL; | 
 | 4111 |     } | 
 | 4112 |     if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4113 | 	&resunicode, &i_newpos)) { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4114 | 	Py_DECREF(restuple); | 
 | 4115 | 	return NULL; | 
 | 4116 |     } | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4117 |     if (i_newpos<0) | 
 | 4118 | 	*newpos = size+i_newpos; | 
 | 4119 |     else | 
 | 4120 |         *newpos = i_newpos; | 
| Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4121 |     if (*newpos<0 || *newpos>size) { | 
| Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 4122 | 	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] | 4123 | 	Py_DECREF(restuple); | 
 | 4124 | 	return NULL; | 
 | 4125 |     } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4126 |     Py_INCREF(resunicode); | 
 | 4127 |     Py_DECREF(restuple); | 
 | 4128 |     return resunicode; | 
 | 4129 | } | 
 | 4130 |  | 
 | 4131 | /* Lookup the character ch in the mapping and put the result in result, | 
 | 4132 |    which must be decrefed by the caller. | 
 | 4133 |    Return 0 on success, -1 on error */ | 
 | 4134 | static | 
 | 4135 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) | 
 | 4136 | { | 
 | 4137 |     PyObject *w = PyInt_FromLong((long)c); | 
 | 4138 |     PyObject *x; | 
 | 4139 |  | 
 | 4140 |     if (w == NULL) | 
 | 4141 | 	 return -1; | 
 | 4142 |     x = PyObject_GetItem(mapping, w); | 
 | 4143 |     Py_DECREF(w); | 
 | 4144 |     if (x == NULL) { | 
 | 4145 | 	if (PyErr_ExceptionMatches(PyExc_LookupError)) { | 
 | 4146 | 	    /* No mapping found means: use 1:1 mapping. */ | 
 | 4147 | 	    PyErr_Clear(); | 
 | 4148 | 	    *result = NULL; | 
 | 4149 | 	    return 0; | 
 | 4150 | 	} else | 
 | 4151 | 	    return -1; | 
 | 4152 |     } | 
 | 4153 |     else if (x == Py_None) { | 
 | 4154 | 	*result = x; | 
 | 4155 | 	return 0; | 
 | 4156 |     } | 
 | 4157 |     else if (PyInt_Check(x)) { | 
 | 4158 | 	long value = PyInt_AS_LONG(x); | 
 | 4159 | 	long max = PyUnicode_GetMax(); | 
 | 4160 | 	if (value < 0 || value > max) { | 
 | 4161 | 	    PyErr_Format(PyExc_TypeError, | 
 | 4162 | 			     "character mapping must be in range(0x%lx)", max+1); | 
 | 4163 | 	    Py_DECREF(x); | 
 | 4164 | 	    return -1; | 
 | 4165 | 	} | 
 | 4166 | 	*result = x; | 
 | 4167 | 	return 0; | 
 | 4168 |     } | 
 | 4169 |     else if (PyUnicode_Check(x)) { | 
 | 4170 | 	*result = x; | 
 | 4171 | 	return 0; | 
 | 4172 |     } | 
 | 4173 |     else { | 
 | 4174 | 	/* wrong return value */ | 
 | 4175 | 	PyErr_SetString(PyExc_TypeError, | 
 | 4176 | 	      "character mapping must return integer, None or unicode"); | 
| Walter Dörwald | 150523e | 2003-08-15 16:52:19 +0000 | [diff] [blame] | 4177 | 	Py_DECREF(x); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4178 | 	return -1; | 
 | 4179 |     } | 
 | 4180 | } | 
 | 4181 | /* ensure that *outobj is at least requiredsize characters long, | 
 | 4182 | if not reallocate and adjust various state variables. | 
 | 4183 | Return 0 on success, -1 on error */ | 
 | 4184 | static | 
| Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4185 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4186 |     Py_ssize_t requiredsize) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4187 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4188 |     Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); | 
| Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4189 |     if (requiredsize > oldsize) { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4190 | 	/* remember old output position */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4191 | 	Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4192 | 	/* exponentially overallocate to minimize reallocations */ | 
| Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4193 | 	if (requiredsize < 2 * oldsize) | 
 | 4194 | 	    requiredsize = 2 * oldsize; | 
| Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4195 | 	if (_PyUnicode_Resize(outobj, requiredsize) < 0) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4196 | 	    return -1; | 
 | 4197 | 	*outp = PyUnicode_AS_UNICODE(*outobj) + outpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4198 |     } | 
 | 4199 |     return 0; | 
 | 4200 | } | 
 | 4201 | /* lookup the character, put the result in the output string and adjust | 
 | 4202 |    various state variables. Return a new reference to the object that | 
 | 4203 |    was put in the output buffer in *result, or Py_None, if the mapping was | 
 | 4204 |    undefined (in which case no character was written). | 
 | 4205 |    The called must decref result. | 
 | 4206 |    Return 0 on success, -1 on error. */ | 
 | 4207 | static | 
| Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4208 | 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] | 4209 |     Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, | 
| Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4210 |     PyObject **res) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4211 | { | 
| Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4212 |     if (charmaptranslate_lookup(*curinp, mapping, res)) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4213 | 	return -1; | 
 | 4214 |     if (*res==NULL) { | 
 | 4215 | 	/* not found => default to 1:1 mapping */ | 
| Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4216 | 	*(*outp)++ = *curinp; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4217 |     } | 
 | 4218 |     else if (*res==Py_None) | 
 | 4219 | 	; | 
 | 4220 |     else if (PyInt_Check(*res)) { | 
 | 4221 | 	/* no overflow check, because we know that the space is enough */ | 
 | 4222 | 	*(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); | 
 | 4223 |     } | 
 | 4224 |     else if (PyUnicode_Check(*res)) { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4225 | 	Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4226 | 	if (repsize==1) { | 
 | 4227 | 	    /* no overflow check, because we know that the space is enough */ | 
 | 4228 | 	    *(*outp)++ = *PyUnicode_AS_UNICODE(*res); | 
 | 4229 | 	} | 
 | 4230 | 	else if (repsize!=0) { | 
 | 4231 | 	    /* more than one character */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4232 | 	    Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + | 
| Walter Dörwald | cd736e7 | 2004-02-05 17:36:00 +0000 | [diff] [blame] | 4233 | 		(insize - (curinp-startinp)) + | 
| Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4234 | 		repsize - 1; | 
 | 4235 | 	    if (charmaptranslate_makespace(outobj, outp, requiredsize)) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4236 | 		return -1; | 
 | 4237 | 	    memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); | 
 | 4238 | 	    *outp += repsize; | 
 | 4239 | 	} | 
 | 4240 |     } | 
 | 4241 |     else | 
 | 4242 | 	return -1; | 
 | 4243 |     return 0; | 
 | 4244 | } | 
 | 4245 |  | 
 | 4246 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4247 | 				     Py_ssize_t size, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4248 | 				     PyObject *mapping, | 
 | 4249 | 				     const char *errors) | 
 | 4250 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4251 |     /* output object */ | 
 | 4252 |     PyObject *res = NULL; | 
 | 4253 |     /* pointers to the beginning and end+1 of input */ | 
 | 4254 |     const Py_UNICODE *startp = p; | 
 | 4255 |     const Py_UNICODE *endp = p + size; | 
 | 4256 |     /* pointer into the output */ | 
 | 4257 |     Py_UNICODE *str; | 
 | 4258 |     /* current output position */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4259 |     Py_ssize_t respos = 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4260 |     char *reason = "character maps to <undefined>"; | 
 | 4261 |     PyObject *errorHandler = NULL; | 
 | 4262 |     PyObject *exc = NULL; | 
 | 4263 |     /* the following variable is used for caching string comparisons | 
 | 4264 |      * -1=not initialized, 0=unknown, 1=strict, 2=replace, | 
 | 4265 |      * 3=ignore, 4=xmlcharrefreplace */ | 
 | 4266 |     int known_errorHandler = -1; | 
 | 4267 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4268 |     if (mapping == NULL) { | 
 | 4269 | 	PyErr_BadArgument(); | 
 | 4270 | 	return NULL; | 
 | 4271 |     } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4272 |  | 
 | 4273 |     /* allocate enough for a simple 1:1 translation without | 
 | 4274 |        replacements, if we need more, we'll resize */ | 
 | 4275 |     res = PyUnicode_FromUnicode(NULL, size); | 
 | 4276 |     if (res == NULL) | 
| Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4277 | 	goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4278 |     if (size == 0) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4279 | 	return res; | 
 | 4280 |     str = PyUnicode_AS_UNICODE(res); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4281 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4282 |     while (p<endp) { | 
 | 4283 | 	/* try to encode it */ | 
 | 4284 | 	PyObject *x = NULL; | 
| Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4285 | 	if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4286 | 	    Py_XDECREF(x); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4287 | 	    goto onError; | 
 | 4288 | 	} | 
| Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 4289 | 	Py_XDECREF(x); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4290 | 	if (x!=Py_None) /* it worked => adjust input pointer */ | 
 | 4291 | 	    ++p; | 
 | 4292 | 	else { /* untranslatable character */ | 
 | 4293 | 	    PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4294 | 	    Py_ssize_t repsize; | 
 | 4295 | 	    Py_ssize_t newpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4296 | 	    Py_UNICODE *uni2; | 
 | 4297 | 	    /* startpos for collecting untranslatable chars */ | 
 | 4298 | 	    const Py_UNICODE *collstart = p; | 
 | 4299 | 	    const Py_UNICODE *collend = p+1; | 
 | 4300 | 	    const Py_UNICODE *coll; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4301 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4302 | 	    /* find all untranslatable characters */ | 
 | 4303 | 	    while (collend < endp) { | 
| Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4304 | 		if (charmaptranslate_lookup(*collend, mapping, &x)) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4305 | 		    goto onError; | 
 | 4306 | 		Py_XDECREF(x); | 
 | 4307 | 		if (x!=Py_None) | 
 | 4308 | 		    break; | 
 | 4309 | 		++collend; | 
 | 4310 | 	    } | 
 | 4311 | 	    /* cache callback name lookup | 
 | 4312 | 	     * (if not done yet, i.e. it's the first error) */ | 
 | 4313 | 	    if (known_errorHandler==-1) { | 
 | 4314 | 		if ((errors==NULL) || (!strcmp(errors, "strict"))) | 
 | 4315 | 		    known_errorHandler = 1; | 
 | 4316 | 		else if (!strcmp(errors, "replace")) | 
 | 4317 | 		    known_errorHandler = 2; | 
 | 4318 | 		else if (!strcmp(errors, "ignore")) | 
 | 4319 | 		    known_errorHandler = 3; | 
 | 4320 | 		else if (!strcmp(errors, "xmlcharrefreplace")) | 
 | 4321 | 		    known_errorHandler = 4; | 
 | 4322 | 		else | 
 | 4323 | 		    known_errorHandler = 0; | 
 | 4324 | 	    } | 
 | 4325 | 	    switch (known_errorHandler) { | 
 | 4326 | 		case 1: /* strict */ | 
 | 4327 | 		    raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); | 
 | 4328 | 		    goto onError; | 
 | 4329 | 		case 2: /* replace */ | 
 | 4330 | 		    /* No need to check for space, this is a 1:1 replacement */ | 
 | 4331 | 		    for (coll = collstart; coll<collend; ++coll) | 
 | 4332 | 			*str++ = '?'; | 
 | 4333 | 		    /* fall through */ | 
 | 4334 | 		case 3: /* ignore */ | 
 | 4335 | 		    p = collend; | 
 | 4336 | 		    break; | 
 | 4337 | 		case 4: /* xmlcharrefreplace */ | 
 | 4338 | 		    /* generate replacement (temporarily (mis)uses p) */ | 
 | 4339 | 		    for (p = collstart; p < collend; ++p) { | 
 | 4340 | 			char buffer[2+29+1+1]; | 
 | 4341 | 			char *cp; | 
 | 4342 | 			sprintf(buffer, "&#%d;", (int)*p); | 
| Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4343 | 			if (charmaptranslate_makespace(&res, &str, | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4344 | 			    (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) | 
 | 4345 | 			    goto onError; | 
 | 4346 | 			for (cp = buffer; *cp; ++cp) | 
 | 4347 | 			    *str++ = *cp; | 
 | 4348 | 		    } | 
 | 4349 | 		    p = collend; | 
 | 4350 | 		    break; | 
 | 4351 | 		default: | 
 | 4352 | 		    repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, | 
 | 4353 | 			reason, startp, size, &exc, | 
 | 4354 | 			collstart-startp, collend-startp, &newpos); | 
 | 4355 | 		    if (repunicode == NULL) | 
 | 4356 | 			goto onError; | 
 | 4357 | 		    /* generate replacement  */ | 
 | 4358 | 		    repsize = PyUnicode_GET_SIZE(repunicode); | 
| Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4359 | 		    if (charmaptranslate_makespace(&res, &str, | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4360 | 			(str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { | 
 | 4361 | 			Py_DECREF(repunicode); | 
 | 4362 | 			goto onError; | 
 | 4363 | 		    } | 
 | 4364 | 		    for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) | 
 | 4365 | 			*str++ = *uni2; | 
 | 4366 | 		    p = startp + newpos; | 
 | 4367 | 		    Py_DECREF(repunicode); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4368 | 	    } | 
 | 4369 | 	} | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4370 |     } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4371 |     /* Resize if we allocated to much */ | 
 | 4372 |     respos = str-PyUnicode_AS_UNICODE(res); | 
| Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4373 |     if (respos<PyUnicode_GET_SIZE(res)) { | 
| Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4374 | 	if (_PyUnicode_Resize(&res, respos) < 0) | 
| Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 4375 | 	    goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4376 |     } | 
 | 4377 |     Py_XDECREF(exc); | 
 | 4378 |     Py_XDECREF(errorHandler); | 
 | 4379 |     return res; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4380 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4381 |     onError: | 
 | 4382 |     Py_XDECREF(res); | 
 | 4383 |     Py_XDECREF(exc); | 
 | 4384 |     Py_XDECREF(errorHandler); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4385 |     return NULL; | 
 | 4386 | } | 
 | 4387 |  | 
 | 4388 | PyObject *PyUnicode_Translate(PyObject *str, | 
 | 4389 | 			      PyObject *mapping, | 
 | 4390 | 			      const char *errors) | 
 | 4391 | { | 
 | 4392 |     PyObject *result; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4393 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4394 |     str = PyUnicode_FromObject(str); | 
 | 4395 |     if (str == NULL) | 
 | 4396 | 	goto onError; | 
 | 4397 |     result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), | 
 | 4398 | 					PyUnicode_GET_SIZE(str), | 
 | 4399 | 					mapping, | 
 | 4400 | 					errors); | 
 | 4401 |     Py_DECREF(str); | 
 | 4402 |     return result; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4403 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4404 |  onError: | 
 | 4405 |     Py_XDECREF(str); | 
 | 4406 |     return NULL; | 
 | 4407 | } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4408 |  | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4409 | /* --- Decimal Encoder ---------------------------------------------------- */ | 
 | 4410 |  | 
 | 4411 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4412 | 			    Py_ssize_t length, | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4413 | 			    char *output, | 
 | 4414 | 			    const char *errors) | 
 | 4415 | { | 
 | 4416 |     Py_UNICODE *p, *end; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4417 |     PyObject *errorHandler = NULL; | 
 | 4418 |     PyObject *exc = NULL; | 
 | 4419 |     const char *encoding = "decimal"; | 
 | 4420 |     const char *reason = "invalid decimal Unicode string"; | 
 | 4421 |     /* the following variable is used for caching string comparisons | 
 | 4422 |      * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ | 
 | 4423 |     int known_errorHandler = -1; | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4424 |  | 
 | 4425 |     if (output == NULL) { | 
 | 4426 | 	PyErr_BadArgument(); | 
 | 4427 | 	return -1; | 
 | 4428 |     } | 
 | 4429 |  | 
 | 4430 |     p = s; | 
 | 4431 |     end = s + length; | 
 | 4432 |     while (p < end) { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4433 | 	register Py_UNICODE ch = *p; | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4434 | 	int decimal; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4435 | 	PyObject *repunicode; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4436 | 	Py_ssize_t repsize; | 
 | 4437 | 	Py_ssize_t newpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4438 | 	Py_UNICODE *uni2; | 
 | 4439 | 	Py_UNICODE *collstart; | 
 | 4440 | 	Py_UNICODE *collend; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4441 |  | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4442 | 	if (Py_UNICODE_ISSPACE(ch)) { | 
 | 4443 | 	    *output++ = ' '; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4444 | 	    ++p; | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4445 | 	    continue; | 
 | 4446 | 	} | 
 | 4447 | 	decimal = Py_UNICODE_TODECIMAL(ch); | 
 | 4448 | 	if (decimal >= 0) { | 
 | 4449 | 	    *output++ = '0' + decimal; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4450 | 	    ++p; | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4451 | 	    continue; | 
 | 4452 | 	} | 
| Guido van Rossum | ba47704 | 2000-04-06 18:18:10 +0000 | [diff] [blame] | 4453 | 	if (0 < ch && ch < 256) { | 
| Guido van Rossum | 42c29aa | 2000-05-03 23:58:29 +0000 | [diff] [blame] | 4454 | 	    *output++ = (char)ch; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4455 | 	    ++p; | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4456 | 	    continue; | 
 | 4457 | 	} | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4458 | 	/* All other characters are considered unencodable */ | 
 | 4459 | 	collstart = p; | 
 | 4460 | 	collend = p+1; | 
 | 4461 | 	while (collend < end) { | 
 | 4462 | 	    if ((0 < *collend && *collend < 256) || | 
 | 4463 | 	        !Py_UNICODE_ISSPACE(*collend) || | 
 | 4464 | 	        Py_UNICODE_TODECIMAL(*collend)) | 
 | 4465 | 		break; | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4466 | 	} | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4467 | 	/* cache callback name lookup | 
 | 4468 | 	 * (if not done yet, i.e. it's the first error) */ | 
 | 4469 | 	if (known_errorHandler==-1) { | 
 | 4470 | 	    if ((errors==NULL) || (!strcmp(errors, "strict"))) | 
 | 4471 | 		known_errorHandler = 1; | 
 | 4472 | 	    else if (!strcmp(errors, "replace")) | 
 | 4473 | 		known_errorHandler = 2; | 
 | 4474 | 	    else if (!strcmp(errors, "ignore")) | 
 | 4475 | 		known_errorHandler = 3; | 
 | 4476 | 	    else if (!strcmp(errors, "xmlcharrefreplace")) | 
 | 4477 | 		known_errorHandler = 4; | 
 | 4478 | 	    else | 
 | 4479 | 		known_errorHandler = 0; | 
 | 4480 | 	} | 
 | 4481 | 	switch (known_errorHandler) { | 
 | 4482 | 	    case 1: /* strict */ | 
 | 4483 | 		raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); | 
 | 4484 | 		goto onError; | 
 | 4485 | 	    case 2: /* replace */ | 
 | 4486 | 		for (p = collstart; p < collend; ++p) | 
 | 4487 | 		    *output++ = '?'; | 
 | 4488 | 		/* fall through */ | 
 | 4489 | 	    case 3: /* ignore */ | 
 | 4490 | 		p = collend; | 
 | 4491 | 		break; | 
 | 4492 | 	    case 4: /* xmlcharrefreplace */ | 
 | 4493 | 		/* generate replacement (temporarily (mis)uses p) */ | 
 | 4494 | 		for (p = collstart; p < collend; ++p) | 
 | 4495 | 		    output += sprintf(output, "&#%d;", (int)*p); | 
 | 4496 | 		p = collend; | 
 | 4497 | 		break; | 
 | 4498 | 	    default: | 
 | 4499 | 		repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, | 
 | 4500 | 		    encoding, reason, s, length, &exc, | 
 | 4501 | 		    collstart-s, collend-s, &newpos); | 
 | 4502 | 		if (repunicode == NULL) | 
 | 4503 | 		    goto onError; | 
 | 4504 | 		/* generate replacement  */ | 
 | 4505 | 		repsize = PyUnicode_GET_SIZE(repunicode); | 
 | 4506 | 		for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { | 
 | 4507 | 		    Py_UNICODE ch = *uni2; | 
 | 4508 | 		    if (Py_UNICODE_ISSPACE(ch)) | 
 | 4509 | 			*output++ = ' '; | 
 | 4510 | 		    else { | 
 | 4511 | 			decimal = Py_UNICODE_TODECIMAL(ch); | 
 | 4512 | 			if (decimal >= 0) | 
 | 4513 | 			    *output++ = '0' + decimal; | 
 | 4514 | 			else if (0 < ch && ch < 256) | 
 | 4515 | 			    *output++ = (char)ch; | 
 | 4516 | 			else { | 
 | 4517 | 			    Py_DECREF(repunicode); | 
 | 4518 | 			    raise_encode_exception(&exc, encoding, | 
 | 4519 | 				s, length, collstart-s, collend-s, reason); | 
 | 4520 | 			    goto onError; | 
 | 4521 | 			} | 
 | 4522 | 		    } | 
 | 4523 | 		} | 
 | 4524 | 		p = s + newpos; | 
 | 4525 | 		Py_DECREF(repunicode); | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4526 | 	} | 
 | 4527 |     } | 
 | 4528 |     /* 0-terminate the output string */ | 
 | 4529 |     *output++ = '\0'; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4530 |     Py_XDECREF(exc); | 
 | 4531 |     Py_XDECREF(errorHandler); | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4532 |     return 0; | 
 | 4533 |  | 
 | 4534 |  onError: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4535 |     Py_XDECREF(exc); | 
 | 4536 |     Py_XDECREF(errorHandler); | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4537 |     return -1; | 
 | 4538 | } | 
 | 4539 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4540 | /* --- Helpers ------------------------------------------------------------ */ | 
 | 4541 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4542 | #define STRINGLIB_CHAR Py_UNICODE | 
 | 4543 |  | 
 | 4544 | #define STRINGLIB_LEN PyUnicode_GET_SIZE | 
 | 4545 | #define STRINGLIB_NEW PyUnicode_FromUnicode | 
 | 4546 | #define STRINGLIB_STR PyUnicode_AS_UNICODE | 
 | 4547 |  | 
 | 4548 | Py_LOCAL_INLINE(int) | 
 | 4549 | 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] | 4550 | { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4551 |     if (str[0] != other[0]) | 
 | 4552 |         return 1; | 
 | 4553 |     return memcmp((void*) str, (void*) other, len * sizeof(Py_UNICODE)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4554 | } | 
 | 4555 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4556 | #define STRINGLIB_EMPTY unicode_empty | 
 | 4557 |  | 
 | 4558 | #include "stringlib/fastsearch.h" | 
 | 4559 |  | 
 | 4560 | #include "stringlib/count.h" | 
 | 4561 | #include "stringlib/find.h" | 
 | 4562 | #include "stringlib/partition.h" | 
 | 4563 |  | 
 | 4564 | /* helper macro to fixup start/end slice values */ | 
 | 4565 | #define FIX_START_END(obj)                      \ | 
 | 4566 |     if (start < 0)                              \ | 
 | 4567 |         start += (obj)->length;                 \ | 
 | 4568 |     if (start < 0)                              \ | 
 | 4569 |         start = 0;                              \ | 
 | 4570 |     if (end > (obj)->length)                    \ | 
 | 4571 |         end = (obj)->length;                    \ | 
 | 4572 |     if (end < 0)                                \ | 
 | 4573 |         end += (obj)->length;                   \ | 
 | 4574 |     if (end < 0)                                \ | 
 | 4575 |         end = 0; | 
 | 4576 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4577 | Py_ssize_t PyUnicode_Count(PyObject *str, | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4578 |                            PyObject *substr, | 
 | 4579 |                            Py_ssize_t start, | 
 | 4580 |                            Py_ssize_t end) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4581 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4582 |     Py_ssize_t result; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4583 |     PyUnicodeObject* str_obj; | 
 | 4584 |     PyUnicodeObject* sub_obj; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4585 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4586 |     str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); | 
 | 4587 |     if (!str_obj) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4588 | 	return -1; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4589 |     sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); | 
 | 4590 |     if (!sub_obj) { | 
 | 4591 | 	Py_DECREF(str_obj); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4592 | 	return -1; | 
 | 4593 |     } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4594 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4595 |     FIX_START_END(str_obj); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4596 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4597 |     result = stringlib_count( | 
 | 4598 |         str_obj->str + start, end - start, sub_obj->str, sub_obj->length | 
 | 4599 |         ); | 
 | 4600 |  | 
 | 4601 |     Py_DECREF(sub_obj); | 
 | 4602 |     Py_DECREF(str_obj); | 
 | 4603 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4604 |     return result; | 
 | 4605 | } | 
 | 4606 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4607 | Py_ssize_t PyUnicode_Find(PyObject *str, | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4608 |                           PyObject *sub, | 
 | 4609 |                           Py_ssize_t start, | 
 | 4610 |                           Py_ssize_t end, | 
 | 4611 |                           int direction) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4612 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4613 |     Py_ssize_t result; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4614 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4615 |     str = PyUnicode_FromObject(str); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4616 |     if (!str) | 
| Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 4617 | 	return -2; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4618 |     sub = PyUnicode_FromObject(sub); | 
 | 4619 |     if (!sub) { | 
| Marc-André Lemburg | 4164439 | 2002-05-29 13:46:29 +0000 | [diff] [blame] | 4620 | 	Py_DECREF(str); | 
| Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 4621 | 	return -2; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4622 |     } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4623 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4624 |     if (direction > 0) | 
 | 4625 |         result = stringlib_find_slice( | 
 | 4626 |             PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), | 
 | 4627 |             PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), | 
 | 4628 |             start, end | 
 | 4629 |             ); | 
 | 4630 |     else | 
 | 4631 |         result = stringlib_rfind_slice( | 
 | 4632 |             PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), | 
 | 4633 |             PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), | 
 | 4634 |             start, end | 
 | 4635 |             ); | 
 | 4636 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4637 |     Py_DECREF(str); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4638 |     Py_DECREF(sub); | 
 | 4639 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4640 |     return result; | 
 | 4641 | } | 
 | 4642 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4643 | static | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4644 | int tailmatch(PyUnicodeObject *self, | 
 | 4645 | 	      PyUnicodeObject *substring, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4646 | 	      Py_ssize_t start, | 
 | 4647 | 	      Py_ssize_t end, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4648 | 	      int direction) | 
 | 4649 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4650 |     if (substring->length == 0) | 
 | 4651 |         return 1; | 
 | 4652 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4653 |     FIX_START_END(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4654 |  | 
 | 4655 |     end -= substring->length; | 
 | 4656 |     if (end < start) | 
 | 4657 | 	return 0; | 
 | 4658 |  | 
 | 4659 |     if (direction > 0) { | 
 | 4660 | 	if (Py_UNICODE_MATCH(self, end, substring)) | 
 | 4661 | 	    return 1; | 
 | 4662 |     } else { | 
 | 4663 |         if (Py_UNICODE_MATCH(self, start, substring)) | 
 | 4664 | 	    return 1; | 
 | 4665 |     } | 
 | 4666 |  | 
 | 4667 |     return 0; | 
 | 4668 | } | 
 | 4669 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4670 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4671 | 			PyObject *substr, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4672 | 			Py_ssize_t start, | 
 | 4673 | 			Py_ssize_t end, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4674 | 			int direction) | 
 | 4675 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4676 |     Py_ssize_t result; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4677 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4678 |     str = PyUnicode_FromObject(str); | 
 | 4679 |     if (str == NULL) | 
 | 4680 | 	return -1; | 
 | 4681 |     substr = PyUnicode_FromObject(substr); | 
 | 4682 |     if (substr == NULL) { | 
| Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 4683 | 	Py_DECREF(str); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4684 | 	return -1; | 
 | 4685 |     } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4686 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4687 |     result = tailmatch((PyUnicodeObject *)str, | 
 | 4688 | 		       (PyUnicodeObject *)substr, | 
 | 4689 | 		       start, end, direction); | 
 | 4690 |     Py_DECREF(str); | 
 | 4691 |     Py_DECREF(substr); | 
 | 4692 |     return result; | 
 | 4693 | } | 
 | 4694 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4695 | /* Apply fixfct filter to the Unicode object self and return a | 
 | 4696 |    reference to the modified object */ | 
 | 4697 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4698 | static | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4699 | PyObject *fixup(PyUnicodeObject *self, | 
 | 4700 | 		int (*fixfct)(PyUnicodeObject *s)) | 
 | 4701 | { | 
 | 4702 |  | 
 | 4703 |     PyUnicodeObject *u; | 
 | 4704 |  | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4705 |     u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4706 |     if (u == NULL) | 
 | 4707 | 	return NULL; | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4708 |  | 
 | 4709 |     Py_UNICODE_COPY(u->str, self->str, self->length); | 
 | 4710 |  | 
| Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4711 |     if (!fixfct(u) && PyUnicode_CheckExact(self)) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4712 | 	/* fixfct should return TRUE if it modified the buffer. If | 
 | 4713 | 	   FALSE, return a reference to the original buffer instead | 
 | 4714 | 	   (to save space, not time) */ | 
 | 4715 | 	Py_INCREF(self); | 
 | 4716 | 	Py_DECREF(u); | 
 | 4717 | 	return (PyObject*) self; | 
 | 4718 |     } | 
 | 4719 |     return (PyObject*) u; | 
 | 4720 | } | 
 | 4721 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4722 | static | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4723 | int fixupper(PyUnicodeObject *self) | 
 | 4724 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4725 |     Py_ssize_t len = self->length; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4726 |     Py_UNICODE *s = self->str; | 
 | 4727 |     int status = 0; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4728 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4729 |     while (len-- > 0) { | 
 | 4730 | 	register Py_UNICODE ch; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4731 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4732 | 	ch = Py_UNICODE_TOUPPER(*s); | 
 | 4733 | 	if (ch != *s) { | 
 | 4734 |             status = 1; | 
 | 4735 | 	    *s = ch; | 
 | 4736 | 	} | 
 | 4737 |         s++; | 
 | 4738 |     } | 
 | 4739 |  | 
 | 4740 |     return status; | 
 | 4741 | } | 
 | 4742 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4743 | static | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4744 | int fixlower(PyUnicodeObject *self) | 
 | 4745 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4746 |     Py_ssize_t len = self->length; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4747 |     Py_UNICODE *s = self->str; | 
 | 4748 |     int status = 0; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4749 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4750 |     while (len-- > 0) { | 
 | 4751 | 	register Py_UNICODE ch; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4752 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4753 | 	ch = Py_UNICODE_TOLOWER(*s); | 
 | 4754 | 	if (ch != *s) { | 
 | 4755 |             status = 1; | 
 | 4756 | 	    *s = ch; | 
 | 4757 | 	} | 
 | 4758 |         s++; | 
 | 4759 |     } | 
 | 4760 |  | 
 | 4761 |     return status; | 
 | 4762 | } | 
 | 4763 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4764 | static | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4765 | int fixswapcase(PyUnicodeObject *self) | 
 | 4766 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4767 |     Py_ssize_t len = self->length; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4768 |     Py_UNICODE *s = self->str; | 
 | 4769 |     int status = 0; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4770 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4771 |     while (len-- > 0) { | 
 | 4772 |         if (Py_UNICODE_ISUPPER(*s)) { | 
 | 4773 |             *s = Py_UNICODE_TOLOWER(*s); | 
 | 4774 |             status = 1; | 
 | 4775 |         } else if (Py_UNICODE_ISLOWER(*s)) { | 
 | 4776 |             *s = Py_UNICODE_TOUPPER(*s); | 
 | 4777 |             status = 1; | 
 | 4778 |         } | 
 | 4779 |         s++; | 
 | 4780 |     } | 
 | 4781 |  | 
 | 4782 |     return status; | 
 | 4783 | } | 
 | 4784 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4785 | static | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4786 | int fixcapitalize(PyUnicodeObject *self) | 
 | 4787 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4788 |     Py_ssize_t len = self->length; | 
| Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4789 |     Py_UNICODE *s = self->str; | 
 | 4790 |     int status = 0; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4791 |  | 
| Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4792 |     if (len == 0) | 
 | 4793 | 	return 0; | 
 | 4794 |     if (Py_UNICODE_ISLOWER(*s)) { | 
 | 4795 | 	*s = Py_UNICODE_TOUPPER(*s); | 
 | 4796 | 	status = 1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4797 |     } | 
| Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4798 |     s++; | 
 | 4799 |     while (--len > 0) { | 
 | 4800 |         if (Py_UNICODE_ISUPPER(*s)) { | 
 | 4801 |             *s = Py_UNICODE_TOLOWER(*s); | 
 | 4802 |             status = 1; | 
 | 4803 |         } | 
 | 4804 |         s++; | 
 | 4805 |     } | 
 | 4806 |     return status; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4807 | } | 
 | 4808 |  | 
 | 4809 | static | 
 | 4810 | int fixtitle(PyUnicodeObject *self) | 
 | 4811 | { | 
 | 4812 |     register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); | 
 | 4813 |     register Py_UNICODE *e; | 
 | 4814 |     int previous_is_cased; | 
 | 4815 |  | 
 | 4816 |     /* Shortcut for single character strings */ | 
 | 4817 |     if (PyUnicode_GET_SIZE(self) == 1) { | 
 | 4818 | 	Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); | 
 | 4819 | 	if (*p != ch) { | 
 | 4820 | 	    *p = ch; | 
 | 4821 | 	    return 1; | 
 | 4822 | 	} | 
 | 4823 | 	else | 
 | 4824 | 	    return 0; | 
 | 4825 |     } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4826 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4827 |     e = p + PyUnicode_GET_SIZE(self); | 
 | 4828 |     previous_is_cased = 0; | 
 | 4829 |     for (; p < e; p++) { | 
 | 4830 | 	register const Py_UNICODE ch = *p; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4831 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4832 | 	if (previous_is_cased) | 
 | 4833 | 	    *p = Py_UNICODE_TOLOWER(ch); | 
 | 4834 | 	else | 
 | 4835 | 	    *p = Py_UNICODE_TOTITLE(ch); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4836 |  | 
 | 4837 | 	if (Py_UNICODE_ISLOWER(ch) || | 
 | 4838 | 	    Py_UNICODE_ISUPPER(ch) || | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4839 | 	    Py_UNICODE_ISTITLE(ch)) | 
 | 4840 | 	    previous_is_cased = 1; | 
 | 4841 | 	else | 
 | 4842 | 	    previous_is_cased = 0; | 
 | 4843 |     } | 
 | 4844 |     return 1; | 
 | 4845 | } | 
 | 4846 |  | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4847 | PyObject * | 
 | 4848 | PyUnicode_Join(PyObject *separator, PyObject *seq) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4849 | { | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4850 |     PyObject *internal_separator = NULL; | 
| Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 4851 |     const Py_UNICODE blank = ' '; | 
 | 4852 |     const Py_UNICODE *sep = ␣ | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4853 |     Py_ssize_t seplen = 1; | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4854 |     PyUnicodeObject *res = NULL; /* the result */ | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4855 |     Py_ssize_t res_alloc = 100;  /* # allocated bytes for string in res */ | 
 | 4856 |     Py_ssize_t res_used;         /* # used bytes */ | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4857 |     Py_UNICODE *res_p;       /* pointer to free byte in res's string area */ | 
 | 4858 |     PyObject *fseq;          /* PySequence_Fast(seq) */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4859 |     Py_ssize_t seqlen;              /* len(fseq) -- number of items in sequence */ | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4860 |     PyObject *item; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4861 |     Py_ssize_t i; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4862 |  | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4863 |     fseq = PySequence_Fast(seq, ""); | 
 | 4864 |     if (fseq == NULL) { | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4865 |     	return NULL; | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4866 |     } | 
 | 4867 |  | 
| Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4868 |     /* Grrrr.  A codec may be invoked to convert str objects to | 
 | 4869 |      * Unicode, and so it's possible to call back into Python code | 
 | 4870 |      * during PyUnicode_FromObject(), and so it's possible for a sick | 
 | 4871 |      * codec to change the size of fseq (if seq is a list).  Therefore | 
 | 4872 |      * we have to keep refetching the size -- can't assume seqlen | 
 | 4873 |      * is invariant. | 
 | 4874 |      */ | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4875 |     seqlen = PySequence_Fast_GET_SIZE(fseq); | 
 | 4876 |     /* If empty sequence, return u"". */ | 
 | 4877 |     if (seqlen == 0) { | 
 | 4878 |     	res = _PyUnicode_New(0);  /* empty sequence; return u"" */ | 
 | 4879 |     	goto Done; | 
 | 4880 |     } | 
 | 4881 |     /* If singleton sequence with an exact Unicode, return that. */ | 
 | 4882 |     if (seqlen == 1) { | 
 | 4883 | 	item = PySequence_Fast_GET_ITEM(fseq, 0); | 
 | 4884 | 	if (PyUnicode_CheckExact(item)) { | 
 | 4885 | 	    Py_INCREF(item); | 
 | 4886 | 	    res = (PyUnicodeObject *)item; | 
 | 4887 | 	    goto Done; | 
 | 4888 | 	} | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4889 |     } | 
 | 4890 |  | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4891 |     /* At least two items to join, or one that isn't exact Unicode. */ | 
 | 4892 |     if (seqlen > 1) { | 
 | 4893 |         /* Set up sep and seplen -- they're needed. */ | 
 | 4894 |     	if (separator == NULL) { | 
 | 4895 | 	    sep = ␣ | 
 | 4896 | 	    seplen = 1; | 
 | 4897 |         } | 
 | 4898 |     	else { | 
 | 4899 | 	    internal_separator = PyUnicode_FromObject(separator); | 
 | 4900 | 	    if (internal_separator == NULL) | 
 | 4901 | 	        goto onError; | 
 | 4902 | 	    sep = PyUnicode_AS_UNICODE(internal_separator); | 
 | 4903 | 	    seplen = PyUnicode_GET_SIZE(internal_separator); | 
| Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4904 | 	    /* In case PyUnicode_FromObject() mutated seq. */ | 
 | 4905 | 	    seqlen = PySequence_Fast_GET_SIZE(fseq); | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4906 |         } | 
 | 4907 |     } | 
 | 4908 |  | 
 | 4909 |     /* Get space. */ | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4910 |     res = _PyUnicode_New(res_alloc); | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4911 |     if (res == NULL) | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4912 |         goto onError; | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4913 |     res_p = PyUnicode_AS_UNICODE(res); | 
 | 4914 |     res_used = 0; | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4915 |  | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4916 |     for (i = 0; i < seqlen; ++i) { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4917 | 	Py_ssize_t itemlen; | 
 | 4918 | 	Py_ssize_t new_res_used; | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4919 |  | 
 | 4920 | 	item = PySequence_Fast_GET_ITEM(fseq, i); | 
 | 4921 | 	/* Convert item to Unicode. */ | 
 | 4922 | 	if (! PyUnicode_Check(item) && ! PyString_Check(item)) { | 
 | 4923 | 	    PyErr_Format(PyExc_TypeError, | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4924 | 			 "sequence item %zd: expected string or Unicode," | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4925 | 			 " %.80s found", | 
 | 4926 | 			 i, item->ob_type->tp_name); | 
| Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 4927 | 	    goto onError; | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4928 | 	} | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4929 | 	item = PyUnicode_FromObject(item); | 
 | 4930 | 	if (item == NULL) | 
 | 4931 | 	    goto onError; | 
 | 4932 | 	/* We own a reference to item from here on. */ | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4933 |  | 
| Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4934 | 	/* In case PyUnicode_FromObject() mutated seq. */ | 
 | 4935 | 	seqlen = PySequence_Fast_GET_SIZE(fseq); | 
 | 4936 |  | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4937 |         /* 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] | 4938 | 	itemlen = PyUnicode_GET_SIZE(item); | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4939 | 	new_res_used = res_used + itemlen; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4940 | 	if (new_res_used < 0) | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4941 | 	    goto Overflow; | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4942 | 	if (i < seqlen - 1) { | 
 | 4943 | 	    new_res_used += seplen; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4944 | 	    if (new_res_used < 0) | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4945 | 		goto Overflow; | 
 | 4946 | 	} | 
 | 4947 | 	if (new_res_used > res_alloc) { | 
 | 4948 | 	    /* double allocated size until it's big enough */ | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4949 | 	    do { | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4950 | 	        res_alloc += res_alloc; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4951 | 	        if (res_alloc <= 0) | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4952 | 	            goto Overflow; | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4953 | 	    } while (new_res_used > res_alloc); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4954 | 	    if (_PyUnicode_Resize(&res, res_alloc) < 0) { | 
| Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4955 | 		Py_DECREF(item); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4956 | 		goto onError; | 
| Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4957 | 	    } | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4958 |             res_p = PyUnicode_AS_UNICODE(res) + res_used; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4959 | 	} | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4960 |  | 
 | 4961 | 	/* Copy item, and maybe the separator. */ | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4962 | 	Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4963 | 	res_p += itemlen; | 
 | 4964 | 	if (i < seqlen - 1) { | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4965 | 	    Py_UNICODE_COPY(res_p, sep, seplen); | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4966 | 	    res_p += seplen; | 
 | 4967 | 	} | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4968 | 	Py_DECREF(item); | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4969 | 	res_used = new_res_used; | 
 | 4970 |     } | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4971 |  | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4972 |     /* Shrink res to match the used area; this probably can't fail, | 
 | 4973 |      * but it's cheap to check. | 
 | 4974 |      */ | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4975 |     if (_PyUnicode_Resize(&res, res_used) < 0) | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4976 | 	goto onError; | 
 | 4977 |  | 
 | 4978 |  Done: | 
 | 4979 |     Py_XDECREF(internal_separator); | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4980 |     Py_DECREF(fseq); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4981 |     return (PyObject *)res; | 
 | 4982 |  | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4983 |  Overflow: | 
 | 4984 |     PyErr_SetString(PyExc_OverflowError, | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4985 |                     "join() result is too long for a Python string"); | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4986 |     Py_DECREF(item); | 
 | 4987 |     /* fall through */ | 
 | 4988 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4989 |  onError: | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4990 |     Py_XDECREF(internal_separator); | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4991 |     Py_DECREF(fseq); | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4992 |     Py_XDECREF(res); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4993 |     return NULL; | 
 | 4994 | } | 
 | 4995 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4996 | static | 
 | 4997 | PyUnicodeObject *pad(PyUnicodeObject *self, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4998 | 		     Py_ssize_t left, | 
 | 4999 | 		     Py_ssize_t right, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5000 | 		     Py_UNICODE fill) | 
 | 5001 | { | 
 | 5002 |     PyUnicodeObject *u; | 
 | 5003 |  | 
 | 5004 |     if (left < 0) | 
 | 5005 |         left = 0; | 
 | 5006 |     if (right < 0) | 
 | 5007 |         right = 0; | 
 | 5008 |  | 
| Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5009 |     if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5010 |         Py_INCREF(self); | 
 | 5011 |         return self; | 
 | 5012 |     } | 
 | 5013 |  | 
 | 5014 |     u = _PyUnicode_New(left + self->length + right); | 
 | 5015 |     if (u) { | 
 | 5016 |         if (left) | 
 | 5017 |             Py_UNICODE_FILL(u->str, fill, left); | 
 | 5018 |         Py_UNICODE_COPY(u->str + left, self->str, self->length); | 
 | 5019 |         if (right) | 
 | 5020 |             Py_UNICODE_FILL(u->str + left + self->length, fill, right); | 
 | 5021 |     } | 
 | 5022 |  | 
 | 5023 |     return u; | 
 | 5024 | } | 
 | 5025 |  | 
 | 5026 | #define SPLIT_APPEND(data, left, right)					\ | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5027 | 	str = PyUnicode_FromUnicode((data) + (left), (right) - (left));	\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5028 | 	if (!str)							\ | 
 | 5029 | 	    goto onError;						\ | 
 | 5030 | 	if (PyList_Append(list, str)) {					\ | 
 | 5031 | 	    Py_DECREF(str);						\ | 
 | 5032 | 	    goto onError;						\ | 
 | 5033 | 	}								\ | 
 | 5034 |         else								\ | 
 | 5035 |             Py_DECREF(str); | 
 | 5036 |  | 
 | 5037 | static | 
 | 5038 | PyObject *split_whitespace(PyUnicodeObject *self, | 
 | 5039 | 			   PyObject *list, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5040 | 			   Py_ssize_t maxcount) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5041 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5042 |     register Py_ssize_t i; | 
 | 5043 |     register Py_ssize_t j; | 
 | 5044 |     Py_ssize_t len = self->length; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5045 |     PyObject *str; | 
 | 5046 |  | 
 | 5047 |     for (i = j = 0; i < len; ) { | 
 | 5048 | 	/* find a token */ | 
 | 5049 | 	while (i < len && Py_UNICODE_ISSPACE(self->str[i])) | 
 | 5050 | 	    i++; | 
 | 5051 | 	j = i; | 
 | 5052 | 	while (i < len && !Py_UNICODE_ISSPACE(self->str[i])) | 
 | 5053 | 	    i++; | 
 | 5054 | 	if (j < i) { | 
 | 5055 | 	    if (maxcount-- <= 0) | 
 | 5056 | 		break; | 
 | 5057 | 	    SPLIT_APPEND(self->str, j, i); | 
 | 5058 | 	    while (i < len && Py_UNICODE_ISSPACE(self->str[i])) | 
 | 5059 | 		i++; | 
 | 5060 | 	    j = i; | 
 | 5061 | 	} | 
 | 5062 |     } | 
 | 5063 |     if (j < len) { | 
 | 5064 | 	SPLIT_APPEND(self->str, j, len); | 
 | 5065 |     } | 
 | 5066 |     return list; | 
 | 5067 |  | 
 | 5068 |  onError: | 
 | 5069 |     Py_DECREF(list); | 
 | 5070 |     return NULL; | 
 | 5071 | } | 
 | 5072 |  | 
 | 5073 | PyObject *PyUnicode_Splitlines(PyObject *string, | 
| Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5074 | 			       int keepends) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5075 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5076 |     register Py_ssize_t i; | 
 | 5077 |     register Py_ssize_t j; | 
 | 5078 |     Py_ssize_t len; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5079 |     PyObject *list; | 
 | 5080 |     PyObject *str; | 
 | 5081 |     Py_UNICODE *data; | 
 | 5082 |  | 
 | 5083 |     string = PyUnicode_FromObject(string); | 
 | 5084 |     if (string == NULL) | 
 | 5085 | 	return NULL; | 
 | 5086 |     data = PyUnicode_AS_UNICODE(string); | 
 | 5087 |     len = PyUnicode_GET_SIZE(string); | 
 | 5088 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5089 |     list = PyList_New(0); | 
 | 5090 |     if (!list) | 
 | 5091 |         goto onError; | 
 | 5092 |  | 
 | 5093 |     for (i = j = 0; i < len; ) { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5094 | 	Py_ssize_t eol; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5095 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5096 | 	/* Find a line and append it */ | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5097 | 	while (i < len && !BLOOM_LINEBREAK(data[i])) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5098 | 	    i++; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5099 |  | 
 | 5100 | 	/* Skip the line break reading CRLF as one line break */ | 
| Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5101 | 	eol = i; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5102 | 	if (i < len) { | 
 | 5103 | 	    if (data[i] == '\r' && i + 1 < len && | 
 | 5104 | 		data[i+1] == '\n') | 
 | 5105 | 		i += 2; | 
 | 5106 | 	    else | 
 | 5107 | 		i++; | 
| Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5108 | 	    if (keepends) | 
 | 5109 | 		eol = i; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5110 | 	} | 
| Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5111 | 	SPLIT_APPEND(data, j, eol); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5112 | 	j = i; | 
 | 5113 |     } | 
 | 5114 |     if (j < len) { | 
 | 5115 | 	SPLIT_APPEND(data, j, len); | 
 | 5116 |     } | 
 | 5117 |  | 
 | 5118 |     Py_DECREF(string); | 
 | 5119 |     return list; | 
 | 5120 |  | 
 | 5121 |  onError: | 
| Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5122 |     Py_XDECREF(list); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5123 |     Py_DECREF(string); | 
 | 5124 |     return NULL; | 
 | 5125 | } | 
 | 5126 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5127 | static | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5128 | PyObject *split_char(PyUnicodeObject *self, | 
 | 5129 | 		     PyObject *list, | 
 | 5130 | 		     Py_UNICODE ch, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5131 | 		     Py_ssize_t maxcount) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5132 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5133 |     register Py_ssize_t i; | 
 | 5134 |     register Py_ssize_t j; | 
 | 5135 |     Py_ssize_t len = self->length; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5136 |     PyObject *str; | 
 | 5137 |  | 
 | 5138 |     for (i = j = 0; i < len; ) { | 
 | 5139 | 	if (self->str[i] == ch) { | 
 | 5140 | 	    if (maxcount-- <= 0) | 
 | 5141 | 		break; | 
 | 5142 | 	    SPLIT_APPEND(self->str, j, i); | 
 | 5143 | 	    i = j = i + 1; | 
 | 5144 | 	} else | 
 | 5145 | 	    i++; | 
 | 5146 |     } | 
 | 5147 |     if (j <= len) { | 
 | 5148 | 	SPLIT_APPEND(self->str, j, len); | 
 | 5149 |     } | 
 | 5150 |     return list; | 
 | 5151 |  | 
 | 5152 |  onError: | 
 | 5153 |     Py_DECREF(list); | 
 | 5154 |     return NULL; | 
 | 5155 | } | 
 | 5156 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5157 | static | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5158 | PyObject *split_substring(PyUnicodeObject *self, | 
 | 5159 | 			  PyObject *list, | 
 | 5160 | 			  PyUnicodeObject *substring, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5161 | 			  Py_ssize_t maxcount) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5162 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5163 |     register Py_ssize_t i; | 
 | 5164 |     register Py_ssize_t j; | 
 | 5165 |     Py_ssize_t len = self->length; | 
 | 5166 |     Py_ssize_t sublen = substring->length; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5167 |     PyObject *str; | 
 | 5168 |  | 
| Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 5169 |     for (i = j = 0; i <= len - sublen; ) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5170 | 	if (Py_UNICODE_MATCH(self, i, substring)) { | 
 | 5171 | 	    if (maxcount-- <= 0) | 
 | 5172 | 		break; | 
 | 5173 | 	    SPLIT_APPEND(self->str, j, i); | 
 | 5174 | 	    i = j = i + sublen; | 
 | 5175 | 	} else | 
 | 5176 | 	    i++; | 
 | 5177 |     } | 
 | 5178 |     if (j <= len) { | 
 | 5179 | 	SPLIT_APPEND(self->str, j, len); | 
 | 5180 |     } | 
 | 5181 |     return list; | 
 | 5182 |  | 
 | 5183 |  onError: | 
 | 5184 |     Py_DECREF(list); | 
 | 5185 |     return NULL; | 
 | 5186 | } | 
 | 5187 |  | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5188 | static | 
 | 5189 | PyObject *rsplit_whitespace(PyUnicodeObject *self, | 
 | 5190 | 			    PyObject *list, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5191 | 			    Py_ssize_t maxcount) | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5192 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5193 |     register Py_ssize_t i; | 
 | 5194 |     register Py_ssize_t j; | 
 | 5195 |     Py_ssize_t len = self->length; | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5196 |     PyObject *str; | 
 | 5197 |  | 
 | 5198 |     for (i = j = len - 1; i >= 0; ) { | 
 | 5199 | 	/* find a token */ | 
 | 5200 | 	while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) | 
 | 5201 | 	    i--; | 
 | 5202 | 	j = i; | 
 | 5203 | 	while (i >= 0 && !Py_UNICODE_ISSPACE(self->str[i])) | 
 | 5204 | 	    i--; | 
 | 5205 | 	if (j > i) { | 
 | 5206 | 	    if (maxcount-- <= 0) | 
 | 5207 | 		break; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5208 | 	    SPLIT_APPEND(self->str, i + 1, j + 1); | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5209 | 	    while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) | 
 | 5210 | 		i--; | 
 | 5211 | 	    j = i; | 
 | 5212 | 	} | 
 | 5213 |     } | 
 | 5214 |     if (j >= 0) { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5215 | 	SPLIT_APPEND(self->str, 0, j + 1); | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5216 |     } | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5217 |     if (PyList_Reverse(list) < 0) | 
 | 5218 |         goto onError; | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5219 |     return list; | 
 | 5220 |  | 
 | 5221 |  onError: | 
 | 5222 |     Py_DECREF(list); | 
 | 5223 |     return NULL; | 
 | 5224 | } | 
 | 5225 |  | 
 | 5226 | static  | 
 | 5227 | PyObject *rsplit_char(PyUnicodeObject *self, | 
 | 5228 | 		      PyObject *list, | 
 | 5229 | 		      Py_UNICODE ch, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5230 | 		      Py_ssize_t maxcount) | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5231 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5232 |     register Py_ssize_t i; | 
 | 5233 |     register Py_ssize_t j; | 
 | 5234 |     Py_ssize_t len = self->length; | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5235 |     PyObject *str; | 
 | 5236 |  | 
 | 5237 |     for (i = j = len - 1; i >= 0; ) { | 
 | 5238 | 	if (self->str[i] == ch) { | 
 | 5239 | 	    if (maxcount-- <= 0) | 
 | 5240 | 		break; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5241 | 	    SPLIT_APPEND(self->str, i + 1, j + 1); | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5242 | 	    j = i = i - 1; | 
 | 5243 | 	} else | 
 | 5244 | 	    i--; | 
 | 5245 |     } | 
| Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 5246 |     if (j >= -1) { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5247 | 	SPLIT_APPEND(self->str, 0, j + 1); | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5248 |     } | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5249 |     if (PyList_Reverse(list) < 0) | 
 | 5250 |         goto onError; | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5251 |     return list; | 
 | 5252 |  | 
 | 5253 |  onError: | 
 | 5254 |     Py_DECREF(list); | 
 | 5255 |     return NULL; | 
 | 5256 | } | 
 | 5257 |  | 
 | 5258 | static  | 
 | 5259 | PyObject *rsplit_substring(PyUnicodeObject *self, | 
 | 5260 | 			   PyObject *list, | 
 | 5261 | 			   PyUnicodeObject *substring, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5262 | 			   Py_ssize_t maxcount) | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5263 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5264 |     register Py_ssize_t i; | 
 | 5265 |     register Py_ssize_t j; | 
 | 5266 |     Py_ssize_t len = self->length; | 
 | 5267 |     Py_ssize_t sublen = substring->length; | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5268 |     PyObject *str; | 
 | 5269 |  | 
 | 5270 |     for (i = len - sublen, j = len; i >= 0; ) { | 
 | 5271 | 	if (Py_UNICODE_MATCH(self, i, substring)) { | 
 | 5272 | 	    if (maxcount-- <= 0) | 
 | 5273 | 		break; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5274 | 	    SPLIT_APPEND(self->str, i + sublen, j); | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5275 | 	    j = i; | 
 | 5276 | 	    i -= sublen; | 
 | 5277 | 	} else | 
 | 5278 | 	    i--; | 
 | 5279 |     } | 
 | 5280 |     if (j >= 0) { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5281 | 	SPLIT_APPEND(self->str, 0, j); | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5282 |     } | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5283 |     if (PyList_Reverse(list) < 0) | 
 | 5284 |         goto onError; | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5285 |     return list; | 
 | 5286 |  | 
 | 5287 |  onError: | 
 | 5288 |     Py_DECREF(list); | 
 | 5289 |     return NULL; | 
 | 5290 | } | 
 | 5291 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5292 | #undef SPLIT_APPEND | 
 | 5293 |  | 
 | 5294 | static | 
 | 5295 | PyObject *split(PyUnicodeObject *self, | 
 | 5296 | 		PyUnicodeObject *substring, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5297 | 		Py_ssize_t maxcount) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5298 | { | 
 | 5299 |     PyObject *list; | 
 | 5300 |  | 
 | 5301 |     if (maxcount < 0) | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5302 |         maxcount = PY_SSIZE_T_MAX; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5303 |  | 
 | 5304 |     list = PyList_New(0); | 
 | 5305 |     if (!list) | 
 | 5306 |         return NULL; | 
 | 5307 |  | 
 | 5308 |     if (substring == NULL) | 
 | 5309 | 	return split_whitespace(self,list,maxcount); | 
 | 5310 |  | 
 | 5311 |     else if (substring->length == 1) | 
 | 5312 | 	return split_char(self,list,substring->str[0],maxcount); | 
 | 5313 |  | 
 | 5314 |     else if (substring->length == 0) { | 
 | 5315 | 	Py_DECREF(list); | 
 | 5316 | 	PyErr_SetString(PyExc_ValueError, "empty separator"); | 
 | 5317 | 	return NULL; | 
 | 5318 |     } | 
 | 5319 |     else | 
 | 5320 | 	return split_substring(self,list,substring,maxcount); | 
 | 5321 | } | 
 | 5322 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5323 | static | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5324 | PyObject *rsplit(PyUnicodeObject *self, | 
 | 5325 | 		 PyUnicodeObject *substring, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5326 | 		 Py_ssize_t maxcount) | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5327 | { | 
 | 5328 |     PyObject *list; | 
 | 5329 |  | 
 | 5330 |     if (maxcount < 0) | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5331 |         maxcount = PY_SSIZE_T_MAX; | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5332 |  | 
 | 5333 |     list = PyList_New(0); | 
 | 5334 |     if (!list) | 
 | 5335 |         return NULL; | 
 | 5336 |  | 
 | 5337 |     if (substring == NULL) | 
 | 5338 | 	return rsplit_whitespace(self,list,maxcount); | 
 | 5339 |  | 
 | 5340 |     else if (substring->length == 1) | 
 | 5341 | 	return rsplit_char(self,list,substring->str[0],maxcount); | 
 | 5342 |  | 
 | 5343 |     else if (substring->length == 0) { | 
 | 5344 | 	Py_DECREF(list); | 
 | 5345 | 	PyErr_SetString(PyExc_ValueError, "empty separator"); | 
 | 5346 | 	return NULL; | 
 | 5347 |     } | 
 | 5348 |     else | 
 | 5349 | 	return rsplit_substring(self,list,substring,maxcount); | 
 | 5350 | } | 
 | 5351 |  | 
 | 5352 | static | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5353 | PyObject *replace(PyUnicodeObject *self, | 
 | 5354 | 		  PyUnicodeObject *str1, | 
 | 5355 | 		  PyUnicodeObject *str2, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5356 | 		  Py_ssize_t maxcount) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5357 | { | 
 | 5358 |     PyUnicodeObject *u; | 
 | 5359 |  | 
 | 5360 |     if (maxcount < 0) | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5361 | 	maxcount = PY_SSIZE_T_MAX; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5362 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5363 |     if (str1->length == str2->length) { | 
 | 5364 |         /* same length */ | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5365 |         Py_ssize_t i; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5366 |         if (str1->length == 1) { | 
 | 5367 |             /* replace characters */ | 
 | 5368 |             Py_UNICODE u1, u2; | 
 | 5369 |             if (!findchar(self->str, self->length, str1->str[0])) | 
 | 5370 |                 goto nothing; | 
 | 5371 |             u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); | 
 | 5372 |             if (!u) | 
 | 5373 |                 return NULL; | 
 | 5374 |             Py_UNICODE_COPY(u->str, self->str, self->length); | 
 | 5375 |             u1 = str1->str[0]; | 
 | 5376 |             u2 = str2->str[0]; | 
 | 5377 |             for (i = 0; i < u->length; i++) | 
 | 5378 |                 if (u->str[i] == u1) { | 
 | 5379 |                     if (--maxcount < 0) | 
 | 5380 |                         break; | 
 | 5381 |                     u->str[i] = u2; | 
 | 5382 |                 } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5383 |         } else { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5384 |             i = fastsearch( | 
 | 5385 |                 self->str, self->length, str1->str, str1->length, FAST_SEARCH | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5386 |                 ); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5387 |             if (i < 0) | 
 | 5388 |                 goto nothing; | 
 | 5389 |             u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); | 
 | 5390 |             if (!u) | 
 | 5391 |                 return NULL; | 
 | 5392 |             Py_UNICODE_COPY(u->str, self->str, self->length); | 
 | 5393 |             while (i <= self->length - str1->length) | 
 | 5394 |                 if (Py_UNICODE_MATCH(self, i, str1)) { | 
 | 5395 |                     if (--maxcount < 0) | 
 | 5396 |                         break; | 
 | 5397 |                     Py_UNICODE_COPY(u->str+i, str2->str, str2->length); | 
 | 5398 |                     i += str1->length; | 
 | 5399 |                 } else | 
 | 5400 |                     i++; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5401 |         } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5402 |     } else { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5403 |  | 
 | 5404 |         Py_ssize_t n, i, j, e; | 
 | 5405 |         Py_ssize_t product, new_size, delta; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5406 |         Py_UNICODE *p; | 
 | 5407 |  | 
 | 5408 |         /* replace strings */ | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5409 |         n = stringlib_count(self->str, self->length, str1->str, str1->length); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5410 |         if (n > maxcount) | 
 | 5411 |             n = maxcount; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5412 |         if (n == 0) | 
 | 5413 |             goto nothing; | 
 | 5414 |         /* new_size = self->length + n * (str2->length - str1->length)); */ | 
 | 5415 |         delta = (str2->length - str1->length); | 
 | 5416 |         if (delta == 0) { | 
 | 5417 |             new_size = self->length; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5418 |         } else { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5419 |             product = n * (str2->length - str1->length); | 
 | 5420 |             if ((product / (str2->length - str1->length)) != n) { | 
 | 5421 |                 PyErr_SetString(PyExc_OverflowError, | 
 | 5422 |                                 "replace string is too long"); | 
 | 5423 |                 return NULL; | 
 | 5424 |             } | 
 | 5425 |             new_size = self->length + product; | 
 | 5426 |             if (new_size < 0) { | 
 | 5427 |                 PyErr_SetString(PyExc_OverflowError, | 
 | 5428 |                                 "replace string is too long"); | 
 | 5429 |                 return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5430 |             } | 
 | 5431 |         } | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5432 |         u = _PyUnicode_New(new_size); | 
 | 5433 |         if (!u) | 
 | 5434 |             return NULL; | 
 | 5435 |         i = 0; | 
 | 5436 |         p = u->str; | 
 | 5437 |         e = self->length - str1->length; | 
 | 5438 |         if (str1->length > 0) { | 
 | 5439 |             while (n-- > 0) { | 
 | 5440 |                 /* look for next match */ | 
 | 5441 |                 j = i; | 
 | 5442 |                 while (j <= e) { | 
 | 5443 |                     if (Py_UNICODE_MATCH(self, j, str1)) | 
 | 5444 |                         break; | 
 | 5445 |                     j++; | 
 | 5446 |                 } | 
 | 5447 | 		if (j > i) { | 
 | 5448 |                     if (j > e) | 
 | 5449 |                         break; | 
 | 5450 |                     /* copy unchanged part [i:j] */ | 
 | 5451 |                     Py_UNICODE_COPY(p, self->str+i, j-i); | 
 | 5452 |                     p += j - i; | 
 | 5453 |                 } | 
 | 5454 |                 /* copy substitution string */ | 
 | 5455 |                 if (str2->length > 0) { | 
 | 5456 |                     Py_UNICODE_COPY(p, str2->str, str2->length); | 
 | 5457 |                     p += str2->length; | 
 | 5458 |                 } | 
 | 5459 |                 i = j + str1->length; | 
 | 5460 |             } | 
 | 5461 |             if (i < self->length) | 
 | 5462 |                 /* copy tail [i:] */ | 
 | 5463 |                 Py_UNICODE_COPY(p, self->str+i, self->length-i); | 
 | 5464 |         } else { | 
 | 5465 |             /* interleave */ | 
 | 5466 |             while (n > 0) { | 
 | 5467 |                 Py_UNICODE_COPY(p, str2->str, str2->length); | 
 | 5468 |                 p += str2->length; | 
 | 5469 |                 if (--n <= 0) | 
 | 5470 |                     break; | 
 | 5471 |                 *p++ = self->str[i++]; | 
 | 5472 |             } | 
 | 5473 |             Py_UNICODE_COPY(p, self->str+i, self->length-i); | 
 | 5474 |         } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5475 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5476 |     return (PyObject *) u; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5477 |  | 
 | 5478 | nothing: | 
 | 5479 |     /* nothing to replace; return original string (when possible) */ | 
 | 5480 |     if (PyUnicode_CheckExact(self)) { | 
 | 5481 |         Py_INCREF(self); | 
 | 5482 |         return (PyObject *) self; | 
 | 5483 |     } | 
 | 5484 |     return PyUnicode_FromUnicode(self->str, self->length); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5485 | } | 
 | 5486 |  | 
 | 5487 | /* --- Unicode Object Methods --------------------------------------------- */ | 
 | 5488 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5489 | PyDoc_STRVAR(title__doc__, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5490 | "S.title() -> unicode\n\ | 
 | 5491 | \n\ | 
 | 5492 | 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] | 5493 | characters, all remaining cased characters have lower case."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5494 |  | 
 | 5495 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5496 | unicode_title(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5497 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5498 |     return fixup(self, fixtitle); | 
 | 5499 | } | 
 | 5500 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5501 | PyDoc_STRVAR(capitalize__doc__, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5502 | "S.capitalize() -> unicode\n\ | 
 | 5503 | \n\ | 
 | 5504 | 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] | 5505 | have upper case."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5506 |  | 
 | 5507 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5508 | unicode_capitalize(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5509 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5510 |     return fixup(self, fixcapitalize); | 
 | 5511 | } | 
 | 5512 |  | 
 | 5513 | #if 0 | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5514 | PyDoc_STRVAR(capwords__doc__, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5515 | "S.capwords() -> unicode\n\ | 
 | 5516 | \n\ | 
 | 5517 | 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] | 5518 | normalized whitespace (all whitespace strings are replaced by ' ')."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5519 |  | 
 | 5520 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5521 | unicode_capwords(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5522 | { | 
 | 5523 |     PyObject *list; | 
 | 5524 |     PyObject *item; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5525 |     Py_ssize_t i; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5526 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5527 |     /* Split into words */ | 
 | 5528 |     list = split(self, NULL, -1); | 
 | 5529 |     if (!list) | 
 | 5530 |         return NULL; | 
 | 5531 |  | 
 | 5532 |     /* Capitalize each word */ | 
 | 5533 |     for (i = 0; i < PyList_GET_SIZE(list); i++) { | 
 | 5534 |         item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), | 
 | 5535 | 		     fixcapitalize); | 
 | 5536 |         if (item == NULL) | 
 | 5537 |             goto onError; | 
 | 5538 |         Py_DECREF(PyList_GET_ITEM(list, i)); | 
 | 5539 |         PyList_SET_ITEM(list, i, item); | 
 | 5540 |     } | 
 | 5541 |  | 
 | 5542 |     /* Join the words to form a new string */ | 
 | 5543 |     item = PyUnicode_Join(NULL, list); | 
 | 5544 |  | 
 | 5545 | onError: | 
 | 5546 |     Py_DECREF(list); | 
 | 5547 |     return (PyObject *)item; | 
 | 5548 | } | 
 | 5549 | #endif | 
 | 5550 |  | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5551 | /* Argument converter.  Coerces to a single unicode character */ | 
 | 5552 |  | 
 | 5553 | static int | 
 | 5554 | convert_uc(PyObject *obj, void *addr) | 
 | 5555 | { | 
 | 5556 | 	Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; | 
 | 5557 | 	PyObject *uniobj; | 
 | 5558 | 	Py_UNICODE *unistr; | 
 | 5559 |  | 
 | 5560 | 	uniobj = PyUnicode_FromObject(obj); | 
 | 5561 | 	if (uniobj == NULL) { | 
 | 5562 | 		PyErr_SetString(PyExc_TypeError, | 
 | 5563 | 			"The fill character cannot be converted to Unicode"); | 
 | 5564 | 		return 0; | 
 | 5565 | 	} | 
 | 5566 | 	if (PyUnicode_GET_SIZE(uniobj) != 1) { | 
 | 5567 | 		PyErr_SetString(PyExc_TypeError, | 
 | 5568 | 			"The fill character must be exactly one character long"); | 
 | 5569 | 		Py_DECREF(uniobj); | 
 | 5570 | 		return 0; | 
 | 5571 | 	} | 
 | 5572 | 	unistr = PyUnicode_AS_UNICODE(uniobj); | 
 | 5573 | 	*fillcharloc = unistr[0]; | 
 | 5574 | 	Py_DECREF(uniobj); | 
 | 5575 | 	return 1; | 
 | 5576 | } | 
 | 5577 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5578 | PyDoc_STRVAR(center__doc__, | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5579 | "S.center(width[, fillchar]) -> unicode\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5580 | \n\ | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5581 | Return S centered in a Unicode string of length width. Padding is\n\ | 
 | 5582 | done using the specified fill character (default is a space)"); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5583 |  | 
 | 5584 | static PyObject * | 
 | 5585 | unicode_center(PyUnicodeObject *self, PyObject *args) | 
 | 5586 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5587 |     Py_ssize_t marg, left; | 
 | 5588 |     Py_ssize_t width; | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5589 |     Py_UNICODE fillchar = ' '; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5590 |  | 
| Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 5591 |     if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5592 |         return NULL; | 
 | 5593 |  | 
| Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5594 |     if (self->length >= width && PyUnicode_CheckExact(self)) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5595 |         Py_INCREF(self); | 
 | 5596 |         return (PyObject*) self; | 
 | 5597 |     } | 
 | 5598 |  | 
 | 5599 |     marg = width - self->length; | 
 | 5600 |     left = marg / 2 + (marg & width & 1); | 
 | 5601 |  | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5602 |     return (PyObject*) pad(self, left, marg - left, fillchar); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5603 | } | 
 | 5604 |  | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5605 | #if 0 | 
 | 5606 |  | 
 | 5607 | /* This code should go into some future Unicode collation support | 
 | 5608 |    module. The basic comparison should compare ordinals on a naive | 
| Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 5609 |    basis (this is what Java does and thus JPython too). */ | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5610 |  | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5611 | /* speedy UTF-16 code point order comparison */ | 
 | 5612 | /* gleaned from: */ | 
 | 5613 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ | 
 | 5614 |  | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 5615 | static short utf16Fixup[32] = | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5616 | { | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5617 |     0, 0, 0, 0, 0, 0, 0, 0, | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5618 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 5619 |     0, 0, 0, 0, 0, 0, 0, 0, | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 5620 |     0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5621 | }; | 
 | 5622 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5623 | static int | 
 | 5624 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) | 
 | 5625 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5626 |     Py_ssize_t len1, len2; | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5627 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5628 |     Py_UNICODE *s1 = str1->str; | 
 | 5629 |     Py_UNICODE *s2 = str2->str; | 
 | 5630 |  | 
 | 5631 |     len1 = str1->length; | 
 | 5632 |     len2 = str2->length; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5633 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5634 |     while (len1 > 0 && len2 > 0) { | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5635 |         Py_UNICODE c1, c2; | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5636 |  | 
 | 5637 |         c1 = *s1++; | 
 | 5638 |         c2 = *s2++; | 
| Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 5639 |  | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5640 | 	if (c1 > (1<<11) * 26) | 
 | 5641 | 	    c1 += utf16Fixup[c1>>11]; | 
 | 5642 | 	if (c2 > (1<<11) * 26) | 
 | 5643 |             c2 += utf16Fixup[c2>>11]; | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5644 |         /* now c1 and c2 are in UTF-32-compatible order */ | 
| Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 5645 |  | 
 | 5646 |         if (c1 != c2) | 
 | 5647 |             return (c1 < c2) ? -1 : 1; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5648 |  | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5649 |         len1--; len2--; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5650 |     } | 
 | 5651 |  | 
 | 5652 |     return (len1 < len2) ? -1 : (len1 != len2); | 
 | 5653 | } | 
 | 5654 |  | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5655 | #else | 
 | 5656 |  | 
 | 5657 | static int | 
 | 5658 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) | 
 | 5659 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5660 |     register Py_ssize_t len1, len2; | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5661 |  | 
 | 5662 |     Py_UNICODE *s1 = str1->str; | 
 | 5663 |     Py_UNICODE *s2 = str2->str; | 
 | 5664 |  | 
 | 5665 |     len1 = str1->length; | 
 | 5666 |     len2 = str2->length; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5667 |  | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5668 |     while (len1 > 0 && len2 > 0) { | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5669 |         Py_UNICODE c1, c2; | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5670 |  | 
| Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 5671 |         c1 = *s1++; | 
 | 5672 |         c2 = *s2++; | 
 | 5673 |  | 
 | 5674 |         if (c1 != c2) | 
 | 5675 |             return (c1 < c2) ? -1 : 1; | 
 | 5676 |  | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5677 |         len1--; len2--; | 
 | 5678 |     } | 
 | 5679 |  | 
 | 5680 |     return (len1 < len2) ? -1 : (len1 != len2); | 
 | 5681 | } | 
 | 5682 |  | 
 | 5683 | #endif | 
 | 5684 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5685 | int PyUnicode_Compare(PyObject *left, | 
 | 5686 | 		      PyObject *right) | 
 | 5687 | { | 
| Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 5688 |     if (PyUnicode_Check(left) && PyUnicode_Check(right)) | 
 | 5689 |         return unicode_compare((PyUnicodeObject *)left, | 
 | 5690 |                                (PyUnicodeObject *)right); | 
 | 5691 |     if ((PyString_Check(left) && PyUnicode_Check(right)) || | 
 | 5692 |         (PyUnicode_Check(left) && PyString_Check(right))) { | 
 | 5693 |         if (PyUnicode_Check(left)) | 
 | 5694 |             left = _PyUnicode_AsDefaultEncodedString(left, NULL); | 
 | 5695 |         if (PyUnicode_Check(right)) | 
 | 5696 |             right = _PyUnicode_AsDefaultEncodedString(right, NULL); | 
 | 5697 |         assert(PyString_Check(left)); | 
 | 5698 |         assert(PyString_Check(right)); | 
 | 5699 |         return PyObject_Compare(left, right); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5700 |     } | 
| Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 5701 |     PyErr_Format(PyExc_TypeError, | 
 | 5702 |                  "Can't compare %.100s and %.100s", | 
 | 5703 |                  left->ob_type->tp_name, | 
 | 5704 |                  right->ob_type->tp_name); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5705 |     return -1; | 
 | 5706 | } | 
 | 5707 |  | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 5708 | PyObject *PyUnicode_RichCompare(PyObject *left, | 
 | 5709 |                                 PyObject *right, | 
 | 5710 |                                 int op) | 
 | 5711 | { | 
 | 5712 |     int result; | 
 | 5713 |  | 
 | 5714 |     result = PyUnicode_Compare(left, right); | 
 | 5715 |     if (result == -1 && PyErr_Occurred()) | 
 | 5716 |         goto onError; | 
 | 5717 |  | 
 | 5718 |     /* Convert the return value to a Boolean */ | 
 | 5719 |     switch (op) { | 
 | 5720 |     case Py_EQ: | 
 | 5721 |         result = (result == 0); | 
 | 5722 |         break; | 
 | 5723 |     case Py_NE: | 
 | 5724 |         result = (result != 0); | 
 | 5725 |         break; | 
 | 5726 |     case Py_LE: | 
 | 5727 |         result = (result <= 0); | 
 | 5728 |         break; | 
 | 5729 |     case Py_GE: | 
 | 5730 |         result = (result >= 0); | 
 | 5731 |         break; | 
 | 5732 |     case Py_LT: | 
 | 5733 |         result = (result == -1); | 
 | 5734 |         break; | 
 | 5735 |     case Py_GT: | 
 | 5736 |         result = (result == 1); | 
 | 5737 |         break; | 
 | 5738 |     } | 
 | 5739 |     return PyBool_FromLong(result); | 
 | 5740 |  | 
 | 5741 |  onError: | 
 | 5742 |  | 
 | 5743 |     /* Standard case | 
 | 5744 |  | 
 | 5745 |        Type errors mean that PyUnicode_FromObject() could not convert | 
 | 5746 |        one of the arguments (usually the right hand side) to Unicode, | 
 | 5747 |        ie. we can't handle the comparison request. However, it is | 
 | 5748 |        possible that the other object knows a comparison method, which | 
 | 5749 |        is why we return Py_NotImplemented to give the other object a | 
 | 5750 |        chance. | 
 | 5751 |  | 
 | 5752 |     */ | 
 | 5753 |     if (PyErr_ExceptionMatches(PyExc_TypeError)) { | 
 | 5754 |         PyErr_Clear(); | 
 | 5755 |         Py_INCREF(Py_NotImplemented); | 
 | 5756 |         return Py_NotImplemented; | 
 | 5757 |     } | 
 | 5758 |     if (op != Py_EQ && op != Py_NE) | 
 | 5759 |         return NULL; | 
 | 5760 |  | 
 | 5761 |     /* Equality comparison. | 
 | 5762 |  | 
 | 5763 |        This is a special case: we silence any PyExc_UnicodeDecodeError | 
 | 5764 |        and instead turn it into a PyErr_UnicodeWarning. | 
 | 5765 |  | 
 | 5766 |     */ | 
 | 5767 |     if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) | 
 | 5768 |         return NULL; | 
 | 5769 |     PyErr_Clear(); | 
 | 5770 |     if (PyErr_Warn(PyExc_UnicodeWarning,  | 
 | 5771 |                    (op == Py_EQ) ?  | 
 | 5772 |                    "Unicode equal comparison " | 
 | 5773 |                    "failed to convert both arguments to Unicode - " | 
 | 5774 |                    "interpreting them as being unequal" : | 
 | 5775 |                    "Unicode unequal comparison " | 
 | 5776 |                    "failed to convert both arguments to Unicode - " | 
 | 5777 |                    "interpreting them as being unequal" | 
 | 5778 |                    ) < 0) | 
 | 5779 |         return NULL; | 
 | 5780 |     result = (op == Py_NE); | 
 | 5781 |     return PyBool_FromLong(result); | 
 | 5782 | } | 
 | 5783 |  | 
| Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5784 | int PyUnicode_Contains(PyObject *container, | 
 | 5785 | 		       PyObject *element) | 
 | 5786 | { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5787 |     PyObject *str, *sub; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5788 |     int result; | 
| Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5789 |  | 
 | 5790 |     /* Coerce the two arguments */ | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5791 |     sub = PyUnicode_FromObject(element); | 
 | 5792 |     if (!sub) { | 
| Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 5793 | 	PyErr_SetString(PyExc_TypeError, | 
| Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 5794 | 	    "'in <string>' requires string as left operand"); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5795 |         return -1; | 
| Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5796 |     } | 
 | 5797 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5798 |     str = PyUnicode_FromObject(container); | 
 | 5799 |     if (!str) { | 
 | 5800 |         Py_DECREF(sub); | 
 | 5801 |         return -1; | 
 | 5802 |     } | 
 | 5803 |  | 
 | 5804 |     result = stringlib_contains_obj(str, sub); | 
 | 5805 |  | 
 | 5806 |     Py_DECREF(str); | 
 | 5807 |     Py_DECREF(sub); | 
 | 5808 |  | 
| Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5809 |     return result; | 
| Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5810 | } | 
 | 5811 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5812 | /* Concat to string or Unicode object giving a new Unicode object. */ | 
 | 5813 |  | 
 | 5814 | PyObject *PyUnicode_Concat(PyObject *left, | 
 | 5815 | 			   PyObject *right) | 
 | 5816 | { | 
 | 5817 |     PyUnicodeObject *u = NULL, *v = NULL, *w; | 
 | 5818 |  | 
| Guido van Rossum | 84d79dd | 2007-04-13 02:23:57 +0000 | [diff] [blame] | 5819 |     if (PyBytes_Check(left) || PyBytes_Check(right)) | 
 | 5820 |         return PyBytes_Concat(left, right); | 
 | 5821 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5822 |     /* Coerce the two arguments */ | 
 | 5823 |     u = (PyUnicodeObject *)PyUnicode_FromObject(left); | 
 | 5824 |     if (u == NULL) | 
 | 5825 | 	goto onError; | 
 | 5826 |     v = (PyUnicodeObject *)PyUnicode_FromObject(right); | 
 | 5827 |     if (v == NULL) | 
 | 5828 | 	goto onError; | 
 | 5829 |  | 
 | 5830 |     /* Shortcuts */ | 
 | 5831 |     if (v == unicode_empty) { | 
 | 5832 | 	Py_DECREF(v); | 
 | 5833 | 	return (PyObject *)u; | 
 | 5834 |     } | 
 | 5835 |     if (u == unicode_empty) { | 
 | 5836 | 	Py_DECREF(u); | 
 | 5837 | 	return (PyObject *)v; | 
 | 5838 |     } | 
 | 5839 |  | 
 | 5840 |     /* Concat the two Unicode strings */ | 
 | 5841 |     w = _PyUnicode_New(u->length + v->length); | 
 | 5842 |     if (w == NULL) | 
 | 5843 | 	goto onError; | 
 | 5844 |     Py_UNICODE_COPY(w->str, u->str, u->length); | 
 | 5845 |     Py_UNICODE_COPY(w->str + u->length, v->str, v->length); | 
 | 5846 |  | 
 | 5847 |     Py_DECREF(u); | 
 | 5848 |     Py_DECREF(v); | 
 | 5849 |     return (PyObject *)w; | 
 | 5850 |  | 
 | 5851 | onError: | 
 | 5852 |     Py_XDECREF(u); | 
 | 5853 |     Py_XDECREF(v); | 
 | 5854 |     return NULL; | 
 | 5855 | } | 
 | 5856 |  | 
| Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame^] | 5857 | void | 
 | 5858 | PyUnicode_Append(PyObject **pleft, PyObject *right) | 
 | 5859 | { | 
 | 5860 | 	PyObject *new; | 
 | 5861 | 	if (*pleft == NULL) | 
 | 5862 | 		return; | 
 | 5863 | 	if (right == NULL || !PyUnicode_Check(*pleft)) { | 
 | 5864 | 		Py_DECREF(*pleft); | 
 | 5865 | 		*pleft = NULL; | 
 | 5866 | 		return; | 
 | 5867 | 	} | 
 | 5868 | 	new = PyUnicode_Concat(*pleft, right); | 
 | 5869 | 	Py_DECREF(*pleft); | 
 | 5870 | 	*pleft = new; | 
 | 5871 | } | 
 | 5872 |  | 
 | 5873 | void | 
 | 5874 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) | 
 | 5875 | { | 
 | 5876 | 	PyUnicode_Append(pleft, right); | 
 | 5877 | 	Py_XDECREF(right); | 
 | 5878 | } | 
 | 5879 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5880 | PyDoc_STRVAR(count__doc__, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5881 | "S.count(sub[, start[, end]]) -> int\n\ | 
 | 5882 | \n\ | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5883 | Return the number of non-overlapping occurrences of substring sub in\n\ | 
 | 5884 | 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] | 5885 | interpreted as in slice notation."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5886 |  | 
 | 5887 | static PyObject * | 
 | 5888 | unicode_count(PyUnicodeObject *self, PyObject *args) | 
 | 5889 | { | 
 | 5890 |     PyUnicodeObject *substring; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5891 |     Py_ssize_t start = 0; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5892 |     Py_ssize_t end = PY_SSIZE_T_MAX; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5893 |     PyObject *result; | 
 | 5894 |  | 
| Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5895 |     if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, | 
 | 5896 | 		_PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5897 |         return NULL; | 
 | 5898 |  | 
 | 5899 |     substring = (PyUnicodeObject *)PyUnicode_FromObject( | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5900 |         (PyObject *)substring); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5901 |     if (substring == NULL) | 
 | 5902 | 	return NULL; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5903 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5904 |     FIX_START_END(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5905 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5906 |     result = PyInt_FromSsize_t( | 
 | 5907 |         stringlib_count(self->str + start, end - start, | 
 | 5908 |                         substring->str, substring->length) | 
 | 5909 |         ); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5910 |  | 
 | 5911 |     Py_DECREF(substring); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5912 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5913 |     return result; | 
 | 5914 | } | 
 | 5915 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5916 | PyDoc_STRVAR(encode__doc__, | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5917 | "S.encode([encoding[,errors]]) -> string or unicode\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5918 | \n\ | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5919 | Encodes S using the codec registered for encoding. encoding defaults\n\ | 
 | 5920 | 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] | 5921 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5922 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ | 
 | 5923 | 'xmlcharrefreplace' as well as any other name registered with\n\ | 
 | 5924 | codecs.register_error that can handle UnicodeEncodeErrors."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5925 |  | 
 | 5926 | static PyObject * | 
 | 5927 | unicode_encode(PyUnicodeObject *self, PyObject *args) | 
 | 5928 | { | 
 | 5929 |     char *encoding = NULL; | 
 | 5930 |     char *errors = NULL; | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5931 |     PyObject *v; | 
 | 5932 |      | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5933 |     if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) | 
 | 5934 |         return NULL; | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5935 |     v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); | 
| Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5936 |     if (v == NULL) | 
 | 5937 |         goto onError; | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 5938 |     if (!PyBytes_Check(v)) { | 
| Guido van Rossum | 4355a47 | 2007-05-04 05:00:04 +0000 | [diff] [blame] | 5939 |         if (PyString_Check(v)) { | 
 | 5940 |             /* Old codec, turn it into bytes */ | 
 | 5941 |             PyObject *b = PyBytes_FromObject(v); | 
 | 5942 |             Py_DECREF(v); | 
 | 5943 |             return b; | 
 | 5944 |         } | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5945 |         PyErr_Format(PyExc_TypeError, | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 5946 |                      "encoder did not return a bytes object " | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5947 |                      "(type=%.400s)", | 
 | 5948 |                      v->ob_type->tp_name); | 
 | 5949 |         Py_DECREF(v); | 
 | 5950 |         return NULL; | 
 | 5951 |     } | 
 | 5952 |     return v; | 
| Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5953 |  | 
 | 5954 |  onError: | 
 | 5955 |     return NULL; | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5956 | } | 
 | 5957 |  | 
 | 5958 | PyDoc_STRVAR(decode__doc__, | 
 | 5959 | "S.decode([encoding[,errors]]) -> string or unicode\n\ | 
 | 5960 | \n\ | 
 | 5961 | Decodes S using the codec registered for encoding. encoding defaults\n\ | 
 | 5962 | to the default encoding. errors may be given to set a different error\n\ | 
 | 5963 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ | 
 | 5964 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ | 
 | 5965 | as well as any other name registerd with codecs.register_error that is\n\ | 
 | 5966 | able to handle UnicodeDecodeErrors."); | 
 | 5967 |  | 
 | 5968 | static PyObject * | 
| Marc-André Lemburg | 126b44c | 2004-07-10 12:04:20 +0000 | [diff] [blame] | 5969 | unicode_decode(PyUnicodeObject *self, PyObject *args) | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5970 | { | 
 | 5971 |     char *encoding = NULL; | 
 | 5972 |     char *errors = NULL; | 
 | 5973 |     PyObject *v; | 
 | 5974 |      | 
 | 5975 |     if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) | 
 | 5976 |         return NULL; | 
 | 5977 |     v = PyUnicode_AsDecodedObject((PyObject *)self, encoding, errors); | 
| Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5978 |     if (v == NULL) | 
 | 5979 |         goto onError; | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5980 |     if (!PyString_Check(v) && !PyUnicode_Check(v)) { | 
 | 5981 |         PyErr_Format(PyExc_TypeError, | 
 | 5982 |                      "decoder did not return a string/unicode object " | 
 | 5983 |                      "(type=%.400s)", | 
 | 5984 |                      v->ob_type->tp_name); | 
 | 5985 |         Py_DECREF(v); | 
 | 5986 |         return NULL; | 
 | 5987 |     } | 
 | 5988 |     return v; | 
| Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5989 |  | 
 | 5990 |  onError: | 
 | 5991 |     return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5992 | } | 
 | 5993 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5994 | PyDoc_STRVAR(expandtabs__doc__, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5995 | "S.expandtabs([tabsize]) -> unicode\n\ | 
 | 5996 | \n\ | 
 | 5997 | 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] | 5998 | 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] | 5999 |  | 
 | 6000 | static PyObject* | 
 | 6001 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) | 
 | 6002 | { | 
 | 6003 |     Py_UNICODE *e; | 
 | 6004 |     Py_UNICODE *p; | 
 | 6005 |     Py_UNICODE *q; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6006 |     Py_ssize_t i, j; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6007 |     PyUnicodeObject *u; | 
 | 6008 |     int tabsize = 8; | 
 | 6009 |  | 
 | 6010 |     if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) | 
 | 6011 | 	return NULL; | 
 | 6012 |  | 
| Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6013 |     /* First pass: determine size of output string */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6014 |     i = j = 0; | 
 | 6015 |     e = self->str + self->length; | 
 | 6016 |     for (p = self->str; p < e; p++) | 
 | 6017 |         if (*p == '\t') { | 
 | 6018 | 	    if (tabsize > 0) | 
 | 6019 | 		j += tabsize - (j % tabsize); | 
 | 6020 | 	} | 
 | 6021 |         else { | 
 | 6022 |             j++; | 
 | 6023 |             if (*p == '\n' || *p == '\r') { | 
 | 6024 |                 i += j; | 
 | 6025 |                 j = 0; | 
 | 6026 |             } | 
 | 6027 |         } | 
 | 6028 |  | 
 | 6029 |     /* Second pass: create output string and fill it */ | 
 | 6030 |     u = _PyUnicode_New(i + j); | 
 | 6031 |     if (!u) | 
 | 6032 |         return NULL; | 
 | 6033 |  | 
 | 6034 |     j = 0; | 
 | 6035 |     q = u->str; | 
 | 6036 |  | 
 | 6037 |     for (p = self->str; p < e; p++) | 
 | 6038 |         if (*p == '\t') { | 
 | 6039 | 	    if (tabsize > 0) { | 
 | 6040 | 		i = tabsize - (j % tabsize); | 
 | 6041 | 		j += i; | 
 | 6042 | 		while (i--) | 
 | 6043 | 		    *q++ = ' '; | 
 | 6044 | 	    } | 
 | 6045 | 	} | 
 | 6046 | 	else { | 
 | 6047 |             j++; | 
 | 6048 | 	    *q++ = *p; | 
 | 6049 |             if (*p == '\n' || *p == '\r') | 
 | 6050 |                 j = 0; | 
 | 6051 |         } | 
 | 6052 |  | 
 | 6053 |     return (PyObject*) u; | 
 | 6054 | } | 
 | 6055 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6056 | PyDoc_STRVAR(find__doc__, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6057 | "S.find(sub [,start [,end]]) -> int\n\ | 
 | 6058 | \n\ | 
 | 6059 | Return the lowest index in S where substring sub is found,\n\ | 
 | 6060 | such that sub is contained within s[start,end].  Optional\n\ | 
 | 6061 | arguments start and end are interpreted as in slice notation.\n\ | 
 | 6062 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6063 | Return -1 on failure."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6064 |  | 
 | 6065 | static PyObject * | 
 | 6066 | unicode_find(PyUnicodeObject *self, PyObject *args) | 
 | 6067 | { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6068 |     PyObject *substring; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6069 |     Py_ssize_t start = 0; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6070 |     Py_ssize_t end = PY_SSIZE_T_MAX; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6071 |     Py_ssize_t result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6072 |  | 
| Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6073 |     if (!PyArg_ParseTuple(args, "O|O&O&:find", &substring, | 
 | 6074 | 		_PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6075 |         return NULL; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6076 |     substring = PyUnicode_FromObject(substring); | 
 | 6077 |     if (!substring) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6078 | 	return NULL; | 
 | 6079 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6080 |     result = stringlib_find_slice( | 
 | 6081 |         PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), | 
 | 6082 |         PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), | 
 | 6083 |         start, end | 
 | 6084 |         ); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6085 |  | 
 | 6086 |     Py_DECREF(substring); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6087 |  | 
 | 6088 |     return PyInt_FromSsize_t(result); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6089 | } | 
 | 6090 |  | 
 | 6091 | static PyObject * | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6092 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6093 | { | 
 | 6094 |     if (index < 0 || index >= self->length) { | 
 | 6095 |         PyErr_SetString(PyExc_IndexError, "string index out of range"); | 
 | 6096 |         return NULL; | 
 | 6097 |     } | 
 | 6098 |  | 
 | 6099 |     return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); | 
 | 6100 | } | 
 | 6101 |  | 
 | 6102 | static long | 
| Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6103 | unicode_hash(PyObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6104 | { | 
| Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6105 |     /* Since Unicode objects compare equal to their UTF-8 string | 
 | 6106 |        counterparts, we hash the UTF-8 string. */ | 
 | 6107 |     PyObject *v = _PyUnicode_AsDefaultEncodedString(self, NULL); | 
 | 6108 |     return PyObject_Hash(v); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6109 | } | 
 | 6110 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6111 | PyDoc_STRVAR(index__doc__, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6112 | "S.index(sub [,start [,end]]) -> int\n\ | 
 | 6113 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6114 | 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] | 6115 |  | 
 | 6116 | static PyObject * | 
 | 6117 | unicode_index(PyUnicodeObject *self, PyObject *args) | 
 | 6118 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6119 |     Py_ssize_t result; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6120 |     PyObject *substring; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6121 |     Py_ssize_t start = 0; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6122 |     Py_ssize_t end = PY_SSIZE_T_MAX; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6123 |  | 
| Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6124 |     if (!PyArg_ParseTuple(args, "O|O&O&:index", &substring, | 
 | 6125 | 		_PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6126 |         return NULL; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6127 |     substring = PyUnicode_FromObject(substring); | 
 | 6128 |     if (!substring) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6129 | 	return NULL; | 
 | 6130 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6131 |     result = stringlib_find_slice( | 
 | 6132 |         PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), | 
 | 6133 |         PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), | 
 | 6134 |         start, end | 
 | 6135 |         ); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6136 |  | 
 | 6137 |     Py_DECREF(substring); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6138 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6139 |     if (result < 0) { | 
 | 6140 |         PyErr_SetString(PyExc_ValueError, "substring not found"); | 
 | 6141 |         return NULL; | 
 | 6142 |     } | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6143 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6144 |     return PyInt_FromSsize_t(result); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6145 | } | 
 | 6146 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6147 | PyDoc_STRVAR(islower__doc__, | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6148 | "S.islower() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6149 | \n\ | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6150 | 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] | 6151 | at least one cased character in S, False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6152 |  | 
 | 6153 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6154 | unicode_islower(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6155 | { | 
 | 6156 |     register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); | 
 | 6157 |     register const Py_UNICODE *e; | 
 | 6158 |     int cased; | 
 | 6159 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6160 |     /* Shortcut for single character strings */ | 
 | 6161 |     if (PyUnicode_GET_SIZE(self) == 1) | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6162 | 	return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6163 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6164 |     /* Special case for empty strings */ | 
| Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6165 |     if (PyUnicode_GET_SIZE(self) == 0) | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6166 | 	return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6167 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6168 |     e = p + PyUnicode_GET_SIZE(self); | 
 | 6169 |     cased = 0; | 
 | 6170 |     for (; p < e; p++) { | 
 | 6171 | 	register const Py_UNICODE ch = *p; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6172 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6173 | 	if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6174 | 	    return PyBool_FromLong(0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6175 | 	else if (!cased && Py_UNICODE_ISLOWER(ch)) | 
 | 6176 | 	    cased = 1; | 
 | 6177 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6178 |     return PyBool_FromLong(cased); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6179 | } | 
 | 6180 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6181 | PyDoc_STRVAR(isupper__doc__, | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6182 | "S.isupper() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6183 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6184 | 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] | 6185 | at least one cased character in S, False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6186 |  | 
 | 6187 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6188 | unicode_isupper(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6189 | { | 
 | 6190 |     register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); | 
 | 6191 |     register const Py_UNICODE *e; | 
 | 6192 |     int cased; | 
 | 6193 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6194 |     /* Shortcut for single character strings */ | 
 | 6195 |     if (PyUnicode_GET_SIZE(self) == 1) | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6196 | 	return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6197 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6198 |     /* Special case for empty strings */ | 
| Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6199 |     if (PyUnicode_GET_SIZE(self) == 0) | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6200 | 	return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6201 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6202 |     e = p + PyUnicode_GET_SIZE(self); | 
 | 6203 |     cased = 0; | 
 | 6204 |     for (; p < e; p++) { | 
 | 6205 | 	register const Py_UNICODE ch = *p; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6206 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6207 | 	if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6208 | 	    return PyBool_FromLong(0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6209 | 	else if (!cased && Py_UNICODE_ISUPPER(ch)) | 
 | 6210 | 	    cased = 1; | 
 | 6211 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6212 |     return PyBool_FromLong(cased); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6213 | } | 
 | 6214 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6215 | PyDoc_STRVAR(istitle__doc__, | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6216 | "S.istitle() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6217 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6218 | Return True if S is a titlecased string and there is at least one\n\ | 
 | 6219 | character in S, i.e. upper- and titlecase characters may only\n\ | 
 | 6220 | follow uncased characters and lowercase characters only cased ones.\n\ | 
 | 6221 | Return False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6222 |  | 
 | 6223 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6224 | unicode_istitle(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6225 | { | 
 | 6226 |     register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); | 
 | 6227 |     register const Py_UNICODE *e; | 
 | 6228 |     int cased, previous_is_cased; | 
 | 6229 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6230 |     /* Shortcut for single character strings */ | 
 | 6231 |     if (PyUnicode_GET_SIZE(self) == 1) | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6232 | 	return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || | 
 | 6233 | 			       (Py_UNICODE_ISUPPER(*p) != 0)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6234 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6235 |     /* Special case for empty strings */ | 
| Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6236 |     if (PyUnicode_GET_SIZE(self) == 0) | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6237 | 	return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6238 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6239 |     e = p + PyUnicode_GET_SIZE(self); | 
 | 6240 |     cased = 0; | 
 | 6241 |     previous_is_cased = 0; | 
 | 6242 |     for (; p < e; p++) { | 
 | 6243 | 	register const Py_UNICODE ch = *p; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6244 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6245 | 	if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { | 
 | 6246 | 	    if (previous_is_cased) | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6247 | 		return PyBool_FromLong(0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6248 | 	    previous_is_cased = 1; | 
 | 6249 | 	    cased = 1; | 
 | 6250 | 	} | 
 | 6251 | 	else if (Py_UNICODE_ISLOWER(ch)) { | 
 | 6252 | 	    if (!previous_is_cased) | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6253 | 		return PyBool_FromLong(0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6254 | 	    previous_is_cased = 1; | 
 | 6255 | 	    cased = 1; | 
 | 6256 | 	} | 
 | 6257 | 	else | 
 | 6258 | 	    previous_is_cased = 0; | 
 | 6259 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6260 |     return PyBool_FromLong(cased); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6261 | } | 
 | 6262 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6263 | PyDoc_STRVAR(isspace__doc__, | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6264 | "S.isspace() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6265 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6266 | Return True if all characters in S are whitespace\n\ | 
 | 6267 | and there is at least one character in S, False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6268 |  | 
 | 6269 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6270 | unicode_isspace(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6271 | { | 
 | 6272 |     register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); | 
 | 6273 |     register const Py_UNICODE *e; | 
 | 6274 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6275 |     /* Shortcut for single character strings */ | 
 | 6276 |     if (PyUnicode_GET_SIZE(self) == 1 && | 
 | 6277 | 	Py_UNICODE_ISSPACE(*p)) | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6278 | 	return PyBool_FromLong(1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6279 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6280 |     /* Special case for empty strings */ | 
| Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6281 |     if (PyUnicode_GET_SIZE(self) == 0) | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6282 | 	return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6283 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6284 |     e = p + PyUnicode_GET_SIZE(self); | 
 | 6285 |     for (; p < e; p++) { | 
 | 6286 | 	if (!Py_UNICODE_ISSPACE(*p)) | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6287 | 	    return PyBool_FromLong(0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6288 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6289 |     return PyBool_FromLong(1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6290 | } | 
 | 6291 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6292 | PyDoc_STRVAR(isalpha__doc__, | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6293 | "S.isalpha() -> bool\n\ | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6294 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6295 | Return True if all characters in S are alphabetic\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6296 | and there is at least one character in S, False otherwise."); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6297 |  | 
 | 6298 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6299 | unicode_isalpha(PyUnicodeObject *self) | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6300 | { | 
 | 6301 |     register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); | 
 | 6302 |     register const Py_UNICODE *e; | 
 | 6303 |  | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6304 |     /* Shortcut for single character strings */ | 
 | 6305 |     if (PyUnicode_GET_SIZE(self) == 1 && | 
 | 6306 | 	Py_UNICODE_ISALPHA(*p)) | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6307 | 	return PyBool_FromLong(1); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6308 |  | 
 | 6309 |     /* Special case for empty strings */ | 
| Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6310 |     if (PyUnicode_GET_SIZE(self) == 0) | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6311 | 	return PyBool_FromLong(0); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6312 |  | 
 | 6313 |     e = p + PyUnicode_GET_SIZE(self); | 
 | 6314 |     for (; p < e; p++) { | 
 | 6315 | 	if (!Py_UNICODE_ISALPHA(*p)) | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6316 | 	    return PyBool_FromLong(0); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6317 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6318 |     return PyBool_FromLong(1); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6319 | } | 
 | 6320 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6321 | PyDoc_STRVAR(isalnum__doc__, | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6322 | "S.isalnum() -> bool\n\ | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6323 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6324 | Return True if all characters in S are alphanumeric\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6325 | and there is at least one character in S, False otherwise."); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6326 |  | 
 | 6327 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6328 | unicode_isalnum(PyUnicodeObject *self) | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6329 | { | 
 | 6330 |     register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); | 
 | 6331 |     register const Py_UNICODE *e; | 
 | 6332 |  | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6333 |     /* Shortcut for single character strings */ | 
 | 6334 |     if (PyUnicode_GET_SIZE(self) == 1 && | 
 | 6335 | 	Py_UNICODE_ISALNUM(*p)) | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6336 | 	return PyBool_FromLong(1); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6337 |  | 
 | 6338 |     /* Special case for empty strings */ | 
| Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6339 |     if (PyUnicode_GET_SIZE(self) == 0) | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6340 | 	return PyBool_FromLong(0); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6341 |  | 
 | 6342 |     e = p + PyUnicode_GET_SIZE(self); | 
 | 6343 |     for (; p < e; p++) { | 
 | 6344 | 	if (!Py_UNICODE_ISALNUM(*p)) | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6345 | 	    return PyBool_FromLong(0); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6346 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6347 |     return PyBool_FromLong(1); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6348 | } | 
 | 6349 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6350 | PyDoc_STRVAR(isdecimal__doc__, | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6351 | "S.isdecimal() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6352 | \n\ | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6353 | 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] | 6354 | False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6355 |  | 
 | 6356 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6357 | unicode_isdecimal(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6358 | { | 
 | 6359 |     register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); | 
 | 6360 |     register const Py_UNICODE *e; | 
 | 6361 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6362 |     /* Shortcut for single character strings */ | 
 | 6363 |     if (PyUnicode_GET_SIZE(self) == 1 && | 
 | 6364 | 	Py_UNICODE_ISDECIMAL(*p)) | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6365 | 	return PyBool_FromLong(1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6366 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6367 |     /* Special case for empty strings */ | 
| Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6368 |     if (PyUnicode_GET_SIZE(self) == 0) | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6369 | 	return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6370 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6371 |     e = p + PyUnicode_GET_SIZE(self); | 
 | 6372 |     for (; p < e; p++) { | 
 | 6373 | 	if (!Py_UNICODE_ISDECIMAL(*p)) | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6374 | 	    return PyBool_FromLong(0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6375 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6376 |     return PyBool_FromLong(1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6377 | } | 
 | 6378 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6379 | PyDoc_STRVAR(isdigit__doc__, | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6380 | "S.isdigit() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6381 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6382 | Return True if all characters in S are digits\n\ | 
 | 6383 | and there is at least one character in S, False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6384 |  | 
 | 6385 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6386 | unicode_isdigit(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6387 | { | 
 | 6388 |     register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); | 
 | 6389 |     register const Py_UNICODE *e; | 
 | 6390 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6391 |     /* Shortcut for single character strings */ | 
 | 6392 |     if (PyUnicode_GET_SIZE(self) == 1 && | 
 | 6393 | 	Py_UNICODE_ISDIGIT(*p)) | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6394 | 	return PyBool_FromLong(1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6395 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6396 |     /* Special case for empty strings */ | 
| Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6397 |     if (PyUnicode_GET_SIZE(self) == 0) | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6398 | 	return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6399 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6400 |     e = p + PyUnicode_GET_SIZE(self); | 
 | 6401 |     for (; p < e; p++) { | 
 | 6402 | 	if (!Py_UNICODE_ISDIGIT(*p)) | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6403 | 	    return PyBool_FromLong(0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6404 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6405 |     return PyBool_FromLong(1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6406 | } | 
 | 6407 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6408 | PyDoc_STRVAR(isnumeric__doc__, | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6409 | "S.isnumeric() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6410 | \n\ | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6411 | 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] | 6412 | False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6413 |  | 
 | 6414 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6415 | unicode_isnumeric(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6416 | { | 
 | 6417 |     register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); | 
 | 6418 |     register const Py_UNICODE *e; | 
 | 6419 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6420 |     /* Shortcut for single character strings */ | 
 | 6421 |     if (PyUnicode_GET_SIZE(self) == 1 && | 
 | 6422 | 	Py_UNICODE_ISNUMERIC(*p)) | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6423 | 	return PyBool_FromLong(1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6424 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6425 |     /* Special case for empty strings */ | 
| Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6426 |     if (PyUnicode_GET_SIZE(self) == 0) | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6427 | 	return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6428 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6429 |     e = p + PyUnicode_GET_SIZE(self); | 
 | 6430 |     for (; p < e; p++) { | 
 | 6431 | 	if (!Py_UNICODE_ISNUMERIC(*p)) | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6432 | 	    return PyBool_FromLong(0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6433 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6434 |     return PyBool_FromLong(1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6435 | } | 
 | 6436 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6437 | PyDoc_STRVAR(join__doc__, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6438 | "S.join(sequence) -> unicode\n\ | 
 | 6439 | \n\ | 
 | 6440 | 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] | 6441 | sequence.  The separator between elements is S."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6442 |  | 
 | 6443 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6444 | unicode_join(PyObject *self, PyObject *data) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6445 | { | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6446 |     return PyUnicode_Join(self, data); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6447 | } | 
 | 6448 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6449 | static Py_ssize_t | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6450 | unicode_length(PyUnicodeObject *self) | 
 | 6451 | { | 
 | 6452 |     return self->length; | 
 | 6453 | } | 
 | 6454 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6455 | PyDoc_STRVAR(ljust__doc__, | 
| Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 6456 | "S.ljust(width[, fillchar]) -> int\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6457 | \n\ | 
 | 6458 | 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] | 6459 | done using the specified fill character (default is a space)."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6460 |  | 
 | 6461 | static PyObject * | 
 | 6462 | unicode_ljust(PyUnicodeObject *self, PyObject *args) | 
 | 6463 | { | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6464 |     Py_ssize_t width; | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6465 |     Py_UNICODE fillchar = ' '; | 
 | 6466 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6467 |     if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6468 |         return NULL; | 
 | 6469 |  | 
| Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6470 |     if (self->length >= width && PyUnicode_CheckExact(self)) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6471 |         Py_INCREF(self); | 
 | 6472 |         return (PyObject*) self; | 
 | 6473 |     } | 
 | 6474 |  | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6475 |     return (PyObject*) pad(self, 0, width - self->length, fillchar); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6476 | } | 
 | 6477 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6478 | PyDoc_STRVAR(lower__doc__, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6479 | "S.lower() -> unicode\n\ | 
 | 6480 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6481 | Return a copy of the string S converted to lowercase."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6482 |  | 
 | 6483 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6484 | unicode_lower(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6485 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6486 |     return fixup(self, fixlower); | 
 | 6487 | } | 
 | 6488 |  | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6489 | #define LEFTSTRIP 0 | 
 | 6490 | #define RIGHTSTRIP 1 | 
 | 6491 | #define BOTHSTRIP 2 | 
 | 6492 |  | 
 | 6493 | /* Arrays indexed by above */ | 
 | 6494 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; | 
 | 6495 |  | 
 | 6496 | #define STRIPNAME(i) (stripformat[i]+3) | 
 | 6497 |  | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6498 | /* externally visible for str.strip(unicode) */ | 
 | 6499 | PyObject * | 
 | 6500 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) | 
 | 6501 | { | 
 | 6502 | 	Py_UNICODE *s = PyUnicode_AS_UNICODE(self); | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6503 | 	Py_ssize_t len = PyUnicode_GET_SIZE(self); | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6504 | 	Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6505 | 	Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); | 
 | 6506 | 	Py_ssize_t i, j; | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6507 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6508 |         BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); | 
 | 6509 |  | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6510 | 	i = 0; | 
 | 6511 | 	if (striptype != RIGHTSTRIP) { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6512 |             while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { | 
 | 6513 |                 i++; | 
 | 6514 |             } | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6515 | 	} | 
 | 6516 |  | 
 | 6517 | 	j = len; | 
 | 6518 | 	if (striptype != LEFTSTRIP) { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6519 |             do { | 
 | 6520 |                 j--; | 
 | 6521 |             } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); | 
 | 6522 |             j++; | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6523 | 	} | 
 | 6524 |  | 
 | 6525 | 	if (i == 0 && j == len && PyUnicode_CheckExact(self)) { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6526 |             Py_INCREF(self); | 
 | 6527 |             return (PyObject*)self; | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6528 | 	} | 
 | 6529 | 	else | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6530 |             return PyUnicode_FromUnicode(s+i, j-i); | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6531 | } | 
 | 6532 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6533 |  | 
 | 6534 | static PyObject * | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6535 | do_strip(PyUnicodeObject *self, int striptype) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6536 | { | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6537 | 	Py_UNICODE *s = PyUnicode_AS_UNICODE(self); | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6538 | 	Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6539 |  | 
 | 6540 | 	i = 0; | 
 | 6541 | 	if (striptype != RIGHTSTRIP) { | 
 | 6542 | 		while (i < len && Py_UNICODE_ISSPACE(s[i])) { | 
 | 6543 | 			i++; | 
 | 6544 | 		} | 
 | 6545 | 	} | 
 | 6546 |  | 
 | 6547 | 	j = len; | 
 | 6548 | 	if (striptype != LEFTSTRIP) { | 
 | 6549 | 		do { | 
 | 6550 | 			j--; | 
 | 6551 | 		} while (j >= i && Py_UNICODE_ISSPACE(s[j])); | 
 | 6552 | 		j++; | 
 | 6553 | 	} | 
 | 6554 |  | 
 | 6555 | 	if (i == 0 && j == len && PyUnicode_CheckExact(self)) { | 
 | 6556 | 		Py_INCREF(self); | 
 | 6557 | 		return (PyObject*)self; | 
 | 6558 | 	} | 
 | 6559 | 	else | 
 | 6560 | 		return PyUnicode_FromUnicode(s+i, j-i); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6561 | } | 
 | 6562 |  | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6563 |  | 
 | 6564 | static PyObject * | 
 | 6565 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) | 
 | 6566 | { | 
 | 6567 | 	PyObject *sep = NULL; | 
 | 6568 |  | 
 | 6569 | 	if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) | 
 | 6570 | 		return NULL; | 
 | 6571 |  | 
 | 6572 | 	if (sep != NULL && sep != Py_None) { | 
 | 6573 | 		if (PyUnicode_Check(sep)) | 
 | 6574 | 			return _PyUnicode_XStrip(self, striptype, sep); | 
 | 6575 | 		else if (PyString_Check(sep)) { | 
 | 6576 | 			PyObject *res; | 
 | 6577 | 			sep = PyUnicode_FromObject(sep); | 
 | 6578 | 			if (sep==NULL) | 
 | 6579 | 				return NULL; | 
 | 6580 | 			res = _PyUnicode_XStrip(self, striptype, sep); | 
 | 6581 | 			Py_DECREF(sep); | 
 | 6582 | 			return res; | 
 | 6583 | 		} | 
 | 6584 | 		else { | 
 | 6585 | 			PyErr_Format(PyExc_TypeError, | 
 | 6586 | 				     "%s arg must be None, unicode or str", | 
 | 6587 | 				     STRIPNAME(striptype)); | 
 | 6588 | 			return NULL; | 
 | 6589 | 		} | 
 | 6590 | 	} | 
 | 6591 |  | 
 | 6592 | 	return do_strip(self, striptype); | 
 | 6593 | } | 
 | 6594 |  | 
 | 6595 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6596 | PyDoc_STRVAR(strip__doc__, | 
| Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6597 | "S.strip([chars]) -> unicode\n\ | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6598 | \n\ | 
 | 6599 | Return a copy of the string S with leading and trailing\n\ | 
 | 6600 | whitespace removed.\n\ | 
| Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6601 | If chars is given and not None, remove characters in chars instead.\n\ | 
 | 6602 | 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] | 6603 |  | 
 | 6604 | static PyObject * | 
 | 6605 | unicode_strip(PyUnicodeObject *self, PyObject *args) | 
 | 6606 | { | 
 | 6607 | 	if (PyTuple_GET_SIZE(args) == 0) | 
 | 6608 | 		return do_strip(self, BOTHSTRIP); /* Common case */ | 
 | 6609 | 	else | 
 | 6610 | 		return do_argstrip(self, BOTHSTRIP, args); | 
 | 6611 | } | 
 | 6612 |  | 
 | 6613 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6614 | PyDoc_STRVAR(lstrip__doc__, | 
| Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6615 | "S.lstrip([chars]) -> unicode\n\ | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6616 | \n\ | 
 | 6617 | Return a copy of the string S with leading whitespace removed.\n\ | 
| Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6618 | If chars is given and not None, remove characters in chars instead.\n\ | 
 | 6619 | 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] | 6620 |  | 
 | 6621 | static PyObject * | 
 | 6622 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) | 
 | 6623 | { | 
 | 6624 | 	if (PyTuple_GET_SIZE(args) == 0) | 
 | 6625 | 		return do_strip(self, LEFTSTRIP); /* Common case */ | 
 | 6626 | 	else | 
 | 6627 | 		return do_argstrip(self, LEFTSTRIP, args); | 
 | 6628 | } | 
 | 6629 |  | 
 | 6630 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6631 | PyDoc_STRVAR(rstrip__doc__, | 
| Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6632 | "S.rstrip([chars]) -> unicode\n\ | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6633 | \n\ | 
 | 6634 | Return a copy of the string S with trailing whitespace removed.\n\ | 
| Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6635 | If chars is given and not None, remove characters in chars instead.\n\ | 
 | 6636 | 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] | 6637 |  | 
 | 6638 | static PyObject * | 
 | 6639 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) | 
 | 6640 | { | 
 | 6641 | 	if (PyTuple_GET_SIZE(args) == 0) | 
 | 6642 | 		return do_strip(self, RIGHTSTRIP); /* Common case */ | 
 | 6643 | 	else | 
 | 6644 | 		return do_argstrip(self, RIGHTSTRIP, args); | 
 | 6645 | } | 
 | 6646 |  | 
 | 6647 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6648 | static PyObject* | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6649 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6650 | { | 
 | 6651 |     PyUnicodeObject *u; | 
 | 6652 |     Py_UNICODE *p; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6653 |     Py_ssize_t nchars; | 
| Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 6654 |     size_t nbytes; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6655 |  | 
 | 6656 |     if (len < 0) | 
 | 6657 |         len = 0; | 
 | 6658 |  | 
| Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6659 |     if (len == 1 && PyUnicode_CheckExact(str)) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6660 |         /* no repeat, return original string */ | 
 | 6661 |         Py_INCREF(str); | 
 | 6662 |         return (PyObject*) str; | 
 | 6663 |     } | 
| Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 6664 |  | 
 | 6665 |     /* ensure # of chars needed doesn't overflow int and # of bytes | 
 | 6666 |      * needed doesn't overflow size_t | 
 | 6667 |      */ | 
 | 6668 |     nchars = len * str->length; | 
 | 6669 |     if (len && nchars / len != str->length) { | 
 | 6670 |         PyErr_SetString(PyExc_OverflowError, | 
 | 6671 |                         "repeated string is too long"); | 
 | 6672 |         return NULL; | 
 | 6673 |     } | 
 | 6674 |     nbytes = (nchars + 1) * sizeof(Py_UNICODE); | 
 | 6675 |     if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { | 
 | 6676 |         PyErr_SetString(PyExc_OverflowError, | 
 | 6677 |                         "repeated string is too long"); | 
 | 6678 |         return NULL; | 
 | 6679 |     } | 
 | 6680 |     u = _PyUnicode_New(nchars); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6681 |     if (!u) | 
 | 6682 |         return NULL; | 
 | 6683 |  | 
 | 6684 |     p = u->str; | 
 | 6685 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6686 |     if (str->length == 1 && len > 0) { | 
 | 6687 |         Py_UNICODE_FILL(p, str->str[0], len); | 
 | 6688 |     } else { | 
 | 6689 | 	Py_ssize_t done = 0; /* number of characters copied this far */ | 
 | 6690 | 	if (done < nchars) { | 
 | 6691 |             Py_UNICODE_COPY(p, str->str, str->length); | 
 | 6692 |             done = str->length; | 
 | 6693 | 	} | 
 | 6694 | 	while (done < nchars) { | 
 | 6695 |             int n = (done <= nchars-done) ? done : nchars-done; | 
 | 6696 |             Py_UNICODE_COPY(p+done, p, n); | 
 | 6697 |             done += n; | 
 | 6698 | 	} | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6699 |     } | 
 | 6700 |  | 
 | 6701 |     return (PyObject*) u; | 
 | 6702 | } | 
 | 6703 |  | 
 | 6704 | PyObject *PyUnicode_Replace(PyObject *obj, | 
 | 6705 | 			    PyObject *subobj, | 
 | 6706 | 			    PyObject *replobj, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6707 | 			    Py_ssize_t maxcount) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6708 | { | 
 | 6709 |     PyObject *self; | 
 | 6710 |     PyObject *str1; | 
 | 6711 |     PyObject *str2; | 
 | 6712 |     PyObject *result; | 
 | 6713 |  | 
 | 6714 |     self = PyUnicode_FromObject(obj); | 
 | 6715 |     if (self == NULL) | 
 | 6716 | 	return NULL; | 
 | 6717 |     str1 = PyUnicode_FromObject(subobj); | 
 | 6718 |     if (str1 == NULL) { | 
 | 6719 | 	Py_DECREF(self); | 
 | 6720 | 	return NULL; | 
 | 6721 |     } | 
 | 6722 |     str2 = PyUnicode_FromObject(replobj); | 
 | 6723 |     if (str2 == NULL) { | 
 | 6724 | 	Py_DECREF(self); | 
 | 6725 | 	Py_DECREF(str1); | 
 | 6726 | 	return NULL; | 
 | 6727 |     } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6728 |     result = replace((PyUnicodeObject *)self, | 
 | 6729 | 		     (PyUnicodeObject *)str1, | 
 | 6730 | 		     (PyUnicodeObject *)str2, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6731 | 		     maxcount); | 
 | 6732 |     Py_DECREF(self); | 
 | 6733 |     Py_DECREF(str1); | 
 | 6734 |     Py_DECREF(str2); | 
 | 6735 |     return result; | 
 | 6736 | } | 
 | 6737 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6738 | PyDoc_STRVAR(replace__doc__, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6739 | "S.replace (old, new[, maxsplit]) -> unicode\n\ | 
 | 6740 | \n\ | 
 | 6741 | Return a copy of S with all occurrences of substring\n\ | 
 | 6742 | 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] | 6743 | given, only the first maxsplit occurrences are replaced."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6744 |  | 
 | 6745 | static PyObject* | 
 | 6746 | unicode_replace(PyUnicodeObject *self, PyObject *args) | 
 | 6747 | { | 
 | 6748 |     PyUnicodeObject *str1; | 
 | 6749 |     PyUnicodeObject *str2; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6750 |     Py_ssize_t maxcount = -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6751 |     PyObject *result; | 
 | 6752 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6753 |     if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6754 |         return NULL; | 
 | 6755 |     str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); | 
 | 6756 |     if (str1 == NULL) | 
 | 6757 | 	return NULL; | 
 | 6758 |     str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); | 
| Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 6759 |     if (str2 == NULL) { | 
 | 6760 | 	Py_DECREF(str1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6761 | 	return NULL; | 
| Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 6762 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6763 |  | 
 | 6764 |     result = replace(self, str1, str2, maxcount); | 
 | 6765 |  | 
 | 6766 |     Py_DECREF(str1); | 
 | 6767 |     Py_DECREF(str2); | 
 | 6768 |     return result; | 
 | 6769 | } | 
 | 6770 |  | 
 | 6771 | static | 
 | 6772 | PyObject *unicode_repr(PyObject *unicode) | 
 | 6773 | { | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6774 |     PyObject *repr; | 
| Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame^] | 6775 |     Py_UNICODE *p; | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6776 |     Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); | 
 | 6777 |     Py_ssize_t size = PyUnicode_GET_SIZE(unicode); | 
 | 6778 |  | 
 | 6779 |     /* XXX(nnorwitz): rather than over-allocating, it would be | 
 | 6780 |        better to choose a different scheme.  Perhaps scan the | 
 | 6781 |        first N-chars of the string and allocate based on that size. | 
 | 6782 |     */ | 
 | 6783 |     /* Initial allocation is based on the longest-possible unichr | 
 | 6784 |        escape. | 
 | 6785 |  | 
 | 6786 |        In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source | 
 | 6787 |        unichr, so in this case it's the longest unichr escape. In | 
 | 6788 |        narrow (UTF-16) builds this is five chars per source unichr | 
 | 6789 |        since there are two unichrs in the surrogate pair, so in narrow | 
 | 6790 |        (UTF-16) builds it's not the longest unichr escape. | 
 | 6791 |  | 
 | 6792 |        In wide or narrow builds '\uxxxx' is 6 chars per source unichr, | 
 | 6793 |        so in the narrow (UTF-16) build case it's the longest unichr | 
 | 6794 |        escape. | 
 | 6795 |     */ | 
 | 6796 |  | 
| Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame^] | 6797 |     repr = PyUnicode_FromUnicode(NULL, | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6798 |         2 /* quotes */ | 
 | 6799 | #ifdef Py_UNICODE_WIDE | 
 | 6800 |         + 10*size | 
 | 6801 | #else | 
 | 6802 |         + 6*size | 
 | 6803 | #endif | 
 | 6804 |         + 1); | 
 | 6805 |     if (repr == NULL) | 
 | 6806 |         return NULL; | 
 | 6807 |  | 
| Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame^] | 6808 |     p = PyUnicode_AS_UNICODE(repr); | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6809 |  | 
 | 6810 |     /* Add quote */ | 
 | 6811 |     *p++ = (findchar(s, size, '\'') && | 
 | 6812 |             !findchar(s, size, '"')) ? '"' : '\''; | 
 | 6813 |     while (size-- > 0) { | 
 | 6814 |         Py_UNICODE ch = *s++; | 
 | 6815 |  | 
 | 6816 |         /* Escape quotes and backslashes */ | 
| Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame^] | 6817 |         if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6818 |             *p++ = '\\'; | 
| Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame^] | 6819 |             *p++ = ch; | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6820 |             continue; | 
 | 6821 |         } | 
 | 6822 |  | 
 | 6823 | #ifdef Py_UNICODE_WIDE | 
 | 6824 |         /* Map 21-bit characters to '\U00xxxxxx' */ | 
 | 6825 |         else if (ch >= 0x10000) { | 
 | 6826 |             *p++ = '\\'; | 
 | 6827 |             *p++ = 'U'; | 
 | 6828 |             *p++ = hexdigits[(ch >> 28) & 0x0000000F]; | 
 | 6829 |             *p++ = hexdigits[(ch >> 24) & 0x0000000F]; | 
 | 6830 |             *p++ = hexdigits[(ch >> 20) & 0x0000000F]; | 
 | 6831 |             *p++ = hexdigits[(ch >> 16) & 0x0000000F]; | 
 | 6832 |             *p++ = hexdigits[(ch >> 12) & 0x0000000F]; | 
 | 6833 |             *p++ = hexdigits[(ch >> 8) & 0x0000000F]; | 
 | 6834 |             *p++ = hexdigits[(ch >> 4) & 0x0000000F]; | 
 | 6835 |             *p++ = hexdigits[ch & 0x0000000F]; | 
 | 6836 | 	    continue; | 
 | 6837 |         } | 
 | 6838 | #else | 
 | 6839 | 	/* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ | 
 | 6840 | 	else if (ch >= 0xD800 && ch < 0xDC00) { | 
 | 6841 | 	    Py_UNICODE ch2; | 
 | 6842 | 	    Py_UCS4 ucs; | 
 | 6843 |  | 
 | 6844 | 	    ch2 = *s++; | 
 | 6845 | 	    size--; | 
 | 6846 | 	    if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { | 
 | 6847 | 		ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; | 
 | 6848 | 		*p++ = '\\'; | 
 | 6849 | 		*p++ = 'U'; | 
 | 6850 | 		*p++ = hexdigits[(ucs >> 28) & 0x0000000F]; | 
 | 6851 | 		*p++ = hexdigits[(ucs >> 24) & 0x0000000F]; | 
 | 6852 | 		*p++ = hexdigits[(ucs >> 20) & 0x0000000F]; | 
 | 6853 | 		*p++ = hexdigits[(ucs >> 16) & 0x0000000F]; | 
 | 6854 | 		*p++ = hexdigits[(ucs >> 12) & 0x0000000F]; | 
 | 6855 | 		*p++ = hexdigits[(ucs >> 8) & 0x0000000F]; | 
 | 6856 | 		*p++ = hexdigits[(ucs >> 4) & 0x0000000F]; | 
 | 6857 | 		*p++ = hexdigits[ucs & 0x0000000F]; | 
 | 6858 | 		continue; | 
 | 6859 | 	    } | 
 | 6860 | 	    /* Fall through: isolated surrogates are copied as-is */ | 
 | 6861 | 	    s--; | 
 | 6862 | 	    size++; | 
 | 6863 | 	} | 
 | 6864 | #endif | 
 | 6865 |  | 
 | 6866 |         /* Map 16-bit characters to '\uxxxx' */ | 
 | 6867 |         if (ch >= 256) { | 
 | 6868 |             *p++ = '\\'; | 
 | 6869 |             *p++ = 'u'; | 
 | 6870 |             *p++ = hexdigits[(ch >> 12) & 0x000F]; | 
 | 6871 |             *p++ = hexdigits[(ch >> 8) & 0x000F]; | 
 | 6872 |             *p++ = hexdigits[(ch >> 4) & 0x000F]; | 
 | 6873 |             *p++ = hexdigits[ch & 0x000F]; | 
 | 6874 |         } | 
 | 6875 |  | 
 | 6876 |         /* Map special whitespace to '\t', \n', '\r' */ | 
 | 6877 |         else if (ch == '\t') { | 
 | 6878 |             *p++ = '\\'; | 
 | 6879 |             *p++ = 't'; | 
 | 6880 |         } | 
 | 6881 |         else if (ch == '\n') { | 
 | 6882 |             *p++ = '\\'; | 
 | 6883 |             *p++ = 'n'; | 
 | 6884 |         } | 
 | 6885 |         else if (ch == '\r') { | 
 | 6886 |             *p++ = '\\'; | 
 | 6887 |             *p++ = 'r'; | 
 | 6888 |         } | 
 | 6889 |  | 
 | 6890 |         /* Map non-printable US ASCII to '\xhh' */ | 
 | 6891 |         else if (ch < ' ' || ch >= 0x7F) { | 
 | 6892 |             *p++ = '\\'; | 
 | 6893 |             *p++ = 'x'; | 
 | 6894 |             *p++ = hexdigits[(ch >> 4) & 0x000F]; | 
 | 6895 |             *p++ = hexdigits[ch & 0x000F]; | 
 | 6896 |         } | 
 | 6897 |  | 
 | 6898 |         /* Copy everything else as-is */ | 
 | 6899 |         else | 
 | 6900 |             *p++ = (char) ch; | 
 | 6901 |     } | 
 | 6902 |     /* Add quote */ | 
| Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame^] | 6903 |     *p++ = PyUnicode_AS_UNICODE(repr)[0]; | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6904 |  | 
 | 6905 |     *p = '\0'; | 
| Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame^] | 6906 |     _PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6907 |     return repr; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6908 | } | 
 | 6909 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6910 | PyDoc_STRVAR(rfind__doc__, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6911 | "S.rfind(sub [,start [,end]]) -> int\n\ | 
 | 6912 | \n\ | 
 | 6913 | Return the highest index in S where substring sub is found,\n\ | 
 | 6914 | such that sub is contained within s[start,end].  Optional\n\ | 
 | 6915 | arguments start and end are interpreted as in slice notation.\n\ | 
 | 6916 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6917 | Return -1 on failure."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6918 |  | 
 | 6919 | static PyObject * | 
 | 6920 | unicode_rfind(PyUnicodeObject *self, PyObject *args) | 
 | 6921 | { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6922 |     PyObject *substring; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6923 |     Py_ssize_t start = 0; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6924 |     Py_ssize_t end = PY_SSIZE_T_MAX; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6925 |     Py_ssize_t result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6926 |  | 
| Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6927 |     if (!PyArg_ParseTuple(args, "O|O&O&:rfind", &substring, | 
 | 6928 | 		_PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6929 |         return NULL; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6930 |     substring = PyUnicode_FromObject(substring); | 
 | 6931 |     if (!substring) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6932 | 	return NULL; | 
 | 6933 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6934 |     result = stringlib_rfind_slice( | 
 | 6935 |         PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), | 
 | 6936 |         PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), | 
 | 6937 |         start, end | 
 | 6938 |         ); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6939 |  | 
 | 6940 |     Py_DECREF(substring); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6941 |  | 
 | 6942 |     return PyInt_FromSsize_t(result); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6943 | } | 
 | 6944 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6945 | PyDoc_STRVAR(rindex__doc__, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6946 | "S.rindex(sub [,start [,end]]) -> int\n\ | 
 | 6947 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6948 | 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] | 6949 |  | 
 | 6950 | static PyObject * | 
 | 6951 | unicode_rindex(PyUnicodeObject *self, PyObject *args) | 
 | 6952 | { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6953 |     PyObject *substring; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6954 |     Py_ssize_t start = 0; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6955 |     Py_ssize_t end = PY_SSIZE_T_MAX; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6956 |     Py_ssize_t result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6957 |  | 
| Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6958 |     if (!PyArg_ParseTuple(args, "O|O&O&:rindex", &substring, | 
 | 6959 | 		_PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6960 |         return NULL; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6961 |     substring = PyUnicode_FromObject(substring); | 
 | 6962 |     if (!substring) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6963 | 	return NULL; | 
 | 6964 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6965 |     result = stringlib_rfind_slice( | 
 | 6966 |         PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), | 
 | 6967 |         PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), | 
 | 6968 |         start, end | 
 | 6969 |         ); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6970 |  | 
 | 6971 |     Py_DECREF(substring); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6972 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6973 |     if (result < 0) { | 
 | 6974 |         PyErr_SetString(PyExc_ValueError, "substring not found"); | 
 | 6975 |         return NULL; | 
 | 6976 |     } | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6977 |     return PyInt_FromSsize_t(result); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6978 | } | 
 | 6979 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6980 | PyDoc_STRVAR(rjust__doc__, | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6981 | "S.rjust(width[, fillchar]) -> unicode\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6982 | \n\ | 
 | 6983 | 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] | 6984 | done using the specified fill character (default is a space)."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6985 |  | 
 | 6986 | static PyObject * | 
 | 6987 | unicode_rjust(PyUnicodeObject *self, PyObject *args) | 
 | 6988 | { | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6989 |     Py_ssize_t width; | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6990 |     Py_UNICODE fillchar = ' '; | 
 | 6991 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6992 |     if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6993 |         return NULL; | 
 | 6994 |  | 
| Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6995 |     if (self->length >= width && PyUnicode_CheckExact(self)) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6996 |         Py_INCREF(self); | 
 | 6997 |         return (PyObject*) self; | 
 | 6998 |     } | 
 | 6999 |  | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7000 |     return (PyObject*) pad(self, width - self->length, 0, fillchar); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7001 | } | 
 | 7002 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7003 | static PyObject* | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7004 | 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] | 7005 | { | 
 | 7006 |     /* standard clamping */ | 
 | 7007 |     if (start < 0) | 
 | 7008 |         start = 0; | 
 | 7009 |     if (end < 0) | 
 | 7010 |         end = 0; | 
 | 7011 |     if (end > self->length) | 
 | 7012 |         end = self->length; | 
| Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7013 |     if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7014 |         /* full slice, return original string */ | 
 | 7015 |         Py_INCREF(self); | 
 | 7016 |         return (PyObject*) self; | 
 | 7017 |     } | 
 | 7018 |     if (start > end) | 
 | 7019 |         start = end; | 
 | 7020 |     /* copy slice */ | 
 | 7021 |     return (PyObject*) PyUnicode_FromUnicode(self->str + start, | 
 | 7022 | 					     end - start); | 
 | 7023 | } | 
 | 7024 |  | 
 | 7025 | PyObject *PyUnicode_Split(PyObject *s, | 
 | 7026 | 			  PyObject *sep, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7027 | 			  Py_ssize_t maxsplit) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7028 | { | 
 | 7029 |     PyObject *result; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7030 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7031 |     s = PyUnicode_FromObject(s); | 
 | 7032 |     if (s == NULL) | 
 | 7033 | 	return NULL; | 
 | 7034 |     if (sep != NULL) { | 
 | 7035 | 	sep = PyUnicode_FromObject(sep); | 
 | 7036 | 	if (sep == NULL) { | 
 | 7037 | 	    Py_DECREF(s); | 
 | 7038 | 	    return NULL; | 
 | 7039 | 	} | 
 | 7040 |     } | 
 | 7041 |  | 
 | 7042 |     result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); | 
 | 7043 |  | 
 | 7044 |     Py_DECREF(s); | 
 | 7045 |     Py_XDECREF(sep); | 
 | 7046 |     return result; | 
 | 7047 | } | 
 | 7048 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7049 | PyDoc_STRVAR(split__doc__, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7050 | "S.split([sep [,maxsplit]]) -> list of strings\n\ | 
 | 7051 | \n\ | 
 | 7052 | Return a list of the words in S, using sep as the\n\ | 
 | 7053 | delimiter string.  If maxsplit is given, at most maxsplit\n\ | 
| Thomas Heller | ca0d2cb | 2004-09-15 11:41:32 +0000 | [diff] [blame] | 7054 | 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] | 7055 | any whitespace string is a separator."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7056 |  | 
 | 7057 | static PyObject* | 
 | 7058 | unicode_split(PyUnicodeObject *self, PyObject *args) | 
 | 7059 | { | 
 | 7060 |     PyObject *substring = Py_None; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7061 |     Py_ssize_t maxcount = -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7062 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7063 |     if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7064 |         return NULL; | 
 | 7065 |  | 
 | 7066 |     if (substring == Py_None) | 
 | 7067 | 	return split(self, NULL, maxcount); | 
 | 7068 |     else if (PyUnicode_Check(substring)) | 
 | 7069 | 	return split(self, (PyUnicodeObject *)substring, maxcount); | 
 | 7070 |     else | 
 | 7071 | 	return PyUnicode_Split((PyObject *)self, substring, maxcount); | 
 | 7072 | } | 
 | 7073 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7074 | PyObject * | 
 | 7075 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) | 
 | 7076 | { | 
 | 7077 |     PyObject* str_obj; | 
 | 7078 |     PyObject* sep_obj; | 
 | 7079 |     PyObject* out; | 
 | 7080 |  | 
 | 7081 |     str_obj = PyUnicode_FromObject(str_in); | 
 | 7082 |     if (!str_obj) | 
 | 7083 | 	return NULL; | 
 | 7084 |     sep_obj = PyUnicode_FromObject(sep_in); | 
 | 7085 |     if (!sep_obj) { | 
 | 7086 |         Py_DECREF(str_obj); | 
 | 7087 |         return NULL; | 
 | 7088 |     } | 
 | 7089 |  | 
 | 7090 |     out = stringlib_partition( | 
 | 7091 |         str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), | 
 | 7092 |         sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) | 
 | 7093 |         ); | 
 | 7094 |  | 
 | 7095 |     Py_DECREF(sep_obj); | 
 | 7096 |     Py_DECREF(str_obj); | 
 | 7097 |  | 
 | 7098 |     return out; | 
 | 7099 | } | 
 | 7100 |  | 
 | 7101 |  | 
 | 7102 | PyObject * | 
 | 7103 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) | 
 | 7104 | { | 
 | 7105 |     PyObject* str_obj; | 
 | 7106 |     PyObject* sep_obj; | 
 | 7107 |     PyObject* out; | 
 | 7108 |  | 
 | 7109 |     str_obj = PyUnicode_FromObject(str_in); | 
 | 7110 |     if (!str_obj) | 
 | 7111 | 	return NULL; | 
 | 7112 |     sep_obj = PyUnicode_FromObject(sep_in); | 
 | 7113 |     if (!sep_obj) { | 
 | 7114 |         Py_DECREF(str_obj); | 
 | 7115 |         return NULL; | 
 | 7116 |     } | 
 | 7117 |  | 
 | 7118 |     out = stringlib_rpartition( | 
 | 7119 |         str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), | 
 | 7120 |         sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) | 
 | 7121 |         ); | 
 | 7122 |  | 
 | 7123 |     Py_DECREF(sep_obj); | 
 | 7124 |     Py_DECREF(str_obj); | 
 | 7125 |  | 
 | 7126 |     return out; | 
 | 7127 | } | 
 | 7128 |  | 
 | 7129 | PyDoc_STRVAR(partition__doc__, | 
 | 7130 | "S.partition(sep) -> (head, sep, tail)\n\ | 
 | 7131 | \n\ | 
 | 7132 | Searches for the separator sep in S, and returns the part before it,\n\ | 
 | 7133 | the separator itself, and the part after it.  If the separator is not\n\ | 
 | 7134 | found, returns S and two empty strings."); | 
 | 7135 |  | 
 | 7136 | static PyObject* | 
 | 7137 | unicode_partition(PyUnicodeObject *self, PyObject *separator) | 
 | 7138 | { | 
 | 7139 |     return PyUnicode_Partition((PyObject *)self, separator); | 
 | 7140 | } | 
 | 7141 |  | 
 | 7142 | PyDoc_STRVAR(rpartition__doc__, | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7143 | "S.rpartition(sep) -> (tail, sep, head)\n\ | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7144 | \n\ | 
 | 7145 | Searches for the separator sep in S, starting at the end of S, and returns\n\ | 
 | 7146 | 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] | 7147 | separator is not found, returns two empty strings and S."); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7148 |  | 
 | 7149 | static PyObject* | 
 | 7150 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) | 
 | 7151 | { | 
 | 7152 |     return PyUnicode_RPartition((PyObject *)self, separator); | 
 | 7153 | } | 
 | 7154 |  | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7155 | PyObject *PyUnicode_RSplit(PyObject *s, | 
 | 7156 | 			   PyObject *sep, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7157 | 			   Py_ssize_t maxsplit) | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7158 | { | 
 | 7159 |     PyObject *result; | 
 | 7160 |      | 
 | 7161 |     s = PyUnicode_FromObject(s); | 
 | 7162 |     if (s == NULL) | 
 | 7163 | 	return NULL; | 
 | 7164 |     if (sep != NULL) { | 
 | 7165 | 	sep = PyUnicode_FromObject(sep); | 
 | 7166 | 	if (sep == NULL) { | 
 | 7167 | 	    Py_DECREF(s); | 
 | 7168 | 	    return NULL; | 
 | 7169 | 	} | 
 | 7170 |     } | 
 | 7171 |  | 
 | 7172 |     result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); | 
 | 7173 |  | 
 | 7174 |     Py_DECREF(s); | 
 | 7175 |     Py_XDECREF(sep); | 
 | 7176 |     return result; | 
 | 7177 | } | 
 | 7178 |  | 
 | 7179 | PyDoc_STRVAR(rsplit__doc__, | 
 | 7180 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ | 
 | 7181 | \n\ | 
 | 7182 | Return a list of the words in S, using sep as the\n\ | 
 | 7183 | delimiter string, starting at the end of the string and\n\ | 
 | 7184 | working to the front.  If maxsplit is given, at most maxsplit\n\ | 
 | 7185 | splits are done. If sep is not specified, any whitespace string\n\ | 
 | 7186 | is a separator."); | 
 | 7187 |  | 
 | 7188 | static PyObject* | 
 | 7189 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) | 
 | 7190 | { | 
 | 7191 |     PyObject *substring = Py_None; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7192 |     Py_ssize_t maxcount = -1; | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7193 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7194 |     if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7195 |         return NULL; | 
 | 7196 |  | 
 | 7197 |     if (substring == Py_None) | 
 | 7198 | 	return rsplit(self, NULL, maxcount); | 
 | 7199 |     else if (PyUnicode_Check(substring)) | 
 | 7200 | 	return rsplit(self, (PyUnicodeObject *)substring, maxcount); | 
 | 7201 |     else | 
 | 7202 | 	return PyUnicode_RSplit((PyObject *)self, substring, maxcount); | 
 | 7203 | } | 
 | 7204 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7205 | PyDoc_STRVAR(splitlines__doc__, | 
| Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7206 | "S.splitlines([keepends]]) -> list of strings\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7207 | \n\ | 
 | 7208 | 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] | 7209 | 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] | 7210 | is given and true."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7211 |  | 
 | 7212 | static PyObject* | 
 | 7213 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) | 
 | 7214 | { | 
| Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7215 |     int keepends = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7216 |  | 
| Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7217 |     if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7218 |         return NULL; | 
 | 7219 |  | 
| Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7220 |     return PyUnicode_Splitlines((PyObject *)self, keepends); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7221 | } | 
 | 7222 |  | 
 | 7223 | static | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 7224 | PyObject *unicode_str(PyObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7225 | { | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 7226 |     PyObject *res = _PyUnicode_AsDefaultEncodedString(self, NULL); | 
 | 7227 |     Py_XINCREF(res); | 
 | 7228 |     return res; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7229 | } | 
 | 7230 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7231 | PyDoc_STRVAR(swapcase__doc__, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7232 | "S.swapcase() -> unicode\n\ | 
 | 7233 | \n\ | 
 | 7234 | 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] | 7235 | and vice versa."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7236 |  | 
 | 7237 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7238 | unicode_swapcase(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7239 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7240 |     return fixup(self, fixswapcase); | 
 | 7241 | } | 
 | 7242 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7243 | PyDoc_STRVAR(translate__doc__, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7244 | "S.translate(table) -> unicode\n\ | 
 | 7245 | \n\ | 
 | 7246 | Return a copy of the string S, where all characters have been mapped\n\ | 
 | 7247 | 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] | 7248 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ | 
 | 7249 | Unmapped characters are left untouched. Characters mapped to None\n\ | 
 | 7250 | are deleted."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7251 |  | 
 | 7252 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7253 | unicode_translate(PyUnicodeObject *self, PyObject *table) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7254 | { | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7255 |     return PyUnicode_TranslateCharmap(self->str, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7256 | 				      self->length, | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7257 | 				      table, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7258 | 				      "ignore"); | 
 | 7259 | } | 
 | 7260 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7261 | PyDoc_STRVAR(upper__doc__, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7262 | "S.upper() -> unicode\n\ | 
 | 7263 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7264 | Return a copy of S converted to uppercase."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7265 |  | 
 | 7266 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7267 | unicode_upper(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7268 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7269 |     return fixup(self, fixupper); | 
 | 7270 | } | 
 | 7271 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7272 | PyDoc_STRVAR(zfill__doc__, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7273 | "S.zfill(width) -> unicode\n\ | 
 | 7274 | \n\ | 
 | 7275 | 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] | 7276 | of the specified width. The string x is never truncated."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7277 |  | 
 | 7278 | static PyObject * | 
 | 7279 | unicode_zfill(PyUnicodeObject *self, PyObject *args) | 
 | 7280 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7281 |     Py_ssize_t fill; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7282 |     PyUnicodeObject *u; | 
 | 7283 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7284 |     Py_ssize_t width; | 
 | 7285 |     if (!PyArg_ParseTuple(args, "n:zfill", &width)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7286 |         return NULL; | 
 | 7287 |  | 
 | 7288 |     if (self->length >= width) { | 
| Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 7289 |         if (PyUnicode_CheckExact(self)) { | 
 | 7290 |             Py_INCREF(self); | 
 | 7291 |             return (PyObject*) self; | 
 | 7292 |         } | 
 | 7293 |         else | 
 | 7294 |             return PyUnicode_FromUnicode( | 
 | 7295 |                 PyUnicode_AS_UNICODE(self), | 
 | 7296 |                 PyUnicode_GET_SIZE(self) | 
 | 7297 |             ); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7298 |     } | 
 | 7299 |  | 
 | 7300 |     fill = width - self->length; | 
 | 7301 |  | 
 | 7302 |     u = pad(self, fill, 0, '0'); | 
 | 7303 |  | 
| Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 7304 |     if (u == NULL) | 
 | 7305 |         return NULL; | 
 | 7306 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7307 |     if (u->str[fill] == '+' || u->str[fill] == '-') { | 
 | 7308 |         /* move sign to beginning of string */ | 
 | 7309 |         u->str[0] = u->str[fill]; | 
 | 7310 |         u->str[fill] = '0'; | 
 | 7311 |     } | 
 | 7312 |  | 
 | 7313 |     return (PyObject*) u; | 
 | 7314 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7315 |  | 
 | 7316 | #if 0 | 
 | 7317 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7318 | unicode_freelistsize(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7319 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7320 |     return PyInt_FromLong(unicode_freelist_size); | 
 | 7321 | } | 
 | 7322 | #endif | 
 | 7323 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7324 | PyDoc_STRVAR(startswith__doc__, | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7325 | "S.startswith(prefix[, start[, end]]) -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7326 | \n\ | 
| Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7327 | Return True if S starts with the specified prefix, False otherwise.\n\ | 
 | 7328 | With optional start, test S beginning at that position.\n\ | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7329 | With optional end, stop comparing S at that position.\n\ | 
 | 7330 | prefix can also be a tuple of strings to try."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7331 |  | 
 | 7332 | static PyObject * | 
 | 7333 | unicode_startswith(PyUnicodeObject *self, | 
 | 7334 | 		   PyObject *args) | 
 | 7335 | { | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7336 |     PyObject *subobj; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7337 |     PyUnicodeObject *substring; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7338 |     Py_ssize_t start = 0; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7339 |     Py_ssize_t end = PY_SSIZE_T_MAX; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7340 |     int result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7341 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7342 |     if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, | 
| Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7343 | 		_PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7344 | 	return NULL; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7345 |     if (PyTuple_Check(subobj)) { | 
 | 7346 |         Py_ssize_t i; | 
 | 7347 |         for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { | 
 | 7348 |             substring = (PyUnicodeObject *)PyUnicode_FromObject( | 
 | 7349 |                             PyTuple_GET_ITEM(subobj, i)); | 
 | 7350 |             if (substring == NULL) | 
 | 7351 |                 return NULL; | 
 | 7352 |             result = tailmatch(self, substring, start, end, -1); | 
 | 7353 |             Py_DECREF(substring); | 
 | 7354 |             if (result) { | 
 | 7355 |                 Py_RETURN_TRUE; | 
 | 7356 |             } | 
 | 7357 |         } | 
 | 7358 |         /* nothing matched */ | 
 | 7359 |         Py_RETURN_FALSE; | 
 | 7360 |     } | 
 | 7361 |     substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7362 |     if (substring == NULL) | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7363 |          return NULL; | 
 | 7364 |     result = tailmatch(self, substring, start, end, -1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7365 |     Py_DECREF(substring); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7366 |     return PyBool_FromLong(result); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7367 | } | 
 | 7368 |  | 
 | 7369 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7370 | PyDoc_STRVAR(endswith__doc__, | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7371 | "S.endswith(suffix[, start[, end]]) -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7372 | \n\ | 
| Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7373 | Return True if S ends with the specified suffix, False otherwise.\n\ | 
 | 7374 | With optional start, test S beginning at that position.\n\ | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7375 | With optional end, stop comparing S at that position.\n\ | 
 | 7376 | suffix can also be a tuple of strings to try."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7377 |  | 
 | 7378 | static PyObject * | 
 | 7379 | unicode_endswith(PyUnicodeObject *self, | 
 | 7380 | 		 PyObject *args) | 
 | 7381 | { | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7382 |     PyObject *subobj; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7383 |     PyUnicodeObject *substring; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7384 |     Py_ssize_t start = 0; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7385 |     Py_ssize_t end = PY_SSIZE_T_MAX; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7386 |     int result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7387 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7388 |     if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, | 
 | 7389 |         _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7390 | 	return NULL; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7391 |     if (PyTuple_Check(subobj)) { | 
 | 7392 |         Py_ssize_t i; | 
 | 7393 |         for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { | 
 | 7394 |             substring = (PyUnicodeObject *)PyUnicode_FromObject( | 
 | 7395 |                             PyTuple_GET_ITEM(subobj, i)); | 
 | 7396 |             if (substring == NULL) | 
 | 7397 |             return NULL; | 
 | 7398 |             result = tailmatch(self, substring, start, end, +1); | 
 | 7399 |             Py_DECREF(substring); | 
 | 7400 |             if (result) { | 
 | 7401 |                 Py_RETURN_TRUE; | 
 | 7402 |             } | 
 | 7403 |         } | 
 | 7404 |         Py_RETURN_FALSE; | 
 | 7405 |     } | 
 | 7406 |     substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7407 |     if (substring == NULL) | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7408 |     return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7409 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7410 |     result = tailmatch(self, substring, start, end, +1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7411 |     Py_DECREF(substring); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7412 |     return PyBool_FromLong(result); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7413 | } | 
 | 7414 |  | 
 | 7415 |  | 
| Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 7416 |  | 
 | 7417 | static PyObject * | 
 | 7418 | unicode_getnewargs(PyUnicodeObject *v) | 
 | 7419 | { | 
 | 7420 | 	return Py_BuildValue("(u#)", v->str, v->length); | 
 | 7421 | } | 
 | 7422 |  | 
 | 7423 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7424 | static PyMethodDef unicode_methods[] = { | 
 | 7425 |  | 
 | 7426 |     /* Order is according to common usage: often used methods should | 
 | 7427 |        appear first, since lookup is done sequentially. */ | 
 | 7428 |  | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7429 |     {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, | 
 | 7430 |     {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, | 
 | 7431 |     {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7432 |     {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7433 |     {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, | 
 | 7434 |     {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, | 
 | 7435 |     {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, | 
 | 7436 |     {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, | 
 | 7437 |     {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, | 
 | 7438 |     {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, | 
 | 7439 |     {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7440 |     {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7441 |     {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, | 
 | 7442 |     {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, | 
 | 7443 |     {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7444 |     {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7445 |     {"decode", (PyCFunction) unicode_decode, METH_VARARGS, decode__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7446 | /*  {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */ | 
 | 7447 |     {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, | 
 | 7448 |     {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, | 
 | 7449 |     {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7450 |     {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7451 |     {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7452 |     {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7453 |     {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7454 |     {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, | 
 | 7455 |     {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, | 
 | 7456 |     {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, | 
 | 7457 |     {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, | 
 | 7458 |     {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, | 
 | 7459 |     {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, | 
 | 7460 |     {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, | 
 | 7461 |     {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, | 
 | 7462 |     {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, | 
 | 7463 |     {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, | 
 | 7464 |     {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, | 
 | 7465 |     {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, | 
 | 7466 |     {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, | 
 | 7467 |     {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7468 |     {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, | 
| Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 7469 | #if 0 | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7470 |     {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7471 | #endif | 
 | 7472 |  | 
 | 7473 | #if 0 | 
 | 7474 |     /* This one is just used for debugging the implementation. */ | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7475 |     {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7476 | #endif | 
 | 7477 |  | 
| Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 7478 |     {"__getnewargs__",	(PyCFunction)unicode_getnewargs, METH_NOARGS}, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7479 |     {NULL, NULL} | 
 | 7480 | }; | 
 | 7481 |  | 
| Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7482 | static PyObject * | 
 | 7483 | unicode_mod(PyObject *v, PyObject *w) | 
 | 7484 | { | 
 | 7485 |        if (!PyUnicode_Check(v)) { | 
 | 7486 |                Py_INCREF(Py_NotImplemented); | 
 | 7487 |                return Py_NotImplemented; | 
 | 7488 |        } | 
 | 7489 |        return PyUnicode_Format(v, w); | 
 | 7490 | } | 
 | 7491 |  | 
 | 7492 | static PyNumberMethods unicode_as_number = { | 
 | 7493 | 	0,				/*nb_add*/ | 
 | 7494 | 	0,				/*nb_subtract*/ | 
 | 7495 | 	0,				/*nb_multiply*/ | 
| Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7496 | 	unicode_mod,			/*nb_remainder*/ | 
 | 7497 | }; | 
 | 7498 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7499 | static PySequenceMethods unicode_as_sequence = { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7500 |     (lenfunc) unicode_length, 		/* sq_length */ | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7501 |     PyUnicode_Concat,		 	/* sq_concat */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7502 |     (ssizeargfunc) unicode_repeat, 	/* sq_repeat */ | 
 | 7503 |     (ssizeargfunc) unicode_getitem, 	/* sq_item */ | 
 | 7504 |     (ssizessizeargfunc) unicode_slice, 	/* sq_slice */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7505 |     0, 					/* sq_ass_item */ | 
 | 7506 |     0, 					/* sq_ass_slice */ | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7507 |     PyUnicode_Contains, 		/* sq_contains */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7508 | }; | 
 | 7509 |  | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7510 | static PyObject* | 
 | 7511 | unicode_subscript(PyUnicodeObject* self, PyObject* item) | 
 | 7512 | { | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7513 |     if (PyIndex_Check(item)) { | 
 | 7514 |         Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7515 |         if (i == -1 && PyErr_Occurred()) | 
 | 7516 |             return NULL; | 
 | 7517 |         if (i < 0) | 
| Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7518 |             i += PyUnicode_GET_SIZE(self); | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7519 |         return unicode_getitem(self, i); | 
 | 7520 |     } else if (PySlice_Check(item)) { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7521 |         Py_ssize_t start, stop, step, slicelength, cur, i; | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7522 |         Py_UNICODE* source_buf; | 
 | 7523 |         Py_UNICODE* result_buf; | 
 | 7524 |         PyObject* result; | 
 | 7525 |  | 
| Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7526 |         if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7527 | 				 &start, &stop, &step, &slicelength) < 0) { | 
 | 7528 |             return NULL; | 
 | 7529 |         } | 
 | 7530 |  | 
 | 7531 |         if (slicelength <= 0) { | 
 | 7532 |             return PyUnicode_FromUnicode(NULL, 0); | 
 | 7533 |         } else { | 
 | 7534 |             source_buf = PyUnicode_AS_UNICODE((PyObject*)self); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7535 |             result_buf = (Py_UNICODE *)PyMem_MALLOC(slicelength* | 
 | 7536 |                                                     sizeof(Py_UNICODE)); | 
| Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7537 | 	     | 
 | 7538 | 	    if (result_buf == NULL) | 
 | 7539 | 		    return PyErr_NoMemory(); | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7540 |  | 
 | 7541 |             for (cur = start, i = 0; i < slicelength; cur += step, i++) { | 
 | 7542 |                 result_buf[i] = source_buf[cur]; | 
 | 7543 |             } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7544 |  | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7545 |             result = PyUnicode_FromUnicode(result_buf, slicelength); | 
 | 7546 |             PyMem_FREE(result_buf); | 
 | 7547 |             return result; | 
 | 7548 |         } | 
 | 7549 |     } else { | 
 | 7550 |         PyErr_SetString(PyExc_TypeError, "string indices must be integers"); | 
 | 7551 |         return NULL; | 
 | 7552 |     } | 
 | 7553 | } | 
 | 7554 |  | 
 | 7555 | static PyMappingMethods unicode_as_mapping = { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7556 |     (lenfunc)unicode_length,		/* mp_length */ | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7557 |     (binaryfunc)unicode_subscript,	/* mp_subscript */ | 
 | 7558 |     (objobjargproc)0,			/* mp_ass_subscript */ | 
 | 7559 | }; | 
 | 7560 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7561 | static Py_ssize_t | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7562 | unicode_buffer_getreadbuf(PyUnicodeObject *self, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7563 | 			  Py_ssize_t index, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7564 | 			  const void **ptr) | 
 | 7565 | { | 
 | 7566 |     if (index != 0) { | 
 | 7567 |         PyErr_SetString(PyExc_SystemError, | 
 | 7568 | 			"accessing non-existent unicode segment"); | 
 | 7569 |         return -1; | 
 | 7570 |     } | 
 | 7571 |     *ptr = (void *) self->str; | 
 | 7572 |     return PyUnicode_GET_DATA_SIZE(self); | 
 | 7573 | } | 
 | 7574 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7575 | static Py_ssize_t | 
 | 7576 | unicode_buffer_getwritebuf(PyUnicodeObject *self, Py_ssize_t index, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7577 | 			   const void **ptr) | 
 | 7578 | { | 
 | 7579 |     PyErr_SetString(PyExc_TypeError, | 
| Neal Norwitz | 20e7213 | 2002-06-13 21:25:17 +0000 | [diff] [blame] | 7580 | 		    "cannot use unicode as modifiable buffer"); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7581 |     return -1; | 
 | 7582 | } | 
 | 7583 |  | 
 | 7584 | static int | 
 | 7585 | unicode_buffer_getsegcount(PyUnicodeObject *self, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7586 | 			   Py_ssize_t *lenp) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7587 | { | 
 | 7588 |     if (lenp) | 
 | 7589 |         *lenp = PyUnicode_GET_DATA_SIZE(self); | 
 | 7590 |     return 1; | 
 | 7591 | } | 
 | 7592 |  | 
| Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 7593 | static Py_ssize_t | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7594 | unicode_buffer_getcharbuf(PyUnicodeObject *self, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7595 | 			  Py_ssize_t index, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7596 | 			  const void **ptr) | 
 | 7597 | { | 
 | 7598 |     PyObject *str; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7599 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7600 |     if (index != 0) { | 
 | 7601 |         PyErr_SetString(PyExc_SystemError, | 
 | 7602 | 			"accessing non-existent unicode segment"); | 
 | 7603 |         return -1; | 
 | 7604 |     } | 
| Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 7605 |     str = _PyUnicode_AsDefaultEncodedString((PyObject *)self, NULL); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7606 |     if (str == NULL) | 
 | 7607 | 	return -1; | 
 | 7608 |     *ptr = (void *) PyString_AS_STRING(str); | 
 | 7609 |     return PyString_GET_SIZE(str); | 
 | 7610 | } | 
 | 7611 |  | 
 | 7612 | /* Helpers for PyUnicode_Format() */ | 
 | 7613 |  | 
 | 7614 | static PyObject * | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7615 | 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] | 7616 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7617 |     Py_ssize_t argidx = *p_argidx; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7618 |     if (argidx < arglen) { | 
 | 7619 | 	(*p_argidx)++; | 
 | 7620 | 	if (arglen < 0) | 
 | 7621 | 	    return args; | 
 | 7622 | 	else | 
 | 7623 | 	    return PyTuple_GetItem(args, argidx); | 
 | 7624 |     } | 
 | 7625 |     PyErr_SetString(PyExc_TypeError, | 
 | 7626 | 		    "not enough arguments for format string"); | 
 | 7627 |     return NULL; | 
 | 7628 | } | 
 | 7629 |  | 
 | 7630 | #define F_LJUST (1<<0) | 
 | 7631 | #define F_SIGN	(1<<1) | 
 | 7632 | #define F_BLANK (1<<2) | 
 | 7633 | #define F_ALT	(1<<3) | 
 | 7634 | #define F_ZERO	(1<<4) | 
 | 7635 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7636 | static Py_ssize_t | 
| Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7637 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7638 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7639 |     register Py_ssize_t i; | 
 | 7640 |     Py_ssize_t len = strlen(charbuffer); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7641 |     for (i = len - 1; i >= 0; i--) | 
 | 7642 | 	buffer[i] = (Py_UNICODE) charbuffer[i]; | 
 | 7643 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7644 |     return len; | 
 | 7645 | } | 
 | 7646 |  | 
| Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7647 | static int | 
 | 7648 | doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) | 
 | 7649 | { | 
| Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 7650 |     Py_ssize_t result; | 
 | 7651 |  | 
| Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7652 |     PyOS_ascii_formatd((char *)buffer, len, format, x); | 
| Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 7653 |     result = strtounicode(buffer, (char *)buffer); | 
 | 7654 |     return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); | 
| Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7655 | } | 
 | 7656 |  | 
 | 7657 | static int | 
 | 7658 | longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) | 
 | 7659 | { | 
| Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 7660 |     Py_ssize_t result; | 
 | 7661 |  | 
| Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7662 |     PyOS_snprintf((char *)buffer, len, format, x); | 
| Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 7663 |     result = strtounicode(buffer, (char *)buffer); | 
 | 7664 |     return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); | 
| Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7665 | } | 
 | 7666 |  | 
| Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 7667 | /* XXX To save some code duplication, formatfloat/long/int could have been | 
 | 7668 |    shared with stringobject.c, converting from 8-bit to Unicode after the | 
 | 7669 |    formatting is done. */ | 
 | 7670 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7671 | static int | 
 | 7672 | formatfloat(Py_UNICODE *buf, | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7673 | 	    size_t buflen, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7674 | 	    int flags, | 
 | 7675 | 	    int prec, | 
 | 7676 | 	    int type, | 
 | 7677 | 	    PyObject *v) | 
 | 7678 | { | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7679 |     /* fmt = '%#.' + `prec` + `type` | 
 | 7680 |        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] | 7681 |     char fmt[20]; | 
 | 7682 |     double x; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7683 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7684 |     x = PyFloat_AsDouble(v); | 
 | 7685 |     if (x == -1.0 && PyErr_Occurred()) | 
 | 7686 | 	return -1; | 
 | 7687 |     if (prec < 0) | 
 | 7688 | 	prec = 6; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7689 |     if (type == 'f' && (fabs(x) / 1e25) >= 1e25) | 
 | 7690 | 	type = 'g'; | 
| Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7691 |     /* Worst case length calc to ensure no buffer overrun: | 
 | 7692 |  | 
 | 7693 |        'g' formats: | 
 | 7694 | 	 fmt = %#.<prec>g | 
 | 7695 | 	 buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp | 
 | 7696 | 	    for any double rep.) | 
 | 7697 | 	 len = 1 + prec + 1 + 2 + 5 = 9 + prec | 
 | 7698 |  | 
 | 7699 |        'f' formats: | 
 | 7700 | 	 buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) | 
 | 7701 | 	 len = 1 + 50 + 1 + prec = 52 + prec | 
 | 7702 |  | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7703 |        If prec=0 the effective precision is 1 (the leading digit is | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7704 |        always given), therefore increase the length by one. | 
| Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7705 |  | 
 | 7706 |     */ | 
 | 7707 |     if ((type == 'g' && buflen <= (size_t)10 + (size_t)prec) || | 
 | 7708 | 	(type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7709 | 	PyErr_SetString(PyExc_OverflowError, | 
| Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7710 | 			"formatted float is too long (precision too large?)"); | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7711 | 	return -1; | 
 | 7712 |     } | 
| Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7713 |     PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", | 
 | 7714 | 		  (flags&F_ALT) ? "#" : "", | 
 | 7715 | 		  prec, type); | 
| Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7716 |     return doubletounicode(buf, buflen, fmt, x); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7717 | } | 
 | 7718 |  | 
| Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7719 | static PyObject* | 
 | 7720 | formatlong(PyObject *val, int flags, int prec, int type) | 
 | 7721 | { | 
 | 7722 | 	char *buf; | 
 | 7723 | 	int i, len; | 
 | 7724 | 	PyObject *str; /* temporary string object. */ | 
 | 7725 | 	PyUnicodeObject *result; | 
 | 7726 |  | 
 | 7727 | 	str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); | 
 | 7728 | 	if (!str) | 
 | 7729 | 		return NULL; | 
 | 7730 | 	result = _PyUnicode_New(len); | 
| Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 7731 | 	if (!result) { | 
 | 7732 | 		Py_DECREF(str); | 
 | 7733 | 		return NULL; | 
 | 7734 | 	} | 
| Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7735 | 	for (i = 0; i < len; i++) | 
 | 7736 | 		result->str[i] = buf[i]; | 
 | 7737 | 	result->str[len] = 0; | 
 | 7738 | 	Py_DECREF(str); | 
 | 7739 | 	return (PyObject*)result; | 
 | 7740 | } | 
 | 7741 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7742 | static int | 
 | 7743 | formatint(Py_UNICODE *buf, | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7744 | 	  size_t buflen, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7745 | 	  int flags, | 
 | 7746 | 	  int prec, | 
 | 7747 | 	  int type, | 
 | 7748 | 	  PyObject *v) | 
 | 7749 | { | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7750 |     /* fmt = '%#.' + `prec` + 'l' + `type` | 
| Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7751 |      * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) | 
 | 7752 |      *                     + 1 + 1 | 
 | 7753 |      *                   = 24 | 
 | 7754 |      */ | 
| Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7755 |     char fmt[64]; /* plenty big enough! */ | 
| Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7756 |     char *sign; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7757 |     long x; | 
 | 7758 |  | 
 | 7759 |     x = PyInt_AsLong(v); | 
 | 7760 |     if (x == -1 && PyErr_Occurred()) | 
| Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7761 |         return -1; | 
| Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7762 |     if (x < 0 && type == 'u') { | 
 | 7763 |         type = 'd'; | 
| Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 7764 |     } | 
| Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7765 |     if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) | 
 | 7766 |         sign = "-"; | 
 | 7767 |     else | 
 | 7768 |         sign = ""; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7769 |     if (prec < 0) | 
| Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7770 |         prec = 1; | 
 | 7771 |  | 
| Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7772 |     /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) | 
 | 7773 |      * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 | 
| Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7774 |      */ | 
| Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7775 |     if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7776 |         PyErr_SetString(PyExc_OverflowError, | 
| Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7777 |     	        "formatted integer is too long (precision too large?)"); | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7778 |         return -1; | 
 | 7779 |     } | 
| Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7780 |  | 
 | 7781 |     if ((flags & F_ALT) && | 
 | 7782 |         (type == 'x' || type == 'X')) { | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7783 |         /* When converting under %#x or %#X, there are a number | 
| Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7784 |          * of issues that cause pain: | 
 | 7785 |          * - when 0 is being converted, the C standard leaves off | 
 | 7786 |          *   the '0x' or '0X', which is inconsistent with other | 
 | 7787 |          *   %#x/%#X conversions and inconsistent with Python's | 
 | 7788 |          *   hex() function | 
 | 7789 |          * - there are platforms that violate the standard and | 
 | 7790 |          *   convert 0 with the '0x' or '0X' | 
 | 7791 |          *   (Metrowerks, Compaq Tru64) | 
 | 7792 |          * - there are platforms that give '0x' when converting | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7793 |          *   under %#X, but convert 0 in accordance with the | 
| Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7794 |          *   standard (OS/2 EMX) | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7795 |          * | 
| Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7796 |          * We can achieve the desired consistency by inserting our | 
 | 7797 |          * own '0x' or '0X' prefix, and substituting %x/%X in place | 
 | 7798 |          * of %#x/%#X. | 
 | 7799 |          * | 
 | 7800 |          * Note that this is the same approach as used in | 
 | 7801 |          * formatint() in stringobject.c | 
| Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 7802 |          */ | 
| Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7803 |         PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", | 
 | 7804 |                       sign, type, prec, type); | 
| Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 7805 |     } | 
| Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7806 |     else { | 
| Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7807 |         PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", | 
 | 7808 |                       sign, (flags&F_ALT) ? "#" : "", | 
| Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7809 |                       prec, type); | 
| Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 7810 |     } | 
| Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7811 |     if (sign[0]) | 
| Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7812 |         return longtounicode(buf, buflen, fmt, -x); | 
| Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7813 |     else | 
| Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7814 |         return longtounicode(buf, buflen, fmt, x); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7815 | } | 
 | 7816 |  | 
 | 7817 | static int | 
 | 7818 | formatchar(Py_UNICODE *buf, | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7819 |            size_t buflen, | 
 | 7820 |            PyObject *v) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7821 | { | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7822 |     /* presume that the buffer is at least 2 characters long */ | 
| Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7823 |     if (PyUnicode_Check(v)) { | 
 | 7824 | 	if (PyUnicode_GET_SIZE(v) != 1) | 
 | 7825 | 	    goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7826 | 	buf[0] = PyUnicode_AS_UNICODE(v)[0]; | 
| Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7827 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7828 |  | 
| Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7829 |     else if (PyString_Check(v)) { | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7830 | 	if (PyString_GET_SIZE(v) != 1) | 
| Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7831 | 	    goto onError; | 
 | 7832 | 	buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0]; | 
 | 7833 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7834 |  | 
 | 7835 |     else { | 
 | 7836 | 	/* Integer input truncated to a character */ | 
 | 7837 |         long x; | 
 | 7838 | 	x = PyInt_AsLong(v); | 
 | 7839 | 	if (x == -1 && PyErr_Occurred()) | 
| Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7840 | 	    goto onError; | 
| Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 7841 | #ifdef Py_UNICODE_WIDE | 
 | 7842 | 	if (x < 0 || x > 0x10ffff) { | 
| Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 7843 | 	    PyErr_SetString(PyExc_OverflowError, | 
| Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 7844 | 			    "%c arg not in range(0x110000) " | 
 | 7845 | 			    "(wide Python build)"); | 
 | 7846 | 	    return -1; | 
 | 7847 | 	} | 
 | 7848 | #else | 
 | 7849 | 	if (x < 0 || x > 0xffff) { | 
| Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 7850 | 	    PyErr_SetString(PyExc_OverflowError, | 
| Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 7851 | 			    "%c arg not in range(0x10000) " | 
 | 7852 | 			    "(narrow Python build)"); | 
 | 7853 | 	    return -1; | 
 | 7854 | 	} | 
 | 7855 | #endif | 
 | 7856 | 	buf[0] = (Py_UNICODE) x; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7857 |     } | 
 | 7858 |     buf[1] = '\0'; | 
 | 7859 |     return 1; | 
| Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7860 |  | 
 | 7861 |  onError: | 
 | 7862 |     PyErr_SetString(PyExc_TypeError, | 
 | 7863 | 		    "%c requires int or char"); | 
 | 7864 |     return -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7865 | } | 
 | 7866 |  | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7867 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) | 
 | 7868 |  | 
 | 7869 |    FORMATBUFLEN is the length of the buffer in which the floats, ints, & | 
 | 7870 |    chars are formatted. XXX This is a magic number. Each formatting | 
 | 7871 |    routine does bounds checking to ensure no overflow, but a better | 
 | 7872 |    solution may be to malloc a buffer of appropriate size for each | 
 | 7873 |    format. For now, the current solution is sufficient. | 
 | 7874 | */ | 
 | 7875 | #define FORMATBUFLEN (size_t)120 | 
 | 7876 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7877 | PyObject *PyUnicode_Format(PyObject *format, | 
 | 7878 | 			   PyObject *args) | 
 | 7879 | { | 
 | 7880 |     Py_UNICODE *fmt, *res; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7881 |     Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7882 |     int args_owned = 0; | 
 | 7883 |     PyUnicodeObject *result = NULL; | 
 | 7884 |     PyObject *dict = NULL; | 
 | 7885 |     PyObject *uformat; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7886 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7887 |     if (format == NULL || args == NULL) { | 
 | 7888 | 	PyErr_BadInternalCall(); | 
 | 7889 | 	return NULL; | 
 | 7890 |     } | 
 | 7891 |     uformat = PyUnicode_FromObject(format); | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7892 |     if (uformat == NULL) | 
 | 7893 | 	return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7894 |     fmt = PyUnicode_AS_UNICODE(uformat); | 
 | 7895 |     fmtcnt = PyUnicode_GET_SIZE(uformat); | 
 | 7896 |  | 
 | 7897 |     reslen = rescnt = fmtcnt + 100; | 
 | 7898 |     result = _PyUnicode_New(reslen); | 
 | 7899 |     if (result == NULL) | 
 | 7900 | 	goto onError; | 
 | 7901 |     res = PyUnicode_AS_UNICODE(result); | 
 | 7902 |  | 
 | 7903 |     if (PyTuple_Check(args)) { | 
 | 7904 | 	arglen = PyTuple_Size(args); | 
 | 7905 | 	argidx = 0; | 
 | 7906 |     } | 
 | 7907 |     else { | 
 | 7908 | 	arglen = -1; | 
 | 7909 | 	argidx = -2; | 
 | 7910 |     } | 
| Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 7911 |     if (args->ob_type->tp_as_mapping && !PyTuple_Check(args) && | 
 | 7912 |         !PyObject_TypeCheck(args, &PyBaseString_Type)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7913 | 	dict = args; | 
 | 7914 |  | 
 | 7915 |     while (--fmtcnt >= 0) { | 
 | 7916 | 	if (*fmt != '%') { | 
 | 7917 | 	    if (--rescnt < 0) { | 
 | 7918 | 		rescnt = fmtcnt + 100; | 
 | 7919 | 		reslen += rescnt; | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7920 | 		if (_PyUnicode_Resize(&result, reslen) < 0) | 
| Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 7921 | 		    goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7922 | 		res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; | 
 | 7923 | 		--rescnt; | 
 | 7924 | 	    } | 
 | 7925 | 	    *res++ = *fmt++; | 
 | 7926 | 	} | 
 | 7927 | 	else { | 
 | 7928 | 	    /* Got a format specifier */ | 
 | 7929 | 	    int flags = 0; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7930 | 	    Py_ssize_t width = -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7931 | 	    int prec = -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7932 | 	    Py_UNICODE c = '\0'; | 
 | 7933 | 	    Py_UNICODE fill; | 
 | 7934 | 	    PyObject *v = NULL; | 
 | 7935 | 	    PyObject *temp = NULL; | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7936 | 	    Py_UNICODE *pbuf; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7937 | 	    Py_UNICODE sign; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7938 | 	    Py_ssize_t len; | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7939 | 	    Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7940 |  | 
 | 7941 | 	    fmt++; | 
 | 7942 | 	    if (*fmt == '(') { | 
 | 7943 | 		Py_UNICODE *keystart; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7944 | 		Py_ssize_t keylen; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7945 | 		PyObject *key; | 
 | 7946 | 		int pcount = 1; | 
 | 7947 |  | 
 | 7948 | 		if (dict == NULL) { | 
 | 7949 | 		    PyErr_SetString(PyExc_TypeError, | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7950 | 				    "format requires a mapping"); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7951 | 		    goto onError; | 
 | 7952 | 		} | 
 | 7953 | 		++fmt; | 
 | 7954 | 		--fmtcnt; | 
 | 7955 | 		keystart = fmt; | 
 | 7956 | 		/* Skip over balanced parentheses */ | 
 | 7957 | 		while (pcount > 0 && --fmtcnt >= 0) { | 
 | 7958 | 		    if (*fmt == ')') | 
 | 7959 | 			--pcount; | 
 | 7960 | 		    else if (*fmt == '(') | 
 | 7961 | 			++pcount; | 
 | 7962 | 		    fmt++; | 
 | 7963 | 		} | 
 | 7964 | 		keylen = fmt - keystart - 1; | 
 | 7965 | 		if (fmtcnt < 0 || pcount > 0) { | 
 | 7966 | 		    PyErr_SetString(PyExc_ValueError, | 
 | 7967 | 				    "incomplete format key"); | 
 | 7968 | 		    goto onError; | 
 | 7969 | 		} | 
| Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 7970 | #if 0 | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7971 | 		/* keys are converted to strings using UTF-8 and | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7972 | 		   then looked up since Python uses strings to hold | 
 | 7973 | 		   variables names etc. in its namespaces and we | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7974 | 		   wouldn't want to break common idioms. */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7975 | 		key = PyUnicode_EncodeUTF8(keystart, | 
 | 7976 | 					   keylen, | 
 | 7977 | 					   NULL); | 
| Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 7978 | #else | 
 | 7979 | 		key = PyUnicode_FromUnicode(keystart, keylen); | 
 | 7980 | #endif | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7981 | 		if (key == NULL) | 
 | 7982 | 		    goto onError; | 
 | 7983 | 		if (args_owned) { | 
 | 7984 | 		    Py_DECREF(args); | 
 | 7985 | 		    args_owned = 0; | 
 | 7986 | 		} | 
 | 7987 | 		args = PyObject_GetItem(dict, key); | 
 | 7988 | 		Py_DECREF(key); | 
 | 7989 | 		if (args == NULL) { | 
 | 7990 | 		    goto onError; | 
 | 7991 | 		} | 
 | 7992 | 		args_owned = 1; | 
 | 7993 | 		arglen = -1; | 
 | 7994 | 		argidx = -2; | 
 | 7995 | 	    } | 
 | 7996 | 	    while (--fmtcnt >= 0) { | 
 | 7997 | 		switch (c = *fmt++) { | 
 | 7998 | 		case '-': flags |= F_LJUST; continue; | 
 | 7999 | 		case '+': flags |= F_SIGN; continue; | 
 | 8000 | 		case ' ': flags |= F_BLANK; continue; | 
 | 8001 | 		case '#': flags |= F_ALT; continue; | 
 | 8002 | 		case '0': flags |= F_ZERO; continue; | 
 | 8003 | 		} | 
 | 8004 | 		break; | 
 | 8005 | 	    } | 
 | 8006 | 	    if (c == '*') { | 
 | 8007 | 		v = getnextarg(args, arglen, &argidx); | 
 | 8008 | 		if (v == NULL) | 
 | 8009 | 		    goto onError; | 
 | 8010 | 		if (!PyInt_Check(v)) { | 
 | 8011 | 		    PyErr_SetString(PyExc_TypeError, | 
 | 8012 | 				    "* wants int"); | 
 | 8013 | 		    goto onError; | 
 | 8014 | 		} | 
 | 8015 | 		width = PyInt_AsLong(v); | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 8016 | 		if (width == -1 && PyErr_Occurred()) | 
 | 8017 | 			goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8018 | 		if (width < 0) { | 
 | 8019 | 		    flags |= F_LJUST; | 
 | 8020 | 		    width = -width; | 
 | 8021 | 		} | 
 | 8022 | 		if (--fmtcnt >= 0) | 
 | 8023 | 		    c = *fmt++; | 
 | 8024 | 	    } | 
 | 8025 | 	    else if (c >= '0' && c <= '9') { | 
 | 8026 | 		width = c - '0'; | 
 | 8027 | 		while (--fmtcnt >= 0) { | 
 | 8028 | 		    c = *fmt++; | 
 | 8029 | 		    if (c < '0' || c > '9') | 
 | 8030 | 			break; | 
 | 8031 | 		    if ((width*10) / 10 != width) { | 
 | 8032 | 			PyErr_SetString(PyExc_ValueError, | 
 | 8033 | 					"width too big"); | 
 | 8034 | 			goto onError; | 
 | 8035 | 		    } | 
 | 8036 | 		    width = width*10 + (c - '0'); | 
 | 8037 | 		} | 
 | 8038 | 	    } | 
 | 8039 | 	    if (c == '.') { | 
 | 8040 | 		prec = 0; | 
 | 8041 | 		if (--fmtcnt >= 0) | 
 | 8042 | 		    c = *fmt++; | 
 | 8043 | 		if (c == '*') { | 
 | 8044 | 		    v = getnextarg(args, arglen, &argidx); | 
 | 8045 | 		    if (v == NULL) | 
 | 8046 | 			goto onError; | 
 | 8047 | 		    if (!PyInt_Check(v)) { | 
 | 8048 | 			PyErr_SetString(PyExc_TypeError, | 
 | 8049 | 					"* wants int"); | 
 | 8050 | 			goto onError; | 
 | 8051 | 		    } | 
 | 8052 | 		    prec = PyInt_AsLong(v); | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 8053 | 		    if (prec == -1 && PyErr_Occurred()) | 
 | 8054 | 			goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8055 | 		    if (prec < 0) | 
 | 8056 | 			prec = 0; | 
 | 8057 | 		    if (--fmtcnt >= 0) | 
 | 8058 | 			c = *fmt++; | 
 | 8059 | 		} | 
 | 8060 | 		else if (c >= '0' && c <= '9') { | 
 | 8061 | 		    prec = c - '0'; | 
 | 8062 | 		    while (--fmtcnt >= 0) { | 
 | 8063 | 			c = Py_CHARMASK(*fmt++); | 
 | 8064 | 			if (c < '0' || c > '9') | 
 | 8065 | 			    break; | 
 | 8066 | 			if ((prec*10) / 10 != prec) { | 
 | 8067 | 			    PyErr_SetString(PyExc_ValueError, | 
 | 8068 | 					    "prec too big"); | 
 | 8069 | 			    goto onError; | 
 | 8070 | 			} | 
 | 8071 | 			prec = prec*10 + (c - '0'); | 
 | 8072 | 		    } | 
 | 8073 | 		} | 
 | 8074 | 	    } /* prec */ | 
 | 8075 | 	    if (fmtcnt >= 0) { | 
 | 8076 | 		if (c == 'h' || c == 'l' || c == 'L') { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8077 | 		    if (--fmtcnt >= 0) | 
 | 8078 | 			c = *fmt++; | 
 | 8079 | 		} | 
 | 8080 | 	    } | 
 | 8081 | 	    if (fmtcnt < 0) { | 
 | 8082 | 		PyErr_SetString(PyExc_ValueError, | 
 | 8083 | 				"incomplete format"); | 
 | 8084 | 		goto onError; | 
 | 8085 | 	    } | 
 | 8086 | 	    if (c != '%') { | 
 | 8087 | 		v = getnextarg(args, arglen, &argidx); | 
 | 8088 | 		if (v == NULL) | 
 | 8089 | 		    goto onError; | 
 | 8090 | 	    } | 
 | 8091 | 	    sign = 0; | 
 | 8092 | 	    fill = ' '; | 
 | 8093 | 	    switch (c) { | 
 | 8094 |  | 
 | 8095 | 	    case '%': | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8096 | 		pbuf = formatbuf; | 
 | 8097 | 		/* presume that buffer length is at least 1 */ | 
 | 8098 | 		pbuf[0] = '%'; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8099 | 		len = 1; | 
 | 8100 | 		break; | 
 | 8101 |  | 
 | 8102 | 	    case 's': | 
 | 8103 | 	    case 'r': | 
 | 8104 | 		if (PyUnicode_Check(v) && c == 's') { | 
 | 8105 | 		    temp = v; | 
 | 8106 | 		    Py_INCREF(temp); | 
 | 8107 | 		} | 
 | 8108 | 		else { | 
 | 8109 | 		    PyObject *unicode; | 
 | 8110 | 		    if (c == 's') | 
| Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8111 | 			temp = PyObject_Unicode(v); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8112 | 		    else | 
 | 8113 | 			temp = PyObject_Repr(v); | 
 | 8114 | 		    if (temp == NULL) | 
 | 8115 | 			goto onError; | 
| Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8116 |                     if (PyUnicode_Check(temp)) | 
 | 8117 |                         /* nothing to do */; | 
 | 8118 |                     else if (PyString_Check(temp)) { | 
 | 8119 |                         /* convert to string to Unicode */ | 
| Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8120 | 		        unicode = PyUnicode_Decode(PyString_AS_STRING(temp), | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8121 | 						   PyString_GET_SIZE(temp), | 
| Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8122 | 						   NULL, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8123 | 						   "strict"); | 
| Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8124 | 		        Py_DECREF(temp); | 
 | 8125 | 		        temp = unicode; | 
 | 8126 | 		        if (temp == NULL) | 
 | 8127 | 			    goto onError; | 
 | 8128 | 		    } | 
| Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8129 | 		    else { | 
 | 8130 | 			Py_DECREF(temp); | 
 | 8131 | 			PyErr_SetString(PyExc_TypeError, | 
 | 8132 | 					"%s argument has non-string str()"); | 
 | 8133 | 			goto onError; | 
 | 8134 | 		    } | 
 | 8135 | 		} | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8136 | 		pbuf = PyUnicode_AS_UNICODE(temp); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8137 | 		len = PyUnicode_GET_SIZE(temp); | 
 | 8138 | 		if (prec >= 0 && len > prec) | 
 | 8139 | 		    len = prec; | 
 | 8140 | 		break; | 
 | 8141 |  | 
 | 8142 | 	    case 'i': | 
 | 8143 | 	    case 'd': | 
 | 8144 | 	    case 'u': | 
 | 8145 | 	    case 'o': | 
 | 8146 | 	    case 'x': | 
 | 8147 | 	    case 'X': | 
 | 8148 | 		if (c == 'i') | 
 | 8149 | 		    c = 'd'; | 
| Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 8150 | 		if (PyLong_Check(v)) { | 
| Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8151 | 		    temp = formatlong(v, flags, prec, c); | 
 | 8152 | 		    if (!temp) | 
 | 8153 | 			goto onError; | 
 | 8154 | 		    pbuf = PyUnicode_AS_UNICODE(temp); | 
 | 8155 | 		    len = PyUnicode_GET_SIZE(temp); | 
| Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8156 | 		    sign = 1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8157 | 		} | 
| Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8158 | 		else { | 
 | 8159 | 		    pbuf = formatbuf; | 
 | 8160 | 		    len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), | 
 | 8161 | 				    flags, prec, c, v); | 
 | 8162 | 		    if (len < 0) | 
 | 8163 | 			goto onError; | 
| Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8164 | 		    sign = 1; | 
| Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8165 | 		} | 
 | 8166 | 		if (flags & F_ZERO) | 
 | 8167 | 		    fill = '0'; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8168 | 		break; | 
 | 8169 |  | 
 | 8170 | 	    case 'e': | 
 | 8171 | 	    case 'E': | 
 | 8172 | 	    case 'f': | 
| Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 8173 | 	    case 'F': | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8174 | 	    case 'g': | 
 | 8175 | 	    case 'G': | 
| Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 8176 | 		if (c == 'F') | 
 | 8177 | 			c = 'f'; | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8178 | 		pbuf = formatbuf; | 
 | 8179 | 		len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), | 
 | 8180 | 			flags, prec, c, v); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8181 | 		if (len < 0) | 
 | 8182 | 		    goto onError; | 
 | 8183 | 		sign = 1; | 
| Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8184 | 		if (flags & F_ZERO) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8185 | 		    fill = '0'; | 
 | 8186 | 		break; | 
 | 8187 |  | 
 | 8188 | 	    case 'c': | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8189 | 		pbuf = formatbuf; | 
 | 8190 | 		len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8191 | 		if (len < 0) | 
 | 8192 | 		    goto onError; | 
 | 8193 | 		break; | 
 | 8194 |  | 
 | 8195 | 	    default: | 
 | 8196 | 		PyErr_Format(PyExc_ValueError, | 
| Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 8197 | 			     "unsupported format character '%c' (0x%x) " | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 8198 | 			     "at index %zd", | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8199 | 			     (31<=c && c<=126) ? (char)c : '?', | 
| Marc-André Lemburg | 24e53b6 | 2002-09-24 09:32:14 +0000 | [diff] [blame] | 8200 |                              (int)c, | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 8201 | 			     (Py_ssize_t)(fmt - 1 - | 
 | 8202 | 					  PyUnicode_AS_UNICODE(uformat))); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8203 | 		goto onError; | 
 | 8204 | 	    } | 
 | 8205 | 	    if (sign) { | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8206 | 		if (*pbuf == '-' || *pbuf == '+') { | 
 | 8207 | 		    sign = *pbuf++; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8208 | 		    len--; | 
 | 8209 | 		} | 
 | 8210 | 		else if (flags & F_SIGN) | 
 | 8211 | 		    sign = '+'; | 
 | 8212 | 		else if (flags & F_BLANK) | 
 | 8213 | 		    sign = ' '; | 
 | 8214 | 		else | 
 | 8215 | 		    sign = 0; | 
 | 8216 | 	    } | 
 | 8217 | 	    if (width < len) | 
 | 8218 | 		width = len; | 
| Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8219 | 	    if (rescnt - (sign != 0) < width) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8220 | 		reslen -= rescnt; | 
 | 8221 | 		rescnt = width + fmtcnt + 100; | 
 | 8222 | 		reslen += rescnt; | 
| Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8223 | 		if (reslen < 0) { | 
| Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8224 | 		    Py_XDECREF(temp); | 
| Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8225 | 		    PyErr_NoMemory(); | 
 | 8226 | 		    goto onError; | 
| Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8227 | 		} | 
| Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8228 | 		if (_PyUnicode_Resize(&result, reslen) < 0) { | 
 | 8229 | 		    Py_XDECREF(temp); | 
 | 8230 | 		    goto onError; | 
 | 8231 | 		} | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8232 | 		res = PyUnicode_AS_UNICODE(result) | 
 | 8233 | 		    + reslen - rescnt; | 
 | 8234 | 	    } | 
 | 8235 | 	    if (sign) { | 
 | 8236 | 		if (fill != ' ') | 
 | 8237 | 		    *res++ = sign; | 
 | 8238 | 		rescnt--; | 
 | 8239 | 		if (width > len) | 
 | 8240 | 		    width--; | 
 | 8241 | 	    } | 
| Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8242 | 	    if ((flags & F_ALT) && (c == 'x' || c == 'X')) { | 
 | 8243 | 		assert(pbuf[0] == '0'); | 
| Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8244 | 		assert(pbuf[1] == c); | 
 | 8245 | 		if (fill != ' ') { | 
 | 8246 | 		    *res++ = *pbuf++; | 
 | 8247 | 		    *res++ = *pbuf++; | 
| Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8248 | 		} | 
| Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8249 | 		rescnt -= 2; | 
 | 8250 | 		width -= 2; | 
 | 8251 | 		if (width < 0) | 
 | 8252 | 		    width = 0; | 
 | 8253 | 		len -= 2; | 
| Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8254 | 	    } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8255 | 	    if (width > len && !(flags & F_LJUST)) { | 
 | 8256 | 		do { | 
 | 8257 | 		    --rescnt; | 
 | 8258 | 		    *res++ = fill; | 
 | 8259 | 		} while (--width > len); | 
 | 8260 | 	    } | 
| Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8261 | 	    if (fill == ' ') { | 
 | 8262 | 		if (sign) | 
 | 8263 | 		    *res++ = sign; | 
| Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8264 | 		if ((flags & F_ALT) && (c == 'x' || c == 'X')) { | 
| Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8265 | 		    assert(pbuf[0] == '0'); | 
| Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8266 | 		    assert(pbuf[1] == c); | 
| Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8267 | 		    *res++ = *pbuf++; | 
 | 8268 | 		    *res++ = *pbuf++; | 
 | 8269 | 		} | 
 | 8270 | 	    } | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8271 | 	    Py_UNICODE_COPY(res, pbuf, len); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8272 | 	    res += len; | 
 | 8273 | 	    rescnt -= len; | 
 | 8274 | 	    while (--width >= len) { | 
 | 8275 | 		--rescnt; | 
 | 8276 | 		*res++ = ' '; | 
 | 8277 | 	    } | 
 | 8278 | 	    if (dict && (argidx < arglen) && c != '%') { | 
 | 8279 | 		PyErr_SetString(PyExc_TypeError, | 
| Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 8280 | 				"not all arguments converted during string formatting"); | 
| Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8281 |                 Py_XDECREF(temp); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8282 | 		goto onError; | 
 | 8283 | 	    } | 
 | 8284 | 	    Py_XDECREF(temp); | 
 | 8285 | 	} /* '%' */ | 
 | 8286 |     } /* until end */ | 
 | 8287 |     if (argidx < arglen && !dict) { | 
 | 8288 | 	PyErr_SetString(PyExc_TypeError, | 
| Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 8289 | 			"not all arguments converted during string formatting"); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8290 | 	goto onError; | 
 | 8291 |     } | 
 | 8292 |  | 
| Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8293 |     if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) | 
 | 8294 | 	goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8295 |     if (args_owned) { | 
 | 8296 | 	Py_DECREF(args); | 
 | 8297 |     } | 
 | 8298 |     Py_DECREF(uformat); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8299 |     return (PyObject *)result; | 
 | 8300 |  | 
 | 8301 |  onError: | 
 | 8302 |     Py_XDECREF(result); | 
 | 8303 |     Py_DECREF(uformat); | 
 | 8304 |     if (args_owned) { | 
 | 8305 | 	Py_DECREF(args); | 
 | 8306 |     } | 
 | 8307 |     return NULL; | 
 | 8308 | } | 
 | 8309 |  | 
 | 8310 | static PyBufferProcs unicode_as_buffer = { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8311 |     (readbufferproc) unicode_buffer_getreadbuf, | 
 | 8312 |     (writebufferproc) unicode_buffer_getwritebuf, | 
 | 8313 |     (segcountproc) unicode_buffer_getsegcount, | 
 | 8314 |     (charbufferproc) unicode_buffer_getcharbuf, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8315 | }; | 
 | 8316 |  | 
| Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 8317 | static PyObject * | 
| Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8318 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); | 
 | 8319 |  | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8320 | static PyObject * | 
 | 8321 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | 
 | 8322 | { | 
 | 8323 |         PyObject *x = NULL; | 
| Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 8324 | 	static char *kwlist[] = {"string", "encoding", "errors", 0}; | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8325 | 	char *encoding = NULL; | 
 | 8326 | 	char *errors = NULL; | 
 | 8327 |  | 
| Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8328 | 	if (type != &PyUnicode_Type) | 
 | 8329 | 		return unicode_subtype_new(type, args, kwds); | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8330 | 	if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", | 
 | 8331 | 					  kwlist, &x, &encoding, &errors)) | 
 | 8332 | 	    return NULL; | 
 | 8333 | 	if (x == NULL) | 
 | 8334 | 		return (PyObject *)_PyUnicode_New(0); | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 8335 | 	if (encoding == NULL && errors == NULL) | 
 | 8336 | 	    return PyObject_Unicode(x); | 
 | 8337 | 	else | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8338 | 	return PyUnicode_FromEncodedObject(x, encoding, errors); | 
 | 8339 | } | 
 | 8340 |  | 
| Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8341 | static PyObject * | 
 | 8342 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | 
 | 8343 | { | 
| Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8344 | 	PyUnicodeObject *tmp, *pnew; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8345 | 	Py_ssize_t n; | 
| Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8346 |  | 
 | 8347 | 	assert(PyType_IsSubtype(type, &PyUnicode_Type)); | 
 | 8348 | 	tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); | 
 | 8349 | 	if (tmp == NULL) | 
 | 8350 | 		return NULL; | 
 | 8351 | 	assert(PyUnicode_Check(tmp)); | 
| Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8352 | 	pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); | 
| Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8353 | 	if (pnew == NULL) { | 
 | 8354 | 		Py_DECREF(tmp); | 
| Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8355 | 		return NULL; | 
| Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8356 | 	} | 
| Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8357 | 	pnew->str = PyMem_NEW(Py_UNICODE, n+1); | 
 | 8358 | 	if (pnew->str == NULL) { | 
 | 8359 | 		_Py_ForgetReference((PyObject *)pnew); | 
| Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8360 | 		PyObject_Del(pnew); | 
| Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8361 | 		Py_DECREF(tmp); | 
| Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 8362 | 		return PyErr_NoMemory(); | 
| Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8363 | 	} | 
| Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8364 | 	Py_UNICODE_COPY(pnew->str, tmp->str, n+1); | 
 | 8365 | 	pnew->length = n; | 
 | 8366 | 	pnew->hash = tmp->hash; | 
| Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8367 | 	Py_DECREF(tmp); | 
| Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8368 | 	return (PyObject *)pnew; | 
| Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8369 | } | 
 | 8370 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8371 | PyDoc_STRVAR(unicode_doc, | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8372 | "unicode(string [, encoding[, errors]]) -> object\n\ | 
 | 8373 | \n\ | 
 | 8374 | Create a new Unicode object from the given encoded string.\n\ | 
| Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 8375 | encoding defaults to the current default string encoding.\n\ | 
 | 8376 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8377 |  | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8378 | static PyObject *unicode_iter(PyObject *seq); | 
 | 8379 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8380 | PyTypeObject PyUnicode_Type = { | 
 | 8381 |     PyObject_HEAD_INIT(&PyType_Type) | 
 | 8382 |     0, 					/* ob_size */ | 
| Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 8383 |     "str", 				/* tp_name */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8384 |     sizeof(PyUnicodeObject), 		/* tp_size */ | 
 | 8385 |     0, 					/* tp_itemsize */ | 
 | 8386 |     /* Slots */ | 
| Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 8387 |     (destructor)unicode_dealloc, 	/* tp_dealloc */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8388 |     0, 					/* tp_print */ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8389 |     0,				 	/* tp_getattr */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8390 |     0, 					/* tp_setattr */ | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8391 |     0, 					/* tp_compare */ | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8392 |     unicode_repr, 			/* tp_repr */ | 
| Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8393 |     &unicode_as_number, 		/* tp_as_number */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8394 |     &unicode_as_sequence, 		/* tp_as_sequence */ | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8395 |     &unicode_as_mapping, 		/* tp_as_mapping */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8396 |     (hashfunc) unicode_hash, 		/* tp_hash*/ | 
 | 8397 |     0, 					/* tp_call*/ | 
 | 8398 |     (reprfunc) unicode_str,	 	/* tp_str */ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8399 |     PyObject_GenericGetAttr, 		/* tp_getattro */ | 
 | 8400 |     0,			 		/* tp_setattro */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8401 |     &unicode_as_buffer,			/* tp_as_buffer */ | 
| Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 8402 |     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |  | 
 | 8403 |         Py_TPFLAGS_UNICODE_SUBCLASS,	/* tp_flags */ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8404 |     unicode_doc,			/* tp_doc */ | 
 | 8405 |     0,					/* tp_traverse */ | 
 | 8406 |     0,					/* tp_clear */ | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8407 |     PyUnicode_RichCompare,		/* tp_richcompare */ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8408 |     0,					/* tp_weaklistoffset */ | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8409 |     unicode_iter,			/* tp_iter */ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8410 |     0,					/* tp_iternext */ | 
 | 8411 |     unicode_methods,			/* tp_methods */ | 
 | 8412 |     0,					/* tp_members */ | 
 | 8413 |     0,					/* tp_getset */ | 
| Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 8414 |     &PyBaseString_Type,			/* tp_base */ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8415 |     0,					/* tp_dict */ | 
 | 8416 |     0,					/* tp_descr_get */ | 
 | 8417 |     0,					/* tp_descr_set */ | 
 | 8418 |     0,					/* tp_dictoffset */ | 
 | 8419 |     0,					/* tp_init */ | 
 | 8420 |     0,					/* tp_alloc */ | 
 | 8421 |     unicode_new,			/* tp_new */ | 
| Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8422 |     PyObject_Del,      		/* tp_free */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8423 | }; | 
 | 8424 |  | 
 | 8425 | /* Initialize the Unicode implementation */ | 
 | 8426 |  | 
| Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 8427 | void _PyUnicode_Init(void) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8428 | { | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8429 |     int i; | 
 | 8430 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8431 |     /* XXX - move this array to unicodectype.c ? */ | 
 | 8432 |     Py_UNICODE linebreak[] = { | 
 | 8433 |         0x000A, /* LINE FEED */ | 
 | 8434 |         0x000D, /* CARRIAGE RETURN */ | 
 | 8435 |         0x001C, /* FILE SEPARATOR */ | 
 | 8436 |         0x001D, /* GROUP SEPARATOR */ | 
 | 8437 |         0x001E, /* RECORD SEPARATOR */ | 
 | 8438 |         0x0085, /* NEXT LINE */ | 
 | 8439 |         0x2028, /* LINE SEPARATOR */ | 
 | 8440 |         0x2029, /* PARAGRAPH SEPARATOR */ | 
 | 8441 |     }; | 
 | 8442 |  | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8443 |     /* Init the implementation */ | 
| Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8444 |     unicode_freelist = NULL; | 
 | 8445 |     unicode_freelist_size = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8446 |     unicode_empty = _PyUnicode_New(0); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8447 |     if (!unicode_empty) | 
 | 8448 | 	return; | 
 | 8449 |  | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8450 |     for (i = 0; i < 256; i++) | 
 | 8451 | 	unicode_latin1[i] = NULL; | 
| Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 8452 |     if (PyType_Ready(&PyUnicode_Type) < 0) | 
 | 8453 | 	Py_FatalError("Can't initialize 'unicode'"); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8454 |  | 
 | 8455 |     /* initialize the linebreak bloom filter */ | 
 | 8456 |     bloom_linebreak = make_bloom_mask( | 
 | 8457 |         linebreak, sizeof(linebreak) / sizeof(linebreak[0]) | 
 | 8458 |         ); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8459 |  | 
 | 8460 |     PyType_Ready(&EncodingMapType); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8461 | } | 
 | 8462 |  | 
 | 8463 | /* Finalize the Unicode implementation */ | 
 | 8464 |  | 
 | 8465 | void | 
| Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 8466 | _PyUnicode_Fini(void) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8467 | { | 
| Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8468 |     PyUnicodeObject *u; | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8469 |     int i; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8470 |  | 
| Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 8471 |     Py_XDECREF(unicode_empty); | 
 | 8472 |     unicode_empty = NULL; | 
| Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8473 |  | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8474 |     for (i = 0; i < 256; i++) { | 
 | 8475 | 	if (unicode_latin1[i]) { | 
 | 8476 | 	    Py_DECREF(unicode_latin1[i]); | 
 | 8477 | 	    unicode_latin1[i] = NULL; | 
 | 8478 | 	} | 
 | 8479 |     } | 
 | 8480 |  | 
| Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8481 |     for (u = unicode_freelist; u != NULL;) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8482 | 	PyUnicodeObject *v = u; | 
 | 8483 | 	u = *(PyUnicodeObject **)u; | 
| Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 8484 | 	if (v->str) | 
| Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 8485 | 	    PyMem_DEL(v->str); | 
| Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 8486 | 	Py_XDECREF(v->defenc); | 
| Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8487 | 	PyObject_Del(v); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8488 |     } | 
| Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8489 |     unicode_freelist = NULL; | 
 | 8490 |     unicode_freelist_size = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8491 | } | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 8492 |  | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8493 |  | 
 | 8494 |  | 
 | 8495 | /********************* Unicode Iterator **************************/ | 
 | 8496 |  | 
 | 8497 | typedef struct { | 
 | 8498 | 	PyObject_HEAD | 
| Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 8499 | 	Py_ssize_t it_index; | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8500 | 	PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ | 
 | 8501 | } unicodeiterobject; | 
 | 8502 |  | 
 | 8503 | static void | 
 | 8504 | unicodeiter_dealloc(unicodeiterobject *it) | 
 | 8505 | { | 
 | 8506 | 	_PyObject_GC_UNTRACK(it); | 
 | 8507 | 	Py_XDECREF(it->it_seq); | 
 | 8508 | 	PyObject_GC_Del(it); | 
 | 8509 | } | 
 | 8510 |  | 
 | 8511 | static int | 
 | 8512 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) | 
 | 8513 | { | 
 | 8514 | 	Py_VISIT(it->it_seq); | 
 | 8515 | 	return 0; | 
 | 8516 | } | 
 | 8517 |  | 
 | 8518 | static PyObject * | 
 | 8519 | unicodeiter_next(unicodeiterobject *it) | 
 | 8520 | { | 
 | 8521 | 	PyUnicodeObject *seq; | 
 | 8522 | 	PyObject *item; | 
 | 8523 |  | 
 | 8524 | 	assert(it != NULL); | 
 | 8525 | 	seq = it->it_seq; | 
 | 8526 | 	if (seq == NULL) | 
 | 8527 | 		return NULL; | 
 | 8528 | 	assert(PyUnicode_Check(seq)); | 
 | 8529 |  | 
 | 8530 | 	if (it->it_index < PyUnicode_GET_SIZE(seq)) { | 
| Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 8531 | 		item = PyUnicode_FromUnicode( | 
 | 8532 |                     PyUnicode_AS_UNICODE(seq)+it->it_index, 1); | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8533 | 		if (item != NULL) | 
 | 8534 | 			++it->it_index; | 
 | 8535 | 		return item; | 
 | 8536 | 	} | 
 | 8537 |  | 
 | 8538 | 	Py_DECREF(seq); | 
 | 8539 | 	it->it_seq = NULL; | 
 | 8540 | 	return NULL; | 
 | 8541 | } | 
 | 8542 |  | 
 | 8543 | static PyObject * | 
 | 8544 | unicodeiter_len(unicodeiterobject *it) | 
 | 8545 | { | 
 | 8546 | 	Py_ssize_t len = 0; | 
 | 8547 | 	if (it->it_seq) | 
 | 8548 | 		len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; | 
 | 8549 | 	return PyInt_FromSsize_t(len); | 
 | 8550 | } | 
 | 8551 |  | 
 | 8552 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); | 
 | 8553 |  | 
 | 8554 | static PyMethodDef unicodeiter_methods[] = { | 
| Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 8555 | 	{"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, | 
 | 8556 |          length_hint_doc}, | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8557 |  	{NULL,		NULL}		/* sentinel */ | 
 | 8558 | }; | 
 | 8559 |  | 
 | 8560 | PyTypeObject PyUnicodeIter_Type = { | 
 | 8561 | 	PyObject_HEAD_INIT(&PyType_Type) | 
 | 8562 | 	0,					/* ob_size */ | 
 | 8563 | 	"unicodeiterator",			/* tp_name */ | 
 | 8564 | 	sizeof(unicodeiterobject),		/* tp_basicsize */ | 
 | 8565 | 	0,					/* tp_itemsize */ | 
 | 8566 | 	/* methods */ | 
| Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 8567 | 	(destructor)unicodeiter_dealloc,	/* tp_dealloc */ | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8568 | 	0,					/* tp_print */ | 
 | 8569 | 	0,					/* tp_getattr */ | 
 | 8570 | 	0,					/* tp_setattr */ | 
 | 8571 | 	0,					/* tp_compare */ | 
 | 8572 | 	0,					/* tp_repr */ | 
 | 8573 | 	0,					/* tp_as_number */ | 
 | 8574 | 	0,					/* tp_as_sequence */ | 
 | 8575 | 	0,					/* tp_as_mapping */ | 
 | 8576 | 	0,					/* tp_hash */ | 
 | 8577 | 	0,					/* tp_call */ | 
 | 8578 | 	0,					/* tp_str */ | 
 | 8579 | 	PyObject_GenericGetAttr,		/* tp_getattro */ | 
 | 8580 | 	0,					/* tp_setattro */ | 
 | 8581 | 	0,					/* tp_as_buffer */ | 
 | 8582 | 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ | 
 | 8583 | 	0,					/* tp_doc */ | 
 | 8584 | 	(traverseproc)unicodeiter_traverse,	/* tp_traverse */ | 
 | 8585 | 	0,					/* tp_clear */ | 
 | 8586 | 	0,					/* tp_richcompare */ | 
 | 8587 | 	0,					/* tp_weaklistoffset */ | 
 | 8588 | 	PyObject_SelfIter,			/* tp_iter */ | 
 | 8589 | 	(iternextfunc)unicodeiter_next,		/* tp_iternext */ | 
 | 8590 | 	unicodeiter_methods,			/* tp_methods */ | 
 | 8591 | 	0, | 
 | 8592 | }; | 
 | 8593 |  | 
 | 8594 | static PyObject * | 
 | 8595 | unicode_iter(PyObject *seq) | 
 | 8596 | { | 
 | 8597 | 	unicodeiterobject *it; | 
 | 8598 |  | 
 | 8599 | 	if (!PyUnicode_Check(seq)) { | 
 | 8600 | 		PyErr_BadInternalCall(); | 
 | 8601 | 		return NULL; | 
 | 8602 | 	} | 
 | 8603 | 	it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); | 
 | 8604 | 	if (it == NULL) | 
 | 8605 | 		return NULL; | 
 | 8606 | 	it->it_index = 0; | 
 | 8607 | 	Py_INCREF(seq); | 
 | 8608 | 	it->it_seq = (PyUnicodeObject *)seq; | 
 | 8609 | 	_PyObject_GC_TRACK(it); | 
 | 8610 | 	return (PyObject *)it; | 
 | 8611 | } | 
 | 8612 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8613 | #ifdef __cplusplus | 
 | 8614 | } | 
 | 8615 | #endif | 
 | 8616 |  | 
 | 8617 |  | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 8618 | /* | 
 | 8619 | Local variables: | 
 | 8620 | c-basic-offset: 4 | 
 | 8621 | indent-tabs-mode: nil | 
 | 8622 | End: | 
 | 8623 | */ |