Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1 | /* |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2 | |
| 3 | Unicode implementation based on original code by Fredrik Lundh, |
Fred Drake | 785d14f | 2000-05-09 19:54:43 +0000 | [diff] [blame] | 4 | modified by Marc-Andre Lemburg <mal@lemburg.com> according to the |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5 | Unicode Integration Proposal (see file Misc/unicode.txt). |
| 6 | |
Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 7 | Copyright (c) Corporation for National Research Initiatives. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 9 | -------------------------------------------------------------------- |
| 10 | The original string type implementation is: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 12 | Copyright (c) 1999 by Secret Labs AB |
| 13 | Copyright (c) 1999 by Fredrik Lundh |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 15 | By obtaining, using, and/or copying this software and/or its |
| 16 | associated documentation, you agree that you have read, understood, |
| 17 | and will comply with the following terms and conditions: |
| 18 | |
| 19 | Permission to use, copy, modify, and distribute this software and its |
| 20 | associated documentation for any purpose and without fee is hereby |
| 21 | granted, provided that the above copyright notice appears in all |
| 22 | copies, and that both that copyright notice and this permission notice |
| 23 | appear in supporting documentation, and that the name of Secret Labs |
| 24 | AB or the author not be used in advertising or publicity pertaining to |
| 25 | distribution of the software without specific, written prior |
| 26 | permission. |
| 27 | |
| 28 | SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 29 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 30 | FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR |
| 31 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 32 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 33 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 34 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 35 | -------------------------------------------------------------------- |
| 36 | |
| 37 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 38 | |
| 39 | #include "Python.h" |
| 40 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 41 | #include "unicodeobject.h" |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 42 | #include "ucnhash.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 43 | |
Nicholas Bastin | 1ce9e4c | 2004-06-17 18:27:18 +0000 | [diff] [blame] | 44 | #ifdef __SUNPRO_C |
| 45 | #pragma error_messages (off,E_END_OF_LOOP_CODE_NOT_REACHED) |
| 46 | #endif |
| 47 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 48 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 49 | #include <windows.h> |
| 50 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 52 | /* Limit for the Unicode object free list */ |
| 53 | |
| 54 | #define MAX_UNICODE_FREELIST_SIZE 1024 |
| 55 | |
| 56 | /* Limit for the Unicode object free list stay alive optimization. |
| 57 | |
| 58 | The implementation will keep allocated Unicode memory intact for |
| 59 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 60 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 61 | |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 62 | At worst this will result in MAX_UNICODE_FREELIST_SIZE * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 63 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 64 | malloc()-overhead) bytes of unused garbage. |
| 65 | |
| 66 | Setting the limit to 0 effectively turns the feature off. |
| 67 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 68 | Note: This is an experimental feature ! If you get core dumps when |
| 69 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 70 | |
| 71 | */ |
| 72 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 73 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 74 | |
| 75 | /* Endianness switches; defaults to little endian */ |
| 76 | |
| 77 | #ifdef WORDS_BIGENDIAN |
| 78 | # define BYTEORDER_IS_BIG_ENDIAN |
| 79 | #else |
| 80 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 81 | #endif |
| 82 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 83 | /* --- Globals ------------------------------------------------------------ |
| 84 | |
| 85 | The globals are initialized by the _PyUnicode_Init() API and should |
| 86 | not be used before calling that API. |
| 87 | |
| 88 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 89 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 90 | /* Free list for Unicode objects */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 91 | static PyUnicodeObject *unicode_freelist; |
| 92 | static int unicode_freelist_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 93 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 94 | /* The empty Unicode object is shared to improve performance. */ |
| 95 | static PyUnicodeObject *unicode_empty; |
| 96 | |
| 97 | /* Single character Unicode strings in the Latin-1 range are being |
| 98 | shared as well. */ |
| 99 | static PyUnicodeObject *unicode_latin1[256]; |
| 100 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 101 | /* Default encoding to use and assume when NULL is passed as encoding |
| 102 | parameter; it is initialized by _PyUnicode_Init(). |
| 103 | |
| 104 | Always use the PyUnicode_SetDefaultEncoding() and |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 105 | PyUnicode_GetDefaultEncoding() APIs to access this global. |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 106 | |
| 107 | */ |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 108 | static char unicode_default_encoding[100]; |
| 109 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 110 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 111 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 112 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 113 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 114 | return 0x10FFFF; |
| 115 | #else |
| 116 | /* This is actually an illegal character, so it should |
| 117 | not be passed to unichr. */ |
| 118 | return 0xFFFF; |
| 119 | #endif |
| 120 | } |
| 121 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 122 | /* --- Unicode Object ----------------------------------------------------- */ |
| 123 | |
| 124 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 125 | int unicode_resize(register PyUnicodeObject *unicode, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 126 | int length) |
| 127 | { |
| 128 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 129 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 130 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 131 | if (unicode->length == length) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 132 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 133 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 134 | /* Resizing shared object (unicode_empty or single character |
| 135 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 136 | instead ! */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 137 | if (unicode == unicode_empty || |
| 138 | (unicode->length == 1 && |
| 139 | /* MvL said unicode->str[] may be signed. Python generally assumes |
| 140 | * an int contains at least 32 bits, and we don't use more than |
| 141 | * 32 bits even in a UCS4 build, so casting to unsigned int should |
| 142 | * be correct. |
| 143 | */ |
| 144 | (unsigned int)unicode->str[0] < 256U && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 145 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 146 | PyErr_SetString(PyExc_SystemError, |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 147 | "can't resize shared unicode objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 148 | return -1; |
| 149 | } |
| 150 | |
| 151 | /* We allocate one more byte to make sure the string is |
| 152 | Ux0000 terminated -- XXX is this needed ? */ |
| 153 | oldstr = unicode->str; |
| 154 | PyMem_RESIZE(unicode->str, Py_UNICODE, length + 1); |
| 155 | if (!unicode->str) { |
| 156 | unicode->str = oldstr; |
| 157 | PyErr_NoMemory(); |
| 158 | return -1; |
| 159 | } |
| 160 | unicode->str[length] = 0; |
| 161 | unicode->length = length; |
| 162 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 163 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 164 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 165 | if (unicode->defenc) { |
| 166 | Py_DECREF(unicode->defenc); |
| 167 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 168 | } |
| 169 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 170 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | /* We allocate one more byte to make sure the string is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 175 | Ux0000 terminated -- XXX is this needed ? |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 176 | |
| 177 | XXX This allocator could further be enhanced by assuring that the |
| 178 | free list never reduces its size below 1. |
| 179 | |
| 180 | */ |
| 181 | |
| 182 | static |
| 183 | PyUnicodeObject *_PyUnicode_New(int length) |
| 184 | { |
| 185 | register PyUnicodeObject *unicode; |
| 186 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 187 | /* Optimization fo empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 188 | if (length == 0 && unicode_empty != NULL) { |
| 189 | Py_INCREF(unicode_empty); |
| 190 | return unicode_empty; |
| 191 | } |
| 192 | |
| 193 | /* Unicode freelist & memory allocation */ |
| 194 | if (unicode_freelist) { |
| 195 | unicode = unicode_freelist; |
Marc-André Lemburg | bea47e7 | 2000-06-17 20:31:17 +0000 | [diff] [blame] | 196 | unicode_freelist = *(PyUnicodeObject **)unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 197 | unicode_freelist_size--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 198 | if (unicode->str) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 199 | /* Keep-Alive optimization: we only upsize the buffer, |
| 200 | never downsize it. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 201 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 202 | unicode_resize(unicode, length) < 0) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 203 | PyMem_DEL(unicode->str); |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 204 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 205 | } |
| 206 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 207 | else { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 208 | unicode->str = PyMem_NEW(Py_UNICODE, length + 1); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 209 | } |
| 210 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 211 | } |
| 212 | else { |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 213 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 214 | if (unicode == NULL) |
| 215 | return NULL; |
| 216 | unicode->str = PyMem_NEW(Py_UNICODE, length + 1); |
| 217 | } |
| 218 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 219 | if (!unicode->str) { |
| 220 | PyErr_NoMemory(); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 221 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 222 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 223 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 224 | * the caller fails before initializing str -- unicode_resize() |
| 225 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 226 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 227 | * We don't want unicode_resize to read uninitialized memory in |
| 228 | * that case. |
| 229 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 230 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 231 | unicode->str[length] = 0; |
| 232 | unicode->length = length; |
| 233 | unicode->hash = -1; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 234 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 235 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 236 | |
| 237 | onError: |
| 238 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 239 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 240 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 244 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 245 | { |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 246 | if (PyUnicode_CheckExact(unicode) && |
| 247 | unicode_freelist_size < MAX_UNICODE_FREELIST_SIZE) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 248 | /* Keep-Alive optimization */ |
| 249 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 250 | PyMem_DEL(unicode->str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 251 | unicode->str = NULL; |
| 252 | unicode->length = 0; |
| 253 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 254 | if (unicode->defenc) { |
| 255 | Py_DECREF(unicode->defenc); |
| 256 | unicode->defenc = NULL; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 257 | } |
| 258 | /* Add to free list */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 259 | *(PyUnicodeObject **)unicode = unicode_freelist; |
| 260 | unicode_freelist = unicode; |
| 261 | unicode_freelist_size++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 262 | } |
| 263 | else { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 264 | PyMem_DEL(unicode->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 265 | Py_XDECREF(unicode->defenc); |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 266 | unicode->ob_type->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 270 | int PyUnicode_Resize(PyObject **unicode, int length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 271 | { |
| 272 | register PyUnicodeObject *v; |
| 273 | |
| 274 | /* Argument checks */ |
| 275 | if (unicode == NULL) { |
| 276 | PyErr_BadInternalCall(); |
| 277 | return -1; |
| 278 | } |
| 279 | v = (PyUnicodeObject *)*unicode; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 280 | if (v == NULL || !PyUnicode_Check(v) || v->ob_refcnt != 1 || length < 0) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 281 | PyErr_BadInternalCall(); |
| 282 | return -1; |
| 283 | } |
| 284 | |
| 285 | /* Resizing unicode_empty and single character objects is not |
| 286 | possible since these are being shared. We simply return a fresh |
| 287 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 288 | if (v->length != length && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 289 | (v == unicode_empty || v->length == 1)) { |
| 290 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 291 | if (w == NULL) |
| 292 | return -1; |
| 293 | Py_UNICODE_COPY(w->str, v->str, |
| 294 | length < v->length ? length : v->length); |
Raymond Hettinger | c8df578 | 2003-03-09 07:30:43 +0000 | [diff] [blame] | 295 | Py_DECREF(*unicode); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 296 | *unicode = (PyObject *)w; |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 301 | objects, since we can modify them in-place. */ |
| 302 | return unicode_resize(v, length); |
| 303 | } |
| 304 | |
| 305 | /* Internal API for use in unicodeobject.c only ! */ |
| 306 | #define _PyUnicode_Resize(unicodevar, length) \ |
| 307 | PyUnicode_Resize(((PyObject **)(unicodevar)), length) |
| 308 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 309 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
| 310 | int size) |
| 311 | { |
| 312 | PyUnicodeObject *unicode; |
| 313 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 314 | /* If the Unicode data is known at construction time, we can apply |
| 315 | some optimizations which share commonly used objects. */ |
| 316 | if (u != NULL) { |
| 317 | |
| 318 | /* Optimization for empty strings */ |
| 319 | if (size == 0 && unicode_empty != NULL) { |
| 320 | Py_INCREF(unicode_empty); |
| 321 | return (PyObject *)unicode_empty; |
| 322 | } |
| 323 | |
| 324 | /* Single character Unicode objects in the Latin-1 range are |
| 325 | shared when using this constructor */ |
| 326 | if (size == 1 && *u < 256) { |
| 327 | unicode = unicode_latin1[*u]; |
| 328 | if (!unicode) { |
| 329 | unicode = _PyUnicode_New(1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 330 | if (!unicode) |
| 331 | return NULL; |
Marc-André Lemburg | 8879a33 | 2001-06-07 12:26:56 +0000 | [diff] [blame] | 332 | unicode->str[0] = *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 333 | unicode_latin1[*u] = unicode; |
| 334 | } |
| 335 | Py_INCREF(unicode); |
| 336 | return (PyObject *)unicode; |
| 337 | } |
| 338 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 339 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 340 | unicode = _PyUnicode_New(size); |
| 341 | if (!unicode) |
| 342 | return NULL; |
| 343 | |
| 344 | /* Copy the Unicode data into the new object */ |
| 345 | if (u != NULL) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 346 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 347 | |
| 348 | return (PyObject *)unicode; |
| 349 | } |
| 350 | |
| 351 | #ifdef HAVE_WCHAR_H |
| 352 | |
| 353 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
| 354 | int size) |
| 355 | { |
| 356 | PyUnicodeObject *unicode; |
| 357 | |
| 358 | if (w == NULL) { |
| 359 | PyErr_BadInternalCall(); |
| 360 | return NULL; |
| 361 | } |
| 362 | |
| 363 | unicode = _PyUnicode_New(size); |
| 364 | if (!unicode) |
| 365 | return NULL; |
| 366 | |
| 367 | /* Copy the wchar_t data into the new object */ |
| 368 | #ifdef HAVE_USABLE_WCHAR_T |
| 369 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 370 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 371 | { |
| 372 | register Py_UNICODE *u; |
| 373 | register int i; |
| 374 | u = PyUnicode_AS_UNICODE(unicode); |
| 375 | for (i = size; i >= 0; i--) |
| 376 | *u++ = *w++; |
| 377 | } |
| 378 | #endif |
| 379 | |
| 380 | return (PyObject *)unicode; |
| 381 | } |
| 382 | |
| 383 | int PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 384 | register wchar_t *w, |
| 385 | int size) |
| 386 | { |
| 387 | if (unicode == NULL) { |
| 388 | PyErr_BadInternalCall(); |
| 389 | return -1; |
| 390 | } |
| 391 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 392 | size = PyUnicode_GET_SIZE(unicode); |
| 393 | #ifdef HAVE_USABLE_WCHAR_T |
| 394 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 395 | #else |
| 396 | { |
| 397 | register Py_UNICODE *u; |
| 398 | register int i; |
| 399 | u = PyUnicode_AS_UNICODE(unicode); |
| 400 | for (i = size; i >= 0; i--) |
| 401 | *w++ = *u++; |
| 402 | } |
| 403 | #endif |
| 404 | |
| 405 | return size; |
| 406 | } |
| 407 | |
| 408 | #endif |
| 409 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 410 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 411 | { |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 412 | Py_UNICODE s[1]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 413 | |
| 414 | #ifdef Py_UNICODE_WIDE |
| 415 | if (ordinal < 0 || ordinal > 0x10ffff) { |
| 416 | PyErr_SetString(PyExc_ValueError, |
| 417 | "unichr() arg not in range(0x110000) " |
| 418 | "(wide Python build)"); |
| 419 | return NULL; |
| 420 | } |
| 421 | #else |
| 422 | if (ordinal < 0 || ordinal > 0xffff) { |
| 423 | PyErr_SetString(PyExc_ValueError, |
| 424 | "unichr() arg not in range(0x10000) " |
| 425 | "(narrow Python build)"); |
| 426 | return NULL; |
| 427 | } |
| 428 | #endif |
| 429 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 430 | s[0] = (Py_UNICODE)ordinal; |
| 431 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 434 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 435 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 436 | /* XXX Perhaps we should make this API an alias of |
| 437 | PyObject_Unicode() instead ?! */ |
| 438 | if (PyUnicode_CheckExact(obj)) { |
| 439 | Py_INCREF(obj); |
| 440 | return obj; |
| 441 | } |
| 442 | if (PyUnicode_Check(obj)) { |
| 443 | /* For a Unicode subtype that's not a Unicode object, |
| 444 | return a true Unicode object with the same data. */ |
| 445 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 446 | PyUnicode_GET_SIZE(obj)); |
| 447 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 448 | return PyUnicode_FromEncodedObject(obj, NULL, "strict"); |
| 449 | } |
| 450 | |
| 451 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
| 452 | const char *encoding, |
| 453 | const char *errors) |
| 454 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 455 | const char *s = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 456 | int len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 457 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 458 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 459 | if (obj == NULL) { |
| 460 | PyErr_BadInternalCall(); |
| 461 | return NULL; |
| 462 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 463 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 464 | #if 0 |
| 465 | /* For b/w compatibility we also accept Unicode objects provided |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 466 | that no encodings is given and then redirect to |
| 467 | PyObject_Unicode() which then applies the additional logic for |
| 468 | Unicode subclasses. |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 469 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 470 | NOTE: This API should really only be used for object which |
| 471 | represent *encoded* Unicode ! |
| 472 | |
| 473 | */ |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 474 | if (PyUnicode_Check(obj)) { |
| 475 | if (encoding) { |
| 476 | PyErr_SetString(PyExc_TypeError, |
| 477 | "decoding Unicode is not supported"); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 478 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 479 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 480 | return PyObject_Unicode(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 481 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 482 | #else |
| 483 | if (PyUnicode_Check(obj)) { |
| 484 | PyErr_SetString(PyExc_TypeError, |
| 485 | "decoding Unicode is not supported"); |
| 486 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 487 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 488 | #endif |
| 489 | |
| 490 | /* Coerce object */ |
| 491 | if (PyString_Check(obj)) { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 492 | s = PyString_AS_STRING(obj); |
| 493 | len = PyString_GET_SIZE(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 494 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 495 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
| 496 | /* Overwrite the error message with something more useful in |
| 497 | case of a TypeError. */ |
| 498 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 499 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 500 | "coercing to Unicode: need string or buffer, " |
| 501 | "%.80s found", |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 502 | obj->ob_type->tp_name); |
| 503 | goto onError; |
| 504 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 505 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 506 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 507 | if (len == 0) { |
| 508 | Py_INCREF(unicode_empty); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 509 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 510 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 511 | else |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 512 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 513 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 514 | return v; |
| 515 | |
| 516 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 517 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | PyObject *PyUnicode_Decode(const char *s, |
| 521 | int size, |
| 522 | const char *encoding, |
| 523 | const char *errors) |
| 524 | { |
| 525 | PyObject *buffer = NULL, *unicode; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 526 | |
| 527 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 528 | encoding = PyUnicode_GetDefaultEncoding(); |
| 529 | |
| 530 | /* Shortcuts for common default encodings */ |
| 531 | if (strcmp(encoding, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 532 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 533 | else if (strcmp(encoding, "latin-1") == 0) |
| 534 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 535 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 536 | else if (strcmp(encoding, "mbcs") == 0) |
| 537 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 538 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 539 | else if (strcmp(encoding, "ascii") == 0) |
| 540 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 541 | |
| 542 | /* Decode via the codec registry */ |
| 543 | buffer = PyBuffer_FromMemory((void *)s, size); |
| 544 | if (buffer == NULL) |
| 545 | goto onError; |
| 546 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 547 | if (unicode == NULL) |
| 548 | goto onError; |
| 549 | if (!PyUnicode_Check(unicode)) { |
| 550 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 551 | "decoder did not return an unicode object (type=%.400s)", |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 552 | unicode->ob_type->tp_name); |
| 553 | Py_DECREF(unicode); |
| 554 | goto onError; |
| 555 | } |
| 556 | Py_DECREF(buffer); |
| 557 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 558 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 559 | onError: |
| 560 | Py_XDECREF(buffer); |
| 561 | return NULL; |
| 562 | } |
| 563 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 564 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 565 | const char *encoding, |
| 566 | const char *errors) |
| 567 | { |
| 568 | PyObject *v; |
| 569 | |
| 570 | if (!PyUnicode_Check(unicode)) { |
| 571 | PyErr_BadArgument(); |
| 572 | goto onError; |
| 573 | } |
| 574 | |
| 575 | if (encoding == NULL) |
| 576 | encoding = PyUnicode_GetDefaultEncoding(); |
| 577 | |
| 578 | /* Decode via the codec registry */ |
| 579 | v = PyCodec_Decode(unicode, encoding, errors); |
| 580 | if (v == NULL) |
| 581 | goto onError; |
| 582 | return v; |
| 583 | |
| 584 | onError: |
| 585 | return NULL; |
| 586 | } |
| 587 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 588 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
| 589 | int size, |
| 590 | const char *encoding, |
| 591 | const char *errors) |
| 592 | { |
| 593 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 594 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 595 | unicode = PyUnicode_FromUnicode(s, size); |
| 596 | if (unicode == NULL) |
| 597 | return NULL; |
| 598 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 599 | Py_DECREF(unicode); |
| 600 | return v; |
| 601 | } |
| 602 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 603 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 604 | const char *encoding, |
| 605 | const char *errors) |
| 606 | { |
| 607 | PyObject *v; |
| 608 | |
| 609 | if (!PyUnicode_Check(unicode)) { |
| 610 | PyErr_BadArgument(); |
| 611 | goto onError; |
| 612 | } |
| 613 | |
| 614 | if (encoding == NULL) |
| 615 | encoding = PyUnicode_GetDefaultEncoding(); |
| 616 | |
| 617 | /* Encode via the codec registry */ |
| 618 | v = PyCodec_Encode(unicode, encoding, errors); |
| 619 | if (v == NULL) |
| 620 | goto onError; |
| 621 | return v; |
| 622 | |
| 623 | onError: |
| 624 | return NULL; |
| 625 | } |
| 626 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 627 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 628 | const char *encoding, |
| 629 | const char *errors) |
| 630 | { |
| 631 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 632 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 633 | if (!PyUnicode_Check(unicode)) { |
| 634 | PyErr_BadArgument(); |
| 635 | goto onError; |
| 636 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 637 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 638 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 639 | encoding = PyUnicode_GetDefaultEncoding(); |
| 640 | |
| 641 | /* Shortcuts for common default encodings */ |
| 642 | if (errors == NULL) { |
| 643 | if (strcmp(encoding, "utf-8") == 0) |
Jeremy Hylton | 9cea41c | 2001-05-29 17:13:15 +0000 | [diff] [blame] | 644 | return PyUnicode_AsUTF8String(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 645 | else if (strcmp(encoding, "latin-1") == 0) |
| 646 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 647 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 648 | else if (strcmp(encoding, "mbcs") == 0) |
| 649 | return PyUnicode_AsMBCSString(unicode); |
| 650 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 651 | else if (strcmp(encoding, "ascii") == 0) |
| 652 | return PyUnicode_AsASCIIString(unicode); |
| 653 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 654 | |
| 655 | /* Encode via the codec registry */ |
| 656 | v = PyCodec_Encode(unicode, encoding, errors); |
| 657 | if (v == NULL) |
| 658 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 659 | if (!PyString_Check(v)) { |
| 660 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 661 | "encoder did not return a string object (type=%.400s)", |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 662 | v->ob_type->tp_name); |
| 663 | Py_DECREF(v); |
| 664 | goto onError; |
| 665 | } |
| 666 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 667 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 668 | onError: |
| 669 | return NULL; |
| 670 | } |
| 671 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 672 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 673 | const char *errors) |
| 674 | { |
| 675 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
| 676 | |
| 677 | if (v) |
| 678 | return v; |
| 679 | v = PyUnicode_AsEncodedString(unicode, NULL, errors); |
| 680 | if (v && errors == NULL) |
| 681 | ((PyUnicodeObject *)unicode)->defenc = v; |
| 682 | return v; |
| 683 | } |
| 684 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 685 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 686 | { |
| 687 | if (!PyUnicode_Check(unicode)) { |
| 688 | PyErr_BadArgument(); |
| 689 | goto onError; |
| 690 | } |
| 691 | return PyUnicode_AS_UNICODE(unicode); |
| 692 | |
| 693 | onError: |
| 694 | return NULL; |
| 695 | } |
| 696 | |
| 697 | int PyUnicode_GetSize(PyObject *unicode) |
| 698 | { |
| 699 | if (!PyUnicode_Check(unicode)) { |
| 700 | PyErr_BadArgument(); |
| 701 | goto onError; |
| 702 | } |
| 703 | return PyUnicode_GET_SIZE(unicode); |
| 704 | |
| 705 | onError: |
| 706 | return -1; |
| 707 | } |
| 708 | |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 709 | int PyUnicode_GetWidth(PyObject *unicode) |
| 710 | { |
| 711 | const Py_UNICODE *p, *e; |
| 712 | int width; |
| 713 | |
| 714 | if (!PyUnicode_Check(unicode)) { |
| 715 | PyErr_BadArgument(); |
| 716 | return -1; |
| 717 | } |
| 718 | |
| 719 | p = PyUnicode_AS_UNICODE(unicode); |
| 720 | e = p + PyUnicode_GET_SIZE(unicode); |
| 721 | for (width = 0; p < e; p++) |
| 722 | if (Py_UNICODE_ISWIDE(*p)) |
| 723 | width += 2; |
| 724 | else |
| 725 | width++; |
| 726 | |
| 727 | return width; |
| 728 | } |
| 729 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 730 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 731 | { |
| 732 | return unicode_default_encoding; |
| 733 | } |
| 734 | |
| 735 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 736 | { |
| 737 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 738 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 739 | /* Make sure the encoding is valid. As side effect, this also |
| 740 | loads the encoding into the codec registry cache. */ |
| 741 | v = _PyCodec_Lookup(encoding); |
| 742 | if (v == NULL) |
| 743 | goto onError; |
| 744 | Py_DECREF(v); |
| 745 | strncpy(unicode_default_encoding, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 746 | encoding, |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 747 | sizeof(unicode_default_encoding)); |
| 748 | return 0; |
| 749 | |
| 750 | onError: |
| 751 | return -1; |
| 752 | } |
| 753 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 754 | /* error handling callback helper: |
| 755 | build arguments, call the callback and check the arguments, |
| 756 | if no exception occured, copy the replacement to the output |
| 757 | and adjust various state variables. |
| 758 | return 0 on success, -1 on error |
| 759 | */ |
| 760 | |
| 761 | static |
| 762 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 763 | const char *encoding, const char *reason, |
| 764 | const char *input, int insize, int *startinpos, int *endinpos, PyObject **exceptionObject, const char **inptr, |
| 765 | PyObject **output, int *outpos, Py_UNICODE **outptr) |
| 766 | { |
| 767 | static char *argparse = "O!i;decoding error handler must return (unicode, int) tuple"; |
| 768 | |
| 769 | PyObject *restuple = NULL; |
| 770 | PyObject *repunicode = NULL; |
| 771 | int outsize = PyUnicode_GET_SIZE(*output); |
| 772 | int requiredsize; |
| 773 | int newpos; |
| 774 | Py_UNICODE *repptr; |
| 775 | int repsize; |
| 776 | int res = -1; |
| 777 | |
| 778 | if (*errorHandler == NULL) { |
| 779 | *errorHandler = PyCodec_LookupError(errors); |
| 780 | if (*errorHandler == NULL) |
| 781 | goto onError; |
| 782 | } |
| 783 | |
| 784 | if (*exceptionObject == NULL) { |
| 785 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 786 | encoding, input, insize, *startinpos, *endinpos, reason); |
| 787 | if (*exceptionObject == NULL) |
| 788 | goto onError; |
| 789 | } |
| 790 | else { |
| 791 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 792 | goto onError; |
| 793 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 794 | goto onError; |
| 795 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 796 | goto onError; |
| 797 | } |
| 798 | |
| 799 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 800 | if (restuple == NULL) |
| 801 | goto onError; |
| 802 | if (!PyTuple_Check(restuple)) { |
| 803 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 804 | goto onError; |
| 805 | } |
| 806 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 807 | goto onError; |
| 808 | if (newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 809 | newpos = insize+newpos; |
| 810 | if (newpos<0 || newpos>insize) { |
| 811 | PyErr_Format(PyExc_IndexError, "position %d from error handler out of bounds", newpos); |
| 812 | goto onError; |
| 813 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 814 | |
| 815 | /* need more space? (at least enough for what we |
| 816 | have+the replacement+the rest of the string (starting |
| 817 | at the new input position), so we won't have to check space |
| 818 | when there are no errors in the rest of the string) */ |
| 819 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 820 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 821 | requiredsize = *outpos + repsize + insize-newpos; |
| 822 | if (requiredsize > outsize) { |
| 823 | if (requiredsize<2*outsize) |
| 824 | requiredsize = 2*outsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 825 | if (PyUnicode_Resize(output, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 826 | goto onError; |
| 827 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
| 828 | } |
| 829 | *endinpos = newpos; |
| 830 | *inptr = input + newpos; |
| 831 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 832 | *outptr += repsize; |
| 833 | *outpos += repsize; |
| 834 | /* we made it! */ |
| 835 | res = 0; |
| 836 | |
| 837 | onError: |
| 838 | Py_XDECREF(restuple); |
| 839 | return res; |
| 840 | } |
| 841 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 842 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 843 | |
| 844 | /* see RFC2152 for details */ |
| 845 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 846 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 847 | char utf7_special[128] = { |
| 848 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 849 | encoded: |
| 850 | 0 - not special |
| 851 | 1 - special |
| 852 | 2 - whitespace (optional) |
| 853 | 3 - RFC2152 Set O (optional) */ |
| 854 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 855 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 856 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 857 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 858 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 859 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 860 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 861 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 862 | |
| 863 | }; |
| 864 | |
| 865 | #define SPECIAL(c, encodeO, encodeWS) \ |
| 866 | (((c)>127 || utf7_special[(c)] == 1) || \ |
| 867 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
| 868 | (encodeO && (utf7_special[(c)] == 3))) |
| 869 | |
| 870 | #define B64(n) ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 871 | #define B64CHAR(c) (isalnum(c) || (c) == '+' || (c) == '/') |
| 872 | #define UB64(c) ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
| 873 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4) |
| 874 | |
| 875 | #define ENCODE(out, ch, bits) \ |
| 876 | while (bits >= 6) { \ |
| 877 | *out++ = B64(ch >> (bits-6)); \ |
| 878 | bits -= 6; \ |
| 879 | } |
| 880 | |
| 881 | #define DECODE(out, ch, bits, surrogate) \ |
| 882 | while (bits >= 16) { \ |
| 883 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 884 | bits -= 16; \ |
| 885 | if (surrogate) { \ |
| 886 | /* We have already generated an error for the high surrogate |
| 887 | so let's not bother seeing if the low surrogate is correct or not */\ |
| 888 | surrogate = 0; \ |
| 889 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
| 890 | /* This is a surrogate pair. Unfortunately we can't represent \ |
| 891 | it in a 16-bit character */ \ |
| 892 | surrogate = 1; \ |
| 893 | errmsg = "code pairs are not supported"; \ |
| 894 | goto utf7Error; \ |
| 895 | } else { \ |
| 896 | *out++ = outCh; \ |
| 897 | } \ |
| 898 | } \ |
| 899 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 900 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
| 901 | int size, |
| 902 | const char *errors) |
| 903 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 904 | const char *starts = s; |
| 905 | int startinpos; |
| 906 | int endinpos; |
| 907 | int outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 908 | const char *e; |
| 909 | PyUnicodeObject *unicode; |
| 910 | Py_UNICODE *p; |
| 911 | const char *errmsg = ""; |
| 912 | int inShift = 0; |
| 913 | unsigned int bitsleft = 0; |
| 914 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 915 | int surrogate = 0; |
| 916 | PyObject *errorHandler = NULL; |
| 917 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 918 | |
| 919 | unicode = _PyUnicode_New(size); |
| 920 | if (!unicode) |
| 921 | return NULL; |
| 922 | if (size == 0) |
| 923 | return (PyObject *)unicode; |
| 924 | |
| 925 | p = unicode->str; |
| 926 | e = s + size; |
| 927 | |
| 928 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 929 | Py_UNICODE ch; |
| 930 | restart: |
| 931 | ch = *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 932 | |
| 933 | if (inShift) { |
| 934 | if ((ch == '-') || !B64CHAR(ch)) { |
| 935 | inShift = 0; |
| 936 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 937 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 938 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 939 | if (bitsleft >= 6) { |
| 940 | /* The shift sequence has a partial character in it. If |
| 941 | bitsleft < 6 then we could just classify it as padding |
| 942 | but that is not the case here */ |
| 943 | |
| 944 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 945 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 946 | } |
| 947 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 948 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 949 | here so indicate the potential of a misencoded character. */ |
| 950 | |
| 951 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 952 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 953 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 954 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | if (ch == '-') { |
| 958 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 959 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 960 | inShift = 1; |
| 961 | } |
| 962 | } else if (SPECIAL(ch,0,0)) { |
| 963 | errmsg = "unexpected special character"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 964 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 965 | } else { |
| 966 | *p++ = ch; |
| 967 | } |
| 968 | } else { |
| 969 | charsleft = (charsleft << 6) | UB64(ch); |
| 970 | bitsleft += 6; |
| 971 | s++; |
| 972 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 973 | } |
| 974 | } |
| 975 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 976 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 977 | s++; |
| 978 | if (s < e && *s == '-') { |
| 979 | s++; |
| 980 | *p++ = '+'; |
| 981 | } else |
| 982 | { |
| 983 | inShift = 1; |
| 984 | bitsleft = 0; |
| 985 | } |
| 986 | } |
| 987 | else if (SPECIAL(ch,0,0)) { |
| 988 | errmsg = "unexpected special character"; |
| 989 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 990 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 991 | } |
| 992 | else { |
| 993 | *p++ = ch; |
| 994 | s++; |
| 995 | } |
| 996 | continue; |
| 997 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 998 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 999 | endinpos = s-starts; |
| 1000 | if (unicode_decode_call_errorhandler( |
| 1001 | errors, &errorHandler, |
| 1002 | "utf7", errmsg, |
| 1003 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1004 | (PyObject **)&unicode, &outpos, &p)) |
| 1005 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | if (inShift) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1009 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1010 | endinpos = size; |
| 1011 | if (unicode_decode_call_errorhandler( |
| 1012 | errors, &errorHandler, |
| 1013 | "utf7", "unterminated shift sequence", |
| 1014 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1015 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1016 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1017 | if (s < e) |
| 1018 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1021 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1022 | goto onError; |
| 1023 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1024 | Py_XDECREF(errorHandler); |
| 1025 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1026 | return (PyObject *)unicode; |
| 1027 | |
| 1028 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1029 | Py_XDECREF(errorHandler); |
| 1030 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1031 | Py_DECREF(unicode); |
| 1032 | return NULL; |
| 1033 | } |
| 1034 | |
| 1035 | |
| 1036 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 1037 | int size, |
| 1038 | int encodeSetO, |
| 1039 | int encodeWhiteSpace, |
| 1040 | const char *errors) |
| 1041 | { |
| 1042 | PyObject *v; |
| 1043 | /* It might be possible to tighten this worst case */ |
| 1044 | unsigned int cbAllocated = 5 * size; |
| 1045 | int inShift = 0; |
| 1046 | int i = 0; |
| 1047 | unsigned int bitsleft = 0; |
| 1048 | unsigned long charsleft = 0; |
| 1049 | char * out; |
| 1050 | char * start; |
| 1051 | |
| 1052 | if (size == 0) |
| 1053 | return PyString_FromStringAndSize(NULL, 0); |
| 1054 | |
| 1055 | v = PyString_FromStringAndSize(NULL, cbAllocated); |
| 1056 | if (v == NULL) |
| 1057 | return NULL; |
| 1058 | |
| 1059 | start = out = PyString_AS_STRING(v); |
| 1060 | for (;i < size; ++i) { |
| 1061 | Py_UNICODE ch = s[i]; |
| 1062 | |
| 1063 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1064 | if (ch == '+') { |
| 1065 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1066 | *out++ = '-'; |
| 1067 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1068 | charsleft = ch; |
| 1069 | bitsleft = 16; |
| 1070 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1071 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1072 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1073 | } else { |
| 1074 | *out++ = (char) ch; |
| 1075 | } |
| 1076 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1077 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1078 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1079 | charsleft = 0; |
| 1080 | bitsleft = 0; |
| 1081 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1082 | so no '-' is required, except if the character is itself a '-' */ |
| 1083 | if (B64CHAR(ch) || ch == '-') { |
| 1084 | *out++ = '-'; |
| 1085 | } |
| 1086 | inShift = 0; |
| 1087 | *out++ = (char) ch; |
| 1088 | } else { |
| 1089 | bitsleft += 16; |
| 1090 | charsleft = (charsleft << 16) | ch; |
| 1091 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1092 | |
| 1093 | /* If the next character is special then we dont' need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1094 | 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] | 1095 | or '-' then the shift sequence will be terminated implicitly and we |
| 1096 | don't have to insert a '-'. */ |
| 1097 | |
| 1098 | if (bitsleft == 0) { |
| 1099 | if (i + 1 < size) { |
| 1100 | Py_UNICODE ch2 = s[i+1]; |
| 1101 | |
| 1102 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1103 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1104 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1105 | *out++ = '-'; |
| 1106 | inShift = 0; |
| 1107 | } else { |
| 1108 | inShift = 0; |
| 1109 | } |
| 1110 | |
| 1111 | } |
| 1112 | else { |
| 1113 | *out++ = '-'; |
| 1114 | inShift = 0; |
| 1115 | } |
| 1116 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1117 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1118 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1119 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1120 | if (bitsleft) { |
| 1121 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1122 | *out++ = '-'; |
| 1123 | } |
| 1124 | |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 1125 | _PyString_Resize(&v, out - start); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1126 | return v; |
| 1127 | } |
| 1128 | |
| 1129 | #undef SPECIAL |
| 1130 | #undef B64 |
| 1131 | #undef B64CHAR |
| 1132 | #undef UB64 |
| 1133 | #undef ENCODE |
| 1134 | #undef DECODE |
| 1135 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1136 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1137 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1138 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1139 | char utf8_code_length[256] = { |
| 1140 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1141 | illegal prefix. see RFC 2279 for details */ |
| 1142 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1143 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1144 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1145 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1146 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1147 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1148 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1149 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1150 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1151 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1152 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1153 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1154 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1155 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1156 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1157 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1158 | }; |
| 1159 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1160 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
| 1161 | int size, |
| 1162 | const char *errors) |
| 1163 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1164 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1165 | int n; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1166 | int startinpos; |
| 1167 | int endinpos; |
| 1168 | int outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1169 | const char *e; |
| 1170 | PyUnicodeObject *unicode; |
| 1171 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1172 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1173 | PyObject *errorHandler = NULL; |
| 1174 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1175 | |
| 1176 | /* Note: size will always be longer than the resulting Unicode |
| 1177 | character count */ |
| 1178 | unicode = _PyUnicode_New(size); |
| 1179 | if (!unicode) |
| 1180 | return NULL; |
| 1181 | if (size == 0) |
| 1182 | return (PyObject *)unicode; |
| 1183 | |
| 1184 | /* Unpack UTF-8 encoded data */ |
| 1185 | p = unicode->str; |
| 1186 | e = s + size; |
| 1187 | |
| 1188 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1189 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1190 | |
| 1191 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1192 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1193 | s++; |
| 1194 | continue; |
| 1195 | } |
| 1196 | |
| 1197 | n = utf8_code_length[ch]; |
| 1198 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1199 | if (s + n > e) { |
| 1200 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1201 | startinpos = s-starts; |
| 1202 | endinpos = size; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1203 | goto utf8Error; |
| 1204 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1205 | |
| 1206 | switch (n) { |
| 1207 | |
| 1208 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1209 | errmsg = "unexpected code byte"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1210 | startinpos = s-starts; |
| 1211 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1212 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1213 | |
| 1214 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1215 | errmsg = "internal error"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1216 | startinpos = s-starts; |
| 1217 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1218 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1219 | |
| 1220 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1221 | if ((s[1] & 0xc0) != 0x80) { |
| 1222 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1223 | startinpos = s-starts; |
| 1224 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1225 | goto utf8Error; |
| 1226 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1227 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1228 | if (ch < 0x80) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1229 | startinpos = s-starts; |
| 1230 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1231 | errmsg = "illegal encoding"; |
| 1232 | goto utf8Error; |
| 1233 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1234 | else |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1235 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1236 | break; |
| 1237 | |
| 1238 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1239 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1240 | (s[2] & 0xc0) != 0x80) { |
| 1241 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1242 | startinpos = s-starts; |
| 1243 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1244 | goto utf8Error; |
| 1245 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1246 | 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] | 1247 | if (ch < 0x0800) { |
| 1248 | /* Note: UTF-8 encodings of surrogates are considered |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1249 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1250 | |
| 1251 | XXX For wide builds (UCS-4) we should probably try |
| 1252 | to recombine the surrogates into a single code |
| 1253 | unit. |
| 1254 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1255 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1256 | startinpos = s-starts; |
| 1257 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1258 | goto utf8Error; |
| 1259 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1260 | else |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1261 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1262 | break; |
| 1263 | |
| 1264 | case 4: |
| 1265 | if ((s[1] & 0xc0) != 0x80 || |
| 1266 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1267 | (s[3] & 0xc0) != 0x80) { |
| 1268 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1269 | startinpos = s-starts; |
| 1270 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1271 | goto utf8Error; |
| 1272 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1273 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 1274 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 1275 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1276 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1277 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1278 | || (ch > 0x10ffff)) /* maximum value allowed for |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1279 | UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1280 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1281 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1282 | startinpos = s-starts; |
| 1283 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1284 | goto utf8Error; |
| 1285 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1286 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1287 | *p++ = (Py_UNICODE)ch; |
| 1288 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1289 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1290 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1291 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 1292 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1293 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1294 | /* high surrogate = top 10 bits added to D800 */ |
| 1295 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1296 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1297 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1298 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1299 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1300 | break; |
| 1301 | |
| 1302 | default: |
| 1303 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1304 | errmsg = "unsupported Unicode code range"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1305 | startinpos = s-starts; |
| 1306 | endinpos = startinpos+n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1307 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1308 | } |
| 1309 | s += n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1310 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1311 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1312 | utf8Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1313 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1314 | if (unicode_decode_call_errorhandler( |
| 1315 | errors, &errorHandler, |
| 1316 | "utf8", errmsg, |
| 1317 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1318 | (PyObject **)&unicode, &outpos, &p)) |
| 1319 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1323 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1324 | goto onError; |
| 1325 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1326 | Py_XDECREF(errorHandler); |
| 1327 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1328 | return (PyObject *)unicode; |
| 1329 | |
| 1330 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1331 | Py_XDECREF(errorHandler); |
| 1332 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1333 | Py_DECREF(unicode); |
| 1334 | return NULL; |
| 1335 | } |
| 1336 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1337 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 1338 | and allocate exactly as much space needed at the end. Else allocate the |
| 1339 | maximum possible needed (4 result bytes per Unicode character), and return |
| 1340 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1341 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1342 | PyObject * |
| 1343 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 1344 | int size, |
| 1345 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1346 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1347 | #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] | 1348 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1349 | int i; /* index into s of next input byte */ |
| 1350 | PyObject *v; /* result string object */ |
| 1351 | char *p; /* next free byte in output buffer */ |
| 1352 | int nallocated; /* number of result bytes allocated */ |
| 1353 | int nneeded; /* number of result bytes needed */ |
| 1354 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1355 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1356 | assert(s != NULL); |
| 1357 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1358 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1359 | if (size <= MAX_SHORT_UNICHARS) { |
| 1360 | /* Write into the stack buffer; nallocated can't overflow. |
| 1361 | * At the end, we'll allocate exactly as much heap space as it |
| 1362 | * turns out we need. |
| 1363 | */ |
| 1364 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
| 1365 | v = NULL; /* will allocate after we're done */ |
| 1366 | p = stackbuf; |
| 1367 | } |
| 1368 | else { |
| 1369 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 1370 | nallocated = size * 4; |
| 1371 | if (nallocated / 4 != size) /* overflow! */ |
| 1372 | return PyErr_NoMemory(); |
| 1373 | v = PyString_FromStringAndSize(NULL, nallocated); |
| 1374 | if (v == NULL) |
| 1375 | return NULL; |
| 1376 | p = PyString_AS_STRING(v); |
| 1377 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1378 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1379 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1380 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1381 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1382 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1383 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1384 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1385 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1386 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1387 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 1388 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 1389 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1390 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1391 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1392 | /* Encode UCS2 Unicode ordinals */ |
| 1393 | if (ch < 0x10000) { |
| 1394 | /* Special case: check for high surrogate */ |
| 1395 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 1396 | Py_UCS4 ch2 = s[i]; |
| 1397 | /* Check for low surrogate and combine the two to |
| 1398 | form a UCS4 value */ |
| 1399 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1400 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1401 | i++; |
| 1402 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1403 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1404 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1405 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1406 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1407 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1408 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1409 | continue; |
| 1410 | } |
| 1411 | encodeUCS4: |
| 1412 | /* Encode UCS4 Unicode ordinals */ |
| 1413 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 1414 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 1415 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1416 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1417 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1418 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 1419 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1420 | if (v == NULL) { |
| 1421 | /* This was stack allocated. */ |
| 1422 | nneeded = Py_SAFE_DOWNCAST(p - stackbuf, long, int); |
| 1423 | assert(nneeded <= nallocated); |
| 1424 | v = PyString_FromStringAndSize(stackbuf, nneeded); |
| 1425 | } |
| 1426 | else { |
| 1427 | /* Cut back to size actually needed. */ |
| 1428 | nneeded = Py_SAFE_DOWNCAST(p - PyString_AS_STRING(v), long, int); |
| 1429 | assert(nneeded <= nallocated); |
| 1430 | _PyString_Resize(&v, nneeded); |
| 1431 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1432 | return v; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1433 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1434 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1437 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 1438 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1439 | if (!PyUnicode_Check(unicode)) { |
| 1440 | PyErr_BadArgument(); |
| 1441 | return NULL; |
| 1442 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 1443 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1444 | PyUnicode_GET_SIZE(unicode), |
| 1445 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 1449 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1450 | PyObject * |
| 1451 | PyUnicode_DecodeUTF16(const char *s, |
| 1452 | int size, |
| 1453 | const char *errors, |
| 1454 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1455 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1456 | const char *starts = s; |
| 1457 | int startinpos; |
| 1458 | int endinpos; |
| 1459 | int outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1460 | PyUnicodeObject *unicode; |
| 1461 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1462 | const unsigned char *q, *e; |
| 1463 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1464 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1465 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 1466 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1467 | int ihi = 1, ilo = 0; |
| 1468 | #else |
| 1469 | int ihi = 0, ilo = 1; |
| 1470 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1471 | PyObject *errorHandler = NULL; |
| 1472 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1473 | |
| 1474 | /* Note: size will always be longer than the resulting Unicode |
| 1475 | character count */ |
| 1476 | unicode = _PyUnicode_New(size); |
| 1477 | if (!unicode) |
| 1478 | return NULL; |
| 1479 | if (size == 0) |
| 1480 | return (PyObject *)unicode; |
| 1481 | |
| 1482 | /* Unpack UTF-16 encoded data */ |
| 1483 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1484 | q = (unsigned char *)s; |
| 1485 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1486 | |
| 1487 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1488 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1489 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1490 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 1491 | byte order setting accordingly. In native mode, the leading BOM |
| 1492 | mark is skipped, in all other modes, it is copied to the output |
| 1493 | stream as-is (giving a ZWNBSP character). */ |
| 1494 | if (bo == 0) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1495 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1496 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1497 | if (bom == 0xFEFF) { |
| 1498 | q += 2; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1499 | bo = -1; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1500 | } |
| 1501 | else if (bom == 0xFFFE) { |
| 1502 | q += 2; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1503 | bo = 1; |
| 1504 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1505 | #else |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1506 | if (bom == 0xFEFF) { |
| 1507 | q += 2; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1508 | bo = 1; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1509 | } |
| 1510 | else if (bom == 0xFFFE) { |
| 1511 | q += 2; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1512 | bo = -1; |
| 1513 | } |
| 1514 | #endif |
| 1515 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1516 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1517 | if (bo == -1) { |
| 1518 | /* force LE */ |
| 1519 | ihi = 1; |
| 1520 | ilo = 0; |
| 1521 | } |
| 1522 | else if (bo == 1) { |
| 1523 | /* force BE */ |
| 1524 | ihi = 0; |
| 1525 | ilo = 1; |
| 1526 | } |
| 1527 | |
| 1528 | while (q < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1529 | Py_UNICODE ch; |
| 1530 | /* remaing bytes at the end? (size should be even) */ |
| 1531 | if (e-q<2) { |
| 1532 | errmsg = "truncated data"; |
| 1533 | startinpos = ((const char *)q)-starts; |
| 1534 | endinpos = ((const char *)e)-starts; |
| 1535 | goto utf16Error; |
| 1536 | /* The remaining input chars are ignored if the callback |
| 1537 | chooses to skip the input */ |
| 1538 | } |
| 1539 | ch = (q[ihi] << 8) | q[ilo]; |
| 1540 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1541 | q += 2; |
| 1542 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1543 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 1544 | *p++ = ch; |
| 1545 | continue; |
| 1546 | } |
| 1547 | |
| 1548 | /* UTF-16 code pair: */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1549 | if (q >= e) { |
| 1550 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1551 | startinpos = (((const char *)q)-2)-starts; |
| 1552 | endinpos = ((const char *)e)-starts; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1553 | goto utf16Error; |
| 1554 | } |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 1555 | if (0xD800 <= ch && ch <= 0xDBFF) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1556 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 1557 | q += 2; |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 1558 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1559 | #ifndef Py_UNICODE_WIDE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1560 | *p++ = ch; |
| 1561 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1562 | #else |
| 1563 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1564 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1565 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1566 | } |
| 1567 | else { |
| 1568 | errmsg = "illegal UTF-16 surrogate"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1569 | startinpos = (((const char *)q)-4)-starts; |
| 1570 | endinpos = startinpos+2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1571 | goto utf16Error; |
| 1572 | } |
| 1573 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1574 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1575 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1576 | startinpos = (((const char *)q)-2)-starts; |
| 1577 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1578 | /* Fall through to report the error */ |
| 1579 | |
| 1580 | utf16Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1581 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1582 | if (unicode_decode_call_errorhandler( |
| 1583 | errors, &errorHandler, |
| 1584 | "utf16", errmsg, |
| 1585 | starts, size, &startinpos, &endinpos, &exc, (const char **)&q, |
| 1586 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1587 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | if (byteorder) |
| 1591 | *byteorder = bo; |
| 1592 | |
| 1593 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1594 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1595 | goto onError; |
| 1596 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1597 | Py_XDECREF(errorHandler); |
| 1598 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1599 | return (PyObject *)unicode; |
| 1600 | |
| 1601 | onError: |
| 1602 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1603 | Py_XDECREF(errorHandler); |
| 1604 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1605 | return NULL; |
| 1606 | } |
| 1607 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1608 | PyObject * |
| 1609 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 1610 | int size, |
| 1611 | const char *errors, |
| 1612 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1613 | { |
| 1614 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1615 | unsigned char *p; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1616 | #ifdef Py_UNICODE_WIDE |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1617 | int i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1618 | #else |
| 1619 | const int pairs = 0; |
| 1620 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1621 | /* Offsets from p for storing byte pairs in the right order. */ |
| 1622 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1623 | int ihi = 1, ilo = 0; |
| 1624 | #else |
| 1625 | int ihi = 0, ilo = 1; |
| 1626 | #endif |
| 1627 | |
| 1628 | #define STORECHAR(CH) \ |
| 1629 | do { \ |
| 1630 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 1631 | p[ilo] = (CH) & 0xff; \ |
| 1632 | p += 2; \ |
| 1633 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1634 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1635 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1636 | for (i = pairs = 0; i < size; i++) |
| 1637 | if (s[i] >= 0x10000) |
| 1638 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1639 | #endif |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1640 | v = PyString_FromStringAndSize(NULL, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1641 | 2 * (size + pairs + (byteorder == 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1642 | if (v == NULL) |
| 1643 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1644 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1645 | p = (unsigned char *)PyString_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1646 | if (byteorder == 0) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1647 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 1648 | if (size == 0) |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 1649 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1650 | |
| 1651 | if (byteorder == -1) { |
| 1652 | /* force LE */ |
| 1653 | ihi = 1; |
| 1654 | ilo = 0; |
| 1655 | } |
| 1656 | else if (byteorder == 1) { |
| 1657 | /* force BE */ |
| 1658 | ihi = 0; |
| 1659 | ilo = 1; |
| 1660 | } |
| 1661 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1662 | while (size-- > 0) { |
| 1663 | Py_UNICODE ch = *s++; |
| 1664 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1665 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1666 | if (ch >= 0x10000) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1667 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 1668 | ch = 0xD800 | ((ch-0x10000) >> 10); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1669 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1670 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1671 | STORECHAR(ch); |
| 1672 | if (ch2) |
| 1673 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1674 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1675 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1676 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 1680 | { |
| 1681 | if (!PyUnicode_Check(unicode)) { |
| 1682 | PyErr_BadArgument(); |
| 1683 | return NULL; |
| 1684 | } |
| 1685 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 1686 | PyUnicode_GET_SIZE(unicode), |
| 1687 | NULL, |
| 1688 | 0); |
| 1689 | } |
| 1690 | |
| 1691 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 1692 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1693 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 1694 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1695 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
| 1696 | int size, |
| 1697 | const char *errors) |
| 1698 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1699 | const char *starts = s; |
| 1700 | int startinpos; |
| 1701 | int endinpos; |
| 1702 | int outpos; |
| 1703 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1704 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1705 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1706 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1707 | char* message; |
| 1708 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1709 | PyObject *errorHandler = NULL; |
| 1710 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1711 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1712 | /* Escaped strings will always be longer than the resulting |
| 1713 | 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] | 1714 | length after conversion to the true value. |
| 1715 | (but if the error callback returns a long replacement string |
| 1716 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1717 | v = _PyUnicode_New(size); |
| 1718 | if (v == NULL) |
| 1719 | goto onError; |
| 1720 | if (size == 0) |
| 1721 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1722 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1723 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1724 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1725 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1726 | while (s < end) { |
| 1727 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 1728 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1729 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1730 | |
| 1731 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 1732 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1733 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1734 | continue; |
| 1735 | } |
| 1736 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1737 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1738 | /* \ - Escapes */ |
| 1739 | s++; |
| 1740 | switch (*s++) { |
| 1741 | |
| 1742 | /* \x escapes */ |
| 1743 | case '\n': break; |
| 1744 | case '\\': *p++ = '\\'; break; |
| 1745 | case '\'': *p++ = '\''; break; |
| 1746 | case '\"': *p++ = '\"'; break; |
| 1747 | case 'b': *p++ = '\b'; break; |
| 1748 | case 'f': *p++ = '\014'; break; /* FF */ |
| 1749 | case 't': *p++ = '\t'; break; |
| 1750 | case 'n': *p++ = '\n'; break; |
| 1751 | case 'r': *p++ = '\r'; break; |
| 1752 | case 'v': *p++ = '\013'; break; /* VT */ |
| 1753 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 1754 | |
| 1755 | /* \OOO (octal) escapes */ |
| 1756 | case '0': case '1': case '2': case '3': |
| 1757 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1758 | x = s[-1] - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1759 | if ('0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1760 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1761 | if ('0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1762 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1763 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1764 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1765 | break; |
| 1766 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1767 | /* hex escapes */ |
| 1768 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1769 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1770 | digits = 2; |
| 1771 | message = "truncated \\xXX escape"; |
| 1772 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1773 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1774 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1775 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1776 | digits = 4; |
| 1777 | message = "truncated \\uXXXX escape"; |
| 1778 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1779 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1780 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1781 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1782 | digits = 8; |
| 1783 | message = "truncated \\UXXXXXXXX escape"; |
| 1784 | hexescape: |
| 1785 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1786 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1787 | if (s+digits>end) { |
| 1788 | endinpos = size; |
| 1789 | if (unicode_decode_call_errorhandler( |
| 1790 | errors, &errorHandler, |
| 1791 | "unicodeescape", "end of string in escape sequence", |
| 1792 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1793 | (PyObject **)&v, &outpos, &p)) |
| 1794 | goto onError; |
| 1795 | goto nextByte; |
| 1796 | } |
| 1797 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1798 | c = (unsigned char) s[i]; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1799 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1800 | endinpos = (s+i+1)-starts; |
| 1801 | if (unicode_decode_call_errorhandler( |
| 1802 | errors, &errorHandler, |
| 1803 | "unicodeescape", message, |
| 1804 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1805 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1806 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1807 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1808 | } |
| 1809 | chr = (chr<<4) & ~0xF; |
| 1810 | if (c >= '0' && c <= '9') |
| 1811 | chr += c - '0'; |
| 1812 | else if (c >= 'a' && c <= 'f') |
| 1813 | chr += 10 + c - 'a'; |
| 1814 | else |
| 1815 | chr += 10 + c - 'A'; |
| 1816 | } |
| 1817 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 1818 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1819 | /* _decoding_error will have already written into the |
| 1820 | target buffer. */ |
| 1821 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1822 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1823 | /* when we get here, chr is a 32-bit unicode character */ |
| 1824 | if (chr <= 0xffff) |
| 1825 | /* UCS-2 character */ |
| 1826 | *p++ = (Py_UNICODE) chr; |
| 1827 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1828 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1829 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1830 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1831 | *p++ = chr; |
| 1832 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1833 | chr -= 0x10000L; |
| 1834 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1835 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1836 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1837 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1838 | endinpos = s-starts; |
| 1839 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1840 | if (unicode_decode_call_errorhandler( |
| 1841 | errors, &errorHandler, |
| 1842 | "unicodeescape", "illegal Unicode character", |
| 1843 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1844 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1845 | goto onError; |
| 1846 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1847 | break; |
| 1848 | |
| 1849 | /* \N{name} */ |
| 1850 | case 'N': |
| 1851 | message = "malformed \\N character escape"; |
| 1852 | if (ucnhash_CAPI == NULL) { |
| 1853 | /* load the unicode data module */ |
| 1854 | PyObject *m, *v; |
| 1855 | m = PyImport_ImportModule("unicodedata"); |
| 1856 | if (m == NULL) |
| 1857 | goto ucnhashError; |
| 1858 | v = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
| 1859 | Py_DECREF(m); |
| 1860 | if (v == NULL) |
| 1861 | goto ucnhashError; |
| 1862 | ucnhash_CAPI = PyCObject_AsVoidPtr(v); |
| 1863 | Py_DECREF(v); |
| 1864 | if (ucnhash_CAPI == NULL) |
| 1865 | goto ucnhashError; |
| 1866 | } |
| 1867 | if (*s == '{') { |
| 1868 | const char *start = s+1; |
| 1869 | /* look for the closing brace */ |
| 1870 | while (*s != '}' && s < end) |
| 1871 | s++; |
| 1872 | if (s > start && s < end && *s == '}') { |
| 1873 | /* found a name. look it up in the unicode database */ |
| 1874 | message = "unknown Unicode character name"; |
| 1875 | s++; |
| 1876 | if (ucnhash_CAPI->getcode(start, s-start-1, &chr)) |
| 1877 | goto store; |
| 1878 | } |
| 1879 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1880 | endinpos = s-starts; |
| 1881 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1882 | if (unicode_decode_call_errorhandler( |
| 1883 | errors, &errorHandler, |
| 1884 | "unicodeescape", message, |
| 1885 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1886 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1887 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1888 | break; |
| 1889 | |
| 1890 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1891 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1892 | message = "\\ at end of string"; |
| 1893 | s--; |
| 1894 | endinpos = s-starts; |
| 1895 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1896 | if (unicode_decode_call_errorhandler( |
| 1897 | errors, &errorHandler, |
| 1898 | "unicodeescape", message, |
| 1899 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1900 | (PyObject **)&v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1901 | goto onError; |
| 1902 | } |
| 1903 | else { |
| 1904 | *p++ = '\\'; |
| 1905 | *p++ = (unsigned char)s[-1]; |
| 1906 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1907 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1908 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1909 | nextByte: |
| 1910 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1911 | } |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1912 | if (_PyUnicode_Resize(&v, (int)(p - PyUnicode_AS_UNICODE(v))) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1913 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 1914 | Py_XDECREF(errorHandler); |
| 1915 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1916 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1917 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1918 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1919 | PyErr_SetString( |
| 1920 | PyExc_UnicodeError, |
| 1921 | "\\N escapes not supported (can't load unicodedata module)" |
| 1922 | ); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1923 | Py_XDECREF(errorHandler); |
| 1924 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 1925 | return NULL; |
| 1926 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1927 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1928 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1929 | Py_XDECREF(errorHandler); |
| 1930 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1931 | return NULL; |
| 1932 | } |
| 1933 | |
| 1934 | /* Return a Unicode-Escape string version of the Unicode object. |
| 1935 | |
| 1936 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 1937 | appropriate. |
| 1938 | |
| 1939 | */ |
| 1940 | |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 1941 | static const Py_UNICODE *findchar(const Py_UNICODE *s, |
| 1942 | int size, |
| 1943 | Py_UNICODE ch); |
| 1944 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1945 | static |
| 1946 | PyObject *unicodeescape_string(const Py_UNICODE *s, |
| 1947 | int size, |
| 1948 | int quotes) |
| 1949 | { |
| 1950 | PyObject *repr; |
| 1951 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1952 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 1953 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1954 | |
| 1955 | repr = PyString_FromStringAndSize(NULL, 2 + 6*size + 1); |
| 1956 | if (repr == NULL) |
| 1957 | return NULL; |
| 1958 | |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1959 | p = PyString_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1960 | |
| 1961 | if (quotes) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1962 | *p++ = 'u'; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1963 | *p++ = (findchar(s, size, '\'') && |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1964 | !findchar(s, size, '"')) ? '"' : '\''; |
| 1965 | } |
| 1966 | while (size-- > 0) { |
| 1967 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1968 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1969 | /* Escape quotes */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1970 | if (quotes && |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1971 | (ch == (Py_UNICODE) PyString_AS_STRING(repr)[1] || ch == '\\')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1972 | *p++ = '\\'; |
| 1973 | *p++ = (char) ch; |
Guido van Rossum | ad9744a | 2001-09-21 15:38:17 +0000 | [diff] [blame] | 1974 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1975 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1976 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 1977 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1978 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 1979 | else if (ch >= 0x10000) { |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1980 | int offset = p - PyString_AS_STRING(repr); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1981 | |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1982 | /* Resize the string if necessary */ |
| 1983 | if (offset + 12 > PyString_GET_SIZE(repr)) { |
| 1984 | if (_PyString_Resize(&repr, PyString_GET_SIZE(repr) + 100)) |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 1985 | return NULL; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1986 | p = PyString_AS_STRING(repr) + offset; |
| 1987 | } |
| 1988 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1989 | *p++ = '\\'; |
| 1990 | *p++ = 'U'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1991 | *p++ = hexdigit[(ch >> 28) & 0x0000000F]; |
| 1992 | *p++ = hexdigit[(ch >> 24) & 0x0000000F]; |
| 1993 | *p++ = hexdigit[(ch >> 20) & 0x0000000F]; |
| 1994 | *p++ = hexdigit[(ch >> 16) & 0x0000000F]; |
| 1995 | *p++ = hexdigit[(ch >> 12) & 0x0000000F]; |
| 1996 | *p++ = hexdigit[(ch >> 8) & 0x0000000F]; |
| 1997 | *p++ = hexdigit[(ch >> 4) & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1998 | *p++ = hexdigit[ch & 0x0000000F]; |
| 1999 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2000 | } |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 2001 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2002 | /* Map UTF-16 surrogate pairs to Unicode \UXXXXXXXX escapes */ |
| 2003 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 2004 | Py_UNICODE ch2; |
| 2005 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2006 | |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2007 | ch2 = *s++; |
| 2008 | size--; |
| 2009 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 2010 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 2011 | *p++ = '\\'; |
| 2012 | *p++ = 'U'; |
| 2013 | *p++ = hexdigit[(ucs >> 28) & 0x0000000F]; |
| 2014 | *p++ = hexdigit[(ucs >> 24) & 0x0000000F]; |
| 2015 | *p++ = hexdigit[(ucs >> 20) & 0x0000000F]; |
| 2016 | *p++ = hexdigit[(ucs >> 16) & 0x0000000F]; |
| 2017 | *p++ = hexdigit[(ucs >> 12) & 0x0000000F]; |
| 2018 | *p++ = hexdigit[(ucs >> 8) & 0x0000000F]; |
| 2019 | *p++ = hexdigit[(ucs >> 4) & 0x0000000F]; |
| 2020 | *p++ = hexdigit[ucs & 0x0000000F]; |
| 2021 | continue; |
| 2022 | } |
| 2023 | /* Fall through: isolated surrogates are copied as-is */ |
| 2024 | s--; |
| 2025 | size++; |
| 2026 | } |
| 2027 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2028 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2029 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2030 | *p++ = '\\'; |
| 2031 | *p++ = 'u'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2032 | *p++ = hexdigit[(ch >> 12) & 0x000F]; |
| 2033 | *p++ = hexdigit[(ch >> 8) & 0x000F]; |
| 2034 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2035 | *p++ = hexdigit[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2036 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2037 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2038 | /* Map special whitespace to '\t', \n', '\r' */ |
| 2039 | else if (ch == '\t') { |
| 2040 | *p++ = '\\'; |
| 2041 | *p++ = 't'; |
| 2042 | } |
| 2043 | else if (ch == '\n') { |
| 2044 | *p++ = '\\'; |
| 2045 | *p++ = 'n'; |
| 2046 | } |
| 2047 | else if (ch == '\r') { |
| 2048 | *p++ = '\\'; |
| 2049 | *p++ = 'r'; |
| 2050 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2051 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2052 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 2053 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2054 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2055 | *p++ = 'x'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2056 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2057 | *p++ = hexdigit[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2058 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2059 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2060 | /* Copy everything else as-is */ |
| 2061 | else |
| 2062 | *p++ = (char) ch; |
| 2063 | } |
| 2064 | if (quotes) |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2065 | *p++ = PyString_AS_STRING(repr)[1]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2066 | |
| 2067 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2068 | _PyString_Resize(&repr, p - PyString_AS_STRING(repr)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2069 | return repr; |
| 2070 | } |
| 2071 | |
| 2072 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 2073 | int size) |
| 2074 | { |
| 2075 | return unicodeescape_string(s, size, 0); |
| 2076 | } |
| 2077 | |
| 2078 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 2079 | { |
| 2080 | if (!PyUnicode_Check(unicode)) { |
| 2081 | PyErr_BadArgument(); |
| 2082 | return NULL; |
| 2083 | } |
| 2084 | return PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2085 | PyUnicode_GET_SIZE(unicode)); |
| 2086 | } |
| 2087 | |
| 2088 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 2089 | |
| 2090 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
| 2091 | int size, |
| 2092 | const char *errors) |
| 2093 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2094 | const char *starts = s; |
| 2095 | int startinpos; |
| 2096 | int endinpos; |
| 2097 | int outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2098 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2099 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2100 | const char *end; |
| 2101 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2102 | PyObject *errorHandler = NULL; |
| 2103 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2104 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2105 | /* Escaped strings will always be longer than the resulting |
| 2106 | 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] | 2107 | length after conversion to the true value. (But decoding error |
| 2108 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2109 | v = _PyUnicode_New(size); |
| 2110 | if (v == NULL) |
| 2111 | goto onError; |
| 2112 | if (size == 0) |
| 2113 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2114 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2115 | end = s + size; |
| 2116 | while (s < end) { |
| 2117 | unsigned char c; |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 2118 | Py_UCS4 x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2119 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2120 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2121 | |
| 2122 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2123 | if (*s != '\\') { |
| 2124 | *p++ = (unsigned char)*s++; |
| 2125 | continue; |
| 2126 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2127 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2128 | |
| 2129 | /* \u-escapes are only interpreted iff the number of leading |
| 2130 | backslashes if odd */ |
| 2131 | bs = s; |
| 2132 | for (;s < end;) { |
| 2133 | if (*s != '\\') |
| 2134 | break; |
| 2135 | *p++ = (unsigned char)*s++; |
| 2136 | } |
| 2137 | if (((s - bs) & 1) == 0 || |
| 2138 | s >= end || |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2139 | (*s != 'u' && *s != 'U')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2140 | continue; |
| 2141 | } |
| 2142 | p--; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2143 | count = *s=='u' ? 4 : 8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2144 | s++; |
| 2145 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2146 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2147 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2148 | for (x = 0, i = 0; i < count; ++i, ++s) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2149 | c = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2150 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2151 | endinpos = s-starts; |
| 2152 | if (unicode_decode_call_errorhandler( |
| 2153 | errors, &errorHandler, |
| 2154 | "rawunicodeescape", "truncated \\uXXXX", |
| 2155 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2156 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2157 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2158 | goto nextByte; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2159 | } |
| 2160 | x = (x<<4) & ~0xF; |
| 2161 | if (c >= '0' && c <= '9') |
| 2162 | x += c - '0'; |
| 2163 | else if (c >= 'a' && c <= 'f') |
| 2164 | x += 10 + c - 'a'; |
| 2165 | else |
| 2166 | x += 10 + c - 'A'; |
| 2167 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2168 | #ifndef Py_UNICODE_WIDE |
| 2169 | if (x > 0x10000) { |
| 2170 | if (unicode_decode_call_errorhandler( |
| 2171 | errors, &errorHandler, |
| 2172 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
| 2173 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2174 | (PyObject **)&v, &outpos, &p)) |
| 2175 | goto onError; |
| 2176 | } |
| 2177 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2178 | *p++ = x; |
| 2179 | nextByte: |
| 2180 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2181 | } |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2182 | if (_PyUnicode_Resize(&v, (int)(p - PyUnicode_AS_UNICODE(v))) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2183 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2184 | Py_XDECREF(errorHandler); |
| 2185 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2186 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2187 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2188 | onError: |
| 2189 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2190 | Py_XDECREF(errorHandler); |
| 2191 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2192 | return NULL; |
| 2193 | } |
| 2194 | |
| 2195 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 2196 | int size) |
| 2197 | { |
| 2198 | PyObject *repr; |
| 2199 | char *p; |
| 2200 | char *q; |
| 2201 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2202 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2203 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2204 | #ifdef Py_UNICODE_WIDE |
| 2205 | repr = PyString_FromStringAndSize(NULL, 10 * size); |
| 2206 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2207 | repr = PyString_FromStringAndSize(NULL, 6 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2208 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2209 | if (repr == NULL) |
| 2210 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2211 | if (size == 0) |
| 2212 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2213 | |
| 2214 | p = q = PyString_AS_STRING(repr); |
| 2215 | while (size-- > 0) { |
| 2216 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2217 | #ifdef Py_UNICODE_WIDE |
| 2218 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2219 | if (ch >= 0x10000) { |
| 2220 | *p++ = '\\'; |
| 2221 | *p++ = 'U'; |
| 2222 | *p++ = hexdigit[(ch >> 28) & 0xf]; |
| 2223 | *p++ = hexdigit[(ch >> 24) & 0xf]; |
| 2224 | *p++ = hexdigit[(ch >> 20) & 0xf]; |
| 2225 | *p++ = hexdigit[(ch >> 16) & 0xf]; |
| 2226 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 2227 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 2228 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 2229 | *p++ = hexdigit[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2230 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2231 | else |
| 2232 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2233 | /* Map 16-bit characters to '\uxxxx' */ |
| 2234 | if (ch >= 256) { |
| 2235 | *p++ = '\\'; |
| 2236 | *p++ = 'u'; |
| 2237 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 2238 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 2239 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 2240 | *p++ = hexdigit[ch & 15]; |
| 2241 | } |
| 2242 | /* Copy everything else as-is */ |
| 2243 | else |
| 2244 | *p++ = (char) ch; |
| 2245 | } |
| 2246 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2247 | _PyString_Resize(&repr, p - q); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2248 | return repr; |
| 2249 | } |
| 2250 | |
| 2251 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 2252 | { |
| 2253 | if (!PyUnicode_Check(unicode)) { |
| 2254 | PyErr_BadArgument(); |
| 2255 | return NULL; |
| 2256 | } |
| 2257 | return PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2258 | PyUnicode_GET_SIZE(unicode)); |
| 2259 | } |
| 2260 | |
| 2261 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 2262 | |
| 2263 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
| 2264 | int size, |
| 2265 | const char *errors) |
| 2266 | { |
| 2267 | PyUnicodeObject *v; |
| 2268 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2269 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2270 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2271 | if (size == 1) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 2272 | Py_UNICODE r = *(unsigned char*)s; |
| 2273 | return PyUnicode_FromUnicode(&r, 1); |
| 2274 | } |
| 2275 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2276 | v = _PyUnicode_New(size); |
| 2277 | if (v == NULL) |
| 2278 | goto onError; |
| 2279 | if (size == 0) |
| 2280 | return (PyObject *)v; |
| 2281 | p = PyUnicode_AS_UNICODE(v); |
| 2282 | while (size-- > 0) |
| 2283 | *p++ = (unsigned char)*s++; |
| 2284 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2285 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2286 | onError: |
| 2287 | Py_XDECREF(v); |
| 2288 | return NULL; |
| 2289 | } |
| 2290 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2291 | /* create or adjust a UnicodeEncodeError */ |
| 2292 | static void make_encode_exception(PyObject **exceptionObject, |
| 2293 | const char *encoding, |
| 2294 | const Py_UNICODE *unicode, int size, |
| 2295 | int startpos, int endpos, |
| 2296 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2297 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2298 | if (*exceptionObject == NULL) { |
| 2299 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 2300 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2301 | } |
| 2302 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2303 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 2304 | goto onError; |
| 2305 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 2306 | goto onError; |
| 2307 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 2308 | goto onError; |
| 2309 | return; |
| 2310 | onError: |
| 2311 | Py_DECREF(*exceptionObject); |
| 2312 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2313 | } |
| 2314 | } |
| 2315 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2316 | /* raises a UnicodeEncodeError */ |
| 2317 | static void raise_encode_exception(PyObject **exceptionObject, |
| 2318 | const char *encoding, |
| 2319 | const Py_UNICODE *unicode, int size, |
| 2320 | int startpos, int endpos, |
| 2321 | const char *reason) |
| 2322 | { |
| 2323 | make_encode_exception(exceptionObject, |
| 2324 | encoding, unicode, size, startpos, endpos, reason); |
| 2325 | if (*exceptionObject != NULL) |
| 2326 | PyCodec_StrictErrors(*exceptionObject); |
| 2327 | } |
| 2328 | |
| 2329 | /* error handling callback helper: |
| 2330 | build arguments, call the callback and check the arguments, |
| 2331 | put the result into newpos and return the replacement string, which |
| 2332 | has to be freed by the caller */ |
| 2333 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 2334 | PyObject **errorHandler, |
| 2335 | const char *encoding, const char *reason, |
| 2336 | const Py_UNICODE *unicode, int size, PyObject **exceptionObject, |
| 2337 | int startpos, int endpos, |
| 2338 | int *newpos) |
| 2339 | { |
| 2340 | static char *argparse = "O!i;encoding error handler must return (unicode, int) tuple"; |
| 2341 | |
| 2342 | PyObject *restuple; |
| 2343 | PyObject *resunicode; |
| 2344 | |
| 2345 | if (*errorHandler == NULL) { |
| 2346 | *errorHandler = PyCodec_LookupError(errors); |
| 2347 | if (*errorHandler == NULL) |
| 2348 | return NULL; |
| 2349 | } |
| 2350 | |
| 2351 | make_encode_exception(exceptionObject, |
| 2352 | encoding, unicode, size, startpos, endpos, reason); |
| 2353 | if (*exceptionObject == NULL) |
| 2354 | return NULL; |
| 2355 | |
| 2356 | restuple = PyObject_CallFunctionObjArgs( |
| 2357 | *errorHandler, *exceptionObject, NULL); |
| 2358 | if (restuple == NULL) |
| 2359 | return NULL; |
| 2360 | if (!PyTuple_Check(restuple)) { |
| 2361 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 2362 | Py_DECREF(restuple); |
| 2363 | return NULL; |
| 2364 | } |
| 2365 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 2366 | &resunicode, newpos)) { |
| 2367 | Py_DECREF(restuple); |
| 2368 | return NULL; |
| 2369 | } |
| 2370 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2371 | *newpos = size+*newpos; |
| 2372 | if (*newpos<0 || *newpos>size) { |
| 2373 | PyErr_Format(PyExc_IndexError, "position %d from error handler out of bounds", *newpos); |
| 2374 | Py_DECREF(restuple); |
| 2375 | return NULL; |
| 2376 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2377 | Py_INCREF(resunicode); |
| 2378 | Py_DECREF(restuple); |
| 2379 | return resunicode; |
| 2380 | } |
| 2381 | |
| 2382 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
| 2383 | int size, |
| 2384 | const char *errors, |
| 2385 | int limit) |
| 2386 | { |
| 2387 | /* output object */ |
| 2388 | PyObject *res; |
| 2389 | /* pointers to the beginning and end+1 of input */ |
| 2390 | const Py_UNICODE *startp = p; |
| 2391 | const Py_UNICODE *endp = p + size; |
| 2392 | /* pointer to the beginning of the unencodable characters */ |
| 2393 | /* const Py_UNICODE *badp = NULL; */ |
| 2394 | /* pointer into the output */ |
| 2395 | char *str; |
| 2396 | /* current output position */ |
| 2397 | int respos = 0; |
| 2398 | int ressize; |
| 2399 | char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 2400 | char *reason = (limit == 256) ? "ordinal not in range(256)" : "ordinal not in range(128)"; |
| 2401 | PyObject *errorHandler = NULL; |
| 2402 | PyObject *exc = NULL; |
| 2403 | /* the following variable is used for caching string comparisons |
| 2404 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 2405 | int known_errorHandler = -1; |
| 2406 | |
| 2407 | /* allocate enough for a simple encoding without |
| 2408 | replacements, if we need more, we'll resize */ |
| 2409 | res = PyString_FromStringAndSize(NULL, size); |
| 2410 | if (res == NULL) |
| 2411 | goto onError; |
| 2412 | if (size == 0) |
| 2413 | return res; |
| 2414 | str = PyString_AS_STRING(res); |
| 2415 | ressize = size; |
| 2416 | |
| 2417 | while (p<endp) { |
| 2418 | Py_UNICODE c = *p; |
| 2419 | |
| 2420 | /* can we encode this? */ |
| 2421 | if (c<limit) { |
| 2422 | /* no overflow check, because we know that the space is enough */ |
| 2423 | *str++ = (char)c; |
| 2424 | ++p; |
| 2425 | } |
| 2426 | else { |
| 2427 | int unicodepos = p-startp; |
| 2428 | int requiredsize; |
| 2429 | PyObject *repunicode; |
| 2430 | int repsize; |
| 2431 | int newpos; |
| 2432 | int respos; |
| 2433 | Py_UNICODE *uni2; |
| 2434 | /* startpos for collecting unencodable chars */ |
| 2435 | const Py_UNICODE *collstart = p; |
| 2436 | const Py_UNICODE *collend = p; |
| 2437 | /* find all unecodable characters */ |
| 2438 | while ((collend < endp) && ((*collend)>=limit)) |
| 2439 | ++collend; |
| 2440 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 2441 | if (known_errorHandler==-1) { |
| 2442 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 2443 | known_errorHandler = 1; |
| 2444 | else if (!strcmp(errors, "replace")) |
| 2445 | known_errorHandler = 2; |
| 2446 | else if (!strcmp(errors, "ignore")) |
| 2447 | known_errorHandler = 3; |
| 2448 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 2449 | known_errorHandler = 4; |
| 2450 | else |
| 2451 | known_errorHandler = 0; |
| 2452 | } |
| 2453 | switch (known_errorHandler) { |
| 2454 | case 1: /* strict */ |
| 2455 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 2456 | goto onError; |
| 2457 | case 2: /* replace */ |
| 2458 | while (collstart++<collend) |
| 2459 | *str++ = '?'; /* fall through */ |
| 2460 | case 3: /* ignore */ |
| 2461 | p = collend; |
| 2462 | break; |
| 2463 | case 4: /* xmlcharrefreplace */ |
| 2464 | respos = str-PyString_AS_STRING(res); |
| 2465 | /* determine replacement size (temporarily (mis)uses p) */ |
| 2466 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 2467 | if (*p<10) |
| 2468 | repsize += 2+1+1; |
| 2469 | else if (*p<100) |
| 2470 | repsize += 2+2+1; |
| 2471 | else if (*p<1000) |
| 2472 | repsize += 2+3+1; |
| 2473 | else if (*p<10000) |
| 2474 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 2475 | #ifndef Py_UNICODE_WIDE |
| 2476 | else |
| 2477 | repsize += 2+5+1; |
| 2478 | #else |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2479 | else if (*p<100000) |
| 2480 | repsize += 2+5+1; |
| 2481 | else if (*p<1000000) |
| 2482 | repsize += 2+6+1; |
| 2483 | else |
| 2484 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2485 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2486 | } |
| 2487 | requiredsize = respos+repsize+(endp-collend); |
| 2488 | if (requiredsize > ressize) { |
| 2489 | if (requiredsize<2*ressize) |
| 2490 | requiredsize = 2*ressize; |
| 2491 | if (_PyString_Resize(&res, requiredsize)) |
| 2492 | goto onError; |
| 2493 | str = PyString_AS_STRING(res) + respos; |
| 2494 | ressize = requiredsize; |
| 2495 | } |
| 2496 | /* generate replacement (temporarily (mis)uses p) */ |
| 2497 | for (p = collstart; p < collend; ++p) { |
| 2498 | str += sprintf(str, "&#%d;", (int)*p); |
| 2499 | } |
| 2500 | p = collend; |
| 2501 | break; |
| 2502 | default: |
| 2503 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 2504 | encoding, reason, startp, size, &exc, |
| 2505 | collstart-startp, collend-startp, &newpos); |
| 2506 | if (repunicode == NULL) |
| 2507 | goto onError; |
| 2508 | /* need more space? (at least enough for what we |
| 2509 | have+the replacement+the rest of the string, so |
| 2510 | we won't have to check space for encodable characters) */ |
| 2511 | respos = str-PyString_AS_STRING(res); |
| 2512 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 2513 | requiredsize = respos+repsize+(endp-collend); |
| 2514 | if (requiredsize > ressize) { |
| 2515 | if (requiredsize<2*ressize) |
| 2516 | requiredsize = 2*ressize; |
| 2517 | if (_PyString_Resize(&res, requiredsize)) { |
| 2518 | Py_DECREF(repunicode); |
| 2519 | goto onError; |
| 2520 | } |
| 2521 | str = PyString_AS_STRING(res) + respos; |
| 2522 | ressize = requiredsize; |
| 2523 | } |
| 2524 | /* check if there is anything unencodable in the replacement |
| 2525 | and copy it to the output */ |
| 2526 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 2527 | c = *uni2; |
| 2528 | if (c >= limit) { |
| 2529 | raise_encode_exception(&exc, encoding, startp, size, |
| 2530 | unicodepos, unicodepos+1, reason); |
| 2531 | Py_DECREF(repunicode); |
| 2532 | goto onError; |
| 2533 | } |
| 2534 | *str = (char)c; |
| 2535 | } |
| 2536 | p = startp + newpos; |
| 2537 | Py_DECREF(repunicode); |
| 2538 | } |
| 2539 | } |
| 2540 | } |
| 2541 | /* Resize if we allocated to much */ |
| 2542 | respos = str-PyString_AS_STRING(res); |
| 2543 | if (respos<ressize) |
| 2544 | /* If this falls res will be NULL */ |
| 2545 | _PyString_Resize(&res, respos); |
| 2546 | Py_XDECREF(errorHandler); |
| 2547 | Py_XDECREF(exc); |
| 2548 | return res; |
| 2549 | |
| 2550 | onError: |
| 2551 | Py_XDECREF(res); |
| 2552 | Py_XDECREF(errorHandler); |
| 2553 | Py_XDECREF(exc); |
| 2554 | return NULL; |
| 2555 | } |
| 2556 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2557 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
| 2558 | int size, |
| 2559 | const char *errors) |
| 2560 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2561 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2562 | } |
| 2563 | |
| 2564 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 2565 | { |
| 2566 | if (!PyUnicode_Check(unicode)) { |
| 2567 | PyErr_BadArgument(); |
| 2568 | return NULL; |
| 2569 | } |
| 2570 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 2571 | PyUnicode_GET_SIZE(unicode), |
| 2572 | NULL); |
| 2573 | } |
| 2574 | |
| 2575 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 2576 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2577 | PyObject *PyUnicode_DecodeASCII(const char *s, |
| 2578 | int size, |
| 2579 | const char *errors) |
| 2580 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2581 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2582 | PyUnicodeObject *v; |
| 2583 | Py_UNICODE *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2584 | int startinpos; |
| 2585 | int endinpos; |
| 2586 | int outpos; |
| 2587 | const char *e; |
| 2588 | PyObject *errorHandler = NULL; |
| 2589 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2590 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2591 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 2592 | if (size == 1 && *(unsigned char*)s < 128) { |
| 2593 | Py_UNICODE r = *(unsigned char*)s; |
| 2594 | return PyUnicode_FromUnicode(&r, 1); |
| 2595 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2596 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2597 | v = _PyUnicode_New(size); |
| 2598 | if (v == NULL) |
| 2599 | goto onError; |
| 2600 | if (size == 0) |
| 2601 | return (PyObject *)v; |
| 2602 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2603 | e = s + size; |
| 2604 | while (s < e) { |
| 2605 | register unsigned char c = (unsigned char)*s; |
| 2606 | if (c < 128) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2607 | *p++ = c; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2608 | ++s; |
| 2609 | } |
| 2610 | else { |
| 2611 | startinpos = s-starts; |
| 2612 | endinpos = startinpos + 1; |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 2613 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2614 | if (unicode_decode_call_errorhandler( |
| 2615 | errors, &errorHandler, |
| 2616 | "ascii", "ordinal not in range(128)", |
| 2617 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2618 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2619 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2620 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2621 | } |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2622 | if (p - PyUnicode_AS_UNICODE(v) < PyString_GET_SIZE(v)) |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2623 | if (_PyUnicode_Resize(&v, (int)(p - PyUnicode_AS_UNICODE(v))) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2624 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2625 | Py_XDECREF(errorHandler); |
| 2626 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2627 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2628 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2629 | onError: |
| 2630 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2631 | Py_XDECREF(errorHandler); |
| 2632 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2633 | return NULL; |
| 2634 | } |
| 2635 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2636 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 2637 | int size, |
| 2638 | const char *errors) |
| 2639 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2640 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2641 | } |
| 2642 | |
| 2643 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 2644 | { |
| 2645 | if (!PyUnicode_Check(unicode)) { |
| 2646 | PyErr_BadArgument(); |
| 2647 | return NULL; |
| 2648 | } |
| 2649 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 2650 | PyUnicode_GET_SIZE(unicode), |
| 2651 | NULL); |
| 2652 | } |
| 2653 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2654 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2655 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2656 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2657 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2658 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
| 2659 | int size, |
| 2660 | const char *errors) |
| 2661 | { |
| 2662 | PyUnicodeObject *v; |
| 2663 | Py_UNICODE *p; |
| 2664 | |
| 2665 | /* First get the size of the result */ |
| 2666 | DWORD usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 2667 | if (size > 0 && usize==0) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2668 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2669 | |
| 2670 | v = _PyUnicode_New(usize); |
| 2671 | if (v == NULL) |
| 2672 | return NULL; |
| 2673 | if (usize == 0) |
| 2674 | return (PyObject *)v; |
| 2675 | p = PyUnicode_AS_UNICODE(v); |
| 2676 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 2677 | Py_DECREF(v); |
| 2678 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2679 | } |
| 2680 | |
| 2681 | return (PyObject *)v; |
| 2682 | } |
| 2683 | |
| 2684 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 2685 | int size, |
| 2686 | const char *errors) |
| 2687 | { |
| 2688 | PyObject *repr; |
| 2689 | char *s; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 2690 | DWORD mbcssize; |
| 2691 | |
| 2692 | /* If there are no characters, bail now! */ |
| 2693 | if (size==0) |
| 2694 | return PyString_FromString(""); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2695 | |
| 2696 | /* First get the size of the result */ |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 2697 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2698 | if (mbcssize==0) |
| 2699 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2700 | |
| 2701 | repr = PyString_FromStringAndSize(NULL, mbcssize); |
| 2702 | if (repr == NULL) |
| 2703 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2704 | if (mbcssize == 0) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2705 | return repr; |
| 2706 | |
| 2707 | /* Do the conversion */ |
| 2708 | s = PyString_AS_STRING(repr); |
| 2709 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 2710 | Py_DECREF(repr); |
| 2711 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2712 | } |
| 2713 | return repr; |
| 2714 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2715 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2716 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 2717 | { |
| 2718 | if (!PyUnicode_Check(unicode)) { |
| 2719 | PyErr_BadArgument(); |
| 2720 | return NULL; |
| 2721 | } |
| 2722 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2723 | PyUnicode_GET_SIZE(unicode), |
| 2724 | NULL); |
| 2725 | } |
| 2726 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2727 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2728 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2729 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 2730 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2731 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
| 2732 | int size, |
| 2733 | PyObject *mapping, |
| 2734 | const char *errors) |
| 2735 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2736 | const char *starts = s; |
| 2737 | int startinpos; |
| 2738 | int endinpos; |
| 2739 | int outpos; |
| 2740 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2741 | PyUnicodeObject *v; |
| 2742 | Py_UNICODE *p; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 2743 | int extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2744 | PyObject *errorHandler = NULL; |
| 2745 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2746 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2747 | /* Default to Latin-1 */ |
| 2748 | if (mapping == NULL) |
| 2749 | return PyUnicode_DecodeLatin1(s, size, errors); |
| 2750 | |
| 2751 | v = _PyUnicode_New(size); |
| 2752 | if (v == NULL) |
| 2753 | goto onError; |
| 2754 | if (size == 0) |
| 2755 | return (PyObject *)v; |
| 2756 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2757 | e = s + size; |
| 2758 | while (s < e) { |
| 2759 | unsigned char ch = *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2760 | PyObject *w, *x; |
| 2761 | |
| 2762 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 2763 | w = PyInt_FromLong((long)ch); |
| 2764 | if (w == NULL) |
| 2765 | goto onError; |
| 2766 | x = PyObject_GetItem(mapping, w); |
| 2767 | Py_DECREF(w); |
| 2768 | if (x == NULL) { |
| 2769 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
Marc-André Lemburg | a866df8 | 2001-01-03 21:29:14 +0000 | [diff] [blame] | 2770 | /* No mapping found means: mapping is undefined. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2771 | PyErr_Clear(); |
Marc-André Lemburg | a866df8 | 2001-01-03 21:29:14 +0000 | [diff] [blame] | 2772 | x = Py_None; |
| 2773 | Py_INCREF(x); |
| 2774 | } else |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 2775 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2776 | } |
| 2777 | |
| 2778 | /* Apply mapping */ |
| 2779 | if (PyInt_Check(x)) { |
Marc-André Lemburg | 85cc4d8 | 2000-07-06 19:43:31 +0000 | [diff] [blame] | 2780 | long value = PyInt_AS_LONG(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2781 | if (value < 0 || value > 65535) { |
| 2782 | PyErr_SetString(PyExc_TypeError, |
Marc-André Lemburg | 07ceb67 | 2000-06-10 09:32:51 +0000 | [diff] [blame] | 2783 | "character mapping must be in range(65536)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2784 | Py_DECREF(x); |
| 2785 | goto onError; |
| 2786 | } |
| 2787 | *p++ = (Py_UNICODE)value; |
| 2788 | } |
| 2789 | else if (x == Py_None) { |
| 2790 | /* undefined mapping */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2791 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2792 | startinpos = s-starts; |
| 2793 | endinpos = startinpos+1; |
| 2794 | if (unicode_decode_call_errorhandler( |
| 2795 | errors, &errorHandler, |
| 2796 | "charmap", "character maps to <undefined>", |
| 2797 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2798 | (PyObject **)&v, &outpos, &p)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2799 | Py_DECREF(x); |
| 2800 | goto onError; |
| 2801 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2802 | continue; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2803 | } |
| 2804 | else if (PyUnicode_Check(x)) { |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 2805 | int targetsize = PyUnicode_GET_SIZE(x); |
| 2806 | |
| 2807 | if (targetsize == 1) |
| 2808 | /* 1-1 mapping */ |
| 2809 | *p++ = *PyUnicode_AS_UNICODE(x); |
| 2810 | |
| 2811 | else if (targetsize > 1) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2812 | /* 1-n mapping */ |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 2813 | if (targetsize > extrachars) { |
| 2814 | /* resize first */ |
| 2815 | int oldpos = (int)(p - PyUnicode_AS_UNICODE(v)); |
| 2816 | int needed = (targetsize - extrachars) + \ |
| 2817 | (targetsize << 2); |
| 2818 | extrachars += needed; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2819 | if (_PyUnicode_Resize(&v, |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2820 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 2821 | Py_DECREF(x); |
| 2822 | goto onError; |
| 2823 | } |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 2824 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 2825 | } |
| 2826 | Py_UNICODE_COPY(p, |
| 2827 | PyUnicode_AS_UNICODE(x), |
| 2828 | targetsize); |
| 2829 | p += targetsize; |
| 2830 | extrachars -= targetsize; |
| 2831 | } |
| 2832 | /* 1-0 mapping: skip the character */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2833 | } |
| 2834 | else { |
| 2835 | /* wrong return value */ |
| 2836 | PyErr_SetString(PyExc_TypeError, |
| 2837 | "character mapping must return integer, None or unicode"); |
| 2838 | Py_DECREF(x); |
| 2839 | goto onError; |
| 2840 | } |
| 2841 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2842 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2843 | } |
| 2844 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2845 | if (_PyUnicode_Resize(&v, (int)(p - PyUnicode_AS_UNICODE(v))) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2846 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2847 | Py_XDECREF(errorHandler); |
| 2848 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2849 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2850 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2851 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2852 | Py_XDECREF(errorHandler); |
| 2853 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2854 | Py_XDECREF(v); |
| 2855 | return NULL; |
| 2856 | } |
| 2857 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2858 | /* Lookup the character ch in the mapping. If the character |
| 2859 | can't be found, Py_None is returned (or NULL, if another |
| 2860 | error occured). */ |
| 2861 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2862 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2863 | PyObject *w = PyInt_FromLong((long)c); |
| 2864 | PyObject *x; |
| 2865 | |
| 2866 | if (w == NULL) |
| 2867 | return NULL; |
| 2868 | x = PyObject_GetItem(mapping, w); |
| 2869 | Py_DECREF(w); |
| 2870 | if (x == NULL) { |
| 2871 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 2872 | /* No mapping found means: mapping is undefined. */ |
| 2873 | PyErr_Clear(); |
| 2874 | x = Py_None; |
| 2875 | Py_INCREF(x); |
| 2876 | return x; |
| 2877 | } else |
| 2878 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2879 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 2880 | else if (x == Py_None) |
| 2881 | return x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2882 | else if (PyInt_Check(x)) { |
| 2883 | long value = PyInt_AS_LONG(x); |
| 2884 | if (value < 0 || value > 255) { |
| 2885 | PyErr_SetString(PyExc_TypeError, |
| 2886 | "character mapping must be in range(256)"); |
| 2887 | Py_DECREF(x); |
| 2888 | return NULL; |
| 2889 | } |
| 2890 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2891 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2892 | else if (PyString_Check(x)) |
| 2893 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2894 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2895 | /* wrong return value */ |
| 2896 | PyErr_SetString(PyExc_TypeError, |
| 2897 | "character mapping must return integer, None or str"); |
| 2898 | Py_DECREF(x); |
| 2899 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2900 | } |
| 2901 | } |
| 2902 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2903 | /* lookup the character, put the result in the output string and adjust |
| 2904 | various state variables. Reallocate the output string if not enough |
| 2905 | space is available. Return a new reference to the object that |
| 2906 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 2907 | (in which case no character was written) or NULL, if a |
| 2908 | reallocation error ocurred. The called must decref the result */ |
| 2909 | static |
| 2910 | PyObject *charmapencode_output(Py_UNICODE c, PyObject *mapping, |
| 2911 | PyObject **outobj, int *outpos) |
| 2912 | { |
| 2913 | PyObject *rep = charmapencode_lookup(c, mapping); |
| 2914 | |
| 2915 | if (rep==NULL) |
| 2916 | return NULL; |
| 2917 | else if (rep==Py_None) |
| 2918 | return rep; |
| 2919 | else { |
| 2920 | char *outstart = PyString_AS_STRING(*outobj); |
| 2921 | int outsize = PyString_GET_SIZE(*outobj); |
| 2922 | if (PyInt_Check(rep)) { |
| 2923 | int requiredsize = *outpos+1; |
| 2924 | if (outsize<requiredsize) { |
| 2925 | /* exponentially overallocate to minimize reallocations */ |
| 2926 | if (requiredsize < 2*outsize) |
| 2927 | requiredsize = 2*outsize; |
| 2928 | if (_PyString_Resize(outobj, requiredsize)) { |
| 2929 | Py_DECREF(rep); |
| 2930 | return NULL; |
| 2931 | } |
| 2932 | outstart = PyString_AS_STRING(*outobj); |
| 2933 | } |
| 2934 | outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); |
| 2935 | } |
| 2936 | else { |
| 2937 | const char *repchars = PyString_AS_STRING(rep); |
| 2938 | int repsize = PyString_GET_SIZE(rep); |
| 2939 | int requiredsize = *outpos+repsize; |
| 2940 | if (outsize<requiredsize) { |
| 2941 | /* exponentially overallocate to minimize reallocations */ |
| 2942 | if (requiredsize < 2*outsize) |
| 2943 | requiredsize = 2*outsize; |
| 2944 | if (_PyString_Resize(outobj, requiredsize)) { |
| 2945 | Py_DECREF(rep); |
| 2946 | return NULL; |
| 2947 | } |
| 2948 | outstart = PyString_AS_STRING(*outobj); |
| 2949 | } |
| 2950 | memcpy(outstart + *outpos, repchars, repsize); |
| 2951 | *outpos += repsize; |
| 2952 | } |
| 2953 | } |
| 2954 | return rep; |
| 2955 | } |
| 2956 | |
| 2957 | /* handle an error in PyUnicode_EncodeCharmap |
| 2958 | Return 0 on success, -1 on error */ |
| 2959 | static |
| 2960 | int charmap_encoding_error( |
| 2961 | const Py_UNICODE *p, int size, int *inpos, PyObject *mapping, |
| 2962 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 2963 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2964 | PyObject **res, int *respos) |
| 2965 | { |
| 2966 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 2967 | int repsize; |
| 2968 | int newpos; |
| 2969 | Py_UNICODE *uni2; |
| 2970 | /* startpos for collecting unencodable chars */ |
| 2971 | int collstartpos = *inpos; |
| 2972 | int collendpos = *inpos+1; |
| 2973 | int collpos; |
| 2974 | char *encoding = "charmap"; |
| 2975 | char *reason = "character maps to <undefined>"; |
| 2976 | |
| 2977 | PyObject *x; |
| 2978 | /* find all unencodable characters */ |
| 2979 | while (collendpos < size) { |
| 2980 | x = charmapencode_lookup(p[collendpos], mapping); |
| 2981 | if (x==NULL) |
| 2982 | return -1; |
| 2983 | else if (x!=Py_None) { |
| 2984 | Py_DECREF(x); |
| 2985 | break; |
| 2986 | } |
| 2987 | Py_DECREF(x); |
| 2988 | ++collendpos; |
| 2989 | } |
| 2990 | /* cache callback name lookup |
| 2991 | * (if not done yet, i.e. it's the first error) */ |
| 2992 | if (*known_errorHandler==-1) { |
| 2993 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 2994 | *known_errorHandler = 1; |
| 2995 | else if (!strcmp(errors, "replace")) |
| 2996 | *known_errorHandler = 2; |
| 2997 | else if (!strcmp(errors, "ignore")) |
| 2998 | *known_errorHandler = 3; |
| 2999 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3000 | *known_errorHandler = 4; |
| 3001 | else |
| 3002 | *known_errorHandler = 0; |
| 3003 | } |
| 3004 | switch (*known_errorHandler) { |
| 3005 | case 1: /* strict */ |
| 3006 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3007 | return -1; |
| 3008 | case 2: /* replace */ |
| 3009 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 3010 | x = charmapencode_output('?', mapping, res, respos); |
| 3011 | if (x==NULL) { |
| 3012 | return -1; |
| 3013 | } |
| 3014 | else if (x==Py_None) { |
| 3015 | Py_DECREF(x); |
| 3016 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3017 | return -1; |
| 3018 | } |
| 3019 | Py_DECREF(x); |
| 3020 | } |
| 3021 | /* fall through */ |
| 3022 | case 3: /* ignore */ |
| 3023 | *inpos = collendpos; |
| 3024 | break; |
| 3025 | case 4: /* xmlcharrefreplace */ |
| 3026 | /* generate replacement (temporarily (mis)uses p) */ |
| 3027 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 3028 | char buffer[2+29+1+1]; |
| 3029 | char *cp; |
| 3030 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 3031 | for (cp = buffer; *cp; ++cp) { |
| 3032 | x = charmapencode_output(*cp, mapping, res, respos); |
| 3033 | if (x==NULL) |
| 3034 | return -1; |
| 3035 | else if (x==Py_None) { |
| 3036 | Py_DECREF(x); |
| 3037 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3038 | return -1; |
| 3039 | } |
| 3040 | Py_DECREF(x); |
| 3041 | } |
| 3042 | } |
| 3043 | *inpos = collendpos; |
| 3044 | break; |
| 3045 | default: |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3046 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3047 | encoding, reason, p, size, exceptionObject, |
| 3048 | collstartpos, collendpos, &newpos); |
| 3049 | if (repunicode == NULL) |
| 3050 | return -1; |
| 3051 | /* generate replacement */ |
| 3052 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3053 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 3054 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 3055 | if (x==NULL) { |
| 3056 | Py_DECREF(repunicode); |
| 3057 | return -1; |
| 3058 | } |
| 3059 | else if (x==Py_None) { |
| 3060 | Py_DECREF(repunicode); |
| 3061 | Py_DECREF(x); |
| 3062 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3063 | return -1; |
| 3064 | } |
| 3065 | Py_DECREF(x); |
| 3066 | } |
| 3067 | *inpos = newpos; |
| 3068 | Py_DECREF(repunicode); |
| 3069 | } |
| 3070 | return 0; |
| 3071 | } |
| 3072 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3073 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 3074 | int size, |
| 3075 | PyObject *mapping, |
| 3076 | const char *errors) |
| 3077 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3078 | /* output object */ |
| 3079 | PyObject *res = NULL; |
| 3080 | /* current input position */ |
| 3081 | int inpos = 0; |
| 3082 | /* current output position */ |
| 3083 | int respos = 0; |
| 3084 | PyObject *errorHandler = NULL; |
| 3085 | PyObject *exc = NULL; |
| 3086 | /* the following variable is used for caching string comparisons |
| 3087 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 3088 | * 3=ignore, 4=xmlcharrefreplace */ |
| 3089 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3090 | |
| 3091 | /* Default to Latin-1 */ |
| 3092 | if (mapping == NULL) |
| 3093 | return PyUnicode_EncodeLatin1(p, size, errors); |
| 3094 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3095 | /* allocate enough for a simple encoding without |
| 3096 | replacements, if we need more, we'll resize */ |
| 3097 | res = PyString_FromStringAndSize(NULL, size); |
| 3098 | if (res == NULL) |
| 3099 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3100 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3101 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3102 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3103 | while (inpos<size) { |
| 3104 | /* try to encode it */ |
| 3105 | PyObject *x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 3106 | if (x==NULL) /* error */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3107 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3108 | if (x==Py_None) { /* unencodable character */ |
| 3109 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 3110 | &exc, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3111 | &known_errorHandler, &errorHandler, errors, |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 3112 | &res, &respos)) { |
| 3113 | Py_DECREF(x); |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3114 | goto onError; |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 3115 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3116 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3117 | else |
| 3118 | /* done with this character => adjust input position */ |
| 3119 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3120 | Py_DECREF(x); |
| 3121 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3122 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3123 | /* Resize if we allocated to much */ |
| 3124 | if (respos<PyString_GET_SIZE(res)) { |
| 3125 | if (_PyString_Resize(&res, respos)) |
| 3126 | goto onError; |
| 3127 | } |
| 3128 | Py_XDECREF(exc); |
| 3129 | Py_XDECREF(errorHandler); |
| 3130 | return res; |
| 3131 | |
| 3132 | onError: |
| 3133 | Py_XDECREF(res); |
| 3134 | Py_XDECREF(exc); |
| 3135 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3136 | return NULL; |
| 3137 | } |
| 3138 | |
| 3139 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 3140 | PyObject *mapping) |
| 3141 | { |
| 3142 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 3143 | PyErr_BadArgument(); |
| 3144 | return NULL; |
| 3145 | } |
| 3146 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 3147 | PyUnicode_GET_SIZE(unicode), |
| 3148 | mapping, |
| 3149 | NULL); |
| 3150 | } |
| 3151 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3152 | /* create or adjust a UnicodeTranslateError */ |
| 3153 | static void make_translate_exception(PyObject **exceptionObject, |
| 3154 | const Py_UNICODE *unicode, int size, |
| 3155 | int startpos, int endpos, |
| 3156 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3157 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3158 | if (*exceptionObject == NULL) { |
| 3159 | *exceptionObject = PyUnicodeTranslateError_Create( |
| 3160 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3161 | } |
| 3162 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3163 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 3164 | goto onError; |
| 3165 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 3166 | goto onError; |
| 3167 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 3168 | goto onError; |
| 3169 | return; |
| 3170 | onError: |
| 3171 | Py_DECREF(*exceptionObject); |
| 3172 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3173 | } |
| 3174 | } |
| 3175 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3176 | /* raises a UnicodeTranslateError */ |
| 3177 | static void raise_translate_exception(PyObject **exceptionObject, |
| 3178 | const Py_UNICODE *unicode, int size, |
| 3179 | int startpos, int endpos, |
| 3180 | const char *reason) |
| 3181 | { |
| 3182 | make_translate_exception(exceptionObject, |
| 3183 | unicode, size, startpos, endpos, reason); |
| 3184 | if (*exceptionObject != NULL) |
| 3185 | PyCodec_StrictErrors(*exceptionObject); |
| 3186 | } |
| 3187 | |
| 3188 | /* error handling callback helper: |
| 3189 | build arguments, call the callback and check the arguments, |
| 3190 | put the result into newpos and return the replacement string, which |
| 3191 | has to be freed by the caller */ |
| 3192 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 3193 | PyObject **errorHandler, |
| 3194 | const char *reason, |
| 3195 | const Py_UNICODE *unicode, int size, PyObject **exceptionObject, |
| 3196 | int startpos, int endpos, |
| 3197 | int *newpos) |
| 3198 | { |
| 3199 | static char *argparse = "O!i;translating error handler must return (unicode, int) tuple"; |
| 3200 | |
| 3201 | PyObject *restuple; |
| 3202 | PyObject *resunicode; |
| 3203 | |
| 3204 | if (*errorHandler == NULL) { |
| 3205 | *errorHandler = PyCodec_LookupError(errors); |
| 3206 | if (*errorHandler == NULL) |
| 3207 | return NULL; |
| 3208 | } |
| 3209 | |
| 3210 | make_translate_exception(exceptionObject, |
| 3211 | unicode, size, startpos, endpos, reason); |
| 3212 | if (*exceptionObject == NULL) |
| 3213 | return NULL; |
| 3214 | |
| 3215 | restuple = PyObject_CallFunctionObjArgs( |
| 3216 | *errorHandler, *exceptionObject, NULL); |
| 3217 | if (restuple == NULL) |
| 3218 | return NULL; |
| 3219 | if (!PyTuple_Check(restuple)) { |
| 3220 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 3221 | Py_DECREF(restuple); |
| 3222 | return NULL; |
| 3223 | } |
| 3224 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 3225 | &resunicode, newpos)) { |
| 3226 | Py_DECREF(restuple); |
| 3227 | return NULL; |
| 3228 | } |
| 3229 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3230 | *newpos = size+*newpos; |
| 3231 | if (*newpos<0 || *newpos>size) { |
| 3232 | PyErr_Format(PyExc_IndexError, "position %d from error handler out of bounds", *newpos); |
| 3233 | Py_DECREF(restuple); |
| 3234 | return NULL; |
| 3235 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3236 | Py_INCREF(resunicode); |
| 3237 | Py_DECREF(restuple); |
| 3238 | return resunicode; |
| 3239 | } |
| 3240 | |
| 3241 | /* Lookup the character ch in the mapping and put the result in result, |
| 3242 | which must be decrefed by the caller. |
| 3243 | Return 0 on success, -1 on error */ |
| 3244 | static |
| 3245 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 3246 | { |
| 3247 | PyObject *w = PyInt_FromLong((long)c); |
| 3248 | PyObject *x; |
| 3249 | |
| 3250 | if (w == NULL) |
| 3251 | return -1; |
| 3252 | x = PyObject_GetItem(mapping, w); |
| 3253 | Py_DECREF(w); |
| 3254 | if (x == NULL) { |
| 3255 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3256 | /* No mapping found means: use 1:1 mapping. */ |
| 3257 | PyErr_Clear(); |
| 3258 | *result = NULL; |
| 3259 | return 0; |
| 3260 | } else |
| 3261 | return -1; |
| 3262 | } |
| 3263 | else if (x == Py_None) { |
| 3264 | *result = x; |
| 3265 | return 0; |
| 3266 | } |
| 3267 | else if (PyInt_Check(x)) { |
| 3268 | long value = PyInt_AS_LONG(x); |
| 3269 | long max = PyUnicode_GetMax(); |
| 3270 | if (value < 0 || value > max) { |
| 3271 | PyErr_Format(PyExc_TypeError, |
| 3272 | "character mapping must be in range(0x%lx)", max+1); |
| 3273 | Py_DECREF(x); |
| 3274 | return -1; |
| 3275 | } |
| 3276 | *result = x; |
| 3277 | return 0; |
| 3278 | } |
| 3279 | else if (PyUnicode_Check(x)) { |
| 3280 | *result = x; |
| 3281 | return 0; |
| 3282 | } |
| 3283 | else { |
| 3284 | /* wrong return value */ |
| 3285 | PyErr_SetString(PyExc_TypeError, |
| 3286 | "character mapping must return integer, None or unicode"); |
Walter Dörwald | 150523e | 2003-08-15 16:52:19 +0000 | [diff] [blame] | 3287 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3288 | return -1; |
| 3289 | } |
| 3290 | } |
| 3291 | /* ensure that *outobj is at least requiredsize characters long, |
| 3292 | if not reallocate and adjust various state variables. |
| 3293 | Return 0 on success, -1 on error */ |
| 3294 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3295 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3296 | int requiredsize) |
| 3297 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3298 | int oldsize = PyUnicode_GET_SIZE(*outobj); |
| 3299 | if (requiredsize > oldsize) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3300 | /* remember old output position */ |
| 3301 | int outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
| 3302 | /* exponentially overallocate to minimize reallocations */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3303 | if (requiredsize < 2 * oldsize) |
| 3304 | requiredsize = 2 * oldsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3305 | if (_PyUnicode_Resize(outobj, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3306 | return -1; |
| 3307 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3308 | } |
| 3309 | return 0; |
| 3310 | } |
| 3311 | /* lookup the character, put the result in the output string and adjust |
| 3312 | various state variables. Return a new reference to the object that |
| 3313 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 3314 | undefined (in which case no character was written). |
| 3315 | The called must decref result. |
| 3316 | Return 0 on success, -1 on error. */ |
| 3317 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3318 | int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
| 3319 | int insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
| 3320 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3321 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3322 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3323 | return -1; |
| 3324 | if (*res==NULL) { |
| 3325 | /* not found => default to 1:1 mapping */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3326 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3327 | } |
| 3328 | else if (*res==Py_None) |
| 3329 | ; |
| 3330 | else if (PyInt_Check(*res)) { |
| 3331 | /* no overflow check, because we know that the space is enough */ |
| 3332 | *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); |
| 3333 | } |
| 3334 | else if (PyUnicode_Check(*res)) { |
| 3335 | int repsize = PyUnicode_GET_SIZE(*res); |
| 3336 | if (repsize==1) { |
| 3337 | /* no overflow check, because we know that the space is enough */ |
| 3338 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 3339 | } |
| 3340 | else if (repsize!=0) { |
| 3341 | /* more than one character */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3342 | int requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
Walter Dörwald | cd736e7 | 2004-02-05 17:36:00 +0000 | [diff] [blame] | 3343 | (insize - (curinp-startinp)) + |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3344 | repsize - 1; |
| 3345 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3346 | return -1; |
| 3347 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 3348 | *outp += repsize; |
| 3349 | } |
| 3350 | } |
| 3351 | else |
| 3352 | return -1; |
| 3353 | return 0; |
| 3354 | } |
| 3355 | |
| 3356 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3357 | int size, |
| 3358 | PyObject *mapping, |
| 3359 | const char *errors) |
| 3360 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3361 | /* output object */ |
| 3362 | PyObject *res = NULL; |
| 3363 | /* pointers to the beginning and end+1 of input */ |
| 3364 | const Py_UNICODE *startp = p; |
| 3365 | const Py_UNICODE *endp = p + size; |
| 3366 | /* pointer into the output */ |
| 3367 | Py_UNICODE *str; |
| 3368 | /* current output position */ |
| 3369 | int respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3370 | char *reason = "character maps to <undefined>"; |
| 3371 | PyObject *errorHandler = NULL; |
| 3372 | PyObject *exc = NULL; |
| 3373 | /* the following variable is used for caching string comparisons |
| 3374 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 3375 | * 3=ignore, 4=xmlcharrefreplace */ |
| 3376 | int known_errorHandler = -1; |
| 3377 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3378 | if (mapping == NULL) { |
| 3379 | PyErr_BadArgument(); |
| 3380 | return NULL; |
| 3381 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3382 | |
| 3383 | /* allocate enough for a simple 1:1 translation without |
| 3384 | replacements, if we need more, we'll resize */ |
| 3385 | res = PyUnicode_FromUnicode(NULL, size); |
| 3386 | if (res == NULL) |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3387 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3388 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3389 | return res; |
| 3390 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3391 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3392 | while (p<endp) { |
| 3393 | /* try to encode it */ |
| 3394 | PyObject *x = NULL; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3395 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3396 | Py_XDECREF(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3397 | goto onError; |
| 3398 | } |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 3399 | Py_XDECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3400 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 3401 | ++p; |
| 3402 | else { /* untranslatable character */ |
| 3403 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 3404 | int repsize; |
| 3405 | int newpos; |
| 3406 | Py_UNICODE *uni2; |
| 3407 | /* startpos for collecting untranslatable chars */ |
| 3408 | const Py_UNICODE *collstart = p; |
| 3409 | const Py_UNICODE *collend = p+1; |
| 3410 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3411 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3412 | /* find all untranslatable characters */ |
| 3413 | while (collend < endp) { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3414 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3415 | goto onError; |
| 3416 | Py_XDECREF(x); |
| 3417 | if (x!=Py_None) |
| 3418 | break; |
| 3419 | ++collend; |
| 3420 | } |
| 3421 | /* cache callback name lookup |
| 3422 | * (if not done yet, i.e. it's the first error) */ |
| 3423 | if (known_errorHandler==-1) { |
| 3424 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3425 | known_errorHandler = 1; |
| 3426 | else if (!strcmp(errors, "replace")) |
| 3427 | known_errorHandler = 2; |
| 3428 | else if (!strcmp(errors, "ignore")) |
| 3429 | known_errorHandler = 3; |
| 3430 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3431 | known_errorHandler = 4; |
| 3432 | else |
| 3433 | known_errorHandler = 0; |
| 3434 | } |
| 3435 | switch (known_errorHandler) { |
| 3436 | case 1: /* strict */ |
| 3437 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
| 3438 | goto onError; |
| 3439 | case 2: /* replace */ |
| 3440 | /* No need to check for space, this is a 1:1 replacement */ |
| 3441 | for (coll = collstart; coll<collend; ++coll) |
| 3442 | *str++ = '?'; |
| 3443 | /* fall through */ |
| 3444 | case 3: /* ignore */ |
| 3445 | p = collend; |
| 3446 | break; |
| 3447 | case 4: /* xmlcharrefreplace */ |
| 3448 | /* generate replacement (temporarily (mis)uses p) */ |
| 3449 | for (p = collstart; p < collend; ++p) { |
| 3450 | char buffer[2+29+1+1]; |
| 3451 | char *cp; |
| 3452 | sprintf(buffer, "&#%d;", (int)*p); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3453 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3454 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 3455 | goto onError; |
| 3456 | for (cp = buffer; *cp; ++cp) |
| 3457 | *str++ = *cp; |
| 3458 | } |
| 3459 | p = collend; |
| 3460 | break; |
| 3461 | default: |
| 3462 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 3463 | reason, startp, size, &exc, |
| 3464 | collstart-startp, collend-startp, &newpos); |
| 3465 | if (repunicode == NULL) |
| 3466 | goto onError; |
| 3467 | /* generate replacement */ |
| 3468 | repsize = PyUnicode_GET_SIZE(repunicode); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3469 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3470 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 3471 | Py_DECREF(repunicode); |
| 3472 | goto onError; |
| 3473 | } |
| 3474 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 3475 | *str++ = *uni2; |
| 3476 | p = startp + newpos; |
| 3477 | Py_DECREF(repunicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3478 | } |
| 3479 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3480 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3481 | /* Resize if we allocated to much */ |
| 3482 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3483 | if (respos<PyUnicode_GET_SIZE(res)) { |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3484 | if (_PyUnicode_Resize(&res, respos) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3485 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3486 | } |
| 3487 | Py_XDECREF(exc); |
| 3488 | Py_XDECREF(errorHandler); |
| 3489 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3490 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3491 | onError: |
| 3492 | Py_XDECREF(res); |
| 3493 | Py_XDECREF(exc); |
| 3494 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3495 | return NULL; |
| 3496 | } |
| 3497 | |
| 3498 | PyObject *PyUnicode_Translate(PyObject *str, |
| 3499 | PyObject *mapping, |
| 3500 | const char *errors) |
| 3501 | { |
| 3502 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3503 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3504 | str = PyUnicode_FromObject(str); |
| 3505 | if (str == NULL) |
| 3506 | goto onError; |
| 3507 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 3508 | PyUnicode_GET_SIZE(str), |
| 3509 | mapping, |
| 3510 | errors); |
| 3511 | Py_DECREF(str); |
| 3512 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3513 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3514 | onError: |
| 3515 | Py_XDECREF(str); |
| 3516 | return NULL; |
| 3517 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3518 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3519 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 3520 | |
| 3521 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 3522 | int length, |
| 3523 | char *output, |
| 3524 | const char *errors) |
| 3525 | { |
| 3526 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3527 | PyObject *errorHandler = NULL; |
| 3528 | PyObject *exc = NULL; |
| 3529 | const char *encoding = "decimal"; |
| 3530 | const char *reason = "invalid decimal Unicode string"; |
| 3531 | /* the following variable is used for caching string comparisons |
| 3532 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 3533 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3534 | |
| 3535 | if (output == NULL) { |
| 3536 | PyErr_BadArgument(); |
| 3537 | return -1; |
| 3538 | } |
| 3539 | |
| 3540 | p = s; |
| 3541 | end = s + length; |
| 3542 | while (p < end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3543 | register Py_UNICODE ch = *p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3544 | int decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3545 | PyObject *repunicode; |
| 3546 | int repsize; |
| 3547 | int newpos; |
| 3548 | Py_UNICODE *uni2; |
| 3549 | Py_UNICODE *collstart; |
| 3550 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3551 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3552 | if (Py_UNICODE_ISSPACE(ch)) { |
| 3553 | *output++ = ' '; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3554 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3555 | continue; |
| 3556 | } |
| 3557 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 3558 | if (decimal >= 0) { |
| 3559 | *output++ = '0' + decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3560 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3561 | continue; |
| 3562 | } |
Guido van Rossum | ba47704 | 2000-04-06 18:18:10 +0000 | [diff] [blame] | 3563 | if (0 < ch && ch < 256) { |
Guido van Rossum | 42c29aa | 2000-05-03 23:58:29 +0000 | [diff] [blame] | 3564 | *output++ = (char)ch; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3565 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3566 | continue; |
| 3567 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3568 | /* All other characters are considered unencodable */ |
| 3569 | collstart = p; |
| 3570 | collend = p+1; |
| 3571 | while (collend < end) { |
| 3572 | if ((0 < *collend && *collend < 256) || |
| 3573 | !Py_UNICODE_ISSPACE(*collend) || |
| 3574 | Py_UNICODE_TODECIMAL(*collend)) |
| 3575 | break; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3576 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3577 | /* cache callback name lookup |
| 3578 | * (if not done yet, i.e. it's the first error) */ |
| 3579 | if (known_errorHandler==-1) { |
| 3580 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3581 | known_errorHandler = 1; |
| 3582 | else if (!strcmp(errors, "replace")) |
| 3583 | known_errorHandler = 2; |
| 3584 | else if (!strcmp(errors, "ignore")) |
| 3585 | known_errorHandler = 3; |
| 3586 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3587 | known_errorHandler = 4; |
| 3588 | else |
| 3589 | known_errorHandler = 0; |
| 3590 | } |
| 3591 | switch (known_errorHandler) { |
| 3592 | case 1: /* strict */ |
| 3593 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 3594 | goto onError; |
| 3595 | case 2: /* replace */ |
| 3596 | for (p = collstart; p < collend; ++p) |
| 3597 | *output++ = '?'; |
| 3598 | /* fall through */ |
| 3599 | case 3: /* ignore */ |
| 3600 | p = collend; |
| 3601 | break; |
| 3602 | case 4: /* xmlcharrefreplace */ |
| 3603 | /* generate replacement (temporarily (mis)uses p) */ |
| 3604 | for (p = collstart; p < collend; ++p) |
| 3605 | output += sprintf(output, "&#%d;", (int)*p); |
| 3606 | p = collend; |
| 3607 | break; |
| 3608 | default: |
| 3609 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 3610 | encoding, reason, s, length, &exc, |
| 3611 | collstart-s, collend-s, &newpos); |
| 3612 | if (repunicode == NULL) |
| 3613 | goto onError; |
| 3614 | /* generate replacement */ |
| 3615 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3616 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 3617 | Py_UNICODE ch = *uni2; |
| 3618 | if (Py_UNICODE_ISSPACE(ch)) |
| 3619 | *output++ = ' '; |
| 3620 | else { |
| 3621 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 3622 | if (decimal >= 0) |
| 3623 | *output++ = '0' + decimal; |
| 3624 | else if (0 < ch && ch < 256) |
| 3625 | *output++ = (char)ch; |
| 3626 | else { |
| 3627 | Py_DECREF(repunicode); |
| 3628 | raise_encode_exception(&exc, encoding, |
| 3629 | s, length, collstart-s, collend-s, reason); |
| 3630 | goto onError; |
| 3631 | } |
| 3632 | } |
| 3633 | } |
| 3634 | p = s + newpos; |
| 3635 | Py_DECREF(repunicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3636 | } |
| 3637 | } |
| 3638 | /* 0-terminate the output string */ |
| 3639 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3640 | Py_XDECREF(exc); |
| 3641 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3642 | return 0; |
| 3643 | |
| 3644 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3645 | Py_XDECREF(exc); |
| 3646 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3647 | return -1; |
| 3648 | } |
| 3649 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3650 | /* --- Helpers ------------------------------------------------------------ */ |
| 3651 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3652 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3653 | int count(PyUnicodeObject *self, |
| 3654 | int start, |
| 3655 | int end, |
| 3656 | PyUnicodeObject *substring) |
| 3657 | { |
| 3658 | int count = 0; |
| 3659 | |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3660 | if (start < 0) |
| 3661 | start += self->length; |
| 3662 | if (start < 0) |
| 3663 | start = 0; |
| 3664 | if (end > self->length) |
| 3665 | end = self->length; |
| 3666 | if (end < 0) |
| 3667 | end += self->length; |
| 3668 | if (end < 0) |
| 3669 | end = 0; |
| 3670 | |
Marc-André Lemburg | 49ef6dc | 2000-06-18 22:25:22 +0000 | [diff] [blame] | 3671 | if (substring->length == 0) |
| 3672 | return (end - start + 1); |
| 3673 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3674 | end -= substring->length; |
| 3675 | |
| 3676 | while (start <= end) |
| 3677 | if (Py_UNICODE_MATCH(self, start, substring)) { |
| 3678 | count++; |
| 3679 | start += substring->length; |
| 3680 | } else |
| 3681 | start++; |
| 3682 | |
| 3683 | return count; |
| 3684 | } |
| 3685 | |
| 3686 | int PyUnicode_Count(PyObject *str, |
| 3687 | PyObject *substr, |
| 3688 | int start, |
| 3689 | int end) |
| 3690 | { |
| 3691 | int result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3692 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3693 | str = PyUnicode_FromObject(str); |
| 3694 | if (str == NULL) |
| 3695 | return -1; |
| 3696 | substr = PyUnicode_FromObject(substr); |
| 3697 | if (substr == NULL) { |
Marc-André Lemburg | 49ef6dc | 2000-06-18 22:25:22 +0000 | [diff] [blame] | 3698 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3699 | return -1; |
| 3700 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3701 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3702 | result = count((PyUnicodeObject *)str, |
| 3703 | start, end, |
| 3704 | (PyUnicodeObject *)substr); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3705 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3706 | Py_DECREF(str); |
| 3707 | Py_DECREF(substr); |
| 3708 | return result; |
| 3709 | } |
| 3710 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3711 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3712 | int findstring(PyUnicodeObject *self, |
| 3713 | PyUnicodeObject *substring, |
| 3714 | int start, |
| 3715 | int end, |
| 3716 | int direction) |
| 3717 | { |
| 3718 | if (start < 0) |
| 3719 | start += self->length; |
| 3720 | if (start < 0) |
| 3721 | start = 0; |
| 3722 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3723 | if (end > self->length) |
| 3724 | end = self->length; |
| 3725 | if (end < 0) |
| 3726 | end += self->length; |
| 3727 | if (end < 0) |
| 3728 | end = 0; |
| 3729 | |
Guido van Rossum | 76afbd9 | 2002-08-20 17:29:29 +0000 | [diff] [blame] | 3730 | if (substring->length == 0) |
| 3731 | return (direction > 0) ? start : end; |
| 3732 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3733 | end -= substring->length; |
| 3734 | |
| 3735 | if (direction < 0) { |
| 3736 | for (; end >= start; end--) |
| 3737 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 3738 | return end; |
| 3739 | } else { |
| 3740 | for (; start <= end; start++) |
| 3741 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 3742 | return start; |
| 3743 | } |
| 3744 | |
| 3745 | return -1; |
| 3746 | } |
| 3747 | |
| 3748 | int PyUnicode_Find(PyObject *str, |
| 3749 | PyObject *substr, |
| 3750 | int start, |
| 3751 | int end, |
| 3752 | int direction) |
| 3753 | { |
| 3754 | int result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3755 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3756 | str = PyUnicode_FromObject(str); |
| 3757 | if (str == NULL) |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 3758 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3759 | substr = PyUnicode_FromObject(substr); |
| 3760 | if (substr == NULL) { |
Marc-André Lemburg | 4164439 | 2002-05-29 13:46:29 +0000 | [diff] [blame] | 3761 | Py_DECREF(str); |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 3762 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3763 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3764 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3765 | result = findstring((PyUnicodeObject *)str, |
| 3766 | (PyUnicodeObject *)substr, |
| 3767 | start, end, direction); |
| 3768 | Py_DECREF(str); |
| 3769 | Py_DECREF(substr); |
| 3770 | return result; |
| 3771 | } |
| 3772 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3773 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3774 | int tailmatch(PyUnicodeObject *self, |
| 3775 | PyUnicodeObject *substring, |
| 3776 | int start, |
| 3777 | int end, |
| 3778 | int direction) |
| 3779 | { |
| 3780 | if (start < 0) |
| 3781 | start += self->length; |
| 3782 | if (start < 0) |
| 3783 | start = 0; |
| 3784 | |
| 3785 | if (substring->length == 0) |
| 3786 | return 1; |
| 3787 | |
| 3788 | if (end > self->length) |
| 3789 | end = self->length; |
| 3790 | if (end < 0) |
| 3791 | end += self->length; |
| 3792 | if (end < 0) |
| 3793 | end = 0; |
| 3794 | |
| 3795 | end -= substring->length; |
| 3796 | if (end < start) |
| 3797 | return 0; |
| 3798 | |
| 3799 | if (direction > 0) { |
| 3800 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 3801 | return 1; |
| 3802 | } else { |
| 3803 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 3804 | return 1; |
| 3805 | } |
| 3806 | |
| 3807 | return 0; |
| 3808 | } |
| 3809 | |
| 3810 | int PyUnicode_Tailmatch(PyObject *str, |
| 3811 | PyObject *substr, |
| 3812 | int start, |
| 3813 | int end, |
| 3814 | int direction) |
| 3815 | { |
| 3816 | int result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3817 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3818 | str = PyUnicode_FromObject(str); |
| 3819 | if (str == NULL) |
| 3820 | return -1; |
| 3821 | substr = PyUnicode_FromObject(substr); |
| 3822 | if (substr == NULL) { |
| 3823 | Py_DECREF(substr); |
| 3824 | return -1; |
| 3825 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3826 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3827 | result = tailmatch((PyUnicodeObject *)str, |
| 3828 | (PyUnicodeObject *)substr, |
| 3829 | start, end, direction); |
| 3830 | Py_DECREF(str); |
| 3831 | Py_DECREF(substr); |
| 3832 | return result; |
| 3833 | } |
| 3834 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3835 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3836 | const Py_UNICODE *findchar(const Py_UNICODE *s, |
| 3837 | int size, |
| 3838 | Py_UNICODE ch) |
| 3839 | { |
| 3840 | /* like wcschr, but doesn't stop at NULL characters */ |
| 3841 | |
| 3842 | while (size-- > 0) { |
| 3843 | if (*s == ch) |
| 3844 | return s; |
| 3845 | s++; |
| 3846 | } |
| 3847 | |
| 3848 | return NULL; |
| 3849 | } |
| 3850 | |
| 3851 | /* Apply fixfct filter to the Unicode object self and return a |
| 3852 | reference to the modified object */ |
| 3853 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3854 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3855 | PyObject *fixup(PyUnicodeObject *self, |
| 3856 | int (*fixfct)(PyUnicodeObject *s)) |
| 3857 | { |
| 3858 | |
| 3859 | PyUnicodeObject *u; |
| 3860 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3861 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3862 | if (u == NULL) |
| 3863 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3864 | |
| 3865 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 3866 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 3867 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3868 | /* fixfct should return TRUE if it modified the buffer. If |
| 3869 | FALSE, return a reference to the original buffer instead |
| 3870 | (to save space, not time) */ |
| 3871 | Py_INCREF(self); |
| 3872 | Py_DECREF(u); |
| 3873 | return (PyObject*) self; |
| 3874 | } |
| 3875 | return (PyObject*) u; |
| 3876 | } |
| 3877 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3878 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3879 | int fixupper(PyUnicodeObject *self) |
| 3880 | { |
| 3881 | int len = self->length; |
| 3882 | Py_UNICODE *s = self->str; |
| 3883 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3884 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3885 | while (len-- > 0) { |
| 3886 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3887 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3888 | ch = Py_UNICODE_TOUPPER(*s); |
| 3889 | if (ch != *s) { |
| 3890 | status = 1; |
| 3891 | *s = ch; |
| 3892 | } |
| 3893 | s++; |
| 3894 | } |
| 3895 | |
| 3896 | return status; |
| 3897 | } |
| 3898 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3899 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3900 | int fixlower(PyUnicodeObject *self) |
| 3901 | { |
| 3902 | int len = self->length; |
| 3903 | Py_UNICODE *s = self->str; |
| 3904 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3905 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3906 | while (len-- > 0) { |
| 3907 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3908 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3909 | ch = Py_UNICODE_TOLOWER(*s); |
| 3910 | if (ch != *s) { |
| 3911 | status = 1; |
| 3912 | *s = ch; |
| 3913 | } |
| 3914 | s++; |
| 3915 | } |
| 3916 | |
| 3917 | return status; |
| 3918 | } |
| 3919 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3920 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3921 | int fixswapcase(PyUnicodeObject *self) |
| 3922 | { |
| 3923 | int len = self->length; |
| 3924 | Py_UNICODE *s = self->str; |
| 3925 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3926 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3927 | while (len-- > 0) { |
| 3928 | if (Py_UNICODE_ISUPPER(*s)) { |
| 3929 | *s = Py_UNICODE_TOLOWER(*s); |
| 3930 | status = 1; |
| 3931 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 3932 | *s = Py_UNICODE_TOUPPER(*s); |
| 3933 | status = 1; |
| 3934 | } |
| 3935 | s++; |
| 3936 | } |
| 3937 | |
| 3938 | return status; |
| 3939 | } |
| 3940 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3941 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3942 | int fixcapitalize(PyUnicodeObject *self) |
| 3943 | { |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 3944 | int len = self->length; |
| 3945 | Py_UNICODE *s = self->str; |
| 3946 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3947 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 3948 | if (len == 0) |
| 3949 | return 0; |
| 3950 | if (Py_UNICODE_ISLOWER(*s)) { |
| 3951 | *s = Py_UNICODE_TOUPPER(*s); |
| 3952 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3953 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 3954 | s++; |
| 3955 | while (--len > 0) { |
| 3956 | if (Py_UNICODE_ISUPPER(*s)) { |
| 3957 | *s = Py_UNICODE_TOLOWER(*s); |
| 3958 | status = 1; |
| 3959 | } |
| 3960 | s++; |
| 3961 | } |
| 3962 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3963 | } |
| 3964 | |
| 3965 | static |
| 3966 | int fixtitle(PyUnicodeObject *self) |
| 3967 | { |
| 3968 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 3969 | register Py_UNICODE *e; |
| 3970 | int previous_is_cased; |
| 3971 | |
| 3972 | /* Shortcut for single character strings */ |
| 3973 | if (PyUnicode_GET_SIZE(self) == 1) { |
| 3974 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 3975 | if (*p != ch) { |
| 3976 | *p = ch; |
| 3977 | return 1; |
| 3978 | } |
| 3979 | else |
| 3980 | return 0; |
| 3981 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3982 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3983 | e = p + PyUnicode_GET_SIZE(self); |
| 3984 | previous_is_cased = 0; |
| 3985 | for (; p < e; p++) { |
| 3986 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3987 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3988 | if (previous_is_cased) |
| 3989 | *p = Py_UNICODE_TOLOWER(ch); |
| 3990 | else |
| 3991 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3992 | |
| 3993 | if (Py_UNICODE_ISLOWER(ch) || |
| 3994 | Py_UNICODE_ISUPPER(ch) || |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3995 | Py_UNICODE_ISTITLE(ch)) |
| 3996 | previous_is_cased = 1; |
| 3997 | else |
| 3998 | previous_is_cased = 0; |
| 3999 | } |
| 4000 | return 1; |
| 4001 | } |
| 4002 | |
| 4003 | PyObject *PyUnicode_Join(PyObject *separator, |
| 4004 | PyObject *seq) |
| 4005 | { |
| 4006 | Py_UNICODE *sep; |
| 4007 | int seplen; |
| 4008 | PyUnicodeObject *res = NULL; |
| 4009 | int reslen = 0; |
| 4010 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4011 | int sz = 100; |
| 4012 | int i; |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 4013 | PyObject *it; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4014 | |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 4015 | it = PyObject_GetIter(seq); |
| 4016 | if (it == NULL) |
| 4017 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4018 | |
| 4019 | if (separator == NULL) { |
| 4020 | Py_UNICODE blank = ' '; |
| 4021 | sep = ␣ |
| 4022 | seplen = 1; |
| 4023 | } |
| 4024 | else { |
| 4025 | separator = PyUnicode_FromObject(separator); |
| 4026 | if (separator == NULL) |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 4027 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4028 | sep = PyUnicode_AS_UNICODE(separator); |
| 4029 | seplen = PyUnicode_GET_SIZE(separator); |
| 4030 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4031 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4032 | res = _PyUnicode_New(sz); |
| 4033 | if (res == NULL) |
| 4034 | goto onError; |
| 4035 | p = PyUnicode_AS_UNICODE(res); |
| 4036 | reslen = 0; |
| 4037 | |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 4038 | for (i = 0; ; ++i) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4039 | int itemlen; |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 4040 | PyObject *item = PyIter_Next(it); |
| 4041 | if (item == NULL) { |
| 4042 | if (PyErr_Occurred()) |
| 4043 | goto onError; |
| 4044 | break; |
| 4045 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4046 | if (!PyUnicode_Check(item)) { |
| 4047 | PyObject *v; |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4048 | if (!PyString_Check(item)) { |
| 4049 | PyErr_Format(PyExc_TypeError, |
| 4050 | "sequence item %i: expected string or Unicode," |
| 4051 | " %.80s found", |
| 4052 | i, item->ob_type->tp_name); |
| 4053 | Py_DECREF(item); |
| 4054 | goto onError; |
| 4055 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4056 | v = PyUnicode_FromObject(item); |
| 4057 | Py_DECREF(item); |
| 4058 | item = v; |
| 4059 | if (item == NULL) |
| 4060 | goto onError; |
| 4061 | } |
| 4062 | itemlen = PyUnicode_GET_SIZE(item); |
| 4063 | while (reslen + itemlen + seplen >= sz) { |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4064 | if (_PyUnicode_Resize(&res, sz*2) < 0) { |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4065 | Py_DECREF(item); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4066 | goto onError; |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4067 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4068 | sz *= 2; |
| 4069 | p = PyUnicode_AS_UNICODE(res) + reslen; |
| 4070 | } |
| 4071 | if (i > 0) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4072 | Py_UNICODE_COPY(p, sep, seplen); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4073 | p += seplen; |
| 4074 | reslen += seplen; |
| 4075 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4076 | Py_UNICODE_COPY(p, PyUnicode_AS_UNICODE(item), itemlen); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4077 | p += itemlen; |
| 4078 | reslen += itemlen; |
| 4079 | Py_DECREF(item); |
| 4080 | } |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4081 | if (_PyUnicode_Resize(&res, reslen) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4082 | goto onError; |
| 4083 | |
| 4084 | Py_XDECREF(separator); |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 4085 | Py_DECREF(it); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4086 | return (PyObject *)res; |
| 4087 | |
| 4088 | onError: |
| 4089 | Py_XDECREF(separator); |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 4090 | Py_XDECREF(res); |
| 4091 | Py_DECREF(it); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4092 | return NULL; |
| 4093 | } |
| 4094 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4095 | static |
| 4096 | PyUnicodeObject *pad(PyUnicodeObject *self, |
| 4097 | int left, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4098 | int right, |
| 4099 | Py_UNICODE fill) |
| 4100 | { |
| 4101 | PyUnicodeObject *u; |
| 4102 | |
| 4103 | if (left < 0) |
| 4104 | left = 0; |
| 4105 | if (right < 0) |
| 4106 | right = 0; |
| 4107 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4108 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4109 | Py_INCREF(self); |
| 4110 | return self; |
| 4111 | } |
| 4112 | |
| 4113 | u = _PyUnicode_New(left + self->length + right); |
| 4114 | if (u) { |
| 4115 | if (left) |
| 4116 | Py_UNICODE_FILL(u->str, fill, left); |
| 4117 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 4118 | if (right) |
| 4119 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 4120 | } |
| 4121 | |
| 4122 | return u; |
| 4123 | } |
| 4124 | |
| 4125 | #define SPLIT_APPEND(data, left, right) \ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4126 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4127 | if (!str) \ |
| 4128 | goto onError; \ |
| 4129 | if (PyList_Append(list, str)) { \ |
| 4130 | Py_DECREF(str); \ |
| 4131 | goto onError; \ |
| 4132 | } \ |
| 4133 | else \ |
| 4134 | Py_DECREF(str); |
| 4135 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4136 | #define SPLIT_INSERT(data, left, right) \ |
| 4137 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
| 4138 | if (!str) \ |
| 4139 | goto onError; \ |
| 4140 | if (PyList_Insert(list, 0, str)) { \ |
| 4141 | Py_DECREF(str); \ |
| 4142 | goto onError; \ |
| 4143 | } \ |
| 4144 | else \ |
| 4145 | Py_DECREF(str); |
| 4146 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4147 | static |
| 4148 | PyObject *split_whitespace(PyUnicodeObject *self, |
| 4149 | PyObject *list, |
| 4150 | int maxcount) |
| 4151 | { |
| 4152 | register int i; |
| 4153 | register int j; |
| 4154 | int len = self->length; |
| 4155 | PyObject *str; |
| 4156 | |
| 4157 | for (i = j = 0; i < len; ) { |
| 4158 | /* find a token */ |
| 4159 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 4160 | i++; |
| 4161 | j = i; |
| 4162 | while (i < len && !Py_UNICODE_ISSPACE(self->str[i])) |
| 4163 | i++; |
| 4164 | if (j < i) { |
| 4165 | if (maxcount-- <= 0) |
| 4166 | break; |
| 4167 | SPLIT_APPEND(self->str, j, i); |
| 4168 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 4169 | i++; |
| 4170 | j = i; |
| 4171 | } |
| 4172 | } |
| 4173 | if (j < len) { |
| 4174 | SPLIT_APPEND(self->str, j, len); |
| 4175 | } |
| 4176 | return list; |
| 4177 | |
| 4178 | onError: |
| 4179 | Py_DECREF(list); |
| 4180 | return NULL; |
| 4181 | } |
| 4182 | |
| 4183 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4184 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4185 | { |
| 4186 | register int i; |
| 4187 | register int j; |
| 4188 | int len; |
| 4189 | PyObject *list; |
| 4190 | PyObject *str; |
| 4191 | Py_UNICODE *data; |
| 4192 | |
| 4193 | string = PyUnicode_FromObject(string); |
| 4194 | if (string == NULL) |
| 4195 | return NULL; |
| 4196 | data = PyUnicode_AS_UNICODE(string); |
| 4197 | len = PyUnicode_GET_SIZE(string); |
| 4198 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4199 | list = PyList_New(0); |
| 4200 | if (!list) |
| 4201 | goto onError; |
| 4202 | |
| 4203 | for (i = j = 0; i < len; ) { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4204 | int eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4205 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4206 | /* Find a line and append it */ |
| 4207 | while (i < len && !Py_UNICODE_ISLINEBREAK(data[i])) |
| 4208 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4209 | |
| 4210 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4211 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4212 | if (i < len) { |
| 4213 | if (data[i] == '\r' && i + 1 < len && |
| 4214 | data[i+1] == '\n') |
| 4215 | i += 2; |
| 4216 | else |
| 4217 | i++; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4218 | if (keepends) |
| 4219 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4220 | } |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4221 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4222 | j = i; |
| 4223 | } |
| 4224 | if (j < len) { |
| 4225 | SPLIT_APPEND(data, j, len); |
| 4226 | } |
| 4227 | |
| 4228 | Py_DECREF(string); |
| 4229 | return list; |
| 4230 | |
| 4231 | onError: |
| 4232 | Py_DECREF(list); |
| 4233 | Py_DECREF(string); |
| 4234 | return NULL; |
| 4235 | } |
| 4236 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4237 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4238 | PyObject *split_char(PyUnicodeObject *self, |
| 4239 | PyObject *list, |
| 4240 | Py_UNICODE ch, |
| 4241 | int maxcount) |
| 4242 | { |
| 4243 | register int i; |
| 4244 | register int j; |
| 4245 | int len = self->length; |
| 4246 | PyObject *str; |
| 4247 | |
| 4248 | for (i = j = 0; i < len; ) { |
| 4249 | if (self->str[i] == ch) { |
| 4250 | if (maxcount-- <= 0) |
| 4251 | break; |
| 4252 | SPLIT_APPEND(self->str, j, i); |
| 4253 | i = j = i + 1; |
| 4254 | } else |
| 4255 | i++; |
| 4256 | } |
| 4257 | if (j <= len) { |
| 4258 | SPLIT_APPEND(self->str, j, len); |
| 4259 | } |
| 4260 | return list; |
| 4261 | |
| 4262 | onError: |
| 4263 | Py_DECREF(list); |
| 4264 | return NULL; |
| 4265 | } |
| 4266 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4267 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4268 | PyObject *split_substring(PyUnicodeObject *self, |
| 4269 | PyObject *list, |
| 4270 | PyUnicodeObject *substring, |
| 4271 | int maxcount) |
| 4272 | { |
| 4273 | register int i; |
| 4274 | register int j; |
| 4275 | int len = self->length; |
| 4276 | int sublen = substring->length; |
| 4277 | PyObject *str; |
| 4278 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 4279 | for (i = j = 0; i <= len - sublen; ) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4280 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 4281 | if (maxcount-- <= 0) |
| 4282 | break; |
| 4283 | SPLIT_APPEND(self->str, j, i); |
| 4284 | i = j = i + sublen; |
| 4285 | } else |
| 4286 | i++; |
| 4287 | } |
| 4288 | if (j <= len) { |
| 4289 | SPLIT_APPEND(self->str, j, len); |
| 4290 | } |
| 4291 | return list; |
| 4292 | |
| 4293 | onError: |
| 4294 | Py_DECREF(list); |
| 4295 | return NULL; |
| 4296 | } |
| 4297 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4298 | static |
| 4299 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
| 4300 | PyObject *list, |
| 4301 | int maxcount) |
| 4302 | { |
| 4303 | register int i; |
| 4304 | register int j; |
| 4305 | int len = self->length; |
| 4306 | PyObject *str; |
| 4307 | |
| 4308 | for (i = j = len - 1; i >= 0; ) { |
| 4309 | /* find a token */ |
| 4310 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 4311 | i--; |
| 4312 | j = i; |
| 4313 | while (i >= 0 && !Py_UNICODE_ISSPACE(self->str[i])) |
| 4314 | i--; |
| 4315 | if (j > i) { |
| 4316 | if (maxcount-- <= 0) |
| 4317 | break; |
| 4318 | SPLIT_INSERT(self->str, i + 1, j + 1); |
| 4319 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 4320 | i--; |
| 4321 | j = i; |
| 4322 | } |
| 4323 | } |
| 4324 | if (j >= 0) { |
| 4325 | SPLIT_INSERT(self->str, 0, j + 1); |
| 4326 | } |
| 4327 | return list; |
| 4328 | |
| 4329 | onError: |
| 4330 | Py_DECREF(list); |
| 4331 | return NULL; |
| 4332 | } |
| 4333 | |
| 4334 | static |
| 4335 | PyObject *rsplit_char(PyUnicodeObject *self, |
| 4336 | PyObject *list, |
| 4337 | Py_UNICODE ch, |
| 4338 | int maxcount) |
| 4339 | { |
| 4340 | register int i; |
| 4341 | register int j; |
| 4342 | int len = self->length; |
| 4343 | PyObject *str; |
| 4344 | |
| 4345 | for (i = j = len - 1; i >= 0; ) { |
| 4346 | if (self->str[i] == ch) { |
| 4347 | if (maxcount-- <= 0) |
| 4348 | break; |
| 4349 | SPLIT_INSERT(self->str, i + 1, j + 1); |
| 4350 | j = i = i - 1; |
| 4351 | } else |
| 4352 | i--; |
| 4353 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 4354 | if (j >= -1) { |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4355 | SPLIT_INSERT(self->str, 0, j + 1); |
| 4356 | } |
| 4357 | return list; |
| 4358 | |
| 4359 | onError: |
| 4360 | Py_DECREF(list); |
| 4361 | return NULL; |
| 4362 | } |
| 4363 | |
| 4364 | static |
| 4365 | PyObject *rsplit_substring(PyUnicodeObject *self, |
| 4366 | PyObject *list, |
| 4367 | PyUnicodeObject *substring, |
| 4368 | int maxcount) |
| 4369 | { |
| 4370 | register int i; |
| 4371 | register int j; |
| 4372 | int len = self->length; |
| 4373 | int sublen = substring->length; |
| 4374 | PyObject *str; |
| 4375 | |
| 4376 | for (i = len - sublen, j = len; i >= 0; ) { |
| 4377 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 4378 | if (maxcount-- <= 0) |
| 4379 | break; |
| 4380 | SPLIT_INSERT(self->str, i + sublen, j); |
| 4381 | j = i; |
| 4382 | i -= sublen; |
| 4383 | } else |
| 4384 | i--; |
| 4385 | } |
| 4386 | if (j >= 0) { |
| 4387 | SPLIT_INSERT(self->str, 0, j); |
| 4388 | } |
| 4389 | return list; |
| 4390 | |
| 4391 | onError: |
| 4392 | Py_DECREF(list); |
| 4393 | return NULL; |
| 4394 | } |
| 4395 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4396 | #undef SPLIT_APPEND |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4397 | #undef SPLIT_INSERT |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4398 | |
| 4399 | static |
| 4400 | PyObject *split(PyUnicodeObject *self, |
| 4401 | PyUnicodeObject *substring, |
| 4402 | int maxcount) |
| 4403 | { |
| 4404 | PyObject *list; |
| 4405 | |
| 4406 | if (maxcount < 0) |
| 4407 | maxcount = INT_MAX; |
| 4408 | |
| 4409 | list = PyList_New(0); |
| 4410 | if (!list) |
| 4411 | return NULL; |
| 4412 | |
| 4413 | if (substring == NULL) |
| 4414 | return split_whitespace(self,list,maxcount); |
| 4415 | |
| 4416 | else if (substring->length == 1) |
| 4417 | return split_char(self,list,substring->str[0],maxcount); |
| 4418 | |
| 4419 | else if (substring->length == 0) { |
| 4420 | Py_DECREF(list); |
| 4421 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 4422 | return NULL; |
| 4423 | } |
| 4424 | else |
| 4425 | return split_substring(self,list,substring,maxcount); |
| 4426 | } |
| 4427 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4428 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4429 | PyObject *rsplit(PyUnicodeObject *self, |
| 4430 | PyUnicodeObject *substring, |
| 4431 | int maxcount) |
| 4432 | { |
| 4433 | PyObject *list; |
| 4434 | |
| 4435 | if (maxcount < 0) |
| 4436 | maxcount = INT_MAX; |
| 4437 | |
| 4438 | list = PyList_New(0); |
| 4439 | if (!list) |
| 4440 | return NULL; |
| 4441 | |
| 4442 | if (substring == NULL) |
| 4443 | return rsplit_whitespace(self,list,maxcount); |
| 4444 | |
| 4445 | else if (substring->length == 1) |
| 4446 | return rsplit_char(self,list,substring->str[0],maxcount); |
| 4447 | |
| 4448 | else if (substring->length == 0) { |
| 4449 | Py_DECREF(list); |
| 4450 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 4451 | return NULL; |
| 4452 | } |
| 4453 | else |
| 4454 | return rsplit_substring(self,list,substring,maxcount); |
| 4455 | } |
| 4456 | |
| 4457 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4458 | PyObject *replace(PyUnicodeObject *self, |
| 4459 | PyUnicodeObject *str1, |
| 4460 | PyUnicodeObject *str2, |
| 4461 | int maxcount) |
| 4462 | { |
| 4463 | PyUnicodeObject *u; |
| 4464 | |
| 4465 | if (maxcount < 0) |
| 4466 | maxcount = INT_MAX; |
| 4467 | |
| 4468 | if (str1->length == 1 && str2->length == 1) { |
| 4469 | int i; |
| 4470 | |
| 4471 | /* replace characters */ |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4472 | if (!findchar(self->str, self->length, str1->str[0]) && |
| 4473 | PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4474 | /* nothing to replace, return original string */ |
| 4475 | Py_INCREF(self); |
| 4476 | u = self; |
| 4477 | } else { |
| 4478 | Py_UNICODE u1 = str1->str[0]; |
| 4479 | Py_UNICODE u2 = str2->str[0]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4480 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4481 | u = (PyUnicodeObject*) PyUnicode_FromUnicode( |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4482 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4483 | self->length |
| 4484 | ); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4485 | if (u != NULL) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4486 | Py_UNICODE_COPY(u->str, self->str, |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4487 | self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4488 | for (i = 0; i < u->length; i++) |
| 4489 | if (u->str[i] == u1) { |
| 4490 | if (--maxcount < 0) |
| 4491 | break; |
| 4492 | u->str[i] = u2; |
| 4493 | } |
| 4494 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4495 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4496 | |
| 4497 | } else { |
| 4498 | int n, i; |
| 4499 | Py_UNICODE *p; |
| 4500 | |
| 4501 | /* replace strings */ |
| 4502 | n = count(self, 0, self->length, str1); |
| 4503 | if (n > maxcount) |
| 4504 | n = maxcount; |
Guido van Rossum | 2023c9b | 2002-08-23 18:50:21 +0000 | [diff] [blame] | 4505 | if (n == 0) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4506 | /* nothing to replace, return original string */ |
Guido van Rossum | 2023c9b | 2002-08-23 18:50:21 +0000 | [diff] [blame] | 4507 | if (PyUnicode_CheckExact(self)) { |
| 4508 | Py_INCREF(self); |
| 4509 | u = self; |
| 4510 | } |
| 4511 | else { |
| 4512 | u = (PyUnicodeObject *) |
| 4513 | PyUnicode_FromUnicode(self->str, self->length); |
| 4514 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4515 | } else { |
| 4516 | u = _PyUnicode_New( |
| 4517 | self->length + n * (str2->length - str1->length)); |
| 4518 | if (u) { |
| 4519 | i = 0; |
| 4520 | p = u->str; |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 4521 | if (str1->length > 0) { |
| 4522 | while (i <= self->length - str1->length) |
| 4523 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 4524 | /* replace string segment */ |
| 4525 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 4526 | p += str2->length; |
| 4527 | i += str1->length; |
| 4528 | if (--n <= 0) { |
| 4529 | /* copy remaining part */ |
| 4530 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 4531 | break; |
| 4532 | } |
| 4533 | } else |
| 4534 | *p++ = self->str[i++]; |
| 4535 | } else { |
| 4536 | while (n > 0) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4537 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 4538 | p += str2->length; |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 4539 | if (--n <= 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4540 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4541 | *p++ = self->str[i++]; |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 4542 | } |
| 4543 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 4544 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4545 | } |
| 4546 | } |
| 4547 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4548 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4549 | return (PyObject *) u; |
| 4550 | } |
| 4551 | |
| 4552 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 4553 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4554 | PyDoc_STRVAR(title__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4555 | "S.title() -> unicode\n\ |
| 4556 | \n\ |
| 4557 | 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] | 4558 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4559 | |
| 4560 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 4561 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4562 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4563 | return fixup(self, fixtitle); |
| 4564 | } |
| 4565 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4566 | PyDoc_STRVAR(capitalize__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4567 | "S.capitalize() -> unicode\n\ |
| 4568 | \n\ |
| 4569 | 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] | 4570 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4571 | |
| 4572 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 4573 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4574 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4575 | return fixup(self, fixcapitalize); |
| 4576 | } |
| 4577 | |
| 4578 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4579 | PyDoc_STRVAR(capwords__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4580 | "S.capwords() -> unicode\n\ |
| 4581 | \n\ |
| 4582 | 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] | 4583 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4584 | |
| 4585 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 4586 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4587 | { |
| 4588 | PyObject *list; |
| 4589 | PyObject *item; |
| 4590 | int i; |
| 4591 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4592 | /* Split into words */ |
| 4593 | list = split(self, NULL, -1); |
| 4594 | if (!list) |
| 4595 | return NULL; |
| 4596 | |
| 4597 | /* Capitalize each word */ |
| 4598 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 4599 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 4600 | fixcapitalize); |
| 4601 | if (item == NULL) |
| 4602 | goto onError; |
| 4603 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 4604 | PyList_SET_ITEM(list, i, item); |
| 4605 | } |
| 4606 | |
| 4607 | /* Join the words to form a new string */ |
| 4608 | item = PyUnicode_Join(NULL, list); |
| 4609 | |
| 4610 | onError: |
| 4611 | Py_DECREF(list); |
| 4612 | return (PyObject *)item; |
| 4613 | } |
| 4614 | #endif |
| 4615 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4616 | /* Argument converter. Coerces to a single unicode character */ |
| 4617 | |
| 4618 | static int |
| 4619 | convert_uc(PyObject *obj, void *addr) |
| 4620 | { |
| 4621 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 4622 | PyObject *uniobj; |
| 4623 | Py_UNICODE *unistr; |
| 4624 | |
| 4625 | uniobj = PyUnicode_FromObject(obj); |
| 4626 | if (uniobj == NULL) { |
| 4627 | PyErr_SetString(PyExc_TypeError, |
| 4628 | "The fill character cannot be converted to Unicode"); |
| 4629 | return 0; |
| 4630 | } |
| 4631 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 4632 | PyErr_SetString(PyExc_TypeError, |
| 4633 | "The fill character must be exactly one character long"); |
| 4634 | Py_DECREF(uniobj); |
| 4635 | return 0; |
| 4636 | } |
| 4637 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 4638 | *fillcharloc = unistr[0]; |
| 4639 | Py_DECREF(uniobj); |
| 4640 | return 1; |
| 4641 | } |
| 4642 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4643 | PyDoc_STRVAR(center__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4644 | "S.center(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4645 | \n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4646 | Return S centered in a Unicode string of length width. Padding is\n\ |
| 4647 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4648 | |
| 4649 | static PyObject * |
| 4650 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 4651 | { |
| 4652 | int marg, left; |
| 4653 | int width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4654 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4655 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4656 | if (!PyArg_ParseTuple(args, "i|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4657 | return NULL; |
| 4658 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4659 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4660 | Py_INCREF(self); |
| 4661 | return (PyObject*) self; |
| 4662 | } |
| 4663 | |
| 4664 | marg = width - self->length; |
| 4665 | left = marg / 2 + (marg & width & 1); |
| 4666 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4667 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4668 | } |
| 4669 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4670 | #if 0 |
| 4671 | |
| 4672 | /* This code should go into some future Unicode collation support |
| 4673 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 4674 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4675 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4676 | /* speedy UTF-16 code point order comparison */ |
| 4677 | /* gleaned from: */ |
| 4678 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 4679 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4680 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4681 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4682 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4683 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 4684 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4685 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4686 | }; |
| 4687 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4688 | static int |
| 4689 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 4690 | { |
| 4691 | int len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4692 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4693 | Py_UNICODE *s1 = str1->str; |
| 4694 | Py_UNICODE *s2 = str2->str; |
| 4695 | |
| 4696 | len1 = str1->length; |
| 4697 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4698 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4699 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4700 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4701 | |
| 4702 | c1 = *s1++; |
| 4703 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 4704 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4705 | if (c1 > (1<<11) * 26) |
| 4706 | c1 += utf16Fixup[c1>>11]; |
| 4707 | if (c2 > (1<<11) * 26) |
| 4708 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4709 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 4710 | |
| 4711 | if (c1 != c2) |
| 4712 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4713 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4714 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4715 | } |
| 4716 | |
| 4717 | return (len1 < len2) ? -1 : (len1 != len2); |
| 4718 | } |
| 4719 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4720 | #else |
| 4721 | |
| 4722 | static int |
| 4723 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 4724 | { |
| 4725 | register int len1, len2; |
| 4726 | |
| 4727 | Py_UNICODE *s1 = str1->str; |
| 4728 | Py_UNICODE *s2 = str2->str; |
| 4729 | |
| 4730 | len1 = str1->length; |
| 4731 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4732 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4733 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4734 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4735 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 4736 | c1 = *s1++; |
| 4737 | c2 = *s2++; |
| 4738 | |
| 4739 | if (c1 != c2) |
| 4740 | return (c1 < c2) ? -1 : 1; |
| 4741 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4742 | len1--; len2--; |
| 4743 | } |
| 4744 | |
| 4745 | return (len1 < len2) ? -1 : (len1 != len2); |
| 4746 | } |
| 4747 | |
| 4748 | #endif |
| 4749 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4750 | int PyUnicode_Compare(PyObject *left, |
| 4751 | PyObject *right) |
| 4752 | { |
| 4753 | PyUnicodeObject *u = NULL, *v = NULL; |
| 4754 | int result; |
| 4755 | |
| 4756 | /* Coerce the two arguments */ |
| 4757 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 4758 | if (u == NULL) |
| 4759 | goto onError; |
| 4760 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 4761 | if (v == NULL) |
| 4762 | goto onError; |
| 4763 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 4764 | /* Shortcut for empty or interned objects */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4765 | if (v == u) { |
| 4766 | Py_DECREF(u); |
| 4767 | Py_DECREF(v); |
| 4768 | return 0; |
| 4769 | } |
| 4770 | |
| 4771 | result = unicode_compare(u, v); |
| 4772 | |
| 4773 | Py_DECREF(u); |
| 4774 | Py_DECREF(v); |
| 4775 | return result; |
| 4776 | |
| 4777 | onError: |
| 4778 | Py_XDECREF(u); |
| 4779 | Py_XDECREF(v); |
| 4780 | return -1; |
| 4781 | } |
| 4782 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4783 | int PyUnicode_Contains(PyObject *container, |
| 4784 | PyObject *element) |
| 4785 | { |
| 4786 | PyUnicodeObject *u = NULL, *v = NULL; |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 4787 | int result, size; |
| 4788 | register const Py_UNICODE *lhs, *end, *rhs; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4789 | |
| 4790 | /* Coerce the two arguments */ |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4791 | v = (PyUnicodeObject *)PyUnicode_FromObject(element); |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 4792 | if (v == NULL) { |
| 4793 | PyErr_SetString(PyExc_TypeError, |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 4794 | "'in <string>' requires string as left operand"); |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4795 | goto onError; |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 4796 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4797 | u = (PyUnicodeObject *)PyUnicode_FromObject(container); |
Marc-André Lemburg | 9cd87aa | 2002-10-23 09:02:46 +0000 | [diff] [blame] | 4798 | if (u == NULL) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4799 | goto onError; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4800 | |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 4801 | size = PyUnicode_GET_SIZE(v); |
| 4802 | rhs = PyUnicode_AS_UNICODE(v); |
| 4803 | lhs = PyUnicode_AS_UNICODE(u); |
| 4804 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4805 | result = 0; |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 4806 | if (size == 1) { |
| 4807 | end = lhs + PyUnicode_GET_SIZE(u); |
| 4808 | while (lhs < end) { |
| 4809 | if (*lhs++ == *rhs) { |
| 4810 | result = 1; |
| 4811 | break; |
| 4812 | } |
| 4813 | } |
| 4814 | } |
| 4815 | else { |
| 4816 | end = lhs + (PyUnicode_GET_SIZE(u) - size); |
| 4817 | while (lhs <= end) { |
Barry Warsaw | 6a043f3 | 2002-08-06 19:03:17 +0000 | [diff] [blame] | 4818 | if (memcmp(lhs++, rhs, size * sizeof(Py_UNICODE)) == 0) { |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 4819 | result = 1; |
| 4820 | break; |
| 4821 | } |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4822 | } |
| 4823 | } |
| 4824 | |
| 4825 | Py_DECREF(u); |
| 4826 | Py_DECREF(v); |
| 4827 | return result; |
| 4828 | |
| 4829 | onError: |
| 4830 | Py_XDECREF(u); |
| 4831 | Py_XDECREF(v); |
| 4832 | return -1; |
| 4833 | } |
| 4834 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4835 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 4836 | |
| 4837 | PyObject *PyUnicode_Concat(PyObject *left, |
| 4838 | PyObject *right) |
| 4839 | { |
| 4840 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 4841 | |
| 4842 | /* Coerce the two arguments */ |
| 4843 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 4844 | if (u == NULL) |
| 4845 | goto onError; |
| 4846 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 4847 | if (v == NULL) |
| 4848 | goto onError; |
| 4849 | |
| 4850 | /* Shortcuts */ |
| 4851 | if (v == unicode_empty) { |
| 4852 | Py_DECREF(v); |
| 4853 | return (PyObject *)u; |
| 4854 | } |
| 4855 | if (u == unicode_empty) { |
| 4856 | Py_DECREF(u); |
| 4857 | return (PyObject *)v; |
| 4858 | } |
| 4859 | |
| 4860 | /* Concat the two Unicode strings */ |
| 4861 | w = _PyUnicode_New(u->length + v->length); |
| 4862 | if (w == NULL) |
| 4863 | goto onError; |
| 4864 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 4865 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 4866 | |
| 4867 | Py_DECREF(u); |
| 4868 | Py_DECREF(v); |
| 4869 | return (PyObject *)w; |
| 4870 | |
| 4871 | onError: |
| 4872 | Py_XDECREF(u); |
| 4873 | Py_XDECREF(v); |
| 4874 | return NULL; |
| 4875 | } |
| 4876 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4877 | PyDoc_STRVAR(count__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4878 | "S.count(sub[, start[, end]]) -> int\n\ |
| 4879 | \n\ |
| 4880 | Return the number of occurrences of substring sub in Unicode string\n\ |
| 4881 | S[start:end]. Optional arguments start and end are\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4882 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4883 | |
| 4884 | static PyObject * |
| 4885 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 4886 | { |
| 4887 | PyUnicodeObject *substring; |
| 4888 | int start = 0; |
| 4889 | int end = INT_MAX; |
| 4890 | PyObject *result; |
| 4891 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 4892 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 4893 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4894 | return NULL; |
| 4895 | |
| 4896 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 4897 | (PyObject *)substring); |
| 4898 | if (substring == NULL) |
| 4899 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4900 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4901 | if (start < 0) |
| 4902 | start += self->length; |
| 4903 | if (start < 0) |
| 4904 | start = 0; |
| 4905 | if (end > self->length) |
| 4906 | end = self->length; |
| 4907 | if (end < 0) |
| 4908 | end += self->length; |
| 4909 | if (end < 0) |
| 4910 | end = 0; |
| 4911 | |
| 4912 | result = PyInt_FromLong((long) count(self, start, end, substring)); |
| 4913 | |
| 4914 | Py_DECREF(substring); |
| 4915 | return result; |
| 4916 | } |
| 4917 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4918 | PyDoc_STRVAR(encode__doc__, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 4919 | "S.encode([encoding[,errors]]) -> string or unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4920 | \n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 4921 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 4922 | to the default encoding. errors may be given to set a different error\n\ |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 4923 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4924 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 4925 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 4926 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4927 | |
| 4928 | static PyObject * |
| 4929 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 4930 | { |
| 4931 | char *encoding = NULL; |
| 4932 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 4933 | PyObject *v; |
| 4934 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4935 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 4936 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 4937 | v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 4938 | if (v == NULL) |
| 4939 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 4940 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 4941 | PyErr_Format(PyExc_TypeError, |
| 4942 | "encoder did not return a string/unicode object " |
| 4943 | "(type=%.400s)", |
| 4944 | v->ob_type->tp_name); |
| 4945 | Py_DECREF(v); |
| 4946 | return NULL; |
| 4947 | } |
| 4948 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 4949 | |
| 4950 | onError: |
| 4951 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 4952 | } |
| 4953 | |
| 4954 | PyDoc_STRVAR(decode__doc__, |
| 4955 | "S.decode([encoding[,errors]]) -> string or unicode\n\ |
| 4956 | \n\ |
| 4957 | Decodes S using the codec registered for encoding. encoding defaults\n\ |
| 4958 | to the default encoding. errors may be given to set a different error\n\ |
| 4959 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 4960 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 4961 | as well as any other name registerd with codecs.register_error that is\n\ |
| 4962 | able to handle UnicodeDecodeErrors."); |
| 4963 | |
| 4964 | static PyObject * |
| 4965 | unicode_decode(PyStringObject *self, PyObject *args) |
| 4966 | { |
| 4967 | char *encoding = NULL; |
| 4968 | char *errors = NULL; |
| 4969 | PyObject *v; |
| 4970 | |
| 4971 | if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
| 4972 | return NULL; |
| 4973 | v = PyUnicode_AsDecodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 4974 | if (v == NULL) |
| 4975 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 4976 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 4977 | PyErr_Format(PyExc_TypeError, |
| 4978 | "decoder did not return a string/unicode object " |
| 4979 | "(type=%.400s)", |
| 4980 | v->ob_type->tp_name); |
| 4981 | Py_DECREF(v); |
| 4982 | return NULL; |
| 4983 | } |
| 4984 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 4985 | |
| 4986 | onError: |
| 4987 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4988 | } |
| 4989 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4990 | PyDoc_STRVAR(expandtabs__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4991 | "S.expandtabs([tabsize]) -> unicode\n\ |
| 4992 | \n\ |
| 4993 | 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] | 4994 | 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] | 4995 | |
| 4996 | static PyObject* |
| 4997 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 4998 | { |
| 4999 | Py_UNICODE *e; |
| 5000 | Py_UNICODE *p; |
| 5001 | Py_UNICODE *q; |
| 5002 | int i, j; |
| 5003 | PyUnicodeObject *u; |
| 5004 | int tabsize = 8; |
| 5005 | |
| 5006 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 5007 | return NULL; |
| 5008 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5009 | /* First pass: determine size of output string */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5010 | i = j = 0; |
| 5011 | e = self->str + self->length; |
| 5012 | for (p = self->str; p < e; p++) |
| 5013 | if (*p == '\t') { |
| 5014 | if (tabsize > 0) |
| 5015 | j += tabsize - (j % tabsize); |
| 5016 | } |
| 5017 | else { |
| 5018 | j++; |
| 5019 | if (*p == '\n' || *p == '\r') { |
| 5020 | i += j; |
| 5021 | j = 0; |
| 5022 | } |
| 5023 | } |
| 5024 | |
| 5025 | /* Second pass: create output string and fill it */ |
| 5026 | u = _PyUnicode_New(i + j); |
| 5027 | if (!u) |
| 5028 | return NULL; |
| 5029 | |
| 5030 | j = 0; |
| 5031 | q = u->str; |
| 5032 | |
| 5033 | for (p = self->str; p < e; p++) |
| 5034 | if (*p == '\t') { |
| 5035 | if (tabsize > 0) { |
| 5036 | i = tabsize - (j % tabsize); |
| 5037 | j += i; |
| 5038 | while (i--) |
| 5039 | *q++ = ' '; |
| 5040 | } |
| 5041 | } |
| 5042 | else { |
| 5043 | j++; |
| 5044 | *q++ = *p; |
| 5045 | if (*p == '\n' || *p == '\r') |
| 5046 | j = 0; |
| 5047 | } |
| 5048 | |
| 5049 | return (PyObject*) u; |
| 5050 | } |
| 5051 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5052 | PyDoc_STRVAR(find__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5053 | "S.find(sub [,start [,end]]) -> int\n\ |
| 5054 | \n\ |
| 5055 | Return the lowest index in S where substring sub is found,\n\ |
| 5056 | such that sub is contained within s[start,end]. Optional\n\ |
| 5057 | arguments start and end are interpreted as in slice notation.\n\ |
| 5058 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5059 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5060 | |
| 5061 | static PyObject * |
| 5062 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 5063 | { |
| 5064 | PyUnicodeObject *substring; |
| 5065 | int start = 0; |
| 5066 | int end = INT_MAX; |
| 5067 | PyObject *result; |
| 5068 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5069 | if (!PyArg_ParseTuple(args, "O|O&O&:find", &substring, |
| 5070 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5071 | return NULL; |
| 5072 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 5073 | (PyObject *)substring); |
| 5074 | if (substring == NULL) |
| 5075 | return NULL; |
| 5076 | |
| 5077 | result = PyInt_FromLong(findstring(self, substring, start, end, 1)); |
| 5078 | |
| 5079 | Py_DECREF(substring); |
| 5080 | return result; |
| 5081 | } |
| 5082 | |
| 5083 | static PyObject * |
| 5084 | unicode_getitem(PyUnicodeObject *self, int index) |
| 5085 | { |
| 5086 | if (index < 0 || index >= self->length) { |
| 5087 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 5088 | return NULL; |
| 5089 | } |
| 5090 | |
| 5091 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 5092 | } |
| 5093 | |
| 5094 | static long |
| 5095 | unicode_hash(PyUnicodeObject *self) |
| 5096 | { |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5097 | /* Since Unicode objects compare equal to their ASCII string |
| 5098 | counterparts, they should use the individual character values |
| 5099 | as basis for their hash value. This is needed to assure that |
| 5100 | strings and Unicode objects behave in the same way as |
| 5101 | dictionary keys. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5102 | |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5103 | register int len; |
| 5104 | register Py_UNICODE *p; |
| 5105 | register long x; |
| 5106 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5107 | if (self->hash != -1) |
| 5108 | return self->hash; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5109 | len = PyUnicode_GET_SIZE(self); |
| 5110 | p = PyUnicode_AS_UNICODE(self); |
| 5111 | x = *p << 7; |
| 5112 | while (--len >= 0) |
| 5113 | x = (1000003*x) ^ *p++; |
| 5114 | x ^= PyUnicode_GET_SIZE(self); |
| 5115 | if (x == -1) |
| 5116 | x = -2; |
| 5117 | self->hash = x; |
| 5118 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5119 | } |
| 5120 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5121 | PyDoc_STRVAR(index__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5122 | "S.index(sub [,start [,end]]) -> int\n\ |
| 5123 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5124 | 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] | 5125 | |
| 5126 | static PyObject * |
| 5127 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 5128 | { |
| 5129 | int result; |
| 5130 | PyUnicodeObject *substring; |
| 5131 | int start = 0; |
| 5132 | int end = INT_MAX; |
| 5133 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5134 | if (!PyArg_ParseTuple(args, "O|O&O&:index", &substring, |
| 5135 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5136 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5137 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5138 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 5139 | (PyObject *)substring); |
| 5140 | if (substring == NULL) |
| 5141 | return NULL; |
| 5142 | |
| 5143 | result = findstring(self, substring, start, end, 1); |
| 5144 | |
| 5145 | Py_DECREF(substring); |
| 5146 | if (result < 0) { |
| 5147 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 5148 | return NULL; |
| 5149 | } |
| 5150 | return PyInt_FromLong(result); |
| 5151 | } |
| 5152 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5153 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5154 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5155 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5156 | 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] | 5157 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5158 | |
| 5159 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5160 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5161 | { |
| 5162 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5163 | register const Py_UNICODE *e; |
| 5164 | int cased; |
| 5165 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5166 | /* Shortcut for single character strings */ |
| 5167 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5168 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5169 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5170 | /* Special case for empty strings */ |
| 5171 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5172 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5173 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5174 | e = p + PyUnicode_GET_SIZE(self); |
| 5175 | cased = 0; |
| 5176 | for (; p < e; p++) { |
| 5177 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5178 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5179 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5180 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5181 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 5182 | cased = 1; |
| 5183 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5184 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5185 | } |
| 5186 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5187 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5188 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5189 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5190 | 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] | 5191 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5192 | |
| 5193 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5194 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5195 | { |
| 5196 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5197 | register const Py_UNICODE *e; |
| 5198 | int cased; |
| 5199 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5200 | /* Shortcut for single character strings */ |
| 5201 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5202 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5203 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5204 | /* Special case for empty strings */ |
| 5205 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5206 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5207 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5208 | e = p + PyUnicode_GET_SIZE(self); |
| 5209 | cased = 0; |
| 5210 | for (; p < e; p++) { |
| 5211 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5212 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5213 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5214 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5215 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 5216 | cased = 1; |
| 5217 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5218 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5219 | } |
| 5220 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5221 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5222 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5223 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5224 | Return True if S is a titlecased string and there is at least one\n\ |
| 5225 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 5226 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 5227 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5228 | |
| 5229 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5230 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5231 | { |
| 5232 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5233 | register const Py_UNICODE *e; |
| 5234 | int cased, previous_is_cased; |
| 5235 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5236 | /* Shortcut for single character strings */ |
| 5237 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5238 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 5239 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5240 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5241 | /* Special case for empty strings */ |
| 5242 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5243 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5244 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5245 | e = p + PyUnicode_GET_SIZE(self); |
| 5246 | cased = 0; |
| 5247 | previous_is_cased = 0; |
| 5248 | for (; p < e; p++) { |
| 5249 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5250 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5251 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 5252 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5253 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5254 | previous_is_cased = 1; |
| 5255 | cased = 1; |
| 5256 | } |
| 5257 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 5258 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5259 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5260 | previous_is_cased = 1; |
| 5261 | cased = 1; |
| 5262 | } |
| 5263 | else |
| 5264 | previous_is_cased = 0; |
| 5265 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5266 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5267 | } |
| 5268 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5269 | PyDoc_STRVAR(isspace__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5270 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5271 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5272 | Return True if all characters in S are whitespace\n\ |
| 5273 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5274 | |
| 5275 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5276 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5277 | { |
| 5278 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5279 | register const Py_UNICODE *e; |
| 5280 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5281 | /* Shortcut for single character strings */ |
| 5282 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5283 | Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5284 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5285 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5286 | /* Special case for empty strings */ |
| 5287 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5288 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5289 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5290 | e = p + PyUnicode_GET_SIZE(self); |
| 5291 | for (; p < e; p++) { |
| 5292 | if (!Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5293 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5294 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5295 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5296 | } |
| 5297 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5298 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5299 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5300 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5301 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5302 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5303 | |
| 5304 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5305 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5306 | { |
| 5307 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5308 | register const Py_UNICODE *e; |
| 5309 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5310 | /* Shortcut for single character strings */ |
| 5311 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5312 | Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5313 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5314 | |
| 5315 | /* Special case for empty strings */ |
| 5316 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5317 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5318 | |
| 5319 | e = p + PyUnicode_GET_SIZE(self); |
| 5320 | for (; p < e; p++) { |
| 5321 | if (!Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5322 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5323 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5324 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5325 | } |
| 5326 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5327 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5328 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5329 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5330 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5331 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5332 | |
| 5333 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5334 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5335 | { |
| 5336 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5337 | register const Py_UNICODE *e; |
| 5338 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5339 | /* Shortcut for single character strings */ |
| 5340 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5341 | Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5342 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5343 | |
| 5344 | /* Special case for empty strings */ |
| 5345 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5346 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5347 | |
| 5348 | e = p + PyUnicode_GET_SIZE(self); |
| 5349 | for (; p < e; p++) { |
| 5350 | if (!Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5351 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5352 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5353 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5354 | } |
| 5355 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5356 | PyDoc_STRVAR(isdecimal__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5357 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5358 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5359 | 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] | 5360 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5361 | |
| 5362 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5363 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5364 | { |
| 5365 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5366 | register const Py_UNICODE *e; |
| 5367 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5368 | /* Shortcut for single character strings */ |
| 5369 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5370 | Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5371 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5372 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5373 | /* Special case for empty strings */ |
| 5374 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5375 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5376 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5377 | e = p + PyUnicode_GET_SIZE(self); |
| 5378 | for (; p < e; p++) { |
| 5379 | if (!Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5380 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5381 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5382 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5383 | } |
| 5384 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5385 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5386 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5387 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5388 | Return True if all characters in S are digits\n\ |
| 5389 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5390 | |
| 5391 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5392 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5393 | { |
| 5394 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5395 | register const Py_UNICODE *e; |
| 5396 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5397 | /* Shortcut for single character strings */ |
| 5398 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5399 | Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5400 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5401 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5402 | /* Special case for empty strings */ |
| 5403 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5404 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5405 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5406 | e = p + PyUnicode_GET_SIZE(self); |
| 5407 | for (; p < e; p++) { |
| 5408 | if (!Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5409 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5410 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5411 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5412 | } |
| 5413 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5414 | PyDoc_STRVAR(isnumeric__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5415 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5416 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5417 | 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] | 5418 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5419 | |
| 5420 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5421 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5422 | { |
| 5423 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5424 | register const Py_UNICODE *e; |
| 5425 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5426 | /* Shortcut for single character strings */ |
| 5427 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5428 | Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5429 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5430 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5431 | /* Special case for empty strings */ |
| 5432 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5433 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5434 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5435 | e = p + PyUnicode_GET_SIZE(self); |
| 5436 | for (; p < e; p++) { |
| 5437 | if (!Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5438 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5439 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5440 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5441 | } |
| 5442 | |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 5443 | PyDoc_STRVAR(iswide__doc__, |
| 5444 | "S.iswide() -> bool\n\ |
| 5445 | \n\ |
| 5446 | Return True if all characters in S are wide width\n\ |
| 5447 | and there is at least one character in S, False otherwise."); |
| 5448 | |
| 5449 | static PyObject* |
| 5450 | unicode_iswide(PyUnicodeObject *self) |
| 5451 | { |
| 5452 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5453 | register const Py_UNICODE *e; |
| 5454 | |
| 5455 | /* Shortcut for single character strings */ |
| 5456 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5457 | Py_UNICODE_ISWIDE(*p)) |
| 5458 | Py_RETURN_TRUE; |
| 5459 | |
| 5460 | /* Special case for empty strings */ |
| 5461 | if (PyString_GET_SIZE(self) == 0) |
| 5462 | Py_RETURN_FALSE; |
| 5463 | |
| 5464 | e = p + PyUnicode_GET_SIZE(self); |
| 5465 | for (; p < e; p++) { |
| 5466 | if (!Py_UNICODE_ISWIDE(*p)) |
| 5467 | Py_RETURN_FALSE; |
| 5468 | } |
| 5469 | Py_RETURN_TRUE; |
| 5470 | } |
| 5471 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5472 | PyDoc_STRVAR(join__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5473 | "S.join(sequence) -> unicode\n\ |
| 5474 | \n\ |
| 5475 | 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] | 5476 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5477 | |
| 5478 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5479 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5480 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5481 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5482 | } |
| 5483 | |
| 5484 | static int |
| 5485 | unicode_length(PyUnicodeObject *self) |
| 5486 | { |
| 5487 | return self->length; |
| 5488 | } |
| 5489 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5490 | PyDoc_STRVAR(ljust__doc__, |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 5491 | "S.ljust(width[, fillchar]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5492 | \n\ |
| 5493 | 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] | 5494 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5495 | |
| 5496 | static PyObject * |
| 5497 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 5498 | { |
| 5499 | int width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5500 | Py_UNICODE fillchar = ' '; |
| 5501 | |
| 5502 | if (!PyArg_ParseTuple(args, "i|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5503 | return NULL; |
| 5504 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5505 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5506 | Py_INCREF(self); |
| 5507 | return (PyObject*) self; |
| 5508 | } |
| 5509 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5510 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5511 | } |
| 5512 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5513 | PyDoc_STRVAR(lower__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5514 | "S.lower() -> unicode\n\ |
| 5515 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5516 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5517 | |
| 5518 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5519 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5520 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5521 | return fixup(self, fixlower); |
| 5522 | } |
| 5523 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5524 | #define LEFTSTRIP 0 |
| 5525 | #define RIGHTSTRIP 1 |
| 5526 | #define BOTHSTRIP 2 |
| 5527 | |
| 5528 | /* Arrays indexed by above */ |
| 5529 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 5530 | |
| 5531 | #define STRIPNAME(i) (stripformat[i]+3) |
| 5532 | |
| 5533 | static const Py_UNICODE * |
| 5534 | unicode_memchr(const Py_UNICODE *s, Py_UNICODE c, size_t n) |
| 5535 | { |
Tim Peters | 030a5ce | 2002-04-22 19:00:10 +0000 | [diff] [blame] | 5536 | size_t i; |
| 5537 | for (i = 0; i < n; ++i) |
| 5538 | if (s[i] == c) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5539 | return s+i; |
| 5540 | return NULL; |
| 5541 | } |
| 5542 | |
| 5543 | /* externally visible for str.strip(unicode) */ |
| 5544 | PyObject * |
| 5545 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 5546 | { |
| 5547 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 5548 | int len = PyUnicode_GET_SIZE(self); |
| 5549 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
| 5550 | int seplen = PyUnicode_GET_SIZE(sepobj); |
| 5551 | int i, j; |
| 5552 | |
| 5553 | i = 0; |
| 5554 | if (striptype != RIGHTSTRIP) { |
| 5555 | while (i < len && unicode_memchr(sep, s[i], seplen)) { |
| 5556 | i++; |
| 5557 | } |
| 5558 | } |
| 5559 | |
| 5560 | j = len; |
| 5561 | if (striptype != LEFTSTRIP) { |
| 5562 | do { |
| 5563 | j--; |
| 5564 | } while (j >= i && unicode_memchr(sep, s[j], seplen)); |
| 5565 | j++; |
| 5566 | } |
| 5567 | |
| 5568 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 5569 | Py_INCREF(self); |
| 5570 | return (PyObject*)self; |
| 5571 | } |
| 5572 | else |
| 5573 | return PyUnicode_FromUnicode(s+i, j-i); |
| 5574 | } |
| 5575 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5576 | |
| 5577 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5578 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5579 | { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5580 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 5581 | int len = PyUnicode_GET_SIZE(self), i, j; |
| 5582 | |
| 5583 | i = 0; |
| 5584 | if (striptype != RIGHTSTRIP) { |
| 5585 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 5586 | i++; |
| 5587 | } |
| 5588 | } |
| 5589 | |
| 5590 | j = len; |
| 5591 | if (striptype != LEFTSTRIP) { |
| 5592 | do { |
| 5593 | j--; |
| 5594 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 5595 | j++; |
| 5596 | } |
| 5597 | |
| 5598 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 5599 | Py_INCREF(self); |
| 5600 | return (PyObject*)self; |
| 5601 | } |
| 5602 | else |
| 5603 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5604 | } |
| 5605 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5606 | |
| 5607 | static PyObject * |
| 5608 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 5609 | { |
| 5610 | PyObject *sep = NULL; |
| 5611 | |
| 5612 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 5613 | return NULL; |
| 5614 | |
| 5615 | if (sep != NULL && sep != Py_None) { |
| 5616 | if (PyUnicode_Check(sep)) |
| 5617 | return _PyUnicode_XStrip(self, striptype, sep); |
| 5618 | else if (PyString_Check(sep)) { |
| 5619 | PyObject *res; |
| 5620 | sep = PyUnicode_FromObject(sep); |
| 5621 | if (sep==NULL) |
| 5622 | return NULL; |
| 5623 | res = _PyUnicode_XStrip(self, striptype, sep); |
| 5624 | Py_DECREF(sep); |
| 5625 | return res; |
| 5626 | } |
| 5627 | else { |
| 5628 | PyErr_Format(PyExc_TypeError, |
| 5629 | "%s arg must be None, unicode or str", |
| 5630 | STRIPNAME(striptype)); |
| 5631 | return NULL; |
| 5632 | } |
| 5633 | } |
| 5634 | |
| 5635 | return do_strip(self, striptype); |
| 5636 | } |
| 5637 | |
| 5638 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5639 | PyDoc_STRVAR(strip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5640 | "S.strip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5641 | \n\ |
| 5642 | Return a copy of the string S with leading and trailing\n\ |
| 5643 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5644 | If chars is given and not None, remove characters in chars instead.\n\ |
| 5645 | 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] | 5646 | |
| 5647 | static PyObject * |
| 5648 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 5649 | { |
| 5650 | if (PyTuple_GET_SIZE(args) == 0) |
| 5651 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 5652 | else |
| 5653 | return do_argstrip(self, BOTHSTRIP, args); |
| 5654 | } |
| 5655 | |
| 5656 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5657 | PyDoc_STRVAR(lstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5658 | "S.lstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5659 | \n\ |
| 5660 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5661 | If chars is given and not None, remove characters in chars instead.\n\ |
| 5662 | 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] | 5663 | |
| 5664 | static PyObject * |
| 5665 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 5666 | { |
| 5667 | if (PyTuple_GET_SIZE(args) == 0) |
| 5668 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 5669 | else |
| 5670 | return do_argstrip(self, LEFTSTRIP, args); |
| 5671 | } |
| 5672 | |
| 5673 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5674 | PyDoc_STRVAR(rstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5675 | "S.rstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5676 | \n\ |
| 5677 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5678 | If chars is given and not None, remove characters in chars instead.\n\ |
| 5679 | 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] | 5680 | |
| 5681 | static PyObject * |
| 5682 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 5683 | { |
| 5684 | if (PyTuple_GET_SIZE(args) == 0) |
| 5685 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 5686 | else |
| 5687 | return do_argstrip(self, RIGHTSTRIP, args); |
| 5688 | } |
| 5689 | |
| 5690 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5691 | static PyObject* |
| 5692 | unicode_repeat(PyUnicodeObject *str, int len) |
| 5693 | { |
| 5694 | PyUnicodeObject *u; |
| 5695 | Py_UNICODE *p; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 5696 | int nchars; |
| 5697 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5698 | |
| 5699 | if (len < 0) |
| 5700 | len = 0; |
| 5701 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5702 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5703 | /* no repeat, return original string */ |
| 5704 | Py_INCREF(str); |
| 5705 | return (PyObject*) str; |
| 5706 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 5707 | |
| 5708 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 5709 | * needed doesn't overflow size_t |
| 5710 | */ |
| 5711 | nchars = len * str->length; |
| 5712 | if (len && nchars / len != str->length) { |
| 5713 | PyErr_SetString(PyExc_OverflowError, |
| 5714 | "repeated string is too long"); |
| 5715 | return NULL; |
| 5716 | } |
| 5717 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 5718 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 5719 | PyErr_SetString(PyExc_OverflowError, |
| 5720 | "repeated string is too long"); |
| 5721 | return NULL; |
| 5722 | } |
| 5723 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5724 | if (!u) |
| 5725 | return NULL; |
| 5726 | |
| 5727 | p = u->str; |
| 5728 | |
| 5729 | while (len-- > 0) { |
| 5730 | Py_UNICODE_COPY(p, str->str, str->length); |
| 5731 | p += str->length; |
| 5732 | } |
| 5733 | |
| 5734 | return (PyObject*) u; |
| 5735 | } |
| 5736 | |
| 5737 | PyObject *PyUnicode_Replace(PyObject *obj, |
| 5738 | PyObject *subobj, |
| 5739 | PyObject *replobj, |
| 5740 | int maxcount) |
| 5741 | { |
| 5742 | PyObject *self; |
| 5743 | PyObject *str1; |
| 5744 | PyObject *str2; |
| 5745 | PyObject *result; |
| 5746 | |
| 5747 | self = PyUnicode_FromObject(obj); |
| 5748 | if (self == NULL) |
| 5749 | return NULL; |
| 5750 | str1 = PyUnicode_FromObject(subobj); |
| 5751 | if (str1 == NULL) { |
| 5752 | Py_DECREF(self); |
| 5753 | return NULL; |
| 5754 | } |
| 5755 | str2 = PyUnicode_FromObject(replobj); |
| 5756 | if (str2 == NULL) { |
| 5757 | Py_DECREF(self); |
| 5758 | Py_DECREF(str1); |
| 5759 | return NULL; |
| 5760 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5761 | result = replace((PyUnicodeObject *)self, |
| 5762 | (PyUnicodeObject *)str1, |
| 5763 | (PyUnicodeObject *)str2, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5764 | maxcount); |
| 5765 | Py_DECREF(self); |
| 5766 | Py_DECREF(str1); |
| 5767 | Py_DECREF(str2); |
| 5768 | return result; |
| 5769 | } |
| 5770 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5771 | PyDoc_STRVAR(replace__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5772 | "S.replace (old, new[, maxsplit]) -> unicode\n\ |
| 5773 | \n\ |
| 5774 | Return a copy of S with all occurrences of substring\n\ |
| 5775 | 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] | 5776 | given, only the first maxsplit occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5777 | |
| 5778 | static PyObject* |
| 5779 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 5780 | { |
| 5781 | PyUnicodeObject *str1; |
| 5782 | PyUnicodeObject *str2; |
| 5783 | int maxcount = -1; |
| 5784 | PyObject *result; |
| 5785 | |
| 5786 | if (!PyArg_ParseTuple(args, "OO|i:replace", &str1, &str2, &maxcount)) |
| 5787 | return NULL; |
| 5788 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 5789 | if (str1 == NULL) |
| 5790 | return NULL; |
| 5791 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 5792 | if (str2 == NULL) { |
| 5793 | Py_DECREF(str1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5794 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 5795 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5796 | |
| 5797 | result = replace(self, str1, str2, maxcount); |
| 5798 | |
| 5799 | Py_DECREF(str1); |
| 5800 | Py_DECREF(str2); |
| 5801 | return result; |
| 5802 | } |
| 5803 | |
| 5804 | static |
| 5805 | PyObject *unicode_repr(PyObject *unicode) |
| 5806 | { |
| 5807 | return unicodeescape_string(PyUnicode_AS_UNICODE(unicode), |
| 5808 | PyUnicode_GET_SIZE(unicode), |
| 5809 | 1); |
| 5810 | } |
| 5811 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5812 | PyDoc_STRVAR(rfind__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5813 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 5814 | \n\ |
| 5815 | Return the highest index in S where substring sub is found,\n\ |
| 5816 | such that sub is contained within s[start,end]. Optional\n\ |
| 5817 | arguments start and end are interpreted as in slice notation.\n\ |
| 5818 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5819 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5820 | |
| 5821 | static PyObject * |
| 5822 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 5823 | { |
| 5824 | PyUnicodeObject *substring; |
| 5825 | int start = 0; |
| 5826 | int end = INT_MAX; |
| 5827 | PyObject *result; |
| 5828 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5829 | if (!PyArg_ParseTuple(args, "O|O&O&:rfind", &substring, |
| 5830 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5831 | return NULL; |
| 5832 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 5833 | (PyObject *)substring); |
| 5834 | if (substring == NULL) |
| 5835 | return NULL; |
| 5836 | |
| 5837 | result = PyInt_FromLong(findstring(self, substring, start, end, -1)); |
| 5838 | |
| 5839 | Py_DECREF(substring); |
| 5840 | return result; |
| 5841 | } |
| 5842 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5843 | PyDoc_STRVAR(rindex__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5844 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 5845 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5846 | 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] | 5847 | |
| 5848 | static PyObject * |
| 5849 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 5850 | { |
| 5851 | int result; |
| 5852 | PyUnicodeObject *substring; |
| 5853 | int start = 0; |
| 5854 | int end = INT_MAX; |
| 5855 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5856 | if (!PyArg_ParseTuple(args, "O|O&O&:rindex", &substring, |
| 5857 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5858 | return NULL; |
| 5859 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 5860 | (PyObject *)substring); |
| 5861 | if (substring == NULL) |
| 5862 | return NULL; |
| 5863 | |
| 5864 | result = findstring(self, substring, start, end, -1); |
| 5865 | |
| 5866 | Py_DECREF(substring); |
| 5867 | if (result < 0) { |
| 5868 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 5869 | return NULL; |
| 5870 | } |
| 5871 | return PyInt_FromLong(result); |
| 5872 | } |
| 5873 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5874 | PyDoc_STRVAR(rjust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5875 | "S.rjust(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5876 | \n\ |
| 5877 | 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] | 5878 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5879 | |
| 5880 | static PyObject * |
| 5881 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 5882 | { |
| 5883 | int width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5884 | Py_UNICODE fillchar = ' '; |
| 5885 | |
| 5886 | if (!PyArg_ParseTuple(args, "i|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5887 | return NULL; |
| 5888 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5889 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5890 | Py_INCREF(self); |
| 5891 | return (PyObject*) self; |
| 5892 | } |
| 5893 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5894 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5895 | } |
| 5896 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5897 | static PyObject* |
| 5898 | unicode_slice(PyUnicodeObject *self, int start, int end) |
| 5899 | { |
| 5900 | /* standard clamping */ |
| 5901 | if (start < 0) |
| 5902 | start = 0; |
| 5903 | if (end < 0) |
| 5904 | end = 0; |
| 5905 | if (end > self->length) |
| 5906 | end = self->length; |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5907 | if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5908 | /* full slice, return original string */ |
| 5909 | Py_INCREF(self); |
| 5910 | return (PyObject*) self; |
| 5911 | } |
| 5912 | if (start > end) |
| 5913 | start = end; |
| 5914 | /* copy slice */ |
| 5915 | return (PyObject*) PyUnicode_FromUnicode(self->str + start, |
| 5916 | end - start); |
| 5917 | } |
| 5918 | |
| 5919 | PyObject *PyUnicode_Split(PyObject *s, |
| 5920 | PyObject *sep, |
| 5921 | int maxsplit) |
| 5922 | { |
| 5923 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5924 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5925 | s = PyUnicode_FromObject(s); |
| 5926 | if (s == NULL) |
| 5927 | return NULL; |
| 5928 | if (sep != NULL) { |
| 5929 | sep = PyUnicode_FromObject(sep); |
| 5930 | if (sep == NULL) { |
| 5931 | Py_DECREF(s); |
| 5932 | return NULL; |
| 5933 | } |
| 5934 | } |
| 5935 | |
| 5936 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 5937 | |
| 5938 | Py_DECREF(s); |
| 5939 | Py_XDECREF(sep); |
| 5940 | return result; |
| 5941 | } |
| 5942 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5943 | PyDoc_STRVAR(split__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5944 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 5945 | \n\ |
| 5946 | Return a list of the words in S, using sep as the\n\ |
| 5947 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
| 5948 | 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] | 5949 | is a separator."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5950 | |
| 5951 | static PyObject* |
| 5952 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 5953 | { |
| 5954 | PyObject *substring = Py_None; |
| 5955 | int maxcount = -1; |
| 5956 | |
| 5957 | if (!PyArg_ParseTuple(args, "|Oi:split", &substring, &maxcount)) |
| 5958 | return NULL; |
| 5959 | |
| 5960 | if (substring == Py_None) |
| 5961 | return split(self, NULL, maxcount); |
| 5962 | else if (PyUnicode_Check(substring)) |
| 5963 | return split(self, (PyUnicodeObject *)substring, maxcount); |
| 5964 | else |
| 5965 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
| 5966 | } |
| 5967 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5968 | PyObject *PyUnicode_RSplit(PyObject *s, |
| 5969 | PyObject *sep, |
| 5970 | int maxsplit) |
| 5971 | { |
| 5972 | PyObject *result; |
| 5973 | |
| 5974 | s = PyUnicode_FromObject(s); |
| 5975 | if (s == NULL) |
| 5976 | return NULL; |
| 5977 | if (sep != NULL) { |
| 5978 | sep = PyUnicode_FromObject(sep); |
| 5979 | if (sep == NULL) { |
| 5980 | Py_DECREF(s); |
| 5981 | return NULL; |
| 5982 | } |
| 5983 | } |
| 5984 | |
| 5985 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 5986 | |
| 5987 | Py_DECREF(s); |
| 5988 | Py_XDECREF(sep); |
| 5989 | return result; |
| 5990 | } |
| 5991 | |
| 5992 | PyDoc_STRVAR(rsplit__doc__, |
| 5993 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
| 5994 | \n\ |
| 5995 | Return a list of the words in S, using sep as the\n\ |
| 5996 | delimiter string, starting at the end of the string and\n\ |
| 5997 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 5998 | splits are done. If sep is not specified, any whitespace string\n\ |
| 5999 | is a separator."); |
| 6000 | |
| 6001 | static PyObject* |
| 6002 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 6003 | { |
| 6004 | PyObject *substring = Py_None; |
| 6005 | int maxcount = -1; |
| 6006 | |
| 6007 | if (!PyArg_ParseTuple(args, "|Oi:rsplit", &substring, &maxcount)) |
| 6008 | return NULL; |
| 6009 | |
| 6010 | if (substring == Py_None) |
| 6011 | return rsplit(self, NULL, maxcount); |
| 6012 | else if (PyUnicode_Check(substring)) |
| 6013 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
| 6014 | else |
| 6015 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
| 6016 | } |
| 6017 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6018 | PyDoc_STRVAR(splitlines__doc__, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6019 | "S.splitlines([keepends]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6020 | \n\ |
| 6021 | 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] | 6022 | 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] | 6023 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6024 | |
| 6025 | static PyObject* |
| 6026 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 6027 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6028 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6029 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6030 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6031 | return NULL; |
| 6032 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6033 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6034 | } |
| 6035 | |
| 6036 | static |
| 6037 | PyObject *unicode_str(PyUnicodeObject *self) |
| 6038 | { |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6039 | return PyUnicode_AsEncodedString((PyObject *)self, NULL, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6040 | } |
| 6041 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6042 | PyDoc_STRVAR(swapcase__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6043 | "S.swapcase() -> unicode\n\ |
| 6044 | \n\ |
| 6045 | 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] | 6046 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6047 | |
| 6048 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6049 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6050 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6051 | return fixup(self, fixswapcase); |
| 6052 | } |
| 6053 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6054 | PyDoc_STRVAR(translate__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6055 | "S.translate(table) -> unicode\n\ |
| 6056 | \n\ |
| 6057 | Return a copy of the string S, where all characters have been mapped\n\ |
| 6058 | 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] | 6059 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ |
| 6060 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 6061 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6062 | |
| 6063 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6064 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6065 | { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6066 | return PyUnicode_TranslateCharmap(self->str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6067 | self->length, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6068 | table, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6069 | "ignore"); |
| 6070 | } |
| 6071 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6072 | PyDoc_STRVAR(upper__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6073 | "S.upper() -> unicode\n\ |
| 6074 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6075 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6076 | |
| 6077 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6078 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6079 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6080 | return fixup(self, fixupper); |
| 6081 | } |
| 6082 | |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 6083 | PyDoc_STRVAR(width__doc__, |
| 6084 | "S.width() -> unicode\n\ |
| 6085 | \n\ |
| 6086 | Return a fixed-width representation length of S."); |
| 6087 | |
| 6088 | static PyObject* |
| 6089 | unicode_width(PyObject *self) |
| 6090 | { |
| 6091 | int width = PyUnicode_GetWidth(self); |
| 6092 | if (width == -1) |
| 6093 | return NULL; |
| 6094 | else |
| 6095 | return PyInt_FromLong((long)width); |
| 6096 | } |
| 6097 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6098 | PyDoc_STRVAR(zfill__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6099 | "S.zfill(width) -> unicode\n\ |
| 6100 | \n\ |
| 6101 | 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] | 6102 | of the specified width. The string x is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6103 | |
| 6104 | static PyObject * |
| 6105 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 6106 | { |
| 6107 | int fill; |
| 6108 | PyUnicodeObject *u; |
| 6109 | |
| 6110 | int width; |
| 6111 | if (!PyArg_ParseTuple(args, "i:zfill", &width)) |
| 6112 | return NULL; |
| 6113 | |
| 6114 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 6115 | if (PyUnicode_CheckExact(self)) { |
| 6116 | Py_INCREF(self); |
| 6117 | return (PyObject*) self; |
| 6118 | } |
| 6119 | else |
| 6120 | return PyUnicode_FromUnicode( |
| 6121 | PyUnicode_AS_UNICODE(self), |
| 6122 | PyUnicode_GET_SIZE(self) |
| 6123 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6124 | } |
| 6125 | |
| 6126 | fill = width - self->length; |
| 6127 | |
| 6128 | u = pad(self, fill, 0, '0'); |
| 6129 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 6130 | if (u == NULL) |
| 6131 | return NULL; |
| 6132 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6133 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 6134 | /* move sign to beginning of string */ |
| 6135 | u->str[0] = u->str[fill]; |
| 6136 | u->str[fill] = '0'; |
| 6137 | } |
| 6138 | |
| 6139 | return (PyObject*) u; |
| 6140 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6141 | |
| 6142 | #if 0 |
| 6143 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6144 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6145 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6146 | return PyInt_FromLong(unicode_freelist_size); |
| 6147 | } |
| 6148 | #endif |
| 6149 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6150 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6151 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6152 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 6153 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 6154 | With optional start, test S beginning at that position.\n\ |
| 6155 | With optional end, stop comparing S at that position."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6156 | |
| 6157 | static PyObject * |
| 6158 | unicode_startswith(PyUnicodeObject *self, |
| 6159 | PyObject *args) |
| 6160 | { |
| 6161 | PyUnicodeObject *substring; |
| 6162 | int start = 0; |
| 6163 | int end = INT_MAX; |
| 6164 | PyObject *result; |
| 6165 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6166 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &substring, |
| 6167 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6168 | return NULL; |
| 6169 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6170 | (PyObject *)substring); |
| 6171 | if (substring == NULL) |
| 6172 | return NULL; |
| 6173 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6174 | result = PyBool_FromLong(tailmatch(self, substring, start, end, -1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6175 | |
| 6176 | Py_DECREF(substring); |
| 6177 | return result; |
| 6178 | } |
| 6179 | |
| 6180 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6181 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6182 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6183 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 6184 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 6185 | With optional start, test S beginning at that position.\n\ |
| 6186 | With optional end, stop comparing S at that position."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6187 | |
| 6188 | static PyObject * |
| 6189 | unicode_endswith(PyUnicodeObject *self, |
| 6190 | PyObject *args) |
| 6191 | { |
| 6192 | PyUnicodeObject *substring; |
| 6193 | int start = 0; |
| 6194 | int end = INT_MAX; |
| 6195 | PyObject *result; |
| 6196 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6197 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &substring, |
| 6198 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6199 | return NULL; |
| 6200 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6201 | (PyObject *)substring); |
| 6202 | if (substring == NULL) |
| 6203 | return NULL; |
| 6204 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6205 | result = PyBool_FromLong(tailmatch(self, substring, start, end, +1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6206 | |
| 6207 | Py_DECREF(substring); |
| 6208 | return result; |
| 6209 | } |
| 6210 | |
| 6211 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 6212 | |
| 6213 | static PyObject * |
| 6214 | unicode_getnewargs(PyUnicodeObject *v) |
| 6215 | { |
| 6216 | return Py_BuildValue("(u#)", v->str, v->length); |
| 6217 | } |
| 6218 | |
| 6219 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6220 | static PyMethodDef unicode_methods[] = { |
| 6221 | |
| 6222 | /* Order is according to common usage: often used methods should |
| 6223 | appear first, since lookup is done sequentially. */ |
| 6224 | |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6225 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
| 6226 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 6227 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6228 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6229 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 6230 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 6231 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 6232 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 6233 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 6234 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 6235 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
| 6236 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 6237 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 6238 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6239 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6240 | {"decode", (PyCFunction) unicode_decode, METH_VARARGS, decode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6241 | /* {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */ |
| 6242 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 6243 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 6244 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6245 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6246 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6247 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6248 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 6249 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 6250 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 6251 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 6252 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 6253 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 6254 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 6255 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 6256 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 6257 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 6258 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 6259 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 6260 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 6261 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 6262 | {"iswide", (PyCFunction) unicode_iswide, METH_NOARGS, iswide__doc__}, |
| 6263 | {"width", (PyCFunction) unicode_width, METH_NOARGS, width__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6264 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 6265 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6266 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6267 | #endif |
| 6268 | |
| 6269 | #if 0 |
| 6270 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6271 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6272 | #endif |
| 6273 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 6274 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6275 | {NULL, NULL} |
| 6276 | }; |
| 6277 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 6278 | static PyObject * |
| 6279 | unicode_mod(PyObject *v, PyObject *w) |
| 6280 | { |
| 6281 | if (!PyUnicode_Check(v)) { |
| 6282 | Py_INCREF(Py_NotImplemented); |
| 6283 | return Py_NotImplemented; |
| 6284 | } |
| 6285 | return PyUnicode_Format(v, w); |
| 6286 | } |
| 6287 | |
| 6288 | static PyNumberMethods unicode_as_number = { |
| 6289 | 0, /*nb_add*/ |
| 6290 | 0, /*nb_subtract*/ |
| 6291 | 0, /*nb_multiply*/ |
| 6292 | 0, /*nb_divide*/ |
| 6293 | unicode_mod, /*nb_remainder*/ |
| 6294 | }; |
| 6295 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6296 | static PySequenceMethods unicode_as_sequence = { |
| 6297 | (inquiry) unicode_length, /* sq_length */ |
| 6298 | (binaryfunc) PyUnicode_Concat, /* sq_concat */ |
| 6299 | (intargfunc) unicode_repeat, /* sq_repeat */ |
| 6300 | (intargfunc) unicode_getitem, /* sq_item */ |
| 6301 | (intintargfunc) unicode_slice, /* sq_slice */ |
| 6302 | 0, /* sq_ass_item */ |
| 6303 | 0, /* sq_ass_slice */ |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6304 | (objobjproc)PyUnicode_Contains, /*sq_contains*/ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6305 | }; |
| 6306 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6307 | static PyObject* |
| 6308 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 6309 | { |
| 6310 | if (PyInt_Check(item)) { |
| 6311 | long i = PyInt_AS_LONG(item); |
| 6312 | if (i < 0) |
| 6313 | i += PyString_GET_SIZE(self); |
| 6314 | return unicode_getitem(self, i); |
| 6315 | } else if (PyLong_Check(item)) { |
| 6316 | long i = PyLong_AsLong(item); |
| 6317 | if (i == -1 && PyErr_Occurred()) |
| 6318 | return NULL; |
| 6319 | if (i < 0) |
| 6320 | i += PyString_GET_SIZE(self); |
| 6321 | return unicode_getitem(self, i); |
| 6322 | } else if (PySlice_Check(item)) { |
| 6323 | int start, stop, step, slicelength, cur, i; |
| 6324 | Py_UNICODE* source_buf; |
| 6325 | Py_UNICODE* result_buf; |
| 6326 | PyObject* result; |
| 6327 | |
| 6328 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyString_GET_SIZE(self), |
| 6329 | &start, &stop, &step, &slicelength) < 0) { |
| 6330 | return NULL; |
| 6331 | } |
| 6332 | |
| 6333 | if (slicelength <= 0) { |
| 6334 | return PyUnicode_FromUnicode(NULL, 0); |
| 6335 | } else { |
| 6336 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
| 6337 | result_buf = PyMem_MALLOC(slicelength*sizeof(Py_UNICODE)); |
| 6338 | |
| 6339 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 6340 | result_buf[i] = source_buf[cur]; |
| 6341 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6342 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6343 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
| 6344 | PyMem_FREE(result_buf); |
| 6345 | return result; |
| 6346 | } |
| 6347 | } else { |
| 6348 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 6349 | return NULL; |
| 6350 | } |
| 6351 | } |
| 6352 | |
| 6353 | static PyMappingMethods unicode_as_mapping = { |
| 6354 | (inquiry)unicode_length, /* mp_length */ |
| 6355 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 6356 | (objobjargproc)0, /* mp_ass_subscript */ |
| 6357 | }; |
| 6358 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6359 | static int |
| 6360 | unicode_buffer_getreadbuf(PyUnicodeObject *self, |
| 6361 | int index, |
| 6362 | const void **ptr) |
| 6363 | { |
| 6364 | if (index != 0) { |
| 6365 | PyErr_SetString(PyExc_SystemError, |
| 6366 | "accessing non-existent unicode segment"); |
| 6367 | return -1; |
| 6368 | } |
| 6369 | *ptr = (void *) self->str; |
| 6370 | return PyUnicode_GET_DATA_SIZE(self); |
| 6371 | } |
| 6372 | |
| 6373 | static int |
| 6374 | unicode_buffer_getwritebuf(PyUnicodeObject *self, int index, |
| 6375 | const void **ptr) |
| 6376 | { |
| 6377 | PyErr_SetString(PyExc_TypeError, |
Neal Norwitz | 20e7213 | 2002-06-13 21:25:17 +0000 | [diff] [blame] | 6378 | "cannot use unicode as modifiable buffer"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6379 | return -1; |
| 6380 | } |
| 6381 | |
| 6382 | static int |
| 6383 | unicode_buffer_getsegcount(PyUnicodeObject *self, |
| 6384 | int *lenp) |
| 6385 | { |
| 6386 | if (lenp) |
| 6387 | *lenp = PyUnicode_GET_DATA_SIZE(self); |
| 6388 | return 1; |
| 6389 | } |
| 6390 | |
| 6391 | static int |
| 6392 | unicode_buffer_getcharbuf(PyUnicodeObject *self, |
| 6393 | int index, |
| 6394 | const void **ptr) |
| 6395 | { |
| 6396 | PyObject *str; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6397 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6398 | if (index != 0) { |
| 6399 | PyErr_SetString(PyExc_SystemError, |
| 6400 | "accessing non-existent unicode segment"); |
| 6401 | return -1; |
| 6402 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 6403 | str = _PyUnicode_AsDefaultEncodedString((PyObject *)self, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6404 | if (str == NULL) |
| 6405 | return -1; |
| 6406 | *ptr = (void *) PyString_AS_STRING(str); |
| 6407 | return PyString_GET_SIZE(str); |
| 6408 | } |
| 6409 | |
| 6410 | /* Helpers for PyUnicode_Format() */ |
| 6411 | |
| 6412 | static PyObject * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 6413 | getnextarg(PyObject *args, int arglen, int *p_argidx) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6414 | { |
| 6415 | int argidx = *p_argidx; |
| 6416 | if (argidx < arglen) { |
| 6417 | (*p_argidx)++; |
| 6418 | if (arglen < 0) |
| 6419 | return args; |
| 6420 | else |
| 6421 | return PyTuple_GetItem(args, argidx); |
| 6422 | } |
| 6423 | PyErr_SetString(PyExc_TypeError, |
| 6424 | "not enough arguments for format string"); |
| 6425 | return NULL; |
| 6426 | } |
| 6427 | |
| 6428 | #define F_LJUST (1<<0) |
| 6429 | #define F_SIGN (1<<1) |
| 6430 | #define F_BLANK (1<<2) |
| 6431 | #define F_ALT (1<<3) |
| 6432 | #define F_ZERO (1<<4) |
| 6433 | |
| 6434 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6435 | int usprintf(register Py_UNICODE *buffer, char *format, ...) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6436 | { |
| 6437 | register int i; |
| 6438 | int len; |
| 6439 | va_list va; |
| 6440 | char *charbuffer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6441 | va_start(va, format); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6442 | |
| 6443 | /* First, format the string as char array, then expand to Py_UNICODE |
| 6444 | array. */ |
| 6445 | charbuffer = (char *)buffer; |
| 6446 | len = vsprintf(charbuffer, format, va); |
| 6447 | for (i = len - 1; i >= 0; i--) |
| 6448 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
| 6449 | |
| 6450 | va_end(va); |
| 6451 | return len; |
| 6452 | } |
| 6453 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 6454 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 6455 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 6456 | formatting is done. */ |
| 6457 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6458 | static int |
| 6459 | formatfloat(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6460 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6461 | int flags, |
| 6462 | int prec, |
| 6463 | int type, |
| 6464 | PyObject *v) |
| 6465 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6466 | /* fmt = '%#.' + `prec` + `type` |
| 6467 | 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] | 6468 | char fmt[20]; |
| 6469 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6470 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6471 | x = PyFloat_AsDouble(v); |
| 6472 | if (x == -1.0 && PyErr_Occurred()) |
| 6473 | return -1; |
| 6474 | if (prec < 0) |
| 6475 | prec = 6; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6476 | if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
| 6477 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6478 | /* Worst case length calc to ensure no buffer overrun: |
| 6479 | |
| 6480 | 'g' formats: |
| 6481 | fmt = %#.<prec>g |
| 6482 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 6483 | for any double rep.) |
| 6484 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 6485 | |
| 6486 | 'f' formats: |
| 6487 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 6488 | len = 1 + 50 + 1 + prec = 52 + prec |
| 6489 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6490 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6491 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6492 | |
| 6493 | */ |
| 6494 | if ((type == 'g' && buflen <= (size_t)10 + (size_t)prec) || |
| 6495 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6496 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6497 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6498 | return -1; |
| 6499 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6500 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 6501 | (flags&F_ALT) ? "#" : "", |
| 6502 | prec, type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6503 | return usprintf(buf, fmt, x); |
| 6504 | } |
| 6505 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6506 | static PyObject* |
| 6507 | formatlong(PyObject *val, int flags, int prec, int type) |
| 6508 | { |
| 6509 | char *buf; |
| 6510 | int i, len; |
| 6511 | PyObject *str; /* temporary string object. */ |
| 6512 | PyUnicodeObject *result; |
| 6513 | |
| 6514 | str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
| 6515 | if (!str) |
| 6516 | return NULL; |
| 6517 | result = _PyUnicode_New(len); |
| 6518 | for (i = 0; i < len; i++) |
| 6519 | result->str[i] = buf[i]; |
| 6520 | result->str[len] = 0; |
| 6521 | Py_DECREF(str); |
| 6522 | return (PyObject*)result; |
| 6523 | } |
| 6524 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6525 | static int |
| 6526 | formatint(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6527 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6528 | int flags, |
| 6529 | int prec, |
| 6530 | int type, |
| 6531 | PyObject *v) |
| 6532 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6533 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6534 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 6535 | * + 1 + 1 |
| 6536 | * = 24 |
| 6537 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6538 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6539 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6540 | long x; |
| 6541 | |
| 6542 | x = PyInt_AsLong(v); |
| 6543 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6544 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6545 | if (x < 0 && type == 'u') { |
| 6546 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 6547 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6548 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 6549 | sign = "-"; |
| 6550 | else |
| 6551 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6552 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6553 | prec = 1; |
| 6554 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6555 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 6556 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6557 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6558 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6559 | PyErr_SetString(PyExc_OverflowError, |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6560 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6561 | return -1; |
| 6562 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6563 | |
| 6564 | if ((flags & F_ALT) && |
| 6565 | (type == 'x' || type == 'X')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6566 | /* When converting under %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6567 | * of issues that cause pain: |
| 6568 | * - when 0 is being converted, the C standard leaves off |
| 6569 | * the '0x' or '0X', which is inconsistent with other |
| 6570 | * %#x/%#X conversions and inconsistent with Python's |
| 6571 | * hex() function |
| 6572 | * - there are platforms that violate the standard and |
| 6573 | * convert 0 with the '0x' or '0X' |
| 6574 | * (Metrowerks, Compaq Tru64) |
| 6575 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6576 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6577 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6578 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6579 | * We can achieve the desired consistency by inserting our |
| 6580 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 6581 | * of %#x/%#X. |
| 6582 | * |
| 6583 | * Note that this is the same approach as used in |
| 6584 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 6585 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6586 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 6587 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 6588 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6589 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6590 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 6591 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6592 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 6593 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6594 | if (sign[0]) |
| 6595 | return usprintf(buf, fmt, -x); |
| 6596 | else |
| 6597 | return usprintf(buf, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6598 | } |
| 6599 | |
| 6600 | static int |
| 6601 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6602 | size_t buflen, |
| 6603 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6604 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6605 | /* presume that the buffer is at least 2 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6606 | if (PyUnicode_Check(v)) { |
| 6607 | if (PyUnicode_GET_SIZE(v) != 1) |
| 6608 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6609 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6610 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6611 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6612 | else if (PyString_Check(v)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6613 | if (PyString_GET_SIZE(v) != 1) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6614 | goto onError; |
| 6615 | buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0]; |
| 6616 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6617 | |
| 6618 | else { |
| 6619 | /* Integer input truncated to a character */ |
| 6620 | long x; |
| 6621 | x = PyInt_AsLong(v); |
| 6622 | if (x == -1 && PyErr_Occurred()) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6623 | goto onError; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 6624 | #ifdef Py_UNICODE_WIDE |
| 6625 | if (x < 0 || x > 0x10ffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 6626 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 6627 | "%c arg not in range(0x110000) " |
| 6628 | "(wide Python build)"); |
| 6629 | return -1; |
| 6630 | } |
| 6631 | #else |
| 6632 | if (x < 0 || x > 0xffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 6633 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 6634 | "%c arg not in range(0x10000) " |
| 6635 | "(narrow Python build)"); |
| 6636 | return -1; |
| 6637 | } |
| 6638 | #endif |
| 6639 | buf[0] = (Py_UNICODE) x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6640 | } |
| 6641 | buf[1] = '\0'; |
| 6642 | return 1; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6643 | |
| 6644 | onError: |
| 6645 | PyErr_SetString(PyExc_TypeError, |
| 6646 | "%c requires int or char"); |
| 6647 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6648 | } |
| 6649 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6650 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 6651 | |
| 6652 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 6653 | chars are formatted. XXX This is a magic number. Each formatting |
| 6654 | routine does bounds checking to ensure no overflow, but a better |
| 6655 | solution may be to malloc a buffer of appropriate size for each |
| 6656 | format. For now, the current solution is sufficient. |
| 6657 | */ |
| 6658 | #define FORMATBUFLEN (size_t)120 |
| 6659 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6660 | PyObject *PyUnicode_Format(PyObject *format, |
| 6661 | PyObject *args) |
| 6662 | { |
| 6663 | Py_UNICODE *fmt, *res; |
| 6664 | int fmtcnt, rescnt, reslen, arglen, argidx; |
| 6665 | int args_owned = 0; |
| 6666 | PyUnicodeObject *result = NULL; |
| 6667 | PyObject *dict = NULL; |
| 6668 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6669 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6670 | if (format == NULL || args == NULL) { |
| 6671 | PyErr_BadInternalCall(); |
| 6672 | return NULL; |
| 6673 | } |
| 6674 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6675 | if (uformat == NULL) |
| 6676 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6677 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 6678 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 6679 | |
| 6680 | reslen = rescnt = fmtcnt + 100; |
| 6681 | result = _PyUnicode_New(reslen); |
| 6682 | if (result == NULL) |
| 6683 | goto onError; |
| 6684 | res = PyUnicode_AS_UNICODE(result); |
| 6685 | |
| 6686 | if (PyTuple_Check(args)) { |
| 6687 | arglen = PyTuple_Size(args); |
| 6688 | argidx = 0; |
| 6689 | } |
| 6690 | else { |
| 6691 | arglen = -1; |
| 6692 | argidx = -2; |
| 6693 | } |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 6694 | if (args->ob_type->tp_as_mapping && !PyTuple_Check(args) && |
| 6695 | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6696 | dict = args; |
| 6697 | |
| 6698 | while (--fmtcnt >= 0) { |
| 6699 | if (*fmt != '%') { |
| 6700 | if (--rescnt < 0) { |
| 6701 | rescnt = fmtcnt + 100; |
| 6702 | reslen += rescnt; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6703 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6704 | return NULL; |
| 6705 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 6706 | --rescnt; |
| 6707 | } |
| 6708 | *res++ = *fmt++; |
| 6709 | } |
| 6710 | else { |
| 6711 | /* Got a format specifier */ |
| 6712 | int flags = 0; |
| 6713 | int width = -1; |
| 6714 | int prec = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6715 | Py_UNICODE c = '\0'; |
| 6716 | Py_UNICODE fill; |
| 6717 | PyObject *v = NULL; |
| 6718 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6719 | Py_UNICODE *pbuf; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6720 | Py_UNICODE sign; |
| 6721 | int len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6722 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6723 | |
| 6724 | fmt++; |
| 6725 | if (*fmt == '(') { |
| 6726 | Py_UNICODE *keystart; |
| 6727 | int keylen; |
| 6728 | PyObject *key; |
| 6729 | int pcount = 1; |
| 6730 | |
| 6731 | if (dict == NULL) { |
| 6732 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6733 | "format requires a mapping"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6734 | goto onError; |
| 6735 | } |
| 6736 | ++fmt; |
| 6737 | --fmtcnt; |
| 6738 | keystart = fmt; |
| 6739 | /* Skip over balanced parentheses */ |
| 6740 | while (pcount > 0 && --fmtcnt >= 0) { |
| 6741 | if (*fmt == ')') |
| 6742 | --pcount; |
| 6743 | else if (*fmt == '(') |
| 6744 | ++pcount; |
| 6745 | fmt++; |
| 6746 | } |
| 6747 | keylen = fmt - keystart - 1; |
| 6748 | if (fmtcnt < 0 || pcount > 0) { |
| 6749 | PyErr_SetString(PyExc_ValueError, |
| 6750 | "incomplete format key"); |
| 6751 | goto onError; |
| 6752 | } |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 6753 | #if 0 |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6754 | /* keys are converted to strings using UTF-8 and |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6755 | then looked up since Python uses strings to hold |
| 6756 | variables names etc. in its namespaces and we |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6757 | wouldn't want to break common idioms. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6758 | key = PyUnicode_EncodeUTF8(keystart, |
| 6759 | keylen, |
| 6760 | NULL); |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 6761 | #else |
| 6762 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 6763 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6764 | if (key == NULL) |
| 6765 | goto onError; |
| 6766 | if (args_owned) { |
| 6767 | Py_DECREF(args); |
| 6768 | args_owned = 0; |
| 6769 | } |
| 6770 | args = PyObject_GetItem(dict, key); |
| 6771 | Py_DECREF(key); |
| 6772 | if (args == NULL) { |
| 6773 | goto onError; |
| 6774 | } |
| 6775 | args_owned = 1; |
| 6776 | arglen = -1; |
| 6777 | argidx = -2; |
| 6778 | } |
| 6779 | while (--fmtcnt >= 0) { |
| 6780 | switch (c = *fmt++) { |
| 6781 | case '-': flags |= F_LJUST; continue; |
| 6782 | case '+': flags |= F_SIGN; continue; |
| 6783 | case ' ': flags |= F_BLANK; continue; |
| 6784 | case '#': flags |= F_ALT; continue; |
| 6785 | case '0': flags |= F_ZERO; continue; |
| 6786 | } |
| 6787 | break; |
| 6788 | } |
| 6789 | if (c == '*') { |
| 6790 | v = getnextarg(args, arglen, &argidx); |
| 6791 | if (v == NULL) |
| 6792 | goto onError; |
| 6793 | if (!PyInt_Check(v)) { |
| 6794 | PyErr_SetString(PyExc_TypeError, |
| 6795 | "* wants int"); |
| 6796 | goto onError; |
| 6797 | } |
| 6798 | width = PyInt_AsLong(v); |
| 6799 | if (width < 0) { |
| 6800 | flags |= F_LJUST; |
| 6801 | width = -width; |
| 6802 | } |
| 6803 | if (--fmtcnt >= 0) |
| 6804 | c = *fmt++; |
| 6805 | } |
| 6806 | else if (c >= '0' && c <= '9') { |
| 6807 | width = c - '0'; |
| 6808 | while (--fmtcnt >= 0) { |
| 6809 | c = *fmt++; |
| 6810 | if (c < '0' || c > '9') |
| 6811 | break; |
| 6812 | if ((width*10) / 10 != width) { |
| 6813 | PyErr_SetString(PyExc_ValueError, |
| 6814 | "width too big"); |
| 6815 | goto onError; |
| 6816 | } |
| 6817 | width = width*10 + (c - '0'); |
| 6818 | } |
| 6819 | } |
| 6820 | if (c == '.') { |
| 6821 | prec = 0; |
| 6822 | if (--fmtcnt >= 0) |
| 6823 | c = *fmt++; |
| 6824 | if (c == '*') { |
| 6825 | v = getnextarg(args, arglen, &argidx); |
| 6826 | if (v == NULL) |
| 6827 | goto onError; |
| 6828 | if (!PyInt_Check(v)) { |
| 6829 | PyErr_SetString(PyExc_TypeError, |
| 6830 | "* wants int"); |
| 6831 | goto onError; |
| 6832 | } |
| 6833 | prec = PyInt_AsLong(v); |
| 6834 | if (prec < 0) |
| 6835 | prec = 0; |
| 6836 | if (--fmtcnt >= 0) |
| 6837 | c = *fmt++; |
| 6838 | } |
| 6839 | else if (c >= '0' && c <= '9') { |
| 6840 | prec = c - '0'; |
| 6841 | while (--fmtcnt >= 0) { |
| 6842 | c = Py_CHARMASK(*fmt++); |
| 6843 | if (c < '0' || c > '9') |
| 6844 | break; |
| 6845 | if ((prec*10) / 10 != prec) { |
| 6846 | PyErr_SetString(PyExc_ValueError, |
| 6847 | "prec too big"); |
| 6848 | goto onError; |
| 6849 | } |
| 6850 | prec = prec*10 + (c - '0'); |
| 6851 | } |
| 6852 | } |
| 6853 | } /* prec */ |
| 6854 | if (fmtcnt >= 0) { |
| 6855 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6856 | if (--fmtcnt >= 0) |
| 6857 | c = *fmt++; |
| 6858 | } |
| 6859 | } |
| 6860 | if (fmtcnt < 0) { |
| 6861 | PyErr_SetString(PyExc_ValueError, |
| 6862 | "incomplete format"); |
| 6863 | goto onError; |
| 6864 | } |
| 6865 | if (c != '%') { |
| 6866 | v = getnextarg(args, arglen, &argidx); |
| 6867 | if (v == NULL) |
| 6868 | goto onError; |
| 6869 | } |
| 6870 | sign = 0; |
| 6871 | fill = ' '; |
| 6872 | switch (c) { |
| 6873 | |
| 6874 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6875 | pbuf = formatbuf; |
| 6876 | /* presume that buffer length is at least 1 */ |
| 6877 | pbuf[0] = '%'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6878 | len = 1; |
| 6879 | break; |
| 6880 | |
| 6881 | case 's': |
| 6882 | case 'r': |
| 6883 | if (PyUnicode_Check(v) && c == 's') { |
| 6884 | temp = v; |
| 6885 | Py_INCREF(temp); |
| 6886 | } |
| 6887 | else { |
| 6888 | PyObject *unicode; |
| 6889 | if (c == 's') |
| 6890 | temp = PyObject_Str(v); |
| 6891 | else |
| 6892 | temp = PyObject_Repr(v); |
| 6893 | if (temp == NULL) |
| 6894 | goto onError; |
| 6895 | if (!PyString_Check(temp)) { |
| 6896 | /* XXX Note: this should never happen, since |
| 6897 | PyObject_Repr() and PyObject_Str() assure |
| 6898 | this */ |
| 6899 | Py_DECREF(temp); |
| 6900 | PyErr_SetString(PyExc_TypeError, |
| 6901 | "%s argument has non-string str()"); |
| 6902 | goto onError; |
| 6903 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6904 | unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6905 | PyString_GET_SIZE(temp), |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6906 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6907 | "strict"); |
| 6908 | Py_DECREF(temp); |
| 6909 | temp = unicode; |
| 6910 | if (temp == NULL) |
| 6911 | goto onError; |
| 6912 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6913 | pbuf = PyUnicode_AS_UNICODE(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6914 | len = PyUnicode_GET_SIZE(temp); |
| 6915 | if (prec >= 0 && len > prec) |
| 6916 | len = prec; |
| 6917 | break; |
| 6918 | |
| 6919 | case 'i': |
| 6920 | case 'd': |
| 6921 | case 'u': |
| 6922 | case 'o': |
| 6923 | case 'x': |
| 6924 | case 'X': |
| 6925 | if (c == 'i') |
| 6926 | c = 'd'; |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 6927 | if (PyLong_Check(v)) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6928 | temp = formatlong(v, flags, prec, c); |
| 6929 | if (!temp) |
| 6930 | goto onError; |
| 6931 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 6932 | len = PyUnicode_GET_SIZE(temp); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6933 | sign = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6934 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6935 | else { |
| 6936 | pbuf = formatbuf; |
| 6937 | len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 6938 | flags, prec, c, v); |
| 6939 | if (len < 0) |
| 6940 | goto onError; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6941 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6942 | } |
| 6943 | if (flags & F_ZERO) |
| 6944 | fill = '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6945 | break; |
| 6946 | |
| 6947 | case 'e': |
| 6948 | case 'E': |
| 6949 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 6950 | case 'F': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6951 | case 'g': |
| 6952 | case 'G': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 6953 | if (c == 'F') |
| 6954 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6955 | pbuf = formatbuf; |
| 6956 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 6957 | flags, prec, c, v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6958 | if (len < 0) |
| 6959 | goto onError; |
| 6960 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6961 | if (flags & F_ZERO) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6962 | fill = '0'; |
| 6963 | break; |
| 6964 | |
| 6965 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6966 | pbuf = formatbuf; |
| 6967 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6968 | if (len < 0) |
| 6969 | goto onError; |
| 6970 | break; |
| 6971 | |
| 6972 | default: |
| 6973 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 6974 | "unsupported format character '%c' (0x%x) " |
| 6975 | "at index %i", |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6976 | (31<=c && c<=126) ? (char)c : '?', |
Marc-André Lemburg | 24e53b6 | 2002-09-24 09:32:14 +0000 | [diff] [blame] | 6977 | (int)c, |
Guido van Rossum | efc1188 | 2002-09-12 14:43:41 +0000 | [diff] [blame] | 6978 | (int)(fmt -1 - PyUnicode_AS_UNICODE(uformat))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6979 | goto onError; |
| 6980 | } |
| 6981 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6982 | if (*pbuf == '-' || *pbuf == '+') { |
| 6983 | sign = *pbuf++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6984 | len--; |
| 6985 | } |
| 6986 | else if (flags & F_SIGN) |
| 6987 | sign = '+'; |
| 6988 | else if (flags & F_BLANK) |
| 6989 | sign = ' '; |
| 6990 | else |
| 6991 | sign = 0; |
| 6992 | } |
| 6993 | if (width < len) |
| 6994 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 6995 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6996 | reslen -= rescnt; |
| 6997 | rescnt = width + fmtcnt + 100; |
| 6998 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 6999 | if (reslen < 0) { |
| 7000 | Py_DECREF(result); |
| 7001 | return PyErr_NoMemory(); |
| 7002 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7003 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7004 | return NULL; |
| 7005 | res = PyUnicode_AS_UNICODE(result) |
| 7006 | + reslen - rescnt; |
| 7007 | } |
| 7008 | if (sign) { |
| 7009 | if (fill != ' ') |
| 7010 | *res++ = sign; |
| 7011 | rescnt--; |
| 7012 | if (width > len) |
| 7013 | width--; |
| 7014 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7015 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 7016 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7017 | assert(pbuf[1] == c); |
| 7018 | if (fill != ' ') { |
| 7019 | *res++ = *pbuf++; |
| 7020 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7021 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7022 | rescnt -= 2; |
| 7023 | width -= 2; |
| 7024 | if (width < 0) |
| 7025 | width = 0; |
| 7026 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7027 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7028 | if (width > len && !(flags & F_LJUST)) { |
| 7029 | do { |
| 7030 | --rescnt; |
| 7031 | *res++ = fill; |
| 7032 | } while (--width > len); |
| 7033 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7034 | if (fill == ' ') { |
| 7035 | if (sign) |
| 7036 | *res++ = sign; |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7037 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7038 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7039 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7040 | *res++ = *pbuf++; |
| 7041 | *res++ = *pbuf++; |
| 7042 | } |
| 7043 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7044 | Py_UNICODE_COPY(res, pbuf, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7045 | res += len; |
| 7046 | rescnt -= len; |
| 7047 | while (--width >= len) { |
| 7048 | --rescnt; |
| 7049 | *res++ = ' '; |
| 7050 | } |
| 7051 | if (dict && (argidx < arglen) && c != '%') { |
| 7052 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 7053 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7054 | goto onError; |
| 7055 | } |
| 7056 | Py_XDECREF(temp); |
| 7057 | } /* '%' */ |
| 7058 | } /* until end */ |
| 7059 | if (argidx < arglen && !dict) { |
| 7060 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 7061 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7062 | goto onError; |
| 7063 | } |
| 7064 | |
| 7065 | if (args_owned) { |
| 7066 | Py_DECREF(args); |
| 7067 | } |
| 7068 | Py_DECREF(uformat); |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 7069 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 7070 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7071 | return (PyObject *)result; |
| 7072 | |
| 7073 | onError: |
| 7074 | Py_XDECREF(result); |
| 7075 | Py_DECREF(uformat); |
| 7076 | if (args_owned) { |
| 7077 | Py_DECREF(args); |
| 7078 | } |
| 7079 | return NULL; |
| 7080 | } |
| 7081 | |
| 7082 | static PyBufferProcs unicode_as_buffer = { |
| 7083 | (getreadbufferproc) unicode_buffer_getreadbuf, |
| 7084 | (getwritebufferproc) unicode_buffer_getwritebuf, |
| 7085 | (getsegcountproc) unicode_buffer_getsegcount, |
| 7086 | (getcharbufferproc) unicode_buffer_getcharbuf, |
| 7087 | }; |
| 7088 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 7089 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7090 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 7091 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7092 | static PyObject * |
| 7093 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 7094 | { |
| 7095 | PyObject *x = NULL; |
| 7096 | static char *kwlist[] = {"string", "encoding", "errors", 0}; |
| 7097 | char *encoding = NULL; |
| 7098 | char *errors = NULL; |
| 7099 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7100 | if (type != &PyUnicode_Type) |
| 7101 | return unicode_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7102 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
| 7103 | kwlist, &x, &encoding, &errors)) |
| 7104 | return NULL; |
| 7105 | if (x == NULL) |
| 7106 | return (PyObject *)_PyUnicode_New(0); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 7107 | if (encoding == NULL && errors == NULL) |
| 7108 | return PyObject_Unicode(x); |
| 7109 | else |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7110 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
| 7111 | } |
| 7112 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7113 | static PyObject * |
| 7114 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 7115 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7116 | PyUnicodeObject *tmp, *pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7117 | int n; |
| 7118 | |
| 7119 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 7120 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 7121 | if (tmp == NULL) |
| 7122 | return NULL; |
| 7123 | assert(PyUnicode_Check(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7124 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7125 | if (pnew == NULL) { |
| 7126 | Py_DECREF(tmp); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7127 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7128 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7129 | pnew->str = PyMem_NEW(Py_UNICODE, n+1); |
| 7130 | if (pnew->str == NULL) { |
| 7131 | _Py_ForgetReference((PyObject *)pnew); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 7132 | PyObject_Del(pnew); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7133 | Py_DECREF(tmp); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 7134 | return PyErr_NoMemory(); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7135 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7136 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 7137 | pnew->length = n; |
| 7138 | pnew->hash = tmp->hash; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7139 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7140 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7141 | } |
| 7142 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7143 | PyDoc_STRVAR(unicode_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7144 | "unicode(string [, encoding[, errors]]) -> object\n\ |
| 7145 | \n\ |
| 7146 | Create a new Unicode object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 7147 | encoding defaults to the current default string encoding.\n\ |
| 7148 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7149 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7150 | PyTypeObject PyUnicode_Type = { |
| 7151 | PyObject_HEAD_INIT(&PyType_Type) |
| 7152 | 0, /* ob_size */ |
| 7153 | "unicode", /* tp_name */ |
| 7154 | sizeof(PyUnicodeObject), /* tp_size */ |
| 7155 | 0, /* tp_itemsize */ |
| 7156 | /* Slots */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 7157 | (destructor)unicode_dealloc, /* tp_dealloc */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7158 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7159 | 0, /* tp_getattr */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7160 | 0, /* tp_setattr */ |
| 7161 | (cmpfunc) unicode_compare, /* tp_compare */ |
| 7162 | (reprfunc) unicode_repr, /* tp_repr */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7163 | &unicode_as_number, /* tp_as_number */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7164 | &unicode_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7165 | &unicode_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7166 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 7167 | 0, /* tp_call*/ |
| 7168 | (reprfunc) unicode_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7169 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 7170 | 0, /* tp_setattro */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7171 | &unicode_as_buffer, /* tp_as_buffer */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7172 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
| 7173 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7174 | unicode_doc, /* tp_doc */ |
| 7175 | 0, /* tp_traverse */ |
| 7176 | 0, /* tp_clear */ |
| 7177 | 0, /* tp_richcompare */ |
| 7178 | 0, /* tp_weaklistoffset */ |
| 7179 | 0, /* tp_iter */ |
| 7180 | 0, /* tp_iternext */ |
| 7181 | unicode_methods, /* tp_methods */ |
| 7182 | 0, /* tp_members */ |
| 7183 | 0, /* tp_getset */ |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 7184 | &PyBaseString_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7185 | 0, /* tp_dict */ |
| 7186 | 0, /* tp_descr_get */ |
| 7187 | 0, /* tp_descr_set */ |
| 7188 | 0, /* tp_dictoffset */ |
| 7189 | 0, /* tp_init */ |
| 7190 | 0, /* tp_alloc */ |
| 7191 | unicode_new, /* tp_new */ |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 7192 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7193 | }; |
| 7194 | |
| 7195 | /* Initialize the Unicode implementation */ |
| 7196 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 7197 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7198 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7199 | int i; |
| 7200 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7201 | /* Init the implementation */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7202 | unicode_freelist = NULL; |
| 7203 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7204 | unicode_empty = _PyUnicode_New(0); |
Marc-André Lemburg | 90e8147 | 2000-06-07 09:13:21 +0000 | [diff] [blame] | 7205 | strcpy(unicode_default_encoding, "ascii"); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7206 | for (i = 0; i < 256; i++) |
| 7207 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 7208 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 7209 | Py_FatalError("Can't initialize 'unicode'"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7210 | } |
| 7211 | |
| 7212 | /* Finalize the Unicode implementation */ |
| 7213 | |
| 7214 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 7215 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7216 | { |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 7217 | PyUnicodeObject *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7218 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7219 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 7220 | Py_XDECREF(unicode_empty); |
| 7221 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 7222 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7223 | for (i = 0; i < 256; i++) { |
| 7224 | if (unicode_latin1[i]) { |
| 7225 | Py_DECREF(unicode_latin1[i]); |
| 7226 | unicode_latin1[i] = NULL; |
| 7227 | } |
| 7228 | } |
| 7229 | |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 7230 | for (u = unicode_freelist; u != NULL;) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7231 | PyUnicodeObject *v = u; |
| 7232 | u = *(PyUnicodeObject **)u; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 7233 | if (v->str) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 7234 | PyMem_DEL(v->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 7235 | Py_XDECREF(v->defenc); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 7236 | PyObject_Del(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7237 | } |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7238 | unicode_freelist = NULL; |
| 7239 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7240 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 7241 | |
| 7242 | /* |
| 7243 | Local variables: |
| 7244 | c-basic-offset: 4 |
| 7245 | indent-tabs-mode: nil |
| 7246 | End: |
| 7247 | */ |