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 | |
Nicholas Bastin | 1ce9e4c | 2004-06-17 18:27:18 +0000 | [diff] [blame] | 44 | #ifdef __SUNPRO_C |
| 45 | #pragma error_messages (off,E_END_OF_LOOP_CODE_NOT_REACHED) |
| 46 | #endif |
| 47 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 48 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 49 | #include <windows.h> |
| 50 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 52 | /* Limit for the Unicode object free list */ |
| 53 | |
| 54 | #define MAX_UNICODE_FREELIST_SIZE 1024 |
| 55 | |
| 56 | /* Limit for the Unicode object free list stay alive optimization. |
| 57 | |
| 58 | The implementation will keep allocated Unicode memory intact for |
| 59 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 60 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 61 | |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 62 | At worst this will result in MAX_UNICODE_FREELIST_SIZE * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 63 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 64 | malloc()-overhead) bytes of unused garbage. |
| 65 | |
| 66 | Setting the limit to 0 effectively turns the feature off. |
| 67 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 68 | Note: This is an experimental feature ! If you get core dumps when |
| 69 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 70 | |
| 71 | */ |
| 72 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 73 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 74 | |
| 75 | /* Endianness switches; defaults to little endian */ |
| 76 | |
| 77 | #ifdef WORDS_BIGENDIAN |
| 78 | # define BYTEORDER_IS_BIG_ENDIAN |
| 79 | #else |
| 80 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 81 | #endif |
| 82 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 83 | /* --- Globals ------------------------------------------------------------ |
| 84 | |
| 85 | The globals are initialized by the _PyUnicode_Init() API and should |
| 86 | not be used before calling that API. |
| 87 | |
| 88 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 89 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 90 | /* Free list for Unicode objects */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 91 | static PyUnicodeObject *unicode_freelist; |
| 92 | static int unicode_freelist_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 93 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 94 | /* The empty Unicode object is shared to improve performance. */ |
| 95 | static PyUnicodeObject *unicode_empty; |
| 96 | |
| 97 | /* Single character Unicode strings in the Latin-1 range are being |
| 98 | shared as well. */ |
| 99 | static PyUnicodeObject *unicode_latin1[256]; |
| 100 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 101 | /* Default encoding to use and assume when NULL is passed as encoding |
| 102 | parameter; it is initialized by _PyUnicode_Init(). |
| 103 | |
| 104 | Always use the PyUnicode_SetDefaultEncoding() and |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 105 | PyUnicode_GetDefaultEncoding() APIs to access this global. |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 106 | |
| 107 | */ |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 108 | static char unicode_default_encoding[100]; |
| 109 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 110 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 111 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 112 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 113 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 114 | return 0x10FFFF; |
| 115 | #else |
| 116 | /* This is actually an illegal character, so it should |
| 117 | not be passed to unichr. */ |
| 118 | return 0xFFFF; |
| 119 | #endif |
| 120 | } |
| 121 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 122 | /* --- Unicode Object ----------------------------------------------------- */ |
| 123 | |
| 124 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 125 | int unicode_resize(register PyUnicodeObject *unicode, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 126 | int length) |
| 127 | { |
| 128 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 129 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 130 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 131 | if (unicode->length == length) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 132 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 133 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 134 | /* Resizing shared object (unicode_empty or single character |
| 135 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 136 | instead ! */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 137 | if (unicode == unicode_empty || |
| 138 | (unicode->length == 1 && |
| 139 | /* MvL said unicode->str[] may be signed. Python generally assumes |
| 140 | * an int contains at least 32 bits, and we don't use more than |
| 141 | * 32 bits even in a UCS4 build, so casting to unsigned int should |
| 142 | * be correct. |
| 143 | */ |
| 144 | (unsigned int)unicode->str[0] < 256U && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 145 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 146 | PyErr_SetString(PyExc_SystemError, |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 147 | "can't resize shared unicode objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 148 | return -1; |
| 149 | } |
| 150 | |
| 151 | /* We allocate one more byte to make sure the string is |
| 152 | Ux0000 terminated -- XXX is this needed ? */ |
| 153 | oldstr = unicode->str; |
| 154 | PyMem_RESIZE(unicode->str, Py_UNICODE, length + 1); |
| 155 | if (!unicode->str) { |
| 156 | unicode->str = oldstr; |
| 157 | PyErr_NoMemory(); |
| 158 | return -1; |
| 159 | } |
| 160 | unicode->str[length] = 0; |
| 161 | unicode->length = length; |
| 162 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 163 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 164 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 165 | if (unicode->defenc) { |
| 166 | Py_DECREF(unicode->defenc); |
| 167 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 168 | } |
| 169 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 170 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | /* We allocate one more byte to make sure the string is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 175 | Ux0000 terminated -- XXX is this needed ? |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 176 | |
| 177 | XXX This allocator could further be enhanced by assuring that the |
| 178 | free list never reduces its size below 1. |
| 179 | |
| 180 | */ |
| 181 | |
| 182 | static |
| 183 | PyUnicodeObject *_PyUnicode_New(int length) |
| 184 | { |
| 185 | register PyUnicodeObject *unicode; |
| 186 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 187 | /* Optimization fo empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 188 | if (length == 0 && unicode_empty != NULL) { |
| 189 | Py_INCREF(unicode_empty); |
| 190 | return unicode_empty; |
| 191 | } |
| 192 | |
| 193 | /* Unicode freelist & memory allocation */ |
| 194 | if (unicode_freelist) { |
| 195 | unicode = unicode_freelist; |
Marc-André Lemburg | bea47e7 | 2000-06-17 20:31:17 +0000 | [diff] [blame] | 196 | unicode_freelist = *(PyUnicodeObject **)unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 197 | unicode_freelist_size--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 198 | if (unicode->str) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 199 | /* Keep-Alive optimization: we only upsize the buffer, |
| 200 | never downsize it. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 201 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 202 | unicode_resize(unicode, length) < 0) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 203 | PyMem_DEL(unicode->str); |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 204 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 205 | } |
| 206 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 207 | else { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 208 | unicode->str = PyMem_NEW(Py_UNICODE, length + 1); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 209 | } |
| 210 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 211 | } |
| 212 | else { |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 213 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 214 | if (unicode == NULL) |
| 215 | return NULL; |
| 216 | unicode->str = PyMem_NEW(Py_UNICODE, length + 1); |
| 217 | } |
| 218 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 219 | if (!unicode->str) { |
| 220 | PyErr_NoMemory(); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 221 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 222 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 223 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 224 | * the caller fails before initializing str -- unicode_resize() |
| 225 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 226 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 227 | * We don't want unicode_resize to read uninitialized memory in |
| 228 | * that case. |
| 229 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 230 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 231 | unicode->str[length] = 0; |
| 232 | unicode->length = length; |
| 233 | unicode->hash = -1; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 234 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 235 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 236 | |
| 237 | onError: |
| 238 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 239 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 240 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 244 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 245 | { |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 246 | if (PyUnicode_CheckExact(unicode) && |
| 247 | unicode_freelist_size < MAX_UNICODE_FREELIST_SIZE) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 248 | /* Keep-Alive optimization */ |
| 249 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 250 | PyMem_DEL(unicode->str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 251 | unicode->str = NULL; |
| 252 | unicode->length = 0; |
| 253 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 254 | if (unicode->defenc) { |
| 255 | Py_DECREF(unicode->defenc); |
| 256 | unicode->defenc = NULL; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 257 | } |
| 258 | /* Add to free list */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 259 | *(PyUnicodeObject **)unicode = unicode_freelist; |
| 260 | unicode_freelist = unicode; |
| 261 | unicode_freelist_size++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 262 | } |
| 263 | else { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 264 | PyMem_DEL(unicode->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 265 | Py_XDECREF(unicode->defenc); |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 266 | unicode->ob_type->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 270 | int PyUnicode_Resize(PyObject **unicode, int length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 271 | { |
| 272 | register PyUnicodeObject *v; |
| 273 | |
| 274 | /* Argument checks */ |
| 275 | if (unicode == NULL) { |
| 276 | PyErr_BadInternalCall(); |
| 277 | return -1; |
| 278 | } |
| 279 | v = (PyUnicodeObject *)*unicode; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 280 | 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] | 281 | PyErr_BadInternalCall(); |
| 282 | return -1; |
| 283 | } |
| 284 | |
| 285 | /* Resizing unicode_empty and single character objects is not |
| 286 | possible since these are being shared. We simply return a fresh |
| 287 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 288 | if (v->length != length && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 289 | (v == unicode_empty || v->length == 1)) { |
| 290 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 291 | if (w == NULL) |
| 292 | return -1; |
| 293 | Py_UNICODE_COPY(w->str, v->str, |
| 294 | length < v->length ? length : v->length); |
Raymond Hettinger | c8df578 | 2003-03-09 07:30:43 +0000 | [diff] [blame] | 295 | Py_DECREF(*unicode); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 296 | *unicode = (PyObject *)w; |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 301 | objects, since we can modify them in-place. */ |
| 302 | return unicode_resize(v, length); |
| 303 | } |
| 304 | |
| 305 | /* Internal API for use in unicodeobject.c only ! */ |
| 306 | #define _PyUnicode_Resize(unicodevar, length) \ |
| 307 | PyUnicode_Resize(((PyObject **)(unicodevar)), length) |
| 308 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 309 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
| 310 | int size) |
| 311 | { |
| 312 | PyUnicodeObject *unicode; |
| 313 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 314 | /* If the Unicode data is known at construction time, we can apply |
| 315 | some optimizations which share commonly used objects. */ |
| 316 | if (u != NULL) { |
| 317 | |
| 318 | /* Optimization for empty strings */ |
| 319 | if (size == 0 && unicode_empty != NULL) { |
| 320 | Py_INCREF(unicode_empty); |
| 321 | return (PyObject *)unicode_empty; |
| 322 | } |
| 323 | |
| 324 | /* Single character Unicode objects in the Latin-1 range are |
| 325 | shared when using this constructor */ |
| 326 | if (size == 1 && *u < 256) { |
| 327 | unicode = unicode_latin1[*u]; |
| 328 | if (!unicode) { |
| 329 | unicode = _PyUnicode_New(1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 330 | if (!unicode) |
| 331 | return NULL; |
Marc-André Lemburg | 8879a33 | 2001-06-07 12:26:56 +0000 | [diff] [blame] | 332 | unicode->str[0] = *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 333 | unicode_latin1[*u] = unicode; |
| 334 | } |
| 335 | Py_INCREF(unicode); |
| 336 | return (PyObject *)unicode; |
| 337 | } |
| 338 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 339 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 340 | unicode = _PyUnicode_New(size); |
| 341 | if (!unicode) |
| 342 | return NULL; |
| 343 | |
| 344 | /* Copy the Unicode data into the new object */ |
| 345 | if (u != NULL) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 346 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 347 | |
| 348 | return (PyObject *)unicode; |
| 349 | } |
| 350 | |
| 351 | #ifdef HAVE_WCHAR_H |
| 352 | |
| 353 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
| 354 | int size) |
| 355 | { |
| 356 | PyUnicodeObject *unicode; |
| 357 | |
| 358 | if (w == NULL) { |
| 359 | PyErr_BadInternalCall(); |
| 360 | return NULL; |
| 361 | } |
| 362 | |
| 363 | unicode = _PyUnicode_New(size); |
| 364 | if (!unicode) |
| 365 | return NULL; |
| 366 | |
| 367 | /* Copy the wchar_t data into the new object */ |
| 368 | #ifdef HAVE_USABLE_WCHAR_T |
| 369 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 370 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 371 | { |
| 372 | register Py_UNICODE *u; |
| 373 | register int i; |
| 374 | u = PyUnicode_AS_UNICODE(unicode); |
| 375 | for (i = size; i >= 0; i--) |
| 376 | *u++ = *w++; |
| 377 | } |
| 378 | #endif |
| 379 | |
| 380 | return (PyObject *)unicode; |
| 381 | } |
| 382 | |
| 383 | int PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 384 | register wchar_t *w, |
| 385 | int size) |
| 386 | { |
| 387 | if (unicode == NULL) { |
| 388 | PyErr_BadInternalCall(); |
| 389 | return -1; |
| 390 | } |
| 391 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 392 | size = PyUnicode_GET_SIZE(unicode); |
| 393 | #ifdef HAVE_USABLE_WCHAR_T |
| 394 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 395 | #else |
| 396 | { |
| 397 | register Py_UNICODE *u; |
| 398 | register int i; |
| 399 | u = PyUnicode_AS_UNICODE(unicode); |
| 400 | for (i = size; i >= 0; i--) |
| 401 | *w++ = *u++; |
| 402 | } |
| 403 | #endif |
| 404 | |
| 405 | return size; |
| 406 | } |
| 407 | |
| 408 | #endif |
| 409 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 410 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 411 | { |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 412 | Py_UNICODE s[1]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 413 | |
| 414 | #ifdef Py_UNICODE_WIDE |
| 415 | if (ordinal < 0 || ordinal > 0x10ffff) { |
| 416 | PyErr_SetString(PyExc_ValueError, |
| 417 | "unichr() arg not in range(0x110000) " |
| 418 | "(wide Python build)"); |
| 419 | return NULL; |
| 420 | } |
| 421 | #else |
| 422 | if (ordinal < 0 || ordinal > 0xffff) { |
| 423 | PyErr_SetString(PyExc_ValueError, |
| 424 | "unichr() arg not in range(0x10000) " |
| 425 | "(narrow Python build)"); |
| 426 | return NULL; |
| 427 | } |
| 428 | #endif |
| 429 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 430 | s[0] = (Py_UNICODE)ordinal; |
| 431 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 434 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 435 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 436 | /* XXX Perhaps we should make this API an alias of |
| 437 | PyObject_Unicode() instead ?! */ |
| 438 | if (PyUnicode_CheckExact(obj)) { |
| 439 | Py_INCREF(obj); |
| 440 | return obj; |
| 441 | } |
| 442 | if (PyUnicode_Check(obj)) { |
| 443 | /* For a Unicode subtype that's not a Unicode object, |
| 444 | return a true Unicode object with the same data. */ |
| 445 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 446 | PyUnicode_GET_SIZE(obj)); |
| 447 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 448 | return PyUnicode_FromEncodedObject(obj, NULL, "strict"); |
| 449 | } |
| 450 | |
| 451 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
| 452 | const char *encoding, |
| 453 | const char *errors) |
| 454 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 455 | const char *s = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 456 | int len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 457 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 458 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 459 | if (obj == NULL) { |
| 460 | PyErr_BadInternalCall(); |
| 461 | return NULL; |
| 462 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 463 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 464 | #if 0 |
| 465 | /* For b/w compatibility we also accept Unicode objects provided |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 466 | that no encodings is given and then redirect to |
| 467 | PyObject_Unicode() which then applies the additional logic for |
| 468 | Unicode subclasses. |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 469 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 470 | NOTE: This API should really only be used for object which |
| 471 | represent *encoded* Unicode ! |
| 472 | |
| 473 | */ |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 474 | if (PyUnicode_Check(obj)) { |
| 475 | if (encoding) { |
| 476 | PyErr_SetString(PyExc_TypeError, |
| 477 | "decoding Unicode is not supported"); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 478 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 479 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 480 | return PyObject_Unicode(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 481 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 482 | #else |
| 483 | if (PyUnicode_Check(obj)) { |
| 484 | PyErr_SetString(PyExc_TypeError, |
| 485 | "decoding Unicode is not supported"); |
| 486 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 487 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 488 | #endif |
| 489 | |
| 490 | /* Coerce object */ |
| 491 | if (PyString_Check(obj)) { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 492 | s = PyString_AS_STRING(obj); |
| 493 | len = PyString_GET_SIZE(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 494 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 495 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
| 496 | /* Overwrite the error message with something more useful in |
| 497 | case of a TypeError. */ |
| 498 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 499 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 500 | "coercing to Unicode: need string or buffer, " |
| 501 | "%.80s found", |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 502 | obj->ob_type->tp_name); |
| 503 | goto onError; |
| 504 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 505 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 506 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 507 | if (len == 0) { |
| 508 | Py_INCREF(unicode_empty); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 509 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 510 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 511 | else |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 512 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 513 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 514 | return v; |
| 515 | |
| 516 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 517 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | PyObject *PyUnicode_Decode(const char *s, |
| 521 | int size, |
| 522 | const char *encoding, |
| 523 | const char *errors) |
| 524 | { |
| 525 | PyObject *buffer = NULL, *unicode; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 526 | |
| 527 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 528 | encoding = PyUnicode_GetDefaultEncoding(); |
| 529 | |
| 530 | /* Shortcuts for common default encodings */ |
| 531 | if (strcmp(encoding, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 532 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 533 | else if (strcmp(encoding, "latin-1") == 0) |
| 534 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 535 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 536 | else if (strcmp(encoding, "mbcs") == 0) |
| 537 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 538 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 539 | else if (strcmp(encoding, "ascii") == 0) |
| 540 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 541 | |
| 542 | /* Decode via the codec registry */ |
| 543 | buffer = PyBuffer_FromMemory((void *)s, size); |
| 544 | if (buffer == NULL) |
| 545 | goto onError; |
| 546 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 547 | if (unicode == NULL) |
| 548 | goto onError; |
| 549 | if (!PyUnicode_Check(unicode)) { |
| 550 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 551 | "decoder did not return an unicode object (type=%.400s)", |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 552 | unicode->ob_type->tp_name); |
| 553 | Py_DECREF(unicode); |
| 554 | goto onError; |
| 555 | } |
| 556 | Py_DECREF(buffer); |
| 557 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 558 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 559 | onError: |
| 560 | Py_XDECREF(buffer); |
| 561 | return NULL; |
| 562 | } |
| 563 | |
| 564 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
| 565 | int size, |
| 566 | const char *encoding, |
| 567 | const char *errors) |
| 568 | { |
| 569 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 570 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 571 | unicode = PyUnicode_FromUnicode(s, size); |
| 572 | if (unicode == NULL) |
| 573 | return NULL; |
| 574 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 575 | Py_DECREF(unicode); |
| 576 | return v; |
| 577 | } |
| 578 | |
| 579 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 580 | const char *encoding, |
| 581 | const char *errors) |
| 582 | { |
| 583 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 584 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 585 | if (!PyUnicode_Check(unicode)) { |
| 586 | PyErr_BadArgument(); |
| 587 | goto onError; |
| 588 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 589 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 590 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 591 | encoding = PyUnicode_GetDefaultEncoding(); |
| 592 | |
| 593 | /* Shortcuts for common default encodings */ |
| 594 | if (errors == NULL) { |
| 595 | if (strcmp(encoding, "utf-8") == 0) |
Jeremy Hylton | 9cea41c | 2001-05-29 17:13:15 +0000 | [diff] [blame] | 596 | return PyUnicode_AsUTF8String(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 597 | else if (strcmp(encoding, "latin-1") == 0) |
| 598 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 599 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 600 | else if (strcmp(encoding, "mbcs") == 0) |
| 601 | return PyUnicode_AsMBCSString(unicode); |
| 602 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 603 | else if (strcmp(encoding, "ascii") == 0) |
| 604 | return PyUnicode_AsASCIIString(unicode); |
| 605 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 606 | |
| 607 | /* Encode via the codec registry */ |
| 608 | v = PyCodec_Encode(unicode, encoding, errors); |
| 609 | if (v == NULL) |
| 610 | goto onError; |
| 611 | /* XXX Should we really enforce this ? */ |
| 612 | if (!PyString_Check(v)) { |
| 613 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 614 | "encoder did not return a string object (type=%.400s)", |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 615 | v->ob_type->tp_name); |
| 616 | Py_DECREF(v); |
| 617 | goto onError; |
| 618 | } |
| 619 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 620 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 621 | onError: |
| 622 | return NULL; |
| 623 | } |
| 624 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 625 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 626 | const char *errors) |
| 627 | { |
| 628 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
| 629 | |
| 630 | if (v) |
| 631 | return v; |
| 632 | v = PyUnicode_AsEncodedString(unicode, NULL, errors); |
| 633 | if (v && errors == NULL) |
| 634 | ((PyUnicodeObject *)unicode)->defenc = v; |
| 635 | return v; |
| 636 | } |
| 637 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 638 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 639 | { |
| 640 | if (!PyUnicode_Check(unicode)) { |
| 641 | PyErr_BadArgument(); |
| 642 | goto onError; |
| 643 | } |
| 644 | return PyUnicode_AS_UNICODE(unicode); |
| 645 | |
| 646 | onError: |
| 647 | return NULL; |
| 648 | } |
| 649 | |
| 650 | int PyUnicode_GetSize(PyObject *unicode) |
| 651 | { |
| 652 | if (!PyUnicode_Check(unicode)) { |
| 653 | PyErr_BadArgument(); |
| 654 | goto onError; |
| 655 | } |
| 656 | return PyUnicode_GET_SIZE(unicode); |
| 657 | |
| 658 | onError: |
| 659 | return -1; |
| 660 | } |
| 661 | |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 662 | int PyUnicode_GetWidth(PyObject *unicode) |
| 663 | { |
| 664 | const Py_UNICODE *p, *e; |
| 665 | int width; |
| 666 | |
| 667 | if (!PyUnicode_Check(unicode)) { |
| 668 | PyErr_BadArgument(); |
| 669 | return -1; |
| 670 | } |
| 671 | |
| 672 | p = PyUnicode_AS_UNICODE(unicode); |
| 673 | e = p + PyUnicode_GET_SIZE(unicode); |
| 674 | for (width = 0; p < e; p++) |
| 675 | if (Py_UNICODE_ISWIDE(*p)) |
| 676 | width += 2; |
| 677 | else |
| 678 | width++; |
| 679 | |
| 680 | return width; |
| 681 | } |
| 682 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 683 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 684 | { |
| 685 | return unicode_default_encoding; |
| 686 | } |
| 687 | |
| 688 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 689 | { |
| 690 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 691 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 692 | /* Make sure the encoding is valid. As side effect, this also |
| 693 | loads the encoding into the codec registry cache. */ |
| 694 | v = _PyCodec_Lookup(encoding); |
| 695 | if (v == NULL) |
| 696 | goto onError; |
| 697 | Py_DECREF(v); |
| 698 | strncpy(unicode_default_encoding, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 699 | encoding, |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 700 | sizeof(unicode_default_encoding)); |
| 701 | return 0; |
| 702 | |
| 703 | onError: |
| 704 | return -1; |
| 705 | } |
| 706 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 707 | /* error handling callback helper: |
| 708 | build arguments, call the callback and check the arguments, |
| 709 | if no exception occured, copy the replacement to the output |
| 710 | and adjust various state variables. |
| 711 | return 0 on success, -1 on error |
| 712 | */ |
| 713 | |
| 714 | static |
| 715 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 716 | const char *encoding, const char *reason, |
| 717 | const char *input, int insize, int *startinpos, int *endinpos, PyObject **exceptionObject, const char **inptr, |
| 718 | PyObject **output, int *outpos, Py_UNICODE **outptr) |
| 719 | { |
| 720 | static char *argparse = "O!i;decoding error handler must return (unicode, int) tuple"; |
| 721 | |
| 722 | PyObject *restuple = NULL; |
| 723 | PyObject *repunicode = NULL; |
| 724 | int outsize = PyUnicode_GET_SIZE(*output); |
| 725 | int requiredsize; |
| 726 | int newpos; |
| 727 | Py_UNICODE *repptr; |
| 728 | int repsize; |
| 729 | int res = -1; |
| 730 | |
| 731 | if (*errorHandler == NULL) { |
| 732 | *errorHandler = PyCodec_LookupError(errors); |
| 733 | if (*errorHandler == NULL) |
| 734 | goto onError; |
| 735 | } |
| 736 | |
| 737 | if (*exceptionObject == NULL) { |
| 738 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 739 | encoding, input, insize, *startinpos, *endinpos, reason); |
| 740 | if (*exceptionObject == NULL) |
| 741 | goto onError; |
| 742 | } |
| 743 | else { |
| 744 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 745 | goto onError; |
| 746 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 747 | goto onError; |
| 748 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 749 | goto onError; |
| 750 | } |
| 751 | |
| 752 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 753 | if (restuple == NULL) |
| 754 | goto onError; |
| 755 | if (!PyTuple_Check(restuple)) { |
| 756 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 757 | goto onError; |
| 758 | } |
| 759 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 760 | goto onError; |
| 761 | if (newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 762 | newpos = insize+newpos; |
| 763 | if (newpos<0 || newpos>insize) { |
| 764 | PyErr_Format(PyExc_IndexError, "position %d from error handler out of bounds", newpos); |
| 765 | goto onError; |
| 766 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 767 | |
| 768 | /* need more space? (at least enough for what we |
| 769 | have+the replacement+the rest of the string (starting |
| 770 | at the new input position), so we won't have to check space |
| 771 | when there are no errors in the rest of the string) */ |
| 772 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 773 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 774 | requiredsize = *outpos + repsize + insize-newpos; |
| 775 | if (requiredsize > outsize) { |
| 776 | if (requiredsize<2*outsize) |
| 777 | requiredsize = 2*outsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 778 | if (PyUnicode_Resize(output, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 779 | goto onError; |
| 780 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
| 781 | } |
| 782 | *endinpos = newpos; |
| 783 | *inptr = input + newpos; |
| 784 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 785 | *outptr += repsize; |
| 786 | *outpos += repsize; |
| 787 | /* we made it! */ |
| 788 | res = 0; |
| 789 | |
| 790 | onError: |
| 791 | Py_XDECREF(restuple); |
| 792 | return res; |
| 793 | } |
| 794 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 795 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 796 | |
| 797 | /* see RFC2152 for details */ |
| 798 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 799 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 800 | char utf7_special[128] = { |
| 801 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 802 | encoded: |
| 803 | 0 - not special |
| 804 | 1 - special |
| 805 | 2 - whitespace (optional) |
| 806 | 3 - RFC2152 Set O (optional) */ |
| 807 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 808 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 809 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 810 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 811 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 812 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 813 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 814 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 815 | |
| 816 | }; |
| 817 | |
| 818 | #define SPECIAL(c, encodeO, encodeWS) \ |
| 819 | (((c)>127 || utf7_special[(c)] == 1) || \ |
| 820 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
| 821 | (encodeO && (utf7_special[(c)] == 3))) |
| 822 | |
| 823 | #define B64(n) ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 824 | #define B64CHAR(c) (isalnum(c) || (c) == '+' || (c) == '/') |
| 825 | #define UB64(c) ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
| 826 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4) |
| 827 | |
| 828 | #define ENCODE(out, ch, bits) \ |
| 829 | while (bits >= 6) { \ |
| 830 | *out++ = B64(ch >> (bits-6)); \ |
| 831 | bits -= 6; \ |
| 832 | } |
| 833 | |
| 834 | #define DECODE(out, ch, bits, surrogate) \ |
| 835 | while (bits >= 16) { \ |
| 836 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 837 | bits -= 16; \ |
| 838 | if (surrogate) { \ |
| 839 | /* We have already generated an error for the high surrogate |
| 840 | so let's not bother seeing if the low surrogate is correct or not */\ |
| 841 | surrogate = 0; \ |
| 842 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
| 843 | /* This is a surrogate pair. Unfortunately we can't represent \ |
| 844 | it in a 16-bit character */ \ |
| 845 | surrogate = 1; \ |
| 846 | errmsg = "code pairs are not supported"; \ |
| 847 | goto utf7Error; \ |
| 848 | } else { \ |
| 849 | *out++ = outCh; \ |
| 850 | } \ |
| 851 | } \ |
| 852 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 853 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
| 854 | int size, |
| 855 | const char *errors) |
| 856 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 857 | const char *starts = s; |
| 858 | int startinpos; |
| 859 | int endinpos; |
| 860 | int outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 861 | const char *e; |
| 862 | PyUnicodeObject *unicode; |
| 863 | Py_UNICODE *p; |
| 864 | const char *errmsg = ""; |
| 865 | int inShift = 0; |
| 866 | unsigned int bitsleft = 0; |
| 867 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 868 | int surrogate = 0; |
| 869 | PyObject *errorHandler = NULL; |
| 870 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 871 | |
| 872 | unicode = _PyUnicode_New(size); |
| 873 | if (!unicode) |
| 874 | return NULL; |
| 875 | if (size == 0) |
| 876 | return (PyObject *)unicode; |
| 877 | |
| 878 | p = unicode->str; |
| 879 | e = s + size; |
| 880 | |
| 881 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 882 | Py_UNICODE ch; |
| 883 | restart: |
| 884 | ch = *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 885 | |
| 886 | if (inShift) { |
| 887 | if ((ch == '-') || !B64CHAR(ch)) { |
| 888 | inShift = 0; |
| 889 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 890 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 891 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 892 | if (bitsleft >= 6) { |
| 893 | /* The shift sequence has a partial character in it. If |
| 894 | bitsleft < 6 then we could just classify it as padding |
| 895 | but that is not the case here */ |
| 896 | |
| 897 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 898 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 899 | } |
| 900 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 901 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 902 | here so indicate the potential of a misencoded character. */ |
| 903 | |
| 904 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 905 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 906 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 907 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | if (ch == '-') { |
| 911 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 912 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 913 | inShift = 1; |
| 914 | } |
| 915 | } else if (SPECIAL(ch,0,0)) { |
| 916 | errmsg = "unexpected special character"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 917 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 918 | } else { |
| 919 | *p++ = ch; |
| 920 | } |
| 921 | } else { |
| 922 | charsleft = (charsleft << 6) | UB64(ch); |
| 923 | bitsleft += 6; |
| 924 | s++; |
| 925 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 926 | } |
| 927 | } |
| 928 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 929 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 930 | s++; |
| 931 | if (s < e && *s == '-') { |
| 932 | s++; |
| 933 | *p++ = '+'; |
| 934 | } else |
| 935 | { |
| 936 | inShift = 1; |
| 937 | bitsleft = 0; |
| 938 | } |
| 939 | } |
| 940 | else if (SPECIAL(ch,0,0)) { |
| 941 | errmsg = "unexpected special character"; |
| 942 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 943 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 944 | } |
| 945 | else { |
| 946 | *p++ = ch; |
| 947 | s++; |
| 948 | } |
| 949 | continue; |
| 950 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 951 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 952 | endinpos = s-starts; |
| 953 | if (unicode_decode_call_errorhandler( |
| 954 | errors, &errorHandler, |
| 955 | "utf7", errmsg, |
| 956 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 957 | (PyObject **)&unicode, &outpos, &p)) |
| 958 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | if (inShift) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 962 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 963 | endinpos = size; |
| 964 | if (unicode_decode_call_errorhandler( |
| 965 | errors, &errorHandler, |
| 966 | "utf7", "unterminated shift sequence", |
| 967 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 968 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 969 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 970 | if (s < e) |
| 971 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 974 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 975 | goto onError; |
| 976 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 977 | Py_XDECREF(errorHandler); |
| 978 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 979 | return (PyObject *)unicode; |
| 980 | |
| 981 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 982 | Py_XDECREF(errorHandler); |
| 983 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 984 | Py_DECREF(unicode); |
| 985 | return NULL; |
| 986 | } |
| 987 | |
| 988 | |
| 989 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 990 | int size, |
| 991 | int encodeSetO, |
| 992 | int encodeWhiteSpace, |
| 993 | const char *errors) |
| 994 | { |
| 995 | PyObject *v; |
| 996 | /* It might be possible to tighten this worst case */ |
| 997 | unsigned int cbAllocated = 5 * size; |
| 998 | int inShift = 0; |
| 999 | int i = 0; |
| 1000 | unsigned int bitsleft = 0; |
| 1001 | unsigned long charsleft = 0; |
| 1002 | char * out; |
| 1003 | char * start; |
| 1004 | |
| 1005 | if (size == 0) |
| 1006 | return PyString_FromStringAndSize(NULL, 0); |
| 1007 | |
| 1008 | v = PyString_FromStringAndSize(NULL, cbAllocated); |
| 1009 | if (v == NULL) |
| 1010 | return NULL; |
| 1011 | |
| 1012 | start = out = PyString_AS_STRING(v); |
| 1013 | for (;i < size; ++i) { |
| 1014 | Py_UNICODE ch = s[i]; |
| 1015 | |
| 1016 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1017 | if (ch == '+') { |
| 1018 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1019 | *out++ = '-'; |
| 1020 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1021 | charsleft = ch; |
| 1022 | bitsleft = 16; |
| 1023 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1024 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1025 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1026 | } else { |
| 1027 | *out++ = (char) ch; |
| 1028 | } |
| 1029 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1030 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1031 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1032 | charsleft = 0; |
| 1033 | bitsleft = 0; |
| 1034 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1035 | so no '-' is required, except if the character is itself a '-' */ |
| 1036 | if (B64CHAR(ch) || ch == '-') { |
| 1037 | *out++ = '-'; |
| 1038 | } |
| 1039 | inShift = 0; |
| 1040 | *out++ = (char) ch; |
| 1041 | } else { |
| 1042 | bitsleft += 16; |
| 1043 | charsleft = (charsleft << 16) | ch; |
| 1044 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1045 | |
| 1046 | /* If the next character is special then we dont' need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1047 | 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] | 1048 | or '-' then the shift sequence will be terminated implicitly and we |
| 1049 | don't have to insert a '-'. */ |
| 1050 | |
| 1051 | if (bitsleft == 0) { |
| 1052 | if (i + 1 < size) { |
| 1053 | Py_UNICODE ch2 = s[i+1]; |
| 1054 | |
| 1055 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1056 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1057 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1058 | *out++ = '-'; |
| 1059 | inShift = 0; |
| 1060 | } else { |
| 1061 | inShift = 0; |
| 1062 | } |
| 1063 | |
| 1064 | } |
| 1065 | else { |
| 1066 | *out++ = '-'; |
| 1067 | inShift = 0; |
| 1068 | } |
| 1069 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1070 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1071 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1072 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1073 | if (bitsleft) { |
| 1074 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1075 | *out++ = '-'; |
| 1076 | } |
| 1077 | |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 1078 | _PyString_Resize(&v, out - start); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1079 | return v; |
| 1080 | } |
| 1081 | |
| 1082 | #undef SPECIAL |
| 1083 | #undef B64 |
| 1084 | #undef B64CHAR |
| 1085 | #undef UB64 |
| 1086 | #undef ENCODE |
| 1087 | #undef DECODE |
| 1088 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1089 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1090 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1091 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1092 | char utf8_code_length[256] = { |
| 1093 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1094 | illegal prefix. see RFC 2279 for details */ |
| 1095 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1096 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1097 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1098 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1099 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1100 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1101 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1102 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1103 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1104 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1105 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1106 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1107 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1108 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1109 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1110 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1111 | }; |
| 1112 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1113 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
| 1114 | int size, |
| 1115 | const char *errors) |
| 1116 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1117 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1118 | int n; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1119 | int startinpos; |
| 1120 | int endinpos; |
| 1121 | int outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1122 | const char *e; |
| 1123 | PyUnicodeObject *unicode; |
| 1124 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1125 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1126 | PyObject *errorHandler = NULL; |
| 1127 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1128 | |
| 1129 | /* Note: size will always be longer than the resulting Unicode |
| 1130 | character count */ |
| 1131 | unicode = _PyUnicode_New(size); |
| 1132 | if (!unicode) |
| 1133 | return NULL; |
| 1134 | if (size == 0) |
| 1135 | return (PyObject *)unicode; |
| 1136 | |
| 1137 | /* Unpack UTF-8 encoded data */ |
| 1138 | p = unicode->str; |
| 1139 | e = s + size; |
| 1140 | |
| 1141 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1142 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1143 | |
| 1144 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1145 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1146 | s++; |
| 1147 | continue; |
| 1148 | } |
| 1149 | |
| 1150 | n = utf8_code_length[ch]; |
| 1151 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1152 | if (s + n > e) { |
| 1153 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1154 | startinpos = s-starts; |
| 1155 | endinpos = size; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1156 | goto utf8Error; |
| 1157 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1158 | |
| 1159 | switch (n) { |
| 1160 | |
| 1161 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1162 | errmsg = "unexpected code byte"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1163 | startinpos = s-starts; |
| 1164 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1165 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1166 | |
| 1167 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1168 | errmsg = "internal error"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1169 | startinpos = s-starts; |
| 1170 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1171 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1172 | |
| 1173 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1174 | if ((s[1] & 0xc0) != 0x80) { |
| 1175 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1176 | startinpos = s-starts; |
| 1177 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1178 | goto utf8Error; |
| 1179 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1180 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1181 | if (ch < 0x80) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1182 | startinpos = s-starts; |
| 1183 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1184 | errmsg = "illegal encoding"; |
| 1185 | goto utf8Error; |
| 1186 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1187 | else |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1188 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1189 | break; |
| 1190 | |
| 1191 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1192 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1193 | (s[2] & 0xc0) != 0x80) { |
| 1194 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1195 | startinpos = s-starts; |
| 1196 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1197 | goto utf8Error; |
| 1198 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1199 | 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] | 1200 | if (ch < 0x0800) { |
| 1201 | /* Note: UTF-8 encodings of surrogates are considered |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1202 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1203 | |
| 1204 | XXX For wide builds (UCS-4) we should probably try |
| 1205 | to recombine the surrogates into a single code |
| 1206 | unit. |
| 1207 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1208 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1209 | startinpos = s-starts; |
| 1210 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1211 | goto utf8Error; |
| 1212 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1213 | else |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1214 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1215 | break; |
| 1216 | |
| 1217 | case 4: |
| 1218 | if ((s[1] & 0xc0) != 0x80 || |
| 1219 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1220 | (s[3] & 0xc0) != 0x80) { |
| 1221 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1222 | startinpos = s-starts; |
| 1223 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1224 | goto utf8Error; |
| 1225 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1226 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 1227 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 1228 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1229 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1230 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1231 | || (ch > 0x10ffff)) /* maximum value allowed for |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1232 | UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1233 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1234 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1235 | startinpos = s-starts; |
| 1236 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1237 | goto utf8Error; |
| 1238 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1239 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1240 | *p++ = (Py_UNICODE)ch; |
| 1241 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1242 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1243 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1244 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 1245 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1246 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1247 | /* high surrogate = top 10 bits added to D800 */ |
| 1248 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1249 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1250 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1251 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1252 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1253 | break; |
| 1254 | |
| 1255 | default: |
| 1256 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1257 | errmsg = "unsupported Unicode code range"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1258 | startinpos = s-starts; |
| 1259 | endinpos = startinpos+n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1260 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1261 | } |
| 1262 | s += n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1263 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1264 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1265 | utf8Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1266 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1267 | if (unicode_decode_call_errorhandler( |
| 1268 | errors, &errorHandler, |
| 1269 | "utf8", errmsg, |
| 1270 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1271 | (PyObject **)&unicode, &outpos, &p)) |
| 1272 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1276 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1277 | goto onError; |
| 1278 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1279 | Py_XDECREF(errorHandler); |
| 1280 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1281 | return (PyObject *)unicode; |
| 1282 | |
| 1283 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1284 | Py_XDECREF(errorHandler); |
| 1285 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1286 | Py_DECREF(unicode); |
| 1287 | return NULL; |
| 1288 | } |
| 1289 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1290 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 1291 | and allocate exactly as much space needed at the end. Else allocate the |
| 1292 | maximum possible needed (4 result bytes per Unicode character), and return |
| 1293 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1294 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1295 | PyObject * |
| 1296 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 1297 | int size, |
| 1298 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1299 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1300 | #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] | 1301 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1302 | int i; /* index into s of next input byte */ |
| 1303 | PyObject *v; /* result string object */ |
| 1304 | char *p; /* next free byte in output buffer */ |
| 1305 | int nallocated; /* number of result bytes allocated */ |
| 1306 | int nneeded; /* number of result bytes needed */ |
| 1307 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1308 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1309 | assert(s != NULL); |
| 1310 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1311 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1312 | if (size <= MAX_SHORT_UNICHARS) { |
| 1313 | /* Write into the stack buffer; nallocated can't overflow. |
| 1314 | * At the end, we'll allocate exactly as much heap space as it |
| 1315 | * turns out we need. |
| 1316 | */ |
| 1317 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
| 1318 | v = NULL; /* will allocate after we're done */ |
| 1319 | p = stackbuf; |
| 1320 | } |
| 1321 | else { |
| 1322 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 1323 | nallocated = size * 4; |
| 1324 | if (nallocated / 4 != size) /* overflow! */ |
| 1325 | return PyErr_NoMemory(); |
| 1326 | v = PyString_FromStringAndSize(NULL, nallocated); |
| 1327 | if (v == NULL) |
| 1328 | return NULL; |
| 1329 | p = PyString_AS_STRING(v); |
| 1330 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1331 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1332 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1333 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1334 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1335 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1336 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1337 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1338 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1339 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1340 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 1341 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 1342 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1343 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1344 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1345 | /* Encode UCS2 Unicode ordinals */ |
| 1346 | if (ch < 0x10000) { |
| 1347 | /* Special case: check for high surrogate */ |
| 1348 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 1349 | Py_UCS4 ch2 = s[i]; |
| 1350 | /* Check for low surrogate and combine the two to |
| 1351 | form a UCS4 value */ |
| 1352 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1353 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1354 | i++; |
| 1355 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1356 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1357 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1358 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1359 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1360 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1361 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1362 | continue; |
| 1363 | } |
| 1364 | encodeUCS4: |
| 1365 | /* Encode UCS4 Unicode ordinals */ |
| 1366 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 1367 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 1368 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1369 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1370 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1371 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 1372 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1373 | if (v == NULL) { |
| 1374 | /* This was stack allocated. */ |
| 1375 | nneeded = Py_SAFE_DOWNCAST(p - stackbuf, long, int); |
| 1376 | assert(nneeded <= nallocated); |
| 1377 | v = PyString_FromStringAndSize(stackbuf, nneeded); |
| 1378 | } |
| 1379 | else { |
| 1380 | /* Cut back to size actually needed. */ |
| 1381 | nneeded = Py_SAFE_DOWNCAST(p - PyString_AS_STRING(v), long, int); |
| 1382 | assert(nneeded <= nallocated); |
| 1383 | _PyString_Resize(&v, nneeded); |
| 1384 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1385 | return v; |
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 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1390 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 1391 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1392 | if (!PyUnicode_Check(unicode)) { |
| 1393 | PyErr_BadArgument(); |
| 1394 | return NULL; |
| 1395 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 1396 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1397 | PyUnicode_GET_SIZE(unicode), |
| 1398 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 1402 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1403 | PyObject * |
| 1404 | PyUnicode_DecodeUTF16(const char *s, |
| 1405 | int size, |
| 1406 | const char *errors, |
| 1407 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1408 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1409 | const char *starts = s; |
| 1410 | int startinpos; |
| 1411 | int endinpos; |
| 1412 | int outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1413 | PyUnicodeObject *unicode; |
| 1414 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1415 | const unsigned char *q, *e; |
| 1416 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1417 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1418 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 1419 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1420 | int ihi = 1, ilo = 0; |
| 1421 | #else |
| 1422 | int ihi = 0, ilo = 1; |
| 1423 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1424 | PyObject *errorHandler = NULL; |
| 1425 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1426 | |
| 1427 | /* Note: size will always be longer than the resulting Unicode |
| 1428 | character count */ |
| 1429 | unicode = _PyUnicode_New(size); |
| 1430 | if (!unicode) |
| 1431 | return NULL; |
| 1432 | if (size == 0) |
| 1433 | return (PyObject *)unicode; |
| 1434 | |
| 1435 | /* Unpack UTF-16 encoded data */ |
| 1436 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1437 | q = (unsigned char *)s; |
| 1438 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1439 | |
| 1440 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1441 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1442 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1443 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 1444 | byte order setting accordingly. In native mode, the leading BOM |
| 1445 | mark is skipped, in all other modes, it is copied to the output |
| 1446 | stream as-is (giving a ZWNBSP character). */ |
| 1447 | if (bo == 0) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1448 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1449 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1450 | if (bom == 0xFEFF) { |
| 1451 | q += 2; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1452 | bo = -1; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1453 | } |
| 1454 | else if (bom == 0xFFFE) { |
| 1455 | q += 2; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1456 | bo = 1; |
| 1457 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1458 | #else |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1459 | if (bom == 0xFEFF) { |
| 1460 | q += 2; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1461 | bo = 1; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1462 | } |
| 1463 | else if (bom == 0xFFFE) { |
| 1464 | q += 2; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1465 | bo = -1; |
| 1466 | } |
| 1467 | #endif |
| 1468 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1469 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1470 | if (bo == -1) { |
| 1471 | /* force LE */ |
| 1472 | ihi = 1; |
| 1473 | ilo = 0; |
| 1474 | } |
| 1475 | else if (bo == 1) { |
| 1476 | /* force BE */ |
| 1477 | ihi = 0; |
| 1478 | ilo = 1; |
| 1479 | } |
| 1480 | |
| 1481 | while (q < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1482 | Py_UNICODE ch; |
| 1483 | /* remaing bytes at the end? (size should be even) */ |
| 1484 | if (e-q<2) { |
| 1485 | errmsg = "truncated data"; |
| 1486 | startinpos = ((const char *)q)-starts; |
| 1487 | endinpos = ((const char *)e)-starts; |
| 1488 | goto utf16Error; |
| 1489 | /* The remaining input chars are ignored if the callback |
| 1490 | chooses to skip the input */ |
| 1491 | } |
| 1492 | ch = (q[ihi] << 8) | q[ilo]; |
| 1493 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1494 | q += 2; |
| 1495 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1496 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 1497 | *p++ = ch; |
| 1498 | continue; |
| 1499 | } |
| 1500 | |
| 1501 | /* UTF-16 code pair: */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1502 | if (q >= e) { |
| 1503 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1504 | startinpos = (((const char *)q)-2)-starts; |
| 1505 | endinpos = ((const char *)e)-starts; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1506 | goto utf16Error; |
| 1507 | } |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 1508 | if (0xD800 <= ch && ch <= 0xDBFF) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1509 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 1510 | q += 2; |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 1511 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1512 | #ifndef Py_UNICODE_WIDE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1513 | *p++ = ch; |
| 1514 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1515 | #else |
| 1516 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1517 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1518 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1519 | } |
| 1520 | else { |
| 1521 | errmsg = "illegal UTF-16 surrogate"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1522 | startinpos = (((const char *)q)-4)-starts; |
| 1523 | endinpos = startinpos+2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1524 | goto utf16Error; |
| 1525 | } |
| 1526 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1527 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1528 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1529 | startinpos = (((const char *)q)-2)-starts; |
| 1530 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1531 | /* Fall through to report the error */ |
| 1532 | |
| 1533 | utf16Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1534 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1535 | if (unicode_decode_call_errorhandler( |
| 1536 | errors, &errorHandler, |
| 1537 | "utf16", errmsg, |
| 1538 | starts, size, &startinpos, &endinpos, &exc, (const char **)&q, |
| 1539 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1540 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | if (byteorder) |
| 1544 | *byteorder = bo; |
| 1545 | |
| 1546 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1547 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1548 | goto onError; |
| 1549 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1550 | Py_XDECREF(errorHandler); |
| 1551 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1552 | return (PyObject *)unicode; |
| 1553 | |
| 1554 | onError: |
| 1555 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1556 | Py_XDECREF(errorHandler); |
| 1557 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1558 | return NULL; |
| 1559 | } |
| 1560 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1561 | PyObject * |
| 1562 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 1563 | int size, |
| 1564 | const char *errors, |
| 1565 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1566 | { |
| 1567 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1568 | unsigned char *p; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1569 | #ifdef Py_UNICODE_WIDE |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1570 | int i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1571 | #else |
| 1572 | const int pairs = 0; |
| 1573 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1574 | /* Offsets from p for storing byte pairs in the right order. */ |
| 1575 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1576 | int ihi = 1, ilo = 0; |
| 1577 | #else |
| 1578 | int ihi = 0, ilo = 1; |
| 1579 | #endif |
| 1580 | |
| 1581 | #define STORECHAR(CH) \ |
| 1582 | do { \ |
| 1583 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 1584 | p[ilo] = (CH) & 0xff; \ |
| 1585 | p += 2; \ |
| 1586 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1587 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1588 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1589 | for (i = pairs = 0; i < size; i++) |
| 1590 | if (s[i] >= 0x10000) |
| 1591 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1592 | #endif |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1593 | v = PyString_FromStringAndSize(NULL, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1594 | 2 * (size + pairs + (byteorder == 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1595 | if (v == NULL) |
| 1596 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1597 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1598 | p = (unsigned char *)PyString_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1599 | if (byteorder == 0) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1600 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 1601 | if (size == 0) |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 1602 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1603 | |
| 1604 | if (byteorder == -1) { |
| 1605 | /* force LE */ |
| 1606 | ihi = 1; |
| 1607 | ilo = 0; |
| 1608 | } |
| 1609 | else if (byteorder == 1) { |
| 1610 | /* force BE */ |
| 1611 | ihi = 0; |
| 1612 | ilo = 1; |
| 1613 | } |
| 1614 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1615 | while (size-- > 0) { |
| 1616 | Py_UNICODE ch = *s++; |
| 1617 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1618 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1619 | if (ch >= 0x10000) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1620 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 1621 | ch = 0xD800 | ((ch-0x10000) >> 10); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1622 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1623 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1624 | STORECHAR(ch); |
| 1625 | if (ch2) |
| 1626 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1627 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1628 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1629 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 1633 | { |
| 1634 | if (!PyUnicode_Check(unicode)) { |
| 1635 | PyErr_BadArgument(); |
| 1636 | return NULL; |
| 1637 | } |
| 1638 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 1639 | PyUnicode_GET_SIZE(unicode), |
| 1640 | NULL, |
| 1641 | 0); |
| 1642 | } |
| 1643 | |
| 1644 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 1645 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1646 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 1647 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1648 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
| 1649 | int size, |
| 1650 | const char *errors) |
| 1651 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1652 | const char *starts = s; |
| 1653 | int startinpos; |
| 1654 | int endinpos; |
| 1655 | int outpos; |
| 1656 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1657 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1658 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1659 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1660 | char* message; |
| 1661 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1662 | PyObject *errorHandler = NULL; |
| 1663 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1664 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1665 | /* Escaped strings will always be longer than the resulting |
| 1666 | 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] | 1667 | length after conversion to the true value. |
| 1668 | (but if the error callback returns a long replacement string |
| 1669 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1670 | v = _PyUnicode_New(size); |
| 1671 | if (v == NULL) |
| 1672 | goto onError; |
| 1673 | if (size == 0) |
| 1674 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1675 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1676 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1677 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1678 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1679 | while (s < end) { |
| 1680 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 1681 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1682 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1683 | |
| 1684 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 1685 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1686 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1687 | continue; |
| 1688 | } |
| 1689 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1690 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1691 | /* \ - Escapes */ |
| 1692 | s++; |
| 1693 | switch (*s++) { |
| 1694 | |
| 1695 | /* \x escapes */ |
| 1696 | case '\n': break; |
| 1697 | case '\\': *p++ = '\\'; break; |
| 1698 | case '\'': *p++ = '\''; break; |
| 1699 | case '\"': *p++ = '\"'; break; |
| 1700 | case 'b': *p++ = '\b'; break; |
| 1701 | case 'f': *p++ = '\014'; break; /* FF */ |
| 1702 | case 't': *p++ = '\t'; break; |
| 1703 | case 'n': *p++ = '\n'; break; |
| 1704 | case 'r': *p++ = '\r'; break; |
| 1705 | case 'v': *p++ = '\013'; break; /* VT */ |
| 1706 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 1707 | |
| 1708 | /* \OOO (octal) escapes */ |
| 1709 | case '0': case '1': case '2': case '3': |
| 1710 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1711 | x = s[-1] - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1712 | if ('0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1713 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1714 | if ('0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1715 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1716 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1717 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1718 | break; |
| 1719 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1720 | /* hex escapes */ |
| 1721 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1722 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1723 | digits = 2; |
| 1724 | message = "truncated \\xXX escape"; |
| 1725 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1726 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1727 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1728 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1729 | digits = 4; |
| 1730 | message = "truncated \\uXXXX escape"; |
| 1731 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1732 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1733 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1734 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1735 | digits = 8; |
| 1736 | message = "truncated \\UXXXXXXXX escape"; |
| 1737 | hexescape: |
| 1738 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1739 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1740 | if (s+digits>end) { |
| 1741 | endinpos = size; |
| 1742 | if (unicode_decode_call_errorhandler( |
| 1743 | errors, &errorHandler, |
| 1744 | "unicodeescape", "end of string in escape sequence", |
| 1745 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1746 | (PyObject **)&v, &outpos, &p)) |
| 1747 | goto onError; |
| 1748 | goto nextByte; |
| 1749 | } |
| 1750 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1751 | c = (unsigned char) s[i]; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1752 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1753 | endinpos = (s+i+1)-starts; |
| 1754 | if (unicode_decode_call_errorhandler( |
| 1755 | errors, &errorHandler, |
| 1756 | "unicodeescape", message, |
| 1757 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1758 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1759 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1760 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1761 | } |
| 1762 | chr = (chr<<4) & ~0xF; |
| 1763 | if (c >= '0' && c <= '9') |
| 1764 | chr += c - '0'; |
| 1765 | else if (c >= 'a' && c <= 'f') |
| 1766 | chr += 10 + c - 'a'; |
| 1767 | else |
| 1768 | chr += 10 + c - 'A'; |
| 1769 | } |
| 1770 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 1771 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1772 | /* _decoding_error will have already written into the |
| 1773 | target buffer. */ |
| 1774 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1775 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1776 | /* when we get here, chr is a 32-bit unicode character */ |
| 1777 | if (chr <= 0xffff) |
| 1778 | /* UCS-2 character */ |
| 1779 | *p++ = (Py_UNICODE) chr; |
| 1780 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1781 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1782 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1783 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1784 | *p++ = chr; |
| 1785 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1786 | chr -= 0x10000L; |
| 1787 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1788 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1789 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1790 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1791 | endinpos = s-starts; |
| 1792 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1793 | if (unicode_decode_call_errorhandler( |
| 1794 | errors, &errorHandler, |
| 1795 | "unicodeescape", "illegal Unicode character", |
| 1796 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1797 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1798 | goto onError; |
| 1799 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1800 | break; |
| 1801 | |
| 1802 | /* \N{name} */ |
| 1803 | case 'N': |
| 1804 | message = "malformed \\N character escape"; |
| 1805 | if (ucnhash_CAPI == NULL) { |
| 1806 | /* load the unicode data module */ |
| 1807 | PyObject *m, *v; |
| 1808 | m = PyImport_ImportModule("unicodedata"); |
| 1809 | if (m == NULL) |
| 1810 | goto ucnhashError; |
| 1811 | v = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
| 1812 | Py_DECREF(m); |
| 1813 | if (v == NULL) |
| 1814 | goto ucnhashError; |
| 1815 | ucnhash_CAPI = PyCObject_AsVoidPtr(v); |
| 1816 | Py_DECREF(v); |
| 1817 | if (ucnhash_CAPI == NULL) |
| 1818 | goto ucnhashError; |
| 1819 | } |
| 1820 | if (*s == '{') { |
| 1821 | const char *start = s+1; |
| 1822 | /* look for the closing brace */ |
| 1823 | while (*s != '}' && s < end) |
| 1824 | s++; |
| 1825 | if (s > start && s < end && *s == '}') { |
| 1826 | /* found a name. look it up in the unicode database */ |
| 1827 | message = "unknown Unicode character name"; |
| 1828 | s++; |
| 1829 | if (ucnhash_CAPI->getcode(start, s-start-1, &chr)) |
| 1830 | goto store; |
| 1831 | } |
| 1832 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1833 | endinpos = s-starts; |
| 1834 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1835 | if (unicode_decode_call_errorhandler( |
| 1836 | errors, &errorHandler, |
| 1837 | "unicodeescape", message, |
| 1838 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1839 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1840 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1841 | break; |
| 1842 | |
| 1843 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1844 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1845 | message = "\\ at end of string"; |
| 1846 | s--; |
| 1847 | endinpos = s-starts; |
| 1848 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1849 | if (unicode_decode_call_errorhandler( |
| 1850 | errors, &errorHandler, |
| 1851 | "unicodeescape", message, |
| 1852 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1853 | (PyObject **)&v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1854 | goto onError; |
| 1855 | } |
| 1856 | else { |
| 1857 | *p++ = '\\'; |
| 1858 | *p++ = (unsigned char)s[-1]; |
| 1859 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1860 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1861 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1862 | nextByte: |
| 1863 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1864 | } |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1865 | if (_PyUnicode_Resize(&v, (int)(p - PyUnicode_AS_UNICODE(v))) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1866 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 1867 | Py_XDECREF(errorHandler); |
| 1868 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1869 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1870 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1871 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1872 | PyErr_SetString( |
| 1873 | PyExc_UnicodeError, |
| 1874 | "\\N escapes not supported (can't load unicodedata module)" |
| 1875 | ); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1876 | Py_XDECREF(errorHandler); |
| 1877 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 1878 | return NULL; |
| 1879 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1880 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1881 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1882 | Py_XDECREF(errorHandler); |
| 1883 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1884 | return NULL; |
| 1885 | } |
| 1886 | |
| 1887 | /* Return a Unicode-Escape string version of the Unicode object. |
| 1888 | |
| 1889 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 1890 | appropriate. |
| 1891 | |
| 1892 | */ |
| 1893 | |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 1894 | static const Py_UNICODE *findchar(const Py_UNICODE *s, |
| 1895 | int size, |
| 1896 | Py_UNICODE ch); |
| 1897 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1898 | static |
| 1899 | PyObject *unicodeescape_string(const Py_UNICODE *s, |
| 1900 | int size, |
| 1901 | int quotes) |
| 1902 | { |
| 1903 | PyObject *repr; |
| 1904 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1905 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 1906 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1907 | |
| 1908 | repr = PyString_FromStringAndSize(NULL, 2 + 6*size + 1); |
| 1909 | if (repr == NULL) |
| 1910 | return NULL; |
| 1911 | |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1912 | p = PyString_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1913 | |
| 1914 | if (quotes) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1915 | *p++ = 'u'; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1916 | *p++ = (findchar(s, size, '\'') && |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1917 | !findchar(s, size, '"')) ? '"' : '\''; |
| 1918 | } |
| 1919 | while (size-- > 0) { |
| 1920 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1921 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1922 | /* Escape quotes */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1923 | if (quotes && |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1924 | (ch == (Py_UNICODE) PyString_AS_STRING(repr)[1] || ch == '\\')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1925 | *p++ = '\\'; |
| 1926 | *p++ = (char) ch; |
Guido van Rossum | ad9744a | 2001-09-21 15:38:17 +0000 | [diff] [blame] | 1927 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1928 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1929 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 1930 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1931 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 1932 | else if (ch >= 0x10000) { |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1933 | int offset = p - PyString_AS_STRING(repr); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1934 | |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1935 | /* Resize the string if necessary */ |
| 1936 | if (offset + 12 > PyString_GET_SIZE(repr)) { |
| 1937 | if (_PyString_Resize(&repr, PyString_GET_SIZE(repr) + 100)) |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 1938 | return NULL; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1939 | p = PyString_AS_STRING(repr) + offset; |
| 1940 | } |
| 1941 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1942 | *p++ = '\\'; |
| 1943 | *p++ = 'U'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1944 | *p++ = hexdigit[(ch >> 28) & 0x0000000F]; |
| 1945 | *p++ = hexdigit[(ch >> 24) & 0x0000000F]; |
| 1946 | *p++ = hexdigit[(ch >> 20) & 0x0000000F]; |
| 1947 | *p++ = hexdigit[(ch >> 16) & 0x0000000F]; |
| 1948 | *p++ = hexdigit[(ch >> 12) & 0x0000000F]; |
| 1949 | *p++ = hexdigit[(ch >> 8) & 0x0000000F]; |
| 1950 | *p++ = hexdigit[(ch >> 4) & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1951 | *p++ = hexdigit[ch & 0x0000000F]; |
| 1952 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1953 | } |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 1954 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1955 | /* Map UTF-16 surrogate pairs to Unicode \UXXXXXXXX escapes */ |
| 1956 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 1957 | Py_UNICODE ch2; |
| 1958 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1959 | |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1960 | ch2 = *s++; |
| 1961 | size--; |
| 1962 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 1963 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 1964 | *p++ = '\\'; |
| 1965 | *p++ = 'U'; |
| 1966 | *p++ = hexdigit[(ucs >> 28) & 0x0000000F]; |
| 1967 | *p++ = hexdigit[(ucs >> 24) & 0x0000000F]; |
| 1968 | *p++ = hexdigit[(ucs >> 20) & 0x0000000F]; |
| 1969 | *p++ = hexdigit[(ucs >> 16) & 0x0000000F]; |
| 1970 | *p++ = hexdigit[(ucs >> 12) & 0x0000000F]; |
| 1971 | *p++ = hexdigit[(ucs >> 8) & 0x0000000F]; |
| 1972 | *p++ = hexdigit[(ucs >> 4) & 0x0000000F]; |
| 1973 | *p++ = hexdigit[ucs & 0x0000000F]; |
| 1974 | continue; |
| 1975 | } |
| 1976 | /* Fall through: isolated surrogates are copied as-is */ |
| 1977 | s--; |
| 1978 | size++; |
| 1979 | } |
| 1980 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1981 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1982 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1983 | *p++ = '\\'; |
| 1984 | *p++ = 'u'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1985 | *p++ = hexdigit[(ch >> 12) & 0x000F]; |
| 1986 | *p++ = hexdigit[(ch >> 8) & 0x000F]; |
| 1987 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 1988 | *p++ = hexdigit[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1989 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1990 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 1991 | /* Map special whitespace to '\t', \n', '\r' */ |
| 1992 | else if (ch == '\t') { |
| 1993 | *p++ = '\\'; |
| 1994 | *p++ = 't'; |
| 1995 | } |
| 1996 | else if (ch == '\n') { |
| 1997 | *p++ = '\\'; |
| 1998 | *p++ = 'n'; |
| 1999 | } |
| 2000 | else if (ch == '\r') { |
| 2001 | *p++ = '\\'; |
| 2002 | *p++ = 'r'; |
| 2003 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2004 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2005 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 2006 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2007 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2008 | *p++ = 'x'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2009 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2010 | *p++ = hexdigit[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2011 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2012 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2013 | /* Copy everything else as-is */ |
| 2014 | else |
| 2015 | *p++ = (char) ch; |
| 2016 | } |
| 2017 | if (quotes) |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2018 | *p++ = PyString_AS_STRING(repr)[1]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2019 | |
| 2020 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2021 | _PyString_Resize(&repr, p - PyString_AS_STRING(repr)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2022 | return repr; |
| 2023 | } |
| 2024 | |
| 2025 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 2026 | int size) |
| 2027 | { |
| 2028 | return unicodeescape_string(s, size, 0); |
| 2029 | } |
| 2030 | |
| 2031 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 2032 | { |
| 2033 | if (!PyUnicode_Check(unicode)) { |
| 2034 | PyErr_BadArgument(); |
| 2035 | return NULL; |
| 2036 | } |
| 2037 | return PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2038 | PyUnicode_GET_SIZE(unicode)); |
| 2039 | } |
| 2040 | |
| 2041 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 2042 | |
| 2043 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
| 2044 | int size, |
| 2045 | const char *errors) |
| 2046 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2047 | const char *starts = s; |
| 2048 | int startinpos; |
| 2049 | int endinpos; |
| 2050 | int outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2051 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2052 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2053 | const char *end; |
| 2054 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2055 | PyObject *errorHandler = NULL; |
| 2056 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2057 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2058 | /* Escaped strings will always be longer than the resulting |
| 2059 | 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] | 2060 | length after conversion to the true value. (But decoding error |
| 2061 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2062 | v = _PyUnicode_New(size); |
| 2063 | if (v == NULL) |
| 2064 | goto onError; |
| 2065 | if (size == 0) |
| 2066 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2067 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2068 | end = s + size; |
| 2069 | while (s < end) { |
| 2070 | unsigned char c; |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 2071 | Py_UCS4 x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2072 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2073 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2074 | |
| 2075 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2076 | if (*s != '\\') { |
| 2077 | *p++ = (unsigned char)*s++; |
| 2078 | continue; |
| 2079 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2080 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2081 | |
| 2082 | /* \u-escapes are only interpreted iff the number of leading |
| 2083 | backslashes if odd */ |
| 2084 | bs = s; |
| 2085 | for (;s < end;) { |
| 2086 | if (*s != '\\') |
| 2087 | break; |
| 2088 | *p++ = (unsigned char)*s++; |
| 2089 | } |
| 2090 | if (((s - bs) & 1) == 0 || |
| 2091 | s >= end || |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2092 | (*s != 'u' && *s != 'U')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2093 | continue; |
| 2094 | } |
| 2095 | p--; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2096 | count = *s=='u' ? 4 : 8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2097 | s++; |
| 2098 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2099 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2100 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2101 | for (x = 0, i = 0; i < count; ++i, ++s) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2102 | c = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2103 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2104 | endinpos = s-starts; |
| 2105 | if (unicode_decode_call_errorhandler( |
| 2106 | errors, &errorHandler, |
| 2107 | "rawunicodeescape", "truncated \\uXXXX", |
| 2108 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2109 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2110 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2111 | goto nextByte; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2112 | } |
| 2113 | x = (x<<4) & ~0xF; |
| 2114 | if (c >= '0' && c <= '9') |
| 2115 | x += c - '0'; |
| 2116 | else if (c >= 'a' && c <= 'f') |
| 2117 | x += 10 + c - 'a'; |
| 2118 | else |
| 2119 | x += 10 + c - 'A'; |
| 2120 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2121 | #ifndef Py_UNICODE_WIDE |
| 2122 | if (x > 0x10000) { |
| 2123 | if (unicode_decode_call_errorhandler( |
| 2124 | errors, &errorHandler, |
| 2125 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
| 2126 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2127 | (PyObject **)&v, &outpos, &p)) |
| 2128 | goto onError; |
| 2129 | } |
| 2130 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2131 | *p++ = x; |
| 2132 | nextByte: |
| 2133 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2134 | } |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2135 | if (_PyUnicode_Resize(&v, (int)(p - PyUnicode_AS_UNICODE(v))) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2136 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2137 | Py_XDECREF(errorHandler); |
| 2138 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2139 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2140 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2141 | onError: |
| 2142 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2143 | Py_XDECREF(errorHandler); |
| 2144 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2145 | return NULL; |
| 2146 | } |
| 2147 | |
| 2148 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 2149 | int size) |
| 2150 | { |
| 2151 | PyObject *repr; |
| 2152 | char *p; |
| 2153 | char *q; |
| 2154 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2155 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2156 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2157 | #ifdef Py_UNICODE_WIDE |
| 2158 | repr = PyString_FromStringAndSize(NULL, 10 * size); |
| 2159 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2160 | repr = PyString_FromStringAndSize(NULL, 6 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2161 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2162 | if (repr == NULL) |
| 2163 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2164 | if (size == 0) |
| 2165 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2166 | |
| 2167 | p = q = PyString_AS_STRING(repr); |
| 2168 | while (size-- > 0) { |
| 2169 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2170 | #ifdef Py_UNICODE_WIDE |
| 2171 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2172 | if (ch >= 0x10000) { |
| 2173 | *p++ = '\\'; |
| 2174 | *p++ = 'U'; |
| 2175 | *p++ = hexdigit[(ch >> 28) & 0xf]; |
| 2176 | *p++ = hexdigit[(ch >> 24) & 0xf]; |
| 2177 | *p++ = hexdigit[(ch >> 20) & 0xf]; |
| 2178 | *p++ = hexdigit[(ch >> 16) & 0xf]; |
| 2179 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 2180 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 2181 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 2182 | *p++ = hexdigit[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2183 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2184 | else |
| 2185 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2186 | /* Map 16-bit characters to '\uxxxx' */ |
| 2187 | if (ch >= 256) { |
| 2188 | *p++ = '\\'; |
| 2189 | *p++ = 'u'; |
| 2190 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 2191 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 2192 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 2193 | *p++ = hexdigit[ch & 15]; |
| 2194 | } |
| 2195 | /* Copy everything else as-is */ |
| 2196 | else |
| 2197 | *p++ = (char) ch; |
| 2198 | } |
| 2199 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2200 | _PyString_Resize(&repr, p - q); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2201 | return repr; |
| 2202 | } |
| 2203 | |
| 2204 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 2205 | { |
| 2206 | if (!PyUnicode_Check(unicode)) { |
| 2207 | PyErr_BadArgument(); |
| 2208 | return NULL; |
| 2209 | } |
| 2210 | return PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2211 | PyUnicode_GET_SIZE(unicode)); |
| 2212 | } |
| 2213 | |
| 2214 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 2215 | |
| 2216 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
| 2217 | int size, |
| 2218 | const char *errors) |
| 2219 | { |
| 2220 | PyUnicodeObject *v; |
| 2221 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2222 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2223 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2224 | if (size == 1) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 2225 | Py_UNICODE r = *(unsigned char*)s; |
| 2226 | return PyUnicode_FromUnicode(&r, 1); |
| 2227 | } |
| 2228 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2229 | v = _PyUnicode_New(size); |
| 2230 | if (v == NULL) |
| 2231 | goto onError; |
| 2232 | if (size == 0) |
| 2233 | return (PyObject *)v; |
| 2234 | p = PyUnicode_AS_UNICODE(v); |
| 2235 | while (size-- > 0) |
| 2236 | *p++ = (unsigned char)*s++; |
| 2237 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2238 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2239 | onError: |
| 2240 | Py_XDECREF(v); |
| 2241 | return NULL; |
| 2242 | } |
| 2243 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2244 | /* create or adjust a UnicodeEncodeError */ |
| 2245 | static void make_encode_exception(PyObject **exceptionObject, |
| 2246 | const char *encoding, |
| 2247 | const Py_UNICODE *unicode, int size, |
| 2248 | int startpos, int endpos, |
| 2249 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2250 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2251 | if (*exceptionObject == NULL) { |
| 2252 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 2253 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2254 | } |
| 2255 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2256 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 2257 | goto onError; |
| 2258 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 2259 | goto onError; |
| 2260 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 2261 | goto onError; |
| 2262 | return; |
| 2263 | onError: |
| 2264 | Py_DECREF(*exceptionObject); |
| 2265 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2266 | } |
| 2267 | } |
| 2268 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2269 | /* raises a UnicodeEncodeError */ |
| 2270 | static void raise_encode_exception(PyObject **exceptionObject, |
| 2271 | const char *encoding, |
| 2272 | const Py_UNICODE *unicode, int size, |
| 2273 | int startpos, int endpos, |
| 2274 | const char *reason) |
| 2275 | { |
| 2276 | make_encode_exception(exceptionObject, |
| 2277 | encoding, unicode, size, startpos, endpos, reason); |
| 2278 | if (*exceptionObject != NULL) |
| 2279 | PyCodec_StrictErrors(*exceptionObject); |
| 2280 | } |
| 2281 | |
| 2282 | /* error handling callback helper: |
| 2283 | build arguments, call the callback and check the arguments, |
| 2284 | put the result into newpos and return the replacement string, which |
| 2285 | has to be freed by the caller */ |
| 2286 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 2287 | PyObject **errorHandler, |
| 2288 | const char *encoding, const char *reason, |
| 2289 | const Py_UNICODE *unicode, int size, PyObject **exceptionObject, |
| 2290 | int startpos, int endpos, |
| 2291 | int *newpos) |
| 2292 | { |
| 2293 | static char *argparse = "O!i;encoding error handler must return (unicode, int) tuple"; |
| 2294 | |
| 2295 | PyObject *restuple; |
| 2296 | PyObject *resunicode; |
| 2297 | |
| 2298 | if (*errorHandler == NULL) { |
| 2299 | *errorHandler = PyCodec_LookupError(errors); |
| 2300 | if (*errorHandler == NULL) |
| 2301 | return NULL; |
| 2302 | } |
| 2303 | |
| 2304 | make_encode_exception(exceptionObject, |
| 2305 | encoding, unicode, size, startpos, endpos, reason); |
| 2306 | if (*exceptionObject == NULL) |
| 2307 | return NULL; |
| 2308 | |
| 2309 | restuple = PyObject_CallFunctionObjArgs( |
| 2310 | *errorHandler, *exceptionObject, NULL); |
| 2311 | if (restuple == NULL) |
| 2312 | return NULL; |
| 2313 | if (!PyTuple_Check(restuple)) { |
| 2314 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 2315 | Py_DECREF(restuple); |
| 2316 | return NULL; |
| 2317 | } |
| 2318 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 2319 | &resunicode, newpos)) { |
| 2320 | Py_DECREF(restuple); |
| 2321 | return NULL; |
| 2322 | } |
| 2323 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2324 | *newpos = size+*newpos; |
| 2325 | if (*newpos<0 || *newpos>size) { |
| 2326 | PyErr_Format(PyExc_IndexError, "position %d from error handler out of bounds", *newpos); |
| 2327 | Py_DECREF(restuple); |
| 2328 | return NULL; |
| 2329 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2330 | Py_INCREF(resunicode); |
| 2331 | Py_DECREF(restuple); |
| 2332 | return resunicode; |
| 2333 | } |
| 2334 | |
| 2335 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
| 2336 | int size, |
| 2337 | const char *errors, |
| 2338 | int limit) |
| 2339 | { |
| 2340 | /* output object */ |
| 2341 | PyObject *res; |
| 2342 | /* pointers to the beginning and end+1 of input */ |
| 2343 | const Py_UNICODE *startp = p; |
| 2344 | const Py_UNICODE *endp = p + size; |
| 2345 | /* pointer to the beginning of the unencodable characters */ |
| 2346 | /* const Py_UNICODE *badp = NULL; */ |
| 2347 | /* pointer into the output */ |
| 2348 | char *str; |
| 2349 | /* current output position */ |
| 2350 | int respos = 0; |
| 2351 | int ressize; |
| 2352 | char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 2353 | char *reason = (limit == 256) ? "ordinal not in range(256)" : "ordinal not in range(128)"; |
| 2354 | PyObject *errorHandler = NULL; |
| 2355 | PyObject *exc = NULL; |
| 2356 | /* the following variable is used for caching string comparisons |
| 2357 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 2358 | int known_errorHandler = -1; |
| 2359 | |
| 2360 | /* allocate enough for a simple encoding without |
| 2361 | replacements, if we need more, we'll resize */ |
| 2362 | res = PyString_FromStringAndSize(NULL, size); |
| 2363 | if (res == NULL) |
| 2364 | goto onError; |
| 2365 | if (size == 0) |
| 2366 | return res; |
| 2367 | str = PyString_AS_STRING(res); |
| 2368 | ressize = size; |
| 2369 | |
| 2370 | while (p<endp) { |
| 2371 | Py_UNICODE c = *p; |
| 2372 | |
| 2373 | /* can we encode this? */ |
| 2374 | if (c<limit) { |
| 2375 | /* no overflow check, because we know that the space is enough */ |
| 2376 | *str++ = (char)c; |
| 2377 | ++p; |
| 2378 | } |
| 2379 | else { |
| 2380 | int unicodepos = p-startp; |
| 2381 | int requiredsize; |
| 2382 | PyObject *repunicode; |
| 2383 | int repsize; |
| 2384 | int newpos; |
| 2385 | int respos; |
| 2386 | Py_UNICODE *uni2; |
| 2387 | /* startpos for collecting unencodable chars */ |
| 2388 | const Py_UNICODE *collstart = p; |
| 2389 | const Py_UNICODE *collend = p; |
| 2390 | /* find all unecodable characters */ |
| 2391 | while ((collend < endp) && ((*collend)>=limit)) |
| 2392 | ++collend; |
| 2393 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 2394 | if (known_errorHandler==-1) { |
| 2395 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 2396 | known_errorHandler = 1; |
| 2397 | else if (!strcmp(errors, "replace")) |
| 2398 | known_errorHandler = 2; |
| 2399 | else if (!strcmp(errors, "ignore")) |
| 2400 | known_errorHandler = 3; |
| 2401 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 2402 | known_errorHandler = 4; |
| 2403 | else |
| 2404 | known_errorHandler = 0; |
| 2405 | } |
| 2406 | switch (known_errorHandler) { |
| 2407 | case 1: /* strict */ |
| 2408 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 2409 | goto onError; |
| 2410 | case 2: /* replace */ |
| 2411 | while (collstart++<collend) |
| 2412 | *str++ = '?'; /* fall through */ |
| 2413 | case 3: /* ignore */ |
| 2414 | p = collend; |
| 2415 | break; |
| 2416 | case 4: /* xmlcharrefreplace */ |
| 2417 | respos = str-PyString_AS_STRING(res); |
| 2418 | /* determine replacement size (temporarily (mis)uses p) */ |
| 2419 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 2420 | if (*p<10) |
| 2421 | repsize += 2+1+1; |
| 2422 | else if (*p<100) |
| 2423 | repsize += 2+2+1; |
| 2424 | else if (*p<1000) |
| 2425 | repsize += 2+3+1; |
| 2426 | else if (*p<10000) |
| 2427 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 2428 | #ifndef Py_UNICODE_WIDE |
| 2429 | else |
| 2430 | repsize += 2+5+1; |
| 2431 | #else |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2432 | else if (*p<100000) |
| 2433 | repsize += 2+5+1; |
| 2434 | else if (*p<1000000) |
| 2435 | repsize += 2+6+1; |
| 2436 | else |
| 2437 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2438 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2439 | } |
| 2440 | requiredsize = respos+repsize+(endp-collend); |
| 2441 | if (requiredsize > ressize) { |
| 2442 | if (requiredsize<2*ressize) |
| 2443 | requiredsize = 2*ressize; |
| 2444 | if (_PyString_Resize(&res, requiredsize)) |
| 2445 | goto onError; |
| 2446 | str = PyString_AS_STRING(res) + respos; |
| 2447 | ressize = requiredsize; |
| 2448 | } |
| 2449 | /* generate replacement (temporarily (mis)uses p) */ |
| 2450 | for (p = collstart; p < collend; ++p) { |
| 2451 | str += sprintf(str, "&#%d;", (int)*p); |
| 2452 | } |
| 2453 | p = collend; |
| 2454 | break; |
| 2455 | default: |
| 2456 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 2457 | encoding, reason, startp, size, &exc, |
| 2458 | collstart-startp, collend-startp, &newpos); |
| 2459 | if (repunicode == NULL) |
| 2460 | goto onError; |
| 2461 | /* need more space? (at least enough for what we |
| 2462 | have+the replacement+the rest of the string, so |
| 2463 | we won't have to check space for encodable characters) */ |
| 2464 | respos = str-PyString_AS_STRING(res); |
| 2465 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 2466 | requiredsize = respos+repsize+(endp-collend); |
| 2467 | if (requiredsize > ressize) { |
| 2468 | if (requiredsize<2*ressize) |
| 2469 | requiredsize = 2*ressize; |
| 2470 | if (_PyString_Resize(&res, requiredsize)) { |
| 2471 | Py_DECREF(repunicode); |
| 2472 | goto onError; |
| 2473 | } |
| 2474 | str = PyString_AS_STRING(res) + respos; |
| 2475 | ressize = requiredsize; |
| 2476 | } |
| 2477 | /* check if there is anything unencodable in the replacement |
| 2478 | and copy it to the output */ |
| 2479 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 2480 | c = *uni2; |
| 2481 | if (c >= limit) { |
| 2482 | raise_encode_exception(&exc, encoding, startp, size, |
| 2483 | unicodepos, unicodepos+1, reason); |
| 2484 | Py_DECREF(repunicode); |
| 2485 | goto onError; |
| 2486 | } |
| 2487 | *str = (char)c; |
| 2488 | } |
| 2489 | p = startp + newpos; |
| 2490 | Py_DECREF(repunicode); |
| 2491 | } |
| 2492 | } |
| 2493 | } |
| 2494 | /* Resize if we allocated to much */ |
| 2495 | respos = str-PyString_AS_STRING(res); |
| 2496 | if (respos<ressize) |
| 2497 | /* If this falls res will be NULL */ |
| 2498 | _PyString_Resize(&res, respos); |
| 2499 | Py_XDECREF(errorHandler); |
| 2500 | Py_XDECREF(exc); |
| 2501 | return res; |
| 2502 | |
| 2503 | onError: |
| 2504 | Py_XDECREF(res); |
| 2505 | Py_XDECREF(errorHandler); |
| 2506 | Py_XDECREF(exc); |
| 2507 | return NULL; |
| 2508 | } |
| 2509 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2510 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
| 2511 | int size, |
| 2512 | const char *errors) |
| 2513 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2514 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2515 | } |
| 2516 | |
| 2517 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 2518 | { |
| 2519 | if (!PyUnicode_Check(unicode)) { |
| 2520 | PyErr_BadArgument(); |
| 2521 | return NULL; |
| 2522 | } |
| 2523 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 2524 | PyUnicode_GET_SIZE(unicode), |
| 2525 | NULL); |
| 2526 | } |
| 2527 | |
| 2528 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 2529 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2530 | PyObject *PyUnicode_DecodeASCII(const char *s, |
| 2531 | int size, |
| 2532 | const char *errors) |
| 2533 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2534 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2535 | PyUnicodeObject *v; |
| 2536 | Py_UNICODE *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2537 | int startinpos; |
| 2538 | int endinpos; |
| 2539 | int outpos; |
| 2540 | const char *e; |
| 2541 | PyObject *errorHandler = NULL; |
| 2542 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2543 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2544 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 2545 | if (size == 1 && *(unsigned char*)s < 128) { |
| 2546 | Py_UNICODE r = *(unsigned char*)s; |
| 2547 | return PyUnicode_FromUnicode(&r, 1); |
| 2548 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2549 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2550 | v = _PyUnicode_New(size); |
| 2551 | if (v == NULL) |
| 2552 | goto onError; |
| 2553 | if (size == 0) |
| 2554 | return (PyObject *)v; |
| 2555 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2556 | e = s + size; |
| 2557 | while (s < e) { |
| 2558 | register unsigned char c = (unsigned char)*s; |
| 2559 | if (c < 128) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2560 | *p++ = c; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2561 | ++s; |
| 2562 | } |
| 2563 | else { |
| 2564 | startinpos = s-starts; |
| 2565 | endinpos = startinpos + 1; |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 2566 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2567 | if (unicode_decode_call_errorhandler( |
| 2568 | errors, &errorHandler, |
| 2569 | "ascii", "ordinal not in range(128)", |
| 2570 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2571 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2572 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2573 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2574 | } |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2575 | if (p - PyUnicode_AS_UNICODE(v) < PyString_GET_SIZE(v)) |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2576 | if (_PyUnicode_Resize(&v, (int)(p - PyUnicode_AS_UNICODE(v))) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2577 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2578 | Py_XDECREF(errorHandler); |
| 2579 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2580 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2581 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2582 | onError: |
| 2583 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2584 | Py_XDECREF(errorHandler); |
| 2585 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2586 | return NULL; |
| 2587 | } |
| 2588 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2589 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 2590 | int size, |
| 2591 | const char *errors) |
| 2592 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2593 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2594 | } |
| 2595 | |
| 2596 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 2597 | { |
| 2598 | if (!PyUnicode_Check(unicode)) { |
| 2599 | PyErr_BadArgument(); |
| 2600 | return NULL; |
| 2601 | } |
| 2602 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 2603 | PyUnicode_GET_SIZE(unicode), |
| 2604 | NULL); |
| 2605 | } |
| 2606 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2607 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2608 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2609 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2610 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2611 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
| 2612 | int size, |
| 2613 | const char *errors) |
| 2614 | { |
| 2615 | PyUnicodeObject *v; |
| 2616 | Py_UNICODE *p; |
| 2617 | |
| 2618 | /* First get the size of the result */ |
| 2619 | DWORD usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 2620 | if (size > 0 && usize==0) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2621 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2622 | |
| 2623 | v = _PyUnicode_New(usize); |
| 2624 | if (v == NULL) |
| 2625 | return NULL; |
| 2626 | if (usize == 0) |
| 2627 | return (PyObject *)v; |
| 2628 | p = PyUnicode_AS_UNICODE(v); |
| 2629 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 2630 | Py_DECREF(v); |
| 2631 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2632 | } |
| 2633 | |
| 2634 | return (PyObject *)v; |
| 2635 | } |
| 2636 | |
| 2637 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 2638 | int size, |
| 2639 | const char *errors) |
| 2640 | { |
| 2641 | PyObject *repr; |
| 2642 | char *s; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 2643 | DWORD mbcssize; |
| 2644 | |
| 2645 | /* If there are no characters, bail now! */ |
| 2646 | if (size==0) |
| 2647 | return PyString_FromString(""); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2648 | |
| 2649 | /* First get the size of the result */ |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 2650 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2651 | if (mbcssize==0) |
| 2652 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2653 | |
| 2654 | repr = PyString_FromStringAndSize(NULL, mbcssize); |
| 2655 | if (repr == NULL) |
| 2656 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2657 | if (mbcssize == 0) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2658 | return repr; |
| 2659 | |
| 2660 | /* Do the conversion */ |
| 2661 | s = PyString_AS_STRING(repr); |
| 2662 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 2663 | Py_DECREF(repr); |
| 2664 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2665 | } |
| 2666 | return repr; |
| 2667 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2668 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2669 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 2670 | { |
| 2671 | if (!PyUnicode_Check(unicode)) { |
| 2672 | PyErr_BadArgument(); |
| 2673 | return NULL; |
| 2674 | } |
| 2675 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2676 | PyUnicode_GET_SIZE(unicode), |
| 2677 | NULL); |
| 2678 | } |
| 2679 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2680 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2681 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2682 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 2683 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2684 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
| 2685 | int size, |
| 2686 | PyObject *mapping, |
| 2687 | const char *errors) |
| 2688 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2689 | const char *starts = s; |
| 2690 | int startinpos; |
| 2691 | int endinpos; |
| 2692 | int outpos; |
| 2693 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2694 | PyUnicodeObject *v; |
| 2695 | Py_UNICODE *p; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 2696 | int extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2697 | PyObject *errorHandler = NULL; |
| 2698 | PyObject *exc = NULL; |
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 | /* Default to Latin-1 */ |
| 2701 | if (mapping == NULL) |
| 2702 | return PyUnicode_DecodeLatin1(s, size, errors); |
| 2703 | |
| 2704 | v = _PyUnicode_New(size); |
| 2705 | if (v == NULL) |
| 2706 | goto onError; |
| 2707 | if (size == 0) |
| 2708 | return (PyObject *)v; |
| 2709 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2710 | e = s + size; |
| 2711 | while (s < e) { |
| 2712 | unsigned char ch = *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2713 | PyObject *w, *x; |
| 2714 | |
| 2715 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 2716 | w = PyInt_FromLong((long)ch); |
| 2717 | if (w == NULL) |
| 2718 | goto onError; |
| 2719 | x = PyObject_GetItem(mapping, w); |
| 2720 | Py_DECREF(w); |
| 2721 | if (x == NULL) { |
| 2722 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
Marc-André Lemburg | a866df8 | 2001-01-03 21:29:14 +0000 | [diff] [blame] | 2723 | /* No mapping found means: mapping is undefined. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2724 | PyErr_Clear(); |
Marc-André Lemburg | a866df8 | 2001-01-03 21:29:14 +0000 | [diff] [blame] | 2725 | x = Py_None; |
| 2726 | Py_INCREF(x); |
| 2727 | } else |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 2728 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2729 | } |
| 2730 | |
| 2731 | /* Apply mapping */ |
| 2732 | if (PyInt_Check(x)) { |
Marc-André Lemburg | 85cc4d8 | 2000-07-06 19:43:31 +0000 | [diff] [blame] | 2733 | long value = PyInt_AS_LONG(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2734 | if (value < 0 || value > 65535) { |
| 2735 | PyErr_SetString(PyExc_TypeError, |
Marc-André Lemburg | 07ceb67 | 2000-06-10 09:32:51 +0000 | [diff] [blame] | 2736 | "character mapping must be in range(65536)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2737 | Py_DECREF(x); |
| 2738 | goto onError; |
| 2739 | } |
| 2740 | *p++ = (Py_UNICODE)value; |
| 2741 | } |
| 2742 | else if (x == Py_None) { |
| 2743 | /* undefined mapping */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2744 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2745 | startinpos = s-starts; |
| 2746 | endinpos = startinpos+1; |
| 2747 | if (unicode_decode_call_errorhandler( |
| 2748 | errors, &errorHandler, |
| 2749 | "charmap", "character maps to <undefined>", |
| 2750 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2751 | (PyObject **)&v, &outpos, &p)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2752 | Py_DECREF(x); |
| 2753 | goto onError; |
| 2754 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2755 | continue; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2756 | } |
| 2757 | else if (PyUnicode_Check(x)) { |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 2758 | int targetsize = PyUnicode_GET_SIZE(x); |
| 2759 | |
| 2760 | if (targetsize == 1) |
| 2761 | /* 1-1 mapping */ |
| 2762 | *p++ = *PyUnicode_AS_UNICODE(x); |
| 2763 | |
| 2764 | else if (targetsize > 1) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2765 | /* 1-n mapping */ |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 2766 | if (targetsize > extrachars) { |
| 2767 | /* resize first */ |
| 2768 | int oldpos = (int)(p - PyUnicode_AS_UNICODE(v)); |
| 2769 | int needed = (targetsize - extrachars) + \ |
| 2770 | (targetsize << 2); |
| 2771 | extrachars += needed; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2772 | if (_PyUnicode_Resize(&v, |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2773 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 2774 | Py_DECREF(x); |
| 2775 | goto onError; |
| 2776 | } |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 2777 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 2778 | } |
| 2779 | Py_UNICODE_COPY(p, |
| 2780 | PyUnicode_AS_UNICODE(x), |
| 2781 | targetsize); |
| 2782 | p += targetsize; |
| 2783 | extrachars -= targetsize; |
| 2784 | } |
| 2785 | /* 1-0 mapping: skip the character */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2786 | } |
| 2787 | else { |
| 2788 | /* wrong return value */ |
| 2789 | PyErr_SetString(PyExc_TypeError, |
| 2790 | "character mapping must return integer, None or unicode"); |
| 2791 | Py_DECREF(x); |
| 2792 | goto onError; |
| 2793 | } |
| 2794 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2795 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2796 | } |
| 2797 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2798 | if (_PyUnicode_Resize(&v, (int)(p - PyUnicode_AS_UNICODE(v))) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2799 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2800 | Py_XDECREF(errorHandler); |
| 2801 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2802 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2803 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2804 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2805 | Py_XDECREF(errorHandler); |
| 2806 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2807 | Py_XDECREF(v); |
| 2808 | return NULL; |
| 2809 | } |
| 2810 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2811 | /* Lookup the character ch in the mapping. If the character |
| 2812 | can't be found, Py_None is returned (or NULL, if another |
| 2813 | error occured). */ |
| 2814 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2815 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2816 | PyObject *w = PyInt_FromLong((long)c); |
| 2817 | PyObject *x; |
| 2818 | |
| 2819 | if (w == NULL) |
| 2820 | return NULL; |
| 2821 | x = PyObject_GetItem(mapping, w); |
| 2822 | Py_DECREF(w); |
| 2823 | if (x == NULL) { |
| 2824 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 2825 | /* No mapping found means: mapping is undefined. */ |
| 2826 | PyErr_Clear(); |
| 2827 | x = Py_None; |
| 2828 | Py_INCREF(x); |
| 2829 | return x; |
| 2830 | } else |
| 2831 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2832 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 2833 | else if (x == Py_None) |
| 2834 | return x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2835 | else if (PyInt_Check(x)) { |
| 2836 | long value = PyInt_AS_LONG(x); |
| 2837 | if (value < 0 || value > 255) { |
| 2838 | PyErr_SetString(PyExc_TypeError, |
| 2839 | "character mapping must be in range(256)"); |
| 2840 | Py_DECREF(x); |
| 2841 | return NULL; |
| 2842 | } |
| 2843 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2844 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2845 | else if (PyString_Check(x)) |
| 2846 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2847 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2848 | /* wrong return value */ |
| 2849 | PyErr_SetString(PyExc_TypeError, |
| 2850 | "character mapping must return integer, None or str"); |
| 2851 | Py_DECREF(x); |
| 2852 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2853 | } |
| 2854 | } |
| 2855 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2856 | /* lookup the character, put the result in the output string and adjust |
| 2857 | various state variables. Reallocate the output string if not enough |
| 2858 | space is available. Return a new reference to the object that |
| 2859 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 2860 | (in which case no character was written) or NULL, if a |
| 2861 | reallocation error ocurred. The called must decref the result */ |
| 2862 | static |
| 2863 | PyObject *charmapencode_output(Py_UNICODE c, PyObject *mapping, |
| 2864 | PyObject **outobj, int *outpos) |
| 2865 | { |
| 2866 | PyObject *rep = charmapencode_lookup(c, mapping); |
| 2867 | |
| 2868 | if (rep==NULL) |
| 2869 | return NULL; |
| 2870 | else if (rep==Py_None) |
| 2871 | return rep; |
| 2872 | else { |
| 2873 | char *outstart = PyString_AS_STRING(*outobj); |
| 2874 | int outsize = PyString_GET_SIZE(*outobj); |
| 2875 | if (PyInt_Check(rep)) { |
| 2876 | int requiredsize = *outpos+1; |
| 2877 | if (outsize<requiredsize) { |
| 2878 | /* exponentially overallocate to minimize reallocations */ |
| 2879 | if (requiredsize < 2*outsize) |
| 2880 | requiredsize = 2*outsize; |
| 2881 | if (_PyString_Resize(outobj, requiredsize)) { |
| 2882 | Py_DECREF(rep); |
| 2883 | return NULL; |
| 2884 | } |
| 2885 | outstart = PyString_AS_STRING(*outobj); |
| 2886 | } |
| 2887 | outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); |
| 2888 | } |
| 2889 | else { |
| 2890 | const char *repchars = PyString_AS_STRING(rep); |
| 2891 | int repsize = PyString_GET_SIZE(rep); |
| 2892 | int requiredsize = *outpos+repsize; |
| 2893 | if (outsize<requiredsize) { |
| 2894 | /* exponentially overallocate to minimize reallocations */ |
| 2895 | if (requiredsize < 2*outsize) |
| 2896 | requiredsize = 2*outsize; |
| 2897 | if (_PyString_Resize(outobj, requiredsize)) { |
| 2898 | Py_DECREF(rep); |
| 2899 | return NULL; |
| 2900 | } |
| 2901 | outstart = PyString_AS_STRING(*outobj); |
| 2902 | } |
| 2903 | memcpy(outstart + *outpos, repchars, repsize); |
| 2904 | *outpos += repsize; |
| 2905 | } |
| 2906 | } |
| 2907 | return rep; |
| 2908 | } |
| 2909 | |
| 2910 | /* handle an error in PyUnicode_EncodeCharmap |
| 2911 | Return 0 on success, -1 on error */ |
| 2912 | static |
| 2913 | int charmap_encoding_error( |
| 2914 | const Py_UNICODE *p, int size, int *inpos, PyObject *mapping, |
| 2915 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 2916 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2917 | PyObject **res, int *respos) |
| 2918 | { |
| 2919 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 2920 | int repsize; |
| 2921 | int newpos; |
| 2922 | Py_UNICODE *uni2; |
| 2923 | /* startpos for collecting unencodable chars */ |
| 2924 | int collstartpos = *inpos; |
| 2925 | int collendpos = *inpos+1; |
| 2926 | int collpos; |
| 2927 | char *encoding = "charmap"; |
| 2928 | char *reason = "character maps to <undefined>"; |
| 2929 | |
| 2930 | PyObject *x; |
| 2931 | /* find all unencodable characters */ |
| 2932 | while (collendpos < size) { |
| 2933 | x = charmapencode_lookup(p[collendpos], mapping); |
| 2934 | if (x==NULL) |
| 2935 | return -1; |
| 2936 | else if (x!=Py_None) { |
| 2937 | Py_DECREF(x); |
| 2938 | break; |
| 2939 | } |
| 2940 | Py_DECREF(x); |
| 2941 | ++collendpos; |
| 2942 | } |
| 2943 | /* cache callback name lookup |
| 2944 | * (if not done yet, i.e. it's the first error) */ |
| 2945 | if (*known_errorHandler==-1) { |
| 2946 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 2947 | *known_errorHandler = 1; |
| 2948 | else if (!strcmp(errors, "replace")) |
| 2949 | *known_errorHandler = 2; |
| 2950 | else if (!strcmp(errors, "ignore")) |
| 2951 | *known_errorHandler = 3; |
| 2952 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 2953 | *known_errorHandler = 4; |
| 2954 | else |
| 2955 | *known_errorHandler = 0; |
| 2956 | } |
| 2957 | switch (*known_errorHandler) { |
| 2958 | case 1: /* strict */ |
| 2959 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 2960 | return -1; |
| 2961 | case 2: /* replace */ |
| 2962 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 2963 | x = charmapencode_output('?', mapping, res, respos); |
| 2964 | if (x==NULL) { |
| 2965 | return -1; |
| 2966 | } |
| 2967 | else if (x==Py_None) { |
| 2968 | Py_DECREF(x); |
| 2969 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 2970 | return -1; |
| 2971 | } |
| 2972 | Py_DECREF(x); |
| 2973 | } |
| 2974 | /* fall through */ |
| 2975 | case 3: /* ignore */ |
| 2976 | *inpos = collendpos; |
| 2977 | break; |
| 2978 | case 4: /* xmlcharrefreplace */ |
| 2979 | /* generate replacement (temporarily (mis)uses p) */ |
| 2980 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 2981 | char buffer[2+29+1+1]; |
| 2982 | char *cp; |
| 2983 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 2984 | for (cp = buffer; *cp; ++cp) { |
| 2985 | x = charmapencode_output(*cp, mapping, res, respos); |
| 2986 | if (x==NULL) |
| 2987 | return -1; |
| 2988 | else if (x==Py_None) { |
| 2989 | Py_DECREF(x); |
| 2990 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 2991 | return -1; |
| 2992 | } |
| 2993 | Py_DECREF(x); |
| 2994 | } |
| 2995 | } |
| 2996 | *inpos = collendpos; |
| 2997 | break; |
| 2998 | default: |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 2999 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3000 | encoding, reason, p, size, exceptionObject, |
| 3001 | collstartpos, collendpos, &newpos); |
| 3002 | if (repunicode == NULL) |
| 3003 | return -1; |
| 3004 | /* generate replacement */ |
| 3005 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3006 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 3007 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 3008 | if (x==NULL) { |
| 3009 | Py_DECREF(repunicode); |
| 3010 | return -1; |
| 3011 | } |
| 3012 | else if (x==Py_None) { |
| 3013 | Py_DECREF(repunicode); |
| 3014 | Py_DECREF(x); |
| 3015 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3016 | return -1; |
| 3017 | } |
| 3018 | Py_DECREF(x); |
| 3019 | } |
| 3020 | *inpos = newpos; |
| 3021 | Py_DECREF(repunicode); |
| 3022 | } |
| 3023 | return 0; |
| 3024 | } |
| 3025 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3026 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 3027 | int size, |
| 3028 | PyObject *mapping, |
| 3029 | const char *errors) |
| 3030 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3031 | /* output object */ |
| 3032 | PyObject *res = NULL; |
| 3033 | /* current input position */ |
| 3034 | int inpos = 0; |
| 3035 | /* current output position */ |
| 3036 | int respos = 0; |
| 3037 | PyObject *errorHandler = NULL; |
| 3038 | PyObject *exc = NULL; |
| 3039 | /* the following variable is used for caching string comparisons |
| 3040 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 3041 | * 3=ignore, 4=xmlcharrefreplace */ |
| 3042 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3043 | |
| 3044 | /* Default to Latin-1 */ |
| 3045 | if (mapping == NULL) |
| 3046 | return PyUnicode_EncodeLatin1(p, size, errors); |
| 3047 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3048 | /* allocate enough for a simple encoding without |
| 3049 | replacements, if we need more, we'll resize */ |
| 3050 | res = PyString_FromStringAndSize(NULL, size); |
| 3051 | if (res == NULL) |
| 3052 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3053 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3054 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3055 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3056 | while (inpos<size) { |
| 3057 | /* try to encode it */ |
| 3058 | PyObject *x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 3059 | if (x==NULL) /* error */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3060 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3061 | if (x==Py_None) { /* unencodable character */ |
| 3062 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 3063 | &exc, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3064 | &known_errorHandler, &errorHandler, errors, |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 3065 | &res, &respos)) { |
| 3066 | Py_DECREF(x); |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3067 | goto onError; |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 3068 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3069 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3070 | else |
| 3071 | /* done with this character => adjust input position */ |
| 3072 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3073 | Py_DECREF(x); |
| 3074 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3075 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3076 | /* Resize if we allocated to much */ |
| 3077 | if (respos<PyString_GET_SIZE(res)) { |
| 3078 | if (_PyString_Resize(&res, respos)) |
| 3079 | goto onError; |
| 3080 | } |
| 3081 | Py_XDECREF(exc); |
| 3082 | Py_XDECREF(errorHandler); |
| 3083 | return res; |
| 3084 | |
| 3085 | onError: |
| 3086 | Py_XDECREF(res); |
| 3087 | Py_XDECREF(exc); |
| 3088 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3089 | return NULL; |
| 3090 | } |
| 3091 | |
| 3092 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 3093 | PyObject *mapping) |
| 3094 | { |
| 3095 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 3096 | PyErr_BadArgument(); |
| 3097 | return NULL; |
| 3098 | } |
| 3099 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 3100 | PyUnicode_GET_SIZE(unicode), |
| 3101 | mapping, |
| 3102 | NULL); |
| 3103 | } |
| 3104 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3105 | /* create or adjust a UnicodeTranslateError */ |
| 3106 | static void make_translate_exception(PyObject **exceptionObject, |
| 3107 | const Py_UNICODE *unicode, int size, |
| 3108 | int startpos, int endpos, |
| 3109 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3110 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3111 | if (*exceptionObject == NULL) { |
| 3112 | *exceptionObject = PyUnicodeTranslateError_Create( |
| 3113 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3114 | } |
| 3115 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3116 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 3117 | goto onError; |
| 3118 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 3119 | goto onError; |
| 3120 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 3121 | goto onError; |
| 3122 | return; |
| 3123 | onError: |
| 3124 | Py_DECREF(*exceptionObject); |
| 3125 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3126 | } |
| 3127 | } |
| 3128 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3129 | /* raises a UnicodeTranslateError */ |
| 3130 | static void raise_translate_exception(PyObject **exceptionObject, |
| 3131 | const Py_UNICODE *unicode, int size, |
| 3132 | int startpos, int endpos, |
| 3133 | const char *reason) |
| 3134 | { |
| 3135 | make_translate_exception(exceptionObject, |
| 3136 | unicode, size, startpos, endpos, reason); |
| 3137 | if (*exceptionObject != NULL) |
| 3138 | PyCodec_StrictErrors(*exceptionObject); |
| 3139 | } |
| 3140 | |
| 3141 | /* error handling callback helper: |
| 3142 | build arguments, call the callback and check the arguments, |
| 3143 | put the result into newpos and return the replacement string, which |
| 3144 | has to be freed by the caller */ |
| 3145 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 3146 | PyObject **errorHandler, |
| 3147 | const char *reason, |
| 3148 | const Py_UNICODE *unicode, int size, PyObject **exceptionObject, |
| 3149 | int startpos, int endpos, |
| 3150 | int *newpos) |
| 3151 | { |
| 3152 | static char *argparse = "O!i;translating error handler must return (unicode, int) tuple"; |
| 3153 | |
| 3154 | PyObject *restuple; |
| 3155 | PyObject *resunicode; |
| 3156 | |
| 3157 | if (*errorHandler == NULL) { |
| 3158 | *errorHandler = PyCodec_LookupError(errors); |
| 3159 | if (*errorHandler == NULL) |
| 3160 | return NULL; |
| 3161 | } |
| 3162 | |
| 3163 | make_translate_exception(exceptionObject, |
| 3164 | unicode, size, startpos, endpos, reason); |
| 3165 | if (*exceptionObject == NULL) |
| 3166 | return NULL; |
| 3167 | |
| 3168 | restuple = PyObject_CallFunctionObjArgs( |
| 3169 | *errorHandler, *exceptionObject, NULL); |
| 3170 | if (restuple == NULL) |
| 3171 | return NULL; |
| 3172 | if (!PyTuple_Check(restuple)) { |
| 3173 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 3174 | Py_DECREF(restuple); |
| 3175 | return NULL; |
| 3176 | } |
| 3177 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 3178 | &resunicode, newpos)) { |
| 3179 | Py_DECREF(restuple); |
| 3180 | return NULL; |
| 3181 | } |
| 3182 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3183 | *newpos = size+*newpos; |
| 3184 | if (*newpos<0 || *newpos>size) { |
| 3185 | PyErr_Format(PyExc_IndexError, "position %d from error handler out of bounds", *newpos); |
| 3186 | Py_DECREF(restuple); |
| 3187 | return NULL; |
| 3188 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3189 | Py_INCREF(resunicode); |
| 3190 | Py_DECREF(restuple); |
| 3191 | return resunicode; |
| 3192 | } |
| 3193 | |
| 3194 | /* Lookup the character ch in the mapping and put the result in result, |
| 3195 | which must be decrefed by the caller. |
| 3196 | Return 0 on success, -1 on error */ |
| 3197 | static |
| 3198 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 3199 | { |
| 3200 | PyObject *w = PyInt_FromLong((long)c); |
| 3201 | PyObject *x; |
| 3202 | |
| 3203 | if (w == NULL) |
| 3204 | return -1; |
| 3205 | x = PyObject_GetItem(mapping, w); |
| 3206 | Py_DECREF(w); |
| 3207 | if (x == NULL) { |
| 3208 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3209 | /* No mapping found means: use 1:1 mapping. */ |
| 3210 | PyErr_Clear(); |
| 3211 | *result = NULL; |
| 3212 | return 0; |
| 3213 | } else |
| 3214 | return -1; |
| 3215 | } |
| 3216 | else if (x == Py_None) { |
| 3217 | *result = x; |
| 3218 | return 0; |
| 3219 | } |
| 3220 | else if (PyInt_Check(x)) { |
| 3221 | long value = PyInt_AS_LONG(x); |
| 3222 | long max = PyUnicode_GetMax(); |
| 3223 | if (value < 0 || value > max) { |
| 3224 | PyErr_Format(PyExc_TypeError, |
| 3225 | "character mapping must be in range(0x%lx)", max+1); |
| 3226 | Py_DECREF(x); |
| 3227 | return -1; |
| 3228 | } |
| 3229 | *result = x; |
| 3230 | return 0; |
| 3231 | } |
| 3232 | else if (PyUnicode_Check(x)) { |
| 3233 | *result = x; |
| 3234 | return 0; |
| 3235 | } |
| 3236 | else { |
| 3237 | /* wrong return value */ |
| 3238 | PyErr_SetString(PyExc_TypeError, |
| 3239 | "character mapping must return integer, None or unicode"); |
Walter Dörwald | 150523e | 2003-08-15 16:52:19 +0000 | [diff] [blame] | 3240 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3241 | return -1; |
| 3242 | } |
| 3243 | } |
| 3244 | /* ensure that *outobj is at least requiredsize characters long, |
| 3245 | if not reallocate and adjust various state variables. |
| 3246 | Return 0 on success, -1 on error */ |
| 3247 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3248 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3249 | int requiredsize) |
| 3250 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3251 | int oldsize = PyUnicode_GET_SIZE(*outobj); |
| 3252 | if (requiredsize > oldsize) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3253 | /* remember old output position */ |
| 3254 | int outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
| 3255 | /* exponentially overallocate to minimize reallocations */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3256 | if (requiredsize < 2 * oldsize) |
| 3257 | requiredsize = 2 * oldsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3258 | if (_PyUnicode_Resize(outobj, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3259 | return -1; |
| 3260 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3261 | } |
| 3262 | return 0; |
| 3263 | } |
| 3264 | /* lookup the character, put the result in the output string and adjust |
| 3265 | various state variables. Return a new reference to the object that |
| 3266 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 3267 | undefined (in which case no character was written). |
| 3268 | The called must decref result. |
| 3269 | Return 0 on success, -1 on error. */ |
| 3270 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3271 | int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
| 3272 | int insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
| 3273 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3274 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3275 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3276 | return -1; |
| 3277 | if (*res==NULL) { |
| 3278 | /* not found => default to 1:1 mapping */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3279 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3280 | } |
| 3281 | else if (*res==Py_None) |
| 3282 | ; |
| 3283 | else if (PyInt_Check(*res)) { |
| 3284 | /* no overflow check, because we know that the space is enough */ |
| 3285 | *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); |
| 3286 | } |
| 3287 | else if (PyUnicode_Check(*res)) { |
| 3288 | int repsize = PyUnicode_GET_SIZE(*res); |
| 3289 | if (repsize==1) { |
| 3290 | /* no overflow check, because we know that the space is enough */ |
| 3291 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 3292 | } |
| 3293 | else if (repsize!=0) { |
| 3294 | /* more than one character */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3295 | int requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
Walter Dörwald | cd736e7 | 2004-02-05 17:36:00 +0000 | [diff] [blame] | 3296 | (insize - (curinp-startinp)) + |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3297 | repsize - 1; |
| 3298 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3299 | return -1; |
| 3300 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 3301 | *outp += repsize; |
| 3302 | } |
| 3303 | } |
| 3304 | else |
| 3305 | return -1; |
| 3306 | return 0; |
| 3307 | } |
| 3308 | |
| 3309 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3310 | int size, |
| 3311 | PyObject *mapping, |
| 3312 | const char *errors) |
| 3313 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3314 | /* output object */ |
| 3315 | PyObject *res = NULL; |
| 3316 | /* pointers to the beginning and end+1 of input */ |
| 3317 | const Py_UNICODE *startp = p; |
| 3318 | const Py_UNICODE *endp = p + size; |
| 3319 | /* pointer into the output */ |
| 3320 | Py_UNICODE *str; |
| 3321 | /* current output position */ |
| 3322 | int respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3323 | char *reason = "character maps to <undefined>"; |
| 3324 | PyObject *errorHandler = NULL; |
| 3325 | PyObject *exc = NULL; |
| 3326 | /* the following variable is used for caching string comparisons |
| 3327 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 3328 | * 3=ignore, 4=xmlcharrefreplace */ |
| 3329 | int known_errorHandler = -1; |
| 3330 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3331 | if (mapping == NULL) { |
| 3332 | PyErr_BadArgument(); |
| 3333 | return NULL; |
| 3334 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3335 | |
| 3336 | /* allocate enough for a simple 1:1 translation without |
| 3337 | replacements, if we need more, we'll resize */ |
| 3338 | res = PyUnicode_FromUnicode(NULL, size); |
| 3339 | if (res == NULL) |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3340 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3341 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3342 | return res; |
| 3343 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3344 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3345 | while (p<endp) { |
| 3346 | /* try to encode it */ |
| 3347 | PyObject *x = NULL; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3348 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3349 | Py_XDECREF(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3350 | goto onError; |
| 3351 | } |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 3352 | Py_XDECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3353 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 3354 | ++p; |
| 3355 | else { /* untranslatable character */ |
| 3356 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 3357 | int repsize; |
| 3358 | int newpos; |
| 3359 | Py_UNICODE *uni2; |
| 3360 | /* startpos for collecting untranslatable chars */ |
| 3361 | const Py_UNICODE *collstart = p; |
| 3362 | const Py_UNICODE *collend = p+1; |
| 3363 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3364 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3365 | /* find all untranslatable characters */ |
| 3366 | while (collend < endp) { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3367 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3368 | goto onError; |
| 3369 | Py_XDECREF(x); |
| 3370 | if (x!=Py_None) |
| 3371 | break; |
| 3372 | ++collend; |
| 3373 | } |
| 3374 | /* cache callback name lookup |
| 3375 | * (if not done yet, i.e. it's the first error) */ |
| 3376 | if (known_errorHandler==-1) { |
| 3377 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3378 | known_errorHandler = 1; |
| 3379 | else if (!strcmp(errors, "replace")) |
| 3380 | known_errorHandler = 2; |
| 3381 | else if (!strcmp(errors, "ignore")) |
| 3382 | known_errorHandler = 3; |
| 3383 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3384 | known_errorHandler = 4; |
| 3385 | else |
| 3386 | known_errorHandler = 0; |
| 3387 | } |
| 3388 | switch (known_errorHandler) { |
| 3389 | case 1: /* strict */ |
| 3390 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
| 3391 | goto onError; |
| 3392 | case 2: /* replace */ |
| 3393 | /* No need to check for space, this is a 1:1 replacement */ |
| 3394 | for (coll = collstart; coll<collend; ++coll) |
| 3395 | *str++ = '?'; |
| 3396 | /* fall through */ |
| 3397 | case 3: /* ignore */ |
| 3398 | p = collend; |
| 3399 | break; |
| 3400 | case 4: /* xmlcharrefreplace */ |
| 3401 | /* generate replacement (temporarily (mis)uses p) */ |
| 3402 | for (p = collstart; p < collend; ++p) { |
| 3403 | char buffer[2+29+1+1]; |
| 3404 | char *cp; |
| 3405 | sprintf(buffer, "&#%d;", (int)*p); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3406 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3407 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 3408 | goto onError; |
| 3409 | for (cp = buffer; *cp; ++cp) |
| 3410 | *str++ = *cp; |
| 3411 | } |
| 3412 | p = collend; |
| 3413 | break; |
| 3414 | default: |
| 3415 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 3416 | reason, startp, size, &exc, |
| 3417 | collstart-startp, collend-startp, &newpos); |
| 3418 | if (repunicode == NULL) |
| 3419 | goto onError; |
| 3420 | /* generate replacement */ |
| 3421 | repsize = PyUnicode_GET_SIZE(repunicode); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3422 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3423 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 3424 | Py_DECREF(repunicode); |
| 3425 | goto onError; |
| 3426 | } |
| 3427 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 3428 | *str++ = *uni2; |
| 3429 | p = startp + newpos; |
| 3430 | Py_DECREF(repunicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3431 | } |
| 3432 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3433 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3434 | /* Resize if we allocated to much */ |
| 3435 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3436 | if (respos<PyUnicode_GET_SIZE(res)) { |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3437 | if (_PyUnicode_Resize(&res, respos) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3438 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3439 | } |
| 3440 | Py_XDECREF(exc); |
| 3441 | Py_XDECREF(errorHandler); |
| 3442 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3443 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3444 | onError: |
| 3445 | Py_XDECREF(res); |
| 3446 | Py_XDECREF(exc); |
| 3447 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3448 | return NULL; |
| 3449 | } |
| 3450 | |
| 3451 | PyObject *PyUnicode_Translate(PyObject *str, |
| 3452 | PyObject *mapping, |
| 3453 | const char *errors) |
| 3454 | { |
| 3455 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3456 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3457 | str = PyUnicode_FromObject(str); |
| 3458 | if (str == NULL) |
| 3459 | goto onError; |
| 3460 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 3461 | PyUnicode_GET_SIZE(str), |
| 3462 | mapping, |
| 3463 | errors); |
| 3464 | Py_DECREF(str); |
| 3465 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3466 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3467 | onError: |
| 3468 | Py_XDECREF(str); |
| 3469 | return NULL; |
| 3470 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3471 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3472 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 3473 | |
| 3474 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 3475 | int length, |
| 3476 | char *output, |
| 3477 | const char *errors) |
| 3478 | { |
| 3479 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3480 | PyObject *errorHandler = NULL; |
| 3481 | PyObject *exc = NULL; |
| 3482 | const char *encoding = "decimal"; |
| 3483 | const char *reason = "invalid decimal Unicode string"; |
| 3484 | /* the following variable is used for caching string comparisons |
| 3485 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 3486 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3487 | |
| 3488 | if (output == NULL) { |
| 3489 | PyErr_BadArgument(); |
| 3490 | return -1; |
| 3491 | } |
| 3492 | |
| 3493 | p = s; |
| 3494 | end = s + length; |
| 3495 | while (p < end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3496 | register Py_UNICODE ch = *p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3497 | int decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3498 | PyObject *repunicode; |
| 3499 | int repsize; |
| 3500 | int newpos; |
| 3501 | Py_UNICODE *uni2; |
| 3502 | Py_UNICODE *collstart; |
| 3503 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3504 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3505 | if (Py_UNICODE_ISSPACE(ch)) { |
| 3506 | *output++ = ' '; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3507 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3508 | continue; |
| 3509 | } |
| 3510 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 3511 | if (decimal >= 0) { |
| 3512 | *output++ = '0' + decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3513 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3514 | continue; |
| 3515 | } |
Guido van Rossum | ba47704 | 2000-04-06 18:18:10 +0000 | [diff] [blame] | 3516 | if (0 < ch && ch < 256) { |
Guido van Rossum | 42c29aa | 2000-05-03 23:58:29 +0000 | [diff] [blame] | 3517 | *output++ = (char)ch; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3518 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3519 | continue; |
| 3520 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3521 | /* All other characters are considered unencodable */ |
| 3522 | collstart = p; |
| 3523 | collend = p+1; |
| 3524 | while (collend < end) { |
| 3525 | if ((0 < *collend && *collend < 256) || |
| 3526 | !Py_UNICODE_ISSPACE(*collend) || |
| 3527 | Py_UNICODE_TODECIMAL(*collend)) |
| 3528 | break; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3529 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3530 | /* cache callback name lookup |
| 3531 | * (if not done yet, i.e. it's the first error) */ |
| 3532 | if (known_errorHandler==-1) { |
| 3533 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3534 | known_errorHandler = 1; |
| 3535 | else if (!strcmp(errors, "replace")) |
| 3536 | known_errorHandler = 2; |
| 3537 | else if (!strcmp(errors, "ignore")) |
| 3538 | known_errorHandler = 3; |
| 3539 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3540 | known_errorHandler = 4; |
| 3541 | else |
| 3542 | known_errorHandler = 0; |
| 3543 | } |
| 3544 | switch (known_errorHandler) { |
| 3545 | case 1: /* strict */ |
| 3546 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 3547 | goto onError; |
| 3548 | case 2: /* replace */ |
| 3549 | for (p = collstart; p < collend; ++p) |
| 3550 | *output++ = '?'; |
| 3551 | /* fall through */ |
| 3552 | case 3: /* ignore */ |
| 3553 | p = collend; |
| 3554 | break; |
| 3555 | case 4: /* xmlcharrefreplace */ |
| 3556 | /* generate replacement (temporarily (mis)uses p) */ |
| 3557 | for (p = collstart; p < collend; ++p) |
| 3558 | output += sprintf(output, "&#%d;", (int)*p); |
| 3559 | p = collend; |
| 3560 | break; |
| 3561 | default: |
| 3562 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 3563 | encoding, reason, s, length, &exc, |
| 3564 | collstart-s, collend-s, &newpos); |
| 3565 | if (repunicode == NULL) |
| 3566 | goto onError; |
| 3567 | /* generate replacement */ |
| 3568 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3569 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 3570 | Py_UNICODE ch = *uni2; |
| 3571 | if (Py_UNICODE_ISSPACE(ch)) |
| 3572 | *output++ = ' '; |
| 3573 | else { |
| 3574 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 3575 | if (decimal >= 0) |
| 3576 | *output++ = '0' + decimal; |
| 3577 | else if (0 < ch && ch < 256) |
| 3578 | *output++ = (char)ch; |
| 3579 | else { |
| 3580 | Py_DECREF(repunicode); |
| 3581 | raise_encode_exception(&exc, encoding, |
| 3582 | s, length, collstart-s, collend-s, reason); |
| 3583 | goto onError; |
| 3584 | } |
| 3585 | } |
| 3586 | } |
| 3587 | p = s + newpos; |
| 3588 | Py_DECREF(repunicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3589 | } |
| 3590 | } |
| 3591 | /* 0-terminate the output string */ |
| 3592 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3593 | Py_XDECREF(exc); |
| 3594 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3595 | return 0; |
| 3596 | |
| 3597 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3598 | Py_XDECREF(exc); |
| 3599 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3600 | return -1; |
| 3601 | } |
| 3602 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3603 | /* --- Helpers ------------------------------------------------------------ */ |
| 3604 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3605 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3606 | int count(PyUnicodeObject *self, |
| 3607 | int start, |
| 3608 | int end, |
| 3609 | PyUnicodeObject *substring) |
| 3610 | { |
| 3611 | int count = 0; |
| 3612 | |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3613 | if (start < 0) |
| 3614 | start += self->length; |
| 3615 | if (start < 0) |
| 3616 | start = 0; |
| 3617 | if (end > self->length) |
| 3618 | end = self->length; |
| 3619 | if (end < 0) |
| 3620 | end += self->length; |
| 3621 | if (end < 0) |
| 3622 | end = 0; |
| 3623 | |
Marc-André Lemburg | 49ef6dc | 2000-06-18 22:25:22 +0000 | [diff] [blame] | 3624 | if (substring->length == 0) |
| 3625 | return (end - start + 1); |
| 3626 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3627 | end -= substring->length; |
| 3628 | |
| 3629 | while (start <= end) |
| 3630 | if (Py_UNICODE_MATCH(self, start, substring)) { |
| 3631 | count++; |
| 3632 | start += substring->length; |
| 3633 | } else |
| 3634 | start++; |
| 3635 | |
| 3636 | return count; |
| 3637 | } |
| 3638 | |
| 3639 | int PyUnicode_Count(PyObject *str, |
| 3640 | PyObject *substr, |
| 3641 | int start, |
| 3642 | int end) |
| 3643 | { |
| 3644 | int result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3645 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3646 | str = PyUnicode_FromObject(str); |
| 3647 | if (str == NULL) |
| 3648 | return -1; |
| 3649 | substr = PyUnicode_FromObject(substr); |
| 3650 | if (substr == NULL) { |
Marc-André Lemburg | 49ef6dc | 2000-06-18 22:25:22 +0000 | [diff] [blame] | 3651 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3652 | return -1; |
| 3653 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3654 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3655 | result = count((PyUnicodeObject *)str, |
| 3656 | start, end, |
| 3657 | (PyUnicodeObject *)substr); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3658 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3659 | Py_DECREF(str); |
| 3660 | Py_DECREF(substr); |
| 3661 | return result; |
| 3662 | } |
| 3663 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3664 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3665 | int findstring(PyUnicodeObject *self, |
| 3666 | PyUnicodeObject *substring, |
| 3667 | int start, |
| 3668 | int end, |
| 3669 | int direction) |
| 3670 | { |
| 3671 | if (start < 0) |
| 3672 | start += self->length; |
| 3673 | if (start < 0) |
| 3674 | start = 0; |
| 3675 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3676 | if (end > self->length) |
| 3677 | end = self->length; |
| 3678 | if (end < 0) |
| 3679 | end += self->length; |
| 3680 | if (end < 0) |
| 3681 | end = 0; |
| 3682 | |
Guido van Rossum | 76afbd9 | 2002-08-20 17:29:29 +0000 | [diff] [blame] | 3683 | if (substring->length == 0) |
| 3684 | return (direction > 0) ? start : end; |
| 3685 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3686 | end -= substring->length; |
| 3687 | |
| 3688 | if (direction < 0) { |
| 3689 | for (; end >= start; end--) |
| 3690 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 3691 | return end; |
| 3692 | } else { |
| 3693 | for (; start <= end; start++) |
| 3694 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 3695 | return start; |
| 3696 | } |
| 3697 | |
| 3698 | return -1; |
| 3699 | } |
| 3700 | |
| 3701 | int PyUnicode_Find(PyObject *str, |
| 3702 | PyObject *substr, |
| 3703 | int start, |
| 3704 | int end, |
| 3705 | int direction) |
| 3706 | { |
| 3707 | int result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3708 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3709 | str = PyUnicode_FromObject(str); |
| 3710 | if (str == NULL) |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 3711 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3712 | substr = PyUnicode_FromObject(substr); |
| 3713 | if (substr == NULL) { |
Marc-André Lemburg | 4164439 | 2002-05-29 13:46:29 +0000 | [diff] [blame] | 3714 | Py_DECREF(str); |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 3715 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3716 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3717 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3718 | result = findstring((PyUnicodeObject *)str, |
| 3719 | (PyUnicodeObject *)substr, |
| 3720 | start, end, direction); |
| 3721 | Py_DECREF(str); |
| 3722 | Py_DECREF(substr); |
| 3723 | return result; |
| 3724 | } |
| 3725 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3726 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3727 | int tailmatch(PyUnicodeObject *self, |
| 3728 | PyUnicodeObject *substring, |
| 3729 | int start, |
| 3730 | int end, |
| 3731 | int direction) |
| 3732 | { |
| 3733 | if (start < 0) |
| 3734 | start += self->length; |
| 3735 | if (start < 0) |
| 3736 | start = 0; |
| 3737 | |
| 3738 | if (substring->length == 0) |
| 3739 | return 1; |
| 3740 | |
| 3741 | if (end > self->length) |
| 3742 | end = self->length; |
| 3743 | if (end < 0) |
| 3744 | end += self->length; |
| 3745 | if (end < 0) |
| 3746 | end = 0; |
| 3747 | |
| 3748 | end -= substring->length; |
| 3749 | if (end < start) |
| 3750 | return 0; |
| 3751 | |
| 3752 | if (direction > 0) { |
| 3753 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 3754 | return 1; |
| 3755 | } else { |
| 3756 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 3757 | return 1; |
| 3758 | } |
| 3759 | |
| 3760 | return 0; |
| 3761 | } |
| 3762 | |
| 3763 | int PyUnicode_Tailmatch(PyObject *str, |
| 3764 | PyObject *substr, |
| 3765 | int start, |
| 3766 | int end, |
| 3767 | int direction) |
| 3768 | { |
| 3769 | int result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3770 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3771 | str = PyUnicode_FromObject(str); |
| 3772 | if (str == NULL) |
| 3773 | return -1; |
| 3774 | substr = PyUnicode_FromObject(substr); |
| 3775 | if (substr == NULL) { |
| 3776 | Py_DECREF(substr); |
| 3777 | return -1; |
| 3778 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3779 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3780 | result = tailmatch((PyUnicodeObject *)str, |
| 3781 | (PyUnicodeObject *)substr, |
| 3782 | start, end, direction); |
| 3783 | Py_DECREF(str); |
| 3784 | Py_DECREF(substr); |
| 3785 | return result; |
| 3786 | } |
| 3787 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3788 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3789 | const Py_UNICODE *findchar(const Py_UNICODE *s, |
| 3790 | int size, |
| 3791 | Py_UNICODE ch) |
| 3792 | { |
| 3793 | /* like wcschr, but doesn't stop at NULL characters */ |
| 3794 | |
| 3795 | while (size-- > 0) { |
| 3796 | if (*s == ch) |
| 3797 | return s; |
| 3798 | s++; |
| 3799 | } |
| 3800 | |
| 3801 | return NULL; |
| 3802 | } |
| 3803 | |
| 3804 | /* Apply fixfct filter to the Unicode object self and return a |
| 3805 | reference to the modified object */ |
| 3806 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3807 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3808 | PyObject *fixup(PyUnicodeObject *self, |
| 3809 | int (*fixfct)(PyUnicodeObject *s)) |
| 3810 | { |
| 3811 | |
| 3812 | PyUnicodeObject *u; |
| 3813 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3814 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3815 | if (u == NULL) |
| 3816 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3817 | |
| 3818 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 3819 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 3820 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3821 | /* fixfct should return TRUE if it modified the buffer. If |
| 3822 | FALSE, return a reference to the original buffer instead |
| 3823 | (to save space, not time) */ |
| 3824 | Py_INCREF(self); |
| 3825 | Py_DECREF(u); |
| 3826 | return (PyObject*) self; |
| 3827 | } |
| 3828 | return (PyObject*) u; |
| 3829 | } |
| 3830 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3831 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3832 | int fixupper(PyUnicodeObject *self) |
| 3833 | { |
| 3834 | int len = self->length; |
| 3835 | Py_UNICODE *s = self->str; |
| 3836 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3837 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3838 | while (len-- > 0) { |
| 3839 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3840 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3841 | ch = Py_UNICODE_TOUPPER(*s); |
| 3842 | if (ch != *s) { |
| 3843 | status = 1; |
| 3844 | *s = ch; |
| 3845 | } |
| 3846 | s++; |
| 3847 | } |
| 3848 | |
| 3849 | return status; |
| 3850 | } |
| 3851 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3852 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3853 | int fixlower(PyUnicodeObject *self) |
| 3854 | { |
| 3855 | int len = self->length; |
| 3856 | Py_UNICODE *s = self->str; |
| 3857 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3858 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3859 | while (len-- > 0) { |
| 3860 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3861 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3862 | ch = Py_UNICODE_TOLOWER(*s); |
| 3863 | if (ch != *s) { |
| 3864 | status = 1; |
| 3865 | *s = ch; |
| 3866 | } |
| 3867 | s++; |
| 3868 | } |
| 3869 | |
| 3870 | return status; |
| 3871 | } |
| 3872 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3873 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3874 | int fixswapcase(PyUnicodeObject *self) |
| 3875 | { |
| 3876 | int len = self->length; |
| 3877 | Py_UNICODE *s = self->str; |
| 3878 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3879 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3880 | while (len-- > 0) { |
| 3881 | if (Py_UNICODE_ISUPPER(*s)) { |
| 3882 | *s = Py_UNICODE_TOLOWER(*s); |
| 3883 | status = 1; |
| 3884 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 3885 | *s = Py_UNICODE_TOUPPER(*s); |
| 3886 | status = 1; |
| 3887 | } |
| 3888 | s++; |
| 3889 | } |
| 3890 | |
| 3891 | return status; |
| 3892 | } |
| 3893 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3894 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3895 | int fixcapitalize(PyUnicodeObject *self) |
| 3896 | { |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 3897 | int len = self->length; |
| 3898 | Py_UNICODE *s = self->str; |
| 3899 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3900 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 3901 | if (len == 0) |
| 3902 | return 0; |
| 3903 | if (Py_UNICODE_ISLOWER(*s)) { |
| 3904 | *s = Py_UNICODE_TOUPPER(*s); |
| 3905 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3906 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 3907 | s++; |
| 3908 | while (--len > 0) { |
| 3909 | if (Py_UNICODE_ISUPPER(*s)) { |
| 3910 | *s = Py_UNICODE_TOLOWER(*s); |
| 3911 | status = 1; |
| 3912 | } |
| 3913 | s++; |
| 3914 | } |
| 3915 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3916 | } |
| 3917 | |
| 3918 | static |
| 3919 | int fixtitle(PyUnicodeObject *self) |
| 3920 | { |
| 3921 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 3922 | register Py_UNICODE *e; |
| 3923 | int previous_is_cased; |
| 3924 | |
| 3925 | /* Shortcut for single character strings */ |
| 3926 | if (PyUnicode_GET_SIZE(self) == 1) { |
| 3927 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 3928 | if (*p != ch) { |
| 3929 | *p = ch; |
| 3930 | return 1; |
| 3931 | } |
| 3932 | else |
| 3933 | return 0; |
| 3934 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3935 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3936 | e = p + PyUnicode_GET_SIZE(self); |
| 3937 | previous_is_cased = 0; |
| 3938 | for (; p < e; p++) { |
| 3939 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3940 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3941 | if (previous_is_cased) |
| 3942 | *p = Py_UNICODE_TOLOWER(ch); |
| 3943 | else |
| 3944 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3945 | |
| 3946 | if (Py_UNICODE_ISLOWER(ch) || |
| 3947 | Py_UNICODE_ISUPPER(ch) || |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3948 | Py_UNICODE_ISTITLE(ch)) |
| 3949 | previous_is_cased = 1; |
| 3950 | else |
| 3951 | previous_is_cased = 0; |
| 3952 | } |
| 3953 | return 1; |
| 3954 | } |
| 3955 | |
| 3956 | PyObject *PyUnicode_Join(PyObject *separator, |
| 3957 | PyObject *seq) |
| 3958 | { |
| 3959 | Py_UNICODE *sep; |
| 3960 | int seplen; |
| 3961 | PyUnicodeObject *res = NULL; |
| 3962 | int reslen = 0; |
| 3963 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3964 | int sz = 100; |
| 3965 | int i; |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 3966 | PyObject *it; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3967 | |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 3968 | it = PyObject_GetIter(seq); |
| 3969 | if (it == NULL) |
| 3970 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3971 | |
| 3972 | if (separator == NULL) { |
| 3973 | Py_UNICODE blank = ' '; |
| 3974 | sep = ␣ |
| 3975 | seplen = 1; |
| 3976 | } |
| 3977 | else { |
| 3978 | separator = PyUnicode_FromObject(separator); |
| 3979 | if (separator == NULL) |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 3980 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3981 | sep = PyUnicode_AS_UNICODE(separator); |
| 3982 | seplen = PyUnicode_GET_SIZE(separator); |
| 3983 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3984 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3985 | res = _PyUnicode_New(sz); |
| 3986 | if (res == NULL) |
| 3987 | goto onError; |
| 3988 | p = PyUnicode_AS_UNICODE(res); |
| 3989 | reslen = 0; |
| 3990 | |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 3991 | for (i = 0; ; ++i) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3992 | int itemlen; |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 3993 | PyObject *item = PyIter_Next(it); |
| 3994 | if (item == NULL) { |
| 3995 | if (PyErr_Occurred()) |
| 3996 | goto onError; |
| 3997 | break; |
| 3998 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3999 | if (!PyUnicode_Check(item)) { |
| 4000 | PyObject *v; |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4001 | if (!PyString_Check(item)) { |
| 4002 | PyErr_Format(PyExc_TypeError, |
| 4003 | "sequence item %i: expected string or Unicode," |
| 4004 | " %.80s found", |
| 4005 | i, item->ob_type->tp_name); |
| 4006 | Py_DECREF(item); |
| 4007 | goto onError; |
| 4008 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4009 | v = PyUnicode_FromObject(item); |
| 4010 | Py_DECREF(item); |
| 4011 | item = v; |
| 4012 | if (item == NULL) |
| 4013 | goto onError; |
| 4014 | } |
| 4015 | itemlen = PyUnicode_GET_SIZE(item); |
| 4016 | while (reslen + itemlen + seplen >= sz) { |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4017 | if (_PyUnicode_Resize(&res, sz*2) < 0) { |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4018 | Py_DECREF(item); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4019 | goto onError; |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4020 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4021 | sz *= 2; |
| 4022 | p = PyUnicode_AS_UNICODE(res) + reslen; |
| 4023 | } |
| 4024 | if (i > 0) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4025 | Py_UNICODE_COPY(p, sep, seplen); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4026 | p += seplen; |
| 4027 | reslen += seplen; |
| 4028 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4029 | Py_UNICODE_COPY(p, PyUnicode_AS_UNICODE(item), itemlen); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4030 | p += itemlen; |
| 4031 | reslen += itemlen; |
| 4032 | Py_DECREF(item); |
| 4033 | } |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4034 | if (_PyUnicode_Resize(&res, reslen) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4035 | goto onError; |
| 4036 | |
| 4037 | Py_XDECREF(separator); |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 4038 | Py_DECREF(it); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4039 | return (PyObject *)res; |
| 4040 | |
| 4041 | onError: |
| 4042 | Py_XDECREF(separator); |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 4043 | Py_XDECREF(res); |
| 4044 | Py_DECREF(it); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4045 | return NULL; |
| 4046 | } |
| 4047 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4048 | static |
| 4049 | PyUnicodeObject *pad(PyUnicodeObject *self, |
| 4050 | int left, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4051 | int right, |
| 4052 | Py_UNICODE fill) |
| 4053 | { |
| 4054 | PyUnicodeObject *u; |
| 4055 | |
| 4056 | if (left < 0) |
| 4057 | left = 0; |
| 4058 | if (right < 0) |
| 4059 | right = 0; |
| 4060 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4061 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4062 | Py_INCREF(self); |
| 4063 | return self; |
| 4064 | } |
| 4065 | |
| 4066 | u = _PyUnicode_New(left + self->length + right); |
| 4067 | if (u) { |
| 4068 | if (left) |
| 4069 | Py_UNICODE_FILL(u->str, fill, left); |
| 4070 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 4071 | if (right) |
| 4072 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 4073 | } |
| 4074 | |
| 4075 | return u; |
| 4076 | } |
| 4077 | |
| 4078 | #define SPLIT_APPEND(data, left, right) \ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4079 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4080 | if (!str) \ |
| 4081 | goto onError; \ |
| 4082 | if (PyList_Append(list, str)) { \ |
| 4083 | Py_DECREF(str); \ |
| 4084 | goto onError; \ |
| 4085 | } \ |
| 4086 | else \ |
| 4087 | Py_DECREF(str); |
| 4088 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4089 | #define SPLIT_INSERT(data, left, right) \ |
| 4090 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
| 4091 | if (!str) \ |
| 4092 | goto onError; \ |
| 4093 | if (PyList_Insert(list, 0, str)) { \ |
| 4094 | Py_DECREF(str); \ |
| 4095 | goto onError; \ |
| 4096 | } \ |
| 4097 | else \ |
| 4098 | Py_DECREF(str); |
| 4099 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4100 | static |
| 4101 | PyObject *split_whitespace(PyUnicodeObject *self, |
| 4102 | PyObject *list, |
| 4103 | int maxcount) |
| 4104 | { |
| 4105 | register int i; |
| 4106 | register int j; |
| 4107 | int len = self->length; |
| 4108 | PyObject *str; |
| 4109 | |
| 4110 | for (i = j = 0; i < len; ) { |
| 4111 | /* find a token */ |
| 4112 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 4113 | i++; |
| 4114 | j = i; |
| 4115 | while (i < len && !Py_UNICODE_ISSPACE(self->str[i])) |
| 4116 | i++; |
| 4117 | if (j < i) { |
| 4118 | if (maxcount-- <= 0) |
| 4119 | break; |
| 4120 | SPLIT_APPEND(self->str, j, i); |
| 4121 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 4122 | i++; |
| 4123 | j = i; |
| 4124 | } |
| 4125 | } |
| 4126 | if (j < len) { |
| 4127 | SPLIT_APPEND(self->str, j, len); |
| 4128 | } |
| 4129 | return list; |
| 4130 | |
| 4131 | onError: |
| 4132 | Py_DECREF(list); |
| 4133 | return NULL; |
| 4134 | } |
| 4135 | |
| 4136 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4137 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4138 | { |
| 4139 | register int i; |
| 4140 | register int j; |
| 4141 | int len; |
| 4142 | PyObject *list; |
| 4143 | PyObject *str; |
| 4144 | Py_UNICODE *data; |
| 4145 | |
| 4146 | string = PyUnicode_FromObject(string); |
| 4147 | if (string == NULL) |
| 4148 | return NULL; |
| 4149 | data = PyUnicode_AS_UNICODE(string); |
| 4150 | len = PyUnicode_GET_SIZE(string); |
| 4151 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4152 | list = PyList_New(0); |
| 4153 | if (!list) |
| 4154 | goto onError; |
| 4155 | |
| 4156 | for (i = j = 0; i < len; ) { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4157 | int eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4158 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4159 | /* Find a line and append it */ |
| 4160 | while (i < len && !Py_UNICODE_ISLINEBREAK(data[i])) |
| 4161 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4162 | |
| 4163 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4164 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4165 | if (i < len) { |
| 4166 | if (data[i] == '\r' && i + 1 < len && |
| 4167 | data[i+1] == '\n') |
| 4168 | i += 2; |
| 4169 | else |
| 4170 | i++; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4171 | if (keepends) |
| 4172 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4173 | } |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4174 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4175 | j = i; |
| 4176 | } |
| 4177 | if (j < len) { |
| 4178 | SPLIT_APPEND(data, j, len); |
| 4179 | } |
| 4180 | |
| 4181 | Py_DECREF(string); |
| 4182 | return list; |
| 4183 | |
| 4184 | onError: |
| 4185 | Py_DECREF(list); |
| 4186 | Py_DECREF(string); |
| 4187 | return NULL; |
| 4188 | } |
| 4189 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4190 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4191 | PyObject *split_char(PyUnicodeObject *self, |
| 4192 | PyObject *list, |
| 4193 | Py_UNICODE ch, |
| 4194 | int maxcount) |
| 4195 | { |
| 4196 | register int i; |
| 4197 | register int j; |
| 4198 | int len = self->length; |
| 4199 | PyObject *str; |
| 4200 | |
| 4201 | for (i = j = 0; i < len; ) { |
| 4202 | if (self->str[i] == ch) { |
| 4203 | if (maxcount-- <= 0) |
| 4204 | break; |
| 4205 | SPLIT_APPEND(self->str, j, i); |
| 4206 | i = j = i + 1; |
| 4207 | } else |
| 4208 | i++; |
| 4209 | } |
| 4210 | if (j <= len) { |
| 4211 | SPLIT_APPEND(self->str, j, len); |
| 4212 | } |
| 4213 | return list; |
| 4214 | |
| 4215 | onError: |
| 4216 | Py_DECREF(list); |
| 4217 | return NULL; |
| 4218 | } |
| 4219 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4220 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4221 | PyObject *split_substring(PyUnicodeObject *self, |
| 4222 | PyObject *list, |
| 4223 | PyUnicodeObject *substring, |
| 4224 | int maxcount) |
| 4225 | { |
| 4226 | register int i; |
| 4227 | register int j; |
| 4228 | int len = self->length; |
| 4229 | int sublen = substring->length; |
| 4230 | PyObject *str; |
| 4231 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 4232 | for (i = j = 0; i <= len - sublen; ) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4233 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 4234 | if (maxcount-- <= 0) |
| 4235 | break; |
| 4236 | SPLIT_APPEND(self->str, j, i); |
| 4237 | i = j = i + sublen; |
| 4238 | } else |
| 4239 | i++; |
| 4240 | } |
| 4241 | if (j <= len) { |
| 4242 | SPLIT_APPEND(self->str, j, len); |
| 4243 | } |
| 4244 | return list; |
| 4245 | |
| 4246 | onError: |
| 4247 | Py_DECREF(list); |
| 4248 | return NULL; |
| 4249 | } |
| 4250 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4251 | static |
| 4252 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
| 4253 | PyObject *list, |
| 4254 | int maxcount) |
| 4255 | { |
| 4256 | register int i; |
| 4257 | register int j; |
| 4258 | int len = self->length; |
| 4259 | PyObject *str; |
| 4260 | |
| 4261 | for (i = j = len - 1; i >= 0; ) { |
| 4262 | /* find a token */ |
| 4263 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 4264 | i--; |
| 4265 | j = i; |
| 4266 | while (i >= 0 && !Py_UNICODE_ISSPACE(self->str[i])) |
| 4267 | i--; |
| 4268 | if (j > i) { |
| 4269 | if (maxcount-- <= 0) |
| 4270 | break; |
| 4271 | SPLIT_INSERT(self->str, i + 1, j + 1); |
| 4272 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 4273 | i--; |
| 4274 | j = i; |
| 4275 | } |
| 4276 | } |
| 4277 | if (j >= 0) { |
| 4278 | SPLIT_INSERT(self->str, 0, j + 1); |
| 4279 | } |
| 4280 | return list; |
| 4281 | |
| 4282 | onError: |
| 4283 | Py_DECREF(list); |
| 4284 | return NULL; |
| 4285 | } |
| 4286 | |
| 4287 | static |
| 4288 | PyObject *rsplit_char(PyUnicodeObject *self, |
| 4289 | PyObject *list, |
| 4290 | Py_UNICODE ch, |
| 4291 | int maxcount) |
| 4292 | { |
| 4293 | register int i; |
| 4294 | register int j; |
| 4295 | int len = self->length; |
| 4296 | PyObject *str; |
| 4297 | |
| 4298 | for (i = j = len - 1; i >= 0; ) { |
| 4299 | if (self->str[i] == ch) { |
| 4300 | if (maxcount-- <= 0) |
| 4301 | break; |
| 4302 | SPLIT_INSERT(self->str, i + 1, j + 1); |
| 4303 | j = i = i - 1; |
| 4304 | } else |
| 4305 | i--; |
| 4306 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 4307 | if (j >= -1) { |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4308 | SPLIT_INSERT(self->str, 0, j + 1); |
| 4309 | } |
| 4310 | return list; |
| 4311 | |
| 4312 | onError: |
| 4313 | Py_DECREF(list); |
| 4314 | return NULL; |
| 4315 | } |
| 4316 | |
| 4317 | static |
| 4318 | PyObject *rsplit_substring(PyUnicodeObject *self, |
| 4319 | PyObject *list, |
| 4320 | PyUnicodeObject *substring, |
| 4321 | int maxcount) |
| 4322 | { |
| 4323 | register int i; |
| 4324 | register int j; |
| 4325 | int len = self->length; |
| 4326 | int sublen = substring->length; |
| 4327 | PyObject *str; |
| 4328 | |
| 4329 | for (i = len - sublen, j = len; i >= 0; ) { |
| 4330 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 4331 | if (maxcount-- <= 0) |
| 4332 | break; |
| 4333 | SPLIT_INSERT(self->str, i + sublen, j); |
| 4334 | j = i; |
| 4335 | i -= sublen; |
| 4336 | } else |
| 4337 | i--; |
| 4338 | } |
| 4339 | if (j >= 0) { |
| 4340 | SPLIT_INSERT(self->str, 0, j); |
| 4341 | } |
| 4342 | return list; |
| 4343 | |
| 4344 | onError: |
| 4345 | Py_DECREF(list); |
| 4346 | return NULL; |
| 4347 | } |
| 4348 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4349 | #undef SPLIT_APPEND |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4350 | #undef SPLIT_INSERT |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4351 | |
| 4352 | static |
| 4353 | PyObject *split(PyUnicodeObject *self, |
| 4354 | PyUnicodeObject *substring, |
| 4355 | int maxcount) |
| 4356 | { |
| 4357 | PyObject *list; |
| 4358 | |
| 4359 | if (maxcount < 0) |
| 4360 | maxcount = INT_MAX; |
| 4361 | |
| 4362 | list = PyList_New(0); |
| 4363 | if (!list) |
| 4364 | return NULL; |
| 4365 | |
| 4366 | if (substring == NULL) |
| 4367 | return split_whitespace(self,list,maxcount); |
| 4368 | |
| 4369 | else if (substring->length == 1) |
| 4370 | return split_char(self,list,substring->str[0],maxcount); |
| 4371 | |
| 4372 | else if (substring->length == 0) { |
| 4373 | Py_DECREF(list); |
| 4374 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 4375 | return NULL; |
| 4376 | } |
| 4377 | else |
| 4378 | return split_substring(self,list,substring,maxcount); |
| 4379 | } |
| 4380 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4381 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4382 | PyObject *rsplit(PyUnicodeObject *self, |
| 4383 | PyUnicodeObject *substring, |
| 4384 | int maxcount) |
| 4385 | { |
| 4386 | PyObject *list; |
| 4387 | |
| 4388 | if (maxcount < 0) |
| 4389 | maxcount = INT_MAX; |
| 4390 | |
| 4391 | list = PyList_New(0); |
| 4392 | if (!list) |
| 4393 | return NULL; |
| 4394 | |
| 4395 | if (substring == NULL) |
| 4396 | return rsplit_whitespace(self,list,maxcount); |
| 4397 | |
| 4398 | else if (substring->length == 1) |
| 4399 | return rsplit_char(self,list,substring->str[0],maxcount); |
| 4400 | |
| 4401 | else if (substring->length == 0) { |
| 4402 | Py_DECREF(list); |
| 4403 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 4404 | return NULL; |
| 4405 | } |
| 4406 | else |
| 4407 | return rsplit_substring(self,list,substring,maxcount); |
| 4408 | } |
| 4409 | |
| 4410 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4411 | PyObject *replace(PyUnicodeObject *self, |
| 4412 | PyUnicodeObject *str1, |
| 4413 | PyUnicodeObject *str2, |
| 4414 | int maxcount) |
| 4415 | { |
| 4416 | PyUnicodeObject *u; |
| 4417 | |
| 4418 | if (maxcount < 0) |
| 4419 | maxcount = INT_MAX; |
| 4420 | |
| 4421 | if (str1->length == 1 && str2->length == 1) { |
| 4422 | int i; |
| 4423 | |
| 4424 | /* replace characters */ |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4425 | if (!findchar(self->str, self->length, str1->str[0]) && |
| 4426 | PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4427 | /* nothing to replace, return original string */ |
| 4428 | Py_INCREF(self); |
| 4429 | u = self; |
| 4430 | } else { |
| 4431 | Py_UNICODE u1 = str1->str[0]; |
| 4432 | Py_UNICODE u2 = str2->str[0]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4433 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4434 | u = (PyUnicodeObject*) PyUnicode_FromUnicode( |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4435 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4436 | self->length |
| 4437 | ); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4438 | if (u != NULL) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4439 | Py_UNICODE_COPY(u->str, self->str, |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4440 | self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4441 | for (i = 0; i < u->length; i++) |
| 4442 | if (u->str[i] == u1) { |
| 4443 | if (--maxcount < 0) |
| 4444 | break; |
| 4445 | u->str[i] = u2; |
| 4446 | } |
| 4447 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4448 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4449 | |
| 4450 | } else { |
| 4451 | int n, i; |
| 4452 | Py_UNICODE *p; |
| 4453 | |
| 4454 | /* replace strings */ |
| 4455 | n = count(self, 0, self->length, str1); |
| 4456 | if (n > maxcount) |
| 4457 | n = maxcount; |
Guido van Rossum | 2023c9b | 2002-08-23 18:50:21 +0000 | [diff] [blame] | 4458 | if (n == 0) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4459 | /* nothing to replace, return original string */ |
Guido van Rossum | 2023c9b | 2002-08-23 18:50:21 +0000 | [diff] [blame] | 4460 | if (PyUnicode_CheckExact(self)) { |
| 4461 | Py_INCREF(self); |
| 4462 | u = self; |
| 4463 | } |
| 4464 | else { |
| 4465 | u = (PyUnicodeObject *) |
| 4466 | PyUnicode_FromUnicode(self->str, self->length); |
| 4467 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4468 | } else { |
| 4469 | u = _PyUnicode_New( |
| 4470 | self->length + n * (str2->length - str1->length)); |
| 4471 | if (u) { |
| 4472 | i = 0; |
| 4473 | p = u->str; |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 4474 | if (str1->length > 0) { |
| 4475 | while (i <= self->length - str1->length) |
| 4476 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 4477 | /* replace string segment */ |
| 4478 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 4479 | p += str2->length; |
| 4480 | i += str1->length; |
| 4481 | if (--n <= 0) { |
| 4482 | /* copy remaining part */ |
| 4483 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 4484 | break; |
| 4485 | } |
| 4486 | } else |
| 4487 | *p++ = self->str[i++]; |
| 4488 | } else { |
| 4489 | while (n > 0) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4490 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 4491 | p += str2->length; |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 4492 | if (--n <= 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4493 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4494 | *p++ = self->str[i++]; |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 4495 | } |
| 4496 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 4497 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4498 | } |
| 4499 | } |
| 4500 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4501 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4502 | return (PyObject *) u; |
| 4503 | } |
| 4504 | |
| 4505 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 4506 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4507 | PyDoc_STRVAR(title__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4508 | "S.title() -> unicode\n\ |
| 4509 | \n\ |
| 4510 | 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] | 4511 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4512 | |
| 4513 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 4514 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4515 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4516 | return fixup(self, fixtitle); |
| 4517 | } |
| 4518 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4519 | PyDoc_STRVAR(capitalize__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4520 | "S.capitalize() -> unicode\n\ |
| 4521 | \n\ |
| 4522 | 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] | 4523 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4524 | |
| 4525 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 4526 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4527 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4528 | return fixup(self, fixcapitalize); |
| 4529 | } |
| 4530 | |
| 4531 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4532 | PyDoc_STRVAR(capwords__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4533 | "S.capwords() -> unicode\n\ |
| 4534 | \n\ |
| 4535 | 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] | 4536 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4537 | |
| 4538 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 4539 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4540 | { |
| 4541 | PyObject *list; |
| 4542 | PyObject *item; |
| 4543 | int i; |
| 4544 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4545 | /* Split into words */ |
| 4546 | list = split(self, NULL, -1); |
| 4547 | if (!list) |
| 4548 | return NULL; |
| 4549 | |
| 4550 | /* Capitalize each word */ |
| 4551 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 4552 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 4553 | fixcapitalize); |
| 4554 | if (item == NULL) |
| 4555 | goto onError; |
| 4556 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 4557 | PyList_SET_ITEM(list, i, item); |
| 4558 | } |
| 4559 | |
| 4560 | /* Join the words to form a new string */ |
| 4561 | item = PyUnicode_Join(NULL, list); |
| 4562 | |
| 4563 | onError: |
| 4564 | Py_DECREF(list); |
| 4565 | return (PyObject *)item; |
| 4566 | } |
| 4567 | #endif |
| 4568 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4569 | /* Argument converter. Coerces to a single unicode character */ |
| 4570 | |
| 4571 | static int |
| 4572 | convert_uc(PyObject *obj, void *addr) |
| 4573 | { |
| 4574 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 4575 | PyObject *uniobj; |
| 4576 | Py_UNICODE *unistr; |
| 4577 | |
| 4578 | uniobj = PyUnicode_FromObject(obj); |
| 4579 | if (uniobj == NULL) { |
| 4580 | PyErr_SetString(PyExc_TypeError, |
| 4581 | "The fill character cannot be converted to Unicode"); |
| 4582 | return 0; |
| 4583 | } |
| 4584 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 4585 | PyErr_SetString(PyExc_TypeError, |
| 4586 | "The fill character must be exactly one character long"); |
| 4587 | Py_DECREF(uniobj); |
| 4588 | return 0; |
| 4589 | } |
| 4590 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 4591 | *fillcharloc = unistr[0]; |
| 4592 | Py_DECREF(uniobj); |
| 4593 | return 1; |
| 4594 | } |
| 4595 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4596 | PyDoc_STRVAR(center__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4597 | "S.center(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4598 | \n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4599 | Return S centered in a Unicode string of length width. Padding is\n\ |
| 4600 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4601 | |
| 4602 | static PyObject * |
| 4603 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 4604 | { |
| 4605 | int marg, left; |
| 4606 | int width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4607 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4608 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4609 | if (!PyArg_ParseTuple(args, "i|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4610 | return NULL; |
| 4611 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4612 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4613 | Py_INCREF(self); |
| 4614 | return (PyObject*) self; |
| 4615 | } |
| 4616 | |
| 4617 | marg = width - self->length; |
| 4618 | left = marg / 2 + (marg & width & 1); |
| 4619 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4620 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4621 | } |
| 4622 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4623 | #if 0 |
| 4624 | |
| 4625 | /* This code should go into some future Unicode collation support |
| 4626 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 4627 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4628 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4629 | /* speedy UTF-16 code point order comparison */ |
| 4630 | /* gleaned from: */ |
| 4631 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 4632 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4633 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4634 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4635 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4636 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 4637 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4638 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4639 | }; |
| 4640 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4641 | static int |
| 4642 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 4643 | { |
| 4644 | int len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4645 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4646 | Py_UNICODE *s1 = str1->str; |
| 4647 | Py_UNICODE *s2 = str2->str; |
| 4648 | |
| 4649 | len1 = str1->length; |
| 4650 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4651 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4652 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4653 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4654 | |
| 4655 | c1 = *s1++; |
| 4656 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 4657 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4658 | if (c1 > (1<<11) * 26) |
| 4659 | c1 += utf16Fixup[c1>>11]; |
| 4660 | if (c2 > (1<<11) * 26) |
| 4661 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4662 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 4663 | |
| 4664 | if (c1 != c2) |
| 4665 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4666 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4667 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4668 | } |
| 4669 | |
| 4670 | return (len1 < len2) ? -1 : (len1 != len2); |
| 4671 | } |
| 4672 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4673 | #else |
| 4674 | |
| 4675 | static int |
| 4676 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 4677 | { |
| 4678 | register int len1, len2; |
| 4679 | |
| 4680 | Py_UNICODE *s1 = str1->str; |
| 4681 | Py_UNICODE *s2 = str2->str; |
| 4682 | |
| 4683 | len1 = str1->length; |
| 4684 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4685 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4686 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4687 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4688 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 4689 | c1 = *s1++; |
| 4690 | c2 = *s2++; |
| 4691 | |
| 4692 | if (c1 != c2) |
| 4693 | return (c1 < c2) ? -1 : 1; |
| 4694 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4695 | len1--; len2--; |
| 4696 | } |
| 4697 | |
| 4698 | return (len1 < len2) ? -1 : (len1 != len2); |
| 4699 | } |
| 4700 | |
| 4701 | #endif |
| 4702 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4703 | int PyUnicode_Compare(PyObject *left, |
| 4704 | PyObject *right) |
| 4705 | { |
| 4706 | PyUnicodeObject *u = NULL, *v = NULL; |
| 4707 | int result; |
| 4708 | |
| 4709 | /* Coerce the two arguments */ |
| 4710 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 4711 | if (u == NULL) |
| 4712 | goto onError; |
| 4713 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 4714 | if (v == NULL) |
| 4715 | goto onError; |
| 4716 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 4717 | /* Shortcut for empty or interned objects */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4718 | if (v == u) { |
| 4719 | Py_DECREF(u); |
| 4720 | Py_DECREF(v); |
| 4721 | return 0; |
| 4722 | } |
| 4723 | |
| 4724 | result = unicode_compare(u, v); |
| 4725 | |
| 4726 | Py_DECREF(u); |
| 4727 | Py_DECREF(v); |
| 4728 | return result; |
| 4729 | |
| 4730 | onError: |
| 4731 | Py_XDECREF(u); |
| 4732 | Py_XDECREF(v); |
| 4733 | return -1; |
| 4734 | } |
| 4735 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4736 | int PyUnicode_Contains(PyObject *container, |
| 4737 | PyObject *element) |
| 4738 | { |
| 4739 | PyUnicodeObject *u = NULL, *v = NULL; |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 4740 | int result, size; |
| 4741 | register const Py_UNICODE *lhs, *end, *rhs; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4742 | |
| 4743 | /* Coerce the two arguments */ |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4744 | v = (PyUnicodeObject *)PyUnicode_FromObject(element); |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 4745 | if (v == NULL) { |
| 4746 | PyErr_SetString(PyExc_TypeError, |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 4747 | "'in <string>' requires string as left operand"); |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4748 | goto onError; |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 4749 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4750 | u = (PyUnicodeObject *)PyUnicode_FromObject(container); |
Marc-André Lemburg | 9cd87aa | 2002-10-23 09:02:46 +0000 | [diff] [blame] | 4751 | if (u == NULL) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4752 | goto onError; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4753 | |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 4754 | size = PyUnicode_GET_SIZE(v); |
| 4755 | rhs = PyUnicode_AS_UNICODE(v); |
| 4756 | lhs = PyUnicode_AS_UNICODE(u); |
| 4757 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4758 | result = 0; |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 4759 | if (size == 1) { |
| 4760 | end = lhs + PyUnicode_GET_SIZE(u); |
| 4761 | while (lhs < end) { |
| 4762 | if (*lhs++ == *rhs) { |
| 4763 | result = 1; |
| 4764 | break; |
| 4765 | } |
| 4766 | } |
| 4767 | } |
| 4768 | else { |
| 4769 | end = lhs + (PyUnicode_GET_SIZE(u) - size); |
| 4770 | while (lhs <= end) { |
Barry Warsaw | 6a043f3 | 2002-08-06 19:03:17 +0000 | [diff] [blame] | 4771 | if (memcmp(lhs++, rhs, size * sizeof(Py_UNICODE)) == 0) { |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 4772 | result = 1; |
| 4773 | break; |
| 4774 | } |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4775 | } |
| 4776 | } |
| 4777 | |
| 4778 | Py_DECREF(u); |
| 4779 | Py_DECREF(v); |
| 4780 | return result; |
| 4781 | |
| 4782 | onError: |
| 4783 | Py_XDECREF(u); |
| 4784 | Py_XDECREF(v); |
| 4785 | return -1; |
| 4786 | } |
| 4787 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4788 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 4789 | |
| 4790 | PyObject *PyUnicode_Concat(PyObject *left, |
| 4791 | PyObject *right) |
| 4792 | { |
| 4793 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 4794 | |
| 4795 | /* Coerce the two arguments */ |
| 4796 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 4797 | if (u == NULL) |
| 4798 | goto onError; |
| 4799 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 4800 | if (v == NULL) |
| 4801 | goto onError; |
| 4802 | |
| 4803 | /* Shortcuts */ |
| 4804 | if (v == unicode_empty) { |
| 4805 | Py_DECREF(v); |
| 4806 | return (PyObject *)u; |
| 4807 | } |
| 4808 | if (u == unicode_empty) { |
| 4809 | Py_DECREF(u); |
| 4810 | return (PyObject *)v; |
| 4811 | } |
| 4812 | |
| 4813 | /* Concat the two Unicode strings */ |
| 4814 | w = _PyUnicode_New(u->length + v->length); |
| 4815 | if (w == NULL) |
| 4816 | goto onError; |
| 4817 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 4818 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 4819 | |
| 4820 | Py_DECREF(u); |
| 4821 | Py_DECREF(v); |
| 4822 | return (PyObject *)w; |
| 4823 | |
| 4824 | onError: |
| 4825 | Py_XDECREF(u); |
| 4826 | Py_XDECREF(v); |
| 4827 | return NULL; |
| 4828 | } |
| 4829 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4830 | PyDoc_STRVAR(count__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4831 | "S.count(sub[, start[, end]]) -> int\n\ |
| 4832 | \n\ |
| 4833 | Return the number of occurrences of substring sub in Unicode string\n\ |
| 4834 | S[start:end]. Optional arguments start and end are\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4835 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4836 | |
| 4837 | static PyObject * |
| 4838 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 4839 | { |
| 4840 | PyUnicodeObject *substring; |
| 4841 | int start = 0; |
| 4842 | int end = INT_MAX; |
| 4843 | PyObject *result; |
| 4844 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 4845 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 4846 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4847 | return NULL; |
| 4848 | |
| 4849 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 4850 | (PyObject *)substring); |
| 4851 | if (substring == NULL) |
| 4852 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4853 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4854 | if (start < 0) |
| 4855 | start += self->length; |
| 4856 | if (start < 0) |
| 4857 | start = 0; |
| 4858 | if (end > self->length) |
| 4859 | end = self->length; |
| 4860 | if (end < 0) |
| 4861 | end += self->length; |
| 4862 | if (end < 0) |
| 4863 | end = 0; |
| 4864 | |
| 4865 | result = PyInt_FromLong((long) count(self, start, end, substring)); |
| 4866 | |
| 4867 | Py_DECREF(substring); |
| 4868 | return result; |
| 4869 | } |
| 4870 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4871 | PyDoc_STRVAR(encode__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4872 | "S.encode([encoding[,errors]]) -> string\n\ |
| 4873 | \n\ |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 4874 | Return an encoded string version of S. Default encoding is the current\n\ |
| 4875 | default string encoding. errors may be given to set a different error\n\ |
| 4876 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4877 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 4878 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 4879 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4880 | |
| 4881 | static PyObject * |
| 4882 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 4883 | { |
| 4884 | char *encoding = NULL; |
| 4885 | char *errors = NULL; |
| 4886 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 4887 | return NULL; |
| 4888 | return PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
| 4889 | } |
| 4890 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4891 | PyDoc_STRVAR(expandtabs__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4892 | "S.expandtabs([tabsize]) -> unicode\n\ |
| 4893 | \n\ |
| 4894 | 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] | 4895 | 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] | 4896 | |
| 4897 | static PyObject* |
| 4898 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 4899 | { |
| 4900 | Py_UNICODE *e; |
| 4901 | Py_UNICODE *p; |
| 4902 | Py_UNICODE *q; |
| 4903 | int i, j; |
| 4904 | PyUnicodeObject *u; |
| 4905 | int tabsize = 8; |
| 4906 | |
| 4907 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 4908 | return NULL; |
| 4909 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 4910 | /* First pass: determine size of output string */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4911 | i = j = 0; |
| 4912 | e = self->str + self->length; |
| 4913 | for (p = self->str; p < e; p++) |
| 4914 | if (*p == '\t') { |
| 4915 | if (tabsize > 0) |
| 4916 | j += tabsize - (j % tabsize); |
| 4917 | } |
| 4918 | else { |
| 4919 | j++; |
| 4920 | if (*p == '\n' || *p == '\r') { |
| 4921 | i += j; |
| 4922 | j = 0; |
| 4923 | } |
| 4924 | } |
| 4925 | |
| 4926 | /* Second pass: create output string and fill it */ |
| 4927 | u = _PyUnicode_New(i + j); |
| 4928 | if (!u) |
| 4929 | return NULL; |
| 4930 | |
| 4931 | j = 0; |
| 4932 | q = u->str; |
| 4933 | |
| 4934 | for (p = self->str; p < e; p++) |
| 4935 | if (*p == '\t') { |
| 4936 | if (tabsize > 0) { |
| 4937 | i = tabsize - (j % tabsize); |
| 4938 | j += i; |
| 4939 | while (i--) |
| 4940 | *q++ = ' '; |
| 4941 | } |
| 4942 | } |
| 4943 | else { |
| 4944 | j++; |
| 4945 | *q++ = *p; |
| 4946 | if (*p == '\n' || *p == '\r') |
| 4947 | j = 0; |
| 4948 | } |
| 4949 | |
| 4950 | return (PyObject*) u; |
| 4951 | } |
| 4952 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4953 | PyDoc_STRVAR(find__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4954 | "S.find(sub [,start [,end]]) -> int\n\ |
| 4955 | \n\ |
| 4956 | Return the lowest index in S where substring sub is found,\n\ |
| 4957 | such that sub is contained within s[start,end]. Optional\n\ |
| 4958 | arguments start and end are interpreted as in slice notation.\n\ |
| 4959 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4960 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4961 | |
| 4962 | static PyObject * |
| 4963 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 4964 | { |
| 4965 | PyUnicodeObject *substring; |
| 4966 | int start = 0; |
| 4967 | int end = INT_MAX; |
| 4968 | PyObject *result; |
| 4969 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 4970 | if (!PyArg_ParseTuple(args, "O|O&O&:find", &substring, |
| 4971 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4972 | return NULL; |
| 4973 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 4974 | (PyObject *)substring); |
| 4975 | if (substring == NULL) |
| 4976 | return NULL; |
| 4977 | |
| 4978 | result = PyInt_FromLong(findstring(self, substring, start, end, 1)); |
| 4979 | |
| 4980 | Py_DECREF(substring); |
| 4981 | return result; |
| 4982 | } |
| 4983 | |
| 4984 | static PyObject * |
| 4985 | unicode_getitem(PyUnicodeObject *self, int index) |
| 4986 | { |
| 4987 | if (index < 0 || index >= self->length) { |
| 4988 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 4989 | return NULL; |
| 4990 | } |
| 4991 | |
| 4992 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 4993 | } |
| 4994 | |
| 4995 | static long |
| 4996 | unicode_hash(PyUnicodeObject *self) |
| 4997 | { |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 4998 | /* Since Unicode objects compare equal to their ASCII string |
| 4999 | counterparts, they should use the individual character values |
| 5000 | as basis for their hash value. This is needed to assure that |
| 5001 | strings and Unicode objects behave in the same way as |
| 5002 | dictionary keys. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5003 | |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5004 | register int len; |
| 5005 | register Py_UNICODE *p; |
| 5006 | register long x; |
| 5007 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5008 | if (self->hash != -1) |
| 5009 | return self->hash; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5010 | len = PyUnicode_GET_SIZE(self); |
| 5011 | p = PyUnicode_AS_UNICODE(self); |
| 5012 | x = *p << 7; |
| 5013 | while (--len >= 0) |
| 5014 | x = (1000003*x) ^ *p++; |
| 5015 | x ^= PyUnicode_GET_SIZE(self); |
| 5016 | if (x == -1) |
| 5017 | x = -2; |
| 5018 | self->hash = x; |
| 5019 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5020 | } |
| 5021 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5022 | PyDoc_STRVAR(index__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5023 | "S.index(sub [,start [,end]]) -> int\n\ |
| 5024 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5025 | 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] | 5026 | |
| 5027 | static PyObject * |
| 5028 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 5029 | { |
| 5030 | int result; |
| 5031 | PyUnicodeObject *substring; |
| 5032 | int start = 0; |
| 5033 | int end = INT_MAX; |
| 5034 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5035 | if (!PyArg_ParseTuple(args, "O|O&O&:index", &substring, |
| 5036 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5037 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5038 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5039 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 5040 | (PyObject *)substring); |
| 5041 | if (substring == NULL) |
| 5042 | return NULL; |
| 5043 | |
| 5044 | result = findstring(self, substring, start, end, 1); |
| 5045 | |
| 5046 | Py_DECREF(substring); |
| 5047 | if (result < 0) { |
| 5048 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 5049 | return NULL; |
| 5050 | } |
| 5051 | return PyInt_FromLong(result); |
| 5052 | } |
| 5053 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5054 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5055 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5056 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5057 | 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] | 5058 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5059 | |
| 5060 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5061 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5062 | { |
| 5063 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5064 | register const Py_UNICODE *e; |
| 5065 | int cased; |
| 5066 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5067 | /* Shortcut for single character strings */ |
| 5068 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5069 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5070 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5071 | /* Special case for empty strings */ |
| 5072 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5073 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5074 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5075 | e = p + PyUnicode_GET_SIZE(self); |
| 5076 | cased = 0; |
| 5077 | for (; p < e; p++) { |
| 5078 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5079 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5080 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5081 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5082 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 5083 | cased = 1; |
| 5084 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5085 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5086 | } |
| 5087 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5088 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5089 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5090 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5091 | 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] | 5092 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5093 | |
| 5094 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5095 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5096 | { |
| 5097 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5098 | register const Py_UNICODE *e; |
| 5099 | int cased; |
| 5100 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5101 | /* Shortcut for single character strings */ |
| 5102 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5103 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5104 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5105 | /* Special case for empty strings */ |
| 5106 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5107 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5108 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5109 | e = p + PyUnicode_GET_SIZE(self); |
| 5110 | cased = 0; |
| 5111 | for (; p < e; p++) { |
| 5112 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5113 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5114 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5115 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5116 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 5117 | cased = 1; |
| 5118 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5119 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5120 | } |
| 5121 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5122 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5123 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5124 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5125 | Return True if S is a titlecased string and there is at least one\n\ |
| 5126 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 5127 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 5128 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5129 | |
| 5130 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5131 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5132 | { |
| 5133 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5134 | register const Py_UNICODE *e; |
| 5135 | int cased, previous_is_cased; |
| 5136 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5137 | /* Shortcut for single character strings */ |
| 5138 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5139 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 5140 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5141 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5142 | /* Special case for empty strings */ |
| 5143 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5144 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5145 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5146 | e = p + PyUnicode_GET_SIZE(self); |
| 5147 | cased = 0; |
| 5148 | previous_is_cased = 0; |
| 5149 | for (; p < e; p++) { |
| 5150 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5151 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5152 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 5153 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5154 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5155 | previous_is_cased = 1; |
| 5156 | cased = 1; |
| 5157 | } |
| 5158 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 5159 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5160 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5161 | previous_is_cased = 1; |
| 5162 | cased = 1; |
| 5163 | } |
| 5164 | else |
| 5165 | previous_is_cased = 0; |
| 5166 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5167 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5168 | } |
| 5169 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5170 | PyDoc_STRVAR(isspace__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5171 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5172 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5173 | Return True if all characters in S are whitespace\n\ |
| 5174 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5175 | |
| 5176 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5177 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5178 | { |
| 5179 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5180 | register const Py_UNICODE *e; |
| 5181 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5182 | /* Shortcut for single character strings */ |
| 5183 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5184 | Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5185 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5186 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5187 | /* Special case for empty strings */ |
| 5188 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5189 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5190 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5191 | e = p + PyUnicode_GET_SIZE(self); |
| 5192 | for (; p < e; p++) { |
| 5193 | if (!Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5194 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5195 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5196 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5197 | } |
| 5198 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5199 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5200 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5201 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5202 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5203 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5204 | |
| 5205 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5206 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5207 | { |
| 5208 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5209 | register const Py_UNICODE *e; |
| 5210 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5211 | /* Shortcut for single character strings */ |
| 5212 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5213 | Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5214 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5215 | |
| 5216 | /* Special case for empty strings */ |
| 5217 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5218 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5219 | |
| 5220 | e = p + PyUnicode_GET_SIZE(self); |
| 5221 | for (; p < e; p++) { |
| 5222 | if (!Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5223 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5224 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5225 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5226 | } |
| 5227 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5228 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5229 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5230 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5231 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5232 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5233 | |
| 5234 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5235 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5236 | { |
| 5237 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5238 | register const Py_UNICODE *e; |
| 5239 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5240 | /* Shortcut for single character strings */ |
| 5241 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5242 | Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5243 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5244 | |
| 5245 | /* Special case for empty strings */ |
| 5246 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5247 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5248 | |
| 5249 | e = p + PyUnicode_GET_SIZE(self); |
| 5250 | for (; p < e; p++) { |
| 5251 | if (!Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5252 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5253 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5254 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5255 | } |
| 5256 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5257 | PyDoc_STRVAR(isdecimal__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5258 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5259 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5260 | 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] | 5261 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5262 | |
| 5263 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5264 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5265 | { |
| 5266 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5267 | register const Py_UNICODE *e; |
| 5268 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5269 | /* Shortcut for single character strings */ |
| 5270 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5271 | Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5272 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5273 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5274 | /* Special case for empty strings */ |
| 5275 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5276 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5277 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5278 | e = p + PyUnicode_GET_SIZE(self); |
| 5279 | for (; p < e; p++) { |
| 5280 | if (!Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5281 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5282 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5283 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5284 | } |
| 5285 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5286 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5287 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5288 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5289 | Return True if all characters in S are digits\n\ |
| 5290 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5291 | |
| 5292 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5293 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5294 | { |
| 5295 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5296 | register const Py_UNICODE *e; |
| 5297 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5298 | /* Shortcut for single character strings */ |
| 5299 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5300 | Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5301 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5302 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5303 | /* Special case for empty strings */ |
| 5304 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5305 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5306 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5307 | e = p + PyUnicode_GET_SIZE(self); |
| 5308 | for (; p < e; p++) { |
| 5309 | if (!Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5310 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5311 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5312 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5313 | } |
| 5314 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5315 | PyDoc_STRVAR(isnumeric__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5316 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5317 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5318 | 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] | 5319 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5320 | |
| 5321 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5322 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5323 | { |
| 5324 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5325 | register const Py_UNICODE *e; |
| 5326 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5327 | /* Shortcut for single character strings */ |
| 5328 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5329 | Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5330 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5331 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5332 | /* Special case for empty strings */ |
| 5333 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5334 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5335 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5336 | e = p + PyUnicode_GET_SIZE(self); |
| 5337 | for (; p < e; p++) { |
| 5338 | if (!Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5339 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5340 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5341 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5342 | } |
| 5343 | |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 5344 | PyDoc_STRVAR(iswide__doc__, |
| 5345 | "S.iswide() -> bool\n\ |
| 5346 | \n\ |
| 5347 | Return True if all characters in S are wide width\n\ |
| 5348 | and there is at least one character in S, False otherwise."); |
| 5349 | |
| 5350 | static PyObject* |
| 5351 | unicode_iswide(PyUnicodeObject *self) |
| 5352 | { |
| 5353 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5354 | register const Py_UNICODE *e; |
| 5355 | |
| 5356 | /* Shortcut for single character strings */ |
| 5357 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5358 | Py_UNICODE_ISWIDE(*p)) |
| 5359 | Py_RETURN_TRUE; |
| 5360 | |
| 5361 | /* Special case for empty strings */ |
| 5362 | if (PyString_GET_SIZE(self) == 0) |
| 5363 | Py_RETURN_FALSE; |
| 5364 | |
| 5365 | e = p + PyUnicode_GET_SIZE(self); |
| 5366 | for (; p < e; p++) { |
| 5367 | if (!Py_UNICODE_ISWIDE(*p)) |
| 5368 | Py_RETURN_FALSE; |
| 5369 | } |
| 5370 | Py_RETURN_TRUE; |
| 5371 | } |
| 5372 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5373 | PyDoc_STRVAR(join__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5374 | "S.join(sequence) -> unicode\n\ |
| 5375 | \n\ |
| 5376 | 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] | 5377 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5378 | |
| 5379 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5380 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5381 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5382 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5383 | } |
| 5384 | |
| 5385 | static int |
| 5386 | unicode_length(PyUnicodeObject *self) |
| 5387 | { |
| 5388 | return self->length; |
| 5389 | } |
| 5390 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5391 | PyDoc_STRVAR(ljust__doc__, |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 5392 | "S.ljust(width[, fillchar]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5393 | \n\ |
| 5394 | 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] | 5395 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5396 | |
| 5397 | static PyObject * |
| 5398 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 5399 | { |
| 5400 | int width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5401 | Py_UNICODE fillchar = ' '; |
| 5402 | |
| 5403 | if (!PyArg_ParseTuple(args, "i|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5404 | return NULL; |
| 5405 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5406 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5407 | Py_INCREF(self); |
| 5408 | return (PyObject*) self; |
| 5409 | } |
| 5410 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5411 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5412 | } |
| 5413 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5414 | PyDoc_STRVAR(lower__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5415 | "S.lower() -> unicode\n\ |
| 5416 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5417 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5418 | |
| 5419 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5420 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5421 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5422 | return fixup(self, fixlower); |
| 5423 | } |
| 5424 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5425 | #define LEFTSTRIP 0 |
| 5426 | #define RIGHTSTRIP 1 |
| 5427 | #define BOTHSTRIP 2 |
| 5428 | |
| 5429 | /* Arrays indexed by above */ |
| 5430 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 5431 | |
| 5432 | #define STRIPNAME(i) (stripformat[i]+3) |
| 5433 | |
| 5434 | static const Py_UNICODE * |
| 5435 | unicode_memchr(const Py_UNICODE *s, Py_UNICODE c, size_t n) |
| 5436 | { |
Tim Peters | 030a5ce | 2002-04-22 19:00:10 +0000 | [diff] [blame] | 5437 | size_t i; |
| 5438 | for (i = 0; i < n; ++i) |
| 5439 | if (s[i] == c) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5440 | return s+i; |
| 5441 | return NULL; |
| 5442 | } |
| 5443 | |
| 5444 | /* externally visible for str.strip(unicode) */ |
| 5445 | PyObject * |
| 5446 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 5447 | { |
| 5448 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 5449 | int len = PyUnicode_GET_SIZE(self); |
| 5450 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
| 5451 | int seplen = PyUnicode_GET_SIZE(sepobj); |
| 5452 | int i, j; |
| 5453 | |
| 5454 | i = 0; |
| 5455 | if (striptype != RIGHTSTRIP) { |
| 5456 | while (i < len && unicode_memchr(sep, s[i], seplen)) { |
| 5457 | i++; |
| 5458 | } |
| 5459 | } |
| 5460 | |
| 5461 | j = len; |
| 5462 | if (striptype != LEFTSTRIP) { |
| 5463 | do { |
| 5464 | j--; |
| 5465 | } while (j >= i && unicode_memchr(sep, s[j], seplen)); |
| 5466 | j++; |
| 5467 | } |
| 5468 | |
| 5469 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 5470 | Py_INCREF(self); |
| 5471 | return (PyObject*)self; |
| 5472 | } |
| 5473 | else |
| 5474 | return PyUnicode_FromUnicode(s+i, j-i); |
| 5475 | } |
| 5476 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5477 | |
| 5478 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5479 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5480 | { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5481 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 5482 | int len = PyUnicode_GET_SIZE(self), i, j; |
| 5483 | |
| 5484 | i = 0; |
| 5485 | if (striptype != RIGHTSTRIP) { |
| 5486 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 5487 | i++; |
| 5488 | } |
| 5489 | } |
| 5490 | |
| 5491 | j = len; |
| 5492 | if (striptype != LEFTSTRIP) { |
| 5493 | do { |
| 5494 | j--; |
| 5495 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 5496 | j++; |
| 5497 | } |
| 5498 | |
| 5499 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 5500 | Py_INCREF(self); |
| 5501 | return (PyObject*)self; |
| 5502 | } |
| 5503 | else |
| 5504 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5505 | } |
| 5506 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5507 | |
| 5508 | static PyObject * |
| 5509 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 5510 | { |
| 5511 | PyObject *sep = NULL; |
| 5512 | |
| 5513 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 5514 | return NULL; |
| 5515 | |
| 5516 | if (sep != NULL && sep != Py_None) { |
| 5517 | if (PyUnicode_Check(sep)) |
| 5518 | return _PyUnicode_XStrip(self, striptype, sep); |
| 5519 | else if (PyString_Check(sep)) { |
| 5520 | PyObject *res; |
| 5521 | sep = PyUnicode_FromObject(sep); |
| 5522 | if (sep==NULL) |
| 5523 | return NULL; |
| 5524 | res = _PyUnicode_XStrip(self, striptype, sep); |
| 5525 | Py_DECREF(sep); |
| 5526 | return res; |
| 5527 | } |
| 5528 | else { |
| 5529 | PyErr_Format(PyExc_TypeError, |
| 5530 | "%s arg must be None, unicode or str", |
| 5531 | STRIPNAME(striptype)); |
| 5532 | return NULL; |
| 5533 | } |
| 5534 | } |
| 5535 | |
| 5536 | return do_strip(self, striptype); |
| 5537 | } |
| 5538 | |
| 5539 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5540 | PyDoc_STRVAR(strip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5541 | "S.strip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5542 | \n\ |
| 5543 | Return a copy of the string S with leading and trailing\n\ |
| 5544 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5545 | If chars is given and not None, remove characters in chars instead.\n\ |
| 5546 | 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] | 5547 | |
| 5548 | static PyObject * |
| 5549 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 5550 | { |
| 5551 | if (PyTuple_GET_SIZE(args) == 0) |
| 5552 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 5553 | else |
| 5554 | return do_argstrip(self, BOTHSTRIP, args); |
| 5555 | } |
| 5556 | |
| 5557 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5558 | PyDoc_STRVAR(lstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5559 | "S.lstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5560 | \n\ |
| 5561 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5562 | If chars is given and not None, remove characters in chars instead.\n\ |
| 5563 | 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] | 5564 | |
| 5565 | static PyObject * |
| 5566 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 5567 | { |
| 5568 | if (PyTuple_GET_SIZE(args) == 0) |
| 5569 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 5570 | else |
| 5571 | return do_argstrip(self, LEFTSTRIP, args); |
| 5572 | } |
| 5573 | |
| 5574 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5575 | PyDoc_STRVAR(rstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5576 | "S.rstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5577 | \n\ |
| 5578 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5579 | If chars is given and not None, remove characters in chars instead.\n\ |
| 5580 | 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] | 5581 | |
| 5582 | static PyObject * |
| 5583 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 5584 | { |
| 5585 | if (PyTuple_GET_SIZE(args) == 0) |
| 5586 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 5587 | else |
| 5588 | return do_argstrip(self, RIGHTSTRIP, args); |
| 5589 | } |
| 5590 | |
| 5591 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5592 | static PyObject* |
| 5593 | unicode_repeat(PyUnicodeObject *str, int len) |
| 5594 | { |
| 5595 | PyUnicodeObject *u; |
| 5596 | Py_UNICODE *p; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 5597 | int nchars; |
| 5598 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5599 | |
| 5600 | if (len < 0) |
| 5601 | len = 0; |
| 5602 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5603 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5604 | /* no repeat, return original string */ |
| 5605 | Py_INCREF(str); |
| 5606 | return (PyObject*) str; |
| 5607 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 5608 | |
| 5609 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 5610 | * needed doesn't overflow size_t |
| 5611 | */ |
| 5612 | nchars = len * str->length; |
| 5613 | if (len && nchars / len != str->length) { |
| 5614 | PyErr_SetString(PyExc_OverflowError, |
| 5615 | "repeated string is too long"); |
| 5616 | return NULL; |
| 5617 | } |
| 5618 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 5619 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 5620 | PyErr_SetString(PyExc_OverflowError, |
| 5621 | "repeated string is too long"); |
| 5622 | return NULL; |
| 5623 | } |
| 5624 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5625 | if (!u) |
| 5626 | return NULL; |
| 5627 | |
| 5628 | p = u->str; |
| 5629 | |
| 5630 | while (len-- > 0) { |
| 5631 | Py_UNICODE_COPY(p, str->str, str->length); |
| 5632 | p += str->length; |
| 5633 | } |
| 5634 | |
| 5635 | return (PyObject*) u; |
| 5636 | } |
| 5637 | |
| 5638 | PyObject *PyUnicode_Replace(PyObject *obj, |
| 5639 | PyObject *subobj, |
| 5640 | PyObject *replobj, |
| 5641 | int maxcount) |
| 5642 | { |
| 5643 | PyObject *self; |
| 5644 | PyObject *str1; |
| 5645 | PyObject *str2; |
| 5646 | PyObject *result; |
| 5647 | |
| 5648 | self = PyUnicode_FromObject(obj); |
| 5649 | if (self == NULL) |
| 5650 | return NULL; |
| 5651 | str1 = PyUnicode_FromObject(subobj); |
| 5652 | if (str1 == NULL) { |
| 5653 | Py_DECREF(self); |
| 5654 | return NULL; |
| 5655 | } |
| 5656 | str2 = PyUnicode_FromObject(replobj); |
| 5657 | if (str2 == NULL) { |
| 5658 | Py_DECREF(self); |
| 5659 | Py_DECREF(str1); |
| 5660 | return NULL; |
| 5661 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5662 | result = replace((PyUnicodeObject *)self, |
| 5663 | (PyUnicodeObject *)str1, |
| 5664 | (PyUnicodeObject *)str2, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5665 | maxcount); |
| 5666 | Py_DECREF(self); |
| 5667 | Py_DECREF(str1); |
| 5668 | Py_DECREF(str2); |
| 5669 | return result; |
| 5670 | } |
| 5671 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5672 | PyDoc_STRVAR(replace__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5673 | "S.replace (old, new[, maxsplit]) -> unicode\n\ |
| 5674 | \n\ |
| 5675 | Return a copy of S with all occurrences of substring\n\ |
| 5676 | 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] | 5677 | given, only the first maxsplit occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5678 | |
| 5679 | static PyObject* |
| 5680 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 5681 | { |
| 5682 | PyUnicodeObject *str1; |
| 5683 | PyUnicodeObject *str2; |
| 5684 | int maxcount = -1; |
| 5685 | PyObject *result; |
| 5686 | |
| 5687 | if (!PyArg_ParseTuple(args, "OO|i:replace", &str1, &str2, &maxcount)) |
| 5688 | return NULL; |
| 5689 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 5690 | if (str1 == NULL) |
| 5691 | return NULL; |
| 5692 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 5693 | if (str2 == NULL) { |
| 5694 | Py_DECREF(str1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5695 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 5696 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5697 | |
| 5698 | result = replace(self, str1, str2, maxcount); |
| 5699 | |
| 5700 | Py_DECREF(str1); |
| 5701 | Py_DECREF(str2); |
| 5702 | return result; |
| 5703 | } |
| 5704 | |
| 5705 | static |
| 5706 | PyObject *unicode_repr(PyObject *unicode) |
| 5707 | { |
| 5708 | return unicodeescape_string(PyUnicode_AS_UNICODE(unicode), |
| 5709 | PyUnicode_GET_SIZE(unicode), |
| 5710 | 1); |
| 5711 | } |
| 5712 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5713 | PyDoc_STRVAR(rfind__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5714 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 5715 | \n\ |
| 5716 | Return the highest index in S where substring sub is found,\n\ |
| 5717 | such that sub is contained within s[start,end]. Optional\n\ |
| 5718 | arguments start and end are interpreted as in slice notation.\n\ |
| 5719 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5720 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5721 | |
| 5722 | static PyObject * |
| 5723 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 5724 | { |
| 5725 | PyUnicodeObject *substring; |
| 5726 | int start = 0; |
| 5727 | int end = INT_MAX; |
| 5728 | PyObject *result; |
| 5729 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5730 | if (!PyArg_ParseTuple(args, "O|O&O&:rfind", &substring, |
| 5731 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5732 | return NULL; |
| 5733 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 5734 | (PyObject *)substring); |
| 5735 | if (substring == NULL) |
| 5736 | return NULL; |
| 5737 | |
| 5738 | result = PyInt_FromLong(findstring(self, substring, start, end, -1)); |
| 5739 | |
| 5740 | Py_DECREF(substring); |
| 5741 | return result; |
| 5742 | } |
| 5743 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5744 | PyDoc_STRVAR(rindex__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5745 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 5746 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5747 | 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] | 5748 | |
| 5749 | static PyObject * |
| 5750 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 5751 | { |
| 5752 | int result; |
| 5753 | PyUnicodeObject *substring; |
| 5754 | int start = 0; |
| 5755 | int end = INT_MAX; |
| 5756 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5757 | if (!PyArg_ParseTuple(args, "O|O&O&:rindex", &substring, |
| 5758 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5759 | return NULL; |
| 5760 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 5761 | (PyObject *)substring); |
| 5762 | if (substring == NULL) |
| 5763 | return NULL; |
| 5764 | |
| 5765 | result = findstring(self, substring, start, end, -1); |
| 5766 | |
| 5767 | Py_DECREF(substring); |
| 5768 | if (result < 0) { |
| 5769 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 5770 | return NULL; |
| 5771 | } |
| 5772 | return PyInt_FromLong(result); |
| 5773 | } |
| 5774 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5775 | PyDoc_STRVAR(rjust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5776 | "S.rjust(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5777 | \n\ |
| 5778 | 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] | 5779 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5780 | |
| 5781 | static PyObject * |
| 5782 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 5783 | { |
| 5784 | int width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5785 | Py_UNICODE fillchar = ' '; |
| 5786 | |
| 5787 | if (!PyArg_ParseTuple(args, "i|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5788 | return NULL; |
| 5789 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5790 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5791 | Py_INCREF(self); |
| 5792 | return (PyObject*) self; |
| 5793 | } |
| 5794 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5795 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5796 | } |
| 5797 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5798 | static PyObject* |
| 5799 | unicode_slice(PyUnicodeObject *self, int start, int end) |
| 5800 | { |
| 5801 | /* standard clamping */ |
| 5802 | if (start < 0) |
| 5803 | start = 0; |
| 5804 | if (end < 0) |
| 5805 | end = 0; |
| 5806 | if (end > self->length) |
| 5807 | end = self->length; |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5808 | if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5809 | /* full slice, return original string */ |
| 5810 | Py_INCREF(self); |
| 5811 | return (PyObject*) self; |
| 5812 | } |
| 5813 | if (start > end) |
| 5814 | start = end; |
| 5815 | /* copy slice */ |
| 5816 | return (PyObject*) PyUnicode_FromUnicode(self->str + start, |
| 5817 | end - start); |
| 5818 | } |
| 5819 | |
| 5820 | PyObject *PyUnicode_Split(PyObject *s, |
| 5821 | PyObject *sep, |
| 5822 | int maxsplit) |
| 5823 | { |
| 5824 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5825 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5826 | s = PyUnicode_FromObject(s); |
| 5827 | if (s == NULL) |
| 5828 | return NULL; |
| 5829 | if (sep != NULL) { |
| 5830 | sep = PyUnicode_FromObject(sep); |
| 5831 | if (sep == NULL) { |
| 5832 | Py_DECREF(s); |
| 5833 | return NULL; |
| 5834 | } |
| 5835 | } |
| 5836 | |
| 5837 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 5838 | |
| 5839 | Py_DECREF(s); |
| 5840 | Py_XDECREF(sep); |
| 5841 | return result; |
| 5842 | } |
| 5843 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5844 | PyDoc_STRVAR(split__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5845 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 5846 | \n\ |
| 5847 | Return a list of the words in S, using sep as the\n\ |
| 5848 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
| 5849 | splits are done. If sep is not specified, any whitespace string\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5850 | is a separator."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5851 | |
| 5852 | static PyObject* |
| 5853 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 5854 | { |
| 5855 | PyObject *substring = Py_None; |
| 5856 | int maxcount = -1; |
| 5857 | |
| 5858 | if (!PyArg_ParseTuple(args, "|Oi:split", &substring, &maxcount)) |
| 5859 | return NULL; |
| 5860 | |
| 5861 | if (substring == Py_None) |
| 5862 | return split(self, NULL, maxcount); |
| 5863 | else if (PyUnicode_Check(substring)) |
| 5864 | return split(self, (PyUnicodeObject *)substring, maxcount); |
| 5865 | else |
| 5866 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
| 5867 | } |
| 5868 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5869 | PyObject *PyUnicode_RSplit(PyObject *s, |
| 5870 | PyObject *sep, |
| 5871 | int maxsplit) |
| 5872 | { |
| 5873 | PyObject *result; |
| 5874 | |
| 5875 | s = PyUnicode_FromObject(s); |
| 5876 | if (s == NULL) |
| 5877 | return NULL; |
| 5878 | if (sep != NULL) { |
| 5879 | sep = PyUnicode_FromObject(sep); |
| 5880 | if (sep == NULL) { |
| 5881 | Py_DECREF(s); |
| 5882 | return NULL; |
| 5883 | } |
| 5884 | } |
| 5885 | |
| 5886 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 5887 | |
| 5888 | Py_DECREF(s); |
| 5889 | Py_XDECREF(sep); |
| 5890 | return result; |
| 5891 | } |
| 5892 | |
| 5893 | PyDoc_STRVAR(rsplit__doc__, |
| 5894 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
| 5895 | \n\ |
| 5896 | Return a list of the words in S, using sep as the\n\ |
| 5897 | delimiter string, starting at the end of the string and\n\ |
| 5898 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 5899 | splits are done. If sep is not specified, any whitespace string\n\ |
| 5900 | is a separator."); |
| 5901 | |
| 5902 | static PyObject* |
| 5903 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 5904 | { |
| 5905 | PyObject *substring = Py_None; |
| 5906 | int maxcount = -1; |
| 5907 | |
| 5908 | if (!PyArg_ParseTuple(args, "|Oi:rsplit", &substring, &maxcount)) |
| 5909 | return NULL; |
| 5910 | |
| 5911 | if (substring == Py_None) |
| 5912 | return rsplit(self, NULL, maxcount); |
| 5913 | else if (PyUnicode_Check(substring)) |
| 5914 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
| 5915 | else |
| 5916 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
| 5917 | } |
| 5918 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5919 | PyDoc_STRVAR(splitlines__doc__, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5920 | "S.splitlines([keepends]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5921 | \n\ |
| 5922 | 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] | 5923 | 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] | 5924 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5925 | |
| 5926 | static PyObject* |
| 5927 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 5928 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5929 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5930 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5931 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5932 | return NULL; |
| 5933 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5934 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5935 | } |
| 5936 | |
| 5937 | static |
| 5938 | PyObject *unicode_str(PyUnicodeObject *self) |
| 5939 | { |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 5940 | return PyUnicode_AsEncodedString((PyObject *)self, NULL, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5941 | } |
| 5942 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5943 | PyDoc_STRVAR(swapcase__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5944 | "S.swapcase() -> unicode\n\ |
| 5945 | \n\ |
| 5946 | 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] | 5947 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5948 | |
| 5949 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5950 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5951 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5952 | return fixup(self, fixswapcase); |
| 5953 | } |
| 5954 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5955 | PyDoc_STRVAR(translate__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5956 | "S.translate(table) -> unicode\n\ |
| 5957 | \n\ |
| 5958 | Return a copy of the string S, where all characters have been mapped\n\ |
| 5959 | 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] | 5960 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ |
| 5961 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 5962 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5963 | |
| 5964 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5965 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5966 | { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5967 | return PyUnicode_TranslateCharmap(self->str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5968 | self->length, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5969 | table, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5970 | "ignore"); |
| 5971 | } |
| 5972 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5973 | PyDoc_STRVAR(upper__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5974 | "S.upper() -> unicode\n\ |
| 5975 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5976 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5977 | |
| 5978 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5979 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5980 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5981 | return fixup(self, fixupper); |
| 5982 | } |
| 5983 | |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 5984 | PyDoc_STRVAR(width__doc__, |
| 5985 | "S.width() -> unicode\n\ |
| 5986 | \n\ |
| 5987 | Return a fixed-width representation length of S."); |
| 5988 | |
| 5989 | static PyObject* |
| 5990 | unicode_width(PyObject *self) |
| 5991 | { |
| 5992 | int width = PyUnicode_GetWidth(self); |
| 5993 | if (width == -1) |
| 5994 | return NULL; |
| 5995 | else |
| 5996 | return PyInt_FromLong((long)width); |
| 5997 | } |
| 5998 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5999 | PyDoc_STRVAR(zfill__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6000 | "S.zfill(width) -> unicode\n\ |
| 6001 | \n\ |
| 6002 | 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] | 6003 | of the specified width. The string x is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6004 | |
| 6005 | static PyObject * |
| 6006 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 6007 | { |
| 6008 | int fill; |
| 6009 | PyUnicodeObject *u; |
| 6010 | |
| 6011 | int width; |
| 6012 | if (!PyArg_ParseTuple(args, "i:zfill", &width)) |
| 6013 | return NULL; |
| 6014 | |
| 6015 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 6016 | if (PyUnicode_CheckExact(self)) { |
| 6017 | Py_INCREF(self); |
| 6018 | return (PyObject*) self; |
| 6019 | } |
| 6020 | else |
| 6021 | return PyUnicode_FromUnicode( |
| 6022 | PyUnicode_AS_UNICODE(self), |
| 6023 | PyUnicode_GET_SIZE(self) |
| 6024 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6025 | } |
| 6026 | |
| 6027 | fill = width - self->length; |
| 6028 | |
| 6029 | u = pad(self, fill, 0, '0'); |
| 6030 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 6031 | if (u == NULL) |
| 6032 | return NULL; |
| 6033 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6034 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 6035 | /* move sign to beginning of string */ |
| 6036 | u->str[0] = u->str[fill]; |
| 6037 | u->str[fill] = '0'; |
| 6038 | } |
| 6039 | |
| 6040 | return (PyObject*) u; |
| 6041 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6042 | |
| 6043 | #if 0 |
| 6044 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6045 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6046 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6047 | return PyInt_FromLong(unicode_freelist_size); |
| 6048 | } |
| 6049 | #endif |
| 6050 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6051 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6052 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6053 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 6054 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 6055 | With optional start, test S beginning at that position.\n\ |
| 6056 | With optional end, stop comparing S at that position."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6057 | |
| 6058 | static PyObject * |
| 6059 | unicode_startswith(PyUnicodeObject *self, |
| 6060 | PyObject *args) |
| 6061 | { |
| 6062 | PyUnicodeObject *substring; |
| 6063 | int start = 0; |
| 6064 | int end = INT_MAX; |
| 6065 | PyObject *result; |
| 6066 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6067 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &substring, |
| 6068 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6069 | return NULL; |
| 6070 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6071 | (PyObject *)substring); |
| 6072 | if (substring == NULL) |
| 6073 | return NULL; |
| 6074 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6075 | result = PyBool_FromLong(tailmatch(self, substring, start, end, -1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6076 | |
| 6077 | Py_DECREF(substring); |
| 6078 | return result; |
| 6079 | } |
| 6080 | |
| 6081 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6082 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6083 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6084 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 6085 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 6086 | With optional start, test S beginning at that position.\n\ |
| 6087 | With optional end, stop comparing S at that position."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6088 | |
| 6089 | static PyObject * |
| 6090 | unicode_endswith(PyUnicodeObject *self, |
| 6091 | PyObject *args) |
| 6092 | { |
| 6093 | PyUnicodeObject *substring; |
| 6094 | int start = 0; |
| 6095 | int end = INT_MAX; |
| 6096 | PyObject *result; |
| 6097 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6098 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &substring, |
| 6099 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6100 | return NULL; |
| 6101 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6102 | (PyObject *)substring); |
| 6103 | if (substring == NULL) |
| 6104 | return NULL; |
| 6105 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6106 | result = PyBool_FromLong(tailmatch(self, substring, start, end, +1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6107 | |
| 6108 | Py_DECREF(substring); |
| 6109 | return result; |
| 6110 | } |
| 6111 | |
| 6112 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 6113 | |
| 6114 | static PyObject * |
| 6115 | unicode_getnewargs(PyUnicodeObject *v) |
| 6116 | { |
| 6117 | return Py_BuildValue("(u#)", v->str, v->length); |
| 6118 | } |
| 6119 | |
| 6120 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6121 | static PyMethodDef unicode_methods[] = { |
| 6122 | |
| 6123 | /* Order is according to common usage: often used methods should |
| 6124 | appear first, since lookup is done sequentially. */ |
| 6125 | |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6126 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
| 6127 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 6128 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6129 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6130 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 6131 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 6132 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 6133 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 6134 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 6135 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 6136 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
| 6137 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 6138 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 6139 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6140 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6141 | /* {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */ |
| 6142 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 6143 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 6144 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6145 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6146 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6147 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6148 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 6149 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 6150 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 6151 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 6152 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 6153 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 6154 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 6155 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 6156 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 6157 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 6158 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 6159 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 6160 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 6161 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 6162 | {"iswide", (PyCFunction) unicode_iswide, METH_NOARGS, iswide__doc__}, |
| 6163 | {"width", (PyCFunction) unicode_width, METH_NOARGS, width__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6164 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 6165 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6166 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6167 | #endif |
| 6168 | |
| 6169 | #if 0 |
| 6170 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6171 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6172 | #endif |
| 6173 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 6174 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6175 | {NULL, NULL} |
| 6176 | }; |
| 6177 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 6178 | static PyObject * |
| 6179 | unicode_mod(PyObject *v, PyObject *w) |
| 6180 | { |
| 6181 | if (!PyUnicode_Check(v)) { |
| 6182 | Py_INCREF(Py_NotImplemented); |
| 6183 | return Py_NotImplemented; |
| 6184 | } |
| 6185 | return PyUnicode_Format(v, w); |
| 6186 | } |
| 6187 | |
| 6188 | static PyNumberMethods unicode_as_number = { |
| 6189 | 0, /*nb_add*/ |
| 6190 | 0, /*nb_subtract*/ |
| 6191 | 0, /*nb_multiply*/ |
| 6192 | 0, /*nb_divide*/ |
| 6193 | unicode_mod, /*nb_remainder*/ |
| 6194 | }; |
| 6195 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6196 | static PySequenceMethods unicode_as_sequence = { |
| 6197 | (inquiry) unicode_length, /* sq_length */ |
| 6198 | (binaryfunc) PyUnicode_Concat, /* sq_concat */ |
| 6199 | (intargfunc) unicode_repeat, /* sq_repeat */ |
| 6200 | (intargfunc) unicode_getitem, /* sq_item */ |
| 6201 | (intintargfunc) unicode_slice, /* sq_slice */ |
| 6202 | 0, /* sq_ass_item */ |
| 6203 | 0, /* sq_ass_slice */ |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6204 | (objobjproc)PyUnicode_Contains, /*sq_contains*/ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6205 | }; |
| 6206 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6207 | static PyObject* |
| 6208 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 6209 | { |
| 6210 | if (PyInt_Check(item)) { |
| 6211 | long i = PyInt_AS_LONG(item); |
| 6212 | if (i < 0) |
| 6213 | i += PyString_GET_SIZE(self); |
| 6214 | return unicode_getitem(self, i); |
| 6215 | } else if (PyLong_Check(item)) { |
| 6216 | long i = PyLong_AsLong(item); |
| 6217 | if (i == -1 && PyErr_Occurred()) |
| 6218 | return NULL; |
| 6219 | if (i < 0) |
| 6220 | i += PyString_GET_SIZE(self); |
| 6221 | return unicode_getitem(self, i); |
| 6222 | } else if (PySlice_Check(item)) { |
| 6223 | int start, stop, step, slicelength, cur, i; |
| 6224 | Py_UNICODE* source_buf; |
| 6225 | Py_UNICODE* result_buf; |
| 6226 | PyObject* result; |
| 6227 | |
| 6228 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyString_GET_SIZE(self), |
| 6229 | &start, &stop, &step, &slicelength) < 0) { |
| 6230 | return NULL; |
| 6231 | } |
| 6232 | |
| 6233 | if (slicelength <= 0) { |
| 6234 | return PyUnicode_FromUnicode(NULL, 0); |
| 6235 | } else { |
| 6236 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
| 6237 | result_buf = PyMem_MALLOC(slicelength*sizeof(Py_UNICODE)); |
| 6238 | |
| 6239 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 6240 | result_buf[i] = source_buf[cur]; |
| 6241 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6242 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6243 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
| 6244 | PyMem_FREE(result_buf); |
| 6245 | return result; |
| 6246 | } |
| 6247 | } else { |
| 6248 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 6249 | return NULL; |
| 6250 | } |
| 6251 | } |
| 6252 | |
| 6253 | static PyMappingMethods unicode_as_mapping = { |
| 6254 | (inquiry)unicode_length, /* mp_length */ |
| 6255 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 6256 | (objobjargproc)0, /* mp_ass_subscript */ |
| 6257 | }; |
| 6258 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6259 | static int |
| 6260 | unicode_buffer_getreadbuf(PyUnicodeObject *self, |
| 6261 | int index, |
| 6262 | const void **ptr) |
| 6263 | { |
| 6264 | if (index != 0) { |
| 6265 | PyErr_SetString(PyExc_SystemError, |
| 6266 | "accessing non-existent unicode segment"); |
| 6267 | return -1; |
| 6268 | } |
| 6269 | *ptr = (void *) self->str; |
| 6270 | return PyUnicode_GET_DATA_SIZE(self); |
| 6271 | } |
| 6272 | |
| 6273 | static int |
| 6274 | unicode_buffer_getwritebuf(PyUnicodeObject *self, int index, |
| 6275 | const void **ptr) |
| 6276 | { |
| 6277 | PyErr_SetString(PyExc_TypeError, |
Neal Norwitz | 20e7213 | 2002-06-13 21:25:17 +0000 | [diff] [blame] | 6278 | "cannot use unicode as modifiable buffer"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6279 | return -1; |
| 6280 | } |
| 6281 | |
| 6282 | static int |
| 6283 | unicode_buffer_getsegcount(PyUnicodeObject *self, |
| 6284 | int *lenp) |
| 6285 | { |
| 6286 | if (lenp) |
| 6287 | *lenp = PyUnicode_GET_DATA_SIZE(self); |
| 6288 | return 1; |
| 6289 | } |
| 6290 | |
| 6291 | static int |
| 6292 | unicode_buffer_getcharbuf(PyUnicodeObject *self, |
| 6293 | int index, |
| 6294 | const void **ptr) |
| 6295 | { |
| 6296 | PyObject *str; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6297 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6298 | if (index != 0) { |
| 6299 | PyErr_SetString(PyExc_SystemError, |
| 6300 | "accessing non-existent unicode segment"); |
| 6301 | return -1; |
| 6302 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 6303 | str = _PyUnicode_AsDefaultEncodedString((PyObject *)self, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6304 | if (str == NULL) |
| 6305 | return -1; |
| 6306 | *ptr = (void *) PyString_AS_STRING(str); |
| 6307 | return PyString_GET_SIZE(str); |
| 6308 | } |
| 6309 | |
| 6310 | /* Helpers for PyUnicode_Format() */ |
| 6311 | |
| 6312 | static PyObject * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 6313 | getnextarg(PyObject *args, int arglen, int *p_argidx) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6314 | { |
| 6315 | int argidx = *p_argidx; |
| 6316 | if (argidx < arglen) { |
| 6317 | (*p_argidx)++; |
| 6318 | if (arglen < 0) |
| 6319 | return args; |
| 6320 | else |
| 6321 | return PyTuple_GetItem(args, argidx); |
| 6322 | } |
| 6323 | PyErr_SetString(PyExc_TypeError, |
| 6324 | "not enough arguments for format string"); |
| 6325 | return NULL; |
| 6326 | } |
| 6327 | |
| 6328 | #define F_LJUST (1<<0) |
| 6329 | #define F_SIGN (1<<1) |
| 6330 | #define F_BLANK (1<<2) |
| 6331 | #define F_ALT (1<<3) |
| 6332 | #define F_ZERO (1<<4) |
| 6333 | |
| 6334 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6335 | int usprintf(register Py_UNICODE *buffer, char *format, ...) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6336 | { |
| 6337 | register int i; |
| 6338 | int len; |
| 6339 | va_list va; |
| 6340 | char *charbuffer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6341 | va_start(va, format); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6342 | |
| 6343 | /* First, format the string as char array, then expand to Py_UNICODE |
| 6344 | array. */ |
| 6345 | charbuffer = (char *)buffer; |
| 6346 | len = vsprintf(charbuffer, format, va); |
| 6347 | for (i = len - 1; i >= 0; i--) |
| 6348 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
| 6349 | |
| 6350 | va_end(va); |
| 6351 | return len; |
| 6352 | } |
| 6353 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 6354 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 6355 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 6356 | formatting is done. */ |
| 6357 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6358 | static int |
| 6359 | formatfloat(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6360 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6361 | int flags, |
| 6362 | int prec, |
| 6363 | int type, |
| 6364 | PyObject *v) |
| 6365 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6366 | /* fmt = '%#.' + `prec` + `type` |
| 6367 | 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] | 6368 | char fmt[20]; |
| 6369 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6370 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6371 | x = PyFloat_AsDouble(v); |
| 6372 | if (x == -1.0 && PyErr_Occurred()) |
| 6373 | return -1; |
| 6374 | if (prec < 0) |
| 6375 | prec = 6; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6376 | if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
| 6377 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6378 | /* Worst case length calc to ensure no buffer overrun: |
| 6379 | |
| 6380 | 'g' formats: |
| 6381 | fmt = %#.<prec>g |
| 6382 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 6383 | for any double rep.) |
| 6384 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 6385 | |
| 6386 | 'f' formats: |
| 6387 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 6388 | len = 1 + 50 + 1 + prec = 52 + prec |
| 6389 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6390 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6391 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6392 | |
| 6393 | */ |
| 6394 | if ((type == 'g' && buflen <= (size_t)10 + (size_t)prec) || |
| 6395 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6396 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6397 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6398 | return -1; |
| 6399 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6400 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 6401 | (flags&F_ALT) ? "#" : "", |
| 6402 | prec, type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6403 | return usprintf(buf, fmt, x); |
| 6404 | } |
| 6405 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6406 | static PyObject* |
| 6407 | formatlong(PyObject *val, int flags, int prec, int type) |
| 6408 | { |
| 6409 | char *buf; |
| 6410 | int i, len; |
| 6411 | PyObject *str; /* temporary string object. */ |
| 6412 | PyUnicodeObject *result; |
| 6413 | |
| 6414 | str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
| 6415 | if (!str) |
| 6416 | return NULL; |
| 6417 | result = _PyUnicode_New(len); |
| 6418 | for (i = 0; i < len; i++) |
| 6419 | result->str[i] = buf[i]; |
| 6420 | result->str[len] = 0; |
| 6421 | Py_DECREF(str); |
| 6422 | return (PyObject*)result; |
| 6423 | } |
| 6424 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6425 | static int |
| 6426 | formatint(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6427 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6428 | int flags, |
| 6429 | int prec, |
| 6430 | int type, |
| 6431 | PyObject *v) |
| 6432 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6433 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6434 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 6435 | * + 1 + 1 |
| 6436 | * = 24 |
| 6437 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6438 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6439 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6440 | long x; |
| 6441 | |
| 6442 | x = PyInt_AsLong(v); |
| 6443 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6444 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6445 | if (x < 0 && type == 'u') { |
| 6446 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 6447 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6448 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 6449 | sign = "-"; |
| 6450 | else |
| 6451 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6452 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6453 | prec = 1; |
| 6454 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6455 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 6456 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6457 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6458 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6459 | PyErr_SetString(PyExc_OverflowError, |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6460 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6461 | return -1; |
| 6462 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6463 | |
| 6464 | if ((flags & F_ALT) && |
| 6465 | (type == 'x' || type == 'X')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6466 | /* When converting under %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6467 | * of issues that cause pain: |
| 6468 | * - when 0 is being converted, the C standard leaves off |
| 6469 | * the '0x' or '0X', which is inconsistent with other |
| 6470 | * %#x/%#X conversions and inconsistent with Python's |
| 6471 | * hex() function |
| 6472 | * - there are platforms that violate the standard and |
| 6473 | * convert 0 with the '0x' or '0X' |
| 6474 | * (Metrowerks, Compaq Tru64) |
| 6475 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6476 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6477 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6478 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6479 | * We can achieve the desired consistency by inserting our |
| 6480 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 6481 | * of %#x/%#X. |
| 6482 | * |
| 6483 | * Note that this is the same approach as used in |
| 6484 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 6485 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6486 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 6487 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 6488 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6489 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6490 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 6491 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6492 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 6493 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6494 | if (sign[0]) |
| 6495 | return usprintf(buf, fmt, -x); |
| 6496 | else |
| 6497 | return usprintf(buf, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6498 | } |
| 6499 | |
| 6500 | static int |
| 6501 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6502 | size_t buflen, |
| 6503 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6504 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6505 | /* presume that the buffer is at least 2 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6506 | if (PyUnicode_Check(v)) { |
| 6507 | if (PyUnicode_GET_SIZE(v) != 1) |
| 6508 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6509 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6510 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6511 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6512 | else if (PyString_Check(v)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6513 | if (PyString_GET_SIZE(v) != 1) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6514 | goto onError; |
| 6515 | buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0]; |
| 6516 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6517 | |
| 6518 | else { |
| 6519 | /* Integer input truncated to a character */ |
| 6520 | long x; |
| 6521 | x = PyInt_AsLong(v); |
| 6522 | if (x == -1 && PyErr_Occurred()) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6523 | goto onError; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 6524 | #ifdef Py_UNICODE_WIDE |
| 6525 | if (x < 0 || x > 0x10ffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 6526 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 6527 | "%c arg not in range(0x110000) " |
| 6528 | "(wide Python build)"); |
| 6529 | return -1; |
| 6530 | } |
| 6531 | #else |
| 6532 | if (x < 0 || x > 0xffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 6533 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 6534 | "%c arg not in range(0x10000) " |
| 6535 | "(narrow Python build)"); |
| 6536 | return -1; |
| 6537 | } |
| 6538 | #endif |
| 6539 | buf[0] = (Py_UNICODE) x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6540 | } |
| 6541 | buf[1] = '\0'; |
| 6542 | return 1; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6543 | |
| 6544 | onError: |
| 6545 | PyErr_SetString(PyExc_TypeError, |
| 6546 | "%c requires int or char"); |
| 6547 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6548 | } |
| 6549 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6550 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 6551 | |
| 6552 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 6553 | chars are formatted. XXX This is a magic number. Each formatting |
| 6554 | routine does bounds checking to ensure no overflow, but a better |
| 6555 | solution may be to malloc a buffer of appropriate size for each |
| 6556 | format. For now, the current solution is sufficient. |
| 6557 | */ |
| 6558 | #define FORMATBUFLEN (size_t)120 |
| 6559 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6560 | PyObject *PyUnicode_Format(PyObject *format, |
| 6561 | PyObject *args) |
| 6562 | { |
| 6563 | Py_UNICODE *fmt, *res; |
| 6564 | int fmtcnt, rescnt, reslen, arglen, argidx; |
| 6565 | int args_owned = 0; |
| 6566 | PyUnicodeObject *result = NULL; |
| 6567 | PyObject *dict = NULL; |
| 6568 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6569 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6570 | if (format == NULL || args == NULL) { |
| 6571 | PyErr_BadInternalCall(); |
| 6572 | return NULL; |
| 6573 | } |
| 6574 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6575 | if (uformat == NULL) |
| 6576 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6577 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 6578 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 6579 | |
| 6580 | reslen = rescnt = fmtcnt + 100; |
| 6581 | result = _PyUnicode_New(reslen); |
| 6582 | if (result == NULL) |
| 6583 | goto onError; |
| 6584 | res = PyUnicode_AS_UNICODE(result); |
| 6585 | |
| 6586 | if (PyTuple_Check(args)) { |
| 6587 | arglen = PyTuple_Size(args); |
| 6588 | argidx = 0; |
| 6589 | } |
| 6590 | else { |
| 6591 | arglen = -1; |
| 6592 | argidx = -2; |
| 6593 | } |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 6594 | if (args->ob_type->tp_as_mapping && !PyTuple_Check(args) && |
| 6595 | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6596 | dict = args; |
| 6597 | |
| 6598 | while (--fmtcnt >= 0) { |
| 6599 | if (*fmt != '%') { |
| 6600 | if (--rescnt < 0) { |
| 6601 | rescnt = fmtcnt + 100; |
| 6602 | reslen += rescnt; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6603 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6604 | return NULL; |
| 6605 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 6606 | --rescnt; |
| 6607 | } |
| 6608 | *res++ = *fmt++; |
| 6609 | } |
| 6610 | else { |
| 6611 | /* Got a format specifier */ |
| 6612 | int flags = 0; |
| 6613 | int width = -1; |
| 6614 | int prec = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6615 | Py_UNICODE c = '\0'; |
| 6616 | Py_UNICODE fill; |
| 6617 | PyObject *v = NULL; |
| 6618 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6619 | Py_UNICODE *pbuf; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6620 | Py_UNICODE sign; |
| 6621 | int len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6622 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6623 | |
| 6624 | fmt++; |
| 6625 | if (*fmt == '(') { |
| 6626 | Py_UNICODE *keystart; |
| 6627 | int keylen; |
| 6628 | PyObject *key; |
| 6629 | int pcount = 1; |
| 6630 | |
| 6631 | if (dict == NULL) { |
| 6632 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6633 | "format requires a mapping"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6634 | goto onError; |
| 6635 | } |
| 6636 | ++fmt; |
| 6637 | --fmtcnt; |
| 6638 | keystart = fmt; |
| 6639 | /* Skip over balanced parentheses */ |
| 6640 | while (pcount > 0 && --fmtcnt >= 0) { |
| 6641 | if (*fmt == ')') |
| 6642 | --pcount; |
| 6643 | else if (*fmt == '(') |
| 6644 | ++pcount; |
| 6645 | fmt++; |
| 6646 | } |
| 6647 | keylen = fmt - keystart - 1; |
| 6648 | if (fmtcnt < 0 || pcount > 0) { |
| 6649 | PyErr_SetString(PyExc_ValueError, |
| 6650 | "incomplete format key"); |
| 6651 | goto onError; |
| 6652 | } |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 6653 | #if 0 |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6654 | /* keys are converted to strings using UTF-8 and |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6655 | then looked up since Python uses strings to hold |
| 6656 | variables names etc. in its namespaces and we |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6657 | wouldn't want to break common idioms. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6658 | key = PyUnicode_EncodeUTF8(keystart, |
| 6659 | keylen, |
| 6660 | NULL); |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 6661 | #else |
| 6662 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 6663 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6664 | if (key == NULL) |
| 6665 | goto onError; |
| 6666 | if (args_owned) { |
| 6667 | Py_DECREF(args); |
| 6668 | args_owned = 0; |
| 6669 | } |
| 6670 | args = PyObject_GetItem(dict, key); |
| 6671 | Py_DECREF(key); |
| 6672 | if (args == NULL) { |
| 6673 | goto onError; |
| 6674 | } |
| 6675 | args_owned = 1; |
| 6676 | arglen = -1; |
| 6677 | argidx = -2; |
| 6678 | } |
| 6679 | while (--fmtcnt >= 0) { |
| 6680 | switch (c = *fmt++) { |
| 6681 | case '-': flags |= F_LJUST; continue; |
| 6682 | case '+': flags |= F_SIGN; continue; |
| 6683 | case ' ': flags |= F_BLANK; continue; |
| 6684 | case '#': flags |= F_ALT; continue; |
| 6685 | case '0': flags |= F_ZERO; continue; |
| 6686 | } |
| 6687 | break; |
| 6688 | } |
| 6689 | if (c == '*') { |
| 6690 | v = getnextarg(args, arglen, &argidx); |
| 6691 | if (v == NULL) |
| 6692 | goto onError; |
| 6693 | if (!PyInt_Check(v)) { |
| 6694 | PyErr_SetString(PyExc_TypeError, |
| 6695 | "* wants int"); |
| 6696 | goto onError; |
| 6697 | } |
| 6698 | width = PyInt_AsLong(v); |
| 6699 | if (width < 0) { |
| 6700 | flags |= F_LJUST; |
| 6701 | width = -width; |
| 6702 | } |
| 6703 | if (--fmtcnt >= 0) |
| 6704 | c = *fmt++; |
| 6705 | } |
| 6706 | else if (c >= '0' && c <= '9') { |
| 6707 | width = c - '0'; |
| 6708 | while (--fmtcnt >= 0) { |
| 6709 | c = *fmt++; |
| 6710 | if (c < '0' || c > '9') |
| 6711 | break; |
| 6712 | if ((width*10) / 10 != width) { |
| 6713 | PyErr_SetString(PyExc_ValueError, |
| 6714 | "width too big"); |
| 6715 | goto onError; |
| 6716 | } |
| 6717 | width = width*10 + (c - '0'); |
| 6718 | } |
| 6719 | } |
| 6720 | if (c == '.') { |
| 6721 | prec = 0; |
| 6722 | if (--fmtcnt >= 0) |
| 6723 | c = *fmt++; |
| 6724 | if (c == '*') { |
| 6725 | v = getnextarg(args, arglen, &argidx); |
| 6726 | if (v == NULL) |
| 6727 | goto onError; |
| 6728 | if (!PyInt_Check(v)) { |
| 6729 | PyErr_SetString(PyExc_TypeError, |
| 6730 | "* wants int"); |
| 6731 | goto onError; |
| 6732 | } |
| 6733 | prec = PyInt_AsLong(v); |
| 6734 | if (prec < 0) |
| 6735 | prec = 0; |
| 6736 | if (--fmtcnt >= 0) |
| 6737 | c = *fmt++; |
| 6738 | } |
| 6739 | else if (c >= '0' && c <= '9') { |
| 6740 | prec = c - '0'; |
| 6741 | while (--fmtcnt >= 0) { |
| 6742 | c = Py_CHARMASK(*fmt++); |
| 6743 | if (c < '0' || c > '9') |
| 6744 | break; |
| 6745 | if ((prec*10) / 10 != prec) { |
| 6746 | PyErr_SetString(PyExc_ValueError, |
| 6747 | "prec too big"); |
| 6748 | goto onError; |
| 6749 | } |
| 6750 | prec = prec*10 + (c - '0'); |
| 6751 | } |
| 6752 | } |
| 6753 | } /* prec */ |
| 6754 | if (fmtcnt >= 0) { |
| 6755 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6756 | if (--fmtcnt >= 0) |
| 6757 | c = *fmt++; |
| 6758 | } |
| 6759 | } |
| 6760 | if (fmtcnt < 0) { |
| 6761 | PyErr_SetString(PyExc_ValueError, |
| 6762 | "incomplete format"); |
| 6763 | goto onError; |
| 6764 | } |
| 6765 | if (c != '%') { |
| 6766 | v = getnextarg(args, arglen, &argidx); |
| 6767 | if (v == NULL) |
| 6768 | goto onError; |
| 6769 | } |
| 6770 | sign = 0; |
| 6771 | fill = ' '; |
| 6772 | switch (c) { |
| 6773 | |
| 6774 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6775 | pbuf = formatbuf; |
| 6776 | /* presume that buffer length is at least 1 */ |
| 6777 | pbuf[0] = '%'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6778 | len = 1; |
| 6779 | break; |
| 6780 | |
| 6781 | case 's': |
| 6782 | case 'r': |
| 6783 | if (PyUnicode_Check(v) && c == 's') { |
| 6784 | temp = v; |
| 6785 | Py_INCREF(temp); |
| 6786 | } |
| 6787 | else { |
| 6788 | PyObject *unicode; |
| 6789 | if (c == 's') |
| 6790 | temp = PyObject_Str(v); |
| 6791 | else |
| 6792 | temp = PyObject_Repr(v); |
| 6793 | if (temp == NULL) |
| 6794 | goto onError; |
| 6795 | if (!PyString_Check(temp)) { |
| 6796 | /* XXX Note: this should never happen, since |
| 6797 | PyObject_Repr() and PyObject_Str() assure |
| 6798 | this */ |
| 6799 | Py_DECREF(temp); |
| 6800 | PyErr_SetString(PyExc_TypeError, |
| 6801 | "%s argument has non-string str()"); |
| 6802 | goto onError; |
| 6803 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6804 | unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6805 | PyString_GET_SIZE(temp), |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6806 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6807 | "strict"); |
| 6808 | Py_DECREF(temp); |
| 6809 | temp = unicode; |
| 6810 | if (temp == NULL) |
| 6811 | goto onError; |
| 6812 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6813 | pbuf = PyUnicode_AS_UNICODE(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6814 | len = PyUnicode_GET_SIZE(temp); |
| 6815 | if (prec >= 0 && len > prec) |
| 6816 | len = prec; |
| 6817 | break; |
| 6818 | |
| 6819 | case 'i': |
| 6820 | case 'd': |
| 6821 | case 'u': |
| 6822 | case 'o': |
| 6823 | case 'x': |
| 6824 | case 'X': |
| 6825 | if (c == 'i') |
| 6826 | c = 'd'; |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 6827 | if (PyLong_Check(v)) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6828 | temp = formatlong(v, flags, prec, c); |
| 6829 | if (!temp) |
| 6830 | goto onError; |
| 6831 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 6832 | len = PyUnicode_GET_SIZE(temp); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6833 | sign = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6834 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6835 | else { |
| 6836 | pbuf = formatbuf; |
| 6837 | len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 6838 | flags, prec, c, v); |
| 6839 | if (len < 0) |
| 6840 | goto onError; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6841 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6842 | } |
| 6843 | if (flags & F_ZERO) |
| 6844 | fill = '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6845 | break; |
| 6846 | |
| 6847 | case 'e': |
| 6848 | case 'E': |
| 6849 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 6850 | case 'F': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6851 | case 'g': |
| 6852 | case 'G': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 6853 | if (c == 'F') |
| 6854 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6855 | pbuf = formatbuf; |
| 6856 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 6857 | flags, prec, c, v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6858 | if (len < 0) |
| 6859 | goto onError; |
| 6860 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6861 | if (flags & F_ZERO) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6862 | fill = '0'; |
| 6863 | break; |
| 6864 | |
| 6865 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6866 | pbuf = formatbuf; |
| 6867 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6868 | if (len < 0) |
| 6869 | goto onError; |
| 6870 | break; |
| 6871 | |
| 6872 | default: |
| 6873 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 6874 | "unsupported format character '%c' (0x%x) " |
| 6875 | "at index %i", |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6876 | (31<=c && c<=126) ? (char)c : '?', |
Marc-André Lemburg | 24e53b6 | 2002-09-24 09:32:14 +0000 | [diff] [blame] | 6877 | (int)c, |
Guido van Rossum | efc1188 | 2002-09-12 14:43:41 +0000 | [diff] [blame] | 6878 | (int)(fmt -1 - PyUnicode_AS_UNICODE(uformat))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6879 | goto onError; |
| 6880 | } |
| 6881 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6882 | if (*pbuf == '-' || *pbuf == '+') { |
| 6883 | sign = *pbuf++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6884 | len--; |
| 6885 | } |
| 6886 | else if (flags & F_SIGN) |
| 6887 | sign = '+'; |
| 6888 | else if (flags & F_BLANK) |
| 6889 | sign = ' '; |
| 6890 | else |
| 6891 | sign = 0; |
| 6892 | } |
| 6893 | if (width < len) |
| 6894 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 6895 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6896 | reslen -= rescnt; |
| 6897 | rescnt = width + fmtcnt + 100; |
| 6898 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 6899 | if (reslen < 0) { |
| 6900 | Py_DECREF(result); |
| 6901 | return PyErr_NoMemory(); |
| 6902 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6903 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6904 | return NULL; |
| 6905 | res = PyUnicode_AS_UNICODE(result) |
| 6906 | + reslen - rescnt; |
| 6907 | } |
| 6908 | if (sign) { |
| 6909 | if (fill != ' ') |
| 6910 | *res++ = sign; |
| 6911 | rescnt--; |
| 6912 | if (width > len) |
| 6913 | width--; |
| 6914 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6915 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 6916 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 6917 | assert(pbuf[1] == c); |
| 6918 | if (fill != ' ') { |
| 6919 | *res++ = *pbuf++; |
| 6920 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6921 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 6922 | rescnt -= 2; |
| 6923 | width -= 2; |
| 6924 | if (width < 0) |
| 6925 | width = 0; |
| 6926 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6927 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6928 | if (width > len && !(flags & F_LJUST)) { |
| 6929 | do { |
| 6930 | --rescnt; |
| 6931 | *res++ = fill; |
| 6932 | } while (--width > len); |
| 6933 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6934 | if (fill == ' ') { |
| 6935 | if (sign) |
| 6936 | *res++ = sign; |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 6937 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6938 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 6939 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6940 | *res++ = *pbuf++; |
| 6941 | *res++ = *pbuf++; |
| 6942 | } |
| 6943 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6944 | Py_UNICODE_COPY(res, pbuf, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6945 | res += len; |
| 6946 | rescnt -= len; |
| 6947 | while (--width >= len) { |
| 6948 | --rescnt; |
| 6949 | *res++ = ' '; |
| 6950 | } |
| 6951 | if (dict && (argidx < arglen) && c != '%') { |
| 6952 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 6953 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6954 | goto onError; |
| 6955 | } |
| 6956 | Py_XDECREF(temp); |
| 6957 | } /* '%' */ |
| 6958 | } /* until end */ |
| 6959 | if (argidx < arglen && !dict) { |
| 6960 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 6961 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6962 | goto onError; |
| 6963 | } |
| 6964 | |
| 6965 | if (args_owned) { |
| 6966 | Py_DECREF(args); |
| 6967 | } |
| 6968 | Py_DECREF(uformat); |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 6969 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 6970 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6971 | return (PyObject *)result; |
| 6972 | |
| 6973 | onError: |
| 6974 | Py_XDECREF(result); |
| 6975 | Py_DECREF(uformat); |
| 6976 | if (args_owned) { |
| 6977 | Py_DECREF(args); |
| 6978 | } |
| 6979 | return NULL; |
| 6980 | } |
| 6981 | |
| 6982 | static PyBufferProcs unicode_as_buffer = { |
| 6983 | (getreadbufferproc) unicode_buffer_getreadbuf, |
| 6984 | (getwritebufferproc) unicode_buffer_getwritebuf, |
| 6985 | (getsegcountproc) unicode_buffer_getsegcount, |
| 6986 | (getcharbufferproc) unicode_buffer_getcharbuf, |
| 6987 | }; |
| 6988 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 6989 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 6990 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 6991 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6992 | static PyObject * |
| 6993 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 6994 | { |
| 6995 | PyObject *x = NULL; |
| 6996 | static char *kwlist[] = {"string", "encoding", "errors", 0}; |
| 6997 | char *encoding = NULL; |
| 6998 | char *errors = NULL; |
| 6999 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7000 | if (type != &PyUnicode_Type) |
| 7001 | return unicode_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7002 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
| 7003 | kwlist, &x, &encoding, &errors)) |
| 7004 | return NULL; |
| 7005 | if (x == NULL) |
| 7006 | return (PyObject *)_PyUnicode_New(0); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 7007 | if (encoding == NULL && errors == NULL) |
| 7008 | return PyObject_Unicode(x); |
| 7009 | else |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7010 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
| 7011 | } |
| 7012 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7013 | static PyObject * |
| 7014 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 7015 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7016 | PyUnicodeObject *tmp, *pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7017 | int n; |
| 7018 | |
| 7019 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 7020 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 7021 | if (tmp == NULL) |
| 7022 | return NULL; |
| 7023 | assert(PyUnicode_Check(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7024 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7025 | if (pnew == NULL) { |
| 7026 | Py_DECREF(tmp); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7027 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7028 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7029 | pnew->str = PyMem_NEW(Py_UNICODE, n+1); |
| 7030 | if (pnew->str == NULL) { |
| 7031 | _Py_ForgetReference((PyObject *)pnew); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 7032 | PyObject_Del(pnew); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7033 | Py_DECREF(tmp); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 7034 | return PyErr_NoMemory(); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7035 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7036 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 7037 | pnew->length = n; |
| 7038 | pnew->hash = tmp->hash; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7039 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7040 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7041 | } |
| 7042 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7043 | PyDoc_STRVAR(unicode_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7044 | "unicode(string [, encoding[, errors]]) -> object\n\ |
| 7045 | \n\ |
| 7046 | Create a new Unicode object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 7047 | encoding defaults to the current default string encoding.\n\ |
| 7048 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7049 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7050 | PyTypeObject PyUnicode_Type = { |
| 7051 | PyObject_HEAD_INIT(&PyType_Type) |
| 7052 | 0, /* ob_size */ |
| 7053 | "unicode", /* tp_name */ |
| 7054 | sizeof(PyUnicodeObject), /* tp_size */ |
| 7055 | 0, /* tp_itemsize */ |
| 7056 | /* Slots */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 7057 | (destructor)unicode_dealloc, /* tp_dealloc */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7058 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7059 | 0, /* tp_getattr */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7060 | 0, /* tp_setattr */ |
| 7061 | (cmpfunc) unicode_compare, /* tp_compare */ |
| 7062 | (reprfunc) unicode_repr, /* tp_repr */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7063 | &unicode_as_number, /* tp_as_number */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7064 | &unicode_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7065 | &unicode_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7066 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 7067 | 0, /* tp_call*/ |
| 7068 | (reprfunc) unicode_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7069 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 7070 | 0, /* tp_setattro */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7071 | &unicode_as_buffer, /* tp_as_buffer */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7072 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
| 7073 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7074 | unicode_doc, /* tp_doc */ |
| 7075 | 0, /* tp_traverse */ |
| 7076 | 0, /* tp_clear */ |
| 7077 | 0, /* tp_richcompare */ |
| 7078 | 0, /* tp_weaklistoffset */ |
| 7079 | 0, /* tp_iter */ |
| 7080 | 0, /* tp_iternext */ |
| 7081 | unicode_methods, /* tp_methods */ |
| 7082 | 0, /* tp_members */ |
| 7083 | 0, /* tp_getset */ |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 7084 | &PyBaseString_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7085 | 0, /* tp_dict */ |
| 7086 | 0, /* tp_descr_get */ |
| 7087 | 0, /* tp_descr_set */ |
| 7088 | 0, /* tp_dictoffset */ |
| 7089 | 0, /* tp_init */ |
| 7090 | 0, /* tp_alloc */ |
| 7091 | unicode_new, /* tp_new */ |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 7092 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7093 | }; |
| 7094 | |
| 7095 | /* Initialize the Unicode implementation */ |
| 7096 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 7097 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7098 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7099 | int i; |
| 7100 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7101 | /* Init the implementation */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7102 | unicode_freelist = NULL; |
| 7103 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7104 | unicode_empty = _PyUnicode_New(0); |
Marc-André Lemburg | 90e8147 | 2000-06-07 09:13:21 +0000 | [diff] [blame] | 7105 | strcpy(unicode_default_encoding, "ascii"); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7106 | for (i = 0; i < 256; i++) |
| 7107 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 7108 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 7109 | Py_FatalError("Can't initialize 'unicode'"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7110 | } |
| 7111 | |
| 7112 | /* Finalize the Unicode implementation */ |
| 7113 | |
| 7114 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 7115 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7116 | { |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 7117 | PyUnicodeObject *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7118 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7119 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 7120 | Py_XDECREF(unicode_empty); |
| 7121 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 7122 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7123 | for (i = 0; i < 256; i++) { |
| 7124 | if (unicode_latin1[i]) { |
| 7125 | Py_DECREF(unicode_latin1[i]); |
| 7126 | unicode_latin1[i] = NULL; |
| 7127 | } |
| 7128 | } |
| 7129 | |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 7130 | for (u = unicode_freelist; u != NULL;) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7131 | PyUnicodeObject *v = u; |
| 7132 | u = *(PyUnicodeObject **)u; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 7133 | if (v->str) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 7134 | PyMem_DEL(v->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 7135 | Py_XDECREF(v->defenc); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 7136 | PyObject_Del(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7137 | } |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7138 | unicode_freelist = NULL; |
| 7139 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7140 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 7141 | |
| 7142 | /* |
| 7143 | Local variables: |
| 7144 | c-basic-offset: 4 |
| 7145 | indent-tabs-mode: nil |
| 7146 | End: |
| 7147 | */ |