Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1 | /* |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2 | |
| 3 | Unicode implementation based on original code by Fredrik Lundh, |
Fred Drake | 785d14f | 2000-05-09 19:54:43 +0000 | [diff] [blame] | 4 | modified by Marc-Andre Lemburg <mal@lemburg.com> according to the |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5 | Unicode Integration Proposal (see file Misc/unicode.txt). |
| 6 | |
Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 7 | Copyright (c) Corporation for National Research Initiatives. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 9 | -------------------------------------------------------------------- |
| 10 | The original string type implementation is: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 12 | Copyright (c) 1999 by Secret Labs AB |
| 13 | Copyright (c) 1999 by Fredrik Lundh |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 15 | By obtaining, using, and/or copying this software and/or its |
| 16 | associated documentation, you agree that you have read, understood, |
| 17 | and will comply with the following terms and conditions: |
| 18 | |
| 19 | Permission to use, copy, modify, and distribute this software and its |
| 20 | associated documentation for any purpose and without fee is hereby |
| 21 | granted, provided that the above copyright notice appears in all |
| 22 | copies, and that both that copyright notice and this permission notice |
| 23 | appear in supporting documentation, and that the name of Secret Labs |
| 24 | AB or the author not be used in advertising or publicity pertaining to |
| 25 | distribution of the software without specific, written prior |
| 26 | permission. |
| 27 | |
| 28 | SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 29 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 30 | FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR |
| 31 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 32 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 33 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 34 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 35 | -------------------------------------------------------------------- |
| 36 | |
| 37 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 38 | |
| 39 | #include "Python.h" |
| 40 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 41 | #include "unicodeobject.h" |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 42 | #include "ucnhash.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 43 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 44 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 45 | #include <windows.h> |
| 46 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 48 | /* Limit for the Unicode object free list */ |
| 49 | |
| 50 | #define MAX_UNICODE_FREELIST_SIZE 1024 |
| 51 | |
| 52 | /* Limit for the Unicode object free list stay alive optimization. |
| 53 | |
| 54 | The implementation will keep allocated Unicode memory intact for |
| 55 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 56 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 57 | |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 58 | At worst this will result in MAX_UNICODE_FREELIST_SIZE * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 59 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 60 | malloc()-overhead) bytes of unused garbage. |
| 61 | |
| 62 | Setting the limit to 0 effectively turns the feature off. |
| 63 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 64 | Note: This is an experimental feature ! If you get core dumps when |
| 65 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 66 | |
| 67 | */ |
| 68 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 69 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 70 | |
| 71 | /* Endianness switches; defaults to little endian */ |
| 72 | |
| 73 | #ifdef WORDS_BIGENDIAN |
| 74 | # define BYTEORDER_IS_BIG_ENDIAN |
| 75 | #else |
| 76 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 77 | #endif |
| 78 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 79 | /* --- Globals ------------------------------------------------------------ |
| 80 | |
| 81 | The globals are initialized by the _PyUnicode_Init() API and should |
| 82 | not be used before calling that API. |
| 83 | |
| 84 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 85 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 86 | /* Free list for Unicode objects */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 87 | static PyUnicodeObject *unicode_freelist; |
| 88 | static int unicode_freelist_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 89 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 90 | /* The empty Unicode object is shared to improve performance. */ |
| 91 | static PyUnicodeObject *unicode_empty; |
| 92 | |
| 93 | /* Single character Unicode strings in the Latin-1 range are being |
| 94 | shared as well. */ |
| 95 | static PyUnicodeObject *unicode_latin1[256]; |
| 96 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 97 | /* Default encoding to use and assume when NULL is passed as encoding |
| 98 | parameter; it is initialized by _PyUnicode_Init(). |
| 99 | |
| 100 | Always use the PyUnicode_SetDefaultEncoding() and |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 101 | PyUnicode_GetDefaultEncoding() APIs to access this global. |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 102 | |
| 103 | */ |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 104 | static char unicode_default_encoding[100]; |
| 105 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 106 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 107 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 108 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 109 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 110 | return 0x10FFFF; |
| 111 | #else |
| 112 | /* This is actually an illegal character, so it should |
| 113 | not be passed to unichr. */ |
| 114 | return 0xFFFF; |
| 115 | #endif |
| 116 | } |
| 117 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 118 | /* --- Unicode Object ----------------------------------------------------- */ |
| 119 | |
| 120 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 121 | int unicode_resize(register PyUnicodeObject *unicode, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 122 | int length) |
| 123 | { |
| 124 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 125 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 126 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 127 | if (unicode->length == length) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 128 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 129 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 130 | /* Resizing shared object (unicode_empty or single character |
| 131 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 132 | instead ! */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 133 | if (unicode == unicode_empty || |
| 134 | (unicode->length == 1 && |
| 135 | /* MvL said unicode->str[] may be signed. Python generally assumes |
| 136 | * an int contains at least 32 bits, and we don't use more than |
| 137 | * 32 bits even in a UCS4 build, so casting to unsigned int should |
| 138 | * be correct. |
| 139 | */ |
| 140 | (unsigned int)unicode->str[0] < 256U && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 141 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 142 | PyErr_SetString(PyExc_SystemError, |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 143 | "can't resize shared unicode objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 144 | return -1; |
| 145 | } |
| 146 | |
| 147 | /* We allocate one more byte to make sure the string is |
| 148 | Ux0000 terminated -- XXX is this needed ? */ |
| 149 | oldstr = unicode->str; |
| 150 | PyMem_RESIZE(unicode->str, Py_UNICODE, length + 1); |
| 151 | if (!unicode->str) { |
| 152 | unicode->str = oldstr; |
| 153 | PyErr_NoMemory(); |
| 154 | return -1; |
| 155 | } |
| 156 | unicode->str[length] = 0; |
| 157 | unicode->length = length; |
| 158 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 159 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 160 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 161 | if (unicode->defenc) { |
| 162 | Py_DECREF(unicode->defenc); |
| 163 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 164 | } |
| 165 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 166 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | /* We allocate one more byte to make sure the string is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 171 | Ux0000 terminated -- XXX is this needed ? |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 172 | |
| 173 | XXX This allocator could further be enhanced by assuring that the |
| 174 | free list never reduces its size below 1. |
| 175 | |
| 176 | */ |
| 177 | |
| 178 | static |
| 179 | PyUnicodeObject *_PyUnicode_New(int length) |
| 180 | { |
| 181 | register PyUnicodeObject *unicode; |
| 182 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 183 | /* Optimization fo empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 184 | if (length == 0 && unicode_empty != NULL) { |
| 185 | Py_INCREF(unicode_empty); |
| 186 | return unicode_empty; |
| 187 | } |
| 188 | |
| 189 | /* Unicode freelist & memory allocation */ |
| 190 | if (unicode_freelist) { |
| 191 | unicode = unicode_freelist; |
Marc-André Lemburg | bea47e7 | 2000-06-17 20:31:17 +0000 | [diff] [blame] | 192 | unicode_freelist = *(PyUnicodeObject **)unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 193 | unicode_freelist_size--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 194 | if (unicode->str) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 195 | /* Keep-Alive optimization: we only upsize the buffer, |
| 196 | never downsize it. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 197 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 198 | unicode_resize(unicode, length) < 0) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 199 | PyMem_DEL(unicode->str); |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 200 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 203 | else { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 204 | unicode->str = PyMem_NEW(Py_UNICODE, length + 1); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 205 | } |
| 206 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 207 | } |
| 208 | else { |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 209 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 210 | if (unicode == NULL) |
| 211 | return NULL; |
| 212 | unicode->str = PyMem_NEW(Py_UNICODE, length + 1); |
| 213 | } |
| 214 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 215 | if (!unicode->str) { |
| 216 | PyErr_NoMemory(); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 217 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 218 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 219 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 220 | * the caller fails before initializing str -- unicode_resize() |
| 221 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 222 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 223 | * We don't want unicode_resize to read uninitialized memory in |
| 224 | * that case. |
| 225 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 226 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 227 | unicode->str[length] = 0; |
| 228 | unicode->length = length; |
| 229 | unicode->hash = -1; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 230 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 231 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 232 | |
| 233 | onError: |
| 234 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 235 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 236 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 240 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 241 | { |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 242 | if (PyUnicode_CheckExact(unicode) && |
| 243 | unicode_freelist_size < MAX_UNICODE_FREELIST_SIZE) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 244 | /* Keep-Alive optimization */ |
| 245 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 246 | PyMem_DEL(unicode->str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 247 | unicode->str = NULL; |
| 248 | unicode->length = 0; |
| 249 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 250 | if (unicode->defenc) { |
| 251 | Py_DECREF(unicode->defenc); |
| 252 | unicode->defenc = NULL; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 253 | } |
| 254 | /* Add to free list */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 255 | *(PyUnicodeObject **)unicode = unicode_freelist; |
| 256 | unicode_freelist = unicode; |
| 257 | unicode_freelist_size++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 258 | } |
| 259 | else { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 260 | PyMem_DEL(unicode->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 261 | Py_XDECREF(unicode->defenc); |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 262 | unicode->ob_type->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 266 | int PyUnicode_Resize(PyObject **unicode, int length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 267 | { |
| 268 | register PyUnicodeObject *v; |
| 269 | |
| 270 | /* Argument checks */ |
| 271 | if (unicode == NULL) { |
| 272 | PyErr_BadInternalCall(); |
| 273 | return -1; |
| 274 | } |
| 275 | v = (PyUnicodeObject *)*unicode; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 276 | 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] | 277 | PyErr_BadInternalCall(); |
| 278 | return -1; |
| 279 | } |
| 280 | |
| 281 | /* Resizing unicode_empty and single character objects is not |
| 282 | possible since these are being shared. We simply return a fresh |
| 283 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 284 | if (v->length != length && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 285 | (v == unicode_empty || v->length == 1)) { |
| 286 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 287 | if (w == NULL) |
| 288 | return -1; |
| 289 | Py_UNICODE_COPY(w->str, v->str, |
| 290 | length < v->length ? length : v->length); |
Raymond Hettinger | c8df578 | 2003-03-09 07:30:43 +0000 | [diff] [blame] | 291 | Py_DECREF(*unicode); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 292 | *unicode = (PyObject *)w; |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 297 | objects, since we can modify them in-place. */ |
| 298 | return unicode_resize(v, length); |
| 299 | } |
| 300 | |
| 301 | /* Internal API for use in unicodeobject.c only ! */ |
| 302 | #define _PyUnicode_Resize(unicodevar, length) \ |
| 303 | PyUnicode_Resize(((PyObject **)(unicodevar)), length) |
| 304 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 305 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
| 306 | int size) |
| 307 | { |
| 308 | PyUnicodeObject *unicode; |
| 309 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 310 | /* If the Unicode data is known at construction time, we can apply |
| 311 | some optimizations which share commonly used objects. */ |
| 312 | if (u != NULL) { |
| 313 | |
| 314 | /* Optimization for empty strings */ |
| 315 | if (size == 0 && unicode_empty != NULL) { |
| 316 | Py_INCREF(unicode_empty); |
| 317 | return (PyObject *)unicode_empty; |
| 318 | } |
| 319 | |
| 320 | /* Single character Unicode objects in the Latin-1 range are |
| 321 | shared when using this constructor */ |
| 322 | if (size == 1 && *u < 256) { |
| 323 | unicode = unicode_latin1[*u]; |
| 324 | if (!unicode) { |
| 325 | unicode = _PyUnicode_New(1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 326 | if (!unicode) |
| 327 | return NULL; |
Marc-André Lemburg | 8879a33 | 2001-06-07 12:26:56 +0000 | [diff] [blame] | 328 | unicode->str[0] = *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 329 | unicode_latin1[*u] = unicode; |
| 330 | } |
| 331 | Py_INCREF(unicode); |
| 332 | return (PyObject *)unicode; |
| 333 | } |
| 334 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 335 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 336 | unicode = _PyUnicode_New(size); |
| 337 | if (!unicode) |
| 338 | return NULL; |
| 339 | |
| 340 | /* Copy the Unicode data into the new object */ |
| 341 | if (u != NULL) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 342 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 343 | |
| 344 | return (PyObject *)unicode; |
| 345 | } |
| 346 | |
| 347 | #ifdef HAVE_WCHAR_H |
| 348 | |
| 349 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
| 350 | int size) |
| 351 | { |
| 352 | PyUnicodeObject *unicode; |
| 353 | |
| 354 | if (w == NULL) { |
| 355 | PyErr_BadInternalCall(); |
| 356 | return NULL; |
| 357 | } |
| 358 | |
| 359 | unicode = _PyUnicode_New(size); |
| 360 | if (!unicode) |
| 361 | return NULL; |
| 362 | |
| 363 | /* Copy the wchar_t data into the new object */ |
| 364 | #ifdef HAVE_USABLE_WCHAR_T |
| 365 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 366 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 367 | { |
| 368 | register Py_UNICODE *u; |
| 369 | register int i; |
| 370 | u = PyUnicode_AS_UNICODE(unicode); |
| 371 | for (i = size; i >= 0; i--) |
| 372 | *u++ = *w++; |
| 373 | } |
| 374 | #endif |
| 375 | |
| 376 | return (PyObject *)unicode; |
| 377 | } |
| 378 | |
| 379 | int PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 380 | register wchar_t *w, |
| 381 | int size) |
| 382 | { |
| 383 | if (unicode == NULL) { |
| 384 | PyErr_BadInternalCall(); |
| 385 | return -1; |
| 386 | } |
| 387 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 388 | size = PyUnicode_GET_SIZE(unicode); |
| 389 | #ifdef HAVE_USABLE_WCHAR_T |
| 390 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 391 | #else |
| 392 | { |
| 393 | register Py_UNICODE *u; |
| 394 | register int i; |
| 395 | u = PyUnicode_AS_UNICODE(unicode); |
| 396 | for (i = size; i >= 0; i--) |
| 397 | *w++ = *u++; |
| 398 | } |
| 399 | #endif |
| 400 | |
| 401 | return size; |
| 402 | } |
| 403 | |
| 404 | #endif |
| 405 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 406 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 407 | { |
| 408 | Py_UNICODE s[2]; |
| 409 | |
| 410 | #ifdef Py_UNICODE_WIDE |
| 411 | if (ordinal < 0 || ordinal > 0x10ffff) { |
| 412 | PyErr_SetString(PyExc_ValueError, |
| 413 | "unichr() arg not in range(0x110000) " |
| 414 | "(wide Python build)"); |
| 415 | return NULL; |
| 416 | } |
| 417 | #else |
| 418 | if (ordinal < 0 || ordinal > 0xffff) { |
| 419 | PyErr_SetString(PyExc_ValueError, |
| 420 | "unichr() arg not in range(0x10000) " |
| 421 | "(narrow Python build)"); |
| 422 | return NULL; |
| 423 | } |
| 424 | #endif |
| 425 | |
| 426 | if (ordinal <= 0xffff) { |
| 427 | /* UCS-2 character */ |
| 428 | s[0] = (Py_UNICODE) ordinal; |
| 429 | return PyUnicode_FromUnicode(s, 1); |
| 430 | } |
| 431 | else { |
| 432 | #ifndef Py_UNICODE_WIDE |
| 433 | /* UCS-4 character. store as two surrogate characters */ |
| 434 | ordinal -= 0x10000L; |
| 435 | s[0] = 0xD800 + (Py_UNICODE) (ordinal >> 10); |
| 436 | s[1] = 0xDC00 + (Py_UNICODE) (ordinal & 0x03FF); |
| 437 | return PyUnicode_FromUnicode(s, 2); |
| 438 | #else |
| 439 | s[0] = (Py_UNICODE)ordinal; |
| 440 | return PyUnicode_FromUnicode(s, 1); |
| 441 | #endif |
| 442 | } |
| 443 | } |
| 444 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 445 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 446 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 447 | /* XXX Perhaps we should make this API an alias of |
| 448 | PyObject_Unicode() instead ?! */ |
| 449 | if (PyUnicode_CheckExact(obj)) { |
| 450 | Py_INCREF(obj); |
| 451 | return obj; |
| 452 | } |
| 453 | if (PyUnicode_Check(obj)) { |
| 454 | /* For a Unicode subtype that's not a Unicode object, |
| 455 | return a true Unicode object with the same data. */ |
| 456 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 457 | PyUnicode_GET_SIZE(obj)); |
| 458 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 459 | return PyUnicode_FromEncodedObject(obj, NULL, "strict"); |
| 460 | } |
| 461 | |
| 462 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
| 463 | const char *encoding, |
| 464 | const char *errors) |
| 465 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 466 | const char *s = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 467 | int len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 468 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 469 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 470 | if (obj == NULL) { |
| 471 | PyErr_BadInternalCall(); |
| 472 | return NULL; |
| 473 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 474 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 475 | #if 0 |
| 476 | /* For b/w compatibility we also accept Unicode objects provided |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 477 | that no encodings is given and then redirect to |
| 478 | PyObject_Unicode() which then applies the additional logic for |
| 479 | Unicode subclasses. |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 480 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 481 | NOTE: This API should really only be used for object which |
| 482 | represent *encoded* Unicode ! |
| 483 | |
| 484 | */ |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 485 | if (PyUnicode_Check(obj)) { |
| 486 | if (encoding) { |
| 487 | PyErr_SetString(PyExc_TypeError, |
| 488 | "decoding Unicode is not supported"); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 489 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 490 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 491 | return PyObject_Unicode(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 492 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 493 | #else |
| 494 | if (PyUnicode_Check(obj)) { |
| 495 | PyErr_SetString(PyExc_TypeError, |
| 496 | "decoding Unicode is not supported"); |
| 497 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 498 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 499 | #endif |
| 500 | |
| 501 | /* Coerce object */ |
| 502 | if (PyString_Check(obj)) { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 503 | s = PyString_AS_STRING(obj); |
| 504 | len = PyString_GET_SIZE(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 505 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 506 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
| 507 | /* Overwrite the error message with something more useful in |
| 508 | case of a TypeError. */ |
| 509 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 510 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 511 | "coercing to Unicode: need string or buffer, " |
| 512 | "%.80s found", |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 513 | obj->ob_type->tp_name); |
| 514 | goto onError; |
| 515 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 516 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 517 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 518 | if (len == 0) { |
| 519 | Py_INCREF(unicode_empty); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 520 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 521 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 522 | else |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 523 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 524 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 525 | return v; |
| 526 | |
| 527 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 528 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | PyObject *PyUnicode_Decode(const char *s, |
| 532 | int size, |
| 533 | const char *encoding, |
| 534 | const char *errors) |
| 535 | { |
| 536 | PyObject *buffer = NULL, *unicode; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 537 | |
| 538 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 539 | encoding = PyUnicode_GetDefaultEncoding(); |
| 540 | |
| 541 | /* Shortcuts for common default encodings */ |
| 542 | if (strcmp(encoding, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 543 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 544 | else if (strcmp(encoding, "latin-1") == 0) |
| 545 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 546 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 547 | else if (strcmp(encoding, "mbcs") == 0) |
| 548 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 549 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 550 | else if (strcmp(encoding, "ascii") == 0) |
| 551 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 552 | |
| 553 | /* Decode via the codec registry */ |
| 554 | buffer = PyBuffer_FromMemory((void *)s, size); |
| 555 | if (buffer == NULL) |
| 556 | goto onError; |
| 557 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 558 | if (unicode == NULL) |
| 559 | goto onError; |
| 560 | if (!PyUnicode_Check(unicode)) { |
| 561 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 562 | "decoder did not return an unicode object (type=%.400s)", |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 563 | unicode->ob_type->tp_name); |
| 564 | Py_DECREF(unicode); |
| 565 | goto onError; |
| 566 | } |
| 567 | Py_DECREF(buffer); |
| 568 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 569 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 570 | onError: |
| 571 | Py_XDECREF(buffer); |
| 572 | return NULL; |
| 573 | } |
| 574 | |
| 575 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
| 576 | int size, |
| 577 | const char *encoding, |
| 578 | const char *errors) |
| 579 | { |
| 580 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 581 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 582 | unicode = PyUnicode_FromUnicode(s, size); |
| 583 | if (unicode == NULL) |
| 584 | return NULL; |
| 585 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 586 | Py_DECREF(unicode); |
| 587 | return v; |
| 588 | } |
| 589 | |
| 590 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 591 | const char *encoding, |
| 592 | const char *errors) |
| 593 | { |
| 594 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 595 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 596 | if (!PyUnicode_Check(unicode)) { |
| 597 | PyErr_BadArgument(); |
| 598 | goto onError; |
| 599 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 600 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 601 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 602 | encoding = PyUnicode_GetDefaultEncoding(); |
| 603 | |
| 604 | /* Shortcuts for common default encodings */ |
| 605 | if (errors == NULL) { |
| 606 | if (strcmp(encoding, "utf-8") == 0) |
Jeremy Hylton | 9cea41c | 2001-05-29 17:13:15 +0000 | [diff] [blame] | 607 | return PyUnicode_AsUTF8String(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 608 | else if (strcmp(encoding, "latin-1") == 0) |
| 609 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 610 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 611 | else if (strcmp(encoding, "mbcs") == 0) |
| 612 | return PyUnicode_AsMBCSString(unicode); |
| 613 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 614 | else if (strcmp(encoding, "ascii") == 0) |
| 615 | return PyUnicode_AsASCIIString(unicode); |
| 616 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 617 | |
| 618 | /* Encode via the codec registry */ |
| 619 | v = PyCodec_Encode(unicode, encoding, errors); |
| 620 | if (v == NULL) |
| 621 | goto onError; |
| 622 | /* XXX Should we really enforce this ? */ |
| 623 | if (!PyString_Check(v)) { |
| 624 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 625 | "encoder did not return a string object (type=%.400s)", |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 626 | v->ob_type->tp_name); |
| 627 | Py_DECREF(v); |
| 628 | goto onError; |
| 629 | } |
| 630 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 631 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 632 | onError: |
| 633 | return NULL; |
| 634 | } |
| 635 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 636 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 637 | const char *errors) |
| 638 | { |
| 639 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
| 640 | |
| 641 | if (v) |
| 642 | return v; |
| 643 | v = PyUnicode_AsEncodedString(unicode, NULL, errors); |
| 644 | if (v && errors == NULL) |
| 645 | ((PyUnicodeObject *)unicode)->defenc = v; |
| 646 | return v; |
| 647 | } |
| 648 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 649 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 650 | { |
| 651 | if (!PyUnicode_Check(unicode)) { |
| 652 | PyErr_BadArgument(); |
| 653 | goto onError; |
| 654 | } |
| 655 | return PyUnicode_AS_UNICODE(unicode); |
| 656 | |
| 657 | onError: |
| 658 | return NULL; |
| 659 | } |
| 660 | |
| 661 | int PyUnicode_GetSize(PyObject *unicode) |
| 662 | { |
| 663 | if (!PyUnicode_Check(unicode)) { |
| 664 | PyErr_BadArgument(); |
| 665 | goto onError; |
| 666 | } |
| 667 | return PyUnicode_GET_SIZE(unicode); |
| 668 | |
| 669 | onError: |
| 670 | return -1; |
| 671 | } |
| 672 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 673 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 674 | { |
| 675 | return unicode_default_encoding; |
| 676 | } |
| 677 | |
| 678 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 679 | { |
| 680 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 681 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 682 | /* Make sure the encoding is valid. As side effect, this also |
| 683 | loads the encoding into the codec registry cache. */ |
| 684 | v = _PyCodec_Lookup(encoding); |
| 685 | if (v == NULL) |
| 686 | goto onError; |
| 687 | Py_DECREF(v); |
| 688 | strncpy(unicode_default_encoding, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 689 | encoding, |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 690 | sizeof(unicode_default_encoding)); |
| 691 | return 0; |
| 692 | |
| 693 | onError: |
| 694 | return -1; |
| 695 | } |
| 696 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 697 | /* error handling callback helper: |
| 698 | build arguments, call the callback and check the arguments, |
| 699 | if no exception occured, copy the replacement to the output |
| 700 | and adjust various state variables. |
| 701 | return 0 on success, -1 on error |
| 702 | */ |
| 703 | |
| 704 | static |
| 705 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 706 | const char *encoding, const char *reason, |
| 707 | const char *input, int insize, int *startinpos, int *endinpos, PyObject **exceptionObject, const char **inptr, |
| 708 | PyObject **output, int *outpos, Py_UNICODE **outptr) |
| 709 | { |
| 710 | static char *argparse = "O!i;decoding error handler must return (unicode, int) tuple"; |
| 711 | |
| 712 | PyObject *restuple = NULL; |
| 713 | PyObject *repunicode = NULL; |
| 714 | int outsize = PyUnicode_GET_SIZE(*output); |
| 715 | int requiredsize; |
| 716 | int newpos; |
| 717 | Py_UNICODE *repptr; |
| 718 | int repsize; |
| 719 | int res = -1; |
| 720 | |
| 721 | if (*errorHandler == NULL) { |
| 722 | *errorHandler = PyCodec_LookupError(errors); |
| 723 | if (*errorHandler == NULL) |
| 724 | goto onError; |
| 725 | } |
| 726 | |
| 727 | if (*exceptionObject == NULL) { |
| 728 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 729 | encoding, input, insize, *startinpos, *endinpos, reason); |
| 730 | if (*exceptionObject == NULL) |
| 731 | goto onError; |
| 732 | } |
| 733 | else { |
| 734 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 735 | goto onError; |
| 736 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 737 | goto onError; |
| 738 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 739 | goto onError; |
| 740 | } |
| 741 | |
| 742 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 743 | if (restuple == NULL) |
| 744 | goto onError; |
| 745 | if (!PyTuple_Check(restuple)) { |
| 746 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 747 | goto onError; |
| 748 | } |
| 749 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 750 | goto onError; |
| 751 | if (newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 752 | newpos = insize+newpos; |
| 753 | if (newpos<0 || newpos>insize) { |
| 754 | PyErr_Format(PyExc_IndexError, "position %d from error handler out of bounds", newpos); |
| 755 | goto onError; |
| 756 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 757 | |
| 758 | /* need more space? (at least enough for what we |
| 759 | have+the replacement+the rest of the string (starting |
| 760 | at the new input position), so we won't have to check space |
| 761 | when there are no errors in the rest of the string) */ |
| 762 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 763 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 764 | requiredsize = *outpos + repsize + insize-newpos; |
| 765 | if (requiredsize > outsize) { |
| 766 | if (requiredsize<2*outsize) |
| 767 | requiredsize = 2*outsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 768 | if (PyUnicode_Resize(output, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 769 | goto onError; |
| 770 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
| 771 | } |
| 772 | *endinpos = newpos; |
| 773 | *inptr = input + newpos; |
| 774 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 775 | *outptr += repsize; |
| 776 | *outpos += repsize; |
| 777 | /* we made it! */ |
| 778 | res = 0; |
| 779 | |
| 780 | onError: |
| 781 | Py_XDECREF(restuple); |
| 782 | return res; |
| 783 | } |
| 784 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 785 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 786 | |
| 787 | /* see RFC2152 for details */ |
| 788 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 789 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 790 | char utf7_special[128] = { |
| 791 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 792 | encoded: |
| 793 | 0 - not special |
| 794 | 1 - special |
| 795 | 2 - whitespace (optional) |
| 796 | 3 - RFC2152 Set O (optional) */ |
| 797 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 798 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 799 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 800 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 801 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 802 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 803 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 804 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 805 | |
| 806 | }; |
| 807 | |
| 808 | #define SPECIAL(c, encodeO, encodeWS) \ |
| 809 | (((c)>127 || utf7_special[(c)] == 1) || \ |
| 810 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
| 811 | (encodeO && (utf7_special[(c)] == 3))) |
| 812 | |
| 813 | #define B64(n) ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 814 | #define B64CHAR(c) (isalnum(c) || (c) == '+' || (c) == '/') |
| 815 | #define UB64(c) ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
| 816 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4) |
| 817 | |
| 818 | #define ENCODE(out, ch, bits) \ |
| 819 | while (bits >= 6) { \ |
| 820 | *out++ = B64(ch >> (bits-6)); \ |
| 821 | bits -= 6; \ |
| 822 | } |
| 823 | |
| 824 | #define DECODE(out, ch, bits, surrogate) \ |
| 825 | while (bits >= 16) { \ |
| 826 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 827 | bits -= 16; \ |
| 828 | if (surrogate) { \ |
| 829 | /* We have already generated an error for the high surrogate |
| 830 | so let's not bother seeing if the low surrogate is correct or not */\ |
| 831 | surrogate = 0; \ |
| 832 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
| 833 | /* This is a surrogate pair. Unfortunately we can't represent \ |
| 834 | it in a 16-bit character */ \ |
| 835 | surrogate = 1; \ |
| 836 | errmsg = "code pairs are not supported"; \ |
| 837 | goto utf7Error; \ |
| 838 | } else { \ |
| 839 | *out++ = outCh; \ |
| 840 | } \ |
| 841 | } \ |
| 842 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 843 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
| 844 | int size, |
| 845 | const char *errors) |
| 846 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 847 | const char *starts = s; |
| 848 | int startinpos; |
| 849 | int endinpos; |
| 850 | int outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 851 | const char *e; |
| 852 | PyUnicodeObject *unicode; |
| 853 | Py_UNICODE *p; |
| 854 | const char *errmsg = ""; |
| 855 | int inShift = 0; |
| 856 | unsigned int bitsleft = 0; |
| 857 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 858 | int surrogate = 0; |
| 859 | PyObject *errorHandler = NULL; |
| 860 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 861 | |
| 862 | unicode = _PyUnicode_New(size); |
| 863 | if (!unicode) |
| 864 | return NULL; |
| 865 | if (size == 0) |
| 866 | return (PyObject *)unicode; |
| 867 | |
| 868 | p = unicode->str; |
| 869 | e = s + size; |
| 870 | |
| 871 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 872 | Py_UNICODE ch; |
| 873 | restart: |
| 874 | ch = *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 875 | |
| 876 | if (inShift) { |
| 877 | if ((ch == '-') || !B64CHAR(ch)) { |
| 878 | inShift = 0; |
| 879 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 880 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 881 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 882 | if (bitsleft >= 6) { |
| 883 | /* The shift sequence has a partial character in it. If |
| 884 | bitsleft < 6 then we could just classify it as padding |
| 885 | but that is not the case here */ |
| 886 | |
| 887 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 888 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 889 | } |
| 890 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 891 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 892 | here so indicate the potential of a misencoded character. */ |
| 893 | |
| 894 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 895 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 896 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 897 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | if (ch == '-') { |
| 901 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 902 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 903 | inShift = 1; |
| 904 | } |
| 905 | } else if (SPECIAL(ch,0,0)) { |
| 906 | errmsg = "unexpected special character"; |
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 | } else { |
| 909 | *p++ = ch; |
| 910 | } |
| 911 | } else { |
| 912 | charsleft = (charsleft << 6) | UB64(ch); |
| 913 | bitsleft += 6; |
| 914 | s++; |
| 915 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 916 | } |
| 917 | } |
| 918 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 919 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 920 | s++; |
| 921 | if (s < e && *s == '-') { |
| 922 | s++; |
| 923 | *p++ = '+'; |
| 924 | } else |
| 925 | { |
| 926 | inShift = 1; |
| 927 | bitsleft = 0; |
| 928 | } |
| 929 | } |
| 930 | else if (SPECIAL(ch,0,0)) { |
| 931 | errmsg = "unexpected special character"; |
| 932 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 933 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 934 | } |
| 935 | else { |
| 936 | *p++ = ch; |
| 937 | s++; |
| 938 | } |
| 939 | continue; |
| 940 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 941 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 942 | endinpos = s-starts; |
| 943 | if (unicode_decode_call_errorhandler( |
| 944 | errors, &errorHandler, |
| 945 | "utf7", errmsg, |
| 946 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 947 | (PyObject **)&unicode, &outpos, &p)) |
| 948 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | if (inShift) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 952 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 953 | endinpos = size; |
| 954 | if (unicode_decode_call_errorhandler( |
| 955 | errors, &errorHandler, |
| 956 | "utf7", "unterminated shift sequence", |
| 957 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 958 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 959 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 960 | if (s < e) |
| 961 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 964 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 965 | goto onError; |
| 966 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 967 | Py_XDECREF(errorHandler); |
| 968 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 969 | return (PyObject *)unicode; |
| 970 | |
| 971 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 972 | Py_XDECREF(errorHandler); |
| 973 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 974 | Py_DECREF(unicode); |
| 975 | return NULL; |
| 976 | } |
| 977 | |
| 978 | |
| 979 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 980 | int size, |
| 981 | int encodeSetO, |
| 982 | int encodeWhiteSpace, |
| 983 | const char *errors) |
| 984 | { |
| 985 | PyObject *v; |
| 986 | /* It might be possible to tighten this worst case */ |
| 987 | unsigned int cbAllocated = 5 * size; |
| 988 | int inShift = 0; |
| 989 | int i = 0; |
| 990 | unsigned int bitsleft = 0; |
| 991 | unsigned long charsleft = 0; |
| 992 | char * out; |
| 993 | char * start; |
| 994 | |
| 995 | if (size == 0) |
| 996 | return PyString_FromStringAndSize(NULL, 0); |
| 997 | |
| 998 | v = PyString_FromStringAndSize(NULL, cbAllocated); |
| 999 | if (v == NULL) |
| 1000 | return NULL; |
| 1001 | |
| 1002 | start = out = PyString_AS_STRING(v); |
| 1003 | for (;i < size; ++i) { |
| 1004 | Py_UNICODE ch = s[i]; |
| 1005 | |
| 1006 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1007 | if (ch == '+') { |
| 1008 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1009 | *out++ = '-'; |
| 1010 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1011 | charsleft = ch; |
| 1012 | bitsleft = 16; |
| 1013 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1014 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1015 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1016 | } else { |
| 1017 | *out++ = (char) ch; |
| 1018 | } |
| 1019 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1020 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1021 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1022 | charsleft = 0; |
| 1023 | bitsleft = 0; |
| 1024 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1025 | so no '-' is required, except if the character is itself a '-' */ |
| 1026 | if (B64CHAR(ch) || ch == '-') { |
| 1027 | *out++ = '-'; |
| 1028 | } |
| 1029 | inShift = 0; |
| 1030 | *out++ = (char) ch; |
| 1031 | } else { |
| 1032 | bitsleft += 16; |
| 1033 | charsleft = (charsleft << 16) | ch; |
| 1034 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1035 | |
| 1036 | /* If the next character is special then we dont' need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1037 | 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] | 1038 | or '-' then the shift sequence will be terminated implicitly and we |
| 1039 | don't have to insert a '-'. */ |
| 1040 | |
| 1041 | if (bitsleft == 0) { |
| 1042 | if (i + 1 < size) { |
| 1043 | Py_UNICODE ch2 = s[i+1]; |
| 1044 | |
| 1045 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1046 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1047 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1048 | *out++ = '-'; |
| 1049 | inShift = 0; |
| 1050 | } else { |
| 1051 | inShift = 0; |
| 1052 | } |
| 1053 | |
| 1054 | } |
| 1055 | else { |
| 1056 | *out++ = '-'; |
| 1057 | inShift = 0; |
| 1058 | } |
| 1059 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1060 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1061 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1062 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1063 | if (bitsleft) { |
| 1064 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1065 | *out++ = '-'; |
| 1066 | } |
| 1067 | |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 1068 | _PyString_Resize(&v, out - start); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1069 | return v; |
| 1070 | } |
| 1071 | |
| 1072 | #undef SPECIAL |
| 1073 | #undef B64 |
| 1074 | #undef B64CHAR |
| 1075 | #undef UB64 |
| 1076 | #undef ENCODE |
| 1077 | #undef DECODE |
| 1078 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1079 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1080 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1081 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1082 | char utf8_code_length[256] = { |
| 1083 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1084 | illegal prefix. see RFC 2279 for details */ |
| 1085 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1086 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1087 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1088 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1089 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1090 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1091 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1092 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1093 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1094 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1095 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1096 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1097 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1098 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1099 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1100 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1101 | }; |
| 1102 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1103 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
| 1104 | int size, |
| 1105 | const char *errors) |
| 1106 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1107 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1108 | int n; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1109 | int startinpos; |
| 1110 | int endinpos; |
| 1111 | int outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1112 | const char *e; |
| 1113 | PyUnicodeObject *unicode; |
| 1114 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1115 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1116 | PyObject *errorHandler = NULL; |
| 1117 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1118 | |
| 1119 | /* Note: size will always be longer than the resulting Unicode |
| 1120 | character count */ |
| 1121 | unicode = _PyUnicode_New(size); |
| 1122 | if (!unicode) |
| 1123 | return NULL; |
| 1124 | if (size == 0) |
| 1125 | return (PyObject *)unicode; |
| 1126 | |
| 1127 | /* Unpack UTF-8 encoded data */ |
| 1128 | p = unicode->str; |
| 1129 | e = s + size; |
| 1130 | |
| 1131 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1132 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1133 | |
| 1134 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1135 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1136 | s++; |
| 1137 | continue; |
| 1138 | } |
| 1139 | |
| 1140 | n = utf8_code_length[ch]; |
| 1141 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1142 | if (s + n > e) { |
| 1143 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1144 | startinpos = s-starts; |
| 1145 | endinpos = size; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1146 | goto utf8Error; |
| 1147 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1148 | |
| 1149 | switch (n) { |
| 1150 | |
| 1151 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1152 | errmsg = "unexpected code byte"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1153 | startinpos = s-starts; |
| 1154 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1155 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1156 | |
| 1157 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1158 | errmsg = "internal error"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1159 | startinpos = s-starts; |
| 1160 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1161 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1162 | |
| 1163 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1164 | if ((s[1] & 0xc0) != 0x80) { |
| 1165 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1166 | startinpos = s-starts; |
| 1167 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1168 | goto utf8Error; |
| 1169 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1170 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1171 | if (ch < 0x80) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1172 | startinpos = s-starts; |
| 1173 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1174 | errmsg = "illegal encoding"; |
| 1175 | goto utf8Error; |
| 1176 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1177 | else |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1178 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1179 | break; |
| 1180 | |
| 1181 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1182 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1183 | (s[2] & 0xc0) != 0x80) { |
| 1184 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1185 | startinpos = s-starts; |
| 1186 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1187 | goto utf8Error; |
| 1188 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1189 | 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] | 1190 | if (ch < 0x0800) { |
| 1191 | /* Note: UTF-8 encodings of surrogates are considered |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1192 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1193 | |
| 1194 | XXX For wide builds (UCS-4) we should probably try |
| 1195 | to recombine the surrogates into a single code |
| 1196 | unit. |
| 1197 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1198 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1199 | startinpos = s-starts; |
| 1200 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1201 | goto utf8Error; |
| 1202 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1203 | else |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1204 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1205 | break; |
| 1206 | |
| 1207 | case 4: |
| 1208 | if ((s[1] & 0xc0) != 0x80 || |
| 1209 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1210 | (s[3] & 0xc0) != 0x80) { |
| 1211 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1212 | startinpos = s-starts; |
| 1213 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1214 | goto utf8Error; |
| 1215 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1216 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 1217 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 1218 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1219 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1220 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1221 | || (ch > 0x10ffff)) /* maximum value allowed for |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1222 | UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1223 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1224 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1225 | startinpos = s-starts; |
| 1226 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1227 | goto utf8Error; |
| 1228 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1229 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1230 | *p++ = (Py_UNICODE)ch; |
| 1231 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1232 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1233 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1234 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 1235 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1236 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1237 | /* high surrogate = top 10 bits added to D800 */ |
| 1238 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1239 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1240 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1241 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1242 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1243 | break; |
| 1244 | |
| 1245 | default: |
| 1246 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1247 | errmsg = "unsupported Unicode code range"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1248 | startinpos = s-starts; |
| 1249 | endinpos = startinpos+n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1250 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1251 | } |
| 1252 | s += n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1253 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1254 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1255 | utf8Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1256 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1257 | if (unicode_decode_call_errorhandler( |
| 1258 | errors, &errorHandler, |
| 1259 | "utf8", errmsg, |
| 1260 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1261 | (PyObject **)&unicode, &outpos, &p)) |
| 1262 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1266 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1267 | goto onError; |
| 1268 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1269 | Py_XDECREF(errorHandler); |
| 1270 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1271 | return (PyObject *)unicode; |
| 1272 | |
| 1273 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1274 | Py_XDECREF(errorHandler); |
| 1275 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1276 | Py_DECREF(unicode); |
| 1277 | return NULL; |
| 1278 | } |
| 1279 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1280 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 1281 | and allocate exactly as much space needed at the end. Else allocate the |
| 1282 | maximum possible needed (4 result bytes per Unicode character), and return |
| 1283 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1284 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1285 | PyObject * |
| 1286 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 1287 | int size, |
| 1288 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1289 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1290 | #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] | 1291 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1292 | int i; /* index into s of next input byte */ |
| 1293 | PyObject *v; /* result string object */ |
| 1294 | char *p; /* next free byte in output buffer */ |
| 1295 | int nallocated; /* number of result bytes allocated */ |
| 1296 | int nneeded; /* number of result bytes needed */ |
| 1297 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1298 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1299 | assert(s != NULL); |
| 1300 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1301 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1302 | if (size <= MAX_SHORT_UNICHARS) { |
| 1303 | /* Write into the stack buffer; nallocated can't overflow. |
| 1304 | * At the end, we'll allocate exactly as much heap space as it |
| 1305 | * turns out we need. |
| 1306 | */ |
| 1307 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
| 1308 | v = NULL; /* will allocate after we're done */ |
| 1309 | p = stackbuf; |
| 1310 | } |
| 1311 | else { |
| 1312 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 1313 | nallocated = size * 4; |
| 1314 | if (nallocated / 4 != size) /* overflow! */ |
| 1315 | return PyErr_NoMemory(); |
| 1316 | v = PyString_FromStringAndSize(NULL, nallocated); |
| 1317 | if (v == NULL) |
| 1318 | return NULL; |
| 1319 | p = PyString_AS_STRING(v); |
| 1320 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1321 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1322 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1323 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1324 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1325 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1326 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1327 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1328 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1329 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1330 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 1331 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 1332 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1333 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1334 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1335 | /* Encode UCS2 Unicode ordinals */ |
| 1336 | if (ch < 0x10000) { |
| 1337 | /* Special case: check for high surrogate */ |
| 1338 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 1339 | Py_UCS4 ch2 = s[i]; |
| 1340 | /* Check for low surrogate and combine the two to |
| 1341 | form a UCS4 value */ |
| 1342 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1343 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1344 | i++; |
| 1345 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1346 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1347 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1348 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1349 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1350 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1351 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1352 | continue; |
| 1353 | } |
| 1354 | encodeUCS4: |
| 1355 | /* Encode UCS4 Unicode ordinals */ |
| 1356 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 1357 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 1358 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1359 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1360 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1361 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 1362 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1363 | if (v == NULL) { |
| 1364 | /* This was stack allocated. */ |
| 1365 | nneeded = Py_SAFE_DOWNCAST(p - stackbuf, long, int); |
| 1366 | assert(nneeded <= nallocated); |
| 1367 | v = PyString_FromStringAndSize(stackbuf, nneeded); |
| 1368 | } |
| 1369 | else { |
| 1370 | /* Cut back to size actually needed. */ |
| 1371 | nneeded = Py_SAFE_DOWNCAST(p - PyString_AS_STRING(v), long, int); |
| 1372 | assert(nneeded <= nallocated); |
| 1373 | _PyString_Resize(&v, nneeded); |
| 1374 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1375 | return v; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1376 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1377 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1380 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 1381 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1382 | if (!PyUnicode_Check(unicode)) { |
| 1383 | PyErr_BadArgument(); |
| 1384 | return NULL; |
| 1385 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 1386 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1387 | PyUnicode_GET_SIZE(unicode), |
| 1388 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 1392 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1393 | PyObject * |
| 1394 | PyUnicode_DecodeUTF16(const char *s, |
| 1395 | int size, |
| 1396 | const char *errors, |
| 1397 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1398 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1399 | const char *starts = s; |
| 1400 | int startinpos; |
| 1401 | int endinpos; |
| 1402 | int outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1403 | PyUnicodeObject *unicode; |
| 1404 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1405 | const unsigned char *q, *e; |
| 1406 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1407 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1408 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 1409 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1410 | int ihi = 1, ilo = 0; |
| 1411 | #else |
| 1412 | int ihi = 0, ilo = 1; |
| 1413 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1414 | PyObject *errorHandler = NULL; |
| 1415 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1416 | |
| 1417 | /* Note: size will always be longer than the resulting Unicode |
| 1418 | character count */ |
| 1419 | unicode = _PyUnicode_New(size); |
| 1420 | if (!unicode) |
| 1421 | return NULL; |
| 1422 | if (size == 0) |
| 1423 | return (PyObject *)unicode; |
| 1424 | |
| 1425 | /* Unpack UTF-16 encoded data */ |
| 1426 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1427 | q = (unsigned char *)s; |
| 1428 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1429 | |
| 1430 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1431 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1432 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1433 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 1434 | byte order setting accordingly. In native mode, the leading BOM |
| 1435 | mark is skipped, in all other modes, it is copied to the output |
| 1436 | stream as-is (giving a ZWNBSP character). */ |
| 1437 | if (bo == 0) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1438 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1439 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1440 | if (bom == 0xFEFF) { |
| 1441 | q += 2; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1442 | bo = -1; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1443 | } |
| 1444 | else if (bom == 0xFFFE) { |
| 1445 | q += 2; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1446 | bo = 1; |
| 1447 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1448 | #else |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1449 | if (bom == 0xFEFF) { |
| 1450 | q += 2; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1451 | bo = 1; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1452 | } |
| 1453 | else if (bom == 0xFFFE) { |
| 1454 | q += 2; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1455 | bo = -1; |
| 1456 | } |
| 1457 | #endif |
| 1458 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1459 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1460 | if (bo == -1) { |
| 1461 | /* force LE */ |
| 1462 | ihi = 1; |
| 1463 | ilo = 0; |
| 1464 | } |
| 1465 | else if (bo == 1) { |
| 1466 | /* force BE */ |
| 1467 | ihi = 0; |
| 1468 | ilo = 1; |
| 1469 | } |
| 1470 | |
| 1471 | while (q < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1472 | Py_UNICODE ch; |
| 1473 | /* remaing bytes at the end? (size should be even) */ |
| 1474 | if (e-q<2) { |
| 1475 | errmsg = "truncated data"; |
| 1476 | startinpos = ((const char *)q)-starts; |
| 1477 | endinpos = ((const char *)e)-starts; |
| 1478 | goto utf16Error; |
| 1479 | /* The remaining input chars are ignored if the callback |
| 1480 | chooses to skip the input */ |
| 1481 | } |
| 1482 | ch = (q[ihi] << 8) | q[ilo]; |
| 1483 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1484 | q += 2; |
| 1485 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1486 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 1487 | *p++ = ch; |
| 1488 | continue; |
| 1489 | } |
| 1490 | |
| 1491 | /* UTF-16 code pair: */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1492 | if (q >= e) { |
| 1493 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1494 | startinpos = (((const char *)q)-2)-starts; |
| 1495 | endinpos = ((const char *)e)-starts; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1496 | goto utf16Error; |
| 1497 | } |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 1498 | if (0xD800 <= ch && ch <= 0xDBFF) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1499 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 1500 | q += 2; |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 1501 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1502 | #ifndef Py_UNICODE_WIDE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1503 | *p++ = ch; |
| 1504 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1505 | #else |
| 1506 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1507 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1508 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1509 | } |
| 1510 | else { |
| 1511 | errmsg = "illegal UTF-16 surrogate"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1512 | startinpos = (((const char *)q)-4)-starts; |
| 1513 | endinpos = startinpos+2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1514 | goto utf16Error; |
| 1515 | } |
| 1516 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1517 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1518 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1519 | startinpos = (((const char *)q)-2)-starts; |
| 1520 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1521 | /* Fall through to report the error */ |
| 1522 | |
| 1523 | utf16Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1524 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1525 | if (unicode_decode_call_errorhandler( |
| 1526 | errors, &errorHandler, |
| 1527 | "utf16", errmsg, |
| 1528 | starts, size, &startinpos, &endinpos, &exc, (const char **)&q, |
| 1529 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1530 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | if (byteorder) |
| 1534 | *byteorder = bo; |
| 1535 | |
| 1536 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1537 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1538 | goto onError; |
| 1539 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1540 | Py_XDECREF(errorHandler); |
| 1541 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1542 | return (PyObject *)unicode; |
| 1543 | |
| 1544 | onError: |
| 1545 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1546 | Py_XDECREF(errorHandler); |
| 1547 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1548 | return NULL; |
| 1549 | } |
| 1550 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1551 | PyObject * |
| 1552 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 1553 | int size, |
| 1554 | const char *errors, |
| 1555 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1556 | { |
| 1557 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1558 | unsigned char *p; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1559 | #ifdef Py_UNICODE_WIDE |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1560 | int i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1561 | #else |
| 1562 | const int pairs = 0; |
| 1563 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1564 | /* Offsets from p for storing byte pairs in the right order. */ |
| 1565 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1566 | int ihi = 1, ilo = 0; |
| 1567 | #else |
| 1568 | int ihi = 0, ilo = 1; |
| 1569 | #endif |
| 1570 | |
| 1571 | #define STORECHAR(CH) \ |
| 1572 | do { \ |
| 1573 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 1574 | p[ilo] = (CH) & 0xff; \ |
| 1575 | p += 2; \ |
| 1576 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1577 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1578 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1579 | for (i = pairs = 0; i < size; i++) |
| 1580 | if (s[i] >= 0x10000) |
| 1581 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1582 | #endif |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1583 | v = PyString_FromStringAndSize(NULL, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1584 | 2 * (size + pairs + (byteorder == 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1585 | if (v == NULL) |
| 1586 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1587 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1588 | p = (unsigned char *)PyString_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1589 | if (byteorder == 0) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1590 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 1591 | if (size == 0) |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 1592 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1593 | |
| 1594 | if (byteorder == -1) { |
| 1595 | /* force LE */ |
| 1596 | ihi = 1; |
| 1597 | ilo = 0; |
| 1598 | } |
| 1599 | else if (byteorder == 1) { |
| 1600 | /* force BE */ |
| 1601 | ihi = 0; |
| 1602 | ilo = 1; |
| 1603 | } |
| 1604 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1605 | while (size-- > 0) { |
| 1606 | Py_UNICODE ch = *s++; |
| 1607 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1608 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1609 | if (ch >= 0x10000) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1610 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 1611 | ch = 0xD800 | ((ch-0x10000) >> 10); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1612 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1613 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1614 | STORECHAR(ch); |
| 1615 | if (ch2) |
| 1616 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1617 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1618 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1619 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 1623 | { |
| 1624 | if (!PyUnicode_Check(unicode)) { |
| 1625 | PyErr_BadArgument(); |
| 1626 | return NULL; |
| 1627 | } |
| 1628 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 1629 | PyUnicode_GET_SIZE(unicode), |
| 1630 | NULL, |
| 1631 | 0); |
| 1632 | } |
| 1633 | |
| 1634 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 1635 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1636 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 1637 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1638 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
| 1639 | int size, |
| 1640 | const char *errors) |
| 1641 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1642 | const char *starts = s; |
| 1643 | int startinpos; |
| 1644 | int endinpos; |
| 1645 | int outpos; |
| 1646 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1647 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1648 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1649 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1650 | char* message; |
| 1651 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1652 | PyObject *errorHandler = NULL; |
| 1653 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1654 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1655 | /* Escaped strings will always be longer than the resulting |
| 1656 | 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] | 1657 | length after conversion to the true value. |
| 1658 | (but if the error callback returns a long replacement string |
| 1659 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1660 | v = _PyUnicode_New(size); |
| 1661 | if (v == NULL) |
| 1662 | goto onError; |
| 1663 | if (size == 0) |
| 1664 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1665 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1666 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1667 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1668 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1669 | while (s < end) { |
| 1670 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 1671 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1672 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1673 | |
| 1674 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 1675 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1676 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1677 | continue; |
| 1678 | } |
| 1679 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1680 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1681 | /* \ - Escapes */ |
| 1682 | s++; |
| 1683 | switch (*s++) { |
| 1684 | |
| 1685 | /* \x escapes */ |
| 1686 | case '\n': break; |
| 1687 | case '\\': *p++ = '\\'; break; |
| 1688 | case '\'': *p++ = '\''; break; |
| 1689 | case '\"': *p++ = '\"'; break; |
| 1690 | case 'b': *p++ = '\b'; break; |
| 1691 | case 'f': *p++ = '\014'; break; /* FF */ |
| 1692 | case 't': *p++ = '\t'; break; |
| 1693 | case 'n': *p++ = '\n'; break; |
| 1694 | case 'r': *p++ = '\r'; break; |
| 1695 | case 'v': *p++ = '\013'; break; /* VT */ |
| 1696 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 1697 | |
| 1698 | /* \OOO (octal) escapes */ |
| 1699 | case '0': case '1': case '2': case '3': |
| 1700 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1701 | x = s[-1] - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1702 | if ('0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1703 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1704 | if ('0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1705 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1706 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1707 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1708 | break; |
| 1709 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1710 | /* hex escapes */ |
| 1711 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1712 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1713 | digits = 2; |
| 1714 | message = "truncated \\xXX escape"; |
| 1715 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1716 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1717 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1718 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1719 | digits = 4; |
| 1720 | message = "truncated \\uXXXX escape"; |
| 1721 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1722 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1723 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1724 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1725 | digits = 8; |
| 1726 | message = "truncated \\UXXXXXXXX escape"; |
| 1727 | hexescape: |
| 1728 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1729 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1730 | if (s+digits>end) { |
| 1731 | endinpos = size; |
| 1732 | if (unicode_decode_call_errorhandler( |
| 1733 | errors, &errorHandler, |
| 1734 | "unicodeescape", "end of string in escape sequence", |
| 1735 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1736 | (PyObject **)&v, &outpos, &p)) |
| 1737 | goto onError; |
| 1738 | goto nextByte; |
| 1739 | } |
| 1740 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1741 | c = (unsigned char) s[i]; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1742 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1743 | endinpos = (s+i+1)-starts; |
| 1744 | if (unicode_decode_call_errorhandler( |
| 1745 | errors, &errorHandler, |
| 1746 | "unicodeescape", message, |
| 1747 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1748 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1749 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1750 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1751 | } |
| 1752 | chr = (chr<<4) & ~0xF; |
| 1753 | if (c >= '0' && c <= '9') |
| 1754 | chr += c - '0'; |
| 1755 | else if (c >= 'a' && c <= 'f') |
| 1756 | chr += 10 + c - 'a'; |
| 1757 | else |
| 1758 | chr += 10 + c - 'A'; |
| 1759 | } |
| 1760 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 1761 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1762 | /* _decoding_error will have already written into the |
| 1763 | target buffer. */ |
| 1764 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1765 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1766 | /* when we get here, chr is a 32-bit unicode character */ |
| 1767 | if (chr <= 0xffff) |
| 1768 | /* UCS-2 character */ |
| 1769 | *p++ = (Py_UNICODE) chr; |
| 1770 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1771 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1772 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1773 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1774 | *p++ = chr; |
| 1775 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1776 | chr -= 0x10000L; |
| 1777 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1778 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1779 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1780 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1781 | endinpos = s-starts; |
| 1782 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1783 | if (unicode_decode_call_errorhandler( |
| 1784 | errors, &errorHandler, |
| 1785 | "unicodeescape", "illegal Unicode character", |
| 1786 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1787 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1788 | goto onError; |
| 1789 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1790 | break; |
| 1791 | |
| 1792 | /* \N{name} */ |
| 1793 | case 'N': |
| 1794 | message = "malformed \\N character escape"; |
| 1795 | if (ucnhash_CAPI == NULL) { |
| 1796 | /* load the unicode data module */ |
| 1797 | PyObject *m, *v; |
| 1798 | m = PyImport_ImportModule("unicodedata"); |
| 1799 | if (m == NULL) |
| 1800 | goto ucnhashError; |
| 1801 | v = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
| 1802 | Py_DECREF(m); |
| 1803 | if (v == NULL) |
| 1804 | goto ucnhashError; |
| 1805 | ucnhash_CAPI = PyCObject_AsVoidPtr(v); |
| 1806 | Py_DECREF(v); |
| 1807 | if (ucnhash_CAPI == NULL) |
| 1808 | goto ucnhashError; |
| 1809 | } |
| 1810 | if (*s == '{') { |
| 1811 | const char *start = s+1; |
| 1812 | /* look for the closing brace */ |
| 1813 | while (*s != '}' && s < end) |
| 1814 | s++; |
| 1815 | if (s > start && s < end && *s == '}') { |
| 1816 | /* found a name. look it up in the unicode database */ |
| 1817 | message = "unknown Unicode character name"; |
| 1818 | s++; |
| 1819 | if (ucnhash_CAPI->getcode(start, s-start-1, &chr)) |
| 1820 | goto store; |
| 1821 | } |
| 1822 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1823 | endinpos = s-starts; |
| 1824 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1825 | if (unicode_decode_call_errorhandler( |
| 1826 | errors, &errorHandler, |
| 1827 | "unicodeescape", message, |
| 1828 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1829 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1830 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1831 | break; |
| 1832 | |
| 1833 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1834 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1835 | message = "\\ at end of string"; |
| 1836 | s--; |
| 1837 | endinpos = s-starts; |
| 1838 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1839 | if (unicode_decode_call_errorhandler( |
| 1840 | errors, &errorHandler, |
| 1841 | "unicodeescape", message, |
| 1842 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1843 | (PyObject **)&v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1844 | goto onError; |
| 1845 | } |
| 1846 | else { |
| 1847 | *p++ = '\\'; |
| 1848 | *p++ = (unsigned char)s[-1]; |
| 1849 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1850 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1851 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1852 | nextByte: |
| 1853 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1854 | } |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1855 | if (_PyUnicode_Resize(&v, (int)(p - PyUnicode_AS_UNICODE(v))) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1856 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 1857 | Py_XDECREF(errorHandler); |
| 1858 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1859 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1860 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1861 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1862 | PyErr_SetString( |
| 1863 | PyExc_UnicodeError, |
| 1864 | "\\N escapes not supported (can't load unicodedata module)" |
| 1865 | ); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1866 | Py_XDECREF(errorHandler); |
| 1867 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 1868 | return NULL; |
| 1869 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1870 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1871 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1872 | Py_XDECREF(errorHandler); |
| 1873 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1874 | return NULL; |
| 1875 | } |
| 1876 | |
| 1877 | /* Return a Unicode-Escape string version of the Unicode object. |
| 1878 | |
| 1879 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 1880 | appropriate. |
| 1881 | |
| 1882 | */ |
| 1883 | |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 1884 | static const Py_UNICODE *findchar(const Py_UNICODE *s, |
| 1885 | int size, |
| 1886 | Py_UNICODE ch); |
| 1887 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1888 | static |
| 1889 | PyObject *unicodeescape_string(const Py_UNICODE *s, |
| 1890 | int size, |
| 1891 | int quotes) |
| 1892 | { |
| 1893 | PyObject *repr; |
| 1894 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1895 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 1896 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1897 | |
| 1898 | repr = PyString_FromStringAndSize(NULL, 2 + 6*size + 1); |
| 1899 | if (repr == NULL) |
| 1900 | return NULL; |
| 1901 | |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1902 | p = PyString_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1903 | |
| 1904 | if (quotes) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1905 | *p++ = 'u'; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1906 | *p++ = (findchar(s, size, '\'') && |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1907 | !findchar(s, size, '"')) ? '"' : '\''; |
| 1908 | } |
| 1909 | while (size-- > 0) { |
| 1910 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1911 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1912 | /* Escape quotes */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1913 | if (quotes && |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1914 | (ch == (Py_UNICODE) PyString_AS_STRING(repr)[1] || ch == '\\')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1915 | *p++ = '\\'; |
| 1916 | *p++ = (char) ch; |
Guido van Rossum | ad9744a | 2001-09-21 15:38:17 +0000 | [diff] [blame] | 1917 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1918 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1919 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 1920 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1921 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 1922 | else if (ch >= 0x10000) { |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1923 | int offset = p - PyString_AS_STRING(repr); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1924 | |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1925 | /* Resize the string if necessary */ |
| 1926 | if (offset + 12 > PyString_GET_SIZE(repr)) { |
| 1927 | if (_PyString_Resize(&repr, PyString_GET_SIZE(repr) + 100)) |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 1928 | return NULL; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1929 | p = PyString_AS_STRING(repr) + offset; |
| 1930 | } |
| 1931 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1932 | *p++ = '\\'; |
| 1933 | *p++ = 'U'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1934 | *p++ = hexdigit[(ch >> 28) & 0x0000000F]; |
| 1935 | *p++ = hexdigit[(ch >> 24) & 0x0000000F]; |
| 1936 | *p++ = hexdigit[(ch >> 20) & 0x0000000F]; |
| 1937 | *p++ = hexdigit[(ch >> 16) & 0x0000000F]; |
| 1938 | *p++ = hexdigit[(ch >> 12) & 0x0000000F]; |
| 1939 | *p++ = hexdigit[(ch >> 8) & 0x0000000F]; |
| 1940 | *p++ = hexdigit[(ch >> 4) & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1941 | *p++ = hexdigit[ch & 0x0000000F]; |
| 1942 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1943 | } |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 1944 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1945 | /* Map UTF-16 surrogate pairs to Unicode \UXXXXXXXX escapes */ |
| 1946 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 1947 | Py_UNICODE ch2; |
| 1948 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1949 | |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1950 | ch2 = *s++; |
| 1951 | size--; |
| 1952 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 1953 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 1954 | *p++ = '\\'; |
| 1955 | *p++ = 'U'; |
| 1956 | *p++ = hexdigit[(ucs >> 28) & 0x0000000F]; |
| 1957 | *p++ = hexdigit[(ucs >> 24) & 0x0000000F]; |
| 1958 | *p++ = hexdigit[(ucs >> 20) & 0x0000000F]; |
| 1959 | *p++ = hexdigit[(ucs >> 16) & 0x0000000F]; |
| 1960 | *p++ = hexdigit[(ucs >> 12) & 0x0000000F]; |
| 1961 | *p++ = hexdigit[(ucs >> 8) & 0x0000000F]; |
| 1962 | *p++ = hexdigit[(ucs >> 4) & 0x0000000F]; |
| 1963 | *p++ = hexdigit[ucs & 0x0000000F]; |
| 1964 | continue; |
| 1965 | } |
| 1966 | /* Fall through: isolated surrogates are copied as-is */ |
| 1967 | s--; |
| 1968 | size++; |
| 1969 | } |
| 1970 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1971 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1972 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1973 | *p++ = '\\'; |
| 1974 | *p++ = 'u'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1975 | *p++ = hexdigit[(ch >> 12) & 0x000F]; |
| 1976 | *p++ = hexdigit[(ch >> 8) & 0x000F]; |
| 1977 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 1978 | *p++ = hexdigit[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1979 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1980 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 1981 | /* Map special whitespace to '\t', \n', '\r' */ |
| 1982 | else if (ch == '\t') { |
| 1983 | *p++ = '\\'; |
| 1984 | *p++ = 't'; |
| 1985 | } |
| 1986 | else if (ch == '\n') { |
| 1987 | *p++ = '\\'; |
| 1988 | *p++ = 'n'; |
| 1989 | } |
| 1990 | else if (ch == '\r') { |
| 1991 | *p++ = '\\'; |
| 1992 | *p++ = 'r'; |
| 1993 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1994 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 1995 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 1996 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1997 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 1998 | *p++ = 'x'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1999 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2000 | *p++ = hexdigit[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2001 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2002 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2003 | /* Copy everything else as-is */ |
| 2004 | else |
| 2005 | *p++ = (char) ch; |
| 2006 | } |
| 2007 | if (quotes) |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2008 | *p++ = PyString_AS_STRING(repr)[1]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2009 | |
| 2010 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2011 | _PyString_Resize(&repr, p - PyString_AS_STRING(repr)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2012 | return repr; |
| 2013 | } |
| 2014 | |
| 2015 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 2016 | int size) |
| 2017 | { |
| 2018 | return unicodeescape_string(s, size, 0); |
| 2019 | } |
| 2020 | |
| 2021 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 2022 | { |
| 2023 | if (!PyUnicode_Check(unicode)) { |
| 2024 | PyErr_BadArgument(); |
| 2025 | return NULL; |
| 2026 | } |
| 2027 | return PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2028 | PyUnicode_GET_SIZE(unicode)); |
| 2029 | } |
| 2030 | |
| 2031 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 2032 | |
| 2033 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
| 2034 | int size, |
| 2035 | const char *errors) |
| 2036 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2037 | const char *starts = s; |
| 2038 | int startinpos; |
| 2039 | int endinpos; |
| 2040 | int outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2041 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2042 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2043 | const char *end; |
| 2044 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2045 | PyObject *errorHandler = NULL; |
| 2046 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2047 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2048 | /* Escaped strings will always be longer than the resulting |
| 2049 | 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] | 2050 | length after conversion to the true value. (But decoding error |
| 2051 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2052 | v = _PyUnicode_New(size); |
| 2053 | if (v == NULL) |
| 2054 | goto onError; |
| 2055 | if (size == 0) |
| 2056 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2057 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2058 | end = s + size; |
| 2059 | while (s < end) { |
| 2060 | unsigned char c; |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 2061 | Py_UCS4 x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2062 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2063 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2064 | |
| 2065 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2066 | if (*s != '\\') { |
| 2067 | *p++ = (unsigned char)*s++; |
| 2068 | continue; |
| 2069 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2070 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2071 | |
| 2072 | /* \u-escapes are only interpreted iff the number of leading |
| 2073 | backslashes if odd */ |
| 2074 | bs = s; |
| 2075 | for (;s < end;) { |
| 2076 | if (*s != '\\') |
| 2077 | break; |
| 2078 | *p++ = (unsigned char)*s++; |
| 2079 | } |
| 2080 | if (((s - bs) & 1) == 0 || |
| 2081 | s >= end || |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2082 | (*s != 'u' && *s != 'U')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2083 | continue; |
| 2084 | } |
| 2085 | p--; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2086 | count = *s=='u' ? 4 : 8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2087 | s++; |
| 2088 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2089 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2090 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2091 | for (x = 0, i = 0; i < count; ++i, ++s) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2092 | c = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2093 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2094 | endinpos = s-starts; |
| 2095 | if (unicode_decode_call_errorhandler( |
| 2096 | errors, &errorHandler, |
| 2097 | "rawunicodeescape", "truncated \\uXXXX", |
| 2098 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2099 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2100 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2101 | goto nextByte; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2102 | } |
| 2103 | x = (x<<4) & ~0xF; |
| 2104 | if (c >= '0' && c <= '9') |
| 2105 | x += c - '0'; |
| 2106 | else if (c >= 'a' && c <= 'f') |
| 2107 | x += 10 + c - 'a'; |
| 2108 | else |
| 2109 | x += 10 + c - 'A'; |
| 2110 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2111 | #ifndef Py_UNICODE_WIDE |
| 2112 | if (x > 0x10000) { |
| 2113 | if (unicode_decode_call_errorhandler( |
| 2114 | errors, &errorHandler, |
| 2115 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
| 2116 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2117 | (PyObject **)&v, &outpos, &p)) |
| 2118 | goto onError; |
| 2119 | } |
| 2120 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2121 | *p++ = x; |
| 2122 | nextByte: |
| 2123 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2124 | } |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2125 | if (_PyUnicode_Resize(&v, (int)(p - PyUnicode_AS_UNICODE(v))) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2126 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2127 | Py_XDECREF(errorHandler); |
| 2128 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2129 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2130 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2131 | onError: |
| 2132 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2133 | Py_XDECREF(errorHandler); |
| 2134 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2135 | return NULL; |
| 2136 | } |
| 2137 | |
| 2138 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 2139 | int size) |
| 2140 | { |
| 2141 | PyObject *repr; |
| 2142 | char *p; |
| 2143 | char *q; |
| 2144 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2145 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2146 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2147 | #ifdef Py_UNICODE_WIDE |
| 2148 | repr = PyString_FromStringAndSize(NULL, 10 * size); |
| 2149 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2150 | repr = PyString_FromStringAndSize(NULL, 6 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2151 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2152 | if (repr == NULL) |
| 2153 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2154 | if (size == 0) |
| 2155 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2156 | |
| 2157 | p = q = PyString_AS_STRING(repr); |
| 2158 | while (size-- > 0) { |
| 2159 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2160 | #ifdef Py_UNICODE_WIDE |
| 2161 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2162 | if (ch >= 0x10000) { |
| 2163 | *p++ = '\\'; |
| 2164 | *p++ = 'U'; |
| 2165 | *p++ = hexdigit[(ch >> 28) & 0xf]; |
| 2166 | *p++ = hexdigit[(ch >> 24) & 0xf]; |
| 2167 | *p++ = hexdigit[(ch >> 20) & 0xf]; |
| 2168 | *p++ = hexdigit[(ch >> 16) & 0xf]; |
| 2169 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 2170 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 2171 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 2172 | *p++ = hexdigit[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2173 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2174 | else |
| 2175 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2176 | /* Map 16-bit characters to '\uxxxx' */ |
| 2177 | if (ch >= 256) { |
| 2178 | *p++ = '\\'; |
| 2179 | *p++ = 'u'; |
| 2180 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 2181 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 2182 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 2183 | *p++ = hexdigit[ch & 15]; |
| 2184 | } |
| 2185 | /* Copy everything else as-is */ |
| 2186 | else |
| 2187 | *p++ = (char) ch; |
| 2188 | } |
| 2189 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2190 | _PyString_Resize(&repr, p - q); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2191 | return repr; |
| 2192 | } |
| 2193 | |
| 2194 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 2195 | { |
| 2196 | if (!PyUnicode_Check(unicode)) { |
| 2197 | PyErr_BadArgument(); |
| 2198 | return NULL; |
| 2199 | } |
| 2200 | return PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2201 | PyUnicode_GET_SIZE(unicode)); |
| 2202 | } |
| 2203 | |
| 2204 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 2205 | |
| 2206 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
| 2207 | int size, |
| 2208 | const char *errors) |
| 2209 | { |
| 2210 | PyUnicodeObject *v; |
| 2211 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2212 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2213 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2214 | if (size == 1) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 2215 | Py_UNICODE r = *(unsigned char*)s; |
| 2216 | return PyUnicode_FromUnicode(&r, 1); |
| 2217 | } |
| 2218 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2219 | v = _PyUnicode_New(size); |
| 2220 | if (v == NULL) |
| 2221 | goto onError; |
| 2222 | if (size == 0) |
| 2223 | return (PyObject *)v; |
| 2224 | p = PyUnicode_AS_UNICODE(v); |
| 2225 | while (size-- > 0) |
| 2226 | *p++ = (unsigned char)*s++; |
| 2227 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2228 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2229 | onError: |
| 2230 | Py_XDECREF(v); |
| 2231 | return NULL; |
| 2232 | } |
| 2233 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2234 | /* create or adjust a UnicodeEncodeError */ |
| 2235 | static void make_encode_exception(PyObject **exceptionObject, |
| 2236 | const char *encoding, |
| 2237 | const Py_UNICODE *unicode, int size, |
| 2238 | int startpos, int endpos, |
| 2239 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2240 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2241 | if (*exceptionObject == NULL) { |
| 2242 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 2243 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2244 | } |
| 2245 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2246 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 2247 | goto onError; |
| 2248 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 2249 | goto onError; |
| 2250 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 2251 | goto onError; |
| 2252 | return; |
| 2253 | onError: |
| 2254 | Py_DECREF(*exceptionObject); |
| 2255 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2256 | } |
| 2257 | } |
| 2258 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2259 | /* raises a UnicodeEncodeError */ |
| 2260 | static void raise_encode_exception(PyObject **exceptionObject, |
| 2261 | const char *encoding, |
| 2262 | const Py_UNICODE *unicode, int size, |
| 2263 | int startpos, int endpos, |
| 2264 | const char *reason) |
| 2265 | { |
| 2266 | make_encode_exception(exceptionObject, |
| 2267 | encoding, unicode, size, startpos, endpos, reason); |
| 2268 | if (*exceptionObject != NULL) |
| 2269 | PyCodec_StrictErrors(*exceptionObject); |
| 2270 | } |
| 2271 | |
| 2272 | /* error handling callback helper: |
| 2273 | build arguments, call the callback and check the arguments, |
| 2274 | put the result into newpos and return the replacement string, which |
| 2275 | has to be freed by the caller */ |
| 2276 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 2277 | PyObject **errorHandler, |
| 2278 | const char *encoding, const char *reason, |
| 2279 | const Py_UNICODE *unicode, int size, PyObject **exceptionObject, |
| 2280 | int startpos, int endpos, |
| 2281 | int *newpos) |
| 2282 | { |
| 2283 | static char *argparse = "O!i;encoding error handler must return (unicode, int) tuple"; |
| 2284 | |
| 2285 | PyObject *restuple; |
| 2286 | PyObject *resunicode; |
| 2287 | |
| 2288 | if (*errorHandler == NULL) { |
| 2289 | *errorHandler = PyCodec_LookupError(errors); |
| 2290 | if (*errorHandler == NULL) |
| 2291 | return NULL; |
| 2292 | } |
| 2293 | |
| 2294 | make_encode_exception(exceptionObject, |
| 2295 | encoding, unicode, size, startpos, endpos, reason); |
| 2296 | if (*exceptionObject == NULL) |
| 2297 | return NULL; |
| 2298 | |
| 2299 | restuple = PyObject_CallFunctionObjArgs( |
| 2300 | *errorHandler, *exceptionObject, NULL); |
| 2301 | if (restuple == NULL) |
| 2302 | return NULL; |
| 2303 | if (!PyTuple_Check(restuple)) { |
| 2304 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 2305 | Py_DECREF(restuple); |
| 2306 | return NULL; |
| 2307 | } |
| 2308 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 2309 | &resunicode, newpos)) { |
| 2310 | Py_DECREF(restuple); |
| 2311 | return NULL; |
| 2312 | } |
| 2313 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2314 | *newpos = size+*newpos; |
| 2315 | if (*newpos<0 || *newpos>size) { |
| 2316 | PyErr_Format(PyExc_IndexError, "position %d from error handler out of bounds", *newpos); |
| 2317 | Py_DECREF(restuple); |
| 2318 | return NULL; |
| 2319 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2320 | Py_INCREF(resunicode); |
| 2321 | Py_DECREF(restuple); |
| 2322 | return resunicode; |
| 2323 | } |
| 2324 | |
| 2325 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
| 2326 | int size, |
| 2327 | const char *errors, |
| 2328 | int limit) |
| 2329 | { |
| 2330 | /* output object */ |
| 2331 | PyObject *res; |
| 2332 | /* pointers to the beginning and end+1 of input */ |
| 2333 | const Py_UNICODE *startp = p; |
| 2334 | const Py_UNICODE *endp = p + size; |
| 2335 | /* pointer to the beginning of the unencodable characters */ |
| 2336 | /* const Py_UNICODE *badp = NULL; */ |
| 2337 | /* pointer into the output */ |
| 2338 | char *str; |
| 2339 | /* current output position */ |
| 2340 | int respos = 0; |
| 2341 | int ressize; |
| 2342 | char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 2343 | char *reason = (limit == 256) ? "ordinal not in range(256)" : "ordinal not in range(128)"; |
| 2344 | PyObject *errorHandler = NULL; |
| 2345 | PyObject *exc = NULL; |
| 2346 | /* the following variable is used for caching string comparisons |
| 2347 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 2348 | int known_errorHandler = -1; |
| 2349 | |
| 2350 | /* allocate enough for a simple encoding without |
| 2351 | replacements, if we need more, we'll resize */ |
| 2352 | res = PyString_FromStringAndSize(NULL, size); |
| 2353 | if (res == NULL) |
| 2354 | goto onError; |
| 2355 | if (size == 0) |
| 2356 | return res; |
| 2357 | str = PyString_AS_STRING(res); |
| 2358 | ressize = size; |
| 2359 | |
| 2360 | while (p<endp) { |
| 2361 | Py_UNICODE c = *p; |
| 2362 | |
| 2363 | /* can we encode this? */ |
| 2364 | if (c<limit) { |
| 2365 | /* no overflow check, because we know that the space is enough */ |
| 2366 | *str++ = (char)c; |
| 2367 | ++p; |
| 2368 | } |
| 2369 | else { |
| 2370 | int unicodepos = p-startp; |
| 2371 | int requiredsize; |
| 2372 | PyObject *repunicode; |
| 2373 | int repsize; |
| 2374 | int newpos; |
| 2375 | int respos; |
| 2376 | Py_UNICODE *uni2; |
| 2377 | /* startpos for collecting unencodable chars */ |
| 2378 | const Py_UNICODE *collstart = p; |
| 2379 | const Py_UNICODE *collend = p; |
| 2380 | /* find all unecodable characters */ |
| 2381 | while ((collend < endp) && ((*collend)>=limit)) |
| 2382 | ++collend; |
| 2383 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 2384 | if (known_errorHandler==-1) { |
| 2385 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 2386 | known_errorHandler = 1; |
| 2387 | else if (!strcmp(errors, "replace")) |
| 2388 | known_errorHandler = 2; |
| 2389 | else if (!strcmp(errors, "ignore")) |
| 2390 | known_errorHandler = 3; |
| 2391 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 2392 | known_errorHandler = 4; |
| 2393 | else |
| 2394 | known_errorHandler = 0; |
| 2395 | } |
| 2396 | switch (known_errorHandler) { |
| 2397 | case 1: /* strict */ |
| 2398 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 2399 | goto onError; |
| 2400 | case 2: /* replace */ |
| 2401 | while (collstart++<collend) |
| 2402 | *str++ = '?'; /* fall through */ |
| 2403 | case 3: /* ignore */ |
| 2404 | p = collend; |
| 2405 | break; |
| 2406 | case 4: /* xmlcharrefreplace */ |
| 2407 | respos = str-PyString_AS_STRING(res); |
| 2408 | /* determine replacement size (temporarily (mis)uses p) */ |
| 2409 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 2410 | if (*p<10) |
| 2411 | repsize += 2+1+1; |
| 2412 | else if (*p<100) |
| 2413 | repsize += 2+2+1; |
| 2414 | else if (*p<1000) |
| 2415 | repsize += 2+3+1; |
| 2416 | else if (*p<10000) |
| 2417 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 2418 | #ifndef Py_UNICODE_WIDE |
| 2419 | else |
| 2420 | repsize += 2+5+1; |
| 2421 | #else |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2422 | else if (*p<100000) |
| 2423 | repsize += 2+5+1; |
| 2424 | else if (*p<1000000) |
| 2425 | repsize += 2+6+1; |
| 2426 | else |
| 2427 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2428 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2429 | } |
| 2430 | requiredsize = respos+repsize+(endp-collend); |
| 2431 | if (requiredsize > ressize) { |
| 2432 | if (requiredsize<2*ressize) |
| 2433 | requiredsize = 2*ressize; |
| 2434 | if (_PyString_Resize(&res, requiredsize)) |
| 2435 | goto onError; |
| 2436 | str = PyString_AS_STRING(res) + respos; |
| 2437 | ressize = requiredsize; |
| 2438 | } |
| 2439 | /* generate replacement (temporarily (mis)uses p) */ |
| 2440 | for (p = collstart; p < collend; ++p) { |
| 2441 | str += sprintf(str, "&#%d;", (int)*p); |
| 2442 | } |
| 2443 | p = collend; |
| 2444 | break; |
| 2445 | default: |
| 2446 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 2447 | encoding, reason, startp, size, &exc, |
| 2448 | collstart-startp, collend-startp, &newpos); |
| 2449 | if (repunicode == NULL) |
| 2450 | goto onError; |
| 2451 | /* need more space? (at least enough for what we |
| 2452 | have+the replacement+the rest of the string, so |
| 2453 | we won't have to check space for encodable characters) */ |
| 2454 | respos = str-PyString_AS_STRING(res); |
| 2455 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 2456 | requiredsize = respos+repsize+(endp-collend); |
| 2457 | if (requiredsize > ressize) { |
| 2458 | if (requiredsize<2*ressize) |
| 2459 | requiredsize = 2*ressize; |
| 2460 | if (_PyString_Resize(&res, requiredsize)) { |
| 2461 | Py_DECREF(repunicode); |
| 2462 | goto onError; |
| 2463 | } |
| 2464 | str = PyString_AS_STRING(res) + respos; |
| 2465 | ressize = requiredsize; |
| 2466 | } |
| 2467 | /* check if there is anything unencodable in the replacement |
| 2468 | and copy it to the output */ |
| 2469 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 2470 | c = *uni2; |
| 2471 | if (c >= limit) { |
| 2472 | raise_encode_exception(&exc, encoding, startp, size, |
| 2473 | unicodepos, unicodepos+1, reason); |
| 2474 | Py_DECREF(repunicode); |
| 2475 | goto onError; |
| 2476 | } |
| 2477 | *str = (char)c; |
| 2478 | } |
| 2479 | p = startp + newpos; |
| 2480 | Py_DECREF(repunicode); |
| 2481 | } |
| 2482 | } |
| 2483 | } |
| 2484 | /* Resize if we allocated to much */ |
| 2485 | respos = str-PyString_AS_STRING(res); |
| 2486 | if (respos<ressize) |
| 2487 | /* If this falls res will be NULL */ |
| 2488 | _PyString_Resize(&res, respos); |
| 2489 | Py_XDECREF(errorHandler); |
| 2490 | Py_XDECREF(exc); |
| 2491 | return res; |
| 2492 | |
| 2493 | onError: |
| 2494 | Py_XDECREF(res); |
| 2495 | Py_XDECREF(errorHandler); |
| 2496 | Py_XDECREF(exc); |
| 2497 | return NULL; |
| 2498 | } |
| 2499 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2500 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
| 2501 | int size, |
| 2502 | const char *errors) |
| 2503 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2504 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2505 | } |
| 2506 | |
| 2507 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 2508 | { |
| 2509 | if (!PyUnicode_Check(unicode)) { |
| 2510 | PyErr_BadArgument(); |
| 2511 | return NULL; |
| 2512 | } |
| 2513 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 2514 | PyUnicode_GET_SIZE(unicode), |
| 2515 | NULL); |
| 2516 | } |
| 2517 | |
| 2518 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 2519 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2520 | PyObject *PyUnicode_DecodeASCII(const char *s, |
| 2521 | int size, |
| 2522 | const char *errors) |
| 2523 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2524 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2525 | PyUnicodeObject *v; |
| 2526 | Py_UNICODE *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2527 | int startinpos; |
| 2528 | int endinpos; |
| 2529 | int outpos; |
| 2530 | const char *e; |
| 2531 | PyObject *errorHandler = NULL; |
| 2532 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2533 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2534 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 2535 | if (size == 1 && *(unsigned char*)s < 128) { |
| 2536 | Py_UNICODE r = *(unsigned char*)s; |
| 2537 | return PyUnicode_FromUnicode(&r, 1); |
| 2538 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2539 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2540 | v = _PyUnicode_New(size); |
| 2541 | if (v == NULL) |
| 2542 | goto onError; |
| 2543 | if (size == 0) |
| 2544 | return (PyObject *)v; |
| 2545 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2546 | e = s + size; |
| 2547 | while (s < e) { |
| 2548 | register unsigned char c = (unsigned char)*s; |
| 2549 | if (c < 128) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2550 | *p++ = c; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2551 | ++s; |
| 2552 | } |
| 2553 | else { |
| 2554 | startinpos = s-starts; |
| 2555 | endinpos = startinpos + 1; |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 2556 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2557 | if (unicode_decode_call_errorhandler( |
| 2558 | errors, &errorHandler, |
| 2559 | "ascii", "ordinal not in range(128)", |
| 2560 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2561 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2562 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2563 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2564 | } |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2565 | if (p - PyUnicode_AS_UNICODE(v) < PyString_GET_SIZE(v)) |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2566 | if (_PyUnicode_Resize(&v, (int)(p - PyUnicode_AS_UNICODE(v))) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2567 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2568 | Py_XDECREF(errorHandler); |
| 2569 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2570 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2571 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2572 | onError: |
| 2573 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2574 | Py_XDECREF(errorHandler); |
| 2575 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2576 | return NULL; |
| 2577 | } |
| 2578 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2579 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 2580 | int size, |
| 2581 | const char *errors) |
| 2582 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2583 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 2587 | { |
| 2588 | if (!PyUnicode_Check(unicode)) { |
| 2589 | PyErr_BadArgument(); |
| 2590 | return NULL; |
| 2591 | } |
| 2592 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 2593 | PyUnicode_GET_SIZE(unicode), |
| 2594 | NULL); |
| 2595 | } |
| 2596 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2597 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2598 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2599 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2600 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2601 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
| 2602 | int size, |
| 2603 | const char *errors) |
| 2604 | { |
| 2605 | PyUnicodeObject *v; |
| 2606 | Py_UNICODE *p; |
| 2607 | |
| 2608 | /* First get the size of the result */ |
| 2609 | DWORD usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 2610 | if (size > 0 && usize==0) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2611 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2612 | |
| 2613 | v = _PyUnicode_New(usize); |
| 2614 | if (v == NULL) |
| 2615 | return NULL; |
| 2616 | if (usize == 0) |
| 2617 | return (PyObject *)v; |
| 2618 | p = PyUnicode_AS_UNICODE(v); |
| 2619 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 2620 | Py_DECREF(v); |
| 2621 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2622 | } |
| 2623 | |
| 2624 | return (PyObject *)v; |
| 2625 | } |
| 2626 | |
| 2627 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 2628 | int size, |
| 2629 | const char *errors) |
| 2630 | { |
| 2631 | PyObject *repr; |
| 2632 | char *s; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 2633 | DWORD mbcssize; |
| 2634 | |
| 2635 | /* If there are no characters, bail now! */ |
| 2636 | if (size==0) |
| 2637 | return PyString_FromString(""); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2638 | |
| 2639 | /* First get the size of the result */ |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 2640 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2641 | if (mbcssize==0) |
| 2642 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2643 | |
| 2644 | repr = PyString_FromStringAndSize(NULL, mbcssize); |
| 2645 | if (repr == NULL) |
| 2646 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2647 | if (mbcssize == 0) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2648 | return repr; |
| 2649 | |
| 2650 | /* Do the conversion */ |
| 2651 | s = PyString_AS_STRING(repr); |
| 2652 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 2653 | Py_DECREF(repr); |
| 2654 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2655 | } |
| 2656 | return repr; |
| 2657 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2658 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2659 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 2660 | { |
| 2661 | if (!PyUnicode_Check(unicode)) { |
| 2662 | PyErr_BadArgument(); |
| 2663 | return NULL; |
| 2664 | } |
| 2665 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2666 | PyUnicode_GET_SIZE(unicode), |
| 2667 | NULL); |
| 2668 | } |
| 2669 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2670 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2671 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2672 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 2673 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2674 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
| 2675 | int size, |
| 2676 | PyObject *mapping, |
| 2677 | const char *errors) |
| 2678 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2679 | const char *starts = s; |
| 2680 | int startinpos; |
| 2681 | int endinpos; |
| 2682 | int outpos; |
| 2683 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2684 | PyUnicodeObject *v; |
| 2685 | Py_UNICODE *p; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 2686 | int extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2687 | PyObject *errorHandler = NULL; |
| 2688 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2689 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2690 | /* Default to Latin-1 */ |
| 2691 | if (mapping == NULL) |
| 2692 | return PyUnicode_DecodeLatin1(s, size, errors); |
| 2693 | |
| 2694 | v = _PyUnicode_New(size); |
| 2695 | if (v == NULL) |
| 2696 | goto onError; |
| 2697 | if (size == 0) |
| 2698 | return (PyObject *)v; |
| 2699 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2700 | e = s + size; |
| 2701 | while (s < e) { |
| 2702 | unsigned char ch = *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2703 | PyObject *w, *x; |
| 2704 | |
| 2705 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 2706 | w = PyInt_FromLong((long)ch); |
| 2707 | if (w == NULL) |
| 2708 | goto onError; |
| 2709 | x = PyObject_GetItem(mapping, w); |
| 2710 | Py_DECREF(w); |
| 2711 | if (x == NULL) { |
| 2712 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
Marc-André Lemburg | a866df8 | 2001-01-03 21:29:14 +0000 | [diff] [blame] | 2713 | /* No mapping found means: mapping is undefined. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2714 | PyErr_Clear(); |
Marc-André Lemburg | a866df8 | 2001-01-03 21:29:14 +0000 | [diff] [blame] | 2715 | x = Py_None; |
| 2716 | Py_INCREF(x); |
| 2717 | } else |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 2718 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2719 | } |
| 2720 | |
| 2721 | /* Apply mapping */ |
| 2722 | if (PyInt_Check(x)) { |
Marc-André Lemburg | 85cc4d8 | 2000-07-06 19:43:31 +0000 | [diff] [blame] | 2723 | long value = PyInt_AS_LONG(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2724 | if (value < 0 || value > 65535) { |
| 2725 | PyErr_SetString(PyExc_TypeError, |
Marc-André Lemburg | 07ceb67 | 2000-06-10 09:32:51 +0000 | [diff] [blame] | 2726 | "character mapping must be in range(65536)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2727 | Py_DECREF(x); |
| 2728 | goto onError; |
| 2729 | } |
| 2730 | *p++ = (Py_UNICODE)value; |
| 2731 | } |
| 2732 | else if (x == Py_None) { |
| 2733 | /* undefined mapping */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2734 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2735 | startinpos = s-starts; |
| 2736 | endinpos = startinpos+1; |
| 2737 | if (unicode_decode_call_errorhandler( |
| 2738 | errors, &errorHandler, |
| 2739 | "charmap", "character maps to <undefined>", |
| 2740 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2741 | (PyObject **)&v, &outpos, &p)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2742 | Py_DECREF(x); |
| 2743 | goto onError; |
| 2744 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2745 | continue; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2746 | } |
| 2747 | else if (PyUnicode_Check(x)) { |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 2748 | int targetsize = PyUnicode_GET_SIZE(x); |
| 2749 | |
| 2750 | if (targetsize == 1) |
| 2751 | /* 1-1 mapping */ |
| 2752 | *p++ = *PyUnicode_AS_UNICODE(x); |
| 2753 | |
| 2754 | else if (targetsize > 1) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2755 | /* 1-n mapping */ |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 2756 | if (targetsize > extrachars) { |
| 2757 | /* resize first */ |
| 2758 | int oldpos = (int)(p - PyUnicode_AS_UNICODE(v)); |
| 2759 | int needed = (targetsize - extrachars) + \ |
| 2760 | (targetsize << 2); |
| 2761 | extrachars += needed; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2762 | if (_PyUnicode_Resize(&v, |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2763 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 2764 | Py_DECREF(x); |
| 2765 | goto onError; |
| 2766 | } |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 2767 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 2768 | } |
| 2769 | Py_UNICODE_COPY(p, |
| 2770 | PyUnicode_AS_UNICODE(x), |
| 2771 | targetsize); |
| 2772 | p += targetsize; |
| 2773 | extrachars -= targetsize; |
| 2774 | } |
| 2775 | /* 1-0 mapping: skip the character */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2776 | } |
| 2777 | else { |
| 2778 | /* wrong return value */ |
| 2779 | PyErr_SetString(PyExc_TypeError, |
| 2780 | "character mapping must return integer, None or unicode"); |
| 2781 | Py_DECREF(x); |
| 2782 | goto onError; |
| 2783 | } |
| 2784 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2785 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2786 | } |
| 2787 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2788 | if (_PyUnicode_Resize(&v, (int)(p - PyUnicode_AS_UNICODE(v))) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2789 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2790 | Py_XDECREF(errorHandler); |
| 2791 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2792 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2793 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2794 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2795 | Py_XDECREF(errorHandler); |
| 2796 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2797 | Py_XDECREF(v); |
| 2798 | return NULL; |
| 2799 | } |
| 2800 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2801 | /* Lookup the character ch in the mapping. If the character |
| 2802 | can't be found, Py_None is returned (or NULL, if another |
| 2803 | error occured). */ |
| 2804 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2805 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2806 | PyObject *w = PyInt_FromLong((long)c); |
| 2807 | PyObject *x; |
| 2808 | |
| 2809 | if (w == NULL) |
| 2810 | return NULL; |
| 2811 | x = PyObject_GetItem(mapping, w); |
| 2812 | Py_DECREF(w); |
| 2813 | if (x == NULL) { |
| 2814 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 2815 | /* No mapping found means: mapping is undefined. */ |
| 2816 | PyErr_Clear(); |
| 2817 | x = Py_None; |
| 2818 | Py_INCREF(x); |
| 2819 | return x; |
| 2820 | } else |
| 2821 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2822 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 2823 | else if (x == Py_None) |
| 2824 | return x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2825 | else if (PyInt_Check(x)) { |
| 2826 | long value = PyInt_AS_LONG(x); |
| 2827 | if (value < 0 || value > 255) { |
| 2828 | PyErr_SetString(PyExc_TypeError, |
| 2829 | "character mapping must be in range(256)"); |
| 2830 | Py_DECREF(x); |
| 2831 | return NULL; |
| 2832 | } |
| 2833 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2834 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2835 | else if (PyString_Check(x)) |
| 2836 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2837 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2838 | /* wrong return value */ |
| 2839 | PyErr_SetString(PyExc_TypeError, |
| 2840 | "character mapping must return integer, None or str"); |
| 2841 | Py_DECREF(x); |
| 2842 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2843 | } |
| 2844 | } |
| 2845 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2846 | /* lookup the character, put the result in the output string and adjust |
| 2847 | various state variables. Reallocate the output string if not enough |
| 2848 | space is available. Return a new reference to the object that |
| 2849 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 2850 | (in which case no character was written) or NULL, if a |
| 2851 | reallocation error ocurred. The called must decref the result */ |
| 2852 | static |
| 2853 | PyObject *charmapencode_output(Py_UNICODE c, PyObject *mapping, |
| 2854 | PyObject **outobj, int *outpos) |
| 2855 | { |
| 2856 | PyObject *rep = charmapencode_lookup(c, mapping); |
| 2857 | |
| 2858 | if (rep==NULL) |
| 2859 | return NULL; |
| 2860 | else if (rep==Py_None) |
| 2861 | return rep; |
| 2862 | else { |
| 2863 | char *outstart = PyString_AS_STRING(*outobj); |
| 2864 | int outsize = PyString_GET_SIZE(*outobj); |
| 2865 | if (PyInt_Check(rep)) { |
| 2866 | int requiredsize = *outpos+1; |
| 2867 | if (outsize<requiredsize) { |
| 2868 | /* exponentially overallocate to minimize reallocations */ |
| 2869 | if (requiredsize < 2*outsize) |
| 2870 | requiredsize = 2*outsize; |
| 2871 | if (_PyString_Resize(outobj, requiredsize)) { |
| 2872 | Py_DECREF(rep); |
| 2873 | return NULL; |
| 2874 | } |
| 2875 | outstart = PyString_AS_STRING(*outobj); |
| 2876 | } |
| 2877 | outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); |
| 2878 | } |
| 2879 | else { |
| 2880 | const char *repchars = PyString_AS_STRING(rep); |
| 2881 | int repsize = PyString_GET_SIZE(rep); |
| 2882 | int requiredsize = *outpos+repsize; |
| 2883 | if (outsize<requiredsize) { |
| 2884 | /* exponentially overallocate to minimize reallocations */ |
| 2885 | if (requiredsize < 2*outsize) |
| 2886 | requiredsize = 2*outsize; |
| 2887 | if (_PyString_Resize(outobj, requiredsize)) { |
| 2888 | Py_DECREF(rep); |
| 2889 | return NULL; |
| 2890 | } |
| 2891 | outstart = PyString_AS_STRING(*outobj); |
| 2892 | } |
| 2893 | memcpy(outstart + *outpos, repchars, repsize); |
| 2894 | *outpos += repsize; |
| 2895 | } |
| 2896 | } |
| 2897 | return rep; |
| 2898 | } |
| 2899 | |
| 2900 | /* handle an error in PyUnicode_EncodeCharmap |
| 2901 | Return 0 on success, -1 on error */ |
| 2902 | static |
| 2903 | int charmap_encoding_error( |
| 2904 | const Py_UNICODE *p, int size, int *inpos, PyObject *mapping, |
| 2905 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 2906 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2907 | PyObject **res, int *respos) |
| 2908 | { |
| 2909 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 2910 | int repsize; |
| 2911 | int newpos; |
| 2912 | Py_UNICODE *uni2; |
| 2913 | /* startpos for collecting unencodable chars */ |
| 2914 | int collstartpos = *inpos; |
| 2915 | int collendpos = *inpos+1; |
| 2916 | int collpos; |
| 2917 | char *encoding = "charmap"; |
| 2918 | char *reason = "character maps to <undefined>"; |
| 2919 | |
| 2920 | PyObject *x; |
| 2921 | /* find all unencodable characters */ |
| 2922 | while (collendpos < size) { |
| 2923 | x = charmapencode_lookup(p[collendpos], mapping); |
| 2924 | if (x==NULL) |
| 2925 | return -1; |
| 2926 | else if (x!=Py_None) { |
| 2927 | Py_DECREF(x); |
| 2928 | break; |
| 2929 | } |
| 2930 | Py_DECREF(x); |
| 2931 | ++collendpos; |
| 2932 | } |
| 2933 | /* cache callback name lookup |
| 2934 | * (if not done yet, i.e. it's the first error) */ |
| 2935 | if (*known_errorHandler==-1) { |
| 2936 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 2937 | *known_errorHandler = 1; |
| 2938 | else if (!strcmp(errors, "replace")) |
| 2939 | *known_errorHandler = 2; |
| 2940 | else if (!strcmp(errors, "ignore")) |
| 2941 | *known_errorHandler = 3; |
| 2942 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 2943 | *known_errorHandler = 4; |
| 2944 | else |
| 2945 | *known_errorHandler = 0; |
| 2946 | } |
| 2947 | switch (*known_errorHandler) { |
| 2948 | case 1: /* strict */ |
| 2949 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 2950 | return -1; |
| 2951 | case 2: /* replace */ |
| 2952 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 2953 | x = charmapencode_output('?', mapping, res, respos); |
| 2954 | if (x==NULL) { |
| 2955 | return -1; |
| 2956 | } |
| 2957 | else if (x==Py_None) { |
| 2958 | Py_DECREF(x); |
| 2959 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 2960 | return -1; |
| 2961 | } |
| 2962 | Py_DECREF(x); |
| 2963 | } |
| 2964 | /* fall through */ |
| 2965 | case 3: /* ignore */ |
| 2966 | *inpos = collendpos; |
| 2967 | break; |
| 2968 | case 4: /* xmlcharrefreplace */ |
| 2969 | /* generate replacement (temporarily (mis)uses p) */ |
| 2970 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 2971 | char buffer[2+29+1+1]; |
| 2972 | char *cp; |
| 2973 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 2974 | for (cp = buffer; *cp; ++cp) { |
| 2975 | x = charmapencode_output(*cp, mapping, res, respos); |
| 2976 | if (x==NULL) |
| 2977 | return -1; |
| 2978 | else if (x==Py_None) { |
| 2979 | Py_DECREF(x); |
| 2980 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 2981 | return -1; |
| 2982 | } |
| 2983 | Py_DECREF(x); |
| 2984 | } |
| 2985 | } |
| 2986 | *inpos = collendpos; |
| 2987 | break; |
| 2988 | default: |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 2989 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2990 | encoding, reason, p, size, exceptionObject, |
| 2991 | collstartpos, collendpos, &newpos); |
| 2992 | if (repunicode == NULL) |
| 2993 | return -1; |
| 2994 | /* generate replacement */ |
| 2995 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 2996 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 2997 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 2998 | if (x==NULL) { |
| 2999 | Py_DECREF(repunicode); |
| 3000 | return -1; |
| 3001 | } |
| 3002 | else if (x==Py_None) { |
| 3003 | Py_DECREF(repunicode); |
| 3004 | Py_DECREF(x); |
| 3005 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3006 | return -1; |
| 3007 | } |
| 3008 | Py_DECREF(x); |
| 3009 | } |
| 3010 | *inpos = newpos; |
| 3011 | Py_DECREF(repunicode); |
| 3012 | } |
| 3013 | return 0; |
| 3014 | } |
| 3015 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3016 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 3017 | int size, |
| 3018 | PyObject *mapping, |
| 3019 | const char *errors) |
| 3020 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3021 | /* output object */ |
| 3022 | PyObject *res = NULL; |
| 3023 | /* current input position */ |
| 3024 | int inpos = 0; |
| 3025 | /* current output position */ |
| 3026 | int respos = 0; |
| 3027 | PyObject *errorHandler = NULL; |
| 3028 | PyObject *exc = NULL; |
| 3029 | /* the following variable is used for caching string comparisons |
| 3030 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 3031 | * 3=ignore, 4=xmlcharrefreplace */ |
| 3032 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3033 | |
| 3034 | /* Default to Latin-1 */ |
| 3035 | if (mapping == NULL) |
| 3036 | return PyUnicode_EncodeLatin1(p, size, errors); |
| 3037 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3038 | /* allocate enough for a simple encoding without |
| 3039 | replacements, if we need more, we'll resize */ |
| 3040 | res = PyString_FromStringAndSize(NULL, size); |
| 3041 | if (res == NULL) |
| 3042 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3043 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3044 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3045 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3046 | while (inpos<size) { |
| 3047 | /* try to encode it */ |
| 3048 | PyObject *x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 3049 | if (x==NULL) /* error */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3050 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3051 | if (x==Py_None) { /* unencodable character */ |
| 3052 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 3053 | &exc, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3054 | &known_errorHandler, &errorHandler, errors, |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 3055 | &res, &respos)) { |
| 3056 | Py_DECREF(x); |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3057 | goto onError; |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 3058 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3059 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3060 | else |
| 3061 | /* done with this character => adjust input position */ |
| 3062 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3063 | Py_DECREF(x); |
| 3064 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3065 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3066 | /* Resize if we allocated to much */ |
| 3067 | if (respos<PyString_GET_SIZE(res)) { |
| 3068 | if (_PyString_Resize(&res, respos)) |
| 3069 | goto onError; |
| 3070 | } |
| 3071 | Py_XDECREF(exc); |
| 3072 | Py_XDECREF(errorHandler); |
| 3073 | return res; |
| 3074 | |
| 3075 | onError: |
| 3076 | Py_XDECREF(res); |
| 3077 | Py_XDECREF(exc); |
| 3078 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3079 | return NULL; |
| 3080 | } |
| 3081 | |
| 3082 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 3083 | PyObject *mapping) |
| 3084 | { |
| 3085 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 3086 | PyErr_BadArgument(); |
| 3087 | return NULL; |
| 3088 | } |
| 3089 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 3090 | PyUnicode_GET_SIZE(unicode), |
| 3091 | mapping, |
| 3092 | NULL); |
| 3093 | } |
| 3094 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3095 | /* create or adjust a UnicodeTranslateError */ |
| 3096 | static void make_translate_exception(PyObject **exceptionObject, |
| 3097 | const Py_UNICODE *unicode, int size, |
| 3098 | int startpos, int endpos, |
| 3099 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3100 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3101 | if (*exceptionObject == NULL) { |
| 3102 | *exceptionObject = PyUnicodeTranslateError_Create( |
| 3103 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3104 | } |
| 3105 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3106 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 3107 | goto onError; |
| 3108 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 3109 | goto onError; |
| 3110 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 3111 | goto onError; |
| 3112 | return; |
| 3113 | onError: |
| 3114 | Py_DECREF(*exceptionObject); |
| 3115 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3116 | } |
| 3117 | } |
| 3118 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3119 | /* raises a UnicodeTranslateError */ |
| 3120 | static void raise_translate_exception(PyObject **exceptionObject, |
| 3121 | const Py_UNICODE *unicode, int size, |
| 3122 | int startpos, int endpos, |
| 3123 | const char *reason) |
| 3124 | { |
| 3125 | make_translate_exception(exceptionObject, |
| 3126 | unicode, size, startpos, endpos, reason); |
| 3127 | if (*exceptionObject != NULL) |
| 3128 | PyCodec_StrictErrors(*exceptionObject); |
| 3129 | } |
| 3130 | |
| 3131 | /* error handling callback helper: |
| 3132 | build arguments, call the callback and check the arguments, |
| 3133 | put the result into newpos and return the replacement string, which |
| 3134 | has to be freed by the caller */ |
| 3135 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 3136 | PyObject **errorHandler, |
| 3137 | const char *reason, |
| 3138 | const Py_UNICODE *unicode, int size, PyObject **exceptionObject, |
| 3139 | int startpos, int endpos, |
| 3140 | int *newpos) |
| 3141 | { |
| 3142 | static char *argparse = "O!i;translating error handler must return (unicode, int) tuple"; |
| 3143 | |
| 3144 | PyObject *restuple; |
| 3145 | PyObject *resunicode; |
| 3146 | |
| 3147 | if (*errorHandler == NULL) { |
| 3148 | *errorHandler = PyCodec_LookupError(errors); |
| 3149 | if (*errorHandler == NULL) |
| 3150 | return NULL; |
| 3151 | } |
| 3152 | |
| 3153 | make_translate_exception(exceptionObject, |
| 3154 | unicode, size, startpos, endpos, reason); |
| 3155 | if (*exceptionObject == NULL) |
| 3156 | return NULL; |
| 3157 | |
| 3158 | restuple = PyObject_CallFunctionObjArgs( |
| 3159 | *errorHandler, *exceptionObject, NULL); |
| 3160 | if (restuple == NULL) |
| 3161 | return NULL; |
| 3162 | if (!PyTuple_Check(restuple)) { |
| 3163 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 3164 | Py_DECREF(restuple); |
| 3165 | return NULL; |
| 3166 | } |
| 3167 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 3168 | &resunicode, newpos)) { |
| 3169 | Py_DECREF(restuple); |
| 3170 | return NULL; |
| 3171 | } |
| 3172 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3173 | *newpos = size+*newpos; |
| 3174 | if (*newpos<0 || *newpos>size) { |
| 3175 | PyErr_Format(PyExc_IndexError, "position %d from error handler out of bounds", *newpos); |
| 3176 | Py_DECREF(restuple); |
| 3177 | return NULL; |
| 3178 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3179 | Py_INCREF(resunicode); |
| 3180 | Py_DECREF(restuple); |
| 3181 | return resunicode; |
| 3182 | } |
| 3183 | |
| 3184 | /* Lookup the character ch in the mapping and put the result in result, |
| 3185 | which must be decrefed by the caller. |
| 3186 | Return 0 on success, -1 on error */ |
| 3187 | static |
| 3188 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 3189 | { |
| 3190 | PyObject *w = PyInt_FromLong((long)c); |
| 3191 | PyObject *x; |
| 3192 | |
| 3193 | if (w == NULL) |
| 3194 | return -1; |
| 3195 | x = PyObject_GetItem(mapping, w); |
| 3196 | Py_DECREF(w); |
| 3197 | if (x == NULL) { |
| 3198 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3199 | /* No mapping found means: use 1:1 mapping. */ |
| 3200 | PyErr_Clear(); |
| 3201 | *result = NULL; |
| 3202 | return 0; |
| 3203 | } else |
| 3204 | return -1; |
| 3205 | } |
| 3206 | else if (x == Py_None) { |
| 3207 | *result = x; |
| 3208 | return 0; |
| 3209 | } |
| 3210 | else if (PyInt_Check(x)) { |
| 3211 | long value = PyInt_AS_LONG(x); |
| 3212 | long max = PyUnicode_GetMax(); |
| 3213 | if (value < 0 || value > max) { |
| 3214 | PyErr_Format(PyExc_TypeError, |
| 3215 | "character mapping must be in range(0x%lx)", max+1); |
| 3216 | Py_DECREF(x); |
| 3217 | return -1; |
| 3218 | } |
| 3219 | *result = x; |
| 3220 | return 0; |
| 3221 | } |
| 3222 | else if (PyUnicode_Check(x)) { |
| 3223 | *result = x; |
| 3224 | return 0; |
| 3225 | } |
| 3226 | else { |
| 3227 | /* wrong return value */ |
| 3228 | PyErr_SetString(PyExc_TypeError, |
| 3229 | "character mapping must return integer, None or unicode"); |
Walter Dörwald | 150523e | 2003-08-15 16:52:19 +0000 | [diff] [blame] | 3230 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3231 | return -1; |
| 3232 | } |
| 3233 | } |
| 3234 | /* ensure that *outobj is at least requiredsize characters long, |
| 3235 | if not reallocate and adjust various state variables. |
| 3236 | Return 0 on success, -1 on error */ |
| 3237 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3238 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3239 | int requiredsize) |
| 3240 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3241 | int oldsize = PyUnicode_GET_SIZE(*outobj); |
| 3242 | if (requiredsize > oldsize) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3243 | /* remember old output position */ |
| 3244 | int outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
| 3245 | /* exponentially overallocate to minimize reallocations */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3246 | if (requiredsize < 2 * oldsize) |
| 3247 | requiredsize = 2 * oldsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3248 | if (_PyUnicode_Resize(outobj, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3249 | return -1; |
| 3250 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3251 | } |
| 3252 | return 0; |
| 3253 | } |
| 3254 | /* lookup the character, put the result in the output string and adjust |
| 3255 | various state variables. Return a new reference to the object that |
| 3256 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 3257 | undefined (in which case no character was written). |
| 3258 | The called must decref result. |
| 3259 | Return 0 on success, -1 on error. */ |
| 3260 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3261 | int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
| 3262 | int insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
| 3263 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3264 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3265 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3266 | return -1; |
| 3267 | if (*res==NULL) { |
| 3268 | /* not found => default to 1:1 mapping */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3269 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3270 | } |
| 3271 | else if (*res==Py_None) |
| 3272 | ; |
| 3273 | else if (PyInt_Check(*res)) { |
| 3274 | /* no overflow check, because we know that the space is enough */ |
| 3275 | *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); |
| 3276 | } |
| 3277 | else if (PyUnicode_Check(*res)) { |
| 3278 | int repsize = PyUnicode_GET_SIZE(*res); |
| 3279 | if (repsize==1) { |
| 3280 | /* no overflow check, because we know that the space is enough */ |
| 3281 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 3282 | } |
| 3283 | else if (repsize!=0) { |
| 3284 | /* more than one character */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3285 | int requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
| 3286 | (insize - (*curinp-*startinp)) + |
| 3287 | repsize - 1; |
| 3288 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3289 | return -1; |
| 3290 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 3291 | *outp += repsize; |
| 3292 | } |
| 3293 | } |
| 3294 | else |
| 3295 | return -1; |
| 3296 | return 0; |
| 3297 | } |
| 3298 | |
| 3299 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3300 | int size, |
| 3301 | PyObject *mapping, |
| 3302 | const char *errors) |
| 3303 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3304 | /* output object */ |
| 3305 | PyObject *res = NULL; |
| 3306 | /* pointers to the beginning and end+1 of input */ |
| 3307 | const Py_UNICODE *startp = p; |
| 3308 | const Py_UNICODE *endp = p + size; |
| 3309 | /* pointer into the output */ |
| 3310 | Py_UNICODE *str; |
| 3311 | /* current output position */ |
| 3312 | int respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3313 | char *reason = "character maps to <undefined>"; |
| 3314 | PyObject *errorHandler = NULL; |
| 3315 | PyObject *exc = NULL; |
| 3316 | /* the following variable is used for caching string comparisons |
| 3317 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 3318 | * 3=ignore, 4=xmlcharrefreplace */ |
| 3319 | int known_errorHandler = -1; |
| 3320 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3321 | if (mapping == NULL) { |
| 3322 | PyErr_BadArgument(); |
| 3323 | return NULL; |
| 3324 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3325 | |
| 3326 | /* allocate enough for a simple 1:1 translation without |
| 3327 | replacements, if we need more, we'll resize */ |
| 3328 | res = PyUnicode_FromUnicode(NULL, size); |
| 3329 | if (res == NULL) |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3330 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3331 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3332 | return res; |
| 3333 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3334 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3335 | while (p<endp) { |
| 3336 | /* try to encode it */ |
| 3337 | PyObject *x = NULL; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3338 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3339 | Py_XDECREF(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3340 | goto onError; |
| 3341 | } |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 3342 | Py_XDECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3343 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 3344 | ++p; |
| 3345 | else { /* untranslatable character */ |
| 3346 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 3347 | int repsize; |
| 3348 | int newpos; |
| 3349 | Py_UNICODE *uni2; |
| 3350 | /* startpos for collecting untranslatable chars */ |
| 3351 | const Py_UNICODE *collstart = p; |
| 3352 | const Py_UNICODE *collend = p+1; |
| 3353 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3354 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3355 | /* find all untranslatable characters */ |
| 3356 | while (collend < endp) { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3357 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3358 | goto onError; |
| 3359 | Py_XDECREF(x); |
| 3360 | if (x!=Py_None) |
| 3361 | break; |
| 3362 | ++collend; |
| 3363 | } |
| 3364 | /* cache callback name lookup |
| 3365 | * (if not done yet, i.e. it's the first error) */ |
| 3366 | if (known_errorHandler==-1) { |
| 3367 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3368 | known_errorHandler = 1; |
| 3369 | else if (!strcmp(errors, "replace")) |
| 3370 | known_errorHandler = 2; |
| 3371 | else if (!strcmp(errors, "ignore")) |
| 3372 | known_errorHandler = 3; |
| 3373 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3374 | known_errorHandler = 4; |
| 3375 | else |
| 3376 | known_errorHandler = 0; |
| 3377 | } |
| 3378 | switch (known_errorHandler) { |
| 3379 | case 1: /* strict */ |
| 3380 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
| 3381 | goto onError; |
| 3382 | case 2: /* replace */ |
| 3383 | /* No need to check for space, this is a 1:1 replacement */ |
| 3384 | for (coll = collstart; coll<collend; ++coll) |
| 3385 | *str++ = '?'; |
| 3386 | /* fall through */ |
| 3387 | case 3: /* ignore */ |
| 3388 | p = collend; |
| 3389 | break; |
| 3390 | case 4: /* xmlcharrefreplace */ |
| 3391 | /* generate replacement (temporarily (mis)uses p) */ |
| 3392 | for (p = collstart; p < collend; ++p) { |
| 3393 | char buffer[2+29+1+1]; |
| 3394 | char *cp; |
| 3395 | sprintf(buffer, "&#%d;", (int)*p); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3396 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3397 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 3398 | goto onError; |
| 3399 | for (cp = buffer; *cp; ++cp) |
| 3400 | *str++ = *cp; |
| 3401 | } |
| 3402 | p = collend; |
| 3403 | break; |
| 3404 | default: |
| 3405 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 3406 | reason, startp, size, &exc, |
| 3407 | collstart-startp, collend-startp, &newpos); |
| 3408 | if (repunicode == NULL) |
| 3409 | goto onError; |
| 3410 | /* generate replacement */ |
| 3411 | repsize = PyUnicode_GET_SIZE(repunicode); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3412 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3413 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 3414 | Py_DECREF(repunicode); |
| 3415 | goto onError; |
| 3416 | } |
| 3417 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 3418 | *str++ = *uni2; |
| 3419 | p = startp + newpos; |
| 3420 | Py_DECREF(repunicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3421 | } |
| 3422 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3423 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3424 | /* Resize if we allocated to much */ |
| 3425 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3426 | if (respos<PyUnicode_GET_SIZE(res)) { |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3427 | if (_PyUnicode_Resize(&res, respos) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3428 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3429 | } |
| 3430 | Py_XDECREF(exc); |
| 3431 | Py_XDECREF(errorHandler); |
| 3432 | return res; |
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 | onError: |
| 3435 | Py_XDECREF(res); |
| 3436 | Py_XDECREF(exc); |
| 3437 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3438 | return NULL; |
| 3439 | } |
| 3440 | |
| 3441 | PyObject *PyUnicode_Translate(PyObject *str, |
| 3442 | PyObject *mapping, |
| 3443 | const char *errors) |
| 3444 | { |
| 3445 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3446 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3447 | str = PyUnicode_FromObject(str); |
| 3448 | if (str == NULL) |
| 3449 | goto onError; |
| 3450 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 3451 | PyUnicode_GET_SIZE(str), |
| 3452 | mapping, |
| 3453 | errors); |
| 3454 | Py_DECREF(str); |
| 3455 | return 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 | onError: |
| 3458 | Py_XDECREF(str); |
| 3459 | return NULL; |
| 3460 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3461 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3462 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 3463 | |
| 3464 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 3465 | int length, |
| 3466 | char *output, |
| 3467 | const char *errors) |
| 3468 | { |
| 3469 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3470 | PyObject *errorHandler = NULL; |
| 3471 | PyObject *exc = NULL; |
| 3472 | const char *encoding = "decimal"; |
| 3473 | const char *reason = "invalid decimal Unicode string"; |
| 3474 | /* the following variable is used for caching string comparisons |
| 3475 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 3476 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3477 | |
| 3478 | if (output == NULL) { |
| 3479 | PyErr_BadArgument(); |
| 3480 | return -1; |
| 3481 | } |
| 3482 | |
| 3483 | p = s; |
| 3484 | end = s + length; |
| 3485 | while (p < end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3486 | register Py_UNICODE ch = *p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3487 | int decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3488 | PyObject *repunicode; |
| 3489 | int repsize; |
| 3490 | int newpos; |
| 3491 | Py_UNICODE *uni2; |
| 3492 | Py_UNICODE *collstart; |
| 3493 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3494 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3495 | if (Py_UNICODE_ISSPACE(ch)) { |
| 3496 | *output++ = ' '; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3497 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3498 | continue; |
| 3499 | } |
| 3500 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 3501 | if (decimal >= 0) { |
| 3502 | *output++ = '0' + decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3503 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3504 | continue; |
| 3505 | } |
Guido van Rossum | ba47704 | 2000-04-06 18:18:10 +0000 | [diff] [blame] | 3506 | if (0 < ch && ch < 256) { |
Guido van Rossum | 42c29aa | 2000-05-03 23:58:29 +0000 | [diff] [blame] | 3507 | *output++ = (char)ch; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3508 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3509 | continue; |
| 3510 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3511 | /* All other characters are considered unencodable */ |
| 3512 | collstart = p; |
| 3513 | collend = p+1; |
| 3514 | while (collend < end) { |
| 3515 | if ((0 < *collend && *collend < 256) || |
| 3516 | !Py_UNICODE_ISSPACE(*collend) || |
| 3517 | Py_UNICODE_TODECIMAL(*collend)) |
| 3518 | break; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3519 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3520 | /* cache callback name lookup |
| 3521 | * (if not done yet, i.e. it's the first error) */ |
| 3522 | if (known_errorHandler==-1) { |
| 3523 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3524 | known_errorHandler = 1; |
| 3525 | else if (!strcmp(errors, "replace")) |
| 3526 | known_errorHandler = 2; |
| 3527 | else if (!strcmp(errors, "ignore")) |
| 3528 | known_errorHandler = 3; |
| 3529 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3530 | known_errorHandler = 4; |
| 3531 | else |
| 3532 | known_errorHandler = 0; |
| 3533 | } |
| 3534 | switch (known_errorHandler) { |
| 3535 | case 1: /* strict */ |
| 3536 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 3537 | goto onError; |
| 3538 | case 2: /* replace */ |
| 3539 | for (p = collstart; p < collend; ++p) |
| 3540 | *output++ = '?'; |
| 3541 | /* fall through */ |
| 3542 | case 3: /* ignore */ |
| 3543 | p = collend; |
| 3544 | break; |
| 3545 | case 4: /* xmlcharrefreplace */ |
| 3546 | /* generate replacement (temporarily (mis)uses p) */ |
| 3547 | for (p = collstart; p < collend; ++p) |
| 3548 | output += sprintf(output, "&#%d;", (int)*p); |
| 3549 | p = collend; |
| 3550 | break; |
| 3551 | default: |
| 3552 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 3553 | encoding, reason, s, length, &exc, |
| 3554 | collstart-s, collend-s, &newpos); |
| 3555 | if (repunicode == NULL) |
| 3556 | goto onError; |
| 3557 | /* generate replacement */ |
| 3558 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3559 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 3560 | Py_UNICODE ch = *uni2; |
| 3561 | if (Py_UNICODE_ISSPACE(ch)) |
| 3562 | *output++ = ' '; |
| 3563 | else { |
| 3564 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 3565 | if (decimal >= 0) |
| 3566 | *output++ = '0' + decimal; |
| 3567 | else if (0 < ch && ch < 256) |
| 3568 | *output++ = (char)ch; |
| 3569 | else { |
| 3570 | Py_DECREF(repunicode); |
| 3571 | raise_encode_exception(&exc, encoding, |
| 3572 | s, length, collstart-s, collend-s, reason); |
| 3573 | goto onError; |
| 3574 | } |
| 3575 | } |
| 3576 | } |
| 3577 | p = s + newpos; |
| 3578 | Py_DECREF(repunicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3579 | } |
| 3580 | } |
| 3581 | /* 0-terminate the output string */ |
| 3582 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3583 | Py_XDECREF(exc); |
| 3584 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3585 | return 0; |
| 3586 | |
| 3587 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3588 | Py_XDECREF(exc); |
| 3589 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3590 | return -1; |
| 3591 | } |
| 3592 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3593 | /* --- Helpers ------------------------------------------------------------ */ |
| 3594 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3595 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3596 | int count(PyUnicodeObject *self, |
| 3597 | int start, |
| 3598 | int end, |
| 3599 | PyUnicodeObject *substring) |
| 3600 | { |
| 3601 | int count = 0; |
| 3602 | |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3603 | if (start < 0) |
| 3604 | start += self->length; |
| 3605 | if (start < 0) |
| 3606 | start = 0; |
| 3607 | if (end > self->length) |
| 3608 | end = self->length; |
| 3609 | if (end < 0) |
| 3610 | end += self->length; |
| 3611 | if (end < 0) |
| 3612 | end = 0; |
| 3613 | |
Marc-André Lemburg | 49ef6dc | 2000-06-18 22:25:22 +0000 | [diff] [blame] | 3614 | if (substring->length == 0) |
| 3615 | return (end - start + 1); |
| 3616 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3617 | end -= substring->length; |
| 3618 | |
| 3619 | while (start <= end) |
| 3620 | if (Py_UNICODE_MATCH(self, start, substring)) { |
| 3621 | count++; |
| 3622 | start += substring->length; |
| 3623 | } else |
| 3624 | start++; |
| 3625 | |
| 3626 | return count; |
| 3627 | } |
| 3628 | |
| 3629 | int PyUnicode_Count(PyObject *str, |
| 3630 | PyObject *substr, |
| 3631 | int start, |
| 3632 | int end) |
| 3633 | { |
| 3634 | int result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3635 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3636 | str = PyUnicode_FromObject(str); |
| 3637 | if (str == NULL) |
| 3638 | return -1; |
| 3639 | substr = PyUnicode_FromObject(substr); |
| 3640 | if (substr == NULL) { |
Marc-André Lemburg | 49ef6dc | 2000-06-18 22:25:22 +0000 | [diff] [blame] | 3641 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3642 | return -1; |
| 3643 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3644 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3645 | result = count((PyUnicodeObject *)str, |
| 3646 | start, end, |
| 3647 | (PyUnicodeObject *)substr); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3648 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3649 | Py_DECREF(str); |
| 3650 | Py_DECREF(substr); |
| 3651 | return result; |
| 3652 | } |
| 3653 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3654 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3655 | int findstring(PyUnicodeObject *self, |
| 3656 | PyUnicodeObject *substring, |
| 3657 | int start, |
| 3658 | int end, |
| 3659 | int direction) |
| 3660 | { |
| 3661 | if (start < 0) |
| 3662 | start += self->length; |
| 3663 | if (start < 0) |
| 3664 | start = 0; |
| 3665 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3666 | if (end > self->length) |
| 3667 | end = self->length; |
| 3668 | if (end < 0) |
| 3669 | end += self->length; |
| 3670 | if (end < 0) |
| 3671 | end = 0; |
| 3672 | |
Guido van Rossum | 76afbd9 | 2002-08-20 17:29:29 +0000 | [diff] [blame] | 3673 | if (substring->length == 0) |
| 3674 | return (direction > 0) ? start : end; |
| 3675 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3676 | end -= substring->length; |
| 3677 | |
| 3678 | if (direction < 0) { |
| 3679 | for (; end >= start; end--) |
| 3680 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 3681 | return end; |
| 3682 | } else { |
| 3683 | for (; start <= end; start++) |
| 3684 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 3685 | return start; |
| 3686 | } |
| 3687 | |
| 3688 | return -1; |
| 3689 | } |
| 3690 | |
| 3691 | int PyUnicode_Find(PyObject *str, |
| 3692 | PyObject *substr, |
| 3693 | int start, |
| 3694 | int end, |
| 3695 | int direction) |
| 3696 | { |
| 3697 | int result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3698 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3699 | str = PyUnicode_FromObject(str); |
| 3700 | if (str == NULL) |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 3701 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3702 | substr = PyUnicode_FromObject(substr); |
| 3703 | if (substr == NULL) { |
Marc-André Lemburg | 4164439 | 2002-05-29 13:46:29 +0000 | [diff] [blame] | 3704 | Py_DECREF(str); |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 3705 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3706 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3707 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3708 | result = findstring((PyUnicodeObject *)str, |
| 3709 | (PyUnicodeObject *)substr, |
| 3710 | start, end, direction); |
| 3711 | Py_DECREF(str); |
| 3712 | Py_DECREF(substr); |
| 3713 | return result; |
| 3714 | } |
| 3715 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3716 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3717 | int tailmatch(PyUnicodeObject *self, |
| 3718 | PyUnicodeObject *substring, |
| 3719 | int start, |
| 3720 | int end, |
| 3721 | int direction) |
| 3722 | { |
| 3723 | if (start < 0) |
| 3724 | start += self->length; |
| 3725 | if (start < 0) |
| 3726 | start = 0; |
| 3727 | |
| 3728 | if (substring->length == 0) |
| 3729 | return 1; |
| 3730 | |
| 3731 | if (end > self->length) |
| 3732 | end = self->length; |
| 3733 | if (end < 0) |
| 3734 | end += self->length; |
| 3735 | if (end < 0) |
| 3736 | end = 0; |
| 3737 | |
| 3738 | end -= substring->length; |
| 3739 | if (end < start) |
| 3740 | return 0; |
| 3741 | |
| 3742 | if (direction > 0) { |
| 3743 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 3744 | return 1; |
| 3745 | } else { |
| 3746 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 3747 | return 1; |
| 3748 | } |
| 3749 | |
| 3750 | return 0; |
| 3751 | } |
| 3752 | |
| 3753 | int PyUnicode_Tailmatch(PyObject *str, |
| 3754 | PyObject *substr, |
| 3755 | int start, |
| 3756 | int end, |
| 3757 | int direction) |
| 3758 | { |
| 3759 | int result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3760 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3761 | str = PyUnicode_FromObject(str); |
| 3762 | if (str == NULL) |
| 3763 | return -1; |
| 3764 | substr = PyUnicode_FromObject(substr); |
| 3765 | if (substr == NULL) { |
| 3766 | Py_DECREF(substr); |
| 3767 | return -1; |
| 3768 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3769 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3770 | result = tailmatch((PyUnicodeObject *)str, |
| 3771 | (PyUnicodeObject *)substr, |
| 3772 | start, end, direction); |
| 3773 | Py_DECREF(str); |
| 3774 | Py_DECREF(substr); |
| 3775 | return result; |
| 3776 | } |
| 3777 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3778 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3779 | const Py_UNICODE *findchar(const Py_UNICODE *s, |
| 3780 | int size, |
| 3781 | Py_UNICODE ch) |
| 3782 | { |
| 3783 | /* like wcschr, but doesn't stop at NULL characters */ |
| 3784 | |
| 3785 | while (size-- > 0) { |
| 3786 | if (*s == ch) |
| 3787 | return s; |
| 3788 | s++; |
| 3789 | } |
| 3790 | |
| 3791 | return NULL; |
| 3792 | } |
| 3793 | |
| 3794 | /* Apply fixfct filter to the Unicode object self and return a |
| 3795 | reference to the modified object */ |
| 3796 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3797 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3798 | PyObject *fixup(PyUnicodeObject *self, |
| 3799 | int (*fixfct)(PyUnicodeObject *s)) |
| 3800 | { |
| 3801 | |
| 3802 | PyUnicodeObject *u; |
| 3803 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3804 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3805 | if (u == NULL) |
| 3806 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3807 | |
| 3808 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 3809 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 3810 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3811 | /* fixfct should return TRUE if it modified the buffer. If |
| 3812 | FALSE, return a reference to the original buffer instead |
| 3813 | (to save space, not time) */ |
| 3814 | Py_INCREF(self); |
| 3815 | Py_DECREF(u); |
| 3816 | return (PyObject*) self; |
| 3817 | } |
| 3818 | return (PyObject*) u; |
| 3819 | } |
| 3820 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3821 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3822 | int fixupper(PyUnicodeObject *self) |
| 3823 | { |
| 3824 | int len = self->length; |
| 3825 | Py_UNICODE *s = self->str; |
| 3826 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3827 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3828 | while (len-- > 0) { |
| 3829 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3830 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3831 | ch = Py_UNICODE_TOUPPER(*s); |
| 3832 | if (ch != *s) { |
| 3833 | status = 1; |
| 3834 | *s = ch; |
| 3835 | } |
| 3836 | s++; |
| 3837 | } |
| 3838 | |
| 3839 | return status; |
| 3840 | } |
| 3841 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3842 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3843 | int fixlower(PyUnicodeObject *self) |
| 3844 | { |
| 3845 | int len = self->length; |
| 3846 | Py_UNICODE *s = self->str; |
| 3847 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3848 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3849 | while (len-- > 0) { |
| 3850 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3851 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3852 | ch = Py_UNICODE_TOLOWER(*s); |
| 3853 | if (ch != *s) { |
| 3854 | status = 1; |
| 3855 | *s = ch; |
| 3856 | } |
| 3857 | s++; |
| 3858 | } |
| 3859 | |
| 3860 | return status; |
| 3861 | } |
| 3862 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3863 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3864 | int fixswapcase(PyUnicodeObject *self) |
| 3865 | { |
| 3866 | int len = self->length; |
| 3867 | Py_UNICODE *s = self->str; |
| 3868 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3869 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3870 | while (len-- > 0) { |
| 3871 | if (Py_UNICODE_ISUPPER(*s)) { |
| 3872 | *s = Py_UNICODE_TOLOWER(*s); |
| 3873 | status = 1; |
| 3874 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 3875 | *s = Py_UNICODE_TOUPPER(*s); |
| 3876 | status = 1; |
| 3877 | } |
| 3878 | s++; |
| 3879 | } |
| 3880 | |
| 3881 | return status; |
| 3882 | } |
| 3883 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3884 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3885 | int fixcapitalize(PyUnicodeObject *self) |
| 3886 | { |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 3887 | int len = self->length; |
| 3888 | Py_UNICODE *s = self->str; |
| 3889 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3890 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 3891 | if (len == 0) |
| 3892 | return 0; |
| 3893 | if (Py_UNICODE_ISLOWER(*s)) { |
| 3894 | *s = Py_UNICODE_TOUPPER(*s); |
| 3895 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3896 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 3897 | s++; |
| 3898 | while (--len > 0) { |
| 3899 | if (Py_UNICODE_ISUPPER(*s)) { |
| 3900 | *s = Py_UNICODE_TOLOWER(*s); |
| 3901 | status = 1; |
| 3902 | } |
| 3903 | s++; |
| 3904 | } |
| 3905 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3906 | } |
| 3907 | |
| 3908 | static |
| 3909 | int fixtitle(PyUnicodeObject *self) |
| 3910 | { |
| 3911 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 3912 | register Py_UNICODE *e; |
| 3913 | int previous_is_cased; |
| 3914 | |
| 3915 | /* Shortcut for single character strings */ |
| 3916 | if (PyUnicode_GET_SIZE(self) == 1) { |
| 3917 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 3918 | if (*p != ch) { |
| 3919 | *p = ch; |
| 3920 | return 1; |
| 3921 | } |
| 3922 | else |
| 3923 | return 0; |
| 3924 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3925 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3926 | e = p + PyUnicode_GET_SIZE(self); |
| 3927 | previous_is_cased = 0; |
| 3928 | for (; p < e; p++) { |
| 3929 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3930 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3931 | if (previous_is_cased) |
| 3932 | *p = Py_UNICODE_TOLOWER(ch); |
| 3933 | else |
| 3934 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3935 | |
| 3936 | if (Py_UNICODE_ISLOWER(ch) || |
| 3937 | Py_UNICODE_ISUPPER(ch) || |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3938 | Py_UNICODE_ISTITLE(ch)) |
| 3939 | previous_is_cased = 1; |
| 3940 | else |
| 3941 | previous_is_cased = 0; |
| 3942 | } |
| 3943 | return 1; |
| 3944 | } |
| 3945 | |
| 3946 | PyObject *PyUnicode_Join(PyObject *separator, |
| 3947 | PyObject *seq) |
| 3948 | { |
| 3949 | Py_UNICODE *sep; |
| 3950 | int seplen; |
| 3951 | PyUnicodeObject *res = NULL; |
| 3952 | int reslen = 0; |
| 3953 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3954 | int sz = 100; |
| 3955 | int i; |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 3956 | PyObject *it; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3957 | |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 3958 | it = PyObject_GetIter(seq); |
| 3959 | if (it == NULL) |
| 3960 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3961 | |
| 3962 | if (separator == NULL) { |
| 3963 | Py_UNICODE blank = ' '; |
| 3964 | sep = ␣ |
| 3965 | seplen = 1; |
| 3966 | } |
| 3967 | else { |
| 3968 | separator = PyUnicode_FromObject(separator); |
| 3969 | if (separator == NULL) |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 3970 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3971 | sep = PyUnicode_AS_UNICODE(separator); |
| 3972 | seplen = PyUnicode_GET_SIZE(separator); |
| 3973 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3974 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3975 | res = _PyUnicode_New(sz); |
| 3976 | if (res == NULL) |
| 3977 | goto onError; |
| 3978 | p = PyUnicode_AS_UNICODE(res); |
| 3979 | reslen = 0; |
| 3980 | |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 3981 | for (i = 0; ; ++i) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3982 | int itemlen; |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 3983 | PyObject *item = PyIter_Next(it); |
| 3984 | if (item == NULL) { |
| 3985 | if (PyErr_Occurred()) |
| 3986 | goto onError; |
| 3987 | break; |
| 3988 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3989 | if (!PyUnicode_Check(item)) { |
| 3990 | PyObject *v; |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 3991 | if (!PyString_Check(item)) { |
| 3992 | PyErr_Format(PyExc_TypeError, |
| 3993 | "sequence item %i: expected string or Unicode," |
| 3994 | " %.80s found", |
| 3995 | i, item->ob_type->tp_name); |
| 3996 | Py_DECREF(item); |
| 3997 | goto onError; |
| 3998 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3999 | v = PyUnicode_FromObject(item); |
| 4000 | Py_DECREF(item); |
| 4001 | item = v; |
| 4002 | if (item == NULL) |
| 4003 | goto onError; |
| 4004 | } |
| 4005 | itemlen = PyUnicode_GET_SIZE(item); |
| 4006 | while (reslen + itemlen + seplen >= sz) { |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4007 | if (_PyUnicode_Resize(&res, sz*2) < 0) { |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4008 | Py_DECREF(item); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4009 | goto onError; |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4010 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4011 | sz *= 2; |
| 4012 | p = PyUnicode_AS_UNICODE(res) + reslen; |
| 4013 | } |
| 4014 | if (i > 0) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4015 | Py_UNICODE_COPY(p, sep, seplen); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4016 | p += seplen; |
| 4017 | reslen += seplen; |
| 4018 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4019 | Py_UNICODE_COPY(p, PyUnicode_AS_UNICODE(item), itemlen); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4020 | p += itemlen; |
| 4021 | reslen += itemlen; |
| 4022 | Py_DECREF(item); |
| 4023 | } |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4024 | if (_PyUnicode_Resize(&res, reslen) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4025 | goto onError; |
| 4026 | |
| 4027 | Py_XDECREF(separator); |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 4028 | Py_DECREF(it); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4029 | return (PyObject *)res; |
| 4030 | |
| 4031 | onError: |
| 4032 | Py_XDECREF(separator); |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 4033 | Py_XDECREF(res); |
| 4034 | Py_DECREF(it); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4035 | return NULL; |
| 4036 | } |
| 4037 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4038 | static |
| 4039 | PyUnicodeObject *pad(PyUnicodeObject *self, |
| 4040 | int left, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4041 | int right, |
| 4042 | Py_UNICODE fill) |
| 4043 | { |
| 4044 | PyUnicodeObject *u; |
| 4045 | |
| 4046 | if (left < 0) |
| 4047 | left = 0; |
| 4048 | if (right < 0) |
| 4049 | right = 0; |
| 4050 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4051 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4052 | Py_INCREF(self); |
| 4053 | return self; |
| 4054 | } |
| 4055 | |
| 4056 | u = _PyUnicode_New(left + self->length + right); |
| 4057 | if (u) { |
| 4058 | if (left) |
| 4059 | Py_UNICODE_FILL(u->str, fill, left); |
| 4060 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 4061 | if (right) |
| 4062 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 4063 | } |
| 4064 | |
| 4065 | return u; |
| 4066 | } |
| 4067 | |
| 4068 | #define SPLIT_APPEND(data, left, right) \ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4069 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4070 | if (!str) \ |
| 4071 | goto onError; \ |
| 4072 | if (PyList_Append(list, str)) { \ |
| 4073 | Py_DECREF(str); \ |
| 4074 | goto onError; \ |
| 4075 | } \ |
| 4076 | else \ |
| 4077 | Py_DECREF(str); |
| 4078 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4079 | #define SPLIT_INSERT(data, left, right) \ |
| 4080 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
| 4081 | if (!str) \ |
| 4082 | goto onError; \ |
| 4083 | if (PyList_Insert(list, 0, str)) { \ |
| 4084 | Py_DECREF(str); \ |
| 4085 | goto onError; \ |
| 4086 | } \ |
| 4087 | else \ |
| 4088 | Py_DECREF(str); |
| 4089 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4090 | static |
| 4091 | PyObject *split_whitespace(PyUnicodeObject *self, |
| 4092 | PyObject *list, |
| 4093 | int maxcount) |
| 4094 | { |
| 4095 | register int i; |
| 4096 | register int j; |
| 4097 | int len = self->length; |
| 4098 | PyObject *str; |
| 4099 | |
| 4100 | for (i = j = 0; i < len; ) { |
| 4101 | /* find a token */ |
| 4102 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 4103 | i++; |
| 4104 | j = i; |
| 4105 | while (i < len && !Py_UNICODE_ISSPACE(self->str[i])) |
| 4106 | i++; |
| 4107 | if (j < i) { |
| 4108 | if (maxcount-- <= 0) |
| 4109 | break; |
| 4110 | SPLIT_APPEND(self->str, j, i); |
| 4111 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 4112 | i++; |
| 4113 | j = i; |
| 4114 | } |
| 4115 | } |
| 4116 | if (j < len) { |
| 4117 | SPLIT_APPEND(self->str, j, len); |
| 4118 | } |
| 4119 | return list; |
| 4120 | |
| 4121 | onError: |
| 4122 | Py_DECREF(list); |
| 4123 | return NULL; |
| 4124 | } |
| 4125 | |
| 4126 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4127 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4128 | { |
| 4129 | register int i; |
| 4130 | register int j; |
| 4131 | int len; |
| 4132 | PyObject *list; |
| 4133 | PyObject *str; |
| 4134 | Py_UNICODE *data; |
| 4135 | |
| 4136 | string = PyUnicode_FromObject(string); |
| 4137 | if (string == NULL) |
| 4138 | return NULL; |
| 4139 | data = PyUnicode_AS_UNICODE(string); |
| 4140 | len = PyUnicode_GET_SIZE(string); |
| 4141 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4142 | list = PyList_New(0); |
| 4143 | if (!list) |
| 4144 | goto onError; |
| 4145 | |
| 4146 | for (i = j = 0; i < len; ) { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4147 | int eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4148 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4149 | /* Find a line and append it */ |
| 4150 | while (i < len && !Py_UNICODE_ISLINEBREAK(data[i])) |
| 4151 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4152 | |
| 4153 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4154 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4155 | if (i < len) { |
| 4156 | if (data[i] == '\r' && i + 1 < len && |
| 4157 | data[i+1] == '\n') |
| 4158 | i += 2; |
| 4159 | else |
| 4160 | i++; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4161 | if (keepends) |
| 4162 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4163 | } |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4164 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4165 | j = i; |
| 4166 | } |
| 4167 | if (j < len) { |
| 4168 | SPLIT_APPEND(data, j, len); |
| 4169 | } |
| 4170 | |
| 4171 | Py_DECREF(string); |
| 4172 | return list; |
| 4173 | |
| 4174 | onError: |
| 4175 | Py_DECREF(list); |
| 4176 | Py_DECREF(string); |
| 4177 | return NULL; |
| 4178 | } |
| 4179 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4180 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4181 | PyObject *split_char(PyUnicodeObject *self, |
| 4182 | PyObject *list, |
| 4183 | Py_UNICODE ch, |
| 4184 | int maxcount) |
| 4185 | { |
| 4186 | register int i; |
| 4187 | register int j; |
| 4188 | int len = self->length; |
| 4189 | PyObject *str; |
| 4190 | |
| 4191 | for (i = j = 0; i < len; ) { |
| 4192 | if (self->str[i] == ch) { |
| 4193 | if (maxcount-- <= 0) |
| 4194 | break; |
| 4195 | SPLIT_APPEND(self->str, j, i); |
| 4196 | i = j = i + 1; |
| 4197 | } else |
| 4198 | i++; |
| 4199 | } |
| 4200 | if (j <= len) { |
| 4201 | SPLIT_APPEND(self->str, j, len); |
| 4202 | } |
| 4203 | return list; |
| 4204 | |
| 4205 | onError: |
| 4206 | Py_DECREF(list); |
| 4207 | return NULL; |
| 4208 | } |
| 4209 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4210 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4211 | PyObject *split_substring(PyUnicodeObject *self, |
| 4212 | PyObject *list, |
| 4213 | PyUnicodeObject *substring, |
| 4214 | int maxcount) |
| 4215 | { |
| 4216 | register int i; |
| 4217 | register int j; |
| 4218 | int len = self->length; |
| 4219 | int sublen = substring->length; |
| 4220 | PyObject *str; |
| 4221 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 4222 | for (i = j = 0; i <= len - sublen; ) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4223 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 4224 | if (maxcount-- <= 0) |
| 4225 | break; |
| 4226 | SPLIT_APPEND(self->str, j, i); |
| 4227 | i = j = i + sublen; |
| 4228 | } else |
| 4229 | i++; |
| 4230 | } |
| 4231 | if (j <= len) { |
| 4232 | SPLIT_APPEND(self->str, j, len); |
| 4233 | } |
| 4234 | return list; |
| 4235 | |
| 4236 | onError: |
| 4237 | Py_DECREF(list); |
| 4238 | return NULL; |
| 4239 | } |
| 4240 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4241 | static |
| 4242 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
| 4243 | PyObject *list, |
| 4244 | int maxcount) |
| 4245 | { |
| 4246 | register int i; |
| 4247 | register int j; |
| 4248 | int len = self->length; |
| 4249 | PyObject *str; |
| 4250 | |
| 4251 | for (i = j = len - 1; i >= 0; ) { |
| 4252 | /* find a token */ |
| 4253 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 4254 | i--; |
| 4255 | j = i; |
| 4256 | while (i >= 0 && !Py_UNICODE_ISSPACE(self->str[i])) |
| 4257 | i--; |
| 4258 | if (j > i) { |
| 4259 | if (maxcount-- <= 0) |
| 4260 | break; |
| 4261 | SPLIT_INSERT(self->str, i + 1, j + 1); |
| 4262 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 4263 | i--; |
| 4264 | j = i; |
| 4265 | } |
| 4266 | } |
| 4267 | if (j >= 0) { |
| 4268 | SPLIT_INSERT(self->str, 0, j + 1); |
| 4269 | } |
| 4270 | return list; |
| 4271 | |
| 4272 | onError: |
| 4273 | Py_DECREF(list); |
| 4274 | return NULL; |
| 4275 | } |
| 4276 | |
| 4277 | static |
| 4278 | PyObject *rsplit_char(PyUnicodeObject *self, |
| 4279 | PyObject *list, |
| 4280 | Py_UNICODE ch, |
| 4281 | int maxcount) |
| 4282 | { |
| 4283 | register int i; |
| 4284 | register int j; |
| 4285 | int len = self->length; |
| 4286 | PyObject *str; |
| 4287 | |
| 4288 | for (i = j = len - 1; i >= 0; ) { |
| 4289 | if (self->str[i] == ch) { |
| 4290 | if (maxcount-- <= 0) |
| 4291 | break; |
| 4292 | SPLIT_INSERT(self->str, i + 1, j + 1); |
| 4293 | j = i = i - 1; |
| 4294 | } else |
| 4295 | i--; |
| 4296 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 4297 | if (j >= -1) { |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4298 | SPLIT_INSERT(self->str, 0, j + 1); |
| 4299 | } |
| 4300 | return list; |
| 4301 | |
| 4302 | onError: |
| 4303 | Py_DECREF(list); |
| 4304 | return NULL; |
| 4305 | } |
| 4306 | |
| 4307 | static |
| 4308 | PyObject *rsplit_substring(PyUnicodeObject *self, |
| 4309 | PyObject *list, |
| 4310 | PyUnicodeObject *substring, |
| 4311 | int maxcount) |
| 4312 | { |
| 4313 | register int i; |
| 4314 | register int j; |
| 4315 | int len = self->length; |
| 4316 | int sublen = substring->length; |
| 4317 | PyObject *str; |
| 4318 | |
| 4319 | for (i = len - sublen, j = len; i >= 0; ) { |
| 4320 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 4321 | if (maxcount-- <= 0) |
| 4322 | break; |
| 4323 | SPLIT_INSERT(self->str, i + sublen, j); |
| 4324 | j = i; |
| 4325 | i -= sublen; |
| 4326 | } else |
| 4327 | i--; |
| 4328 | } |
| 4329 | if (j >= 0) { |
| 4330 | SPLIT_INSERT(self->str, 0, j); |
| 4331 | } |
| 4332 | return list; |
| 4333 | |
| 4334 | onError: |
| 4335 | Py_DECREF(list); |
| 4336 | return NULL; |
| 4337 | } |
| 4338 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4339 | #undef SPLIT_APPEND |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4340 | #undef SPLIT_INSERT |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4341 | |
| 4342 | static |
| 4343 | PyObject *split(PyUnicodeObject *self, |
| 4344 | PyUnicodeObject *substring, |
| 4345 | int maxcount) |
| 4346 | { |
| 4347 | PyObject *list; |
| 4348 | |
| 4349 | if (maxcount < 0) |
| 4350 | maxcount = INT_MAX; |
| 4351 | |
| 4352 | list = PyList_New(0); |
| 4353 | if (!list) |
| 4354 | return NULL; |
| 4355 | |
| 4356 | if (substring == NULL) |
| 4357 | return split_whitespace(self,list,maxcount); |
| 4358 | |
| 4359 | else if (substring->length == 1) |
| 4360 | return split_char(self,list,substring->str[0],maxcount); |
| 4361 | |
| 4362 | else if (substring->length == 0) { |
| 4363 | Py_DECREF(list); |
| 4364 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 4365 | return NULL; |
| 4366 | } |
| 4367 | else |
| 4368 | return split_substring(self,list,substring,maxcount); |
| 4369 | } |
| 4370 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4371 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4372 | PyObject *rsplit(PyUnicodeObject *self, |
| 4373 | PyUnicodeObject *substring, |
| 4374 | int maxcount) |
| 4375 | { |
| 4376 | PyObject *list; |
| 4377 | |
| 4378 | if (maxcount < 0) |
| 4379 | maxcount = INT_MAX; |
| 4380 | |
| 4381 | list = PyList_New(0); |
| 4382 | if (!list) |
| 4383 | return NULL; |
| 4384 | |
| 4385 | if (substring == NULL) |
| 4386 | return rsplit_whitespace(self,list,maxcount); |
| 4387 | |
| 4388 | else if (substring->length == 1) |
| 4389 | return rsplit_char(self,list,substring->str[0],maxcount); |
| 4390 | |
| 4391 | else if (substring->length == 0) { |
| 4392 | Py_DECREF(list); |
| 4393 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 4394 | return NULL; |
| 4395 | } |
| 4396 | else |
| 4397 | return rsplit_substring(self,list,substring,maxcount); |
| 4398 | } |
| 4399 | |
| 4400 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4401 | PyObject *replace(PyUnicodeObject *self, |
| 4402 | PyUnicodeObject *str1, |
| 4403 | PyUnicodeObject *str2, |
| 4404 | int maxcount) |
| 4405 | { |
| 4406 | PyUnicodeObject *u; |
| 4407 | |
| 4408 | if (maxcount < 0) |
| 4409 | maxcount = INT_MAX; |
| 4410 | |
| 4411 | if (str1->length == 1 && str2->length == 1) { |
| 4412 | int i; |
| 4413 | |
| 4414 | /* replace characters */ |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4415 | if (!findchar(self->str, self->length, str1->str[0]) && |
| 4416 | PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4417 | /* nothing to replace, return original string */ |
| 4418 | Py_INCREF(self); |
| 4419 | u = self; |
| 4420 | } else { |
| 4421 | Py_UNICODE u1 = str1->str[0]; |
| 4422 | Py_UNICODE u2 = str2->str[0]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4423 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4424 | u = (PyUnicodeObject*) PyUnicode_FromUnicode( |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4425 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4426 | self->length |
| 4427 | ); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4428 | if (u != NULL) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4429 | Py_UNICODE_COPY(u->str, self->str, |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4430 | self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4431 | for (i = 0; i < u->length; i++) |
| 4432 | if (u->str[i] == u1) { |
| 4433 | if (--maxcount < 0) |
| 4434 | break; |
| 4435 | u->str[i] = u2; |
| 4436 | } |
| 4437 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4438 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4439 | |
| 4440 | } else { |
| 4441 | int n, i; |
| 4442 | Py_UNICODE *p; |
| 4443 | |
| 4444 | /* replace strings */ |
| 4445 | n = count(self, 0, self->length, str1); |
| 4446 | if (n > maxcount) |
| 4447 | n = maxcount; |
Guido van Rossum | 2023c9b | 2002-08-23 18:50:21 +0000 | [diff] [blame] | 4448 | if (n == 0) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4449 | /* nothing to replace, return original string */ |
Guido van Rossum | 2023c9b | 2002-08-23 18:50:21 +0000 | [diff] [blame] | 4450 | if (PyUnicode_CheckExact(self)) { |
| 4451 | Py_INCREF(self); |
| 4452 | u = self; |
| 4453 | } |
| 4454 | else { |
| 4455 | u = (PyUnicodeObject *) |
| 4456 | PyUnicode_FromUnicode(self->str, self->length); |
| 4457 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4458 | } else { |
| 4459 | u = _PyUnicode_New( |
| 4460 | self->length + n * (str2->length - str1->length)); |
| 4461 | if (u) { |
| 4462 | i = 0; |
| 4463 | p = u->str; |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 4464 | if (str1->length > 0) { |
| 4465 | while (i <= self->length - str1->length) |
| 4466 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 4467 | /* replace string segment */ |
| 4468 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 4469 | p += str2->length; |
| 4470 | i += str1->length; |
| 4471 | if (--n <= 0) { |
| 4472 | /* copy remaining part */ |
| 4473 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 4474 | break; |
| 4475 | } |
| 4476 | } else |
| 4477 | *p++ = self->str[i++]; |
| 4478 | } else { |
| 4479 | while (n > 0) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4480 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 4481 | p += str2->length; |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 4482 | if (--n <= 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4483 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4484 | *p++ = self->str[i++]; |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 4485 | } |
| 4486 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 4487 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4488 | } |
| 4489 | } |
| 4490 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4491 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4492 | return (PyObject *) u; |
| 4493 | } |
| 4494 | |
| 4495 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 4496 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4497 | PyDoc_STRVAR(title__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4498 | "S.title() -> unicode\n\ |
| 4499 | \n\ |
| 4500 | 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] | 4501 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4502 | |
| 4503 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 4504 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4505 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4506 | return fixup(self, fixtitle); |
| 4507 | } |
| 4508 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4509 | PyDoc_STRVAR(capitalize__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4510 | "S.capitalize() -> unicode\n\ |
| 4511 | \n\ |
| 4512 | 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] | 4513 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4514 | |
| 4515 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 4516 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4517 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4518 | return fixup(self, fixcapitalize); |
| 4519 | } |
| 4520 | |
| 4521 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4522 | PyDoc_STRVAR(capwords__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4523 | "S.capwords() -> unicode\n\ |
| 4524 | \n\ |
| 4525 | 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] | 4526 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4527 | |
| 4528 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 4529 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4530 | { |
| 4531 | PyObject *list; |
| 4532 | PyObject *item; |
| 4533 | int i; |
| 4534 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4535 | /* Split into words */ |
| 4536 | list = split(self, NULL, -1); |
| 4537 | if (!list) |
| 4538 | return NULL; |
| 4539 | |
| 4540 | /* Capitalize each word */ |
| 4541 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 4542 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 4543 | fixcapitalize); |
| 4544 | if (item == NULL) |
| 4545 | goto onError; |
| 4546 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 4547 | PyList_SET_ITEM(list, i, item); |
| 4548 | } |
| 4549 | |
| 4550 | /* Join the words to form a new string */ |
| 4551 | item = PyUnicode_Join(NULL, list); |
| 4552 | |
| 4553 | onError: |
| 4554 | Py_DECREF(list); |
| 4555 | return (PyObject *)item; |
| 4556 | } |
| 4557 | #endif |
| 4558 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4559 | /* Argument converter. Coerces to a single unicode character */ |
| 4560 | |
| 4561 | static int |
| 4562 | convert_uc(PyObject *obj, void *addr) |
| 4563 | { |
| 4564 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 4565 | PyObject *uniobj; |
| 4566 | Py_UNICODE *unistr; |
| 4567 | |
| 4568 | uniobj = PyUnicode_FromObject(obj); |
| 4569 | if (uniobj == NULL) { |
| 4570 | PyErr_SetString(PyExc_TypeError, |
| 4571 | "The fill character cannot be converted to Unicode"); |
| 4572 | return 0; |
| 4573 | } |
| 4574 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 4575 | PyErr_SetString(PyExc_TypeError, |
| 4576 | "The fill character must be exactly one character long"); |
| 4577 | Py_DECREF(uniobj); |
| 4578 | return 0; |
| 4579 | } |
| 4580 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 4581 | *fillcharloc = unistr[0]; |
| 4582 | Py_DECREF(uniobj); |
| 4583 | return 1; |
| 4584 | } |
| 4585 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4586 | PyDoc_STRVAR(center__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4587 | "S.center(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4588 | \n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4589 | Return S centered in a Unicode string of length width. Padding is\n\ |
| 4590 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4591 | |
| 4592 | static PyObject * |
| 4593 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 4594 | { |
| 4595 | int marg, left; |
| 4596 | int width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4597 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4598 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4599 | if (!PyArg_ParseTuple(args, "i|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4600 | return NULL; |
| 4601 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4602 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4603 | Py_INCREF(self); |
| 4604 | return (PyObject*) self; |
| 4605 | } |
| 4606 | |
| 4607 | marg = width - self->length; |
| 4608 | left = marg / 2 + (marg & width & 1); |
| 4609 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4610 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4611 | } |
| 4612 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4613 | #if 0 |
| 4614 | |
| 4615 | /* This code should go into some future Unicode collation support |
| 4616 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 4617 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4618 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4619 | /* speedy UTF-16 code point order comparison */ |
| 4620 | /* gleaned from: */ |
| 4621 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 4622 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4623 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4624 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4625 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4626 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 4627 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4628 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4629 | }; |
| 4630 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4631 | static int |
| 4632 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 4633 | { |
| 4634 | int len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4635 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4636 | Py_UNICODE *s1 = str1->str; |
| 4637 | Py_UNICODE *s2 = str2->str; |
| 4638 | |
| 4639 | len1 = str1->length; |
| 4640 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4641 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4642 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4643 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4644 | |
| 4645 | c1 = *s1++; |
| 4646 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 4647 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4648 | if (c1 > (1<<11) * 26) |
| 4649 | c1 += utf16Fixup[c1>>11]; |
| 4650 | if (c2 > (1<<11) * 26) |
| 4651 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4652 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 4653 | |
| 4654 | if (c1 != c2) |
| 4655 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4656 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4657 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4658 | } |
| 4659 | |
| 4660 | return (len1 < len2) ? -1 : (len1 != len2); |
| 4661 | } |
| 4662 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4663 | #else |
| 4664 | |
| 4665 | static int |
| 4666 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 4667 | { |
| 4668 | register int len1, len2; |
| 4669 | |
| 4670 | Py_UNICODE *s1 = str1->str; |
| 4671 | Py_UNICODE *s2 = str2->str; |
| 4672 | |
| 4673 | len1 = str1->length; |
| 4674 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4675 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4676 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4677 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4678 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 4679 | c1 = *s1++; |
| 4680 | c2 = *s2++; |
| 4681 | |
| 4682 | if (c1 != c2) |
| 4683 | return (c1 < c2) ? -1 : 1; |
| 4684 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4685 | len1--; len2--; |
| 4686 | } |
| 4687 | |
| 4688 | return (len1 < len2) ? -1 : (len1 != len2); |
| 4689 | } |
| 4690 | |
| 4691 | #endif |
| 4692 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4693 | int PyUnicode_Compare(PyObject *left, |
| 4694 | PyObject *right) |
| 4695 | { |
| 4696 | PyUnicodeObject *u = NULL, *v = NULL; |
| 4697 | int result; |
| 4698 | |
| 4699 | /* Coerce the two arguments */ |
| 4700 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 4701 | if (u == NULL) |
| 4702 | goto onError; |
| 4703 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 4704 | if (v == NULL) |
| 4705 | goto onError; |
| 4706 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 4707 | /* Shortcut for empty or interned objects */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4708 | if (v == u) { |
| 4709 | Py_DECREF(u); |
| 4710 | Py_DECREF(v); |
| 4711 | return 0; |
| 4712 | } |
| 4713 | |
| 4714 | result = unicode_compare(u, v); |
| 4715 | |
| 4716 | Py_DECREF(u); |
| 4717 | Py_DECREF(v); |
| 4718 | return result; |
| 4719 | |
| 4720 | onError: |
| 4721 | Py_XDECREF(u); |
| 4722 | Py_XDECREF(v); |
| 4723 | return -1; |
| 4724 | } |
| 4725 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4726 | int PyUnicode_Contains(PyObject *container, |
| 4727 | PyObject *element) |
| 4728 | { |
| 4729 | PyUnicodeObject *u = NULL, *v = NULL; |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 4730 | int result, size; |
| 4731 | register const Py_UNICODE *lhs, *end, *rhs; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4732 | |
| 4733 | /* Coerce the two arguments */ |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4734 | v = (PyUnicodeObject *)PyUnicode_FromObject(element); |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 4735 | if (v == NULL) { |
| 4736 | PyErr_SetString(PyExc_TypeError, |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 4737 | "'in <string>' requires string as left operand"); |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4738 | goto onError; |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 4739 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4740 | u = (PyUnicodeObject *)PyUnicode_FromObject(container); |
Marc-André Lemburg | 9cd87aa | 2002-10-23 09:02:46 +0000 | [diff] [blame] | 4741 | if (u == NULL) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4742 | goto onError; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4743 | |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 4744 | size = PyUnicode_GET_SIZE(v); |
| 4745 | rhs = PyUnicode_AS_UNICODE(v); |
| 4746 | lhs = PyUnicode_AS_UNICODE(u); |
| 4747 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4748 | result = 0; |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 4749 | if (size == 1) { |
| 4750 | end = lhs + PyUnicode_GET_SIZE(u); |
| 4751 | while (lhs < end) { |
| 4752 | if (*lhs++ == *rhs) { |
| 4753 | result = 1; |
| 4754 | break; |
| 4755 | } |
| 4756 | } |
| 4757 | } |
| 4758 | else { |
| 4759 | end = lhs + (PyUnicode_GET_SIZE(u) - size); |
| 4760 | while (lhs <= end) { |
Barry Warsaw | 6a043f3 | 2002-08-06 19:03:17 +0000 | [diff] [blame] | 4761 | if (memcmp(lhs++, rhs, size * sizeof(Py_UNICODE)) == 0) { |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 4762 | result = 1; |
| 4763 | break; |
| 4764 | } |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4765 | } |
| 4766 | } |
| 4767 | |
| 4768 | Py_DECREF(u); |
| 4769 | Py_DECREF(v); |
| 4770 | return result; |
| 4771 | |
| 4772 | onError: |
| 4773 | Py_XDECREF(u); |
| 4774 | Py_XDECREF(v); |
| 4775 | return -1; |
| 4776 | } |
| 4777 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4778 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 4779 | |
| 4780 | PyObject *PyUnicode_Concat(PyObject *left, |
| 4781 | PyObject *right) |
| 4782 | { |
| 4783 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 4784 | |
| 4785 | /* Coerce the two arguments */ |
| 4786 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 4787 | if (u == NULL) |
| 4788 | goto onError; |
| 4789 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 4790 | if (v == NULL) |
| 4791 | goto onError; |
| 4792 | |
| 4793 | /* Shortcuts */ |
| 4794 | if (v == unicode_empty) { |
| 4795 | Py_DECREF(v); |
| 4796 | return (PyObject *)u; |
| 4797 | } |
| 4798 | if (u == unicode_empty) { |
| 4799 | Py_DECREF(u); |
| 4800 | return (PyObject *)v; |
| 4801 | } |
| 4802 | |
| 4803 | /* Concat the two Unicode strings */ |
| 4804 | w = _PyUnicode_New(u->length + v->length); |
| 4805 | if (w == NULL) |
| 4806 | goto onError; |
| 4807 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 4808 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 4809 | |
| 4810 | Py_DECREF(u); |
| 4811 | Py_DECREF(v); |
| 4812 | return (PyObject *)w; |
| 4813 | |
| 4814 | onError: |
| 4815 | Py_XDECREF(u); |
| 4816 | Py_XDECREF(v); |
| 4817 | return NULL; |
| 4818 | } |
| 4819 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4820 | PyDoc_STRVAR(count__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4821 | "S.count(sub[, start[, end]]) -> int\n\ |
| 4822 | \n\ |
| 4823 | Return the number of occurrences of substring sub in Unicode string\n\ |
| 4824 | S[start:end]. Optional arguments start and end are\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4825 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4826 | |
| 4827 | static PyObject * |
| 4828 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 4829 | { |
| 4830 | PyUnicodeObject *substring; |
| 4831 | int start = 0; |
| 4832 | int end = INT_MAX; |
| 4833 | PyObject *result; |
| 4834 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 4835 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 4836 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4837 | return NULL; |
| 4838 | |
| 4839 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 4840 | (PyObject *)substring); |
| 4841 | if (substring == NULL) |
| 4842 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4843 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4844 | if (start < 0) |
| 4845 | start += self->length; |
| 4846 | if (start < 0) |
| 4847 | start = 0; |
| 4848 | if (end > self->length) |
| 4849 | end = self->length; |
| 4850 | if (end < 0) |
| 4851 | end += self->length; |
| 4852 | if (end < 0) |
| 4853 | end = 0; |
| 4854 | |
| 4855 | result = PyInt_FromLong((long) count(self, start, end, substring)); |
| 4856 | |
| 4857 | Py_DECREF(substring); |
| 4858 | return result; |
| 4859 | } |
| 4860 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4861 | PyDoc_STRVAR(encode__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4862 | "S.encode([encoding[,errors]]) -> string\n\ |
| 4863 | \n\ |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 4864 | Return an encoded string version of S. Default encoding is the current\n\ |
| 4865 | default string encoding. errors may be given to set a different error\n\ |
| 4866 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4867 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 4868 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 4869 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4870 | |
| 4871 | static PyObject * |
| 4872 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 4873 | { |
| 4874 | char *encoding = NULL; |
| 4875 | char *errors = NULL; |
| 4876 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 4877 | return NULL; |
| 4878 | return PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
| 4879 | } |
| 4880 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4881 | PyDoc_STRVAR(expandtabs__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4882 | "S.expandtabs([tabsize]) -> unicode\n\ |
| 4883 | \n\ |
| 4884 | 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] | 4885 | 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] | 4886 | |
| 4887 | static PyObject* |
| 4888 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 4889 | { |
| 4890 | Py_UNICODE *e; |
| 4891 | Py_UNICODE *p; |
| 4892 | Py_UNICODE *q; |
| 4893 | int i, j; |
| 4894 | PyUnicodeObject *u; |
| 4895 | int tabsize = 8; |
| 4896 | |
| 4897 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 4898 | return NULL; |
| 4899 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 4900 | /* First pass: determine size of output string */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4901 | i = j = 0; |
| 4902 | e = self->str + self->length; |
| 4903 | for (p = self->str; p < e; p++) |
| 4904 | if (*p == '\t') { |
| 4905 | if (tabsize > 0) |
| 4906 | j += tabsize - (j % tabsize); |
| 4907 | } |
| 4908 | else { |
| 4909 | j++; |
| 4910 | if (*p == '\n' || *p == '\r') { |
| 4911 | i += j; |
| 4912 | j = 0; |
| 4913 | } |
| 4914 | } |
| 4915 | |
| 4916 | /* Second pass: create output string and fill it */ |
| 4917 | u = _PyUnicode_New(i + j); |
| 4918 | if (!u) |
| 4919 | return NULL; |
| 4920 | |
| 4921 | j = 0; |
| 4922 | q = u->str; |
| 4923 | |
| 4924 | for (p = self->str; p < e; p++) |
| 4925 | if (*p == '\t') { |
| 4926 | if (tabsize > 0) { |
| 4927 | i = tabsize - (j % tabsize); |
| 4928 | j += i; |
| 4929 | while (i--) |
| 4930 | *q++ = ' '; |
| 4931 | } |
| 4932 | } |
| 4933 | else { |
| 4934 | j++; |
| 4935 | *q++ = *p; |
| 4936 | if (*p == '\n' || *p == '\r') |
| 4937 | j = 0; |
| 4938 | } |
| 4939 | |
| 4940 | return (PyObject*) u; |
| 4941 | } |
| 4942 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4943 | PyDoc_STRVAR(find__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4944 | "S.find(sub [,start [,end]]) -> int\n\ |
| 4945 | \n\ |
| 4946 | Return the lowest index in S where substring sub is found,\n\ |
| 4947 | such that sub is contained within s[start,end]. Optional\n\ |
| 4948 | arguments start and end are interpreted as in slice notation.\n\ |
| 4949 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4950 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4951 | |
| 4952 | static PyObject * |
| 4953 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 4954 | { |
| 4955 | PyUnicodeObject *substring; |
| 4956 | int start = 0; |
| 4957 | int end = INT_MAX; |
| 4958 | PyObject *result; |
| 4959 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 4960 | if (!PyArg_ParseTuple(args, "O|O&O&:find", &substring, |
| 4961 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4962 | return NULL; |
| 4963 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 4964 | (PyObject *)substring); |
| 4965 | if (substring == NULL) |
| 4966 | return NULL; |
| 4967 | |
| 4968 | result = PyInt_FromLong(findstring(self, substring, start, end, 1)); |
| 4969 | |
| 4970 | Py_DECREF(substring); |
| 4971 | return result; |
| 4972 | } |
| 4973 | |
| 4974 | static PyObject * |
| 4975 | unicode_getitem(PyUnicodeObject *self, int index) |
| 4976 | { |
| 4977 | if (index < 0 || index >= self->length) { |
| 4978 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 4979 | return NULL; |
| 4980 | } |
| 4981 | |
| 4982 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 4983 | } |
| 4984 | |
| 4985 | static long |
| 4986 | unicode_hash(PyUnicodeObject *self) |
| 4987 | { |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 4988 | /* Since Unicode objects compare equal to their ASCII string |
| 4989 | counterparts, they should use the individual character values |
| 4990 | as basis for their hash value. This is needed to assure that |
| 4991 | strings and Unicode objects behave in the same way as |
| 4992 | dictionary keys. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4993 | |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 4994 | register int len; |
| 4995 | register Py_UNICODE *p; |
| 4996 | register long x; |
| 4997 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4998 | if (self->hash != -1) |
| 4999 | return self->hash; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5000 | len = PyUnicode_GET_SIZE(self); |
| 5001 | p = PyUnicode_AS_UNICODE(self); |
| 5002 | x = *p << 7; |
| 5003 | while (--len >= 0) |
| 5004 | x = (1000003*x) ^ *p++; |
| 5005 | x ^= PyUnicode_GET_SIZE(self); |
| 5006 | if (x == -1) |
| 5007 | x = -2; |
| 5008 | self->hash = x; |
| 5009 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5010 | } |
| 5011 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5012 | PyDoc_STRVAR(index__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5013 | "S.index(sub [,start [,end]]) -> int\n\ |
| 5014 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5015 | 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] | 5016 | |
| 5017 | static PyObject * |
| 5018 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 5019 | { |
| 5020 | int result; |
| 5021 | PyUnicodeObject *substring; |
| 5022 | int start = 0; |
| 5023 | int end = INT_MAX; |
| 5024 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5025 | if (!PyArg_ParseTuple(args, "O|O&O&:index", &substring, |
| 5026 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5027 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5028 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5029 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 5030 | (PyObject *)substring); |
| 5031 | if (substring == NULL) |
| 5032 | return NULL; |
| 5033 | |
| 5034 | result = findstring(self, substring, start, end, 1); |
| 5035 | |
| 5036 | Py_DECREF(substring); |
| 5037 | if (result < 0) { |
| 5038 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 5039 | return NULL; |
| 5040 | } |
| 5041 | return PyInt_FromLong(result); |
| 5042 | } |
| 5043 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5044 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5045 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5046 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5047 | 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] | 5048 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5049 | |
| 5050 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5051 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5052 | { |
| 5053 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5054 | register const Py_UNICODE *e; |
| 5055 | int cased; |
| 5056 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5057 | /* Shortcut for single character strings */ |
| 5058 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5059 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5060 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5061 | /* Special case for empty strings */ |
| 5062 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5063 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5064 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5065 | e = p + PyUnicode_GET_SIZE(self); |
| 5066 | cased = 0; |
| 5067 | for (; p < e; p++) { |
| 5068 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5069 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5070 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5071 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5072 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 5073 | cased = 1; |
| 5074 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5075 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5076 | } |
| 5077 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5078 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5079 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5080 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5081 | 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] | 5082 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5083 | |
| 5084 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5085 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5086 | { |
| 5087 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5088 | register const Py_UNICODE *e; |
| 5089 | int cased; |
| 5090 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5091 | /* Shortcut for single character strings */ |
| 5092 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5093 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5094 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5095 | /* Special case for empty strings */ |
| 5096 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5097 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5098 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5099 | e = p + PyUnicode_GET_SIZE(self); |
| 5100 | cased = 0; |
| 5101 | for (; p < e; p++) { |
| 5102 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5103 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5104 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5105 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5106 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 5107 | cased = 1; |
| 5108 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5109 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5110 | } |
| 5111 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5112 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5113 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5114 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5115 | Return True if S is a titlecased string and there is at least one\n\ |
| 5116 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 5117 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 5118 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5119 | |
| 5120 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5121 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5122 | { |
| 5123 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5124 | register const Py_UNICODE *e; |
| 5125 | int cased, previous_is_cased; |
| 5126 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5127 | /* Shortcut for single character strings */ |
| 5128 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5129 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 5130 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5131 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5132 | /* Special case for empty strings */ |
| 5133 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5134 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5135 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5136 | e = p + PyUnicode_GET_SIZE(self); |
| 5137 | cased = 0; |
| 5138 | previous_is_cased = 0; |
| 5139 | for (; p < e; p++) { |
| 5140 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5141 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5142 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 5143 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5144 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5145 | previous_is_cased = 1; |
| 5146 | cased = 1; |
| 5147 | } |
| 5148 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 5149 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5150 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5151 | previous_is_cased = 1; |
| 5152 | cased = 1; |
| 5153 | } |
| 5154 | else |
| 5155 | previous_is_cased = 0; |
| 5156 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5157 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5158 | } |
| 5159 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5160 | PyDoc_STRVAR(isspace__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5161 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5162 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5163 | Return True if all characters in S are whitespace\n\ |
| 5164 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5165 | |
| 5166 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5167 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5168 | { |
| 5169 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5170 | register const Py_UNICODE *e; |
| 5171 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5172 | /* Shortcut for single character strings */ |
| 5173 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5174 | Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5175 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5176 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5177 | /* Special case for empty strings */ |
| 5178 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5179 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5180 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5181 | e = p + PyUnicode_GET_SIZE(self); |
| 5182 | for (; p < e; p++) { |
| 5183 | if (!Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5184 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5185 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5186 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5187 | } |
| 5188 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5189 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5190 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5191 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5192 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5193 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5194 | |
| 5195 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5196 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5197 | { |
| 5198 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5199 | register const Py_UNICODE *e; |
| 5200 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5201 | /* Shortcut for single character strings */ |
| 5202 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5203 | Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5204 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5205 | |
| 5206 | /* Special case for empty strings */ |
| 5207 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5208 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5209 | |
| 5210 | e = p + PyUnicode_GET_SIZE(self); |
| 5211 | for (; p < e; p++) { |
| 5212 | if (!Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5213 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5214 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5215 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5216 | } |
| 5217 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5218 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5219 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5220 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5221 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5222 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5223 | |
| 5224 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5225 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5226 | { |
| 5227 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5228 | register const Py_UNICODE *e; |
| 5229 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5230 | /* Shortcut for single character strings */ |
| 5231 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5232 | Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5233 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5234 | |
| 5235 | /* Special case for empty strings */ |
| 5236 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5237 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5238 | |
| 5239 | e = p + PyUnicode_GET_SIZE(self); |
| 5240 | for (; p < e; p++) { |
| 5241 | if (!Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5242 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5243 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5244 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5245 | } |
| 5246 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5247 | PyDoc_STRVAR(isdecimal__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5248 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5249 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5250 | 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] | 5251 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5252 | |
| 5253 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5254 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5255 | { |
| 5256 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5257 | register const Py_UNICODE *e; |
| 5258 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5259 | /* Shortcut for single character strings */ |
| 5260 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5261 | Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5262 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5263 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5264 | /* Special case for empty strings */ |
| 5265 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5266 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5267 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5268 | e = p + PyUnicode_GET_SIZE(self); |
| 5269 | for (; p < e; p++) { |
| 5270 | if (!Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5271 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5272 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5273 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5274 | } |
| 5275 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5276 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5277 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5278 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5279 | Return True if all characters in S are digits\n\ |
| 5280 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5281 | |
| 5282 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5283 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5284 | { |
| 5285 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5286 | register const Py_UNICODE *e; |
| 5287 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5288 | /* Shortcut for single character strings */ |
| 5289 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5290 | Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5291 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5292 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5293 | /* Special case for empty strings */ |
| 5294 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5295 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5296 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5297 | e = p + PyUnicode_GET_SIZE(self); |
| 5298 | for (; p < e; p++) { |
| 5299 | if (!Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5300 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5301 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5302 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5303 | } |
| 5304 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5305 | PyDoc_STRVAR(isnumeric__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5306 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5307 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5308 | 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] | 5309 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5310 | |
| 5311 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5312 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5313 | { |
| 5314 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5315 | register const Py_UNICODE *e; |
| 5316 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5317 | /* Shortcut for single character strings */ |
| 5318 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5319 | Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5320 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5321 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5322 | /* Special case for empty strings */ |
| 5323 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5324 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5325 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5326 | e = p + PyUnicode_GET_SIZE(self); |
| 5327 | for (; p < e; p++) { |
| 5328 | if (!Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5329 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5330 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5331 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5332 | } |
| 5333 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5334 | PyDoc_STRVAR(join__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5335 | "S.join(sequence) -> unicode\n\ |
| 5336 | \n\ |
| 5337 | 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] | 5338 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5339 | |
| 5340 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5341 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5342 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5343 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5344 | } |
| 5345 | |
| 5346 | static int |
| 5347 | unicode_length(PyUnicodeObject *self) |
| 5348 | { |
| 5349 | return self->length; |
| 5350 | } |
| 5351 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5352 | PyDoc_STRVAR(ljust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5353 | "S.ljust(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5354 | \n\ |
| 5355 | 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] | 5356 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5357 | |
| 5358 | static PyObject * |
| 5359 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 5360 | { |
| 5361 | int width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5362 | Py_UNICODE fillchar = ' '; |
| 5363 | |
| 5364 | if (!PyArg_ParseTuple(args, "i|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5365 | return NULL; |
| 5366 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5367 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5368 | Py_INCREF(self); |
| 5369 | return (PyObject*) self; |
| 5370 | } |
| 5371 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5372 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5373 | } |
| 5374 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5375 | PyDoc_STRVAR(lower__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5376 | "S.lower() -> unicode\n\ |
| 5377 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5378 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5379 | |
| 5380 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5381 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5382 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5383 | return fixup(self, fixlower); |
| 5384 | } |
| 5385 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5386 | #define LEFTSTRIP 0 |
| 5387 | #define RIGHTSTRIP 1 |
| 5388 | #define BOTHSTRIP 2 |
| 5389 | |
| 5390 | /* Arrays indexed by above */ |
| 5391 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 5392 | |
| 5393 | #define STRIPNAME(i) (stripformat[i]+3) |
| 5394 | |
| 5395 | static const Py_UNICODE * |
| 5396 | unicode_memchr(const Py_UNICODE *s, Py_UNICODE c, size_t n) |
| 5397 | { |
Tim Peters | 030a5ce | 2002-04-22 19:00:10 +0000 | [diff] [blame] | 5398 | size_t i; |
| 5399 | for (i = 0; i < n; ++i) |
| 5400 | if (s[i] == c) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5401 | return s+i; |
| 5402 | return NULL; |
| 5403 | } |
| 5404 | |
| 5405 | /* externally visible for str.strip(unicode) */ |
| 5406 | PyObject * |
| 5407 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 5408 | { |
| 5409 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 5410 | int len = PyUnicode_GET_SIZE(self); |
| 5411 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
| 5412 | int seplen = PyUnicode_GET_SIZE(sepobj); |
| 5413 | int i, j; |
| 5414 | |
| 5415 | i = 0; |
| 5416 | if (striptype != RIGHTSTRIP) { |
| 5417 | while (i < len && unicode_memchr(sep, s[i], seplen)) { |
| 5418 | i++; |
| 5419 | } |
| 5420 | } |
| 5421 | |
| 5422 | j = len; |
| 5423 | if (striptype != LEFTSTRIP) { |
| 5424 | do { |
| 5425 | j--; |
| 5426 | } while (j >= i && unicode_memchr(sep, s[j], seplen)); |
| 5427 | j++; |
| 5428 | } |
| 5429 | |
| 5430 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 5431 | Py_INCREF(self); |
| 5432 | return (PyObject*)self; |
| 5433 | } |
| 5434 | else |
| 5435 | return PyUnicode_FromUnicode(s+i, j-i); |
| 5436 | } |
| 5437 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5438 | |
| 5439 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5440 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5441 | { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5442 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 5443 | int len = PyUnicode_GET_SIZE(self), i, j; |
| 5444 | |
| 5445 | i = 0; |
| 5446 | if (striptype != RIGHTSTRIP) { |
| 5447 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 5448 | i++; |
| 5449 | } |
| 5450 | } |
| 5451 | |
| 5452 | j = len; |
| 5453 | if (striptype != LEFTSTRIP) { |
| 5454 | do { |
| 5455 | j--; |
| 5456 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 5457 | j++; |
| 5458 | } |
| 5459 | |
| 5460 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 5461 | Py_INCREF(self); |
| 5462 | return (PyObject*)self; |
| 5463 | } |
| 5464 | else |
| 5465 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5466 | } |
| 5467 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5468 | |
| 5469 | static PyObject * |
| 5470 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 5471 | { |
| 5472 | PyObject *sep = NULL; |
| 5473 | |
| 5474 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 5475 | return NULL; |
| 5476 | |
| 5477 | if (sep != NULL && sep != Py_None) { |
| 5478 | if (PyUnicode_Check(sep)) |
| 5479 | return _PyUnicode_XStrip(self, striptype, sep); |
| 5480 | else if (PyString_Check(sep)) { |
| 5481 | PyObject *res; |
| 5482 | sep = PyUnicode_FromObject(sep); |
| 5483 | if (sep==NULL) |
| 5484 | return NULL; |
| 5485 | res = _PyUnicode_XStrip(self, striptype, sep); |
| 5486 | Py_DECREF(sep); |
| 5487 | return res; |
| 5488 | } |
| 5489 | else { |
| 5490 | PyErr_Format(PyExc_TypeError, |
| 5491 | "%s arg must be None, unicode or str", |
| 5492 | STRIPNAME(striptype)); |
| 5493 | return NULL; |
| 5494 | } |
| 5495 | } |
| 5496 | |
| 5497 | return do_strip(self, striptype); |
| 5498 | } |
| 5499 | |
| 5500 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5501 | PyDoc_STRVAR(strip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5502 | "S.strip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5503 | \n\ |
| 5504 | Return a copy of the string S with leading and trailing\n\ |
| 5505 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5506 | If chars is given and not None, remove characters in chars instead.\n\ |
| 5507 | 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] | 5508 | |
| 5509 | static PyObject * |
| 5510 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 5511 | { |
| 5512 | if (PyTuple_GET_SIZE(args) == 0) |
| 5513 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 5514 | else |
| 5515 | return do_argstrip(self, BOTHSTRIP, args); |
| 5516 | } |
| 5517 | |
| 5518 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5519 | PyDoc_STRVAR(lstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5520 | "S.lstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5521 | \n\ |
| 5522 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5523 | If chars is given and not None, remove characters in chars instead.\n\ |
| 5524 | 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] | 5525 | |
| 5526 | static PyObject * |
| 5527 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 5528 | { |
| 5529 | if (PyTuple_GET_SIZE(args) == 0) |
| 5530 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 5531 | else |
| 5532 | return do_argstrip(self, LEFTSTRIP, args); |
| 5533 | } |
| 5534 | |
| 5535 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5536 | PyDoc_STRVAR(rstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5537 | "S.rstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5538 | \n\ |
| 5539 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5540 | If chars is given and not None, remove characters in chars instead.\n\ |
| 5541 | 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] | 5542 | |
| 5543 | static PyObject * |
| 5544 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 5545 | { |
| 5546 | if (PyTuple_GET_SIZE(args) == 0) |
| 5547 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 5548 | else |
| 5549 | return do_argstrip(self, RIGHTSTRIP, args); |
| 5550 | } |
| 5551 | |
| 5552 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5553 | static PyObject* |
| 5554 | unicode_repeat(PyUnicodeObject *str, int len) |
| 5555 | { |
| 5556 | PyUnicodeObject *u; |
| 5557 | Py_UNICODE *p; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 5558 | int nchars; |
| 5559 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5560 | |
| 5561 | if (len < 0) |
| 5562 | len = 0; |
| 5563 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5564 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5565 | /* no repeat, return original string */ |
| 5566 | Py_INCREF(str); |
| 5567 | return (PyObject*) str; |
| 5568 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 5569 | |
| 5570 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 5571 | * needed doesn't overflow size_t |
| 5572 | */ |
| 5573 | nchars = len * str->length; |
| 5574 | if (len && nchars / len != str->length) { |
| 5575 | PyErr_SetString(PyExc_OverflowError, |
| 5576 | "repeated string is too long"); |
| 5577 | return NULL; |
| 5578 | } |
| 5579 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 5580 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 5581 | PyErr_SetString(PyExc_OverflowError, |
| 5582 | "repeated string is too long"); |
| 5583 | return NULL; |
| 5584 | } |
| 5585 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5586 | if (!u) |
| 5587 | return NULL; |
| 5588 | |
| 5589 | p = u->str; |
| 5590 | |
| 5591 | while (len-- > 0) { |
| 5592 | Py_UNICODE_COPY(p, str->str, str->length); |
| 5593 | p += str->length; |
| 5594 | } |
| 5595 | |
| 5596 | return (PyObject*) u; |
| 5597 | } |
| 5598 | |
| 5599 | PyObject *PyUnicode_Replace(PyObject *obj, |
| 5600 | PyObject *subobj, |
| 5601 | PyObject *replobj, |
| 5602 | int maxcount) |
| 5603 | { |
| 5604 | PyObject *self; |
| 5605 | PyObject *str1; |
| 5606 | PyObject *str2; |
| 5607 | PyObject *result; |
| 5608 | |
| 5609 | self = PyUnicode_FromObject(obj); |
| 5610 | if (self == NULL) |
| 5611 | return NULL; |
| 5612 | str1 = PyUnicode_FromObject(subobj); |
| 5613 | if (str1 == NULL) { |
| 5614 | Py_DECREF(self); |
| 5615 | return NULL; |
| 5616 | } |
| 5617 | str2 = PyUnicode_FromObject(replobj); |
| 5618 | if (str2 == NULL) { |
| 5619 | Py_DECREF(self); |
| 5620 | Py_DECREF(str1); |
| 5621 | return NULL; |
| 5622 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5623 | result = replace((PyUnicodeObject *)self, |
| 5624 | (PyUnicodeObject *)str1, |
| 5625 | (PyUnicodeObject *)str2, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5626 | maxcount); |
| 5627 | Py_DECREF(self); |
| 5628 | Py_DECREF(str1); |
| 5629 | Py_DECREF(str2); |
| 5630 | return result; |
| 5631 | } |
| 5632 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5633 | PyDoc_STRVAR(replace__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5634 | "S.replace (old, new[, maxsplit]) -> unicode\n\ |
| 5635 | \n\ |
| 5636 | Return a copy of S with all occurrences of substring\n\ |
| 5637 | 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] | 5638 | given, only the first maxsplit occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5639 | |
| 5640 | static PyObject* |
| 5641 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 5642 | { |
| 5643 | PyUnicodeObject *str1; |
| 5644 | PyUnicodeObject *str2; |
| 5645 | int maxcount = -1; |
| 5646 | PyObject *result; |
| 5647 | |
| 5648 | if (!PyArg_ParseTuple(args, "OO|i:replace", &str1, &str2, &maxcount)) |
| 5649 | return NULL; |
| 5650 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 5651 | if (str1 == NULL) |
| 5652 | return NULL; |
| 5653 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 5654 | if (str2 == NULL) { |
| 5655 | Py_DECREF(str1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5656 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 5657 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5658 | |
| 5659 | result = replace(self, str1, str2, maxcount); |
| 5660 | |
| 5661 | Py_DECREF(str1); |
| 5662 | Py_DECREF(str2); |
| 5663 | return result; |
| 5664 | } |
| 5665 | |
| 5666 | static |
| 5667 | PyObject *unicode_repr(PyObject *unicode) |
| 5668 | { |
| 5669 | return unicodeescape_string(PyUnicode_AS_UNICODE(unicode), |
| 5670 | PyUnicode_GET_SIZE(unicode), |
| 5671 | 1); |
| 5672 | } |
| 5673 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5674 | PyDoc_STRVAR(rfind__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5675 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 5676 | \n\ |
| 5677 | Return the highest index in S where substring sub is found,\n\ |
| 5678 | such that sub is contained within s[start,end]. Optional\n\ |
| 5679 | arguments start and end are interpreted as in slice notation.\n\ |
| 5680 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5681 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5682 | |
| 5683 | static PyObject * |
| 5684 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 5685 | { |
| 5686 | PyUnicodeObject *substring; |
| 5687 | int start = 0; |
| 5688 | int end = INT_MAX; |
| 5689 | PyObject *result; |
| 5690 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5691 | if (!PyArg_ParseTuple(args, "O|O&O&:rfind", &substring, |
| 5692 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5693 | return NULL; |
| 5694 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 5695 | (PyObject *)substring); |
| 5696 | if (substring == NULL) |
| 5697 | return NULL; |
| 5698 | |
| 5699 | result = PyInt_FromLong(findstring(self, substring, start, end, -1)); |
| 5700 | |
| 5701 | Py_DECREF(substring); |
| 5702 | return result; |
| 5703 | } |
| 5704 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5705 | PyDoc_STRVAR(rindex__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5706 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 5707 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5708 | 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] | 5709 | |
| 5710 | static PyObject * |
| 5711 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 5712 | { |
| 5713 | int result; |
| 5714 | PyUnicodeObject *substring; |
| 5715 | int start = 0; |
| 5716 | int end = INT_MAX; |
| 5717 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5718 | if (!PyArg_ParseTuple(args, "O|O&O&:rindex", &substring, |
| 5719 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5720 | return NULL; |
| 5721 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 5722 | (PyObject *)substring); |
| 5723 | if (substring == NULL) |
| 5724 | return NULL; |
| 5725 | |
| 5726 | result = findstring(self, substring, start, end, -1); |
| 5727 | |
| 5728 | Py_DECREF(substring); |
| 5729 | if (result < 0) { |
| 5730 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 5731 | return NULL; |
| 5732 | } |
| 5733 | return PyInt_FromLong(result); |
| 5734 | } |
| 5735 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5736 | PyDoc_STRVAR(rjust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5737 | "S.rjust(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5738 | \n\ |
| 5739 | 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] | 5740 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5741 | |
| 5742 | static PyObject * |
| 5743 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 5744 | { |
| 5745 | int width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5746 | Py_UNICODE fillchar = ' '; |
| 5747 | |
| 5748 | if (!PyArg_ParseTuple(args, "i|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5749 | return NULL; |
| 5750 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5751 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5752 | Py_INCREF(self); |
| 5753 | return (PyObject*) self; |
| 5754 | } |
| 5755 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5756 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5757 | } |
| 5758 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5759 | static PyObject* |
| 5760 | unicode_slice(PyUnicodeObject *self, int start, int end) |
| 5761 | { |
| 5762 | /* standard clamping */ |
| 5763 | if (start < 0) |
| 5764 | start = 0; |
| 5765 | if (end < 0) |
| 5766 | end = 0; |
| 5767 | if (end > self->length) |
| 5768 | end = self->length; |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5769 | if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5770 | /* full slice, return original string */ |
| 5771 | Py_INCREF(self); |
| 5772 | return (PyObject*) self; |
| 5773 | } |
| 5774 | if (start > end) |
| 5775 | start = end; |
| 5776 | /* copy slice */ |
| 5777 | return (PyObject*) PyUnicode_FromUnicode(self->str + start, |
| 5778 | end - start); |
| 5779 | } |
| 5780 | |
| 5781 | PyObject *PyUnicode_Split(PyObject *s, |
| 5782 | PyObject *sep, |
| 5783 | int maxsplit) |
| 5784 | { |
| 5785 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5786 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5787 | s = PyUnicode_FromObject(s); |
| 5788 | if (s == NULL) |
| 5789 | return NULL; |
| 5790 | if (sep != NULL) { |
| 5791 | sep = PyUnicode_FromObject(sep); |
| 5792 | if (sep == NULL) { |
| 5793 | Py_DECREF(s); |
| 5794 | return NULL; |
| 5795 | } |
| 5796 | } |
| 5797 | |
| 5798 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 5799 | |
| 5800 | Py_DECREF(s); |
| 5801 | Py_XDECREF(sep); |
| 5802 | return result; |
| 5803 | } |
| 5804 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5805 | PyDoc_STRVAR(split__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5806 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 5807 | \n\ |
| 5808 | Return a list of the words in S, using sep as the\n\ |
| 5809 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
| 5810 | 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] | 5811 | is a separator."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5812 | |
| 5813 | static PyObject* |
| 5814 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 5815 | { |
| 5816 | PyObject *substring = Py_None; |
| 5817 | int maxcount = -1; |
| 5818 | |
| 5819 | if (!PyArg_ParseTuple(args, "|Oi:split", &substring, &maxcount)) |
| 5820 | return NULL; |
| 5821 | |
| 5822 | if (substring == Py_None) |
| 5823 | return split(self, NULL, maxcount); |
| 5824 | else if (PyUnicode_Check(substring)) |
| 5825 | return split(self, (PyUnicodeObject *)substring, maxcount); |
| 5826 | else |
| 5827 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
| 5828 | } |
| 5829 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5830 | PyObject *PyUnicode_RSplit(PyObject *s, |
| 5831 | PyObject *sep, |
| 5832 | int maxsplit) |
| 5833 | { |
| 5834 | PyObject *result; |
| 5835 | |
| 5836 | s = PyUnicode_FromObject(s); |
| 5837 | if (s == NULL) |
| 5838 | return NULL; |
| 5839 | if (sep != NULL) { |
| 5840 | sep = PyUnicode_FromObject(sep); |
| 5841 | if (sep == NULL) { |
| 5842 | Py_DECREF(s); |
| 5843 | return NULL; |
| 5844 | } |
| 5845 | } |
| 5846 | |
| 5847 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 5848 | |
| 5849 | Py_DECREF(s); |
| 5850 | Py_XDECREF(sep); |
| 5851 | return result; |
| 5852 | } |
| 5853 | |
| 5854 | PyDoc_STRVAR(rsplit__doc__, |
| 5855 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
| 5856 | \n\ |
| 5857 | Return a list of the words in S, using sep as the\n\ |
| 5858 | delimiter string, starting at the end of the string and\n\ |
| 5859 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 5860 | splits are done. If sep is not specified, any whitespace string\n\ |
| 5861 | is a separator."); |
| 5862 | |
| 5863 | static PyObject* |
| 5864 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 5865 | { |
| 5866 | PyObject *substring = Py_None; |
| 5867 | int maxcount = -1; |
| 5868 | |
| 5869 | if (!PyArg_ParseTuple(args, "|Oi:rsplit", &substring, &maxcount)) |
| 5870 | return NULL; |
| 5871 | |
| 5872 | if (substring == Py_None) |
| 5873 | return rsplit(self, NULL, maxcount); |
| 5874 | else if (PyUnicode_Check(substring)) |
| 5875 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
| 5876 | else |
| 5877 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
| 5878 | } |
| 5879 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5880 | PyDoc_STRVAR(splitlines__doc__, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5881 | "S.splitlines([keepends]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5882 | \n\ |
| 5883 | 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] | 5884 | 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] | 5885 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5886 | |
| 5887 | static PyObject* |
| 5888 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 5889 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5890 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5891 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5892 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5893 | return NULL; |
| 5894 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5895 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5896 | } |
| 5897 | |
| 5898 | static |
| 5899 | PyObject *unicode_str(PyUnicodeObject *self) |
| 5900 | { |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 5901 | return PyUnicode_AsEncodedString((PyObject *)self, NULL, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5902 | } |
| 5903 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5904 | PyDoc_STRVAR(swapcase__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5905 | "S.swapcase() -> unicode\n\ |
| 5906 | \n\ |
| 5907 | 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] | 5908 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5909 | |
| 5910 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5911 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5912 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5913 | return fixup(self, fixswapcase); |
| 5914 | } |
| 5915 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5916 | PyDoc_STRVAR(translate__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5917 | "S.translate(table) -> unicode\n\ |
| 5918 | \n\ |
| 5919 | Return a copy of the string S, where all characters have been mapped\n\ |
| 5920 | 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] | 5921 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ |
| 5922 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 5923 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5924 | |
| 5925 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5926 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5927 | { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5928 | return PyUnicode_TranslateCharmap(self->str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5929 | self->length, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5930 | table, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5931 | "ignore"); |
| 5932 | } |
| 5933 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5934 | PyDoc_STRVAR(upper__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5935 | "S.upper() -> unicode\n\ |
| 5936 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5937 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5938 | |
| 5939 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5940 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5941 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5942 | return fixup(self, fixupper); |
| 5943 | } |
| 5944 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5945 | PyDoc_STRVAR(zfill__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5946 | "S.zfill(width) -> unicode\n\ |
| 5947 | \n\ |
| 5948 | 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] | 5949 | of the specified width. The string x is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5950 | |
| 5951 | static PyObject * |
| 5952 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 5953 | { |
| 5954 | int fill; |
| 5955 | PyUnicodeObject *u; |
| 5956 | |
| 5957 | int width; |
| 5958 | if (!PyArg_ParseTuple(args, "i:zfill", &width)) |
| 5959 | return NULL; |
| 5960 | |
| 5961 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 5962 | if (PyUnicode_CheckExact(self)) { |
| 5963 | Py_INCREF(self); |
| 5964 | return (PyObject*) self; |
| 5965 | } |
| 5966 | else |
| 5967 | return PyUnicode_FromUnicode( |
| 5968 | PyUnicode_AS_UNICODE(self), |
| 5969 | PyUnicode_GET_SIZE(self) |
| 5970 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5971 | } |
| 5972 | |
| 5973 | fill = width - self->length; |
| 5974 | |
| 5975 | u = pad(self, fill, 0, '0'); |
| 5976 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 5977 | if (u == NULL) |
| 5978 | return NULL; |
| 5979 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5980 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 5981 | /* move sign to beginning of string */ |
| 5982 | u->str[0] = u->str[fill]; |
| 5983 | u->str[fill] = '0'; |
| 5984 | } |
| 5985 | |
| 5986 | return (PyObject*) u; |
| 5987 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5988 | |
| 5989 | #if 0 |
| 5990 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5991 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5992 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5993 | return PyInt_FromLong(unicode_freelist_size); |
| 5994 | } |
| 5995 | #endif |
| 5996 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5997 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5998 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5999 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 6000 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 6001 | With optional start, test S beginning at that position.\n\ |
| 6002 | With optional end, stop comparing S at that position."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6003 | |
| 6004 | static PyObject * |
| 6005 | unicode_startswith(PyUnicodeObject *self, |
| 6006 | PyObject *args) |
| 6007 | { |
| 6008 | PyUnicodeObject *substring; |
| 6009 | int start = 0; |
| 6010 | int end = INT_MAX; |
| 6011 | PyObject *result; |
| 6012 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6013 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &substring, |
| 6014 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6015 | return NULL; |
| 6016 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6017 | (PyObject *)substring); |
| 6018 | if (substring == NULL) |
| 6019 | return NULL; |
| 6020 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6021 | result = PyBool_FromLong(tailmatch(self, substring, start, end, -1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6022 | |
| 6023 | Py_DECREF(substring); |
| 6024 | return result; |
| 6025 | } |
| 6026 | |
| 6027 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6028 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6029 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6030 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 6031 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 6032 | With optional start, test S beginning at that position.\n\ |
| 6033 | With optional end, stop comparing S at that position."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6034 | |
| 6035 | static PyObject * |
| 6036 | unicode_endswith(PyUnicodeObject *self, |
| 6037 | PyObject *args) |
| 6038 | { |
| 6039 | PyUnicodeObject *substring; |
| 6040 | int start = 0; |
| 6041 | int end = INT_MAX; |
| 6042 | PyObject *result; |
| 6043 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6044 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &substring, |
| 6045 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6046 | return NULL; |
| 6047 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6048 | (PyObject *)substring); |
| 6049 | if (substring == NULL) |
| 6050 | return NULL; |
| 6051 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6052 | result = PyBool_FromLong(tailmatch(self, substring, start, end, +1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6053 | |
| 6054 | Py_DECREF(substring); |
| 6055 | return result; |
| 6056 | } |
| 6057 | |
| 6058 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 6059 | |
| 6060 | static PyObject * |
| 6061 | unicode_getnewargs(PyUnicodeObject *v) |
| 6062 | { |
| 6063 | return Py_BuildValue("(u#)", v->str, v->length); |
| 6064 | } |
| 6065 | |
| 6066 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6067 | static PyMethodDef unicode_methods[] = { |
| 6068 | |
| 6069 | /* Order is according to common usage: often used methods should |
| 6070 | appear first, since lookup is done sequentially. */ |
| 6071 | |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6072 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
| 6073 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 6074 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6075 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6076 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 6077 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 6078 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 6079 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 6080 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 6081 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 6082 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
| 6083 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 6084 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 6085 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6086 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6087 | /* {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */ |
| 6088 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 6089 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 6090 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6091 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6092 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6093 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6094 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 6095 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 6096 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 6097 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 6098 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 6099 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 6100 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 6101 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 6102 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 6103 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 6104 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 6105 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 6106 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 6107 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6108 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 6109 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6110 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6111 | #endif |
| 6112 | |
| 6113 | #if 0 |
| 6114 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6115 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6116 | #endif |
| 6117 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 6118 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6119 | {NULL, NULL} |
| 6120 | }; |
| 6121 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 6122 | static PyObject * |
| 6123 | unicode_mod(PyObject *v, PyObject *w) |
| 6124 | { |
| 6125 | if (!PyUnicode_Check(v)) { |
| 6126 | Py_INCREF(Py_NotImplemented); |
| 6127 | return Py_NotImplemented; |
| 6128 | } |
| 6129 | return PyUnicode_Format(v, w); |
| 6130 | } |
| 6131 | |
| 6132 | static PyNumberMethods unicode_as_number = { |
| 6133 | 0, /*nb_add*/ |
| 6134 | 0, /*nb_subtract*/ |
| 6135 | 0, /*nb_multiply*/ |
| 6136 | 0, /*nb_divide*/ |
| 6137 | unicode_mod, /*nb_remainder*/ |
| 6138 | }; |
| 6139 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6140 | static PySequenceMethods unicode_as_sequence = { |
| 6141 | (inquiry) unicode_length, /* sq_length */ |
| 6142 | (binaryfunc) PyUnicode_Concat, /* sq_concat */ |
| 6143 | (intargfunc) unicode_repeat, /* sq_repeat */ |
| 6144 | (intargfunc) unicode_getitem, /* sq_item */ |
| 6145 | (intintargfunc) unicode_slice, /* sq_slice */ |
| 6146 | 0, /* sq_ass_item */ |
| 6147 | 0, /* sq_ass_slice */ |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6148 | (objobjproc)PyUnicode_Contains, /*sq_contains*/ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6149 | }; |
| 6150 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6151 | static PyObject* |
| 6152 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 6153 | { |
| 6154 | if (PyInt_Check(item)) { |
| 6155 | long i = PyInt_AS_LONG(item); |
| 6156 | if (i < 0) |
| 6157 | i += PyString_GET_SIZE(self); |
| 6158 | return unicode_getitem(self, i); |
| 6159 | } else if (PyLong_Check(item)) { |
| 6160 | long i = PyLong_AsLong(item); |
| 6161 | if (i == -1 && PyErr_Occurred()) |
| 6162 | return NULL; |
| 6163 | if (i < 0) |
| 6164 | i += PyString_GET_SIZE(self); |
| 6165 | return unicode_getitem(self, i); |
| 6166 | } else if (PySlice_Check(item)) { |
| 6167 | int start, stop, step, slicelength, cur, i; |
| 6168 | Py_UNICODE* source_buf; |
| 6169 | Py_UNICODE* result_buf; |
| 6170 | PyObject* result; |
| 6171 | |
| 6172 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyString_GET_SIZE(self), |
| 6173 | &start, &stop, &step, &slicelength) < 0) { |
| 6174 | return NULL; |
| 6175 | } |
| 6176 | |
| 6177 | if (slicelength <= 0) { |
| 6178 | return PyUnicode_FromUnicode(NULL, 0); |
| 6179 | } else { |
| 6180 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
| 6181 | result_buf = PyMem_MALLOC(slicelength*sizeof(Py_UNICODE)); |
| 6182 | |
| 6183 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 6184 | result_buf[i] = source_buf[cur]; |
| 6185 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6186 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6187 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
| 6188 | PyMem_FREE(result_buf); |
| 6189 | return result; |
| 6190 | } |
| 6191 | } else { |
| 6192 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 6193 | return NULL; |
| 6194 | } |
| 6195 | } |
| 6196 | |
| 6197 | static PyMappingMethods unicode_as_mapping = { |
| 6198 | (inquiry)unicode_length, /* mp_length */ |
| 6199 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 6200 | (objobjargproc)0, /* mp_ass_subscript */ |
| 6201 | }; |
| 6202 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6203 | static int |
| 6204 | unicode_buffer_getreadbuf(PyUnicodeObject *self, |
| 6205 | int index, |
| 6206 | const void **ptr) |
| 6207 | { |
| 6208 | if (index != 0) { |
| 6209 | PyErr_SetString(PyExc_SystemError, |
| 6210 | "accessing non-existent unicode segment"); |
| 6211 | return -1; |
| 6212 | } |
| 6213 | *ptr = (void *) self->str; |
| 6214 | return PyUnicode_GET_DATA_SIZE(self); |
| 6215 | } |
| 6216 | |
| 6217 | static int |
| 6218 | unicode_buffer_getwritebuf(PyUnicodeObject *self, int index, |
| 6219 | const void **ptr) |
| 6220 | { |
| 6221 | PyErr_SetString(PyExc_TypeError, |
Neal Norwitz | 20e7213 | 2002-06-13 21:25:17 +0000 | [diff] [blame] | 6222 | "cannot use unicode as modifiable buffer"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6223 | return -1; |
| 6224 | } |
| 6225 | |
| 6226 | static int |
| 6227 | unicode_buffer_getsegcount(PyUnicodeObject *self, |
| 6228 | int *lenp) |
| 6229 | { |
| 6230 | if (lenp) |
| 6231 | *lenp = PyUnicode_GET_DATA_SIZE(self); |
| 6232 | return 1; |
| 6233 | } |
| 6234 | |
| 6235 | static int |
| 6236 | unicode_buffer_getcharbuf(PyUnicodeObject *self, |
| 6237 | int index, |
| 6238 | const void **ptr) |
| 6239 | { |
| 6240 | PyObject *str; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6241 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6242 | if (index != 0) { |
| 6243 | PyErr_SetString(PyExc_SystemError, |
| 6244 | "accessing non-existent unicode segment"); |
| 6245 | return -1; |
| 6246 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 6247 | str = _PyUnicode_AsDefaultEncodedString((PyObject *)self, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6248 | if (str == NULL) |
| 6249 | return -1; |
| 6250 | *ptr = (void *) PyString_AS_STRING(str); |
| 6251 | return PyString_GET_SIZE(str); |
| 6252 | } |
| 6253 | |
| 6254 | /* Helpers for PyUnicode_Format() */ |
| 6255 | |
| 6256 | static PyObject * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 6257 | getnextarg(PyObject *args, int arglen, int *p_argidx) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6258 | { |
| 6259 | int argidx = *p_argidx; |
| 6260 | if (argidx < arglen) { |
| 6261 | (*p_argidx)++; |
| 6262 | if (arglen < 0) |
| 6263 | return args; |
| 6264 | else |
| 6265 | return PyTuple_GetItem(args, argidx); |
| 6266 | } |
| 6267 | PyErr_SetString(PyExc_TypeError, |
| 6268 | "not enough arguments for format string"); |
| 6269 | return NULL; |
| 6270 | } |
| 6271 | |
| 6272 | #define F_LJUST (1<<0) |
| 6273 | #define F_SIGN (1<<1) |
| 6274 | #define F_BLANK (1<<2) |
| 6275 | #define F_ALT (1<<3) |
| 6276 | #define F_ZERO (1<<4) |
| 6277 | |
| 6278 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6279 | int usprintf(register Py_UNICODE *buffer, char *format, ...) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6280 | { |
| 6281 | register int i; |
| 6282 | int len; |
| 6283 | va_list va; |
| 6284 | char *charbuffer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6285 | va_start(va, format); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6286 | |
| 6287 | /* First, format the string as char array, then expand to Py_UNICODE |
| 6288 | array. */ |
| 6289 | charbuffer = (char *)buffer; |
| 6290 | len = vsprintf(charbuffer, format, va); |
| 6291 | for (i = len - 1; i >= 0; i--) |
| 6292 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
| 6293 | |
| 6294 | va_end(va); |
| 6295 | return len; |
| 6296 | } |
| 6297 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 6298 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 6299 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 6300 | formatting is done. */ |
| 6301 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6302 | static int |
| 6303 | formatfloat(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6304 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6305 | int flags, |
| 6306 | int prec, |
| 6307 | int type, |
| 6308 | PyObject *v) |
| 6309 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6310 | /* fmt = '%#.' + `prec` + `type` |
| 6311 | 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] | 6312 | char fmt[20]; |
| 6313 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6314 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6315 | x = PyFloat_AsDouble(v); |
| 6316 | if (x == -1.0 && PyErr_Occurred()) |
| 6317 | return -1; |
| 6318 | if (prec < 0) |
| 6319 | prec = 6; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6320 | if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
| 6321 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6322 | /* Worst case length calc to ensure no buffer overrun: |
| 6323 | |
| 6324 | 'g' formats: |
| 6325 | fmt = %#.<prec>g |
| 6326 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 6327 | for any double rep.) |
| 6328 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 6329 | |
| 6330 | 'f' formats: |
| 6331 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 6332 | len = 1 + 50 + 1 + prec = 52 + prec |
| 6333 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6334 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6335 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6336 | |
| 6337 | */ |
| 6338 | if ((type == 'g' && buflen <= (size_t)10 + (size_t)prec) || |
| 6339 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6340 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6341 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6342 | return -1; |
| 6343 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6344 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 6345 | (flags&F_ALT) ? "#" : "", |
| 6346 | prec, type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6347 | return usprintf(buf, fmt, x); |
| 6348 | } |
| 6349 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6350 | static PyObject* |
| 6351 | formatlong(PyObject *val, int flags, int prec, int type) |
| 6352 | { |
| 6353 | char *buf; |
| 6354 | int i, len; |
| 6355 | PyObject *str; /* temporary string object. */ |
| 6356 | PyUnicodeObject *result; |
| 6357 | |
| 6358 | str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
| 6359 | if (!str) |
| 6360 | return NULL; |
| 6361 | result = _PyUnicode_New(len); |
| 6362 | for (i = 0; i < len; i++) |
| 6363 | result->str[i] = buf[i]; |
| 6364 | result->str[len] = 0; |
| 6365 | Py_DECREF(str); |
| 6366 | return (PyObject*)result; |
| 6367 | } |
| 6368 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6369 | static int |
| 6370 | formatint(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6371 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6372 | int flags, |
| 6373 | int prec, |
| 6374 | int type, |
| 6375 | PyObject *v) |
| 6376 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6377 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6378 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 6379 | * + 1 + 1 |
| 6380 | * = 24 |
| 6381 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6382 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6383 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6384 | long x; |
| 6385 | |
| 6386 | x = PyInt_AsLong(v); |
| 6387 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6388 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6389 | if (x < 0 && type == 'u') { |
| 6390 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 6391 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6392 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 6393 | sign = "-"; |
| 6394 | else |
| 6395 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6396 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6397 | prec = 1; |
| 6398 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6399 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 6400 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6401 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6402 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6403 | PyErr_SetString(PyExc_OverflowError, |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6404 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6405 | return -1; |
| 6406 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6407 | |
| 6408 | if ((flags & F_ALT) && |
| 6409 | (type == 'x' || type == 'X')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6410 | /* When converting under %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6411 | * of issues that cause pain: |
| 6412 | * - when 0 is being converted, the C standard leaves off |
| 6413 | * the '0x' or '0X', which is inconsistent with other |
| 6414 | * %#x/%#X conversions and inconsistent with Python's |
| 6415 | * hex() function |
| 6416 | * - there are platforms that violate the standard and |
| 6417 | * convert 0 with the '0x' or '0X' |
| 6418 | * (Metrowerks, Compaq Tru64) |
| 6419 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6420 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6421 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6422 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6423 | * We can achieve the desired consistency by inserting our |
| 6424 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 6425 | * of %#x/%#X. |
| 6426 | * |
| 6427 | * Note that this is the same approach as used in |
| 6428 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 6429 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6430 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 6431 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 6432 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6433 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6434 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 6435 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6436 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 6437 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6438 | if (sign[0]) |
| 6439 | return usprintf(buf, fmt, -x); |
| 6440 | else |
| 6441 | return usprintf(buf, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6442 | } |
| 6443 | |
| 6444 | static int |
| 6445 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6446 | size_t buflen, |
| 6447 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6448 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6449 | /* presume that the buffer is at least 2 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6450 | if (PyUnicode_Check(v)) { |
| 6451 | if (PyUnicode_GET_SIZE(v) != 1) |
| 6452 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6453 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6454 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6455 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6456 | else if (PyString_Check(v)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6457 | if (PyString_GET_SIZE(v) != 1) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6458 | goto onError; |
| 6459 | buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0]; |
| 6460 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6461 | |
| 6462 | else { |
| 6463 | /* Integer input truncated to a character */ |
| 6464 | long x; |
| 6465 | x = PyInt_AsLong(v); |
| 6466 | if (x == -1 && PyErr_Occurred()) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6467 | goto onError; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 6468 | #ifdef Py_UNICODE_WIDE |
| 6469 | if (x < 0 || x > 0x10ffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 6470 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 6471 | "%c arg not in range(0x110000) " |
| 6472 | "(wide Python build)"); |
| 6473 | return -1; |
| 6474 | } |
| 6475 | #else |
| 6476 | if (x < 0 || x > 0xffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 6477 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 6478 | "%c arg not in range(0x10000) " |
| 6479 | "(narrow Python build)"); |
| 6480 | return -1; |
| 6481 | } |
| 6482 | #endif |
| 6483 | buf[0] = (Py_UNICODE) x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6484 | } |
| 6485 | buf[1] = '\0'; |
| 6486 | return 1; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6487 | |
| 6488 | onError: |
| 6489 | PyErr_SetString(PyExc_TypeError, |
| 6490 | "%c requires int or char"); |
| 6491 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6492 | } |
| 6493 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6494 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 6495 | |
| 6496 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 6497 | chars are formatted. XXX This is a magic number. Each formatting |
| 6498 | routine does bounds checking to ensure no overflow, but a better |
| 6499 | solution may be to malloc a buffer of appropriate size for each |
| 6500 | format. For now, the current solution is sufficient. |
| 6501 | */ |
| 6502 | #define FORMATBUFLEN (size_t)120 |
| 6503 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6504 | PyObject *PyUnicode_Format(PyObject *format, |
| 6505 | PyObject *args) |
| 6506 | { |
| 6507 | Py_UNICODE *fmt, *res; |
| 6508 | int fmtcnt, rescnt, reslen, arglen, argidx; |
| 6509 | int args_owned = 0; |
| 6510 | PyUnicodeObject *result = NULL; |
| 6511 | PyObject *dict = NULL; |
| 6512 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6513 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6514 | if (format == NULL || args == NULL) { |
| 6515 | PyErr_BadInternalCall(); |
| 6516 | return NULL; |
| 6517 | } |
| 6518 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6519 | if (uformat == NULL) |
| 6520 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6521 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 6522 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 6523 | |
| 6524 | reslen = rescnt = fmtcnt + 100; |
| 6525 | result = _PyUnicode_New(reslen); |
| 6526 | if (result == NULL) |
| 6527 | goto onError; |
| 6528 | res = PyUnicode_AS_UNICODE(result); |
| 6529 | |
| 6530 | if (PyTuple_Check(args)) { |
| 6531 | arglen = PyTuple_Size(args); |
| 6532 | argidx = 0; |
| 6533 | } |
| 6534 | else { |
| 6535 | arglen = -1; |
| 6536 | argidx = -2; |
| 6537 | } |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 6538 | if (args->ob_type->tp_as_mapping && !PyTuple_Check(args) && |
| 6539 | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6540 | dict = args; |
| 6541 | |
| 6542 | while (--fmtcnt >= 0) { |
| 6543 | if (*fmt != '%') { |
| 6544 | if (--rescnt < 0) { |
| 6545 | rescnt = fmtcnt + 100; |
| 6546 | reslen += rescnt; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6547 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6548 | return NULL; |
| 6549 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 6550 | --rescnt; |
| 6551 | } |
| 6552 | *res++ = *fmt++; |
| 6553 | } |
| 6554 | else { |
| 6555 | /* Got a format specifier */ |
| 6556 | int flags = 0; |
| 6557 | int width = -1; |
| 6558 | int prec = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6559 | Py_UNICODE c = '\0'; |
| 6560 | Py_UNICODE fill; |
| 6561 | PyObject *v = NULL; |
| 6562 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6563 | Py_UNICODE *pbuf; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6564 | Py_UNICODE sign; |
| 6565 | int len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6566 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6567 | |
| 6568 | fmt++; |
| 6569 | if (*fmt == '(') { |
| 6570 | Py_UNICODE *keystart; |
| 6571 | int keylen; |
| 6572 | PyObject *key; |
| 6573 | int pcount = 1; |
| 6574 | |
| 6575 | if (dict == NULL) { |
| 6576 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6577 | "format requires a mapping"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6578 | goto onError; |
| 6579 | } |
| 6580 | ++fmt; |
| 6581 | --fmtcnt; |
| 6582 | keystart = fmt; |
| 6583 | /* Skip over balanced parentheses */ |
| 6584 | while (pcount > 0 && --fmtcnt >= 0) { |
| 6585 | if (*fmt == ')') |
| 6586 | --pcount; |
| 6587 | else if (*fmt == '(') |
| 6588 | ++pcount; |
| 6589 | fmt++; |
| 6590 | } |
| 6591 | keylen = fmt - keystart - 1; |
| 6592 | if (fmtcnt < 0 || pcount > 0) { |
| 6593 | PyErr_SetString(PyExc_ValueError, |
| 6594 | "incomplete format key"); |
| 6595 | goto onError; |
| 6596 | } |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 6597 | #if 0 |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6598 | /* keys are converted to strings using UTF-8 and |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6599 | then looked up since Python uses strings to hold |
| 6600 | variables names etc. in its namespaces and we |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6601 | wouldn't want to break common idioms. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6602 | key = PyUnicode_EncodeUTF8(keystart, |
| 6603 | keylen, |
| 6604 | NULL); |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 6605 | #else |
| 6606 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 6607 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6608 | if (key == NULL) |
| 6609 | goto onError; |
| 6610 | if (args_owned) { |
| 6611 | Py_DECREF(args); |
| 6612 | args_owned = 0; |
| 6613 | } |
| 6614 | args = PyObject_GetItem(dict, key); |
| 6615 | Py_DECREF(key); |
| 6616 | if (args == NULL) { |
| 6617 | goto onError; |
| 6618 | } |
| 6619 | args_owned = 1; |
| 6620 | arglen = -1; |
| 6621 | argidx = -2; |
| 6622 | } |
| 6623 | while (--fmtcnt >= 0) { |
| 6624 | switch (c = *fmt++) { |
| 6625 | case '-': flags |= F_LJUST; continue; |
| 6626 | case '+': flags |= F_SIGN; continue; |
| 6627 | case ' ': flags |= F_BLANK; continue; |
| 6628 | case '#': flags |= F_ALT; continue; |
| 6629 | case '0': flags |= F_ZERO; continue; |
| 6630 | } |
| 6631 | break; |
| 6632 | } |
| 6633 | if (c == '*') { |
| 6634 | v = getnextarg(args, arglen, &argidx); |
| 6635 | if (v == NULL) |
| 6636 | goto onError; |
| 6637 | if (!PyInt_Check(v)) { |
| 6638 | PyErr_SetString(PyExc_TypeError, |
| 6639 | "* wants int"); |
| 6640 | goto onError; |
| 6641 | } |
| 6642 | width = PyInt_AsLong(v); |
| 6643 | if (width < 0) { |
| 6644 | flags |= F_LJUST; |
| 6645 | width = -width; |
| 6646 | } |
| 6647 | if (--fmtcnt >= 0) |
| 6648 | c = *fmt++; |
| 6649 | } |
| 6650 | else if (c >= '0' && c <= '9') { |
| 6651 | width = c - '0'; |
| 6652 | while (--fmtcnt >= 0) { |
| 6653 | c = *fmt++; |
| 6654 | if (c < '0' || c > '9') |
| 6655 | break; |
| 6656 | if ((width*10) / 10 != width) { |
| 6657 | PyErr_SetString(PyExc_ValueError, |
| 6658 | "width too big"); |
| 6659 | goto onError; |
| 6660 | } |
| 6661 | width = width*10 + (c - '0'); |
| 6662 | } |
| 6663 | } |
| 6664 | if (c == '.') { |
| 6665 | prec = 0; |
| 6666 | if (--fmtcnt >= 0) |
| 6667 | c = *fmt++; |
| 6668 | if (c == '*') { |
| 6669 | v = getnextarg(args, arglen, &argidx); |
| 6670 | if (v == NULL) |
| 6671 | goto onError; |
| 6672 | if (!PyInt_Check(v)) { |
| 6673 | PyErr_SetString(PyExc_TypeError, |
| 6674 | "* wants int"); |
| 6675 | goto onError; |
| 6676 | } |
| 6677 | prec = PyInt_AsLong(v); |
| 6678 | if (prec < 0) |
| 6679 | prec = 0; |
| 6680 | if (--fmtcnt >= 0) |
| 6681 | c = *fmt++; |
| 6682 | } |
| 6683 | else if (c >= '0' && c <= '9') { |
| 6684 | prec = c - '0'; |
| 6685 | while (--fmtcnt >= 0) { |
| 6686 | c = Py_CHARMASK(*fmt++); |
| 6687 | if (c < '0' || c > '9') |
| 6688 | break; |
| 6689 | if ((prec*10) / 10 != prec) { |
| 6690 | PyErr_SetString(PyExc_ValueError, |
| 6691 | "prec too big"); |
| 6692 | goto onError; |
| 6693 | } |
| 6694 | prec = prec*10 + (c - '0'); |
| 6695 | } |
| 6696 | } |
| 6697 | } /* prec */ |
| 6698 | if (fmtcnt >= 0) { |
| 6699 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6700 | if (--fmtcnt >= 0) |
| 6701 | c = *fmt++; |
| 6702 | } |
| 6703 | } |
| 6704 | if (fmtcnt < 0) { |
| 6705 | PyErr_SetString(PyExc_ValueError, |
| 6706 | "incomplete format"); |
| 6707 | goto onError; |
| 6708 | } |
| 6709 | if (c != '%') { |
| 6710 | v = getnextarg(args, arglen, &argidx); |
| 6711 | if (v == NULL) |
| 6712 | goto onError; |
| 6713 | } |
| 6714 | sign = 0; |
| 6715 | fill = ' '; |
| 6716 | switch (c) { |
| 6717 | |
| 6718 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6719 | pbuf = formatbuf; |
| 6720 | /* presume that buffer length is at least 1 */ |
| 6721 | pbuf[0] = '%'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6722 | len = 1; |
| 6723 | break; |
| 6724 | |
| 6725 | case 's': |
| 6726 | case 'r': |
| 6727 | if (PyUnicode_Check(v) && c == 's') { |
| 6728 | temp = v; |
| 6729 | Py_INCREF(temp); |
| 6730 | } |
| 6731 | else { |
| 6732 | PyObject *unicode; |
| 6733 | if (c == 's') |
| 6734 | temp = PyObject_Str(v); |
| 6735 | else |
| 6736 | temp = PyObject_Repr(v); |
| 6737 | if (temp == NULL) |
| 6738 | goto onError; |
| 6739 | if (!PyString_Check(temp)) { |
| 6740 | /* XXX Note: this should never happen, since |
| 6741 | PyObject_Repr() and PyObject_Str() assure |
| 6742 | this */ |
| 6743 | Py_DECREF(temp); |
| 6744 | PyErr_SetString(PyExc_TypeError, |
| 6745 | "%s argument has non-string str()"); |
| 6746 | goto onError; |
| 6747 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6748 | unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6749 | PyString_GET_SIZE(temp), |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6750 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6751 | "strict"); |
| 6752 | Py_DECREF(temp); |
| 6753 | temp = unicode; |
| 6754 | if (temp == NULL) |
| 6755 | goto onError; |
| 6756 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6757 | pbuf = PyUnicode_AS_UNICODE(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6758 | len = PyUnicode_GET_SIZE(temp); |
| 6759 | if (prec >= 0 && len > prec) |
| 6760 | len = prec; |
| 6761 | break; |
| 6762 | |
| 6763 | case 'i': |
| 6764 | case 'd': |
| 6765 | case 'u': |
| 6766 | case 'o': |
| 6767 | case 'x': |
| 6768 | case 'X': |
| 6769 | if (c == 'i') |
| 6770 | c = 'd'; |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 6771 | if (PyLong_Check(v)) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6772 | temp = formatlong(v, flags, prec, c); |
| 6773 | if (!temp) |
| 6774 | goto onError; |
| 6775 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 6776 | len = PyUnicode_GET_SIZE(temp); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6777 | sign = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6778 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6779 | else { |
| 6780 | pbuf = formatbuf; |
| 6781 | len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 6782 | flags, prec, c, v); |
| 6783 | if (len < 0) |
| 6784 | goto onError; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6785 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6786 | } |
| 6787 | if (flags & F_ZERO) |
| 6788 | fill = '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6789 | break; |
| 6790 | |
| 6791 | case 'e': |
| 6792 | case 'E': |
| 6793 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 6794 | case 'F': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6795 | case 'g': |
| 6796 | case 'G': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 6797 | if (c == 'F') |
| 6798 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6799 | pbuf = formatbuf; |
| 6800 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 6801 | flags, prec, c, v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6802 | if (len < 0) |
| 6803 | goto onError; |
| 6804 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6805 | if (flags & F_ZERO) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6806 | fill = '0'; |
| 6807 | break; |
| 6808 | |
| 6809 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6810 | pbuf = formatbuf; |
| 6811 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6812 | if (len < 0) |
| 6813 | goto onError; |
| 6814 | break; |
| 6815 | |
| 6816 | default: |
| 6817 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 6818 | "unsupported format character '%c' (0x%x) " |
| 6819 | "at index %i", |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6820 | (31<=c && c<=126) ? (char)c : '?', |
Marc-André Lemburg | 24e53b6 | 2002-09-24 09:32:14 +0000 | [diff] [blame] | 6821 | (int)c, |
Guido van Rossum | efc1188 | 2002-09-12 14:43:41 +0000 | [diff] [blame] | 6822 | (int)(fmt -1 - PyUnicode_AS_UNICODE(uformat))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6823 | goto onError; |
| 6824 | } |
| 6825 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6826 | if (*pbuf == '-' || *pbuf == '+') { |
| 6827 | sign = *pbuf++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6828 | len--; |
| 6829 | } |
| 6830 | else if (flags & F_SIGN) |
| 6831 | sign = '+'; |
| 6832 | else if (flags & F_BLANK) |
| 6833 | sign = ' '; |
| 6834 | else |
| 6835 | sign = 0; |
| 6836 | } |
| 6837 | if (width < len) |
| 6838 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 6839 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6840 | reslen -= rescnt; |
| 6841 | rescnt = width + fmtcnt + 100; |
| 6842 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 6843 | if (reslen < 0) { |
| 6844 | Py_DECREF(result); |
| 6845 | return PyErr_NoMemory(); |
| 6846 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6847 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6848 | return NULL; |
| 6849 | res = PyUnicode_AS_UNICODE(result) |
| 6850 | + reslen - rescnt; |
| 6851 | } |
| 6852 | if (sign) { |
| 6853 | if (fill != ' ') |
| 6854 | *res++ = sign; |
| 6855 | rescnt--; |
| 6856 | if (width > len) |
| 6857 | width--; |
| 6858 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6859 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 6860 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 6861 | assert(pbuf[1] == c); |
| 6862 | if (fill != ' ') { |
| 6863 | *res++ = *pbuf++; |
| 6864 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6865 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 6866 | rescnt -= 2; |
| 6867 | width -= 2; |
| 6868 | if (width < 0) |
| 6869 | width = 0; |
| 6870 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6871 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6872 | if (width > len && !(flags & F_LJUST)) { |
| 6873 | do { |
| 6874 | --rescnt; |
| 6875 | *res++ = fill; |
| 6876 | } while (--width > len); |
| 6877 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6878 | if (fill == ' ') { |
| 6879 | if (sign) |
| 6880 | *res++ = sign; |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 6881 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6882 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 6883 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6884 | *res++ = *pbuf++; |
| 6885 | *res++ = *pbuf++; |
| 6886 | } |
| 6887 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6888 | Py_UNICODE_COPY(res, pbuf, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6889 | res += len; |
| 6890 | rescnt -= len; |
| 6891 | while (--width >= len) { |
| 6892 | --rescnt; |
| 6893 | *res++ = ' '; |
| 6894 | } |
| 6895 | if (dict && (argidx < arglen) && c != '%') { |
| 6896 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 6897 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6898 | goto onError; |
| 6899 | } |
| 6900 | Py_XDECREF(temp); |
| 6901 | } /* '%' */ |
| 6902 | } /* until end */ |
| 6903 | if (argidx < arglen && !dict) { |
| 6904 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 6905 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6906 | goto onError; |
| 6907 | } |
| 6908 | |
| 6909 | if (args_owned) { |
| 6910 | Py_DECREF(args); |
| 6911 | } |
| 6912 | Py_DECREF(uformat); |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 6913 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 6914 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6915 | return (PyObject *)result; |
| 6916 | |
| 6917 | onError: |
| 6918 | Py_XDECREF(result); |
| 6919 | Py_DECREF(uformat); |
| 6920 | if (args_owned) { |
| 6921 | Py_DECREF(args); |
| 6922 | } |
| 6923 | return NULL; |
| 6924 | } |
| 6925 | |
| 6926 | static PyBufferProcs unicode_as_buffer = { |
| 6927 | (getreadbufferproc) unicode_buffer_getreadbuf, |
| 6928 | (getwritebufferproc) unicode_buffer_getwritebuf, |
| 6929 | (getsegcountproc) unicode_buffer_getsegcount, |
| 6930 | (getcharbufferproc) unicode_buffer_getcharbuf, |
| 6931 | }; |
| 6932 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 6933 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 6934 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 6935 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6936 | static PyObject * |
| 6937 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 6938 | { |
| 6939 | PyObject *x = NULL; |
| 6940 | static char *kwlist[] = {"string", "encoding", "errors", 0}; |
| 6941 | char *encoding = NULL; |
| 6942 | char *errors = NULL; |
| 6943 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 6944 | if (type != &PyUnicode_Type) |
| 6945 | return unicode_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6946 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
| 6947 | kwlist, &x, &encoding, &errors)) |
| 6948 | return NULL; |
| 6949 | if (x == NULL) |
| 6950 | return (PyObject *)_PyUnicode_New(0); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 6951 | if (encoding == NULL && errors == NULL) |
| 6952 | return PyObject_Unicode(x); |
| 6953 | else |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6954 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
| 6955 | } |
| 6956 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 6957 | static PyObject * |
| 6958 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 6959 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 6960 | PyUnicodeObject *tmp, *pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 6961 | int n; |
| 6962 | |
| 6963 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 6964 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 6965 | if (tmp == NULL) |
| 6966 | return NULL; |
| 6967 | assert(PyUnicode_Check(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 6968 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 6969 | if (pnew == NULL) { |
| 6970 | Py_DECREF(tmp); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 6971 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 6972 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 6973 | pnew->str = PyMem_NEW(Py_UNICODE, n+1); |
| 6974 | if (pnew->str == NULL) { |
| 6975 | _Py_ForgetReference((PyObject *)pnew); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 6976 | PyObject_Del(pnew); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 6977 | Py_DECREF(tmp); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 6978 | return PyErr_NoMemory(); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 6979 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 6980 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 6981 | pnew->length = n; |
| 6982 | pnew->hash = tmp->hash; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 6983 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 6984 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 6985 | } |
| 6986 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6987 | PyDoc_STRVAR(unicode_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6988 | "unicode(string [, encoding[, errors]]) -> object\n\ |
| 6989 | \n\ |
| 6990 | Create a new Unicode object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 6991 | encoding defaults to the current default string encoding.\n\ |
| 6992 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6993 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6994 | PyTypeObject PyUnicode_Type = { |
| 6995 | PyObject_HEAD_INIT(&PyType_Type) |
| 6996 | 0, /* ob_size */ |
| 6997 | "unicode", /* tp_name */ |
| 6998 | sizeof(PyUnicodeObject), /* tp_size */ |
| 6999 | 0, /* tp_itemsize */ |
| 7000 | /* Slots */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 7001 | (destructor)unicode_dealloc, /* tp_dealloc */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7002 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7003 | 0, /* tp_getattr */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7004 | 0, /* tp_setattr */ |
| 7005 | (cmpfunc) unicode_compare, /* tp_compare */ |
| 7006 | (reprfunc) unicode_repr, /* tp_repr */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7007 | &unicode_as_number, /* tp_as_number */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7008 | &unicode_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7009 | &unicode_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7010 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 7011 | 0, /* tp_call*/ |
| 7012 | (reprfunc) unicode_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7013 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 7014 | 0, /* tp_setattro */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7015 | &unicode_as_buffer, /* tp_as_buffer */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7016 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
| 7017 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7018 | unicode_doc, /* tp_doc */ |
| 7019 | 0, /* tp_traverse */ |
| 7020 | 0, /* tp_clear */ |
| 7021 | 0, /* tp_richcompare */ |
| 7022 | 0, /* tp_weaklistoffset */ |
| 7023 | 0, /* tp_iter */ |
| 7024 | 0, /* tp_iternext */ |
| 7025 | unicode_methods, /* tp_methods */ |
| 7026 | 0, /* tp_members */ |
| 7027 | 0, /* tp_getset */ |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 7028 | &PyBaseString_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7029 | 0, /* tp_dict */ |
| 7030 | 0, /* tp_descr_get */ |
| 7031 | 0, /* tp_descr_set */ |
| 7032 | 0, /* tp_dictoffset */ |
| 7033 | 0, /* tp_init */ |
| 7034 | 0, /* tp_alloc */ |
| 7035 | unicode_new, /* tp_new */ |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 7036 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7037 | }; |
| 7038 | |
| 7039 | /* Initialize the Unicode implementation */ |
| 7040 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 7041 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7042 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7043 | int i; |
| 7044 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7045 | /* Init the implementation */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7046 | unicode_freelist = NULL; |
| 7047 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7048 | unicode_empty = _PyUnicode_New(0); |
Marc-André Lemburg | 90e8147 | 2000-06-07 09:13:21 +0000 | [diff] [blame] | 7049 | strcpy(unicode_default_encoding, "ascii"); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7050 | for (i = 0; i < 256; i++) |
| 7051 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 7052 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 7053 | Py_FatalError("Can't initialize 'unicode'"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7054 | } |
| 7055 | |
| 7056 | /* Finalize the Unicode implementation */ |
| 7057 | |
| 7058 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 7059 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7060 | { |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 7061 | PyUnicodeObject *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7062 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7063 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 7064 | Py_XDECREF(unicode_empty); |
| 7065 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 7066 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7067 | for (i = 0; i < 256; i++) { |
| 7068 | if (unicode_latin1[i]) { |
| 7069 | Py_DECREF(unicode_latin1[i]); |
| 7070 | unicode_latin1[i] = NULL; |
| 7071 | } |
| 7072 | } |
| 7073 | |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 7074 | for (u = unicode_freelist; u != NULL;) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7075 | PyUnicodeObject *v = u; |
| 7076 | u = *(PyUnicodeObject **)u; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 7077 | if (v->str) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 7078 | PyMem_DEL(v->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 7079 | Py_XDECREF(v->defenc); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 7080 | PyObject_Del(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7081 | } |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7082 | unicode_freelist = NULL; |
| 7083 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7084 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 7085 | |
| 7086 | /* |
| 7087 | Local variables: |
| 7088 | c-basic-offset: 4 |
| 7089 | indent-tabs-mode: nil |
| 7090 | End: |
| 7091 | */ |