Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1 | /* |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2 | |
| 3 | Unicode implementation based on original code by Fredrik Lundh, |
Fred Drake | 785d14f | 2000-05-09 19:54:43 +0000 | [diff] [blame] | 4 | modified by Marc-Andre Lemburg <mal@lemburg.com> according to the |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5 | Unicode Integration Proposal (see file Misc/unicode.txt). |
| 6 | |
Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 7 | Copyright (c) Corporation for National Research Initiatives. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 9 | -------------------------------------------------------------------- |
| 10 | The original string type implementation is: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 12 | Copyright (c) 1999 by Secret Labs AB |
| 13 | Copyright (c) 1999 by Fredrik Lundh |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 15 | By obtaining, using, and/or copying this software and/or its |
| 16 | associated documentation, you agree that you have read, understood, |
| 17 | and will comply with the following terms and conditions: |
| 18 | |
| 19 | Permission to use, copy, modify, and distribute this software and its |
| 20 | associated documentation for any purpose and without fee is hereby |
| 21 | granted, provided that the above copyright notice appears in all |
| 22 | copies, and that both that copyright notice and this permission notice |
| 23 | appear in supporting documentation, and that the name of Secret Labs |
| 24 | AB or the author not be used in advertising or publicity pertaining to |
| 25 | distribution of the software without specific, written prior |
| 26 | permission. |
| 27 | |
| 28 | SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 29 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 30 | FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR |
| 31 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 32 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 33 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 34 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 35 | -------------------------------------------------------------------- |
| 36 | |
| 37 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 38 | |
| 39 | #include "Python.h" |
| 40 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 41 | #include "unicodeobject.h" |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 42 | #include "ucnhash.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 43 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 44 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 45 | #include <windows.h> |
| 46 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 48 | /* Limit for the Unicode object free list */ |
| 49 | |
| 50 | #define MAX_UNICODE_FREELIST_SIZE 1024 |
| 51 | |
| 52 | /* Limit for the Unicode object free list stay alive optimization. |
| 53 | |
| 54 | The implementation will keep allocated Unicode memory intact for |
| 55 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 56 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 57 | |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 58 | At worst this will result in MAX_UNICODE_FREELIST_SIZE * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 59 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 60 | malloc()-overhead) bytes of unused garbage. |
| 61 | |
| 62 | Setting the limit to 0 effectively turns the feature off. |
| 63 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 64 | Note: This is an experimental feature ! If you get core dumps when |
| 65 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 66 | |
| 67 | */ |
| 68 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 69 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 70 | |
| 71 | /* Endianness switches; defaults to little endian */ |
| 72 | |
| 73 | #ifdef WORDS_BIGENDIAN |
| 74 | # define BYTEORDER_IS_BIG_ENDIAN |
| 75 | #else |
| 76 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 77 | #endif |
| 78 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 79 | /* --- Globals ------------------------------------------------------------ |
| 80 | |
| 81 | The globals are initialized by the _PyUnicode_Init() API and should |
| 82 | not be used before calling that API. |
| 83 | |
| 84 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 85 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 86 | /* Free list for Unicode objects */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 87 | static PyUnicodeObject *unicode_freelist; |
| 88 | static int unicode_freelist_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 89 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 90 | /* The empty Unicode object is shared to improve performance. */ |
| 91 | static PyUnicodeObject *unicode_empty; |
| 92 | |
| 93 | /* Single character Unicode strings in the Latin-1 range are being |
| 94 | shared as well. */ |
| 95 | static PyUnicodeObject *unicode_latin1[256]; |
| 96 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 97 | /* Default encoding to use and assume when NULL is passed as encoding |
| 98 | parameter; it is initialized by _PyUnicode_Init(). |
| 99 | |
| 100 | Always use the PyUnicode_SetDefaultEncoding() and |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 101 | PyUnicode_GetDefaultEncoding() APIs to access this global. |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 102 | |
| 103 | */ |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 104 | static char unicode_default_encoding[100]; |
| 105 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 106 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 107 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 108 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 109 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 110 | return 0x10FFFF; |
| 111 | #else |
| 112 | /* This is actually an illegal character, so it should |
| 113 | not be passed to unichr. */ |
| 114 | return 0xFFFF; |
| 115 | #endif |
| 116 | } |
| 117 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 118 | /* --- Unicode Object ----------------------------------------------------- */ |
| 119 | |
| 120 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 121 | int unicode_resize(register PyUnicodeObject *unicode, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 122 | int length) |
| 123 | { |
| 124 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 125 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 126 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 127 | if (unicode->length == length) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 128 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 129 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 130 | /* Resizing shared object (unicode_empty or single character |
| 131 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 132 | instead ! */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 133 | if (unicode == unicode_empty || |
| 134 | (unicode->length == 1 && |
| 135 | /* MvL said unicode->str[] may be signed. Python generally assumes |
| 136 | * an int contains at least 32 bits, and we don't use more than |
| 137 | * 32 bits even in a UCS4 build, so casting to unsigned int should |
| 138 | * be correct. |
| 139 | */ |
| 140 | (unsigned int)unicode->str[0] < 256U && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 141 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 142 | PyErr_SetString(PyExc_SystemError, |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 143 | "can't resize shared unicode objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 144 | return -1; |
| 145 | } |
| 146 | |
| 147 | /* We allocate one more byte to make sure the string is |
| 148 | Ux0000 terminated -- XXX is this needed ? */ |
| 149 | oldstr = unicode->str; |
| 150 | PyMem_RESIZE(unicode->str, Py_UNICODE, length + 1); |
| 151 | if (!unicode->str) { |
| 152 | unicode->str = oldstr; |
| 153 | PyErr_NoMemory(); |
| 154 | return -1; |
| 155 | } |
| 156 | unicode->str[length] = 0; |
| 157 | unicode->length = length; |
| 158 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 159 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 160 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 161 | if (unicode->defenc) { |
| 162 | Py_DECREF(unicode->defenc); |
| 163 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 164 | } |
| 165 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 166 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | /* We allocate one more byte to make sure the string is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 171 | Ux0000 terminated -- XXX is this needed ? |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 172 | |
| 173 | XXX This allocator could further be enhanced by assuring that the |
| 174 | free list never reduces its size below 1. |
| 175 | |
| 176 | */ |
| 177 | |
| 178 | static |
| 179 | PyUnicodeObject *_PyUnicode_New(int length) |
| 180 | { |
| 181 | register PyUnicodeObject *unicode; |
| 182 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 183 | /* Optimization fo empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 184 | if (length == 0 && unicode_empty != NULL) { |
| 185 | Py_INCREF(unicode_empty); |
| 186 | return unicode_empty; |
| 187 | } |
| 188 | |
| 189 | /* Unicode freelist & memory allocation */ |
| 190 | if (unicode_freelist) { |
| 191 | unicode = unicode_freelist; |
Marc-André Lemburg | bea47e7 | 2000-06-17 20:31:17 +0000 | [diff] [blame] | 192 | unicode_freelist = *(PyUnicodeObject **)unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 193 | unicode_freelist_size--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 194 | if (unicode->str) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 195 | /* Keep-Alive optimization: we only upsize the buffer, |
| 196 | never downsize it. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 197 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 198 | unicode_resize(unicode, length) < 0) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 199 | PyMem_DEL(unicode->str); |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 200 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 203 | else { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 204 | unicode->str = PyMem_NEW(Py_UNICODE, length + 1); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 205 | } |
| 206 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 207 | } |
| 208 | else { |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 209 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 210 | if (unicode == NULL) |
| 211 | return NULL; |
| 212 | unicode->str = PyMem_NEW(Py_UNICODE, length + 1); |
| 213 | } |
| 214 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 215 | if (!unicode->str) { |
| 216 | PyErr_NoMemory(); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 217 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 218 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 219 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 220 | * the caller fails before initializing str -- unicode_resize() |
| 221 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 222 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 223 | * We don't want unicode_resize to read uninitialized memory in |
| 224 | * that case. |
| 225 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 226 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 227 | unicode->str[length] = 0; |
| 228 | unicode->length = length; |
| 229 | unicode->hash = -1; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 230 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 231 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 232 | |
| 233 | onError: |
| 234 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 235 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 236 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 240 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 241 | { |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 242 | if (PyUnicode_CheckExact(unicode) && |
| 243 | unicode_freelist_size < MAX_UNICODE_FREELIST_SIZE) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 244 | /* Keep-Alive optimization */ |
| 245 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 246 | PyMem_DEL(unicode->str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 247 | unicode->str = NULL; |
| 248 | unicode->length = 0; |
| 249 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 250 | if (unicode->defenc) { |
| 251 | Py_DECREF(unicode->defenc); |
| 252 | unicode->defenc = NULL; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 253 | } |
| 254 | /* Add to free list */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 255 | *(PyUnicodeObject **)unicode = unicode_freelist; |
| 256 | unicode_freelist = unicode; |
| 257 | unicode_freelist_size++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 258 | } |
| 259 | else { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 260 | PyMem_DEL(unicode->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 261 | Py_XDECREF(unicode->defenc); |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 262 | unicode->ob_type->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 266 | int PyUnicode_Resize(PyObject **unicode, int length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 267 | { |
| 268 | register PyUnicodeObject *v; |
| 269 | |
| 270 | /* Argument checks */ |
| 271 | if (unicode == NULL) { |
| 272 | PyErr_BadInternalCall(); |
| 273 | return -1; |
| 274 | } |
| 275 | v = (PyUnicodeObject *)*unicode; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 276 | if (v == NULL || !PyUnicode_Check(v) || v->ob_refcnt != 1 || length < 0) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 277 | PyErr_BadInternalCall(); |
| 278 | return -1; |
| 279 | } |
| 280 | |
| 281 | /* Resizing unicode_empty and single character objects is not |
| 282 | possible since these are being shared. We simply return a fresh |
| 283 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 284 | if (v->length != length && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 285 | (v == unicode_empty || v->length == 1)) { |
| 286 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 287 | if (w == NULL) |
| 288 | return -1; |
| 289 | Py_UNICODE_COPY(w->str, v->str, |
| 290 | length < v->length ? length : v->length); |
Raymond Hettinger | c8df578 | 2003-03-09 07:30:43 +0000 | [diff] [blame] | 291 | Py_DECREF(*unicode); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 292 | *unicode = (PyObject *)w; |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 297 | objects, since we can modify them in-place. */ |
| 298 | return unicode_resize(v, length); |
| 299 | } |
| 300 | |
| 301 | /* Internal API for use in unicodeobject.c only ! */ |
| 302 | #define _PyUnicode_Resize(unicodevar, length) \ |
| 303 | PyUnicode_Resize(((PyObject **)(unicodevar)), length) |
| 304 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 305 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
| 306 | int size) |
| 307 | { |
| 308 | PyUnicodeObject *unicode; |
| 309 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 310 | /* If the Unicode data is known at construction time, we can apply |
| 311 | some optimizations which share commonly used objects. */ |
| 312 | if (u != NULL) { |
| 313 | |
| 314 | /* Optimization for empty strings */ |
| 315 | if (size == 0 && unicode_empty != NULL) { |
| 316 | Py_INCREF(unicode_empty); |
| 317 | return (PyObject *)unicode_empty; |
| 318 | } |
| 319 | |
| 320 | /* Single character Unicode objects in the Latin-1 range are |
| 321 | shared when using this constructor */ |
| 322 | if (size == 1 && *u < 256) { |
| 323 | unicode = unicode_latin1[*u]; |
| 324 | if (!unicode) { |
| 325 | unicode = _PyUnicode_New(1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 326 | if (!unicode) |
| 327 | return NULL; |
Marc-André Lemburg | 8879a33 | 2001-06-07 12:26:56 +0000 | [diff] [blame] | 328 | unicode->str[0] = *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 329 | unicode_latin1[*u] = unicode; |
| 330 | } |
| 331 | Py_INCREF(unicode); |
| 332 | return (PyObject *)unicode; |
| 333 | } |
| 334 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 335 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 336 | unicode = _PyUnicode_New(size); |
| 337 | if (!unicode) |
| 338 | return NULL; |
| 339 | |
| 340 | /* Copy the Unicode data into the new object */ |
| 341 | if (u != NULL) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 342 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 343 | |
| 344 | return (PyObject *)unicode; |
| 345 | } |
| 346 | |
| 347 | #ifdef HAVE_WCHAR_H |
| 348 | |
| 349 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
| 350 | int size) |
| 351 | { |
| 352 | PyUnicodeObject *unicode; |
| 353 | |
| 354 | if (w == NULL) { |
| 355 | PyErr_BadInternalCall(); |
| 356 | return NULL; |
| 357 | } |
| 358 | |
| 359 | unicode = _PyUnicode_New(size); |
| 360 | if (!unicode) |
| 361 | return NULL; |
| 362 | |
| 363 | /* Copy the wchar_t data into the new object */ |
| 364 | #ifdef HAVE_USABLE_WCHAR_T |
| 365 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 366 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 367 | { |
| 368 | register Py_UNICODE *u; |
| 369 | register int i; |
| 370 | u = PyUnicode_AS_UNICODE(unicode); |
| 371 | for (i = size; i >= 0; i--) |
| 372 | *u++ = *w++; |
| 373 | } |
| 374 | #endif |
| 375 | |
| 376 | return (PyObject *)unicode; |
| 377 | } |
| 378 | |
| 379 | int PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 380 | register wchar_t *w, |
| 381 | int size) |
| 382 | { |
| 383 | if (unicode == NULL) { |
| 384 | PyErr_BadInternalCall(); |
| 385 | return -1; |
| 386 | } |
| 387 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 388 | size = PyUnicode_GET_SIZE(unicode); |
| 389 | #ifdef HAVE_USABLE_WCHAR_T |
| 390 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 391 | #else |
| 392 | { |
| 393 | register Py_UNICODE *u; |
| 394 | register int i; |
| 395 | u = PyUnicode_AS_UNICODE(unicode); |
| 396 | for (i = size; i >= 0; i--) |
| 397 | *w++ = *u++; |
| 398 | } |
| 399 | #endif |
| 400 | |
| 401 | return size; |
| 402 | } |
| 403 | |
| 404 | #endif |
| 405 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 406 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 407 | { |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 408 | Py_UNICODE s[1]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 409 | |
| 410 | #ifdef Py_UNICODE_WIDE |
| 411 | if (ordinal < 0 || ordinal > 0x10ffff) { |
| 412 | PyErr_SetString(PyExc_ValueError, |
| 413 | "unichr() arg not in range(0x110000) " |
| 414 | "(wide Python build)"); |
| 415 | return NULL; |
| 416 | } |
| 417 | #else |
| 418 | if (ordinal < 0 || ordinal > 0xffff) { |
| 419 | PyErr_SetString(PyExc_ValueError, |
| 420 | "unichr() arg not in range(0x10000) " |
| 421 | "(narrow Python build)"); |
| 422 | return NULL; |
| 423 | } |
| 424 | #endif |
| 425 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 426 | s[0] = (Py_UNICODE)ordinal; |
| 427 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 430 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 431 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 432 | /* XXX Perhaps we should make this API an alias of |
| 433 | PyObject_Unicode() instead ?! */ |
| 434 | if (PyUnicode_CheckExact(obj)) { |
| 435 | Py_INCREF(obj); |
| 436 | return obj; |
| 437 | } |
| 438 | if (PyUnicode_Check(obj)) { |
| 439 | /* For a Unicode subtype that's not a Unicode object, |
| 440 | return a true Unicode object with the same data. */ |
| 441 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 442 | PyUnicode_GET_SIZE(obj)); |
| 443 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 444 | return PyUnicode_FromEncodedObject(obj, NULL, "strict"); |
| 445 | } |
| 446 | |
| 447 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
| 448 | const char *encoding, |
| 449 | const char *errors) |
| 450 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 451 | const char *s = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 452 | int len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 453 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 454 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 455 | if (obj == NULL) { |
| 456 | PyErr_BadInternalCall(); |
| 457 | return NULL; |
| 458 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 459 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 460 | #if 0 |
| 461 | /* For b/w compatibility we also accept Unicode objects provided |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 462 | that no encodings is given and then redirect to |
| 463 | PyObject_Unicode() which then applies the additional logic for |
| 464 | Unicode subclasses. |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 465 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 466 | NOTE: This API should really only be used for object which |
| 467 | represent *encoded* Unicode ! |
| 468 | |
| 469 | */ |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 470 | if (PyUnicode_Check(obj)) { |
| 471 | if (encoding) { |
| 472 | PyErr_SetString(PyExc_TypeError, |
| 473 | "decoding Unicode is not supported"); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 474 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 475 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 476 | return PyObject_Unicode(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 477 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 478 | #else |
| 479 | if (PyUnicode_Check(obj)) { |
| 480 | PyErr_SetString(PyExc_TypeError, |
| 481 | "decoding Unicode is not supported"); |
| 482 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 483 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 484 | #endif |
| 485 | |
| 486 | /* Coerce object */ |
| 487 | if (PyString_Check(obj)) { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 488 | s = PyString_AS_STRING(obj); |
| 489 | len = PyString_GET_SIZE(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 490 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 491 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
| 492 | /* Overwrite the error message with something more useful in |
| 493 | case of a TypeError. */ |
| 494 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 495 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 496 | "coercing to Unicode: need string or buffer, " |
| 497 | "%.80s found", |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 498 | obj->ob_type->tp_name); |
| 499 | goto onError; |
| 500 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 501 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 502 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 503 | if (len == 0) { |
| 504 | Py_INCREF(unicode_empty); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 505 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 506 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 507 | else |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 508 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 509 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 510 | return v; |
| 511 | |
| 512 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 513 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | PyObject *PyUnicode_Decode(const char *s, |
| 517 | int size, |
| 518 | const char *encoding, |
| 519 | const char *errors) |
| 520 | { |
| 521 | PyObject *buffer = NULL, *unicode; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 522 | |
| 523 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 524 | encoding = PyUnicode_GetDefaultEncoding(); |
| 525 | |
| 526 | /* Shortcuts for common default encodings */ |
| 527 | if (strcmp(encoding, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 528 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 529 | else if (strcmp(encoding, "latin-1") == 0) |
| 530 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 531 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 532 | else if (strcmp(encoding, "mbcs") == 0) |
| 533 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 534 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 535 | else if (strcmp(encoding, "ascii") == 0) |
| 536 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 537 | |
| 538 | /* Decode via the codec registry */ |
| 539 | buffer = PyBuffer_FromMemory((void *)s, size); |
| 540 | if (buffer == NULL) |
| 541 | goto onError; |
| 542 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 543 | if (unicode == NULL) |
| 544 | goto onError; |
| 545 | if (!PyUnicode_Check(unicode)) { |
| 546 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 547 | "decoder did not return an unicode object (type=%.400s)", |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 548 | unicode->ob_type->tp_name); |
| 549 | Py_DECREF(unicode); |
| 550 | goto onError; |
| 551 | } |
| 552 | Py_DECREF(buffer); |
| 553 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 554 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 555 | onError: |
| 556 | Py_XDECREF(buffer); |
| 557 | return NULL; |
| 558 | } |
| 559 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 560 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 561 | const char *encoding, |
| 562 | const char *errors) |
| 563 | { |
| 564 | PyObject *v; |
| 565 | |
| 566 | if (!PyUnicode_Check(unicode)) { |
| 567 | PyErr_BadArgument(); |
| 568 | goto onError; |
| 569 | } |
| 570 | |
| 571 | if (encoding == NULL) |
| 572 | encoding = PyUnicode_GetDefaultEncoding(); |
| 573 | |
| 574 | /* Decode via the codec registry */ |
| 575 | v = PyCodec_Decode(unicode, encoding, errors); |
| 576 | if (v == NULL) |
| 577 | goto onError; |
| 578 | return v; |
| 579 | |
| 580 | onError: |
| 581 | return NULL; |
| 582 | } |
| 583 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 584 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
| 585 | int size, |
| 586 | const char *encoding, |
| 587 | const char *errors) |
| 588 | { |
| 589 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 590 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 591 | unicode = PyUnicode_FromUnicode(s, size); |
| 592 | if (unicode == NULL) |
| 593 | return NULL; |
| 594 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 595 | Py_DECREF(unicode); |
| 596 | return v; |
| 597 | } |
| 598 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 599 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 600 | const char *encoding, |
| 601 | const char *errors) |
| 602 | { |
| 603 | PyObject *v; |
| 604 | |
| 605 | if (!PyUnicode_Check(unicode)) { |
| 606 | PyErr_BadArgument(); |
| 607 | goto onError; |
| 608 | } |
| 609 | |
| 610 | if (encoding == NULL) |
| 611 | encoding = PyUnicode_GetDefaultEncoding(); |
| 612 | |
| 613 | /* Encode via the codec registry */ |
| 614 | v = PyCodec_Encode(unicode, encoding, errors); |
| 615 | if (v == NULL) |
| 616 | goto onError; |
| 617 | return v; |
| 618 | |
| 619 | onError: |
| 620 | return NULL; |
| 621 | } |
| 622 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 623 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 624 | const char *encoding, |
| 625 | const char *errors) |
| 626 | { |
| 627 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 628 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 629 | if (!PyUnicode_Check(unicode)) { |
| 630 | PyErr_BadArgument(); |
| 631 | goto onError; |
| 632 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 633 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 634 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 635 | encoding = PyUnicode_GetDefaultEncoding(); |
| 636 | |
| 637 | /* Shortcuts for common default encodings */ |
| 638 | if (errors == NULL) { |
| 639 | if (strcmp(encoding, "utf-8") == 0) |
Jeremy Hylton | 9cea41c | 2001-05-29 17:13:15 +0000 | [diff] [blame] | 640 | return PyUnicode_AsUTF8String(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 641 | else if (strcmp(encoding, "latin-1") == 0) |
| 642 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 643 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 644 | else if (strcmp(encoding, "mbcs") == 0) |
| 645 | return PyUnicode_AsMBCSString(unicode); |
| 646 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 647 | else if (strcmp(encoding, "ascii") == 0) |
| 648 | return PyUnicode_AsASCIIString(unicode); |
| 649 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 650 | |
| 651 | /* Encode via the codec registry */ |
| 652 | v = PyCodec_Encode(unicode, encoding, errors); |
| 653 | if (v == NULL) |
| 654 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 655 | if (!PyString_Check(v)) { |
| 656 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 657 | "encoder did not return a string object (type=%.400s)", |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 658 | v->ob_type->tp_name); |
| 659 | Py_DECREF(v); |
| 660 | goto onError; |
| 661 | } |
| 662 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 663 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 664 | onError: |
| 665 | return NULL; |
| 666 | } |
| 667 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 668 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 669 | const char *errors) |
| 670 | { |
| 671 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
| 672 | |
| 673 | if (v) |
| 674 | return v; |
| 675 | v = PyUnicode_AsEncodedString(unicode, NULL, errors); |
| 676 | if (v && errors == NULL) |
| 677 | ((PyUnicodeObject *)unicode)->defenc = v; |
| 678 | return v; |
| 679 | } |
| 680 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 681 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 682 | { |
| 683 | if (!PyUnicode_Check(unicode)) { |
| 684 | PyErr_BadArgument(); |
| 685 | goto onError; |
| 686 | } |
| 687 | return PyUnicode_AS_UNICODE(unicode); |
| 688 | |
| 689 | onError: |
| 690 | return NULL; |
| 691 | } |
| 692 | |
| 693 | int PyUnicode_GetSize(PyObject *unicode) |
| 694 | { |
| 695 | if (!PyUnicode_Check(unicode)) { |
| 696 | PyErr_BadArgument(); |
| 697 | goto onError; |
| 698 | } |
| 699 | return PyUnicode_GET_SIZE(unicode); |
| 700 | |
| 701 | onError: |
| 702 | return -1; |
| 703 | } |
| 704 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 705 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 706 | { |
| 707 | return unicode_default_encoding; |
| 708 | } |
| 709 | |
| 710 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 711 | { |
| 712 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 713 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 714 | /* Make sure the encoding is valid. As side effect, this also |
| 715 | loads the encoding into the codec registry cache. */ |
| 716 | v = _PyCodec_Lookup(encoding); |
| 717 | if (v == NULL) |
| 718 | goto onError; |
| 719 | Py_DECREF(v); |
| 720 | strncpy(unicode_default_encoding, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 721 | encoding, |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 722 | sizeof(unicode_default_encoding)); |
| 723 | return 0; |
| 724 | |
| 725 | onError: |
| 726 | return -1; |
| 727 | } |
| 728 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 729 | /* error handling callback helper: |
| 730 | build arguments, call the callback and check the arguments, |
| 731 | if no exception occured, copy the replacement to the output |
| 732 | and adjust various state variables. |
| 733 | return 0 on success, -1 on error |
| 734 | */ |
| 735 | |
| 736 | static |
| 737 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 738 | const char *encoding, const char *reason, |
| 739 | const char *input, int insize, int *startinpos, int *endinpos, PyObject **exceptionObject, const char **inptr, |
| 740 | PyObject **output, int *outpos, Py_UNICODE **outptr) |
| 741 | { |
| 742 | static char *argparse = "O!i;decoding error handler must return (unicode, int) tuple"; |
| 743 | |
| 744 | PyObject *restuple = NULL; |
| 745 | PyObject *repunicode = NULL; |
| 746 | int outsize = PyUnicode_GET_SIZE(*output); |
| 747 | int requiredsize; |
| 748 | int newpos; |
| 749 | Py_UNICODE *repptr; |
| 750 | int repsize; |
| 751 | int res = -1; |
| 752 | |
| 753 | if (*errorHandler == NULL) { |
| 754 | *errorHandler = PyCodec_LookupError(errors); |
| 755 | if (*errorHandler == NULL) |
| 756 | goto onError; |
| 757 | } |
| 758 | |
| 759 | if (*exceptionObject == NULL) { |
| 760 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 761 | encoding, input, insize, *startinpos, *endinpos, reason); |
| 762 | if (*exceptionObject == NULL) |
| 763 | goto onError; |
| 764 | } |
| 765 | else { |
| 766 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 767 | goto onError; |
| 768 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 769 | goto onError; |
| 770 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 771 | goto onError; |
| 772 | } |
| 773 | |
| 774 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 775 | if (restuple == NULL) |
| 776 | goto onError; |
| 777 | if (!PyTuple_Check(restuple)) { |
| 778 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 779 | goto onError; |
| 780 | } |
| 781 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 782 | goto onError; |
| 783 | if (newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 784 | newpos = insize+newpos; |
| 785 | if (newpos<0 || newpos>insize) { |
| 786 | PyErr_Format(PyExc_IndexError, "position %d from error handler out of bounds", newpos); |
| 787 | goto onError; |
| 788 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 789 | |
| 790 | /* need more space? (at least enough for what we |
| 791 | have+the replacement+the rest of the string (starting |
| 792 | at the new input position), so we won't have to check space |
| 793 | when there are no errors in the rest of the string) */ |
| 794 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 795 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 796 | requiredsize = *outpos + repsize + insize-newpos; |
| 797 | if (requiredsize > outsize) { |
| 798 | if (requiredsize<2*outsize) |
| 799 | requiredsize = 2*outsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 800 | if (PyUnicode_Resize(output, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 801 | goto onError; |
| 802 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
| 803 | } |
| 804 | *endinpos = newpos; |
| 805 | *inptr = input + newpos; |
| 806 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 807 | *outptr += repsize; |
| 808 | *outpos += repsize; |
| 809 | /* we made it! */ |
| 810 | res = 0; |
| 811 | |
| 812 | onError: |
| 813 | Py_XDECREF(restuple); |
| 814 | return res; |
| 815 | } |
| 816 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 817 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 818 | |
| 819 | /* see RFC2152 for details */ |
| 820 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 821 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 822 | char utf7_special[128] = { |
| 823 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 824 | encoded: |
| 825 | 0 - not special |
| 826 | 1 - special |
| 827 | 2 - whitespace (optional) |
| 828 | 3 - RFC2152 Set O (optional) */ |
| 829 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 830 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 831 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 832 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 833 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 834 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 835 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 836 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 837 | |
| 838 | }; |
| 839 | |
| 840 | #define SPECIAL(c, encodeO, encodeWS) \ |
| 841 | (((c)>127 || utf7_special[(c)] == 1) || \ |
| 842 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
| 843 | (encodeO && (utf7_special[(c)] == 3))) |
| 844 | |
| 845 | #define B64(n) ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 846 | #define B64CHAR(c) (isalnum(c) || (c) == '+' || (c) == '/') |
| 847 | #define UB64(c) ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
| 848 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4) |
| 849 | |
| 850 | #define ENCODE(out, ch, bits) \ |
| 851 | while (bits >= 6) { \ |
| 852 | *out++ = B64(ch >> (bits-6)); \ |
| 853 | bits -= 6; \ |
| 854 | } |
| 855 | |
| 856 | #define DECODE(out, ch, bits, surrogate) \ |
| 857 | while (bits >= 16) { \ |
| 858 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 859 | bits -= 16; \ |
| 860 | if (surrogate) { \ |
| 861 | /* We have already generated an error for the high surrogate |
| 862 | so let's not bother seeing if the low surrogate is correct or not */\ |
| 863 | surrogate = 0; \ |
| 864 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
| 865 | /* This is a surrogate pair. Unfortunately we can't represent \ |
| 866 | it in a 16-bit character */ \ |
| 867 | surrogate = 1; \ |
| 868 | errmsg = "code pairs are not supported"; \ |
| 869 | goto utf7Error; \ |
| 870 | } else { \ |
| 871 | *out++ = outCh; \ |
| 872 | } \ |
| 873 | } \ |
| 874 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 875 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
| 876 | int size, |
| 877 | const char *errors) |
| 878 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 879 | const char *starts = s; |
| 880 | int startinpos; |
| 881 | int endinpos; |
| 882 | int outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 883 | const char *e; |
| 884 | PyUnicodeObject *unicode; |
| 885 | Py_UNICODE *p; |
| 886 | const char *errmsg = ""; |
| 887 | int inShift = 0; |
| 888 | unsigned int bitsleft = 0; |
| 889 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 890 | int surrogate = 0; |
| 891 | PyObject *errorHandler = NULL; |
| 892 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 893 | |
| 894 | unicode = _PyUnicode_New(size); |
| 895 | if (!unicode) |
| 896 | return NULL; |
| 897 | if (size == 0) |
| 898 | return (PyObject *)unicode; |
| 899 | |
| 900 | p = unicode->str; |
| 901 | e = s + size; |
| 902 | |
| 903 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 904 | Py_UNICODE ch; |
| 905 | restart: |
| 906 | ch = *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 907 | |
| 908 | if (inShift) { |
| 909 | if ((ch == '-') || !B64CHAR(ch)) { |
| 910 | inShift = 0; |
| 911 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 912 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 913 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 914 | if (bitsleft >= 6) { |
| 915 | /* The shift sequence has a partial character in it. If |
| 916 | bitsleft < 6 then we could just classify it as padding |
| 917 | but that is not the case here */ |
| 918 | |
| 919 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 920 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 921 | } |
| 922 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 923 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 924 | here so indicate the potential of a misencoded character. */ |
| 925 | |
| 926 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 927 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 928 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 929 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | if (ch == '-') { |
| 933 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 934 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 935 | inShift = 1; |
| 936 | } |
| 937 | } else if (SPECIAL(ch,0,0)) { |
| 938 | errmsg = "unexpected special character"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 939 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 940 | } else { |
| 941 | *p++ = ch; |
| 942 | } |
| 943 | } else { |
| 944 | charsleft = (charsleft << 6) | UB64(ch); |
| 945 | bitsleft += 6; |
| 946 | s++; |
| 947 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 948 | } |
| 949 | } |
| 950 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 951 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 952 | s++; |
| 953 | if (s < e && *s == '-') { |
| 954 | s++; |
| 955 | *p++ = '+'; |
| 956 | } else |
| 957 | { |
| 958 | inShift = 1; |
| 959 | bitsleft = 0; |
| 960 | } |
| 961 | } |
| 962 | else if (SPECIAL(ch,0,0)) { |
| 963 | errmsg = "unexpected special character"; |
| 964 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 965 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 966 | } |
| 967 | else { |
| 968 | *p++ = ch; |
| 969 | s++; |
| 970 | } |
| 971 | continue; |
| 972 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 973 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 974 | endinpos = s-starts; |
| 975 | if (unicode_decode_call_errorhandler( |
| 976 | errors, &errorHandler, |
| 977 | "utf7", errmsg, |
| 978 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 979 | (PyObject **)&unicode, &outpos, &p)) |
| 980 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | if (inShift) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 984 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 985 | endinpos = size; |
| 986 | if (unicode_decode_call_errorhandler( |
| 987 | errors, &errorHandler, |
| 988 | "utf7", "unterminated shift sequence", |
| 989 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 990 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 991 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 992 | if (s < e) |
| 993 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 994 | } |
| 995 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 996 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 997 | goto onError; |
| 998 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 999 | Py_XDECREF(errorHandler); |
| 1000 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1001 | return (PyObject *)unicode; |
| 1002 | |
| 1003 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1004 | Py_XDECREF(errorHandler); |
| 1005 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1006 | Py_DECREF(unicode); |
| 1007 | return NULL; |
| 1008 | } |
| 1009 | |
| 1010 | |
| 1011 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 1012 | int size, |
| 1013 | int encodeSetO, |
| 1014 | int encodeWhiteSpace, |
| 1015 | const char *errors) |
| 1016 | { |
| 1017 | PyObject *v; |
| 1018 | /* It might be possible to tighten this worst case */ |
| 1019 | unsigned int cbAllocated = 5 * size; |
| 1020 | int inShift = 0; |
| 1021 | int i = 0; |
| 1022 | unsigned int bitsleft = 0; |
| 1023 | unsigned long charsleft = 0; |
| 1024 | char * out; |
| 1025 | char * start; |
| 1026 | |
| 1027 | if (size == 0) |
| 1028 | return PyString_FromStringAndSize(NULL, 0); |
| 1029 | |
| 1030 | v = PyString_FromStringAndSize(NULL, cbAllocated); |
| 1031 | if (v == NULL) |
| 1032 | return NULL; |
| 1033 | |
| 1034 | start = out = PyString_AS_STRING(v); |
| 1035 | for (;i < size; ++i) { |
| 1036 | Py_UNICODE ch = s[i]; |
| 1037 | |
| 1038 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1039 | if (ch == '+') { |
| 1040 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1041 | *out++ = '-'; |
| 1042 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1043 | charsleft = ch; |
| 1044 | bitsleft = 16; |
| 1045 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1046 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1047 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1048 | } else { |
| 1049 | *out++ = (char) ch; |
| 1050 | } |
| 1051 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1052 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1053 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1054 | charsleft = 0; |
| 1055 | bitsleft = 0; |
| 1056 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1057 | so no '-' is required, except if the character is itself a '-' */ |
| 1058 | if (B64CHAR(ch) || ch == '-') { |
| 1059 | *out++ = '-'; |
| 1060 | } |
| 1061 | inShift = 0; |
| 1062 | *out++ = (char) ch; |
| 1063 | } else { |
| 1064 | bitsleft += 16; |
| 1065 | charsleft = (charsleft << 16) | ch; |
| 1066 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1067 | |
| 1068 | /* If the next character is special then we dont' need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1069 | 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] | 1070 | or '-' then the shift sequence will be terminated implicitly and we |
| 1071 | don't have to insert a '-'. */ |
| 1072 | |
| 1073 | if (bitsleft == 0) { |
| 1074 | if (i + 1 < size) { |
| 1075 | Py_UNICODE ch2 = s[i+1]; |
| 1076 | |
| 1077 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1078 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1079 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1080 | *out++ = '-'; |
| 1081 | inShift = 0; |
| 1082 | } else { |
| 1083 | inShift = 0; |
| 1084 | } |
| 1085 | |
| 1086 | } |
| 1087 | else { |
| 1088 | *out++ = '-'; |
| 1089 | inShift = 0; |
| 1090 | } |
| 1091 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1092 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1093 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1094 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1095 | if (bitsleft) { |
| 1096 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1097 | *out++ = '-'; |
| 1098 | } |
| 1099 | |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 1100 | _PyString_Resize(&v, out - start); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1101 | return v; |
| 1102 | } |
| 1103 | |
| 1104 | #undef SPECIAL |
| 1105 | #undef B64 |
| 1106 | #undef B64CHAR |
| 1107 | #undef UB64 |
| 1108 | #undef ENCODE |
| 1109 | #undef DECODE |
| 1110 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1111 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1112 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1113 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1114 | char utf8_code_length[256] = { |
| 1115 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1116 | illegal prefix. see RFC 2279 for details */ |
| 1117 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1118 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1119 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1120 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1121 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1122 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1123 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1124 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1125 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1126 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1127 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1128 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1129 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1130 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1131 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1132 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1133 | }; |
| 1134 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1135 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
| 1136 | int size, |
| 1137 | const char *errors) |
| 1138 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1139 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1140 | int n; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1141 | int startinpos; |
| 1142 | int endinpos; |
| 1143 | int outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1144 | const char *e; |
| 1145 | PyUnicodeObject *unicode; |
| 1146 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1147 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1148 | PyObject *errorHandler = NULL; |
| 1149 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1150 | |
| 1151 | /* Note: size will always be longer than the resulting Unicode |
| 1152 | character count */ |
| 1153 | unicode = _PyUnicode_New(size); |
| 1154 | if (!unicode) |
| 1155 | return NULL; |
| 1156 | if (size == 0) |
| 1157 | return (PyObject *)unicode; |
| 1158 | |
| 1159 | /* Unpack UTF-8 encoded data */ |
| 1160 | p = unicode->str; |
| 1161 | e = s + size; |
| 1162 | |
| 1163 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1164 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1165 | |
| 1166 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1167 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1168 | s++; |
| 1169 | continue; |
| 1170 | } |
| 1171 | |
| 1172 | n = utf8_code_length[ch]; |
| 1173 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1174 | if (s + n > e) { |
| 1175 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1176 | startinpos = s-starts; |
| 1177 | endinpos = size; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1178 | goto utf8Error; |
| 1179 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1180 | |
| 1181 | switch (n) { |
| 1182 | |
| 1183 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1184 | errmsg = "unexpected code byte"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1185 | startinpos = s-starts; |
| 1186 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1187 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1188 | |
| 1189 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1190 | errmsg = "internal error"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1191 | startinpos = s-starts; |
| 1192 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1193 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1194 | |
| 1195 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1196 | if ((s[1] & 0xc0) != 0x80) { |
| 1197 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1198 | startinpos = s-starts; |
| 1199 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1200 | goto utf8Error; |
| 1201 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1202 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1203 | if (ch < 0x80) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1204 | startinpos = s-starts; |
| 1205 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1206 | errmsg = "illegal encoding"; |
| 1207 | goto utf8Error; |
| 1208 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1209 | else |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1210 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1211 | break; |
| 1212 | |
| 1213 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1214 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1215 | (s[2] & 0xc0) != 0x80) { |
| 1216 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1217 | startinpos = s-starts; |
| 1218 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1219 | goto utf8Error; |
| 1220 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1221 | 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] | 1222 | if (ch < 0x0800) { |
| 1223 | /* Note: UTF-8 encodings of surrogates are considered |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1224 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1225 | |
| 1226 | XXX For wide builds (UCS-4) we should probably try |
| 1227 | to recombine the surrogates into a single code |
| 1228 | unit. |
| 1229 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1230 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1231 | startinpos = s-starts; |
| 1232 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1233 | goto utf8Error; |
| 1234 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1235 | else |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1236 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1237 | break; |
| 1238 | |
| 1239 | case 4: |
| 1240 | if ((s[1] & 0xc0) != 0x80 || |
| 1241 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1242 | (s[3] & 0xc0) != 0x80) { |
| 1243 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1244 | startinpos = s-starts; |
| 1245 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1246 | goto utf8Error; |
| 1247 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1248 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 1249 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 1250 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1251 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1252 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1253 | || (ch > 0x10ffff)) /* maximum value allowed for |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1254 | UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1255 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1256 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1257 | startinpos = s-starts; |
| 1258 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1259 | goto utf8Error; |
| 1260 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1261 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1262 | *p++ = (Py_UNICODE)ch; |
| 1263 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1264 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1265 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1266 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 1267 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1268 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1269 | /* high surrogate = top 10 bits added to D800 */ |
| 1270 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1271 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1272 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1273 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1274 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1275 | break; |
| 1276 | |
| 1277 | default: |
| 1278 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1279 | errmsg = "unsupported Unicode code range"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1280 | startinpos = s-starts; |
| 1281 | endinpos = startinpos+n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1282 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1283 | } |
| 1284 | s += n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1285 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1286 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1287 | utf8Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1288 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1289 | if (unicode_decode_call_errorhandler( |
| 1290 | errors, &errorHandler, |
| 1291 | "utf8", errmsg, |
| 1292 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1293 | (PyObject **)&unicode, &outpos, &p)) |
| 1294 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1298 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1299 | goto onError; |
| 1300 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1301 | Py_XDECREF(errorHandler); |
| 1302 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1303 | return (PyObject *)unicode; |
| 1304 | |
| 1305 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1306 | Py_XDECREF(errorHandler); |
| 1307 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1308 | Py_DECREF(unicode); |
| 1309 | return NULL; |
| 1310 | } |
| 1311 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1312 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 1313 | and allocate exactly as much space needed at the end. Else allocate the |
| 1314 | maximum possible needed (4 result bytes per Unicode character), and return |
| 1315 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1316 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1317 | PyObject * |
| 1318 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 1319 | int size, |
| 1320 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1321 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1322 | #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] | 1323 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1324 | int i; /* index into s of next input byte */ |
| 1325 | PyObject *v; /* result string object */ |
| 1326 | char *p; /* next free byte in output buffer */ |
| 1327 | int nallocated; /* number of result bytes allocated */ |
| 1328 | int nneeded; /* number of result bytes needed */ |
| 1329 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1330 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1331 | assert(s != NULL); |
| 1332 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1333 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1334 | if (size <= MAX_SHORT_UNICHARS) { |
| 1335 | /* Write into the stack buffer; nallocated can't overflow. |
| 1336 | * At the end, we'll allocate exactly as much heap space as it |
| 1337 | * turns out we need. |
| 1338 | */ |
| 1339 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
| 1340 | v = NULL; /* will allocate after we're done */ |
| 1341 | p = stackbuf; |
| 1342 | } |
| 1343 | else { |
| 1344 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 1345 | nallocated = size * 4; |
| 1346 | if (nallocated / 4 != size) /* overflow! */ |
| 1347 | return PyErr_NoMemory(); |
| 1348 | v = PyString_FromStringAndSize(NULL, nallocated); |
| 1349 | if (v == NULL) |
| 1350 | return NULL; |
| 1351 | p = PyString_AS_STRING(v); |
| 1352 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1353 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1354 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1355 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1356 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1357 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1358 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1359 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1360 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1361 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1362 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 1363 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 1364 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1365 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1366 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1367 | /* Encode UCS2 Unicode ordinals */ |
| 1368 | if (ch < 0x10000) { |
| 1369 | /* Special case: check for high surrogate */ |
| 1370 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 1371 | Py_UCS4 ch2 = s[i]; |
| 1372 | /* Check for low surrogate and combine the two to |
| 1373 | form a UCS4 value */ |
| 1374 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1375 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1376 | i++; |
| 1377 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1378 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1379 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1380 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1381 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1382 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1383 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1384 | continue; |
| 1385 | } |
| 1386 | encodeUCS4: |
| 1387 | /* Encode UCS4 Unicode ordinals */ |
| 1388 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 1389 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 1390 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1391 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1392 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1393 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 1394 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1395 | if (v == NULL) { |
| 1396 | /* This was stack allocated. */ |
| 1397 | nneeded = Py_SAFE_DOWNCAST(p - stackbuf, long, int); |
| 1398 | assert(nneeded <= nallocated); |
| 1399 | v = PyString_FromStringAndSize(stackbuf, nneeded); |
| 1400 | } |
| 1401 | else { |
| 1402 | /* Cut back to size actually needed. */ |
| 1403 | nneeded = Py_SAFE_DOWNCAST(p - PyString_AS_STRING(v), long, int); |
| 1404 | assert(nneeded <= nallocated); |
| 1405 | _PyString_Resize(&v, nneeded); |
| 1406 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1407 | return v; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1408 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1409 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1412 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 1413 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1414 | if (!PyUnicode_Check(unicode)) { |
| 1415 | PyErr_BadArgument(); |
| 1416 | return NULL; |
| 1417 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 1418 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1419 | PyUnicode_GET_SIZE(unicode), |
| 1420 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 1424 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1425 | PyObject * |
| 1426 | PyUnicode_DecodeUTF16(const char *s, |
| 1427 | int size, |
| 1428 | const char *errors, |
| 1429 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1430 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1431 | const char *starts = s; |
| 1432 | int startinpos; |
| 1433 | int endinpos; |
| 1434 | int outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1435 | PyUnicodeObject *unicode; |
| 1436 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1437 | const unsigned char *q, *e; |
| 1438 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1439 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1440 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 1441 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1442 | int ihi = 1, ilo = 0; |
| 1443 | #else |
| 1444 | int ihi = 0, ilo = 1; |
| 1445 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1446 | PyObject *errorHandler = NULL; |
| 1447 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1448 | |
| 1449 | /* Note: size will always be longer than the resulting Unicode |
| 1450 | character count */ |
| 1451 | unicode = _PyUnicode_New(size); |
| 1452 | if (!unicode) |
| 1453 | return NULL; |
| 1454 | if (size == 0) |
| 1455 | return (PyObject *)unicode; |
| 1456 | |
| 1457 | /* Unpack UTF-16 encoded data */ |
| 1458 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1459 | q = (unsigned char *)s; |
| 1460 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1461 | |
| 1462 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1463 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1464 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1465 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 1466 | byte order setting accordingly. In native mode, the leading BOM |
| 1467 | mark is skipped, in all other modes, it is copied to the output |
| 1468 | stream as-is (giving a ZWNBSP character). */ |
| 1469 | if (bo == 0) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1470 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1471 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1472 | if (bom == 0xFEFF) { |
| 1473 | q += 2; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1474 | bo = -1; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1475 | } |
| 1476 | else if (bom == 0xFFFE) { |
| 1477 | q += 2; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1478 | bo = 1; |
| 1479 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1480 | #else |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1481 | if (bom == 0xFEFF) { |
| 1482 | q += 2; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1483 | bo = 1; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1484 | } |
| 1485 | else if (bom == 0xFFFE) { |
| 1486 | q += 2; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1487 | bo = -1; |
| 1488 | } |
| 1489 | #endif |
| 1490 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1491 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1492 | if (bo == -1) { |
| 1493 | /* force LE */ |
| 1494 | ihi = 1; |
| 1495 | ilo = 0; |
| 1496 | } |
| 1497 | else if (bo == 1) { |
| 1498 | /* force BE */ |
| 1499 | ihi = 0; |
| 1500 | ilo = 1; |
| 1501 | } |
| 1502 | |
| 1503 | while (q < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1504 | Py_UNICODE ch; |
| 1505 | /* remaing bytes at the end? (size should be even) */ |
| 1506 | if (e-q<2) { |
| 1507 | errmsg = "truncated data"; |
| 1508 | startinpos = ((const char *)q)-starts; |
| 1509 | endinpos = ((const char *)e)-starts; |
| 1510 | goto utf16Error; |
| 1511 | /* The remaining input chars are ignored if the callback |
| 1512 | chooses to skip the input */ |
| 1513 | } |
| 1514 | ch = (q[ihi] << 8) | q[ilo]; |
| 1515 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1516 | q += 2; |
| 1517 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1518 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 1519 | *p++ = ch; |
| 1520 | continue; |
| 1521 | } |
| 1522 | |
| 1523 | /* UTF-16 code pair: */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1524 | if (q >= e) { |
| 1525 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1526 | startinpos = (((const char *)q)-2)-starts; |
| 1527 | endinpos = ((const char *)e)-starts; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1528 | goto utf16Error; |
| 1529 | } |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 1530 | if (0xD800 <= ch && ch <= 0xDBFF) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1531 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 1532 | q += 2; |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 1533 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1534 | #ifndef Py_UNICODE_WIDE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1535 | *p++ = ch; |
| 1536 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1537 | #else |
| 1538 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1539 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1540 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1541 | } |
| 1542 | else { |
| 1543 | errmsg = "illegal UTF-16 surrogate"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1544 | startinpos = (((const char *)q)-4)-starts; |
| 1545 | endinpos = startinpos+2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1546 | goto utf16Error; |
| 1547 | } |
| 1548 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1549 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1550 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1551 | startinpos = (((const char *)q)-2)-starts; |
| 1552 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1553 | /* Fall through to report the error */ |
| 1554 | |
| 1555 | utf16Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1556 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1557 | if (unicode_decode_call_errorhandler( |
| 1558 | errors, &errorHandler, |
| 1559 | "utf16", errmsg, |
| 1560 | starts, size, &startinpos, &endinpos, &exc, (const char **)&q, |
| 1561 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1562 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | if (byteorder) |
| 1566 | *byteorder = bo; |
| 1567 | |
| 1568 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1569 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1570 | goto onError; |
| 1571 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1572 | Py_XDECREF(errorHandler); |
| 1573 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1574 | return (PyObject *)unicode; |
| 1575 | |
| 1576 | onError: |
| 1577 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1578 | Py_XDECREF(errorHandler); |
| 1579 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1580 | return NULL; |
| 1581 | } |
| 1582 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1583 | PyObject * |
| 1584 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 1585 | int size, |
| 1586 | const char *errors, |
| 1587 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1588 | { |
| 1589 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1590 | unsigned char *p; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1591 | #ifdef Py_UNICODE_WIDE |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1592 | int i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1593 | #else |
| 1594 | const int pairs = 0; |
| 1595 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1596 | /* Offsets from p for storing byte pairs in the right order. */ |
| 1597 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1598 | int ihi = 1, ilo = 0; |
| 1599 | #else |
| 1600 | int ihi = 0, ilo = 1; |
| 1601 | #endif |
| 1602 | |
| 1603 | #define STORECHAR(CH) \ |
| 1604 | do { \ |
| 1605 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 1606 | p[ilo] = (CH) & 0xff; \ |
| 1607 | p += 2; \ |
| 1608 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1609 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1610 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1611 | for (i = pairs = 0; i < size; i++) |
| 1612 | if (s[i] >= 0x10000) |
| 1613 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1614 | #endif |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1615 | v = PyString_FromStringAndSize(NULL, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1616 | 2 * (size + pairs + (byteorder == 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1617 | if (v == NULL) |
| 1618 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1619 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1620 | p = (unsigned char *)PyString_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1621 | if (byteorder == 0) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1622 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 1623 | if (size == 0) |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 1624 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1625 | |
| 1626 | if (byteorder == -1) { |
| 1627 | /* force LE */ |
| 1628 | ihi = 1; |
| 1629 | ilo = 0; |
| 1630 | } |
| 1631 | else if (byteorder == 1) { |
| 1632 | /* force BE */ |
| 1633 | ihi = 0; |
| 1634 | ilo = 1; |
| 1635 | } |
| 1636 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1637 | while (size-- > 0) { |
| 1638 | Py_UNICODE ch = *s++; |
| 1639 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1640 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1641 | if (ch >= 0x10000) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1642 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 1643 | ch = 0xD800 | ((ch-0x10000) >> 10); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1644 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1645 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1646 | STORECHAR(ch); |
| 1647 | if (ch2) |
| 1648 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1649 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1650 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1651 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
| 1654 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 1655 | { |
| 1656 | if (!PyUnicode_Check(unicode)) { |
| 1657 | PyErr_BadArgument(); |
| 1658 | return NULL; |
| 1659 | } |
| 1660 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 1661 | PyUnicode_GET_SIZE(unicode), |
| 1662 | NULL, |
| 1663 | 0); |
| 1664 | } |
| 1665 | |
| 1666 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 1667 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1668 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 1669 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1670 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
| 1671 | int size, |
| 1672 | const char *errors) |
| 1673 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1674 | const char *starts = s; |
| 1675 | int startinpos; |
| 1676 | int endinpos; |
| 1677 | int outpos; |
| 1678 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1679 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1680 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1681 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1682 | char* message; |
| 1683 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1684 | PyObject *errorHandler = NULL; |
| 1685 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1686 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1687 | /* Escaped strings will always be longer than the resulting |
| 1688 | 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] | 1689 | length after conversion to the true value. |
| 1690 | (but if the error callback returns a long replacement string |
| 1691 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1692 | v = _PyUnicode_New(size); |
| 1693 | if (v == NULL) |
| 1694 | goto onError; |
| 1695 | if (size == 0) |
| 1696 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1697 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1698 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1699 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1700 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1701 | while (s < end) { |
| 1702 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 1703 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1704 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1705 | |
| 1706 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 1707 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1708 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1709 | continue; |
| 1710 | } |
| 1711 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1712 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1713 | /* \ - Escapes */ |
| 1714 | s++; |
| 1715 | switch (*s++) { |
| 1716 | |
| 1717 | /* \x escapes */ |
| 1718 | case '\n': break; |
| 1719 | case '\\': *p++ = '\\'; break; |
| 1720 | case '\'': *p++ = '\''; break; |
| 1721 | case '\"': *p++ = '\"'; break; |
| 1722 | case 'b': *p++ = '\b'; break; |
| 1723 | case 'f': *p++ = '\014'; break; /* FF */ |
| 1724 | case 't': *p++ = '\t'; break; |
| 1725 | case 'n': *p++ = '\n'; break; |
| 1726 | case 'r': *p++ = '\r'; break; |
| 1727 | case 'v': *p++ = '\013'; break; /* VT */ |
| 1728 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 1729 | |
| 1730 | /* \OOO (octal) escapes */ |
| 1731 | case '0': case '1': case '2': case '3': |
| 1732 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1733 | x = s[-1] - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1734 | if ('0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1735 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1736 | if ('0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1737 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1738 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1739 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1740 | break; |
| 1741 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1742 | /* hex escapes */ |
| 1743 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1744 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1745 | digits = 2; |
| 1746 | message = "truncated \\xXX escape"; |
| 1747 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1748 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1749 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1750 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1751 | digits = 4; |
| 1752 | message = "truncated \\uXXXX escape"; |
| 1753 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1754 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1755 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1756 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1757 | digits = 8; |
| 1758 | message = "truncated \\UXXXXXXXX escape"; |
| 1759 | hexescape: |
| 1760 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1761 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1762 | if (s+digits>end) { |
| 1763 | endinpos = size; |
| 1764 | if (unicode_decode_call_errorhandler( |
| 1765 | errors, &errorHandler, |
| 1766 | "unicodeescape", "end of string in escape sequence", |
| 1767 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1768 | (PyObject **)&v, &outpos, &p)) |
| 1769 | goto onError; |
| 1770 | goto nextByte; |
| 1771 | } |
| 1772 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1773 | c = (unsigned char) s[i]; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1774 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1775 | endinpos = (s+i+1)-starts; |
| 1776 | if (unicode_decode_call_errorhandler( |
| 1777 | errors, &errorHandler, |
| 1778 | "unicodeescape", message, |
| 1779 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1780 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1781 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1782 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1783 | } |
| 1784 | chr = (chr<<4) & ~0xF; |
| 1785 | if (c >= '0' && c <= '9') |
| 1786 | chr += c - '0'; |
| 1787 | else if (c >= 'a' && c <= 'f') |
| 1788 | chr += 10 + c - 'a'; |
| 1789 | else |
| 1790 | chr += 10 + c - 'A'; |
| 1791 | } |
| 1792 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 1793 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1794 | /* _decoding_error will have already written into the |
| 1795 | target buffer. */ |
| 1796 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1797 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1798 | /* when we get here, chr is a 32-bit unicode character */ |
| 1799 | if (chr <= 0xffff) |
| 1800 | /* UCS-2 character */ |
| 1801 | *p++ = (Py_UNICODE) chr; |
| 1802 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1803 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1804 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1805 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1806 | *p++ = chr; |
| 1807 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1808 | chr -= 0x10000L; |
| 1809 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1810 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1811 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1812 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1813 | endinpos = s-starts; |
| 1814 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1815 | if (unicode_decode_call_errorhandler( |
| 1816 | errors, &errorHandler, |
| 1817 | "unicodeescape", "illegal Unicode character", |
| 1818 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1819 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1820 | goto onError; |
| 1821 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1822 | break; |
| 1823 | |
| 1824 | /* \N{name} */ |
| 1825 | case 'N': |
| 1826 | message = "malformed \\N character escape"; |
| 1827 | if (ucnhash_CAPI == NULL) { |
| 1828 | /* load the unicode data module */ |
| 1829 | PyObject *m, *v; |
| 1830 | m = PyImport_ImportModule("unicodedata"); |
| 1831 | if (m == NULL) |
| 1832 | goto ucnhashError; |
| 1833 | v = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
| 1834 | Py_DECREF(m); |
| 1835 | if (v == NULL) |
| 1836 | goto ucnhashError; |
| 1837 | ucnhash_CAPI = PyCObject_AsVoidPtr(v); |
| 1838 | Py_DECREF(v); |
| 1839 | if (ucnhash_CAPI == NULL) |
| 1840 | goto ucnhashError; |
| 1841 | } |
| 1842 | if (*s == '{') { |
| 1843 | const char *start = s+1; |
| 1844 | /* look for the closing brace */ |
| 1845 | while (*s != '}' && s < end) |
| 1846 | s++; |
| 1847 | if (s > start && s < end && *s == '}') { |
| 1848 | /* found a name. look it up in the unicode database */ |
| 1849 | message = "unknown Unicode character name"; |
| 1850 | s++; |
| 1851 | if (ucnhash_CAPI->getcode(start, s-start-1, &chr)) |
| 1852 | goto store; |
| 1853 | } |
| 1854 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1855 | endinpos = s-starts; |
| 1856 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1857 | if (unicode_decode_call_errorhandler( |
| 1858 | errors, &errorHandler, |
| 1859 | "unicodeescape", message, |
| 1860 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1861 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1862 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1863 | break; |
| 1864 | |
| 1865 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1866 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1867 | message = "\\ at end of string"; |
| 1868 | s--; |
| 1869 | endinpos = s-starts; |
| 1870 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1871 | if (unicode_decode_call_errorhandler( |
| 1872 | errors, &errorHandler, |
| 1873 | "unicodeescape", message, |
| 1874 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1875 | (PyObject **)&v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1876 | goto onError; |
| 1877 | } |
| 1878 | else { |
| 1879 | *p++ = '\\'; |
| 1880 | *p++ = (unsigned char)s[-1]; |
| 1881 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1882 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1883 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1884 | nextByte: |
| 1885 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1886 | } |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1887 | if (_PyUnicode_Resize(&v, (int)(p - PyUnicode_AS_UNICODE(v))) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1888 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 1889 | Py_XDECREF(errorHandler); |
| 1890 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1891 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1892 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1893 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1894 | PyErr_SetString( |
| 1895 | PyExc_UnicodeError, |
| 1896 | "\\N escapes not supported (can't load unicodedata module)" |
| 1897 | ); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1898 | Py_XDECREF(errorHandler); |
| 1899 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 1900 | return NULL; |
| 1901 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1902 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1903 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1904 | Py_XDECREF(errorHandler); |
| 1905 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1906 | return NULL; |
| 1907 | } |
| 1908 | |
| 1909 | /* Return a Unicode-Escape string version of the Unicode object. |
| 1910 | |
| 1911 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 1912 | appropriate. |
| 1913 | |
| 1914 | */ |
| 1915 | |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 1916 | static const Py_UNICODE *findchar(const Py_UNICODE *s, |
| 1917 | int size, |
| 1918 | Py_UNICODE ch); |
| 1919 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1920 | static |
| 1921 | PyObject *unicodeescape_string(const Py_UNICODE *s, |
| 1922 | int size, |
| 1923 | int quotes) |
| 1924 | { |
| 1925 | PyObject *repr; |
| 1926 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1927 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 1928 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1929 | |
| 1930 | repr = PyString_FromStringAndSize(NULL, 2 + 6*size + 1); |
| 1931 | if (repr == NULL) |
| 1932 | return NULL; |
| 1933 | |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1934 | p = PyString_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1935 | |
| 1936 | if (quotes) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1937 | *p++ = 'u'; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1938 | *p++ = (findchar(s, size, '\'') && |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1939 | !findchar(s, size, '"')) ? '"' : '\''; |
| 1940 | } |
| 1941 | while (size-- > 0) { |
| 1942 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1943 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1944 | /* Escape quotes */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1945 | if (quotes && |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1946 | (ch == (Py_UNICODE) PyString_AS_STRING(repr)[1] || ch == '\\')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1947 | *p++ = '\\'; |
| 1948 | *p++ = (char) ch; |
Guido van Rossum | ad9744a | 2001-09-21 15:38:17 +0000 | [diff] [blame] | 1949 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1950 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1951 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 1952 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1953 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 1954 | else if (ch >= 0x10000) { |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1955 | int offset = p - PyString_AS_STRING(repr); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1956 | |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1957 | /* Resize the string if necessary */ |
| 1958 | if (offset + 12 > PyString_GET_SIZE(repr)) { |
| 1959 | if (_PyString_Resize(&repr, PyString_GET_SIZE(repr) + 100)) |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 1960 | return NULL; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1961 | p = PyString_AS_STRING(repr) + offset; |
| 1962 | } |
| 1963 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1964 | *p++ = '\\'; |
| 1965 | *p++ = 'U'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1966 | *p++ = hexdigit[(ch >> 28) & 0x0000000F]; |
| 1967 | *p++ = hexdigit[(ch >> 24) & 0x0000000F]; |
| 1968 | *p++ = hexdigit[(ch >> 20) & 0x0000000F]; |
| 1969 | *p++ = hexdigit[(ch >> 16) & 0x0000000F]; |
| 1970 | *p++ = hexdigit[(ch >> 12) & 0x0000000F]; |
| 1971 | *p++ = hexdigit[(ch >> 8) & 0x0000000F]; |
| 1972 | *p++ = hexdigit[(ch >> 4) & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1973 | *p++ = hexdigit[ch & 0x0000000F]; |
| 1974 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1975 | } |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 1976 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1977 | /* Map UTF-16 surrogate pairs to Unicode \UXXXXXXXX escapes */ |
| 1978 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 1979 | Py_UNICODE ch2; |
| 1980 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1981 | |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1982 | ch2 = *s++; |
| 1983 | size--; |
| 1984 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 1985 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 1986 | *p++ = '\\'; |
| 1987 | *p++ = 'U'; |
| 1988 | *p++ = hexdigit[(ucs >> 28) & 0x0000000F]; |
| 1989 | *p++ = hexdigit[(ucs >> 24) & 0x0000000F]; |
| 1990 | *p++ = hexdigit[(ucs >> 20) & 0x0000000F]; |
| 1991 | *p++ = hexdigit[(ucs >> 16) & 0x0000000F]; |
| 1992 | *p++ = hexdigit[(ucs >> 12) & 0x0000000F]; |
| 1993 | *p++ = hexdigit[(ucs >> 8) & 0x0000000F]; |
| 1994 | *p++ = hexdigit[(ucs >> 4) & 0x0000000F]; |
| 1995 | *p++ = hexdigit[ucs & 0x0000000F]; |
| 1996 | continue; |
| 1997 | } |
| 1998 | /* Fall through: isolated surrogates are copied as-is */ |
| 1999 | s--; |
| 2000 | size++; |
| 2001 | } |
| 2002 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2003 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2004 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2005 | *p++ = '\\'; |
| 2006 | *p++ = 'u'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2007 | *p++ = hexdigit[(ch >> 12) & 0x000F]; |
| 2008 | *p++ = hexdigit[(ch >> 8) & 0x000F]; |
| 2009 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2010 | *p++ = hexdigit[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2011 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2012 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2013 | /* Map special whitespace to '\t', \n', '\r' */ |
| 2014 | else if (ch == '\t') { |
| 2015 | *p++ = '\\'; |
| 2016 | *p++ = 't'; |
| 2017 | } |
| 2018 | else if (ch == '\n') { |
| 2019 | *p++ = '\\'; |
| 2020 | *p++ = 'n'; |
| 2021 | } |
| 2022 | else if (ch == '\r') { |
| 2023 | *p++ = '\\'; |
| 2024 | *p++ = 'r'; |
| 2025 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2026 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2027 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 2028 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2029 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2030 | *p++ = 'x'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2031 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2032 | *p++ = hexdigit[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2033 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2034 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2035 | /* Copy everything else as-is */ |
| 2036 | else |
| 2037 | *p++ = (char) ch; |
| 2038 | } |
| 2039 | if (quotes) |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2040 | *p++ = PyString_AS_STRING(repr)[1]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2041 | |
| 2042 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2043 | _PyString_Resize(&repr, p - PyString_AS_STRING(repr)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2044 | return repr; |
| 2045 | } |
| 2046 | |
| 2047 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 2048 | int size) |
| 2049 | { |
| 2050 | return unicodeescape_string(s, size, 0); |
| 2051 | } |
| 2052 | |
| 2053 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 2054 | { |
| 2055 | if (!PyUnicode_Check(unicode)) { |
| 2056 | PyErr_BadArgument(); |
| 2057 | return NULL; |
| 2058 | } |
| 2059 | return PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2060 | PyUnicode_GET_SIZE(unicode)); |
| 2061 | } |
| 2062 | |
| 2063 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 2064 | |
| 2065 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
| 2066 | int size, |
| 2067 | const char *errors) |
| 2068 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2069 | const char *starts = s; |
| 2070 | int startinpos; |
| 2071 | int endinpos; |
| 2072 | int outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2073 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2074 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2075 | const char *end; |
| 2076 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2077 | PyObject *errorHandler = NULL; |
| 2078 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2079 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2080 | /* Escaped strings will always be longer than the resulting |
| 2081 | 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] | 2082 | length after conversion to the true value. (But decoding error |
| 2083 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2084 | v = _PyUnicode_New(size); |
| 2085 | if (v == NULL) |
| 2086 | goto onError; |
| 2087 | if (size == 0) |
| 2088 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2089 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2090 | end = s + size; |
| 2091 | while (s < end) { |
| 2092 | unsigned char c; |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 2093 | Py_UCS4 x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2094 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2095 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2096 | |
| 2097 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2098 | if (*s != '\\') { |
| 2099 | *p++ = (unsigned char)*s++; |
| 2100 | continue; |
| 2101 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2102 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2103 | |
| 2104 | /* \u-escapes are only interpreted iff the number of leading |
| 2105 | backslashes if odd */ |
| 2106 | bs = s; |
| 2107 | for (;s < end;) { |
| 2108 | if (*s != '\\') |
| 2109 | break; |
| 2110 | *p++ = (unsigned char)*s++; |
| 2111 | } |
| 2112 | if (((s - bs) & 1) == 0 || |
| 2113 | s >= end || |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2114 | (*s != 'u' && *s != 'U')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2115 | continue; |
| 2116 | } |
| 2117 | p--; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2118 | count = *s=='u' ? 4 : 8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2119 | s++; |
| 2120 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2121 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2122 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2123 | for (x = 0, i = 0; i < count; ++i, ++s) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2124 | c = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2125 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2126 | endinpos = s-starts; |
| 2127 | if (unicode_decode_call_errorhandler( |
| 2128 | errors, &errorHandler, |
| 2129 | "rawunicodeescape", "truncated \\uXXXX", |
| 2130 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2131 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2132 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2133 | goto nextByte; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2134 | } |
| 2135 | x = (x<<4) & ~0xF; |
| 2136 | if (c >= '0' && c <= '9') |
| 2137 | x += c - '0'; |
| 2138 | else if (c >= 'a' && c <= 'f') |
| 2139 | x += 10 + c - 'a'; |
| 2140 | else |
| 2141 | x += 10 + c - 'A'; |
| 2142 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2143 | #ifndef Py_UNICODE_WIDE |
| 2144 | if (x > 0x10000) { |
| 2145 | if (unicode_decode_call_errorhandler( |
| 2146 | errors, &errorHandler, |
| 2147 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
| 2148 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2149 | (PyObject **)&v, &outpos, &p)) |
| 2150 | goto onError; |
| 2151 | } |
| 2152 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2153 | *p++ = x; |
| 2154 | nextByte: |
| 2155 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2156 | } |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2157 | if (_PyUnicode_Resize(&v, (int)(p - PyUnicode_AS_UNICODE(v))) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2158 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2159 | Py_XDECREF(errorHandler); |
| 2160 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2161 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2162 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2163 | onError: |
| 2164 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2165 | Py_XDECREF(errorHandler); |
| 2166 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2167 | return NULL; |
| 2168 | } |
| 2169 | |
| 2170 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 2171 | int size) |
| 2172 | { |
| 2173 | PyObject *repr; |
| 2174 | char *p; |
| 2175 | char *q; |
| 2176 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2177 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2178 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2179 | #ifdef Py_UNICODE_WIDE |
| 2180 | repr = PyString_FromStringAndSize(NULL, 10 * size); |
| 2181 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2182 | repr = PyString_FromStringAndSize(NULL, 6 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2183 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2184 | if (repr == NULL) |
| 2185 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2186 | if (size == 0) |
| 2187 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2188 | |
| 2189 | p = q = PyString_AS_STRING(repr); |
| 2190 | while (size-- > 0) { |
| 2191 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2192 | #ifdef Py_UNICODE_WIDE |
| 2193 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2194 | if (ch >= 0x10000) { |
| 2195 | *p++ = '\\'; |
| 2196 | *p++ = 'U'; |
| 2197 | *p++ = hexdigit[(ch >> 28) & 0xf]; |
| 2198 | *p++ = hexdigit[(ch >> 24) & 0xf]; |
| 2199 | *p++ = hexdigit[(ch >> 20) & 0xf]; |
| 2200 | *p++ = hexdigit[(ch >> 16) & 0xf]; |
| 2201 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 2202 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 2203 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 2204 | *p++ = hexdigit[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2205 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2206 | else |
| 2207 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2208 | /* Map 16-bit characters to '\uxxxx' */ |
| 2209 | if (ch >= 256) { |
| 2210 | *p++ = '\\'; |
| 2211 | *p++ = 'u'; |
| 2212 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 2213 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 2214 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 2215 | *p++ = hexdigit[ch & 15]; |
| 2216 | } |
| 2217 | /* Copy everything else as-is */ |
| 2218 | else |
| 2219 | *p++ = (char) ch; |
| 2220 | } |
| 2221 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2222 | _PyString_Resize(&repr, p - q); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2223 | return repr; |
| 2224 | } |
| 2225 | |
| 2226 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 2227 | { |
| 2228 | if (!PyUnicode_Check(unicode)) { |
| 2229 | PyErr_BadArgument(); |
| 2230 | return NULL; |
| 2231 | } |
| 2232 | return PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2233 | PyUnicode_GET_SIZE(unicode)); |
| 2234 | } |
| 2235 | |
| 2236 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 2237 | |
| 2238 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
| 2239 | int size, |
| 2240 | const char *errors) |
| 2241 | { |
| 2242 | PyUnicodeObject *v; |
| 2243 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2244 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2245 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2246 | if (size == 1) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 2247 | Py_UNICODE r = *(unsigned char*)s; |
| 2248 | return PyUnicode_FromUnicode(&r, 1); |
| 2249 | } |
| 2250 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2251 | v = _PyUnicode_New(size); |
| 2252 | if (v == NULL) |
| 2253 | goto onError; |
| 2254 | if (size == 0) |
| 2255 | return (PyObject *)v; |
| 2256 | p = PyUnicode_AS_UNICODE(v); |
| 2257 | while (size-- > 0) |
| 2258 | *p++ = (unsigned char)*s++; |
| 2259 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2260 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2261 | onError: |
| 2262 | Py_XDECREF(v); |
| 2263 | return NULL; |
| 2264 | } |
| 2265 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2266 | /* create or adjust a UnicodeEncodeError */ |
| 2267 | static void make_encode_exception(PyObject **exceptionObject, |
| 2268 | const char *encoding, |
| 2269 | const Py_UNICODE *unicode, int size, |
| 2270 | int startpos, int endpos, |
| 2271 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2272 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2273 | if (*exceptionObject == NULL) { |
| 2274 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 2275 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2276 | } |
| 2277 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2278 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 2279 | goto onError; |
| 2280 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 2281 | goto onError; |
| 2282 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 2283 | goto onError; |
| 2284 | return; |
| 2285 | onError: |
| 2286 | Py_DECREF(*exceptionObject); |
| 2287 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2288 | } |
| 2289 | } |
| 2290 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2291 | /* raises a UnicodeEncodeError */ |
| 2292 | static void raise_encode_exception(PyObject **exceptionObject, |
| 2293 | const char *encoding, |
| 2294 | const Py_UNICODE *unicode, int size, |
| 2295 | int startpos, int endpos, |
| 2296 | const char *reason) |
| 2297 | { |
| 2298 | make_encode_exception(exceptionObject, |
| 2299 | encoding, unicode, size, startpos, endpos, reason); |
| 2300 | if (*exceptionObject != NULL) |
| 2301 | PyCodec_StrictErrors(*exceptionObject); |
| 2302 | } |
| 2303 | |
| 2304 | /* error handling callback helper: |
| 2305 | build arguments, call the callback and check the arguments, |
| 2306 | put the result into newpos and return the replacement string, which |
| 2307 | has to be freed by the caller */ |
| 2308 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 2309 | PyObject **errorHandler, |
| 2310 | const char *encoding, const char *reason, |
| 2311 | const Py_UNICODE *unicode, int size, PyObject **exceptionObject, |
| 2312 | int startpos, int endpos, |
| 2313 | int *newpos) |
| 2314 | { |
| 2315 | static char *argparse = "O!i;encoding error handler must return (unicode, int) tuple"; |
| 2316 | |
| 2317 | PyObject *restuple; |
| 2318 | PyObject *resunicode; |
| 2319 | |
| 2320 | if (*errorHandler == NULL) { |
| 2321 | *errorHandler = PyCodec_LookupError(errors); |
| 2322 | if (*errorHandler == NULL) |
| 2323 | return NULL; |
| 2324 | } |
| 2325 | |
| 2326 | make_encode_exception(exceptionObject, |
| 2327 | encoding, unicode, size, startpos, endpos, reason); |
| 2328 | if (*exceptionObject == NULL) |
| 2329 | return NULL; |
| 2330 | |
| 2331 | restuple = PyObject_CallFunctionObjArgs( |
| 2332 | *errorHandler, *exceptionObject, NULL); |
| 2333 | if (restuple == NULL) |
| 2334 | return NULL; |
| 2335 | if (!PyTuple_Check(restuple)) { |
| 2336 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 2337 | Py_DECREF(restuple); |
| 2338 | return NULL; |
| 2339 | } |
| 2340 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 2341 | &resunicode, newpos)) { |
| 2342 | Py_DECREF(restuple); |
| 2343 | return NULL; |
| 2344 | } |
| 2345 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2346 | *newpos = size+*newpos; |
| 2347 | if (*newpos<0 || *newpos>size) { |
| 2348 | PyErr_Format(PyExc_IndexError, "position %d from error handler out of bounds", *newpos); |
| 2349 | Py_DECREF(restuple); |
| 2350 | return NULL; |
| 2351 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2352 | Py_INCREF(resunicode); |
| 2353 | Py_DECREF(restuple); |
| 2354 | return resunicode; |
| 2355 | } |
| 2356 | |
| 2357 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
| 2358 | int size, |
| 2359 | const char *errors, |
| 2360 | int limit) |
| 2361 | { |
| 2362 | /* output object */ |
| 2363 | PyObject *res; |
| 2364 | /* pointers to the beginning and end+1 of input */ |
| 2365 | const Py_UNICODE *startp = p; |
| 2366 | const Py_UNICODE *endp = p + size; |
| 2367 | /* pointer to the beginning of the unencodable characters */ |
| 2368 | /* const Py_UNICODE *badp = NULL; */ |
| 2369 | /* pointer into the output */ |
| 2370 | char *str; |
| 2371 | /* current output position */ |
| 2372 | int respos = 0; |
| 2373 | int ressize; |
| 2374 | char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 2375 | char *reason = (limit == 256) ? "ordinal not in range(256)" : "ordinal not in range(128)"; |
| 2376 | PyObject *errorHandler = NULL; |
| 2377 | PyObject *exc = NULL; |
| 2378 | /* the following variable is used for caching string comparisons |
| 2379 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 2380 | int known_errorHandler = -1; |
| 2381 | |
| 2382 | /* allocate enough for a simple encoding without |
| 2383 | replacements, if we need more, we'll resize */ |
| 2384 | res = PyString_FromStringAndSize(NULL, size); |
| 2385 | if (res == NULL) |
| 2386 | goto onError; |
| 2387 | if (size == 0) |
| 2388 | return res; |
| 2389 | str = PyString_AS_STRING(res); |
| 2390 | ressize = size; |
| 2391 | |
| 2392 | while (p<endp) { |
| 2393 | Py_UNICODE c = *p; |
| 2394 | |
| 2395 | /* can we encode this? */ |
| 2396 | if (c<limit) { |
| 2397 | /* no overflow check, because we know that the space is enough */ |
| 2398 | *str++ = (char)c; |
| 2399 | ++p; |
| 2400 | } |
| 2401 | else { |
| 2402 | int unicodepos = p-startp; |
| 2403 | int requiredsize; |
| 2404 | PyObject *repunicode; |
| 2405 | int repsize; |
| 2406 | int newpos; |
| 2407 | int respos; |
| 2408 | Py_UNICODE *uni2; |
| 2409 | /* startpos for collecting unencodable chars */ |
| 2410 | const Py_UNICODE *collstart = p; |
| 2411 | const Py_UNICODE *collend = p; |
| 2412 | /* find all unecodable characters */ |
| 2413 | while ((collend < endp) && ((*collend)>=limit)) |
| 2414 | ++collend; |
| 2415 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 2416 | if (known_errorHandler==-1) { |
| 2417 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 2418 | known_errorHandler = 1; |
| 2419 | else if (!strcmp(errors, "replace")) |
| 2420 | known_errorHandler = 2; |
| 2421 | else if (!strcmp(errors, "ignore")) |
| 2422 | known_errorHandler = 3; |
| 2423 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 2424 | known_errorHandler = 4; |
| 2425 | else |
| 2426 | known_errorHandler = 0; |
| 2427 | } |
| 2428 | switch (known_errorHandler) { |
| 2429 | case 1: /* strict */ |
| 2430 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 2431 | goto onError; |
| 2432 | case 2: /* replace */ |
| 2433 | while (collstart++<collend) |
| 2434 | *str++ = '?'; /* fall through */ |
| 2435 | case 3: /* ignore */ |
| 2436 | p = collend; |
| 2437 | break; |
| 2438 | case 4: /* xmlcharrefreplace */ |
| 2439 | respos = str-PyString_AS_STRING(res); |
| 2440 | /* determine replacement size (temporarily (mis)uses p) */ |
| 2441 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 2442 | if (*p<10) |
| 2443 | repsize += 2+1+1; |
| 2444 | else if (*p<100) |
| 2445 | repsize += 2+2+1; |
| 2446 | else if (*p<1000) |
| 2447 | repsize += 2+3+1; |
| 2448 | else if (*p<10000) |
| 2449 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 2450 | #ifndef Py_UNICODE_WIDE |
| 2451 | else |
| 2452 | repsize += 2+5+1; |
| 2453 | #else |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2454 | else if (*p<100000) |
| 2455 | repsize += 2+5+1; |
| 2456 | else if (*p<1000000) |
| 2457 | repsize += 2+6+1; |
| 2458 | else |
| 2459 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2460 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2461 | } |
| 2462 | requiredsize = respos+repsize+(endp-collend); |
| 2463 | if (requiredsize > ressize) { |
| 2464 | if (requiredsize<2*ressize) |
| 2465 | requiredsize = 2*ressize; |
| 2466 | if (_PyString_Resize(&res, requiredsize)) |
| 2467 | goto onError; |
| 2468 | str = PyString_AS_STRING(res) + respos; |
| 2469 | ressize = requiredsize; |
| 2470 | } |
| 2471 | /* generate replacement (temporarily (mis)uses p) */ |
| 2472 | for (p = collstart; p < collend; ++p) { |
| 2473 | str += sprintf(str, "&#%d;", (int)*p); |
| 2474 | } |
| 2475 | p = collend; |
| 2476 | break; |
| 2477 | default: |
| 2478 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 2479 | encoding, reason, startp, size, &exc, |
| 2480 | collstart-startp, collend-startp, &newpos); |
| 2481 | if (repunicode == NULL) |
| 2482 | goto onError; |
| 2483 | /* need more space? (at least enough for what we |
| 2484 | have+the replacement+the rest of the string, so |
| 2485 | we won't have to check space for encodable characters) */ |
| 2486 | respos = str-PyString_AS_STRING(res); |
| 2487 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 2488 | requiredsize = respos+repsize+(endp-collend); |
| 2489 | if (requiredsize > ressize) { |
| 2490 | if (requiredsize<2*ressize) |
| 2491 | requiredsize = 2*ressize; |
| 2492 | if (_PyString_Resize(&res, requiredsize)) { |
| 2493 | Py_DECREF(repunicode); |
| 2494 | goto onError; |
| 2495 | } |
| 2496 | str = PyString_AS_STRING(res) + respos; |
| 2497 | ressize = requiredsize; |
| 2498 | } |
| 2499 | /* check if there is anything unencodable in the replacement |
| 2500 | and copy it to the output */ |
| 2501 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 2502 | c = *uni2; |
| 2503 | if (c >= limit) { |
| 2504 | raise_encode_exception(&exc, encoding, startp, size, |
| 2505 | unicodepos, unicodepos+1, reason); |
| 2506 | Py_DECREF(repunicode); |
| 2507 | goto onError; |
| 2508 | } |
| 2509 | *str = (char)c; |
| 2510 | } |
| 2511 | p = startp + newpos; |
| 2512 | Py_DECREF(repunicode); |
| 2513 | } |
| 2514 | } |
| 2515 | } |
| 2516 | /* Resize if we allocated to much */ |
| 2517 | respos = str-PyString_AS_STRING(res); |
| 2518 | if (respos<ressize) |
| 2519 | /* If this falls res will be NULL */ |
| 2520 | _PyString_Resize(&res, respos); |
| 2521 | Py_XDECREF(errorHandler); |
| 2522 | Py_XDECREF(exc); |
| 2523 | return res; |
| 2524 | |
| 2525 | onError: |
| 2526 | Py_XDECREF(res); |
| 2527 | Py_XDECREF(errorHandler); |
| 2528 | Py_XDECREF(exc); |
| 2529 | return NULL; |
| 2530 | } |
| 2531 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2532 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
| 2533 | int size, |
| 2534 | const char *errors) |
| 2535 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2536 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2537 | } |
| 2538 | |
| 2539 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 2540 | { |
| 2541 | if (!PyUnicode_Check(unicode)) { |
| 2542 | PyErr_BadArgument(); |
| 2543 | return NULL; |
| 2544 | } |
| 2545 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 2546 | PyUnicode_GET_SIZE(unicode), |
| 2547 | NULL); |
| 2548 | } |
| 2549 | |
| 2550 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 2551 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2552 | PyObject *PyUnicode_DecodeASCII(const char *s, |
| 2553 | int size, |
| 2554 | const char *errors) |
| 2555 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2556 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2557 | PyUnicodeObject *v; |
| 2558 | Py_UNICODE *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2559 | int startinpos; |
| 2560 | int endinpos; |
| 2561 | int outpos; |
| 2562 | const char *e; |
| 2563 | PyObject *errorHandler = NULL; |
| 2564 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2565 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2566 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 2567 | if (size == 1 && *(unsigned char*)s < 128) { |
| 2568 | Py_UNICODE r = *(unsigned char*)s; |
| 2569 | return PyUnicode_FromUnicode(&r, 1); |
| 2570 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2571 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2572 | v = _PyUnicode_New(size); |
| 2573 | if (v == NULL) |
| 2574 | goto onError; |
| 2575 | if (size == 0) |
| 2576 | return (PyObject *)v; |
| 2577 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2578 | e = s + size; |
| 2579 | while (s < e) { |
| 2580 | register unsigned char c = (unsigned char)*s; |
| 2581 | if (c < 128) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2582 | *p++ = c; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2583 | ++s; |
| 2584 | } |
| 2585 | else { |
| 2586 | startinpos = s-starts; |
| 2587 | endinpos = startinpos + 1; |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 2588 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2589 | if (unicode_decode_call_errorhandler( |
| 2590 | errors, &errorHandler, |
| 2591 | "ascii", "ordinal not in range(128)", |
| 2592 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2593 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2594 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2595 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2596 | } |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2597 | if (p - PyUnicode_AS_UNICODE(v) < PyString_GET_SIZE(v)) |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2598 | if (_PyUnicode_Resize(&v, (int)(p - PyUnicode_AS_UNICODE(v))) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2599 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2600 | Py_XDECREF(errorHandler); |
| 2601 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2602 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2603 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2604 | onError: |
| 2605 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2606 | Py_XDECREF(errorHandler); |
| 2607 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2608 | return NULL; |
| 2609 | } |
| 2610 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2611 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 2612 | int size, |
| 2613 | const char *errors) |
| 2614 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2615 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2616 | } |
| 2617 | |
| 2618 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 2619 | { |
| 2620 | if (!PyUnicode_Check(unicode)) { |
| 2621 | PyErr_BadArgument(); |
| 2622 | return NULL; |
| 2623 | } |
| 2624 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 2625 | PyUnicode_GET_SIZE(unicode), |
| 2626 | NULL); |
| 2627 | } |
| 2628 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2629 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2630 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2631 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2632 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2633 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
| 2634 | int size, |
| 2635 | const char *errors) |
| 2636 | { |
| 2637 | PyUnicodeObject *v; |
| 2638 | Py_UNICODE *p; |
| 2639 | |
| 2640 | /* First get the size of the result */ |
| 2641 | DWORD usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 2642 | if (size > 0 && usize==0) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2643 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2644 | |
| 2645 | v = _PyUnicode_New(usize); |
| 2646 | if (v == NULL) |
| 2647 | return NULL; |
| 2648 | if (usize == 0) |
| 2649 | return (PyObject *)v; |
| 2650 | p = PyUnicode_AS_UNICODE(v); |
| 2651 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 2652 | Py_DECREF(v); |
| 2653 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2654 | } |
| 2655 | |
| 2656 | return (PyObject *)v; |
| 2657 | } |
| 2658 | |
| 2659 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 2660 | int size, |
| 2661 | const char *errors) |
| 2662 | { |
| 2663 | PyObject *repr; |
| 2664 | char *s; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 2665 | DWORD mbcssize; |
| 2666 | |
| 2667 | /* If there are no characters, bail now! */ |
| 2668 | if (size==0) |
| 2669 | return PyString_FromString(""); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2670 | |
| 2671 | /* First get the size of the result */ |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 2672 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2673 | if (mbcssize==0) |
| 2674 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2675 | |
| 2676 | repr = PyString_FromStringAndSize(NULL, mbcssize); |
| 2677 | if (repr == NULL) |
| 2678 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2679 | if (mbcssize == 0) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2680 | return repr; |
| 2681 | |
| 2682 | /* Do the conversion */ |
| 2683 | s = PyString_AS_STRING(repr); |
| 2684 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 2685 | Py_DECREF(repr); |
| 2686 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2687 | } |
| 2688 | return repr; |
| 2689 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2690 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2691 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 2692 | { |
| 2693 | if (!PyUnicode_Check(unicode)) { |
| 2694 | PyErr_BadArgument(); |
| 2695 | return NULL; |
| 2696 | } |
| 2697 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2698 | PyUnicode_GET_SIZE(unicode), |
| 2699 | NULL); |
| 2700 | } |
| 2701 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2702 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2703 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2704 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 2705 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2706 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
| 2707 | int size, |
| 2708 | PyObject *mapping, |
| 2709 | const char *errors) |
| 2710 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2711 | const char *starts = s; |
| 2712 | int startinpos; |
| 2713 | int endinpos; |
| 2714 | int outpos; |
| 2715 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2716 | PyUnicodeObject *v; |
| 2717 | Py_UNICODE *p; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 2718 | int extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2719 | PyObject *errorHandler = NULL; |
| 2720 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2721 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2722 | /* Default to Latin-1 */ |
| 2723 | if (mapping == NULL) |
| 2724 | return PyUnicode_DecodeLatin1(s, size, errors); |
| 2725 | |
| 2726 | v = _PyUnicode_New(size); |
| 2727 | if (v == NULL) |
| 2728 | goto onError; |
| 2729 | if (size == 0) |
| 2730 | return (PyObject *)v; |
| 2731 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2732 | e = s + size; |
| 2733 | while (s < e) { |
| 2734 | unsigned char ch = *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2735 | PyObject *w, *x; |
| 2736 | |
| 2737 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 2738 | w = PyInt_FromLong((long)ch); |
| 2739 | if (w == NULL) |
| 2740 | goto onError; |
| 2741 | x = PyObject_GetItem(mapping, w); |
| 2742 | Py_DECREF(w); |
| 2743 | if (x == NULL) { |
| 2744 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
Marc-André Lemburg | a866df8 | 2001-01-03 21:29:14 +0000 | [diff] [blame] | 2745 | /* No mapping found means: mapping is undefined. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2746 | PyErr_Clear(); |
Marc-André Lemburg | a866df8 | 2001-01-03 21:29:14 +0000 | [diff] [blame] | 2747 | x = Py_None; |
| 2748 | Py_INCREF(x); |
| 2749 | } else |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 2750 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2751 | } |
| 2752 | |
| 2753 | /* Apply mapping */ |
| 2754 | if (PyInt_Check(x)) { |
Marc-André Lemburg | 85cc4d8 | 2000-07-06 19:43:31 +0000 | [diff] [blame] | 2755 | long value = PyInt_AS_LONG(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2756 | if (value < 0 || value > 65535) { |
| 2757 | PyErr_SetString(PyExc_TypeError, |
Marc-André Lemburg | 07ceb67 | 2000-06-10 09:32:51 +0000 | [diff] [blame] | 2758 | "character mapping must be in range(65536)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2759 | Py_DECREF(x); |
| 2760 | goto onError; |
| 2761 | } |
| 2762 | *p++ = (Py_UNICODE)value; |
| 2763 | } |
| 2764 | else if (x == Py_None) { |
| 2765 | /* undefined mapping */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2766 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2767 | startinpos = s-starts; |
| 2768 | endinpos = startinpos+1; |
| 2769 | if (unicode_decode_call_errorhandler( |
| 2770 | errors, &errorHandler, |
| 2771 | "charmap", "character maps to <undefined>", |
| 2772 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2773 | (PyObject **)&v, &outpos, &p)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2774 | Py_DECREF(x); |
| 2775 | goto onError; |
| 2776 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2777 | continue; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2778 | } |
| 2779 | else if (PyUnicode_Check(x)) { |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 2780 | int targetsize = PyUnicode_GET_SIZE(x); |
| 2781 | |
| 2782 | if (targetsize == 1) |
| 2783 | /* 1-1 mapping */ |
| 2784 | *p++ = *PyUnicode_AS_UNICODE(x); |
| 2785 | |
| 2786 | else if (targetsize > 1) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2787 | /* 1-n mapping */ |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 2788 | if (targetsize > extrachars) { |
| 2789 | /* resize first */ |
| 2790 | int oldpos = (int)(p - PyUnicode_AS_UNICODE(v)); |
| 2791 | int needed = (targetsize - extrachars) + \ |
| 2792 | (targetsize << 2); |
| 2793 | extrachars += needed; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2794 | if (_PyUnicode_Resize(&v, |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2795 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 2796 | Py_DECREF(x); |
| 2797 | goto onError; |
| 2798 | } |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 2799 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 2800 | } |
| 2801 | Py_UNICODE_COPY(p, |
| 2802 | PyUnicode_AS_UNICODE(x), |
| 2803 | targetsize); |
| 2804 | p += targetsize; |
| 2805 | extrachars -= targetsize; |
| 2806 | } |
| 2807 | /* 1-0 mapping: skip the character */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2808 | } |
| 2809 | else { |
| 2810 | /* wrong return value */ |
| 2811 | PyErr_SetString(PyExc_TypeError, |
| 2812 | "character mapping must return integer, None or unicode"); |
| 2813 | Py_DECREF(x); |
| 2814 | goto onError; |
| 2815 | } |
| 2816 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2817 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2818 | } |
| 2819 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2820 | if (_PyUnicode_Resize(&v, (int)(p - PyUnicode_AS_UNICODE(v))) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2821 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2822 | Py_XDECREF(errorHandler); |
| 2823 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2824 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2825 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2826 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2827 | Py_XDECREF(errorHandler); |
| 2828 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2829 | Py_XDECREF(v); |
| 2830 | return NULL; |
| 2831 | } |
| 2832 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2833 | /* Lookup the character ch in the mapping. If the character |
| 2834 | can't be found, Py_None is returned (or NULL, if another |
| 2835 | error occured). */ |
| 2836 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2837 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2838 | PyObject *w = PyInt_FromLong((long)c); |
| 2839 | PyObject *x; |
| 2840 | |
| 2841 | if (w == NULL) |
| 2842 | return NULL; |
| 2843 | x = PyObject_GetItem(mapping, w); |
| 2844 | Py_DECREF(w); |
| 2845 | if (x == NULL) { |
| 2846 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 2847 | /* No mapping found means: mapping is undefined. */ |
| 2848 | PyErr_Clear(); |
| 2849 | x = Py_None; |
| 2850 | Py_INCREF(x); |
| 2851 | return x; |
| 2852 | } else |
| 2853 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2854 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 2855 | else if (x == Py_None) |
| 2856 | return x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2857 | else if (PyInt_Check(x)) { |
| 2858 | long value = PyInt_AS_LONG(x); |
| 2859 | if (value < 0 || value > 255) { |
| 2860 | PyErr_SetString(PyExc_TypeError, |
| 2861 | "character mapping must be in range(256)"); |
| 2862 | Py_DECREF(x); |
| 2863 | return NULL; |
| 2864 | } |
| 2865 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2866 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2867 | else if (PyString_Check(x)) |
| 2868 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2869 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2870 | /* wrong return value */ |
| 2871 | PyErr_SetString(PyExc_TypeError, |
| 2872 | "character mapping must return integer, None or str"); |
| 2873 | Py_DECREF(x); |
| 2874 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2875 | } |
| 2876 | } |
| 2877 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2878 | /* lookup the character, put the result in the output string and adjust |
| 2879 | various state variables. Reallocate the output string if not enough |
| 2880 | space is available. Return a new reference to the object that |
| 2881 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 2882 | (in which case no character was written) or NULL, if a |
| 2883 | reallocation error ocurred. The called must decref the result */ |
| 2884 | static |
| 2885 | PyObject *charmapencode_output(Py_UNICODE c, PyObject *mapping, |
| 2886 | PyObject **outobj, int *outpos) |
| 2887 | { |
| 2888 | PyObject *rep = charmapencode_lookup(c, mapping); |
| 2889 | |
| 2890 | if (rep==NULL) |
| 2891 | return NULL; |
| 2892 | else if (rep==Py_None) |
| 2893 | return rep; |
| 2894 | else { |
| 2895 | char *outstart = PyString_AS_STRING(*outobj); |
| 2896 | int outsize = PyString_GET_SIZE(*outobj); |
| 2897 | if (PyInt_Check(rep)) { |
| 2898 | int requiredsize = *outpos+1; |
| 2899 | if (outsize<requiredsize) { |
| 2900 | /* exponentially overallocate to minimize reallocations */ |
| 2901 | if (requiredsize < 2*outsize) |
| 2902 | requiredsize = 2*outsize; |
| 2903 | if (_PyString_Resize(outobj, requiredsize)) { |
| 2904 | Py_DECREF(rep); |
| 2905 | return NULL; |
| 2906 | } |
| 2907 | outstart = PyString_AS_STRING(*outobj); |
| 2908 | } |
| 2909 | outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); |
| 2910 | } |
| 2911 | else { |
| 2912 | const char *repchars = PyString_AS_STRING(rep); |
| 2913 | int repsize = PyString_GET_SIZE(rep); |
| 2914 | int requiredsize = *outpos+repsize; |
| 2915 | if (outsize<requiredsize) { |
| 2916 | /* exponentially overallocate to minimize reallocations */ |
| 2917 | if (requiredsize < 2*outsize) |
| 2918 | requiredsize = 2*outsize; |
| 2919 | if (_PyString_Resize(outobj, requiredsize)) { |
| 2920 | Py_DECREF(rep); |
| 2921 | return NULL; |
| 2922 | } |
| 2923 | outstart = PyString_AS_STRING(*outobj); |
| 2924 | } |
| 2925 | memcpy(outstart + *outpos, repchars, repsize); |
| 2926 | *outpos += repsize; |
| 2927 | } |
| 2928 | } |
| 2929 | return rep; |
| 2930 | } |
| 2931 | |
| 2932 | /* handle an error in PyUnicode_EncodeCharmap |
| 2933 | Return 0 on success, -1 on error */ |
| 2934 | static |
| 2935 | int charmap_encoding_error( |
| 2936 | const Py_UNICODE *p, int size, int *inpos, PyObject *mapping, |
| 2937 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 2938 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2939 | PyObject **res, int *respos) |
| 2940 | { |
| 2941 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 2942 | int repsize; |
| 2943 | int newpos; |
| 2944 | Py_UNICODE *uni2; |
| 2945 | /* startpos for collecting unencodable chars */ |
| 2946 | int collstartpos = *inpos; |
| 2947 | int collendpos = *inpos+1; |
| 2948 | int collpos; |
| 2949 | char *encoding = "charmap"; |
| 2950 | char *reason = "character maps to <undefined>"; |
| 2951 | |
| 2952 | PyObject *x; |
| 2953 | /* find all unencodable characters */ |
| 2954 | while (collendpos < size) { |
| 2955 | x = charmapencode_lookup(p[collendpos], mapping); |
| 2956 | if (x==NULL) |
| 2957 | return -1; |
| 2958 | else if (x!=Py_None) { |
| 2959 | Py_DECREF(x); |
| 2960 | break; |
| 2961 | } |
| 2962 | Py_DECREF(x); |
| 2963 | ++collendpos; |
| 2964 | } |
| 2965 | /* cache callback name lookup |
| 2966 | * (if not done yet, i.e. it's the first error) */ |
| 2967 | if (*known_errorHandler==-1) { |
| 2968 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 2969 | *known_errorHandler = 1; |
| 2970 | else if (!strcmp(errors, "replace")) |
| 2971 | *known_errorHandler = 2; |
| 2972 | else if (!strcmp(errors, "ignore")) |
| 2973 | *known_errorHandler = 3; |
| 2974 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 2975 | *known_errorHandler = 4; |
| 2976 | else |
| 2977 | *known_errorHandler = 0; |
| 2978 | } |
| 2979 | switch (*known_errorHandler) { |
| 2980 | case 1: /* strict */ |
| 2981 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 2982 | return -1; |
| 2983 | case 2: /* replace */ |
| 2984 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 2985 | x = charmapencode_output('?', mapping, res, respos); |
| 2986 | if (x==NULL) { |
| 2987 | return -1; |
| 2988 | } |
| 2989 | else if (x==Py_None) { |
| 2990 | Py_DECREF(x); |
| 2991 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 2992 | return -1; |
| 2993 | } |
| 2994 | Py_DECREF(x); |
| 2995 | } |
| 2996 | /* fall through */ |
| 2997 | case 3: /* ignore */ |
| 2998 | *inpos = collendpos; |
| 2999 | break; |
| 3000 | case 4: /* xmlcharrefreplace */ |
| 3001 | /* generate replacement (temporarily (mis)uses p) */ |
| 3002 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 3003 | char buffer[2+29+1+1]; |
| 3004 | char *cp; |
| 3005 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 3006 | for (cp = buffer; *cp; ++cp) { |
| 3007 | x = charmapencode_output(*cp, mapping, res, respos); |
| 3008 | if (x==NULL) |
| 3009 | return -1; |
| 3010 | else if (x==Py_None) { |
| 3011 | Py_DECREF(x); |
| 3012 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3013 | return -1; |
| 3014 | } |
| 3015 | Py_DECREF(x); |
| 3016 | } |
| 3017 | } |
| 3018 | *inpos = collendpos; |
| 3019 | break; |
| 3020 | default: |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3021 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3022 | encoding, reason, p, size, exceptionObject, |
| 3023 | collstartpos, collendpos, &newpos); |
| 3024 | if (repunicode == NULL) |
| 3025 | return -1; |
| 3026 | /* generate replacement */ |
| 3027 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3028 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 3029 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 3030 | if (x==NULL) { |
| 3031 | Py_DECREF(repunicode); |
| 3032 | return -1; |
| 3033 | } |
| 3034 | else if (x==Py_None) { |
| 3035 | Py_DECREF(repunicode); |
| 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 | *inpos = newpos; |
| 3043 | Py_DECREF(repunicode); |
| 3044 | } |
| 3045 | return 0; |
| 3046 | } |
| 3047 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3048 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 3049 | int size, |
| 3050 | PyObject *mapping, |
| 3051 | const char *errors) |
| 3052 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3053 | /* output object */ |
| 3054 | PyObject *res = NULL; |
| 3055 | /* current input position */ |
| 3056 | int inpos = 0; |
| 3057 | /* current output position */ |
| 3058 | int respos = 0; |
| 3059 | PyObject *errorHandler = NULL; |
| 3060 | PyObject *exc = NULL; |
| 3061 | /* the following variable is used for caching string comparisons |
| 3062 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 3063 | * 3=ignore, 4=xmlcharrefreplace */ |
| 3064 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3065 | |
| 3066 | /* Default to Latin-1 */ |
| 3067 | if (mapping == NULL) |
| 3068 | return PyUnicode_EncodeLatin1(p, size, errors); |
| 3069 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3070 | /* allocate enough for a simple encoding without |
| 3071 | replacements, if we need more, we'll resize */ |
| 3072 | res = PyString_FromStringAndSize(NULL, size); |
| 3073 | if (res == NULL) |
| 3074 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3075 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3076 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3077 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3078 | while (inpos<size) { |
| 3079 | /* try to encode it */ |
| 3080 | PyObject *x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 3081 | if (x==NULL) /* error */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3082 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3083 | if (x==Py_None) { /* unencodable character */ |
| 3084 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 3085 | &exc, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3086 | &known_errorHandler, &errorHandler, errors, |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 3087 | &res, &respos)) { |
| 3088 | Py_DECREF(x); |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3089 | goto onError; |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 3090 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3091 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3092 | else |
| 3093 | /* done with this character => adjust input position */ |
| 3094 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3095 | Py_DECREF(x); |
| 3096 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3097 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3098 | /* Resize if we allocated to much */ |
| 3099 | if (respos<PyString_GET_SIZE(res)) { |
| 3100 | if (_PyString_Resize(&res, respos)) |
| 3101 | goto onError; |
| 3102 | } |
| 3103 | Py_XDECREF(exc); |
| 3104 | Py_XDECREF(errorHandler); |
| 3105 | return res; |
| 3106 | |
| 3107 | onError: |
| 3108 | Py_XDECREF(res); |
| 3109 | Py_XDECREF(exc); |
| 3110 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3111 | return NULL; |
| 3112 | } |
| 3113 | |
| 3114 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 3115 | PyObject *mapping) |
| 3116 | { |
| 3117 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 3118 | PyErr_BadArgument(); |
| 3119 | return NULL; |
| 3120 | } |
| 3121 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 3122 | PyUnicode_GET_SIZE(unicode), |
| 3123 | mapping, |
| 3124 | NULL); |
| 3125 | } |
| 3126 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3127 | /* create or adjust a UnicodeTranslateError */ |
| 3128 | static void make_translate_exception(PyObject **exceptionObject, |
| 3129 | const Py_UNICODE *unicode, int size, |
| 3130 | int startpos, int endpos, |
| 3131 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3132 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3133 | if (*exceptionObject == NULL) { |
| 3134 | *exceptionObject = PyUnicodeTranslateError_Create( |
| 3135 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3136 | } |
| 3137 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3138 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 3139 | goto onError; |
| 3140 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 3141 | goto onError; |
| 3142 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 3143 | goto onError; |
| 3144 | return; |
| 3145 | onError: |
| 3146 | Py_DECREF(*exceptionObject); |
| 3147 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3148 | } |
| 3149 | } |
| 3150 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3151 | /* raises a UnicodeTranslateError */ |
| 3152 | static void raise_translate_exception(PyObject **exceptionObject, |
| 3153 | const Py_UNICODE *unicode, int size, |
| 3154 | int startpos, int endpos, |
| 3155 | const char *reason) |
| 3156 | { |
| 3157 | make_translate_exception(exceptionObject, |
| 3158 | unicode, size, startpos, endpos, reason); |
| 3159 | if (*exceptionObject != NULL) |
| 3160 | PyCodec_StrictErrors(*exceptionObject); |
| 3161 | } |
| 3162 | |
| 3163 | /* error handling callback helper: |
| 3164 | build arguments, call the callback and check the arguments, |
| 3165 | put the result into newpos and return the replacement string, which |
| 3166 | has to be freed by the caller */ |
| 3167 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 3168 | PyObject **errorHandler, |
| 3169 | const char *reason, |
| 3170 | const Py_UNICODE *unicode, int size, PyObject **exceptionObject, |
| 3171 | int startpos, int endpos, |
| 3172 | int *newpos) |
| 3173 | { |
| 3174 | static char *argparse = "O!i;translating error handler must return (unicode, int) tuple"; |
| 3175 | |
| 3176 | PyObject *restuple; |
| 3177 | PyObject *resunicode; |
| 3178 | |
| 3179 | if (*errorHandler == NULL) { |
| 3180 | *errorHandler = PyCodec_LookupError(errors); |
| 3181 | if (*errorHandler == NULL) |
| 3182 | return NULL; |
| 3183 | } |
| 3184 | |
| 3185 | make_translate_exception(exceptionObject, |
| 3186 | unicode, size, startpos, endpos, reason); |
| 3187 | if (*exceptionObject == NULL) |
| 3188 | return NULL; |
| 3189 | |
| 3190 | restuple = PyObject_CallFunctionObjArgs( |
| 3191 | *errorHandler, *exceptionObject, NULL); |
| 3192 | if (restuple == NULL) |
| 3193 | return NULL; |
| 3194 | if (!PyTuple_Check(restuple)) { |
| 3195 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 3196 | Py_DECREF(restuple); |
| 3197 | return NULL; |
| 3198 | } |
| 3199 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 3200 | &resunicode, newpos)) { |
| 3201 | Py_DECREF(restuple); |
| 3202 | return NULL; |
| 3203 | } |
| 3204 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3205 | *newpos = size+*newpos; |
| 3206 | if (*newpos<0 || *newpos>size) { |
| 3207 | PyErr_Format(PyExc_IndexError, "position %d from error handler out of bounds", *newpos); |
| 3208 | Py_DECREF(restuple); |
| 3209 | return NULL; |
| 3210 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3211 | Py_INCREF(resunicode); |
| 3212 | Py_DECREF(restuple); |
| 3213 | return resunicode; |
| 3214 | } |
| 3215 | |
| 3216 | /* Lookup the character ch in the mapping and put the result in result, |
| 3217 | which must be decrefed by the caller. |
| 3218 | Return 0 on success, -1 on error */ |
| 3219 | static |
| 3220 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 3221 | { |
| 3222 | PyObject *w = PyInt_FromLong((long)c); |
| 3223 | PyObject *x; |
| 3224 | |
| 3225 | if (w == NULL) |
| 3226 | return -1; |
| 3227 | x = PyObject_GetItem(mapping, w); |
| 3228 | Py_DECREF(w); |
| 3229 | if (x == NULL) { |
| 3230 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3231 | /* No mapping found means: use 1:1 mapping. */ |
| 3232 | PyErr_Clear(); |
| 3233 | *result = NULL; |
| 3234 | return 0; |
| 3235 | } else |
| 3236 | return -1; |
| 3237 | } |
| 3238 | else if (x == Py_None) { |
| 3239 | *result = x; |
| 3240 | return 0; |
| 3241 | } |
| 3242 | else if (PyInt_Check(x)) { |
| 3243 | long value = PyInt_AS_LONG(x); |
| 3244 | long max = PyUnicode_GetMax(); |
| 3245 | if (value < 0 || value > max) { |
| 3246 | PyErr_Format(PyExc_TypeError, |
| 3247 | "character mapping must be in range(0x%lx)", max+1); |
| 3248 | Py_DECREF(x); |
| 3249 | return -1; |
| 3250 | } |
| 3251 | *result = x; |
| 3252 | return 0; |
| 3253 | } |
| 3254 | else if (PyUnicode_Check(x)) { |
| 3255 | *result = x; |
| 3256 | return 0; |
| 3257 | } |
| 3258 | else { |
| 3259 | /* wrong return value */ |
| 3260 | PyErr_SetString(PyExc_TypeError, |
| 3261 | "character mapping must return integer, None or unicode"); |
Walter Dörwald | 150523e | 2003-08-15 16:52:19 +0000 | [diff] [blame] | 3262 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3263 | return -1; |
| 3264 | } |
| 3265 | } |
| 3266 | /* ensure that *outobj is at least requiredsize characters long, |
| 3267 | if not reallocate and adjust various state variables. |
| 3268 | Return 0 on success, -1 on error */ |
| 3269 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3270 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3271 | int requiredsize) |
| 3272 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3273 | int oldsize = PyUnicode_GET_SIZE(*outobj); |
| 3274 | if (requiredsize > oldsize) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3275 | /* remember old output position */ |
| 3276 | int outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
| 3277 | /* exponentially overallocate to minimize reallocations */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3278 | if (requiredsize < 2 * oldsize) |
| 3279 | requiredsize = 2 * oldsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3280 | if (_PyUnicode_Resize(outobj, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3281 | return -1; |
| 3282 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3283 | } |
| 3284 | return 0; |
| 3285 | } |
| 3286 | /* lookup the character, put the result in the output string and adjust |
| 3287 | various state variables. Return a new reference to the object that |
| 3288 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 3289 | undefined (in which case no character was written). |
| 3290 | The called must decref result. |
| 3291 | Return 0 on success, -1 on error. */ |
| 3292 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3293 | int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
| 3294 | int insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
| 3295 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3296 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3297 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3298 | return -1; |
| 3299 | if (*res==NULL) { |
| 3300 | /* not found => default to 1:1 mapping */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3301 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3302 | } |
| 3303 | else if (*res==Py_None) |
| 3304 | ; |
| 3305 | else if (PyInt_Check(*res)) { |
| 3306 | /* no overflow check, because we know that the space is enough */ |
| 3307 | *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); |
| 3308 | } |
| 3309 | else if (PyUnicode_Check(*res)) { |
| 3310 | int repsize = PyUnicode_GET_SIZE(*res); |
| 3311 | if (repsize==1) { |
| 3312 | /* no overflow check, because we know that the space is enough */ |
| 3313 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 3314 | } |
| 3315 | else if (repsize!=0) { |
| 3316 | /* more than one character */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3317 | int requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
Walter Dörwald | cd736e7 | 2004-02-05 17:36:00 +0000 | [diff] [blame] | 3318 | (insize - (curinp-startinp)) + |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3319 | repsize - 1; |
| 3320 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3321 | return -1; |
| 3322 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 3323 | *outp += repsize; |
| 3324 | } |
| 3325 | } |
| 3326 | else |
| 3327 | return -1; |
| 3328 | return 0; |
| 3329 | } |
| 3330 | |
| 3331 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3332 | int size, |
| 3333 | PyObject *mapping, |
| 3334 | const char *errors) |
| 3335 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3336 | /* output object */ |
| 3337 | PyObject *res = NULL; |
| 3338 | /* pointers to the beginning and end+1 of input */ |
| 3339 | const Py_UNICODE *startp = p; |
| 3340 | const Py_UNICODE *endp = p + size; |
| 3341 | /* pointer into the output */ |
| 3342 | Py_UNICODE *str; |
| 3343 | /* current output position */ |
| 3344 | int respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3345 | char *reason = "character maps to <undefined>"; |
| 3346 | PyObject *errorHandler = NULL; |
| 3347 | PyObject *exc = NULL; |
| 3348 | /* the following variable is used for caching string comparisons |
| 3349 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 3350 | * 3=ignore, 4=xmlcharrefreplace */ |
| 3351 | int known_errorHandler = -1; |
| 3352 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3353 | if (mapping == NULL) { |
| 3354 | PyErr_BadArgument(); |
| 3355 | return NULL; |
| 3356 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3357 | |
| 3358 | /* allocate enough for a simple 1:1 translation without |
| 3359 | replacements, if we need more, we'll resize */ |
| 3360 | res = PyUnicode_FromUnicode(NULL, size); |
| 3361 | if (res == NULL) |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3362 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3363 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3364 | return res; |
| 3365 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3366 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3367 | while (p<endp) { |
| 3368 | /* try to encode it */ |
| 3369 | PyObject *x = NULL; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3370 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3371 | Py_XDECREF(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3372 | goto onError; |
| 3373 | } |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 3374 | Py_XDECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3375 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 3376 | ++p; |
| 3377 | else { /* untranslatable character */ |
| 3378 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 3379 | int repsize; |
| 3380 | int newpos; |
| 3381 | Py_UNICODE *uni2; |
| 3382 | /* startpos for collecting untranslatable chars */ |
| 3383 | const Py_UNICODE *collstart = p; |
| 3384 | const Py_UNICODE *collend = p+1; |
| 3385 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3386 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3387 | /* find all untranslatable characters */ |
| 3388 | while (collend < endp) { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3389 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3390 | goto onError; |
| 3391 | Py_XDECREF(x); |
| 3392 | if (x!=Py_None) |
| 3393 | break; |
| 3394 | ++collend; |
| 3395 | } |
| 3396 | /* cache callback name lookup |
| 3397 | * (if not done yet, i.e. it's the first error) */ |
| 3398 | if (known_errorHandler==-1) { |
| 3399 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3400 | known_errorHandler = 1; |
| 3401 | else if (!strcmp(errors, "replace")) |
| 3402 | known_errorHandler = 2; |
| 3403 | else if (!strcmp(errors, "ignore")) |
| 3404 | known_errorHandler = 3; |
| 3405 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3406 | known_errorHandler = 4; |
| 3407 | else |
| 3408 | known_errorHandler = 0; |
| 3409 | } |
| 3410 | switch (known_errorHandler) { |
| 3411 | case 1: /* strict */ |
| 3412 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
| 3413 | goto onError; |
| 3414 | case 2: /* replace */ |
| 3415 | /* No need to check for space, this is a 1:1 replacement */ |
| 3416 | for (coll = collstart; coll<collend; ++coll) |
| 3417 | *str++ = '?'; |
| 3418 | /* fall through */ |
| 3419 | case 3: /* ignore */ |
| 3420 | p = collend; |
| 3421 | break; |
| 3422 | case 4: /* xmlcharrefreplace */ |
| 3423 | /* generate replacement (temporarily (mis)uses p) */ |
| 3424 | for (p = collstart; p < collend; ++p) { |
| 3425 | char buffer[2+29+1+1]; |
| 3426 | char *cp; |
| 3427 | sprintf(buffer, "&#%d;", (int)*p); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3428 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3429 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 3430 | goto onError; |
| 3431 | for (cp = buffer; *cp; ++cp) |
| 3432 | *str++ = *cp; |
| 3433 | } |
| 3434 | p = collend; |
| 3435 | break; |
| 3436 | default: |
| 3437 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 3438 | reason, startp, size, &exc, |
| 3439 | collstart-startp, collend-startp, &newpos); |
| 3440 | if (repunicode == NULL) |
| 3441 | goto onError; |
| 3442 | /* generate replacement */ |
| 3443 | repsize = PyUnicode_GET_SIZE(repunicode); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3444 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3445 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 3446 | Py_DECREF(repunicode); |
| 3447 | goto onError; |
| 3448 | } |
| 3449 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 3450 | *str++ = *uni2; |
| 3451 | p = startp + newpos; |
| 3452 | Py_DECREF(repunicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3453 | } |
| 3454 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3455 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3456 | /* Resize if we allocated to much */ |
| 3457 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3458 | if (respos<PyUnicode_GET_SIZE(res)) { |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3459 | if (_PyUnicode_Resize(&res, respos) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3460 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3461 | } |
| 3462 | Py_XDECREF(exc); |
| 3463 | Py_XDECREF(errorHandler); |
| 3464 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3465 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3466 | onError: |
| 3467 | Py_XDECREF(res); |
| 3468 | Py_XDECREF(exc); |
| 3469 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3470 | return NULL; |
| 3471 | } |
| 3472 | |
| 3473 | PyObject *PyUnicode_Translate(PyObject *str, |
| 3474 | PyObject *mapping, |
| 3475 | const char *errors) |
| 3476 | { |
| 3477 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3478 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3479 | str = PyUnicode_FromObject(str); |
| 3480 | if (str == NULL) |
| 3481 | goto onError; |
| 3482 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 3483 | PyUnicode_GET_SIZE(str), |
| 3484 | mapping, |
| 3485 | errors); |
| 3486 | Py_DECREF(str); |
| 3487 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3488 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3489 | onError: |
| 3490 | Py_XDECREF(str); |
| 3491 | return NULL; |
| 3492 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3493 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3494 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 3495 | |
| 3496 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 3497 | int length, |
| 3498 | char *output, |
| 3499 | const char *errors) |
| 3500 | { |
| 3501 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3502 | PyObject *errorHandler = NULL; |
| 3503 | PyObject *exc = NULL; |
| 3504 | const char *encoding = "decimal"; |
| 3505 | const char *reason = "invalid decimal Unicode string"; |
| 3506 | /* the following variable is used for caching string comparisons |
| 3507 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 3508 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3509 | |
| 3510 | if (output == NULL) { |
| 3511 | PyErr_BadArgument(); |
| 3512 | return -1; |
| 3513 | } |
| 3514 | |
| 3515 | p = s; |
| 3516 | end = s + length; |
| 3517 | while (p < end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3518 | register Py_UNICODE ch = *p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3519 | int decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3520 | PyObject *repunicode; |
| 3521 | int repsize; |
| 3522 | int newpos; |
| 3523 | Py_UNICODE *uni2; |
| 3524 | Py_UNICODE *collstart; |
| 3525 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3526 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3527 | if (Py_UNICODE_ISSPACE(ch)) { |
| 3528 | *output++ = ' '; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3529 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3530 | continue; |
| 3531 | } |
| 3532 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 3533 | if (decimal >= 0) { |
| 3534 | *output++ = '0' + decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3535 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3536 | continue; |
| 3537 | } |
Guido van Rossum | ba47704 | 2000-04-06 18:18:10 +0000 | [diff] [blame] | 3538 | if (0 < ch && ch < 256) { |
Guido van Rossum | 42c29aa | 2000-05-03 23:58:29 +0000 | [diff] [blame] | 3539 | *output++ = (char)ch; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3540 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3541 | continue; |
| 3542 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3543 | /* All other characters are considered unencodable */ |
| 3544 | collstart = p; |
| 3545 | collend = p+1; |
| 3546 | while (collend < end) { |
| 3547 | if ((0 < *collend && *collend < 256) || |
| 3548 | !Py_UNICODE_ISSPACE(*collend) || |
| 3549 | Py_UNICODE_TODECIMAL(*collend)) |
| 3550 | break; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3551 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3552 | /* cache callback name lookup |
| 3553 | * (if not done yet, i.e. it's the first error) */ |
| 3554 | if (known_errorHandler==-1) { |
| 3555 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3556 | known_errorHandler = 1; |
| 3557 | else if (!strcmp(errors, "replace")) |
| 3558 | known_errorHandler = 2; |
| 3559 | else if (!strcmp(errors, "ignore")) |
| 3560 | known_errorHandler = 3; |
| 3561 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3562 | known_errorHandler = 4; |
| 3563 | else |
| 3564 | known_errorHandler = 0; |
| 3565 | } |
| 3566 | switch (known_errorHandler) { |
| 3567 | case 1: /* strict */ |
| 3568 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 3569 | goto onError; |
| 3570 | case 2: /* replace */ |
| 3571 | for (p = collstart; p < collend; ++p) |
| 3572 | *output++ = '?'; |
| 3573 | /* fall through */ |
| 3574 | case 3: /* ignore */ |
| 3575 | p = collend; |
| 3576 | break; |
| 3577 | case 4: /* xmlcharrefreplace */ |
| 3578 | /* generate replacement (temporarily (mis)uses p) */ |
| 3579 | for (p = collstart; p < collend; ++p) |
| 3580 | output += sprintf(output, "&#%d;", (int)*p); |
| 3581 | p = collend; |
| 3582 | break; |
| 3583 | default: |
| 3584 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 3585 | encoding, reason, s, length, &exc, |
| 3586 | collstart-s, collend-s, &newpos); |
| 3587 | if (repunicode == NULL) |
| 3588 | goto onError; |
| 3589 | /* generate replacement */ |
| 3590 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3591 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 3592 | Py_UNICODE ch = *uni2; |
| 3593 | if (Py_UNICODE_ISSPACE(ch)) |
| 3594 | *output++ = ' '; |
| 3595 | else { |
| 3596 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 3597 | if (decimal >= 0) |
| 3598 | *output++ = '0' + decimal; |
| 3599 | else if (0 < ch && ch < 256) |
| 3600 | *output++ = (char)ch; |
| 3601 | else { |
| 3602 | Py_DECREF(repunicode); |
| 3603 | raise_encode_exception(&exc, encoding, |
| 3604 | s, length, collstart-s, collend-s, reason); |
| 3605 | goto onError; |
| 3606 | } |
| 3607 | } |
| 3608 | } |
| 3609 | p = s + newpos; |
| 3610 | Py_DECREF(repunicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3611 | } |
| 3612 | } |
| 3613 | /* 0-terminate the output string */ |
| 3614 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3615 | Py_XDECREF(exc); |
| 3616 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3617 | return 0; |
| 3618 | |
| 3619 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3620 | Py_XDECREF(exc); |
| 3621 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3622 | return -1; |
| 3623 | } |
| 3624 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3625 | /* --- Helpers ------------------------------------------------------------ */ |
| 3626 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3627 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3628 | int count(PyUnicodeObject *self, |
| 3629 | int start, |
| 3630 | int end, |
| 3631 | PyUnicodeObject *substring) |
| 3632 | { |
| 3633 | int count = 0; |
| 3634 | |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3635 | if (start < 0) |
| 3636 | start += self->length; |
| 3637 | if (start < 0) |
| 3638 | start = 0; |
| 3639 | if (end > self->length) |
| 3640 | end = self->length; |
| 3641 | if (end < 0) |
| 3642 | end += self->length; |
| 3643 | if (end < 0) |
| 3644 | end = 0; |
| 3645 | |
Marc-André Lemburg | 49ef6dc | 2000-06-18 22:25:22 +0000 | [diff] [blame] | 3646 | if (substring->length == 0) |
| 3647 | return (end - start + 1); |
| 3648 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3649 | end -= substring->length; |
| 3650 | |
| 3651 | while (start <= end) |
| 3652 | if (Py_UNICODE_MATCH(self, start, substring)) { |
| 3653 | count++; |
| 3654 | start += substring->length; |
| 3655 | } else |
| 3656 | start++; |
| 3657 | |
| 3658 | return count; |
| 3659 | } |
| 3660 | |
| 3661 | int PyUnicode_Count(PyObject *str, |
| 3662 | PyObject *substr, |
| 3663 | int start, |
| 3664 | int end) |
| 3665 | { |
| 3666 | int result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3667 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3668 | str = PyUnicode_FromObject(str); |
| 3669 | if (str == NULL) |
| 3670 | return -1; |
| 3671 | substr = PyUnicode_FromObject(substr); |
| 3672 | if (substr == NULL) { |
Marc-André Lemburg | 49ef6dc | 2000-06-18 22:25:22 +0000 | [diff] [blame] | 3673 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3674 | return -1; |
| 3675 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3676 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3677 | result = count((PyUnicodeObject *)str, |
| 3678 | start, end, |
| 3679 | (PyUnicodeObject *)substr); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3680 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3681 | Py_DECREF(str); |
| 3682 | Py_DECREF(substr); |
| 3683 | return result; |
| 3684 | } |
| 3685 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3686 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3687 | int findstring(PyUnicodeObject *self, |
| 3688 | PyUnicodeObject *substring, |
| 3689 | int start, |
| 3690 | int end, |
| 3691 | int direction) |
| 3692 | { |
| 3693 | if (start < 0) |
| 3694 | start += self->length; |
| 3695 | if (start < 0) |
| 3696 | start = 0; |
| 3697 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3698 | if (end > self->length) |
| 3699 | end = self->length; |
| 3700 | if (end < 0) |
| 3701 | end += self->length; |
| 3702 | if (end < 0) |
| 3703 | end = 0; |
| 3704 | |
Guido van Rossum | 76afbd9 | 2002-08-20 17:29:29 +0000 | [diff] [blame] | 3705 | if (substring->length == 0) |
| 3706 | return (direction > 0) ? start : end; |
| 3707 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3708 | end -= substring->length; |
| 3709 | |
| 3710 | if (direction < 0) { |
| 3711 | for (; end >= start; end--) |
| 3712 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 3713 | return end; |
| 3714 | } else { |
| 3715 | for (; start <= end; start++) |
| 3716 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 3717 | return start; |
| 3718 | } |
| 3719 | |
| 3720 | return -1; |
| 3721 | } |
| 3722 | |
| 3723 | int PyUnicode_Find(PyObject *str, |
| 3724 | PyObject *substr, |
| 3725 | int start, |
| 3726 | int end, |
| 3727 | int direction) |
| 3728 | { |
| 3729 | int result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3730 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3731 | str = PyUnicode_FromObject(str); |
| 3732 | if (str == NULL) |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 3733 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3734 | substr = PyUnicode_FromObject(substr); |
| 3735 | if (substr == NULL) { |
Marc-André Lemburg | 4164439 | 2002-05-29 13:46:29 +0000 | [diff] [blame] | 3736 | Py_DECREF(str); |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 3737 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3738 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3739 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3740 | result = findstring((PyUnicodeObject *)str, |
| 3741 | (PyUnicodeObject *)substr, |
| 3742 | start, end, direction); |
| 3743 | Py_DECREF(str); |
| 3744 | Py_DECREF(substr); |
| 3745 | return result; |
| 3746 | } |
| 3747 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3748 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3749 | int tailmatch(PyUnicodeObject *self, |
| 3750 | PyUnicodeObject *substring, |
| 3751 | int start, |
| 3752 | int end, |
| 3753 | int direction) |
| 3754 | { |
| 3755 | if (start < 0) |
| 3756 | start += self->length; |
| 3757 | if (start < 0) |
| 3758 | start = 0; |
| 3759 | |
| 3760 | if (substring->length == 0) |
| 3761 | return 1; |
| 3762 | |
| 3763 | if (end > self->length) |
| 3764 | end = self->length; |
| 3765 | if (end < 0) |
| 3766 | end += self->length; |
| 3767 | if (end < 0) |
| 3768 | end = 0; |
| 3769 | |
| 3770 | end -= substring->length; |
| 3771 | if (end < start) |
| 3772 | return 0; |
| 3773 | |
| 3774 | if (direction > 0) { |
| 3775 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 3776 | return 1; |
| 3777 | } else { |
| 3778 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 3779 | return 1; |
| 3780 | } |
| 3781 | |
| 3782 | return 0; |
| 3783 | } |
| 3784 | |
| 3785 | int PyUnicode_Tailmatch(PyObject *str, |
| 3786 | PyObject *substr, |
| 3787 | int start, |
| 3788 | int end, |
| 3789 | int direction) |
| 3790 | { |
| 3791 | int result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3792 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3793 | str = PyUnicode_FromObject(str); |
| 3794 | if (str == NULL) |
| 3795 | return -1; |
| 3796 | substr = PyUnicode_FromObject(substr); |
| 3797 | if (substr == NULL) { |
| 3798 | Py_DECREF(substr); |
| 3799 | return -1; |
| 3800 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3801 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3802 | result = tailmatch((PyUnicodeObject *)str, |
| 3803 | (PyUnicodeObject *)substr, |
| 3804 | start, end, direction); |
| 3805 | Py_DECREF(str); |
| 3806 | Py_DECREF(substr); |
| 3807 | return result; |
| 3808 | } |
| 3809 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3810 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3811 | const Py_UNICODE *findchar(const Py_UNICODE *s, |
| 3812 | int size, |
| 3813 | Py_UNICODE ch) |
| 3814 | { |
| 3815 | /* like wcschr, but doesn't stop at NULL characters */ |
| 3816 | |
| 3817 | while (size-- > 0) { |
| 3818 | if (*s == ch) |
| 3819 | return s; |
| 3820 | s++; |
| 3821 | } |
| 3822 | |
| 3823 | return NULL; |
| 3824 | } |
| 3825 | |
| 3826 | /* Apply fixfct filter to the Unicode object self and return a |
| 3827 | reference to the modified object */ |
| 3828 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3829 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3830 | PyObject *fixup(PyUnicodeObject *self, |
| 3831 | int (*fixfct)(PyUnicodeObject *s)) |
| 3832 | { |
| 3833 | |
| 3834 | PyUnicodeObject *u; |
| 3835 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3836 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3837 | if (u == NULL) |
| 3838 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3839 | |
| 3840 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 3841 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 3842 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3843 | /* fixfct should return TRUE if it modified the buffer. If |
| 3844 | FALSE, return a reference to the original buffer instead |
| 3845 | (to save space, not time) */ |
| 3846 | Py_INCREF(self); |
| 3847 | Py_DECREF(u); |
| 3848 | return (PyObject*) self; |
| 3849 | } |
| 3850 | return (PyObject*) u; |
| 3851 | } |
| 3852 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3853 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3854 | int fixupper(PyUnicodeObject *self) |
| 3855 | { |
| 3856 | int len = self->length; |
| 3857 | Py_UNICODE *s = self->str; |
| 3858 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3859 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3860 | while (len-- > 0) { |
| 3861 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3862 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3863 | ch = Py_UNICODE_TOUPPER(*s); |
| 3864 | if (ch != *s) { |
| 3865 | status = 1; |
| 3866 | *s = ch; |
| 3867 | } |
| 3868 | s++; |
| 3869 | } |
| 3870 | |
| 3871 | return status; |
| 3872 | } |
| 3873 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3874 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3875 | int fixlower(PyUnicodeObject *self) |
| 3876 | { |
| 3877 | int len = self->length; |
| 3878 | Py_UNICODE *s = self->str; |
| 3879 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3880 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3881 | while (len-- > 0) { |
| 3882 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3883 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3884 | ch = Py_UNICODE_TOLOWER(*s); |
| 3885 | if (ch != *s) { |
| 3886 | status = 1; |
| 3887 | *s = ch; |
| 3888 | } |
| 3889 | s++; |
| 3890 | } |
| 3891 | |
| 3892 | return status; |
| 3893 | } |
| 3894 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3895 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3896 | int fixswapcase(PyUnicodeObject *self) |
| 3897 | { |
| 3898 | int len = self->length; |
| 3899 | Py_UNICODE *s = self->str; |
| 3900 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3901 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3902 | while (len-- > 0) { |
| 3903 | if (Py_UNICODE_ISUPPER(*s)) { |
| 3904 | *s = Py_UNICODE_TOLOWER(*s); |
| 3905 | status = 1; |
| 3906 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 3907 | *s = Py_UNICODE_TOUPPER(*s); |
| 3908 | status = 1; |
| 3909 | } |
| 3910 | s++; |
| 3911 | } |
| 3912 | |
| 3913 | return status; |
| 3914 | } |
| 3915 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3916 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3917 | int fixcapitalize(PyUnicodeObject *self) |
| 3918 | { |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 3919 | int len = self->length; |
| 3920 | Py_UNICODE *s = self->str; |
| 3921 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3922 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 3923 | if (len == 0) |
| 3924 | return 0; |
| 3925 | if (Py_UNICODE_ISLOWER(*s)) { |
| 3926 | *s = Py_UNICODE_TOUPPER(*s); |
| 3927 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3928 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 3929 | s++; |
| 3930 | while (--len > 0) { |
| 3931 | if (Py_UNICODE_ISUPPER(*s)) { |
| 3932 | *s = Py_UNICODE_TOLOWER(*s); |
| 3933 | status = 1; |
| 3934 | } |
| 3935 | s++; |
| 3936 | } |
| 3937 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3938 | } |
| 3939 | |
| 3940 | static |
| 3941 | int fixtitle(PyUnicodeObject *self) |
| 3942 | { |
| 3943 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 3944 | register Py_UNICODE *e; |
| 3945 | int previous_is_cased; |
| 3946 | |
| 3947 | /* Shortcut for single character strings */ |
| 3948 | if (PyUnicode_GET_SIZE(self) == 1) { |
| 3949 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 3950 | if (*p != ch) { |
| 3951 | *p = ch; |
| 3952 | return 1; |
| 3953 | } |
| 3954 | else |
| 3955 | return 0; |
| 3956 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3957 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3958 | e = p + PyUnicode_GET_SIZE(self); |
| 3959 | previous_is_cased = 0; |
| 3960 | for (; p < e; p++) { |
| 3961 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3962 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3963 | if (previous_is_cased) |
| 3964 | *p = Py_UNICODE_TOLOWER(ch); |
| 3965 | else |
| 3966 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3967 | |
| 3968 | if (Py_UNICODE_ISLOWER(ch) || |
| 3969 | Py_UNICODE_ISUPPER(ch) || |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3970 | Py_UNICODE_ISTITLE(ch)) |
| 3971 | previous_is_cased = 1; |
| 3972 | else |
| 3973 | previous_is_cased = 0; |
| 3974 | } |
| 3975 | return 1; |
| 3976 | } |
| 3977 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 3978 | PyObject * |
| 3979 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3980 | { |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 3981 | PyObject *internal_separator = NULL; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame^] | 3982 | const Py_UNICODE *sep; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 3983 | size_t seplen; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame^] | 3984 | PyUnicodeObject *res = NULL; /* the result */ |
| 3985 | size_t res_alloc = 100; /* # allocated bytes for string in res */ |
| 3986 | size_t res_used; /* # used bytes */ |
| 3987 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 3988 | PyObject *fseq; /* PySequence_Fast(seq) */ |
| 3989 | int seqlen; /* len(fseq) -- number of items in sequence */ |
| 3990 | const Py_UNICODE blank = ' '; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 3991 | PyObject *item; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3992 | int i; |
| 3993 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame^] | 3994 | fseq = PySequence_Fast(seq, ""); |
| 3995 | if (fseq == NULL) { |
| 3996 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 3997 | PyErr_Format(PyExc_TypeError, |
| 3998 | "sequence expected, %.80s found", |
| 3999 | seq->ob_type->tp_name); |
| 4000 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4001 | } |
| 4002 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame^] | 4003 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 4004 | /* If empty sequence, return u"". */ |
| 4005 | if (seqlen == 0) { |
| 4006 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 4007 | goto Done; |
| 4008 | } |
| 4009 | /* If singleton sequence with an exact Unicode, return that. */ |
| 4010 | if (seqlen == 1) { |
| 4011 | item = PySequence_Fast_GET_ITEM(fseq, 0); |
| 4012 | if (PyUnicode_CheckExact(item)) { |
| 4013 | Py_INCREF(item); |
| 4014 | res = (PyUnicodeObject *)item; |
| 4015 | goto Done; |
| 4016 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4017 | } |
| 4018 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame^] | 4019 | /* At least two items to join, or one that isn't exact Unicode. */ |
| 4020 | if (seqlen > 1) { |
| 4021 | /* Set up sep and seplen -- they're needed. */ |
| 4022 | if (separator == NULL) { |
| 4023 | sep = ␣ |
| 4024 | seplen = 1; |
| 4025 | } |
| 4026 | else { |
| 4027 | internal_separator = PyUnicode_FromObject(separator); |
| 4028 | if (internal_separator == NULL) |
| 4029 | goto onError; |
| 4030 | sep = PyUnicode_AS_UNICODE(internal_separator); |
| 4031 | seplen = PyUnicode_GET_SIZE(internal_separator); |
| 4032 | } |
| 4033 | } |
| 4034 | |
| 4035 | /* Get space. */ |
| 4036 | res = _PyUnicode_New((int)res_alloc); |
| 4037 | if (res == NULL) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4038 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame^] | 4039 | res_p = PyUnicode_AS_UNICODE(res); |
| 4040 | res_used = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4041 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame^] | 4042 | for (i = 0; i < seqlen; ++i) { |
| 4043 | size_t itemlen; |
| 4044 | size_t new_res_used; |
| 4045 | |
| 4046 | item = PySequence_Fast_GET_ITEM(fseq, i); |
| 4047 | /* Convert item to Unicode. */ |
| 4048 | if (! PyUnicode_Check(item) && ! 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); |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 4053 | goto onError; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4054 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame^] | 4055 | item = PyUnicode_FromObject(item); |
| 4056 | if (item == NULL) |
| 4057 | goto onError; |
| 4058 | /* We own a reference to item from here on. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4059 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4060 | /* Make sure we have enough space for the separator and the item. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4061 | itemlen = PyUnicode_GET_SIZE(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame^] | 4062 | new_res_used = res_used + itemlen; |
| 4063 | if (new_res_used < res_used || new_res_used > INT_MAX) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4064 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame^] | 4065 | if (i < seqlen - 1) { |
| 4066 | new_res_used += seplen; |
| 4067 | if (new_res_used < res_used || new_res_used > INT_MAX) |
| 4068 | goto Overflow; |
| 4069 | } |
| 4070 | if (new_res_used > res_alloc) { |
| 4071 | /* double allocated size until it's big enough */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4072 | do { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame^] | 4073 | size_t oldsize = res_alloc; |
| 4074 | res_alloc += res_alloc; |
| 4075 | if (res_alloc < oldsize || res_alloc > INT_MAX) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4076 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame^] | 4077 | } while (new_res_used > res_alloc); |
| 4078 | if (_PyUnicode_Resize(&res, (int)res_alloc) < 0) { |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4079 | Py_DECREF(item); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4080 | goto onError; |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4081 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame^] | 4082 | res_p = PyUnicode_AS_UNICODE(res) + res_used; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4083 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame^] | 4084 | |
| 4085 | /* Copy item, and maybe the separator. */ |
| 4086 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), (int)itemlen); |
| 4087 | res_p += itemlen; |
| 4088 | if (i < seqlen - 1) { |
| 4089 | Py_UNICODE_COPY(res_p, sep, (int)seplen); |
| 4090 | res_p += seplen; |
| 4091 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4092 | Py_DECREF(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame^] | 4093 | res_used = new_res_used; |
| 4094 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4095 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame^] | 4096 | /* Shrink res to match the used area; this probably can't fail, |
| 4097 | * but it's cheap to check. |
| 4098 | */ |
| 4099 | if (_PyUnicode_Resize(&res, (int)res_used) < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4100 | goto onError; |
| 4101 | |
| 4102 | Done: |
| 4103 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame^] | 4104 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4105 | return (PyObject *)res; |
| 4106 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4107 | Overflow: |
| 4108 | PyErr_SetString(PyExc_OverflowError, |
| 4109 | "join() is too long for a Python string"); |
| 4110 | Py_DECREF(item); |
| 4111 | /* fall through */ |
| 4112 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4113 | onError: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4114 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame^] | 4115 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4116 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4117 | return NULL; |
| 4118 | } |
| 4119 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4120 | static |
| 4121 | PyUnicodeObject *pad(PyUnicodeObject *self, |
| 4122 | int left, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4123 | int right, |
| 4124 | Py_UNICODE fill) |
| 4125 | { |
| 4126 | PyUnicodeObject *u; |
| 4127 | |
| 4128 | if (left < 0) |
| 4129 | left = 0; |
| 4130 | if (right < 0) |
| 4131 | right = 0; |
| 4132 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4133 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4134 | Py_INCREF(self); |
| 4135 | return self; |
| 4136 | } |
| 4137 | |
| 4138 | u = _PyUnicode_New(left + self->length + right); |
| 4139 | if (u) { |
| 4140 | if (left) |
| 4141 | Py_UNICODE_FILL(u->str, fill, left); |
| 4142 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 4143 | if (right) |
| 4144 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 4145 | } |
| 4146 | |
| 4147 | return u; |
| 4148 | } |
| 4149 | |
| 4150 | #define SPLIT_APPEND(data, left, right) \ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4151 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4152 | if (!str) \ |
| 4153 | goto onError; \ |
| 4154 | if (PyList_Append(list, str)) { \ |
| 4155 | Py_DECREF(str); \ |
| 4156 | goto onError; \ |
| 4157 | } \ |
| 4158 | else \ |
| 4159 | Py_DECREF(str); |
| 4160 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4161 | #define SPLIT_INSERT(data, left, right) \ |
| 4162 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
| 4163 | if (!str) \ |
| 4164 | goto onError; \ |
| 4165 | if (PyList_Insert(list, 0, str)) { \ |
| 4166 | Py_DECREF(str); \ |
| 4167 | goto onError; \ |
| 4168 | } \ |
| 4169 | else \ |
| 4170 | Py_DECREF(str); |
| 4171 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4172 | static |
| 4173 | PyObject *split_whitespace(PyUnicodeObject *self, |
| 4174 | PyObject *list, |
| 4175 | int maxcount) |
| 4176 | { |
| 4177 | register int i; |
| 4178 | register int j; |
| 4179 | int len = self->length; |
| 4180 | PyObject *str; |
| 4181 | |
| 4182 | for (i = j = 0; i < len; ) { |
| 4183 | /* find a token */ |
| 4184 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 4185 | i++; |
| 4186 | j = i; |
| 4187 | while (i < len && !Py_UNICODE_ISSPACE(self->str[i])) |
| 4188 | i++; |
| 4189 | if (j < i) { |
| 4190 | if (maxcount-- <= 0) |
| 4191 | break; |
| 4192 | SPLIT_APPEND(self->str, j, i); |
| 4193 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 4194 | i++; |
| 4195 | j = i; |
| 4196 | } |
| 4197 | } |
| 4198 | if (j < len) { |
| 4199 | SPLIT_APPEND(self->str, j, len); |
| 4200 | } |
| 4201 | return list; |
| 4202 | |
| 4203 | onError: |
| 4204 | Py_DECREF(list); |
| 4205 | return NULL; |
| 4206 | } |
| 4207 | |
| 4208 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4209 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4210 | { |
| 4211 | register int i; |
| 4212 | register int j; |
| 4213 | int len; |
| 4214 | PyObject *list; |
| 4215 | PyObject *str; |
| 4216 | Py_UNICODE *data; |
| 4217 | |
| 4218 | string = PyUnicode_FromObject(string); |
| 4219 | if (string == NULL) |
| 4220 | return NULL; |
| 4221 | data = PyUnicode_AS_UNICODE(string); |
| 4222 | len = PyUnicode_GET_SIZE(string); |
| 4223 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4224 | list = PyList_New(0); |
| 4225 | if (!list) |
| 4226 | goto onError; |
| 4227 | |
| 4228 | for (i = j = 0; i < len; ) { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4229 | int eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4230 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4231 | /* Find a line and append it */ |
| 4232 | while (i < len && !Py_UNICODE_ISLINEBREAK(data[i])) |
| 4233 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4234 | |
| 4235 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4236 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4237 | if (i < len) { |
| 4238 | if (data[i] == '\r' && i + 1 < len && |
| 4239 | data[i+1] == '\n') |
| 4240 | i += 2; |
| 4241 | else |
| 4242 | i++; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4243 | if (keepends) |
| 4244 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4245 | } |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4246 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4247 | j = i; |
| 4248 | } |
| 4249 | if (j < len) { |
| 4250 | SPLIT_APPEND(data, j, len); |
| 4251 | } |
| 4252 | |
| 4253 | Py_DECREF(string); |
| 4254 | return list; |
| 4255 | |
| 4256 | onError: |
| 4257 | Py_DECREF(list); |
| 4258 | Py_DECREF(string); |
| 4259 | return NULL; |
| 4260 | } |
| 4261 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4262 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4263 | PyObject *split_char(PyUnicodeObject *self, |
| 4264 | PyObject *list, |
| 4265 | Py_UNICODE ch, |
| 4266 | int maxcount) |
| 4267 | { |
| 4268 | register int i; |
| 4269 | register int j; |
| 4270 | int len = self->length; |
| 4271 | PyObject *str; |
| 4272 | |
| 4273 | for (i = j = 0; i < len; ) { |
| 4274 | if (self->str[i] == ch) { |
| 4275 | if (maxcount-- <= 0) |
| 4276 | break; |
| 4277 | SPLIT_APPEND(self->str, j, i); |
| 4278 | i = j = i + 1; |
| 4279 | } else |
| 4280 | i++; |
| 4281 | } |
| 4282 | if (j <= len) { |
| 4283 | SPLIT_APPEND(self->str, j, len); |
| 4284 | } |
| 4285 | return list; |
| 4286 | |
| 4287 | onError: |
| 4288 | Py_DECREF(list); |
| 4289 | return NULL; |
| 4290 | } |
| 4291 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4292 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4293 | PyObject *split_substring(PyUnicodeObject *self, |
| 4294 | PyObject *list, |
| 4295 | PyUnicodeObject *substring, |
| 4296 | int maxcount) |
| 4297 | { |
| 4298 | register int i; |
| 4299 | register int j; |
| 4300 | int len = self->length; |
| 4301 | int sublen = substring->length; |
| 4302 | PyObject *str; |
| 4303 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 4304 | for (i = j = 0; i <= len - sublen; ) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4305 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 4306 | if (maxcount-- <= 0) |
| 4307 | break; |
| 4308 | SPLIT_APPEND(self->str, j, i); |
| 4309 | i = j = i + sublen; |
| 4310 | } else |
| 4311 | i++; |
| 4312 | } |
| 4313 | if (j <= len) { |
| 4314 | SPLIT_APPEND(self->str, j, len); |
| 4315 | } |
| 4316 | return list; |
| 4317 | |
| 4318 | onError: |
| 4319 | Py_DECREF(list); |
| 4320 | return NULL; |
| 4321 | } |
| 4322 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4323 | static |
| 4324 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
| 4325 | PyObject *list, |
| 4326 | int maxcount) |
| 4327 | { |
| 4328 | register int i; |
| 4329 | register int j; |
| 4330 | int len = self->length; |
| 4331 | PyObject *str; |
| 4332 | |
| 4333 | for (i = j = len - 1; i >= 0; ) { |
| 4334 | /* find a token */ |
| 4335 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 4336 | i--; |
| 4337 | j = i; |
| 4338 | while (i >= 0 && !Py_UNICODE_ISSPACE(self->str[i])) |
| 4339 | i--; |
| 4340 | if (j > i) { |
| 4341 | if (maxcount-- <= 0) |
| 4342 | break; |
| 4343 | SPLIT_INSERT(self->str, i + 1, j + 1); |
| 4344 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 4345 | i--; |
| 4346 | j = i; |
| 4347 | } |
| 4348 | } |
| 4349 | if (j >= 0) { |
| 4350 | SPLIT_INSERT(self->str, 0, j + 1); |
| 4351 | } |
| 4352 | return list; |
| 4353 | |
| 4354 | onError: |
| 4355 | Py_DECREF(list); |
| 4356 | return NULL; |
| 4357 | } |
| 4358 | |
| 4359 | static |
| 4360 | PyObject *rsplit_char(PyUnicodeObject *self, |
| 4361 | PyObject *list, |
| 4362 | Py_UNICODE ch, |
| 4363 | int maxcount) |
| 4364 | { |
| 4365 | register int i; |
| 4366 | register int j; |
| 4367 | int len = self->length; |
| 4368 | PyObject *str; |
| 4369 | |
| 4370 | for (i = j = len - 1; i >= 0; ) { |
| 4371 | if (self->str[i] == ch) { |
| 4372 | if (maxcount-- <= 0) |
| 4373 | break; |
| 4374 | SPLIT_INSERT(self->str, i + 1, j + 1); |
| 4375 | j = i = i - 1; |
| 4376 | } else |
| 4377 | i--; |
| 4378 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 4379 | if (j >= -1) { |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4380 | SPLIT_INSERT(self->str, 0, j + 1); |
| 4381 | } |
| 4382 | return list; |
| 4383 | |
| 4384 | onError: |
| 4385 | Py_DECREF(list); |
| 4386 | return NULL; |
| 4387 | } |
| 4388 | |
| 4389 | static |
| 4390 | PyObject *rsplit_substring(PyUnicodeObject *self, |
| 4391 | PyObject *list, |
| 4392 | PyUnicodeObject *substring, |
| 4393 | int maxcount) |
| 4394 | { |
| 4395 | register int i; |
| 4396 | register int j; |
| 4397 | int len = self->length; |
| 4398 | int sublen = substring->length; |
| 4399 | PyObject *str; |
| 4400 | |
| 4401 | for (i = len - sublen, j = len; i >= 0; ) { |
| 4402 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 4403 | if (maxcount-- <= 0) |
| 4404 | break; |
| 4405 | SPLIT_INSERT(self->str, i + sublen, j); |
| 4406 | j = i; |
| 4407 | i -= sublen; |
| 4408 | } else |
| 4409 | i--; |
| 4410 | } |
| 4411 | if (j >= 0) { |
| 4412 | SPLIT_INSERT(self->str, 0, j); |
| 4413 | } |
| 4414 | return list; |
| 4415 | |
| 4416 | onError: |
| 4417 | Py_DECREF(list); |
| 4418 | return NULL; |
| 4419 | } |
| 4420 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4421 | #undef SPLIT_APPEND |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4422 | #undef SPLIT_INSERT |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4423 | |
| 4424 | static |
| 4425 | PyObject *split(PyUnicodeObject *self, |
| 4426 | PyUnicodeObject *substring, |
| 4427 | int maxcount) |
| 4428 | { |
| 4429 | PyObject *list; |
| 4430 | |
| 4431 | if (maxcount < 0) |
| 4432 | maxcount = INT_MAX; |
| 4433 | |
| 4434 | list = PyList_New(0); |
| 4435 | if (!list) |
| 4436 | return NULL; |
| 4437 | |
| 4438 | if (substring == NULL) |
| 4439 | return split_whitespace(self,list,maxcount); |
| 4440 | |
| 4441 | else if (substring->length == 1) |
| 4442 | return split_char(self,list,substring->str[0],maxcount); |
| 4443 | |
| 4444 | else if (substring->length == 0) { |
| 4445 | Py_DECREF(list); |
| 4446 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 4447 | return NULL; |
| 4448 | } |
| 4449 | else |
| 4450 | return split_substring(self,list,substring,maxcount); |
| 4451 | } |
| 4452 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4453 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4454 | PyObject *rsplit(PyUnicodeObject *self, |
| 4455 | PyUnicodeObject *substring, |
| 4456 | int maxcount) |
| 4457 | { |
| 4458 | PyObject *list; |
| 4459 | |
| 4460 | if (maxcount < 0) |
| 4461 | maxcount = INT_MAX; |
| 4462 | |
| 4463 | list = PyList_New(0); |
| 4464 | if (!list) |
| 4465 | return NULL; |
| 4466 | |
| 4467 | if (substring == NULL) |
| 4468 | return rsplit_whitespace(self,list,maxcount); |
| 4469 | |
| 4470 | else if (substring->length == 1) |
| 4471 | return rsplit_char(self,list,substring->str[0],maxcount); |
| 4472 | |
| 4473 | else if (substring->length == 0) { |
| 4474 | Py_DECREF(list); |
| 4475 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 4476 | return NULL; |
| 4477 | } |
| 4478 | else |
| 4479 | return rsplit_substring(self,list,substring,maxcount); |
| 4480 | } |
| 4481 | |
| 4482 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4483 | PyObject *replace(PyUnicodeObject *self, |
| 4484 | PyUnicodeObject *str1, |
| 4485 | PyUnicodeObject *str2, |
| 4486 | int maxcount) |
| 4487 | { |
| 4488 | PyUnicodeObject *u; |
| 4489 | |
| 4490 | if (maxcount < 0) |
| 4491 | maxcount = INT_MAX; |
| 4492 | |
| 4493 | if (str1->length == 1 && str2->length == 1) { |
| 4494 | int i; |
| 4495 | |
| 4496 | /* replace characters */ |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4497 | if (!findchar(self->str, self->length, str1->str[0]) && |
| 4498 | PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4499 | /* nothing to replace, return original string */ |
| 4500 | Py_INCREF(self); |
| 4501 | u = self; |
| 4502 | } else { |
| 4503 | Py_UNICODE u1 = str1->str[0]; |
| 4504 | Py_UNICODE u2 = str2->str[0]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4505 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4506 | u = (PyUnicodeObject*) PyUnicode_FromUnicode( |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4507 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4508 | self->length |
| 4509 | ); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4510 | if (u != NULL) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4511 | Py_UNICODE_COPY(u->str, self->str, |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4512 | self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4513 | for (i = 0; i < u->length; i++) |
| 4514 | if (u->str[i] == u1) { |
| 4515 | if (--maxcount < 0) |
| 4516 | break; |
| 4517 | u->str[i] = u2; |
| 4518 | } |
| 4519 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4520 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4521 | |
| 4522 | } else { |
| 4523 | int n, i; |
| 4524 | Py_UNICODE *p; |
| 4525 | |
| 4526 | /* replace strings */ |
| 4527 | n = count(self, 0, self->length, str1); |
| 4528 | if (n > maxcount) |
| 4529 | n = maxcount; |
Guido van Rossum | 2023c9b | 2002-08-23 18:50:21 +0000 | [diff] [blame] | 4530 | if (n == 0) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4531 | /* nothing to replace, return original string */ |
Guido van Rossum | 2023c9b | 2002-08-23 18:50:21 +0000 | [diff] [blame] | 4532 | if (PyUnicode_CheckExact(self)) { |
| 4533 | Py_INCREF(self); |
| 4534 | u = self; |
| 4535 | } |
| 4536 | else { |
| 4537 | u = (PyUnicodeObject *) |
| 4538 | PyUnicode_FromUnicode(self->str, self->length); |
| 4539 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4540 | } else { |
| 4541 | u = _PyUnicode_New( |
| 4542 | self->length + n * (str2->length - str1->length)); |
| 4543 | if (u) { |
| 4544 | i = 0; |
| 4545 | p = u->str; |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 4546 | if (str1->length > 0) { |
| 4547 | while (i <= self->length - str1->length) |
| 4548 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 4549 | /* replace string segment */ |
| 4550 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 4551 | p += str2->length; |
| 4552 | i += str1->length; |
| 4553 | if (--n <= 0) { |
| 4554 | /* copy remaining part */ |
| 4555 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 4556 | break; |
| 4557 | } |
| 4558 | } else |
| 4559 | *p++ = self->str[i++]; |
| 4560 | } else { |
| 4561 | while (n > 0) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4562 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 4563 | p += str2->length; |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 4564 | if (--n <= 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4565 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4566 | *p++ = self->str[i++]; |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 4567 | } |
| 4568 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 4569 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4570 | } |
| 4571 | } |
| 4572 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4573 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4574 | return (PyObject *) u; |
| 4575 | } |
| 4576 | |
| 4577 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 4578 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4579 | PyDoc_STRVAR(title__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4580 | "S.title() -> unicode\n\ |
| 4581 | \n\ |
| 4582 | 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] | 4583 | characters, all remaining cased characters have lower case."); |
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_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4587 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4588 | return fixup(self, fixtitle); |
| 4589 | } |
| 4590 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4591 | PyDoc_STRVAR(capitalize__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4592 | "S.capitalize() -> unicode\n\ |
| 4593 | \n\ |
| 4594 | 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] | 4595 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4596 | |
| 4597 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 4598 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4599 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4600 | return fixup(self, fixcapitalize); |
| 4601 | } |
| 4602 | |
| 4603 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4604 | PyDoc_STRVAR(capwords__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4605 | "S.capwords() -> unicode\n\ |
| 4606 | \n\ |
| 4607 | 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] | 4608 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4609 | |
| 4610 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 4611 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4612 | { |
| 4613 | PyObject *list; |
| 4614 | PyObject *item; |
| 4615 | int i; |
| 4616 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4617 | /* Split into words */ |
| 4618 | list = split(self, NULL, -1); |
| 4619 | if (!list) |
| 4620 | return NULL; |
| 4621 | |
| 4622 | /* Capitalize each word */ |
| 4623 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 4624 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 4625 | fixcapitalize); |
| 4626 | if (item == NULL) |
| 4627 | goto onError; |
| 4628 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 4629 | PyList_SET_ITEM(list, i, item); |
| 4630 | } |
| 4631 | |
| 4632 | /* Join the words to form a new string */ |
| 4633 | item = PyUnicode_Join(NULL, list); |
| 4634 | |
| 4635 | onError: |
| 4636 | Py_DECREF(list); |
| 4637 | return (PyObject *)item; |
| 4638 | } |
| 4639 | #endif |
| 4640 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4641 | /* Argument converter. Coerces to a single unicode character */ |
| 4642 | |
| 4643 | static int |
| 4644 | convert_uc(PyObject *obj, void *addr) |
| 4645 | { |
| 4646 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 4647 | PyObject *uniobj; |
| 4648 | Py_UNICODE *unistr; |
| 4649 | |
| 4650 | uniobj = PyUnicode_FromObject(obj); |
| 4651 | if (uniobj == NULL) { |
| 4652 | PyErr_SetString(PyExc_TypeError, |
| 4653 | "The fill character cannot be converted to Unicode"); |
| 4654 | return 0; |
| 4655 | } |
| 4656 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 4657 | PyErr_SetString(PyExc_TypeError, |
| 4658 | "The fill character must be exactly one character long"); |
| 4659 | Py_DECREF(uniobj); |
| 4660 | return 0; |
| 4661 | } |
| 4662 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 4663 | *fillcharloc = unistr[0]; |
| 4664 | Py_DECREF(uniobj); |
| 4665 | return 1; |
| 4666 | } |
| 4667 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4668 | PyDoc_STRVAR(center__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4669 | "S.center(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4670 | \n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4671 | Return S centered in a Unicode string of length width. Padding is\n\ |
| 4672 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4673 | |
| 4674 | static PyObject * |
| 4675 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 4676 | { |
| 4677 | int marg, left; |
| 4678 | int width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4679 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4680 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4681 | if (!PyArg_ParseTuple(args, "i|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4682 | return NULL; |
| 4683 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4684 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4685 | Py_INCREF(self); |
| 4686 | return (PyObject*) self; |
| 4687 | } |
| 4688 | |
| 4689 | marg = width - self->length; |
| 4690 | left = marg / 2 + (marg & width & 1); |
| 4691 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4692 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4693 | } |
| 4694 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4695 | #if 0 |
| 4696 | |
| 4697 | /* This code should go into some future Unicode collation support |
| 4698 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 4699 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4700 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4701 | /* speedy UTF-16 code point order comparison */ |
| 4702 | /* gleaned from: */ |
| 4703 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 4704 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4705 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4706 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4707 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4708 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 4709 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4710 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4711 | }; |
| 4712 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4713 | static int |
| 4714 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 4715 | { |
| 4716 | int len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4717 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4718 | Py_UNICODE *s1 = str1->str; |
| 4719 | Py_UNICODE *s2 = str2->str; |
| 4720 | |
| 4721 | len1 = str1->length; |
| 4722 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4723 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4724 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4725 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4726 | |
| 4727 | c1 = *s1++; |
| 4728 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 4729 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4730 | if (c1 > (1<<11) * 26) |
| 4731 | c1 += utf16Fixup[c1>>11]; |
| 4732 | if (c2 > (1<<11) * 26) |
| 4733 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4734 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 4735 | |
| 4736 | if (c1 != c2) |
| 4737 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4738 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4739 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4740 | } |
| 4741 | |
| 4742 | return (len1 < len2) ? -1 : (len1 != len2); |
| 4743 | } |
| 4744 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4745 | #else |
| 4746 | |
| 4747 | static int |
| 4748 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 4749 | { |
| 4750 | register int len1, len2; |
| 4751 | |
| 4752 | Py_UNICODE *s1 = str1->str; |
| 4753 | Py_UNICODE *s2 = str2->str; |
| 4754 | |
| 4755 | len1 = str1->length; |
| 4756 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4757 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4758 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4759 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4760 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 4761 | c1 = *s1++; |
| 4762 | c2 = *s2++; |
| 4763 | |
| 4764 | if (c1 != c2) |
| 4765 | return (c1 < c2) ? -1 : 1; |
| 4766 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4767 | len1--; len2--; |
| 4768 | } |
| 4769 | |
| 4770 | return (len1 < len2) ? -1 : (len1 != len2); |
| 4771 | } |
| 4772 | |
| 4773 | #endif |
| 4774 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4775 | int PyUnicode_Compare(PyObject *left, |
| 4776 | PyObject *right) |
| 4777 | { |
| 4778 | PyUnicodeObject *u = NULL, *v = NULL; |
| 4779 | int result; |
| 4780 | |
| 4781 | /* Coerce the two arguments */ |
| 4782 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 4783 | if (u == NULL) |
| 4784 | goto onError; |
| 4785 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 4786 | if (v == NULL) |
| 4787 | goto onError; |
| 4788 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 4789 | /* Shortcut for empty or interned objects */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4790 | if (v == u) { |
| 4791 | Py_DECREF(u); |
| 4792 | Py_DECREF(v); |
| 4793 | return 0; |
| 4794 | } |
| 4795 | |
| 4796 | result = unicode_compare(u, v); |
| 4797 | |
| 4798 | Py_DECREF(u); |
| 4799 | Py_DECREF(v); |
| 4800 | return result; |
| 4801 | |
| 4802 | onError: |
| 4803 | Py_XDECREF(u); |
| 4804 | Py_XDECREF(v); |
| 4805 | return -1; |
| 4806 | } |
| 4807 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4808 | int PyUnicode_Contains(PyObject *container, |
| 4809 | PyObject *element) |
| 4810 | { |
| 4811 | PyUnicodeObject *u = NULL, *v = NULL; |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 4812 | int result, size; |
| 4813 | register const Py_UNICODE *lhs, *end, *rhs; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4814 | |
| 4815 | /* Coerce the two arguments */ |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4816 | v = (PyUnicodeObject *)PyUnicode_FromObject(element); |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 4817 | if (v == NULL) { |
| 4818 | PyErr_SetString(PyExc_TypeError, |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 4819 | "'in <string>' requires string as left operand"); |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4820 | goto onError; |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 4821 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4822 | u = (PyUnicodeObject *)PyUnicode_FromObject(container); |
Marc-André Lemburg | 9cd87aa | 2002-10-23 09:02:46 +0000 | [diff] [blame] | 4823 | if (u == NULL) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4824 | goto onError; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4825 | |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 4826 | size = PyUnicode_GET_SIZE(v); |
| 4827 | rhs = PyUnicode_AS_UNICODE(v); |
| 4828 | lhs = PyUnicode_AS_UNICODE(u); |
| 4829 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4830 | result = 0; |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 4831 | if (size == 1) { |
| 4832 | end = lhs + PyUnicode_GET_SIZE(u); |
| 4833 | while (lhs < end) { |
| 4834 | if (*lhs++ == *rhs) { |
| 4835 | result = 1; |
| 4836 | break; |
| 4837 | } |
| 4838 | } |
| 4839 | } |
| 4840 | else { |
| 4841 | end = lhs + (PyUnicode_GET_SIZE(u) - size); |
| 4842 | while (lhs <= end) { |
Barry Warsaw | 6a043f3 | 2002-08-06 19:03:17 +0000 | [diff] [blame] | 4843 | if (memcmp(lhs++, rhs, size * sizeof(Py_UNICODE)) == 0) { |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 4844 | result = 1; |
| 4845 | break; |
| 4846 | } |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4847 | } |
| 4848 | } |
| 4849 | |
| 4850 | Py_DECREF(u); |
| 4851 | Py_DECREF(v); |
| 4852 | return result; |
| 4853 | |
| 4854 | onError: |
| 4855 | Py_XDECREF(u); |
| 4856 | Py_XDECREF(v); |
| 4857 | return -1; |
| 4858 | } |
| 4859 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4860 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 4861 | |
| 4862 | PyObject *PyUnicode_Concat(PyObject *left, |
| 4863 | PyObject *right) |
| 4864 | { |
| 4865 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 4866 | |
| 4867 | /* Coerce the two arguments */ |
| 4868 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 4869 | if (u == NULL) |
| 4870 | goto onError; |
| 4871 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 4872 | if (v == NULL) |
| 4873 | goto onError; |
| 4874 | |
| 4875 | /* Shortcuts */ |
| 4876 | if (v == unicode_empty) { |
| 4877 | Py_DECREF(v); |
| 4878 | return (PyObject *)u; |
| 4879 | } |
| 4880 | if (u == unicode_empty) { |
| 4881 | Py_DECREF(u); |
| 4882 | return (PyObject *)v; |
| 4883 | } |
| 4884 | |
| 4885 | /* Concat the two Unicode strings */ |
| 4886 | w = _PyUnicode_New(u->length + v->length); |
| 4887 | if (w == NULL) |
| 4888 | goto onError; |
| 4889 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 4890 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 4891 | |
| 4892 | Py_DECREF(u); |
| 4893 | Py_DECREF(v); |
| 4894 | return (PyObject *)w; |
| 4895 | |
| 4896 | onError: |
| 4897 | Py_XDECREF(u); |
| 4898 | Py_XDECREF(v); |
| 4899 | return NULL; |
| 4900 | } |
| 4901 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4902 | PyDoc_STRVAR(count__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4903 | "S.count(sub[, start[, end]]) -> int\n\ |
| 4904 | \n\ |
| 4905 | Return the number of occurrences of substring sub in Unicode string\n\ |
| 4906 | S[start:end]. Optional arguments start and end are\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4907 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4908 | |
| 4909 | static PyObject * |
| 4910 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 4911 | { |
| 4912 | PyUnicodeObject *substring; |
| 4913 | int start = 0; |
| 4914 | int end = INT_MAX; |
| 4915 | PyObject *result; |
| 4916 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 4917 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 4918 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4919 | return NULL; |
| 4920 | |
| 4921 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 4922 | (PyObject *)substring); |
| 4923 | if (substring == NULL) |
| 4924 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4925 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4926 | if (start < 0) |
| 4927 | start += self->length; |
| 4928 | if (start < 0) |
| 4929 | start = 0; |
| 4930 | if (end > self->length) |
| 4931 | end = self->length; |
| 4932 | if (end < 0) |
| 4933 | end += self->length; |
| 4934 | if (end < 0) |
| 4935 | end = 0; |
| 4936 | |
| 4937 | result = PyInt_FromLong((long) count(self, start, end, substring)); |
| 4938 | |
| 4939 | Py_DECREF(substring); |
| 4940 | return result; |
| 4941 | } |
| 4942 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4943 | PyDoc_STRVAR(encode__doc__, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 4944 | "S.encode([encoding[,errors]]) -> string or unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4945 | \n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 4946 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 4947 | 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] | 4948 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4949 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 4950 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 4951 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4952 | |
| 4953 | static PyObject * |
| 4954 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 4955 | { |
| 4956 | char *encoding = NULL; |
| 4957 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 4958 | PyObject *v; |
| 4959 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4960 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 4961 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 4962 | v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 4963 | if (v == NULL) |
| 4964 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 4965 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 4966 | PyErr_Format(PyExc_TypeError, |
| 4967 | "encoder did not return a string/unicode object " |
| 4968 | "(type=%.400s)", |
| 4969 | v->ob_type->tp_name); |
| 4970 | Py_DECREF(v); |
| 4971 | return NULL; |
| 4972 | } |
| 4973 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 4974 | |
| 4975 | onError: |
| 4976 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 4977 | } |
| 4978 | |
| 4979 | PyDoc_STRVAR(decode__doc__, |
| 4980 | "S.decode([encoding[,errors]]) -> string or unicode\n\ |
| 4981 | \n\ |
| 4982 | Decodes S using the codec registered for encoding. encoding defaults\n\ |
| 4983 | to the default encoding. errors may be given to set a different error\n\ |
| 4984 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 4985 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 4986 | as well as any other name registerd with codecs.register_error that is\n\ |
| 4987 | able to handle UnicodeDecodeErrors."); |
| 4988 | |
| 4989 | static PyObject * |
Marc-André Lemburg | 126b44c | 2004-07-10 12:04:20 +0000 | [diff] [blame] | 4990 | unicode_decode(PyUnicodeObject *self, PyObject *args) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 4991 | { |
| 4992 | char *encoding = NULL; |
| 4993 | char *errors = NULL; |
| 4994 | PyObject *v; |
| 4995 | |
| 4996 | if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
| 4997 | return NULL; |
| 4998 | v = PyUnicode_AsDecodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 4999 | if (v == NULL) |
| 5000 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5001 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 5002 | PyErr_Format(PyExc_TypeError, |
| 5003 | "decoder did not return a string/unicode object " |
| 5004 | "(type=%.400s)", |
| 5005 | v->ob_type->tp_name); |
| 5006 | Py_DECREF(v); |
| 5007 | return NULL; |
| 5008 | } |
| 5009 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5010 | |
| 5011 | onError: |
| 5012 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5013 | } |
| 5014 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5015 | PyDoc_STRVAR(expandtabs__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5016 | "S.expandtabs([tabsize]) -> unicode\n\ |
| 5017 | \n\ |
| 5018 | 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] | 5019 | 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] | 5020 | |
| 5021 | static PyObject* |
| 5022 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 5023 | { |
| 5024 | Py_UNICODE *e; |
| 5025 | Py_UNICODE *p; |
| 5026 | Py_UNICODE *q; |
| 5027 | int i, j; |
| 5028 | PyUnicodeObject *u; |
| 5029 | int tabsize = 8; |
| 5030 | |
| 5031 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 5032 | return NULL; |
| 5033 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5034 | /* First pass: determine size of output string */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5035 | i = j = 0; |
| 5036 | e = self->str + self->length; |
| 5037 | for (p = self->str; p < e; p++) |
| 5038 | if (*p == '\t') { |
| 5039 | if (tabsize > 0) |
| 5040 | j += tabsize - (j % tabsize); |
| 5041 | } |
| 5042 | else { |
| 5043 | j++; |
| 5044 | if (*p == '\n' || *p == '\r') { |
| 5045 | i += j; |
| 5046 | j = 0; |
| 5047 | } |
| 5048 | } |
| 5049 | |
| 5050 | /* Second pass: create output string and fill it */ |
| 5051 | u = _PyUnicode_New(i + j); |
| 5052 | if (!u) |
| 5053 | return NULL; |
| 5054 | |
| 5055 | j = 0; |
| 5056 | q = u->str; |
| 5057 | |
| 5058 | for (p = self->str; p < e; p++) |
| 5059 | if (*p == '\t') { |
| 5060 | if (tabsize > 0) { |
| 5061 | i = tabsize - (j % tabsize); |
| 5062 | j += i; |
| 5063 | while (i--) |
| 5064 | *q++ = ' '; |
| 5065 | } |
| 5066 | } |
| 5067 | else { |
| 5068 | j++; |
| 5069 | *q++ = *p; |
| 5070 | if (*p == '\n' || *p == '\r') |
| 5071 | j = 0; |
| 5072 | } |
| 5073 | |
| 5074 | return (PyObject*) u; |
| 5075 | } |
| 5076 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5077 | PyDoc_STRVAR(find__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5078 | "S.find(sub [,start [,end]]) -> int\n\ |
| 5079 | \n\ |
| 5080 | Return the lowest index in S where substring sub is found,\n\ |
| 5081 | such that sub is contained within s[start,end]. Optional\n\ |
| 5082 | arguments start and end are interpreted as in slice notation.\n\ |
| 5083 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5084 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5085 | |
| 5086 | static PyObject * |
| 5087 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 5088 | { |
| 5089 | PyUnicodeObject *substring; |
| 5090 | int start = 0; |
| 5091 | int end = INT_MAX; |
| 5092 | PyObject *result; |
| 5093 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5094 | if (!PyArg_ParseTuple(args, "O|O&O&:find", &substring, |
| 5095 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5096 | return NULL; |
| 5097 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 5098 | (PyObject *)substring); |
| 5099 | if (substring == NULL) |
| 5100 | return NULL; |
| 5101 | |
| 5102 | result = PyInt_FromLong(findstring(self, substring, start, end, 1)); |
| 5103 | |
| 5104 | Py_DECREF(substring); |
| 5105 | return result; |
| 5106 | } |
| 5107 | |
| 5108 | static PyObject * |
| 5109 | unicode_getitem(PyUnicodeObject *self, int index) |
| 5110 | { |
| 5111 | if (index < 0 || index >= self->length) { |
| 5112 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 5113 | return NULL; |
| 5114 | } |
| 5115 | |
| 5116 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 5117 | } |
| 5118 | |
| 5119 | static long |
| 5120 | unicode_hash(PyUnicodeObject *self) |
| 5121 | { |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5122 | /* Since Unicode objects compare equal to their ASCII string |
| 5123 | counterparts, they should use the individual character values |
| 5124 | as basis for their hash value. This is needed to assure that |
| 5125 | strings and Unicode objects behave in the same way as |
| 5126 | dictionary keys. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5127 | |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5128 | register int len; |
| 5129 | register Py_UNICODE *p; |
| 5130 | register long x; |
| 5131 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5132 | if (self->hash != -1) |
| 5133 | return self->hash; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5134 | len = PyUnicode_GET_SIZE(self); |
| 5135 | p = PyUnicode_AS_UNICODE(self); |
| 5136 | x = *p << 7; |
| 5137 | while (--len >= 0) |
| 5138 | x = (1000003*x) ^ *p++; |
| 5139 | x ^= PyUnicode_GET_SIZE(self); |
| 5140 | if (x == -1) |
| 5141 | x = -2; |
| 5142 | self->hash = x; |
| 5143 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5144 | } |
| 5145 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5146 | PyDoc_STRVAR(index__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5147 | "S.index(sub [,start [,end]]) -> int\n\ |
| 5148 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5149 | 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] | 5150 | |
| 5151 | static PyObject * |
| 5152 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 5153 | { |
| 5154 | int result; |
| 5155 | PyUnicodeObject *substring; |
| 5156 | int start = 0; |
| 5157 | int end = INT_MAX; |
| 5158 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5159 | if (!PyArg_ParseTuple(args, "O|O&O&:index", &substring, |
| 5160 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5161 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5162 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5163 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 5164 | (PyObject *)substring); |
| 5165 | if (substring == NULL) |
| 5166 | return NULL; |
| 5167 | |
| 5168 | result = findstring(self, substring, start, end, 1); |
| 5169 | |
| 5170 | Py_DECREF(substring); |
| 5171 | if (result < 0) { |
| 5172 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 5173 | return NULL; |
| 5174 | } |
| 5175 | return PyInt_FromLong(result); |
| 5176 | } |
| 5177 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5178 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5179 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5180 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5181 | 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] | 5182 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5183 | |
| 5184 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5185 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5186 | { |
| 5187 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5188 | register const Py_UNICODE *e; |
| 5189 | int cased; |
| 5190 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5191 | /* Shortcut for single character strings */ |
| 5192 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5193 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5194 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5195 | /* Special case for empty strings */ |
| 5196 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5197 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5198 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5199 | e = p + PyUnicode_GET_SIZE(self); |
| 5200 | cased = 0; |
| 5201 | for (; p < e; p++) { |
| 5202 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5203 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5204 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5205 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5206 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 5207 | cased = 1; |
| 5208 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5209 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5210 | } |
| 5211 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5212 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5213 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5214 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5215 | 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] | 5216 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5217 | |
| 5218 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5219 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5220 | { |
| 5221 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5222 | register const Py_UNICODE *e; |
| 5223 | int cased; |
| 5224 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5225 | /* Shortcut for single character strings */ |
| 5226 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5227 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5228 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5229 | /* Special case for empty strings */ |
| 5230 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5231 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5232 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5233 | e = p + PyUnicode_GET_SIZE(self); |
| 5234 | cased = 0; |
| 5235 | for (; p < e; p++) { |
| 5236 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5237 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5238 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5239 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5240 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 5241 | cased = 1; |
| 5242 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5243 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5244 | } |
| 5245 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5246 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5247 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5248 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5249 | Return True if S is a titlecased string and there is at least one\n\ |
| 5250 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 5251 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 5252 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5253 | |
| 5254 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5255 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5256 | { |
| 5257 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5258 | register const Py_UNICODE *e; |
| 5259 | int cased, previous_is_cased; |
| 5260 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5261 | /* Shortcut for single character strings */ |
| 5262 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5263 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 5264 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5265 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5266 | /* Special case for empty strings */ |
| 5267 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5268 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5269 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5270 | e = p + PyUnicode_GET_SIZE(self); |
| 5271 | cased = 0; |
| 5272 | previous_is_cased = 0; |
| 5273 | for (; p < e; p++) { |
| 5274 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5275 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5276 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 5277 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5278 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5279 | previous_is_cased = 1; |
| 5280 | cased = 1; |
| 5281 | } |
| 5282 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 5283 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5284 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5285 | previous_is_cased = 1; |
| 5286 | cased = 1; |
| 5287 | } |
| 5288 | else |
| 5289 | previous_is_cased = 0; |
| 5290 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5291 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5292 | } |
| 5293 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5294 | PyDoc_STRVAR(isspace__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5295 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5296 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5297 | Return True if all characters in S are whitespace\n\ |
| 5298 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5299 | |
| 5300 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5301 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5302 | { |
| 5303 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5304 | register const Py_UNICODE *e; |
| 5305 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5306 | /* Shortcut for single character strings */ |
| 5307 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5308 | Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5309 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5310 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5311 | /* Special case for empty strings */ |
| 5312 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5313 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5314 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5315 | e = p + PyUnicode_GET_SIZE(self); |
| 5316 | for (; p < e; p++) { |
| 5317 | if (!Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5318 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5319 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5320 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5321 | } |
| 5322 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5323 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5324 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5325 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5326 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5327 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5328 | |
| 5329 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5330 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5331 | { |
| 5332 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5333 | register const Py_UNICODE *e; |
| 5334 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5335 | /* Shortcut for single character strings */ |
| 5336 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5337 | Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5338 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5339 | |
| 5340 | /* Special case for empty strings */ |
| 5341 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5342 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5343 | |
| 5344 | e = p + PyUnicode_GET_SIZE(self); |
| 5345 | for (; p < e; p++) { |
| 5346 | if (!Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5347 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5348 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5349 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5350 | } |
| 5351 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5352 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5353 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5354 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5355 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5356 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5357 | |
| 5358 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5359 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5360 | { |
| 5361 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5362 | register const Py_UNICODE *e; |
| 5363 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5364 | /* Shortcut for single character strings */ |
| 5365 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5366 | Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5367 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5368 | |
| 5369 | /* Special case for empty strings */ |
| 5370 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5371 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5372 | |
| 5373 | e = p + PyUnicode_GET_SIZE(self); |
| 5374 | for (; p < e; p++) { |
| 5375 | if (!Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5376 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5377 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5378 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5379 | } |
| 5380 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5381 | PyDoc_STRVAR(isdecimal__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5382 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5383 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5384 | 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] | 5385 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5386 | |
| 5387 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5388 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5389 | { |
| 5390 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5391 | register const Py_UNICODE *e; |
| 5392 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5393 | /* Shortcut for single character strings */ |
| 5394 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5395 | Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5396 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5397 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5398 | /* Special case for empty strings */ |
| 5399 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5400 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5401 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5402 | e = p + PyUnicode_GET_SIZE(self); |
| 5403 | for (; p < e; p++) { |
| 5404 | if (!Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5405 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5406 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5407 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5408 | } |
| 5409 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5410 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5411 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5412 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5413 | Return True if all characters in S are digits\n\ |
| 5414 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5415 | |
| 5416 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5417 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5418 | { |
| 5419 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5420 | register const Py_UNICODE *e; |
| 5421 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5422 | /* Shortcut for single character strings */ |
| 5423 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5424 | Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5425 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5426 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5427 | /* Special case for empty strings */ |
| 5428 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5429 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5430 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5431 | e = p + PyUnicode_GET_SIZE(self); |
| 5432 | for (; p < e; p++) { |
| 5433 | if (!Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5434 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5435 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5436 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5437 | } |
| 5438 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5439 | PyDoc_STRVAR(isnumeric__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5440 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5441 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5442 | 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] | 5443 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5444 | |
| 5445 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5446 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5447 | { |
| 5448 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5449 | register const Py_UNICODE *e; |
| 5450 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5451 | /* Shortcut for single character strings */ |
| 5452 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5453 | Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5454 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5455 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5456 | /* Special case for empty strings */ |
| 5457 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5458 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5459 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5460 | e = p + PyUnicode_GET_SIZE(self); |
| 5461 | for (; p < e; p++) { |
| 5462 | if (!Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5463 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5464 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5465 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5466 | } |
| 5467 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5468 | PyDoc_STRVAR(join__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5469 | "S.join(sequence) -> unicode\n\ |
| 5470 | \n\ |
| 5471 | 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] | 5472 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5473 | |
| 5474 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5475 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5476 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5477 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5478 | } |
| 5479 | |
| 5480 | static int |
| 5481 | unicode_length(PyUnicodeObject *self) |
| 5482 | { |
| 5483 | return self->length; |
| 5484 | } |
| 5485 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5486 | PyDoc_STRVAR(ljust__doc__, |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 5487 | "S.ljust(width[, fillchar]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5488 | \n\ |
| 5489 | 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] | 5490 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5491 | |
| 5492 | static PyObject * |
| 5493 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 5494 | { |
| 5495 | int width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5496 | Py_UNICODE fillchar = ' '; |
| 5497 | |
| 5498 | if (!PyArg_ParseTuple(args, "i|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5499 | return NULL; |
| 5500 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5501 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5502 | Py_INCREF(self); |
| 5503 | return (PyObject*) self; |
| 5504 | } |
| 5505 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5506 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5507 | } |
| 5508 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5509 | PyDoc_STRVAR(lower__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5510 | "S.lower() -> unicode\n\ |
| 5511 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5512 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5513 | |
| 5514 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5515 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5516 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5517 | return fixup(self, fixlower); |
| 5518 | } |
| 5519 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5520 | #define LEFTSTRIP 0 |
| 5521 | #define RIGHTSTRIP 1 |
| 5522 | #define BOTHSTRIP 2 |
| 5523 | |
| 5524 | /* Arrays indexed by above */ |
| 5525 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 5526 | |
| 5527 | #define STRIPNAME(i) (stripformat[i]+3) |
| 5528 | |
| 5529 | static const Py_UNICODE * |
| 5530 | unicode_memchr(const Py_UNICODE *s, Py_UNICODE c, size_t n) |
| 5531 | { |
Tim Peters | 030a5ce | 2002-04-22 19:00:10 +0000 | [diff] [blame] | 5532 | size_t i; |
| 5533 | for (i = 0; i < n; ++i) |
| 5534 | if (s[i] == c) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5535 | return s+i; |
| 5536 | return NULL; |
| 5537 | } |
| 5538 | |
| 5539 | /* externally visible for str.strip(unicode) */ |
| 5540 | PyObject * |
| 5541 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 5542 | { |
| 5543 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 5544 | int len = PyUnicode_GET_SIZE(self); |
| 5545 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
| 5546 | int seplen = PyUnicode_GET_SIZE(sepobj); |
| 5547 | int i, j; |
| 5548 | |
| 5549 | i = 0; |
| 5550 | if (striptype != RIGHTSTRIP) { |
| 5551 | while (i < len && unicode_memchr(sep, s[i], seplen)) { |
| 5552 | i++; |
| 5553 | } |
| 5554 | } |
| 5555 | |
| 5556 | j = len; |
| 5557 | if (striptype != LEFTSTRIP) { |
| 5558 | do { |
| 5559 | j--; |
| 5560 | } while (j >= i && unicode_memchr(sep, s[j], seplen)); |
| 5561 | j++; |
| 5562 | } |
| 5563 | |
| 5564 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 5565 | Py_INCREF(self); |
| 5566 | return (PyObject*)self; |
| 5567 | } |
| 5568 | else |
| 5569 | return PyUnicode_FromUnicode(s+i, j-i); |
| 5570 | } |
| 5571 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5572 | |
| 5573 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5574 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5575 | { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5576 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 5577 | int len = PyUnicode_GET_SIZE(self), i, j; |
| 5578 | |
| 5579 | i = 0; |
| 5580 | if (striptype != RIGHTSTRIP) { |
| 5581 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 5582 | i++; |
| 5583 | } |
| 5584 | } |
| 5585 | |
| 5586 | j = len; |
| 5587 | if (striptype != LEFTSTRIP) { |
| 5588 | do { |
| 5589 | j--; |
| 5590 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 5591 | j++; |
| 5592 | } |
| 5593 | |
| 5594 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 5595 | Py_INCREF(self); |
| 5596 | return (PyObject*)self; |
| 5597 | } |
| 5598 | else |
| 5599 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5600 | } |
| 5601 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5602 | |
| 5603 | static PyObject * |
| 5604 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 5605 | { |
| 5606 | PyObject *sep = NULL; |
| 5607 | |
| 5608 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 5609 | return NULL; |
| 5610 | |
| 5611 | if (sep != NULL && sep != Py_None) { |
| 5612 | if (PyUnicode_Check(sep)) |
| 5613 | return _PyUnicode_XStrip(self, striptype, sep); |
| 5614 | else if (PyString_Check(sep)) { |
| 5615 | PyObject *res; |
| 5616 | sep = PyUnicode_FromObject(sep); |
| 5617 | if (sep==NULL) |
| 5618 | return NULL; |
| 5619 | res = _PyUnicode_XStrip(self, striptype, sep); |
| 5620 | Py_DECREF(sep); |
| 5621 | return res; |
| 5622 | } |
| 5623 | else { |
| 5624 | PyErr_Format(PyExc_TypeError, |
| 5625 | "%s arg must be None, unicode or str", |
| 5626 | STRIPNAME(striptype)); |
| 5627 | return NULL; |
| 5628 | } |
| 5629 | } |
| 5630 | |
| 5631 | return do_strip(self, striptype); |
| 5632 | } |
| 5633 | |
| 5634 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5635 | PyDoc_STRVAR(strip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5636 | "S.strip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5637 | \n\ |
| 5638 | Return a copy of the string S with leading and trailing\n\ |
| 5639 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5640 | If chars is given and not None, remove characters in chars instead.\n\ |
| 5641 | 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] | 5642 | |
| 5643 | static PyObject * |
| 5644 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 5645 | { |
| 5646 | if (PyTuple_GET_SIZE(args) == 0) |
| 5647 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 5648 | else |
| 5649 | return do_argstrip(self, BOTHSTRIP, args); |
| 5650 | } |
| 5651 | |
| 5652 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5653 | PyDoc_STRVAR(lstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5654 | "S.lstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5655 | \n\ |
| 5656 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5657 | If chars is given and not None, remove characters in chars instead.\n\ |
| 5658 | 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] | 5659 | |
| 5660 | static PyObject * |
| 5661 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 5662 | { |
| 5663 | if (PyTuple_GET_SIZE(args) == 0) |
| 5664 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 5665 | else |
| 5666 | return do_argstrip(self, LEFTSTRIP, args); |
| 5667 | } |
| 5668 | |
| 5669 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5670 | PyDoc_STRVAR(rstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5671 | "S.rstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5672 | \n\ |
| 5673 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5674 | If chars is given and not None, remove characters in chars instead.\n\ |
| 5675 | 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] | 5676 | |
| 5677 | static PyObject * |
| 5678 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 5679 | { |
| 5680 | if (PyTuple_GET_SIZE(args) == 0) |
| 5681 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 5682 | else |
| 5683 | return do_argstrip(self, RIGHTSTRIP, args); |
| 5684 | } |
| 5685 | |
| 5686 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5687 | static PyObject* |
| 5688 | unicode_repeat(PyUnicodeObject *str, int len) |
| 5689 | { |
| 5690 | PyUnicodeObject *u; |
| 5691 | Py_UNICODE *p; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 5692 | int nchars; |
| 5693 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5694 | |
| 5695 | if (len < 0) |
| 5696 | len = 0; |
| 5697 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5698 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5699 | /* no repeat, return original string */ |
| 5700 | Py_INCREF(str); |
| 5701 | return (PyObject*) str; |
| 5702 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 5703 | |
| 5704 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 5705 | * needed doesn't overflow size_t |
| 5706 | */ |
| 5707 | nchars = len * str->length; |
| 5708 | if (len && nchars / len != str->length) { |
| 5709 | PyErr_SetString(PyExc_OverflowError, |
| 5710 | "repeated string is too long"); |
| 5711 | return NULL; |
| 5712 | } |
| 5713 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 5714 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 5715 | PyErr_SetString(PyExc_OverflowError, |
| 5716 | "repeated string is too long"); |
| 5717 | return NULL; |
| 5718 | } |
| 5719 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5720 | if (!u) |
| 5721 | return NULL; |
| 5722 | |
| 5723 | p = u->str; |
| 5724 | |
| 5725 | while (len-- > 0) { |
| 5726 | Py_UNICODE_COPY(p, str->str, str->length); |
| 5727 | p += str->length; |
| 5728 | } |
| 5729 | |
| 5730 | return (PyObject*) u; |
| 5731 | } |
| 5732 | |
| 5733 | PyObject *PyUnicode_Replace(PyObject *obj, |
| 5734 | PyObject *subobj, |
| 5735 | PyObject *replobj, |
| 5736 | int maxcount) |
| 5737 | { |
| 5738 | PyObject *self; |
| 5739 | PyObject *str1; |
| 5740 | PyObject *str2; |
| 5741 | PyObject *result; |
| 5742 | |
| 5743 | self = PyUnicode_FromObject(obj); |
| 5744 | if (self == NULL) |
| 5745 | return NULL; |
| 5746 | str1 = PyUnicode_FromObject(subobj); |
| 5747 | if (str1 == NULL) { |
| 5748 | Py_DECREF(self); |
| 5749 | return NULL; |
| 5750 | } |
| 5751 | str2 = PyUnicode_FromObject(replobj); |
| 5752 | if (str2 == NULL) { |
| 5753 | Py_DECREF(self); |
| 5754 | Py_DECREF(str1); |
| 5755 | return NULL; |
| 5756 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5757 | result = replace((PyUnicodeObject *)self, |
| 5758 | (PyUnicodeObject *)str1, |
| 5759 | (PyUnicodeObject *)str2, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5760 | maxcount); |
| 5761 | Py_DECREF(self); |
| 5762 | Py_DECREF(str1); |
| 5763 | Py_DECREF(str2); |
| 5764 | return result; |
| 5765 | } |
| 5766 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5767 | PyDoc_STRVAR(replace__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5768 | "S.replace (old, new[, maxsplit]) -> unicode\n\ |
| 5769 | \n\ |
| 5770 | Return a copy of S with all occurrences of substring\n\ |
| 5771 | 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] | 5772 | given, only the first maxsplit occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5773 | |
| 5774 | static PyObject* |
| 5775 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 5776 | { |
| 5777 | PyUnicodeObject *str1; |
| 5778 | PyUnicodeObject *str2; |
| 5779 | int maxcount = -1; |
| 5780 | PyObject *result; |
| 5781 | |
| 5782 | if (!PyArg_ParseTuple(args, "OO|i:replace", &str1, &str2, &maxcount)) |
| 5783 | return NULL; |
| 5784 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 5785 | if (str1 == NULL) |
| 5786 | return NULL; |
| 5787 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 5788 | if (str2 == NULL) { |
| 5789 | Py_DECREF(str1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5790 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 5791 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5792 | |
| 5793 | result = replace(self, str1, str2, maxcount); |
| 5794 | |
| 5795 | Py_DECREF(str1); |
| 5796 | Py_DECREF(str2); |
| 5797 | return result; |
| 5798 | } |
| 5799 | |
| 5800 | static |
| 5801 | PyObject *unicode_repr(PyObject *unicode) |
| 5802 | { |
| 5803 | return unicodeescape_string(PyUnicode_AS_UNICODE(unicode), |
| 5804 | PyUnicode_GET_SIZE(unicode), |
| 5805 | 1); |
| 5806 | } |
| 5807 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5808 | PyDoc_STRVAR(rfind__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5809 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 5810 | \n\ |
| 5811 | Return the highest index in S where substring sub is found,\n\ |
| 5812 | such that sub is contained within s[start,end]. Optional\n\ |
| 5813 | arguments start and end are interpreted as in slice notation.\n\ |
| 5814 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5815 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5816 | |
| 5817 | static PyObject * |
| 5818 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 5819 | { |
| 5820 | PyUnicodeObject *substring; |
| 5821 | int start = 0; |
| 5822 | int end = INT_MAX; |
| 5823 | PyObject *result; |
| 5824 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5825 | if (!PyArg_ParseTuple(args, "O|O&O&:rfind", &substring, |
| 5826 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5827 | return NULL; |
| 5828 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 5829 | (PyObject *)substring); |
| 5830 | if (substring == NULL) |
| 5831 | return NULL; |
| 5832 | |
| 5833 | result = PyInt_FromLong(findstring(self, substring, start, end, -1)); |
| 5834 | |
| 5835 | Py_DECREF(substring); |
| 5836 | return result; |
| 5837 | } |
| 5838 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5839 | PyDoc_STRVAR(rindex__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5840 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 5841 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5842 | 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] | 5843 | |
| 5844 | static PyObject * |
| 5845 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 5846 | { |
| 5847 | int result; |
| 5848 | PyUnicodeObject *substring; |
| 5849 | int start = 0; |
| 5850 | int end = INT_MAX; |
| 5851 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5852 | if (!PyArg_ParseTuple(args, "O|O&O&:rindex", &substring, |
| 5853 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5854 | return NULL; |
| 5855 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 5856 | (PyObject *)substring); |
| 5857 | if (substring == NULL) |
| 5858 | return NULL; |
| 5859 | |
| 5860 | result = findstring(self, substring, start, end, -1); |
| 5861 | |
| 5862 | Py_DECREF(substring); |
| 5863 | if (result < 0) { |
| 5864 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 5865 | return NULL; |
| 5866 | } |
| 5867 | return PyInt_FromLong(result); |
| 5868 | } |
| 5869 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5870 | PyDoc_STRVAR(rjust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5871 | "S.rjust(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5872 | \n\ |
| 5873 | 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] | 5874 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5875 | |
| 5876 | static PyObject * |
| 5877 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 5878 | { |
| 5879 | int width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5880 | Py_UNICODE fillchar = ' '; |
| 5881 | |
| 5882 | if (!PyArg_ParseTuple(args, "i|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5883 | return NULL; |
| 5884 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5885 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5886 | Py_INCREF(self); |
| 5887 | return (PyObject*) self; |
| 5888 | } |
| 5889 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5890 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5891 | } |
| 5892 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5893 | static PyObject* |
| 5894 | unicode_slice(PyUnicodeObject *self, int start, int end) |
| 5895 | { |
| 5896 | /* standard clamping */ |
| 5897 | if (start < 0) |
| 5898 | start = 0; |
| 5899 | if (end < 0) |
| 5900 | end = 0; |
| 5901 | if (end > self->length) |
| 5902 | end = self->length; |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5903 | if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5904 | /* full slice, return original string */ |
| 5905 | Py_INCREF(self); |
| 5906 | return (PyObject*) self; |
| 5907 | } |
| 5908 | if (start > end) |
| 5909 | start = end; |
| 5910 | /* copy slice */ |
| 5911 | return (PyObject*) PyUnicode_FromUnicode(self->str + start, |
| 5912 | end - start); |
| 5913 | } |
| 5914 | |
| 5915 | PyObject *PyUnicode_Split(PyObject *s, |
| 5916 | PyObject *sep, |
| 5917 | int maxsplit) |
| 5918 | { |
| 5919 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5920 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5921 | s = PyUnicode_FromObject(s); |
| 5922 | if (s == NULL) |
| 5923 | return NULL; |
| 5924 | if (sep != NULL) { |
| 5925 | sep = PyUnicode_FromObject(sep); |
| 5926 | if (sep == NULL) { |
| 5927 | Py_DECREF(s); |
| 5928 | return NULL; |
| 5929 | } |
| 5930 | } |
| 5931 | |
| 5932 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 5933 | |
| 5934 | Py_DECREF(s); |
| 5935 | Py_XDECREF(sep); |
| 5936 | return result; |
| 5937 | } |
| 5938 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5939 | PyDoc_STRVAR(split__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5940 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 5941 | \n\ |
| 5942 | Return a list of the words in S, using sep as the\n\ |
| 5943 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
| 5944 | 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] | 5945 | is a separator."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5946 | |
| 5947 | static PyObject* |
| 5948 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 5949 | { |
| 5950 | PyObject *substring = Py_None; |
| 5951 | int maxcount = -1; |
| 5952 | |
| 5953 | if (!PyArg_ParseTuple(args, "|Oi:split", &substring, &maxcount)) |
| 5954 | return NULL; |
| 5955 | |
| 5956 | if (substring == Py_None) |
| 5957 | return split(self, NULL, maxcount); |
| 5958 | else if (PyUnicode_Check(substring)) |
| 5959 | return split(self, (PyUnicodeObject *)substring, maxcount); |
| 5960 | else |
| 5961 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
| 5962 | } |
| 5963 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5964 | PyObject *PyUnicode_RSplit(PyObject *s, |
| 5965 | PyObject *sep, |
| 5966 | int maxsplit) |
| 5967 | { |
| 5968 | PyObject *result; |
| 5969 | |
| 5970 | s = PyUnicode_FromObject(s); |
| 5971 | if (s == NULL) |
| 5972 | return NULL; |
| 5973 | if (sep != NULL) { |
| 5974 | sep = PyUnicode_FromObject(sep); |
| 5975 | if (sep == NULL) { |
| 5976 | Py_DECREF(s); |
| 5977 | return NULL; |
| 5978 | } |
| 5979 | } |
| 5980 | |
| 5981 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 5982 | |
| 5983 | Py_DECREF(s); |
| 5984 | Py_XDECREF(sep); |
| 5985 | return result; |
| 5986 | } |
| 5987 | |
| 5988 | PyDoc_STRVAR(rsplit__doc__, |
| 5989 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
| 5990 | \n\ |
| 5991 | Return a list of the words in S, using sep as the\n\ |
| 5992 | delimiter string, starting at the end of the string and\n\ |
| 5993 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 5994 | splits are done. If sep is not specified, any whitespace string\n\ |
| 5995 | is a separator."); |
| 5996 | |
| 5997 | static PyObject* |
| 5998 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 5999 | { |
| 6000 | PyObject *substring = Py_None; |
| 6001 | int maxcount = -1; |
| 6002 | |
| 6003 | if (!PyArg_ParseTuple(args, "|Oi:rsplit", &substring, &maxcount)) |
| 6004 | return NULL; |
| 6005 | |
| 6006 | if (substring == Py_None) |
| 6007 | return rsplit(self, NULL, maxcount); |
| 6008 | else if (PyUnicode_Check(substring)) |
| 6009 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
| 6010 | else |
| 6011 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
| 6012 | } |
| 6013 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6014 | PyDoc_STRVAR(splitlines__doc__, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6015 | "S.splitlines([keepends]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6016 | \n\ |
| 6017 | 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] | 6018 | 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] | 6019 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6020 | |
| 6021 | static PyObject* |
| 6022 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 6023 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6024 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6025 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6026 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6027 | return NULL; |
| 6028 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6029 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6030 | } |
| 6031 | |
| 6032 | static |
| 6033 | PyObject *unicode_str(PyUnicodeObject *self) |
| 6034 | { |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6035 | return PyUnicode_AsEncodedString((PyObject *)self, NULL, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6036 | } |
| 6037 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6038 | PyDoc_STRVAR(swapcase__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6039 | "S.swapcase() -> unicode\n\ |
| 6040 | \n\ |
| 6041 | 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] | 6042 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6043 | |
| 6044 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6045 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6046 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6047 | return fixup(self, fixswapcase); |
| 6048 | } |
| 6049 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6050 | PyDoc_STRVAR(translate__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6051 | "S.translate(table) -> unicode\n\ |
| 6052 | \n\ |
| 6053 | Return a copy of the string S, where all characters have been mapped\n\ |
| 6054 | 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] | 6055 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ |
| 6056 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 6057 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6058 | |
| 6059 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6060 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6061 | { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6062 | return PyUnicode_TranslateCharmap(self->str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6063 | self->length, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6064 | table, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6065 | "ignore"); |
| 6066 | } |
| 6067 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6068 | PyDoc_STRVAR(upper__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6069 | "S.upper() -> unicode\n\ |
| 6070 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6071 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6072 | |
| 6073 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6074 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6075 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6076 | return fixup(self, fixupper); |
| 6077 | } |
| 6078 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6079 | PyDoc_STRVAR(zfill__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6080 | "S.zfill(width) -> unicode\n\ |
| 6081 | \n\ |
| 6082 | 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] | 6083 | of the specified width. The string x is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6084 | |
| 6085 | static PyObject * |
| 6086 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 6087 | { |
| 6088 | int fill; |
| 6089 | PyUnicodeObject *u; |
| 6090 | |
| 6091 | int width; |
| 6092 | if (!PyArg_ParseTuple(args, "i:zfill", &width)) |
| 6093 | return NULL; |
| 6094 | |
| 6095 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 6096 | if (PyUnicode_CheckExact(self)) { |
| 6097 | Py_INCREF(self); |
| 6098 | return (PyObject*) self; |
| 6099 | } |
| 6100 | else |
| 6101 | return PyUnicode_FromUnicode( |
| 6102 | PyUnicode_AS_UNICODE(self), |
| 6103 | PyUnicode_GET_SIZE(self) |
| 6104 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6105 | } |
| 6106 | |
| 6107 | fill = width - self->length; |
| 6108 | |
| 6109 | u = pad(self, fill, 0, '0'); |
| 6110 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 6111 | if (u == NULL) |
| 6112 | return NULL; |
| 6113 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6114 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 6115 | /* move sign to beginning of string */ |
| 6116 | u->str[0] = u->str[fill]; |
| 6117 | u->str[fill] = '0'; |
| 6118 | } |
| 6119 | |
| 6120 | return (PyObject*) u; |
| 6121 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6122 | |
| 6123 | #if 0 |
| 6124 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6125 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6126 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6127 | return PyInt_FromLong(unicode_freelist_size); |
| 6128 | } |
| 6129 | #endif |
| 6130 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6131 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6132 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6133 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 6134 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 6135 | With optional start, test S beginning at that position.\n\ |
| 6136 | With optional end, stop comparing S at that position."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6137 | |
| 6138 | static PyObject * |
| 6139 | unicode_startswith(PyUnicodeObject *self, |
| 6140 | PyObject *args) |
| 6141 | { |
| 6142 | PyUnicodeObject *substring; |
| 6143 | int start = 0; |
| 6144 | int end = INT_MAX; |
| 6145 | PyObject *result; |
| 6146 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6147 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &substring, |
| 6148 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6149 | return NULL; |
| 6150 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6151 | (PyObject *)substring); |
| 6152 | if (substring == NULL) |
| 6153 | return NULL; |
| 6154 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6155 | result = PyBool_FromLong(tailmatch(self, substring, start, end, -1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6156 | |
| 6157 | Py_DECREF(substring); |
| 6158 | return result; |
| 6159 | } |
| 6160 | |
| 6161 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6162 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6163 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6164 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 6165 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 6166 | With optional start, test S beginning at that position.\n\ |
| 6167 | With optional end, stop comparing S at that position."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6168 | |
| 6169 | static PyObject * |
| 6170 | unicode_endswith(PyUnicodeObject *self, |
| 6171 | PyObject *args) |
| 6172 | { |
| 6173 | PyUnicodeObject *substring; |
| 6174 | int start = 0; |
| 6175 | int end = INT_MAX; |
| 6176 | PyObject *result; |
| 6177 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6178 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &substring, |
| 6179 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6180 | return NULL; |
| 6181 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6182 | (PyObject *)substring); |
| 6183 | if (substring == NULL) |
| 6184 | return NULL; |
| 6185 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6186 | result = PyBool_FromLong(tailmatch(self, substring, start, end, +1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6187 | |
| 6188 | Py_DECREF(substring); |
| 6189 | return result; |
| 6190 | } |
| 6191 | |
| 6192 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 6193 | |
| 6194 | static PyObject * |
| 6195 | unicode_getnewargs(PyUnicodeObject *v) |
| 6196 | { |
| 6197 | return Py_BuildValue("(u#)", v->str, v->length); |
| 6198 | } |
| 6199 | |
| 6200 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6201 | static PyMethodDef unicode_methods[] = { |
| 6202 | |
| 6203 | /* Order is according to common usage: often used methods should |
| 6204 | appear first, since lookup is done sequentially. */ |
| 6205 | |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6206 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
| 6207 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 6208 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6209 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6210 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 6211 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 6212 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 6213 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 6214 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 6215 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 6216 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
| 6217 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 6218 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 6219 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6220 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6221 | {"decode", (PyCFunction) unicode_decode, METH_VARARGS, decode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6222 | /* {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */ |
| 6223 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 6224 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 6225 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6226 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6227 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6228 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6229 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 6230 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 6231 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 6232 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 6233 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 6234 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 6235 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 6236 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 6237 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 6238 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 6239 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 6240 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 6241 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 6242 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6243 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 6244 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6245 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6246 | #endif |
| 6247 | |
| 6248 | #if 0 |
| 6249 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6250 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6251 | #endif |
| 6252 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 6253 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6254 | {NULL, NULL} |
| 6255 | }; |
| 6256 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 6257 | static PyObject * |
| 6258 | unicode_mod(PyObject *v, PyObject *w) |
| 6259 | { |
| 6260 | if (!PyUnicode_Check(v)) { |
| 6261 | Py_INCREF(Py_NotImplemented); |
| 6262 | return Py_NotImplemented; |
| 6263 | } |
| 6264 | return PyUnicode_Format(v, w); |
| 6265 | } |
| 6266 | |
| 6267 | static PyNumberMethods unicode_as_number = { |
| 6268 | 0, /*nb_add*/ |
| 6269 | 0, /*nb_subtract*/ |
| 6270 | 0, /*nb_multiply*/ |
| 6271 | 0, /*nb_divide*/ |
| 6272 | unicode_mod, /*nb_remainder*/ |
| 6273 | }; |
| 6274 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6275 | static PySequenceMethods unicode_as_sequence = { |
| 6276 | (inquiry) unicode_length, /* sq_length */ |
| 6277 | (binaryfunc) PyUnicode_Concat, /* sq_concat */ |
| 6278 | (intargfunc) unicode_repeat, /* sq_repeat */ |
| 6279 | (intargfunc) unicode_getitem, /* sq_item */ |
| 6280 | (intintargfunc) unicode_slice, /* sq_slice */ |
| 6281 | 0, /* sq_ass_item */ |
| 6282 | 0, /* sq_ass_slice */ |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6283 | (objobjproc)PyUnicode_Contains, /*sq_contains*/ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6284 | }; |
| 6285 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6286 | static PyObject* |
| 6287 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 6288 | { |
| 6289 | if (PyInt_Check(item)) { |
| 6290 | long i = PyInt_AS_LONG(item); |
| 6291 | if (i < 0) |
| 6292 | i += PyString_GET_SIZE(self); |
| 6293 | return unicode_getitem(self, i); |
| 6294 | } else if (PyLong_Check(item)) { |
| 6295 | long i = PyLong_AsLong(item); |
| 6296 | if (i == -1 && PyErr_Occurred()) |
| 6297 | return NULL; |
| 6298 | if (i < 0) |
| 6299 | i += PyString_GET_SIZE(self); |
| 6300 | return unicode_getitem(self, i); |
| 6301 | } else if (PySlice_Check(item)) { |
| 6302 | int start, stop, step, slicelength, cur, i; |
| 6303 | Py_UNICODE* source_buf; |
| 6304 | Py_UNICODE* result_buf; |
| 6305 | PyObject* result; |
| 6306 | |
| 6307 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyString_GET_SIZE(self), |
| 6308 | &start, &stop, &step, &slicelength) < 0) { |
| 6309 | return NULL; |
| 6310 | } |
| 6311 | |
| 6312 | if (slicelength <= 0) { |
| 6313 | return PyUnicode_FromUnicode(NULL, 0); |
| 6314 | } else { |
| 6315 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
| 6316 | result_buf = PyMem_MALLOC(slicelength*sizeof(Py_UNICODE)); |
| 6317 | |
| 6318 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 6319 | result_buf[i] = source_buf[cur]; |
| 6320 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6321 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6322 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
| 6323 | PyMem_FREE(result_buf); |
| 6324 | return result; |
| 6325 | } |
| 6326 | } else { |
| 6327 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 6328 | return NULL; |
| 6329 | } |
| 6330 | } |
| 6331 | |
| 6332 | static PyMappingMethods unicode_as_mapping = { |
| 6333 | (inquiry)unicode_length, /* mp_length */ |
| 6334 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 6335 | (objobjargproc)0, /* mp_ass_subscript */ |
| 6336 | }; |
| 6337 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6338 | static int |
| 6339 | unicode_buffer_getreadbuf(PyUnicodeObject *self, |
| 6340 | int index, |
| 6341 | const void **ptr) |
| 6342 | { |
| 6343 | if (index != 0) { |
| 6344 | PyErr_SetString(PyExc_SystemError, |
| 6345 | "accessing non-existent unicode segment"); |
| 6346 | return -1; |
| 6347 | } |
| 6348 | *ptr = (void *) self->str; |
| 6349 | return PyUnicode_GET_DATA_SIZE(self); |
| 6350 | } |
| 6351 | |
| 6352 | static int |
| 6353 | unicode_buffer_getwritebuf(PyUnicodeObject *self, int index, |
| 6354 | const void **ptr) |
| 6355 | { |
| 6356 | PyErr_SetString(PyExc_TypeError, |
Neal Norwitz | 20e7213 | 2002-06-13 21:25:17 +0000 | [diff] [blame] | 6357 | "cannot use unicode as modifiable buffer"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6358 | return -1; |
| 6359 | } |
| 6360 | |
| 6361 | static int |
| 6362 | unicode_buffer_getsegcount(PyUnicodeObject *self, |
| 6363 | int *lenp) |
| 6364 | { |
| 6365 | if (lenp) |
| 6366 | *lenp = PyUnicode_GET_DATA_SIZE(self); |
| 6367 | return 1; |
| 6368 | } |
| 6369 | |
| 6370 | static int |
| 6371 | unicode_buffer_getcharbuf(PyUnicodeObject *self, |
| 6372 | int index, |
| 6373 | const void **ptr) |
| 6374 | { |
| 6375 | PyObject *str; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6376 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6377 | if (index != 0) { |
| 6378 | PyErr_SetString(PyExc_SystemError, |
| 6379 | "accessing non-existent unicode segment"); |
| 6380 | return -1; |
| 6381 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 6382 | str = _PyUnicode_AsDefaultEncodedString((PyObject *)self, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6383 | if (str == NULL) |
| 6384 | return -1; |
| 6385 | *ptr = (void *) PyString_AS_STRING(str); |
| 6386 | return PyString_GET_SIZE(str); |
| 6387 | } |
| 6388 | |
| 6389 | /* Helpers for PyUnicode_Format() */ |
| 6390 | |
| 6391 | static PyObject * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 6392 | getnextarg(PyObject *args, int arglen, int *p_argidx) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6393 | { |
| 6394 | int argidx = *p_argidx; |
| 6395 | if (argidx < arglen) { |
| 6396 | (*p_argidx)++; |
| 6397 | if (arglen < 0) |
| 6398 | return args; |
| 6399 | else |
| 6400 | return PyTuple_GetItem(args, argidx); |
| 6401 | } |
| 6402 | PyErr_SetString(PyExc_TypeError, |
| 6403 | "not enough arguments for format string"); |
| 6404 | return NULL; |
| 6405 | } |
| 6406 | |
| 6407 | #define F_LJUST (1<<0) |
| 6408 | #define F_SIGN (1<<1) |
| 6409 | #define F_BLANK (1<<2) |
| 6410 | #define F_ALT (1<<3) |
| 6411 | #define F_ZERO (1<<4) |
| 6412 | |
| 6413 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6414 | int usprintf(register Py_UNICODE *buffer, char *format, ...) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6415 | { |
| 6416 | register int i; |
| 6417 | int len; |
| 6418 | va_list va; |
| 6419 | char *charbuffer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6420 | va_start(va, format); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6421 | |
| 6422 | /* First, format the string as char array, then expand to Py_UNICODE |
| 6423 | array. */ |
| 6424 | charbuffer = (char *)buffer; |
| 6425 | len = vsprintf(charbuffer, format, va); |
| 6426 | for (i = len - 1; i >= 0; i--) |
| 6427 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
| 6428 | |
| 6429 | va_end(va); |
| 6430 | return len; |
| 6431 | } |
| 6432 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 6433 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 6434 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 6435 | formatting is done. */ |
| 6436 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6437 | static int |
| 6438 | formatfloat(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6439 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6440 | int flags, |
| 6441 | int prec, |
| 6442 | int type, |
| 6443 | PyObject *v) |
| 6444 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6445 | /* fmt = '%#.' + `prec` + `type` |
| 6446 | 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] | 6447 | char fmt[20]; |
| 6448 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6449 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6450 | x = PyFloat_AsDouble(v); |
| 6451 | if (x == -1.0 && PyErr_Occurred()) |
| 6452 | return -1; |
| 6453 | if (prec < 0) |
| 6454 | prec = 6; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6455 | if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
| 6456 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6457 | /* Worst case length calc to ensure no buffer overrun: |
| 6458 | |
| 6459 | 'g' formats: |
| 6460 | fmt = %#.<prec>g |
| 6461 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 6462 | for any double rep.) |
| 6463 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 6464 | |
| 6465 | 'f' formats: |
| 6466 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 6467 | len = 1 + 50 + 1 + prec = 52 + prec |
| 6468 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6469 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6470 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6471 | |
| 6472 | */ |
| 6473 | if ((type == 'g' && buflen <= (size_t)10 + (size_t)prec) || |
| 6474 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6475 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6476 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6477 | return -1; |
| 6478 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6479 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 6480 | (flags&F_ALT) ? "#" : "", |
| 6481 | prec, type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6482 | return usprintf(buf, fmt, x); |
| 6483 | } |
| 6484 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6485 | static PyObject* |
| 6486 | formatlong(PyObject *val, int flags, int prec, int type) |
| 6487 | { |
| 6488 | char *buf; |
| 6489 | int i, len; |
| 6490 | PyObject *str; /* temporary string object. */ |
| 6491 | PyUnicodeObject *result; |
| 6492 | |
| 6493 | str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
| 6494 | if (!str) |
| 6495 | return NULL; |
| 6496 | result = _PyUnicode_New(len); |
| 6497 | for (i = 0; i < len; i++) |
| 6498 | result->str[i] = buf[i]; |
| 6499 | result->str[len] = 0; |
| 6500 | Py_DECREF(str); |
| 6501 | return (PyObject*)result; |
| 6502 | } |
| 6503 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6504 | static int |
| 6505 | formatint(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6506 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6507 | int flags, |
| 6508 | int prec, |
| 6509 | int type, |
| 6510 | PyObject *v) |
| 6511 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6512 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6513 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 6514 | * + 1 + 1 |
| 6515 | * = 24 |
| 6516 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6517 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6518 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6519 | long x; |
| 6520 | |
| 6521 | x = PyInt_AsLong(v); |
| 6522 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6523 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6524 | if (x < 0 && type == 'u') { |
| 6525 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 6526 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6527 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 6528 | sign = "-"; |
| 6529 | else |
| 6530 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6531 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6532 | prec = 1; |
| 6533 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6534 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 6535 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6536 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6537 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6538 | PyErr_SetString(PyExc_OverflowError, |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6539 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6540 | return -1; |
| 6541 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6542 | |
| 6543 | if ((flags & F_ALT) && |
| 6544 | (type == 'x' || type == 'X')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6545 | /* When converting under %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6546 | * of issues that cause pain: |
| 6547 | * - when 0 is being converted, the C standard leaves off |
| 6548 | * the '0x' or '0X', which is inconsistent with other |
| 6549 | * %#x/%#X conversions and inconsistent with Python's |
| 6550 | * hex() function |
| 6551 | * - there are platforms that violate the standard and |
| 6552 | * convert 0 with the '0x' or '0X' |
| 6553 | * (Metrowerks, Compaq Tru64) |
| 6554 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6555 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6556 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6557 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6558 | * We can achieve the desired consistency by inserting our |
| 6559 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 6560 | * of %#x/%#X. |
| 6561 | * |
| 6562 | * Note that this is the same approach as used in |
| 6563 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 6564 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6565 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 6566 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 6567 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6568 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6569 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 6570 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6571 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 6572 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6573 | if (sign[0]) |
| 6574 | return usprintf(buf, fmt, -x); |
| 6575 | else |
| 6576 | return usprintf(buf, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6577 | } |
| 6578 | |
| 6579 | static int |
| 6580 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6581 | size_t buflen, |
| 6582 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6583 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6584 | /* presume that the buffer is at least 2 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6585 | if (PyUnicode_Check(v)) { |
| 6586 | if (PyUnicode_GET_SIZE(v) != 1) |
| 6587 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6588 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6589 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6590 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6591 | else if (PyString_Check(v)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6592 | if (PyString_GET_SIZE(v) != 1) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6593 | goto onError; |
| 6594 | buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0]; |
| 6595 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6596 | |
| 6597 | else { |
| 6598 | /* Integer input truncated to a character */ |
| 6599 | long x; |
| 6600 | x = PyInt_AsLong(v); |
| 6601 | if (x == -1 && PyErr_Occurred()) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6602 | goto onError; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 6603 | #ifdef Py_UNICODE_WIDE |
| 6604 | if (x < 0 || x > 0x10ffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 6605 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 6606 | "%c arg not in range(0x110000) " |
| 6607 | "(wide Python build)"); |
| 6608 | return -1; |
| 6609 | } |
| 6610 | #else |
| 6611 | if (x < 0 || x > 0xffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 6612 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 6613 | "%c arg not in range(0x10000) " |
| 6614 | "(narrow Python build)"); |
| 6615 | return -1; |
| 6616 | } |
| 6617 | #endif |
| 6618 | buf[0] = (Py_UNICODE) x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6619 | } |
| 6620 | buf[1] = '\0'; |
| 6621 | return 1; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6622 | |
| 6623 | onError: |
| 6624 | PyErr_SetString(PyExc_TypeError, |
| 6625 | "%c requires int or char"); |
| 6626 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6627 | } |
| 6628 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6629 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 6630 | |
| 6631 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 6632 | chars are formatted. XXX This is a magic number. Each formatting |
| 6633 | routine does bounds checking to ensure no overflow, but a better |
| 6634 | solution may be to malloc a buffer of appropriate size for each |
| 6635 | format. For now, the current solution is sufficient. |
| 6636 | */ |
| 6637 | #define FORMATBUFLEN (size_t)120 |
| 6638 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6639 | PyObject *PyUnicode_Format(PyObject *format, |
| 6640 | PyObject *args) |
| 6641 | { |
| 6642 | Py_UNICODE *fmt, *res; |
| 6643 | int fmtcnt, rescnt, reslen, arglen, argidx; |
| 6644 | int args_owned = 0; |
| 6645 | PyUnicodeObject *result = NULL; |
| 6646 | PyObject *dict = NULL; |
| 6647 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6648 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6649 | if (format == NULL || args == NULL) { |
| 6650 | PyErr_BadInternalCall(); |
| 6651 | return NULL; |
| 6652 | } |
| 6653 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6654 | if (uformat == NULL) |
| 6655 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6656 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 6657 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 6658 | |
| 6659 | reslen = rescnt = fmtcnt + 100; |
| 6660 | result = _PyUnicode_New(reslen); |
| 6661 | if (result == NULL) |
| 6662 | goto onError; |
| 6663 | res = PyUnicode_AS_UNICODE(result); |
| 6664 | |
| 6665 | if (PyTuple_Check(args)) { |
| 6666 | arglen = PyTuple_Size(args); |
| 6667 | argidx = 0; |
| 6668 | } |
| 6669 | else { |
| 6670 | arglen = -1; |
| 6671 | argidx = -2; |
| 6672 | } |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 6673 | if (args->ob_type->tp_as_mapping && !PyTuple_Check(args) && |
| 6674 | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6675 | dict = args; |
| 6676 | |
| 6677 | while (--fmtcnt >= 0) { |
| 6678 | if (*fmt != '%') { |
| 6679 | if (--rescnt < 0) { |
| 6680 | rescnt = fmtcnt + 100; |
| 6681 | reslen += rescnt; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6682 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6683 | return NULL; |
| 6684 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 6685 | --rescnt; |
| 6686 | } |
| 6687 | *res++ = *fmt++; |
| 6688 | } |
| 6689 | else { |
| 6690 | /* Got a format specifier */ |
| 6691 | int flags = 0; |
| 6692 | int width = -1; |
| 6693 | int prec = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6694 | Py_UNICODE c = '\0'; |
| 6695 | Py_UNICODE fill; |
| 6696 | PyObject *v = NULL; |
| 6697 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6698 | Py_UNICODE *pbuf; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6699 | Py_UNICODE sign; |
| 6700 | int len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6701 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6702 | |
| 6703 | fmt++; |
| 6704 | if (*fmt == '(') { |
| 6705 | Py_UNICODE *keystart; |
| 6706 | int keylen; |
| 6707 | PyObject *key; |
| 6708 | int pcount = 1; |
| 6709 | |
| 6710 | if (dict == NULL) { |
| 6711 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6712 | "format requires a mapping"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6713 | goto onError; |
| 6714 | } |
| 6715 | ++fmt; |
| 6716 | --fmtcnt; |
| 6717 | keystart = fmt; |
| 6718 | /* Skip over balanced parentheses */ |
| 6719 | while (pcount > 0 && --fmtcnt >= 0) { |
| 6720 | if (*fmt == ')') |
| 6721 | --pcount; |
| 6722 | else if (*fmt == '(') |
| 6723 | ++pcount; |
| 6724 | fmt++; |
| 6725 | } |
| 6726 | keylen = fmt - keystart - 1; |
| 6727 | if (fmtcnt < 0 || pcount > 0) { |
| 6728 | PyErr_SetString(PyExc_ValueError, |
| 6729 | "incomplete format key"); |
| 6730 | goto onError; |
| 6731 | } |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 6732 | #if 0 |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6733 | /* keys are converted to strings using UTF-8 and |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6734 | then looked up since Python uses strings to hold |
| 6735 | variables names etc. in its namespaces and we |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6736 | wouldn't want to break common idioms. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6737 | key = PyUnicode_EncodeUTF8(keystart, |
| 6738 | keylen, |
| 6739 | NULL); |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 6740 | #else |
| 6741 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 6742 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6743 | if (key == NULL) |
| 6744 | goto onError; |
| 6745 | if (args_owned) { |
| 6746 | Py_DECREF(args); |
| 6747 | args_owned = 0; |
| 6748 | } |
| 6749 | args = PyObject_GetItem(dict, key); |
| 6750 | Py_DECREF(key); |
| 6751 | if (args == NULL) { |
| 6752 | goto onError; |
| 6753 | } |
| 6754 | args_owned = 1; |
| 6755 | arglen = -1; |
| 6756 | argidx = -2; |
| 6757 | } |
| 6758 | while (--fmtcnt >= 0) { |
| 6759 | switch (c = *fmt++) { |
| 6760 | case '-': flags |= F_LJUST; continue; |
| 6761 | case '+': flags |= F_SIGN; continue; |
| 6762 | case ' ': flags |= F_BLANK; continue; |
| 6763 | case '#': flags |= F_ALT; continue; |
| 6764 | case '0': flags |= F_ZERO; continue; |
| 6765 | } |
| 6766 | break; |
| 6767 | } |
| 6768 | if (c == '*') { |
| 6769 | v = getnextarg(args, arglen, &argidx); |
| 6770 | if (v == NULL) |
| 6771 | goto onError; |
| 6772 | if (!PyInt_Check(v)) { |
| 6773 | PyErr_SetString(PyExc_TypeError, |
| 6774 | "* wants int"); |
| 6775 | goto onError; |
| 6776 | } |
| 6777 | width = PyInt_AsLong(v); |
| 6778 | if (width < 0) { |
| 6779 | flags |= F_LJUST; |
| 6780 | width = -width; |
| 6781 | } |
| 6782 | if (--fmtcnt >= 0) |
| 6783 | c = *fmt++; |
| 6784 | } |
| 6785 | else if (c >= '0' && c <= '9') { |
| 6786 | width = c - '0'; |
| 6787 | while (--fmtcnt >= 0) { |
| 6788 | c = *fmt++; |
| 6789 | if (c < '0' || c > '9') |
| 6790 | break; |
| 6791 | if ((width*10) / 10 != width) { |
| 6792 | PyErr_SetString(PyExc_ValueError, |
| 6793 | "width too big"); |
| 6794 | goto onError; |
| 6795 | } |
| 6796 | width = width*10 + (c - '0'); |
| 6797 | } |
| 6798 | } |
| 6799 | if (c == '.') { |
| 6800 | prec = 0; |
| 6801 | if (--fmtcnt >= 0) |
| 6802 | c = *fmt++; |
| 6803 | if (c == '*') { |
| 6804 | v = getnextarg(args, arglen, &argidx); |
| 6805 | if (v == NULL) |
| 6806 | goto onError; |
| 6807 | if (!PyInt_Check(v)) { |
| 6808 | PyErr_SetString(PyExc_TypeError, |
| 6809 | "* wants int"); |
| 6810 | goto onError; |
| 6811 | } |
| 6812 | prec = PyInt_AsLong(v); |
| 6813 | if (prec < 0) |
| 6814 | prec = 0; |
| 6815 | if (--fmtcnt >= 0) |
| 6816 | c = *fmt++; |
| 6817 | } |
| 6818 | else if (c >= '0' && c <= '9') { |
| 6819 | prec = c - '0'; |
| 6820 | while (--fmtcnt >= 0) { |
| 6821 | c = Py_CHARMASK(*fmt++); |
| 6822 | if (c < '0' || c > '9') |
| 6823 | break; |
| 6824 | if ((prec*10) / 10 != prec) { |
| 6825 | PyErr_SetString(PyExc_ValueError, |
| 6826 | "prec too big"); |
| 6827 | goto onError; |
| 6828 | } |
| 6829 | prec = prec*10 + (c - '0'); |
| 6830 | } |
| 6831 | } |
| 6832 | } /* prec */ |
| 6833 | if (fmtcnt >= 0) { |
| 6834 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6835 | if (--fmtcnt >= 0) |
| 6836 | c = *fmt++; |
| 6837 | } |
| 6838 | } |
| 6839 | if (fmtcnt < 0) { |
| 6840 | PyErr_SetString(PyExc_ValueError, |
| 6841 | "incomplete format"); |
| 6842 | goto onError; |
| 6843 | } |
| 6844 | if (c != '%') { |
| 6845 | v = getnextarg(args, arglen, &argidx); |
| 6846 | if (v == NULL) |
| 6847 | goto onError; |
| 6848 | } |
| 6849 | sign = 0; |
| 6850 | fill = ' '; |
| 6851 | switch (c) { |
| 6852 | |
| 6853 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6854 | pbuf = formatbuf; |
| 6855 | /* presume that buffer length is at least 1 */ |
| 6856 | pbuf[0] = '%'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6857 | len = 1; |
| 6858 | break; |
| 6859 | |
| 6860 | case 's': |
| 6861 | case 'r': |
| 6862 | if (PyUnicode_Check(v) && c == 's') { |
| 6863 | temp = v; |
| 6864 | Py_INCREF(temp); |
| 6865 | } |
| 6866 | else { |
| 6867 | PyObject *unicode; |
| 6868 | if (c == 's') |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 6869 | temp = PyObject_Unicode(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6870 | else |
| 6871 | temp = PyObject_Repr(v); |
| 6872 | if (temp == NULL) |
| 6873 | goto onError; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 6874 | if (PyUnicode_Check(temp)) |
| 6875 | /* nothing to do */; |
| 6876 | else if (PyString_Check(temp)) { |
| 6877 | /* convert to string to Unicode */ |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6878 | unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6879 | PyString_GET_SIZE(temp), |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6880 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6881 | "strict"); |
| 6882 | Py_DECREF(temp); |
| 6883 | temp = unicode; |
| 6884 | if (temp == NULL) |
| 6885 | goto onError; |
| 6886 | } |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 6887 | else { |
| 6888 | Py_DECREF(temp); |
| 6889 | PyErr_SetString(PyExc_TypeError, |
| 6890 | "%s argument has non-string str()"); |
| 6891 | goto onError; |
| 6892 | } |
| 6893 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6894 | pbuf = PyUnicode_AS_UNICODE(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6895 | len = PyUnicode_GET_SIZE(temp); |
| 6896 | if (prec >= 0 && len > prec) |
| 6897 | len = prec; |
| 6898 | break; |
| 6899 | |
| 6900 | case 'i': |
| 6901 | case 'd': |
| 6902 | case 'u': |
| 6903 | case 'o': |
| 6904 | case 'x': |
| 6905 | case 'X': |
| 6906 | if (c == 'i') |
| 6907 | c = 'd'; |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 6908 | if (PyLong_Check(v)) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6909 | temp = formatlong(v, flags, prec, c); |
| 6910 | if (!temp) |
| 6911 | goto onError; |
| 6912 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 6913 | len = PyUnicode_GET_SIZE(temp); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6914 | sign = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6915 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6916 | else { |
| 6917 | pbuf = formatbuf; |
| 6918 | len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 6919 | flags, prec, c, v); |
| 6920 | if (len < 0) |
| 6921 | goto onError; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6922 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6923 | } |
| 6924 | if (flags & F_ZERO) |
| 6925 | fill = '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6926 | break; |
| 6927 | |
| 6928 | case 'e': |
| 6929 | case 'E': |
| 6930 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 6931 | case 'F': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6932 | case 'g': |
| 6933 | case 'G': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 6934 | if (c == 'F') |
| 6935 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6936 | pbuf = formatbuf; |
| 6937 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 6938 | flags, prec, c, v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6939 | if (len < 0) |
| 6940 | goto onError; |
| 6941 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6942 | if (flags & F_ZERO) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6943 | fill = '0'; |
| 6944 | break; |
| 6945 | |
| 6946 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6947 | pbuf = formatbuf; |
| 6948 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6949 | if (len < 0) |
| 6950 | goto onError; |
| 6951 | break; |
| 6952 | |
| 6953 | default: |
| 6954 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 6955 | "unsupported format character '%c' (0x%x) " |
| 6956 | "at index %i", |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6957 | (31<=c && c<=126) ? (char)c : '?', |
Marc-André Lemburg | 24e53b6 | 2002-09-24 09:32:14 +0000 | [diff] [blame] | 6958 | (int)c, |
Guido van Rossum | efc1188 | 2002-09-12 14:43:41 +0000 | [diff] [blame] | 6959 | (int)(fmt -1 - PyUnicode_AS_UNICODE(uformat))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6960 | goto onError; |
| 6961 | } |
| 6962 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6963 | if (*pbuf == '-' || *pbuf == '+') { |
| 6964 | sign = *pbuf++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6965 | len--; |
| 6966 | } |
| 6967 | else if (flags & F_SIGN) |
| 6968 | sign = '+'; |
| 6969 | else if (flags & F_BLANK) |
| 6970 | sign = ' '; |
| 6971 | else |
| 6972 | sign = 0; |
| 6973 | } |
| 6974 | if (width < len) |
| 6975 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 6976 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6977 | reslen -= rescnt; |
| 6978 | rescnt = width + fmtcnt + 100; |
| 6979 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 6980 | if (reslen < 0) { |
| 6981 | Py_DECREF(result); |
| 6982 | return PyErr_NoMemory(); |
| 6983 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6984 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6985 | return NULL; |
| 6986 | res = PyUnicode_AS_UNICODE(result) |
| 6987 | + reslen - rescnt; |
| 6988 | } |
| 6989 | if (sign) { |
| 6990 | if (fill != ' ') |
| 6991 | *res++ = sign; |
| 6992 | rescnt--; |
| 6993 | if (width > len) |
| 6994 | width--; |
| 6995 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6996 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 6997 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 6998 | assert(pbuf[1] == c); |
| 6999 | if (fill != ' ') { |
| 7000 | *res++ = *pbuf++; |
| 7001 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7002 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7003 | rescnt -= 2; |
| 7004 | width -= 2; |
| 7005 | if (width < 0) |
| 7006 | width = 0; |
| 7007 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7008 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7009 | if (width > len && !(flags & F_LJUST)) { |
| 7010 | do { |
| 7011 | --rescnt; |
| 7012 | *res++ = fill; |
| 7013 | } while (--width > len); |
| 7014 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7015 | if (fill == ' ') { |
| 7016 | if (sign) |
| 7017 | *res++ = sign; |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7018 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7019 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7020 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7021 | *res++ = *pbuf++; |
| 7022 | *res++ = *pbuf++; |
| 7023 | } |
| 7024 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7025 | Py_UNICODE_COPY(res, pbuf, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7026 | res += len; |
| 7027 | rescnt -= len; |
| 7028 | while (--width >= len) { |
| 7029 | --rescnt; |
| 7030 | *res++ = ' '; |
| 7031 | } |
| 7032 | if (dict && (argidx < arglen) && c != '%') { |
| 7033 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 7034 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7035 | goto onError; |
| 7036 | } |
| 7037 | Py_XDECREF(temp); |
| 7038 | } /* '%' */ |
| 7039 | } /* until end */ |
| 7040 | if (argidx < arglen && !dict) { |
| 7041 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 7042 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7043 | goto onError; |
| 7044 | } |
| 7045 | |
| 7046 | if (args_owned) { |
| 7047 | Py_DECREF(args); |
| 7048 | } |
| 7049 | Py_DECREF(uformat); |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 7050 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 7051 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7052 | return (PyObject *)result; |
| 7053 | |
| 7054 | onError: |
| 7055 | Py_XDECREF(result); |
| 7056 | Py_DECREF(uformat); |
| 7057 | if (args_owned) { |
| 7058 | Py_DECREF(args); |
| 7059 | } |
| 7060 | return NULL; |
| 7061 | } |
| 7062 | |
| 7063 | static PyBufferProcs unicode_as_buffer = { |
| 7064 | (getreadbufferproc) unicode_buffer_getreadbuf, |
| 7065 | (getwritebufferproc) unicode_buffer_getwritebuf, |
| 7066 | (getsegcountproc) unicode_buffer_getsegcount, |
| 7067 | (getcharbufferproc) unicode_buffer_getcharbuf, |
| 7068 | }; |
| 7069 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 7070 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7071 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 7072 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7073 | static PyObject * |
| 7074 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 7075 | { |
| 7076 | PyObject *x = NULL; |
| 7077 | static char *kwlist[] = {"string", "encoding", "errors", 0}; |
| 7078 | char *encoding = NULL; |
| 7079 | char *errors = NULL; |
| 7080 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7081 | if (type != &PyUnicode_Type) |
| 7082 | return unicode_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7083 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
| 7084 | kwlist, &x, &encoding, &errors)) |
| 7085 | return NULL; |
| 7086 | if (x == NULL) |
| 7087 | return (PyObject *)_PyUnicode_New(0); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 7088 | if (encoding == NULL && errors == NULL) |
| 7089 | return PyObject_Unicode(x); |
| 7090 | else |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7091 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
| 7092 | } |
| 7093 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7094 | static PyObject * |
| 7095 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 7096 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7097 | PyUnicodeObject *tmp, *pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7098 | int n; |
| 7099 | |
| 7100 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 7101 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 7102 | if (tmp == NULL) |
| 7103 | return NULL; |
| 7104 | assert(PyUnicode_Check(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7105 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7106 | if (pnew == NULL) { |
| 7107 | Py_DECREF(tmp); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7108 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7109 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7110 | pnew->str = PyMem_NEW(Py_UNICODE, n+1); |
| 7111 | if (pnew->str == NULL) { |
| 7112 | _Py_ForgetReference((PyObject *)pnew); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 7113 | PyObject_Del(pnew); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7114 | Py_DECREF(tmp); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 7115 | return PyErr_NoMemory(); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7116 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7117 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 7118 | pnew->length = n; |
| 7119 | pnew->hash = tmp->hash; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7120 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7121 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7122 | } |
| 7123 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7124 | PyDoc_STRVAR(unicode_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7125 | "unicode(string [, encoding[, errors]]) -> object\n\ |
| 7126 | \n\ |
| 7127 | Create a new Unicode object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 7128 | encoding defaults to the current default string encoding.\n\ |
| 7129 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7130 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7131 | PyTypeObject PyUnicode_Type = { |
| 7132 | PyObject_HEAD_INIT(&PyType_Type) |
| 7133 | 0, /* ob_size */ |
| 7134 | "unicode", /* tp_name */ |
| 7135 | sizeof(PyUnicodeObject), /* tp_size */ |
| 7136 | 0, /* tp_itemsize */ |
| 7137 | /* Slots */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 7138 | (destructor)unicode_dealloc, /* tp_dealloc */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7139 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7140 | 0, /* tp_getattr */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7141 | 0, /* tp_setattr */ |
| 7142 | (cmpfunc) unicode_compare, /* tp_compare */ |
| 7143 | (reprfunc) unicode_repr, /* tp_repr */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7144 | &unicode_as_number, /* tp_as_number */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7145 | &unicode_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7146 | &unicode_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7147 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 7148 | 0, /* tp_call*/ |
| 7149 | (reprfunc) unicode_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7150 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 7151 | 0, /* tp_setattro */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7152 | &unicode_as_buffer, /* tp_as_buffer */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7153 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
| 7154 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7155 | unicode_doc, /* tp_doc */ |
| 7156 | 0, /* tp_traverse */ |
| 7157 | 0, /* tp_clear */ |
| 7158 | 0, /* tp_richcompare */ |
| 7159 | 0, /* tp_weaklistoffset */ |
| 7160 | 0, /* tp_iter */ |
| 7161 | 0, /* tp_iternext */ |
| 7162 | unicode_methods, /* tp_methods */ |
| 7163 | 0, /* tp_members */ |
| 7164 | 0, /* tp_getset */ |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 7165 | &PyBaseString_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7166 | 0, /* tp_dict */ |
| 7167 | 0, /* tp_descr_get */ |
| 7168 | 0, /* tp_descr_set */ |
| 7169 | 0, /* tp_dictoffset */ |
| 7170 | 0, /* tp_init */ |
| 7171 | 0, /* tp_alloc */ |
| 7172 | unicode_new, /* tp_new */ |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 7173 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7174 | }; |
| 7175 | |
| 7176 | /* Initialize the Unicode implementation */ |
| 7177 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 7178 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7179 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7180 | int i; |
| 7181 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7182 | /* Init the implementation */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7183 | unicode_freelist = NULL; |
| 7184 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7185 | unicode_empty = _PyUnicode_New(0); |
Marc-André Lemburg | 90e8147 | 2000-06-07 09:13:21 +0000 | [diff] [blame] | 7186 | strcpy(unicode_default_encoding, "ascii"); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7187 | for (i = 0; i < 256; i++) |
| 7188 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 7189 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 7190 | Py_FatalError("Can't initialize 'unicode'"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7191 | } |
| 7192 | |
| 7193 | /* Finalize the Unicode implementation */ |
| 7194 | |
| 7195 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 7196 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7197 | { |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 7198 | PyUnicodeObject *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7199 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7200 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 7201 | Py_XDECREF(unicode_empty); |
| 7202 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 7203 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7204 | for (i = 0; i < 256; i++) { |
| 7205 | if (unicode_latin1[i]) { |
| 7206 | Py_DECREF(unicode_latin1[i]); |
| 7207 | unicode_latin1[i] = NULL; |
| 7208 | } |
| 7209 | } |
| 7210 | |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 7211 | for (u = unicode_freelist; u != NULL;) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7212 | PyUnicodeObject *v = u; |
| 7213 | u = *(PyUnicodeObject **)u; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 7214 | if (v->str) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 7215 | PyMem_DEL(v->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 7216 | Py_XDECREF(v->defenc); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 7217 | PyObject_Del(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7218 | } |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7219 | unicode_freelist = NULL; |
| 7220 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7221 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 7222 | |
| 7223 | /* |
| 7224 | Local variables: |
| 7225 | c-basic-offset: 4 |
| 7226 | indent-tabs-mode: nil |
| 7227 | End: |
| 7228 | */ |