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 | |
Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 7 | Copyright (c) Corporation for National Research Initiatives. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 9 | -------------------------------------------------------------------- |
| 10 | The original string type implementation is: |
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 | Copyright (c) 1999 by Secret Labs AB |
| 13 | Copyright (c) 1999 by Fredrik Lundh |
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 | By obtaining, using, and/or copying this software and/or its |
| 16 | associated documentation, you agree that you have read, understood, |
| 17 | and will comply with the following terms and conditions: |
| 18 | |
| 19 | Permission to use, copy, modify, and distribute this software and its |
| 20 | associated documentation for any purpose and without fee is hereby |
| 21 | granted, provided that the above copyright notice appears in all |
| 22 | copies, and that both that copyright notice and this permission notice |
| 23 | appear in supporting documentation, and that the name of Secret Labs |
| 24 | AB or the author not be used in advertising or publicity pertaining to |
| 25 | distribution of the software without specific, written prior |
| 26 | permission. |
| 27 | |
| 28 | SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 29 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 30 | FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR |
| 31 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 32 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 33 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 34 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 35 | -------------------------------------------------------------------- |
| 36 | |
| 37 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 38 | |
| 39 | #include "Python.h" |
| 40 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 41 | #include "unicodeobject.h" |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 42 | #include "ucnhash.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 43 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 44 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 45 | #include <windows.h> |
| 46 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 48 | /* Limit for the Unicode object free list */ |
| 49 | |
| 50 | #define MAX_UNICODE_FREELIST_SIZE 1024 |
| 51 | |
| 52 | /* Limit for the Unicode object free list stay alive optimization. |
| 53 | |
| 54 | The implementation will keep allocated Unicode memory intact for |
| 55 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 56 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 57 | |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 58 | At worst this will result in MAX_UNICODE_FREELIST_SIZE * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 59 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 60 | malloc()-overhead) bytes of unused garbage. |
| 61 | |
| 62 | Setting the limit to 0 effectively turns the feature off. |
| 63 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 64 | Note: This is an experimental feature ! If you get core dumps when |
| 65 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 66 | |
| 67 | */ |
| 68 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 69 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 70 | |
| 71 | /* Endianness switches; defaults to little endian */ |
| 72 | |
| 73 | #ifdef WORDS_BIGENDIAN |
| 74 | # define BYTEORDER_IS_BIG_ENDIAN |
| 75 | #else |
| 76 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 77 | #endif |
| 78 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 79 | /* --- Globals ------------------------------------------------------------ |
| 80 | |
| 81 | The globals are initialized by the _PyUnicode_Init() API and should |
| 82 | not be used before calling that API. |
| 83 | |
| 84 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 85 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 86 | /* Free list for Unicode objects */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 87 | static PyUnicodeObject *unicode_freelist; |
| 88 | static int unicode_freelist_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 89 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 90 | /* The empty Unicode object is shared to improve performance. */ |
| 91 | static PyUnicodeObject *unicode_empty; |
| 92 | |
| 93 | /* Single character Unicode strings in the Latin-1 range are being |
| 94 | shared as well. */ |
| 95 | static PyUnicodeObject *unicode_latin1[256]; |
| 96 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 97 | /* Default encoding to use and assume when NULL is passed as encoding |
| 98 | parameter; it is initialized by _PyUnicode_Init(). |
| 99 | |
| 100 | Always use the PyUnicode_SetDefaultEncoding() and |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 101 | PyUnicode_GetDefaultEncoding() APIs to access this global. |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 102 | |
| 103 | */ |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 104 | static char unicode_default_encoding[100]; |
| 105 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 106 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 107 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 108 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 109 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 110 | return 0x10FFFF; |
| 111 | #else |
| 112 | /* This is actually an illegal character, so it should |
| 113 | not be passed to unichr. */ |
| 114 | return 0xFFFF; |
| 115 | #endif |
| 116 | } |
| 117 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 118 | /* --- Unicode Object ----------------------------------------------------- */ |
| 119 | |
| 120 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 121 | int unicode_resize(register PyUnicodeObject *unicode, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 122 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 123 | { |
| 124 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 125 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 126 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 127 | if (unicode->length == length) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 128 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 129 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 130 | /* Resizing shared object (unicode_empty or single character |
| 131 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 132 | instead ! */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 133 | if (unicode == unicode_empty || |
| 134 | (unicode->length == 1 && |
| 135 | /* MvL said unicode->str[] may be signed. Python generally assumes |
| 136 | * an int contains at least 32 bits, and we don't use more than |
| 137 | * 32 bits even in a UCS4 build, so casting to unsigned int should |
| 138 | * be correct. |
| 139 | */ |
| 140 | (unsigned int)unicode->str[0] < 256U && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 141 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 142 | PyErr_SetString(PyExc_SystemError, |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 143 | "can't resize shared unicode objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 144 | return -1; |
| 145 | } |
| 146 | |
| 147 | /* We allocate one more byte to make sure the string is |
| 148 | Ux0000 terminated -- XXX is this needed ? */ |
| 149 | oldstr = unicode->str; |
| 150 | PyMem_RESIZE(unicode->str, Py_UNICODE, length + 1); |
| 151 | if (!unicode->str) { |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 152 | unicode->str = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 153 | PyErr_NoMemory(); |
| 154 | return -1; |
| 155 | } |
| 156 | unicode->str[length] = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 157 | assert(length < INT_MAX); |
| 158 | unicode->length = (int)length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 159 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 160 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 161 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 162 | if (unicode->defenc) { |
| 163 | Py_DECREF(unicode->defenc); |
| 164 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 165 | } |
| 166 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 167 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | /* We allocate one more byte to make sure the string is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 172 | Ux0000 terminated -- XXX is this needed ? |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 173 | |
| 174 | XXX This allocator could further be enhanced by assuring that the |
| 175 | free list never reduces its size below 1. |
| 176 | |
| 177 | */ |
| 178 | |
| 179 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 180 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 181 | { |
| 182 | register PyUnicodeObject *unicode; |
| 183 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 184 | /* Optimization fo empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 185 | if (length == 0 && unicode_empty != NULL) { |
| 186 | Py_INCREF(unicode_empty); |
| 187 | return unicode_empty; |
| 188 | } |
| 189 | |
| 190 | /* Unicode freelist & memory allocation */ |
| 191 | if (unicode_freelist) { |
| 192 | unicode = unicode_freelist; |
Marc-André Lemburg | bea47e7 | 2000-06-17 20:31:17 +0000 | [diff] [blame] | 193 | unicode_freelist = *(PyUnicodeObject **)unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 194 | unicode_freelist_size--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 195 | if (unicode->str) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 196 | /* Keep-Alive optimization: we only upsize the buffer, |
| 197 | never downsize it. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 198 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 199 | unicode_resize(unicode, length) < 0) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 200 | PyMem_DEL(unicode->str); |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 201 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 204 | else { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 205 | unicode->str = PyMem_NEW(Py_UNICODE, length + 1); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 206 | } |
| 207 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 208 | } |
| 209 | else { |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 210 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 211 | if (unicode == NULL) |
| 212 | return NULL; |
| 213 | unicode->str = PyMem_NEW(Py_UNICODE, length + 1); |
| 214 | } |
| 215 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 216 | if (!unicode->str) { |
| 217 | PyErr_NoMemory(); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 218 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 219 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 220 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 221 | * the caller fails before initializing str -- unicode_resize() |
| 222 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 223 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 224 | * We don't want unicode_resize to read uninitialized memory in |
| 225 | * that case. |
| 226 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 227 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 228 | unicode->str[length] = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 229 | assert(length<INT_MAX); |
| 230 | unicode->length = (int)length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 231 | unicode->hash = -1; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 232 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 233 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 234 | |
| 235 | onError: |
| 236 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 237 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 238 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 242 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 243 | { |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 244 | if (PyUnicode_CheckExact(unicode) && |
| 245 | unicode_freelist_size < MAX_UNICODE_FREELIST_SIZE) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 246 | /* Keep-Alive optimization */ |
| 247 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 248 | PyMem_DEL(unicode->str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 249 | unicode->str = NULL; |
| 250 | unicode->length = 0; |
| 251 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 252 | if (unicode->defenc) { |
| 253 | Py_DECREF(unicode->defenc); |
| 254 | unicode->defenc = NULL; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 255 | } |
| 256 | /* Add to free list */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 257 | *(PyUnicodeObject **)unicode = unicode_freelist; |
| 258 | unicode_freelist = unicode; |
| 259 | unicode_freelist_size++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 260 | } |
| 261 | else { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 262 | PyMem_DEL(unicode->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 263 | Py_XDECREF(unicode->defenc); |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 264 | unicode->ob_type->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 268 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 269 | { |
| 270 | register PyUnicodeObject *v; |
| 271 | |
| 272 | /* Argument checks */ |
| 273 | if (unicode == NULL) { |
| 274 | PyErr_BadInternalCall(); |
| 275 | return -1; |
| 276 | } |
| 277 | v = (PyUnicodeObject *)*unicode; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 278 | 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] | 279 | PyErr_BadInternalCall(); |
| 280 | return -1; |
| 281 | } |
| 282 | |
| 283 | /* Resizing unicode_empty and single character objects is not |
| 284 | possible since these are being shared. We simply return a fresh |
| 285 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 286 | if (v->length != length && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 287 | (v == unicode_empty || v->length == 1)) { |
| 288 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 289 | if (w == NULL) |
| 290 | return -1; |
| 291 | Py_UNICODE_COPY(w->str, v->str, |
| 292 | length < v->length ? length : v->length); |
Raymond Hettinger | c8df578 | 2003-03-09 07:30:43 +0000 | [diff] [blame] | 293 | Py_DECREF(*unicode); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 294 | *unicode = (PyObject *)w; |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 299 | objects, since we can modify them in-place. */ |
| 300 | return unicode_resize(v, length); |
| 301 | } |
| 302 | |
| 303 | /* Internal API for use in unicodeobject.c only ! */ |
| 304 | #define _PyUnicode_Resize(unicodevar, length) \ |
| 305 | PyUnicode_Resize(((PyObject **)(unicodevar)), length) |
| 306 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 307 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 308 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 309 | { |
| 310 | PyUnicodeObject *unicode; |
| 311 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 312 | /* If the Unicode data is known at construction time, we can apply |
| 313 | some optimizations which share commonly used objects. */ |
| 314 | if (u != NULL) { |
| 315 | |
| 316 | /* Optimization for empty strings */ |
| 317 | if (size == 0 && unicode_empty != NULL) { |
| 318 | Py_INCREF(unicode_empty); |
| 319 | return (PyObject *)unicode_empty; |
| 320 | } |
| 321 | |
| 322 | /* Single character Unicode objects in the Latin-1 range are |
| 323 | shared when using this constructor */ |
| 324 | if (size == 1 && *u < 256) { |
| 325 | unicode = unicode_latin1[*u]; |
| 326 | if (!unicode) { |
| 327 | unicode = _PyUnicode_New(1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 328 | if (!unicode) |
| 329 | return NULL; |
Marc-André Lemburg | 8879a33 | 2001-06-07 12:26:56 +0000 | [diff] [blame] | 330 | unicode->str[0] = *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 331 | unicode_latin1[*u] = unicode; |
| 332 | } |
| 333 | Py_INCREF(unicode); |
| 334 | return (PyObject *)unicode; |
| 335 | } |
| 336 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 337 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 338 | unicode = _PyUnicode_New(size); |
| 339 | if (!unicode) |
| 340 | return NULL; |
| 341 | |
| 342 | /* Copy the Unicode data into the new object */ |
| 343 | if (u != NULL) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 344 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 345 | |
| 346 | return (PyObject *)unicode; |
| 347 | } |
| 348 | |
| 349 | #ifdef HAVE_WCHAR_H |
| 350 | |
| 351 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 352 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 353 | { |
| 354 | PyUnicodeObject *unicode; |
| 355 | |
| 356 | if (w == NULL) { |
| 357 | PyErr_BadInternalCall(); |
| 358 | return NULL; |
| 359 | } |
| 360 | |
| 361 | unicode = _PyUnicode_New(size); |
| 362 | if (!unicode) |
| 363 | return NULL; |
| 364 | |
| 365 | /* Copy the wchar_t data into the new object */ |
| 366 | #ifdef HAVE_USABLE_WCHAR_T |
| 367 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 368 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 369 | { |
| 370 | register Py_UNICODE *u; |
| 371 | register int i; |
| 372 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 373 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 374 | *u++ = *w++; |
| 375 | } |
| 376 | #endif |
| 377 | |
| 378 | return (PyObject *)unicode; |
| 379 | } |
| 380 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 381 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 382 | wchar_t *w, |
| 383 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 384 | { |
| 385 | if (unicode == NULL) { |
| 386 | PyErr_BadInternalCall(); |
| 387 | return -1; |
| 388 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 389 | |
| 390 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 391 | if (size > PyUnicode_GET_SIZE(unicode)) |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 392 | size = PyUnicode_GET_SIZE(unicode) + 1; |
| 393 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 394 | #ifdef HAVE_USABLE_WCHAR_T |
| 395 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 396 | #else |
| 397 | { |
| 398 | register Py_UNICODE *u; |
| 399 | register int i; |
| 400 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 401 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 402 | *w++ = *u++; |
| 403 | } |
| 404 | #endif |
| 405 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 406 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 407 | return PyUnicode_GET_SIZE(unicode); |
| 408 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 409 | return size; |
| 410 | } |
| 411 | |
| 412 | #endif |
| 413 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 414 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 415 | { |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 416 | Py_UNICODE s[1]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 417 | |
| 418 | #ifdef Py_UNICODE_WIDE |
| 419 | if (ordinal < 0 || ordinal > 0x10ffff) { |
| 420 | PyErr_SetString(PyExc_ValueError, |
| 421 | "unichr() arg not in range(0x110000) " |
| 422 | "(wide Python build)"); |
| 423 | return NULL; |
| 424 | } |
| 425 | #else |
| 426 | if (ordinal < 0 || ordinal > 0xffff) { |
| 427 | PyErr_SetString(PyExc_ValueError, |
| 428 | "unichr() arg not in range(0x10000) " |
| 429 | "(narrow Python build)"); |
| 430 | return NULL; |
| 431 | } |
| 432 | #endif |
| 433 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 434 | s[0] = (Py_UNICODE)ordinal; |
| 435 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 438 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 439 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 440 | /* XXX Perhaps we should make this API an alias of |
| 441 | PyObject_Unicode() instead ?! */ |
| 442 | if (PyUnicode_CheckExact(obj)) { |
| 443 | Py_INCREF(obj); |
| 444 | return obj; |
| 445 | } |
| 446 | if (PyUnicode_Check(obj)) { |
| 447 | /* For a Unicode subtype that's not a Unicode object, |
| 448 | return a true Unicode object with the same data. */ |
| 449 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 450 | PyUnicode_GET_SIZE(obj)); |
| 451 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 452 | return PyUnicode_FromEncodedObject(obj, NULL, "strict"); |
| 453 | } |
| 454 | |
| 455 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
| 456 | const char *encoding, |
| 457 | const char *errors) |
| 458 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 459 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 460 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 461 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 462 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 463 | if (obj == NULL) { |
| 464 | PyErr_BadInternalCall(); |
| 465 | return NULL; |
| 466 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 467 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 468 | #if 0 |
| 469 | /* For b/w compatibility we also accept Unicode objects provided |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 470 | that no encodings is given and then redirect to |
| 471 | PyObject_Unicode() which then applies the additional logic for |
| 472 | Unicode subclasses. |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 473 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 474 | NOTE: This API should really only be used for object which |
| 475 | represent *encoded* Unicode ! |
| 476 | |
| 477 | */ |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 478 | if (PyUnicode_Check(obj)) { |
| 479 | if (encoding) { |
| 480 | PyErr_SetString(PyExc_TypeError, |
| 481 | "decoding Unicode is not supported"); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 482 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 483 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 484 | return PyObject_Unicode(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 485 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 486 | #else |
| 487 | if (PyUnicode_Check(obj)) { |
| 488 | PyErr_SetString(PyExc_TypeError, |
| 489 | "decoding Unicode is not supported"); |
| 490 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 491 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 492 | #endif |
| 493 | |
| 494 | /* Coerce object */ |
| 495 | if (PyString_Check(obj)) { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 496 | s = PyString_AS_STRING(obj); |
| 497 | len = PyString_GET_SIZE(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 498 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 499 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
| 500 | /* Overwrite the error message with something more useful in |
| 501 | case of a TypeError. */ |
| 502 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 503 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 504 | "coercing to Unicode: need string or buffer, " |
| 505 | "%.80s found", |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 506 | obj->ob_type->tp_name); |
| 507 | goto onError; |
| 508 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 509 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 510 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 511 | if (len == 0) { |
| 512 | Py_INCREF(unicode_empty); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 513 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 514 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 515 | else |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 516 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 517 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 518 | return v; |
| 519 | |
| 520 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 521 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | PyObject *PyUnicode_Decode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 525 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 526 | const char *encoding, |
| 527 | const char *errors) |
| 528 | { |
| 529 | PyObject *buffer = NULL, *unicode; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 530 | |
| 531 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 532 | encoding = PyUnicode_GetDefaultEncoding(); |
| 533 | |
| 534 | /* Shortcuts for common default encodings */ |
| 535 | if (strcmp(encoding, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 536 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 537 | else if (strcmp(encoding, "latin-1") == 0) |
| 538 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 539 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 540 | else if (strcmp(encoding, "mbcs") == 0) |
| 541 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 542 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 543 | else if (strcmp(encoding, "ascii") == 0) |
| 544 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 545 | |
| 546 | /* Decode via the codec registry */ |
| 547 | buffer = PyBuffer_FromMemory((void *)s, size); |
| 548 | if (buffer == NULL) |
| 549 | goto onError; |
| 550 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 551 | if (unicode == NULL) |
| 552 | goto onError; |
| 553 | if (!PyUnicode_Check(unicode)) { |
| 554 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 555 | "decoder did not return an unicode object (type=%.400s)", |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 556 | unicode->ob_type->tp_name); |
| 557 | Py_DECREF(unicode); |
| 558 | goto onError; |
| 559 | } |
| 560 | Py_DECREF(buffer); |
| 561 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 562 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 563 | onError: |
| 564 | Py_XDECREF(buffer); |
| 565 | return NULL; |
| 566 | } |
| 567 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 568 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 569 | const char *encoding, |
| 570 | const char *errors) |
| 571 | { |
| 572 | PyObject *v; |
| 573 | |
| 574 | if (!PyUnicode_Check(unicode)) { |
| 575 | PyErr_BadArgument(); |
| 576 | goto onError; |
| 577 | } |
| 578 | |
| 579 | if (encoding == NULL) |
| 580 | encoding = PyUnicode_GetDefaultEncoding(); |
| 581 | |
| 582 | /* Decode via the codec registry */ |
| 583 | v = PyCodec_Decode(unicode, encoding, errors); |
| 584 | if (v == NULL) |
| 585 | goto onError; |
| 586 | return v; |
| 587 | |
| 588 | onError: |
| 589 | return NULL; |
| 590 | } |
| 591 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 592 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 593 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 594 | const char *encoding, |
| 595 | const char *errors) |
| 596 | { |
| 597 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 598 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 599 | unicode = PyUnicode_FromUnicode(s, size); |
| 600 | if (unicode == NULL) |
| 601 | return NULL; |
| 602 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 603 | Py_DECREF(unicode); |
| 604 | return v; |
| 605 | } |
| 606 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 607 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 608 | const char *encoding, |
| 609 | const char *errors) |
| 610 | { |
| 611 | PyObject *v; |
| 612 | |
| 613 | if (!PyUnicode_Check(unicode)) { |
| 614 | PyErr_BadArgument(); |
| 615 | goto onError; |
| 616 | } |
| 617 | |
| 618 | if (encoding == NULL) |
| 619 | encoding = PyUnicode_GetDefaultEncoding(); |
| 620 | |
| 621 | /* Encode via the codec registry */ |
| 622 | v = PyCodec_Encode(unicode, encoding, errors); |
| 623 | if (v == NULL) |
| 624 | goto onError; |
| 625 | return v; |
| 626 | |
| 627 | onError: |
| 628 | return NULL; |
| 629 | } |
| 630 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 631 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 632 | const char *encoding, |
| 633 | const char *errors) |
| 634 | { |
| 635 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 636 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 637 | if (!PyUnicode_Check(unicode)) { |
| 638 | PyErr_BadArgument(); |
| 639 | goto onError; |
| 640 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 641 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 642 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 643 | encoding = PyUnicode_GetDefaultEncoding(); |
| 644 | |
| 645 | /* Shortcuts for common default encodings */ |
| 646 | if (errors == NULL) { |
| 647 | if (strcmp(encoding, "utf-8") == 0) |
Jeremy Hylton | 9cea41c | 2001-05-29 17:13:15 +0000 | [diff] [blame] | 648 | return PyUnicode_AsUTF8String(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 649 | else if (strcmp(encoding, "latin-1") == 0) |
| 650 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 651 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 652 | else if (strcmp(encoding, "mbcs") == 0) |
| 653 | return PyUnicode_AsMBCSString(unicode); |
| 654 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 655 | else if (strcmp(encoding, "ascii") == 0) |
| 656 | return PyUnicode_AsASCIIString(unicode); |
| 657 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 658 | |
| 659 | /* Encode via the codec registry */ |
| 660 | v = PyCodec_Encode(unicode, encoding, errors); |
| 661 | if (v == NULL) |
| 662 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 663 | if (!PyString_Check(v)) { |
| 664 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 665 | "encoder did not return a string object (type=%.400s)", |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 666 | v->ob_type->tp_name); |
| 667 | Py_DECREF(v); |
| 668 | goto onError; |
| 669 | } |
| 670 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 671 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 672 | onError: |
| 673 | return NULL; |
| 674 | } |
| 675 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 676 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 677 | const char *errors) |
| 678 | { |
| 679 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
| 680 | |
| 681 | if (v) |
| 682 | return v; |
| 683 | v = PyUnicode_AsEncodedString(unicode, NULL, errors); |
| 684 | if (v && errors == NULL) |
| 685 | ((PyUnicodeObject *)unicode)->defenc = v; |
| 686 | return v; |
| 687 | } |
| 688 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 689 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 690 | { |
| 691 | if (!PyUnicode_Check(unicode)) { |
| 692 | PyErr_BadArgument(); |
| 693 | goto onError; |
| 694 | } |
| 695 | return PyUnicode_AS_UNICODE(unicode); |
| 696 | |
| 697 | onError: |
| 698 | return NULL; |
| 699 | } |
| 700 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 701 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 702 | { |
| 703 | if (!PyUnicode_Check(unicode)) { |
| 704 | PyErr_BadArgument(); |
| 705 | goto onError; |
| 706 | } |
| 707 | return PyUnicode_GET_SIZE(unicode); |
| 708 | |
| 709 | onError: |
| 710 | return -1; |
| 711 | } |
| 712 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 713 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 714 | { |
| 715 | return unicode_default_encoding; |
| 716 | } |
| 717 | |
| 718 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 719 | { |
| 720 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 721 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 722 | /* Make sure the encoding is valid. As side effect, this also |
| 723 | loads the encoding into the codec registry cache. */ |
| 724 | v = _PyCodec_Lookup(encoding); |
| 725 | if (v == NULL) |
| 726 | goto onError; |
| 727 | Py_DECREF(v); |
| 728 | strncpy(unicode_default_encoding, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 729 | encoding, |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 730 | sizeof(unicode_default_encoding)); |
| 731 | return 0; |
| 732 | |
| 733 | onError: |
| 734 | return -1; |
| 735 | } |
| 736 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 737 | /* error handling callback helper: |
| 738 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 739 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 740 | and adjust various state variables. |
| 741 | return 0 on success, -1 on error |
| 742 | */ |
| 743 | |
| 744 | static |
| 745 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 746 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 747 | const char *input, Py_ssize_t insize, Py_ssize_t *startinpos, Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 748 | PyObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 749 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 750 | 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] | 751 | |
| 752 | PyObject *restuple = NULL; |
| 753 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 754 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
| 755 | Py_ssize_t requiredsize; |
| 756 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 757 | Py_UNICODE *repptr; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 758 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 759 | int res = -1; |
| 760 | |
| 761 | if (*errorHandler == NULL) { |
| 762 | *errorHandler = PyCodec_LookupError(errors); |
| 763 | if (*errorHandler == NULL) |
| 764 | goto onError; |
| 765 | } |
| 766 | |
| 767 | if (*exceptionObject == NULL) { |
| 768 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 769 | encoding, input, insize, *startinpos, *endinpos, reason); |
| 770 | if (*exceptionObject == NULL) |
| 771 | goto onError; |
| 772 | } |
| 773 | else { |
| 774 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 775 | goto onError; |
| 776 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 777 | goto onError; |
| 778 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 779 | goto onError; |
| 780 | } |
| 781 | |
| 782 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 783 | if (restuple == NULL) |
| 784 | goto onError; |
| 785 | if (!PyTuple_Check(restuple)) { |
| 786 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 787 | goto onError; |
| 788 | } |
| 789 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 790 | goto onError; |
| 791 | if (newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 792 | newpos = insize+newpos; |
| 793 | if (newpos<0 || newpos>insize) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 794 | 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] | 795 | goto onError; |
| 796 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 797 | |
| 798 | /* need more space? (at least enough for what we |
| 799 | have+the replacement+the rest of the string (starting |
| 800 | at the new input position), so we won't have to check space |
| 801 | when there are no errors in the rest of the string) */ |
| 802 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 803 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 804 | requiredsize = *outpos + repsize + insize-newpos; |
| 805 | if (requiredsize > outsize) { |
| 806 | if (requiredsize<2*outsize) |
| 807 | requiredsize = 2*outsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 808 | if (PyUnicode_Resize(output, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 809 | goto onError; |
| 810 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
| 811 | } |
| 812 | *endinpos = newpos; |
| 813 | *inptr = input + newpos; |
| 814 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 815 | *outptr += repsize; |
| 816 | *outpos += repsize; |
| 817 | /* we made it! */ |
| 818 | res = 0; |
| 819 | |
| 820 | onError: |
| 821 | Py_XDECREF(restuple); |
| 822 | return res; |
| 823 | } |
| 824 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 825 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 826 | |
| 827 | /* see RFC2152 for details */ |
| 828 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 829 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 830 | char utf7_special[128] = { |
| 831 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 832 | encoded: |
| 833 | 0 - not special |
| 834 | 1 - special |
| 835 | 2 - whitespace (optional) |
| 836 | 3 - RFC2152 Set O (optional) */ |
| 837 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 838 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 839 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 840 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 841 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 842 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 843 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 844 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 845 | |
| 846 | }; |
| 847 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 848 | /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
| 849 | warnings about the comparison always being false; since |
| 850 | utf7_special[0] is 1, we can safely make that one comparison |
| 851 | true */ |
| 852 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 853 | #define SPECIAL(c, encodeO, encodeWS) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 854 | ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 855 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 856 | (encodeO && (utf7_special[(c)] == 3))) |
| 857 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 858 | #define B64(n) \ |
| 859 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 860 | #define B64CHAR(c) \ |
| 861 | (isalnum(c) || (c) == '+' || (c) == '/') |
| 862 | #define UB64(c) \ |
| 863 | ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
| 864 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 865 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 866 | #define ENCODE(out, ch, bits) \ |
| 867 | while (bits >= 6) { \ |
| 868 | *out++ = B64(ch >> (bits-6)); \ |
| 869 | bits -= 6; \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 872 | #define DECODE(out, ch, bits, surrogate) \ |
| 873 | while (bits >= 16) { \ |
| 874 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 875 | bits -= 16; \ |
| 876 | if (surrogate) { \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 877 | /* We have already generated an error for the high surrogate \ |
| 878 | 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] | 879 | surrogate = 0; \ |
| 880 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 881 | /* This is a surrogate pair. Unfortunately we can't represent \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 882 | it in a 16-bit character */ \ |
| 883 | surrogate = 1; \ |
| 884 | errmsg = "code pairs are not supported"; \ |
| 885 | goto utf7Error; \ |
| 886 | } else { \ |
| 887 | *out++ = outCh; \ |
| 888 | } \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 889 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 890 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 891 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 892 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 893 | const char *errors) |
| 894 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 895 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 896 | Py_ssize_t startinpos; |
| 897 | Py_ssize_t endinpos; |
| 898 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 899 | const char *e; |
| 900 | PyUnicodeObject *unicode; |
| 901 | Py_UNICODE *p; |
| 902 | const char *errmsg = ""; |
| 903 | int inShift = 0; |
| 904 | unsigned int bitsleft = 0; |
| 905 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 906 | int surrogate = 0; |
| 907 | PyObject *errorHandler = NULL; |
| 908 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 909 | |
| 910 | unicode = _PyUnicode_New(size); |
| 911 | if (!unicode) |
| 912 | return NULL; |
| 913 | if (size == 0) |
| 914 | return (PyObject *)unicode; |
| 915 | |
| 916 | p = unicode->str; |
| 917 | e = s + size; |
| 918 | |
| 919 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 920 | Py_UNICODE ch; |
| 921 | restart: |
| 922 | ch = *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 923 | |
| 924 | if (inShift) { |
| 925 | if ((ch == '-') || !B64CHAR(ch)) { |
| 926 | inShift = 0; |
| 927 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 928 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 929 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 930 | if (bitsleft >= 6) { |
| 931 | /* The shift sequence has a partial character in it. If |
| 932 | bitsleft < 6 then we could just classify it as padding |
| 933 | but that is not the case here */ |
| 934 | |
| 935 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 936 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 937 | } |
| 938 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 939 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 940 | here so indicate the potential of a misencoded character. */ |
| 941 | |
| 942 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 943 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 944 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 945 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | if (ch == '-') { |
| 949 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 950 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 951 | inShift = 1; |
| 952 | } |
| 953 | } else if (SPECIAL(ch,0,0)) { |
| 954 | errmsg = "unexpected special character"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 955 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 956 | } else { |
| 957 | *p++ = ch; |
| 958 | } |
| 959 | } else { |
| 960 | charsleft = (charsleft << 6) | UB64(ch); |
| 961 | bitsleft += 6; |
| 962 | s++; |
| 963 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 964 | } |
| 965 | } |
| 966 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 967 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 968 | s++; |
| 969 | if (s < e && *s == '-') { |
| 970 | s++; |
| 971 | *p++ = '+'; |
| 972 | } else |
| 973 | { |
| 974 | inShift = 1; |
| 975 | bitsleft = 0; |
| 976 | } |
| 977 | } |
| 978 | else if (SPECIAL(ch,0,0)) { |
| 979 | errmsg = "unexpected special character"; |
| 980 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 981 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 982 | } |
| 983 | else { |
| 984 | *p++ = ch; |
| 985 | s++; |
| 986 | } |
| 987 | continue; |
| 988 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 989 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 990 | endinpos = s-starts; |
| 991 | if (unicode_decode_call_errorhandler( |
| 992 | errors, &errorHandler, |
| 993 | "utf7", errmsg, |
| 994 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 995 | (PyObject **)&unicode, &outpos, &p)) |
| 996 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | if (inShift) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1000 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1001 | endinpos = size; |
| 1002 | if (unicode_decode_call_errorhandler( |
| 1003 | errors, &errorHandler, |
| 1004 | "utf7", "unterminated shift sequence", |
| 1005 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1006 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1007 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1008 | if (s < e) |
| 1009 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1012 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1013 | goto onError; |
| 1014 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1015 | Py_XDECREF(errorHandler); |
| 1016 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1017 | return (PyObject *)unicode; |
| 1018 | |
| 1019 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1020 | Py_XDECREF(errorHandler); |
| 1021 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1022 | Py_DECREF(unicode); |
| 1023 | return NULL; |
| 1024 | } |
| 1025 | |
| 1026 | |
| 1027 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1028 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1029 | int encodeSetO, |
| 1030 | int encodeWhiteSpace, |
| 1031 | const char *errors) |
| 1032 | { |
| 1033 | PyObject *v; |
| 1034 | /* It might be possible to tighten this worst case */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1035 | Py_ssize_t cbAllocated = 5 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1036 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1037 | Py_ssize_t i = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1038 | unsigned int bitsleft = 0; |
| 1039 | unsigned long charsleft = 0; |
| 1040 | char * out; |
| 1041 | char * start; |
| 1042 | |
| 1043 | if (size == 0) |
| 1044 | return PyString_FromStringAndSize(NULL, 0); |
| 1045 | |
| 1046 | v = PyString_FromStringAndSize(NULL, cbAllocated); |
| 1047 | if (v == NULL) |
| 1048 | return NULL; |
| 1049 | |
| 1050 | start = out = PyString_AS_STRING(v); |
| 1051 | for (;i < size; ++i) { |
| 1052 | Py_UNICODE ch = s[i]; |
| 1053 | |
| 1054 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1055 | if (ch == '+') { |
| 1056 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1057 | *out++ = '-'; |
| 1058 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1059 | charsleft = ch; |
| 1060 | bitsleft = 16; |
| 1061 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1062 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1063 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1064 | } else { |
| 1065 | *out++ = (char) ch; |
| 1066 | } |
| 1067 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1068 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1069 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1070 | charsleft = 0; |
| 1071 | bitsleft = 0; |
| 1072 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1073 | so no '-' is required, except if the character is itself a '-' */ |
| 1074 | if (B64CHAR(ch) || ch == '-') { |
| 1075 | *out++ = '-'; |
| 1076 | } |
| 1077 | inShift = 0; |
| 1078 | *out++ = (char) ch; |
| 1079 | } else { |
| 1080 | bitsleft += 16; |
| 1081 | charsleft = (charsleft << 16) | ch; |
| 1082 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1083 | |
| 1084 | /* If the next character is special then we dont' need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1085 | 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] | 1086 | or '-' then the shift sequence will be terminated implicitly and we |
| 1087 | don't have to insert a '-'. */ |
| 1088 | |
| 1089 | if (bitsleft == 0) { |
| 1090 | if (i + 1 < size) { |
| 1091 | Py_UNICODE ch2 = s[i+1]; |
| 1092 | |
| 1093 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1094 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1095 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1096 | *out++ = '-'; |
| 1097 | inShift = 0; |
| 1098 | } else { |
| 1099 | inShift = 0; |
| 1100 | } |
| 1101 | |
| 1102 | } |
| 1103 | else { |
| 1104 | *out++ = '-'; |
| 1105 | inShift = 0; |
| 1106 | } |
| 1107 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1108 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1109 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1110 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1111 | if (bitsleft) { |
| 1112 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1113 | *out++ = '-'; |
| 1114 | } |
| 1115 | |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 1116 | _PyString_Resize(&v, out - start); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1117 | return v; |
| 1118 | } |
| 1119 | |
| 1120 | #undef SPECIAL |
| 1121 | #undef B64 |
| 1122 | #undef B64CHAR |
| 1123 | #undef UB64 |
| 1124 | #undef ENCODE |
| 1125 | #undef DECODE |
| 1126 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1127 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1128 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1129 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1130 | char utf8_code_length[256] = { |
| 1131 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1132 | illegal prefix. see RFC 2279 for details */ |
| 1133 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1134 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1135 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1136 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1137 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1138 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1139 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1140 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1141 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1142 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1143 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1144 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1145 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1146 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1147 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1148 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1149 | }; |
| 1150 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1151 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1152 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1153 | const char *errors) |
| 1154 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1155 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 1156 | } |
| 1157 | |
| 1158 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1159 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1160 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1161 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1162 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1163 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1164 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1165 | Py_ssize_t startinpos; |
| 1166 | Py_ssize_t endinpos; |
| 1167 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1168 | const char *e; |
| 1169 | PyUnicodeObject *unicode; |
| 1170 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1171 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1172 | PyObject *errorHandler = NULL; |
| 1173 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1174 | |
| 1175 | /* Note: size will always be longer than the resulting Unicode |
| 1176 | character count */ |
| 1177 | unicode = _PyUnicode_New(size); |
| 1178 | if (!unicode) |
| 1179 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1180 | if (size == 0) { |
| 1181 | if (consumed) |
| 1182 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1183 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1184 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1185 | |
| 1186 | /* Unpack UTF-8 encoded data */ |
| 1187 | p = unicode->str; |
| 1188 | e = s + size; |
| 1189 | |
| 1190 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1191 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1192 | |
| 1193 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1194 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1195 | s++; |
| 1196 | continue; |
| 1197 | } |
| 1198 | |
| 1199 | n = utf8_code_length[ch]; |
| 1200 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1201 | if (s + n > e) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1202 | if (consumed) |
| 1203 | break; |
| 1204 | else { |
| 1205 | errmsg = "unexpected end of data"; |
| 1206 | startinpos = s-starts; |
| 1207 | endinpos = size; |
| 1208 | goto utf8Error; |
| 1209 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1210 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1211 | |
| 1212 | switch (n) { |
| 1213 | |
| 1214 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1215 | errmsg = "unexpected code byte"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1216 | startinpos = s-starts; |
| 1217 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1218 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1219 | |
| 1220 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1221 | errmsg = "internal error"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1222 | startinpos = s-starts; |
| 1223 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1224 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1225 | |
| 1226 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1227 | if ((s[1] & 0xc0) != 0x80) { |
| 1228 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1229 | startinpos = s-starts; |
| 1230 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1231 | goto utf8Error; |
| 1232 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1233 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1234 | if (ch < 0x80) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1235 | startinpos = s-starts; |
| 1236 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1237 | errmsg = "illegal encoding"; |
| 1238 | goto utf8Error; |
| 1239 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1240 | else |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1241 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1242 | break; |
| 1243 | |
| 1244 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1245 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1246 | (s[2] & 0xc0) != 0x80) { |
| 1247 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1248 | startinpos = s-starts; |
| 1249 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1250 | goto utf8Error; |
| 1251 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1252 | 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] | 1253 | if (ch < 0x0800) { |
| 1254 | /* Note: UTF-8 encodings of surrogates are considered |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1255 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1256 | |
| 1257 | XXX For wide builds (UCS-4) we should probably try |
| 1258 | to recombine the surrogates into a single code |
| 1259 | unit. |
| 1260 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1261 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1262 | startinpos = s-starts; |
| 1263 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1264 | goto utf8Error; |
| 1265 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1266 | else |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1267 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1268 | break; |
| 1269 | |
| 1270 | case 4: |
| 1271 | if ((s[1] & 0xc0) != 0x80 || |
| 1272 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1273 | (s[3] & 0xc0) != 0x80) { |
| 1274 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1275 | startinpos = s-starts; |
| 1276 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1277 | goto utf8Error; |
| 1278 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1279 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 1280 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 1281 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1282 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1283 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1284 | || (ch > 0x10ffff)) /* maximum value allowed for |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1285 | UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1286 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1287 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1288 | startinpos = s-starts; |
| 1289 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1290 | goto utf8Error; |
| 1291 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1292 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1293 | *p++ = (Py_UNICODE)ch; |
| 1294 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1295 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1296 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1297 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 1298 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1299 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1300 | /* high surrogate = top 10 bits added to D800 */ |
| 1301 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1302 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1303 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1304 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1305 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1306 | break; |
| 1307 | |
| 1308 | default: |
| 1309 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1310 | errmsg = "unsupported Unicode code range"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1311 | startinpos = s-starts; |
| 1312 | endinpos = startinpos+n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1313 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1314 | } |
| 1315 | s += n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1316 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1317 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1318 | utf8Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1319 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1320 | if (unicode_decode_call_errorhandler( |
| 1321 | errors, &errorHandler, |
| 1322 | "utf8", errmsg, |
| 1323 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1324 | (PyObject **)&unicode, &outpos, &p)) |
| 1325 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1326 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1327 | if (consumed) |
| 1328 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1329 | |
| 1330 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1331 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1332 | goto onError; |
| 1333 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1334 | Py_XDECREF(errorHandler); |
| 1335 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1336 | return (PyObject *)unicode; |
| 1337 | |
| 1338 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1339 | Py_XDECREF(errorHandler); |
| 1340 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1341 | Py_DECREF(unicode); |
| 1342 | return NULL; |
| 1343 | } |
| 1344 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1345 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 1346 | and allocate exactly as much space needed at the end. Else allocate the |
| 1347 | maximum possible needed (4 result bytes per Unicode character), and return |
| 1348 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1349 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1350 | PyObject * |
| 1351 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1352 | Py_ssize_t size, |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1353 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1354 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1355 | #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] | 1356 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1357 | Py_ssize_t i; /* index into s of next input byte */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1358 | PyObject *v; /* result string object */ |
| 1359 | char *p; /* next free byte in output buffer */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1360 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1361 | int nneeded; /* number of result bytes needed */ |
| 1362 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1363 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1364 | assert(s != NULL); |
| 1365 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1366 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1367 | if (size <= MAX_SHORT_UNICHARS) { |
| 1368 | /* Write into the stack buffer; nallocated can't overflow. |
| 1369 | * At the end, we'll allocate exactly as much heap space as it |
| 1370 | * turns out we need. |
| 1371 | */ |
| 1372 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
| 1373 | v = NULL; /* will allocate after we're done */ |
| 1374 | p = stackbuf; |
| 1375 | } |
| 1376 | else { |
| 1377 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 1378 | nallocated = size * 4; |
| 1379 | if (nallocated / 4 != size) /* overflow! */ |
| 1380 | return PyErr_NoMemory(); |
| 1381 | v = PyString_FromStringAndSize(NULL, nallocated); |
| 1382 | if (v == NULL) |
| 1383 | return NULL; |
| 1384 | p = PyString_AS_STRING(v); |
| 1385 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1386 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1387 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1388 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1389 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1390 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1391 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1392 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1393 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1394 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1395 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 1396 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 1397 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1398 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1399 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1400 | /* Encode UCS2 Unicode ordinals */ |
| 1401 | if (ch < 0x10000) { |
| 1402 | /* Special case: check for high surrogate */ |
| 1403 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 1404 | Py_UCS4 ch2 = s[i]; |
| 1405 | /* Check for low surrogate and combine the two to |
| 1406 | form a UCS4 value */ |
| 1407 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1408 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1409 | i++; |
| 1410 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1411 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1412 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1413 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1414 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1415 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1416 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1417 | continue; |
| 1418 | } |
| 1419 | encodeUCS4: |
| 1420 | /* Encode UCS4 Unicode ordinals */ |
| 1421 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 1422 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 1423 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1424 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1425 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1426 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 1427 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1428 | if (v == NULL) { |
| 1429 | /* This was stack allocated. */ |
| 1430 | nneeded = Py_SAFE_DOWNCAST(p - stackbuf, long, int); |
| 1431 | assert(nneeded <= nallocated); |
| 1432 | v = PyString_FromStringAndSize(stackbuf, nneeded); |
| 1433 | } |
| 1434 | else { |
| 1435 | /* Cut back to size actually needed. */ |
| 1436 | nneeded = Py_SAFE_DOWNCAST(p - PyString_AS_STRING(v), long, int); |
| 1437 | assert(nneeded <= nallocated); |
| 1438 | _PyString_Resize(&v, nneeded); |
| 1439 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1440 | return v; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1441 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1442 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1445 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 1446 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1447 | if (!PyUnicode_Check(unicode)) { |
| 1448 | PyErr_BadArgument(); |
| 1449 | return NULL; |
| 1450 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 1451 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1452 | PyUnicode_GET_SIZE(unicode), |
| 1453 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 1457 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1458 | PyObject * |
| 1459 | PyUnicode_DecodeUTF16(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1460 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1461 | const char *errors, |
| 1462 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1463 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1464 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 1465 | } |
| 1466 | |
| 1467 | PyObject * |
| 1468 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1469 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1470 | const char *errors, |
| 1471 | int *byteorder, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1472 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1473 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1474 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1475 | Py_ssize_t startinpos; |
| 1476 | Py_ssize_t endinpos; |
| 1477 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1478 | PyUnicodeObject *unicode; |
| 1479 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1480 | const unsigned char *q, *e; |
| 1481 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1482 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1483 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 1484 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1485 | int ihi = 1, ilo = 0; |
| 1486 | #else |
| 1487 | int ihi = 0, ilo = 1; |
| 1488 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1489 | PyObject *errorHandler = NULL; |
| 1490 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1491 | |
| 1492 | /* Note: size will always be longer than the resulting Unicode |
| 1493 | character count */ |
| 1494 | unicode = _PyUnicode_New(size); |
| 1495 | if (!unicode) |
| 1496 | return NULL; |
| 1497 | if (size == 0) |
| 1498 | return (PyObject *)unicode; |
| 1499 | |
| 1500 | /* Unpack UTF-16 encoded data */ |
| 1501 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1502 | q = (unsigned char *)s; |
| 1503 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1504 | |
| 1505 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1506 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1507 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1508 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 1509 | byte order setting accordingly. In native mode, the leading BOM |
| 1510 | mark is skipped, in all other modes, it is copied to the output |
| 1511 | stream as-is (giving a ZWNBSP character). */ |
| 1512 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1513 | if (size >= 2) { |
| 1514 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1515 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1516 | if (bom == 0xFEFF) { |
| 1517 | q += 2; |
| 1518 | bo = -1; |
| 1519 | } |
| 1520 | else if (bom == 0xFFFE) { |
| 1521 | q += 2; |
| 1522 | bo = 1; |
| 1523 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1524 | #else |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1525 | if (bom == 0xFEFF) { |
| 1526 | q += 2; |
| 1527 | bo = 1; |
| 1528 | } |
| 1529 | else if (bom == 0xFFFE) { |
| 1530 | q += 2; |
| 1531 | bo = -1; |
| 1532 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1533 | #endif |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1534 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1535 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1536 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1537 | if (bo == -1) { |
| 1538 | /* force LE */ |
| 1539 | ihi = 1; |
| 1540 | ilo = 0; |
| 1541 | } |
| 1542 | else if (bo == 1) { |
| 1543 | /* force BE */ |
| 1544 | ihi = 0; |
| 1545 | ilo = 1; |
| 1546 | } |
| 1547 | |
| 1548 | while (q < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1549 | Py_UNICODE ch; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1550 | /* remaining bytes at the end? (size should be even) */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1551 | if (e-q<2) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1552 | if (consumed) |
| 1553 | break; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1554 | errmsg = "truncated data"; |
| 1555 | startinpos = ((const char *)q)-starts; |
| 1556 | endinpos = ((const char *)e)-starts; |
| 1557 | goto utf16Error; |
| 1558 | /* The remaining input chars are ignored if the callback |
| 1559 | chooses to skip the input */ |
| 1560 | } |
| 1561 | ch = (q[ihi] << 8) | q[ilo]; |
| 1562 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1563 | q += 2; |
| 1564 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1565 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 1566 | *p++ = ch; |
| 1567 | continue; |
| 1568 | } |
| 1569 | |
| 1570 | /* UTF-16 code pair: */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1571 | if (q >= e) { |
| 1572 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1573 | startinpos = (((const char *)q)-2)-starts; |
| 1574 | endinpos = ((const char *)e)-starts; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1575 | goto utf16Error; |
| 1576 | } |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 1577 | if (0xD800 <= ch && ch <= 0xDBFF) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1578 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 1579 | q += 2; |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 1580 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1581 | #ifndef Py_UNICODE_WIDE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1582 | *p++ = ch; |
| 1583 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1584 | #else |
| 1585 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1586 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1587 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1588 | } |
| 1589 | else { |
| 1590 | errmsg = "illegal UTF-16 surrogate"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1591 | startinpos = (((const char *)q)-4)-starts; |
| 1592 | endinpos = startinpos+2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1593 | goto utf16Error; |
| 1594 | } |
| 1595 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1596 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1597 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1598 | startinpos = (((const char *)q)-2)-starts; |
| 1599 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1600 | /* Fall through to report the error */ |
| 1601 | |
| 1602 | utf16Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1603 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1604 | if (unicode_decode_call_errorhandler( |
| 1605 | errors, &errorHandler, |
| 1606 | "utf16", errmsg, |
| 1607 | starts, size, &startinpos, &endinpos, &exc, (const char **)&q, |
| 1608 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1609 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | if (byteorder) |
| 1613 | *byteorder = bo; |
| 1614 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1615 | if (consumed) |
| 1616 | *consumed = (const char *)q-starts; |
| 1617 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1618 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1619 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1620 | goto onError; |
| 1621 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1622 | Py_XDECREF(errorHandler); |
| 1623 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1624 | return (PyObject *)unicode; |
| 1625 | |
| 1626 | onError: |
| 1627 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1628 | Py_XDECREF(errorHandler); |
| 1629 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1630 | return NULL; |
| 1631 | } |
| 1632 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1633 | PyObject * |
| 1634 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1635 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1636 | const char *errors, |
| 1637 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1638 | { |
| 1639 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1640 | unsigned char *p; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1641 | #ifdef Py_UNICODE_WIDE |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1642 | int i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1643 | #else |
| 1644 | const int pairs = 0; |
| 1645 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1646 | /* Offsets from p for storing byte pairs in the right order. */ |
| 1647 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1648 | int ihi = 1, ilo = 0; |
| 1649 | #else |
| 1650 | int ihi = 0, ilo = 1; |
| 1651 | #endif |
| 1652 | |
| 1653 | #define STORECHAR(CH) \ |
| 1654 | do { \ |
| 1655 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 1656 | p[ilo] = (CH) & 0xff; \ |
| 1657 | p += 2; \ |
| 1658 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1659 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1660 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1661 | for (i = pairs = 0; i < size; i++) |
| 1662 | if (s[i] >= 0x10000) |
| 1663 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1664 | #endif |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1665 | v = PyString_FromStringAndSize(NULL, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1666 | 2 * (size + pairs + (byteorder == 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1667 | if (v == NULL) |
| 1668 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1669 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1670 | p = (unsigned char *)PyString_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1671 | if (byteorder == 0) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1672 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 1673 | if (size == 0) |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 1674 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1675 | |
| 1676 | if (byteorder == -1) { |
| 1677 | /* force LE */ |
| 1678 | ihi = 1; |
| 1679 | ilo = 0; |
| 1680 | } |
| 1681 | else if (byteorder == 1) { |
| 1682 | /* force BE */ |
| 1683 | ihi = 0; |
| 1684 | ilo = 1; |
| 1685 | } |
| 1686 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1687 | while (size-- > 0) { |
| 1688 | Py_UNICODE ch = *s++; |
| 1689 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1690 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1691 | if (ch >= 0x10000) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1692 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 1693 | ch = 0xD800 | ((ch-0x10000) >> 10); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1694 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1695 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1696 | STORECHAR(ch); |
| 1697 | if (ch2) |
| 1698 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1699 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1700 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1701 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
| 1704 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 1705 | { |
| 1706 | if (!PyUnicode_Check(unicode)) { |
| 1707 | PyErr_BadArgument(); |
| 1708 | return NULL; |
| 1709 | } |
| 1710 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 1711 | PyUnicode_GET_SIZE(unicode), |
| 1712 | NULL, |
| 1713 | 0); |
| 1714 | } |
| 1715 | |
| 1716 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 1717 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1718 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 1719 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1720 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1721 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1722 | const char *errors) |
| 1723 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1724 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1725 | Py_ssize_t startinpos; |
| 1726 | Py_ssize_t endinpos; |
| 1727 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1728 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1729 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1730 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1731 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1732 | char* message; |
| 1733 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1734 | PyObject *errorHandler = NULL; |
| 1735 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1736 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1737 | /* Escaped strings will always be longer than the resulting |
| 1738 | 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] | 1739 | length after conversion to the true value. |
| 1740 | (but if the error callback returns a long replacement string |
| 1741 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1742 | v = _PyUnicode_New(size); |
| 1743 | if (v == NULL) |
| 1744 | goto onError; |
| 1745 | if (size == 0) |
| 1746 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1747 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1748 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1749 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1750 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1751 | while (s < end) { |
| 1752 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 1753 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1754 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1755 | |
| 1756 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 1757 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1758 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1759 | continue; |
| 1760 | } |
| 1761 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1762 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1763 | /* \ - Escapes */ |
| 1764 | s++; |
| 1765 | switch (*s++) { |
| 1766 | |
| 1767 | /* \x escapes */ |
| 1768 | case '\n': break; |
| 1769 | case '\\': *p++ = '\\'; break; |
| 1770 | case '\'': *p++ = '\''; break; |
| 1771 | case '\"': *p++ = '\"'; break; |
| 1772 | case 'b': *p++ = '\b'; break; |
| 1773 | case 'f': *p++ = '\014'; break; /* FF */ |
| 1774 | case 't': *p++ = '\t'; break; |
| 1775 | case 'n': *p++ = '\n'; break; |
| 1776 | case 'r': *p++ = '\r'; break; |
| 1777 | case 'v': *p++ = '\013'; break; /* VT */ |
| 1778 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 1779 | |
| 1780 | /* \OOO (octal) escapes */ |
| 1781 | case '0': case '1': case '2': case '3': |
| 1782 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1783 | x = s[-1] - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1784 | if ('0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1785 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1786 | if ('0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1787 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1788 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1789 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1790 | break; |
| 1791 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1792 | /* hex escapes */ |
| 1793 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1794 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1795 | digits = 2; |
| 1796 | message = "truncated \\xXX escape"; |
| 1797 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1798 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1799 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1800 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1801 | digits = 4; |
| 1802 | message = "truncated \\uXXXX escape"; |
| 1803 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1804 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1805 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1806 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1807 | digits = 8; |
| 1808 | message = "truncated \\UXXXXXXXX escape"; |
| 1809 | hexescape: |
| 1810 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1811 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1812 | if (s+digits>end) { |
| 1813 | endinpos = size; |
| 1814 | if (unicode_decode_call_errorhandler( |
| 1815 | errors, &errorHandler, |
| 1816 | "unicodeescape", "end of string in escape sequence", |
| 1817 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1818 | (PyObject **)&v, &outpos, &p)) |
| 1819 | goto onError; |
| 1820 | goto nextByte; |
| 1821 | } |
| 1822 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1823 | c = (unsigned char) s[i]; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1824 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1825 | endinpos = (s+i+1)-starts; |
| 1826 | if (unicode_decode_call_errorhandler( |
| 1827 | errors, &errorHandler, |
| 1828 | "unicodeescape", message, |
| 1829 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1830 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1831 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1832 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1833 | } |
| 1834 | chr = (chr<<4) & ~0xF; |
| 1835 | if (c >= '0' && c <= '9') |
| 1836 | chr += c - '0'; |
| 1837 | else if (c >= 'a' && c <= 'f') |
| 1838 | chr += 10 + c - 'a'; |
| 1839 | else |
| 1840 | chr += 10 + c - 'A'; |
| 1841 | } |
| 1842 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 1843 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1844 | /* _decoding_error will have already written into the |
| 1845 | target buffer. */ |
| 1846 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1847 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1848 | /* when we get here, chr is a 32-bit unicode character */ |
| 1849 | if (chr <= 0xffff) |
| 1850 | /* UCS-2 character */ |
| 1851 | *p++ = (Py_UNICODE) chr; |
| 1852 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1853 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1854 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1855 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1856 | *p++ = chr; |
| 1857 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1858 | chr -= 0x10000L; |
| 1859 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1860 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1861 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1862 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1863 | endinpos = s-starts; |
| 1864 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1865 | if (unicode_decode_call_errorhandler( |
| 1866 | errors, &errorHandler, |
| 1867 | "unicodeescape", "illegal Unicode character", |
| 1868 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1869 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1870 | goto onError; |
| 1871 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1872 | break; |
| 1873 | |
| 1874 | /* \N{name} */ |
| 1875 | case 'N': |
| 1876 | message = "malformed \\N character escape"; |
| 1877 | if (ucnhash_CAPI == NULL) { |
| 1878 | /* load the unicode data module */ |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1879 | PyObject *m, *api; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1880 | m = PyImport_ImportModule("unicodedata"); |
| 1881 | if (m == NULL) |
| 1882 | goto ucnhashError; |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1883 | api = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1884 | Py_DECREF(m); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1885 | if (api == NULL) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1886 | goto ucnhashError; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 1887 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1888 | Py_DECREF(api); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1889 | if (ucnhash_CAPI == NULL) |
| 1890 | goto ucnhashError; |
| 1891 | } |
| 1892 | if (*s == '{') { |
| 1893 | const char *start = s+1; |
| 1894 | /* look for the closing brace */ |
| 1895 | while (*s != '}' && s < end) |
| 1896 | s++; |
| 1897 | if (s > start && s < end && *s == '}') { |
| 1898 | /* found a name. look it up in the unicode database */ |
| 1899 | message = "unknown Unicode character name"; |
| 1900 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 1901 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1902 | goto store; |
| 1903 | } |
| 1904 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1905 | endinpos = s-starts; |
| 1906 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1907 | if (unicode_decode_call_errorhandler( |
| 1908 | errors, &errorHandler, |
| 1909 | "unicodeescape", message, |
| 1910 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1911 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1912 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1913 | break; |
| 1914 | |
| 1915 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1916 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1917 | message = "\\ at end of string"; |
| 1918 | s--; |
| 1919 | endinpos = s-starts; |
| 1920 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1921 | if (unicode_decode_call_errorhandler( |
| 1922 | errors, &errorHandler, |
| 1923 | "unicodeescape", message, |
| 1924 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1925 | (PyObject **)&v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1926 | goto onError; |
| 1927 | } |
| 1928 | else { |
| 1929 | *p++ = '\\'; |
| 1930 | *p++ = (unsigned char)s[-1]; |
| 1931 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1932 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1933 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1934 | nextByte: |
| 1935 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1936 | } |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1937 | if (_PyUnicode_Resize(&v, (int)(p - PyUnicode_AS_UNICODE(v))) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1938 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 1939 | Py_XDECREF(errorHandler); |
| 1940 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1941 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1942 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1943 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1944 | PyErr_SetString( |
| 1945 | PyExc_UnicodeError, |
| 1946 | "\\N escapes not supported (can't load unicodedata module)" |
| 1947 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1948 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1949 | Py_XDECREF(errorHandler); |
| 1950 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 1951 | return NULL; |
| 1952 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1953 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1954 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1955 | Py_XDECREF(errorHandler); |
| 1956 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1957 | return NULL; |
| 1958 | } |
| 1959 | |
| 1960 | /* Return a Unicode-Escape string version of the Unicode object. |
| 1961 | |
| 1962 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 1963 | appropriate. |
| 1964 | |
| 1965 | */ |
| 1966 | |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 1967 | static const Py_UNICODE *findchar(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1968 | Py_ssize_t size, |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 1969 | Py_UNICODE ch); |
| 1970 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1971 | static |
| 1972 | PyObject *unicodeescape_string(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1973 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1974 | int quotes) |
| 1975 | { |
| 1976 | PyObject *repr; |
| 1977 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1978 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 1979 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1980 | |
| 1981 | repr = PyString_FromStringAndSize(NULL, 2 + 6*size + 1); |
| 1982 | if (repr == NULL) |
| 1983 | return NULL; |
| 1984 | |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1985 | p = PyString_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1986 | |
| 1987 | if (quotes) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1988 | *p++ = 'u'; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1989 | *p++ = (findchar(s, size, '\'') && |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1990 | !findchar(s, size, '"')) ? '"' : '\''; |
| 1991 | } |
| 1992 | while (size-- > 0) { |
| 1993 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1994 | |
Hye-Shik Chang | 835b243 | 2005-12-17 04:38:31 +0000 | [diff] [blame] | 1995 | /* Escape quotes and backslashes */ |
| 1996 | if ((quotes && |
| 1997 | ch == (Py_UNICODE) PyString_AS_STRING(repr)[1]) || ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1998 | *p++ = '\\'; |
| 1999 | *p++ = (char) ch; |
Guido van Rossum | ad9744a | 2001-09-21 15:38:17 +0000 | [diff] [blame] | 2000 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2001 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2002 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 2003 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2004 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 2005 | else if (ch >= 0x10000) { |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2006 | int offset = p - PyString_AS_STRING(repr); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2007 | |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2008 | /* Resize the string if necessary */ |
| 2009 | if (offset + 12 > PyString_GET_SIZE(repr)) { |
| 2010 | if (_PyString_Resize(&repr, PyString_GET_SIZE(repr) + 100)) |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2011 | return NULL; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2012 | p = PyString_AS_STRING(repr) + offset; |
| 2013 | } |
| 2014 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2015 | *p++ = '\\'; |
| 2016 | *p++ = 'U'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2017 | *p++ = hexdigit[(ch >> 28) & 0x0000000F]; |
| 2018 | *p++ = hexdigit[(ch >> 24) & 0x0000000F]; |
| 2019 | *p++ = hexdigit[(ch >> 20) & 0x0000000F]; |
| 2020 | *p++ = hexdigit[(ch >> 16) & 0x0000000F]; |
| 2021 | *p++ = hexdigit[(ch >> 12) & 0x0000000F]; |
| 2022 | *p++ = hexdigit[(ch >> 8) & 0x0000000F]; |
| 2023 | *p++ = hexdigit[(ch >> 4) & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2024 | *p++ = hexdigit[ch & 0x0000000F]; |
| 2025 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2026 | } |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 2027 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2028 | /* Map UTF-16 surrogate pairs to Unicode \UXXXXXXXX escapes */ |
| 2029 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 2030 | Py_UNICODE ch2; |
| 2031 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2032 | |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2033 | ch2 = *s++; |
| 2034 | size--; |
| 2035 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 2036 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 2037 | *p++ = '\\'; |
| 2038 | *p++ = 'U'; |
| 2039 | *p++ = hexdigit[(ucs >> 28) & 0x0000000F]; |
| 2040 | *p++ = hexdigit[(ucs >> 24) & 0x0000000F]; |
| 2041 | *p++ = hexdigit[(ucs >> 20) & 0x0000000F]; |
| 2042 | *p++ = hexdigit[(ucs >> 16) & 0x0000000F]; |
| 2043 | *p++ = hexdigit[(ucs >> 12) & 0x0000000F]; |
| 2044 | *p++ = hexdigit[(ucs >> 8) & 0x0000000F]; |
| 2045 | *p++ = hexdigit[(ucs >> 4) & 0x0000000F]; |
| 2046 | *p++ = hexdigit[ucs & 0x0000000F]; |
| 2047 | continue; |
| 2048 | } |
| 2049 | /* Fall through: isolated surrogates are copied as-is */ |
| 2050 | s--; |
| 2051 | size++; |
| 2052 | } |
| 2053 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2054 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2055 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2056 | *p++ = '\\'; |
| 2057 | *p++ = 'u'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2058 | *p++ = hexdigit[(ch >> 12) & 0x000F]; |
| 2059 | *p++ = hexdigit[(ch >> 8) & 0x000F]; |
| 2060 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2061 | *p++ = hexdigit[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2062 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2063 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2064 | /* Map special whitespace to '\t', \n', '\r' */ |
| 2065 | else if (ch == '\t') { |
| 2066 | *p++ = '\\'; |
| 2067 | *p++ = 't'; |
| 2068 | } |
| 2069 | else if (ch == '\n') { |
| 2070 | *p++ = '\\'; |
| 2071 | *p++ = 'n'; |
| 2072 | } |
| 2073 | else if (ch == '\r') { |
| 2074 | *p++ = '\\'; |
| 2075 | *p++ = 'r'; |
| 2076 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2077 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2078 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 2079 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2080 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2081 | *p++ = 'x'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2082 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2083 | *p++ = hexdigit[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2084 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2085 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2086 | /* Copy everything else as-is */ |
| 2087 | else |
| 2088 | *p++ = (char) ch; |
| 2089 | } |
| 2090 | if (quotes) |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2091 | *p++ = PyString_AS_STRING(repr)[1]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2092 | |
| 2093 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2094 | _PyString_Resize(&repr, p - PyString_AS_STRING(repr)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2095 | return repr; |
| 2096 | } |
| 2097 | |
| 2098 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2099 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2100 | { |
| 2101 | return unicodeescape_string(s, size, 0); |
| 2102 | } |
| 2103 | |
| 2104 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 2105 | { |
| 2106 | if (!PyUnicode_Check(unicode)) { |
| 2107 | PyErr_BadArgument(); |
| 2108 | return NULL; |
| 2109 | } |
| 2110 | return PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2111 | PyUnicode_GET_SIZE(unicode)); |
| 2112 | } |
| 2113 | |
| 2114 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 2115 | |
| 2116 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2117 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2118 | const char *errors) |
| 2119 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2120 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2121 | Py_ssize_t startinpos; |
| 2122 | Py_ssize_t endinpos; |
| 2123 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2124 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2125 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2126 | const char *end; |
| 2127 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2128 | PyObject *errorHandler = NULL; |
| 2129 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2130 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2131 | /* Escaped strings will always be longer than the resulting |
| 2132 | 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] | 2133 | length after conversion to the true value. (But decoding error |
| 2134 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2135 | v = _PyUnicode_New(size); |
| 2136 | if (v == NULL) |
| 2137 | goto onError; |
| 2138 | if (size == 0) |
| 2139 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2140 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2141 | end = s + size; |
| 2142 | while (s < end) { |
| 2143 | unsigned char c; |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 2144 | Py_UCS4 x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2145 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2146 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2147 | |
| 2148 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2149 | if (*s != '\\') { |
| 2150 | *p++ = (unsigned char)*s++; |
| 2151 | continue; |
| 2152 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2153 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2154 | |
| 2155 | /* \u-escapes are only interpreted iff the number of leading |
| 2156 | backslashes if odd */ |
| 2157 | bs = s; |
| 2158 | for (;s < end;) { |
| 2159 | if (*s != '\\') |
| 2160 | break; |
| 2161 | *p++ = (unsigned char)*s++; |
| 2162 | } |
| 2163 | if (((s - bs) & 1) == 0 || |
| 2164 | s >= end || |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2165 | (*s != 'u' && *s != 'U')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2166 | continue; |
| 2167 | } |
| 2168 | p--; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2169 | count = *s=='u' ? 4 : 8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2170 | s++; |
| 2171 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2172 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2173 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2174 | for (x = 0, i = 0; i < count; ++i, ++s) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2175 | c = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2176 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2177 | endinpos = s-starts; |
| 2178 | if (unicode_decode_call_errorhandler( |
| 2179 | errors, &errorHandler, |
| 2180 | "rawunicodeescape", "truncated \\uXXXX", |
| 2181 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2182 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2183 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2184 | goto nextByte; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2185 | } |
| 2186 | x = (x<<4) & ~0xF; |
| 2187 | if (c >= '0' && c <= '9') |
| 2188 | x += c - '0'; |
| 2189 | else if (c >= 'a' && c <= 'f') |
| 2190 | x += 10 + c - 'a'; |
| 2191 | else |
| 2192 | x += 10 + c - 'A'; |
| 2193 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2194 | #ifndef Py_UNICODE_WIDE |
| 2195 | if (x > 0x10000) { |
| 2196 | if (unicode_decode_call_errorhandler( |
| 2197 | errors, &errorHandler, |
| 2198 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
| 2199 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2200 | (PyObject **)&v, &outpos, &p)) |
| 2201 | goto onError; |
| 2202 | } |
| 2203 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2204 | *p++ = x; |
| 2205 | nextByte: |
| 2206 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2207 | } |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2208 | if (_PyUnicode_Resize(&v, (int)(p - PyUnicode_AS_UNICODE(v))) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2209 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2210 | Py_XDECREF(errorHandler); |
| 2211 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2212 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2213 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2214 | onError: |
| 2215 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2216 | Py_XDECREF(errorHandler); |
| 2217 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2218 | return NULL; |
| 2219 | } |
| 2220 | |
| 2221 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2222 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2223 | { |
| 2224 | PyObject *repr; |
| 2225 | char *p; |
| 2226 | char *q; |
| 2227 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2228 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2229 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2230 | #ifdef Py_UNICODE_WIDE |
| 2231 | repr = PyString_FromStringAndSize(NULL, 10 * size); |
| 2232 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2233 | repr = PyString_FromStringAndSize(NULL, 6 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2234 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2235 | if (repr == NULL) |
| 2236 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2237 | if (size == 0) |
| 2238 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2239 | |
| 2240 | p = q = PyString_AS_STRING(repr); |
| 2241 | while (size-- > 0) { |
| 2242 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2243 | #ifdef Py_UNICODE_WIDE |
| 2244 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2245 | if (ch >= 0x10000) { |
| 2246 | *p++ = '\\'; |
| 2247 | *p++ = 'U'; |
| 2248 | *p++ = hexdigit[(ch >> 28) & 0xf]; |
| 2249 | *p++ = hexdigit[(ch >> 24) & 0xf]; |
| 2250 | *p++ = hexdigit[(ch >> 20) & 0xf]; |
| 2251 | *p++ = hexdigit[(ch >> 16) & 0xf]; |
| 2252 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 2253 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 2254 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 2255 | *p++ = hexdigit[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2256 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2257 | else |
| 2258 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2259 | /* Map 16-bit characters to '\uxxxx' */ |
| 2260 | if (ch >= 256) { |
| 2261 | *p++ = '\\'; |
| 2262 | *p++ = 'u'; |
| 2263 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 2264 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 2265 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 2266 | *p++ = hexdigit[ch & 15]; |
| 2267 | } |
| 2268 | /* Copy everything else as-is */ |
| 2269 | else |
| 2270 | *p++ = (char) ch; |
| 2271 | } |
| 2272 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2273 | _PyString_Resize(&repr, p - q); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2274 | return repr; |
| 2275 | } |
| 2276 | |
| 2277 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 2278 | { |
| 2279 | if (!PyUnicode_Check(unicode)) { |
| 2280 | PyErr_BadArgument(); |
| 2281 | return NULL; |
| 2282 | } |
| 2283 | return PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2284 | PyUnicode_GET_SIZE(unicode)); |
| 2285 | } |
| 2286 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2287 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 2288 | |
| 2289 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2290 | Py_ssize_t size, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2291 | const char *errors) |
| 2292 | { |
| 2293 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2294 | Py_ssize_t startinpos; |
| 2295 | Py_ssize_t endinpos; |
| 2296 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2297 | PyUnicodeObject *v; |
| 2298 | Py_UNICODE *p; |
| 2299 | const char *end; |
| 2300 | const char *reason; |
| 2301 | PyObject *errorHandler = NULL; |
| 2302 | PyObject *exc = NULL; |
| 2303 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 2304 | #ifdef Py_UNICODE_WIDE |
| 2305 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 2306 | #endif |
| 2307 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2308 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 2309 | if (v == NULL) |
| 2310 | goto onError; |
| 2311 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
| 2312 | return (PyObject *)v; |
| 2313 | p = PyUnicode_AS_UNICODE(v); |
| 2314 | end = s + size; |
| 2315 | |
| 2316 | while (s < end) { |
| 2317 | *p = *(Py_UNICODE *)s; |
| 2318 | /* We have to sanity check the raw data, otherwise doom looms for |
| 2319 | some malformed UCS-4 data. */ |
| 2320 | if ( |
| 2321 | #ifdef Py_UNICODE_WIDE |
| 2322 | *p > unimax || *p < 0 || |
| 2323 | #endif |
| 2324 | end-s < Py_UNICODE_SIZE |
| 2325 | ) |
| 2326 | { |
| 2327 | startinpos = s - starts; |
| 2328 | if (end-s < Py_UNICODE_SIZE) { |
| 2329 | endinpos = end-starts; |
| 2330 | reason = "truncated input"; |
| 2331 | } |
| 2332 | else { |
| 2333 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 2334 | reason = "illegal code point (> 0x10FFFF)"; |
| 2335 | } |
| 2336 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 2337 | if (unicode_decode_call_errorhandler( |
| 2338 | errors, &errorHandler, |
| 2339 | "unicode_internal", reason, |
| 2340 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2341 | (PyObject **)&v, &outpos, &p)) { |
| 2342 | goto onError; |
| 2343 | } |
| 2344 | } |
| 2345 | else { |
| 2346 | p++; |
| 2347 | s += Py_UNICODE_SIZE; |
| 2348 | } |
| 2349 | } |
| 2350 | |
| 2351 | if (_PyUnicode_Resize(&v, (int)(p - PyUnicode_AS_UNICODE(v))) < 0) |
| 2352 | goto onError; |
| 2353 | Py_XDECREF(errorHandler); |
| 2354 | Py_XDECREF(exc); |
| 2355 | return (PyObject *)v; |
| 2356 | |
| 2357 | onError: |
| 2358 | Py_XDECREF(v); |
| 2359 | Py_XDECREF(errorHandler); |
| 2360 | Py_XDECREF(exc); |
| 2361 | return NULL; |
| 2362 | } |
| 2363 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2364 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 2365 | |
| 2366 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2367 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2368 | const char *errors) |
| 2369 | { |
| 2370 | PyUnicodeObject *v; |
| 2371 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2372 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2373 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2374 | if (size == 1) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 2375 | Py_UNICODE r = *(unsigned char*)s; |
| 2376 | return PyUnicode_FromUnicode(&r, 1); |
| 2377 | } |
| 2378 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2379 | v = _PyUnicode_New(size); |
| 2380 | if (v == NULL) |
| 2381 | goto onError; |
| 2382 | if (size == 0) |
| 2383 | return (PyObject *)v; |
| 2384 | p = PyUnicode_AS_UNICODE(v); |
| 2385 | while (size-- > 0) |
| 2386 | *p++ = (unsigned char)*s++; |
| 2387 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2388 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2389 | onError: |
| 2390 | Py_XDECREF(v); |
| 2391 | return NULL; |
| 2392 | } |
| 2393 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2394 | /* create or adjust a UnicodeEncodeError */ |
| 2395 | static void make_encode_exception(PyObject **exceptionObject, |
| 2396 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2397 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 2398 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2399 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2400 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2401 | if (*exceptionObject == NULL) { |
| 2402 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 2403 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2404 | } |
| 2405 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2406 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 2407 | goto onError; |
| 2408 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 2409 | goto onError; |
| 2410 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 2411 | goto onError; |
| 2412 | return; |
| 2413 | onError: |
| 2414 | Py_DECREF(*exceptionObject); |
| 2415 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2416 | } |
| 2417 | } |
| 2418 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2419 | /* raises a UnicodeEncodeError */ |
| 2420 | static void raise_encode_exception(PyObject **exceptionObject, |
| 2421 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2422 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 2423 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2424 | const char *reason) |
| 2425 | { |
| 2426 | make_encode_exception(exceptionObject, |
| 2427 | encoding, unicode, size, startpos, endpos, reason); |
| 2428 | if (*exceptionObject != NULL) |
| 2429 | PyCodec_StrictErrors(*exceptionObject); |
| 2430 | } |
| 2431 | |
| 2432 | /* error handling callback helper: |
| 2433 | build arguments, call the callback and check the arguments, |
| 2434 | put the result into newpos and return the replacement string, which |
| 2435 | has to be freed by the caller */ |
| 2436 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 2437 | PyObject **errorHandler, |
| 2438 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2439 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 2440 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 2441 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2442 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2443 | 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] | 2444 | |
| 2445 | PyObject *restuple; |
| 2446 | PyObject *resunicode; |
| 2447 | |
| 2448 | if (*errorHandler == NULL) { |
| 2449 | *errorHandler = PyCodec_LookupError(errors); |
| 2450 | if (*errorHandler == NULL) |
| 2451 | return NULL; |
| 2452 | } |
| 2453 | |
| 2454 | make_encode_exception(exceptionObject, |
| 2455 | encoding, unicode, size, startpos, endpos, reason); |
| 2456 | if (*exceptionObject == NULL) |
| 2457 | return NULL; |
| 2458 | |
| 2459 | restuple = PyObject_CallFunctionObjArgs( |
| 2460 | *errorHandler, *exceptionObject, NULL); |
| 2461 | if (restuple == NULL) |
| 2462 | return NULL; |
| 2463 | if (!PyTuple_Check(restuple)) { |
| 2464 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 2465 | Py_DECREF(restuple); |
| 2466 | return NULL; |
| 2467 | } |
| 2468 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 2469 | &resunicode, newpos)) { |
| 2470 | Py_DECREF(restuple); |
| 2471 | return NULL; |
| 2472 | } |
| 2473 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2474 | *newpos = size+*newpos; |
| 2475 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 2476 | 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] | 2477 | Py_DECREF(restuple); |
| 2478 | return NULL; |
| 2479 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2480 | Py_INCREF(resunicode); |
| 2481 | Py_DECREF(restuple); |
| 2482 | return resunicode; |
| 2483 | } |
| 2484 | |
| 2485 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2486 | Py_ssize_t size, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2487 | const char *errors, |
| 2488 | int limit) |
| 2489 | { |
| 2490 | /* output object */ |
| 2491 | PyObject *res; |
| 2492 | /* pointers to the beginning and end+1 of input */ |
| 2493 | const Py_UNICODE *startp = p; |
| 2494 | const Py_UNICODE *endp = p + size; |
| 2495 | /* pointer to the beginning of the unencodable characters */ |
| 2496 | /* const Py_UNICODE *badp = NULL; */ |
| 2497 | /* pointer into the output */ |
| 2498 | char *str; |
| 2499 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2500 | Py_ssize_t respos = 0; |
| 2501 | Py_ssize_t ressize; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2502 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 2503 | 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] | 2504 | PyObject *errorHandler = NULL; |
| 2505 | PyObject *exc = NULL; |
| 2506 | /* the following variable is used for caching string comparisons |
| 2507 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 2508 | int known_errorHandler = -1; |
| 2509 | |
| 2510 | /* allocate enough for a simple encoding without |
| 2511 | replacements, if we need more, we'll resize */ |
| 2512 | res = PyString_FromStringAndSize(NULL, size); |
| 2513 | if (res == NULL) |
| 2514 | goto onError; |
| 2515 | if (size == 0) |
| 2516 | return res; |
| 2517 | str = PyString_AS_STRING(res); |
| 2518 | ressize = size; |
| 2519 | |
| 2520 | while (p<endp) { |
| 2521 | Py_UNICODE c = *p; |
| 2522 | |
| 2523 | /* can we encode this? */ |
| 2524 | if (c<limit) { |
| 2525 | /* no overflow check, because we know that the space is enough */ |
| 2526 | *str++ = (char)c; |
| 2527 | ++p; |
| 2528 | } |
| 2529 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2530 | Py_ssize_t unicodepos = p-startp; |
| 2531 | Py_ssize_t requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2532 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2533 | Py_ssize_t repsize; |
| 2534 | Py_ssize_t newpos; |
| 2535 | Py_ssize_t respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2536 | Py_UNICODE *uni2; |
| 2537 | /* startpos for collecting unencodable chars */ |
| 2538 | const Py_UNICODE *collstart = p; |
| 2539 | const Py_UNICODE *collend = p; |
| 2540 | /* find all unecodable characters */ |
| 2541 | while ((collend < endp) && ((*collend)>=limit)) |
| 2542 | ++collend; |
| 2543 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 2544 | if (known_errorHandler==-1) { |
| 2545 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 2546 | known_errorHandler = 1; |
| 2547 | else if (!strcmp(errors, "replace")) |
| 2548 | known_errorHandler = 2; |
| 2549 | else if (!strcmp(errors, "ignore")) |
| 2550 | known_errorHandler = 3; |
| 2551 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 2552 | known_errorHandler = 4; |
| 2553 | else |
| 2554 | known_errorHandler = 0; |
| 2555 | } |
| 2556 | switch (known_errorHandler) { |
| 2557 | case 1: /* strict */ |
| 2558 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 2559 | goto onError; |
| 2560 | case 2: /* replace */ |
| 2561 | while (collstart++<collend) |
| 2562 | *str++ = '?'; /* fall through */ |
| 2563 | case 3: /* ignore */ |
| 2564 | p = collend; |
| 2565 | break; |
| 2566 | case 4: /* xmlcharrefreplace */ |
| 2567 | respos = str-PyString_AS_STRING(res); |
| 2568 | /* determine replacement size (temporarily (mis)uses p) */ |
| 2569 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 2570 | if (*p<10) |
| 2571 | repsize += 2+1+1; |
| 2572 | else if (*p<100) |
| 2573 | repsize += 2+2+1; |
| 2574 | else if (*p<1000) |
| 2575 | repsize += 2+3+1; |
| 2576 | else if (*p<10000) |
| 2577 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 2578 | #ifndef Py_UNICODE_WIDE |
| 2579 | else |
| 2580 | repsize += 2+5+1; |
| 2581 | #else |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2582 | else if (*p<100000) |
| 2583 | repsize += 2+5+1; |
| 2584 | else if (*p<1000000) |
| 2585 | repsize += 2+6+1; |
| 2586 | else |
| 2587 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2588 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2589 | } |
| 2590 | requiredsize = respos+repsize+(endp-collend); |
| 2591 | if (requiredsize > ressize) { |
| 2592 | if (requiredsize<2*ressize) |
| 2593 | requiredsize = 2*ressize; |
| 2594 | if (_PyString_Resize(&res, requiredsize)) |
| 2595 | goto onError; |
| 2596 | str = PyString_AS_STRING(res) + respos; |
| 2597 | ressize = requiredsize; |
| 2598 | } |
| 2599 | /* generate replacement (temporarily (mis)uses p) */ |
| 2600 | for (p = collstart; p < collend; ++p) { |
| 2601 | str += sprintf(str, "&#%d;", (int)*p); |
| 2602 | } |
| 2603 | p = collend; |
| 2604 | break; |
| 2605 | default: |
| 2606 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 2607 | encoding, reason, startp, size, &exc, |
| 2608 | collstart-startp, collend-startp, &newpos); |
| 2609 | if (repunicode == NULL) |
| 2610 | goto onError; |
| 2611 | /* need more space? (at least enough for what we |
| 2612 | have+the replacement+the rest of the string, so |
| 2613 | we won't have to check space for encodable characters) */ |
| 2614 | respos = str-PyString_AS_STRING(res); |
| 2615 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 2616 | requiredsize = respos+repsize+(endp-collend); |
| 2617 | if (requiredsize > ressize) { |
| 2618 | if (requiredsize<2*ressize) |
| 2619 | requiredsize = 2*ressize; |
| 2620 | if (_PyString_Resize(&res, requiredsize)) { |
| 2621 | Py_DECREF(repunicode); |
| 2622 | goto onError; |
| 2623 | } |
| 2624 | str = PyString_AS_STRING(res) + respos; |
| 2625 | ressize = requiredsize; |
| 2626 | } |
| 2627 | /* check if there is anything unencodable in the replacement |
| 2628 | and copy it to the output */ |
| 2629 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 2630 | c = *uni2; |
| 2631 | if (c >= limit) { |
| 2632 | raise_encode_exception(&exc, encoding, startp, size, |
| 2633 | unicodepos, unicodepos+1, reason); |
| 2634 | Py_DECREF(repunicode); |
| 2635 | goto onError; |
| 2636 | } |
| 2637 | *str = (char)c; |
| 2638 | } |
| 2639 | p = startp + newpos; |
| 2640 | Py_DECREF(repunicode); |
| 2641 | } |
| 2642 | } |
| 2643 | } |
| 2644 | /* Resize if we allocated to much */ |
| 2645 | respos = str-PyString_AS_STRING(res); |
| 2646 | if (respos<ressize) |
| 2647 | /* If this falls res will be NULL */ |
| 2648 | _PyString_Resize(&res, respos); |
| 2649 | Py_XDECREF(errorHandler); |
| 2650 | Py_XDECREF(exc); |
| 2651 | return res; |
| 2652 | |
| 2653 | onError: |
| 2654 | Py_XDECREF(res); |
| 2655 | Py_XDECREF(errorHandler); |
| 2656 | Py_XDECREF(exc); |
| 2657 | return NULL; |
| 2658 | } |
| 2659 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2660 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2661 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2662 | const char *errors) |
| 2663 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2664 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2665 | } |
| 2666 | |
| 2667 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 2668 | { |
| 2669 | if (!PyUnicode_Check(unicode)) { |
| 2670 | PyErr_BadArgument(); |
| 2671 | return NULL; |
| 2672 | } |
| 2673 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 2674 | PyUnicode_GET_SIZE(unicode), |
| 2675 | NULL); |
| 2676 | } |
| 2677 | |
| 2678 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 2679 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2680 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2681 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2682 | const char *errors) |
| 2683 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2684 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2685 | PyUnicodeObject *v; |
| 2686 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2687 | Py_ssize_t startinpos; |
| 2688 | Py_ssize_t endinpos; |
| 2689 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2690 | const char *e; |
| 2691 | PyObject *errorHandler = NULL; |
| 2692 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2693 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2694 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 2695 | if (size == 1 && *(unsigned char*)s < 128) { |
| 2696 | Py_UNICODE r = *(unsigned char*)s; |
| 2697 | return PyUnicode_FromUnicode(&r, 1); |
| 2698 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2699 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2700 | v = _PyUnicode_New(size); |
| 2701 | if (v == NULL) |
| 2702 | goto onError; |
| 2703 | if (size == 0) |
| 2704 | return (PyObject *)v; |
| 2705 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2706 | e = s + size; |
| 2707 | while (s < e) { |
| 2708 | register unsigned char c = (unsigned char)*s; |
| 2709 | if (c < 128) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2710 | *p++ = c; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2711 | ++s; |
| 2712 | } |
| 2713 | else { |
| 2714 | startinpos = s-starts; |
| 2715 | endinpos = startinpos + 1; |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 2716 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2717 | if (unicode_decode_call_errorhandler( |
| 2718 | errors, &errorHandler, |
| 2719 | "ascii", "ordinal not in range(128)", |
| 2720 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2721 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2722 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2723 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2724 | } |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2725 | if (p - PyUnicode_AS_UNICODE(v) < PyString_GET_SIZE(v)) |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2726 | if (_PyUnicode_Resize(&v, (int)(p - PyUnicode_AS_UNICODE(v))) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2727 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2728 | Py_XDECREF(errorHandler); |
| 2729 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2730 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2731 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2732 | onError: |
| 2733 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2734 | Py_XDECREF(errorHandler); |
| 2735 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2736 | return NULL; |
| 2737 | } |
| 2738 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2739 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2740 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2741 | const char *errors) |
| 2742 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2743 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2744 | } |
| 2745 | |
| 2746 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 2747 | { |
| 2748 | if (!PyUnicode_Check(unicode)) { |
| 2749 | PyErr_BadArgument(); |
| 2750 | return NULL; |
| 2751 | } |
| 2752 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 2753 | PyUnicode_GET_SIZE(unicode), |
| 2754 | NULL); |
| 2755 | } |
| 2756 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2757 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2758 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2759 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2760 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2761 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2762 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2763 | const char *errors) |
| 2764 | { |
| 2765 | PyUnicodeObject *v; |
| 2766 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2767 | DWORD usize; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2768 | |
| 2769 | /* First get the size of the result */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2770 | assert(size < INT_MAX); |
| 2771 | usize = MultiByteToWideChar(CP_ACP, 0, s, (int)size, NULL, 0); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 2772 | if (size > 0 && usize==0) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2773 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2774 | |
| 2775 | v = _PyUnicode_New(usize); |
| 2776 | if (v == NULL) |
| 2777 | return NULL; |
| 2778 | if (usize == 0) |
| 2779 | return (PyObject *)v; |
| 2780 | p = PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2781 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, (int)size, p, usize)) { |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2782 | Py_DECREF(v); |
| 2783 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2784 | } |
| 2785 | |
| 2786 | return (PyObject *)v; |
| 2787 | } |
| 2788 | |
| 2789 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2790 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2791 | const char *errors) |
| 2792 | { |
| 2793 | PyObject *repr; |
| 2794 | char *s; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 2795 | DWORD mbcssize; |
| 2796 | |
| 2797 | /* If there are no characters, bail now! */ |
| 2798 | if (size==0) |
| 2799 | return PyString_FromString(""); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2800 | |
| 2801 | /* First get the size of the result */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2802 | assert(size<INT_MAX); |
| 2803 | 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] | 2804 | if (mbcssize==0) |
| 2805 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2806 | |
| 2807 | repr = PyString_FromStringAndSize(NULL, mbcssize); |
| 2808 | if (repr == NULL) |
| 2809 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2810 | if (mbcssize == 0) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2811 | return repr; |
| 2812 | |
| 2813 | /* Do the conversion */ |
| 2814 | s = PyString_AS_STRING(repr); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2815 | assert(size < INT_MAX); |
| 2816 | 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] | 2817 | Py_DECREF(repr); |
| 2818 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2819 | } |
| 2820 | return repr; |
| 2821 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2822 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2823 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 2824 | { |
| 2825 | if (!PyUnicode_Check(unicode)) { |
| 2826 | PyErr_BadArgument(); |
| 2827 | return NULL; |
| 2828 | } |
| 2829 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2830 | PyUnicode_GET_SIZE(unicode), |
| 2831 | NULL); |
| 2832 | } |
| 2833 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2834 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2835 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2836 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 2837 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2838 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2839 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2840 | PyObject *mapping, |
| 2841 | const char *errors) |
| 2842 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2843 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2844 | Py_ssize_t startinpos; |
| 2845 | Py_ssize_t endinpos; |
| 2846 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2847 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2848 | PyUnicodeObject *v; |
| 2849 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2850 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2851 | PyObject *errorHandler = NULL; |
| 2852 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2853 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2854 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2855 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2856 | /* Default to Latin-1 */ |
| 2857 | if (mapping == NULL) |
| 2858 | return PyUnicode_DecodeLatin1(s, size, errors); |
| 2859 | |
| 2860 | v = _PyUnicode_New(size); |
| 2861 | if (v == NULL) |
| 2862 | goto onError; |
| 2863 | if (size == 0) |
| 2864 | return (PyObject *)v; |
| 2865 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2866 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2867 | if (PyUnicode_CheckExact(mapping)) { |
| 2868 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 2869 | maplen = PyUnicode_GET_SIZE(mapping); |
| 2870 | while (s < e) { |
| 2871 | unsigned char ch = *s; |
| 2872 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2873 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2874 | if (ch < maplen) |
| 2875 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2876 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2877 | if (x == 0xfffe) { |
| 2878 | /* undefined mapping */ |
| 2879 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2880 | startinpos = s-starts; |
| 2881 | endinpos = startinpos+1; |
| 2882 | if (unicode_decode_call_errorhandler( |
| 2883 | errors, &errorHandler, |
| 2884 | "charmap", "character maps to <undefined>", |
| 2885 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2886 | (PyObject **)&v, &outpos, &p)) { |
| 2887 | goto onError; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 2888 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2889 | continue; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 2890 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2891 | *p++ = x; |
| 2892 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2893 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2894 | } |
| 2895 | else { |
| 2896 | while (s < e) { |
| 2897 | unsigned char ch = *s; |
| 2898 | PyObject *w, *x; |
| 2899 | |
| 2900 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 2901 | w = PyInt_FromLong((long)ch); |
| 2902 | if (w == NULL) |
| 2903 | goto onError; |
| 2904 | x = PyObject_GetItem(mapping, w); |
| 2905 | Py_DECREF(w); |
| 2906 | if (x == NULL) { |
| 2907 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 2908 | /* No mapping found means: mapping is undefined. */ |
| 2909 | PyErr_Clear(); |
| 2910 | x = Py_None; |
| 2911 | Py_INCREF(x); |
| 2912 | } else |
| 2913 | goto onError; |
| 2914 | } |
| 2915 | |
| 2916 | /* Apply mapping */ |
| 2917 | if (PyInt_Check(x)) { |
| 2918 | long value = PyInt_AS_LONG(x); |
| 2919 | if (value < 0 || value > 65535) { |
| 2920 | PyErr_SetString(PyExc_TypeError, |
| 2921 | "character mapping must be in range(65536)"); |
| 2922 | Py_DECREF(x); |
| 2923 | goto onError; |
| 2924 | } |
| 2925 | *p++ = (Py_UNICODE)value; |
| 2926 | } |
| 2927 | else if (x == Py_None) { |
| 2928 | /* undefined mapping */ |
| 2929 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2930 | startinpos = s-starts; |
| 2931 | endinpos = startinpos+1; |
| 2932 | if (unicode_decode_call_errorhandler( |
| 2933 | errors, &errorHandler, |
| 2934 | "charmap", "character maps to <undefined>", |
| 2935 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2936 | (PyObject **)&v, &outpos, &p)) { |
| 2937 | Py_DECREF(x); |
| 2938 | goto onError; |
| 2939 | } |
Walter Dörwald | d4fff17 | 2005-11-28 22:15:56 +0000 | [diff] [blame] | 2940 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2941 | continue; |
| 2942 | } |
| 2943 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2944 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2945 | |
| 2946 | if (targetsize == 1) |
| 2947 | /* 1-1 mapping */ |
| 2948 | *p++ = *PyUnicode_AS_UNICODE(x); |
| 2949 | |
| 2950 | else if (targetsize > 1) { |
| 2951 | /* 1-n mapping */ |
| 2952 | if (targetsize > extrachars) { |
| 2953 | /* resize first */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2954 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 2955 | Py_ssize_t needed = (targetsize - extrachars) + \ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2956 | (targetsize << 2); |
| 2957 | extrachars += needed; |
| 2958 | if (_PyUnicode_Resize(&v, |
| 2959 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 2960 | Py_DECREF(x); |
| 2961 | goto onError; |
| 2962 | } |
| 2963 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 2964 | } |
| 2965 | Py_UNICODE_COPY(p, |
| 2966 | PyUnicode_AS_UNICODE(x), |
| 2967 | targetsize); |
| 2968 | p += targetsize; |
| 2969 | extrachars -= targetsize; |
| 2970 | } |
| 2971 | /* 1-0 mapping: skip the character */ |
| 2972 | } |
| 2973 | else { |
| 2974 | /* wrong return value */ |
| 2975 | PyErr_SetString(PyExc_TypeError, |
| 2976 | "character mapping must return integer, None or unicode"); |
| 2977 | Py_DECREF(x); |
| 2978 | goto onError; |
| 2979 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2980 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2981 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2982 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2983 | } |
| 2984 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2985 | if (_PyUnicode_Resize(&v, (int)(p - PyUnicode_AS_UNICODE(v))) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2986 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2987 | Py_XDECREF(errorHandler); |
| 2988 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2989 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2990 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2991 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2992 | Py_XDECREF(errorHandler); |
| 2993 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2994 | Py_XDECREF(v); |
| 2995 | return NULL; |
| 2996 | } |
| 2997 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2998 | /* Lookup the character ch in the mapping. If the character |
| 2999 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3000 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3001 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3002 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3003 | PyObject *w = PyInt_FromLong((long)c); |
| 3004 | PyObject *x; |
| 3005 | |
| 3006 | if (w == NULL) |
| 3007 | return NULL; |
| 3008 | x = PyObject_GetItem(mapping, w); |
| 3009 | Py_DECREF(w); |
| 3010 | if (x == NULL) { |
| 3011 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3012 | /* No mapping found means: mapping is undefined. */ |
| 3013 | PyErr_Clear(); |
| 3014 | x = Py_None; |
| 3015 | Py_INCREF(x); |
| 3016 | return x; |
| 3017 | } else |
| 3018 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3019 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 3020 | else if (x == Py_None) |
| 3021 | return x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3022 | else if (PyInt_Check(x)) { |
| 3023 | long value = PyInt_AS_LONG(x); |
| 3024 | if (value < 0 || value > 255) { |
| 3025 | PyErr_SetString(PyExc_TypeError, |
| 3026 | "character mapping must be in range(256)"); |
| 3027 | Py_DECREF(x); |
| 3028 | return NULL; |
| 3029 | } |
| 3030 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3031 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3032 | else if (PyString_Check(x)) |
| 3033 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3034 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3035 | /* wrong return value */ |
| 3036 | PyErr_SetString(PyExc_TypeError, |
| 3037 | "character mapping must return integer, None or str"); |
| 3038 | Py_DECREF(x); |
| 3039 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3040 | } |
| 3041 | } |
| 3042 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3043 | /* lookup the character, put the result in the output string and adjust |
| 3044 | various state variables. Reallocate the output string if not enough |
| 3045 | space is available. Return a new reference to the object that |
| 3046 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 3047 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 3048 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3049 | static |
| 3050 | PyObject *charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3051 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3052 | { |
| 3053 | PyObject *rep = charmapencode_lookup(c, mapping); |
| 3054 | |
| 3055 | if (rep==NULL) |
| 3056 | return NULL; |
| 3057 | else if (rep==Py_None) |
| 3058 | return rep; |
| 3059 | else { |
| 3060 | char *outstart = PyString_AS_STRING(*outobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3061 | Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3062 | if (PyInt_Check(rep)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3063 | Py_ssize_t requiredsize = *outpos+1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3064 | if (outsize<requiredsize) { |
| 3065 | /* exponentially overallocate to minimize reallocations */ |
| 3066 | if (requiredsize < 2*outsize) |
| 3067 | requiredsize = 2*outsize; |
| 3068 | if (_PyString_Resize(outobj, requiredsize)) { |
| 3069 | Py_DECREF(rep); |
| 3070 | return NULL; |
| 3071 | } |
| 3072 | outstart = PyString_AS_STRING(*outobj); |
| 3073 | } |
| 3074 | outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); |
| 3075 | } |
| 3076 | else { |
| 3077 | const char *repchars = PyString_AS_STRING(rep); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3078 | Py_ssize_t repsize = PyString_GET_SIZE(rep); |
| 3079 | Py_ssize_t requiredsize = *outpos+repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3080 | if (outsize<requiredsize) { |
| 3081 | /* exponentially overallocate to minimize reallocations */ |
| 3082 | if (requiredsize < 2*outsize) |
| 3083 | requiredsize = 2*outsize; |
| 3084 | if (_PyString_Resize(outobj, requiredsize)) { |
| 3085 | Py_DECREF(rep); |
| 3086 | return NULL; |
| 3087 | } |
| 3088 | outstart = PyString_AS_STRING(*outobj); |
| 3089 | } |
| 3090 | memcpy(outstart + *outpos, repchars, repsize); |
| 3091 | *outpos += repsize; |
| 3092 | } |
| 3093 | } |
| 3094 | return rep; |
| 3095 | } |
| 3096 | |
| 3097 | /* handle an error in PyUnicode_EncodeCharmap |
| 3098 | Return 0 on success, -1 on error */ |
| 3099 | static |
| 3100 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3101 | 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] | 3102 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3103 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3104 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3105 | { |
| 3106 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3107 | Py_ssize_t repsize; |
| 3108 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3109 | Py_UNICODE *uni2; |
| 3110 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3111 | Py_ssize_t collstartpos = *inpos; |
| 3112 | Py_ssize_t collendpos = *inpos+1; |
| 3113 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3114 | char *encoding = "charmap"; |
| 3115 | char *reason = "character maps to <undefined>"; |
| 3116 | |
| 3117 | PyObject *x; |
| 3118 | /* find all unencodable characters */ |
| 3119 | while (collendpos < size) { |
| 3120 | x = charmapencode_lookup(p[collendpos], mapping); |
| 3121 | if (x==NULL) |
| 3122 | return -1; |
| 3123 | else if (x!=Py_None) { |
| 3124 | Py_DECREF(x); |
| 3125 | break; |
| 3126 | } |
| 3127 | Py_DECREF(x); |
| 3128 | ++collendpos; |
| 3129 | } |
| 3130 | /* cache callback name lookup |
| 3131 | * (if not done yet, i.e. it's the first error) */ |
| 3132 | if (*known_errorHandler==-1) { |
| 3133 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3134 | *known_errorHandler = 1; |
| 3135 | else if (!strcmp(errors, "replace")) |
| 3136 | *known_errorHandler = 2; |
| 3137 | else if (!strcmp(errors, "ignore")) |
| 3138 | *known_errorHandler = 3; |
| 3139 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3140 | *known_errorHandler = 4; |
| 3141 | else |
| 3142 | *known_errorHandler = 0; |
| 3143 | } |
| 3144 | switch (*known_errorHandler) { |
| 3145 | case 1: /* strict */ |
| 3146 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3147 | return -1; |
| 3148 | case 2: /* replace */ |
| 3149 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 3150 | x = charmapencode_output('?', mapping, res, respos); |
| 3151 | if (x==NULL) { |
| 3152 | return -1; |
| 3153 | } |
| 3154 | else if (x==Py_None) { |
| 3155 | Py_DECREF(x); |
| 3156 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3157 | return -1; |
| 3158 | } |
| 3159 | Py_DECREF(x); |
| 3160 | } |
| 3161 | /* fall through */ |
| 3162 | case 3: /* ignore */ |
| 3163 | *inpos = collendpos; |
| 3164 | break; |
| 3165 | case 4: /* xmlcharrefreplace */ |
| 3166 | /* generate replacement (temporarily (mis)uses p) */ |
| 3167 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 3168 | char buffer[2+29+1+1]; |
| 3169 | char *cp; |
| 3170 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 3171 | for (cp = buffer; *cp; ++cp) { |
| 3172 | x = charmapencode_output(*cp, mapping, res, respos); |
| 3173 | if (x==NULL) |
| 3174 | return -1; |
| 3175 | else if (x==Py_None) { |
| 3176 | Py_DECREF(x); |
| 3177 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3178 | return -1; |
| 3179 | } |
| 3180 | Py_DECREF(x); |
| 3181 | } |
| 3182 | } |
| 3183 | *inpos = collendpos; |
| 3184 | break; |
| 3185 | default: |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3186 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3187 | encoding, reason, p, size, exceptionObject, |
| 3188 | collstartpos, collendpos, &newpos); |
| 3189 | if (repunicode == NULL) |
| 3190 | return -1; |
| 3191 | /* generate replacement */ |
| 3192 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3193 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 3194 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 3195 | if (x==NULL) { |
| 3196 | Py_DECREF(repunicode); |
| 3197 | return -1; |
| 3198 | } |
| 3199 | else if (x==Py_None) { |
| 3200 | Py_DECREF(repunicode); |
| 3201 | Py_DECREF(x); |
| 3202 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3203 | return -1; |
| 3204 | } |
| 3205 | Py_DECREF(x); |
| 3206 | } |
| 3207 | *inpos = newpos; |
| 3208 | Py_DECREF(repunicode); |
| 3209 | } |
| 3210 | return 0; |
| 3211 | } |
| 3212 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3213 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3214 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3215 | PyObject *mapping, |
| 3216 | const char *errors) |
| 3217 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3218 | /* output object */ |
| 3219 | PyObject *res = NULL; |
| 3220 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3221 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3222 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3223 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3224 | PyObject *errorHandler = NULL; |
| 3225 | PyObject *exc = NULL; |
| 3226 | /* the following variable is used for caching string comparisons |
| 3227 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 3228 | * 3=ignore, 4=xmlcharrefreplace */ |
| 3229 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3230 | |
| 3231 | /* Default to Latin-1 */ |
| 3232 | if (mapping == NULL) |
| 3233 | return PyUnicode_EncodeLatin1(p, size, errors); |
| 3234 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3235 | /* allocate enough for a simple encoding without |
| 3236 | replacements, if we need more, we'll resize */ |
| 3237 | res = PyString_FromStringAndSize(NULL, size); |
| 3238 | if (res == NULL) |
| 3239 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3240 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3241 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3242 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3243 | while (inpos<size) { |
| 3244 | /* try to encode it */ |
| 3245 | PyObject *x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 3246 | if (x==NULL) /* error */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3247 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3248 | if (x==Py_None) { /* unencodable character */ |
| 3249 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 3250 | &exc, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3251 | &known_errorHandler, &errorHandler, errors, |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 3252 | &res, &respos)) { |
| 3253 | Py_DECREF(x); |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3254 | goto onError; |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 3255 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3256 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3257 | else |
| 3258 | /* done with this character => adjust input position */ |
| 3259 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3260 | Py_DECREF(x); |
| 3261 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3262 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3263 | /* Resize if we allocated to much */ |
| 3264 | if (respos<PyString_GET_SIZE(res)) { |
| 3265 | if (_PyString_Resize(&res, respos)) |
| 3266 | goto onError; |
| 3267 | } |
| 3268 | Py_XDECREF(exc); |
| 3269 | Py_XDECREF(errorHandler); |
| 3270 | return res; |
| 3271 | |
| 3272 | onError: |
| 3273 | Py_XDECREF(res); |
| 3274 | Py_XDECREF(exc); |
| 3275 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3276 | return NULL; |
| 3277 | } |
| 3278 | |
| 3279 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 3280 | PyObject *mapping) |
| 3281 | { |
| 3282 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 3283 | PyErr_BadArgument(); |
| 3284 | return NULL; |
| 3285 | } |
| 3286 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 3287 | PyUnicode_GET_SIZE(unicode), |
| 3288 | mapping, |
| 3289 | NULL); |
| 3290 | } |
| 3291 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3292 | /* create or adjust a UnicodeTranslateError */ |
| 3293 | static void make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3294 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3295 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3296 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3297 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3298 | if (*exceptionObject == NULL) { |
| 3299 | *exceptionObject = PyUnicodeTranslateError_Create( |
| 3300 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3301 | } |
| 3302 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3303 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 3304 | goto onError; |
| 3305 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 3306 | goto onError; |
| 3307 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 3308 | goto onError; |
| 3309 | return; |
| 3310 | onError: |
| 3311 | Py_DECREF(*exceptionObject); |
| 3312 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3313 | } |
| 3314 | } |
| 3315 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3316 | /* raises a UnicodeTranslateError */ |
| 3317 | static void raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3318 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3319 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3320 | const char *reason) |
| 3321 | { |
| 3322 | make_translate_exception(exceptionObject, |
| 3323 | unicode, size, startpos, endpos, reason); |
| 3324 | if (*exceptionObject != NULL) |
| 3325 | PyCodec_StrictErrors(*exceptionObject); |
| 3326 | } |
| 3327 | |
| 3328 | /* error handling callback helper: |
| 3329 | build arguments, call the callback and check the arguments, |
| 3330 | put the result into newpos and return the replacement string, which |
| 3331 | has to be freed by the caller */ |
| 3332 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 3333 | PyObject **errorHandler, |
| 3334 | const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3335 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 3336 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3337 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3338 | { |
| 3339 | static char *argparse = "O!i;translating error handler must return (unicode, int) tuple"; |
| 3340 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3341 | int i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3342 | PyObject *restuple; |
| 3343 | PyObject *resunicode; |
| 3344 | |
| 3345 | if (*errorHandler == NULL) { |
| 3346 | *errorHandler = PyCodec_LookupError(errors); |
| 3347 | if (*errorHandler == NULL) |
| 3348 | return NULL; |
| 3349 | } |
| 3350 | |
| 3351 | make_translate_exception(exceptionObject, |
| 3352 | unicode, size, startpos, endpos, reason); |
| 3353 | if (*exceptionObject == NULL) |
| 3354 | return NULL; |
| 3355 | |
| 3356 | restuple = PyObject_CallFunctionObjArgs( |
| 3357 | *errorHandler, *exceptionObject, NULL); |
| 3358 | if (restuple == NULL) |
| 3359 | return NULL; |
| 3360 | if (!PyTuple_Check(restuple)) { |
| 3361 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 3362 | Py_DECREF(restuple); |
| 3363 | return NULL; |
| 3364 | } |
| 3365 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3366 | &resunicode, &i_newpos)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3367 | Py_DECREF(restuple); |
| 3368 | return NULL; |
| 3369 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3370 | if (i_newpos<0) |
| 3371 | *newpos = size+i_newpos; |
| 3372 | else |
| 3373 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3374 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 3375 | 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] | 3376 | Py_DECREF(restuple); |
| 3377 | return NULL; |
| 3378 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3379 | Py_INCREF(resunicode); |
| 3380 | Py_DECREF(restuple); |
| 3381 | return resunicode; |
| 3382 | } |
| 3383 | |
| 3384 | /* Lookup the character ch in the mapping and put the result in result, |
| 3385 | which must be decrefed by the caller. |
| 3386 | Return 0 on success, -1 on error */ |
| 3387 | static |
| 3388 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 3389 | { |
| 3390 | PyObject *w = PyInt_FromLong((long)c); |
| 3391 | PyObject *x; |
| 3392 | |
| 3393 | if (w == NULL) |
| 3394 | return -1; |
| 3395 | x = PyObject_GetItem(mapping, w); |
| 3396 | Py_DECREF(w); |
| 3397 | if (x == NULL) { |
| 3398 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3399 | /* No mapping found means: use 1:1 mapping. */ |
| 3400 | PyErr_Clear(); |
| 3401 | *result = NULL; |
| 3402 | return 0; |
| 3403 | } else |
| 3404 | return -1; |
| 3405 | } |
| 3406 | else if (x == Py_None) { |
| 3407 | *result = x; |
| 3408 | return 0; |
| 3409 | } |
| 3410 | else if (PyInt_Check(x)) { |
| 3411 | long value = PyInt_AS_LONG(x); |
| 3412 | long max = PyUnicode_GetMax(); |
| 3413 | if (value < 0 || value > max) { |
| 3414 | PyErr_Format(PyExc_TypeError, |
| 3415 | "character mapping must be in range(0x%lx)", max+1); |
| 3416 | Py_DECREF(x); |
| 3417 | return -1; |
| 3418 | } |
| 3419 | *result = x; |
| 3420 | return 0; |
| 3421 | } |
| 3422 | else if (PyUnicode_Check(x)) { |
| 3423 | *result = x; |
| 3424 | return 0; |
| 3425 | } |
| 3426 | else { |
| 3427 | /* wrong return value */ |
| 3428 | PyErr_SetString(PyExc_TypeError, |
| 3429 | "character mapping must return integer, None or unicode"); |
Walter Dörwald | 150523e | 2003-08-15 16:52:19 +0000 | [diff] [blame] | 3430 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3431 | return -1; |
| 3432 | } |
| 3433 | } |
| 3434 | /* ensure that *outobj is at least requiredsize characters long, |
| 3435 | if not reallocate and adjust various state variables. |
| 3436 | Return 0 on success, -1 on error */ |
| 3437 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3438 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3439 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3440 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3441 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3442 | if (requiredsize > oldsize) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3443 | /* remember old output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3444 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3445 | /* exponentially overallocate to minimize reallocations */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3446 | if (requiredsize < 2 * oldsize) |
| 3447 | requiredsize = 2 * oldsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3448 | if (_PyUnicode_Resize(outobj, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3449 | return -1; |
| 3450 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3451 | } |
| 3452 | return 0; |
| 3453 | } |
| 3454 | /* lookup the character, put the result in the output string and adjust |
| 3455 | various state variables. Return a new reference to the object that |
| 3456 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 3457 | undefined (in which case no character was written). |
| 3458 | The called must decref result. |
| 3459 | Return 0 on success, -1 on error. */ |
| 3460 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3461 | 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] | 3462 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3463 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3464 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3465 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3466 | return -1; |
| 3467 | if (*res==NULL) { |
| 3468 | /* not found => default to 1:1 mapping */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3469 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3470 | } |
| 3471 | else if (*res==Py_None) |
| 3472 | ; |
| 3473 | else if (PyInt_Check(*res)) { |
| 3474 | /* no overflow check, because we know that the space is enough */ |
| 3475 | *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); |
| 3476 | } |
| 3477 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3478 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3479 | if (repsize==1) { |
| 3480 | /* no overflow check, because we know that the space is enough */ |
| 3481 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 3482 | } |
| 3483 | else if (repsize!=0) { |
| 3484 | /* more than one character */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3485 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
Walter Dörwald | cd736e7 | 2004-02-05 17:36:00 +0000 | [diff] [blame] | 3486 | (insize - (curinp-startinp)) + |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3487 | repsize - 1; |
| 3488 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3489 | return -1; |
| 3490 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 3491 | *outp += repsize; |
| 3492 | } |
| 3493 | } |
| 3494 | else |
| 3495 | return -1; |
| 3496 | return 0; |
| 3497 | } |
| 3498 | |
| 3499 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3500 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3501 | PyObject *mapping, |
| 3502 | const char *errors) |
| 3503 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3504 | /* output object */ |
| 3505 | PyObject *res = NULL; |
| 3506 | /* pointers to the beginning and end+1 of input */ |
| 3507 | const Py_UNICODE *startp = p; |
| 3508 | const Py_UNICODE *endp = p + size; |
| 3509 | /* pointer into the output */ |
| 3510 | Py_UNICODE *str; |
| 3511 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3512 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3513 | char *reason = "character maps to <undefined>"; |
| 3514 | PyObject *errorHandler = NULL; |
| 3515 | PyObject *exc = NULL; |
| 3516 | /* the following variable is used for caching string comparisons |
| 3517 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 3518 | * 3=ignore, 4=xmlcharrefreplace */ |
| 3519 | int known_errorHandler = -1; |
| 3520 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3521 | if (mapping == NULL) { |
| 3522 | PyErr_BadArgument(); |
| 3523 | return NULL; |
| 3524 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3525 | |
| 3526 | /* allocate enough for a simple 1:1 translation without |
| 3527 | replacements, if we need more, we'll resize */ |
| 3528 | res = PyUnicode_FromUnicode(NULL, size); |
| 3529 | if (res == NULL) |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3530 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3531 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3532 | return res; |
| 3533 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3534 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3535 | while (p<endp) { |
| 3536 | /* try to encode it */ |
| 3537 | PyObject *x = NULL; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3538 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3539 | Py_XDECREF(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3540 | goto onError; |
| 3541 | } |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 3542 | Py_XDECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3543 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 3544 | ++p; |
| 3545 | else { /* untranslatable character */ |
| 3546 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3547 | Py_ssize_t repsize; |
| 3548 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3549 | Py_UNICODE *uni2; |
| 3550 | /* startpos for collecting untranslatable chars */ |
| 3551 | const Py_UNICODE *collstart = p; |
| 3552 | const Py_UNICODE *collend = p+1; |
| 3553 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3554 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3555 | /* find all untranslatable characters */ |
| 3556 | while (collend < endp) { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3557 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3558 | goto onError; |
| 3559 | Py_XDECREF(x); |
| 3560 | if (x!=Py_None) |
| 3561 | break; |
| 3562 | ++collend; |
| 3563 | } |
| 3564 | /* cache callback name lookup |
| 3565 | * (if not done yet, i.e. it's the first error) */ |
| 3566 | if (known_errorHandler==-1) { |
| 3567 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3568 | known_errorHandler = 1; |
| 3569 | else if (!strcmp(errors, "replace")) |
| 3570 | known_errorHandler = 2; |
| 3571 | else if (!strcmp(errors, "ignore")) |
| 3572 | known_errorHandler = 3; |
| 3573 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3574 | known_errorHandler = 4; |
| 3575 | else |
| 3576 | known_errorHandler = 0; |
| 3577 | } |
| 3578 | switch (known_errorHandler) { |
| 3579 | case 1: /* strict */ |
| 3580 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
| 3581 | goto onError; |
| 3582 | case 2: /* replace */ |
| 3583 | /* No need to check for space, this is a 1:1 replacement */ |
| 3584 | for (coll = collstart; coll<collend; ++coll) |
| 3585 | *str++ = '?'; |
| 3586 | /* fall through */ |
| 3587 | case 3: /* ignore */ |
| 3588 | p = collend; |
| 3589 | break; |
| 3590 | case 4: /* xmlcharrefreplace */ |
| 3591 | /* generate replacement (temporarily (mis)uses p) */ |
| 3592 | for (p = collstart; p < collend; ++p) { |
| 3593 | char buffer[2+29+1+1]; |
| 3594 | char *cp; |
| 3595 | sprintf(buffer, "&#%d;", (int)*p); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3596 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3597 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 3598 | goto onError; |
| 3599 | for (cp = buffer; *cp; ++cp) |
| 3600 | *str++ = *cp; |
| 3601 | } |
| 3602 | p = collend; |
| 3603 | break; |
| 3604 | default: |
| 3605 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 3606 | reason, startp, size, &exc, |
| 3607 | collstart-startp, collend-startp, &newpos); |
| 3608 | if (repunicode == NULL) |
| 3609 | goto onError; |
| 3610 | /* generate replacement */ |
| 3611 | repsize = PyUnicode_GET_SIZE(repunicode); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3612 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3613 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 3614 | Py_DECREF(repunicode); |
| 3615 | goto onError; |
| 3616 | } |
| 3617 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 3618 | *str++ = *uni2; |
| 3619 | p = startp + newpos; |
| 3620 | Py_DECREF(repunicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3621 | } |
| 3622 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3623 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3624 | /* Resize if we allocated to much */ |
| 3625 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3626 | if (respos<PyUnicode_GET_SIZE(res)) { |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3627 | if (_PyUnicode_Resize(&res, respos) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3628 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3629 | } |
| 3630 | Py_XDECREF(exc); |
| 3631 | Py_XDECREF(errorHandler); |
| 3632 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3633 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3634 | onError: |
| 3635 | Py_XDECREF(res); |
| 3636 | Py_XDECREF(exc); |
| 3637 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3638 | return NULL; |
| 3639 | } |
| 3640 | |
| 3641 | PyObject *PyUnicode_Translate(PyObject *str, |
| 3642 | PyObject *mapping, |
| 3643 | const char *errors) |
| 3644 | { |
| 3645 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3646 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3647 | str = PyUnicode_FromObject(str); |
| 3648 | if (str == NULL) |
| 3649 | goto onError; |
| 3650 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 3651 | PyUnicode_GET_SIZE(str), |
| 3652 | mapping, |
| 3653 | errors); |
| 3654 | Py_DECREF(str); |
| 3655 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3656 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3657 | onError: |
| 3658 | Py_XDECREF(str); |
| 3659 | return NULL; |
| 3660 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3661 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3662 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 3663 | |
| 3664 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3665 | Py_ssize_t length, |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3666 | char *output, |
| 3667 | const char *errors) |
| 3668 | { |
| 3669 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3670 | PyObject *errorHandler = NULL; |
| 3671 | PyObject *exc = NULL; |
| 3672 | const char *encoding = "decimal"; |
| 3673 | const char *reason = "invalid decimal Unicode string"; |
| 3674 | /* the following variable is used for caching string comparisons |
| 3675 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 3676 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3677 | |
| 3678 | if (output == NULL) { |
| 3679 | PyErr_BadArgument(); |
| 3680 | return -1; |
| 3681 | } |
| 3682 | |
| 3683 | p = s; |
| 3684 | end = s + length; |
| 3685 | while (p < end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3686 | register Py_UNICODE ch = *p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3687 | int decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3688 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3689 | Py_ssize_t repsize; |
| 3690 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3691 | Py_UNICODE *uni2; |
| 3692 | Py_UNICODE *collstart; |
| 3693 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3694 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3695 | if (Py_UNICODE_ISSPACE(ch)) { |
| 3696 | *output++ = ' '; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3697 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3698 | continue; |
| 3699 | } |
| 3700 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 3701 | if (decimal >= 0) { |
| 3702 | *output++ = '0' + decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3703 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3704 | continue; |
| 3705 | } |
Guido van Rossum | ba47704 | 2000-04-06 18:18:10 +0000 | [diff] [blame] | 3706 | if (0 < ch && ch < 256) { |
Guido van Rossum | 42c29aa | 2000-05-03 23:58:29 +0000 | [diff] [blame] | 3707 | *output++ = (char)ch; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3708 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3709 | continue; |
| 3710 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3711 | /* All other characters are considered unencodable */ |
| 3712 | collstart = p; |
| 3713 | collend = p+1; |
| 3714 | while (collend < end) { |
| 3715 | if ((0 < *collend && *collend < 256) || |
| 3716 | !Py_UNICODE_ISSPACE(*collend) || |
| 3717 | Py_UNICODE_TODECIMAL(*collend)) |
| 3718 | break; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3719 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3720 | /* cache callback name lookup |
| 3721 | * (if not done yet, i.e. it's the first error) */ |
| 3722 | if (known_errorHandler==-1) { |
| 3723 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3724 | known_errorHandler = 1; |
| 3725 | else if (!strcmp(errors, "replace")) |
| 3726 | known_errorHandler = 2; |
| 3727 | else if (!strcmp(errors, "ignore")) |
| 3728 | known_errorHandler = 3; |
| 3729 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3730 | known_errorHandler = 4; |
| 3731 | else |
| 3732 | known_errorHandler = 0; |
| 3733 | } |
| 3734 | switch (known_errorHandler) { |
| 3735 | case 1: /* strict */ |
| 3736 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 3737 | goto onError; |
| 3738 | case 2: /* replace */ |
| 3739 | for (p = collstart; p < collend; ++p) |
| 3740 | *output++ = '?'; |
| 3741 | /* fall through */ |
| 3742 | case 3: /* ignore */ |
| 3743 | p = collend; |
| 3744 | break; |
| 3745 | case 4: /* xmlcharrefreplace */ |
| 3746 | /* generate replacement (temporarily (mis)uses p) */ |
| 3747 | for (p = collstart; p < collend; ++p) |
| 3748 | output += sprintf(output, "&#%d;", (int)*p); |
| 3749 | p = collend; |
| 3750 | break; |
| 3751 | default: |
| 3752 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 3753 | encoding, reason, s, length, &exc, |
| 3754 | collstart-s, collend-s, &newpos); |
| 3755 | if (repunicode == NULL) |
| 3756 | goto onError; |
| 3757 | /* generate replacement */ |
| 3758 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3759 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 3760 | Py_UNICODE ch = *uni2; |
| 3761 | if (Py_UNICODE_ISSPACE(ch)) |
| 3762 | *output++ = ' '; |
| 3763 | else { |
| 3764 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 3765 | if (decimal >= 0) |
| 3766 | *output++ = '0' + decimal; |
| 3767 | else if (0 < ch && ch < 256) |
| 3768 | *output++ = (char)ch; |
| 3769 | else { |
| 3770 | Py_DECREF(repunicode); |
| 3771 | raise_encode_exception(&exc, encoding, |
| 3772 | s, length, collstart-s, collend-s, reason); |
| 3773 | goto onError; |
| 3774 | } |
| 3775 | } |
| 3776 | } |
| 3777 | p = s + newpos; |
| 3778 | Py_DECREF(repunicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3779 | } |
| 3780 | } |
| 3781 | /* 0-terminate the output string */ |
| 3782 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3783 | Py_XDECREF(exc); |
| 3784 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3785 | return 0; |
| 3786 | |
| 3787 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3788 | Py_XDECREF(exc); |
| 3789 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3790 | return -1; |
| 3791 | } |
| 3792 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3793 | /* --- Helpers ------------------------------------------------------------ */ |
| 3794 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3795 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3796 | Py_ssize_t count(PyUnicodeObject *self, |
| 3797 | Py_ssize_t start, |
| 3798 | Py_ssize_t end, |
| 3799 | PyUnicodeObject *substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3800 | { |
| 3801 | int count = 0; |
| 3802 | |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3803 | if (start < 0) |
| 3804 | start += self->length; |
| 3805 | if (start < 0) |
| 3806 | start = 0; |
| 3807 | if (end > self->length) |
| 3808 | end = self->length; |
| 3809 | if (end < 0) |
| 3810 | end += self->length; |
| 3811 | if (end < 0) |
| 3812 | end = 0; |
| 3813 | |
Marc-André Lemburg | 49ef6dc | 2000-06-18 22:25:22 +0000 | [diff] [blame] | 3814 | if (substring->length == 0) |
| 3815 | return (end - start + 1); |
| 3816 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3817 | end -= substring->length; |
| 3818 | |
| 3819 | while (start <= end) |
| 3820 | if (Py_UNICODE_MATCH(self, start, substring)) { |
| 3821 | count++; |
| 3822 | start += substring->length; |
| 3823 | } else |
| 3824 | start++; |
| 3825 | |
| 3826 | return count; |
| 3827 | } |
| 3828 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3829 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3830 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3831 | Py_ssize_t start, |
| 3832 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3833 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3834 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3835 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3836 | str = PyUnicode_FromObject(str); |
| 3837 | if (str == NULL) |
| 3838 | return -1; |
| 3839 | substr = PyUnicode_FromObject(substr); |
| 3840 | if (substr == NULL) { |
Marc-André Lemburg | 49ef6dc | 2000-06-18 22:25:22 +0000 | [diff] [blame] | 3841 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3842 | return -1; |
| 3843 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3844 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3845 | result = count((PyUnicodeObject *)str, |
| 3846 | start, end, |
| 3847 | (PyUnicodeObject *)substr); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3848 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3849 | Py_DECREF(str); |
| 3850 | Py_DECREF(substr); |
| 3851 | return result; |
| 3852 | } |
| 3853 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3854 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3855 | Py_ssize_t findstring(PyUnicodeObject *self, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3856 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3857 | Py_ssize_t start, |
| 3858 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3859 | int direction) |
| 3860 | { |
| 3861 | if (start < 0) |
| 3862 | start += self->length; |
| 3863 | if (start < 0) |
| 3864 | start = 0; |
| 3865 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3866 | if (end > self->length) |
| 3867 | end = self->length; |
| 3868 | if (end < 0) |
| 3869 | end += self->length; |
| 3870 | if (end < 0) |
| 3871 | end = 0; |
| 3872 | |
Guido van Rossum | 76afbd9 | 2002-08-20 17:29:29 +0000 | [diff] [blame] | 3873 | if (substring->length == 0) |
| 3874 | return (direction > 0) ? start : end; |
| 3875 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3876 | end -= substring->length; |
| 3877 | |
| 3878 | if (direction < 0) { |
| 3879 | for (; end >= start; end--) |
| 3880 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 3881 | return end; |
| 3882 | } else { |
| 3883 | for (; start <= end; start++) |
| 3884 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 3885 | return start; |
| 3886 | } |
| 3887 | |
| 3888 | return -1; |
| 3889 | } |
| 3890 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3891 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3892 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3893 | Py_ssize_t start, |
| 3894 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3895 | int direction) |
| 3896 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3897 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3898 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3899 | str = PyUnicode_FromObject(str); |
| 3900 | if (str == NULL) |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 3901 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3902 | substr = PyUnicode_FromObject(substr); |
| 3903 | if (substr == NULL) { |
Marc-André Lemburg | 4164439 | 2002-05-29 13:46:29 +0000 | [diff] [blame] | 3904 | Py_DECREF(str); |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 3905 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3906 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3907 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3908 | result = findstring((PyUnicodeObject *)str, |
| 3909 | (PyUnicodeObject *)substr, |
| 3910 | start, end, direction); |
| 3911 | Py_DECREF(str); |
| 3912 | Py_DECREF(substr); |
| 3913 | return result; |
| 3914 | } |
| 3915 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3916 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3917 | int tailmatch(PyUnicodeObject *self, |
| 3918 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3919 | Py_ssize_t start, |
| 3920 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3921 | int direction) |
| 3922 | { |
| 3923 | if (start < 0) |
| 3924 | start += self->length; |
| 3925 | if (start < 0) |
| 3926 | start = 0; |
| 3927 | |
| 3928 | if (substring->length == 0) |
| 3929 | return 1; |
| 3930 | |
| 3931 | if (end > self->length) |
| 3932 | end = self->length; |
| 3933 | if (end < 0) |
| 3934 | end += self->length; |
| 3935 | if (end < 0) |
| 3936 | end = 0; |
| 3937 | |
| 3938 | end -= substring->length; |
| 3939 | if (end < start) |
| 3940 | return 0; |
| 3941 | |
| 3942 | if (direction > 0) { |
| 3943 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 3944 | return 1; |
| 3945 | } else { |
| 3946 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 3947 | return 1; |
| 3948 | } |
| 3949 | |
| 3950 | return 0; |
| 3951 | } |
| 3952 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3953 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3954 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3955 | Py_ssize_t start, |
| 3956 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3957 | int direction) |
| 3958 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3959 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3960 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3961 | str = PyUnicode_FromObject(str); |
| 3962 | if (str == NULL) |
| 3963 | return -1; |
| 3964 | substr = PyUnicode_FromObject(substr); |
| 3965 | if (substr == NULL) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3966 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3967 | return -1; |
| 3968 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3969 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3970 | result = tailmatch((PyUnicodeObject *)str, |
| 3971 | (PyUnicodeObject *)substr, |
| 3972 | start, end, direction); |
| 3973 | Py_DECREF(str); |
| 3974 | Py_DECREF(substr); |
| 3975 | return result; |
| 3976 | } |
| 3977 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3978 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3979 | const Py_UNICODE *findchar(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3980 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3981 | Py_UNICODE ch) |
| 3982 | { |
| 3983 | /* like wcschr, but doesn't stop at NULL characters */ |
| 3984 | |
| 3985 | while (size-- > 0) { |
| 3986 | if (*s == ch) |
| 3987 | return s; |
| 3988 | s++; |
| 3989 | } |
| 3990 | |
| 3991 | return NULL; |
| 3992 | } |
| 3993 | |
| 3994 | /* Apply fixfct filter to the Unicode object self and return a |
| 3995 | reference to the modified object */ |
| 3996 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3997 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3998 | PyObject *fixup(PyUnicodeObject *self, |
| 3999 | int (*fixfct)(PyUnicodeObject *s)) |
| 4000 | { |
| 4001 | |
| 4002 | PyUnicodeObject *u; |
| 4003 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4004 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4005 | if (u == NULL) |
| 4006 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4007 | |
| 4008 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 4009 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4010 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4011 | /* fixfct should return TRUE if it modified the buffer. If |
| 4012 | FALSE, return a reference to the original buffer instead |
| 4013 | (to save space, not time) */ |
| 4014 | Py_INCREF(self); |
| 4015 | Py_DECREF(u); |
| 4016 | return (PyObject*) self; |
| 4017 | } |
| 4018 | return (PyObject*) u; |
| 4019 | } |
| 4020 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4021 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4022 | int fixupper(PyUnicodeObject *self) |
| 4023 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4024 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4025 | Py_UNICODE *s = self->str; |
| 4026 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4027 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4028 | while (len-- > 0) { |
| 4029 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4030 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4031 | ch = Py_UNICODE_TOUPPER(*s); |
| 4032 | if (ch != *s) { |
| 4033 | status = 1; |
| 4034 | *s = ch; |
| 4035 | } |
| 4036 | s++; |
| 4037 | } |
| 4038 | |
| 4039 | return status; |
| 4040 | } |
| 4041 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4042 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4043 | int fixlower(PyUnicodeObject *self) |
| 4044 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4045 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4046 | Py_UNICODE *s = self->str; |
| 4047 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4048 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4049 | while (len-- > 0) { |
| 4050 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4051 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4052 | ch = Py_UNICODE_TOLOWER(*s); |
| 4053 | if (ch != *s) { |
| 4054 | status = 1; |
| 4055 | *s = ch; |
| 4056 | } |
| 4057 | s++; |
| 4058 | } |
| 4059 | |
| 4060 | return status; |
| 4061 | } |
| 4062 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4063 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4064 | int fixswapcase(PyUnicodeObject *self) |
| 4065 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4066 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4067 | Py_UNICODE *s = self->str; |
| 4068 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4069 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4070 | while (len-- > 0) { |
| 4071 | if (Py_UNICODE_ISUPPER(*s)) { |
| 4072 | *s = Py_UNICODE_TOLOWER(*s); |
| 4073 | status = 1; |
| 4074 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 4075 | *s = Py_UNICODE_TOUPPER(*s); |
| 4076 | status = 1; |
| 4077 | } |
| 4078 | s++; |
| 4079 | } |
| 4080 | |
| 4081 | return status; |
| 4082 | } |
| 4083 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4084 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4085 | int fixcapitalize(PyUnicodeObject *self) |
| 4086 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4087 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4088 | Py_UNICODE *s = self->str; |
| 4089 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4090 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4091 | if (len == 0) |
| 4092 | return 0; |
| 4093 | if (Py_UNICODE_ISLOWER(*s)) { |
| 4094 | *s = Py_UNICODE_TOUPPER(*s); |
| 4095 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4096 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4097 | s++; |
| 4098 | while (--len > 0) { |
| 4099 | if (Py_UNICODE_ISUPPER(*s)) { |
| 4100 | *s = Py_UNICODE_TOLOWER(*s); |
| 4101 | status = 1; |
| 4102 | } |
| 4103 | s++; |
| 4104 | } |
| 4105 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4106 | } |
| 4107 | |
| 4108 | static |
| 4109 | int fixtitle(PyUnicodeObject *self) |
| 4110 | { |
| 4111 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 4112 | register Py_UNICODE *e; |
| 4113 | int previous_is_cased; |
| 4114 | |
| 4115 | /* Shortcut for single character strings */ |
| 4116 | if (PyUnicode_GET_SIZE(self) == 1) { |
| 4117 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 4118 | if (*p != ch) { |
| 4119 | *p = ch; |
| 4120 | return 1; |
| 4121 | } |
| 4122 | else |
| 4123 | return 0; |
| 4124 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4125 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4126 | e = p + PyUnicode_GET_SIZE(self); |
| 4127 | previous_is_cased = 0; |
| 4128 | for (; p < e; p++) { |
| 4129 | register const Py_UNICODE ch = *p; |
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 | if (previous_is_cased) |
| 4132 | *p = Py_UNICODE_TOLOWER(ch); |
| 4133 | else |
| 4134 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4135 | |
| 4136 | if (Py_UNICODE_ISLOWER(ch) || |
| 4137 | Py_UNICODE_ISUPPER(ch) || |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4138 | Py_UNICODE_ISTITLE(ch)) |
| 4139 | previous_is_cased = 1; |
| 4140 | else |
| 4141 | previous_is_cased = 0; |
| 4142 | } |
| 4143 | return 1; |
| 4144 | } |
| 4145 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4146 | PyObject * |
| 4147 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4148 | { |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4149 | PyObject *internal_separator = NULL; |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 4150 | const Py_UNICODE blank = ' '; |
| 4151 | const Py_UNICODE *sep = ␣ |
| 4152 | size_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4153 | PyUnicodeObject *res = NULL; /* the result */ |
| 4154 | size_t res_alloc = 100; /* # allocated bytes for string in res */ |
| 4155 | size_t res_used; /* # used bytes */ |
| 4156 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 4157 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4158 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4159 | PyObject *item; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4160 | int i; |
| 4161 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4162 | fseq = PySequence_Fast(seq, ""); |
| 4163 | if (fseq == NULL) { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4164 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4165 | } |
| 4166 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4167 | /* Grrrr. A codec may be invoked to convert str objects to |
| 4168 | * Unicode, and so it's possible to call back into Python code |
| 4169 | * during PyUnicode_FromObject(), and so it's possible for a sick |
| 4170 | * codec to change the size of fseq (if seq is a list). Therefore |
| 4171 | * we have to keep refetching the size -- can't assume seqlen |
| 4172 | * is invariant. |
| 4173 | */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4174 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 4175 | /* If empty sequence, return u"". */ |
| 4176 | if (seqlen == 0) { |
| 4177 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 4178 | goto Done; |
| 4179 | } |
| 4180 | /* If singleton sequence with an exact Unicode, return that. */ |
| 4181 | if (seqlen == 1) { |
| 4182 | item = PySequence_Fast_GET_ITEM(fseq, 0); |
| 4183 | if (PyUnicode_CheckExact(item)) { |
| 4184 | Py_INCREF(item); |
| 4185 | res = (PyUnicodeObject *)item; |
| 4186 | goto Done; |
| 4187 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4188 | } |
| 4189 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4190 | /* At least two items to join, or one that isn't exact Unicode. */ |
| 4191 | if (seqlen > 1) { |
| 4192 | /* Set up sep and seplen -- they're needed. */ |
| 4193 | if (separator == NULL) { |
| 4194 | sep = ␣ |
| 4195 | seplen = 1; |
| 4196 | } |
| 4197 | else { |
| 4198 | internal_separator = PyUnicode_FromObject(separator); |
| 4199 | if (internal_separator == NULL) |
| 4200 | goto onError; |
| 4201 | sep = PyUnicode_AS_UNICODE(internal_separator); |
| 4202 | seplen = PyUnicode_GET_SIZE(internal_separator); |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4203 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 4204 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4205 | } |
| 4206 | } |
| 4207 | |
| 4208 | /* Get space. */ |
| 4209 | res = _PyUnicode_New((int)res_alloc); |
| 4210 | if (res == NULL) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4211 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4212 | res_p = PyUnicode_AS_UNICODE(res); |
| 4213 | res_used = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4214 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4215 | for (i = 0; i < seqlen; ++i) { |
| 4216 | size_t itemlen; |
| 4217 | size_t new_res_used; |
| 4218 | |
| 4219 | item = PySequence_Fast_GET_ITEM(fseq, i); |
| 4220 | /* Convert item to Unicode. */ |
| 4221 | if (! PyUnicode_Check(item) && ! PyString_Check(item)) { |
| 4222 | PyErr_Format(PyExc_TypeError, |
| 4223 | "sequence item %i: expected string or Unicode," |
| 4224 | " %.80s found", |
| 4225 | i, item->ob_type->tp_name); |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 4226 | goto onError; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4227 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4228 | item = PyUnicode_FromObject(item); |
| 4229 | if (item == NULL) |
| 4230 | goto onError; |
| 4231 | /* We own a reference to item from here on. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4232 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4233 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 4234 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 4235 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4236 | /* 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] | 4237 | itemlen = PyUnicode_GET_SIZE(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4238 | new_res_used = res_used + itemlen; |
| 4239 | if (new_res_used < res_used || new_res_used > INT_MAX) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4240 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4241 | if (i < seqlen - 1) { |
| 4242 | new_res_used += seplen; |
| 4243 | if (new_res_used < res_used || new_res_used > INT_MAX) |
| 4244 | goto Overflow; |
| 4245 | } |
| 4246 | if (new_res_used > res_alloc) { |
| 4247 | /* double allocated size until it's big enough */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4248 | do { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4249 | size_t oldsize = res_alloc; |
| 4250 | res_alloc += res_alloc; |
| 4251 | if (res_alloc < oldsize || res_alloc > INT_MAX) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4252 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4253 | } while (new_res_used > res_alloc); |
| 4254 | if (_PyUnicode_Resize(&res, (int)res_alloc) < 0) { |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4255 | Py_DECREF(item); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4256 | goto onError; |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4257 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4258 | res_p = PyUnicode_AS_UNICODE(res) + res_used; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4259 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4260 | |
| 4261 | /* Copy item, and maybe the separator. */ |
| 4262 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), (int)itemlen); |
| 4263 | res_p += itemlen; |
| 4264 | if (i < seqlen - 1) { |
| 4265 | Py_UNICODE_COPY(res_p, sep, (int)seplen); |
| 4266 | res_p += seplen; |
| 4267 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4268 | Py_DECREF(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4269 | res_used = new_res_used; |
| 4270 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4271 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4272 | /* Shrink res to match the used area; this probably can't fail, |
| 4273 | * but it's cheap to check. |
| 4274 | */ |
| 4275 | if (_PyUnicode_Resize(&res, (int)res_used) < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4276 | goto onError; |
| 4277 | |
| 4278 | Done: |
| 4279 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4280 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4281 | return (PyObject *)res; |
| 4282 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4283 | Overflow: |
| 4284 | PyErr_SetString(PyExc_OverflowError, |
| 4285 | "join() is too long for a Python string"); |
| 4286 | Py_DECREF(item); |
| 4287 | /* fall through */ |
| 4288 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4289 | onError: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4290 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4291 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4292 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4293 | return NULL; |
| 4294 | } |
| 4295 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4296 | static |
| 4297 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4298 | Py_ssize_t left, |
| 4299 | Py_ssize_t right, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4300 | Py_UNICODE fill) |
| 4301 | { |
| 4302 | PyUnicodeObject *u; |
| 4303 | |
| 4304 | if (left < 0) |
| 4305 | left = 0; |
| 4306 | if (right < 0) |
| 4307 | right = 0; |
| 4308 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4309 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4310 | Py_INCREF(self); |
| 4311 | return self; |
| 4312 | } |
| 4313 | |
| 4314 | u = _PyUnicode_New(left + self->length + right); |
| 4315 | if (u) { |
| 4316 | if (left) |
| 4317 | Py_UNICODE_FILL(u->str, fill, left); |
| 4318 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 4319 | if (right) |
| 4320 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 4321 | } |
| 4322 | |
| 4323 | return u; |
| 4324 | } |
| 4325 | |
| 4326 | #define SPLIT_APPEND(data, left, right) \ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4327 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4328 | if (!str) \ |
| 4329 | goto onError; \ |
| 4330 | if (PyList_Append(list, str)) { \ |
| 4331 | Py_DECREF(str); \ |
| 4332 | goto onError; \ |
| 4333 | } \ |
| 4334 | else \ |
| 4335 | Py_DECREF(str); |
| 4336 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4337 | #define SPLIT_INSERT(data, left, right) \ |
| 4338 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
| 4339 | if (!str) \ |
| 4340 | goto onError; \ |
| 4341 | if (PyList_Insert(list, 0, str)) { \ |
| 4342 | Py_DECREF(str); \ |
| 4343 | goto onError; \ |
| 4344 | } \ |
| 4345 | else \ |
| 4346 | Py_DECREF(str); |
| 4347 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4348 | static |
| 4349 | PyObject *split_whitespace(PyUnicodeObject *self, |
| 4350 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4351 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4352 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4353 | register Py_ssize_t i; |
| 4354 | register Py_ssize_t j; |
| 4355 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4356 | PyObject *str; |
| 4357 | |
| 4358 | for (i = j = 0; i < len; ) { |
| 4359 | /* find a token */ |
| 4360 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 4361 | i++; |
| 4362 | j = i; |
| 4363 | while (i < len && !Py_UNICODE_ISSPACE(self->str[i])) |
| 4364 | i++; |
| 4365 | if (j < i) { |
| 4366 | if (maxcount-- <= 0) |
| 4367 | break; |
| 4368 | SPLIT_APPEND(self->str, j, i); |
| 4369 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 4370 | i++; |
| 4371 | j = i; |
| 4372 | } |
| 4373 | } |
| 4374 | if (j < len) { |
| 4375 | SPLIT_APPEND(self->str, j, len); |
| 4376 | } |
| 4377 | return list; |
| 4378 | |
| 4379 | onError: |
| 4380 | Py_DECREF(list); |
| 4381 | return NULL; |
| 4382 | } |
| 4383 | |
| 4384 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4385 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4386 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4387 | register Py_ssize_t i; |
| 4388 | register Py_ssize_t j; |
| 4389 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4390 | PyObject *list; |
| 4391 | PyObject *str; |
| 4392 | Py_UNICODE *data; |
| 4393 | |
| 4394 | string = PyUnicode_FromObject(string); |
| 4395 | if (string == NULL) |
| 4396 | return NULL; |
| 4397 | data = PyUnicode_AS_UNICODE(string); |
| 4398 | len = PyUnicode_GET_SIZE(string); |
| 4399 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4400 | list = PyList_New(0); |
| 4401 | if (!list) |
| 4402 | goto onError; |
| 4403 | |
| 4404 | for (i = j = 0; i < len; ) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4405 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4406 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4407 | /* Find a line and append it */ |
| 4408 | while (i < len && !Py_UNICODE_ISLINEBREAK(data[i])) |
| 4409 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4410 | |
| 4411 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4412 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4413 | if (i < len) { |
| 4414 | if (data[i] == '\r' && i + 1 < len && |
| 4415 | data[i+1] == '\n') |
| 4416 | i += 2; |
| 4417 | else |
| 4418 | i++; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4419 | if (keepends) |
| 4420 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4421 | } |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4422 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4423 | j = i; |
| 4424 | } |
| 4425 | if (j < len) { |
| 4426 | SPLIT_APPEND(data, j, len); |
| 4427 | } |
| 4428 | |
| 4429 | Py_DECREF(string); |
| 4430 | return list; |
| 4431 | |
| 4432 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 4433 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4434 | Py_DECREF(string); |
| 4435 | return NULL; |
| 4436 | } |
| 4437 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4438 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4439 | PyObject *split_char(PyUnicodeObject *self, |
| 4440 | PyObject *list, |
| 4441 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4442 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4443 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4444 | register Py_ssize_t i; |
| 4445 | register Py_ssize_t j; |
| 4446 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4447 | PyObject *str; |
| 4448 | |
| 4449 | for (i = j = 0; i < len; ) { |
| 4450 | if (self->str[i] == ch) { |
| 4451 | if (maxcount-- <= 0) |
| 4452 | break; |
| 4453 | SPLIT_APPEND(self->str, j, i); |
| 4454 | i = j = i + 1; |
| 4455 | } else |
| 4456 | i++; |
| 4457 | } |
| 4458 | if (j <= len) { |
| 4459 | SPLIT_APPEND(self->str, j, len); |
| 4460 | } |
| 4461 | return list; |
| 4462 | |
| 4463 | onError: |
| 4464 | Py_DECREF(list); |
| 4465 | return NULL; |
| 4466 | } |
| 4467 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4468 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4469 | PyObject *split_substring(PyUnicodeObject *self, |
| 4470 | PyObject *list, |
| 4471 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4472 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4473 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4474 | register Py_ssize_t i; |
| 4475 | register Py_ssize_t j; |
| 4476 | Py_ssize_t len = self->length; |
| 4477 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4478 | PyObject *str; |
| 4479 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 4480 | for (i = j = 0; i <= len - sublen; ) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4481 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 4482 | if (maxcount-- <= 0) |
| 4483 | break; |
| 4484 | SPLIT_APPEND(self->str, j, i); |
| 4485 | i = j = i + sublen; |
| 4486 | } else |
| 4487 | i++; |
| 4488 | } |
| 4489 | if (j <= len) { |
| 4490 | SPLIT_APPEND(self->str, j, len); |
| 4491 | } |
| 4492 | return list; |
| 4493 | |
| 4494 | onError: |
| 4495 | Py_DECREF(list); |
| 4496 | return NULL; |
| 4497 | } |
| 4498 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4499 | static |
| 4500 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
| 4501 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4502 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4503 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4504 | register Py_ssize_t i; |
| 4505 | register Py_ssize_t j; |
| 4506 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4507 | PyObject *str; |
| 4508 | |
| 4509 | for (i = j = len - 1; i >= 0; ) { |
| 4510 | /* find a token */ |
| 4511 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 4512 | i--; |
| 4513 | j = i; |
| 4514 | while (i >= 0 && !Py_UNICODE_ISSPACE(self->str[i])) |
| 4515 | i--; |
| 4516 | if (j > i) { |
| 4517 | if (maxcount-- <= 0) |
| 4518 | break; |
| 4519 | SPLIT_INSERT(self->str, i + 1, j + 1); |
| 4520 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 4521 | i--; |
| 4522 | j = i; |
| 4523 | } |
| 4524 | } |
| 4525 | if (j >= 0) { |
| 4526 | SPLIT_INSERT(self->str, 0, j + 1); |
| 4527 | } |
| 4528 | return list; |
| 4529 | |
| 4530 | onError: |
| 4531 | Py_DECREF(list); |
| 4532 | return NULL; |
| 4533 | } |
| 4534 | |
| 4535 | static |
| 4536 | PyObject *rsplit_char(PyUnicodeObject *self, |
| 4537 | PyObject *list, |
| 4538 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4539 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4540 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4541 | register Py_ssize_t i; |
| 4542 | register Py_ssize_t j; |
| 4543 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4544 | PyObject *str; |
| 4545 | |
| 4546 | for (i = j = len - 1; i >= 0; ) { |
| 4547 | if (self->str[i] == ch) { |
| 4548 | if (maxcount-- <= 0) |
| 4549 | break; |
| 4550 | SPLIT_INSERT(self->str, i + 1, j + 1); |
| 4551 | j = i = i - 1; |
| 4552 | } else |
| 4553 | i--; |
| 4554 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 4555 | if (j >= -1) { |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4556 | SPLIT_INSERT(self->str, 0, j + 1); |
| 4557 | } |
| 4558 | return list; |
| 4559 | |
| 4560 | onError: |
| 4561 | Py_DECREF(list); |
| 4562 | return NULL; |
| 4563 | } |
| 4564 | |
| 4565 | static |
| 4566 | PyObject *rsplit_substring(PyUnicodeObject *self, |
| 4567 | PyObject *list, |
| 4568 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4569 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4570 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4571 | register Py_ssize_t i; |
| 4572 | register Py_ssize_t j; |
| 4573 | Py_ssize_t len = self->length; |
| 4574 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4575 | PyObject *str; |
| 4576 | |
| 4577 | for (i = len - sublen, j = len; i >= 0; ) { |
| 4578 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 4579 | if (maxcount-- <= 0) |
| 4580 | break; |
| 4581 | SPLIT_INSERT(self->str, i + sublen, j); |
| 4582 | j = i; |
| 4583 | i -= sublen; |
| 4584 | } else |
| 4585 | i--; |
| 4586 | } |
| 4587 | if (j >= 0) { |
| 4588 | SPLIT_INSERT(self->str, 0, j); |
| 4589 | } |
| 4590 | return list; |
| 4591 | |
| 4592 | onError: |
| 4593 | Py_DECREF(list); |
| 4594 | return NULL; |
| 4595 | } |
| 4596 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4597 | #undef SPLIT_APPEND |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4598 | #undef SPLIT_INSERT |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4599 | |
| 4600 | static |
| 4601 | PyObject *split(PyUnicodeObject *self, |
| 4602 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4603 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4604 | { |
| 4605 | PyObject *list; |
| 4606 | |
| 4607 | if (maxcount < 0) |
| 4608 | maxcount = INT_MAX; |
| 4609 | |
| 4610 | list = PyList_New(0); |
| 4611 | if (!list) |
| 4612 | return NULL; |
| 4613 | |
| 4614 | if (substring == NULL) |
| 4615 | return split_whitespace(self,list,maxcount); |
| 4616 | |
| 4617 | else if (substring->length == 1) |
| 4618 | return split_char(self,list,substring->str[0],maxcount); |
| 4619 | |
| 4620 | else if (substring->length == 0) { |
| 4621 | Py_DECREF(list); |
| 4622 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 4623 | return NULL; |
| 4624 | } |
| 4625 | else |
| 4626 | return split_substring(self,list,substring,maxcount); |
| 4627 | } |
| 4628 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4629 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4630 | PyObject *rsplit(PyUnicodeObject *self, |
| 4631 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4632 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4633 | { |
| 4634 | PyObject *list; |
| 4635 | |
| 4636 | if (maxcount < 0) |
| 4637 | maxcount = INT_MAX; |
| 4638 | |
| 4639 | list = PyList_New(0); |
| 4640 | if (!list) |
| 4641 | return NULL; |
| 4642 | |
| 4643 | if (substring == NULL) |
| 4644 | return rsplit_whitespace(self,list,maxcount); |
| 4645 | |
| 4646 | else if (substring->length == 1) |
| 4647 | return rsplit_char(self,list,substring->str[0],maxcount); |
| 4648 | |
| 4649 | else if (substring->length == 0) { |
| 4650 | Py_DECREF(list); |
| 4651 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 4652 | return NULL; |
| 4653 | } |
| 4654 | else |
| 4655 | return rsplit_substring(self,list,substring,maxcount); |
| 4656 | } |
| 4657 | |
| 4658 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4659 | PyObject *replace(PyUnicodeObject *self, |
| 4660 | PyUnicodeObject *str1, |
| 4661 | PyUnicodeObject *str2, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4662 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4663 | { |
| 4664 | PyUnicodeObject *u; |
| 4665 | |
| 4666 | if (maxcount < 0) |
| 4667 | maxcount = INT_MAX; |
| 4668 | |
| 4669 | if (str1->length == 1 && str2->length == 1) { |
| 4670 | int i; |
| 4671 | |
| 4672 | /* replace characters */ |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4673 | if (!findchar(self->str, self->length, str1->str[0]) && |
| 4674 | PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4675 | /* nothing to replace, return original string */ |
| 4676 | Py_INCREF(self); |
| 4677 | u = self; |
| 4678 | } else { |
| 4679 | Py_UNICODE u1 = str1->str[0]; |
| 4680 | Py_UNICODE u2 = str2->str[0]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4681 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4682 | u = (PyUnicodeObject*) PyUnicode_FromUnicode( |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4683 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4684 | self->length |
| 4685 | ); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4686 | if (u != NULL) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4687 | Py_UNICODE_COPY(u->str, self->str, |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4688 | self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4689 | for (i = 0; i < u->length; i++) |
| 4690 | if (u->str[i] == u1) { |
| 4691 | if (--maxcount < 0) |
| 4692 | break; |
| 4693 | u->str[i] = u2; |
| 4694 | } |
| 4695 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4696 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4697 | |
| 4698 | } else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4699 | Py_ssize_t n, i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4700 | Py_UNICODE *p; |
| 4701 | |
| 4702 | /* replace strings */ |
| 4703 | n = count(self, 0, self->length, str1); |
| 4704 | if (n > maxcount) |
| 4705 | n = maxcount; |
Guido van Rossum | 2023c9b | 2002-08-23 18:50:21 +0000 | [diff] [blame] | 4706 | if (n == 0) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4707 | /* nothing to replace, return original string */ |
Guido van Rossum | 2023c9b | 2002-08-23 18:50:21 +0000 | [diff] [blame] | 4708 | if (PyUnicode_CheckExact(self)) { |
| 4709 | Py_INCREF(self); |
| 4710 | u = self; |
| 4711 | } |
| 4712 | else { |
| 4713 | u = (PyUnicodeObject *) |
| 4714 | PyUnicode_FromUnicode(self->str, self->length); |
| 4715 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4716 | } else { |
| 4717 | u = _PyUnicode_New( |
| 4718 | self->length + n * (str2->length - str1->length)); |
| 4719 | if (u) { |
| 4720 | i = 0; |
| 4721 | p = u->str; |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 4722 | if (str1->length > 0) { |
| 4723 | while (i <= self->length - str1->length) |
| 4724 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 4725 | /* replace string segment */ |
| 4726 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 4727 | p += str2->length; |
| 4728 | i += str1->length; |
| 4729 | if (--n <= 0) { |
| 4730 | /* copy remaining part */ |
| 4731 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 4732 | break; |
| 4733 | } |
| 4734 | } else |
| 4735 | *p++ = self->str[i++]; |
| 4736 | } else { |
| 4737 | while (n > 0) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4738 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 4739 | p += str2->length; |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 4740 | if (--n <= 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4741 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4742 | *p++ = self->str[i++]; |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 4743 | } |
| 4744 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 4745 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4746 | } |
| 4747 | } |
| 4748 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4749 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4750 | return (PyObject *) u; |
| 4751 | } |
| 4752 | |
| 4753 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 4754 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4755 | PyDoc_STRVAR(title__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4756 | "S.title() -> unicode\n\ |
| 4757 | \n\ |
| 4758 | 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] | 4759 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4760 | |
| 4761 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 4762 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4763 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4764 | return fixup(self, fixtitle); |
| 4765 | } |
| 4766 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4767 | PyDoc_STRVAR(capitalize__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4768 | "S.capitalize() -> unicode\n\ |
| 4769 | \n\ |
| 4770 | 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] | 4771 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4772 | |
| 4773 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 4774 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4775 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4776 | return fixup(self, fixcapitalize); |
| 4777 | } |
| 4778 | |
| 4779 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4780 | PyDoc_STRVAR(capwords__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4781 | "S.capwords() -> unicode\n\ |
| 4782 | \n\ |
| 4783 | 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] | 4784 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4785 | |
| 4786 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 4787 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4788 | { |
| 4789 | PyObject *list; |
| 4790 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4791 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4792 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4793 | /* Split into words */ |
| 4794 | list = split(self, NULL, -1); |
| 4795 | if (!list) |
| 4796 | return NULL; |
| 4797 | |
| 4798 | /* Capitalize each word */ |
| 4799 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 4800 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 4801 | fixcapitalize); |
| 4802 | if (item == NULL) |
| 4803 | goto onError; |
| 4804 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 4805 | PyList_SET_ITEM(list, i, item); |
| 4806 | } |
| 4807 | |
| 4808 | /* Join the words to form a new string */ |
| 4809 | item = PyUnicode_Join(NULL, list); |
| 4810 | |
| 4811 | onError: |
| 4812 | Py_DECREF(list); |
| 4813 | return (PyObject *)item; |
| 4814 | } |
| 4815 | #endif |
| 4816 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4817 | /* Argument converter. Coerces to a single unicode character */ |
| 4818 | |
| 4819 | static int |
| 4820 | convert_uc(PyObject *obj, void *addr) |
| 4821 | { |
| 4822 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 4823 | PyObject *uniobj; |
| 4824 | Py_UNICODE *unistr; |
| 4825 | |
| 4826 | uniobj = PyUnicode_FromObject(obj); |
| 4827 | if (uniobj == NULL) { |
| 4828 | PyErr_SetString(PyExc_TypeError, |
| 4829 | "The fill character cannot be converted to Unicode"); |
| 4830 | return 0; |
| 4831 | } |
| 4832 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 4833 | PyErr_SetString(PyExc_TypeError, |
| 4834 | "The fill character must be exactly one character long"); |
| 4835 | Py_DECREF(uniobj); |
| 4836 | return 0; |
| 4837 | } |
| 4838 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 4839 | *fillcharloc = unistr[0]; |
| 4840 | Py_DECREF(uniobj); |
| 4841 | return 1; |
| 4842 | } |
| 4843 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4844 | PyDoc_STRVAR(center__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4845 | "S.center(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4846 | \n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4847 | Return S centered in a Unicode string of length width. Padding is\n\ |
| 4848 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4849 | |
| 4850 | static PyObject * |
| 4851 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 4852 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4853 | Py_ssize_t marg, left; |
| 4854 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4855 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4856 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 4857 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4858 | return NULL; |
| 4859 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4860 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4861 | Py_INCREF(self); |
| 4862 | return (PyObject*) self; |
| 4863 | } |
| 4864 | |
| 4865 | marg = width - self->length; |
| 4866 | left = marg / 2 + (marg & width & 1); |
| 4867 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4868 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4869 | } |
| 4870 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4871 | #if 0 |
| 4872 | |
| 4873 | /* This code should go into some future Unicode collation support |
| 4874 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 4875 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4876 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4877 | /* speedy UTF-16 code point order comparison */ |
| 4878 | /* gleaned from: */ |
| 4879 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 4880 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4881 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4882 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4883 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4884 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 4885 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4886 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4887 | }; |
| 4888 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4889 | static int |
| 4890 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 4891 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4892 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4893 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4894 | Py_UNICODE *s1 = str1->str; |
| 4895 | Py_UNICODE *s2 = str2->str; |
| 4896 | |
| 4897 | len1 = str1->length; |
| 4898 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4899 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4900 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4901 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4902 | |
| 4903 | c1 = *s1++; |
| 4904 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 4905 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4906 | if (c1 > (1<<11) * 26) |
| 4907 | c1 += utf16Fixup[c1>>11]; |
| 4908 | if (c2 > (1<<11) * 26) |
| 4909 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4910 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 4911 | |
| 4912 | if (c1 != c2) |
| 4913 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4914 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4915 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4916 | } |
| 4917 | |
| 4918 | return (len1 < len2) ? -1 : (len1 != len2); |
| 4919 | } |
| 4920 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4921 | #else |
| 4922 | |
| 4923 | static int |
| 4924 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 4925 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4926 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4927 | |
| 4928 | Py_UNICODE *s1 = str1->str; |
| 4929 | Py_UNICODE *s2 = str2->str; |
| 4930 | |
| 4931 | len1 = str1->length; |
| 4932 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4933 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4934 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4935 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4936 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 4937 | c1 = *s1++; |
| 4938 | c2 = *s2++; |
| 4939 | |
| 4940 | if (c1 != c2) |
| 4941 | return (c1 < c2) ? -1 : 1; |
| 4942 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4943 | len1--; len2--; |
| 4944 | } |
| 4945 | |
| 4946 | return (len1 < len2) ? -1 : (len1 != len2); |
| 4947 | } |
| 4948 | |
| 4949 | #endif |
| 4950 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4951 | int PyUnicode_Compare(PyObject *left, |
| 4952 | PyObject *right) |
| 4953 | { |
| 4954 | PyUnicodeObject *u = NULL, *v = NULL; |
| 4955 | int result; |
| 4956 | |
| 4957 | /* Coerce the two arguments */ |
| 4958 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 4959 | if (u == NULL) |
| 4960 | goto onError; |
| 4961 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 4962 | if (v == NULL) |
| 4963 | goto onError; |
| 4964 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 4965 | /* Shortcut for empty or interned objects */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4966 | if (v == u) { |
| 4967 | Py_DECREF(u); |
| 4968 | Py_DECREF(v); |
| 4969 | return 0; |
| 4970 | } |
| 4971 | |
| 4972 | result = unicode_compare(u, v); |
| 4973 | |
| 4974 | Py_DECREF(u); |
| 4975 | Py_DECREF(v); |
| 4976 | return result; |
| 4977 | |
| 4978 | onError: |
| 4979 | Py_XDECREF(u); |
| 4980 | Py_XDECREF(v); |
| 4981 | return -1; |
| 4982 | } |
| 4983 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4984 | int PyUnicode_Contains(PyObject *container, |
| 4985 | PyObject *element) |
| 4986 | { |
| 4987 | PyUnicodeObject *u = NULL, *v = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4988 | int result; |
| 4989 | Py_ssize_t size; |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 4990 | register const Py_UNICODE *lhs, *end, *rhs; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4991 | |
| 4992 | /* Coerce the two arguments */ |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4993 | v = (PyUnicodeObject *)PyUnicode_FromObject(element); |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 4994 | if (v == NULL) { |
| 4995 | PyErr_SetString(PyExc_TypeError, |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 4996 | "'in <string>' requires string as left operand"); |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4997 | goto onError; |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 4998 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4999 | u = (PyUnicodeObject *)PyUnicode_FromObject(container); |
Marc-André Lemburg | 9cd87aa | 2002-10-23 09:02:46 +0000 | [diff] [blame] | 5000 | if (u == NULL) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5001 | goto onError; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5002 | |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 5003 | size = PyUnicode_GET_SIZE(v); |
| 5004 | rhs = PyUnicode_AS_UNICODE(v); |
| 5005 | lhs = PyUnicode_AS_UNICODE(u); |
| 5006 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5007 | result = 0; |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 5008 | if (size == 1) { |
| 5009 | end = lhs + PyUnicode_GET_SIZE(u); |
| 5010 | while (lhs < end) { |
| 5011 | if (*lhs++ == *rhs) { |
| 5012 | result = 1; |
| 5013 | break; |
| 5014 | } |
| 5015 | } |
| 5016 | } |
| 5017 | else { |
| 5018 | end = lhs + (PyUnicode_GET_SIZE(u) - size); |
| 5019 | while (lhs <= end) { |
Barry Warsaw | 6a043f3 | 2002-08-06 19:03:17 +0000 | [diff] [blame] | 5020 | if (memcmp(lhs++, rhs, size * sizeof(Py_UNICODE)) == 0) { |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 5021 | result = 1; |
| 5022 | break; |
| 5023 | } |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5024 | } |
| 5025 | } |
| 5026 | |
| 5027 | Py_DECREF(u); |
| 5028 | Py_DECREF(v); |
| 5029 | return result; |
| 5030 | |
| 5031 | onError: |
| 5032 | Py_XDECREF(u); |
| 5033 | Py_XDECREF(v); |
| 5034 | return -1; |
| 5035 | } |
| 5036 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5037 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 5038 | |
| 5039 | PyObject *PyUnicode_Concat(PyObject *left, |
| 5040 | PyObject *right) |
| 5041 | { |
| 5042 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 5043 | |
| 5044 | /* Coerce the two arguments */ |
| 5045 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 5046 | if (u == NULL) |
| 5047 | goto onError; |
| 5048 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 5049 | if (v == NULL) |
| 5050 | goto onError; |
| 5051 | |
| 5052 | /* Shortcuts */ |
| 5053 | if (v == unicode_empty) { |
| 5054 | Py_DECREF(v); |
| 5055 | return (PyObject *)u; |
| 5056 | } |
| 5057 | if (u == unicode_empty) { |
| 5058 | Py_DECREF(u); |
| 5059 | return (PyObject *)v; |
| 5060 | } |
| 5061 | |
| 5062 | /* Concat the two Unicode strings */ |
| 5063 | w = _PyUnicode_New(u->length + v->length); |
| 5064 | if (w == NULL) |
| 5065 | goto onError; |
| 5066 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 5067 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 5068 | |
| 5069 | Py_DECREF(u); |
| 5070 | Py_DECREF(v); |
| 5071 | return (PyObject *)w; |
| 5072 | |
| 5073 | onError: |
| 5074 | Py_XDECREF(u); |
| 5075 | Py_XDECREF(v); |
| 5076 | return NULL; |
| 5077 | } |
| 5078 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5079 | PyDoc_STRVAR(count__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5080 | "S.count(sub[, start[, end]]) -> int\n\ |
| 5081 | \n\ |
| 5082 | Return the number of occurrences of substring sub in Unicode string\n\ |
| 5083 | S[start:end]. Optional arguments start and end are\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5084 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5085 | |
| 5086 | static PyObject * |
| 5087 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 5088 | { |
| 5089 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5090 | Py_ssize_t start = 0; |
| 5091 | Py_ssize_t end = INT_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5092 | PyObject *result; |
| 5093 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5094 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 5095 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5096 | return NULL; |
| 5097 | |
| 5098 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 5099 | (PyObject *)substring); |
| 5100 | if (substring == NULL) |
| 5101 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5102 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5103 | if (start < 0) |
| 5104 | start += self->length; |
| 5105 | if (start < 0) |
| 5106 | start = 0; |
| 5107 | if (end > self->length) |
| 5108 | end = self->length; |
| 5109 | if (end < 0) |
| 5110 | end += self->length; |
| 5111 | if (end < 0) |
| 5112 | end = 0; |
| 5113 | |
| 5114 | result = PyInt_FromLong((long) count(self, start, end, substring)); |
| 5115 | |
| 5116 | Py_DECREF(substring); |
| 5117 | return result; |
| 5118 | } |
| 5119 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5120 | PyDoc_STRVAR(encode__doc__, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5121 | "S.encode([encoding[,errors]]) -> string or unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5122 | \n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5123 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 5124 | 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] | 5125 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5126 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 5127 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 5128 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5129 | |
| 5130 | static PyObject * |
| 5131 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 5132 | { |
| 5133 | char *encoding = NULL; |
| 5134 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5135 | PyObject *v; |
| 5136 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5137 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 5138 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5139 | v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5140 | if (v == NULL) |
| 5141 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5142 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 5143 | PyErr_Format(PyExc_TypeError, |
| 5144 | "encoder did not return a string/unicode object " |
| 5145 | "(type=%.400s)", |
| 5146 | v->ob_type->tp_name); |
| 5147 | Py_DECREF(v); |
| 5148 | return NULL; |
| 5149 | } |
| 5150 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5151 | |
| 5152 | onError: |
| 5153 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5154 | } |
| 5155 | |
| 5156 | PyDoc_STRVAR(decode__doc__, |
| 5157 | "S.decode([encoding[,errors]]) -> string or unicode\n\ |
| 5158 | \n\ |
| 5159 | Decodes S using the codec registered for encoding. encoding defaults\n\ |
| 5160 | to the default encoding. errors may be given to set a different error\n\ |
| 5161 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 5162 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 5163 | as well as any other name registerd with codecs.register_error that is\n\ |
| 5164 | able to handle UnicodeDecodeErrors."); |
| 5165 | |
| 5166 | static PyObject * |
Marc-André Lemburg | 126b44c | 2004-07-10 12:04:20 +0000 | [diff] [blame] | 5167 | unicode_decode(PyUnicodeObject *self, PyObject *args) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5168 | { |
| 5169 | char *encoding = NULL; |
| 5170 | char *errors = NULL; |
| 5171 | PyObject *v; |
| 5172 | |
| 5173 | if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
| 5174 | return NULL; |
| 5175 | v = PyUnicode_AsDecodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5176 | if (v == NULL) |
| 5177 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5178 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 5179 | PyErr_Format(PyExc_TypeError, |
| 5180 | "decoder did not return a string/unicode object " |
| 5181 | "(type=%.400s)", |
| 5182 | v->ob_type->tp_name); |
| 5183 | Py_DECREF(v); |
| 5184 | return NULL; |
| 5185 | } |
| 5186 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5187 | |
| 5188 | onError: |
| 5189 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5190 | } |
| 5191 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5192 | PyDoc_STRVAR(expandtabs__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5193 | "S.expandtabs([tabsize]) -> unicode\n\ |
| 5194 | \n\ |
| 5195 | 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] | 5196 | 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] | 5197 | |
| 5198 | static PyObject* |
| 5199 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 5200 | { |
| 5201 | Py_UNICODE *e; |
| 5202 | Py_UNICODE *p; |
| 5203 | Py_UNICODE *q; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5204 | Py_ssize_t i, j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5205 | PyUnicodeObject *u; |
| 5206 | int tabsize = 8; |
| 5207 | |
| 5208 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 5209 | return NULL; |
| 5210 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5211 | /* First pass: determine size of output string */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5212 | i = j = 0; |
| 5213 | e = self->str + self->length; |
| 5214 | for (p = self->str; p < e; p++) |
| 5215 | if (*p == '\t') { |
| 5216 | if (tabsize > 0) |
| 5217 | j += tabsize - (j % tabsize); |
| 5218 | } |
| 5219 | else { |
| 5220 | j++; |
| 5221 | if (*p == '\n' || *p == '\r') { |
| 5222 | i += j; |
| 5223 | j = 0; |
| 5224 | } |
| 5225 | } |
| 5226 | |
| 5227 | /* Second pass: create output string and fill it */ |
| 5228 | u = _PyUnicode_New(i + j); |
| 5229 | if (!u) |
| 5230 | return NULL; |
| 5231 | |
| 5232 | j = 0; |
| 5233 | q = u->str; |
| 5234 | |
| 5235 | for (p = self->str; p < e; p++) |
| 5236 | if (*p == '\t') { |
| 5237 | if (tabsize > 0) { |
| 5238 | i = tabsize - (j % tabsize); |
| 5239 | j += i; |
| 5240 | while (i--) |
| 5241 | *q++ = ' '; |
| 5242 | } |
| 5243 | } |
| 5244 | else { |
| 5245 | j++; |
| 5246 | *q++ = *p; |
| 5247 | if (*p == '\n' || *p == '\r') |
| 5248 | j = 0; |
| 5249 | } |
| 5250 | |
| 5251 | return (PyObject*) u; |
| 5252 | } |
| 5253 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5254 | PyDoc_STRVAR(find__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5255 | "S.find(sub [,start [,end]]) -> int\n\ |
| 5256 | \n\ |
| 5257 | Return the lowest index in S where substring sub is found,\n\ |
| 5258 | such that sub is contained within s[start,end]. Optional\n\ |
| 5259 | arguments start and end are interpreted as in slice notation.\n\ |
| 5260 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5261 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5262 | |
| 5263 | static PyObject * |
| 5264 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 5265 | { |
| 5266 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5267 | Py_ssize_t start = 0; |
| 5268 | Py_ssize_t end = INT_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5269 | PyObject *result; |
| 5270 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5271 | if (!PyArg_ParseTuple(args, "O|O&O&:find", &substring, |
| 5272 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5273 | return NULL; |
| 5274 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 5275 | (PyObject *)substring); |
| 5276 | if (substring == NULL) |
| 5277 | return NULL; |
| 5278 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5279 | result = PyInt_FromSsize_t(findstring(self, substring, start, end, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5280 | |
| 5281 | Py_DECREF(substring); |
| 5282 | return result; |
| 5283 | } |
| 5284 | |
| 5285 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5286 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5287 | { |
| 5288 | if (index < 0 || index >= self->length) { |
| 5289 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 5290 | return NULL; |
| 5291 | } |
| 5292 | |
| 5293 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 5294 | } |
| 5295 | |
| 5296 | static long |
| 5297 | unicode_hash(PyUnicodeObject *self) |
| 5298 | { |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5299 | /* Since Unicode objects compare equal to their ASCII string |
| 5300 | counterparts, they should use the individual character values |
| 5301 | as basis for their hash value. This is needed to assure that |
| 5302 | strings and Unicode objects behave in the same way as |
| 5303 | dictionary keys. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5304 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5305 | register Py_ssize_t len; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5306 | register Py_UNICODE *p; |
| 5307 | register long x; |
| 5308 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5309 | if (self->hash != -1) |
| 5310 | return self->hash; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5311 | len = PyUnicode_GET_SIZE(self); |
| 5312 | p = PyUnicode_AS_UNICODE(self); |
| 5313 | x = *p << 7; |
| 5314 | while (--len >= 0) |
| 5315 | x = (1000003*x) ^ *p++; |
| 5316 | x ^= PyUnicode_GET_SIZE(self); |
| 5317 | if (x == -1) |
| 5318 | x = -2; |
| 5319 | self->hash = x; |
| 5320 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5321 | } |
| 5322 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5323 | PyDoc_STRVAR(index__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5324 | "S.index(sub [,start [,end]]) -> int\n\ |
| 5325 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5326 | 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] | 5327 | |
| 5328 | static PyObject * |
| 5329 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 5330 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5331 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5332 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5333 | Py_ssize_t start = 0; |
| 5334 | Py_ssize_t end = INT_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5335 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5336 | if (!PyArg_ParseTuple(args, "O|O&O&:index", &substring, |
| 5337 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5338 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5339 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5340 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 5341 | (PyObject *)substring); |
| 5342 | if (substring == NULL) |
| 5343 | return NULL; |
| 5344 | |
| 5345 | result = findstring(self, substring, start, end, 1); |
| 5346 | |
| 5347 | Py_DECREF(substring); |
| 5348 | if (result < 0) { |
| 5349 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 5350 | return NULL; |
| 5351 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5352 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5353 | } |
| 5354 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5355 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5356 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5357 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5358 | 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] | 5359 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5360 | |
| 5361 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5362 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5363 | { |
| 5364 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5365 | register const Py_UNICODE *e; |
| 5366 | int cased; |
| 5367 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5368 | /* Shortcut for single character strings */ |
| 5369 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5370 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5371 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5372 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5373 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5374 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5375 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5376 | e = p + PyUnicode_GET_SIZE(self); |
| 5377 | cased = 0; |
| 5378 | for (; p < e; p++) { |
| 5379 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5380 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5381 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5382 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5383 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 5384 | cased = 1; |
| 5385 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5386 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5387 | } |
| 5388 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5389 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5390 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5391 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5392 | 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] | 5393 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5394 | |
| 5395 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5396 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5397 | { |
| 5398 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5399 | register const Py_UNICODE *e; |
| 5400 | int cased; |
| 5401 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5402 | /* Shortcut for single character strings */ |
| 5403 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5404 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5405 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5406 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5407 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5408 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5409 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5410 | e = p + PyUnicode_GET_SIZE(self); |
| 5411 | cased = 0; |
| 5412 | for (; p < e; p++) { |
| 5413 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5414 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5415 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5416 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5417 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 5418 | cased = 1; |
| 5419 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5420 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5421 | } |
| 5422 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5423 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5424 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5425 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5426 | Return True if S is a titlecased string and there is at least one\n\ |
| 5427 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 5428 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 5429 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5430 | |
| 5431 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5432 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5433 | { |
| 5434 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5435 | register const Py_UNICODE *e; |
| 5436 | int cased, previous_is_cased; |
| 5437 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5438 | /* Shortcut for single character strings */ |
| 5439 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5440 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 5441 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5442 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5443 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5444 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5445 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5446 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5447 | e = p + PyUnicode_GET_SIZE(self); |
| 5448 | cased = 0; |
| 5449 | previous_is_cased = 0; |
| 5450 | for (; p < e; p++) { |
| 5451 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5452 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5453 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 5454 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5455 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5456 | previous_is_cased = 1; |
| 5457 | cased = 1; |
| 5458 | } |
| 5459 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 5460 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5461 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5462 | previous_is_cased = 1; |
| 5463 | cased = 1; |
| 5464 | } |
| 5465 | else |
| 5466 | previous_is_cased = 0; |
| 5467 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5468 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5469 | } |
| 5470 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5471 | PyDoc_STRVAR(isspace__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5472 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5473 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5474 | Return True if all characters in S are whitespace\n\ |
| 5475 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5476 | |
| 5477 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5478 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5479 | { |
| 5480 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5481 | register const Py_UNICODE *e; |
| 5482 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5483 | /* Shortcut for single character strings */ |
| 5484 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5485 | Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5486 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5487 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5488 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5489 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5490 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5491 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5492 | e = p + PyUnicode_GET_SIZE(self); |
| 5493 | for (; p < e; p++) { |
| 5494 | if (!Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5495 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5496 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5497 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5498 | } |
| 5499 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5500 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5501 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5502 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5503 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5504 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5505 | |
| 5506 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5507 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5508 | { |
| 5509 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5510 | register const Py_UNICODE *e; |
| 5511 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5512 | /* Shortcut for single character strings */ |
| 5513 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5514 | Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5515 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5516 | |
| 5517 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5518 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5519 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5520 | |
| 5521 | e = p + PyUnicode_GET_SIZE(self); |
| 5522 | for (; p < e; p++) { |
| 5523 | if (!Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5524 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5525 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5526 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5527 | } |
| 5528 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5529 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5530 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5531 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5532 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5533 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5534 | |
| 5535 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5536 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5537 | { |
| 5538 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5539 | register const Py_UNICODE *e; |
| 5540 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5541 | /* Shortcut for single character strings */ |
| 5542 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5543 | Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5544 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5545 | |
| 5546 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5547 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5548 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5549 | |
| 5550 | e = p + PyUnicode_GET_SIZE(self); |
| 5551 | for (; p < e; p++) { |
| 5552 | if (!Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5553 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5554 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5555 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5556 | } |
| 5557 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5558 | PyDoc_STRVAR(isdecimal__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5559 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5560 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5561 | 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] | 5562 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5563 | |
| 5564 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5565 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5566 | { |
| 5567 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5568 | register const Py_UNICODE *e; |
| 5569 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5570 | /* Shortcut for single character strings */ |
| 5571 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5572 | Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5573 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5574 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5575 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5576 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5577 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5578 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5579 | e = p + PyUnicode_GET_SIZE(self); |
| 5580 | for (; p < e; p++) { |
| 5581 | if (!Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5582 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5583 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5584 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5585 | } |
| 5586 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5587 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5588 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5589 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5590 | Return True if all characters in S are digits\n\ |
| 5591 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5592 | |
| 5593 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5594 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5595 | { |
| 5596 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5597 | register const Py_UNICODE *e; |
| 5598 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5599 | /* Shortcut for single character strings */ |
| 5600 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5601 | Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5602 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5603 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5604 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5605 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5606 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5607 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5608 | e = p + PyUnicode_GET_SIZE(self); |
| 5609 | for (; p < e; p++) { |
| 5610 | if (!Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5611 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5612 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5613 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5614 | } |
| 5615 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5616 | PyDoc_STRVAR(isnumeric__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5617 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5618 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5619 | 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] | 5620 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5621 | |
| 5622 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5623 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5624 | { |
| 5625 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5626 | register const Py_UNICODE *e; |
| 5627 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5628 | /* Shortcut for single character strings */ |
| 5629 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5630 | Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5631 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5632 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5633 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5634 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5635 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5636 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5637 | e = p + PyUnicode_GET_SIZE(self); |
| 5638 | for (; p < e; p++) { |
| 5639 | if (!Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5640 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5641 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5642 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5643 | } |
| 5644 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5645 | PyDoc_STRVAR(join__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5646 | "S.join(sequence) -> unicode\n\ |
| 5647 | \n\ |
| 5648 | 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] | 5649 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5650 | |
| 5651 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5652 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5653 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5654 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5655 | } |
| 5656 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5657 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5658 | unicode_length(PyUnicodeObject *self) |
| 5659 | { |
| 5660 | return self->length; |
| 5661 | } |
| 5662 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5663 | PyDoc_STRVAR(ljust__doc__, |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 5664 | "S.ljust(width[, fillchar]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5665 | \n\ |
| 5666 | 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] | 5667 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5668 | |
| 5669 | static PyObject * |
| 5670 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 5671 | { |
| 5672 | int width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5673 | Py_UNICODE fillchar = ' '; |
| 5674 | |
| 5675 | if (!PyArg_ParseTuple(args, "i|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5676 | return NULL; |
| 5677 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5678 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5679 | Py_INCREF(self); |
| 5680 | return (PyObject*) self; |
| 5681 | } |
| 5682 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5683 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5684 | } |
| 5685 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5686 | PyDoc_STRVAR(lower__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5687 | "S.lower() -> unicode\n\ |
| 5688 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5689 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5690 | |
| 5691 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5692 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5693 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5694 | return fixup(self, fixlower); |
| 5695 | } |
| 5696 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5697 | #define LEFTSTRIP 0 |
| 5698 | #define RIGHTSTRIP 1 |
| 5699 | #define BOTHSTRIP 2 |
| 5700 | |
| 5701 | /* Arrays indexed by above */ |
| 5702 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 5703 | |
| 5704 | #define STRIPNAME(i) (stripformat[i]+3) |
| 5705 | |
| 5706 | static const Py_UNICODE * |
| 5707 | unicode_memchr(const Py_UNICODE *s, Py_UNICODE c, size_t n) |
| 5708 | { |
Tim Peters | 030a5ce | 2002-04-22 19:00:10 +0000 | [diff] [blame] | 5709 | size_t i; |
| 5710 | for (i = 0; i < n; ++i) |
| 5711 | if (s[i] == c) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5712 | return s+i; |
| 5713 | return NULL; |
| 5714 | } |
| 5715 | |
| 5716 | /* externally visible for str.strip(unicode) */ |
| 5717 | PyObject * |
| 5718 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 5719 | { |
| 5720 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5721 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5722 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5723 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 5724 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5725 | |
| 5726 | i = 0; |
| 5727 | if (striptype != RIGHTSTRIP) { |
| 5728 | while (i < len && unicode_memchr(sep, s[i], seplen)) { |
| 5729 | i++; |
| 5730 | } |
| 5731 | } |
| 5732 | |
| 5733 | j = len; |
| 5734 | if (striptype != LEFTSTRIP) { |
| 5735 | do { |
| 5736 | j--; |
| 5737 | } while (j >= i && unicode_memchr(sep, s[j], seplen)); |
| 5738 | j++; |
| 5739 | } |
| 5740 | |
| 5741 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 5742 | Py_INCREF(self); |
| 5743 | return (PyObject*)self; |
| 5744 | } |
| 5745 | else |
| 5746 | return PyUnicode_FromUnicode(s+i, j-i); |
| 5747 | } |
| 5748 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5749 | |
| 5750 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5751 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5752 | { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5753 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5754 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5755 | |
| 5756 | i = 0; |
| 5757 | if (striptype != RIGHTSTRIP) { |
| 5758 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 5759 | i++; |
| 5760 | } |
| 5761 | } |
| 5762 | |
| 5763 | j = len; |
| 5764 | if (striptype != LEFTSTRIP) { |
| 5765 | do { |
| 5766 | j--; |
| 5767 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 5768 | j++; |
| 5769 | } |
| 5770 | |
| 5771 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 5772 | Py_INCREF(self); |
| 5773 | return (PyObject*)self; |
| 5774 | } |
| 5775 | else |
| 5776 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5777 | } |
| 5778 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5779 | |
| 5780 | static PyObject * |
| 5781 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 5782 | { |
| 5783 | PyObject *sep = NULL; |
| 5784 | |
| 5785 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 5786 | return NULL; |
| 5787 | |
| 5788 | if (sep != NULL && sep != Py_None) { |
| 5789 | if (PyUnicode_Check(sep)) |
| 5790 | return _PyUnicode_XStrip(self, striptype, sep); |
| 5791 | else if (PyString_Check(sep)) { |
| 5792 | PyObject *res; |
| 5793 | sep = PyUnicode_FromObject(sep); |
| 5794 | if (sep==NULL) |
| 5795 | return NULL; |
| 5796 | res = _PyUnicode_XStrip(self, striptype, sep); |
| 5797 | Py_DECREF(sep); |
| 5798 | return res; |
| 5799 | } |
| 5800 | else { |
| 5801 | PyErr_Format(PyExc_TypeError, |
| 5802 | "%s arg must be None, unicode or str", |
| 5803 | STRIPNAME(striptype)); |
| 5804 | return NULL; |
| 5805 | } |
| 5806 | } |
| 5807 | |
| 5808 | return do_strip(self, striptype); |
| 5809 | } |
| 5810 | |
| 5811 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5812 | PyDoc_STRVAR(strip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5813 | "S.strip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5814 | \n\ |
| 5815 | Return a copy of the string S with leading and trailing\n\ |
| 5816 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5817 | If chars is given and not None, remove characters in chars instead.\n\ |
| 5818 | 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] | 5819 | |
| 5820 | static PyObject * |
| 5821 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 5822 | { |
| 5823 | if (PyTuple_GET_SIZE(args) == 0) |
| 5824 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 5825 | else |
| 5826 | return do_argstrip(self, BOTHSTRIP, args); |
| 5827 | } |
| 5828 | |
| 5829 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5830 | PyDoc_STRVAR(lstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5831 | "S.lstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5832 | \n\ |
| 5833 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5834 | If chars is given and not None, remove characters in chars instead.\n\ |
| 5835 | 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] | 5836 | |
| 5837 | static PyObject * |
| 5838 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 5839 | { |
| 5840 | if (PyTuple_GET_SIZE(args) == 0) |
| 5841 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 5842 | else |
| 5843 | return do_argstrip(self, LEFTSTRIP, args); |
| 5844 | } |
| 5845 | |
| 5846 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5847 | PyDoc_STRVAR(rstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5848 | "S.rstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5849 | \n\ |
| 5850 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5851 | If chars is given and not None, remove characters in chars instead.\n\ |
| 5852 | 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] | 5853 | |
| 5854 | static PyObject * |
| 5855 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 5856 | { |
| 5857 | if (PyTuple_GET_SIZE(args) == 0) |
| 5858 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 5859 | else |
| 5860 | return do_argstrip(self, RIGHTSTRIP, args); |
| 5861 | } |
| 5862 | |
| 5863 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5864 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5865 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5866 | { |
| 5867 | PyUnicodeObject *u; |
| 5868 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5869 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 5870 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5871 | |
| 5872 | if (len < 0) |
| 5873 | len = 0; |
| 5874 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5875 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5876 | /* no repeat, return original string */ |
| 5877 | Py_INCREF(str); |
| 5878 | return (PyObject*) str; |
| 5879 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 5880 | |
| 5881 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 5882 | * needed doesn't overflow size_t |
| 5883 | */ |
| 5884 | nchars = len * str->length; |
| 5885 | if (len && nchars / len != str->length) { |
| 5886 | PyErr_SetString(PyExc_OverflowError, |
| 5887 | "repeated string is too long"); |
| 5888 | return NULL; |
| 5889 | } |
| 5890 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 5891 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 5892 | PyErr_SetString(PyExc_OverflowError, |
| 5893 | "repeated string is too long"); |
| 5894 | return NULL; |
| 5895 | } |
| 5896 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5897 | if (!u) |
| 5898 | return NULL; |
| 5899 | |
| 5900 | p = u->str; |
| 5901 | |
| 5902 | while (len-- > 0) { |
| 5903 | Py_UNICODE_COPY(p, str->str, str->length); |
| 5904 | p += str->length; |
| 5905 | } |
| 5906 | |
| 5907 | return (PyObject*) u; |
| 5908 | } |
| 5909 | |
| 5910 | PyObject *PyUnicode_Replace(PyObject *obj, |
| 5911 | PyObject *subobj, |
| 5912 | PyObject *replobj, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5913 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5914 | { |
| 5915 | PyObject *self; |
| 5916 | PyObject *str1; |
| 5917 | PyObject *str2; |
| 5918 | PyObject *result; |
| 5919 | |
| 5920 | self = PyUnicode_FromObject(obj); |
| 5921 | if (self == NULL) |
| 5922 | return NULL; |
| 5923 | str1 = PyUnicode_FromObject(subobj); |
| 5924 | if (str1 == NULL) { |
| 5925 | Py_DECREF(self); |
| 5926 | return NULL; |
| 5927 | } |
| 5928 | str2 = PyUnicode_FromObject(replobj); |
| 5929 | if (str2 == NULL) { |
| 5930 | Py_DECREF(self); |
| 5931 | Py_DECREF(str1); |
| 5932 | return NULL; |
| 5933 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5934 | result = replace((PyUnicodeObject *)self, |
| 5935 | (PyUnicodeObject *)str1, |
| 5936 | (PyUnicodeObject *)str2, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5937 | maxcount); |
| 5938 | Py_DECREF(self); |
| 5939 | Py_DECREF(str1); |
| 5940 | Py_DECREF(str2); |
| 5941 | return result; |
| 5942 | } |
| 5943 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5944 | PyDoc_STRVAR(replace__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5945 | "S.replace (old, new[, maxsplit]) -> unicode\n\ |
| 5946 | \n\ |
| 5947 | Return a copy of S with all occurrences of substring\n\ |
| 5948 | 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] | 5949 | given, only the first maxsplit occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5950 | |
| 5951 | static PyObject* |
| 5952 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 5953 | { |
| 5954 | PyUnicodeObject *str1; |
| 5955 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5956 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5957 | PyObject *result; |
| 5958 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5959 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5960 | return NULL; |
| 5961 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 5962 | if (str1 == NULL) |
| 5963 | return NULL; |
| 5964 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 5965 | if (str2 == NULL) { |
| 5966 | Py_DECREF(str1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5967 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 5968 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5969 | |
| 5970 | result = replace(self, str1, str2, maxcount); |
| 5971 | |
| 5972 | Py_DECREF(str1); |
| 5973 | Py_DECREF(str2); |
| 5974 | return result; |
| 5975 | } |
| 5976 | |
| 5977 | static |
| 5978 | PyObject *unicode_repr(PyObject *unicode) |
| 5979 | { |
| 5980 | return unicodeescape_string(PyUnicode_AS_UNICODE(unicode), |
| 5981 | PyUnicode_GET_SIZE(unicode), |
| 5982 | 1); |
| 5983 | } |
| 5984 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5985 | PyDoc_STRVAR(rfind__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5986 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 5987 | \n\ |
| 5988 | Return the highest index in S where substring sub is found,\n\ |
| 5989 | such that sub is contained within s[start,end]. Optional\n\ |
| 5990 | arguments start and end are interpreted as in slice notation.\n\ |
| 5991 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5992 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5993 | |
| 5994 | static PyObject * |
| 5995 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 5996 | { |
| 5997 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5998 | Py_ssize_t start = 0; |
| 5999 | Py_ssize_t end = INT_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6000 | PyObject *result; |
| 6001 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6002 | if (!PyArg_ParseTuple(args, "O|O&O&:rfind", &substring, |
| 6003 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6004 | return NULL; |
| 6005 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6006 | (PyObject *)substring); |
| 6007 | if (substring == NULL) |
| 6008 | return NULL; |
| 6009 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6010 | result = PyInt_FromSsize_t(findstring(self, substring, start, end, -1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6011 | |
| 6012 | Py_DECREF(substring); |
| 6013 | return result; |
| 6014 | } |
| 6015 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6016 | PyDoc_STRVAR(rindex__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6017 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 6018 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6019 | 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] | 6020 | |
| 6021 | static PyObject * |
| 6022 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 6023 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6024 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6025 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6026 | Py_ssize_t start = 0; |
| 6027 | Py_ssize_t end = INT_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6028 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6029 | if (!PyArg_ParseTuple(args, "O|O&O&:rindex", &substring, |
| 6030 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6031 | return NULL; |
| 6032 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6033 | (PyObject *)substring); |
| 6034 | if (substring == NULL) |
| 6035 | return NULL; |
| 6036 | |
| 6037 | result = findstring(self, substring, start, end, -1); |
| 6038 | |
| 6039 | Py_DECREF(substring); |
| 6040 | if (result < 0) { |
| 6041 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 6042 | return NULL; |
| 6043 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6044 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6045 | } |
| 6046 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6047 | PyDoc_STRVAR(rjust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6048 | "S.rjust(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6049 | \n\ |
| 6050 | 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] | 6051 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6052 | |
| 6053 | static PyObject * |
| 6054 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 6055 | { |
| 6056 | int width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6057 | Py_UNICODE fillchar = ' '; |
| 6058 | |
| 6059 | if (!PyArg_ParseTuple(args, "i|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6060 | return NULL; |
| 6061 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6062 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6063 | Py_INCREF(self); |
| 6064 | return (PyObject*) self; |
| 6065 | } |
| 6066 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6067 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6068 | } |
| 6069 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6070 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6071 | 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] | 6072 | { |
| 6073 | /* standard clamping */ |
| 6074 | if (start < 0) |
| 6075 | start = 0; |
| 6076 | if (end < 0) |
| 6077 | end = 0; |
| 6078 | if (end > self->length) |
| 6079 | end = self->length; |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6080 | if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6081 | /* full slice, return original string */ |
| 6082 | Py_INCREF(self); |
| 6083 | return (PyObject*) self; |
| 6084 | } |
| 6085 | if (start > end) |
| 6086 | start = end; |
| 6087 | /* copy slice */ |
| 6088 | return (PyObject*) PyUnicode_FromUnicode(self->str + start, |
| 6089 | end - start); |
| 6090 | } |
| 6091 | |
| 6092 | PyObject *PyUnicode_Split(PyObject *s, |
| 6093 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6094 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6095 | { |
| 6096 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6097 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6098 | s = PyUnicode_FromObject(s); |
| 6099 | if (s == NULL) |
| 6100 | return NULL; |
| 6101 | if (sep != NULL) { |
| 6102 | sep = PyUnicode_FromObject(sep); |
| 6103 | if (sep == NULL) { |
| 6104 | Py_DECREF(s); |
| 6105 | return NULL; |
| 6106 | } |
| 6107 | } |
| 6108 | |
| 6109 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 6110 | |
| 6111 | Py_DECREF(s); |
| 6112 | Py_XDECREF(sep); |
| 6113 | return result; |
| 6114 | } |
| 6115 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6116 | PyDoc_STRVAR(split__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6117 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 6118 | \n\ |
| 6119 | Return a list of the words in S, using sep as the\n\ |
| 6120 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Thomas Heller | ca0d2cb | 2004-09-15 11:41:32 +0000 | [diff] [blame] | 6121 | 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] | 6122 | any whitespace string is a separator."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6123 | |
| 6124 | static PyObject* |
| 6125 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 6126 | { |
| 6127 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6128 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6129 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6130 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6131 | return NULL; |
| 6132 | |
| 6133 | if (substring == Py_None) |
| 6134 | return split(self, NULL, maxcount); |
| 6135 | else if (PyUnicode_Check(substring)) |
| 6136 | return split(self, (PyUnicodeObject *)substring, maxcount); |
| 6137 | else |
| 6138 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
| 6139 | } |
| 6140 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6141 | PyObject *PyUnicode_RSplit(PyObject *s, |
| 6142 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6143 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6144 | { |
| 6145 | PyObject *result; |
| 6146 | |
| 6147 | s = PyUnicode_FromObject(s); |
| 6148 | if (s == NULL) |
| 6149 | return NULL; |
| 6150 | if (sep != NULL) { |
| 6151 | sep = PyUnicode_FromObject(sep); |
| 6152 | if (sep == NULL) { |
| 6153 | Py_DECREF(s); |
| 6154 | return NULL; |
| 6155 | } |
| 6156 | } |
| 6157 | |
| 6158 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 6159 | |
| 6160 | Py_DECREF(s); |
| 6161 | Py_XDECREF(sep); |
| 6162 | return result; |
| 6163 | } |
| 6164 | |
| 6165 | PyDoc_STRVAR(rsplit__doc__, |
| 6166 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
| 6167 | \n\ |
| 6168 | Return a list of the words in S, using sep as the\n\ |
| 6169 | delimiter string, starting at the end of the string and\n\ |
| 6170 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 6171 | splits are done. If sep is not specified, any whitespace string\n\ |
| 6172 | is a separator."); |
| 6173 | |
| 6174 | static PyObject* |
| 6175 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 6176 | { |
| 6177 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6178 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6179 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6180 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6181 | return NULL; |
| 6182 | |
| 6183 | if (substring == Py_None) |
| 6184 | return rsplit(self, NULL, maxcount); |
| 6185 | else if (PyUnicode_Check(substring)) |
| 6186 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
| 6187 | else |
| 6188 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
| 6189 | } |
| 6190 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6191 | PyDoc_STRVAR(splitlines__doc__, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6192 | "S.splitlines([keepends]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6193 | \n\ |
| 6194 | 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] | 6195 | 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] | 6196 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6197 | |
| 6198 | static PyObject* |
| 6199 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 6200 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6201 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6202 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6203 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6204 | return NULL; |
| 6205 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6206 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6207 | } |
| 6208 | |
| 6209 | static |
| 6210 | PyObject *unicode_str(PyUnicodeObject *self) |
| 6211 | { |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6212 | return PyUnicode_AsEncodedString((PyObject *)self, NULL, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6213 | } |
| 6214 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6215 | PyDoc_STRVAR(swapcase__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6216 | "S.swapcase() -> unicode\n\ |
| 6217 | \n\ |
| 6218 | 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] | 6219 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6220 | |
| 6221 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6222 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6223 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6224 | return fixup(self, fixswapcase); |
| 6225 | } |
| 6226 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6227 | PyDoc_STRVAR(translate__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6228 | "S.translate(table) -> unicode\n\ |
| 6229 | \n\ |
| 6230 | Return a copy of the string S, where all characters have been mapped\n\ |
| 6231 | 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] | 6232 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ |
| 6233 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 6234 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6235 | |
| 6236 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6237 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6238 | { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6239 | return PyUnicode_TranslateCharmap(self->str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6240 | self->length, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6241 | table, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6242 | "ignore"); |
| 6243 | } |
| 6244 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6245 | PyDoc_STRVAR(upper__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6246 | "S.upper() -> unicode\n\ |
| 6247 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6248 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6249 | |
| 6250 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6251 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6252 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6253 | return fixup(self, fixupper); |
| 6254 | } |
| 6255 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6256 | PyDoc_STRVAR(zfill__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6257 | "S.zfill(width) -> unicode\n\ |
| 6258 | \n\ |
| 6259 | 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] | 6260 | of the specified width. The string x is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6261 | |
| 6262 | static PyObject * |
| 6263 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 6264 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6265 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6266 | PyUnicodeObject *u; |
| 6267 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6268 | Py_ssize_t width; |
| 6269 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6270 | return NULL; |
| 6271 | |
| 6272 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 6273 | if (PyUnicode_CheckExact(self)) { |
| 6274 | Py_INCREF(self); |
| 6275 | return (PyObject*) self; |
| 6276 | } |
| 6277 | else |
| 6278 | return PyUnicode_FromUnicode( |
| 6279 | PyUnicode_AS_UNICODE(self), |
| 6280 | PyUnicode_GET_SIZE(self) |
| 6281 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6282 | } |
| 6283 | |
| 6284 | fill = width - self->length; |
| 6285 | |
| 6286 | u = pad(self, fill, 0, '0'); |
| 6287 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 6288 | if (u == NULL) |
| 6289 | return NULL; |
| 6290 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6291 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 6292 | /* move sign to beginning of string */ |
| 6293 | u->str[0] = u->str[fill]; |
| 6294 | u->str[fill] = '0'; |
| 6295 | } |
| 6296 | |
| 6297 | return (PyObject*) u; |
| 6298 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6299 | |
| 6300 | #if 0 |
| 6301 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6302 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6303 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6304 | return PyInt_FromLong(unicode_freelist_size); |
| 6305 | } |
| 6306 | #endif |
| 6307 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6308 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6309 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6310 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 6311 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 6312 | With optional start, test S beginning at that position.\n\ |
| 6313 | With optional end, stop comparing S at that position."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6314 | |
| 6315 | static PyObject * |
| 6316 | unicode_startswith(PyUnicodeObject *self, |
| 6317 | PyObject *args) |
| 6318 | { |
| 6319 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6320 | Py_ssize_t start = 0; |
| 6321 | Py_ssize_t end = INT_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6322 | PyObject *result; |
| 6323 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6324 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &substring, |
| 6325 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6326 | return NULL; |
| 6327 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6328 | (PyObject *)substring); |
| 6329 | if (substring == NULL) |
| 6330 | return NULL; |
| 6331 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6332 | result = PyBool_FromLong(tailmatch(self, substring, start, end, -1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6333 | |
| 6334 | Py_DECREF(substring); |
| 6335 | return result; |
| 6336 | } |
| 6337 | |
| 6338 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6339 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6340 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6341 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 6342 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 6343 | With optional start, test S beginning at that position.\n\ |
| 6344 | With optional end, stop comparing S at that position."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6345 | |
| 6346 | static PyObject * |
| 6347 | unicode_endswith(PyUnicodeObject *self, |
| 6348 | PyObject *args) |
| 6349 | { |
| 6350 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6351 | Py_ssize_t start = 0; |
| 6352 | Py_ssize_t end = INT_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6353 | PyObject *result; |
| 6354 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6355 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &substring, |
| 6356 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6357 | return NULL; |
| 6358 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6359 | (PyObject *)substring); |
| 6360 | if (substring == NULL) |
| 6361 | return NULL; |
| 6362 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6363 | result = PyBool_FromLong(tailmatch(self, substring, start, end, +1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6364 | |
| 6365 | Py_DECREF(substring); |
| 6366 | return result; |
| 6367 | } |
| 6368 | |
| 6369 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 6370 | |
| 6371 | static PyObject * |
| 6372 | unicode_getnewargs(PyUnicodeObject *v) |
| 6373 | { |
| 6374 | return Py_BuildValue("(u#)", v->str, v->length); |
| 6375 | } |
| 6376 | |
| 6377 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6378 | static PyMethodDef unicode_methods[] = { |
| 6379 | |
| 6380 | /* Order is according to common usage: often used methods should |
| 6381 | appear first, since lookup is done sequentially. */ |
| 6382 | |
Georg Brandl | ecdc0a9 | 2006-03-30 12:19:07 +0000 | [diff] [blame] | 6383 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6384 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 6385 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6386 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6387 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 6388 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 6389 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 6390 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 6391 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 6392 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 6393 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
| 6394 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 6395 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 6396 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6397 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6398 | {"decode", (PyCFunction) unicode_decode, METH_VARARGS, decode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6399 | /* {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */ |
| 6400 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 6401 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 6402 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6403 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6404 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6405 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6406 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 6407 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 6408 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 6409 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 6410 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 6411 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 6412 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 6413 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 6414 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 6415 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 6416 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 6417 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 6418 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 6419 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6420 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 6421 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6422 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6423 | #endif |
| 6424 | |
| 6425 | #if 0 |
| 6426 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6427 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6428 | #endif |
| 6429 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 6430 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6431 | {NULL, NULL} |
| 6432 | }; |
| 6433 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 6434 | static PyObject * |
| 6435 | unicode_mod(PyObject *v, PyObject *w) |
| 6436 | { |
| 6437 | if (!PyUnicode_Check(v)) { |
| 6438 | Py_INCREF(Py_NotImplemented); |
| 6439 | return Py_NotImplemented; |
| 6440 | } |
| 6441 | return PyUnicode_Format(v, w); |
| 6442 | } |
| 6443 | |
| 6444 | static PyNumberMethods unicode_as_number = { |
| 6445 | 0, /*nb_add*/ |
| 6446 | 0, /*nb_subtract*/ |
| 6447 | 0, /*nb_multiply*/ |
| 6448 | 0, /*nb_divide*/ |
| 6449 | unicode_mod, /*nb_remainder*/ |
| 6450 | }; |
| 6451 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6452 | static PySequenceMethods unicode_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6453 | (lenfunc) unicode_length, /* sq_length */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 6454 | PyUnicode_Concat, /* sq_concat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6455 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 6456 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 6457 | (ssizessizeargfunc) unicode_slice, /* sq_slice */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6458 | 0, /* sq_ass_item */ |
| 6459 | 0, /* sq_ass_slice */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 6460 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6461 | }; |
| 6462 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 6463 | #define HASINDEX(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_HAVE_INDEX) |
| 6464 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6465 | static PyObject* |
| 6466 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 6467 | { |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 6468 | PyNumberMethods *nb = item->ob_type->tp_as_number; |
| 6469 | if (nb != NULL && HASINDEX(item) && nb->nb_index != NULL) { |
| 6470 | Py_ssize_t i = nb->nb_index(item); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6471 | if (i == -1 && PyErr_Occurred()) |
| 6472 | return NULL; |
| 6473 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6474 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6475 | return unicode_getitem(self, i); |
| 6476 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6477 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6478 | Py_UNICODE* source_buf; |
| 6479 | Py_UNICODE* result_buf; |
| 6480 | PyObject* result; |
| 6481 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6482 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6483 | &start, &stop, &step, &slicelength) < 0) { |
| 6484 | return NULL; |
| 6485 | } |
| 6486 | |
| 6487 | if (slicelength <= 0) { |
| 6488 | return PyUnicode_FromUnicode(NULL, 0); |
| 6489 | } else { |
| 6490 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 6491 | result_buf = (Py_UNICODE *)PyMem_MALLOC(slicelength* |
| 6492 | sizeof(Py_UNICODE)); |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6493 | |
| 6494 | if (result_buf == NULL) |
| 6495 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6496 | |
| 6497 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 6498 | result_buf[i] = source_buf[cur]; |
| 6499 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6500 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6501 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
| 6502 | PyMem_FREE(result_buf); |
| 6503 | return result; |
| 6504 | } |
| 6505 | } else { |
| 6506 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 6507 | return NULL; |
| 6508 | } |
| 6509 | } |
| 6510 | |
| 6511 | static PyMappingMethods unicode_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6512 | (lenfunc)unicode_length, /* mp_length */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6513 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 6514 | (objobjargproc)0, /* mp_ass_subscript */ |
| 6515 | }; |
| 6516 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6517 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6518 | unicode_buffer_getreadbuf(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6519 | Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6520 | const void **ptr) |
| 6521 | { |
| 6522 | if (index != 0) { |
| 6523 | PyErr_SetString(PyExc_SystemError, |
| 6524 | "accessing non-existent unicode segment"); |
| 6525 | return -1; |
| 6526 | } |
| 6527 | *ptr = (void *) self->str; |
| 6528 | return PyUnicode_GET_DATA_SIZE(self); |
| 6529 | } |
| 6530 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6531 | static Py_ssize_t |
| 6532 | unicode_buffer_getwritebuf(PyUnicodeObject *self, Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6533 | const void **ptr) |
| 6534 | { |
| 6535 | PyErr_SetString(PyExc_TypeError, |
Neal Norwitz | 20e7213 | 2002-06-13 21:25:17 +0000 | [diff] [blame] | 6536 | "cannot use unicode as modifiable buffer"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6537 | return -1; |
| 6538 | } |
| 6539 | |
| 6540 | static int |
| 6541 | unicode_buffer_getsegcount(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6542 | Py_ssize_t *lenp) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6543 | { |
| 6544 | if (lenp) |
| 6545 | *lenp = PyUnicode_GET_DATA_SIZE(self); |
| 6546 | return 1; |
| 6547 | } |
| 6548 | |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 6549 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6550 | unicode_buffer_getcharbuf(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6551 | Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6552 | const void **ptr) |
| 6553 | { |
| 6554 | PyObject *str; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6555 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6556 | if (index != 0) { |
| 6557 | PyErr_SetString(PyExc_SystemError, |
| 6558 | "accessing non-existent unicode segment"); |
| 6559 | return -1; |
| 6560 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 6561 | str = _PyUnicode_AsDefaultEncodedString((PyObject *)self, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6562 | if (str == NULL) |
| 6563 | return -1; |
| 6564 | *ptr = (void *) PyString_AS_STRING(str); |
| 6565 | return PyString_GET_SIZE(str); |
| 6566 | } |
| 6567 | |
| 6568 | /* Helpers for PyUnicode_Format() */ |
| 6569 | |
| 6570 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6571 | 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] | 6572 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6573 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6574 | if (argidx < arglen) { |
| 6575 | (*p_argidx)++; |
| 6576 | if (arglen < 0) |
| 6577 | return args; |
| 6578 | else |
| 6579 | return PyTuple_GetItem(args, argidx); |
| 6580 | } |
| 6581 | PyErr_SetString(PyExc_TypeError, |
| 6582 | "not enough arguments for format string"); |
| 6583 | return NULL; |
| 6584 | } |
| 6585 | |
| 6586 | #define F_LJUST (1<<0) |
| 6587 | #define F_SIGN (1<<1) |
| 6588 | #define F_BLANK (1<<2) |
| 6589 | #define F_ALT (1<<3) |
| 6590 | #define F_ZERO (1<<4) |
| 6591 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6592 | static Py_ssize_t |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6593 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6594 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6595 | register Py_ssize_t i; |
| 6596 | Py_ssize_t len = strlen(charbuffer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6597 | for (i = len - 1; i >= 0; i--) |
| 6598 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
| 6599 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6600 | return len; |
| 6601 | } |
| 6602 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6603 | static int |
| 6604 | doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) |
| 6605 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 6606 | Py_ssize_t result; |
| 6607 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6608 | PyOS_ascii_formatd((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 6609 | result = strtounicode(buffer, (char *)buffer); |
| 6610 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6611 | } |
| 6612 | |
| 6613 | static int |
| 6614 | longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) |
| 6615 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 6616 | Py_ssize_t result; |
| 6617 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6618 | PyOS_snprintf((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 6619 | result = strtounicode(buffer, (char *)buffer); |
| 6620 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6621 | } |
| 6622 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 6623 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 6624 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 6625 | formatting is done. */ |
| 6626 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6627 | static int |
| 6628 | formatfloat(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6629 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6630 | int flags, |
| 6631 | int prec, |
| 6632 | int type, |
| 6633 | PyObject *v) |
| 6634 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6635 | /* fmt = '%#.' + `prec` + `type` |
| 6636 | 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] | 6637 | char fmt[20]; |
| 6638 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6639 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6640 | x = PyFloat_AsDouble(v); |
| 6641 | if (x == -1.0 && PyErr_Occurred()) |
| 6642 | return -1; |
| 6643 | if (prec < 0) |
| 6644 | prec = 6; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6645 | if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
| 6646 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6647 | /* Worst case length calc to ensure no buffer overrun: |
| 6648 | |
| 6649 | 'g' formats: |
| 6650 | fmt = %#.<prec>g |
| 6651 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 6652 | for any double rep.) |
| 6653 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 6654 | |
| 6655 | 'f' formats: |
| 6656 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 6657 | len = 1 + 50 + 1 + prec = 52 + prec |
| 6658 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6659 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6660 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6661 | |
| 6662 | */ |
| 6663 | if ((type == 'g' && buflen <= (size_t)10 + (size_t)prec) || |
| 6664 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6665 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6666 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6667 | return -1; |
| 6668 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6669 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 6670 | (flags&F_ALT) ? "#" : "", |
| 6671 | prec, type); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6672 | return doubletounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6673 | } |
| 6674 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6675 | static PyObject* |
| 6676 | formatlong(PyObject *val, int flags, int prec, int type) |
| 6677 | { |
| 6678 | char *buf; |
| 6679 | int i, len; |
| 6680 | PyObject *str; /* temporary string object. */ |
| 6681 | PyUnicodeObject *result; |
| 6682 | |
| 6683 | str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
| 6684 | if (!str) |
| 6685 | return NULL; |
| 6686 | result = _PyUnicode_New(len); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 6687 | if (!result) { |
| 6688 | Py_DECREF(str); |
| 6689 | return NULL; |
| 6690 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6691 | for (i = 0; i < len; i++) |
| 6692 | result->str[i] = buf[i]; |
| 6693 | result->str[len] = 0; |
| 6694 | Py_DECREF(str); |
| 6695 | return (PyObject*)result; |
| 6696 | } |
| 6697 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6698 | static int |
| 6699 | formatint(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6700 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6701 | int flags, |
| 6702 | int prec, |
| 6703 | int type, |
| 6704 | PyObject *v) |
| 6705 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6706 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6707 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 6708 | * + 1 + 1 |
| 6709 | * = 24 |
| 6710 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6711 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6712 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6713 | long x; |
| 6714 | |
| 6715 | x = PyInt_AsLong(v); |
| 6716 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6717 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6718 | if (x < 0 && type == 'u') { |
| 6719 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 6720 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6721 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 6722 | sign = "-"; |
| 6723 | else |
| 6724 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6725 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6726 | prec = 1; |
| 6727 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6728 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 6729 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6730 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6731 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6732 | PyErr_SetString(PyExc_OverflowError, |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6733 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6734 | return -1; |
| 6735 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6736 | |
| 6737 | if ((flags & F_ALT) && |
| 6738 | (type == 'x' || type == 'X')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6739 | /* When converting under %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6740 | * of issues that cause pain: |
| 6741 | * - when 0 is being converted, the C standard leaves off |
| 6742 | * the '0x' or '0X', which is inconsistent with other |
| 6743 | * %#x/%#X conversions and inconsistent with Python's |
| 6744 | * hex() function |
| 6745 | * - there are platforms that violate the standard and |
| 6746 | * convert 0 with the '0x' or '0X' |
| 6747 | * (Metrowerks, Compaq Tru64) |
| 6748 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6749 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6750 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6751 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6752 | * We can achieve the desired consistency by inserting our |
| 6753 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 6754 | * of %#x/%#X. |
| 6755 | * |
| 6756 | * Note that this is the same approach as used in |
| 6757 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 6758 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6759 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 6760 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 6761 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6762 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6763 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 6764 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6765 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 6766 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6767 | if (sign[0]) |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6768 | return longtounicode(buf, buflen, fmt, -x); |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6769 | else |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6770 | return longtounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6771 | } |
| 6772 | |
| 6773 | static int |
| 6774 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6775 | size_t buflen, |
| 6776 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6777 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6778 | /* presume that the buffer is at least 2 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6779 | if (PyUnicode_Check(v)) { |
| 6780 | if (PyUnicode_GET_SIZE(v) != 1) |
| 6781 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6782 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6783 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6784 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6785 | else if (PyString_Check(v)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6786 | if (PyString_GET_SIZE(v) != 1) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6787 | goto onError; |
| 6788 | buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0]; |
| 6789 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6790 | |
| 6791 | else { |
| 6792 | /* Integer input truncated to a character */ |
| 6793 | long x; |
| 6794 | x = PyInt_AsLong(v); |
| 6795 | if (x == -1 && PyErr_Occurred()) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6796 | goto onError; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 6797 | #ifdef Py_UNICODE_WIDE |
| 6798 | if (x < 0 || x > 0x10ffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 6799 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 6800 | "%c arg not in range(0x110000) " |
| 6801 | "(wide Python build)"); |
| 6802 | return -1; |
| 6803 | } |
| 6804 | #else |
| 6805 | if (x < 0 || x > 0xffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 6806 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 6807 | "%c arg not in range(0x10000) " |
| 6808 | "(narrow Python build)"); |
| 6809 | return -1; |
| 6810 | } |
| 6811 | #endif |
| 6812 | buf[0] = (Py_UNICODE) x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6813 | } |
| 6814 | buf[1] = '\0'; |
| 6815 | return 1; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6816 | |
| 6817 | onError: |
| 6818 | PyErr_SetString(PyExc_TypeError, |
| 6819 | "%c requires int or char"); |
| 6820 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6821 | } |
| 6822 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6823 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 6824 | |
| 6825 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 6826 | chars are formatted. XXX This is a magic number. Each formatting |
| 6827 | routine does bounds checking to ensure no overflow, but a better |
| 6828 | solution may be to malloc a buffer of appropriate size for each |
| 6829 | format. For now, the current solution is sufficient. |
| 6830 | */ |
| 6831 | #define FORMATBUFLEN (size_t)120 |
| 6832 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6833 | PyObject *PyUnicode_Format(PyObject *format, |
| 6834 | PyObject *args) |
| 6835 | { |
| 6836 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6837 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6838 | int args_owned = 0; |
| 6839 | PyUnicodeObject *result = NULL; |
| 6840 | PyObject *dict = NULL; |
| 6841 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6842 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6843 | if (format == NULL || args == NULL) { |
| 6844 | PyErr_BadInternalCall(); |
| 6845 | return NULL; |
| 6846 | } |
| 6847 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6848 | if (uformat == NULL) |
| 6849 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6850 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 6851 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 6852 | |
| 6853 | reslen = rescnt = fmtcnt + 100; |
| 6854 | result = _PyUnicode_New(reslen); |
| 6855 | if (result == NULL) |
| 6856 | goto onError; |
| 6857 | res = PyUnicode_AS_UNICODE(result); |
| 6858 | |
| 6859 | if (PyTuple_Check(args)) { |
| 6860 | arglen = PyTuple_Size(args); |
| 6861 | argidx = 0; |
| 6862 | } |
| 6863 | else { |
| 6864 | arglen = -1; |
| 6865 | argidx = -2; |
| 6866 | } |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 6867 | if (args->ob_type->tp_as_mapping && !PyTuple_Check(args) && |
| 6868 | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6869 | dict = args; |
| 6870 | |
| 6871 | while (--fmtcnt >= 0) { |
| 6872 | if (*fmt != '%') { |
| 6873 | if (--rescnt < 0) { |
| 6874 | rescnt = fmtcnt + 100; |
| 6875 | reslen += rescnt; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6876 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 6877 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6878 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 6879 | --rescnt; |
| 6880 | } |
| 6881 | *res++ = *fmt++; |
| 6882 | } |
| 6883 | else { |
| 6884 | /* Got a format specifier */ |
| 6885 | int flags = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6886 | Py_ssize_t width = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6887 | int prec = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6888 | Py_UNICODE c = '\0'; |
| 6889 | Py_UNICODE fill; |
| 6890 | PyObject *v = NULL; |
| 6891 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6892 | Py_UNICODE *pbuf; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6893 | Py_UNICODE sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6894 | Py_ssize_t len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6895 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6896 | |
| 6897 | fmt++; |
| 6898 | if (*fmt == '(') { |
| 6899 | Py_UNICODE *keystart; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6900 | Py_ssize_t keylen; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6901 | PyObject *key; |
| 6902 | int pcount = 1; |
| 6903 | |
| 6904 | if (dict == NULL) { |
| 6905 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6906 | "format requires a mapping"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6907 | goto onError; |
| 6908 | } |
| 6909 | ++fmt; |
| 6910 | --fmtcnt; |
| 6911 | keystart = fmt; |
| 6912 | /* Skip over balanced parentheses */ |
| 6913 | while (pcount > 0 && --fmtcnt >= 0) { |
| 6914 | if (*fmt == ')') |
| 6915 | --pcount; |
| 6916 | else if (*fmt == '(') |
| 6917 | ++pcount; |
| 6918 | fmt++; |
| 6919 | } |
| 6920 | keylen = fmt - keystart - 1; |
| 6921 | if (fmtcnt < 0 || pcount > 0) { |
| 6922 | PyErr_SetString(PyExc_ValueError, |
| 6923 | "incomplete format key"); |
| 6924 | goto onError; |
| 6925 | } |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 6926 | #if 0 |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6927 | /* keys are converted to strings using UTF-8 and |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6928 | then looked up since Python uses strings to hold |
| 6929 | variables names etc. in its namespaces and we |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6930 | wouldn't want to break common idioms. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6931 | key = PyUnicode_EncodeUTF8(keystart, |
| 6932 | keylen, |
| 6933 | NULL); |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 6934 | #else |
| 6935 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 6936 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6937 | if (key == NULL) |
| 6938 | goto onError; |
| 6939 | if (args_owned) { |
| 6940 | Py_DECREF(args); |
| 6941 | args_owned = 0; |
| 6942 | } |
| 6943 | args = PyObject_GetItem(dict, key); |
| 6944 | Py_DECREF(key); |
| 6945 | if (args == NULL) { |
| 6946 | goto onError; |
| 6947 | } |
| 6948 | args_owned = 1; |
| 6949 | arglen = -1; |
| 6950 | argidx = -2; |
| 6951 | } |
| 6952 | while (--fmtcnt >= 0) { |
| 6953 | switch (c = *fmt++) { |
| 6954 | case '-': flags |= F_LJUST; continue; |
| 6955 | case '+': flags |= F_SIGN; continue; |
| 6956 | case ' ': flags |= F_BLANK; continue; |
| 6957 | case '#': flags |= F_ALT; continue; |
| 6958 | case '0': flags |= F_ZERO; continue; |
| 6959 | } |
| 6960 | break; |
| 6961 | } |
| 6962 | if (c == '*') { |
| 6963 | v = getnextarg(args, arglen, &argidx); |
| 6964 | if (v == NULL) |
| 6965 | goto onError; |
| 6966 | if (!PyInt_Check(v)) { |
| 6967 | PyErr_SetString(PyExc_TypeError, |
| 6968 | "* wants int"); |
| 6969 | goto onError; |
| 6970 | } |
| 6971 | width = PyInt_AsLong(v); |
| 6972 | if (width < 0) { |
| 6973 | flags |= F_LJUST; |
| 6974 | width = -width; |
| 6975 | } |
| 6976 | if (--fmtcnt >= 0) |
| 6977 | c = *fmt++; |
| 6978 | } |
| 6979 | else if (c >= '0' && c <= '9') { |
| 6980 | width = c - '0'; |
| 6981 | while (--fmtcnt >= 0) { |
| 6982 | c = *fmt++; |
| 6983 | if (c < '0' || c > '9') |
| 6984 | break; |
| 6985 | if ((width*10) / 10 != width) { |
| 6986 | PyErr_SetString(PyExc_ValueError, |
| 6987 | "width too big"); |
| 6988 | goto onError; |
| 6989 | } |
| 6990 | width = width*10 + (c - '0'); |
| 6991 | } |
| 6992 | } |
| 6993 | if (c == '.') { |
| 6994 | prec = 0; |
| 6995 | if (--fmtcnt >= 0) |
| 6996 | c = *fmt++; |
| 6997 | if (c == '*') { |
| 6998 | v = getnextarg(args, arglen, &argidx); |
| 6999 | if (v == NULL) |
| 7000 | goto onError; |
| 7001 | if (!PyInt_Check(v)) { |
| 7002 | PyErr_SetString(PyExc_TypeError, |
| 7003 | "* wants int"); |
| 7004 | goto onError; |
| 7005 | } |
| 7006 | prec = PyInt_AsLong(v); |
| 7007 | if (prec < 0) |
| 7008 | prec = 0; |
| 7009 | if (--fmtcnt >= 0) |
| 7010 | c = *fmt++; |
| 7011 | } |
| 7012 | else if (c >= '0' && c <= '9') { |
| 7013 | prec = c - '0'; |
| 7014 | while (--fmtcnt >= 0) { |
| 7015 | c = Py_CHARMASK(*fmt++); |
| 7016 | if (c < '0' || c > '9') |
| 7017 | break; |
| 7018 | if ((prec*10) / 10 != prec) { |
| 7019 | PyErr_SetString(PyExc_ValueError, |
| 7020 | "prec too big"); |
| 7021 | goto onError; |
| 7022 | } |
| 7023 | prec = prec*10 + (c - '0'); |
| 7024 | } |
| 7025 | } |
| 7026 | } /* prec */ |
| 7027 | if (fmtcnt >= 0) { |
| 7028 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7029 | if (--fmtcnt >= 0) |
| 7030 | c = *fmt++; |
| 7031 | } |
| 7032 | } |
| 7033 | if (fmtcnt < 0) { |
| 7034 | PyErr_SetString(PyExc_ValueError, |
| 7035 | "incomplete format"); |
| 7036 | goto onError; |
| 7037 | } |
| 7038 | if (c != '%') { |
| 7039 | v = getnextarg(args, arglen, &argidx); |
| 7040 | if (v == NULL) |
| 7041 | goto onError; |
| 7042 | } |
| 7043 | sign = 0; |
| 7044 | fill = ' '; |
| 7045 | switch (c) { |
| 7046 | |
| 7047 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7048 | pbuf = formatbuf; |
| 7049 | /* presume that buffer length is at least 1 */ |
| 7050 | pbuf[0] = '%'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7051 | len = 1; |
| 7052 | break; |
| 7053 | |
| 7054 | case 's': |
| 7055 | case 'r': |
| 7056 | if (PyUnicode_Check(v) && c == 's') { |
| 7057 | temp = v; |
| 7058 | Py_INCREF(temp); |
| 7059 | } |
| 7060 | else { |
| 7061 | PyObject *unicode; |
| 7062 | if (c == 's') |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 7063 | temp = PyObject_Unicode(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7064 | else |
| 7065 | temp = PyObject_Repr(v); |
| 7066 | if (temp == NULL) |
| 7067 | goto onError; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 7068 | if (PyUnicode_Check(temp)) |
| 7069 | /* nothing to do */; |
| 7070 | else if (PyString_Check(temp)) { |
| 7071 | /* convert to string to Unicode */ |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7072 | unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7073 | PyString_GET_SIZE(temp), |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7074 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7075 | "strict"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7076 | Py_DECREF(temp); |
| 7077 | temp = unicode; |
| 7078 | if (temp == NULL) |
| 7079 | goto onError; |
| 7080 | } |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 7081 | else { |
| 7082 | Py_DECREF(temp); |
| 7083 | PyErr_SetString(PyExc_TypeError, |
| 7084 | "%s argument has non-string str()"); |
| 7085 | goto onError; |
| 7086 | } |
| 7087 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7088 | pbuf = PyUnicode_AS_UNICODE(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7089 | len = PyUnicode_GET_SIZE(temp); |
| 7090 | if (prec >= 0 && len > prec) |
| 7091 | len = prec; |
| 7092 | break; |
| 7093 | |
| 7094 | case 'i': |
| 7095 | case 'd': |
| 7096 | case 'u': |
| 7097 | case 'o': |
| 7098 | case 'x': |
| 7099 | case 'X': |
| 7100 | if (c == 'i') |
| 7101 | c = 'd'; |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 7102 | if (PyLong_Check(v)) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7103 | temp = formatlong(v, flags, prec, c); |
| 7104 | if (!temp) |
| 7105 | goto onError; |
| 7106 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 7107 | len = PyUnicode_GET_SIZE(temp); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7108 | sign = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7109 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7110 | else { |
| 7111 | pbuf = formatbuf; |
| 7112 | len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 7113 | flags, prec, c, v); |
| 7114 | if (len < 0) |
| 7115 | goto onError; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7116 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7117 | } |
| 7118 | if (flags & F_ZERO) |
| 7119 | fill = '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7120 | break; |
| 7121 | |
| 7122 | case 'e': |
| 7123 | case 'E': |
| 7124 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 7125 | case 'F': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7126 | case 'g': |
| 7127 | case 'G': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 7128 | if (c == 'F') |
| 7129 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7130 | pbuf = formatbuf; |
| 7131 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 7132 | flags, prec, c, v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7133 | if (len < 0) |
| 7134 | goto onError; |
| 7135 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7136 | if (flags & F_ZERO) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7137 | fill = '0'; |
| 7138 | break; |
| 7139 | |
| 7140 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7141 | pbuf = formatbuf; |
| 7142 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7143 | if (len < 0) |
| 7144 | goto onError; |
| 7145 | break; |
| 7146 | |
| 7147 | default: |
| 7148 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 7149 | "unsupported format character '%c' (0x%x) " |
| 7150 | "at index %i", |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7151 | (31<=c && c<=126) ? (char)c : '?', |
Marc-André Lemburg | 24e53b6 | 2002-09-24 09:32:14 +0000 | [diff] [blame] | 7152 | (int)c, |
Guido van Rossum | efc1188 | 2002-09-12 14:43:41 +0000 | [diff] [blame] | 7153 | (int)(fmt -1 - PyUnicode_AS_UNICODE(uformat))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7154 | goto onError; |
| 7155 | } |
| 7156 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7157 | if (*pbuf == '-' || *pbuf == '+') { |
| 7158 | sign = *pbuf++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7159 | len--; |
| 7160 | } |
| 7161 | else if (flags & F_SIGN) |
| 7162 | sign = '+'; |
| 7163 | else if (flags & F_BLANK) |
| 7164 | sign = ' '; |
| 7165 | else |
| 7166 | sign = 0; |
| 7167 | } |
| 7168 | if (width < len) |
| 7169 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 7170 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7171 | reslen -= rescnt; |
| 7172 | rescnt = width + fmtcnt + 100; |
| 7173 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 7174 | if (reslen < 0) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 7175 | Py_XDECREF(temp); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7176 | PyErr_NoMemory(); |
| 7177 | goto onError; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 7178 | } |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7179 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 7180 | Py_XDECREF(temp); |
| 7181 | goto onError; |
| 7182 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7183 | res = PyUnicode_AS_UNICODE(result) |
| 7184 | + reslen - rescnt; |
| 7185 | } |
| 7186 | if (sign) { |
| 7187 | if (fill != ' ') |
| 7188 | *res++ = sign; |
| 7189 | rescnt--; |
| 7190 | if (width > len) |
| 7191 | width--; |
| 7192 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7193 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 7194 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7195 | assert(pbuf[1] == c); |
| 7196 | if (fill != ' ') { |
| 7197 | *res++ = *pbuf++; |
| 7198 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7199 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7200 | rescnt -= 2; |
| 7201 | width -= 2; |
| 7202 | if (width < 0) |
| 7203 | width = 0; |
| 7204 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7205 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7206 | if (width > len && !(flags & F_LJUST)) { |
| 7207 | do { |
| 7208 | --rescnt; |
| 7209 | *res++ = fill; |
| 7210 | } while (--width > len); |
| 7211 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7212 | if (fill == ' ') { |
| 7213 | if (sign) |
| 7214 | *res++ = sign; |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7215 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7216 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7217 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7218 | *res++ = *pbuf++; |
| 7219 | *res++ = *pbuf++; |
| 7220 | } |
| 7221 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7222 | Py_UNICODE_COPY(res, pbuf, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7223 | res += len; |
| 7224 | rescnt -= len; |
| 7225 | while (--width >= len) { |
| 7226 | --rescnt; |
| 7227 | *res++ = ' '; |
| 7228 | } |
| 7229 | if (dict && (argidx < arglen) && c != '%') { |
| 7230 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 7231 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7232 | Py_XDECREF(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7233 | goto onError; |
| 7234 | } |
| 7235 | Py_XDECREF(temp); |
| 7236 | } /* '%' */ |
| 7237 | } /* until end */ |
| 7238 | if (argidx < arglen && !dict) { |
| 7239 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 7240 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7241 | goto onError; |
| 7242 | } |
| 7243 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7244 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
| 7245 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7246 | if (args_owned) { |
| 7247 | Py_DECREF(args); |
| 7248 | } |
| 7249 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7250 | return (PyObject *)result; |
| 7251 | |
| 7252 | onError: |
| 7253 | Py_XDECREF(result); |
| 7254 | Py_DECREF(uformat); |
| 7255 | if (args_owned) { |
| 7256 | Py_DECREF(args); |
| 7257 | } |
| 7258 | return NULL; |
| 7259 | } |
| 7260 | |
| 7261 | static PyBufferProcs unicode_as_buffer = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7262 | (readbufferproc) unicode_buffer_getreadbuf, |
| 7263 | (writebufferproc) unicode_buffer_getwritebuf, |
| 7264 | (segcountproc) unicode_buffer_getsegcount, |
| 7265 | (charbufferproc) unicode_buffer_getcharbuf, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7266 | }; |
| 7267 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 7268 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7269 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 7270 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7271 | static PyObject * |
| 7272 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 7273 | { |
| 7274 | PyObject *x = NULL; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 7275 | static char *kwlist[] = {"string", "encoding", "errors", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7276 | char *encoding = NULL; |
| 7277 | char *errors = NULL; |
| 7278 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7279 | if (type != &PyUnicode_Type) |
| 7280 | return unicode_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7281 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
| 7282 | kwlist, &x, &encoding, &errors)) |
| 7283 | return NULL; |
| 7284 | if (x == NULL) |
| 7285 | return (PyObject *)_PyUnicode_New(0); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 7286 | if (encoding == NULL && errors == NULL) |
| 7287 | return PyObject_Unicode(x); |
| 7288 | else |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7289 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
| 7290 | } |
| 7291 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7292 | static PyObject * |
| 7293 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 7294 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7295 | PyUnicodeObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7296 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7297 | |
| 7298 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 7299 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 7300 | if (tmp == NULL) |
| 7301 | return NULL; |
| 7302 | assert(PyUnicode_Check(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7303 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7304 | if (pnew == NULL) { |
| 7305 | Py_DECREF(tmp); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7306 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7307 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7308 | pnew->str = PyMem_NEW(Py_UNICODE, n+1); |
| 7309 | if (pnew->str == NULL) { |
| 7310 | _Py_ForgetReference((PyObject *)pnew); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 7311 | PyObject_Del(pnew); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7312 | Py_DECREF(tmp); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 7313 | return PyErr_NoMemory(); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7314 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7315 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 7316 | pnew->length = n; |
| 7317 | pnew->hash = tmp->hash; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7318 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7319 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7320 | } |
| 7321 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7322 | PyDoc_STRVAR(unicode_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7323 | "unicode(string [, encoding[, errors]]) -> object\n\ |
| 7324 | \n\ |
| 7325 | Create a new Unicode object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 7326 | encoding defaults to the current default string encoding.\n\ |
| 7327 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7328 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7329 | PyTypeObject PyUnicode_Type = { |
| 7330 | PyObject_HEAD_INIT(&PyType_Type) |
| 7331 | 0, /* ob_size */ |
| 7332 | "unicode", /* tp_name */ |
| 7333 | sizeof(PyUnicodeObject), /* tp_size */ |
| 7334 | 0, /* tp_itemsize */ |
| 7335 | /* Slots */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 7336 | (destructor)unicode_dealloc, /* tp_dealloc */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7337 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7338 | 0, /* tp_getattr */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7339 | 0, /* tp_setattr */ |
| 7340 | (cmpfunc) unicode_compare, /* tp_compare */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 7341 | unicode_repr, /* tp_repr */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7342 | &unicode_as_number, /* tp_as_number */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7343 | &unicode_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7344 | &unicode_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7345 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 7346 | 0, /* tp_call*/ |
| 7347 | (reprfunc) unicode_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7348 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 7349 | 0, /* tp_setattro */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7350 | &unicode_as_buffer, /* tp_as_buffer */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7351 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
| 7352 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7353 | unicode_doc, /* tp_doc */ |
| 7354 | 0, /* tp_traverse */ |
| 7355 | 0, /* tp_clear */ |
| 7356 | 0, /* tp_richcompare */ |
| 7357 | 0, /* tp_weaklistoffset */ |
| 7358 | 0, /* tp_iter */ |
| 7359 | 0, /* tp_iternext */ |
| 7360 | unicode_methods, /* tp_methods */ |
| 7361 | 0, /* tp_members */ |
| 7362 | 0, /* tp_getset */ |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 7363 | &PyBaseString_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7364 | 0, /* tp_dict */ |
| 7365 | 0, /* tp_descr_get */ |
| 7366 | 0, /* tp_descr_set */ |
| 7367 | 0, /* tp_dictoffset */ |
| 7368 | 0, /* tp_init */ |
| 7369 | 0, /* tp_alloc */ |
| 7370 | unicode_new, /* tp_new */ |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 7371 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7372 | }; |
| 7373 | |
| 7374 | /* Initialize the Unicode implementation */ |
| 7375 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 7376 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7377 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7378 | int i; |
| 7379 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7380 | /* Init the implementation */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7381 | unicode_freelist = NULL; |
| 7382 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7383 | unicode_empty = _PyUnicode_New(0); |
Marc-André Lemburg | 90e8147 | 2000-06-07 09:13:21 +0000 | [diff] [blame] | 7384 | strcpy(unicode_default_encoding, "ascii"); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7385 | for (i = 0; i < 256; i++) |
| 7386 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 7387 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 7388 | Py_FatalError("Can't initialize 'unicode'"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7389 | } |
| 7390 | |
| 7391 | /* Finalize the Unicode implementation */ |
| 7392 | |
| 7393 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 7394 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7395 | { |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 7396 | PyUnicodeObject *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7397 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7398 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 7399 | Py_XDECREF(unicode_empty); |
| 7400 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 7401 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7402 | for (i = 0; i < 256; i++) { |
| 7403 | if (unicode_latin1[i]) { |
| 7404 | Py_DECREF(unicode_latin1[i]); |
| 7405 | unicode_latin1[i] = NULL; |
| 7406 | } |
| 7407 | } |
| 7408 | |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 7409 | for (u = unicode_freelist; u != NULL;) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7410 | PyUnicodeObject *v = u; |
| 7411 | u = *(PyUnicodeObject **)u; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 7412 | if (v->str) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 7413 | PyMem_DEL(v->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 7414 | Py_XDECREF(v->defenc); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 7415 | PyObject_Del(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7416 | } |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7417 | unicode_freelist = NULL; |
| 7418 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7419 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 7420 | |
| 7421 | /* |
| 7422 | Local variables: |
| 7423 | c-basic-offset: 4 |
| 7424 | indent-tabs-mode: nil |
| 7425 | End: |
| 7426 | */ |