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 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +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 | |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +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 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 52 | #undef USE_INLINE /* XXX - set via configure? */ |
| 53 | |
| 54 | #if defined(_MSC_VER) /* this is taken from _sre.c */ |
| 55 | #pragma warning(disable: 4710) |
| 56 | /* fastest possible local call under MSVC */ |
| 57 | #define LOCAL(type) static __inline type __fastcall |
| 58 | #elif defined(USE_INLINE) |
| 59 | #define LOCAL(type) static inline type |
| 60 | #else |
| 61 | #define LOCAL(type) static type |
| 62 | #endif |
| 63 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 64 | /* Limit for the Unicode object free list */ |
| 65 | |
| 66 | #define MAX_UNICODE_FREELIST_SIZE 1024 |
| 67 | |
| 68 | /* Limit for the Unicode object free list stay alive optimization. |
| 69 | |
| 70 | The implementation will keep allocated Unicode memory intact for |
| 71 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 72 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 73 | |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 74 | At worst this will result in MAX_UNICODE_FREELIST_SIZE * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 75 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 76 | malloc()-overhead) bytes of unused garbage. |
| 77 | |
| 78 | Setting the limit to 0 effectively turns the feature off. |
| 79 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 80 | Note: This is an experimental feature ! If you get core dumps when |
| 81 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 82 | |
| 83 | */ |
| 84 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 85 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 86 | |
| 87 | /* Endianness switches; defaults to little endian */ |
| 88 | |
| 89 | #ifdef WORDS_BIGENDIAN |
| 90 | # define BYTEORDER_IS_BIG_ENDIAN |
| 91 | #else |
| 92 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 93 | #endif |
| 94 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 95 | /* --- Globals ------------------------------------------------------------ |
| 96 | |
| 97 | The globals are initialized by the _PyUnicode_Init() API and should |
| 98 | not be used before calling that API. |
| 99 | |
| 100 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 101 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 102 | |
| 103 | #ifdef __cplusplus |
| 104 | extern "C" { |
| 105 | #endif |
| 106 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 107 | /* Free list for Unicode objects */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 108 | static PyUnicodeObject *unicode_freelist; |
| 109 | static int unicode_freelist_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 110 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 111 | /* The empty Unicode object is shared to improve performance. */ |
| 112 | static PyUnicodeObject *unicode_empty; |
| 113 | |
| 114 | /* Single character Unicode strings in the Latin-1 range are being |
| 115 | shared as well. */ |
| 116 | static PyUnicodeObject *unicode_latin1[256]; |
| 117 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 118 | /* Default encoding to use and assume when NULL is passed as encoding |
| 119 | parameter; it is initialized by _PyUnicode_Init(). |
| 120 | |
| 121 | Always use the PyUnicode_SetDefaultEncoding() and |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 122 | PyUnicode_GetDefaultEncoding() APIs to access this global. |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 123 | |
| 124 | */ |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 125 | static char unicode_default_encoding[100]; |
| 126 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 127 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 128 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 129 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 130 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 131 | return 0x10FFFF; |
| 132 | #else |
| 133 | /* This is actually an illegal character, so it should |
| 134 | not be passed to unichr. */ |
| 135 | return 0xFFFF; |
| 136 | #endif |
| 137 | } |
| 138 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 139 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 140 | |
| 141 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 142 | to keep things simple, we use a single bitmask, using the least 5 |
| 143 | bits from each unicode characters as the bit index. */ |
| 144 | |
| 145 | /* the linebreak mask is set up by Unicode_Init below */ |
| 146 | |
| 147 | #define BLOOM_MASK unsigned long |
| 148 | |
| 149 | static BLOOM_MASK bloom_linebreak; |
| 150 | |
| 151 | #define BLOOM(mask, ch) ((mask & (1 << ((ch) & 0x1F)))) |
| 152 | |
| 153 | #define BLOOM_LINEBREAK(ch)\ |
| 154 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK((ch))) |
| 155 | |
| 156 | LOCAL(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len) |
| 157 | { |
| 158 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 159 | |
| 160 | long mask; |
| 161 | Py_ssize_t i; |
| 162 | |
| 163 | mask = 0; |
| 164 | for (i = 0; i < len; i++) |
| 165 | mask |= (1 << (ptr[i] & 0x1F)); |
| 166 | |
| 167 | return mask; |
| 168 | } |
| 169 | |
| 170 | LOCAL(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen) |
| 171 | { |
| 172 | Py_ssize_t i; |
| 173 | |
| 174 | for (i = 0; i < setlen; i++) |
| 175 | if (set[i] == chr) |
| 176 | return 1; |
| 177 | |
Fredrik Lundh | 7763351 | 2006-05-23 19:47:35 +0000 | [diff] [blame] | 178 | return 0; |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | #define BLOOM_MEMBER(mask, chr, set, setlen)\ |
| 182 | BLOOM(mask, chr) && unicode_member(chr, set, setlen) |
| 183 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 184 | /* --- Unicode Object ----------------------------------------------------- */ |
| 185 | |
| 186 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 187 | int unicode_resize(register PyUnicodeObject *unicode, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 188 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 189 | { |
| 190 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 191 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 192 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 193 | if (unicode->length == length) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 194 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 195 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 196 | /* Resizing shared object (unicode_empty or single character |
| 197 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 198 | instead ! */ |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 199 | |
Martin v. Löwis | 80d2e59 | 2006-04-13 06:06:08 +0000 | [diff] [blame] | 200 | if (unicode == unicode_empty || |
| 201 | (unicode->length == 1 && |
| 202 | unicode->str[0] < 256U && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 203 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 204 | PyErr_SetString(PyExc_SystemError, |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 205 | "can't resize shared unicode objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 206 | return -1; |
| 207 | } |
| 208 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 209 | /* We allocate one more byte to make sure the string is Ux0000 terminated. |
| 210 | The overallocation is also used by fastsearch, which assumes that it's |
| 211 | safe to look at str[length] (without makeing any assumptions about what |
| 212 | it contains). */ |
| 213 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 214 | oldstr = unicode->str; |
| 215 | PyMem_RESIZE(unicode->str, Py_UNICODE, length + 1); |
| 216 | if (!unicode->str) { |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 217 | unicode->str = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 218 | PyErr_NoMemory(); |
| 219 | return -1; |
| 220 | } |
| 221 | unicode->str[length] = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 222 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 223 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 224 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 225 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 226 | if (unicode->defenc) { |
| 227 | Py_DECREF(unicode->defenc); |
| 228 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 229 | } |
| 230 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 231 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | /* We allocate one more byte to make sure the string is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 236 | Ux0000 terminated -- XXX is this needed ? |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 237 | |
| 238 | XXX This allocator could further be enhanced by assuring that the |
| 239 | free list never reduces its size below 1. |
| 240 | |
| 241 | */ |
| 242 | |
| 243 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 244 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 245 | { |
| 246 | register PyUnicodeObject *unicode; |
| 247 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 248 | /* Optimization fo empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 249 | if (length == 0 && unicode_empty != NULL) { |
| 250 | Py_INCREF(unicode_empty); |
| 251 | return unicode_empty; |
| 252 | } |
| 253 | |
| 254 | /* Unicode freelist & memory allocation */ |
| 255 | if (unicode_freelist) { |
| 256 | unicode = unicode_freelist; |
Marc-André Lemburg | bea47e7 | 2000-06-17 20:31:17 +0000 | [diff] [blame] | 257 | unicode_freelist = *(PyUnicodeObject **)unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 258 | unicode_freelist_size--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 259 | if (unicode->str) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 260 | /* Keep-Alive optimization: we only upsize the buffer, |
| 261 | never downsize it. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 262 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 263 | unicode_resize(unicode, length) < 0) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 264 | PyMem_DEL(unicode->str); |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 265 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 266 | } |
| 267 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 268 | else { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 269 | unicode->str = PyMem_NEW(Py_UNICODE, length + 1); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 270 | } |
| 271 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 272 | } |
| 273 | else { |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 274 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 275 | if (unicode == NULL) |
| 276 | return NULL; |
| 277 | unicode->str = PyMem_NEW(Py_UNICODE, length + 1); |
| 278 | } |
| 279 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 280 | if (!unicode->str) { |
| 281 | PyErr_NoMemory(); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 282 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 283 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 284 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 285 | * the caller fails before initializing str -- unicode_resize() |
| 286 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 287 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 288 | * We don't want unicode_resize to read uninitialized memory in |
| 289 | * that case. |
| 290 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 291 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 292 | unicode->str[length] = 0; |
Martin v. Löwis | f15da69 | 2006-04-13 07:24:50 +0000 | [diff] [blame] | 293 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 294 | unicode->hash = -1; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 295 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 296 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 297 | |
| 298 | onError: |
| 299 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 300 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 301 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 305 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 306 | { |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 307 | if (PyUnicode_CheckExact(unicode) && |
| 308 | unicode_freelist_size < MAX_UNICODE_FREELIST_SIZE) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 309 | /* Keep-Alive optimization */ |
| 310 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 311 | PyMem_DEL(unicode->str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 312 | unicode->str = NULL; |
| 313 | unicode->length = 0; |
| 314 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 315 | if (unicode->defenc) { |
| 316 | Py_DECREF(unicode->defenc); |
| 317 | unicode->defenc = NULL; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 318 | } |
| 319 | /* Add to free list */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 320 | *(PyUnicodeObject **)unicode = unicode_freelist; |
| 321 | unicode_freelist = unicode; |
| 322 | unicode_freelist_size++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 323 | } |
| 324 | else { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 325 | PyMem_DEL(unicode->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 326 | Py_XDECREF(unicode->defenc); |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 327 | unicode->ob_type->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 331 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 332 | { |
| 333 | register PyUnicodeObject *v; |
| 334 | |
| 335 | /* Argument checks */ |
| 336 | if (unicode == NULL) { |
| 337 | PyErr_BadInternalCall(); |
| 338 | return -1; |
| 339 | } |
| 340 | v = (PyUnicodeObject *)*unicode; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 341 | 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] | 342 | PyErr_BadInternalCall(); |
| 343 | return -1; |
| 344 | } |
| 345 | |
| 346 | /* Resizing unicode_empty and single character objects is not |
| 347 | possible since these are being shared. We simply return a fresh |
| 348 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 349 | if (v->length != length && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 350 | (v == unicode_empty || v->length == 1)) { |
| 351 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 352 | if (w == NULL) |
| 353 | return -1; |
| 354 | Py_UNICODE_COPY(w->str, v->str, |
| 355 | length < v->length ? length : v->length); |
Raymond Hettinger | c8df578 | 2003-03-09 07:30:43 +0000 | [diff] [blame] | 356 | Py_DECREF(*unicode); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 357 | *unicode = (PyObject *)w; |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 362 | objects, since we can modify them in-place. */ |
| 363 | return unicode_resize(v, length); |
| 364 | } |
| 365 | |
| 366 | /* Internal API for use in unicodeobject.c only ! */ |
| 367 | #define _PyUnicode_Resize(unicodevar, length) \ |
| 368 | PyUnicode_Resize(((PyObject **)(unicodevar)), length) |
| 369 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 370 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 371 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 372 | { |
| 373 | PyUnicodeObject *unicode; |
| 374 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 375 | /* If the Unicode data is known at construction time, we can apply |
| 376 | some optimizations which share commonly used objects. */ |
| 377 | if (u != NULL) { |
| 378 | |
| 379 | /* Optimization for empty strings */ |
| 380 | if (size == 0 && unicode_empty != NULL) { |
| 381 | Py_INCREF(unicode_empty); |
| 382 | return (PyObject *)unicode_empty; |
| 383 | } |
| 384 | |
| 385 | /* Single character Unicode objects in the Latin-1 range are |
| 386 | shared when using this constructor */ |
| 387 | if (size == 1 && *u < 256) { |
| 388 | unicode = unicode_latin1[*u]; |
| 389 | if (!unicode) { |
| 390 | unicode = _PyUnicode_New(1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 391 | if (!unicode) |
| 392 | return NULL; |
Marc-André Lemburg | 8879a33 | 2001-06-07 12:26:56 +0000 | [diff] [blame] | 393 | unicode->str[0] = *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 394 | unicode_latin1[*u] = unicode; |
| 395 | } |
| 396 | Py_INCREF(unicode); |
| 397 | return (PyObject *)unicode; |
| 398 | } |
| 399 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 400 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 401 | unicode = _PyUnicode_New(size); |
| 402 | if (!unicode) |
| 403 | return NULL; |
| 404 | |
| 405 | /* Copy the Unicode data into the new object */ |
| 406 | if (u != NULL) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 407 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 408 | |
| 409 | return (PyObject *)unicode; |
| 410 | } |
| 411 | |
| 412 | #ifdef HAVE_WCHAR_H |
| 413 | |
| 414 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 415 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 416 | { |
| 417 | PyUnicodeObject *unicode; |
| 418 | |
| 419 | if (w == NULL) { |
| 420 | PyErr_BadInternalCall(); |
| 421 | return NULL; |
| 422 | } |
| 423 | |
| 424 | unicode = _PyUnicode_New(size); |
| 425 | if (!unicode) |
| 426 | return NULL; |
| 427 | |
| 428 | /* Copy the wchar_t data into the new object */ |
| 429 | #ifdef HAVE_USABLE_WCHAR_T |
| 430 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 431 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 432 | { |
| 433 | register Py_UNICODE *u; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 434 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 435 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 436 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 437 | *u++ = *w++; |
| 438 | } |
| 439 | #endif |
| 440 | |
| 441 | return (PyObject *)unicode; |
| 442 | } |
| 443 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 444 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 445 | wchar_t *w, |
| 446 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 447 | { |
| 448 | if (unicode == NULL) { |
| 449 | PyErr_BadInternalCall(); |
| 450 | return -1; |
| 451 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 452 | |
| 453 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 454 | if (size > PyUnicode_GET_SIZE(unicode)) |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 455 | size = PyUnicode_GET_SIZE(unicode) + 1; |
| 456 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 457 | #ifdef HAVE_USABLE_WCHAR_T |
| 458 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 459 | #else |
| 460 | { |
| 461 | register Py_UNICODE *u; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 462 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 463 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 464 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 465 | *w++ = *u++; |
| 466 | } |
| 467 | #endif |
| 468 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 469 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 470 | return PyUnicode_GET_SIZE(unicode); |
| 471 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 472 | return size; |
| 473 | } |
| 474 | |
| 475 | #endif |
| 476 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 477 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 478 | { |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 479 | Py_UNICODE s[1]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 480 | |
| 481 | #ifdef Py_UNICODE_WIDE |
| 482 | if (ordinal < 0 || ordinal > 0x10ffff) { |
| 483 | PyErr_SetString(PyExc_ValueError, |
| 484 | "unichr() arg not in range(0x110000) " |
| 485 | "(wide Python build)"); |
| 486 | return NULL; |
| 487 | } |
| 488 | #else |
| 489 | if (ordinal < 0 || ordinal > 0xffff) { |
| 490 | PyErr_SetString(PyExc_ValueError, |
| 491 | "unichr() arg not in range(0x10000) " |
| 492 | "(narrow Python build)"); |
| 493 | return NULL; |
| 494 | } |
| 495 | #endif |
| 496 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 497 | s[0] = (Py_UNICODE)ordinal; |
| 498 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 501 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 502 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 503 | /* XXX Perhaps we should make this API an alias of |
| 504 | PyObject_Unicode() instead ?! */ |
| 505 | if (PyUnicode_CheckExact(obj)) { |
| 506 | Py_INCREF(obj); |
| 507 | return obj; |
| 508 | } |
| 509 | if (PyUnicode_Check(obj)) { |
| 510 | /* For a Unicode subtype that's not a Unicode object, |
| 511 | return a true Unicode object with the same data. */ |
| 512 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 513 | PyUnicode_GET_SIZE(obj)); |
| 514 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 515 | return PyUnicode_FromEncodedObject(obj, NULL, "strict"); |
| 516 | } |
| 517 | |
| 518 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
| 519 | const char *encoding, |
| 520 | const char *errors) |
| 521 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 522 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 523 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 524 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 525 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 526 | if (obj == NULL) { |
| 527 | PyErr_BadInternalCall(); |
| 528 | return NULL; |
| 529 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 530 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 531 | #if 0 |
| 532 | /* For b/w compatibility we also accept Unicode objects provided |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 533 | that no encodings is given and then redirect to |
| 534 | PyObject_Unicode() which then applies the additional logic for |
| 535 | Unicode subclasses. |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 536 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 537 | NOTE: This API should really only be used for object which |
| 538 | represent *encoded* Unicode ! |
| 539 | |
| 540 | */ |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 541 | if (PyUnicode_Check(obj)) { |
| 542 | if (encoding) { |
| 543 | PyErr_SetString(PyExc_TypeError, |
| 544 | "decoding Unicode is not supported"); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 545 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 546 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 547 | return PyObject_Unicode(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 548 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 549 | #else |
| 550 | if (PyUnicode_Check(obj)) { |
| 551 | PyErr_SetString(PyExc_TypeError, |
| 552 | "decoding Unicode is not supported"); |
| 553 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 554 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 555 | #endif |
| 556 | |
| 557 | /* Coerce object */ |
| 558 | if (PyString_Check(obj)) { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 559 | s = PyString_AS_STRING(obj); |
| 560 | len = PyString_GET_SIZE(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 561 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 562 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
| 563 | /* Overwrite the error message with something more useful in |
| 564 | case of a TypeError. */ |
| 565 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 566 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 567 | "coercing to Unicode: need string or buffer, " |
| 568 | "%.80s found", |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 569 | obj->ob_type->tp_name); |
| 570 | goto onError; |
| 571 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 572 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 573 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 574 | if (len == 0) { |
| 575 | Py_INCREF(unicode_empty); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 576 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 577 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 578 | else |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 579 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 580 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 581 | return v; |
| 582 | |
| 583 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 584 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | PyObject *PyUnicode_Decode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 588 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 589 | const char *encoding, |
| 590 | const char *errors) |
| 591 | { |
| 592 | PyObject *buffer = NULL, *unicode; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 593 | |
| 594 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 595 | encoding = PyUnicode_GetDefaultEncoding(); |
| 596 | |
| 597 | /* Shortcuts for common default encodings */ |
| 598 | if (strcmp(encoding, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 599 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 600 | else if (strcmp(encoding, "latin-1") == 0) |
| 601 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 602 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 603 | else if (strcmp(encoding, "mbcs") == 0) |
| 604 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 605 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 606 | else if (strcmp(encoding, "ascii") == 0) |
| 607 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 608 | |
| 609 | /* Decode via the codec registry */ |
| 610 | buffer = PyBuffer_FromMemory((void *)s, size); |
| 611 | if (buffer == NULL) |
| 612 | goto onError; |
| 613 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 614 | if (unicode == NULL) |
| 615 | goto onError; |
| 616 | if (!PyUnicode_Check(unicode)) { |
| 617 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 618 | "decoder did not return an unicode object (type=%.400s)", |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 619 | unicode->ob_type->tp_name); |
| 620 | Py_DECREF(unicode); |
| 621 | goto onError; |
| 622 | } |
| 623 | Py_DECREF(buffer); |
| 624 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 625 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 626 | onError: |
| 627 | Py_XDECREF(buffer); |
| 628 | return NULL; |
| 629 | } |
| 630 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 631 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 632 | const char *encoding, |
| 633 | const char *errors) |
| 634 | { |
| 635 | PyObject *v; |
| 636 | |
| 637 | if (!PyUnicode_Check(unicode)) { |
| 638 | PyErr_BadArgument(); |
| 639 | goto onError; |
| 640 | } |
| 641 | |
| 642 | if (encoding == NULL) |
| 643 | encoding = PyUnicode_GetDefaultEncoding(); |
| 644 | |
| 645 | /* Decode via the codec registry */ |
| 646 | v = PyCodec_Decode(unicode, encoding, errors); |
| 647 | if (v == NULL) |
| 648 | goto onError; |
| 649 | return v; |
| 650 | |
| 651 | onError: |
| 652 | return NULL; |
| 653 | } |
| 654 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 655 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 656 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 657 | const char *encoding, |
| 658 | const char *errors) |
| 659 | { |
| 660 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 661 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 662 | unicode = PyUnicode_FromUnicode(s, size); |
| 663 | if (unicode == NULL) |
| 664 | return NULL; |
| 665 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 666 | Py_DECREF(unicode); |
| 667 | return v; |
| 668 | } |
| 669 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 670 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 671 | const char *encoding, |
| 672 | const char *errors) |
| 673 | { |
| 674 | PyObject *v; |
| 675 | |
| 676 | if (!PyUnicode_Check(unicode)) { |
| 677 | PyErr_BadArgument(); |
| 678 | goto onError; |
| 679 | } |
| 680 | |
| 681 | if (encoding == NULL) |
| 682 | encoding = PyUnicode_GetDefaultEncoding(); |
| 683 | |
| 684 | /* Encode via the codec registry */ |
| 685 | v = PyCodec_Encode(unicode, encoding, errors); |
| 686 | if (v == NULL) |
| 687 | goto onError; |
| 688 | return v; |
| 689 | |
| 690 | onError: |
| 691 | return NULL; |
| 692 | } |
| 693 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 694 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 695 | const char *encoding, |
| 696 | const char *errors) |
| 697 | { |
| 698 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 699 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 700 | if (!PyUnicode_Check(unicode)) { |
| 701 | PyErr_BadArgument(); |
| 702 | goto onError; |
| 703 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 704 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 705 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 706 | encoding = PyUnicode_GetDefaultEncoding(); |
| 707 | |
| 708 | /* Shortcuts for common default encodings */ |
| 709 | if (errors == NULL) { |
| 710 | if (strcmp(encoding, "utf-8") == 0) |
Jeremy Hylton | 9cea41c | 2001-05-29 17:13:15 +0000 | [diff] [blame] | 711 | return PyUnicode_AsUTF8String(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 712 | else if (strcmp(encoding, "latin-1") == 0) |
| 713 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 714 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 715 | else if (strcmp(encoding, "mbcs") == 0) |
| 716 | return PyUnicode_AsMBCSString(unicode); |
| 717 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 718 | else if (strcmp(encoding, "ascii") == 0) |
| 719 | return PyUnicode_AsASCIIString(unicode); |
| 720 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 721 | |
| 722 | /* Encode via the codec registry */ |
| 723 | v = PyCodec_Encode(unicode, encoding, errors); |
| 724 | if (v == NULL) |
| 725 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 726 | if (!PyString_Check(v)) { |
| 727 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 728 | "encoder did not return a string object (type=%.400s)", |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 729 | v->ob_type->tp_name); |
| 730 | Py_DECREF(v); |
| 731 | goto onError; |
| 732 | } |
| 733 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 734 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 735 | onError: |
| 736 | return NULL; |
| 737 | } |
| 738 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 739 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 740 | const char *errors) |
| 741 | { |
| 742 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
| 743 | |
| 744 | if (v) |
| 745 | return v; |
| 746 | v = PyUnicode_AsEncodedString(unicode, NULL, errors); |
| 747 | if (v && errors == NULL) |
| 748 | ((PyUnicodeObject *)unicode)->defenc = v; |
| 749 | return v; |
| 750 | } |
| 751 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 752 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 753 | { |
| 754 | if (!PyUnicode_Check(unicode)) { |
| 755 | PyErr_BadArgument(); |
| 756 | goto onError; |
| 757 | } |
| 758 | return PyUnicode_AS_UNICODE(unicode); |
| 759 | |
| 760 | onError: |
| 761 | return NULL; |
| 762 | } |
| 763 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 764 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 765 | { |
| 766 | if (!PyUnicode_Check(unicode)) { |
| 767 | PyErr_BadArgument(); |
| 768 | goto onError; |
| 769 | } |
| 770 | return PyUnicode_GET_SIZE(unicode); |
| 771 | |
| 772 | onError: |
| 773 | return -1; |
| 774 | } |
| 775 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 776 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 777 | { |
| 778 | return unicode_default_encoding; |
| 779 | } |
| 780 | |
| 781 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 782 | { |
| 783 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 784 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 785 | /* Make sure the encoding is valid. As side effect, this also |
| 786 | loads the encoding into the codec registry cache. */ |
| 787 | v = _PyCodec_Lookup(encoding); |
| 788 | if (v == NULL) |
| 789 | goto onError; |
| 790 | Py_DECREF(v); |
| 791 | strncpy(unicode_default_encoding, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 792 | encoding, |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 793 | sizeof(unicode_default_encoding)); |
| 794 | return 0; |
| 795 | |
| 796 | onError: |
| 797 | return -1; |
| 798 | } |
| 799 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 800 | /* error handling callback helper: |
| 801 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 802 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 803 | and adjust various state variables. |
| 804 | return 0 on success, -1 on error |
| 805 | */ |
| 806 | |
| 807 | static |
| 808 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 809 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 810 | const char *input, Py_ssize_t insize, Py_ssize_t *startinpos, Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 811 | PyObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 812 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 813 | 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] | 814 | |
| 815 | PyObject *restuple = NULL; |
| 816 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 817 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
| 818 | Py_ssize_t requiredsize; |
| 819 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 820 | Py_UNICODE *repptr; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 821 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 822 | int res = -1; |
| 823 | |
| 824 | if (*errorHandler == NULL) { |
| 825 | *errorHandler = PyCodec_LookupError(errors); |
| 826 | if (*errorHandler == NULL) |
| 827 | goto onError; |
| 828 | } |
| 829 | |
| 830 | if (*exceptionObject == NULL) { |
| 831 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 832 | encoding, input, insize, *startinpos, *endinpos, reason); |
| 833 | if (*exceptionObject == NULL) |
| 834 | goto onError; |
| 835 | } |
| 836 | else { |
| 837 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 838 | goto onError; |
| 839 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 840 | goto onError; |
| 841 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 842 | goto onError; |
| 843 | } |
| 844 | |
| 845 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 846 | if (restuple == NULL) |
| 847 | goto onError; |
| 848 | if (!PyTuple_Check(restuple)) { |
| 849 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 850 | goto onError; |
| 851 | } |
| 852 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 853 | goto onError; |
| 854 | if (newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 855 | newpos = insize+newpos; |
| 856 | if (newpos<0 || newpos>insize) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 857 | 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] | 858 | goto onError; |
| 859 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 860 | |
| 861 | /* need more space? (at least enough for what we |
| 862 | have+the replacement+the rest of the string (starting |
| 863 | at the new input position), so we won't have to check space |
| 864 | when there are no errors in the rest of the string) */ |
| 865 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 866 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 867 | requiredsize = *outpos + repsize + insize-newpos; |
| 868 | if (requiredsize > outsize) { |
| 869 | if (requiredsize<2*outsize) |
| 870 | requiredsize = 2*outsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 871 | if (PyUnicode_Resize(output, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 872 | goto onError; |
| 873 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
| 874 | } |
| 875 | *endinpos = newpos; |
| 876 | *inptr = input + newpos; |
| 877 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 878 | *outptr += repsize; |
| 879 | *outpos += repsize; |
| 880 | /* we made it! */ |
| 881 | res = 0; |
| 882 | |
| 883 | onError: |
| 884 | Py_XDECREF(restuple); |
| 885 | return res; |
| 886 | } |
| 887 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 888 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 889 | |
| 890 | /* see RFC2152 for details */ |
| 891 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 892 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 893 | char utf7_special[128] = { |
| 894 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 895 | encoded: |
| 896 | 0 - not special |
| 897 | 1 - special |
| 898 | 2 - whitespace (optional) |
| 899 | 3 - RFC2152 Set O (optional) */ |
| 900 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 901 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 902 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 903 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 904 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 905 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 906 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 907 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 908 | |
| 909 | }; |
| 910 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 911 | /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
| 912 | warnings about the comparison always being false; since |
| 913 | utf7_special[0] is 1, we can safely make that one comparison |
| 914 | true */ |
| 915 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 916 | #define SPECIAL(c, encodeO, encodeWS) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 917 | ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 918 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 919 | (encodeO && (utf7_special[(c)] == 3))) |
| 920 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 921 | #define B64(n) \ |
| 922 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 923 | #define B64CHAR(c) \ |
| 924 | (isalnum(c) || (c) == '+' || (c) == '/') |
| 925 | #define UB64(c) \ |
| 926 | ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
| 927 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 928 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 929 | #define ENCODE(out, ch, bits) \ |
| 930 | while (bits >= 6) { \ |
| 931 | *out++ = B64(ch >> (bits-6)); \ |
| 932 | bits -= 6; \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 935 | #define DECODE(out, ch, bits, surrogate) \ |
| 936 | while (bits >= 16) { \ |
| 937 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 938 | bits -= 16; \ |
| 939 | if (surrogate) { \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 940 | /* We have already generated an error for the high surrogate \ |
| 941 | 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] | 942 | surrogate = 0; \ |
| 943 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 944 | /* This is a surrogate pair. Unfortunately we can't represent \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 945 | it in a 16-bit character */ \ |
| 946 | surrogate = 1; \ |
| 947 | errmsg = "code pairs are not supported"; \ |
| 948 | goto utf7Error; \ |
| 949 | } else { \ |
| 950 | *out++ = outCh; \ |
| 951 | } \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 952 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 953 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 954 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 955 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 956 | const char *errors) |
| 957 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 958 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 959 | Py_ssize_t startinpos; |
| 960 | Py_ssize_t endinpos; |
| 961 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 962 | const char *e; |
| 963 | PyUnicodeObject *unicode; |
| 964 | Py_UNICODE *p; |
| 965 | const char *errmsg = ""; |
| 966 | int inShift = 0; |
| 967 | unsigned int bitsleft = 0; |
| 968 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 969 | int surrogate = 0; |
| 970 | PyObject *errorHandler = NULL; |
| 971 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 972 | |
| 973 | unicode = _PyUnicode_New(size); |
| 974 | if (!unicode) |
| 975 | return NULL; |
| 976 | if (size == 0) |
| 977 | return (PyObject *)unicode; |
| 978 | |
| 979 | p = unicode->str; |
| 980 | e = s + size; |
| 981 | |
| 982 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 983 | Py_UNICODE ch; |
| 984 | restart: |
| 985 | ch = *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 986 | |
| 987 | if (inShift) { |
| 988 | if ((ch == '-') || !B64CHAR(ch)) { |
| 989 | inShift = 0; |
| 990 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 991 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 992 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 993 | if (bitsleft >= 6) { |
| 994 | /* The shift sequence has a partial character in it. If |
| 995 | bitsleft < 6 then we could just classify it as padding |
| 996 | but that is not the case here */ |
| 997 | |
| 998 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 999 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1000 | } |
| 1001 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1002 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1003 | here so indicate the potential of a misencoded character. */ |
| 1004 | |
| 1005 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 1006 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 1007 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1008 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | if (ch == '-') { |
| 1012 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1013 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1014 | inShift = 1; |
| 1015 | } |
| 1016 | } else if (SPECIAL(ch,0,0)) { |
| 1017 | errmsg = "unexpected special character"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1018 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1019 | } else { |
| 1020 | *p++ = ch; |
| 1021 | } |
| 1022 | } else { |
| 1023 | charsleft = (charsleft << 6) | UB64(ch); |
| 1024 | bitsleft += 6; |
| 1025 | s++; |
| 1026 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1027 | } |
| 1028 | } |
| 1029 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1030 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1031 | s++; |
| 1032 | if (s < e && *s == '-') { |
| 1033 | s++; |
| 1034 | *p++ = '+'; |
| 1035 | } else |
| 1036 | { |
| 1037 | inShift = 1; |
| 1038 | bitsleft = 0; |
| 1039 | } |
| 1040 | } |
| 1041 | else if (SPECIAL(ch,0,0)) { |
| 1042 | errmsg = "unexpected special character"; |
| 1043 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1044 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1045 | } |
| 1046 | else { |
| 1047 | *p++ = ch; |
| 1048 | s++; |
| 1049 | } |
| 1050 | continue; |
| 1051 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1052 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1053 | endinpos = s-starts; |
| 1054 | if (unicode_decode_call_errorhandler( |
| 1055 | errors, &errorHandler, |
| 1056 | "utf7", errmsg, |
| 1057 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1058 | (PyObject **)&unicode, &outpos, &p)) |
| 1059 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | if (inShift) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1063 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1064 | endinpos = size; |
| 1065 | if (unicode_decode_call_errorhandler( |
| 1066 | errors, &errorHandler, |
| 1067 | "utf7", "unterminated shift sequence", |
| 1068 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1069 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1070 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1071 | if (s < e) |
| 1072 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1075 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1076 | goto onError; |
| 1077 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1078 | Py_XDECREF(errorHandler); |
| 1079 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1080 | return (PyObject *)unicode; |
| 1081 | |
| 1082 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1083 | Py_XDECREF(errorHandler); |
| 1084 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1085 | Py_DECREF(unicode); |
| 1086 | return NULL; |
| 1087 | } |
| 1088 | |
| 1089 | |
| 1090 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1091 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1092 | int encodeSetO, |
| 1093 | int encodeWhiteSpace, |
| 1094 | const char *errors) |
| 1095 | { |
| 1096 | PyObject *v; |
| 1097 | /* It might be possible to tighten this worst case */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1098 | Py_ssize_t cbAllocated = 5 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1099 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1100 | Py_ssize_t i = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1101 | unsigned int bitsleft = 0; |
| 1102 | unsigned long charsleft = 0; |
| 1103 | char * out; |
| 1104 | char * start; |
| 1105 | |
| 1106 | if (size == 0) |
| 1107 | return PyString_FromStringAndSize(NULL, 0); |
| 1108 | |
| 1109 | v = PyString_FromStringAndSize(NULL, cbAllocated); |
| 1110 | if (v == NULL) |
| 1111 | return NULL; |
| 1112 | |
| 1113 | start = out = PyString_AS_STRING(v); |
| 1114 | for (;i < size; ++i) { |
| 1115 | Py_UNICODE ch = s[i]; |
| 1116 | |
| 1117 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1118 | if (ch == '+') { |
| 1119 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1120 | *out++ = '-'; |
| 1121 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1122 | charsleft = ch; |
| 1123 | bitsleft = 16; |
| 1124 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1125 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1126 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1127 | } else { |
| 1128 | *out++ = (char) ch; |
| 1129 | } |
| 1130 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1131 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1132 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1133 | charsleft = 0; |
| 1134 | bitsleft = 0; |
| 1135 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1136 | so no '-' is required, except if the character is itself a '-' */ |
| 1137 | if (B64CHAR(ch) || ch == '-') { |
| 1138 | *out++ = '-'; |
| 1139 | } |
| 1140 | inShift = 0; |
| 1141 | *out++ = (char) ch; |
| 1142 | } else { |
| 1143 | bitsleft += 16; |
| 1144 | charsleft = (charsleft << 16) | ch; |
| 1145 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1146 | |
| 1147 | /* If the next character is special then we dont' need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1148 | 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] | 1149 | or '-' then the shift sequence will be terminated implicitly and we |
| 1150 | don't have to insert a '-'. */ |
| 1151 | |
| 1152 | if (bitsleft == 0) { |
| 1153 | if (i + 1 < size) { |
| 1154 | Py_UNICODE ch2 = s[i+1]; |
| 1155 | |
| 1156 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1157 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1158 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1159 | *out++ = '-'; |
| 1160 | inShift = 0; |
| 1161 | } else { |
| 1162 | inShift = 0; |
| 1163 | } |
| 1164 | |
| 1165 | } |
| 1166 | else { |
| 1167 | *out++ = '-'; |
| 1168 | inShift = 0; |
| 1169 | } |
| 1170 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1171 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1172 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1173 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1174 | if (bitsleft) { |
| 1175 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1176 | *out++ = '-'; |
| 1177 | } |
| 1178 | |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 1179 | _PyString_Resize(&v, out - start); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1180 | return v; |
| 1181 | } |
| 1182 | |
| 1183 | #undef SPECIAL |
| 1184 | #undef B64 |
| 1185 | #undef B64CHAR |
| 1186 | #undef UB64 |
| 1187 | #undef ENCODE |
| 1188 | #undef DECODE |
| 1189 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1190 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1191 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1192 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1193 | char utf8_code_length[256] = { |
| 1194 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1195 | illegal prefix. see RFC 2279 for details */ |
| 1196 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1197 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1198 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1199 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1200 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1201 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1202 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1203 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1204 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1205 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1206 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1207 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1208 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1209 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1210 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1211 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1212 | }; |
| 1213 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1214 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1215 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1216 | const char *errors) |
| 1217 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1218 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 1219 | } |
| 1220 | |
| 1221 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1222 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1223 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1224 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1225 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1226 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1227 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1228 | Py_ssize_t startinpos; |
| 1229 | Py_ssize_t endinpos; |
| 1230 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1231 | const char *e; |
| 1232 | PyUnicodeObject *unicode; |
| 1233 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1234 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1235 | PyObject *errorHandler = NULL; |
| 1236 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1237 | |
| 1238 | /* Note: size will always be longer than the resulting Unicode |
| 1239 | character count */ |
| 1240 | unicode = _PyUnicode_New(size); |
| 1241 | if (!unicode) |
| 1242 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1243 | if (size == 0) { |
| 1244 | if (consumed) |
| 1245 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1246 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1247 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1248 | |
| 1249 | /* Unpack UTF-8 encoded data */ |
| 1250 | p = unicode->str; |
| 1251 | e = s + size; |
| 1252 | |
| 1253 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1254 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1255 | |
| 1256 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1257 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1258 | s++; |
| 1259 | continue; |
| 1260 | } |
| 1261 | |
| 1262 | n = utf8_code_length[ch]; |
| 1263 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1264 | if (s + n > e) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1265 | if (consumed) |
| 1266 | break; |
| 1267 | else { |
| 1268 | errmsg = "unexpected end of data"; |
| 1269 | startinpos = s-starts; |
| 1270 | endinpos = size; |
| 1271 | goto utf8Error; |
| 1272 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1273 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1274 | |
| 1275 | switch (n) { |
| 1276 | |
| 1277 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1278 | errmsg = "unexpected code byte"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1279 | startinpos = s-starts; |
| 1280 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1281 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1282 | |
| 1283 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1284 | errmsg = "internal error"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1285 | startinpos = s-starts; |
| 1286 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1287 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1288 | |
| 1289 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1290 | if ((s[1] & 0xc0) != 0x80) { |
| 1291 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1292 | startinpos = s-starts; |
| 1293 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1294 | goto utf8Error; |
| 1295 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1296 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1297 | if (ch < 0x80) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1298 | startinpos = s-starts; |
| 1299 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1300 | errmsg = "illegal encoding"; |
| 1301 | goto utf8Error; |
| 1302 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1303 | else |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1304 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1305 | break; |
| 1306 | |
| 1307 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1308 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1309 | (s[2] & 0xc0) != 0x80) { |
| 1310 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1311 | startinpos = s-starts; |
| 1312 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1313 | goto utf8Error; |
| 1314 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1315 | 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] | 1316 | if (ch < 0x0800) { |
| 1317 | /* Note: UTF-8 encodings of surrogates are considered |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1318 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1319 | |
| 1320 | XXX For wide builds (UCS-4) we should probably try |
| 1321 | to recombine the surrogates into a single code |
| 1322 | unit. |
| 1323 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1324 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1325 | startinpos = s-starts; |
| 1326 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1327 | goto utf8Error; |
| 1328 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1329 | else |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1330 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1331 | break; |
| 1332 | |
| 1333 | case 4: |
| 1334 | if ((s[1] & 0xc0) != 0x80 || |
| 1335 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1336 | (s[3] & 0xc0) != 0x80) { |
| 1337 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1338 | startinpos = s-starts; |
| 1339 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1340 | goto utf8Error; |
| 1341 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1342 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 1343 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 1344 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1345 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1346 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1347 | || (ch > 0x10ffff)) /* maximum value allowed for |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1348 | UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1349 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1350 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1351 | startinpos = s-starts; |
| 1352 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1353 | goto utf8Error; |
| 1354 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1355 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1356 | *p++ = (Py_UNICODE)ch; |
| 1357 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1358 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1359 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1360 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 1361 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1362 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1363 | /* high surrogate = top 10 bits added to D800 */ |
| 1364 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1365 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1366 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1367 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1368 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1369 | break; |
| 1370 | |
| 1371 | default: |
| 1372 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1373 | errmsg = "unsupported Unicode code range"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1374 | startinpos = s-starts; |
| 1375 | endinpos = startinpos+n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1376 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1377 | } |
| 1378 | s += n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1379 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1380 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1381 | utf8Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1382 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1383 | if (unicode_decode_call_errorhandler( |
| 1384 | errors, &errorHandler, |
| 1385 | "utf8", errmsg, |
| 1386 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1387 | (PyObject **)&unicode, &outpos, &p)) |
| 1388 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1389 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1390 | if (consumed) |
| 1391 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1392 | |
| 1393 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1394 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1395 | goto onError; |
| 1396 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1397 | Py_XDECREF(errorHandler); |
| 1398 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1399 | return (PyObject *)unicode; |
| 1400 | |
| 1401 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1402 | Py_XDECREF(errorHandler); |
| 1403 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1404 | Py_DECREF(unicode); |
| 1405 | return NULL; |
| 1406 | } |
| 1407 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1408 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 1409 | and allocate exactly as much space needed at the end. Else allocate the |
| 1410 | maximum possible needed (4 result bytes per Unicode character), and return |
| 1411 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1412 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1413 | PyObject * |
| 1414 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1415 | Py_ssize_t size, |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1416 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1417 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1418 | #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] | 1419 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1420 | Py_ssize_t i; /* index into s of next input byte */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1421 | PyObject *v; /* result string object */ |
| 1422 | char *p; /* next free byte in output buffer */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1423 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1424 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1425 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1426 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1427 | assert(s != NULL); |
| 1428 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1429 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1430 | if (size <= MAX_SHORT_UNICHARS) { |
| 1431 | /* Write into the stack buffer; nallocated can't overflow. |
| 1432 | * At the end, we'll allocate exactly as much heap space as it |
| 1433 | * turns out we need. |
| 1434 | */ |
| 1435 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
| 1436 | v = NULL; /* will allocate after we're done */ |
| 1437 | p = stackbuf; |
| 1438 | } |
| 1439 | else { |
| 1440 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 1441 | nallocated = size * 4; |
| 1442 | if (nallocated / 4 != size) /* overflow! */ |
| 1443 | return PyErr_NoMemory(); |
| 1444 | v = PyString_FromStringAndSize(NULL, nallocated); |
| 1445 | if (v == NULL) |
| 1446 | return NULL; |
| 1447 | p = PyString_AS_STRING(v); |
| 1448 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1449 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1450 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1451 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1452 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1453 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1454 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1455 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1456 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1457 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1458 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 1459 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 1460 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1461 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1462 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1463 | /* Encode UCS2 Unicode ordinals */ |
| 1464 | if (ch < 0x10000) { |
| 1465 | /* Special case: check for high surrogate */ |
| 1466 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 1467 | Py_UCS4 ch2 = s[i]; |
| 1468 | /* Check for low surrogate and combine the two to |
| 1469 | form a UCS4 value */ |
| 1470 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1471 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1472 | i++; |
| 1473 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1474 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1475 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1476 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1477 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1478 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1479 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1480 | continue; |
| 1481 | } |
| 1482 | encodeUCS4: |
| 1483 | /* Encode UCS4 Unicode ordinals */ |
| 1484 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 1485 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 1486 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1487 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1488 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1489 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 1490 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1491 | if (v == NULL) { |
| 1492 | /* This was stack allocated. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1493 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1494 | assert(nneeded <= nallocated); |
| 1495 | v = PyString_FromStringAndSize(stackbuf, nneeded); |
| 1496 | } |
| 1497 | else { |
| 1498 | /* Cut back to size actually needed. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1499 | nneeded = p - PyString_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1500 | assert(nneeded <= nallocated); |
| 1501 | _PyString_Resize(&v, nneeded); |
| 1502 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1503 | return v; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1504 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1505 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1508 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 1509 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1510 | if (!PyUnicode_Check(unicode)) { |
| 1511 | PyErr_BadArgument(); |
| 1512 | return NULL; |
| 1513 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 1514 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1515 | PyUnicode_GET_SIZE(unicode), |
| 1516 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 1520 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1521 | PyObject * |
| 1522 | PyUnicode_DecodeUTF16(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1523 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1524 | const char *errors, |
| 1525 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1526 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1527 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 1528 | } |
| 1529 | |
| 1530 | PyObject * |
| 1531 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1532 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1533 | const char *errors, |
| 1534 | int *byteorder, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1535 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1536 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1537 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1538 | Py_ssize_t startinpos; |
| 1539 | Py_ssize_t endinpos; |
| 1540 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1541 | PyUnicodeObject *unicode; |
| 1542 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1543 | const unsigned char *q, *e; |
| 1544 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1545 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1546 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 1547 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1548 | int ihi = 1, ilo = 0; |
| 1549 | #else |
| 1550 | int ihi = 0, ilo = 1; |
| 1551 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1552 | PyObject *errorHandler = NULL; |
| 1553 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1554 | |
| 1555 | /* Note: size will always be longer than the resulting Unicode |
| 1556 | character count */ |
| 1557 | unicode = _PyUnicode_New(size); |
| 1558 | if (!unicode) |
| 1559 | return NULL; |
| 1560 | if (size == 0) |
| 1561 | return (PyObject *)unicode; |
| 1562 | |
| 1563 | /* Unpack UTF-16 encoded data */ |
| 1564 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1565 | q = (unsigned char *)s; |
| 1566 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1567 | |
| 1568 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1569 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1570 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1571 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 1572 | byte order setting accordingly. In native mode, the leading BOM |
| 1573 | mark is skipped, in all other modes, it is copied to the output |
| 1574 | stream as-is (giving a ZWNBSP character). */ |
| 1575 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1576 | if (size >= 2) { |
| 1577 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1578 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1579 | if (bom == 0xFEFF) { |
| 1580 | q += 2; |
| 1581 | bo = -1; |
| 1582 | } |
| 1583 | else if (bom == 0xFFFE) { |
| 1584 | q += 2; |
| 1585 | bo = 1; |
| 1586 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1587 | #else |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1588 | if (bom == 0xFEFF) { |
| 1589 | q += 2; |
| 1590 | bo = 1; |
| 1591 | } |
| 1592 | else if (bom == 0xFFFE) { |
| 1593 | q += 2; |
| 1594 | bo = -1; |
| 1595 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1596 | #endif |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1597 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1598 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1599 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1600 | if (bo == -1) { |
| 1601 | /* force LE */ |
| 1602 | ihi = 1; |
| 1603 | ilo = 0; |
| 1604 | } |
| 1605 | else if (bo == 1) { |
| 1606 | /* force BE */ |
| 1607 | ihi = 0; |
| 1608 | ilo = 1; |
| 1609 | } |
| 1610 | |
| 1611 | while (q < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1612 | Py_UNICODE ch; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1613 | /* remaining bytes at the end? (size should be even) */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1614 | if (e-q<2) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1615 | if (consumed) |
| 1616 | break; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1617 | errmsg = "truncated data"; |
| 1618 | startinpos = ((const char *)q)-starts; |
| 1619 | endinpos = ((const char *)e)-starts; |
| 1620 | goto utf16Error; |
| 1621 | /* The remaining input chars are ignored if the callback |
| 1622 | chooses to skip the input */ |
| 1623 | } |
| 1624 | ch = (q[ihi] << 8) | q[ilo]; |
| 1625 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1626 | q += 2; |
| 1627 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1628 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 1629 | *p++ = ch; |
| 1630 | continue; |
| 1631 | } |
| 1632 | |
| 1633 | /* UTF-16 code pair: */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1634 | if (q >= e) { |
| 1635 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1636 | startinpos = (((const char *)q)-2)-starts; |
| 1637 | endinpos = ((const char *)e)-starts; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1638 | goto utf16Error; |
| 1639 | } |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 1640 | if (0xD800 <= ch && ch <= 0xDBFF) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1641 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 1642 | q += 2; |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 1643 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1644 | #ifndef Py_UNICODE_WIDE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1645 | *p++ = ch; |
| 1646 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1647 | #else |
| 1648 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1649 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1650 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1651 | } |
| 1652 | else { |
| 1653 | errmsg = "illegal UTF-16 surrogate"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1654 | startinpos = (((const char *)q)-4)-starts; |
| 1655 | endinpos = startinpos+2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1656 | goto utf16Error; |
| 1657 | } |
| 1658 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1659 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1660 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1661 | startinpos = (((const char *)q)-2)-starts; |
| 1662 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1663 | /* Fall through to report the error */ |
| 1664 | |
| 1665 | utf16Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1666 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1667 | if (unicode_decode_call_errorhandler( |
| 1668 | errors, &errorHandler, |
| 1669 | "utf16", errmsg, |
| 1670 | starts, size, &startinpos, &endinpos, &exc, (const char **)&q, |
| 1671 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1672 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | if (byteorder) |
| 1676 | *byteorder = bo; |
| 1677 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1678 | if (consumed) |
| 1679 | *consumed = (const char *)q-starts; |
| 1680 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1681 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1682 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1683 | goto onError; |
| 1684 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1685 | Py_XDECREF(errorHandler); |
| 1686 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1687 | return (PyObject *)unicode; |
| 1688 | |
| 1689 | onError: |
| 1690 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1691 | Py_XDECREF(errorHandler); |
| 1692 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1693 | return NULL; |
| 1694 | } |
| 1695 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1696 | PyObject * |
| 1697 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1698 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1699 | const char *errors, |
| 1700 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1701 | { |
| 1702 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1703 | unsigned char *p; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1704 | #ifdef Py_UNICODE_WIDE |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1705 | int i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1706 | #else |
| 1707 | const int pairs = 0; |
| 1708 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1709 | /* Offsets from p for storing byte pairs in the right order. */ |
| 1710 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1711 | int ihi = 1, ilo = 0; |
| 1712 | #else |
| 1713 | int ihi = 0, ilo = 1; |
| 1714 | #endif |
| 1715 | |
| 1716 | #define STORECHAR(CH) \ |
| 1717 | do { \ |
| 1718 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 1719 | p[ilo] = (CH) & 0xff; \ |
| 1720 | p += 2; \ |
| 1721 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1722 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1723 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1724 | for (i = pairs = 0; i < size; i++) |
| 1725 | if (s[i] >= 0x10000) |
| 1726 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1727 | #endif |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1728 | v = PyString_FromStringAndSize(NULL, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1729 | 2 * (size + pairs + (byteorder == 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1730 | if (v == NULL) |
| 1731 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1732 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1733 | p = (unsigned char *)PyString_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1734 | if (byteorder == 0) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1735 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 1736 | if (size == 0) |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 1737 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1738 | |
| 1739 | if (byteorder == -1) { |
| 1740 | /* force LE */ |
| 1741 | ihi = 1; |
| 1742 | ilo = 0; |
| 1743 | } |
| 1744 | else if (byteorder == 1) { |
| 1745 | /* force BE */ |
| 1746 | ihi = 0; |
| 1747 | ilo = 1; |
| 1748 | } |
| 1749 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1750 | while (size-- > 0) { |
| 1751 | Py_UNICODE ch = *s++; |
| 1752 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1753 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1754 | if (ch >= 0x10000) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1755 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 1756 | ch = 0xD800 | ((ch-0x10000) >> 10); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1757 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1758 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1759 | STORECHAR(ch); |
| 1760 | if (ch2) |
| 1761 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1762 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1763 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1764 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1765 | } |
| 1766 | |
| 1767 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 1768 | { |
| 1769 | if (!PyUnicode_Check(unicode)) { |
| 1770 | PyErr_BadArgument(); |
| 1771 | return NULL; |
| 1772 | } |
| 1773 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 1774 | PyUnicode_GET_SIZE(unicode), |
| 1775 | NULL, |
| 1776 | 0); |
| 1777 | } |
| 1778 | |
| 1779 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 1780 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1781 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 1782 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1783 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1784 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1785 | const char *errors) |
| 1786 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1787 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1788 | Py_ssize_t startinpos; |
| 1789 | Py_ssize_t endinpos; |
| 1790 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1791 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1792 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1793 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1794 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1795 | char* message; |
| 1796 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1797 | PyObject *errorHandler = NULL; |
| 1798 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1799 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1800 | /* Escaped strings will always be longer than the resulting |
| 1801 | 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] | 1802 | length after conversion to the true value. |
| 1803 | (but if the error callback returns a long replacement string |
| 1804 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1805 | v = _PyUnicode_New(size); |
| 1806 | if (v == NULL) |
| 1807 | goto onError; |
| 1808 | if (size == 0) |
| 1809 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1810 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1811 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1812 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1813 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1814 | while (s < end) { |
| 1815 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 1816 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1817 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1818 | |
| 1819 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 1820 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1821 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1822 | continue; |
| 1823 | } |
| 1824 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1825 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1826 | /* \ - Escapes */ |
| 1827 | s++; |
| 1828 | switch (*s++) { |
| 1829 | |
| 1830 | /* \x escapes */ |
| 1831 | case '\n': break; |
| 1832 | case '\\': *p++ = '\\'; break; |
| 1833 | case '\'': *p++ = '\''; break; |
| 1834 | case '\"': *p++ = '\"'; break; |
| 1835 | case 'b': *p++ = '\b'; break; |
| 1836 | case 'f': *p++ = '\014'; break; /* FF */ |
| 1837 | case 't': *p++ = '\t'; break; |
| 1838 | case 'n': *p++ = '\n'; break; |
| 1839 | case 'r': *p++ = '\r'; break; |
| 1840 | case 'v': *p++ = '\013'; break; /* VT */ |
| 1841 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 1842 | |
| 1843 | /* \OOO (octal) escapes */ |
| 1844 | case '0': case '1': case '2': case '3': |
| 1845 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1846 | x = s[-1] - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1847 | if ('0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1848 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1849 | if ('0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1850 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1851 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1852 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1853 | break; |
| 1854 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1855 | /* hex escapes */ |
| 1856 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1857 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1858 | digits = 2; |
| 1859 | message = "truncated \\xXX escape"; |
| 1860 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1861 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1862 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1863 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1864 | digits = 4; |
| 1865 | message = "truncated \\uXXXX escape"; |
| 1866 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1867 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1868 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1869 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1870 | digits = 8; |
| 1871 | message = "truncated \\UXXXXXXXX escape"; |
| 1872 | hexescape: |
| 1873 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1874 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1875 | if (s+digits>end) { |
| 1876 | endinpos = size; |
| 1877 | if (unicode_decode_call_errorhandler( |
| 1878 | errors, &errorHandler, |
| 1879 | "unicodeescape", "end of string in escape sequence", |
| 1880 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1881 | (PyObject **)&v, &outpos, &p)) |
| 1882 | goto onError; |
| 1883 | goto nextByte; |
| 1884 | } |
| 1885 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1886 | c = (unsigned char) s[i]; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1887 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1888 | endinpos = (s+i+1)-starts; |
| 1889 | if (unicode_decode_call_errorhandler( |
| 1890 | errors, &errorHandler, |
| 1891 | "unicodeescape", message, |
| 1892 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1893 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1894 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1895 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1896 | } |
| 1897 | chr = (chr<<4) & ~0xF; |
| 1898 | if (c >= '0' && c <= '9') |
| 1899 | chr += c - '0'; |
| 1900 | else if (c >= 'a' && c <= 'f') |
| 1901 | chr += 10 + c - 'a'; |
| 1902 | else |
| 1903 | chr += 10 + c - 'A'; |
| 1904 | } |
| 1905 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 1906 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1907 | /* _decoding_error will have already written into the |
| 1908 | target buffer. */ |
| 1909 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1910 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1911 | /* when we get here, chr is a 32-bit unicode character */ |
| 1912 | if (chr <= 0xffff) |
| 1913 | /* UCS-2 character */ |
| 1914 | *p++ = (Py_UNICODE) chr; |
| 1915 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1916 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1917 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1918 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1919 | *p++ = chr; |
| 1920 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1921 | chr -= 0x10000L; |
| 1922 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1923 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1924 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1925 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1926 | endinpos = s-starts; |
| 1927 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1928 | if (unicode_decode_call_errorhandler( |
| 1929 | errors, &errorHandler, |
| 1930 | "unicodeescape", "illegal Unicode character", |
| 1931 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1932 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1933 | goto onError; |
| 1934 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1935 | break; |
| 1936 | |
| 1937 | /* \N{name} */ |
| 1938 | case 'N': |
| 1939 | message = "malformed \\N character escape"; |
| 1940 | if (ucnhash_CAPI == NULL) { |
| 1941 | /* load the unicode data module */ |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1942 | PyObject *m, *api; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1943 | m = PyImport_ImportModule("unicodedata"); |
| 1944 | if (m == NULL) |
| 1945 | goto ucnhashError; |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1946 | api = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1947 | Py_DECREF(m); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1948 | if (api == NULL) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1949 | goto ucnhashError; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 1950 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1951 | Py_DECREF(api); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1952 | if (ucnhash_CAPI == NULL) |
| 1953 | goto ucnhashError; |
| 1954 | } |
| 1955 | if (*s == '{') { |
| 1956 | const char *start = s+1; |
| 1957 | /* look for the closing brace */ |
| 1958 | while (*s != '}' && s < end) |
| 1959 | s++; |
| 1960 | if (s > start && s < end && *s == '}') { |
| 1961 | /* found a name. look it up in the unicode database */ |
| 1962 | message = "unknown Unicode character name"; |
| 1963 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 1964 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1965 | goto store; |
| 1966 | } |
| 1967 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1968 | endinpos = s-starts; |
| 1969 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1970 | if (unicode_decode_call_errorhandler( |
| 1971 | errors, &errorHandler, |
| 1972 | "unicodeescape", message, |
| 1973 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1974 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1975 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1976 | break; |
| 1977 | |
| 1978 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1979 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1980 | message = "\\ at end of string"; |
| 1981 | s--; |
| 1982 | endinpos = s-starts; |
| 1983 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1984 | if (unicode_decode_call_errorhandler( |
| 1985 | errors, &errorHandler, |
| 1986 | "unicodeescape", message, |
| 1987 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1988 | (PyObject **)&v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1989 | goto onError; |
| 1990 | } |
| 1991 | else { |
| 1992 | *p++ = '\\'; |
| 1993 | *p++ = (unsigned char)s[-1]; |
| 1994 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1995 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1996 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1997 | nextByte: |
| 1998 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1999 | } |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2000 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2001 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 2002 | Py_XDECREF(errorHandler); |
| 2003 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2004 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2005 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2006 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2007 | PyErr_SetString( |
| 2008 | PyExc_UnicodeError, |
| 2009 | "\\N escapes not supported (can't load unicodedata module)" |
| 2010 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2011 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2012 | Py_XDECREF(errorHandler); |
| 2013 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 2014 | return NULL; |
| 2015 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2016 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2017 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2018 | Py_XDECREF(errorHandler); |
| 2019 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2020 | return NULL; |
| 2021 | } |
| 2022 | |
| 2023 | /* Return a Unicode-Escape string version of the Unicode object. |
| 2024 | |
| 2025 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 2026 | appropriate. |
| 2027 | |
| 2028 | */ |
| 2029 | |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 2030 | LOCAL(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
| 2031 | Py_ssize_t size, |
| 2032 | Py_UNICODE ch) |
| 2033 | { |
| 2034 | /* like wcschr, but doesn't stop at NULL characters */ |
| 2035 | |
| 2036 | while (size-- > 0) { |
| 2037 | if (*s == ch) |
| 2038 | return s; |
| 2039 | s++; |
| 2040 | } |
| 2041 | |
| 2042 | return NULL; |
| 2043 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 2044 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2045 | static |
| 2046 | PyObject *unicodeescape_string(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2047 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2048 | int quotes) |
| 2049 | { |
| 2050 | PyObject *repr; |
| 2051 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2052 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2053 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2054 | |
| 2055 | repr = PyString_FromStringAndSize(NULL, 2 + 6*size + 1); |
| 2056 | if (repr == NULL) |
| 2057 | return NULL; |
| 2058 | |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2059 | p = PyString_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2060 | |
| 2061 | if (quotes) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2062 | *p++ = 'u'; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2063 | *p++ = (findchar(s, size, '\'') && |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2064 | !findchar(s, size, '"')) ? '"' : '\''; |
| 2065 | } |
| 2066 | while (size-- > 0) { |
| 2067 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2068 | |
Hye-Shik Chang | 835b243 | 2005-12-17 04:38:31 +0000 | [diff] [blame] | 2069 | /* Escape quotes and backslashes */ |
| 2070 | if ((quotes && |
| 2071 | ch == (Py_UNICODE) PyString_AS_STRING(repr)[1]) || ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2072 | *p++ = '\\'; |
| 2073 | *p++ = (char) ch; |
Guido van Rossum | ad9744a | 2001-09-21 15:38:17 +0000 | [diff] [blame] | 2074 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2075 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2076 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 2077 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2078 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 2079 | else if (ch >= 0x10000) { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2080 | Py_ssize_t offset = p - PyString_AS_STRING(repr); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2081 | |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2082 | /* Resize the string if necessary */ |
| 2083 | if (offset + 12 > PyString_GET_SIZE(repr)) { |
| 2084 | if (_PyString_Resize(&repr, PyString_GET_SIZE(repr) + 100)) |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2085 | return NULL; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2086 | p = PyString_AS_STRING(repr) + offset; |
| 2087 | } |
| 2088 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2089 | *p++ = '\\'; |
| 2090 | *p++ = 'U'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2091 | *p++ = hexdigit[(ch >> 28) & 0x0000000F]; |
| 2092 | *p++ = hexdigit[(ch >> 24) & 0x0000000F]; |
| 2093 | *p++ = hexdigit[(ch >> 20) & 0x0000000F]; |
| 2094 | *p++ = hexdigit[(ch >> 16) & 0x0000000F]; |
| 2095 | *p++ = hexdigit[(ch >> 12) & 0x0000000F]; |
| 2096 | *p++ = hexdigit[(ch >> 8) & 0x0000000F]; |
| 2097 | *p++ = hexdigit[(ch >> 4) & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2098 | *p++ = hexdigit[ch & 0x0000000F]; |
| 2099 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2100 | } |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 2101 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2102 | /* Map UTF-16 surrogate pairs to Unicode \UXXXXXXXX escapes */ |
| 2103 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 2104 | Py_UNICODE ch2; |
| 2105 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2106 | |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2107 | ch2 = *s++; |
| 2108 | size--; |
| 2109 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 2110 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 2111 | *p++ = '\\'; |
| 2112 | *p++ = 'U'; |
| 2113 | *p++ = hexdigit[(ucs >> 28) & 0x0000000F]; |
| 2114 | *p++ = hexdigit[(ucs >> 24) & 0x0000000F]; |
| 2115 | *p++ = hexdigit[(ucs >> 20) & 0x0000000F]; |
| 2116 | *p++ = hexdigit[(ucs >> 16) & 0x0000000F]; |
| 2117 | *p++ = hexdigit[(ucs >> 12) & 0x0000000F]; |
| 2118 | *p++ = hexdigit[(ucs >> 8) & 0x0000000F]; |
| 2119 | *p++ = hexdigit[(ucs >> 4) & 0x0000000F]; |
| 2120 | *p++ = hexdigit[ucs & 0x0000000F]; |
| 2121 | continue; |
| 2122 | } |
| 2123 | /* Fall through: isolated surrogates are copied as-is */ |
| 2124 | s--; |
| 2125 | size++; |
| 2126 | } |
| 2127 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2128 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2129 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2130 | *p++ = '\\'; |
| 2131 | *p++ = 'u'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2132 | *p++ = hexdigit[(ch >> 12) & 0x000F]; |
| 2133 | *p++ = hexdigit[(ch >> 8) & 0x000F]; |
| 2134 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2135 | *p++ = hexdigit[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2136 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2137 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2138 | /* Map special whitespace to '\t', \n', '\r' */ |
| 2139 | else if (ch == '\t') { |
| 2140 | *p++ = '\\'; |
| 2141 | *p++ = 't'; |
| 2142 | } |
| 2143 | else if (ch == '\n') { |
| 2144 | *p++ = '\\'; |
| 2145 | *p++ = 'n'; |
| 2146 | } |
| 2147 | else if (ch == '\r') { |
| 2148 | *p++ = '\\'; |
| 2149 | *p++ = 'r'; |
| 2150 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2151 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2152 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 2153 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2154 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2155 | *p++ = 'x'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2156 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2157 | *p++ = hexdigit[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2158 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2159 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2160 | /* Copy everything else as-is */ |
| 2161 | else |
| 2162 | *p++ = (char) ch; |
| 2163 | } |
| 2164 | if (quotes) |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2165 | *p++ = PyString_AS_STRING(repr)[1]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2166 | |
| 2167 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2168 | _PyString_Resize(&repr, p - PyString_AS_STRING(repr)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2169 | return repr; |
| 2170 | } |
| 2171 | |
| 2172 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2173 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2174 | { |
| 2175 | return unicodeescape_string(s, size, 0); |
| 2176 | } |
| 2177 | |
| 2178 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 2179 | { |
| 2180 | if (!PyUnicode_Check(unicode)) { |
| 2181 | PyErr_BadArgument(); |
| 2182 | return NULL; |
| 2183 | } |
| 2184 | return PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2185 | PyUnicode_GET_SIZE(unicode)); |
| 2186 | } |
| 2187 | |
| 2188 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 2189 | |
| 2190 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2191 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2192 | const char *errors) |
| 2193 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2194 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2195 | Py_ssize_t startinpos; |
| 2196 | Py_ssize_t endinpos; |
| 2197 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2198 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2199 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2200 | const char *end; |
| 2201 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2202 | PyObject *errorHandler = NULL; |
| 2203 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2204 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2205 | /* Escaped strings will always be longer than the resulting |
| 2206 | 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] | 2207 | length after conversion to the true value. (But decoding error |
| 2208 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2209 | v = _PyUnicode_New(size); |
| 2210 | if (v == NULL) |
| 2211 | goto onError; |
| 2212 | if (size == 0) |
| 2213 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2214 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2215 | end = s + size; |
| 2216 | while (s < end) { |
| 2217 | unsigned char c; |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 2218 | Py_UCS4 x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2219 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2220 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2221 | |
| 2222 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2223 | if (*s != '\\') { |
| 2224 | *p++ = (unsigned char)*s++; |
| 2225 | continue; |
| 2226 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2227 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2228 | |
| 2229 | /* \u-escapes are only interpreted iff the number of leading |
| 2230 | backslashes if odd */ |
| 2231 | bs = s; |
| 2232 | for (;s < end;) { |
| 2233 | if (*s != '\\') |
| 2234 | break; |
| 2235 | *p++ = (unsigned char)*s++; |
| 2236 | } |
| 2237 | if (((s - bs) & 1) == 0 || |
| 2238 | s >= end || |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2239 | (*s != 'u' && *s != 'U')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2240 | continue; |
| 2241 | } |
| 2242 | p--; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2243 | count = *s=='u' ? 4 : 8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2244 | s++; |
| 2245 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2246 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2247 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2248 | for (x = 0, i = 0; i < count; ++i, ++s) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2249 | c = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2250 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2251 | endinpos = s-starts; |
| 2252 | if (unicode_decode_call_errorhandler( |
| 2253 | errors, &errorHandler, |
| 2254 | "rawunicodeescape", "truncated \\uXXXX", |
| 2255 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2256 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2257 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2258 | goto nextByte; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2259 | } |
| 2260 | x = (x<<4) & ~0xF; |
| 2261 | if (c >= '0' && c <= '9') |
| 2262 | x += c - '0'; |
| 2263 | else if (c >= 'a' && c <= 'f') |
| 2264 | x += 10 + c - 'a'; |
| 2265 | else |
| 2266 | x += 10 + c - 'A'; |
| 2267 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2268 | #ifndef Py_UNICODE_WIDE |
| 2269 | if (x > 0x10000) { |
| 2270 | if (unicode_decode_call_errorhandler( |
| 2271 | errors, &errorHandler, |
| 2272 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
| 2273 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2274 | (PyObject **)&v, &outpos, &p)) |
| 2275 | goto onError; |
| 2276 | } |
| 2277 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2278 | *p++ = x; |
| 2279 | nextByte: |
| 2280 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2281 | } |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2282 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2283 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2284 | Py_XDECREF(errorHandler); |
| 2285 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2286 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2287 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2288 | onError: |
| 2289 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2290 | Py_XDECREF(errorHandler); |
| 2291 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2292 | return NULL; |
| 2293 | } |
| 2294 | |
| 2295 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2296 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2297 | { |
| 2298 | PyObject *repr; |
| 2299 | char *p; |
| 2300 | char *q; |
| 2301 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2302 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2303 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2304 | #ifdef Py_UNICODE_WIDE |
| 2305 | repr = PyString_FromStringAndSize(NULL, 10 * size); |
| 2306 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2307 | repr = PyString_FromStringAndSize(NULL, 6 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2308 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2309 | if (repr == NULL) |
| 2310 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2311 | if (size == 0) |
| 2312 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2313 | |
| 2314 | p = q = PyString_AS_STRING(repr); |
| 2315 | while (size-- > 0) { |
| 2316 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2317 | #ifdef Py_UNICODE_WIDE |
| 2318 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2319 | if (ch >= 0x10000) { |
| 2320 | *p++ = '\\'; |
| 2321 | *p++ = 'U'; |
| 2322 | *p++ = hexdigit[(ch >> 28) & 0xf]; |
| 2323 | *p++ = hexdigit[(ch >> 24) & 0xf]; |
| 2324 | *p++ = hexdigit[(ch >> 20) & 0xf]; |
| 2325 | *p++ = hexdigit[(ch >> 16) & 0xf]; |
| 2326 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 2327 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 2328 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 2329 | *p++ = hexdigit[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2330 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2331 | else |
| 2332 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2333 | /* Map 16-bit characters to '\uxxxx' */ |
| 2334 | if (ch >= 256) { |
| 2335 | *p++ = '\\'; |
| 2336 | *p++ = 'u'; |
| 2337 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 2338 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 2339 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 2340 | *p++ = hexdigit[ch & 15]; |
| 2341 | } |
| 2342 | /* Copy everything else as-is */ |
| 2343 | else |
| 2344 | *p++ = (char) ch; |
| 2345 | } |
| 2346 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2347 | _PyString_Resize(&repr, p - q); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2348 | return repr; |
| 2349 | } |
| 2350 | |
| 2351 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 2352 | { |
| 2353 | if (!PyUnicode_Check(unicode)) { |
| 2354 | PyErr_BadArgument(); |
| 2355 | return NULL; |
| 2356 | } |
| 2357 | return PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2358 | PyUnicode_GET_SIZE(unicode)); |
| 2359 | } |
| 2360 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2361 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 2362 | |
| 2363 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2364 | Py_ssize_t size, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2365 | const char *errors) |
| 2366 | { |
| 2367 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2368 | Py_ssize_t startinpos; |
| 2369 | Py_ssize_t endinpos; |
| 2370 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2371 | PyUnicodeObject *v; |
| 2372 | Py_UNICODE *p; |
| 2373 | const char *end; |
| 2374 | const char *reason; |
| 2375 | PyObject *errorHandler = NULL; |
| 2376 | PyObject *exc = NULL; |
| 2377 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 2378 | #ifdef Py_UNICODE_WIDE |
| 2379 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 2380 | #endif |
| 2381 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2382 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 2383 | if (v == NULL) |
| 2384 | goto onError; |
| 2385 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
| 2386 | return (PyObject *)v; |
| 2387 | p = PyUnicode_AS_UNICODE(v); |
| 2388 | end = s + size; |
| 2389 | |
| 2390 | while (s < end) { |
Neal Norwitz | 1004a53 | 2006-05-15 07:17:23 +0000 | [diff] [blame] | 2391 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2392 | /* We have to sanity check the raw data, otherwise doom looms for |
| 2393 | some malformed UCS-4 data. */ |
| 2394 | if ( |
| 2395 | #ifdef Py_UNICODE_WIDE |
| 2396 | *p > unimax || *p < 0 || |
| 2397 | #endif |
| 2398 | end-s < Py_UNICODE_SIZE |
| 2399 | ) |
| 2400 | { |
| 2401 | startinpos = s - starts; |
| 2402 | if (end-s < Py_UNICODE_SIZE) { |
| 2403 | endinpos = end-starts; |
| 2404 | reason = "truncated input"; |
| 2405 | } |
| 2406 | else { |
| 2407 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 2408 | reason = "illegal code point (> 0x10FFFF)"; |
| 2409 | } |
| 2410 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 2411 | if (unicode_decode_call_errorhandler( |
| 2412 | errors, &errorHandler, |
| 2413 | "unicode_internal", reason, |
| 2414 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2415 | (PyObject **)&v, &outpos, &p)) { |
| 2416 | goto onError; |
| 2417 | } |
| 2418 | } |
| 2419 | else { |
| 2420 | p++; |
| 2421 | s += Py_UNICODE_SIZE; |
| 2422 | } |
| 2423 | } |
| 2424 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2425 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2426 | goto onError; |
| 2427 | Py_XDECREF(errorHandler); |
| 2428 | Py_XDECREF(exc); |
| 2429 | return (PyObject *)v; |
| 2430 | |
| 2431 | onError: |
| 2432 | Py_XDECREF(v); |
| 2433 | Py_XDECREF(errorHandler); |
| 2434 | Py_XDECREF(exc); |
| 2435 | return NULL; |
| 2436 | } |
| 2437 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2438 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 2439 | |
| 2440 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2441 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2442 | const char *errors) |
| 2443 | { |
| 2444 | PyUnicodeObject *v; |
| 2445 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2446 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2447 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2448 | if (size == 1) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 2449 | Py_UNICODE r = *(unsigned char*)s; |
| 2450 | return PyUnicode_FromUnicode(&r, 1); |
| 2451 | } |
| 2452 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2453 | v = _PyUnicode_New(size); |
| 2454 | if (v == NULL) |
| 2455 | goto onError; |
| 2456 | if (size == 0) |
| 2457 | return (PyObject *)v; |
| 2458 | p = PyUnicode_AS_UNICODE(v); |
| 2459 | while (size-- > 0) |
| 2460 | *p++ = (unsigned char)*s++; |
| 2461 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2462 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2463 | onError: |
| 2464 | Py_XDECREF(v); |
| 2465 | return NULL; |
| 2466 | } |
| 2467 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2468 | /* create or adjust a UnicodeEncodeError */ |
| 2469 | static void make_encode_exception(PyObject **exceptionObject, |
| 2470 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2471 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 2472 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2473 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2474 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2475 | if (*exceptionObject == NULL) { |
| 2476 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 2477 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2478 | } |
| 2479 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2480 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 2481 | goto onError; |
| 2482 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 2483 | goto onError; |
| 2484 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 2485 | goto onError; |
| 2486 | return; |
| 2487 | onError: |
| 2488 | Py_DECREF(*exceptionObject); |
| 2489 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2490 | } |
| 2491 | } |
| 2492 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2493 | /* raises a UnicodeEncodeError */ |
| 2494 | static void raise_encode_exception(PyObject **exceptionObject, |
| 2495 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2496 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 2497 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2498 | const char *reason) |
| 2499 | { |
| 2500 | make_encode_exception(exceptionObject, |
| 2501 | encoding, unicode, size, startpos, endpos, reason); |
| 2502 | if (*exceptionObject != NULL) |
| 2503 | PyCodec_StrictErrors(*exceptionObject); |
| 2504 | } |
| 2505 | |
| 2506 | /* error handling callback helper: |
| 2507 | build arguments, call the callback and check the arguments, |
| 2508 | put the result into newpos and return the replacement string, which |
| 2509 | has to be freed by the caller */ |
| 2510 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 2511 | PyObject **errorHandler, |
| 2512 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2513 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 2514 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 2515 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2516 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2517 | 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] | 2518 | |
| 2519 | PyObject *restuple; |
| 2520 | PyObject *resunicode; |
| 2521 | |
| 2522 | if (*errorHandler == NULL) { |
| 2523 | *errorHandler = PyCodec_LookupError(errors); |
| 2524 | if (*errorHandler == NULL) |
| 2525 | return NULL; |
| 2526 | } |
| 2527 | |
| 2528 | make_encode_exception(exceptionObject, |
| 2529 | encoding, unicode, size, startpos, endpos, reason); |
| 2530 | if (*exceptionObject == NULL) |
| 2531 | return NULL; |
| 2532 | |
| 2533 | restuple = PyObject_CallFunctionObjArgs( |
| 2534 | *errorHandler, *exceptionObject, NULL); |
| 2535 | if (restuple == NULL) |
| 2536 | return NULL; |
| 2537 | if (!PyTuple_Check(restuple)) { |
| 2538 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 2539 | Py_DECREF(restuple); |
| 2540 | return NULL; |
| 2541 | } |
| 2542 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 2543 | &resunicode, newpos)) { |
| 2544 | Py_DECREF(restuple); |
| 2545 | return NULL; |
| 2546 | } |
| 2547 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2548 | *newpos = size+*newpos; |
| 2549 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 2550 | 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] | 2551 | Py_DECREF(restuple); |
| 2552 | return NULL; |
| 2553 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2554 | Py_INCREF(resunicode); |
| 2555 | Py_DECREF(restuple); |
| 2556 | return resunicode; |
| 2557 | } |
| 2558 | |
| 2559 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2560 | Py_ssize_t size, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2561 | const char *errors, |
| 2562 | int limit) |
| 2563 | { |
| 2564 | /* output object */ |
| 2565 | PyObject *res; |
| 2566 | /* pointers to the beginning and end+1 of input */ |
| 2567 | const Py_UNICODE *startp = p; |
| 2568 | const Py_UNICODE *endp = p + size; |
| 2569 | /* pointer to the beginning of the unencodable characters */ |
| 2570 | /* const Py_UNICODE *badp = NULL; */ |
| 2571 | /* pointer into the output */ |
| 2572 | char *str; |
| 2573 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2574 | Py_ssize_t respos = 0; |
| 2575 | Py_ssize_t ressize; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2576 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 2577 | 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] | 2578 | PyObject *errorHandler = NULL; |
| 2579 | PyObject *exc = NULL; |
| 2580 | /* the following variable is used for caching string comparisons |
| 2581 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 2582 | int known_errorHandler = -1; |
| 2583 | |
| 2584 | /* allocate enough for a simple encoding without |
| 2585 | replacements, if we need more, we'll resize */ |
| 2586 | res = PyString_FromStringAndSize(NULL, size); |
| 2587 | if (res == NULL) |
| 2588 | goto onError; |
| 2589 | if (size == 0) |
| 2590 | return res; |
| 2591 | str = PyString_AS_STRING(res); |
| 2592 | ressize = size; |
| 2593 | |
| 2594 | while (p<endp) { |
| 2595 | Py_UNICODE c = *p; |
| 2596 | |
| 2597 | /* can we encode this? */ |
| 2598 | if (c<limit) { |
| 2599 | /* no overflow check, because we know that the space is enough */ |
| 2600 | *str++ = (char)c; |
| 2601 | ++p; |
| 2602 | } |
| 2603 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2604 | Py_ssize_t unicodepos = p-startp; |
| 2605 | Py_ssize_t requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2606 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2607 | Py_ssize_t repsize; |
| 2608 | Py_ssize_t newpos; |
| 2609 | Py_ssize_t respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2610 | Py_UNICODE *uni2; |
| 2611 | /* startpos for collecting unencodable chars */ |
| 2612 | const Py_UNICODE *collstart = p; |
| 2613 | const Py_UNICODE *collend = p; |
| 2614 | /* find all unecodable characters */ |
| 2615 | while ((collend < endp) && ((*collend)>=limit)) |
| 2616 | ++collend; |
| 2617 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 2618 | if (known_errorHandler==-1) { |
| 2619 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 2620 | known_errorHandler = 1; |
| 2621 | else if (!strcmp(errors, "replace")) |
| 2622 | known_errorHandler = 2; |
| 2623 | else if (!strcmp(errors, "ignore")) |
| 2624 | known_errorHandler = 3; |
| 2625 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 2626 | known_errorHandler = 4; |
| 2627 | else |
| 2628 | known_errorHandler = 0; |
| 2629 | } |
| 2630 | switch (known_errorHandler) { |
| 2631 | case 1: /* strict */ |
| 2632 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 2633 | goto onError; |
| 2634 | case 2: /* replace */ |
| 2635 | while (collstart++<collend) |
| 2636 | *str++ = '?'; /* fall through */ |
| 2637 | case 3: /* ignore */ |
| 2638 | p = collend; |
| 2639 | break; |
| 2640 | case 4: /* xmlcharrefreplace */ |
| 2641 | respos = str-PyString_AS_STRING(res); |
| 2642 | /* determine replacement size (temporarily (mis)uses p) */ |
| 2643 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 2644 | if (*p<10) |
| 2645 | repsize += 2+1+1; |
| 2646 | else if (*p<100) |
| 2647 | repsize += 2+2+1; |
| 2648 | else if (*p<1000) |
| 2649 | repsize += 2+3+1; |
| 2650 | else if (*p<10000) |
| 2651 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 2652 | #ifndef Py_UNICODE_WIDE |
| 2653 | else |
| 2654 | repsize += 2+5+1; |
| 2655 | #else |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2656 | else if (*p<100000) |
| 2657 | repsize += 2+5+1; |
| 2658 | else if (*p<1000000) |
| 2659 | repsize += 2+6+1; |
| 2660 | else |
| 2661 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2662 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2663 | } |
| 2664 | requiredsize = respos+repsize+(endp-collend); |
| 2665 | if (requiredsize > ressize) { |
| 2666 | if (requiredsize<2*ressize) |
| 2667 | requiredsize = 2*ressize; |
| 2668 | if (_PyString_Resize(&res, requiredsize)) |
| 2669 | goto onError; |
| 2670 | str = PyString_AS_STRING(res) + respos; |
| 2671 | ressize = requiredsize; |
| 2672 | } |
| 2673 | /* generate replacement (temporarily (mis)uses p) */ |
| 2674 | for (p = collstart; p < collend; ++p) { |
| 2675 | str += sprintf(str, "&#%d;", (int)*p); |
| 2676 | } |
| 2677 | p = collend; |
| 2678 | break; |
| 2679 | default: |
| 2680 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 2681 | encoding, reason, startp, size, &exc, |
| 2682 | collstart-startp, collend-startp, &newpos); |
| 2683 | if (repunicode == NULL) |
| 2684 | goto onError; |
| 2685 | /* need more space? (at least enough for what we |
| 2686 | have+the replacement+the rest of the string, so |
| 2687 | we won't have to check space for encodable characters) */ |
| 2688 | respos = str-PyString_AS_STRING(res); |
| 2689 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 2690 | requiredsize = respos+repsize+(endp-collend); |
| 2691 | if (requiredsize > ressize) { |
| 2692 | if (requiredsize<2*ressize) |
| 2693 | requiredsize = 2*ressize; |
| 2694 | if (_PyString_Resize(&res, requiredsize)) { |
| 2695 | Py_DECREF(repunicode); |
| 2696 | goto onError; |
| 2697 | } |
| 2698 | str = PyString_AS_STRING(res) + respos; |
| 2699 | ressize = requiredsize; |
| 2700 | } |
| 2701 | /* check if there is anything unencodable in the replacement |
| 2702 | and copy it to the output */ |
| 2703 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 2704 | c = *uni2; |
| 2705 | if (c >= limit) { |
| 2706 | raise_encode_exception(&exc, encoding, startp, size, |
| 2707 | unicodepos, unicodepos+1, reason); |
| 2708 | Py_DECREF(repunicode); |
| 2709 | goto onError; |
| 2710 | } |
| 2711 | *str = (char)c; |
| 2712 | } |
| 2713 | p = startp + newpos; |
| 2714 | Py_DECREF(repunicode); |
| 2715 | } |
| 2716 | } |
| 2717 | } |
| 2718 | /* Resize if we allocated to much */ |
| 2719 | respos = str-PyString_AS_STRING(res); |
| 2720 | if (respos<ressize) |
| 2721 | /* If this falls res will be NULL */ |
| 2722 | _PyString_Resize(&res, respos); |
| 2723 | Py_XDECREF(errorHandler); |
| 2724 | Py_XDECREF(exc); |
| 2725 | return res; |
| 2726 | |
| 2727 | onError: |
| 2728 | Py_XDECREF(res); |
| 2729 | Py_XDECREF(errorHandler); |
| 2730 | Py_XDECREF(exc); |
| 2731 | return NULL; |
| 2732 | } |
| 2733 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2734 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2735 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2736 | const char *errors) |
| 2737 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2738 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2739 | } |
| 2740 | |
| 2741 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 2742 | { |
| 2743 | if (!PyUnicode_Check(unicode)) { |
| 2744 | PyErr_BadArgument(); |
| 2745 | return NULL; |
| 2746 | } |
| 2747 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 2748 | PyUnicode_GET_SIZE(unicode), |
| 2749 | NULL); |
| 2750 | } |
| 2751 | |
| 2752 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 2753 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2754 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2755 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2756 | const char *errors) |
| 2757 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2758 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2759 | PyUnicodeObject *v; |
| 2760 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2761 | Py_ssize_t startinpos; |
| 2762 | Py_ssize_t endinpos; |
| 2763 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2764 | const char *e; |
| 2765 | PyObject *errorHandler = NULL; |
| 2766 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2767 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2768 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 2769 | if (size == 1 && *(unsigned char*)s < 128) { |
| 2770 | Py_UNICODE r = *(unsigned char*)s; |
| 2771 | return PyUnicode_FromUnicode(&r, 1); |
| 2772 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2773 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2774 | v = _PyUnicode_New(size); |
| 2775 | if (v == NULL) |
| 2776 | goto onError; |
| 2777 | if (size == 0) |
| 2778 | return (PyObject *)v; |
| 2779 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2780 | e = s + size; |
| 2781 | while (s < e) { |
| 2782 | register unsigned char c = (unsigned char)*s; |
| 2783 | if (c < 128) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2784 | *p++ = c; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2785 | ++s; |
| 2786 | } |
| 2787 | else { |
| 2788 | startinpos = s-starts; |
| 2789 | endinpos = startinpos + 1; |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 2790 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2791 | if (unicode_decode_call_errorhandler( |
| 2792 | errors, &errorHandler, |
| 2793 | "ascii", "ordinal not in range(128)", |
| 2794 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2795 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2796 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2797 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2798 | } |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2799 | if (p - PyUnicode_AS_UNICODE(v) < PyString_GET_SIZE(v)) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2800 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2801 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2802 | Py_XDECREF(errorHandler); |
| 2803 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2804 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2805 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2806 | onError: |
| 2807 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2808 | Py_XDECREF(errorHandler); |
| 2809 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2810 | return NULL; |
| 2811 | } |
| 2812 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2813 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2814 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2815 | const char *errors) |
| 2816 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2817 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2818 | } |
| 2819 | |
| 2820 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 2821 | { |
| 2822 | if (!PyUnicode_Check(unicode)) { |
| 2823 | PyErr_BadArgument(); |
| 2824 | return NULL; |
| 2825 | } |
| 2826 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 2827 | PyUnicode_GET_SIZE(unicode), |
| 2828 | NULL); |
| 2829 | } |
| 2830 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2831 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2832 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2833 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2834 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2835 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2836 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2837 | const char *errors) |
| 2838 | { |
| 2839 | PyUnicodeObject *v; |
| 2840 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2841 | DWORD usize; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2842 | |
| 2843 | /* First get the size of the result */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2844 | assert(size < INT_MAX); |
| 2845 | usize = MultiByteToWideChar(CP_ACP, 0, s, (int)size, NULL, 0); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 2846 | if (size > 0 && usize==0) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2847 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2848 | |
| 2849 | v = _PyUnicode_New(usize); |
| 2850 | if (v == NULL) |
| 2851 | return NULL; |
| 2852 | if (usize == 0) |
| 2853 | return (PyObject *)v; |
| 2854 | p = PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2855 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, (int)size, p, usize)) { |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2856 | Py_DECREF(v); |
| 2857 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2858 | } |
| 2859 | |
| 2860 | return (PyObject *)v; |
| 2861 | } |
| 2862 | |
| 2863 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2864 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2865 | const char *errors) |
| 2866 | { |
| 2867 | PyObject *repr; |
| 2868 | char *s; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 2869 | DWORD mbcssize; |
| 2870 | |
| 2871 | /* If there are no characters, bail now! */ |
| 2872 | if (size==0) |
| 2873 | return PyString_FromString(""); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2874 | |
| 2875 | /* First get the size of the result */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2876 | assert(size<INT_MAX); |
| 2877 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, (int)size, NULL, 0, NULL, NULL); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2878 | if (mbcssize==0) |
| 2879 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2880 | |
| 2881 | repr = PyString_FromStringAndSize(NULL, mbcssize); |
| 2882 | if (repr == NULL) |
| 2883 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2884 | if (mbcssize == 0) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2885 | return repr; |
| 2886 | |
| 2887 | /* Do the conversion */ |
| 2888 | s = PyString_AS_STRING(repr); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2889 | assert(size < INT_MAX); |
| 2890 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, (int)size, s, mbcssize, NULL, NULL)) { |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2891 | Py_DECREF(repr); |
| 2892 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2893 | } |
| 2894 | return repr; |
| 2895 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2896 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2897 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 2898 | { |
| 2899 | if (!PyUnicode_Check(unicode)) { |
| 2900 | PyErr_BadArgument(); |
| 2901 | return NULL; |
| 2902 | } |
| 2903 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2904 | PyUnicode_GET_SIZE(unicode), |
| 2905 | NULL); |
| 2906 | } |
| 2907 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2908 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2909 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2910 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 2911 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2912 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2913 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2914 | PyObject *mapping, |
| 2915 | const char *errors) |
| 2916 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2917 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2918 | Py_ssize_t startinpos; |
| 2919 | Py_ssize_t endinpos; |
| 2920 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2921 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2922 | PyUnicodeObject *v; |
| 2923 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2924 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2925 | PyObject *errorHandler = NULL; |
| 2926 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2927 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2928 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2929 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2930 | /* Default to Latin-1 */ |
| 2931 | if (mapping == NULL) |
| 2932 | return PyUnicode_DecodeLatin1(s, size, errors); |
| 2933 | |
| 2934 | v = _PyUnicode_New(size); |
| 2935 | if (v == NULL) |
| 2936 | goto onError; |
| 2937 | if (size == 0) |
| 2938 | return (PyObject *)v; |
| 2939 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2940 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2941 | if (PyUnicode_CheckExact(mapping)) { |
| 2942 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 2943 | maplen = PyUnicode_GET_SIZE(mapping); |
| 2944 | while (s < e) { |
| 2945 | unsigned char ch = *s; |
| 2946 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2947 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2948 | if (ch < maplen) |
| 2949 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2950 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2951 | if (x == 0xfffe) { |
| 2952 | /* undefined mapping */ |
| 2953 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2954 | startinpos = s-starts; |
| 2955 | endinpos = startinpos+1; |
| 2956 | if (unicode_decode_call_errorhandler( |
| 2957 | errors, &errorHandler, |
| 2958 | "charmap", "character maps to <undefined>", |
| 2959 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2960 | (PyObject **)&v, &outpos, &p)) { |
| 2961 | goto onError; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 2962 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2963 | continue; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 2964 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2965 | *p++ = x; |
| 2966 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2967 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2968 | } |
| 2969 | else { |
| 2970 | while (s < e) { |
| 2971 | unsigned char ch = *s; |
| 2972 | PyObject *w, *x; |
| 2973 | |
| 2974 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 2975 | w = PyInt_FromLong((long)ch); |
| 2976 | if (w == NULL) |
| 2977 | goto onError; |
| 2978 | x = PyObject_GetItem(mapping, w); |
| 2979 | Py_DECREF(w); |
| 2980 | if (x == NULL) { |
| 2981 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 2982 | /* No mapping found means: mapping is undefined. */ |
| 2983 | PyErr_Clear(); |
| 2984 | x = Py_None; |
| 2985 | Py_INCREF(x); |
| 2986 | } else |
| 2987 | goto onError; |
| 2988 | } |
| 2989 | |
| 2990 | /* Apply mapping */ |
| 2991 | if (PyInt_Check(x)) { |
| 2992 | long value = PyInt_AS_LONG(x); |
| 2993 | if (value < 0 || value > 65535) { |
| 2994 | PyErr_SetString(PyExc_TypeError, |
| 2995 | "character mapping must be in range(65536)"); |
| 2996 | Py_DECREF(x); |
| 2997 | goto onError; |
| 2998 | } |
| 2999 | *p++ = (Py_UNICODE)value; |
| 3000 | } |
| 3001 | else if (x == Py_None) { |
| 3002 | /* undefined mapping */ |
| 3003 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3004 | startinpos = s-starts; |
| 3005 | endinpos = startinpos+1; |
| 3006 | if (unicode_decode_call_errorhandler( |
| 3007 | errors, &errorHandler, |
| 3008 | "charmap", "character maps to <undefined>", |
| 3009 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3010 | (PyObject **)&v, &outpos, &p)) { |
| 3011 | Py_DECREF(x); |
| 3012 | goto onError; |
| 3013 | } |
Walter Dörwald | d4fff17 | 2005-11-28 22:15:56 +0000 | [diff] [blame] | 3014 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3015 | continue; |
| 3016 | } |
| 3017 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3018 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3019 | |
| 3020 | if (targetsize == 1) |
| 3021 | /* 1-1 mapping */ |
| 3022 | *p++ = *PyUnicode_AS_UNICODE(x); |
| 3023 | |
| 3024 | else if (targetsize > 1) { |
| 3025 | /* 1-n mapping */ |
| 3026 | if (targetsize > extrachars) { |
| 3027 | /* resize first */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3028 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 3029 | Py_ssize_t needed = (targetsize - extrachars) + \ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3030 | (targetsize << 2); |
| 3031 | extrachars += needed; |
| 3032 | if (_PyUnicode_Resize(&v, |
| 3033 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 3034 | Py_DECREF(x); |
| 3035 | goto onError; |
| 3036 | } |
| 3037 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 3038 | } |
| 3039 | Py_UNICODE_COPY(p, |
| 3040 | PyUnicode_AS_UNICODE(x), |
| 3041 | targetsize); |
| 3042 | p += targetsize; |
| 3043 | extrachars -= targetsize; |
| 3044 | } |
| 3045 | /* 1-0 mapping: skip the character */ |
| 3046 | } |
| 3047 | else { |
| 3048 | /* wrong return value */ |
| 3049 | PyErr_SetString(PyExc_TypeError, |
| 3050 | "character mapping must return integer, None or unicode"); |
| 3051 | Py_DECREF(x); |
| 3052 | goto onError; |
| 3053 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3054 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3055 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3056 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3057 | } |
| 3058 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3059 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3060 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3061 | Py_XDECREF(errorHandler); |
| 3062 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3063 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3064 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3065 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3066 | Py_XDECREF(errorHandler); |
| 3067 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3068 | Py_XDECREF(v); |
| 3069 | return NULL; |
| 3070 | } |
| 3071 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3072 | /* Lookup the character ch in the mapping. If the character |
| 3073 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3074 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3075 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3076 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3077 | PyObject *w = PyInt_FromLong((long)c); |
| 3078 | PyObject *x; |
| 3079 | |
| 3080 | if (w == NULL) |
| 3081 | return NULL; |
| 3082 | x = PyObject_GetItem(mapping, w); |
| 3083 | Py_DECREF(w); |
| 3084 | if (x == NULL) { |
| 3085 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3086 | /* No mapping found means: mapping is undefined. */ |
| 3087 | PyErr_Clear(); |
| 3088 | x = Py_None; |
| 3089 | Py_INCREF(x); |
| 3090 | return x; |
| 3091 | } else |
| 3092 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3093 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 3094 | else if (x == Py_None) |
| 3095 | return x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3096 | else if (PyInt_Check(x)) { |
| 3097 | long value = PyInt_AS_LONG(x); |
| 3098 | if (value < 0 || value > 255) { |
| 3099 | PyErr_SetString(PyExc_TypeError, |
| 3100 | "character mapping must be in range(256)"); |
| 3101 | Py_DECREF(x); |
| 3102 | return NULL; |
| 3103 | } |
| 3104 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3105 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3106 | else if (PyString_Check(x)) |
| 3107 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3108 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3109 | /* wrong return value */ |
| 3110 | PyErr_SetString(PyExc_TypeError, |
| 3111 | "character mapping must return integer, None or str"); |
| 3112 | Py_DECREF(x); |
| 3113 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3114 | } |
| 3115 | } |
| 3116 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3117 | /* lookup the character, put the result in the output string and adjust |
| 3118 | various state variables. Reallocate the output string if not enough |
| 3119 | space is available. Return a new reference to the object that |
| 3120 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 3121 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 3122 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3123 | static |
| 3124 | PyObject *charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3125 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3126 | { |
| 3127 | PyObject *rep = charmapencode_lookup(c, mapping); |
| 3128 | |
| 3129 | if (rep==NULL) |
| 3130 | return NULL; |
| 3131 | else if (rep==Py_None) |
| 3132 | return rep; |
| 3133 | else { |
| 3134 | char *outstart = PyString_AS_STRING(*outobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3135 | Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3136 | if (PyInt_Check(rep)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3137 | Py_ssize_t requiredsize = *outpos+1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3138 | if (outsize<requiredsize) { |
| 3139 | /* exponentially overallocate to minimize reallocations */ |
| 3140 | if (requiredsize < 2*outsize) |
| 3141 | requiredsize = 2*outsize; |
| 3142 | if (_PyString_Resize(outobj, requiredsize)) { |
| 3143 | Py_DECREF(rep); |
| 3144 | return NULL; |
| 3145 | } |
| 3146 | outstart = PyString_AS_STRING(*outobj); |
| 3147 | } |
| 3148 | outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); |
| 3149 | } |
| 3150 | else { |
| 3151 | const char *repchars = PyString_AS_STRING(rep); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3152 | Py_ssize_t repsize = PyString_GET_SIZE(rep); |
| 3153 | Py_ssize_t requiredsize = *outpos+repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3154 | if (outsize<requiredsize) { |
| 3155 | /* exponentially overallocate to minimize reallocations */ |
| 3156 | if (requiredsize < 2*outsize) |
| 3157 | requiredsize = 2*outsize; |
| 3158 | if (_PyString_Resize(outobj, requiredsize)) { |
| 3159 | Py_DECREF(rep); |
| 3160 | return NULL; |
| 3161 | } |
| 3162 | outstart = PyString_AS_STRING(*outobj); |
| 3163 | } |
| 3164 | memcpy(outstart + *outpos, repchars, repsize); |
| 3165 | *outpos += repsize; |
| 3166 | } |
| 3167 | } |
| 3168 | return rep; |
| 3169 | } |
| 3170 | |
| 3171 | /* handle an error in PyUnicode_EncodeCharmap |
| 3172 | Return 0 on success, -1 on error */ |
| 3173 | static |
| 3174 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3175 | 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] | 3176 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3177 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3178 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3179 | { |
| 3180 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3181 | Py_ssize_t repsize; |
| 3182 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3183 | Py_UNICODE *uni2; |
| 3184 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3185 | Py_ssize_t collstartpos = *inpos; |
| 3186 | Py_ssize_t collendpos = *inpos+1; |
| 3187 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3188 | char *encoding = "charmap"; |
| 3189 | char *reason = "character maps to <undefined>"; |
| 3190 | |
| 3191 | PyObject *x; |
| 3192 | /* find all unencodable characters */ |
| 3193 | while (collendpos < size) { |
| 3194 | x = charmapencode_lookup(p[collendpos], mapping); |
| 3195 | if (x==NULL) |
| 3196 | return -1; |
| 3197 | else if (x!=Py_None) { |
| 3198 | Py_DECREF(x); |
| 3199 | break; |
| 3200 | } |
| 3201 | Py_DECREF(x); |
| 3202 | ++collendpos; |
| 3203 | } |
| 3204 | /* cache callback name lookup |
| 3205 | * (if not done yet, i.e. it's the first error) */ |
| 3206 | if (*known_errorHandler==-1) { |
| 3207 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3208 | *known_errorHandler = 1; |
| 3209 | else if (!strcmp(errors, "replace")) |
| 3210 | *known_errorHandler = 2; |
| 3211 | else if (!strcmp(errors, "ignore")) |
| 3212 | *known_errorHandler = 3; |
| 3213 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3214 | *known_errorHandler = 4; |
| 3215 | else |
| 3216 | *known_errorHandler = 0; |
| 3217 | } |
| 3218 | switch (*known_errorHandler) { |
| 3219 | case 1: /* strict */ |
| 3220 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3221 | return -1; |
| 3222 | case 2: /* replace */ |
| 3223 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 3224 | x = charmapencode_output('?', mapping, res, respos); |
| 3225 | if (x==NULL) { |
| 3226 | return -1; |
| 3227 | } |
| 3228 | else if (x==Py_None) { |
| 3229 | Py_DECREF(x); |
| 3230 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3231 | return -1; |
| 3232 | } |
| 3233 | Py_DECREF(x); |
| 3234 | } |
| 3235 | /* fall through */ |
| 3236 | case 3: /* ignore */ |
| 3237 | *inpos = collendpos; |
| 3238 | break; |
| 3239 | case 4: /* xmlcharrefreplace */ |
| 3240 | /* generate replacement (temporarily (mis)uses p) */ |
| 3241 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 3242 | char buffer[2+29+1+1]; |
| 3243 | char *cp; |
| 3244 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 3245 | for (cp = buffer; *cp; ++cp) { |
| 3246 | x = charmapencode_output(*cp, mapping, res, respos); |
| 3247 | if (x==NULL) |
| 3248 | return -1; |
| 3249 | else if (x==Py_None) { |
| 3250 | Py_DECREF(x); |
| 3251 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3252 | return -1; |
| 3253 | } |
| 3254 | Py_DECREF(x); |
| 3255 | } |
| 3256 | } |
| 3257 | *inpos = collendpos; |
| 3258 | break; |
| 3259 | default: |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3260 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3261 | encoding, reason, p, size, exceptionObject, |
| 3262 | collstartpos, collendpos, &newpos); |
| 3263 | if (repunicode == NULL) |
| 3264 | return -1; |
| 3265 | /* generate replacement */ |
| 3266 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3267 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 3268 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 3269 | if (x==NULL) { |
| 3270 | Py_DECREF(repunicode); |
| 3271 | return -1; |
| 3272 | } |
| 3273 | else if (x==Py_None) { |
| 3274 | Py_DECREF(repunicode); |
| 3275 | Py_DECREF(x); |
| 3276 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3277 | return -1; |
| 3278 | } |
| 3279 | Py_DECREF(x); |
| 3280 | } |
| 3281 | *inpos = newpos; |
| 3282 | Py_DECREF(repunicode); |
| 3283 | } |
| 3284 | return 0; |
| 3285 | } |
| 3286 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3287 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3288 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3289 | PyObject *mapping, |
| 3290 | const char *errors) |
| 3291 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3292 | /* output object */ |
| 3293 | PyObject *res = NULL; |
| 3294 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3295 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3296 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3297 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3298 | PyObject *errorHandler = NULL; |
| 3299 | PyObject *exc = NULL; |
| 3300 | /* the following variable is used for caching string comparisons |
| 3301 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 3302 | * 3=ignore, 4=xmlcharrefreplace */ |
| 3303 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3304 | |
| 3305 | /* Default to Latin-1 */ |
| 3306 | if (mapping == NULL) |
| 3307 | return PyUnicode_EncodeLatin1(p, size, errors); |
| 3308 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3309 | /* allocate enough for a simple encoding without |
| 3310 | replacements, if we need more, we'll resize */ |
| 3311 | res = PyString_FromStringAndSize(NULL, size); |
| 3312 | if (res == NULL) |
| 3313 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3314 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3315 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3316 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3317 | while (inpos<size) { |
| 3318 | /* try to encode it */ |
| 3319 | PyObject *x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 3320 | if (x==NULL) /* error */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3321 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3322 | if (x==Py_None) { /* unencodable character */ |
| 3323 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 3324 | &exc, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3325 | &known_errorHandler, &errorHandler, errors, |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 3326 | &res, &respos)) { |
| 3327 | Py_DECREF(x); |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3328 | goto onError; |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 3329 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3330 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3331 | else |
| 3332 | /* done with this character => adjust input position */ |
| 3333 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3334 | Py_DECREF(x); |
| 3335 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3336 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3337 | /* Resize if we allocated to much */ |
| 3338 | if (respos<PyString_GET_SIZE(res)) { |
| 3339 | if (_PyString_Resize(&res, respos)) |
| 3340 | goto onError; |
| 3341 | } |
| 3342 | Py_XDECREF(exc); |
| 3343 | Py_XDECREF(errorHandler); |
| 3344 | return res; |
| 3345 | |
| 3346 | onError: |
| 3347 | Py_XDECREF(res); |
| 3348 | Py_XDECREF(exc); |
| 3349 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3350 | return NULL; |
| 3351 | } |
| 3352 | |
| 3353 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 3354 | PyObject *mapping) |
| 3355 | { |
| 3356 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 3357 | PyErr_BadArgument(); |
| 3358 | return NULL; |
| 3359 | } |
| 3360 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 3361 | PyUnicode_GET_SIZE(unicode), |
| 3362 | mapping, |
| 3363 | NULL); |
| 3364 | } |
| 3365 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3366 | /* create or adjust a UnicodeTranslateError */ |
| 3367 | static void make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3368 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3369 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3370 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3371 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3372 | if (*exceptionObject == NULL) { |
| 3373 | *exceptionObject = PyUnicodeTranslateError_Create( |
| 3374 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3375 | } |
| 3376 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3377 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 3378 | goto onError; |
| 3379 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 3380 | goto onError; |
| 3381 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 3382 | goto onError; |
| 3383 | return; |
| 3384 | onError: |
| 3385 | Py_DECREF(*exceptionObject); |
| 3386 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3387 | } |
| 3388 | } |
| 3389 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3390 | /* raises a UnicodeTranslateError */ |
| 3391 | static void raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3392 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3393 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3394 | const char *reason) |
| 3395 | { |
| 3396 | make_translate_exception(exceptionObject, |
| 3397 | unicode, size, startpos, endpos, reason); |
| 3398 | if (*exceptionObject != NULL) |
| 3399 | PyCodec_StrictErrors(*exceptionObject); |
| 3400 | } |
| 3401 | |
| 3402 | /* error handling callback helper: |
| 3403 | build arguments, call the callback and check the arguments, |
| 3404 | put the result into newpos and return the replacement string, which |
| 3405 | has to be freed by the caller */ |
| 3406 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 3407 | PyObject **errorHandler, |
| 3408 | const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3409 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 3410 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3411 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3412 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3413 | 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] | 3414 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3415 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3416 | PyObject *restuple; |
| 3417 | PyObject *resunicode; |
| 3418 | |
| 3419 | if (*errorHandler == NULL) { |
| 3420 | *errorHandler = PyCodec_LookupError(errors); |
| 3421 | if (*errorHandler == NULL) |
| 3422 | return NULL; |
| 3423 | } |
| 3424 | |
| 3425 | make_translate_exception(exceptionObject, |
| 3426 | unicode, size, startpos, endpos, reason); |
| 3427 | if (*exceptionObject == NULL) |
| 3428 | return NULL; |
| 3429 | |
| 3430 | restuple = PyObject_CallFunctionObjArgs( |
| 3431 | *errorHandler, *exceptionObject, NULL); |
| 3432 | if (restuple == NULL) |
| 3433 | return NULL; |
| 3434 | if (!PyTuple_Check(restuple)) { |
| 3435 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 3436 | Py_DECREF(restuple); |
| 3437 | return NULL; |
| 3438 | } |
| 3439 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3440 | &resunicode, &i_newpos)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3441 | Py_DECREF(restuple); |
| 3442 | return NULL; |
| 3443 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3444 | if (i_newpos<0) |
| 3445 | *newpos = size+i_newpos; |
| 3446 | else |
| 3447 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3448 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 3449 | 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] | 3450 | Py_DECREF(restuple); |
| 3451 | return NULL; |
| 3452 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3453 | Py_INCREF(resunicode); |
| 3454 | Py_DECREF(restuple); |
| 3455 | return resunicode; |
| 3456 | } |
| 3457 | |
| 3458 | /* Lookup the character ch in the mapping and put the result in result, |
| 3459 | which must be decrefed by the caller. |
| 3460 | Return 0 on success, -1 on error */ |
| 3461 | static |
| 3462 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 3463 | { |
| 3464 | PyObject *w = PyInt_FromLong((long)c); |
| 3465 | PyObject *x; |
| 3466 | |
| 3467 | if (w == NULL) |
| 3468 | return -1; |
| 3469 | x = PyObject_GetItem(mapping, w); |
| 3470 | Py_DECREF(w); |
| 3471 | if (x == NULL) { |
| 3472 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3473 | /* No mapping found means: use 1:1 mapping. */ |
| 3474 | PyErr_Clear(); |
| 3475 | *result = NULL; |
| 3476 | return 0; |
| 3477 | } else |
| 3478 | return -1; |
| 3479 | } |
| 3480 | else if (x == Py_None) { |
| 3481 | *result = x; |
| 3482 | return 0; |
| 3483 | } |
| 3484 | else if (PyInt_Check(x)) { |
| 3485 | long value = PyInt_AS_LONG(x); |
| 3486 | long max = PyUnicode_GetMax(); |
| 3487 | if (value < 0 || value > max) { |
| 3488 | PyErr_Format(PyExc_TypeError, |
| 3489 | "character mapping must be in range(0x%lx)", max+1); |
| 3490 | Py_DECREF(x); |
| 3491 | return -1; |
| 3492 | } |
| 3493 | *result = x; |
| 3494 | return 0; |
| 3495 | } |
| 3496 | else if (PyUnicode_Check(x)) { |
| 3497 | *result = x; |
| 3498 | return 0; |
| 3499 | } |
| 3500 | else { |
| 3501 | /* wrong return value */ |
| 3502 | PyErr_SetString(PyExc_TypeError, |
| 3503 | "character mapping must return integer, None or unicode"); |
Walter Dörwald | 150523e | 2003-08-15 16:52:19 +0000 | [diff] [blame] | 3504 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3505 | return -1; |
| 3506 | } |
| 3507 | } |
| 3508 | /* ensure that *outobj is at least requiredsize characters long, |
| 3509 | if not reallocate and adjust various state variables. |
| 3510 | Return 0 on success, -1 on error */ |
| 3511 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3512 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3513 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3514 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3515 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3516 | if (requiredsize > oldsize) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3517 | /* remember old output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3518 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3519 | /* exponentially overallocate to minimize reallocations */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3520 | if (requiredsize < 2 * oldsize) |
| 3521 | requiredsize = 2 * oldsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3522 | if (_PyUnicode_Resize(outobj, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3523 | return -1; |
| 3524 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3525 | } |
| 3526 | return 0; |
| 3527 | } |
| 3528 | /* lookup the character, put the result in the output string and adjust |
| 3529 | various state variables. Return a new reference to the object that |
| 3530 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 3531 | undefined (in which case no character was written). |
| 3532 | The called must decref result. |
| 3533 | Return 0 on success, -1 on error. */ |
| 3534 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3535 | 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] | 3536 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3537 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3538 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3539 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3540 | return -1; |
| 3541 | if (*res==NULL) { |
| 3542 | /* not found => default to 1:1 mapping */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3543 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3544 | } |
| 3545 | else if (*res==Py_None) |
| 3546 | ; |
| 3547 | else if (PyInt_Check(*res)) { |
| 3548 | /* no overflow check, because we know that the space is enough */ |
| 3549 | *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); |
| 3550 | } |
| 3551 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3552 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3553 | if (repsize==1) { |
| 3554 | /* no overflow check, because we know that the space is enough */ |
| 3555 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 3556 | } |
| 3557 | else if (repsize!=0) { |
| 3558 | /* more than one character */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3559 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
Walter Dörwald | cd736e7 | 2004-02-05 17:36:00 +0000 | [diff] [blame] | 3560 | (insize - (curinp-startinp)) + |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3561 | repsize - 1; |
| 3562 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3563 | return -1; |
| 3564 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 3565 | *outp += repsize; |
| 3566 | } |
| 3567 | } |
| 3568 | else |
| 3569 | return -1; |
| 3570 | return 0; |
| 3571 | } |
| 3572 | |
| 3573 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3574 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3575 | PyObject *mapping, |
| 3576 | const char *errors) |
| 3577 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3578 | /* output object */ |
| 3579 | PyObject *res = NULL; |
| 3580 | /* pointers to the beginning and end+1 of input */ |
| 3581 | const Py_UNICODE *startp = p; |
| 3582 | const Py_UNICODE *endp = p + size; |
| 3583 | /* pointer into the output */ |
| 3584 | Py_UNICODE *str; |
| 3585 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3586 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3587 | char *reason = "character maps to <undefined>"; |
| 3588 | PyObject *errorHandler = NULL; |
| 3589 | PyObject *exc = NULL; |
| 3590 | /* the following variable is used for caching string comparisons |
| 3591 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 3592 | * 3=ignore, 4=xmlcharrefreplace */ |
| 3593 | int known_errorHandler = -1; |
| 3594 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3595 | if (mapping == NULL) { |
| 3596 | PyErr_BadArgument(); |
| 3597 | return NULL; |
| 3598 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3599 | |
| 3600 | /* allocate enough for a simple 1:1 translation without |
| 3601 | replacements, if we need more, we'll resize */ |
| 3602 | res = PyUnicode_FromUnicode(NULL, size); |
| 3603 | if (res == NULL) |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3604 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3605 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3606 | return res; |
| 3607 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3608 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3609 | while (p<endp) { |
| 3610 | /* try to encode it */ |
| 3611 | PyObject *x = NULL; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3612 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3613 | Py_XDECREF(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3614 | goto onError; |
| 3615 | } |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 3616 | Py_XDECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3617 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 3618 | ++p; |
| 3619 | else { /* untranslatable character */ |
| 3620 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3621 | Py_ssize_t repsize; |
| 3622 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3623 | Py_UNICODE *uni2; |
| 3624 | /* startpos for collecting untranslatable chars */ |
| 3625 | const Py_UNICODE *collstart = p; |
| 3626 | const Py_UNICODE *collend = p+1; |
| 3627 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3628 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3629 | /* find all untranslatable characters */ |
| 3630 | while (collend < endp) { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3631 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3632 | goto onError; |
| 3633 | Py_XDECREF(x); |
| 3634 | if (x!=Py_None) |
| 3635 | break; |
| 3636 | ++collend; |
| 3637 | } |
| 3638 | /* cache callback name lookup |
| 3639 | * (if not done yet, i.e. it's the first error) */ |
| 3640 | if (known_errorHandler==-1) { |
| 3641 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3642 | known_errorHandler = 1; |
| 3643 | else if (!strcmp(errors, "replace")) |
| 3644 | known_errorHandler = 2; |
| 3645 | else if (!strcmp(errors, "ignore")) |
| 3646 | known_errorHandler = 3; |
| 3647 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3648 | known_errorHandler = 4; |
| 3649 | else |
| 3650 | known_errorHandler = 0; |
| 3651 | } |
| 3652 | switch (known_errorHandler) { |
| 3653 | case 1: /* strict */ |
| 3654 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
| 3655 | goto onError; |
| 3656 | case 2: /* replace */ |
| 3657 | /* No need to check for space, this is a 1:1 replacement */ |
| 3658 | for (coll = collstart; coll<collend; ++coll) |
| 3659 | *str++ = '?'; |
| 3660 | /* fall through */ |
| 3661 | case 3: /* ignore */ |
| 3662 | p = collend; |
| 3663 | break; |
| 3664 | case 4: /* xmlcharrefreplace */ |
| 3665 | /* generate replacement (temporarily (mis)uses p) */ |
| 3666 | for (p = collstart; p < collend; ++p) { |
| 3667 | char buffer[2+29+1+1]; |
| 3668 | char *cp; |
| 3669 | sprintf(buffer, "&#%d;", (int)*p); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3670 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3671 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 3672 | goto onError; |
| 3673 | for (cp = buffer; *cp; ++cp) |
| 3674 | *str++ = *cp; |
| 3675 | } |
| 3676 | p = collend; |
| 3677 | break; |
| 3678 | default: |
| 3679 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 3680 | reason, startp, size, &exc, |
| 3681 | collstart-startp, collend-startp, &newpos); |
| 3682 | if (repunicode == NULL) |
| 3683 | goto onError; |
| 3684 | /* generate replacement */ |
| 3685 | repsize = PyUnicode_GET_SIZE(repunicode); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3686 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3687 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 3688 | Py_DECREF(repunicode); |
| 3689 | goto onError; |
| 3690 | } |
| 3691 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 3692 | *str++ = *uni2; |
| 3693 | p = startp + newpos; |
| 3694 | Py_DECREF(repunicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3695 | } |
| 3696 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3697 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3698 | /* Resize if we allocated to much */ |
| 3699 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3700 | if (respos<PyUnicode_GET_SIZE(res)) { |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3701 | if (_PyUnicode_Resize(&res, respos) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3702 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3703 | } |
| 3704 | Py_XDECREF(exc); |
| 3705 | Py_XDECREF(errorHandler); |
| 3706 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3707 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3708 | onError: |
| 3709 | Py_XDECREF(res); |
| 3710 | Py_XDECREF(exc); |
| 3711 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3712 | return NULL; |
| 3713 | } |
| 3714 | |
| 3715 | PyObject *PyUnicode_Translate(PyObject *str, |
| 3716 | PyObject *mapping, |
| 3717 | const char *errors) |
| 3718 | { |
| 3719 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3720 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3721 | str = PyUnicode_FromObject(str); |
| 3722 | if (str == NULL) |
| 3723 | goto onError; |
| 3724 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 3725 | PyUnicode_GET_SIZE(str), |
| 3726 | mapping, |
| 3727 | errors); |
| 3728 | Py_DECREF(str); |
| 3729 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3730 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3731 | onError: |
| 3732 | Py_XDECREF(str); |
| 3733 | return NULL; |
| 3734 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3735 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3736 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 3737 | |
| 3738 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3739 | Py_ssize_t length, |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3740 | char *output, |
| 3741 | const char *errors) |
| 3742 | { |
| 3743 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3744 | PyObject *errorHandler = NULL; |
| 3745 | PyObject *exc = NULL; |
| 3746 | const char *encoding = "decimal"; |
| 3747 | const char *reason = "invalid decimal Unicode string"; |
| 3748 | /* the following variable is used for caching string comparisons |
| 3749 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 3750 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3751 | |
| 3752 | if (output == NULL) { |
| 3753 | PyErr_BadArgument(); |
| 3754 | return -1; |
| 3755 | } |
| 3756 | |
| 3757 | p = s; |
| 3758 | end = s + length; |
| 3759 | while (p < end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3760 | register Py_UNICODE ch = *p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3761 | int decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3762 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3763 | Py_ssize_t repsize; |
| 3764 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3765 | Py_UNICODE *uni2; |
| 3766 | Py_UNICODE *collstart; |
| 3767 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3768 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3769 | if (Py_UNICODE_ISSPACE(ch)) { |
| 3770 | *output++ = ' '; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3771 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3772 | continue; |
| 3773 | } |
| 3774 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 3775 | if (decimal >= 0) { |
| 3776 | *output++ = '0' + decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3777 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3778 | continue; |
| 3779 | } |
Guido van Rossum | ba47704 | 2000-04-06 18:18:10 +0000 | [diff] [blame] | 3780 | if (0 < ch && ch < 256) { |
Guido van Rossum | 42c29aa | 2000-05-03 23:58:29 +0000 | [diff] [blame] | 3781 | *output++ = (char)ch; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3782 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3783 | continue; |
| 3784 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3785 | /* All other characters are considered unencodable */ |
| 3786 | collstart = p; |
| 3787 | collend = p+1; |
| 3788 | while (collend < end) { |
| 3789 | if ((0 < *collend && *collend < 256) || |
| 3790 | !Py_UNICODE_ISSPACE(*collend) || |
| 3791 | Py_UNICODE_TODECIMAL(*collend)) |
| 3792 | break; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3793 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3794 | /* cache callback name lookup |
| 3795 | * (if not done yet, i.e. it's the first error) */ |
| 3796 | if (known_errorHandler==-1) { |
| 3797 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3798 | known_errorHandler = 1; |
| 3799 | else if (!strcmp(errors, "replace")) |
| 3800 | known_errorHandler = 2; |
| 3801 | else if (!strcmp(errors, "ignore")) |
| 3802 | known_errorHandler = 3; |
| 3803 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3804 | known_errorHandler = 4; |
| 3805 | else |
| 3806 | known_errorHandler = 0; |
| 3807 | } |
| 3808 | switch (known_errorHandler) { |
| 3809 | case 1: /* strict */ |
| 3810 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 3811 | goto onError; |
| 3812 | case 2: /* replace */ |
| 3813 | for (p = collstart; p < collend; ++p) |
| 3814 | *output++ = '?'; |
| 3815 | /* fall through */ |
| 3816 | case 3: /* ignore */ |
| 3817 | p = collend; |
| 3818 | break; |
| 3819 | case 4: /* xmlcharrefreplace */ |
| 3820 | /* generate replacement (temporarily (mis)uses p) */ |
| 3821 | for (p = collstart; p < collend; ++p) |
| 3822 | output += sprintf(output, "&#%d;", (int)*p); |
| 3823 | p = collend; |
| 3824 | break; |
| 3825 | default: |
| 3826 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 3827 | encoding, reason, s, length, &exc, |
| 3828 | collstart-s, collend-s, &newpos); |
| 3829 | if (repunicode == NULL) |
| 3830 | goto onError; |
| 3831 | /* generate replacement */ |
| 3832 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3833 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 3834 | Py_UNICODE ch = *uni2; |
| 3835 | if (Py_UNICODE_ISSPACE(ch)) |
| 3836 | *output++ = ' '; |
| 3837 | else { |
| 3838 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 3839 | if (decimal >= 0) |
| 3840 | *output++ = '0' + decimal; |
| 3841 | else if (0 < ch && ch < 256) |
| 3842 | *output++ = (char)ch; |
| 3843 | else { |
| 3844 | Py_DECREF(repunicode); |
| 3845 | raise_encode_exception(&exc, encoding, |
| 3846 | s, length, collstart-s, collend-s, reason); |
| 3847 | goto onError; |
| 3848 | } |
| 3849 | } |
| 3850 | } |
| 3851 | p = s + newpos; |
| 3852 | Py_DECREF(repunicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3853 | } |
| 3854 | } |
| 3855 | /* 0-terminate the output string */ |
| 3856 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3857 | Py_XDECREF(exc); |
| 3858 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3859 | return 0; |
| 3860 | |
| 3861 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3862 | Py_XDECREF(exc); |
| 3863 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3864 | return -1; |
| 3865 | } |
| 3866 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3867 | /* --- Helpers ------------------------------------------------------------ */ |
| 3868 | |
Fredrik Lundh | 6471ee4 | 2006-05-24 14:28:11 +0000 | [diff] [blame] | 3869 | /* fast search/count implementation, based on a mix between boyer- |
| 3870 | moore and horspool, with a few more bells and whistles on the top. |
| 3871 | for some more background, see: http://effbot.org/stringlib */ |
| 3872 | |
Fredrik Lundh | d5e0dc5 | 2006-05-24 15:11:01 +0000 | [diff] [blame] | 3873 | /* note: fastsearch may access s[n], which isn't a problem when using |
Fredrik Lundh | 0c71f88 | 2006-05-25 16:46:54 +0000 | [diff] [blame] | 3874 | Python's ordinary string types, but may cause problems if you're |
| 3875 | using this code in other contexts. also, the count mode returns -1 |
| 3876 | if there cannot possible be a match in the target string, and 0 if |
| 3877 | it has actually checked for matches, but didn't find any. callers |
| 3878 | beware! */ |
Fredrik Lundh | d5e0dc5 | 2006-05-24 15:11:01 +0000 | [diff] [blame] | 3879 | |
Fredrik Lundh | 6471ee4 | 2006-05-24 14:28:11 +0000 | [diff] [blame] | 3880 | #define FAST_COUNT 0 |
| 3881 | #define FAST_SEARCH 1 |
| 3882 | |
Fredrik Lundh | d5e0dc5 | 2006-05-24 15:11:01 +0000 | [diff] [blame] | 3883 | LOCAL(Py_ssize_t) |
| 3884 | fastsearch(Py_UNICODE* s, Py_ssize_t n, Py_UNICODE* p, Py_ssize_t m, int mode) |
Fredrik Lundh | 6471ee4 | 2006-05-24 14:28:11 +0000 | [diff] [blame] | 3885 | { |
| 3886 | long mask; |
Fredrik Lundh | f2c0dfd | 2006-05-26 10:27:17 +0000 | [diff] [blame^] | 3887 | Py_ssize_t skip, count = 0; |
Fredrik Lundh | 6471ee4 | 2006-05-24 14:28:11 +0000 | [diff] [blame] | 3888 | Py_ssize_t i, j, mlast, w; |
| 3889 | |
| 3890 | w = n - m; |
| 3891 | |
| 3892 | if (w < 0) |
| 3893 | return -1; |
| 3894 | |
| 3895 | /* look for special cases */ |
| 3896 | if (m <= 1) { |
Fredrik Lundh | d5e0dc5 | 2006-05-24 15:11:01 +0000 | [diff] [blame] | 3897 | if (m <= 0) |
Fredrik Lundh | 6471ee4 | 2006-05-24 14:28:11 +0000 | [diff] [blame] | 3898 | return -1; |
| 3899 | /* use special case for 1-character strings */ |
| 3900 | if (mode == FAST_COUNT) { |
| 3901 | for (i = 0; i < n; i++) |
| 3902 | if (s[i] == p[0]) |
| 3903 | count++; |
| 3904 | return count; |
| 3905 | } else { |
| 3906 | for (i = 0; i < n; i++) |
| 3907 | if (s[i] == p[0]) |
| 3908 | return i; |
| 3909 | } |
| 3910 | return -1; |
| 3911 | } |
| 3912 | |
| 3913 | mlast = m - 1; |
| 3914 | |
| 3915 | /* create compressed boyer-moore delta 1 table */ |
| 3916 | skip = mlast - 1; |
| 3917 | /* process pattern[:-1] */ |
| 3918 | for (mask = i = 0; i < mlast; i++) { |
| 3919 | mask |= (1 << (p[i] & 0x1F)); |
| 3920 | if (p[i] == p[mlast]) |
| 3921 | skip = mlast - i - 1; |
| 3922 | } |
| 3923 | /* process pattern[-1] outside the loop */ |
| 3924 | mask |= (1 << (p[mlast] & 0x1F)); |
| 3925 | |
| 3926 | for (i = 0; i <= w; i++) { |
| 3927 | /* note: using mlast in the skip path slows things down on x86 */ |
| 3928 | if (s[i+m-1] == p[m-1]) { |
| 3929 | /* candidate match */ |
| 3930 | for (j = 0; j < mlast; j++) |
| 3931 | if (s[i+j] != p[j]) |
| 3932 | break; |
| 3933 | if (j == mlast) { |
| 3934 | /* got a match! */ |
| 3935 | if (mode != FAST_COUNT) |
| 3936 | return i; |
| 3937 | count++; |
| 3938 | i = i + mlast; |
| 3939 | continue; |
| 3940 | } |
| 3941 | /* miss: check if next character is part of pattern */ |
| 3942 | if (!(mask & (1 << (s[i+m] & 0x1F)))) |
| 3943 | i = i + m; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 3944 | else |
Fredrik Lundh | 6471ee4 | 2006-05-24 14:28:11 +0000 | [diff] [blame] | 3945 | i = i + skip; |
Fredrik Lundh | 6471ee4 | 2006-05-24 14:28:11 +0000 | [diff] [blame] | 3946 | } else { |
| 3947 | /* skip: check if next character is part of pattern */ |
| 3948 | if (!(mask & (1 << (s[i+m] & 0x1F)))) |
| 3949 | i = i + m; |
| 3950 | } |
| 3951 | } |
| 3952 | |
| 3953 | if (mode != FAST_COUNT) |
| 3954 | return -1; |
| 3955 | return count; |
| 3956 | } |
| 3957 | |
| 3958 | LOCAL(Py_ssize_t) count(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3959 | Py_ssize_t start, |
| 3960 | Py_ssize_t end, |
| 3961 | PyUnicodeObject *substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3962 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3963 | Py_ssize_t count = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3964 | |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3965 | if (start < 0) |
| 3966 | start += self->length; |
| 3967 | if (start < 0) |
| 3968 | start = 0; |
| 3969 | if (end > self->length) |
| 3970 | end = self->length; |
| 3971 | if (end < 0) |
| 3972 | end += self->length; |
| 3973 | if (end < 0) |
| 3974 | end = 0; |
| 3975 | |
Marc-André Lemburg | 49ef6dc | 2000-06-18 22:25:22 +0000 | [diff] [blame] | 3976 | if (substring->length == 0) |
| 3977 | return (end - start + 1); |
| 3978 | |
Fredrik Lundh | 6471ee4 | 2006-05-24 14:28:11 +0000 | [diff] [blame] | 3979 | count = fastsearch( |
| 3980 | PyUnicode_AS_UNICODE(self) + start, end - start, |
| 3981 | substring->str, substring->length, FAST_COUNT |
| 3982 | ); |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 3983 | |
Fredrik Lundh | 6471ee4 | 2006-05-24 14:28:11 +0000 | [diff] [blame] | 3984 | if (count < 0) |
| 3985 | count = 0; /* no match */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3986 | |
| 3987 | return count; |
| 3988 | } |
| 3989 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3990 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3991 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3992 | Py_ssize_t start, |
| 3993 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3994 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3995 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3996 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3997 | str = PyUnicode_FromObject(str); |
| 3998 | if (str == NULL) |
| 3999 | return -1; |
| 4000 | substr = PyUnicode_FromObject(substr); |
| 4001 | if (substr == NULL) { |
Marc-André Lemburg | 49ef6dc | 2000-06-18 22:25:22 +0000 | [diff] [blame] | 4002 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4003 | return -1; |
| 4004 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4005 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4006 | result = count((PyUnicodeObject *)str, |
| 4007 | start, end, |
| 4008 | (PyUnicodeObject *)substr); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4009 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4010 | Py_DECREF(str); |
| 4011 | Py_DECREF(substr); |
| 4012 | return result; |
| 4013 | } |
| 4014 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4015 | static Py_ssize_t findstring(PyUnicodeObject *self, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4016 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4017 | Py_ssize_t start, |
| 4018 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4019 | int direction) |
| 4020 | { |
| 4021 | if (start < 0) |
| 4022 | start += self->length; |
| 4023 | if (start < 0) |
| 4024 | start = 0; |
| 4025 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4026 | if (end > self->length) |
| 4027 | end = self->length; |
| 4028 | if (end < 0) |
| 4029 | end += self->length; |
| 4030 | if (end < 0) |
| 4031 | end = 0; |
| 4032 | |
Guido van Rossum | 76afbd9 | 2002-08-20 17:29:29 +0000 | [diff] [blame] | 4033 | if (substring->length == 0) |
| 4034 | return (direction > 0) ? start : end; |
| 4035 | |
Fredrik Lundh | 6471ee4 | 2006-05-24 14:28:11 +0000 | [diff] [blame] | 4036 | if (direction > 0) { |
| 4037 | Py_ssize_t pos = fastsearch( |
| 4038 | PyUnicode_AS_UNICODE(self) + start, end - start, |
| 4039 | substring->str, substring->length, FAST_SEARCH |
| 4040 | ); |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 4041 | if (pos >= 0) |
| 4042 | return pos + start; |
| 4043 | } else { |
| 4044 | end -= substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4045 | for (; end >= start; end--) |
| 4046 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 4047 | return end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4048 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4049 | return -1; |
| 4050 | } |
| 4051 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4052 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4053 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4054 | Py_ssize_t start, |
| 4055 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4056 | int direction) |
| 4057 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4058 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4059 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4060 | str = PyUnicode_FromObject(str); |
| 4061 | if (str == NULL) |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 4062 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4063 | substr = PyUnicode_FromObject(substr); |
| 4064 | if (substr == NULL) { |
Marc-André Lemburg | 4164439 | 2002-05-29 13:46:29 +0000 | [diff] [blame] | 4065 | Py_DECREF(str); |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 4066 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4067 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4068 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4069 | result = findstring((PyUnicodeObject *)str, |
| 4070 | (PyUnicodeObject *)substr, |
| 4071 | start, end, direction); |
| 4072 | Py_DECREF(str); |
| 4073 | Py_DECREF(substr); |
| 4074 | return result; |
| 4075 | } |
| 4076 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4077 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4078 | int tailmatch(PyUnicodeObject *self, |
| 4079 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4080 | Py_ssize_t start, |
| 4081 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4082 | int direction) |
| 4083 | { |
| 4084 | if (start < 0) |
| 4085 | start += self->length; |
| 4086 | if (start < 0) |
| 4087 | start = 0; |
| 4088 | |
| 4089 | if (substring->length == 0) |
| 4090 | return 1; |
| 4091 | |
| 4092 | if (end > self->length) |
| 4093 | end = self->length; |
| 4094 | if (end < 0) |
| 4095 | end += self->length; |
| 4096 | if (end < 0) |
| 4097 | end = 0; |
| 4098 | |
| 4099 | end -= substring->length; |
| 4100 | if (end < start) |
| 4101 | return 0; |
| 4102 | |
| 4103 | if (direction > 0) { |
| 4104 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 4105 | return 1; |
| 4106 | } else { |
| 4107 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 4108 | return 1; |
| 4109 | } |
| 4110 | |
| 4111 | return 0; |
| 4112 | } |
| 4113 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4114 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4115 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4116 | Py_ssize_t start, |
| 4117 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4118 | int direction) |
| 4119 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4120 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4121 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4122 | str = PyUnicode_FromObject(str); |
| 4123 | if (str == NULL) |
| 4124 | return -1; |
| 4125 | substr = PyUnicode_FromObject(substr); |
| 4126 | if (substr == NULL) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 4127 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4128 | return -1; |
| 4129 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4130 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4131 | result = tailmatch((PyUnicodeObject *)str, |
| 4132 | (PyUnicodeObject *)substr, |
| 4133 | start, end, direction); |
| 4134 | Py_DECREF(str); |
| 4135 | Py_DECREF(substr); |
| 4136 | return result; |
| 4137 | } |
| 4138 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4139 | /* Apply fixfct filter to the Unicode object self and return a |
| 4140 | reference to the modified object */ |
| 4141 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4142 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4143 | PyObject *fixup(PyUnicodeObject *self, |
| 4144 | int (*fixfct)(PyUnicodeObject *s)) |
| 4145 | { |
| 4146 | |
| 4147 | PyUnicodeObject *u; |
| 4148 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4149 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4150 | if (u == NULL) |
| 4151 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4152 | |
| 4153 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 4154 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4155 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4156 | /* fixfct should return TRUE if it modified the buffer. If |
| 4157 | FALSE, return a reference to the original buffer instead |
| 4158 | (to save space, not time) */ |
| 4159 | Py_INCREF(self); |
| 4160 | Py_DECREF(u); |
| 4161 | return (PyObject*) self; |
| 4162 | } |
| 4163 | return (PyObject*) u; |
| 4164 | } |
| 4165 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4166 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4167 | int fixupper(PyUnicodeObject *self) |
| 4168 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4169 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4170 | Py_UNICODE *s = self->str; |
| 4171 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4172 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4173 | while (len-- > 0) { |
| 4174 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4175 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4176 | ch = Py_UNICODE_TOUPPER(*s); |
| 4177 | if (ch != *s) { |
| 4178 | status = 1; |
| 4179 | *s = ch; |
| 4180 | } |
| 4181 | s++; |
| 4182 | } |
| 4183 | |
| 4184 | return status; |
| 4185 | } |
| 4186 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4187 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4188 | int fixlower(PyUnicodeObject *self) |
| 4189 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4190 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4191 | Py_UNICODE *s = self->str; |
| 4192 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4193 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4194 | while (len-- > 0) { |
| 4195 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4196 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4197 | ch = Py_UNICODE_TOLOWER(*s); |
| 4198 | if (ch != *s) { |
| 4199 | status = 1; |
| 4200 | *s = ch; |
| 4201 | } |
| 4202 | s++; |
| 4203 | } |
| 4204 | |
| 4205 | return status; |
| 4206 | } |
| 4207 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4208 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4209 | int fixswapcase(PyUnicodeObject *self) |
| 4210 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4211 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4212 | Py_UNICODE *s = self->str; |
| 4213 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4214 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4215 | while (len-- > 0) { |
| 4216 | if (Py_UNICODE_ISUPPER(*s)) { |
| 4217 | *s = Py_UNICODE_TOLOWER(*s); |
| 4218 | status = 1; |
| 4219 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 4220 | *s = Py_UNICODE_TOUPPER(*s); |
| 4221 | status = 1; |
| 4222 | } |
| 4223 | s++; |
| 4224 | } |
| 4225 | |
| 4226 | return status; |
| 4227 | } |
| 4228 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4229 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4230 | int fixcapitalize(PyUnicodeObject *self) |
| 4231 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4232 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4233 | Py_UNICODE *s = self->str; |
| 4234 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4235 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4236 | if (len == 0) |
| 4237 | return 0; |
| 4238 | if (Py_UNICODE_ISLOWER(*s)) { |
| 4239 | *s = Py_UNICODE_TOUPPER(*s); |
| 4240 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4241 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4242 | s++; |
| 4243 | while (--len > 0) { |
| 4244 | if (Py_UNICODE_ISUPPER(*s)) { |
| 4245 | *s = Py_UNICODE_TOLOWER(*s); |
| 4246 | status = 1; |
| 4247 | } |
| 4248 | s++; |
| 4249 | } |
| 4250 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4251 | } |
| 4252 | |
| 4253 | static |
| 4254 | int fixtitle(PyUnicodeObject *self) |
| 4255 | { |
| 4256 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 4257 | register Py_UNICODE *e; |
| 4258 | int previous_is_cased; |
| 4259 | |
| 4260 | /* Shortcut for single character strings */ |
| 4261 | if (PyUnicode_GET_SIZE(self) == 1) { |
| 4262 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 4263 | if (*p != ch) { |
| 4264 | *p = ch; |
| 4265 | return 1; |
| 4266 | } |
| 4267 | else |
| 4268 | return 0; |
| 4269 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4270 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4271 | e = p + PyUnicode_GET_SIZE(self); |
| 4272 | previous_is_cased = 0; |
| 4273 | for (; p < e; p++) { |
| 4274 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4275 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4276 | if (previous_is_cased) |
| 4277 | *p = Py_UNICODE_TOLOWER(ch); |
| 4278 | else |
| 4279 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4280 | |
| 4281 | if (Py_UNICODE_ISLOWER(ch) || |
| 4282 | Py_UNICODE_ISUPPER(ch) || |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4283 | Py_UNICODE_ISTITLE(ch)) |
| 4284 | previous_is_cased = 1; |
| 4285 | else |
| 4286 | previous_is_cased = 0; |
| 4287 | } |
| 4288 | return 1; |
| 4289 | } |
| 4290 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4291 | PyObject * |
| 4292 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4293 | { |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4294 | PyObject *internal_separator = NULL; |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 4295 | const Py_UNICODE blank = ' '; |
| 4296 | const Py_UNICODE *sep = ␣ |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4297 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4298 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4299 | Py_ssize_t res_alloc = 100; /* # allocated bytes for string in res */ |
| 4300 | Py_ssize_t res_used; /* # used bytes */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4301 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 4302 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4303 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4304 | PyObject *item; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4305 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4306 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4307 | fseq = PySequence_Fast(seq, ""); |
| 4308 | if (fseq == NULL) { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4309 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4310 | } |
| 4311 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4312 | /* Grrrr. A codec may be invoked to convert str objects to |
| 4313 | * Unicode, and so it's possible to call back into Python code |
| 4314 | * during PyUnicode_FromObject(), and so it's possible for a sick |
| 4315 | * codec to change the size of fseq (if seq is a list). Therefore |
| 4316 | * we have to keep refetching the size -- can't assume seqlen |
| 4317 | * is invariant. |
| 4318 | */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4319 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 4320 | /* If empty sequence, return u"". */ |
| 4321 | if (seqlen == 0) { |
| 4322 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 4323 | goto Done; |
| 4324 | } |
| 4325 | /* If singleton sequence with an exact Unicode, return that. */ |
| 4326 | if (seqlen == 1) { |
| 4327 | item = PySequence_Fast_GET_ITEM(fseq, 0); |
| 4328 | if (PyUnicode_CheckExact(item)) { |
| 4329 | Py_INCREF(item); |
| 4330 | res = (PyUnicodeObject *)item; |
| 4331 | goto Done; |
| 4332 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4333 | } |
| 4334 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4335 | /* At least two items to join, or one that isn't exact Unicode. */ |
| 4336 | if (seqlen > 1) { |
| 4337 | /* Set up sep and seplen -- they're needed. */ |
| 4338 | if (separator == NULL) { |
| 4339 | sep = ␣ |
| 4340 | seplen = 1; |
| 4341 | } |
| 4342 | else { |
| 4343 | internal_separator = PyUnicode_FromObject(separator); |
| 4344 | if (internal_separator == NULL) |
| 4345 | goto onError; |
| 4346 | sep = PyUnicode_AS_UNICODE(internal_separator); |
| 4347 | seplen = PyUnicode_GET_SIZE(internal_separator); |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4348 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 4349 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4350 | } |
| 4351 | } |
| 4352 | |
| 4353 | /* Get space. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4354 | res = _PyUnicode_New(res_alloc); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4355 | if (res == NULL) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4356 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4357 | res_p = PyUnicode_AS_UNICODE(res); |
| 4358 | res_used = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4359 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4360 | for (i = 0; i < seqlen; ++i) { |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4361 | Py_ssize_t itemlen; |
| 4362 | Py_ssize_t new_res_used; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4363 | |
| 4364 | item = PySequence_Fast_GET_ITEM(fseq, i); |
| 4365 | /* Convert item to Unicode. */ |
| 4366 | if (! PyUnicode_Check(item) && ! PyString_Check(item)) { |
| 4367 | PyErr_Format(PyExc_TypeError, |
Thomas Wouters | 715a4cd | 2006-04-16 22:04:49 +0000 | [diff] [blame] | 4368 | "sequence item %zd: expected string or Unicode," |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4369 | " %.80s found", |
| 4370 | i, item->ob_type->tp_name); |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 4371 | goto onError; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4372 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4373 | item = PyUnicode_FromObject(item); |
| 4374 | if (item == NULL) |
| 4375 | goto onError; |
| 4376 | /* We own a reference to item from here on. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4377 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4378 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 4379 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 4380 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4381 | /* 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] | 4382 | itemlen = PyUnicode_GET_SIZE(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4383 | new_res_used = res_used + itemlen; |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4384 | if (new_res_used <= 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4385 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4386 | if (i < seqlen - 1) { |
| 4387 | new_res_used += seplen; |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4388 | if (new_res_used <= 0) |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4389 | goto Overflow; |
| 4390 | } |
| 4391 | if (new_res_used > res_alloc) { |
| 4392 | /* double allocated size until it's big enough */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4393 | do { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4394 | res_alloc += res_alloc; |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4395 | if (res_alloc <= 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4396 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4397 | } while (new_res_used > res_alloc); |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4398 | if (_PyUnicode_Resize(&res, res_alloc) < 0) { |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4399 | Py_DECREF(item); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4400 | goto onError; |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4401 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4402 | res_p = PyUnicode_AS_UNICODE(res) + res_used; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4403 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4404 | |
| 4405 | /* Copy item, and maybe the separator. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4406 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4407 | res_p += itemlen; |
| 4408 | if (i < seqlen - 1) { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4409 | Py_UNICODE_COPY(res_p, sep, seplen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4410 | res_p += seplen; |
| 4411 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4412 | Py_DECREF(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4413 | res_used = new_res_used; |
| 4414 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4415 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4416 | /* Shrink res to match the used area; this probably can't fail, |
| 4417 | * but it's cheap to check. |
| 4418 | */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4419 | if (_PyUnicode_Resize(&res, res_used) < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4420 | goto onError; |
| 4421 | |
| 4422 | Done: |
| 4423 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4424 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4425 | return (PyObject *)res; |
| 4426 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4427 | Overflow: |
| 4428 | PyErr_SetString(PyExc_OverflowError, |
| 4429 | "join() is too long for a Python string"); |
| 4430 | Py_DECREF(item); |
| 4431 | /* fall through */ |
| 4432 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4433 | onError: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4434 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4435 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4436 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4437 | return NULL; |
| 4438 | } |
| 4439 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4440 | static |
| 4441 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4442 | Py_ssize_t left, |
| 4443 | Py_ssize_t right, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4444 | Py_UNICODE fill) |
| 4445 | { |
| 4446 | PyUnicodeObject *u; |
| 4447 | |
| 4448 | if (left < 0) |
| 4449 | left = 0; |
| 4450 | if (right < 0) |
| 4451 | right = 0; |
| 4452 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4453 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4454 | Py_INCREF(self); |
| 4455 | return self; |
| 4456 | } |
| 4457 | |
| 4458 | u = _PyUnicode_New(left + self->length + right); |
| 4459 | if (u) { |
| 4460 | if (left) |
| 4461 | Py_UNICODE_FILL(u->str, fill, left); |
| 4462 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 4463 | if (right) |
| 4464 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 4465 | } |
| 4466 | |
| 4467 | return u; |
| 4468 | } |
| 4469 | |
| 4470 | #define SPLIT_APPEND(data, left, right) \ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4471 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4472 | if (!str) \ |
| 4473 | goto onError; \ |
| 4474 | if (PyList_Append(list, str)) { \ |
| 4475 | Py_DECREF(str); \ |
| 4476 | goto onError; \ |
| 4477 | } \ |
| 4478 | else \ |
| 4479 | Py_DECREF(str); |
| 4480 | |
| 4481 | static |
| 4482 | PyObject *split_whitespace(PyUnicodeObject *self, |
| 4483 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4484 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4485 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4486 | register Py_ssize_t i; |
| 4487 | register Py_ssize_t j; |
| 4488 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4489 | PyObject *str; |
| 4490 | |
| 4491 | for (i = j = 0; i < len; ) { |
| 4492 | /* find a token */ |
| 4493 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 4494 | i++; |
| 4495 | j = i; |
| 4496 | while (i < len && !Py_UNICODE_ISSPACE(self->str[i])) |
| 4497 | i++; |
| 4498 | if (j < i) { |
| 4499 | if (maxcount-- <= 0) |
| 4500 | break; |
| 4501 | SPLIT_APPEND(self->str, j, i); |
| 4502 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 4503 | i++; |
| 4504 | j = i; |
| 4505 | } |
| 4506 | } |
| 4507 | if (j < len) { |
| 4508 | SPLIT_APPEND(self->str, j, len); |
| 4509 | } |
| 4510 | return list; |
| 4511 | |
| 4512 | onError: |
| 4513 | Py_DECREF(list); |
| 4514 | return NULL; |
| 4515 | } |
| 4516 | |
| 4517 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4518 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4519 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4520 | register Py_ssize_t i; |
| 4521 | register Py_ssize_t j; |
| 4522 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4523 | PyObject *list; |
| 4524 | PyObject *str; |
| 4525 | Py_UNICODE *data; |
| 4526 | |
| 4527 | string = PyUnicode_FromObject(string); |
| 4528 | if (string == NULL) |
| 4529 | return NULL; |
| 4530 | data = PyUnicode_AS_UNICODE(string); |
| 4531 | len = PyUnicode_GET_SIZE(string); |
| 4532 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4533 | list = PyList_New(0); |
| 4534 | if (!list) |
| 4535 | goto onError; |
| 4536 | |
| 4537 | for (i = j = 0; i < len; ) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4538 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4539 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4540 | /* Find a line and append it */ |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4541 | while (i < len && !BLOOM_LINEBREAK(data[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4542 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4543 | |
| 4544 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4545 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4546 | if (i < len) { |
| 4547 | if (data[i] == '\r' && i + 1 < len && |
| 4548 | data[i+1] == '\n') |
| 4549 | i += 2; |
| 4550 | else |
| 4551 | i++; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4552 | if (keepends) |
| 4553 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4554 | } |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4555 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4556 | j = i; |
| 4557 | } |
| 4558 | if (j < len) { |
| 4559 | SPLIT_APPEND(data, j, len); |
| 4560 | } |
| 4561 | |
| 4562 | Py_DECREF(string); |
| 4563 | return list; |
| 4564 | |
| 4565 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 4566 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4567 | Py_DECREF(string); |
| 4568 | return NULL; |
| 4569 | } |
| 4570 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4571 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4572 | PyObject *split_char(PyUnicodeObject *self, |
| 4573 | PyObject *list, |
| 4574 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4575 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4576 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4577 | register Py_ssize_t i; |
| 4578 | register Py_ssize_t j; |
| 4579 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4580 | PyObject *str; |
| 4581 | |
| 4582 | for (i = j = 0; i < len; ) { |
| 4583 | if (self->str[i] == ch) { |
| 4584 | if (maxcount-- <= 0) |
| 4585 | break; |
| 4586 | SPLIT_APPEND(self->str, j, i); |
| 4587 | i = j = i + 1; |
| 4588 | } else |
| 4589 | i++; |
| 4590 | } |
| 4591 | if (j <= len) { |
| 4592 | SPLIT_APPEND(self->str, j, len); |
| 4593 | } |
| 4594 | return list; |
| 4595 | |
| 4596 | onError: |
| 4597 | Py_DECREF(list); |
| 4598 | return NULL; |
| 4599 | } |
| 4600 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4601 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4602 | PyObject *split_substring(PyUnicodeObject *self, |
| 4603 | PyObject *list, |
| 4604 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4605 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4606 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4607 | register Py_ssize_t i; |
| 4608 | register Py_ssize_t j; |
| 4609 | Py_ssize_t len = self->length; |
| 4610 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4611 | PyObject *str; |
| 4612 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 4613 | for (i = j = 0; i <= len - sublen; ) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4614 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 4615 | if (maxcount-- <= 0) |
| 4616 | break; |
| 4617 | SPLIT_APPEND(self->str, j, i); |
| 4618 | i = j = i + sublen; |
| 4619 | } else |
| 4620 | i++; |
| 4621 | } |
| 4622 | if (j <= len) { |
| 4623 | SPLIT_APPEND(self->str, j, len); |
| 4624 | } |
| 4625 | return list; |
| 4626 | |
| 4627 | onError: |
| 4628 | Py_DECREF(list); |
| 4629 | return NULL; |
| 4630 | } |
| 4631 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4632 | static |
| 4633 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
| 4634 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4635 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4636 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4637 | register Py_ssize_t i; |
| 4638 | register Py_ssize_t j; |
| 4639 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4640 | PyObject *str; |
| 4641 | |
| 4642 | for (i = j = len - 1; i >= 0; ) { |
| 4643 | /* find a token */ |
| 4644 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 4645 | i--; |
| 4646 | j = i; |
| 4647 | while (i >= 0 && !Py_UNICODE_ISSPACE(self->str[i])) |
| 4648 | i--; |
| 4649 | if (j > i) { |
| 4650 | if (maxcount-- <= 0) |
| 4651 | break; |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4652 | SPLIT_APPEND(self->str, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4653 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 4654 | i--; |
| 4655 | j = i; |
| 4656 | } |
| 4657 | } |
| 4658 | if (j >= 0) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4659 | SPLIT_APPEND(self->str, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4660 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4661 | if (PyList_Reverse(list) < 0) |
| 4662 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4663 | return list; |
| 4664 | |
| 4665 | onError: |
| 4666 | Py_DECREF(list); |
| 4667 | return NULL; |
| 4668 | } |
| 4669 | |
| 4670 | static |
| 4671 | PyObject *rsplit_char(PyUnicodeObject *self, |
| 4672 | PyObject *list, |
| 4673 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4674 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4675 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4676 | register Py_ssize_t i; |
| 4677 | register Py_ssize_t j; |
| 4678 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4679 | PyObject *str; |
| 4680 | |
| 4681 | for (i = j = len - 1; i >= 0; ) { |
| 4682 | if (self->str[i] == ch) { |
| 4683 | if (maxcount-- <= 0) |
| 4684 | break; |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4685 | SPLIT_APPEND(self->str, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4686 | j = i = i - 1; |
| 4687 | } else |
| 4688 | i--; |
| 4689 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 4690 | if (j >= -1) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4691 | SPLIT_APPEND(self->str, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4692 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4693 | if (PyList_Reverse(list) < 0) |
| 4694 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4695 | return list; |
| 4696 | |
| 4697 | onError: |
| 4698 | Py_DECREF(list); |
| 4699 | return NULL; |
| 4700 | } |
| 4701 | |
| 4702 | static |
| 4703 | PyObject *rsplit_substring(PyUnicodeObject *self, |
| 4704 | PyObject *list, |
| 4705 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4706 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4707 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4708 | register Py_ssize_t i; |
| 4709 | register Py_ssize_t j; |
| 4710 | Py_ssize_t len = self->length; |
| 4711 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4712 | PyObject *str; |
| 4713 | |
| 4714 | for (i = len - sublen, j = len; i >= 0; ) { |
| 4715 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 4716 | if (maxcount-- <= 0) |
| 4717 | break; |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4718 | SPLIT_APPEND(self->str, i + sublen, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4719 | j = i; |
| 4720 | i -= sublen; |
| 4721 | } else |
| 4722 | i--; |
| 4723 | } |
| 4724 | if (j >= 0) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4725 | SPLIT_APPEND(self->str, 0, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4726 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4727 | if (PyList_Reverse(list) < 0) |
| 4728 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4729 | return list; |
| 4730 | |
| 4731 | onError: |
| 4732 | Py_DECREF(list); |
| 4733 | return NULL; |
| 4734 | } |
| 4735 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4736 | #undef SPLIT_APPEND |
| 4737 | |
| 4738 | static |
| 4739 | PyObject *split(PyUnicodeObject *self, |
| 4740 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4741 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4742 | { |
| 4743 | PyObject *list; |
| 4744 | |
| 4745 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4746 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4747 | |
| 4748 | list = PyList_New(0); |
| 4749 | if (!list) |
| 4750 | return NULL; |
| 4751 | |
| 4752 | if (substring == NULL) |
| 4753 | return split_whitespace(self,list,maxcount); |
| 4754 | |
| 4755 | else if (substring->length == 1) |
| 4756 | return split_char(self,list,substring->str[0],maxcount); |
| 4757 | |
| 4758 | else if (substring->length == 0) { |
| 4759 | Py_DECREF(list); |
| 4760 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 4761 | return NULL; |
| 4762 | } |
| 4763 | else |
| 4764 | return split_substring(self,list,substring,maxcount); |
| 4765 | } |
| 4766 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4767 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4768 | PyObject *rsplit(PyUnicodeObject *self, |
| 4769 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4770 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4771 | { |
| 4772 | PyObject *list; |
| 4773 | |
| 4774 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4775 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4776 | |
| 4777 | list = PyList_New(0); |
| 4778 | if (!list) |
| 4779 | return NULL; |
| 4780 | |
| 4781 | if (substring == NULL) |
| 4782 | return rsplit_whitespace(self,list,maxcount); |
| 4783 | |
| 4784 | else if (substring->length == 1) |
| 4785 | return rsplit_char(self,list,substring->str[0],maxcount); |
| 4786 | |
| 4787 | else if (substring->length == 0) { |
| 4788 | Py_DECREF(list); |
| 4789 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 4790 | return NULL; |
| 4791 | } |
| 4792 | else |
| 4793 | return rsplit_substring(self,list,substring,maxcount); |
| 4794 | } |
| 4795 | |
| 4796 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4797 | PyObject *replace(PyUnicodeObject *self, |
| 4798 | PyUnicodeObject *str1, |
| 4799 | PyUnicodeObject *str2, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4800 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4801 | { |
| 4802 | PyUnicodeObject *u; |
| 4803 | |
| 4804 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4805 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4806 | |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 4807 | if (str1->length == str2->length) { |
| 4808 | /* same length */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4809 | Py_ssize_t i; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 4810 | if (str1->length == 1) { |
| 4811 | /* replace characters */ |
| 4812 | Py_UNICODE u1, u2; |
| 4813 | if (!findchar(self->str, self->length, str1->str[0])) |
| 4814 | goto nothing; |
| 4815 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 4816 | if (!u) |
| 4817 | return NULL; |
| 4818 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 4819 | u1 = str1->str[0]; |
| 4820 | u2 = str2->str[0]; |
| 4821 | for (i = 0; i < u->length; i++) |
| 4822 | if (u->str[i] == u1) { |
| 4823 | if (--maxcount < 0) |
| 4824 | break; |
| 4825 | u->str[i] = u2; |
| 4826 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4827 | } else { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 4828 | i = fastsearch( |
| 4829 | self->str, self->length, str1->str, str1->length, FAST_SEARCH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4830 | ); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 4831 | if (i < 0) |
| 4832 | goto nothing; |
| 4833 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 4834 | if (!u) |
| 4835 | return NULL; |
| 4836 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 4837 | while (i <= self->length - str1->length) |
| 4838 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 4839 | if (--maxcount < 0) |
| 4840 | break; |
| 4841 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 4842 | i += str1->length; |
| 4843 | } else |
| 4844 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4845 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4846 | } else { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 4847 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4848 | Py_ssize_t n, i; |
Fredrik Lundh | 0c71f88 | 2006-05-25 16:46:54 +0000 | [diff] [blame] | 4849 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4850 | Py_UNICODE *p; |
| 4851 | |
| 4852 | /* replace strings */ |
| 4853 | n = count(self, 0, self->length, str1); |
| 4854 | if (n > maxcount) |
| 4855 | n = maxcount; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 4856 | if (n == 0) |
| 4857 | goto nothing; |
Fredrik Lundh | 0c71f88 | 2006-05-25 16:46:54 +0000 | [diff] [blame] | 4858 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 4859 | delta = (str2->length - str1->length); |
| 4860 | if (delta == 0) { |
| 4861 | new_size = self->length; |
| 4862 | } else { |
| 4863 | product = n * (str2->length - str1->length); |
| 4864 | if ((product / (str2->length - str1->length)) != n) { |
| 4865 | PyErr_SetString(PyExc_OverflowError, |
| 4866 | "replace string is too long"); |
| 4867 | return NULL; |
| 4868 | } |
| 4869 | new_size = self->length + product; |
| 4870 | if (new_size < 0) { |
| 4871 | PyErr_SetString(PyExc_OverflowError, |
| 4872 | "replace string is too long"); |
| 4873 | return NULL; |
| 4874 | } |
| 4875 | } |
| 4876 | u = _PyUnicode_New(new_size); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 4877 | if (!u) |
| 4878 | return NULL; |
| 4879 | i = 0; |
| 4880 | p = u->str; |
| 4881 | if (str1->length > 0) { |
| 4882 | while (i <= self->length - str1->length) |
| 4883 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 4884 | /* replace string segment */ |
| 4885 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 4886 | p += str2->length; |
| 4887 | i += str1->length; |
| 4888 | if (--n <= 0) { |
| 4889 | /* copy remaining part */ |
| 4890 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 4891 | break; |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 4892 | } |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 4893 | } else |
| 4894 | *p++ = self->str[i++]; |
| 4895 | } else { |
| 4896 | while (n > 0) { |
| 4897 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 4898 | p += str2->length; |
| 4899 | if (--n <= 0) |
| 4900 | break; |
| 4901 | *p++ = self->str[i++]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4902 | } |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 4903 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4904 | } |
| 4905 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4906 | return (PyObject *) u; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 4907 | |
| 4908 | nothing: |
| 4909 | /* nothing to replace; return original string (when possible) */ |
| 4910 | if (PyUnicode_CheckExact(self)) { |
| 4911 | Py_INCREF(self); |
| 4912 | return (PyObject *) self; |
| 4913 | } |
| 4914 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4915 | } |
| 4916 | |
| 4917 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 4918 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4919 | PyDoc_STRVAR(title__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4920 | "S.title() -> unicode\n\ |
| 4921 | \n\ |
| 4922 | 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] | 4923 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4924 | |
| 4925 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 4926 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4927 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4928 | return fixup(self, fixtitle); |
| 4929 | } |
| 4930 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4931 | PyDoc_STRVAR(capitalize__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4932 | "S.capitalize() -> unicode\n\ |
| 4933 | \n\ |
| 4934 | 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] | 4935 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4936 | |
| 4937 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 4938 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4939 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4940 | return fixup(self, fixcapitalize); |
| 4941 | } |
| 4942 | |
| 4943 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4944 | PyDoc_STRVAR(capwords__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4945 | "S.capwords() -> unicode\n\ |
| 4946 | \n\ |
| 4947 | 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] | 4948 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4949 | |
| 4950 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 4951 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4952 | { |
| 4953 | PyObject *list; |
| 4954 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4955 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4956 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4957 | /* Split into words */ |
| 4958 | list = split(self, NULL, -1); |
| 4959 | if (!list) |
| 4960 | return NULL; |
| 4961 | |
| 4962 | /* Capitalize each word */ |
| 4963 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 4964 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 4965 | fixcapitalize); |
| 4966 | if (item == NULL) |
| 4967 | goto onError; |
| 4968 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 4969 | PyList_SET_ITEM(list, i, item); |
| 4970 | } |
| 4971 | |
| 4972 | /* Join the words to form a new string */ |
| 4973 | item = PyUnicode_Join(NULL, list); |
| 4974 | |
| 4975 | onError: |
| 4976 | Py_DECREF(list); |
| 4977 | return (PyObject *)item; |
| 4978 | } |
| 4979 | #endif |
| 4980 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4981 | /* Argument converter. Coerces to a single unicode character */ |
| 4982 | |
| 4983 | static int |
| 4984 | convert_uc(PyObject *obj, void *addr) |
| 4985 | { |
| 4986 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 4987 | PyObject *uniobj; |
| 4988 | Py_UNICODE *unistr; |
| 4989 | |
| 4990 | uniobj = PyUnicode_FromObject(obj); |
| 4991 | if (uniobj == NULL) { |
| 4992 | PyErr_SetString(PyExc_TypeError, |
| 4993 | "The fill character cannot be converted to Unicode"); |
| 4994 | return 0; |
| 4995 | } |
| 4996 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 4997 | PyErr_SetString(PyExc_TypeError, |
| 4998 | "The fill character must be exactly one character long"); |
| 4999 | Py_DECREF(uniobj); |
| 5000 | return 0; |
| 5001 | } |
| 5002 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 5003 | *fillcharloc = unistr[0]; |
| 5004 | Py_DECREF(uniobj); |
| 5005 | return 1; |
| 5006 | } |
| 5007 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5008 | PyDoc_STRVAR(center__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5009 | "S.center(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5010 | \n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5011 | Return S centered in a Unicode string of length width. Padding is\n\ |
| 5012 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5013 | |
| 5014 | static PyObject * |
| 5015 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 5016 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5017 | Py_ssize_t marg, left; |
| 5018 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5019 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5020 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 5021 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5022 | return NULL; |
| 5023 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5024 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5025 | Py_INCREF(self); |
| 5026 | return (PyObject*) self; |
| 5027 | } |
| 5028 | |
| 5029 | marg = width - self->length; |
| 5030 | left = marg / 2 + (marg & width & 1); |
| 5031 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5032 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5033 | } |
| 5034 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5035 | #if 0 |
| 5036 | |
| 5037 | /* This code should go into some future Unicode collation support |
| 5038 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 5039 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5040 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5041 | /* speedy UTF-16 code point order comparison */ |
| 5042 | /* gleaned from: */ |
| 5043 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 5044 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 5045 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5046 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5047 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5048 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 5049 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 5050 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5051 | }; |
| 5052 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5053 | static int |
| 5054 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 5055 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5056 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5057 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5058 | Py_UNICODE *s1 = str1->str; |
| 5059 | Py_UNICODE *s2 = str2->str; |
| 5060 | |
| 5061 | len1 = str1->length; |
| 5062 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5063 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5064 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5065 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5066 | |
| 5067 | c1 = *s1++; |
| 5068 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 5069 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5070 | if (c1 > (1<<11) * 26) |
| 5071 | c1 += utf16Fixup[c1>>11]; |
| 5072 | if (c2 > (1<<11) * 26) |
| 5073 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5074 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 5075 | |
| 5076 | if (c1 != c2) |
| 5077 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5078 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5079 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5080 | } |
| 5081 | |
| 5082 | return (len1 < len2) ? -1 : (len1 != len2); |
| 5083 | } |
| 5084 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5085 | #else |
| 5086 | |
| 5087 | static int |
| 5088 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 5089 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5090 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5091 | |
| 5092 | Py_UNICODE *s1 = str1->str; |
| 5093 | Py_UNICODE *s2 = str2->str; |
| 5094 | |
| 5095 | len1 = str1->length; |
| 5096 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5097 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5098 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5099 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5100 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 5101 | c1 = *s1++; |
| 5102 | c2 = *s2++; |
| 5103 | |
| 5104 | if (c1 != c2) |
| 5105 | return (c1 < c2) ? -1 : 1; |
| 5106 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5107 | len1--; len2--; |
| 5108 | } |
| 5109 | |
| 5110 | return (len1 < len2) ? -1 : (len1 != len2); |
| 5111 | } |
| 5112 | |
| 5113 | #endif |
| 5114 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5115 | int PyUnicode_Compare(PyObject *left, |
| 5116 | PyObject *right) |
| 5117 | { |
| 5118 | PyUnicodeObject *u = NULL, *v = NULL; |
| 5119 | int result; |
| 5120 | |
| 5121 | /* Coerce the two arguments */ |
| 5122 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 5123 | if (u == NULL) |
| 5124 | goto onError; |
| 5125 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 5126 | if (v == NULL) |
| 5127 | goto onError; |
| 5128 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5129 | /* Shortcut for empty or interned objects */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5130 | if (v == u) { |
| 5131 | Py_DECREF(u); |
| 5132 | Py_DECREF(v); |
| 5133 | return 0; |
| 5134 | } |
| 5135 | |
| 5136 | result = unicode_compare(u, v); |
| 5137 | |
| 5138 | Py_DECREF(u); |
| 5139 | Py_DECREF(v); |
| 5140 | return result; |
| 5141 | |
| 5142 | onError: |
| 5143 | Py_XDECREF(u); |
| 5144 | Py_XDECREF(v); |
| 5145 | return -1; |
| 5146 | } |
| 5147 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5148 | int PyUnicode_Contains(PyObject *container, |
| 5149 | PyObject *element) |
| 5150 | { |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5151 | PyUnicodeObject *u, *v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5152 | Py_ssize_t size; |
Fredrik Lundh | d5e0dc5 | 2006-05-24 15:11:01 +0000 | [diff] [blame] | 5153 | Py_ssize_t pos; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5154 | |
| 5155 | /* Coerce the two arguments */ |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5156 | v = (PyUnicodeObject *) PyUnicode_FromObject(element); |
| 5157 | if (!v) { |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 5158 | PyErr_SetString(PyExc_TypeError, |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 5159 | "'in <string>' requires string as left operand"); |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5160 | return -1; |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 5161 | } |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5162 | |
| 5163 | u = (PyUnicodeObject *) PyUnicode_FromObject(container); |
| 5164 | if (!u) { |
| 5165 | Py_DECREF(v); |
| 5166 | return -1; |
| 5167 | } |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5168 | |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 5169 | size = PyUnicode_GET_SIZE(v); |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5170 | if (!size) { |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 5171 | pos = 0; |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5172 | goto done; |
| 5173 | } |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 5174 | |
Fredrik Lundh | d5e0dc5 | 2006-05-24 15:11:01 +0000 | [diff] [blame] | 5175 | pos = fastsearch( |
| 5176 | PyUnicode_AS_UNICODE(u), PyUnicode_GET_SIZE(u), |
| 5177 | PyUnicode_AS_UNICODE(v), size, FAST_SEARCH |
| 5178 | ); |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5179 | |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5180 | done: |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5181 | Py_DECREF(u); |
| 5182 | Py_DECREF(v); |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 5183 | return (pos != -1); |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5184 | } |
| 5185 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5186 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 5187 | |
| 5188 | PyObject *PyUnicode_Concat(PyObject *left, |
| 5189 | PyObject *right) |
| 5190 | { |
| 5191 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 5192 | |
| 5193 | /* Coerce the two arguments */ |
| 5194 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 5195 | if (u == NULL) |
| 5196 | goto onError; |
| 5197 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 5198 | if (v == NULL) |
| 5199 | goto onError; |
| 5200 | |
| 5201 | /* Shortcuts */ |
| 5202 | if (v == unicode_empty) { |
| 5203 | Py_DECREF(v); |
| 5204 | return (PyObject *)u; |
| 5205 | } |
| 5206 | if (u == unicode_empty) { |
| 5207 | Py_DECREF(u); |
| 5208 | return (PyObject *)v; |
| 5209 | } |
| 5210 | |
| 5211 | /* Concat the two Unicode strings */ |
| 5212 | w = _PyUnicode_New(u->length + v->length); |
| 5213 | if (w == NULL) |
| 5214 | goto onError; |
| 5215 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 5216 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 5217 | |
| 5218 | Py_DECREF(u); |
| 5219 | Py_DECREF(v); |
| 5220 | return (PyObject *)w; |
| 5221 | |
| 5222 | onError: |
| 5223 | Py_XDECREF(u); |
| 5224 | Py_XDECREF(v); |
| 5225 | return NULL; |
| 5226 | } |
| 5227 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5228 | PyDoc_STRVAR(count__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5229 | "S.count(sub[, start[, end]]) -> int\n\ |
| 5230 | \n\ |
Fredrik Lundh | 763b50f | 2006-05-22 15:35:12 +0000 | [diff] [blame] | 5231 | Return the number of non-overlapping occurrences of substring sub in\n\ |
| 5232 | 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] | 5233 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5234 | |
| 5235 | static PyObject * |
| 5236 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 5237 | { |
| 5238 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5239 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5240 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5241 | PyObject *result; |
| 5242 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5243 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 5244 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5245 | return NULL; |
| 5246 | |
| 5247 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 5248 | (PyObject *)substring); |
| 5249 | if (substring == NULL) |
| 5250 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5251 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5252 | if (start < 0) |
| 5253 | start += self->length; |
| 5254 | if (start < 0) |
| 5255 | start = 0; |
| 5256 | if (end > self->length) |
| 5257 | end = self->length; |
| 5258 | if (end < 0) |
| 5259 | end += self->length; |
| 5260 | if (end < 0) |
| 5261 | end = 0; |
| 5262 | |
Andrew Dalke | b552c4d | 2006-05-25 18:03:25 +0000 | [diff] [blame] | 5263 | result = PyInt_FromSsize_t(count(self, start, end, substring)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5264 | |
| 5265 | Py_DECREF(substring); |
| 5266 | return result; |
| 5267 | } |
| 5268 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5269 | PyDoc_STRVAR(encode__doc__, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5270 | "S.encode([encoding[,errors]]) -> string or unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5271 | \n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5272 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 5273 | 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] | 5274 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5275 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 5276 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 5277 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5278 | |
| 5279 | static PyObject * |
| 5280 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 5281 | { |
| 5282 | char *encoding = NULL; |
| 5283 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5284 | PyObject *v; |
| 5285 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5286 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 5287 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5288 | v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5289 | if (v == NULL) |
| 5290 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5291 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 5292 | PyErr_Format(PyExc_TypeError, |
| 5293 | "encoder did not return a string/unicode object " |
| 5294 | "(type=%.400s)", |
| 5295 | v->ob_type->tp_name); |
| 5296 | Py_DECREF(v); |
| 5297 | return NULL; |
| 5298 | } |
| 5299 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5300 | |
| 5301 | onError: |
| 5302 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5303 | } |
| 5304 | |
| 5305 | PyDoc_STRVAR(decode__doc__, |
| 5306 | "S.decode([encoding[,errors]]) -> string or unicode\n\ |
| 5307 | \n\ |
| 5308 | Decodes S using the codec registered for encoding. encoding defaults\n\ |
| 5309 | to the default encoding. errors may be given to set a different error\n\ |
| 5310 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 5311 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 5312 | as well as any other name registerd with codecs.register_error that is\n\ |
| 5313 | able to handle UnicodeDecodeErrors."); |
| 5314 | |
| 5315 | static PyObject * |
Marc-André Lemburg | 126b44c | 2004-07-10 12:04:20 +0000 | [diff] [blame] | 5316 | unicode_decode(PyUnicodeObject *self, PyObject *args) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5317 | { |
| 5318 | char *encoding = NULL; |
| 5319 | char *errors = NULL; |
| 5320 | PyObject *v; |
| 5321 | |
| 5322 | if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
| 5323 | return NULL; |
| 5324 | v = PyUnicode_AsDecodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5325 | if (v == NULL) |
| 5326 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5327 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 5328 | PyErr_Format(PyExc_TypeError, |
| 5329 | "decoder did not return a string/unicode object " |
| 5330 | "(type=%.400s)", |
| 5331 | v->ob_type->tp_name); |
| 5332 | Py_DECREF(v); |
| 5333 | return NULL; |
| 5334 | } |
| 5335 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5336 | |
| 5337 | onError: |
| 5338 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5339 | } |
| 5340 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5341 | PyDoc_STRVAR(expandtabs__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5342 | "S.expandtabs([tabsize]) -> unicode\n\ |
| 5343 | \n\ |
| 5344 | 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] | 5345 | 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] | 5346 | |
| 5347 | static PyObject* |
| 5348 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 5349 | { |
| 5350 | Py_UNICODE *e; |
| 5351 | Py_UNICODE *p; |
| 5352 | Py_UNICODE *q; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5353 | Py_ssize_t i, j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5354 | PyUnicodeObject *u; |
| 5355 | int tabsize = 8; |
| 5356 | |
| 5357 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 5358 | return NULL; |
| 5359 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5360 | /* First pass: determine size of output string */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5361 | i = j = 0; |
| 5362 | e = self->str + self->length; |
| 5363 | for (p = self->str; p < e; p++) |
| 5364 | if (*p == '\t') { |
| 5365 | if (tabsize > 0) |
| 5366 | j += tabsize - (j % tabsize); |
| 5367 | } |
| 5368 | else { |
| 5369 | j++; |
| 5370 | if (*p == '\n' || *p == '\r') { |
| 5371 | i += j; |
| 5372 | j = 0; |
| 5373 | } |
| 5374 | } |
| 5375 | |
| 5376 | /* Second pass: create output string and fill it */ |
| 5377 | u = _PyUnicode_New(i + j); |
| 5378 | if (!u) |
| 5379 | return NULL; |
| 5380 | |
| 5381 | j = 0; |
| 5382 | q = u->str; |
| 5383 | |
| 5384 | for (p = self->str; p < e; p++) |
| 5385 | if (*p == '\t') { |
| 5386 | if (tabsize > 0) { |
| 5387 | i = tabsize - (j % tabsize); |
| 5388 | j += i; |
| 5389 | while (i--) |
| 5390 | *q++ = ' '; |
| 5391 | } |
| 5392 | } |
| 5393 | else { |
| 5394 | j++; |
| 5395 | *q++ = *p; |
| 5396 | if (*p == '\n' || *p == '\r') |
| 5397 | j = 0; |
| 5398 | } |
| 5399 | |
| 5400 | return (PyObject*) u; |
| 5401 | } |
| 5402 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5403 | PyDoc_STRVAR(find__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5404 | "S.find(sub [,start [,end]]) -> int\n\ |
| 5405 | \n\ |
| 5406 | Return the lowest index in S where substring sub is found,\n\ |
| 5407 | such that sub is contained within s[start,end]. Optional\n\ |
| 5408 | arguments start and end are interpreted as in slice notation.\n\ |
| 5409 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5410 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5411 | |
| 5412 | static PyObject * |
| 5413 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 5414 | { |
| 5415 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5416 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5417 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5418 | PyObject *result; |
| 5419 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5420 | if (!PyArg_ParseTuple(args, "O|O&O&:find", &substring, |
| 5421 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5422 | return NULL; |
| 5423 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 5424 | (PyObject *)substring); |
| 5425 | if (substring == NULL) |
| 5426 | return NULL; |
| 5427 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5428 | result = PyInt_FromSsize_t(findstring(self, substring, start, end, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5429 | |
| 5430 | Py_DECREF(substring); |
| 5431 | return result; |
| 5432 | } |
| 5433 | |
| 5434 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5435 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5436 | { |
| 5437 | if (index < 0 || index >= self->length) { |
| 5438 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 5439 | return NULL; |
| 5440 | } |
| 5441 | |
| 5442 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 5443 | } |
| 5444 | |
| 5445 | static long |
| 5446 | unicode_hash(PyUnicodeObject *self) |
| 5447 | { |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5448 | /* Since Unicode objects compare equal to their ASCII string |
| 5449 | counterparts, they should use the individual character values |
| 5450 | as basis for their hash value. This is needed to assure that |
| 5451 | strings and Unicode objects behave in the same way as |
| 5452 | dictionary keys. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5453 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5454 | register Py_ssize_t len; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5455 | register Py_UNICODE *p; |
| 5456 | register long x; |
| 5457 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5458 | if (self->hash != -1) |
| 5459 | return self->hash; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5460 | len = PyUnicode_GET_SIZE(self); |
| 5461 | p = PyUnicode_AS_UNICODE(self); |
| 5462 | x = *p << 7; |
| 5463 | while (--len >= 0) |
| 5464 | x = (1000003*x) ^ *p++; |
| 5465 | x ^= PyUnicode_GET_SIZE(self); |
| 5466 | if (x == -1) |
| 5467 | x = -2; |
| 5468 | self->hash = x; |
| 5469 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5470 | } |
| 5471 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5472 | PyDoc_STRVAR(index__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5473 | "S.index(sub [,start [,end]]) -> int\n\ |
| 5474 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5475 | 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] | 5476 | |
| 5477 | static PyObject * |
| 5478 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 5479 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5480 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5481 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5482 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5483 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5484 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5485 | if (!PyArg_ParseTuple(args, "O|O&O&:index", &substring, |
| 5486 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5487 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5488 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5489 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 5490 | (PyObject *)substring); |
| 5491 | if (substring == NULL) |
| 5492 | return NULL; |
| 5493 | |
| 5494 | result = findstring(self, substring, start, end, 1); |
| 5495 | |
| 5496 | Py_DECREF(substring); |
| 5497 | if (result < 0) { |
| 5498 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 5499 | return NULL; |
| 5500 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5501 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5502 | } |
| 5503 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5504 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5505 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5506 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5507 | 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] | 5508 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5509 | |
| 5510 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5511 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5512 | { |
| 5513 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5514 | register const Py_UNICODE *e; |
| 5515 | int cased; |
| 5516 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5517 | /* Shortcut for single character strings */ |
| 5518 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5519 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5520 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5521 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5522 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5523 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5524 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5525 | e = p + PyUnicode_GET_SIZE(self); |
| 5526 | cased = 0; |
| 5527 | for (; p < e; p++) { |
| 5528 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5529 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5530 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5531 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5532 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 5533 | cased = 1; |
| 5534 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5535 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5536 | } |
| 5537 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5538 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5539 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5540 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5541 | 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] | 5542 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5543 | |
| 5544 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5545 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5546 | { |
| 5547 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5548 | register const Py_UNICODE *e; |
| 5549 | int cased; |
| 5550 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5551 | /* Shortcut for single character strings */ |
| 5552 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5553 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5554 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5555 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5556 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5557 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5558 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5559 | e = p + PyUnicode_GET_SIZE(self); |
| 5560 | cased = 0; |
| 5561 | for (; p < e; p++) { |
| 5562 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5563 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5564 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5565 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5566 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 5567 | cased = 1; |
| 5568 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5569 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5570 | } |
| 5571 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5572 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5573 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5574 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5575 | Return True if S is a titlecased string and there is at least one\n\ |
| 5576 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 5577 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 5578 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5579 | |
| 5580 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5581 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5582 | { |
| 5583 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5584 | register const Py_UNICODE *e; |
| 5585 | int cased, previous_is_cased; |
| 5586 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5587 | /* Shortcut for single character strings */ |
| 5588 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5589 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 5590 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5591 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5592 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5593 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5594 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5595 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5596 | e = p + PyUnicode_GET_SIZE(self); |
| 5597 | cased = 0; |
| 5598 | previous_is_cased = 0; |
| 5599 | for (; p < e; p++) { |
| 5600 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5601 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5602 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 5603 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5604 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5605 | previous_is_cased = 1; |
| 5606 | cased = 1; |
| 5607 | } |
| 5608 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 5609 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5610 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5611 | previous_is_cased = 1; |
| 5612 | cased = 1; |
| 5613 | } |
| 5614 | else |
| 5615 | previous_is_cased = 0; |
| 5616 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5617 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5618 | } |
| 5619 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5620 | PyDoc_STRVAR(isspace__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5621 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5622 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5623 | Return True if all characters in S are whitespace\n\ |
| 5624 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5625 | |
| 5626 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5627 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5628 | { |
| 5629 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5630 | register const Py_UNICODE *e; |
| 5631 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5632 | /* Shortcut for single character strings */ |
| 5633 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5634 | Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5635 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5636 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5637 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5638 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5639 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5640 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5641 | e = p + PyUnicode_GET_SIZE(self); |
| 5642 | for (; p < e; p++) { |
| 5643 | if (!Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5644 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5645 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5646 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5647 | } |
| 5648 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5649 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5650 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5651 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5652 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5653 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5654 | |
| 5655 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5656 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5657 | { |
| 5658 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5659 | register const Py_UNICODE *e; |
| 5660 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5661 | /* Shortcut for single character strings */ |
| 5662 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5663 | Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5664 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5665 | |
| 5666 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5667 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5668 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5669 | |
| 5670 | e = p + PyUnicode_GET_SIZE(self); |
| 5671 | for (; p < e; p++) { |
| 5672 | if (!Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5673 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5674 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5675 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5676 | } |
| 5677 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5678 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5679 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5680 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5681 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5682 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5683 | |
| 5684 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5685 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5686 | { |
| 5687 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5688 | register const Py_UNICODE *e; |
| 5689 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5690 | /* Shortcut for single character strings */ |
| 5691 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5692 | Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5693 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5694 | |
| 5695 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5696 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5697 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5698 | |
| 5699 | e = p + PyUnicode_GET_SIZE(self); |
| 5700 | for (; p < e; p++) { |
| 5701 | if (!Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5702 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5703 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5704 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5705 | } |
| 5706 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5707 | PyDoc_STRVAR(isdecimal__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5708 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5709 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5710 | 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] | 5711 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5712 | |
| 5713 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5714 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5715 | { |
| 5716 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5717 | register const Py_UNICODE *e; |
| 5718 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5719 | /* Shortcut for single character strings */ |
| 5720 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5721 | Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5722 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5723 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5724 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5725 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5726 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5727 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5728 | e = p + PyUnicode_GET_SIZE(self); |
| 5729 | for (; p < e; p++) { |
| 5730 | if (!Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5731 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5732 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5733 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5734 | } |
| 5735 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5736 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5737 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5738 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5739 | Return True if all characters in S are digits\n\ |
| 5740 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5741 | |
| 5742 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5743 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5744 | { |
| 5745 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5746 | register const Py_UNICODE *e; |
| 5747 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5748 | /* Shortcut for single character strings */ |
| 5749 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5750 | Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5751 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5752 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5753 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5754 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5755 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5756 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5757 | e = p + PyUnicode_GET_SIZE(self); |
| 5758 | for (; p < e; p++) { |
| 5759 | if (!Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5760 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5761 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5762 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5763 | } |
| 5764 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5765 | PyDoc_STRVAR(isnumeric__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5766 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5767 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5768 | 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] | 5769 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5770 | |
| 5771 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5772 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5773 | { |
| 5774 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5775 | register const Py_UNICODE *e; |
| 5776 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5777 | /* Shortcut for single character strings */ |
| 5778 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5779 | Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5780 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5781 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5782 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5783 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5784 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5785 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5786 | e = p + PyUnicode_GET_SIZE(self); |
| 5787 | for (; p < e; p++) { |
| 5788 | if (!Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5789 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5790 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5791 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5792 | } |
| 5793 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5794 | PyDoc_STRVAR(join__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5795 | "S.join(sequence) -> unicode\n\ |
| 5796 | \n\ |
| 5797 | 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] | 5798 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5799 | |
| 5800 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5801 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5802 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5803 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5804 | } |
| 5805 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5806 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5807 | unicode_length(PyUnicodeObject *self) |
| 5808 | { |
| 5809 | return self->length; |
| 5810 | } |
| 5811 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5812 | PyDoc_STRVAR(ljust__doc__, |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 5813 | "S.ljust(width[, fillchar]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5814 | \n\ |
| 5815 | 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] | 5816 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5817 | |
| 5818 | static PyObject * |
| 5819 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 5820 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5821 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5822 | Py_UNICODE fillchar = ' '; |
| 5823 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5824 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5825 | return NULL; |
| 5826 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5827 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5828 | Py_INCREF(self); |
| 5829 | return (PyObject*) self; |
| 5830 | } |
| 5831 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5832 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5833 | } |
| 5834 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5835 | PyDoc_STRVAR(lower__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5836 | "S.lower() -> unicode\n\ |
| 5837 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5838 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5839 | |
| 5840 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5841 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5842 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5843 | return fixup(self, fixlower); |
| 5844 | } |
| 5845 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5846 | #define LEFTSTRIP 0 |
| 5847 | #define RIGHTSTRIP 1 |
| 5848 | #define BOTHSTRIP 2 |
| 5849 | |
| 5850 | /* Arrays indexed by above */ |
| 5851 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 5852 | |
| 5853 | #define STRIPNAME(i) (stripformat[i]+3) |
| 5854 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5855 | /* externally visible for str.strip(unicode) */ |
| 5856 | PyObject * |
| 5857 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 5858 | { |
| 5859 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5860 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5861 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5862 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 5863 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5864 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5865 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
| 5866 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5867 | i = 0; |
| 5868 | if (striptype != RIGHTSTRIP) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5869 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 5870 | i++; |
| 5871 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5872 | } |
| 5873 | |
| 5874 | j = len; |
| 5875 | if (striptype != LEFTSTRIP) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5876 | do { |
| 5877 | j--; |
| 5878 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 5879 | j++; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5880 | } |
| 5881 | |
| 5882 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5883 | Py_INCREF(self); |
| 5884 | return (PyObject*)self; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5885 | } |
| 5886 | else |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5887 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5888 | } |
| 5889 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5890 | |
| 5891 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5892 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5893 | { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5894 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5895 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5896 | |
| 5897 | i = 0; |
| 5898 | if (striptype != RIGHTSTRIP) { |
| 5899 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 5900 | i++; |
| 5901 | } |
| 5902 | } |
| 5903 | |
| 5904 | j = len; |
| 5905 | if (striptype != LEFTSTRIP) { |
| 5906 | do { |
| 5907 | j--; |
| 5908 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 5909 | j++; |
| 5910 | } |
| 5911 | |
| 5912 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 5913 | Py_INCREF(self); |
| 5914 | return (PyObject*)self; |
| 5915 | } |
| 5916 | else |
| 5917 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5918 | } |
| 5919 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5920 | |
| 5921 | static PyObject * |
| 5922 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 5923 | { |
| 5924 | PyObject *sep = NULL; |
| 5925 | |
| 5926 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 5927 | return NULL; |
| 5928 | |
| 5929 | if (sep != NULL && sep != Py_None) { |
| 5930 | if (PyUnicode_Check(sep)) |
| 5931 | return _PyUnicode_XStrip(self, striptype, sep); |
| 5932 | else if (PyString_Check(sep)) { |
| 5933 | PyObject *res; |
| 5934 | sep = PyUnicode_FromObject(sep); |
| 5935 | if (sep==NULL) |
| 5936 | return NULL; |
| 5937 | res = _PyUnicode_XStrip(self, striptype, sep); |
| 5938 | Py_DECREF(sep); |
| 5939 | return res; |
| 5940 | } |
| 5941 | else { |
| 5942 | PyErr_Format(PyExc_TypeError, |
| 5943 | "%s arg must be None, unicode or str", |
| 5944 | STRIPNAME(striptype)); |
| 5945 | return NULL; |
| 5946 | } |
| 5947 | } |
| 5948 | |
| 5949 | return do_strip(self, striptype); |
| 5950 | } |
| 5951 | |
| 5952 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5953 | PyDoc_STRVAR(strip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5954 | "S.strip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5955 | \n\ |
| 5956 | Return a copy of the string S with leading and trailing\n\ |
| 5957 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5958 | If chars is given and not None, remove characters in chars instead.\n\ |
| 5959 | 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] | 5960 | |
| 5961 | static PyObject * |
| 5962 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 5963 | { |
| 5964 | if (PyTuple_GET_SIZE(args) == 0) |
| 5965 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 5966 | else |
| 5967 | return do_argstrip(self, BOTHSTRIP, args); |
| 5968 | } |
| 5969 | |
| 5970 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5971 | PyDoc_STRVAR(lstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5972 | "S.lstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5973 | \n\ |
| 5974 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5975 | If chars is given and not None, remove characters in chars instead.\n\ |
| 5976 | 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] | 5977 | |
| 5978 | static PyObject * |
| 5979 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 5980 | { |
| 5981 | if (PyTuple_GET_SIZE(args) == 0) |
| 5982 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 5983 | else |
| 5984 | return do_argstrip(self, LEFTSTRIP, args); |
| 5985 | } |
| 5986 | |
| 5987 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5988 | PyDoc_STRVAR(rstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5989 | "S.rstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5990 | \n\ |
| 5991 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5992 | If chars is given and not None, remove characters in chars instead.\n\ |
| 5993 | 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] | 5994 | |
| 5995 | static PyObject * |
| 5996 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 5997 | { |
| 5998 | if (PyTuple_GET_SIZE(args) == 0) |
| 5999 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 6000 | else |
| 6001 | return do_argstrip(self, RIGHTSTRIP, args); |
| 6002 | } |
| 6003 | |
| 6004 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6005 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6006 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6007 | { |
| 6008 | PyUnicodeObject *u; |
| 6009 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6010 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 6011 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6012 | |
| 6013 | if (len < 0) |
| 6014 | len = 0; |
| 6015 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6016 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6017 | /* no repeat, return original string */ |
| 6018 | Py_INCREF(str); |
| 6019 | return (PyObject*) str; |
| 6020 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 6021 | |
| 6022 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 6023 | * needed doesn't overflow size_t |
| 6024 | */ |
| 6025 | nchars = len * str->length; |
| 6026 | if (len && nchars / len != str->length) { |
| 6027 | PyErr_SetString(PyExc_OverflowError, |
| 6028 | "repeated string is too long"); |
| 6029 | return NULL; |
| 6030 | } |
| 6031 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 6032 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 6033 | PyErr_SetString(PyExc_OverflowError, |
| 6034 | "repeated string is too long"); |
| 6035 | return NULL; |
| 6036 | } |
| 6037 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6038 | if (!u) |
| 6039 | return NULL; |
| 6040 | |
| 6041 | p = u->str; |
| 6042 | |
Fredrik Lundh | f1d60a5 | 2006-05-22 16:29:30 +0000 | [diff] [blame] | 6043 | if (str->length == 1 && len > 0) { |
| 6044 | Py_UNICODE_FILL(p, str->str[0], len); |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 6045 | } else { |
Tim Peters | 1bacc64 | 2006-05-23 05:47:16 +0000 | [diff] [blame] | 6046 | Py_ssize_t done = 0; /* number of characters copied this far */ |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 6047 | if (done < nchars) { |
Fredrik Lundh | f1d60a5 | 2006-05-22 16:29:30 +0000 | [diff] [blame] | 6048 | Py_UNICODE_COPY(p, str->str, str->length); |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 6049 | done = str->length; |
| 6050 | } |
| 6051 | while (done < nchars) { |
| 6052 | int n = (done <= nchars-done) ? done : nchars-done; |
| 6053 | Py_UNICODE_COPY(p+done, p, n); |
| 6054 | done += n; |
| 6055 | } |
| 6056 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6057 | |
| 6058 | return (PyObject*) u; |
| 6059 | } |
| 6060 | |
| 6061 | PyObject *PyUnicode_Replace(PyObject *obj, |
| 6062 | PyObject *subobj, |
| 6063 | PyObject *replobj, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6064 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6065 | { |
| 6066 | PyObject *self; |
| 6067 | PyObject *str1; |
| 6068 | PyObject *str2; |
| 6069 | PyObject *result; |
| 6070 | |
| 6071 | self = PyUnicode_FromObject(obj); |
| 6072 | if (self == NULL) |
| 6073 | return NULL; |
| 6074 | str1 = PyUnicode_FromObject(subobj); |
| 6075 | if (str1 == NULL) { |
| 6076 | Py_DECREF(self); |
| 6077 | return NULL; |
| 6078 | } |
| 6079 | str2 = PyUnicode_FromObject(replobj); |
| 6080 | if (str2 == NULL) { |
| 6081 | Py_DECREF(self); |
| 6082 | Py_DECREF(str1); |
| 6083 | return NULL; |
| 6084 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6085 | result = replace((PyUnicodeObject *)self, |
| 6086 | (PyUnicodeObject *)str1, |
| 6087 | (PyUnicodeObject *)str2, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6088 | maxcount); |
| 6089 | Py_DECREF(self); |
| 6090 | Py_DECREF(str1); |
| 6091 | Py_DECREF(str2); |
| 6092 | return result; |
| 6093 | } |
| 6094 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6095 | PyDoc_STRVAR(replace__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6096 | "S.replace (old, new[, maxsplit]) -> unicode\n\ |
| 6097 | \n\ |
| 6098 | Return a copy of S with all occurrences of substring\n\ |
| 6099 | 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] | 6100 | given, only the first maxsplit occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6101 | |
| 6102 | static PyObject* |
| 6103 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 6104 | { |
| 6105 | PyUnicodeObject *str1; |
| 6106 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6107 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6108 | PyObject *result; |
| 6109 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6110 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6111 | return NULL; |
| 6112 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 6113 | if (str1 == NULL) |
| 6114 | return NULL; |
| 6115 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 6116 | if (str2 == NULL) { |
| 6117 | Py_DECREF(str1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6118 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 6119 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6120 | |
| 6121 | result = replace(self, str1, str2, maxcount); |
| 6122 | |
| 6123 | Py_DECREF(str1); |
| 6124 | Py_DECREF(str2); |
| 6125 | return result; |
| 6126 | } |
| 6127 | |
| 6128 | static |
| 6129 | PyObject *unicode_repr(PyObject *unicode) |
| 6130 | { |
| 6131 | return unicodeescape_string(PyUnicode_AS_UNICODE(unicode), |
| 6132 | PyUnicode_GET_SIZE(unicode), |
| 6133 | 1); |
| 6134 | } |
| 6135 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6136 | PyDoc_STRVAR(rfind__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6137 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 6138 | \n\ |
| 6139 | Return the highest index in S where substring sub is found,\n\ |
| 6140 | such that sub is contained within s[start,end]. Optional\n\ |
| 6141 | arguments start and end are interpreted as in slice notation.\n\ |
| 6142 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6143 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6144 | |
| 6145 | static PyObject * |
| 6146 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 6147 | { |
| 6148 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6149 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6150 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6151 | PyObject *result; |
| 6152 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6153 | if (!PyArg_ParseTuple(args, "O|O&O&:rfind", &substring, |
| 6154 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6155 | return NULL; |
| 6156 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6157 | (PyObject *)substring); |
| 6158 | if (substring == NULL) |
| 6159 | return NULL; |
| 6160 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6161 | result = PyInt_FromSsize_t(findstring(self, substring, start, end, -1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6162 | |
| 6163 | Py_DECREF(substring); |
| 6164 | return result; |
| 6165 | } |
| 6166 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6167 | PyDoc_STRVAR(rindex__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6168 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 6169 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6170 | 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] | 6171 | |
| 6172 | static PyObject * |
| 6173 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 6174 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6175 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6176 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6177 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6178 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6179 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6180 | if (!PyArg_ParseTuple(args, "O|O&O&:rindex", &substring, |
| 6181 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6182 | return NULL; |
| 6183 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6184 | (PyObject *)substring); |
| 6185 | if (substring == NULL) |
| 6186 | return NULL; |
| 6187 | |
| 6188 | result = findstring(self, substring, start, end, -1); |
| 6189 | |
| 6190 | Py_DECREF(substring); |
| 6191 | if (result < 0) { |
| 6192 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 6193 | return NULL; |
| 6194 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6195 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6196 | } |
| 6197 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6198 | PyDoc_STRVAR(rjust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6199 | "S.rjust(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6200 | \n\ |
| 6201 | 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] | 6202 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6203 | |
| 6204 | static PyObject * |
| 6205 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 6206 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6207 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6208 | Py_UNICODE fillchar = ' '; |
| 6209 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6210 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6211 | return NULL; |
| 6212 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6213 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6214 | Py_INCREF(self); |
| 6215 | return (PyObject*) self; |
| 6216 | } |
| 6217 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6218 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6219 | } |
| 6220 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6221 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6222 | 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] | 6223 | { |
| 6224 | /* standard clamping */ |
| 6225 | if (start < 0) |
| 6226 | start = 0; |
| 6227 | if (end < 0) |
| 6228 | end = 0; |
| 6229 | if (end > self->length) |
| 6230 | end = self->length; |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6231 | if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6232 | /* full slice, return original string */ |
| 6233 | Py_INCREF(self); |
| 6234 | return (PyObject*) self; |
| 6235 | } |
| 6236 | if (start > end) |
| 6237 | start = end; |
| 6238 | /* copy slice */ |
| 6239 | return (PyObject*) PyUnicode_FromUnicode(self->str + start, |
| 6240 | end - start); |
| 6241 | } |
| 6242 | |
| 6243 | PyObject *PyUnicode_Split(PyObject *s, |
| 6244 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6245 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6246 | { |
| 6247 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6248 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6249 | s = PyUnicode_FromObject(s); |
| 6250 | if (s == NULL) |
| 6251 | return NULL; |
| 6252 | if (sep != NULL) { |
| 6253 | sep = PyUnicode_FromObject(sep); |
| 6254 | if (sep == NULL) { |
| 6255 | Py_DECREF(s); |
| 6256 | return NULL; |
| 6257 | } |
| 6258 | } |
| 6259 | |
| 6260 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 6261 | |
| 6262 | Py_DECREF(s); |
| 6263 | Py_XDECREF(sep); |
| 6264 | return result; |
| 6265 | } |
| 6266 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6267 | PyDoc_STRVAR(split__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6268 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 6269 | \n\ |
| 6270 | Return a list of the words in S, using sep as the\n\ |
| 6271 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Thomas Heller | ca0d2cb | 2004-09-15 11:41:32 +0000 | [diff] [blame] | 6272 | 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] | 6273 | any whitespace string is a separator."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6274 | |
| 6275 | static PyObject* |
| 6276 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 6277 | { |
| 6278 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6279 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6280 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6281 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6282 | return NULL; |
| 6283 | |
| 6284 | if (substring == Py_None) |
| 6285 | return split(self, NULL, maxcount); |
| 6286 | else if (PyUnicode_Check(substring)) |
| 6287 | return split(self, (PyUnicodeObject *)substring, maxcount); |
| 6288 | else |
| 6289 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
| 6290 | } |
| 6291 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6292 | PyObject * |
| 6293 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 6294 | { |
| 6295 | PyObject* str_obj; |
| 6296 | PyObject* sep_obj; |
| 6297 | Py_UNICODE *str, *sep; |
| 6298 | Py_ssize_t len, sep_len, pos; |
| 6299 | PyObject* out; |
| 6300 | |
| 6301 | str_obj = PyUnicode_FromObject(str_in); |
| 6302 | if (!str_obj) |
| 6303 | return NULL; |
| 6304 | sep_obj = PyUnicode_FromObject(sep_in); |
| 6305 | if (!sep_obj) |
| 6306 | goto error; |
| 6307 | |
| 6308 | str = PyUnicode_AS_UNICODE(str_obj); |
| 6309 | len = PyUnicode_GET_SIZE(str_obj); |
| 6310 | |
| 6311 | sep = PyUnicode_AS_UNICODE(sep_obj); |
| 6312 | sep_len = PyUnicode_GET_SIZE(sep_obj); |
| 6313 | |
| 6314 | if (sep_len == 0) { |
| 6315 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 6316 | goto error; |
| 6317 | } |
| 6318 | |
| 6319 | out = PyTuple_New(3); |
| 6320 | if (!out) |
| 6321 | goto error; |
| 6322 | |
| 6323 | pos = fastsearch(str, len, sep, sep_len, FAST_SEARCH); |
| 6324 | if (pos < 0) { |
| 6325 | Py_INCREF(str_obj); |
| 6326 | PyTuple_SET_ITEM(out, 0, (PyObject*) str_obj); |
| 6327 | Py_INCREF(unicode_empty); |
| 6328 | PyTuple_SET_ITEM(out, 1, (PyObject*) unicode_empty); |
| 6329 | Py_INCREF(unicode_empty); |
| 6330 | PyTuple_SET_ITEM(out, 2, (PyObject*) unicode_empty); |
| 6331 | } else { |
| 6332 | PyObject* obj; |
| 6333 | PyTuple_SET_ITEM(out, 0, PyUnicode_FromUnicode(str, pos)); |
| 6334 | Py_INCREF(sep_obj); |
| 6335 | PyTuple_SET_ITEM(out, 1, sep_obj); |
| 6336 | obj = PyUnicode_FromUnicode(str + sep_len + pos, len - sep_len - pos); |
| 6337 | PyTuple_SET_ITEM(out, 2, obj); |
| 6338 | if (PyErr_Occurred()) { |
| 6339 | Py_DECREF(out); |
| 6340 | goto error; |
| 6341 | } |
| 6342 | } |
| 6343 | |
| 6344 | return out; |
| 6345 | |
| 6346 | error: |
| 6347 | Py_XDECREF(sep_obj); |
| 6348 | Py_DECREF(str_obj); |
| 6349 | return NULL; |
| 6350 | } |
| 6351 | |
| 6352 | PyDoc_STRVAR(partition__doc__, |
| 6353 | "S.partition(sep) -> (head, sep, tail)\n\ |
| 6354 | \n\ |
| 6355 | Searches for the separator sep in S, and returns the part before it,\n\ |
| 6356 | the separator itself, and the part after it. If the separator is not\n\ |
| 6357 | found, returns S and two empty strings."); |
| 6358 | |
| 6359 | static PyObject* |
Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 6360 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6361 | { |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6362 | return PyUnicode_Partition((PyObject *)self, separator); |
| 6363 | } |
| 6364 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6365 | PyObject *PyUnicode_RSplit(PyObject *s, |
| 6366 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6367 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6368 | { |
| 6369 | PyObject *result; |
| 6370 | |
| 6371 | s = PyUnicode_FromObject(s); |
| 6372 | if (s == NULL) |
| 6373 | return NULL; |
| 6374 | if (sep != NULL) { |
| 6375 | sep = PyUnicode_FromObject(sep); |
| 6376 | if (sep == NULL) { |
| 6377 | Py_DECREF(s); |
| 6378 | return NULL; |
| 6379 | } |
| 6380 | } |
| 6381 | |
| 6382 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 6383 | |
| 6384 | Py_DECREF(s); |
| 6385 | Py_XDECREF(sep); |
| 6386 | return result; |
| 6387 | } |
| 6388 | |
| 6389 | PyDoc_STRVAR(rsplit__doc__, |
| 6390 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
| 6391 | \n\ |
| 6392 | Return a list of the words in S, using sep as the\n\ |
| 6393 | delimiter string, starting at the end of the string and\n\ |
| 6394 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 6395 | splits are done. If sep is not specified, any whitespace string\n\ |
| 6396 | is a separator."); |
| 6397 | |
| 6398 | static PyObject* |
| 6399 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 6400 | { |
| 6401 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6402 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6403 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6404 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6405 | return NULL; |
| 6406 | |
| 6407 | if (substring == Py_None) |
| 6408 | return rsplit(self, NULL, maxcount); |
| 6409 | else if (PyUnicode_Check(substring)) |
| 6410 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
| 6411 | else |
| 6412 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
| 6413 | } |
| 6414 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6415 | PyDoc_STRVAR(splitlines__doc__, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6416 | "S.splitlines([keepends]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6417 | \n\ |
| 6418 | 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] | 6419 | 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] | 6420 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6421 | |
| 6422 | static PyObject* |
| 6423 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 6424 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6425 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6426 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6427 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6428 | return NULL; |
| 6429 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6430 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6431 | } |
| 6432 | |
| 6433 | static |
| 6434 | PyObject *unicode_str(PyUnicodeObject *self) |
| 6435 | { |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6436 | return PyUnicode_AsEncodedString((PyObject *)self, NULL, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6437 | } |
| 6438 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6439 | PyDoc_STRVAR(swapcase__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6440 | "S.swapcase() -> unicode\n\ |
| 6441 | \n\ |
| 6442 | 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] | 6443 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6444 | |
| 6445 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6446 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6447 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6448 | return fixup(self, fixswapcase); |
| 6449 | } |
| 6450 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6451 | PyDoc_STRVAR(translate__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6452 | "S.translate(table) -> unicode\n\ |
| 6453 | \n\ |
| 6454 | Return a copy of the string S, where all characters have been mapped\n\ |
| 6455 | 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] | 6456 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ |
| 6457 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 6458 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6459 | |
| 6460 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6461 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6462 | { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6463 | return PyUnicode_TranslateCharmap(self->str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6464 | self->length, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6465 | table, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6466 | "ignore"); |
| 6467 | } |
| 6468 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6469 | PyDoc_STRVAR(upper__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6470 | "S.upper() -> unicode\n\ |
| 6471 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6472 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6473 | |
| 6474 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6475 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6476 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6477 | return fixup(self, fixupper); |
| 6478 | } |
| 6479 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6480 | PyDoc_STRVAR(zfill__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6481 | "S.zfill(width) -> unicode\n\ |
| 6482 | \n\ |
| 6483 | 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] | 6484 | of the specified width. The string x is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6485 | |
| 6486 | static PyObject * |
| 6487 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 6488 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6489 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6490 | PyUnicodeObject *u; |
| 6491 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6492 | Py_ssize_t width; |
| 6493 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6494 | return NULL; |
| 6495 | |
| 6496 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 6497 | if (PyUnicode_CheckExact(self)) { |
| 6498 | Py_INCREF(self); |
| 6499 | return (PyObject*) self; |
| 6500 | } |
| 6501 | else |
| 6502 | return PyUnicode_FromUnicode( |
| 6503 | PyUnicode_AS_UNICODE(self), |
| 6504 | PyUnicode_GET_SIZE(self) |
| 6505 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6506 | } |
| 6507 | |
| 6508 | fill = width - self->length; |
| 6509 | |
| 6510 | u = pad(self, fill, 0, '0'); |
| 6511 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 6512 | if (u == NULL) |
| 6513 | return NULL; |
| 6514 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6515 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 6516 | /* move sign to beginning of string */ |
| 6517 | u->str[0] = u->str[fill]; |
| 6518 | u->str[fill] = '0'; |
| 6519 | } |
| 6520 | |
| 6521 | return (PyObject*) u; |
| 6522 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6523 | |
| 6524 | #if 0 |
| 6525 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6526 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6527 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6528 | return PyInt_FromLong(unicode_freelist_size); |
| 6529 | } |
| 6530 | #endif |
| 6531 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6532 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6533 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6534 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 6535 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 6536 | With optional start, test S beginning at that position.\n\ |
| 6537 | With optional end, stop comparing S at that position."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6538 | |
| 6539 | static PyObject * |
| 6540 | unicode_startswith(PyUnicodeObject *self, |
| 6541 | PyObject *args) |
| 6542 | { |
| 6543 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6544 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6545 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6546 | PyObject *result; |
| 6547 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6548 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &substring, |
| 6549 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6550 | return NULL; |
| 6551 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6552 | (PyObject *)substring); |
| 6553 | if (substring == NULL) |
| 6554 | return NULL; |
| 6555 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6556 | result = PyBool_FromLong(tailmatch(self, substring, start, end, -1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6557 | |
| 6558 | Py_DECREF(substring); |
| 6559 | return result; |
| 6560 | } |
| 6561 | |
| 6562 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6563 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6564 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6565 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 6566 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 6567 | With optional start, test S beginning at that position.\n\ |
| 6568 | With optional end, stop comparing S at that position."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6569 | |
| 6570 | static PyObject * |
| 6571 | unicode_endswith(PyUnicodeObject *self, |
| 6572 | PyObject *args) |
| 6573 | { |
| 6574 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6575 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6576 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6577 | PyObject *result; |
| 6578 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6579 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &substring, |
| 6580 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6581 | return NULL; |
| 6582 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6583 | (PyObject *)substring); |
| 6584 | if (substring == NULL) |
| 6585 | return NULL; |
| 6586 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6587 | result = PyBool_FromLong(tailmatch(self, substring, start, end, +1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6588 | |
| 6589 | Py_DECREF(substring); |
| 6590 | return result; |
| 6591 | } |
| 6592 | |
| 6593 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 6594 | |
| 6595 | static PyObject * |
| 6596 | unicode_getnewargs(PyUnicodeObject *v) |
| 6597 | { |
| 6598 | return Py_BuildValue("(u#)", v->str, v->length); |
| 6599 | } |
| 6600 | |
| 6601 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6602 | static PyMethodDef unicode_methods[] = { |
| 6603 | |
| 6604 | /* Order is according to common usage: often used methods should |
| 6605 | appear first, since lookup is done sequentially. */ |
| 6606 | |
Georg Brandl | ecdc0a9 | 2006-03-30 12:19:07 +0000 | [diff] [blame] | 6607 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6608 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 6609 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6610 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6611 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 6612 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 6613 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 6614 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 6615 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 6616 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 6617 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 6618 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6619 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 6620 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 6621 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6622 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6623 | {"decode", (PyCFunction) unicode_decode, METH_VARARGS, decode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6624 | /* {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */ |
| 6625 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 6626 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 6627 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6628 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6629 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6630 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6631 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 6632 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 6633 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 6634 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 6635 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 6636 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 6637 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 6638 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 6639 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 6640 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 6641 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 6642 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 6643 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 6644 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6645 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 6646 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6647 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6648 | #endif |
| 6649 | |
| 6650 | #if 0 |
| 6651 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6652 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6653 | #endif |
| 6654 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 6655 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6656 | {NULL, NULL} |
| 6657 | }; |
| 6658 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 6659 | static PyObject * |
| 6660 | unicode_mod(PyObject *v, PyObject *w) |
| 6661 | { |
| 6662 | if (!PyUnicode_Check(v)) { |
| 6663 | Py_INCREF(Py_NotImplemented); |
| 6664 | return Py_NotImplemented; |
| 6665 | } |
| 6666 | return PyUnicode_Format(v, w); |
| 6667 | } |
| 6668 | |
| 6669 | static PyNumberMethods unicode_as_number = { |
| 6670 | 0, /*nb_add*/ |
| 6671 | 0, /*nb_subtract*/ |
| 6672 | 0, /*nb_multiply*/ |
| 6673 | 0, /*nb_divide*/ |
| 6674 | unicode_mod, /*nb_remainder*/ |
| 6675 | }; |
| 6676 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6677 | static PySequenceMethods unicode_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6678 | (lenfunc) unicode_length, /* sq_length */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 6679 | PyUnicode_Concat, /* sq_concat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6680 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 6681 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 6682 | (ssizessizeargfunc) unicode_slice, /* sq_slice */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6683 | 0, /* sq_ass_item */ |
| 6684 | 0, /* sq_ass_slice */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 6685 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6686 | }; |
| 6687 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 6688 | #define HASINDEX(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_HAVE_INDEX) |
| 6689 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6690 | static PyObject* |
| 6691 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 6692 | { |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 6693 | PyNumberMethods *nb = item->ob_type->tp_as_number; |
| 6694 | if (nb != NULL && HASINDEX(item) && nb->nb_index != NULL) { |
| 6695 | Py_ssize_t i = nb->nb_index(item); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6696 | if (i == -1 && PyErr_Occurred()) |
| 6697 | return NULL; |
| 6698 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6699 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6700 | return unicode_getitem(self, i); |
| 6701 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6702 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6703 | Py_UNICODE* source_buf; |
| 6704 | Py_UNICODE* result_buf; |
| 6705 | PyObject* result; |
| 6706 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6707 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6708 | &start, &stop, &step, &slicelength) < 0) { |
| 6709 | return NULL; |
| 6710 | } |
| 6711 | |
| 6712 | if (slicelength <= 0) { |
| 6713 | return PyUnicode_FromUnicode(NULL, 0); |
| 6714 | } else { |
| 6715 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 6716 | result_buf = (Py_UNICODE *)PyMem_MALLOC(slicelength* |
| 6717 | sizeof(Py_UNICODE)); |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6718 | |
| 6719 | if (result_buf == NULL) |
| 6720 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6721 | |
| 6722 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 6723 | result_buf[i] = source_buf[cur]; |
| 6724 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6725 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6726 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
| 6727 | PyMem_FREE(result_buf); |
| 6728 | return result; |
| 6729 | } |
| 6730 | } else { |
| 6731 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 6732 | return NULL; |
| 6733 | } |
| 6734 | } |
| 6735 | |
| 6736 | static PyMappingMethods unicode_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6737 | (lenfunc)unicode_length, /* mp_length */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6738 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 6739 | (objobjargproc)0, /* mp_ass_subscript */ |
| 6740 | }; |
| 6741 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6742 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6743 | unicode_buffer_getreadbuf(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6744 | Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6745 | const void **ptr) |
| 6746 | { |
| 6747 | if (index != 0) { |
| 6748 | PyErr_SetString(PyExc_SystemError, |
| 6749 | "accessing non-existent unicode segment"); |
| 6750 | return -1; |
| 6751 | } |
| 6752 | *ptr = (void *) self->str; |
| 6753 | return PyUnicode_GET_DATA_SIZE(self); |
| 6754 | } |
| 6755 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6756 | static Py_ssize_t |
| 6757 | unicode_buffer_getwritebuf(PyUnicodeObject *self, Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6758 | const void **ptr) |
| 6759 | { |
| 6760 | PyErr_SetString(PyExc_TypeError, |
Neal Norwitz | 20e7213 | 2002-06-13 21:25:17 +0000 | [diff] [blame] | 6761 | "cannot use unicode as modifiable buffer"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6762 | return -1; |
| 6763 | } |
| 6764 | |
| 6765 | static int |
| 6766 | unicode_buffer_getsegcount(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6767 | Py_ssize_t *lenp) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6768 | { |
| 6769 | if (lenp) |
| 6770 | *lenp = PyUnicode_GET_DATA_SIZE(self); |
| 6771 | return 1; |
| 6772 | } |
| 6773 | |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 6774 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6775 | unicode_buffer_getcharbuf(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6776 | Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6777 | const void **ptr) |
| 6778 | { |
| 6779 | PyObject *str; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6780 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6781 | if (index != 0) { |
| 6782 | PyErr_SetString(PyExc_SystemError, |
| 6783 | "accessing non-existent unicode segment"); |
| 6784 | return -1; |
| 6785 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 6786 | str = _PyUnicode_AsDefaultEncodedString((PyObject *)self, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6787 | if (str == NULL) |
| 6788 | return -1; |
| 6789 | *ptr = (void *) PyString_AS_STRING(str); |
| 6790 | return PyString_GET_SIZE(str); |
| 6791 | } |
| 6792 | |
| 6793 | /* Helpers for PyUnicode_Format() */ |
| 6794 | |
| 6795 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6796 | 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] | 6797 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6798 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6799 | if (argidx < arglen) { |
| 6800 | (*p_argidx)++; |
| 6801 | if (arglen < 0) |
| 6802 | return args; |
| 6803 | else |
| 6804 | return PyTuple_GetItem(args, argidx); |
| 6805 | } |
| 6806 | PyErr_SetString(PyExc_TypeError, |
| 6807 | "not enough arguments for format string"); |
| 6808 | return NULL; |
| 6809 | } |
| 6810 | |
| 6811 | #define F_LJUST (1<<0) |
| 6812 | #define F_SIGN (1<<1) |
| 6813 | #define F_BLANK (1<<2) |
| 6814 | #define F_ALT (1<<3) |
| 6815 | #define F_ZERO (1<<4) |
| 6816 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6817 | static Py_ssize_t |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6818 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6819 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6820 | register Py_ssize_t i; |
| 6821 | Py_ssize_t len = strlen(charbuffer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6822 | for (i = len - 1; i >= 0; i--) |
| 6823 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
| 6824 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6825 | return len; |
| 6826 | } |
| 6827 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6828 | static int |
| 6829 | doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) |
| 6830 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 6831 | Py_ssize_t result; |
| 6832 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6833 | PyOS_ascii_formatd((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 6834 | result = strtounicode(buffer, (char *)buffer); |
| 6835 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6836 | } |
| 6837 | |
| 6838 | static int |
| 6839 | longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) |
| 6840 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 6841 | Py_ssize_t result; |
| 6842 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6843 | PyOS_snprintf((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 6844 | result = strtounicode(buffer, (char *)buffer); |
| 6845 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6846 | } |
| 6847 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 6848 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 6849 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 6850 | formatting is done. */ |
| 6851 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6852 | static int |
| 6853 | formatfloat(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6854 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6855 | int flags, |
| 6856 | int prec, |
| 6857 | int type, |
| 6858 | PyObject *v) |
| 6859 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6860 | /* fmt = '%#.' + `prec` + `type` |
| 6861 | 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] | 6862 | char fmt[20]; |
| 6863 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6864 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6865 | x = PyFloat_AsDouble(v); |
| 6866 | if (x == -1.0 && PyErr_Occurred()) |
| 6867 | return -1; |
| 6868 | if (prec < 0) |
| 6869 | prec = 6; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6870 | if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
| 6871 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6872 | /* Worst case length calc to ensure no buffer overrun: |
| 6873 | |
| 6874 | 'g' formats: |
| 6875 | fmt = %#.<prec>g |
| 6876 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 6877 | for any double rep.) |
| 6878 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 6879 | |
| 6880 | 'f' formats: |
| 6881 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 6882 | len = 1 + 50 + 1 + prec = 52 + prec |
| 6883 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6884 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6885 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6886 | |
| 6887 | */ |
| 6888 | if ((type == 'g' && buflen <= (size_t)10 + (size_t)prec) || |
| 6889 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6890 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6891 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6892 | return -1; |
| 6893 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6894 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 6895 | (flags&F_ALT) ? "#" : "", |
| 6896 | prec, type); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6897 | return doubletounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6898 | } |
| 6899 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6900 | static PyObject* |
| 6901 | formatlong(PyObject *val, int flags, int prec, int type) |
| 6902 | { |
| 6903 | char *buf; |
| 6904 | int i, len; |
| 6905 | PyObject *str; /* temporary string object. */ |
| 6906 | PyUnicodeObject *result; |
| 6907 | |
| 6908 | str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
| 6909 | if (!str) |
| 6910 | return NULL; |
| 6911 | result = _PyUnicode_New(len); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 6912 | if (!result) { |
| 6913 | Py_DECREF(str); |
| 6914 | return NULL; |
| 6915 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6916 | for (i = 0; i < len; i++) |
| 6917 | result->str[i] = buf[i]; |
| 6918 | result->str[len] = 0; |
| 6919 | Py_DECREF(str); |
| 6920 | return (PyObject*)result; |
| 6921 | } |
| 6922 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6923 | static int |
| 6924 | formatint(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6925 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6926 | int flags, |
| 6927 | int prec, |
| 6928 | int type, |
| 6929 | PyObject *v) |
| 6930 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6931 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6932 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 6933 | * + 1 + 1 |
| 6934 | * = 24 |
| 6935 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6936 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6937 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6938 | long x; |
| 6939 | |
| 6940 | x = PyInt_AsLong(v); |
| 6941 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6942 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6943 | if (x < 0 && type == 'u') { |
| 6944 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 6945 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6946 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 6947 | sign = "-"; |
| 6948 | else |
| 6949 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6950 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6951 | prec = 1; |
| 6952 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6953 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 6954 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6955 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6956 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6957 | PyErr_SetString(PyExc_OverflowError, |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6958 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6959 | return -1; |
| 6960 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6961 | |
| 6962 | if ((flags & F_ALT) && |
| 6963 | (type == 'x' || type == 'X')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6964 | /* When converting under %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6965 | * of issues that cause pain: |
| 6966 | * - when 0 is being converted, the C standard leaves off |
| 6967 | * the '0x' or '0X', which is inconsistent with other |
| 6968 | * %#x/%#X conversions and inconsistent with Python's |
| 6969 | * hex() function |
| 6970 | * - there are platforms that violate the standard and |
| 6971 | * convert 0 with the '0x' or '0X' |
| 6972 | * (Metrowerks, Compaq Tru64) |
| 6973 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6974 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6975 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6976 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6977 | * We can achieve the desired consistency by inserting our |
| 6978 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 6979 | * of %#x/%#X. |
| 6980 | * |
| 6981 | * Note that this is the same approach as used in |
| 6982 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 6983 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6984 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 6985 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 6986 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6987 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6988 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 6989 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6990 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 6991 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6992 | if (sign[0]) |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6993 | return longtounicode(buf, buflen, fmt, -x); |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6994 | else |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6995 | return longtounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6996 | } |
| 6997 | |
| 6998 | static int |
| 6999 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7000 | size_t buflen, |
| 7001 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7002 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7003 | /* presume that the buffer is at least 2 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7004 | if (PyUnicode_Check(v)) { |
| 7005 | if (PyUnicode_GET_SIZE(v) != 1) |
| 7006 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7007 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7008 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7009 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7010 | else if (PyString_Check(v)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7011 | if (PyString_GET_SIZE(v) != 1) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7012 | goto onError; |
| 7013 | buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0]; |
| 7014 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7015 | |
| 7016 | else { |
| 7017 | /* Integer input truncated to a character */ |
| 7018 | long x; |
| 7019 | x = PyInt_AsLong(v); |
| 7020 | if (x == -1 && PyErr_Occurred()) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7021 | goto onError; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 7022 | #ifdef Py_UNICODE_WIDE |
| 7023 | if (x < 0 || x > 0x10ffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 7024 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 7025 | "%c arg not in range(0x110000) " |
| 7026 | "(wide Python build)"); |
| 7027 | return -1; |
| 7028 | } |
| 7029 | #else |
| 7030 | if (x < 0 || x > 0xffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 7031 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 7032 | "%c arg not in range(0x10000) " |
| 7033 | "(narrow Python build)"); |
| 7034 | return -1; |
| 7035 | } |
| 7036 | #endif |
| 7037 | buf[0] = (Py_UNICODE) x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7038 | } |
| 7039 | buf[1] = '\0'; |
| 7040 | return 1; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7041 | |
| 7042 | onError: |
| 7043 | PyErr_SetString(PyExc_TypeError, |
| 7044 | "%c requires int or char"); |
| 7045 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7046 | } |
| 7047 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7048 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 7049 | |
| 7050 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 7051 | chars are formatted. XXX This is a magic number. Each formatting |
| 7052 | routine does bounds checking to ensure no overflow, but a better |
| 7053 | solution may be to malloc a buffer of appropriate size for each |
| 7054 | format. For now, the current solution is sufficient. |
| 7055 | */ |
| 7056 | #define FORMATBUFLEN (size_t)120 |
| 7057 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7058 | PyObject *PyUnicode_Format(PyObject *format, |
| 7059 | PyObject *args) |
| 7060 | { |
| 7061 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7062 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7063 | int args_owned = 0; |
| 7064 | PyUnicodeObject *result = NULL; |
| 7065 | PyObject *dict = NULL; |
| 7066 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7067 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7068 | if (format == NULL || args == NULL) { |
| 7069 | PyErr_BadInternalCall(); |
| 7070 | return NULL; |
| 7071 | } |
| 7072 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7073 | if (uformat == NULL) |
| 7074 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7075 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 7076 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 7077 | |
| 7078 | reslen = rescnt = fmtcnt + 100; |
| 7079 | result = _PyUnicode_New(reslen); |
| 7080 | if (result == NULL) |
| 7081 | goto onError; |
| 7082 | res = PyUnicode_AS_UNICODE(result); |
| 7083 | |
| 7084 | if (PyTuple_Check(args)) { |
| 7085 | arglen = PyTuple_Size(args); |
| 7086 | argidx = 0; |
| 7087 | } |
| 7088 | else { |
| 7089 | arglen = -1; |
| 7090 | argidx = -2; |
| 7091 | } |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 7092 | if (args->ob_type->tp_as_mapping && !PyTuple_Check(args) && |
| 7093 | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7094 | dict = args; |
| 7095 | |
| 7096 | while (--fmtcnt >= 0) { |
| 7097 | if (*fmt != '%') { |
| 7098 | if (--rescnt < 0) { |
| 7099 | rescnt = fmtcnt + 100; |
| 7100 | reslen += rescnt; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7101 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 7102 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7103 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 7104 | --rescnt; |
| 7105 | } |
| 7106 | *res++ = *fmt++; |
| 7107 | } |
| 7108 | else { |
| 7109 | /* Got a format specifier */ |
| 7110 | int flags = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7111 | Py_ssize_t width = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7112 | int prec = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7113 | Py_UNICODE c = '\0'; |
| 7114 | Py_UNICODE fill; |
| 7115 | PyObject *v = NULL; |
| 7116 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7117 | Py_UNICODE *pbuf; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7118 | Py_UNICODE sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7119 | Py_ssize_t len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7120 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7121 | |
| 7122 | fmt++; |
| 7123 | if (*fmt == '(') { |
| 7124 | Py_UNICODE *keystart; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7125 | Py_ssize_t keylen; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7126 | PyObject *key; |
| 7127 | int pcount = 1; |
| 7128 | |
| 7129 | if (dict == NULL) { |
| 7130 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7131 | "format requires a mapping"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7132 | goto onError; |
| 7133 | } |
| 7134 | ++fmt; |
| 7135 | --fmtcnt; |
| 7136 | keystart = fmt; |
| 7137 | /* Skip over balanced parentheses */ |
| 7138 | while (pcount > 0 && --fmtcnt >= 0) { |
| 7139 | if (*fmt == ')') |
| 7140 | --pcount; |
| 7141 | else if (*fmt == '(') |
| 7142 | ++pcount; |
| 7143 | fmt++; |
| 7144 | } |
| 7145 | keylen = fmt - keystart - 1; |
| 7146 | if (fmtcnt < 0 || pcount > 0) { |
| 7147 | PyErr_SetString(PyExc_ValueError, |
| 7148 | "incomplete format key"); |
| 7149 | goto onError; |
| 7150 | } |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 7151 | #if 0 |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7152 | /* keys are converted to strings using UTF-8 and |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7153 | then looked up since Python uses strings to hold |
| 7154 | variables names etc. in its namespaces and we |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7155 | wouldn't want to break common idioms. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7156 | key = PyUnicode_EncodeUTF8(keystart, |
| 7157 | keylen, |
| 7158 | NULL); |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 7159 | #else |
| 7160 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 7161 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7162 | if (key == NULL) |
| 7163 | goto onError; |
| 7164 | if (args_owned) { |
| 7165 | Py_DECREF(args); |
| 7166 | args_owned = 0; |
| 7167 | } |
| 7168 | args = PyObject_GetItem(dict, key); |
| 7169 | Py_DECREF(key); |
| 7170 | if (args == NULL) { |
| 7171 | goto onError; |
| 7172 | } |
| 7173 | args_owned = 1; |
| 7174 | arglen = -1; |
| 7175 | argidx = -2; |
| 7176 | } |
| 7177 | while (--fmtcnt >= 0) { |
| 7178 | switch (c = *fmt++) { |
| 7179 | case '-': flags |= F_LJUST; continue; |
| 7180 | case '+': flags |= F_SIGN; continue; |
| 7181 | case ' ': flags |= F_BLANK; continue; |
| 7182 | case '#': flags |= F_ALT; continue; |
| 7183 | case '0': flags |= F_ZERO; continue; |
| 7184 | } |
| 7185 | break; |
| 7186 | } |
| 7187 | if (c == '*') { |
| 7188 | v = getnextarg(args, arglen, &argidx); |
| 7189 | if (v == NULL) |
| 7190 | goto onError; |
| 7191 | if (!PyInt_Check(v)) { |
| 7192 | PyErr_SetString(PyExc_TypeError, |
| 7193 | "* wants int"); |
| 7194 | goto onError; |
| 7195 | } |
| 7196 | width = PyInt_AsLong(v); |
| 7197 | if (width < 0) { |
| 7198 | flags |= F_LJUST; |
| 7199 | width = -width; |
| 7200 | } |
| 7201 | if (--fmtcnt >= 0) |
| 7202 | c = *fmt++; |
| 7203 | } |
| 7204 | else if (c >= '0' && c <= '9') { |
| 7205 | width = c - '0'; |
| 7206 | while (--fmtcnt >= 0) { |
| 7207 | c = *fmt++; |
| 7208 | if (c < '0' || c > '9') |
| 7209 | break; |
| 7210 | if ((width*10) / 10 != width) { |
| 7211 | PyErr_SetString(PyExc_ValueError, |
| 7212 | "width too big"); |
| 7213 | goto onError; |
| 7214 | } |
| 7215 | width = width*10 + (c - '0'); |
| 7216 | } |
| 7217 | } |
| 7218 | if (c == '.') { |
| 7219 | prec = 0; |
| 7220 | if (--fmtcnt >= 0) |
| 7221 | c = *fmt++; |
| 7222 | if (c == '*') { |
| 7223 | v = getnextarg(args, arglen, &argidx); |
| 7224 | if (v == NULL) |
| 7225 | goto onError; |
| 7226 | if (!PyInt_Check(v)) { |
| 7227 | PyErr_SetString(PyExc_TypeError, |
| 7228 | "* wants int"); |
| 7229 | goto onError; |
| 7230 | } |
| 7231 | prec = PyInt_AsLong(v); |
| 7232 | if (prec < 0) |
| 7233 | prec = 0; |
| 7234 | if (--fmtcnt >= 0) |
| 7235 | c = *fmt++; |
| 7236 | } |
| 7237 | else if (c >= '0' && c <= '9') { |
| 7238 | prec = c - '0'; |
| 7239 | while (--fmtcnt >= 0) { |
| 7240 | c = Py_CHARMASK(*fmt++); |
| 7241 | if (c < '0' || c > '9') |
| 7242 | break; |
| 7243 | if ((prec*10) / 10 != prec) { |
| 7244 | PyErr_SetString(PyExc_ValueError, |
| 7245 | "prec too big"); |
| 7246 | goto onError; |
| 7247 | } |
| 7248 | prec = prec*10 + (c - '0'); |
| 7249 | } |
| 7250 | } |
| 7251 | } /* prec */ |
| 7252 | if (fmtcnt >= 0) { |
| 7253 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7254 | if (--fmtcnt >= 0) |
| 7255 | c = *fmt++; |
| 7256 | } |
| 7257 | } |
| 7258 | if (fmtcnt < 0) { |
| 7259 | PyErr_SetString(PyExc_ValueError, |
| 7260 | "incomplete format"); |
| 7261 | goto onError; |
| 7262 | } |
| 7263 | if (c != '%') { |
| 7264 | v = getnextarg(args, arglen, &argidx); |
| 7265 | if (v == NULL) |
| 7266 | goto onError; |
| 7267 | } |
| 7268 | sign = 0; |
| 7269 | fill = ' '; |
| 7270 | switch (c) { |
| 7271 | |
| 7272 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7273 | pbuf = formatbuf; |
| 7274 | /* presume that buffer length is at least 1 */ |
| 7275 | pbuf[0] = '%'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7276 | len = 1; |
| 7277 | break; |
| 7278 | |
| 7279 | case 's': |
| 7280 | case 'r': |
| 7281 | if (PyUnicode_Check(v) && c == 's') { |
| 7282 | temp = v; |
| 7283 | Py_INCREF(temp); |
| 7284 | } |
| 7285 | else { |
| 7286 | PyObject *unicode; |
| 7287 | if (c == 's') |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 7288 | temp = PyObject_Unicode(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7289 | else |
| 7290 | temp = PyObject_Repr(v); |
| 7291 | if (temp == NULL) |
| 7292 | goto onError; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 7293 | if (PyUnicode_Check(temp)) |
| 7294 | /* nothing to do */; |
| 7295 | else if (PyString_Check(temp)) { |
| 7296 | /* convert to string to Unicode */ |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7297 | unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7298 | PyString_GET_SIZE(temp), |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7299 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7300 | "strict"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7301 | Py_DECREF(temp); |
| 7302 | temp = unicode; |
| 7303 | if (temp == NULL) |
| 7304 | goto onError; |
| 7305 | } |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 7306 | else { |
| 7307 | Py_DECREF(temp); |
| 7308 | PyErr_SetString(PyExc_TypeError, |
| 7309 | "%s argument has non-string str()"); |
| 7310 | goto onError; |
| 7311 | } |
| 7312 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7313 | pbuf = PyUnicode_AS_UNICODE(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7314 | len = PyUnicode_GET_SIZE(temp); |
| 7315 | if (prec >= 0 && len > prec) |
| 7316 | len = prec; |
| 7317 | break; |
| 7318 | |
| 7319 | case 'i': |
| 7320 | case 'd': |
| 7321 | case 'u': |
| 7322 | case 'o': |
| 7323 | case 'x': |
| 7324 | case 'X': |
| 7325 | if (c == 'i') |
| 7326 | c = 'd'; |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 7327 | if (PyLong_Check(v)) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7328 | temp = formatlong(v, flags, prec, c); |
| 7329 | if (!temp) |
| 7330 | goto onError; |
| 7331 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 7332 | len = PyUnicode_GET_SIZE(temp); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7333 | sign = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7334 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7335 | else { |
| 7336 | pbuf = formatbuf; |
| 7337 | len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 7338 | flags, prec, c, v); |
| 7339 | if (len < 0) |
| 7340 | goto onError; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7341 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7342 | } |
| 7343 | if (flags & F_ZERO) |
| 7344 | fill = '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7345 | break; |
| 7346 | |
| 7347 | case 'e': |
| 7348 | case 'E': |
| 7349 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 7350 | case 'F': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7351 | case 'g': |
| 7352 | case 'G': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 7353 | if (c == 'F') |
| 7354 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7355 | pbuf = formatbuf; |
| 7356 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 7357 | flags, prec, c, v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7358 | if (len < 0) |
| 7359 | goto onError; |
| 7360 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7361 | if (flags & F_ZERO) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7362 | fill = '0'; |
| 7363 | break; |
| 7364 | |
| 7365 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7366 | pbuf = formatbuf; |
| 7367 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7368 | if (len < 0) |
| 7369 | goto onError; |
| 7370 | break; |
| 7371 | |
| 7372 | default: |
| 7373 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 7374 | "unsupported format character '%c' (0x%x) " |
| 7375 | "at index %i", |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7376 | (31<=c && c<=126) ? (char)c : '?', |
Marc-André Lemburg | 24e53b6 | 2002-09-24 09:32:14 +0000 | [diff] [blame] | 7377 | (int)c, |
Guido van Rossum | efc1188 | 2002-09-12 14:43:41 +0000 | [diff] [blame] | 7378 | (int)(fmt -1 - PyUnicode_AS_UNICODE(uformat))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7379 | goto onError; |
| 7380 | } |
| 7381 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7382 | if (*pbuf == '-' || *pbuf == '+') { |
| 7383 | sign = *pbuf++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7384 | len--; |
| 7385 | } |
| 7386 | else if (flags & F_SIGN) |
| 7387 | sign = '+'; |
| 7388 | else if (flags & F_BLANK) |
| 7389 | sign = ' '; |
| 7390 | else |
| 7391 | sign = 0; |
| 7392 | } |
| 7393 | if (width < len) |
| 7394 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 7395 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7396 | reslen -= rescnt; |
| 7397 | rescnt = width + fmtcnt + 100; |
| 7398 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 7399 | if (reslen < 0) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 7400 | Py_XDECREF(temp); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7401 | PyErr_NoMemory(); |
| 7402 | goto onError; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 7403 | } |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7404 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 7405 | Py_XDECREF(temp); |
| 7406 | goto onError; |
| 7407 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7408 | res = PyUnicode_AS_UNICODE(result) |
| 7409 | + reslen - rescnt; |
| 7410 | } |
| 7411 | if (sign) { |
| 7412 | if (fill != ' ') |
| 7413 | *res++ = sign; |
| 7414 | rescnt--; |
| 7415 | if (width > len) |
| 7416 | width--; |
| 7417 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7418 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 7419 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7420 | assert(pbuf[1] == c); |
| 7421 | if (fill != ' ') { |
| 7422 | *res++ = *pbuf++; |
| 7423 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7424 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7425 | rescnt -= 2; |
| 7426 | width -= 2; |
| 7427 | if (width < 0) |
| 7428 | width = 0; |
| 7429 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7430 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7431 | if (width > len && !(flags & F_LJUST)) { |
| 7432 | do { |
| 7433 | --rescnt; |
| 7434 | *res++ = fill; |
| 7435 | } while (--width > len); |
| 7436 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7437 | if (fill == ' ') { |
| 7438 | if (sign) |
| 7439 | *res++ = sign; |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7440 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7441 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7442 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7443 | *res++ = *pbuf++; |
| 7444 | *res++ = *pbuf++; |
| 7445 | } |
| 7446 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7447 | Py_UNICODE_COPY(res, pbuf, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7448 | res += len; |
| 7449 | rescnt -= len; |
| 7450 | while (--width >= len) { |
| 7451 | --rescnt; |
| 7452 | *res++ = ' '; |
| 7453 | } |
| 7454 | if (dict && (argidx < arglen) && c != '%') { |
| 7455 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 7456 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7457 | Py_XDECREF(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7458 | goto onError; |
| 7459 | } |
| 7460 | Py_XDECREF(temp); |
| 7461 | } /* '%' */ |
| 7462 | } /* until end */ |
| 7463 | if (argidx < arglen && !dict) { |
| 7464 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 7465 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7466 | goto onError; |
| 7467 | } |
| 7468 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7469 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
| 7470 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7471 | if (args_owned) { |
| 7472 | Py_DECREF(args); |
| 7473 | } |
| 7474 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7475 | return (PyObject *)result; |
| 7476 | |
| 7477 | onError: |
| 7478 | Py_XDECREF(result); |
| 7479 | Py_DECREF(uformat); |
| 7480 | if (args_owned) { |
| 7481 | Py_DECREF(args); |
| 7482 | } |
| 7483 | return NULL; |
| 7484 | } |
| 7485 | |
| 7486 | static PyBufferProcs unicode_as_buffer = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7487 | (readbufferproc) unicode_buffer_getreadbuf, |
| 7488 | (writebufferproc) unicode_buffer_getwritebuf, |
| 7489 | (segcountproc) unicode_buffer_getsegcount, |
| 7490 | (charbufferproc) unicode_buffer_getcharbuf, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7491 | }; |
| 7492 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 7493 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7494 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 7495 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7496 | static PyObject * |
| 7497 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 7498 | { |
| 7499 | PyObject *x = NULL; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 7500 | static char *kwlist[] = {"string", "encoding", "errors", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7501 | char *encoding = NULL; |
| 7502 | char *errors = NULL; |
| 7503 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7504 | if (type != &PyUnicode_Type) |
| 7505 | return unicode_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7506 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
| 7507 | kwlist, &x, &encoding, &errors)) |
| 7508 | return NULL; |
| 7509 | if (x == NULL) |
| 7510 | return (PyObject *)_PyUnicode_New(0); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 7511 | if (encoding == NULL && errors == NULL) |
| 7512 | return PyObject_Unicode(x); |
| 7513 | else |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7514 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
| 7515 | } |
| 7516 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7517 | static PyObject * |
| 7518 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 7519 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7520 | PyUnicodeObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7521 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7522 | |
| 7523 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 7524 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 7525 | if (tmp == NULL) |
| 7526 | return NULL; |
| 7527 | assert(PyUnicode_Check(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7528 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7529 | if (pnew == NULL) { |
| 7530 | Py_DECREF(tmp); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7531 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7532 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7533 | pnew->str = PyMem_NEW(Py_UNICODE, n+1); |
| 7534 | if (pnew->str == NULL) { |
| 7535 | _Py_ForgetReference((PyObject *)pnew); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 7536 | PyObject_Del(pnew); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7537 | Py_DECREF(tmp); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 7538 | return PyErr_NoMemory(); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7539 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7540 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 7541 | pnew->length = n; |
| 7542 | pnew->hash = tmp->hash; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7543 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7544 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7545 | } |
| 7546 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7547 | PyDoc_STRVAR(unicode_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7548 | "unicode(string [, encoding[, errors]]) -> object\n\ |
| 7549 | \n\ |
| 7550 | Create a new Unicode object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 7551 | encoding defaults to the current default string encoding.\n\ |
| 7552 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7553 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7554 | PyTypeObject PyUnicode_Type = { |
| 7555 | PyObject_HEAD_INIT(&PyType_Type) |
| 7556 | 0, /* ob_size */ |
| 7557 | "unicode", /* tp_name */ |
| 7558 | sizeof(PyUnicodeObject), /* tp_size */ |
| 7559 | 0, /* tp_itemsize */ |
| 7560 | /* Slots */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 7561 | (destructor)unicode_dealloc, /* tp_dealloc */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7562 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7563 | 0, /* tp_getattr */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7564 | 0, /* tp_setattr */ |
| 7565 | (cmpfunc) unicode_compare, /* tp_compare */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 7566 | unicode_repr, /* tp_repr */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7567 | &unicode_as_number, /* tp_as_number */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7568 | &unicode_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7569 | &unicode_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7570 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 7571 | 0, /* tp_call*/ |
| 7572 | (reprfunc) unicode_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7573 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 7574 | 0, /* tp_setattro */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7575 | &unicode_as_buffer, /* tp_as_buffer */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7576 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
| 7577 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7578 | unicode_doc, /* tp_doc */ |
| 7579 | 0, /* tp_traverse */ |
| 7580 | 0, /* tp_clear */ |
| 7581 | 0, /* tp_richcompare */ |
| 7582 | 0, /* tp_weaklistoffset */ |
| 7583 | 0, /* tp_iter */ |
| 7584 | 0, /* tp_iternext */ |
| 7585 | unicode_methods, /* tp_methods */ |
| 7586 | 0, /* tp_members */ |
| 7587 | 0, /* tp_getset */ |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 7588 | &PyBaseString_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7589 | 0, /* tp_dict */ |
| 7590 | 0, /* tp_descr_get */ |
| 7591 | 0, /* tp_descr_set */ |
| 7592 | 0, /* tp_dictoffset */ |
| 7593 | 0, /* tp_init */ |
| 7594 | 0, /* tp_alloc */ |
| 7595 | unicode_new, /* tp_new */ |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 7596 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7597 | }; |
| 7598 | |
| 7599 | /* Initialize the Unicode implementation */ |
| 7600 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 7601 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7602 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7603 | int i; |
| 7604 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 7605 | /* XXX - move this array to unicodectype.c ? */ |
| 7606 | Py_UNICODE linebreak[] = { |
| 7607 | 0x000A, /* LINE FEED */ |
| 7608 | 0x000D, /* CARRIAGE RETURN */ |
| 7609 | 0x001C, /* FILE SEPARATOR */ |
| 7610 | 0x001D, /* GROUP SEPARATOR */ |
| 7611 | 0x001E, /* RECORD SEPARATOR */ |
| 7612 | 0x0085, /* NEXT LINE */ |
| 7613 | 0x2028, /* LINE SEPARATOR */ |
| 7614 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 7615 | }; |
| 7616 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7617 | /* Init the implementation */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7618 | unicode_freelist = NULL; |
| 7619 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7620 | unicode_empty = _PyUnicode_New(0); |
Marc-André Lemburg | 90e8147 | 2000-06-07 09:13:21 +0000 | [diff] [blame] | 7621 | strcpy(unicode_default_encoding, "ascii"); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7622 | for (i = 0; i < 256; i++) |
| 7623 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 7624 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 7625 | Py_FatalError("Can't initialize 'unicode'"); |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 7626 | |
| 7627 | /* initialize the linebreak bloom filter */ |
| 7628 | bloom_linebreak = make_bloom_mask( |
| 7629 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 7630 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7631 | } |
| 7632 | |
| 7633 | /* Finalize the Unicode implementation */ |
| 7634 | |
| 7635 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 7636 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7637 | { |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 7638 | PyUnicodeObject *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7639 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7640 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 7641 | Py_XDECREF(unicode_empty); |
| 7642 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 7643 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7644 | for (i = 0; i < 256; i++) { |
| 7645 | if (unicode_latin1[i]) { |
| 7646 | Py_DECREF(unicode_latin1[i]); |
| 7647 | unicode_latin1[i] = NULL; |
| 7648 | } |
| 7649 | } |
| 7650 | |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 7651 | for (u = unicode_freelist; u != NULL;) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7652 | PyUnicodeObject *v = u; |
| 7653 | u = *(PyUnicodeObject **)u; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 7654 | if (v->str) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 7655 | PyMem_DEL(v->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 7656 | Py_XDECREF(v->defenc); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 7657 | PyObject_Del(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7658 | } |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7659 | unicode_freelist = NULL; |
| 7660 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7661 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 7662 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 7663 | #ifdef __cplusplus |
| 7664 | } |
| 7665 | #endif |
| 7666 | |
| 7667 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 7668 | /* |
| 7669 | Local variables: |
| 7670 | c-basic-offset: 4 |
| 7671 | indent-tabs-mode: nil |
| 7672 | End: |
| 7673 | */ |