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 | |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 39 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 40 | #include "Python.h" |
| 41 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 42 | #include "unicodeobject.h" |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 43 | #include "ucnhash.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 44 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 45 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 46 | #include <windows.h> |
| 47 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 48 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 49 | /* Limit for the Unicode object free list */ |
| 50 | |
| 51 | #define MAX_UNICODE_FREELIST_SIZE 1024 |
| 52 | |
| 53 | /* Limit for the Unicode object free list stay alive optimization. |
| 54 | |
| 55 | The implementation will keep allocated Unicode memory intact for |
| 56 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 57 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 58 | |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 59 | At worst this will result in MAX_UNICODE_FREELIST_SIZE * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 60 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 61 | malloc()-overhead) bytes of unused garbage. |
| 62 | |
| 63 | Setting the limit to 0 effectively turns the feature off. |
| 64 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 65 | Note: This is an experimental feature ! If you get core dumps when |
| 66 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 67 | |
| 68 | */ |
| 69 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 70 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 71 | |
| 72 | /* Endianness switches; defaults to little endian */ |
| 73 | |
| 74 | #ifdef WORDS_BIGENDIAN |
| 75 | # define BYTEORDER_IS_BIG_ENDIAN |
| 76 | #else |
| 77 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 78 | #endif |
| 79 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 80 | /* --- Globals ------------------------------------------------------------ |
| 81 | |
| 82 | The globals are initialized by the _PyUnicode_Init() API and should |
| 83 | not be used before calling that API. |
| 84 | |
| 85 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 86 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 87 | |
| 88 | #ifdef __cplusplus |
| 89 | extern "C" { |
| 90 | #endif |
| 91 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 92 | /* Free list for Unicode objects */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 93 | static PyUnicodeObject *unicode_freelist; |
| 94 | static int unicode_freelist_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 95 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 96 | /* The empty Unicode object is shared to improve performance. */ |
| 97 | static PyUnicodeObject *unicode_empty; |
| 98 | |
| 99 | /* Single character Unicode strings in the Latin-1 range are being |
| 100 | shared as well. */ |
| 101 | static PyUnicodeObject *unicode_latin1[256]; |
| 102 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 103 | /* Default encoding to use and assume when NULL is passed as encoding |
| 104 | parameter; it is initialized by _PyUnicode_Init(). |
| 105 | |
| 106 | Always use the PyUnicode_SetDefaultEncoding() and |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 107 | PyUnicode_GetDefaultEncoding() APIs to access this global. |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 108 | |
| 109 | */ |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 110 | static char unicode_default_encoding[100]; |
| 111 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 112 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 113 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 114 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 115 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 116 | return 0x10FFFF; |
| 117 | #else |
| 118 | /* This is actually an illegal character, so it should |
| 119 | not be passed to unichr. */ |
| 120 | return 0xFFFF; |
| 121 | #endif |
| 122 | } |
| 123 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 124 | /* --- Unicode Object ----------------------------------------------------- */ |
| 125 | |
| 126 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 127 | int unicode_resize(register PyUnicodeObject *unicode, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 128 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 129 | { |
| 130 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 131 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 132 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 133 | if (unicode->length == length) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 134 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 135 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 136 | /* Resizing shared object (unicode_empty or single character |
| 137 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 138 | instead ! */ |
Martin v. Löwis | 80d2e59 | 2006-04-13 06:06:08 +0000 | [diff] [blame] | 139 | if (unicode == unicode_empty || |
| 140 | (unicode->length == 1 && |
| 141 | unicode->str[0] < 256U && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 142 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 143 | PyErr_SetString(PyExc_SystemError, |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 144 | "can't resize shared unicode objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 145 | return -1; |
| 146 | } |
| 147 | |
| 148 | /* We allocate one more byte to make sure the string is |
| 149 | Ux0000 terminated -- XXX is this needed ? */ |
| 150 | oldstr = unicode->str; |
| 151 | PyMem_RESIZE(unicode->str, Py_UNICODE, length + 1); |
| 152 | if (!unicode->str) { |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 153 | unicode->str = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 154 | PyErr_NoMemory(); |
| 155 | return -1; |
| 156 | } |
| 157 | unicode->str[length] = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 158 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 159 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 160 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 161 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 162 | if (unicode->defenc) { |
| 163 | Py_DECREF(unicode->defenc); |
| 164 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 165 | } |
| 166 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 167 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | /* We allocate one more byte to make sure the string is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 172 | Ux0000 terminated -- XXX is this needed ? |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 173 | |
| 174 | XXX This allocator could further be enhanced by assuring that the |
| 175 | free list never reduces its size below 1. |
| 176 | |
| 177 | */ |
| 178 | |
| 179 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 180 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 181 | { |
| 182 | register PyUnicodeObject *unicode; |
| 183 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 184 | /* Optimization fo empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 185 | if (length == 0 && unicode_empty != NULL) { |
| 186 | Py_INCREF(unicode_empty); |
| 187 | return unicode_empty; |
| 188 | } |
| 189 | |
| 190 | /* Unicode freelist & memory allocation */ |
| 191 | if (unicode_freelist) { |
| 192 | unicode = unicode_freelist; |
Marc-André Lemburg | bea47e7 | 2000-06-17 20:31:17 +0000 | [diff] [blame] | 193 | unicode_freelist = *(PyUnicodeObject **)unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 194 | unicode_freelist_size--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 195 | if (unicode->str) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 196 | /* Keep-Alive optimization: we only upsize the buffer, |
| 197 | never downsize it. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 198 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 199 | unicode_resize(unicode, length) < 0) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 200 | PyMem_DEL(unicode->str); |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 201 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 204 | else { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 205 | unicode->str = PyMem_NEW(Py_UNICODE, length + 1); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 206 | } |
| 207 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 208 | } |
| 209 | else { |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 210 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 211 | if (unicode == NULL) |
| 212 | return NULL; |
| 213 | unicode->str = PyMem_NEW(Py_UNICODE, length + 1); |
| 214 | } |
| 215 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 216 | if (!unicode->str) { |
| 217 | PyErr_NoMemory(); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 218 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 219 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 220 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 221 | * the caller fails before initializing str -- unicode_resize() |
| 222 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 223 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 224 | * We don't want unicode_resize to read uninitialized memory in |
| 225 | * that case. |
| 226 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 227 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 228 | unicode->str[length] = 0; |
Martin v. Löwis | f15da69 | 2006-04-13 07:24:50 +0000 | [diff] [blame] | 229 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 230 | unicode->hash = -1; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 231 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 232 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 233 | |
| 234 | onError: |
| 235 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 236 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 237 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 241 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 242 | { |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 243 | if (PyUnicode_CheckExact(unicode) && |
| 244 | unicode_freelist_size < MAX_UNICODE_FREELIST_SIZE) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 245 | /* Keep-Alive optimization */ |
| 246 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 247 | PyMem_DEL(unicode->str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 248 | unicode->str = NULL; |
| 249 | unicode->length = 0; |
| 250 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 251 | if (unicode->defenc) { |
| 252 | Py_DECREF(unicode->defenc); |
| 253 | unicode->defenc = NULL; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 254 | } |
| 255 | /* Add to free list */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 256 | *(PyUnicodeObject **)unicode = unicode_freelist; |
| 257 | unicode_freelist = unicode; |
| 258 | unicode_freelist_size++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 259 | } |
| 260 | else { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 261 | PyMem_DEL(unicode->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 262 | Py_XDECREF(unicode->defenc); |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 263 | unicode->ob_type->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 267 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 268 | { |
| 269 | register PyUnicodeObject *v; |
| 270 | |
| 271 | /* Argument checks */ |
| 272 | if (unicode == NULL) { |
| 273 | PyErr_BadInternalCall(); |
| 274 | return -1; |
| 275 | } |
| 276 | v = (PyUnicodeObject *)*unicode; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 277 | 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] | 278 | PyErr_BadInternalCall(); |
| 279 | return -1; |
| 280 | } |
| 281 | |
| 282 | /* Resizing unicode_empty and single character objects is not |
| 283 | possible since these are being shared. We simply return a fresh |
| 284 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 285 | if (v->length != length && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 286 | (v == unicode_empty || v->length == 1)) { |
| 287 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 288 | if (w == NULL) |
| 289 | return -1; |
| 290 | Py_UNICODE_COPY(w->str, v->str, |
| 291 | length < v->length ? length : v->length); |
Raymond Hettinger | c8df578 | 2003-03-09 07:30:43 +0000 | [diff] [blame] | 292 | Py_DECREF(*unicode); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 293 | *unicode = (PyObject *)w; |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 298 | objects, since we can modify them in-place. */ |
| 299 | return unicode_resize(v, length); |
| 300 | } |
| 301 | |
| 302 | /* Internal API for use in unicodeobject.c only ! */ |
| 303 | #define _PyUnicode_Resize(unicodevar, length) \ |
| 304 | PyUnicode_Resize(((PyObject **)(unicodevar)), length) |
| 305 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 306 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 307 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 308 | { |
| 309 | PyUnicodeObject *unicode; |
| 310 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 311 | /* If the Unicode data is known at construction time, we can apply |
| 312 | some optimizations which share commonly used objects. */ |
| 313 | if (u != NULL) { |
| 314 | |
| 315 | /* Optimization for empty strings */ |
| 316 | if (size == 0 && unicode_empty != NULL) { |
| 317 | Py_INCREF(unicode_empty); |
| 318 | return (PyObject *)unicode_empty; |
| 319 | } |
| 320 | |
| 321 | /* Single character Unicode objects in the Latin-1 range are |
| 322 | shared when using this constructor */ |
| 323 | if (size == 1 && *u < 256) { |
| 324 | unicode = unicode_latin1[*u]; |
| 325 | if (!unicode) { |
| 326 | unicode = _PyUnicode_New(1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 327 | if (!unicode) |
| 328 | return NULL; |
Marc-André Lemburg | 8879a33 | 2001-06-07 12:26:56 +0000 | [diff] [blame] | 329 | unicode->str[0] = *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 330 | unicode_latin1[*u] = unicode; |
| 331 | } |
| 332 | Py_INCREF(unicode); |
| 333 | return (PyObject *)unicode; |
| 334 | } |
| 335 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 336 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 337 | unicode = _PyUnicode_New(size); |
| 338 | if (!unicode) |
| 339 | return NULL; |
| 340 | |
| 341 | /* Copy the Unicode data into the new object */ |
| 342 | if (u != NULL) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 343 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 344 | |
| 345 | return (PyObject *)unicode; |
| 346 | } |
| 347 | |
| 348 | #ifdef HAVE_WCHAR_H |
| 349 | |
| 350 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 351 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 352 | { |
| 353 | PyUnicodeObject *unicode; |
| 354 | |
| 355 | if (w == NULL) { |
| 356 | PyErr_BadInternalCall(); |
| 357 | return NULL; |
| 358 | } |
| 359 | |
| 360 | unicode = _PyUnicode_New(size); |
| 361 | if (!unicode) |
| 362 | return NULL; |
| 363 | |
| 364 | /* Copy the wchar_t data into the new object */ |
| 365 | #ifdef HAVE_USABLE_WCHAR_T |
| 366 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 367 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 368 | { |
| 369 | register Py_UNICODE *u; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 370 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 371 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 372 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 373 | *u++ = *w++; |
| 374 | } |
| 375 | #endif |
| 376 | |
| 377 | return (PyObject *)unicode; |
| 378 | } |
| 379 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 380 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 381 | wchar_t *w, |
| 382 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 383 | { |
| 384 | if (unicode == NULL) { |
| 385 | PyErr_BadInternalCall(); |
| 386 | return -1; |
| 387 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 388 | |
| 389 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 390 | if (size > PyUnicode_GET_SIZE(unicode)) |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 391 | size = PyUnicode_GET_SIZE(unicode) + 1; |
| 392 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 393 | #ifdef HAVE_USABLE_WCHAR_T |
| 394 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 395 | #else |
| 396 | { |
| 397 | register Py_UNICODE *u; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 398 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 399 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 400 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 401 | *w++ = *u++; |
| 402 | } |
| 403 | #endif |
| 404 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 405 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 406 | return PyUnicode_GET_SIZE(unicode); |
| 407 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 408 | return size; |
| 409 | } |
| 410 | |
| 411 | #endif |
| 412 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 413 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 414 | { |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 415 | Py_UNICODE s[1]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 416 | |
| 417 | #ifdef Py_UNICODE_WIDE |
| 418 | if (ordinal < 0 || ordinal > 0x10ffff) { |
| 419 | PyErr_SetString(PyExc_ValueError, |
| 420 | "unichr() arg not in range(0x110000) " |
| 421 | "(wide Python build)"); |
| 422 | return NULL; |
| 423 | } |
| 424 | #else |
| 425 | if (ordinal < 0 || ordinal > 0xffff) { |
| 426 | PyErr_SetString(PyExc_ValueError, |
| 427 | "unichr() arg not in range(0x10000) " |
| 428 | "(narrow Python build)"); |
| 429 | return NULL; |
| 430 | } |
| 431 | #endif |
| 432 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 433 | s[0] = (Py_UNICODE)ordinal; |
| 434 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 437 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 438 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 439 | /* XXX Perhaps we should make this API an alias of |
| 440 | PyObject_Unicode() instead ?! */ |
| 441 | if (PyUnicode_CheckExact(obj)) { |
| 442 | Py_INCREF(obj); |
| 443 | return obj; |
| 444 | } |
| 445 | if (PyUnicode_Check(obj)) { |
| 446 | /* For a Unicode subtype that's not a Unicode object, |
| 447 | return a true Unicode object with the same data. */ |
| 448 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 449 | PyUnicode_GET_SIZE(obj)); |
| 450 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 451 | return PyUnicode_FromEncodedObject(obj, NULL, "strict"); |
| 452 | } |
| 453 | |
| 454 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
| 455 | const char *encoding, |
| 456 | const char *errors) |
| 457 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 458 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 459 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 460 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 461 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 462 | if (obj == NULL) { |
| 463 | PyErr_BadInternalCall(); |
| 464 | return NULL; |
| 465 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 466 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 467 | #if 0 |
| 468 | /* For b/w compatibility we also accept Unicode objects provided |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 469 | that no encodings is given and then redirect to |
| 470 | PyObject_Unicode() which then applies the additional logic for |
| 471 | Unicode subclasses. |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 472 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 473 | NOTE: This API should really only be used for object which |
| 474 | represent *encoded* Unicode ! |
| 475 | |
| 476 | */ |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 477 | if (PyUnicode_Check(obj)) { |
| 478 | if (encoding) { |
| 479 | PyErr_SetString(PyExc_TypeError, |
| 480 | "decoding Unicode is not supported"); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 481 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 482 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 483 | return PyObject_Unicode(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 484 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 485 | #else |
| 486 | if (PyUnicode_Check(obj)) { |
| 487 | PyErr_SetString(PyExc_TypeError, |
| 488 | "decoding Unicode is not supported"); |
| 489 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 490 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 491 | #endif |
| 492 | |
| 493 | /* Coerce object */ |
| 494 | if (PyString_Check(obj)) { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 495 | s = PyString_AS_STRING(obj); |
| 496 | len = PyString_GET_SIZE(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 497 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 498 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
| 499 | /* Overwrite the error message with something more useful in |
| 500 | case of a TypeError. */ |
| 501 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 502 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 503 | "coercing to Unicode: need string or buffer, " |
| 504 | "%.80s found", |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 505 | obj->ob_type->tp_name); |
| 506 | goto onError; |
| 507 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 508 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 509 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 510 | if (len == 0) { |
| 511 | Py_INCREF(unicode_empty); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 512 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 513 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 514 | else |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 515 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 516 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 517 | return v; |
| 518 | |
| 519 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 520 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | PyObject *PyUnicode_Decode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 524 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 525 | const char *encoding, |
| 526 | const char *errors) |
| 527 | { |
| 528 | PyObject *buffer = NULL, *unicode; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 529 | |
| 530 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 531 | encoding = PyUnicode_GetDefaultEncoding(); |
| 532 | |
| 533 | /* Shortcuts for common default encodings */ |
| 534 | if (strcmp(encoding, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 535 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 536 | else if (strcmp(encoding, "latin-1") == 0) |
| 537 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 538 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 539 | else if (strcmp(encoding, "mbcs") == 0) |
| 540 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 541 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 542 | else if (strcmp(encoding, "ascii") == 0) |
| 543 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 544 | |
| 545 | /* Decode via the codec registry */ |
| 546 | buffer = PyBuffer_FromMemory((void *)s, size); |
| 547 | if (buffer == NULL) |
| 548 | goto onError; |
| 549 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 550 | if (unicode == NULL) |
| 551 | goto onError; |
| 552 | if (!PyUnicode_Check(unicode)) { |
| 553 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 554 | "decoder did not return an unicode object (type=%.400s)", |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 555 | unicode->ob_type->tp_name); |
| 556 | Py_DECREF(unicode); |
| 557 | goto onError; |
| 558 | } |
| 559 | Py_DECREF(buffer); |
| 560 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 561 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 562 | onError: |
| 563 | Py_XDECREF(buffer); |
| 564 | return NULL; |
| 565 | } |
| 566 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 567 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 568 | const char *encoding, |
| 569 | const char *errors) |
| 570 | { |
| 571 | PyObject *v; |
| 572 | |
| 573 | if (!PyUnicode_Check(unicode)) { |
| 574 | PyErr_BadArgument(); |
| 575 | goto onError; |
| 576 | } |
| 577 | |
| 578 | if (encoding == NULL) |
| 579 | encoding = PyUnicode_GetDefaultEncoding(); |
| 580 | |
| 581 | /* Decode via the codec registry */ |
| 582 | v = PyCodec_Decode(unicode, encoding, errors); |
| 583 | if (v == NULL) |
| 584 | goto onError; |
| 585 | return v; |
| 586 | |
| 587 | onError: |
| 588 | return NULL; |
| 589 | } |
| 590 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 591 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 592 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 593 | const char *encoding, |
| 594 | const char *errors) |
| 595 | { |
| 596 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 597 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 598 | unicode = PyUnicode_FromUnicode(s, size); |
| 599 | if (unicode == NULL) |
| 600 | return NULL; |
| 601 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 602 | Py_DECREF(unicode); |
| 603 | return v; |
| 604 | } |
| 605 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 606 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 607 | const char *encoding, |
| 608 | const char *errors) |
| 609 | { |
| 610 | PyObject *v; |
| 611 | |
| 612 | if (!PyUnicode_Check(unicode)) { |
| 613 | PyErr_BadArgument(); |
| 614 | goto onError; |
| 615 | } |
| 616 | |
| 617 | if (encoding == NULL) |
| 618 | encoding = PyUnicode_GetDefaultEncoding(); |
| 619 | |
| 620 | /* Encode via the codec registry */ |
| 621 | v = PyCodec_Encode(unicode, encoding, errors); |
| 622 | if (v == NULL) |
| 623 | goto onError; |
| 624 | return v; |
| 625 | |
| 626 | onError: |
| 627 | return NULL; |
| 628 | } |
| 629 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 630 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 631 | const char *encoding, |
| 632 | const char *errors) |
| 633 | { |
| 634 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 635 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 636 | if (!PyUnicode_Check(unicode)) { |
| 637 | PyErr_BadArgument(); |
| 638 | goto onError; |
| 639 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 640 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 641 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 642 | encoding = PyUnicode_GetDefaultEncoding(); |
| 643 | |
| 644 | /* Shortcuts for common default encodings */ |
| 645 | if (errors == NULL) { |
| 646 | if (strcmp(encoding, "utf-8") == 0) |
Jeremy Hylton | 9cea41c | 2001-05-29 17:13:15 +0000 | [diff] [blame] | 647 | return PyUnicode_AsUTF8String(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 648 | else if (strcmp(encoding, "latin-1") == 0) |
| 649 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 650 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 651 | else if (strcmp(encoding, "mbcs") == 0) |
| 652 | return PyUnicode_AsMBCSString(unicode); |
| 653 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 654 | else if (strcmp(encoding, "ascii") == 0) |
| 655 | return PyUnicode_AsASCIIString(unicode); |
| 656 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 657 | |
| 658 | /* Encode via the codec registry */ |
| 659 | v = PyCodec_Encode(unicode, encoding, errors); |
| 660 | if (v == NULL) |
| 661 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 662 | if (!PyString_Check(v)) { |
| 663 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 664 | "encoder did not return a string object (type=%.400s)", |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 665 | v->ob_type->tp_name); |
| 666 | Py_DECREF(v); |
| 667 | goto onError; |
| 668 | } |
| 669 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 670 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 671 | onError: |
| 672 | return NULL; |
| 673 | } |
| 674 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 675 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 676 | const char *errors) |
| 677 | { |
| 678 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
| 679 | |
| 680 | if (v) |
| 681 | return v; |
| 682 | v = PyUnicode_AsEncodedString(unicode, NULL, errors); |
| 683 | if (v && errors == NULL) |
| 684 | ((PyUnicodeObject *)unicode)->defenc = v; |
| 685 | return v; |
| 686 | } |
| 687 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 688 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 689 | { |
| 690 | if (!PyUnicode_Check(unicode)) { |
| 691 | PyErr_BadArgument(); |
| 692 | goto onError; |
| 693 | } |
| 694 | return PyUnicode_AS_UNICODE(unicode); |
| 695 | |
| 696 | onError: |
| 697 | return NULL; |
| 698 | } |
| 699 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 700 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 701 | { |
| 702 | if (!PyUnicode_Check(unicode)) { |
| 703 | PyErr_BadArgument(); |
| 704 | goto onError; |
| 705 | } |
| 706 | return PyUnicode_GET_SIZE(unicode); |
| 707 | |
| 708 | onError: |
| 709 | return -1; |
| 710 | } |
| 711 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 712 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 713 | { |
| 714 | return unicode_default_encoding; |
| 715 | } |
| 716 | |
| 717 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 718 | { |
| 719 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 720 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 721 | /* Make sure the encoding is valid. As side effect, this also |
| 722 | loads the encoding into the codec registry cache. */ |
| 723 | v = _PyCodec_Lookup(encoding); |
| 724 | if (v == NULL) |
| 725 | goto onError; |
| 726 | Py_DECREF(v); |
| 727 | strncpy(unicode_default_encoding, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 728 | encoding, |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 729 | sizeof(unicode_default_encoding)); |
| 730 | return 0; |
| 731 | |
| 732 | onError: |
| 733 | return -1; |
| 734 | } |
| 735 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 736 | /* error handling callback helper: |
| 737 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 738 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 739 | and adjust various state variables. |
| 740 | return 0 on success, -1 on error |
| 741 | */ |
| 742 | |
| 743 | static |
| 744 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 745 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 746 | const char *input, Py_ssize_t insize, Py_ssize_t *startinpos, Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 747 | PyObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 748 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 749 | static char *argparse = "O!n;decoding error handler must return (unicode, int) tuple"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 750 | |
| 751 | PyObject *restuple = NULL; |
| 752 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 753 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
| 754 | Py_ssize_t requiredsize; |
| 755 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 756 | Py_UNICODE *repptr; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 757 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 758 | int res = -1; |
| 759 | |
| 760 | if (*errorHandler == NULL) { |
| 761 | *errorHandler = PyCodec_LookupError(errors); |
| 762 | if (*errorHandler == NULL) |
| 763 | goto onError; |
| 764 | } |
| 765 | |
| 766 | if (*exceptionObject == NULL) { |
| 767 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 768 | encoding, input, insize, *startinpos, *endinpos, reason); |
| 769 | if (*exceptionObject == NULL) |
| 770 | goto onError; |
| 771 | } |
| 772 | else { |
| 773 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 774 | goto onError; |
| 775 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 776 | goto onError; |
| 777 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 778 | goto onError; |
| 779 | } |
| 780 | |
| 781 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 782 | if (restuple == NULL) |
| 783 | goto onError; |
| 784 | if (!PyTuple_Check(restuple)) { |
| 785 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 786 | goto onError; |
| 787 | } |
| 788 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 789 | goto onError; |
| 790 | if (newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 791 | newpos = insize+newpos; |
| 792 | if (newpos<0 || newpos>insize) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 793 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 794 | goto onError; |
| 795 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 796 | |
| 797 | /* need more space? (at least enough for what we |
| 798 | have+the replacement+the rest of the string (starting |
| 799 | at the new input position), so we won't have to check space |
| 800 | when there are no errors in the rest of the string) */ |
| 801 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 802 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 803 | requiredsize = *outpos + repsize + insize-newpos; |
| 804 | if (requiredsize > outsize) { |
| 805 | if (requiredsize<2*outsize) |
| 806 | requiredsize = 2*outsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 807 | if (PyUnicode_Resize(output, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 808 | goto onError; |
| 809 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
| 810 | } |
| 811 | *endinpos = newpos; |
| 812 | *inptr = input + newpos; |
| 813 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 814 | *outptr += repsize; |
| 815 | *outpos += repsize; |
| 816 | /* we made it! */ |
| 817 | res = 0; |
| 818 | |
| 819 | onError: |
| 820 | Py_XDECREF(restuple); |
| 821 | return res; |
| 822 | } |
| 823 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 824 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 825 | |
| 826 | /* see RFC2152 for details */ |
| 827 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 828 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 829 | char utf7_special[128] = { |
| 830 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 831 | encoded: |
| 832 | 0 - not special |
| 833 | 1 - special |
| 834 | 2 - whitespace (optional) |
| 835 | 3 - RFC2152 Set O (optional) */ |
| 836 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 837 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 838 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 839 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 840 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 841 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 842 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 843 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 844 | |
| 845 | }; |
| 846 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 847 | /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
| 848 | warnings about the comparison always being false; since |
| 849 | utf7_special[0] is 1, we can safely make that one comparison |
| 850 | true */ |
| 851 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 852 | #define SPECIAL(c, encodeO, encodeWS) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 853 | ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 854 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 855 | (encodeO && (utf7_special[(c)] == 3))) |
| 856 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 857 | #define B64(n) \ |
| 858 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 859 | #define B64CHAR(c) \ |
| 860 | (isalnum(c) || (c) == '+' || (c) == '/') |
| 861 | #define UB64(c) \ |
| 862 | ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
| 863 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 864 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 865 | #define ENCODE(out, ch, bits) \ |
| 866 | while (bits >= 6) { \ |
| 867 | *out++ = B64(ch >> (bits-6)); \ |
| 868 | bits -= 6; \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 871 | #define DECODE(out, ch, bits, surrogate) \ |
| 872 | while (bits >= 16) { \ |
| 873 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 874 | bits -= 16; \ |
| 875 | if (surrogate) { \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 876 | /* We have already generated an error for the high surrogate \ |
| 877 | so let's not bother seeing if the low surrogate is correct or not */ \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 878 | surrogate = 0; \ |
| 879 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 880 | /* This is a surrogate pair. Unfortunately we can't represent \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 881 | it in a 16-bit character */ \ |
| 882 | surrogate = 1; \ |
| 883 | errmsg = "code pairs are not supported"; \ |
| 884 | goto utf7Error; \ |
| 885 | } else { \ |
| 886 | *out++ = outCh; \ |
| 887 | } \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 888 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 889 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 890 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 891 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 892 | const char *errors) |
| 893 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 894 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 895 | Py_ssize_t startinpos; |
| 896 | Py_ssize_t endinpos; |
| 897 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 898 | const char *e; |
| 899 | PyUnicodeObject *unicode; |
| 900 | Py_UNICODE *p; |
| 901 | const char *errmsg = ""; |
| 902 | int inShift = 0; |
| 903 | unsigned int bitsleft = 0; |
| 904 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 905 | int surrogate = 0; |
| 906 | PyObject *errorHandler = NULL; |
| 907 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 908 | |
| 909 | unicode = _PyUnicode_New(size); |
| 910 | if (!unicode) |
| 911 | return NULL; |
| 912 | if (size == 0) |
| 913 | return (PyObject *)unicode; |
| 914 | |
| 915 | p = unicode->str; |
| 916 | e = s + size; |
| 917 | |
| 918 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 919 | Py_UNICODE ch; |
| 920 | restart: |
| 921 | ch = *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 922 | |
| 923 | if (inShift) { |
| 924 | if ((ch == '-') || !B64CHAR(ch)) { |
| 925 | inShift = 0; |
| 926 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 927 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 928 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 929 | if (bitsleft >= 6) { |
| 930 | /* The shift sequence has a partial character in it. If |
| 931 | bitsleft < 6 then we could just classify it as padding |
| 932 | but that is not the case here */ |
| 933 | |
| 934 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 935 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 936 | } |
| 937 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 938 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 939 | here so indicate the potential of a misencoded character. */ |
| 940 | |
| 941 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 942 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 943 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 944 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | if (ch == '-') { |
| 948 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 949 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 950 | inShift = 1; |
| 951 | } |
| 952 | } else if (SPECIAL(ch,0,0)) { |
| 953 | errmsg = "unexpected special character"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 954 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 955 | } else { |
| 956 | *p++ = ch; |
| 957 | } |
| 958 | } else { |
| 959 | charsleft = (charsleft << 6) | UB64(ch); |
| 960 | bitsleft += 6; |
| 961 | s++; |
| 962 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 963 | } |
| 964 | } |
| 965 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 966 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 967 | s++; |
| 968 | if (s < e && *s == '-') { |
| 969 | s++; |
| 970 | *p++ = '+'; |
| 971 | } else |
| 972 | { |
| 973 | inShift = 1; |
| 974 | bitsleft = 0; |
| 975 | } |
| 976 | } |
| 977 | else if (SPECIAL(ch,0,0)) { |
| 978 | errmsg = "unexpected special character"; |
| 979 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 980 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 981 | } |
| 982 | else { |
| 983 | *p++ = ch; |
| 984 | s++; |
| 985 | } |
| 986 | continue; |
| 987 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 988 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 989 | endinpos = s-starts; |
| 990 | if (unicode_decode_call_errorhandler( |
| 991 | errors, &errorHandler, |
| 992 | "utf7", errmsg, |
| 993 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 994 | (PyObject **)&unicode, &outpos, &p)) |
| 995 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | if (inShift) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 999 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1000 | endinpos = size; |
| 1001 | if (unicode_decode_call_errorhandler( |
| 1002 | errors, &errorHandler, |
| 1003 | "utf7", "unterminated shift sequence", |
| 1004 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1005 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1006 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1007 | if (s < e) |
| 1008 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1011 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1012 | goto onError; |
| 1013 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1014 | Py_XDECREF(errorHandler); |
| 1015 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1016 | return (PyObject *)unicode; |
| 1017 | |
| 1018 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1019 | Py_XDECREF(errorHandler); |
| 1020 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1021 | Py_DECREF(unicode); |
| 1022 | return NULL; |
| 1023 | } |
| 1024 | |
| 1025 | |
| 1026 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1027 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1028 | int encodeSetO, |
| 1029 | int encodeWhiteSpace, |
| 1030 | const char *errors) |
| 1031 | { |
| 1032 | PyObject *v; |
| 1033 | /* It might be possible to tighten this worst case */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1034 | Py_ssize_t cbAllocated = 5 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1035 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1036 | Py_ssize_t i = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1037 | unsigned int bitsleft = 0; |
| 1038 | unsigned long charsleft = 0; |
| 1039 | char * out; |
| 1040 | char * start; |
| 1041 | |
| 1042 | if (size == 0) |
| 1043 | return PyString_FromStringAndSize(NULL, 0); |
| 1044 | |
| 1045 | v = PyString_FromStringAndSize(NULL, cbAllocated); |
| 1046 | if (v == NULL) |
| 1047 | return NULL; |
| 1048 | |
| 1049 | start = out = PyString_AS_STRING(v); |
| 1050 | for (;i < size; ++i) { |
| 1051 | Py_UNICODE ch = s[i]; |
| 1052 | |
| 1053 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1054 | if (ch == '+') { |
| 1055 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1056 | *out++ = '-'; |
| 1057 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1058 | charsleft = ch; |
| 1059 | bitsleft = 16; |
| 1060 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1061 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1062 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1063 | } else { |
| 1064 | *out++ = (char) ch; |
| 1065 | } |
| 1066 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1067 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1068 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1069 | charsleft = 0; |
| 1070 | bitsleft = 0; |
| 1071 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1072 | so no '-' is required, except if the character is itself a '-' */ |
| 1073 | if (B64CHAR(ch) || ch == '-') { |
| 1074 | *out++ = '-'; |
| 1075 | } |
| 1076 | inShift = 0; |
| 1077 | *out++ = (char) ch; |
| 1078 | } else { |
| 1079 | bitsleft += 16; |
| 1080 | charsleft = (charsleft << 16) | ch; |
| 1081 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1082 | |
| 1083 | /* If the next character is special then we dont' need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1084 | 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] | 1085 | or '-' then the shift sequence will be terminated implicitly and we |
| 1086 | don't have to insert a '-'. */ |
| 1087 | |
| 1088 | if (bitsleft == 0) { |
| 1089 | if (i + 1 < size) { |
| 1090 | Py_UNICODE ch2 = s[i+1]; |
| 1091 | |
| 1092 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1093 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1094 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1095 | *out++ = '-'; |
| 1096 | inShift = 0; |
| 1097 | } else { |
| 1098 | inShift = 0; |
| 1099 | } |
| 1100 | |
| 1101 | } |
| 1102 | else { |
| 1103 | *out++ = '-'; |
| 1104 | inShift = 0; |
| 1105 | } |
| 1106 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1107 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1108 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1109 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1110 | if (bitsleft) { |
| 1111 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1112 | *out++ = '-'; |
| 1113 | } |
| 1114 | |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 1115 | _PyString_Resize(&v, out - start); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1116 | return v; |
| 1117 | } |
| 1118 | |
| 1119 | #undef SPECIAL |
| 1120 | #undef B64 |
| 1121 | #undef B64CHAR |
| 1122 | #undef UB64 |
| 1123 | #undef ENCODE |
| 1124 | #undef DECODE |
| 1125 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1126 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1127 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1128 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1129 | char utf8_code_length[256] = { |
| 1130 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1131 | illegal prefix. see RFC 2279 for details */ |
| 1132 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1133 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1134 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1135 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1136 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1137 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1138 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1139 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1140 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1141 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1142 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1143 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1144 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1145 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1146 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1147 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1148 | }; |
| 1149 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1150 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1151 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1152 | const char *errors) |
| 1153 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1154 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 1155 | } |
| 1156 | |
| 1157 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1158 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1159 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1160 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1161 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1162 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1163 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1164 | Py_ssize_t startinpos; |
| 1165 | Py_ssize_t endinpos; |
| 1166 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1167 | const char *e; |
| 1168 | PyUnicodeObject *unicode; |
| 1169 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1170 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1171 | PyObject *errorHandler = NULL; |
| 1172 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1173 | |
| 1174 | /* Note: size will always be longer than the resulting Unicode |
| 1175 | character count */ |
| 1176 | unicode = _PyUnicode_New(size); |
| 1177 | if (!unicode) |
| 1178 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1179 | if (size == 0) { |
| 1180 | if (consumed) |
| 1181 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1182 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1183 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1184 | |
| 1185 | /* Unpack UTF-8 encoded data */ |
| 1186 | p = unicode->str; |
| 1187 | e = s + size; |
| 1188 | |
| 1189 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1190 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1191 | |
| 1192 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1193 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1194 | s++; |
| 1195 | continue; |
| 1196 | } |
| 1197 | |
| 1198 | n = utf8_code_length[ch]; |
| 1199 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1200 | if (s + n > e) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1201 | if (consumed) |
| 1202 | break; |
| 1203 | else { |
| 1204 | errmsg = "unexpected end of data"; |
| 1205 | startinpos = s-starts; |
| 1206 | endinpos = size; |
| 1207 | goto utf8Error; |
| 1208 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1209 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1210 | |
| 1211 | switch (n) { |
| 1212 | |
| 1213 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1214 | errmsg = "unexpected code byte"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1215 | startinpos = s-starts; |
| 1216 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1217 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1218 | |
| 1219 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1220 | errmsg = "internal error"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1221 | startinpos = s-starts; |
| 1222 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1223 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1224 | |
| 1225 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1226 | if ((s[1] & 0xc0) != 0x80) { |
| 1227 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1228 | startinpos = s-starts; |
| 1229 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1230 | goto utf8Error; |
| 1231 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1232 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1233 | if (ch < 0x80) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1234 | startinpos = s-starts; |
| 1235 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1236 | errmsg = "illegal encoding"; |
| 1237 | goto utf8Error; |
| 1238 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1239 | else |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1240 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1241 | break; |
| 1242 | |
| 1243 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1244 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1245 | (s[2] & 0xc0) != 0x80) { |
| 1246 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1247 | startinpos = s-starts; |
| 1248 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1249 | goto utf8Error; |
| 1250 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1251 | 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] | 1252 | if (ch < 0x0800) { |
| 1253 | /* Note: UTF-8 encodings of surrogates are considered |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1254 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1255 | |
| 1256 | XXX For wide builds (UCS-4) we should probably try |
| 1257 | to recombine the surrogates into a single code |
| 1258 | unit. |
| 1259 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1260 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1261 | startinpos = s-starts; |
| 1262 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1263 | goto utf8Error; |
| 1264 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1265 | else |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1266 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1267 | break; |
| 1268 | |
| 1269 | case 4: |
| 1270 | if ((s[1] & 0xc0) != 0x80 || |
| 1271 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1272 | (s[3] & 0xc0) != 0x80) { |
| 1273 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1274 | startinpos = s-starts; |
| 1275 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1276 | goto utf8Error; |
| 1277 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1278 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 1279 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 1280 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1281 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1282 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1283 | || (ch > 0x10ffff)) /* maximum value allowed for |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1284 | UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1285 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1286 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1287 | startinpos = s-starts; |
| 1288 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1289 | goto utf8Error; |
| 1290 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1291 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1292 | *p++ = (Py_UNICODE)ch; |
| 1293 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1294 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1295 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1296 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 1297 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1298 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1299 | /* high surrogate = top 10 bits added to D800 */ |
| 1300 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1301 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1302 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1303 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1304 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1305 | break; |
| 1306 | |
| 1307 | default: |
| 1308 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1309 | errmsg = "unsupported Unicode code range"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1310 | startinpos = s-starts; |
| 1311 | endinpos = startinpos+n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1312 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1313 | } |
| 1314 | s += n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1315 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1316 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1317 | utf8Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1318 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1319 | if (unicode_decode_call_errorhandler( |
| 1320 | errors, &errorHandler, |
| 1321 | "utf8", errmsg, |
| 1322 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1323 | (PyObject **)&unicode, &outpos, &p)) |
| 1324 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1325 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1326 | if (consumed) |
| 1327 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1328 | |
| 1329 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1330 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1331 | goto onError; |
| 1332 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1333 | Py_XDECREF(errorHandler); |
| 1334 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1335 | return (PyObject *)unicode; |
| 1336 | |
| 1337 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1338 | Py_XDECREF(errorHandler); |
| 1339 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1340 | Py_DECREF(unicode); |
| 1341 | return NULL; |
| 1342 | } |
| 1343 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1344 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 1345 | and allocate exactly as much space needed at the end. Else allocate the |
| 1346 | maximum possible needed (4 result bytes per Unicode character), and return |
| 1347 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1348 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1349 | PyObject * |
| 1350 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1351 | Py_ssize_t size, |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1352 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1353 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1354 | #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] | 1355 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1356 | Py_ssize_t i; /* index into s of next input byte */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1357 | PyObject *v; /* result string object */ |
| 1358 | char *p; /* next free byte in output buffer */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1359 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1360 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1361 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1362 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1363 | assert(s != NULL); |
| 1364 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1365 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1366 | if (size <= MAX_SHORT_UNICHARS) { |
| 1367 | /* Write into the stack buffer; nallocated can't overflow. |
| 1368 | * At the end, we'll allocate exactly as much heap space as it |
| 1369 | * turns out we need. |
| 1370 | */ |
| 1371 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
| 1372 | v = NULL; /* will allocate after we're done */ |
| 1373 | p = stackbuf; |
| 1374 | } |
| 1375 | else { |
| 1376 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 1377 | nallocated = size * 4; |
| 1378 | if (nallocated / 4 != size) /* overflow! */ |
| 1379 | return PyErr_NoMemory(); |
| 1380 | v = PyString_FromStringAndSize(NULL, nallocated); |
| 1381 | if (v == NULL) |
| 1382 | return NULL; |
| 1383 | p = PyString_AS_STRING(v); |
| 1384 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1385 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1386 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1387 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1388 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1389 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1390 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1391 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1392 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1393 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1394 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 1395 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 1396 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1397 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1398 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1399 | /* Encode UCS2 Unicode ordinals */ |
| 1400 | if (ch < 0x10000) { |
| 1401 | /* Special case: check for high surrogate */ |
| 1402 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 1403 | Py_UCS4 ch2 = s[i]; |
| 1404 | /* Check for low surrogate and combine the two to |
| 1405 | form a UCS4 value */ |
| 1406 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1407 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1408 | i++; |
| 1409 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1410 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1411 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1412 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1413 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1414 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1415 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1416 | continue; |
| 1417 | } |
| 1418 | encodeUCS4: |
| 1419 | /* Encode UCS4 Unicode ordinals */ |
| 1420 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 1421 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 1422 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1423 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1424 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1425 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 1426 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1427 | if (v == NULL) { |
| 1428 | /* This was stack allocated. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1429 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1430 | assert(nneeded <= nallocated); |
| 1431 | v = PyString_FromStringAndSize(stackbuf, nneeded); |
| 1432 | } |
| 1433 | else { |
| 1434 | /* Cut back to size actually needed. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1435 | nneeded = p - PyString_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1436 | assert(nneeded <= nallocated); |
| 1437 | _PyString_Resize(&v, nneeded); |
| 1438 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1439 | return v; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1440 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1441 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1444 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 1445 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1446 | if (!PyUnicode_Check(unicode)) { |
| 1447 | PyErr_BadArgument(); |
| 1448 | return NULL; |
| 1449 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 1450 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1451 | PyUnicode_GET_SIZE(unicode), |
| 1452 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
| 1455 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 1456 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1457 | PyObject * |
| 1458 | PyUnicode_DecodeUTF16(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1459 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1460 | const char *errors, |
| 1461 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1462 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1463 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 1464 | } |
| 1465 | |
| 1466 | PyObject * |
| 1467 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1468 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1469 | const char *errors, |
| 1470 | int *byteorder, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1471 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1472 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1473 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1474 | Py_ssize_t startinpos; |
| 1475 | Py_ssize_t endinpos; |
| 1476 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1477 | PyUnicodeObject *unicode; |
| 1478 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1479 | const unsigned char *q, *e; |
| 1480 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1481 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1482 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 1483 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1484 | int ihi = 1, ilo = 0; |
| 1485 | #else |
| 1486 | int ihi = 0, ilo = 1; |
| 1487 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1488 | PyObject *errorHandler = NULL; |
| 1489 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1490 | |
| 1491 | /* Note: size will always be longer than the resulting Unicode |
| 1492 | character count */ |
| 1493 | unicode = _PyUnicode_New(size); |
| 1494 | if (!unicode) |
| 1495 | return NULL; |
| 1496 | if (size == 0) |
| 1497 | return (PyObject *)unicode; |
| 1498 | |
| 1499 | /* Unpack UTF-16 encoded data */ |
| 1500 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1501 | q = (unsigned char *)s; |
| 1502 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1503 | |
| 1504 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1505 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1506 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1507 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 1508 | byte order setting accordingly. In native mode, the leading BOM |
| 1509 | mark is skipped, in all other modes, it is copied to the output |
| 1510 | stream as-is (giving a ZWNBSP character). */ |
| 1511 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1512 | if (size >= 2) { |
| 1513 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1514 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1515 | if (bom == 0xFEFF) { |
| 1516 | q += 2; |
| 1517 | bo = -1; |
| 1518 | } |
| 1519 | else if (bom == 0xFFFE) { |
| 1520 | q += 2; |
| 1521 | bo = 1; |
| 1522 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1523 | #else |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1524 | if (bom == 0xFEFF) { |
| 1525 | q += 2; |
| 1526 | bo = 1; |
| 1527 | } |
| 1528 | else if (bom == 0xFFFE) { |
| 1529 | q += 2; |
| 1530 | bo = -1; |
| 1531 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1532 | #endif |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1533 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1534 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1535 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1536 | if (bo == -1) { |
| 1537 | /* force LE */ |
| 1538 | ihi = 1; |
| 1539 | ilo = 0; |
| 1540 | } |
| 1541 | else if (bo == 1) { |
| 1542 | /* force BE */ |
| 1543 | ihi = 0; |
| 1544 | ilo = 1; |
| 1545 | } |
| 1546 | |
| 1547 | while (q < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1548 | Py_UNICODE ch; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1549 | /* remaining bytes at the end? (size should be even) */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1550 | if (e-q<2) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1551 | if (consumed) |
| 1552 | break; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1553 | errmsg = "truncated data"; |
| 1554 | startinpos = ((const char *)q)-starts; |
| 1555 | endinpos = ((const char *)e)-starts; |
| 1556 | goto utf16Error; |
| 1557 | /* The remaining input chars are ignored if the callback |
| 1558 | chooses to skip the input */ |
| 1559 | } |
| 1560 | ch = (q[ihi] << 8) | q[ilo]; |
| 1561 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1562 | q += 2; |
| 1563 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1564 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 1565 | *p++ = ch; |
| 1566 | continue; |
| 1567 | } |
| 1568 | |
| 1569 | /* UTF-16 code pair: */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1570 | if (q >= e) { |
| 1571 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1572 | startinpos = (((const char *)q)-2)-starts; |
| 1573 | endinpos = ((const char *)e)-starts; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1574 | goto utf16Error; |
| 1575 | } |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 1576 | if (0xD800 <= ch && ch <= 0xDBFF) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1577 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 1578 | q += 2; |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 1579 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1580 | #ifndef Py_UNICODE_WIDE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1581 | *p++ = ch; |
| 1582 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1583 | #else |
| 1584 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1585 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1586 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1587 | } |
| 1588 | else { |
| 1589 | errmsg = "illegal UTF-16 surrogate"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1590 | startinpos = (((const char *)q)-4)-starts; |
| 1591 | endinpos = startinpos+2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1592 | goto utf16Error; |
| 1593 | } |
| 1594 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1595 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1596 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1597 | startinpos = (((const char *)q)-2)-starts; |
| 1598 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1599 | /* Fall through to report the error */ |
| 1600 | |
| 1601 | utf16Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1602 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1603 | if (unicode_decode_call_errorhandler( |
| 1604 | errors, &errorHandler, |
| 1605 | "utf16", errmsg, |
| 1606 | starts, size, &startinpos, &endinpos, &exc, (const char **)&q, |
| 1607 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1608 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
| 1611 | if (byteorder) |
| 1612 | *byteorder = bo; |
| 1613 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1614 | if (consumed) |
| 1615 | *consumed = (const char *)q-starts; |
| 1616 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1617 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1618 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1619 | goto onError; |
| 1620 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1621 | Py_XDECREF(errorHandler); |
| 1622 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1623 | return (PyObject *)unicode; |
| 1624 | |
| 1625 | onError: |
| 1626 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1627 | Py_XDECREF(errorHandler); |
| 1628 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1629 | return NULL; |
| 1630 | } |
| 1631 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1632 | PyObject * |
| 1633 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1634 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1635 | const char *errors, |
| 1636 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1637 | { |
| 1638 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1639 | unsigned char *p; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1640 | #ifdef Py_UNICODE_WIDE |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1641 | int i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1642 | #else |
| 1643 | const int pairs = 0; |
| 1644 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1645 | /* Offsets from p for storing byte pairs in the right order. */ |
| 1646 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1647 | int ihi = 1, ilo = 0; |
| 1648 | #else |
| 1649 | int ihi = 0, ilo = 1; |
| 1650 | #endif |
| 1651 | |
| 1652 | #define STORECHAR(CH) \ |
| 1653 | do { \ |
| 1654 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 1655 | p[ilo] = (CH) & 0xff; \ |
| 1656 | p += 2; \ |
| 1657 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1658 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1659 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1660 | for (i = pairs = 0; i < size; i++) |
| 1661 | if (s[i] >= 0x10000) |
| 1662 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1663 | #endif |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1664 | v = PyString_FromStringAndSize(NULL, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1665 | 2 * (size + pairs + (byteorder == 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1666 | if (v == NULL) |
| 1667 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1668 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1669 | p = (unsigned char *)PyString_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1670 | if (byteorder == 0) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1671 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 1672 | if (size == 0) |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 1673 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1674 | |
| 1675 | if (byteorder == -1) { |
| 1676 | /* force LE */ |
| 1677 | ihi = 1; |
| 1678 | ilo = 0; |
| 1679 | } |
| 1680 | else if (byteorder == 1) { |
| 1681 | /* force BE */ |
| 1682 | ihi = 0; |
| 1683 | ilo = 1; |
| 1684 | } |
| 1685 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1686 | while (size-- > 0) { |
| 1687 | Py_UNICODE ch = *s++; |
| 1688 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1689 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1690 | if (ch >= 0x10000) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1691 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 1692 | ch = 0xD800 | ((ch-0x10000) >> 10); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1693 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1694 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1695 | STORECHAR(ch); |
| 1696 | if (ch2) |
| 1697 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1698 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1699 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1700 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
| 1703 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 1704 | { |
| 1705 | if (!PyUnicode_Check(unicode)) { |
| 1706 | PyErr_BadArgument(); |
| 1707 | return NULL; |
| 1708 | } |
| 1709 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 1710 | PyUnicode_GET_SIZE(unicode), |
| 1711 | NULL, |
| 1712 | 0); |
| 1713 | } |
| 1714 | |
| 1715 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 1716 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1717 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 1718 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1719 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1720 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1721 | const char *errors) |
| 1722 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1723 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1724 | Py_ssize_t startinpos; |
| 1725 | Py_ssize_t endinpos; |
| 1726 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1727 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1728 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1729 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1730 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1731 | char* message; |
| 1732 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1733 | PyObject *errorHandler = NULL; |
| 1734 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1735 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1736 | /* Escaped strings will always be longer than the resulting |
| 1737 | 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] | 1738 | length after conversion to the true value. |
| 1739 | (but if the error callback returns a long replacement string |
| 1740 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1741 | v = _PyUnicode_New(size); |
| 1742 | if (v == NULL) |
| 1743 | goto onError; |
| 1744 | if (size == 0) |
| 1745 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1746 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1747 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1748 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1749 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1750 | while (s < end) { |
| 1751 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 1752 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1753 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1754 | |
| 1755 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 1756 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1757 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1758 | continue; |
| 1759 | } |
| 1760 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1761 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1762 | /* \ - Escapes */ |
| 1763 | s++; |
| 1764 | switch (*s++) { |
| 1765 | |
| 1766 | /* \x escapes */ |
| 1767 | case '\n': break; |
| 1768 | case '\\': *p++ = '\\'; break; |
| 1769 | case '\'': *p++ = '\''; break; |
| 1770 | case '\"': *p++ = '\"'; break; |
| 1771 | case 'b': *p++ = '\b'; break; |
| 1772 | case 'f': *p++ = '\014'; break; /* FF */ |
| 1773 | case 't': *p++ = '\t'; break; |
| 1774 | case 'n': *p++ = '\n'; break; |
| 1775 | case 'r': *p++ = '\r'; break; |
| 1776 | case 'v': *p++ = '\013'; break; /* VT */ |
| 1777 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 1778 | |
| 1779 | /* \OOO (octal) escapes */ |
| 1780 | case '0': case '1': case '2': case '3': |
| 1781 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1782 | x = s[-1] - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1783 | if ('0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1784 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1785 | if ('0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1786 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1787 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1788 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1789 | break; |
| 1790 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1791 | /* hex escapes */ |
| 1792 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1793 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1794 | digits = 2; |
| 1795 | message = "truncated \\xXX escape"; |
| 1796 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1797 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1798 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1799 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1800 | digits = 4; |
| 1801 | message = "truncated \\uXXXX escape"; |
| 1802 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1803 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1804 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1805 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1806 | digits = 8; |
| 1807 | message = "truncated \\UXXXXXXXX escape"; |
| 1808 | hexescape: |
| 1809 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1810 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1811 | if (s+digits>end) { |
| 1812 | endinpos = size; |
| 1813 | if (unicode_decode_call_errorhandler( |
| 1814 | errors, &errorHandler, |
| 1815 | "unicodeescape", "end of string in escape sequence", |
| 1816 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1817 | (PyObject **)&v, &outpos, &p)) |
| 1818 | goto onError; |
| 1819 | goto nextByte; |
| 1820 | } |
| 1821 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1822 | c = (unsigned char) s[i]; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1823 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1824 | endinpos = (s+i+1)-starts; |
| 1825 | if (unicode_decode_call_errorhandler( |
| 1826 | errors, &errorHandler, |
| 1827 | "unicodeescape", message, |
| 1828 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1829 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1830 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1831 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1832 | } |
| 1833 | chr = (chr<<4) & ~0xF; |
| 1834 | if (c >= '0' && c <= '9') |
| 1835 | chr += c - '0'; |
| 1836 | else if (c >= 'a' && c <= 'f') |
| 1837 | chr += 10 + c - 'a'; |
| 1838 | else |
| 1839 | chr += 10 + c - 'A'; |
| 1840 | } |
| 1841 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 1842 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1843 | /* _decoding_error will have already written into the |
| 1844 | target buffer. */ |
| 1845 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1846 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1847 | /* when we get here, chr is a 32-bit unicode character */ |
| 1848 | if (chr <= 0xffff) |
| 1849 | /* UCS-2 character */ |
| 1850 | *p++ = (Py_UNICODE) chr; |
| 1851 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1852 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1853 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1854 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1855 | *p++ = chr; |
| 1856 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1857 | chr -= 0x10000L; |
| 1858 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1859 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1860 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1861 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1862 | endinpos = s-starts; |
| 1863 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1864 | if (unicode_decode_call_errorhandler( |
| 1865 | errors, &errorHandler, |
| 1866 | "unicodeescape", "illegal Unicode character", |
| 1867 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1868 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1869 | goto onError; |
| 1870 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1871 | break; |
| 1872 | |
| 1873 | /* \N{name} */ |
| 1874 | case 'N': |
| 1875 | message = "malformed \\N character escape"; |
| 1876 | if (ucnhash_CAPI == NULL) { |
| 1877 | /* load the unicode data module */ |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1878 | PyObject *m, *api; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1879 | m = PyImport_ImportModule("unicodedata"); |
| 1880 | if (m == NULL) |
| 1881 | goto ucnhashError; |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1882 | api = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1883 | Py_DECREF(m); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1884 | if (api == NULL) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1885 | goto ucnhashError; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 1886 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1887 | Py_DECREF(api); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1888 | if (ucnhash_CAPI == NULL) |
| 1889 | goto ucnhashError; |
| 1890 | } |
| 1891 | if (*s == '{') { |
| 1892 | const char *start = s+1; |
| 1893 | /* look for the closing brace */ |
| 1894 | while (*s != '}' && s < end) |
| 1895 | s++; |
| 1896 | if (s > start && s < end && *s == '}') { |
| 1897 | /* found a name. look it up in the unicode database */ |
| 1898 | message = "unknown Unicode character name"; |
| 1899 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 1900 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1901 | goto store; |
| 1902 | } |
| 1903 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1904 | endinpos = s-starts; |
| 1905 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1906 | if (unicode_decode_call_errorhandler( |
| 1907 | errors, &errorHandler, |
| 1908 | "unicodeescape", message, |
| 1909 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1910 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1911 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1912 | break; |
| 1913 | |
| 1914 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1915 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1916 | message = "\\ at end of string"; |
| 1917 | s--; |
| 1918 | endinpos = s-starts; |
| 1919 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1920 | if (unicode_decode_call_errorhandler( |
| 1921 | errors, &errorHandler, |
| 1922 | "unicodeescape", message, |
| 1923 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1924 | (PyObject **)&v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1925 | goto onError; |
| 1926 | } |
| 1927 | else { |
| 1928 | *p++ = '\\'; |
| 1929 | *p++ = (unsigned char)s[-1]; |
| 1930 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1931 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1932 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1933 | nextByte: |
| 1934 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1935 | } |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1936 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1937 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 1938 | Py_XDECREF(errorHandler); |
| 1939 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1940 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1941 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1942 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1943 | PyErr_SetString( |
| 1944 | PyExc_UnicodeError, |
| 1945 | "\\N escapes not supported (can't load unicodedata module)" |
| 1946 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1947 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1948 | Py_XDECREF(errorHandler); |
| 1949 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 1950 | return NULL; |
| 1951 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1952 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1953 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1954 | Py_XDECREF(errorHandler); |
| 1955 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1956 | return NULL; |
| 1957 | } |
| 1958 | |
| 1959 | /* Return a Unicode-Escape string version of the Unicode object. |
| 1960 | |
| 1961 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 1962 | appropriate. |
| 1963 | |
| 1964 | */ |
| 1965 | |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 1966 | static const Py_UNICODE *findchar(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1967 | Py_ssize_t size, |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 1968 | Py_UNICODE ch); |
| 1969 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1970 | static |
| 1971 | PyObject *unicodeescape_string(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1972 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1973 | int quotes) |
| 1974 | { |
| 1975 | PyObject *repr; |
| 1976 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1977 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 1978 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1979 | |
| 1980 | repr = PyString_FromStringAndSize(NULL, 2 + 6*size + 1); |
| 1981 | if (repr == NULL) |
| 1982 | return NULL; |
| 1983 | |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1984 | p = PyString_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1985 | |
| 1986 | if (quotes) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1987 | *p++ = 'u'; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1988 | *p++ = (findchar(s, size, '\'') && |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1989 | !findchar(s, size, '"')) ? '"' : '\''; |
| 1990 | } |
| 1991 | while (size-- > 0) { |
| 1992 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 1993 | |
Hye-Shik Chang | 835b243 | 2005-12-17 04:38:31 +0000 | [diff] [blame] | 1994 | /* Escape quotes and backslashes */ |
| 1995 | if ((quotes && |
| 1996 | ch == (Py_UNICODE) PyString_AS_STRING(repr)[1]) || ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1997 | *p++ = '\\'; |
| 1998 | *p++ = (char) ch; |
Guido van Rossum | ad9744a | 2001-09-21 15:38:17 +0000 | [diff] [blame] | 1999 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2000 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2001 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 2002 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2003 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 2004 | else if (ch >= 0x10000) { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2005 | Py_ssize_t offset = p - PyString_AS_STRING(repr); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2006 | |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2007 | /* Resize the string if necessary */ |
| 2008 | if (offset + 12 > PyString_GET_SIZE(repr)) { |
| 2009 | if (_PyString_Resize(&repr, PyString_GET_SIZE(repr) + 100)) |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2010 | return NULL; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2011 | p = PyString_AS_STRING(repr) + offset; |
| 2012 | } |
| 2013 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2014 | *p++ = '\\'; |
| 2015 | *p++ = 'U'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2016 | *p++ = hexdigit[(ch >> 28) & 0x0000000F]; |
| 2017 | *p++ = hexdigit[(ch >> 24) & 0x0000000F]; |
| 2018 | *p++ = hexdigit[(ch >> 20) & 0x0000000F]; |
| 2019 | *p++ = hexdigit[(ch >> 16) & 0x0000000F]; |
| 2020 | *p++ = hexdigit[(ch >> 12) & 0x0000000F]; |
| 2021 | *p++ = hexdigit[(ch >> 8) & 0x0000000F]; |
| 2022 | *p++ = hexdigit[(ch >> 4) & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2023 | *p++ = hexdigit[ch & 0x0000000F]; |
| 2024 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2025 | } |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 2026 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2027 | /* Map UTF-16 surrogate pairs to Unicode \UXXXXXXXX escapes */ |
| 2028 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 2029 | Py_UNICODE ch2; |
| 2030 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2031 | |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2032 | ch2 = *s++; |
| 2033 | size--; |
| 2034 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 2035 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 2036 | *p++ = '\\'; |
| 2037 | *p++ = 'U'; |
| 2038 | *p++ = hexdigit[(ucs >> 28) & 0x0000000F]; |
| 2039 | *p++ = hexdigit[(ucs >> 24) & 0x0000000F]; |
| 2040 | *p++ = hexdigit[(ucs >> 20) & 0x0000000F]; |
| 2041 | *p++ = hexdigit[(ucs >> 16) & 0x0000000F]; |
| 2042 | *p++ = hexdigit[(ucs >> 12) & 0x0000000F]; |
| 2043 | *p++ = hexdigit[(ucs >> 8) & 0x0000000F]; |
| 2044 | *p++ = hexdigit[(ucs >> 4) & 0x0000000F]; |
| 2045 | *p++ = hexdigit[ucs & 0x0000000F]; |
| 2046 | continue; |
| 2047 | } |
| 2048 | /* Fall through: isolated surrogates are copied as-is */ |
| 2049 | s--; |
| 2050 | size++; |
| 2051 | } |
| 2052 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2053 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2054 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2055 | *p++ = '\\'; |
| 2056 | *p++ = 'u'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2057 | *p++ = hexdigit[(ch >> 12) & 0x000F]; |
| 2058 | *p++ = hexdigit[(ch >> 8) & 0x000F]; |
| 2059 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2060 | *p++ = hexdigit[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2061 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2062 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2063 | /* Map special whitespace to '\t', \n', '\r' */ |
| 2064 | else if (ch == '\t') { |
| 2065 | *p++ = '\\'; |
| 2066 | *p++ = 't'; |
| 2067 | } |
| 2068 | else if (ch == '\n') { |
| 2069 | *p++ = '\\'; |
| 2070 | *p++ = 'n'; |
| 2071 | } |
| 2072 | else if (ch == '\r') { |
| 2073 | *p++ = '\\'; |
| 2074 | *p++ = 'r'; |
| 2075 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2076 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2077 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 2078 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2079 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2080 | *p++ = 'x'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2081 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2082 | *p++ = hexdigit[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2083 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2084 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2085 | /* Copy everything else as-is */ |
| 2086 | else |
| 2087 | *p++ = (char) ch; |
| 2088 | } |
| 2089 | if (quotes) |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2090 | *p++ = PyString_AS_STRING(repr)[1]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2091 | |
| 2092 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2093 | _PyString_Resize(&repr, p - PyString_AS_STRING(repr)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2094 | return repr; |
| 2095 | } |
| 2096 | |
| 2097 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2098 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2099 | { |
| 2100 | return unicodeescape_string(s, size, 0); |
| 2101 | } |
| 2102 | |
| 2103 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 2104 | { |
| 2105 | if (!PyUnicode_Check(unicode)) { |
| 2106 | PyErr_BadArgument(); |
| 2107 | return NULL; |
| 2108 | } |
| 2109 | return PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2110 | PyUnicode_GET_SIZE(unicode)); |
| 2111 | } |
| 2112 | |
| 2113 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 2114 | |
| 2115 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2116 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2117 | const char *errors) |
| 2118 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2119 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2120 | Py_ssize_t startinpos; |
| 2121 | Py_ssize_t endinpos; |
| 2122 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2123 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2124 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2125 | const char *end; |
| 2126 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2127 | PyObject *errorHandler = NULL; |
| 2128 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2129 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2130 | /* Escaped strings will always be longer than the resulting |
| 2131 | 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] | 2132 | length after conversion to the true value. (But decoding error |
| 2133 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2134 | v = _PyUnicode_New(size); |
| 2135 | if (v == NULL) |
| 2136 | goto onError; |
| 2137 | if (size == 0) |
| 2138 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2139 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2140 | end = s + size; |
| 2141 | while (s < end) { |
| 2142 | unsigned char c; |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 2143 | Py_UCS4 x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2144 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2145 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2146 | |
| 2147 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2148 | if (*s != '\\') { |
| 2149 | *p++ = (unsigned char)*s++; |
| 2150 | continue; |
| 2151 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2152 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2153 | |
| 2154 | /* \u-escapes are only interpreted iff the number of leading |
| 2155 | backslashes if odd */ |
| 2156 | bs = s; |
| 2157 | for (;s < end;) { |
| 2158 | if (*s != '\\') |
| 2159 | break; |
| 2160 | *p++ = (unsigned char)*s++; |
| 2161 | } |
| 2162 | if (((s - bs) & 1) == 0 || |
| 2163 | s >= end || |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2164 | (*s != 'u' && *s != 'U')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2165 | continue; |
| 2166 | } |
| 2167 | p--; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2168 | count = *s=='u' ? 4 : 8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2169 | s++; |
| 2170 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2171 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2172 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2173 | for (x = 0, i = 0; i < count; ++i, ++s) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2174 | c = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2175 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2176 | endinpos = s-starts; |
| 2177 | if (unicode_decode_call_errorhandler( |
| 2178 | errors, &errorHandler, |
| 2179 | "rawunicodeescape", "truncated \\uXXXX", |
| 2180 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2181 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2182 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2183 | goto nextByte; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2184 | } |
| 2185 | x = (x<<4) & ~0xF; |
| 2186 | if (c >= '0' && c <= '9') |
| 2187 | x += c - '0'; |
| 2188 | else if (c >= 'a' && c <= 'f') |
| 2189 | x += 10 + c - 'a'; |
| 2190 | else |
| 2191 | x += 10 + c - 'A'; |
| 2192 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2193 | #ifndef Py_UNICODE_WIDE |
| 2194 | if (x > 0x10000) { |
| 2195 | if (unicode_decode_call_errorhandler( |
| 2196 | errors, &errorHandler, |
| 2197 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
| 2198 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2199 | (PyObject **)&v, &outpos, &p)) |
| 2200 | goto onError; |
| 2201 | } |
| 2202 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2203 | *p++ = x; |
| 2204 | nextByte: |
| 2205 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2206 | } |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2207 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2208 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2209 | Py_XDECREF(errorHandler); |
| 2210 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2211 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2212 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2213 | onError: |
| 2214 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2215 | Py_XDECREF(errorHandler); |
| 2216 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2217 | return NULL; |
| 2218 | } |
| 2219 | |
| 2220 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2221 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2222 | { |
| 2223 | PyObject *repr; |
| 2224 | char *p; |
| 2225 | char *q; |
| 2226 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2227 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2228 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2229 | #ifdef Py_UNICODE_WIDE |
| 2230 | repr = PyString_FromStringAndSize(NULL, 10 * size); |
| 2231 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2232 | repr = PyString_FromStringAndSize(NULL, 6 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2233 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2234 | if (repr == NULL) |
| 2235 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2236 | if (size == 0) |
| 2237 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2238 | |
| 2239 | p = q = PyString_AS_STRING(repr); |
| 2240 | while (size-- > 0) { |
| 2241 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2242 | #ifdef Py_UNICODE_WIDE |
| 2243 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2244 | if (ch >= 0x10000) { |
| 2245 | *p++ = '\\'; |
| 2246 | *p++ = 'U'; |
| 2247 | *p++ = hexdigit[(ch >> 28) & 0xf]; |
| 2248 | *p++ = hexdigit[(ch >> 24) & 0xf]; |
| 2249 | *p++ = hexdigit[(ch >> 20) & 0xf]; |
| 2250 | *p++ = hexdigit[(ch >> 16) & 0xf]; |
| 2251 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 2252 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 2253 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 2254 | *p++ = hexdigit[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2255 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2256 | else |
| 2257 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2258 | /* Map 16-bit characters to '\uxxxx' */ |
| 2259 | if (ch >= 256) { |
| 2260 | *p++ = '\\'; |
| 2261 | *p++ = 'u'; |
| 2262 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 2263 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 2264 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 2265 | *p++ = hexdigit[ch & 15]; |
| 2266 | } |
| 2267 | /* Copy everything else as-is */ |
| 2268 | else |
| 2269 | *p++ = (char) ch; |
| 2270 | } |
| 2271 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2272 | _PyString_Resize(&repr, p - q); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2273 | return repr; |
| 2274 | } |
| 2275 | |
| 2276 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 2277 | { |
| 2278 | if (!PyUnicode_Check(unicode)) { |
| 2279 | PyErr_BadArgument(); |
| 2280 | return NULL; |
| 2281 | } |
| 2282 | return PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2283 | PyUnicode_GET_SIZE(unicode)); |
| 2284 | } |
| 2285 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2286 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 2287 | |
| 2288 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2289 | Py_ssize_t size, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2290 | const char *errors) |
| 2291 | { |
| 2292 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2293 | Py_ssize_t startinpos; |
| 2294 | Py_ssize_t endinpos; |
| 2295 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2296 | PyUnicodeObject *v; |
| 2297 | Py_UNICODE *p; |
| 2298 | const char *end; |
| 2299 | const char *reason; |
| 2300 | PyObject *errorHandler = NULL; |
| 2301 | PyObject *exc = NULL; |
| 2302 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 2303 | #ifdef Py_UNICODE_WIDE |
| 2304 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 2305 | #endif |
| 2306 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2307 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 2308 | if (v == NULL) |
| 2309 | goto onError; |
| 2310 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
| 2311 | return (PyObject *)v; |
| 2312 | p = PyUnicode_AS_UNICODE(v); |
| 2313 | end = s + size; |
| 2314 | |
| 2315 | while (s < end) { |
| 2316 | *p = *(Py_UNICODE *)s; |
| 2317 | /* We have to sanity check the raw data, otherwise doom looms for |
| 2318 | some malformed UCS-4 data. */ |
| 2319 | if ( |
| 2320 | #ifdef Py_UNICODE_WIDE |
| 2321 | *p > unimax || *p < 0 || |
| 2322 | #endif |
| 2323 | end-s < Py_UNICODE_SIZE |
| 2324 | ) |
| 2325 | { |
| 2326 | startinpos = s - starts; |
| 2327 | if (end-s < Py_UNICODE_SIZE) { |
| 2328 | endinpos = end-starts; |
| 2329 | reason = "truncated input"; |
| 2330 | } |
| 2331 | else { |
| 2332 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 2333 | reason = "illegal code point (> 0x10FFFF)"; |
| 2334 | } |
| 2335 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 2336 | if (unicode_decode_call_errorhandler( |
| 2337 | errors, &errorHandler, |
| 2338 | "unicode_internal", reason, |
| 2339 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2340 | (PyObject **)&v, &outpos, &p)) { |
| 2341 | goto onError; |
| 2342 | } |
| 2343 | } |
| 2344 | else { |
| 2345 | p++; |
| 2346 | s += Py_UNICODE_SIZE; |
| 2347 | } |
| 2348 | } |
| 2349 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2350 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2351 | goto onError; |
| 2352 | Py_XDECREF(errorHandler); |
| 2353 | Py_XDECREF(exc); |
| 2354 | return (PyObject *)v; |
| 2355 | |
| 2356 | onError: |
| 2357 | Py_XDECREF(v); |
| 2358 | Py_XDECREF(errorHandler); |
| 2359 | Py_XDECREF(exc); |
| 2360 | return NULL; |
| 2361 | } |
| 2362 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2363 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 2364 | |
| 2365 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2366 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2367 | const char *errors) |
| 2368 | { |
| 2369 | PyUnicodeObject *v; |
| 2370 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2371 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2372 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2373 | if (size == 1) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 2374 | Py_UNICODE r = *(unsigned char*)s; |
| 2375 | return PyUnicode_FromUnicode(&r, 1); |
| 2376 | } |
| 2377 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2378 | v = _PyUnicode_New(size); |
| 2379 | if (v == NULL) |
| 2380 | goto onError; |
| 2381 | if (size == 0) |
| 2382 | return (PyObject *)v; |
| 2383 | p = PyUnicode_AS_UNICODE(v); |
| 2384 | while (size-- > 0) |
| 2385 | *p++ = (unsigned char)*s++; |
| 2386 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2387 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2388 | onError: |
| 2389 | Py_XDECREF(v); |
| 2390 | return NULL; |
| 2391 | } |
| 2392 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2393 | /* create or adjust a UnicodeEncodeError */ |
| 2394 | static void make_encode_exception(PyObject **exceptionObject, |
| 2395 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2396 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 2397 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2398 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2399 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2400 | if (*exceptionObject == NULL) { |
| 2401 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 2402 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2403 | } |
| 2404 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2405 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 2406 | goto onError; |
| 2407 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 2408 | goto onError; |
| 2409 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 2410 | goto onError; |
| 2411 | return; |
| 2412 | onError: |
| 2413 | Py_DECREF(*exceptionObject); |
| 2414 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2415 | } |
| 2416 | } |
| 2417 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2418 | /* raises a UnicodeEncodeError */ |
| 2419 | static void raise_encode_exception(PyObject **exceptionObject, |
| 2420 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2421 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 2422 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2423 | const char *reason) |
| 2424 | { |
| 2425 | make_encode_exception(exceptionObject, |
| 2426 | encoding, unicode, size, startpos, endpos, reason); |
| 2427 | if (*exceptionObject != NULL) |
| 2428 | PyCodec_StrictErrors(*exceptionObject); |
| 2429 | } |
| 2430 | |
| 2431 | /* error handling callback helper: |
| 2432 | build arguments, call the callback and check the arguments, |
| 2433 | put the result into newpos and return the replacement string, which |
| 2434 | has to be freed by the caller */ |
| 2435 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 2436 | PyObject **errorHandler, |
| 2437 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2438 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 2439 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 2440 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2441 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2442 | static char *argparse = "O!n;encoding error handler must return (unicode, int) tuple"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2443 | |
| 2444 | PyObject *restuple; |
| 2445 | PyObject *resunicode; |
| 2446 | |
| 2447 | if (*errorHandler == NULL) { |
| 2448 | *errorHandler = PyCodec_LookupError(errors); |
| 2449 | if (*errorHandler == NULL) |
| 2450 | return NULL; |
| 2451 | } |
| 2452 | |
| 2453 | make_encode_exception(exceptionObject, |
| 2454 | encoding, unicode, size, startpos, endpos, reason); |
| 2455 | if (*exceptionObject == NULL) |
| 2456 | return NULL; |
| 2457 | |
| 2458 | restuple = PyObject_CallFunctionObjArgs( |
| 2459 | *errorHandler, *exceptionObject, NULL); |
| 2460 | if (restuple == NULL) |
| 2461 | return NULL; |
| 2462 | if (!PyTuple_Check(restuple)) { |
| 2463 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 2464 | Py_DECREF(restuple); |
| 2465 | return NULL; |
| 2466 | } |
| 2467 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 2468 | &resunicode, newpos)) { |
| 2469 | Py_DECREF(restuple); |
| 2470 | return NULL; |
| 2471 | } |
| 2472 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2473 | *newpos = size+*newpos; |
| 2474 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 2475 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2476 | Py_DECREF(restuple); |
| 2477 | return NULL; |
| 2478 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2479 | Py_INCREF(resunicode); |
| 2480 | Py_DECREF(restuple); |
| 2481 | return resunicode; |
| 2482 | } |
| 2483 | |
| 2484 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2485 | Py_ssize_t size, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2486 | const char *errors, |
| 2487 | int limit) |
| 2488 | { |
| 2489 | /* output object */ |
| 2490 | PyObject *res; |
| 2491 | /* pointers to the beginning and end+1 of input */ |
| 2492 | const Py_UNICODE *startp = p; |
| 2493 | const Py_UNICODE *endp = p + size; |
| 2494 | /* pointer to the beginning of the unencodable characters */ |
| 2495 | /* const Py_UNICODE *badp = NULL; */ |
| 2496 | /* pointer into the output */ |
| 2497 | char *str; |
| 2498 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2499 | Py_ssize_t respos = 0; |
| 2500 | Py_ssize_t ressize; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2501 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 2502 | const char *reason = (limit == 256) ? "ordinal not in range(256)" : "ordinal not in range(128)"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2503 | PyObject *errorHandler = NULL; |
| 2504 | PyObject *exc = NULL; |
| 2505 | /* the following variable is used for caching string comparisons |
| 2506 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 2507 | int known_errorHandler = -1; |
| 2508 | |
| 2509 | /* allocate enough for a simple encoding without |
| 2510 | replacements, if we need more, we'll resize */ |
| 2511 | res = PyString_FromStringAndSize(NULL, size); |
| 2512 | if (res == NULL) |
| 2513 | goto onError; |
| 2514 | if (size == 0) |
| 2515 | return res; |
| 2516 | str = PyString_AS_STRING(res); |
| 2517 | ressize = size; |
| 2518 | |
| 2519 | while (p<endp) { |
| 2520 | Py_UNICODE c = *p; |
| 2521 | |
| 2522 | /* can we encode this? */ |
| 2523 | if (c<limit) { |
| 2524 | /* no overflow check, because we know that the space is enough */ |
| 2525 | *str++ = (char)c; |
| 2526 | ++p; |
| 2527 | } |
| 2528 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2529 | Py_ssize_t unicodepos = p-startp; |
| 2530 | Py_ssize_t requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2531 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2532 | Py_ssize_t repsize; |
| 2533 | Py_ssize_t newpos; |
| 2534 | Py_ssize_t respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2535 | Py_UNICODE *uni2; |
| 2536 | /* startpos for collecting unencodable chars */ |
| 2537 | const Py_UNICODE *collstart = p; |
| 2538 | const Py_UNICODE *collend = p; |
| 2539 | /* find all unecodable characters */ |
| 2540 | while ((collend < endp) && ((*collend)>=limit)) |
| 2541 | ++collend; |
| 2542 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 2543 | if (known_errorHandler==-1) { |
| 2544 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 2545 | known_errorHandler = 1; |
| 2546 | else if (!strcmp(errors, "replace")) |
| 2547 | known_errorHandler = 2; |
| 2548 | else if (!strcmp(errors, "ignore")) |
| 2549 | known_errorHandler = 3; |
| 2550 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 2551 | known_errorHandler = 4; |
| 2552 | else |
| 2553 | known_errorHandler = 0; |
| 2554 | } |
| 2555 | switch (known_errorHandler) { |
| 2556 | case 1: /* strict */ |
| 2557 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 2558 | goto onError; |
| 2559 | case 2: /* replace */ |
| 2560 | while (collstart++<collend) |
| 2561 | *str++ = '?'; /* fall through */ |
| 2562 | case 3: /* ignore */ |
| 2563 | p = collend; |
| 2564 | break; |
| 2565 | case 4: /* xmlcharrefreplace */ |
| 2566 | respos = str-PyString_AS_STRING(res); |
| 2567 | /* determine replacement size (temporarily (mis)uses p) */ |
| 2568 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 2569 | if (*p<10) |
| 2570 | repsize += 2+1+1; |
| 2571 | else if (*p<100) |
| 2572 | repsize += 2+2+1; |
| 2573 | else if (*p<1000) |
| 2574 | repsize += 2+3+1; |
| 2575 | else if (*p<10000) |
| 2576 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 2577 | #ifndef Py_UNICODE_WIDE |
| 2578 | else |
| 2579 | repsize += 2+5+1; |
| 2580 | #else |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2581 | else if (*p<100000) |
| 2582 | repsize += 2+5+1; |
| 2583 | else if (*p<1000000) |
| 2584 | repsize += 2+6+1; |
| 2585 | else |
| 2586 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2587 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2588 | } |
| 2589 | requiredsize = respos+repsize+(endp-collend); |
| 2590 | if (requiredsize > ressize) { |
| 2591 | if (requiredsize<2*ressize) |
| 2592 | requiredsize = 2*ressize; |
| 2593 | if (_PyString_Resize(&res, requiredsize)) |
| 2594 | goto onError; |
| 2595 | str = PyString_AS_STRING(res) + respos; |
| 2596 | ressize = requiredsize; |
| 2597 | } |
| 2598 | /* generate replacement (temporarily (mis)uses p) */ |
| 2599 | for (p = collstart; p < collend; ++p) { |
| 2600 | str += sprintf(str, "&#%d;", (int)*p); |
| 2601 | } |
| 2602 | p = collend; |
| 2603 | break; |
| 2604 | default: |
| 2605 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 2606 | encoding, reason, startp, size, &exc, |
| 2607 | collstart-startp, collend-startp, &newpos); |
| 2608 | if (repunicode == NULL) |
| 2609 | goto onError; |
| 2610 | /* need more space? (at least enough for what we |
| 2611 | have+the replacement+the rest of the string, so |
| 2612 | we won't have to check space for encodable characters) */ |
| 2613 | respos = str-PyString_AS_STRING(res); |
| 2614 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 2615 | requiredsize = respos+repsize+(endp-collend); |
| 2616 | if (requiredsize > ressize) { |
| 2617 | if (requiredsize<2*ressize) |
| 2618 | requiredsize = 2*ressize; |
| 2619 | if (_PyString_Resize(&res, requiredsize)) { |
| 2620 | Py_DECREF(repunicode); |
| 2621 | goto onError; |
| 2622 | } |
| 2623 | str = PyString_AS_STRING(res) + respos; |
| 2624 | ressize = requiredsize; |
| 2625 | } |
| 2626 | /* check if there is anything unencodable in the replacement |
| 2627 | and copy it to the output */ |
| 2628 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 2629 | c = *uni2; |
| 2630 | if (c >= limit) { |
| 2631 | raise_encode_exception(&exc, encoding, startp, size, |
| 2632 | unicodepos, unicodepos+1, reason); |
| 2633 | Py_DECREF(repunicode); |
| 2634 | goto onError; |
| 2635 | } |
| 2636 | *str = (char)c; |
| 2637 | } |
| 2638 | p = startp + newpos; |
| 2639 | Py_DECREF(repunicode); |
| 2640 | } |
| 2641 | } |
| 2642 | } |
| 2643 | /* Resize if we allocated to much */ |
| 2644 | respos = str-PyString_AS_STRING(res); |
| 2645 | if (respos<ressize) |
| 2646 | /* If this falls res will be NULL */ |
| 2647 | _PyString_Resize(&res, respos); |
| 2648 | Py_XDECREF(errorHandler); |
| 2649 | Py_XDECREF(exc); |
| 2650 | return res; |
| 2651 | |
| 2652 | onError: |
| 2653 | Py_XDECREF(res); |
| 2654 | Py_XDECREF(errorHandler); |
| 2655 | Py_XDECREF(exc); |
| 2656 | return NULL; |
| 2657 | } |
| 2658 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2659 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2660 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2661 | const char *errors) |
| 2662 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2663 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2664 | } |
| 2665 | |
| 2666 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 2667 | { |
| 2668 | if (!PyUnicode_Check(unicode)) { |
| 2669 | PyErr_BadArgument(); |
| 2670 | return NULL; |
| 2671 | } |
| 2672 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 2673 | PyUnicode_GET_SIZE(unicode), |
| 2674 | NULL); |
| 2675 | } |
| 2676 | |
| 2677 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 2678 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2679 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2680 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2681 | const char *errors) |
| 2682 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2683 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2684 | PyUnicodeObject *v; |
| 2685 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2686 | Py_ssize_t startinpos; |
| 2687 | Py_ssize_t endinpos; |
| 2688 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2689 | const char *e; |
| 2690 | PyObject *errorHandler = NULL; |
| 2691 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2692 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2693 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 2694 | if (size == 1 && *(unsigned char*)s < 128) { |
| 2695 | Py_UNICODE r = *(unsigned char*)s; |
| 2696 | return PyUnicode_FromUnicode(&r, 1); |
| 2697 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2698 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2699 | v = _PyUnicode_New(size); |
| 2700 | if (v == NULL) |
| 2701 | goto onError; |
| 2702 | if (size == 0) |
| 2703 | return (PyObject *)v; |
| 2704 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2705 | e = s + size; |
| 2706 | while (s < e) { |
| 2707 | register unsigned char c = (unsigned char)*s; |
| 2708 | if (c < 128) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2709 | *p++ = c; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2710 | ++s; |
| 2711 | } |
| 2712 | else { |
| 2713 | startinpos = s-starts; |
| 2714 | endinpos = startinpos + 1; |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 2715 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2716 | if (unicode_decode_call_errorhandler( |
| 2717 | errors, &errorHandler, |
| 2718 | "ascii", "ordinal not in range(128)", |
| 2719 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2720 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2721 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2722 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2723 | } |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2724 | if (p - PyUnicode_AS_UNICODE(v) < PyString_GET_SIZE(v)) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2725 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2726 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2727 | Py_XDECREF(errorHandler); |
| 2728 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2729 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2730 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2731 | onError: |
| 2732 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2733 | Py_XDECREF(errorHandler); |
| 2734 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2735 | return NULL; |
| 2736 | } |
| 2737 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2738 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2739 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2740 | const char *errors) |
| 2741 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2742 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2743 | } |
| 2744 | |
| 2745 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 2746 | { |
| 2747 | if (!PyUnicode_Check(unicode)) { |
| 2748 | PyErr_BadArgument(); |
| 2749 | return NULL; |
| 2750 | } |
| 2751 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 2752 | PyUnicode_GET_SIZE(unicode), |
| 2753 | NULL); |
| 2754 | } |
| 2755 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2756 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2757 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2758 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2759 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2760 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2761 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2762 | const char *errors) |
| 2763 | { |
| 2764 | PyUnicodeObject *v; |
| 2765 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2766 | DWORD usize; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2767 | |
| 2768 | /* First get the size of the result */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2769 | assert(size < INT_MAX); |
| 2770 | usize = MultiByteToWideChar(CP_ACP, 0, s, (int)size, NULL, 0); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 2771 | if (size > 0 && usize==0) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2772 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2773 | |
| 2774 | v = _PyUnicode_New(usize); |
| 2775 | if (v == NULL) |
| 2776 | return NULL; |
| 2777 | if (usize == 0) |
| 2778 | return (PyObject *)v; |
| 2779 | p = PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2780 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, (int)size, p, usize)) { |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2781 | Py_DECREF(v); |
| 2782 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2783 | } |
| 2784 | |
| 2785 | return (PyObject *)v; |
| 2786 | } |
| 2787 | |
| 2788 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2789 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2790 | const char *errors) |
| 2791 | { |
| 2792 | PyObject *repr; |
| 2793 | char *s; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 2794 | DWORD mbcssize; |
| 2795 | |
| 2796 | /* If there are no characters, bail now! */ |
| 2797 | if (size==0) |
| 2798 | return PyString_FromString(""); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2799 | |
| 2800 | /* First get the size of the result */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2801 | assert(size<INT_MAX); |
| 2802 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, (int)size, NULL, 0, NULL, NULL); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2803 | if (mbcssize==0) |
| 2804 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2805 | |
| 2806 | repr = PyString_FromStringAndSize(NULL, mbcssize); |
| 2807 | if (repr == NULL) |
| 2808 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2809 | if (mbcssize == 0) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2810 | return repr; |
| 2811 | |
| 2812 | /* Do the conversion */ |
| 2813 | s = PyString_AS_STRING(repr); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2814 | assert(size < INT_MAX); |
| 2815 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, (int)size, s, mbcssize, NULL, NULL)) { |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2816 | Py_DECREF(repr); |
| 2817 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2818 | } |
| 2819 | return repr; |
| 2820 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2821 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2822 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 2823 | { |
| 2824 | if (!PyUnicode_Check(unicode)) { |
| 2825 | PyErr_BadArgument(); |
| 2826 | return NULL; |
| 2827 | } |
| 2828 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2829 | PyUnicode_GET_SIZE(unicode), |
| 2830 | NULL); |
| 2831 | } |
| 2832 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2833 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2834 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2835 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 2836 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2837 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2838 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2839 | PyObject *mapping, |
| 2840 | const char *errors) |
| 2841 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2842 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2843 | Py_ssize_t startinpos; |
| 2844 | Py_ssize_t endinpos; |
| 2845 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2846 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2847 | PyUnicodeObject *v; |
| 2848 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2849 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2850 | PyObject *errorHandler = NULL; |
| 2851 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2852 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2853 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2854 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2855 | /* Default to Latin-1 */ |
| 2856 | if (mapping == NULL) |
| 2857 | return PyUnicode_DecodeLatin1(s, size, errors); |
| 2858 | |
| 2859 | v = _PyUnicode_New(size); |
| 2860 | if (v == NULL) |
| 2861 | goto onError; |
| 2862 | if (size == 0) |
| 2863 | return (PyObject *)v; |
| 2864 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2865 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2866 | if (PyUnicode_CheckExact(mapping)) { |
| 2867 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 2868 | maplen = PyUnicode_GET_SIZE(mapping); |
| 2869 | while (s < e) { |
| 2870 | unsigned char ch = *s; |
| 2871 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2872 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2873 | if (ch < maplen) |
| 2874 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2875 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2876 | if (x == 0xfffe) { |
| 2877 | /* undefined mapping */ |
| 2878 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2879 | startinpos = s-starts; |
| 2880 | endinpos = startinpos+1; |
| 2881 | if (unicode_decode_call_errorhandler( |
| 2882 | errors, &errorHandler, |
| 2883 | "charmap", "character maps to <undefined>", |
| 2884 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2885 | (PyObject **)&v, &outpos, &p)) { |
| 2886 | goto onError; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 2887 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2888 | continue; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 2889 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2890 | *p++ = x; |
| 2891 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2892 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2893 | } |
| 2894 | else { |
| 2895 | while (s < e) { |
| 2896 | unsigned char ch = *s; |
| 2897 | PyObject *w, *x; |
| 2898 | |
| 2899 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 2900 | w = PyInt_FromLong((long)ch); |
| 2901 | if (w == NULL) |
| 2902 | goto onError; |
| 2903 | x = PyObject_GetItem(mapping, w); |
| 2904 | Py_DECREF(w); |
| 2905 | if (x == NULL) { |
| 2906 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 2907 | /* No mapping found means: mapping is undefined. */ |
| 2908 | PyErr_Clear(); |
| 2909 | x = Py_None; |
| 2910 | Py_INCREF(x); |
| 2911 | } else |
| 2912 | goto onError; |
| 2913 | } |
| 2914 | |
| 2915 | /* Apply mapping */ |
| 2916 | if (PyInt_Check(x)) { |
| 2917 | long value = PyInt_AS_LONG(x); |
| 2918 | if (value < 0 || value > 65535) { |
| 2919 | PyErr_SetString(PyExc_TypeError, |
| 2920 | "character mapping must be in range(65536)"); |
| 2921 | Py_DECREF(x); |
| 2922 | goto onError; |
| 2923 | } |
| 2924 | *p++ = (Py_UNICODE)value; |
| 2925 | } |
| 2926 | else if (x == Py_None) { |
| 2927 | /* undefined mapping */ |
| 2928 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2929 | startinpos = s-starts; |
| 2930 | endinpos = startinpos+1; |
| 2931 | if (unicode_decode_call_errorhandler( |
| 2932 | errors, &errorHandler, |
| 2933 | "charmap", "character maps to <undefined>", |
| 2934 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2935 | (PyObject **)&v, &outpos, &p)) { |
| 2936 | Py_DECREF(x); |
| 2937 | goto onError; |
| 2938 | } |
Walter Dörwald | d4fff17 | 2005-11-28 22:15:56 +0000 | [diff] [blame] | 2939 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2940 | continue; |
| 2941 | } |
| 2942 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2943 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2944 | |
| 2945 | if (targetsize == 1) |
| 2946 | /* 1-1 mapping */ |
| 2947 | *p++ = *PyUnicode_AS_UNICODE(x); |
| 2948 | |
| 2949 | else if (targetsize > 1) { |
| 2950 | /* 1-n mapping */ |
| 2951 | if (targetsize > extrachars) { |
| 2952 | /* resize first */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2953 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 2954 | Py_ssize_t needed = (targetsize - extrachars) + \ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2955 | (targetsize << 2); |
| 2956 | extrachars += needed; |
| 2957 | if (_PyUnicode_Resize(&v, |
| 2958 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 2959 | Py_DECREF(x); |
| 2960 | goto onError; |
| 2961 | } |
| 2962 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 2963 | } |
| 2964 | Py_UNICODE_COPY(p, |
| 2965 | PyUnicode_AS_UNICODE(x), |
| 2966 | targetsize); |
| 2967 | p += targetsize; |
| 2968 | extrachars -= targetsize; |
| 2969 | } |
| 2970 | /* 1-0 mapping: skip the character */ |
| 2971 | } |
| 2972 | else { |
| 2973 | /* wrong return value */ |
| 2974 | PyErr_SetString(PyExc_TypeError, |
| 2975 | "character mapping must return integer, None or unicode"); |
| 2976 | Py_DECREF(x); |
| 2977 | goto onError; |
| 2978 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2979 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2980 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2981 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2982 | } |
| 2983 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2984 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2985 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2986 | Py_XDECREF(errorHandler); |
| 2987 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2988 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2989 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2990 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2991 | Py_XDECREF(errorHandler); |
| 2992 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2993 | Py_XDECREF(v); |
| 2994 | return NULL; |
| 2995 | } |
| 2996 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2997 | /* Lookup the character ch in the mapping. If the character |
| 2998 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 2999 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3000 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3001 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3002 | PyObject *w = PyInt_FromLong((long)c); |
| 3003 | PyObject *x; |
| 3004 | |
| 3005 | if (w == NULL) |
| 3006 | return NULL; |
| 3007 | x = PyObject_GetItem(mapping, w); |
| 3008 | Py_DECREF(w); |
| 3009 | if (x == NULL) { |
| 3010 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3011 | /* No mapping found means: mapping is undefined. */ |
| 3012 | PyErr_Clear(); |
| 3013 | x = Py_None; |
| 3014 | Py_INCREF(x); |
| 3015 | return x; |
| 3016 | } else |
| 3017 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3018 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 3019 | else if (x == Py_None) |
| 3020 | return x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3021 | else if (PyInt_Check(x)) { |
| 3022 | long value = PyInt_AS_LONG(x); |
| 3023 | if (value < 0 || value > 255) { |
| 3024 | PyErr_SetString(PyExc_TypeError, |
| 3025 | "character mapping must be in range(256)"); |
| 3026 | Py_DECREF(x); |
| 3027 | return NULL; |
| 3028 | } |
| 3029 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3030 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3031 | else if (PyString_Check(x)) |
| 3032 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3033 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3034 | /* wrong return value */ |
| 3035 | PyErr_SetString(PyExc_TypeError, |
| 3036 | "character mapping must return integer, None or str"); |
| 3037 | Py_DECREF(x); |
| 3038 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3039 | } |
| 3040 | } |
| 3041 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3042 | /* lookup the character, put the result in the output string and adjust |
| 3043 | various state variables. Reallocate the output string if not enough |
| 3044 | space is available. Return a new reference to the object that |
| 3045 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 3046 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 3047 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3048 | static |
| 3049 | PyObject *charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3050 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3051 | { |
| 3052 | PyObject *rep = charmapencode_lookup(c, mapping); |
| 3053 | |
| 3054 | if (rep==NULL) |
| 3055 | return NULL; |
| 3056 | else if (rep==Py_None) |
| 3057 | return rep; |
| 3058 | else { |
| 3059 | char *outstart = PyString_AS_STRING(*outobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3060 | Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3061 | if (PyInt_Check(rep)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3062 | Py_ssize_t requiredsize = *outpos+1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3063 | if (outsize<requiredsize) { |
| 3064 | /* exponentially overallocate to minimize reallocations */ |
| 3065 | if (requiredsize < 2*outsize) |
| 3066 | requiredsize = 2*outsize; |
| 3067 | if (_PyString_Resize(outobj, requiredsize)) { |
| 3068 | Py_DECREF(rep); |
| 3069 | return NULL; |
| 3070 | } |
| 3071 | outstart = PyString_AS_STRING(*outobj); |
| 3072 | } |
| 3073 | outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); |
| 3074 | } |
| 3075 | else { |
| 3076 | const char *repchars = PyString_AS_STRING(rep); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3077 | Py_ssize_t repsize = PyString_GET_SIZE(rep); |
| 3078 | Py_ssize_t requiredsize = *outpos+repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3079 | if (outsize<requiredsize) { |
| 3080 | /* exponentially overallocate to minimize reallocations */ |
| 3081 | if (requiredsize < 2*outsize) |
| 3082 | requiredsize = 2*outsize; |
| 3083 | if (_PyString_Resize(outobj, requiredsize)) { |
| 3084 | Py_DECREF(rep); |
| 3085 | return NULL; |
| 3086 | } |
| 3087 | outstart = PyString_AS_STRING(*outobj); |
| 3088 | } |
| 3089 | memcpy(outstart + *outpos, repchars, repsize); |
| 3090 | *outpos += repsize; |
| 3091 | } |
| 3092 | } |
| 3093 | return rep; |
| 3094 | } |
| 3095 | |
| 3096 | /* handle an error in PyUnicode_EncodeCharmap |
| 3097 | Return 0 on success, -1 on error */ |
| 3098 | static |
| 3099 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3100 | const Py_UNICODE *p, Py_ssize_t size, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3101 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3102 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3103 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3104 | { |
| 3105 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3106 | Py_ssize_t repsize; |
| 3107 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3108 | Py_UNICODE *uni2; |
| 3109 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3110 | Py_ssize_t collstartpos = *inpos; |
| 3111 | Py_ssize_t collendpos = *inpos+1; |
| 3112 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3113 | char *encoding = "charmap"; |
| 3114 | char *reason = "character maps to <undefined>"; |
| 3115 | |
| 3116 | PyObject *x; |
| 3117 | /* find all unencodable characters */ |
| 3118 | while (collendpos < size) { |
| 3119 | x = charmapencode_lookup(p[collendpos], mapping); |
| 3120 | if (x==NULL) |
| 3121 | return -1; |
| 3122 | else if (x!=Py_None) { |
| 3123 | Py_DECREF(x); |
| 3124 | break; |
| 3125 | } |
| 3126 | Py_DECREF(x); |
| 3127 | ++collendpos; |
| 3128 | } |
| 3129 | /* cache callback name lookup |
| 3130 | * (if not done yet, i.e. it's the first error) */ |
| 3131 | if (*known_errorHandler==-1) { |
| 3132 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3133 | *known_errorHandler = 1; |
| 3134 | else if (!strcmp(errors, "replace")) |
| 3135 | *known_errorHandler = 2; |
| 3136 | else if (!strcmp(errors, "ignore")) |
| 3137 | *known_errorHandler = 3; |
| 3138 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3139 | *known_errorHandler = 4; |
| 3140 | else |
| 3141 | *known_errorHandler = 0; |
| 3142 | } |
| 3143 | switch (*known_errorHandler) { |
| 3144 | case 1: /* strict */ |
| 3145 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3146 | return -1; |
| 3147 | case 2: /* replace */ |
| 3148 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 3149 | x = charmapencode_output('?', mapping, res, respos); |
| 3150 | if (x==NULL) { |
| 3151 | return -1; |
| 3152 | } |
| 3153 | else if (x==Py_None) { |
| 3154 | Py_DECREF(x); |
| 3155 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3156 | return -1; |
| 3157 | } |
| 3158 | Py_DECREF(x); |
| 3159 | } |
| 3160 | /* fall through */ |
| 3161 | case 3: /* ignore */ |
| 3162 | *inpos = collendpos; |
| 3163 | break; |
| 3164 | case 4: /* xmlcharrefreplace */ |
| 3165 | /* generate replacement (temporarily (mis)uses p) */ |
| 3166 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 3167 | char buffer[2+29+1+1]; |
| 3168 | char *cp; |
| 3169 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 3170 | for (cp = buffer; *cp; ++cp) { |
| 3171 | x = charmapencode_output(*cp, mapping, res, respos); |
| 3172 | if (x==NULL) |
| 3173 | return -1; |
| 3174 | else if (x==Py_None) { |
| 3175 | Py_DECREF(x); |
| 3176 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3177 | return -1; |
| 3178 | } |
| 3179 | Py_DECREF(x); |
| 3180 | } |
| 3181 | } |
| 3182 | *inpos = collendpos; |
| 3183 | break; |
| 3184 | default: |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3185 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3186 | encoding, reason, p, size, exceptionObject, |
| 3187 | collstartpos, collendpos, &newpos); |
| 3188 | if (repunicode == NULL) |
| 3189 | return -1; |
| 3190 | /* generate replacement */ |
| 3191 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3192 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 3193 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 3194 | if (x==NULL) { |
| 3195 | Py_DECREF(repunicode); |
| 3196 | return -1; |
| 3197 | } |
| 3198 | else if (x==Py_None) { |
| 3199 | Py_DECREF(repunicode); |
| 3200 | Py_DECREF(x); |
| 3201 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3202 | return -1; |
| 3203 | } |
| 3204 | Py_DECREF(x); |
| 3205 | } |
| 3206 | *inpos = newpos; |
| 3207 | Py_DECREF(repunicode); |
| 3208 | } |
| 3209 | return 0; |
| 3210 | } |
| 3211 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3212 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3213 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3214 | PyObject *mapping, |
| 3215 | const char *errors) |
| 3216 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3217 | /* output object */ |
| 3218 | PyObject *res = NULL; |
| 3219 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3220 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3221 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3222 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3223 | PyObject *errorHandler = NULL; |
| 3224 | PyObject *exc = NULL; |
| 3225 | /* the following variable is used for caching string comparisons |
| 3226 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 3227 | * 3=ignore, 4=xmlcharrefreplace */ |
| 3228 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3229 | |
| 3230 | /* Default to Latin-1 */ |
| 3231 | if (mapping == NULL) |
| 3232 | return PyUnicode_EncodeLatin1(p, size, errors); |
| 3233 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3234 | /* allocate enough for a simple encoding without |
| 3235 | replacements, if we need more, we'll resize */ |
| 3236 | res = PyString_FromStringAndSize(NULL, size); |
| 3237 | if (res == NULL) |
| 3238 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3239 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3240 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3241 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3242 | while (inpos<size) { |
| 3243 | /* try to encode it */ |
| 3244 | PyObject *x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 3245 | if (x==NULL) /* error */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3246 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3247 | if (x==Py_None) { /* unencodable character */ |
| 3248 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 3249 | &exc, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3250 | &known_errorHandler, &errorHandler, errors, |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 3251 | &res, &respos)) { |
| 3252 | Py_DECREF(x); |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3253 | goto onError; |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 3254 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3255 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3256 | else |
| 3257 | /* done with this character => adjust input position */ |
| 3258 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3259 | Py_DECREF(x); |
| 3260 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3261 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3262 | /* Resize if we allocated to much */ |
| 3263 | if (respos<PyString_GET_SIZE(res)) { |
| 3264 | if (_PyString_Resize(&res, respos)) |
| 3265 | goto onError; |
| 3266 | } |
| 3267 | Py_XDECREF(exc); |
| 3268 | Py_XDECREF(errorHandler); |
| 3269 | return res; |
| 3270 | |
| 3271 | onError: |
| 3272 | Py_XDECREF(res); |
| 3273 | Py_XDECREF(exc); |
| 3274 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3275 | return NULL; |
| 3276 | } |
| 3277 | |
| 3278 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 3279 | PyObject *mapping) |
| 3280 | { |
| 3281 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 3282 | PyErr_BadArgument(); |
| 3283 | return NULL; |
| 3284 | } |
| 3285 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 3286 | PyUnicode_GET_SIZE(unicode), |
| 3287 | mapping, |
| 3288 | NULL); |
| 3289 | } |
| 3290 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3291 | /* create or adjust a UnicodeTranslateError */ |
| 3292 | static void make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3293 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3294 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3295 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3296 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3297 | if (*exceptionObject == NULL) { |
| 3298 | *exceptionObject = PyUnicodeTranslateError_Create( |
| 3299 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3300 | } |
| 3301 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3302 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 3303 | goto onError; |
| 3304 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 3305 | goto onError; |
| 3306 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 3307 | goto onError; |
| 3308 | return; |
| 3309 | onError: |
| 3310 | Py_DECREF(*exceptionObject); |
| 3311 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3312 | } |
| 3313 | } |
| 3314 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3315 | /* raises a UnicodeTranslateError */ |
| 3316 | static void raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3317 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3318 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3319 | const char *reason) |
| 3320 | { |
| 3321 | make_translate_exception(exceptionObject, |
| 3322 | unicode, size, startpos, endpos, reason); |
| 3323 | if (*exceptionObject != NULL) |
| 3324 | PyCodec_StrictErrors(*exceptionObject); |
| 3325 | } |
| 3326 | |
| 3327 | /* error handling callback helper: |
| 3328 | build arguments, call the callback and check the arguments, |
| 3329 | put the result into newpos and return the replacement string, which |
| 3330 | has to be freed by the caller */ |
| 3331 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 3332 | PyObject **errorHandler, |
| 3333 | const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3334 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 3335 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3336 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3337 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3338 | static char *argparse = "O!n;translating error handler must return (unicode, int) tuple"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3339 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3340 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3341 | PyObject *restuple; |
| 3342 | PyObject *resunicode; |
| 3343 | |
| 3344 | if (*errorHandler == NULL) { |
| 3345 | *errorHandler = PyCodec_LookupError(errors); |
| 3346 | if (*errorHandler == NULL) |
| 3347 | return NULL; |
| 3348 | } |
| 3349 | |
| 3350 | make_translate_exception(exceptionObject, |
| 3351 | unicode, size, startpos, endpos, reason); |
| 3352 | if (*exceptionObject == NULL) |
| 3353 | return NULL; |
| 3354 | |
| 3355 | restuple = PyObject_CallFunctionObjArgs( |
| 3356 | *errorHandler, *exceptionObject, NULL); |
| 3357 | if (restuple == NULL) |
| 3358 | return NULL; |
| 3359 | if (!PyTuple_Check(restuple)) { |
| 3360 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 3361 | Py_DECREF(restuple); |
| 3362 | return NULL; |
| 3363 | } |
| 3364 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3365 | &resunicode, &i_newpos)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3366 | Py_DECREF(restuple); |
| 3367 | return NULL; |
| 3368 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3369 | if (i_newpos<0) |
| 3370 | *newpos = size+i_newpos; |
| 3371 | else |
| 3372 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3373 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 3374 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3375 | Py_DECREF(restuple); |
| 3376 | return NULL; |
| 3377 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3378 | Py_INCREF(resunicode); |
| 3379 | Py_DECREF(restuple); |
| 3380 | return resunicode; |
| 3381 | } |
| 3382 | |
| 3383 | /* Lookup the character ch in the mapping and put the result in result, |
| 3384 | which must be decrefed by the caller. |
| 3385 | Return 0 on success, -1 on error */ |
| 3386 | static |
| 3387 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 3388 | { |
| 3389 | PyObject *w = PyInt_FromLong((long)c); |
| 3390 | PyObject *x; |
| 3391 | |
| 3392 | if (w == NULL) |
| 3393 | return -1; |
| 3394 | x = PyObject_GetItem(mapping, w); |
| 3395 | Py_DECREF(w); |
| 3396 | if (x == NULL) { |
| 3397 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3398 | /* No mapping found means: use 1:1 mapping. */ |
| 3399 | PyErr_Clear(); |
| 3400 | *result = NULL; |
| 3401 | return 0; |
| 3402 | } else |
| 3403 | return -1; |
| 3404 | } |
| 3405 | else if (x == Py_None) { |
| 3406 | *result = x; |
| 3407 | return 0; |
| 3408 | } |
| 3409 | else if (PyInt_Check(x)) { |
| 3410 | long value = PyInt_AS_LONG(x); |
| 3411 | long max = PyUnicode_GetMax(); |
| 3412 | if (value < 0 || value > max) { |
| 3413 | PyErr_Format(PyExc_TypeError, |
| 3414 | "character mapping must be in range(0x%lx)", max+1); |
| 3415 | Py_DECREF(x); |
| 3416 | return -1; |
| 3417 | } |
| 3418 | *result = x; |
| 3419 | return 0; |
| 3420 | } |
| 3421 | else if (PyUnicode_Check(x)) { |
| 3422 | *result = x; |
| 3423 | return 0; |
| 3424 | } |
| 3425 | else { |
| 3426 | /* wrong return value */ |
| 3427 | PyErr_SetString(PyExc_TypeError, |
| 3428 | "character mapping must return integer, None or unicode"); |
Walter Dörwald | 150523e | 2003-08-15 16:52:19 +0000 | [diff] [blame] | 3429 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3430 | return -1; |
| 3431 | } |
| 3432 | } |
| 3433 | /* ensure that *outobj is at least requiredsize characters long, |
| 3434 | if not reallocate and adjust various state variables. |
| 3435 | Return 0 on success, -1 on error */ |
| 3436 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3437 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3438 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3439 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3440 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3441 | if (requiredsize > oldsize) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3442 | /* remember old output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3443 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3444 | /* exponentially overallocate to minimize reallocations */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3445 | if (requiredsize < 2 * oldsize) |
| 3446 | requiredsize = 2 * oldsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3447 | if (_PyUnicode_Resize(outobj, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3448 | return -1; |
| 3449 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3450 | } |
| 3451 | return 0; |
| 3452 | } |
| 3453 | /* lookup the character, put the result in the output string and adjust |
| 3454 | various state variables. Return a new reference to the object that |
| 3455 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 3456 | undefined (in which case no character was written). |
| 3457 | The called must decref result. |
| 3458 | Return 0 on success, -1 on error. */ |
| 3459 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3460 | int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3461 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3462 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3463 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3464 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3465 | return -1; |
| 3466 | if (*res==NULL) { |
| 3467 | /* not found => default to 1:1 mapping */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3468 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3469 | } |
| 3470 | else if (*res==Py_None) |
| 3471 | ; |
| 3472 | else if (PyInt_Check(*res)) { |
| 3473 | /* no overflow check, because we know that the space is enough */ |
| 3474 | *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); |
| 3475 | } |
| 3476 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3477 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3478 | if (repsize==1) { |
| 3479 | /* no overflow check, because we know that the space is enough */ |
| 3480 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 3481 | } |
| 3482 | else if (repsize!=0) { |
| 3483 | /* more than one character */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3484 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
Walter Dörwald | cd736e7 | 2004-02-05 17:36:00 +0000 | [diff] [blame] | 3485 | (insize - (curinp-startinp)) + |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3486 | repsize - 1; |
| 3487 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3488 | return -1; |
| 3489 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 3490 | *outp += repsize; |
| 3491 | } |
| 3492 | } |
| 3493 | else |
| 3494 | return -1; |
| 3495 | return 0; |
| 3496 | } |
| 3497 | |
| 3498 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3499 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3500 | PyObject *mapping, |
| 3501 | const char *errors) |
| 3502 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3503 | /* output object */ |
| 3504 | PyObject *res = NULL; |
| 3505 | /* pointers to the beginning and end+1 of input */ |
| 3506 | const Py_UNICODE *startp = p; |
| 3507 | const Py_UNICODE *endp = p + size; |
| 3508 | /* pointer into the output */ |
| 3509 | Py_UNICODE *str; |
| 3510 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3511 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3512 | char *reason = "character maps to <undefined>"; |
| 3513 | PyObject *errorHandler = NULL; |
| 3514 | PyObject *exc = NULL; |
| 3515 | /* the following variable is used for caching string comparisons |
| 3516 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 3517 | * 3=ignore, 4=xmlcharrefreplace */ |
| 3518 | int known_errorHandler = -1; |
| 3519 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3520 | if (mapping == NULL) { |
| 3521 | PyErr_BadArgument(); |
| 3522 | return NULL; |
| 3523 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3524 | |
| 3525 | /* allocate enough for a simple 1:1 translation without |
| 3526 | replacements, if we need more, we'll resize */ |
| 3527 | res = PyUnicode_FromUnicode(NULL, size); |
| 3528 | if (res == NULL) |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3529 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3530 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3531 | return res; |
| 3532 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3533 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3534 | while (p<endp) { |
| 3535 | /* try to encode it */ |
| 3536 | PyObject *x = NULL; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3537 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3538 | Py_XDECREF(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3539 | goto onError; |
| 3540 | } |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 3541 | Py_XDECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3542 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 3543 | ++p; |
| 3544 | else { /* untranslatable character */ |
| 3545 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3546 | Py_ssize_t repsize; |
| 3547 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3548 | Py_UNICODE *uni2; |
| 3549 | /* startpos for collecting untranslatable chars */ |
| 3550 | const Py_UNICODE *collstart = p; |
| 3551 | const Py_UNICODE *collend = p+1; |
| 3552 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3553 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3554 | /* find all untranslatable characters */ |
| 3555 | while (collend < endp) { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3556 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3557 | goto onError; |
| 3558 | Py_XDECREF(x); |
| 3559 | if (x!=Py_None) |
| 3560 | break; |
| 3561 | ++collend; |
| 3562 | } |
| 3563 | /* cache callback name lookup |
| 3564 | * (if not done yet, i.e. it's the first error) */ |
| 3565 | if (known_errorHandler==-1) { |
| 3566 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3567 | known_errorHandler = 1; |
| 3568 | else if (!strcmp(errors, "replace")) |
| 3569 | known_errorHandler = 2; |
| 3570 | else if (!strcmp(errors, "ignore")) |
| 3571 | known_errorHandler = 3; |
| 3572 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3573 | known_errorHandler = 4; |
| 3574 | else |
| 3575 | known_errorHandler = 0; |
| 3576 | } |
| 3577 | switch (known_errorHandler) { |
| 3578 | case 1: /* strict */ |
| 3579 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
| 3580 | goto onError; |
| 3581 | case 2: /* replace */ |
| 3582 | /* No need to check for space, this is a 1:1 replacement */ |
| 3583 | for (coll = collstart; coll<collend; ++coll) |
| 3584 | *str++ = '?'; |
| 3585 | /* fall through */ |
| 3586 | case 3: /* ignore */ |
| 3587 | p = collend; |
| 3588 | break; |
| 3589 | case 4: /* xmlcharrefreplace */ |
| 3590 | /* generate replacement (temporarily (mis)uses p) */ |
| 3591 | for (p = collstart; p < collend; ++p) { |
| 3592 | char buffer[2+29+1+1]; |
| 3593 | char *cp; |
| 3594 | sprintf(buffer, "&#%d;", (int)*p); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3595 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3596 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 3597 | goto onError; |
| 3598 | for (cp = buffer; *cp; ++cp) |
| 3599 | *str++ = *cp; |
| 3600 | } |
| 3601 | p = collend; |
| 3602 | break; |
| 3603 | default: |
| 3604 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 3605 | reason, startp, size, &exc, |
| 3606 | collstart-startp, collend-startp, &newpos); |
| 3607 | if (repunicode == NULL) |
| 3608 | goto onError; |
| 3609 | /* generate replacement */ |
| 3610 | repsize = PyUnicode_GET_SIZE(repunicode); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3611 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3612 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 3613 | Py_DECREF(repunicode); |
| 3614 | goto onError; |
| 3615 | } |
| 3616 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 3617 | *str++ = *uni2; |
| 3618 | p = startp + newpos; |
| 3619 | Py_DECREF(repunicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3620 | } |
| 3621 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3622 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3623 | /* Resize if we allocated to much */ |
| 3624 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3625 | if (respos<PyUnicode_GET_SIZE(res)) { |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3626 | if (_PyUnicode_Resize(&res, respos) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3627 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3628 | } |
| 3629 | Py_XDECREF(exc); |
| 3630 | Py_XDECREF(errorHandler); |
| 3631 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3632 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3633 | onError: |
| 3634 | Py_XDECREF(res); |
| 3635 | Py_XDECREF(exc); |
| 3636 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3637 | return NULL; |
| 3638 | } |
| 3639 | |
| 3640 | PyObject *PyUnicode_Translate(PyObject *str, |
| 3641 | PyObject *mapping, |
| 3642 | const char *errors) |
| 3643 | { |
| 3644 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3645 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3646 | str = PyUnicode_FromObject(str); |
| 3647 | if (str == NULL) |
| 3648 | goto onError; |
| 3649 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 3650 | PyUnicode_GET_SIZE(str), |
| 3651 | mapping, |
| 3652 | errors); |
| 3653 | Py_DECREF(str); |
| 3654 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3655 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3656 | onError: |
| 3657 | Py_XDECREF(str); |
| 3658 | return NULL; |
| 3659 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3660 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3661 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 3662 | |
| 3663 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3664 | Py_ssize_t length, |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3665 | char *output, |
| 3666 | const char *errors) |
| 3667 | { |
| 3668 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3669 | PyObject *errorHandler = NULL; |
| 3670 | PyObject *exc = NULL; |
| 3671 | const char *encoding = "decimal"; |
| 3672 | const char *reason = "invalid decimal Unicode string"; |
| 3673 | /* the following variable is used for caching string comparisons |
| 3674 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 3675 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3676 | |
| 3677 | if (output == NULL) { |
| 3678 | PyErr_BadArgument(); |
| 3679 | return -1; |
| 3680 | } |
| 3681 | |
| 3682 | p = s; |
| 3683 | end = s + length; |
| 3684 | while (p < end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3685 | register Py_UNICODE ch = *p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3686 | int decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3687 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3688 | Py_ssize_t repsize; |
| 3689 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3690 | Py_UNICODE *uni2; |
| 3691 | Py_UNICODE *collstart; |
| 3692 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3693 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3694 | if (Py_UNICODE_ISSPACE(ch)) { |
| 3695 | *output++ = ' '; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3696 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3697 | continue; |
| 3698 | } |
| 3699 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 3700 | if (decimal >= 0) { |
| 3701 | *output++ = '0' + decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3702 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3703 | continue; |
| 3704 | } |
Guido van Rossum | ba47704 | 2000-04-06 18:18:10 +0000 | [diff] [blame] | 3705 | if (0 < ch && ch < 256) { |
Guido van Rossum | 42c29aa | 2000-05-03 23:58:29 +0000 | [diff] [blame] | 3706 | *output++ = (char)ch; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3707 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3708 | continue; |
| 3709 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3710 | /* All other characters are considered unencodable */ |
| 3711 | collstart = p; |
| 3712 | collend = p+1; |
| 3713 | while (collend < end) { |
| 3714 | if ((0 < *collend && *collend < 256) || |
| 3715 | !Py_UNICODE_ISSPACE(*collend) || |
| 3716 | Py_UNICODE_TODECIMAL(*collend)) |
| 3717 | break; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3718 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3719 | /* cache callback name lookup |
| 3720 | * (if not done yet, i.e. it's the first error) */ |
| 3721 | if (known_errorHandler==-1) { |
| 3722 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3723 | known_errorHandler = 1; |
| 3724 | else if (!strcmp(errors, "replace")) |
| 3725 | known_errorHandler = 2; |
| 3726 | else if (!strcmp(errors, "ignore")) |
| 3727 | known_errorHandler = 3; |
| 3728 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3729 | known_errorHandler = 4; |
| 3730 | else |
| 3731 | known_errorHandler = 0; |
| 3732 | } |
| 3733 | switch (known_errorHandler) { |
| 3734 | case 1: /* strict */ |
| 3735 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 3736 | goto onError; |
| 3737 | case 2: /* replace */ |
| 3738 | for (p = collstart; p < collend; ++p) |
| 3739 | *output++ = '?'; |
| 3740 | /* fall through */ |
| 3741 | case 3: /* ignore */ |
| 3742 | p = collend; |
| 3743 | break; |
| 3744 | case 4: /* xmlcharrefreplace */ |
| 3745 | /* generate replacement (temporarily (mis)uses p) */ |
| 3746 | for (p = collstart; p < collend; ++p) |
| 3747 | output += sprintf(output, "&#%d;", (int)*p); |
| 3748 | p = collend; |
| 3749 | break; |
| 3750 | default: |
| 3751 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 3752 | encoding, reason, s, length, &exc, |
| 3753 | collstart-s, collend-s, &newpos); |
| 3754 | if (repunicode == NULL) |
| 3755 | goto onError; |
| 3756 | /* generate replacement */ |
| 3757 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3758 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 3759 | Py_UNICODE ch = *uni2; |
| 3760 | if (Py_UNICODE_ISSPACE(ch)) |
| 3761 | *output++ = ' '; |
| 3762 | else { |
| 3763 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 3764 | if (decimal >= 0) |
| 3765 | *output++ = '0' + decimal; |
| 3766 | else if (0 < ch && ch < 256) |
| 3767 | *output++ = (char)ch; |
| 3768 | else { |
| 3769 | Py_DECREF(repunicode); |
| 3770 | raise_encode_exception(&exc, encoding, |
| 3771 | s, length, collstart-s, collend-s, reason); |
| 3772 | goto onError; |
| 3773 | } |
| 3774 | } |
| 3775 | } |
| 3776 | p = s + newpos; |
| 3777 | Py_DECREF(repunicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3778 | } |
| 3779 | } |
| 3780 | /* 0-terminate the output string */ |
| 3781 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3782 | Py_XDECREF(exc); |
| 3783 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3784 | return 0; |
| 3785 | |
| 3786 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3787 | Py_XDECREF(exc); |
| 3788 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3789 | return -1; |
| 3790 | } |
| 3791 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3792 | /* --- Helpers ------------------------------------------------------------ */ |
| 3793 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3794 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3795 | Py_ssize_t count(PyUnicodeObject *self, |
| 3796 | Py_ssize_t start, |
| 3797 | Py_ssize_t end, |
| 3798 | PyUnicodeObject *substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3799 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3800 | Py_ssize_t count = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3801 | |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3802 | if (start < 0) |
| 3803 | start += self->length; |
| 3804 | if (start < 0) |
| 3805 | start = 0; |
| 3806 | if (end > self->length) |
| 3807 | end = self->length; |
| 3808 | if (end < 0) |
| 3809 | end += self->length; |
| 3810 | if (end < 0) |
| 3811 | end = 0; |
| 3812 | |
Marc-André Lemburg | 49ef6dc | 2000-06-18 22:25:22 +0000 | [diff] [blame] | 3813 | if (substring->length == 0) |
| 3814 | return (end - start + 1); |
| 3815 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3816 | end -= substring->length; |
| 3817 | |
| 3818 | while (start <= end) |
| 3819 | if (Py_UNICODE_MATCH(self, start, substring)) { |
| 3820 | count++; |
| 3821 | start += substring->length; |
| 3822 | } else |
| 3823 | start++; |
| 3824 | |
| 3825 | return count; |
| 3826 | } |
| 3827 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3828 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3829 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3830 | Py_ssize_t start, |
| 3831 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3832 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3833 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3834 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3835 | str = PyUnicode_FromObject(str); |
| 3836 | if (str == NULL) |
| 3837 | return -1; |
| 3838 | substr = PyUnicode_FromObject(substr); |
| 3839 | if (substr == NULL) { |
Marc-André Lemburg | 49ef6dc | 2000-06-18 22:25:22 +0000 | [diff] [blame] | 3840 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3841 | return -1; |
| 3842 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3843 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3844 | result = count((PyUnicodeObject *)str, |
| 3845 | start, end, |
| 3846 | (PyUnicodeObject *)substr); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3847 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3848 | Py_DECREF(str); |
| 3849 | Py_DECREF(substr); |
| 3850 | return result; |
| 3851 | } |
| 3852 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3853 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3854 | Py_ssize_t findstring(PyUnicodeObject *self, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3855 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3856 | Py_ssize_t start, |
| 3857 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3858 | int direction) |
| 3859 | { |
| 3860 | if (start < 0) |
| 3861 | start += self->length; |
| 3862 | if (start < 0) |
| 3863 | start = 0; |
| 3864 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3865 | if (end > self->length) |
| 3866 | end = self->length; |
| 3867 | if (end < 0) |
| 3868 | end += self->length; |
| 3869 | if (end < 0) |
| 3870 | end = 0; |
| 3871 | |
Guido van Rossum | 76afbd9 | 2002-08-20 17:29:29 +0000 | [diff] [blame] | 3872 | if (substring->length == 0) |
| 3873 | return (direction > 0) ? start : end; |
| 3874 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3875 | end -= substring->length; |
| 3876 | |
| 3877 | if (direction < 0) { |
| 3878 | for (; end >= start; end--) |
| 3879 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 3880 | return end; |
| 3881 | } else { |
| 3882 | for (; start <= end; start++) |
| 3883 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 3884 | return start; |
| 3885 | } |
| 3886 | |
| 3887 | return -1; |
| 3888 | } |
| 3889 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3890 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3891 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3892 | Py_ssize_t start, |
| 3893 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3894 | int direction) |
| 3895 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3896 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3897 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3898 | str = PyUnicode_FromObject(str); |
| 3899 | if (str == NULL) |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 3900 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3901 | substr = PyUnicode_FromObject(substr); |
| 3902 | if (substr == NULL) { |
Marc-André Lemburg | 4164439 | 2002-05-29 13:46:29 +0000 | [diff] [blame] | 3903 | Py_DECREF(str); |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 3904 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3905 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3906 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3907 | result = findstring((PyUnicodeObject *)str, |
| 3908 | (PyUnicodeObject *)substr, |
| 3909 | start, end, direction); |
| 3910 | Py_DECREF(str); |
| 3911 | Py_DECREF(substr); |
| 3912 | return result; |
| 3913 | } |
| 3914 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3915 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3916 | int tailmatch(PyUnicodeObject *self, |
| 3917 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3918 | Py_ssize_t start, |
| 3919 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3920 | int direction) |
| 3921 | { |
| 3922 | if (start < 0) |
| 3923 | start += self->length; |
| 3924 | if (start < 0) |
| 3925 | start = 0; |
| 3926 | |
| 3927 | if (substring->length == 0) |
| 3928 | return 1; |
| 3929 | |
| 3930 | if (end > self->length) |
| 3931 | end = self->length; |
| 3932 | if (end < 0) |
| 3933 | end += self->length; |
| 3934 | if (end < 0) |
| 3935 | end = 0; |
| 3936 | |
| 3937 | end -= substring->length; |
| 3938 | if (end < start) |
| 3939 | return 0; |
| 3940 | |
| 3941 | if (direction > 0) { |
| 3942 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 3943 | return 1; |
| 3944 | } else { |
| 3945 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 3946 | return 1; |
| 3947 | } |
| 3948 | |
| 3949 | return 0; |
| 3950 | } |
| 3951 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3952 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3953 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3954 | Py_ssize_t start, |
| 3955 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3956 | int direction) |
| 3957 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3958 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3959 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3960 | str = PyUnicode_FromObject(str); |
| 3961 | if (str == NULL) |
| 3962 | return -1; |
| 3963 | substr = PyUnicode_FromObject(substr); |
| 3964 | if (substr == NULL) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3965 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3966 | return -1; |
| 3967 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3968 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3969 | result = tailmatch((PyUnicodeObject *)str, |
| 3970 | (PyUnicodeObject *)substr, |
| 3971 | start, end, direction); |
| 3972 | Py_DECREF(str); |
| 3973 | Py_DECREF(substr); |
| 3974 | return result; |
| 3975 | } |
| 3976 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3977 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3978 | const Py_UNICODE *findchar(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3979 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3980 | Py_UNICODE ch) |
| 3981 | { |
| 3982 | /* like wcschr, but doesn't stop at NULL characters */ |
| 3983 | |
| 3984 | while (size-- > 0) { |
| 3985 | if (*s == ch) |
| 3986 | return s; |
| 3987 | s++; |
| 3988 | } |
| 3989 | |
| 3990 | return NULL; |
| 3991 | } |
| 3992 | |
| 3993 | /* Apply fixfct filter to the Unicode object self and return a |
| 3994 | reference to the modified object */ |
| 3995 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3996 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3997 | PyObject *fixup(PyUnicodeObject *self, |
| 3998 | int (*fixfct)(PyUnicodeObject *s)) |
| 3999 | { |
| 4000 | |
| 4001 | PyUnicodeObject *u; |
| 4002 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4003 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4004 | if (u == NULL) |
| 4005 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4006 | |
| 4007 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 4008 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4009 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4010 | /* fixfct should return TRUE if it modified the buffer. If |
| 4011 | FALSE, return a reference to the original buffer instead |
| 4012 | (to save space, not time) */ |
| 4013 | Py_INCREF(self); |
| 4014 | Py_DECREF(u); |
| 4015 | return (PyObject*) self; |
| 4016 | } |
| 4017 | return (PyObject*) u; |
| 4018 | } |
| 4019 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4020 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4021 | int fixupper(PyUnicodeObject *self) |
| 4022 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4023 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4024 | Py_UNICODE *s = self->str; |
| 4025 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4026 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4027 | while (len-- > 0) { |
| 4028 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4029 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4030 | ch = Py_UNICODE_TOUPPER(*s); |
| 4031 | if (ch != *s) { |
| 4032 | status = 1; |
| 4033 | *s = ch; |
| 4034 | } |
| 4035 | s++; |
| 4036 | } |
| 4037 | |
| 4038 | return status; |
| 4039 | } |
| 4040 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4041 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4042 | int fixlower(PyUnicodeObject *self) |
| 4043 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4044 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4045 | Py_UNICODE *s = self->str; |
| 4046 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4047 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4048 | while (len-- > 0) { |
| 4049 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4050 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4051 | ch = Py_UNICODE_TOLOWER(*s); |
| 4052 | if (ch != *s) { |
| 4053 | status = 1; |
| 4054 | *s = ch; |
| 4055 | } |
| 4056 | s++; |
| 4057 | } |
| 4058 | |
| 4059 | return status; |
| 4060 | } |
| 4061 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4062 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4063 | int fixswapcase(PyUnicodeObject *self) |
| 4064 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4065 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4066 | Py_UNICODE *s = self->str; |
| 4067 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4068 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4069 | while (len-- > 0) { |
| 4070 | if (Py_UNICODE_ISUPPER(*s)) { |
| 4071 | *s = Py_UNICODE_TOLOWER(*s); |
| 4072 | status = 1; |
| 4073 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 4074 | *s = Py_UNICODE_TOUPPER(*s); |
| 4075 | status = 1; |
| 4076 | } |
| 4077 | s++; |
| 4078 | } |
| 4079 | |
| 4080 | return status; |
| 4081 | } |
| 4082 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4083 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4084 | int fixcapitalize(PyUnicodeObject *self) |
| 4085 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4086 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4087 | Py_UNICODE *s = self->str; |
| 4088 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4089 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4090 | if (len == 0) |
| 4091 | return 0; |
| 4092 | if (Py_UNICODE_ISLOWER(*s)) { |
| 4093 | *s = Py_UNICODE_TOUPPER(*s); |
| 4094 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4095 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4096 | s++; |
| 4097 | while (--len > 0) { |
| 4098 | if (Py_UNICODE_ISUPPER(*s)) { |
| 4099 | *s = Py_UNICODE_TOLOWER(*s); |
| 4100 | status = 1; |
| 4101 | } |
| 4102 | s++; |
| 4103 | } |
| 4104 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4105 | } |
| 4106 | |
| 4107 | static |
| 4108 | int fixtitle(PyUnicodeObject *self) |
| 4109 | { |
| 4110 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 4111 | register Py_UNICODE *e; |
| 4112 | int previous_is_cased; |
| 4113 | |
| 4114 | /* Shortcut for single character strings */ |
| 4115 | if (PyUnicode_GET_SIZE(self) == 1) { |
| 4116 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 4117 | if (*p != ch) { |
| 4118 | *p = ch; |
| 4119 | return 1; |
| 4120 | } |
| 4121 | else |
| 4122 | return 0; |
| 4123 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4124 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4125 | e = p + PyUnicode_GET_SIZE(self); |
| 4126 | previous_is_cased = 0; |
| 4127 | for (; p < e; p++) { |
| 4128 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4129 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4130 | if (previous_is_cased) |
| 4131 | *p = Py_UNICODE_TOLOWER(ch); |
| 4132 | else |
| 4133 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4134 | |
| 4135 | if (Py_UNICODE_ISLOWER(ch) || |
| 4136 | Py_UNICODE_ISUPPER(ch) || |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4137 | Py_UNICODE_ISTITLE(ch)) |
| 4138 | previous_is_cased = 1; |
| 4139 | else |
| 4140 | previous_is_cased = 0; |
| 4141 | } |
| 4142 | return 1; |
| 4143 | } |
| 4144 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4145 | PyObject * |
| 4146 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4147 | { |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4148 | PyObject *internal_separator = NULL; |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 4149 | const Py_UNICODE blank = ' '; |
| 4150 | const Py_UNICODE *sep = ␣ |
| 4151 | size_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4152 | PyUnicodeObject *res = NULL; /* the result */ |
| 4153 | size_t res_alloc = 100; /* # allocated bytes for string in res */ |
| 4154 | size_t res_used; /* # used bytes */ |
| 4155 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 4156 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4157 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4158 | PyObject *item; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4159 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4160 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4161 | fseq = PySequence_Fast(seq, ""); |
| 4162 | if (fseq == NULL) { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4163 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4164 | } |
| 4165 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4166 | /* Grrrr. A codec may be invoked to convert str objects to |
| 4167 | * Unicode, and so it's possible to call back into Python code |
| 4168 | * during PyUnicode_FromObject(), and so it's possible for a sick |
| 4169 | * codec to change the size of fseq (if seq is a list). Therefore |
| 4170 | * we have to keep refetching the size -- can't assume seqlen |
| 4171 | * is invariant. |
| 4172 | */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4173 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 4174 | /* If empty sequence, return u"". */ |
| 4175 | if (seqlen == 0) { |
| 4176 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 4177 | goto Done; |
| 4178 | } |
| 4179 | /* If singleton sequence with an exact Unicode, return that. */ |
| 4180 | if (seqlen == 1) { |
| 4181 | item = PySequence_Fast_GET_ITEM(fseq, 0); |
| 4182 | if (PyUnicode_CheckExact(item)) { |
| 4183 | Py_INCREF(item); |
| 4184 | res = (PyUnicodeObject *)item; |
| 4185 | goto Done; |
| 4186 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4187 | } |
| 4188 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4189 | /* At least two items to join, or one that isn't exact Unicode. */ |
| 4190 | if (seqlen > 1) { |
| 4191 | /* Set up sep and seplen -- they're needed. */ |
| 4192 | if (separator == NULL) { |
| 4193 | sep = ␣ |
| 4194 | seplen = 1; |
| 4195 | } |
| 4196 | else { |
| 4197 | internal_separator = PyUnicode_FromObject(separator); |
| 4198 | if (internal_separator == NULL) |
| 4199 | goto onError; |
| 4200 | sep = PyUnicode_AS_UNICODE(internal_separator); |
| 4201 | seplen = PyUnicode_GET_SIZE(internal_separator); |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4202 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 4203 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4204 | } |
| 4205 | } |
| 4206 | |
| 4207 | /* Get space. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4208 | res = _PyUnicode_New(res_alloc); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4209 | if (res == NULL) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4210 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4211 | res_p = PyUnicode_AS_UNICODE(res); |
| 4212 | res_used = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4213 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4214 | for (i = 0; i < seqlen; ++i) { |
| 4215 | size_t itemlen; |
| 4216 | size_t new_res_used; |
| 4217 | |
| 4218 | item = PySequence_Fast_GET_ITEM(fseq, i); |
| 4219 | /* Convert item to Unicode. */ |
| 4220 | if (! PyUnicode_Check(item) && ! PyString_Check(item)) { |
| 4221 | PyErr_Format(PyExc_TypeError, |
| 4222 | "sequence item %i: expected string or Unicode," |
| 4223 | " %.80s found", |
| 4224 | i, item->ob_type->tp_name); |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 4225 | goto onError; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4226 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4227 | item = PyUnicode_FromObject(item); |
| 4228 | if (item == NULL) |
| 4229 | goto onError; |
| 4230 | /* We own a reference to item from here on. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4231 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4232 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 4233 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 4234 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4235 | /* 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] | 4236 | itemlen = PyUnicode_GET_SIZE(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4237 | new_res_used = res_used + itemlen; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4238 | if (new_res_used < res_used || new_res_used > PY_SSIZE_T_MAX) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4239 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4240 | if (i < seqlen - 1) { |
| 4241 | new_res_used += seplen; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4242 | if (new_res_used < res_used || new_res_used > PY_SSIZE_T_MAX) |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4243 | goto Overflow; |
| 4244 | } |
| 4245 | if (new_res_used > res_alloc) { |
| 4246 | /* double allocated size until it's big enough */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4247 | do { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4248 | size_t oldsize = res_alloc; |
| 4249 | res_alloc += res_alloc; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4250 | if (res_alloc < oldsize || res_alloc > PY_SSIZE_T_MAX) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4251 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4252 | } while (new_res_used > res_alloc); |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4253 | if (_PyUnicode_Resize(&res, res_alloc) < 0) { |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4254 | Py_DECREF(item); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4255 | goto onError; |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4256 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4257 | res_p = PyUnicode_AS_UNICODE(res) + res_used; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4258 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4259 | |
| 4260 | /* Copy item, and maybe the separator. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4261 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4262 | res_p += itemlen; |
| 4263 | if (i < seqlen - 1) { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4264 | Py_UNICODE_COPY(res_p, sep, seplen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4265 | res_p += seplen; |
| 4266 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4267 | Py_DECREF(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4268 | res_used = new_res_used; |
| 4269 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4270 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4271 | /* Shrink res to match the used area; this probably can't fail, |
| 4272 | * but it's cheap to check. |
| 4273 | */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4274 | if (_PyUnicode_Resize(&res, res_used) < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4275 | goto onError; |
| 4276 | |
| 4277 | Done: |
| 4278 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4279 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4280 | return (PyObject *)res; |
| 4281 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4282 | Overflow: |
| 4283 | PyErr_SetString(PyExc_OverflowError, |
| 4284 | "join() is too long for a Python string"); |
| 4285 | Py_DECREF(item); |
| 4286 | /* fall through */ |
| 4287 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4288 | onError: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4289 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4290 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4291 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4292 | return NULL; |
| 4293 | } |
| 4294 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4295 | static |
| 4296 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4297 | Py_ssize_t left, |
| 4298 | Py_ssize_t right, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4299 | Py_UNICODE fill) |
| 4300 | { |
| 4301 | PyUnicodeObject *u; |
| 4302 | |
| 4303 | if (left < 0) |
| 4304 | left = 0; |
| 4305 | if (right < 0) |
| 4306 | right = 0; |
| 4307 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4308 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4309 | Py_INCREF(self); |
| 4310 | return self; |
| 4311 | } |
| 4312 | |
| 4313 | u = _PyUnicode_New(left + self->length + right); |
| 4314 | if (u) { |
| 4315 | if (left) |
| 4316 | Py_UNICODE_FILL(u->str, fill, left); |
| 4317 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 4318 | if (right) |
| 4319 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 4320 | } |
| 4321 | |
| 4322 | return u; |
| 4323 | } |
| 4324 | |
| 4325 | #define SPLIT_APPEND(data, left, right) \ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4326 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4327 | if (!str) \ |
| 4328 | goto onError; \ |
| 4329 | if (PyList_Append(list, str)) { \ |
| 4330 | Py_DECREF(str); \ |
| 4331 | goto onError; \ |
| 4332 | } \ |
| 4333 | else \ |
| 4334 | Py_DECREF(str); |
| 4335 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4336 | #define SPLIT_INSERT(data, left, right) \ |
| 4337 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
| 4338 | if (!str) \ |
| 4339 | goto onError; \ |
| 4340 | if (PyList_Insert(list, 0, str)) { \ |
| 4341 | Py_DECREF(str); \ |
| 4342 | goto onError; \ |
| 4343 | } \ |
| 4344 | else \ |
| 4345 | Py_DECREF(str); |
| 4346 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4347 | static |
| 4348 | PyObject *split_whitespace(PyUnicodeObject *self, |
| 4349 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4350 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4351 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4352 | register Py_ssize_t i; |
| 4353 | register Py_ssize_t j; |
| 4354 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4355 | PyObject *str; |
| 4356 | |
| 4357 | for (i = j = 0; i < len; ) { |
| 4358 | /* find a token */ |
| 4359 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 4360 | i++; |
| 4361 | j = i; |
| 4362 | while (i < len && !Py_UNICODE_ISSPACE(self->str[i])) |
| 4363 | i++; |
| 4364 | if (j < i) { |
| 4365 | if (maxcount-- <= 0) |
| 4366 | break; |
| 4367 | SPLIT_APPEND(self->str, j, i); |
| 4368 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 4369 | i++; |
| 4370 | j = i; |
| 4371 | } |
| 4372 | } |
| 4373 | if (j < len) { |
| 4374 | SPLIT_APPEND(self->str, j, len); |
| 4375 | } |
| 4376 | return list; |
| 4377 | |
| 4378 | onError: |
| 4379 | Py_DECREF(list); |
| 4380 | return NULL; |
| 4381 | } |
| 4382 | |
| 4383 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4384 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4385 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4386 | register Py_ssize_t i; |
| 4387 | register Py_ssize_t j; |
| 4388 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4389 | PyObject *list; |
| 4390 | PyObject *str; |
| 4391 | Py_UNICODE *data; |
| 4392 | |
| 4393 | string = PyUnicode_FromObject(string); |
| 4394 | if (string == NULL) |
| 4395 | return NULL; |
| 4396 | data = PyUnicode_AS_UNICODE(string); |
| 4397 | len = PyUnicode_GET_SIZE(string); |
| 4398 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4399 | list = PyList_New(0); |
| 4400 | if (!list) |
| 4401 | goto onError; |
| 4402 | |
| 4403 | for (i = j = 0; i < len; ) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4404 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4405 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4406 | /* Find a line and append it */ |
| 4407 | while (i < len && !Py_UNICODE_ISLINEBREAK(data[i])) |
| 4408 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4409 | |
| 4410 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4411 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4412 | if (i < len) { |
| 4413 | if (data[i] == '\r' && i + 1 < len && |
| 4414 | data[i+1] == '\n') |
| 4415 | i += 2; |
| 4416 | else |
| 4417 | i++; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4418 | if (keepends) |
| 4419 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4420 | } |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4421 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4422 | j = i; |
| 4423 | } |
| 4424 | if (j < len) { |
| 4425 | SPLIT_APPEND(data, j, len); |
| 4426 | } |
| 4427 | |
| 4428 | Py_DECREF(string); |
| 4429 | return list; |
| 4430 | |
| 4431 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 4432 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4433 | Py_DECREF(string); |
| 4434 | return NULL; |
| 4435 | } |
| 4436 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4437 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4438 | PyObject *split_char(PyUnicodeObject *self, |
| 4439 | PyObject *list, |
| 4440 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4441 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4442 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4443 | register Py_ssize_t i; |
| 4444 | register Py_ssize_t j; |
| 4445 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4446 | PyObject *str; |
| 4447 | |
| 4448 | for (i = j = 0; i < len; ) { |
| 4449 | if (self->str[i] == ch) { |
| 4450 | if (maxcount-- <= 0) |
| 4451 | break; |
| 4452 | SPLIT_APPEND(self->str, j, i); |
| 4453 | i = j = i + 1; |
| 4454 | } else |
| 4455 | i++; |
| 4456 | } |
| 4457 | if (j <= len) { |
| 4458 | SPLIT_APPEND(self->str, j, len); |
| 4459 | } |
| 4460 | return list; |
| 4461 | |
| 4462 | onError: |
| 4463 | Py_DECREF(list); |
| 4464 | return NULL; |
| 4465 | } |
| 4466 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4467 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4468 | PyObject *split_substring(PyUnicodeObject *self, |
| 4469 | PyObject *list, |
| 4470 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4471 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4472 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4473 | register Py_ssize_t i; |
| 4474 | register Py_ssize_t j; |
| 4475 | Py_ssize_t len = self->length; |
| 4476 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4477 | PyObject *str; |
| 4478 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 4479 | for (i = j = 0; i <= len - sublen; ) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4480 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 4481 | if (maxcount-- <= 0) |
| 4482 | break; |
| 4483 | SPLIT_APPEND(self->str, j, i); |
| 4484 | i = j = i + sublen; |
| 4485 | } else |
| 4486 | i++; |
| 4487 | } |
| 4488 | if (j <= len) { |
| 4489 | SPLIT_APPEND(self->str, j, len); |
| 4490 | } |
| 4491 | return list; |
| 4492 | |
| 4493 | onError: |
| 4494 | Py_DECREF(list); |
| 4495 | return NULL; |
| 4496 | } |
| 4497 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4498 | static |
| 4499 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
| 4500 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4501 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4502 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4503 | register Py_ssize_t i; |
| 4504 | register Py_ssize_t j; |
| 4505 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4506 | PyObject *str; |
| 4507 | |
| 4508 | for (i = j = len - 1; i >= 0; ) { |
| 4509 | /* find a token */ |
| 4510 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 4511 | i--; |
| 4512 | j = i; |
| 4513 | while (i >= 0 && !Py_UNICODE_ISSPACE(self->str[i])) |
| 4514 | i--; |
| 4515 | if (j > i) { |
| 4516 | if (maxcount-- <= 0) |
| 4517 | break; |
| 4518 | SPLIT_INSERT(self->str, i + 1, j + 1); |
| 4519 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 4520 | i--; |
| 4521 | j = i; |
| 4522 | } |
| 4523 | } |
| 4524 | if (j >= 0) { |
| 4525 | SPLIT_INSERT(self->str, 0, j + 1); |
| 4526 | } |
| 4527 | return list; |
| 4528 | |
| 4529 | onError: |
| 4530 | Py_DECREF(list); |
| 4531 | return NULL; |
| 4532 | } |
| 4533 | |
| 4534 | static |
| 4535 | PyObject *rsplit_char(PyUnicodeObject *self, |
| 4536 | PyObject *list, |
| 4537 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4538 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4539 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4540 | register Py_ssize_t i; |
| 4541 | register Py_ssize_t j; |
| 4542 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4543 | PyObject *str; |
| 4544 | |
| 4545 | for (i = j = len - 1; i >= 0; ) { |
| 4546 | if (self->str[i] == ch) { |
| 4547 | if (maxcount-- <= 0) |
| 4548 | break; |
| 4549 | SPLIT_INSERT(self->str, i + 1, j + 1); |
| 4550 | j = i = i - 1; |
| 4551 | } else |
| 4552 | i--; |
| 4553 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 4554 | if (j >= -1) { |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4555 | SPLIT_INSERT(self->str, 0, j + 1); |
| 4556 | } |
| 4557 | return list; |
| 4558 | |
| 4559 | onError: |
| 4560 | Py_DECREF(list); |
| 4561 | return NULL; |
| 4562 | } |
| 4563 | |
| 4564 | static |
| 4565 | PyObject *rsplit_substring(PyUnicodeObject *self, |
| 4566 | PyObject *list, |
| 4567 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4568 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4569 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4570 | register Py_ssize_t i; |
| 4571 | register Py_ssize_t j; |
| 4572 | Py_ssize_t len = self->length; |
| 4573 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4574 | PyObject *str; |
| 4575 | |
| 4576 | for (i = len - sublen, j = len; i >= 0; ) { |
| 4577 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 4578 | if (maxcount-- <= 0) |
| 4579 | break; |
| 4580 | SPLIT_INSERT(self->str, i + sublen, j); |
| 4581 | j = i; |
| 4582 | i -= sublen; |
| 4583 | } else |
| 4584 | i--; |
| 4585 | } |
| 4586 | if (j >= 0) { |
| 4587 | SPLIT_INSERT(self->str, 0, j); |
| 4588 | } |
| 4589 | return list; |
| 4590 | |
| 4591 | onError: |
| 4592 | Py_DECREF(list); |
| 4593 | return NULL; |
| 4594 | } |
| 4595 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4596 | #undef SPLIT_APPEND |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4597 | #undef SPLIT_INSERT |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4598 | |
| 4599 | static |
| 4600 | PyObject *split(PyUnicodeObject *self, |
| 4601 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4602 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4603 | { |
| 4604 | PyObject *list; |
| 4605 | |
| 4606 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4607 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4608 | |
| 4609 | list = PyList_New(0); |
| 4610 | if (!list) |
| 4611 | return NULL; |
| 4612 | |
| 4613 | if (substring == NULL) |
| 4614 | return split_whitespace(self,list,maxcount); |
| 4615 | |
| 4616 | else if (substring->length == 1) |
| 4617 | return split_char(self,list,substring->str[0],maxcount); |
| 4618 | |
| 4619 | else if (substring->length == 0) { |
| 4620 | Py_DECREF(list); |
| 4621 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 4622 | return NULL; |
| 4623 | } |
| 4624 | else |
| 4625 | return split_substring(self,list,substring,maxcount); |
| 4626 | } |
| 4627 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4628 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4629 | PyObject *rsplit(PyUnicodeObject *self, |
| 4630 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4631 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4632 | { |
| 4633 | PyObject *list; |
| 4634 | |
| 4635 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4636 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4637 | |
| 4638 | list = PyList_New(0); |
| 4639 | if (!list) |
| 4640 | return NULL; |
| 4641 | |
| 4642 | if (substring == NULL) |
| 4643 | return rsplit_whitespace(self,list,maxcount); |
| 4644 | |
| 4645 | else if (substring->length == 1) |
| 4646 | return rsplit_char(self,list,substring->str[0],maxcount); |
| 4647 | |
| 4648 | else if (substring->length == 0) { |
| 4649 | Py_DECREF(list); |
| 4650 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 4651 | return NULL; |
| 4652 | } |
| 4653 | else |
| 4654 | return rsplit_substring(self,list,substring,maxcount); |
| 4655 | } |
| 4656 | |
| 4657 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4658 | PyObject *replace(PyUnicodeObject *self, |
| 4659 | PyUnicodeObject *str1, |
| 4660 | PyUnicodeObject *str2, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4661 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4662 | { |
| 4663 | PyUnicodeObject *u; |
| 4664 | |
| 4665 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4666 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4667 | |
| 4668 | if (str1->length == 1 && str2->length == 1) { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4669 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4670 | |
| 4671 | /* replace characters */ |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4672 | if (!findchar(self->str, self->length, str1->str[0]) && |
| 4673 | PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4674 | /* nothing to replace, return original string */ |
| 4675 | Py_INCREF(self); |
| 4676 | u = self; |
| 4677 | } else { |
| 4678 | Py_UNICODE u1 = str1->str[0]; |
| 4679 | Py_UNICODE u2 = str2->str[0]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4680 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4681 | u = (PyUnicodeObject*) PyUnicode_FromUnicode( |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4682 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4683 | self->length |
| 4684 | ); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4685 | if (u != NULL) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4686 | Py_UNICODE_COPY(u->str, self->str, |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4687 | self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4688 | for (i = 0; i < u->length; i++) |
| 4689 | if (u->str[i] == u1) { |
| 4690 | if (--maxcount < 0) |
| 4691 | break; |
| 4692 | u->str[i] = u2; |
| 4693 | } |
| 4694 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4695 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4696 | |
| 4697 | } else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4698 | Py_ssize_t n, i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4699 | Py_UNICODE *p; |
| 4700 | |
| 4701 | /* replace strings */ |
| 4702 | n = count(self, 0, self->length, str1); |
| 4703 | if (n > maxcount) |
| 4704 | n = maxcount; |
Guido van Rossum | 2023c9b | 2002-08-23 18:50:21 +0000 | [diff] [blame] | 4705 | if (n == 0) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4706 | /* nothing to replace, return original string */ |
Guido van Rossum | 2023c9b | 2002-08-23 18:50:21 +0000 | [diff] [blame] | 4707 | if (PyUnicode_CheckExact(self)) { |
| 4708 | Py_INCREF(self); |
| 4709 | u = self; |
| 4710 | } |
| 4711 | else { |
| 4712 | u = (PyUnicodeObject *) |
| 4713 | PyUnicode_FromUnicode(self->str, self->length); |
| 4714 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4715 | } else { |
| 4716 | u = _PyUnicode_New( |
| 4717 | self->length + n * (str2->length - str1->length)); |
| 4718 | if (u) { |
| 4719 | i = 0; |
| 4720 | p = u->str; |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 4721 | if (str1->length > 0) { |
| 4722 | while (i <= self->length - str1->length) |
| 4723 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 4724 | /* replace string segment */ |
| 4725 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 4726 | p += str2->length; |
| 4727 | i += str1->length; |
| 4728 | if (--n <= 0) { |
| 4729 | /* copy remaining part */ |
| 4730 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 4731 | break; |
| 4732 | } |
| 4733 | } else |
| 4734 | *p++ = self->str[i++]; |
| 4735 | } else { |
| 4736 | while (n > 0) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4737 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 4738 | p += str2->length; |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 4739 | if (--n <= 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4740 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4741 | *p++ = self->str[i++]; |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 4742 | } |
| 4743 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 4744 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4745 | } |
| 4746 | } |
| 4747 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4748 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4749 | return (PyObject *) u; |
| 4750 | } |
| 4751 | |
| 4752 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 4753 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4754 | PyDoc_STRVAR(title__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4755 | "S.title() -> unicode\n\ |
| 4756 | \n\ |
| 4757 | 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] | 4758 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4759 | |
| 4760 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 4761 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4762 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4763 | return fixup(self, fixtitle); |
| 4764 | } |
| 4765 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4766 | PyDoc_STRVAR(capitalize__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4767 | "S.capitalize() -> unicode\n\ |
| 4768 | \n\ |
| 4769 | 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] | 4770 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4771 | |
| 4772 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 4773 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4774 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4775 | return fixup(self, fixcapitalize); |
| 4776 | } |
| 4777 | |
| 4778 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4779 | PyDoc_STRVAR(capwords__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4780 | "S.capwords() -> unicode\n\ |
| 4781 | \n\ |
| 4782 | 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] | 4783 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4784 | |
| 4785 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 4786 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4787 | { |
| 4788 | PyObject *list; |
| 4789 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4790 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4791 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4792 | /* Split into words */ |
| 4793 | list = split(self, NULL, -1); |
| 4794 | if (!list) |
| 4795 | return NULL; |
| 4796 | |
| 4797 | /* Capitalize each word */ |
| 4798 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 4799 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 4800 | fixcapitalize); |
| 4801 | if (item == NULL) |
| 4802 | goto onError; |
| 4803 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 4804 | PyList_SET_ITEM(list, i, item); |
| 4805 | } |
| 4806 | |
| 4807 | /* Join the words to form a new string */ |
| 4808 | item = PyUnicode_Join(NULL, list); |
| 4809 | |
| 4810 | onError: |
| 4811 | Py_DECREF(list); |
| 4812 | return (PyObject *)item; |
| 4813 | } |
| 4814 | #endif |
| 4815 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4816 | /* Argument converter. Coerces to a single unicode character */ |
| 4817 | |
| 4818 | static int |
| 4819 | convert_uc(PyObject *obj, void *addr) |
| 4820 | { |
| 4821 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 4822 | PyObject *uniobj; |
| 4823 | Py_UNICODE *unistr; |
| 4824 | |
| 4825 | uniobj = PyUnicode_FromObject(obj); |
| 4826 | if (uniobj == NULL) { |
| 4827 | PyErr_SetString(PyExc_TypeError, |
| 4828 | "The fill character cannot be converted to Unicode"); |
| 4829 | return 0; |
| 4830 | } |
| 4831 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 4832 | PyErr_SetString(PyExc_TypeError, |
| 4833 | "The fill character must be exactly one character long"); |
| 4834 | Py_DECREF(uniobj); |
| 4835 | return 0; |
| 4836 | } |
| 4837 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 4838 | *fillcharloc = unistr[0]; |
| 4839 | Py_DECREF(uniobj); |
| 4840 | return 1; |
| 4841 | } |
| 4842 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4843 | PyDoc_STRVAR(center__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4844 | "S.center(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4845 | \n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4846 | Return S centered in a Unicode string of length width. Padding is\n\ |
| 4847 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4848 | |
| 4849 | static PyObject * |
| 4850 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 4851 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4852 | Py_ssize_t marg, left; |
| 4853 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4854 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4855 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 4856 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4857 | return NULL; |
| 4858 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4859 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4860 | Py_INCREF(self); |
| 4861 | return (PyObject*) self; |
| 4862 | } |
| 4863 | |
| 4864 | marg = width - self->length; |
| 4865 | left = marg / 2 + (marg & width & 1); |
| 4866 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4867 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4868 | } |
| 4869 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4870 | #if 0 |
| 4871 | |
| 4872 | /* This code should go into some future Unicode collation support |
| 4873 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 4874 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4875 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4876 | /* speedy UTF-16 code point order comparison */ |
| 4877 | /* gleaned from: */ |
| 4878 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 4879 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4880 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4881 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4882 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4883 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 4884 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4885 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4886 | }; |
| 4887 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4888 | static int |
| 4889 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 4890 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4891 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4892 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4893 | Py_UNICODE *s1 = str1->str; |
| 4894 | Py_UNICODE *s2 = str2->str; |
| 4895 | |
| 4896 | len1 = str1->length; |
| 4897 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4898 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4899 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4900 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4901 | |
| 4902 | c1 = *s1++; |
| 4903 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 4904 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4905 | if (c1 > (1<<11) * 26) |
| 4906 | c1 += utf16Fixup[c1>>11]; |
| 4907 | if (c2 > (1<<11) * 26) |
| 4908 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4909 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 4910 | |
| 4911 | if (c1 != c2) |
| 4912 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4913 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4914 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4915 | } |
| 4916 | |
| 4917 | return (len1 < len2) ? -1 : (len1 != len2); |
| 4918 | } |
| 4919 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4920 | #else |
| 4921 | |
| 4922 | static int |
| 4923 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 4924 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4925 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4926 | |
| 4927 | Py_UNICODE *s1 = str1->str; |
| 4928 | Py_UNICODE *s2 = str2->str; |
| 4929 | |
| 4930 | len1 = str1->length; |
| 4931 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4932 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4933 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4934 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4935 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 4936 | c1 = *s1++; |
| 4937 | c2 = *s2++; |
| 4938 | |
| 4939 | if (c1 != c2) |
| 4940 | return (c1 < c2) ? -1 : 1; |
| 4941 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4942 | len1--; len2--; |
| 4943 | } |
| 4944 | |
| 4945 | return (len1 < len2) ? -1 : (len1 != len2); |
| 4946 | } |
| 4947 | |
| 4948 | #endif |
| 4949 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4950 | int PyUnicode_Compare(PyObject *left, |
| 4951 | PyObject *right) |
| 4952 | { |
| 4953 | PyUnicodeObject *u = NULL, *v = NULL; |
| 4954 | int result; |
| 4955 | |
| 4956 | /* Coerce the two arguments */ |
| 4957 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 4958 | if (u == NULL) |
| 4959 | goto onError; |
| 4960 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 4961 | if (v == NULL) |
| 4962 | goto onError; |
| 4963 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 4964 | /* Shortcut for empty or interned objects */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4965 | if (v == u) { |
| 4966 | Py_DECREF(u); |
| 4967 | Py_DECREF(v); |
| 4968 | return 0; |
| 4969 | } |
| 4970 | |
| 4971 | result = unicode_compare(u, v); |
| 4972 | |
| 4973 | Py_DECREF(u); |
| 4974 | Py_DECREF(v); |
| 4975 | return result; |
| 4976 | |
| 4977 | onError: |
| 4978 | Py_XDECREF(u); |
| 4979 | Py_XDECREF(v); |
| 4980 | return -1; |
| 4981 | } |
| 4982 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4983 | int PyUnicode_Contains(PyObject *container, |
| 4984 | PyObject *element) |
| 4985 | { |
| 4986 | PyUnicodeObject *u = NULL, *v = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4987 | int result; |
| 4988 | Py_ssize_t size; |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 4989 | register const Py_UNICODE *lhs, *end, *rhs; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4990 | |
| 4991 | /* Coerce the two arguments */ |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4992 | v = (PyUnicodeObject *)PyUnicode_FromObject(element); |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 4993 | if (v == NULL) { |
| 4994 | PyErr_SetString(PyExc_TypeError, |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 4995 | "'in <string>' requires string as left operand"); |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 4996 | goto onError; |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 4997 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4998 | u = (PyUnicodeObject *)PyUnicode_FromObject(container); |
Marc-André Lemburg | 9cd87aa | 2002-10-23 09:02:46 +0000 | [diff] [blame] | 4999 | if (u == NULL) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5000 | goto onError; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5001 | |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 5002 | size = PyUnicode_GET_SIZE(v); |
| 5003 | rhs = PyUnicode_AS_UNICODE(v); |
| 5004 | lhs = PyUnicode_AS_UNICODE(u); |
| 5005 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5006 | result = 0; |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 5007 | if (size == 1) { |
| 5008 | end = lhs + PyUnicode_GET_SIZE(u); |
| 5009 | while (lhs < end) { |
| 5010 | if (*lhs++ == *rhs) { |
| 5011 | result = 1; |
| 5012 | break; |
| 5013 | } |
| 5014 | } |
| 5015 | } |
| 5016 | else { |
| 5017 | end = lhs + (PyUnicode_GET_SIZE(u) - size); |
| 5018 | while (lhs <= end) { |
Barry Warsaw | 6a043f3 | 2002-08-06 19:03:17 +0000 | [diff] [blame] | 5019 | if (memcmp(lhs++, rhs, size * sizeof(Py_UNICODE)) == 0) { |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 5020 | result = 1; |
| 5021 | break; |
| 5022 | } |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5023 | } |
| 5024 | } |
| 5025 | |
| 5026 | Py_DECREF(u); |
| 5027 | Py_DECREF(v); |
| 5028 | return result; |
| 5029 | |
| 5030 | onError: |
| 5031 | Py_XDECREF(u); |
| 5032 | Py_XDECREF(v); |
| 5033 | return -1; |
| 5034 | } |
| 5035 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5036 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 5037 | |
| 5038 | PyObject *PyUnicode_Concat(PyObject *left, |
| 5039 | PyObject *right) |
| 5040 | { |
| 5041 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 5042 | |
| 5043 | /* Coerce the two arguments */ |
| 5044 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 5045 | if (u == NULL) |
| 5046 | goto onError; |
| 5047 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 5048 | if (v == NULL) |
| 5049 | goto onError; |
| 5050 | |
| 5051 | /* Shortcuts */ |
| 5052 | if (v == unicode_empty) { |
| 5053 | Py_DECREF(v); |
| 5054 | return (PyObject *)u; |
| 5055 | } |
| 5056 | if (u == unicode_empty) { |
| 5057 | Py_DECREF(u); |
| 5058 | return (PyObject *)v; |
| 5059 | } |
| 5060 | |
| 5061 | /* Concat the two Unicode strings */ |
| 5062 | w = _PyUnicode_New(u->length + v->length); |
| 5063 | if (w == NULL) |
| 5064 | goto onError; |
| 5065 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 5066 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 5067 | |
| 5068 | Py_DECREF(u); |
| 5069 | Py_DECREF(v); |
| 5070 | return (PyObject *)w; |
| 5071 | |
| 5072 | onError: |
| 5073 | Py_XDECREF(u); |
| 5074 | Py_XDECREF(v); |
| 5075 | return NULL; |
| 5076 | } |
| 5077 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5078 | PyDoc_STRVAR(count__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5079 | "S.count(sub[, start[, end]]) -> int\n\ |
| 5080 | \n\ |
| 5081 | Return the number of occurrences of substring sub in Unicode string\n\ |
| 5082 | S[start:end]. Optional arguments start and end are\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5083 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5084 | |
| 5085 | static PyObject * |
| 5086 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 5087 | { |
| 5088 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5089 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5090 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5091 | PyObject *result; |
| 5092 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5093 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 5094 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5095 | return NULL; |
| 5096 | |
| 5097 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 5098 | (PyObject *)substring); |
| 5099 | if (substring == NULL) |
| 5100 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5101 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5102 | if (start < 0) |
| 5103 | start += self->length; |
| 5104 | if (start < 0) |
| 5105 | start = 0; |
| 5106 | if (end > self->length) |
| 5107 | end = self->length; |
| 5108 | if (end < 0) |
| 5109 | end += self->length; |
| 5110 | if (end < 0) |
| 5111 | end = 0; |
| 5112 | |
| 5113 | result = PyInt_FromLong((long) count(self, start, end, substring)); |
| 5114 | |
| 5115 | Py_DECREF(substring); |
| 5116 | return result; |
| 5117 | } |
| 5118 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5119 | PyDoc_STRVAR(encode__doc__, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5120 | "S.encode([encoding[,errors]]) -> string or unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5121 | \n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5122 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 5123 | 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] | 5124 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5125 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 5126 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 5127 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5128 | |
| 5129 | static PyObject * |
| 5130 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 5131 | { |
| 5132 | char *encoding = NULL; |
| 5133 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5134 | PyObject *v; |
| 5135 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5136 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 5137 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5138 | v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5139 | if (v == NULL) |
| 5140 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5141 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 5142 | PyErr_Format(PyExc_TypeError, |
| 5143 | "encoder did not return a string/unicode object " |
| 5144 | "(type=%.400s)", |
| 5145 | v->ob_type->tp_name); |
| 5146 | Py_DECREF(v); |
| 5147 | return NULL; |
| 5148 | } |
| 5149 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5150 | |
| 5151 | onError: |
| 5152 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5153 | } |
| 5154 | |
| 5155 | PyDoc_STRVAR(decode__doc__, |
| 5156 | "S.decode([encoding[,errors]]) -> string or unicode\n\ |
| 5157 | \n\ |
| 5158 | Decodes S using the codec registered for encoding. encoding defaults\n\ |
| 5159 | to the default encoding. errors may be given to set a different error\n\ |
| 5160 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 5161 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 5162 | as well as any other name registerd with codecs.register_error that is\n\ |
| 5163 | able to handle UnicodeDecodeErrors."); |
| 5164 | |
| 5165 | static PyObject * |
Marc-André Lemburg | 126b44c | 2004-07-10 12:04:20 +0000 | [diff] [blame] | 5166 | unicode_decode(PyUnicodeObject *self, PyObject *args) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5167 | { |
| 5168 | char *encoding = NULL; |
| 5169 | char *errors = NULL; |
| 5170 | PyObject *v; |
| 5171 | |
| 5172 | if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
| 5173 | return NULL; |
| 5174 | v = PyUnicode_AsDecodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5175 | if (v == NULL) |
| 5176 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5177 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 5178 | PyErr_Format(PyExc_TypeError, |
| 5179 | "decoder did not return a string/unicode object " |
| 5180 | "(type=%.400s)", |
| 5181 | v->ob_type->tp_name); |
| 5182 | Py_DECREF(v); |
| 5183 | return NULL; |
| 5184 | } |
| 5185 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5186 | |
| 5187 | onError: |
| 5188 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5189 | } |
| 5190 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5191 | PyDoc_STRVAR(expandtabs__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5192 | "S.expandtabs([tabsize]) -> unicode\n\ |
| 5193 | \n\ |
| 5194 | 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] | 5195 | 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] | 5196 | |
| 5197 | static PyObject* |
| 5198 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 5199 | { |
| 5200 | Py_UNICODE *e; |
| 5201 | Py_UNICODE *p; |
| 5202 | Py_UNICODE *q; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5203 | Py_ssize_t i, j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5204 | PyUnicodeObject *u; |
| 5205 | int tabsize = 8; |
| 5206 | |
| 5207 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 5208 | return NULL; |
| 5209 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5210 | /* First pass: determine size of output string */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5211 | i = j = 0; |
| 5212 | e = self->str + self->length; |
| 5213 | for (p = self->str; p < e; p++) |
| 5214 | if (*p == '\t') { |
| 5215 | if (tabsize > 0) |
| 5216 | j += tabsize - (j % tabsize); |
| 5217 | } |
| 5218 | else { |
| 5219 | j++; |
| 5220 | if (*p == '\n' || *p == '\r') { |
| 5221 | i += j; |
| 5222 | j = 0; |
| 5223 | } |
| 5224 | } |
| 5225 | |
| 5226 | /* Second pass: create output string and fill it */ |
| 5227 | u = _PyUnicode_New(i + j); |
| 5228 | if (!u) |
| 5229 | return NULL; |
| 5230 | |
| 5231 | j = 0; |
| 5232 | q = u->str; |
| 5233 | |
| 5234 | for (p = self->str; p < e; p++) |
| 5235 | if (*p == '\t') { |
| 5236 | if (tabsize > 0) { |
| 5237 | i = tabsize - (j % tabsize); |
| 5238 | j += i; |
| 5239 | while (i--) |
| 5240 | *q++ = ' '; |
| 5241 | } |
| 5242 | } |
| 5243 | else { |
| 5244 | j++; |
| 5245 | *q++ = *p; |
| 5246 | if (*p == '\n' || *p == '\r') |
| 5247 | j = 0; |
| 5248 | } |
| 5249 | |
| 5250 | return (PyObject*) u; |
| 5251 | } |
| 5252 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5253 | PyDoc_STRVAR(find__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5254 | "S.find(sub [,start [,end]]) -> int\n\ |
| 5255 | \n\ |
| 5256 | Return the lowest index in S where substring sub is found,\n\ |
| 5257 | such that sub is contained within s[start,end]. Optional\n\ |
| 5258 | arguments start and end are interpreted as in slice notation.\n\ |
| 5259 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5260 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5261 | |
| 5262 | static PyObject * |
| 5263 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 5264 | { |
| 5265 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5266 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5267 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5268 | PyObject *result; |
| 5269 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5270 | if (!PyArg_ParseTuple(args, "O|O&O&:find", &substring, |
| 5271 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5272 | return NULL; |
| 5273 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 5274 | (PyObject *)substring); |
| 5275 | if (substring == NULL) |
| 5276 | return NULL; |
| 5277 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5278 | result = PyInt_FromSsize_t(findstring(self, substring, start, end, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5279 | |
| 5280 | Py_DECREF(substring); |
| 5281 | return result; |
| 5282 | } |
| 5283 | |
| 5284 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5285 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5286 | { |
| 5287 | if (index < 0 || index >= self->length) { |
| 5288 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 5289 | return NULL; |
| 5290 | } |
| 5291 | |
| 5292 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 5293 | } |
| 5294 | |
| 5295 | static long |
| 5296 | unicode_hash(PyUnicodeObject *self) |
| 5297 | { |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5298 | /* Since Unicode objects compare equal to their ASCII string |
| 5299 | counterparts, they should use the individual character values |
| 5300 | as basis for their hash value. This is needed to assure that |
| 5301 | strings and Unicode objects behave in the same way as |
| 5302 | dictionary keys. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5303 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5304 | register Py_ssize_t len; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5305 | register Py_UNICODE *p; |
| 5306 | register long x; |
| 5307 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5308 | if (self->hash != -1) |
| 5309 | return self->hash; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5310 | len = PyUnicode_GET_SIZE(self); |
| 5311 | p = PyUnicode_AS_UNICODE(self); |
| 5312 | x = *p << 7; |
| 5313 | while (--len >= 0) |
| 5314 | x = (1000003*x) ^ *p++; |
| 5315 | x ^= PyUnicode_GET_SIZE(self); |
| 5316 | if (x == -1) |
| 5317 | x = -2; |
| 5318 | self->hash = x; |
| 5319 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5320 | } |
| 5321 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5322 | PyDoc_STRVAR(index__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5323 | "S.index(sub [,start [,end]]) -> int\n\ |
| 5324 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5325 | 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] | 5326 | |
| 5327 | static PyObject * |
| 5328 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 5329 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5330 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5331 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5332 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5333 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5334 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5335 | if (!PyArg_ParseTuple(args, "O|O&O&:index", &substring, |
| 5336 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5337 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5338 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5339 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 5340 | (PyObject *)substring); |
| 5341 | if (substring == NULL) |
| 5342 | return NULL; |
| 5343 | |
| 5344 | result = findstring(self, substring, start, end, 1); |
| 5345 | |
| 5346 | Py_DECREF(substring); |
| 5347 | if (result < 0) { |
| 5348 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 5349 | return NULL; |
| 5350 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5351 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5352 | } |
| 5353 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5354 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5355 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5356 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5357 | 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] | 5358 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5359 | |
| 5360 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5361 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5362 | { |
| 5363 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5364 | register const Py_UNICODE *e; |
| 5365 | int cased; |
| 5366 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5367 | /* Shortcut for single character strings */ |
| 5368 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5369 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5370 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5371 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5372 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5373 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5374 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5375 | e = p + PyUnicode_GET_SIZE(self); |
| 5376 | cased = 0; |
| 5377 | for (; p < e; p++) { |
| 5378 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5379 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5380 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5381 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5382 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 5383 | cased = 1; |
| 5384 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5385 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5386 | } |
| 5387 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5388 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5389 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5390 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5391 | 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] | 5392 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5393 | |
| 5394 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5395 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5396 | { |
| 5397 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5398 | register const Py_UNICODE *e; |
| 5399 | int cased; |
| 5400 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5401 | /* Shortcut for single character strings */ |
| 5402 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5403 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5404 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5405 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5406 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5407 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5408 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5409 | e = p + PyUnicode_GET_SIZE(self); |
| 5410 | cased = 0; |
| 5411 | for (; p < e; p++) { |
| 5412 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5413 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5414 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5415 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5416 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 5417 | cased = 1; |
| 5418 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5419 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5420 | } |
| 5421 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5422 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5423 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5424 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5425 | Return True if S is a titlecased string and there is at least one\n\ |
| 5426 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 5427 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 5428 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5429 | |
| 5430 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5431 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5432 | { |
| 5433 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5434 | register const Py_UNICODE *e; |
| 5435 | int cased, previous_is_cased; |
| 5436 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5437 | /* Shortcut for single character strings */ |
| 5438 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5439 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 5440 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5441 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5442 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5443 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5444 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5445 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5446 | e = p + PyUnicode_GET_SIZE(self); |
| 5447 | cased = 0; |
| 5448 | previous_is_cased = 0; |
| 5449 | for (; p < e; p++) { |
| 5450 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5451 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5452 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 5453 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5454 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5455 | previous_is_cased = 1; |
| 5456 | cased = 1; |
| 5457 | } |
| 5458 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 5459 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5460 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5461 | previous_is_cased = 1; |
| 5462 | cased = 1; |
| 5463 | } |
| 5464 | else |
| 5465 | previous_is_cased = 0; |
| 5466 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5467 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5468 | } |
| 5469 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5470 | PyDoc_STRVAR(isspace__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5471 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5472 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5473 | Return True if all characters in S are whitespace\n\ |
| 5474 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5475 | |
| 5476 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5477 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5478 | { |
| 5479 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5480 | register const Py_UNICODE *e; |
| 5481 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5482 | /* Shortcut for single character strings */ |
| 5483 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5484 | Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5485 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5486 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5487 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5488 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5489 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5490 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5491 | e = p + PyUnicode_GET_SIZE(self); |
| 5492 | for (; p < e; p++) { |
| 5493 | if (!Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5494 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5495 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5496 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5497 | } |
| 5498 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5499 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5500 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5501 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5502 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5503 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5504 | |
| 5505 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5506 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5507 | { |
| 5508 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5509 | register const Py_UNICODE *e; |
| 5510 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5511 | /* Shortcut for single character strings */ |
| 5512 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5513 | Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5514 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5515 | |
| 5516 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5517 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5518 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5519 | |
| 5520 | e = p + PyUnicode_GET_SIZE(self); |
| 5521 | for (; p < e; p++) { |
| 5522 | if (!Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5523 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5524 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5525 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5526 | } |
| 5527 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5528 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5529 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5530 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5531 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5532 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5533 | |
| 5534 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5535 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5536 | { |
| 5537 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5538 | register const Py_UNICODE *e; |
| 5539 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5540 | /* Shortcut for single character strings */ |
| 5541 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5542 | Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5543 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5544 | |
| 5545 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5546 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5547 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5548 | |
| 5549 | e = p + PyUnicode_GET_SIZE(self); |
| 5550 | for (; p < e; p++) { |
| 5551 | if (!Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5552 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5553 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5554 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5555 | } |
| 5556 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5557 | PyDoc_STRVAR(isdecimal__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5558 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5559 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5560 | 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] | 5561 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5562 | |
| 5563 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5564 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5565 | { |
| 5566 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5567 | register const Py_UNICODE *e; |
| 5568 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5569 | /* Shortcut for single character strings */ |
| 5570 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5571 | Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5572 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5573 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5574 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5575 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5576 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5577 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5578 | e = p + PyUnicode_GET_SIZE(self); |
| 5579 | for (; p < e; p++) { |
| 5580 | if (!Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5581 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5582 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5583 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5584 | } |
| 5585 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5586 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5587 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5588 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5589 | Return True if all characters in S are digits\n\ |
| 5590 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5591 | |
| 5592 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5593 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5594 | { |
| 5595 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5596 | register const Py_UNICODE *e; |
| 5597 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5598 | /* Shortcut for single character strings */ |
| 5599 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5600 | Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5601 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5602 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5603 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5604 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5605 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5606 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5607 | e = p + PyUnicode_GET_SIZE(self); |
| 5608 | for (; p < e; p++) { |
| 5609 | if (!Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5610 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5611 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5612 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5613 | } |
| 5614 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5615 | PyDoc_STRVAR(isnumeric__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5616 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5617 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5618 | 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] | 5619 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5620 | |
| 5621 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5622 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5623 | { |
| 5624 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5625 | register const Py_UNICODE *e; |
| 5626 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5627 | /* Shortcut for single character strings */ |
| 5628 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5629 | Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5630 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5631 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5632 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5633 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5634 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5635 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5636 | e = p + PyUnicode_GET_SIZE(self); |
| 5637 | for (; p < e; p++) { |
| 5638 | if (!Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5639 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5640 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5641 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5642 | } |
| 5643 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5644 | PyDoc_STRVAR(join__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5645 | "S.join(sequence) -> unicode\n\ |
| 5646 | \n\ |
| 5647 | 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] | 5648 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5649 | |
| 5650 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5651 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5652 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5653 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5654 | } |
| 5655 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5656 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5657 | unicode_length(PyUnicodeObject *self) |
| 5658 | { |
| 5659 | return self->length; |
| 5660 | } |
| 5661 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5662 | PyDoc_STRVAR(ljust__doc__, |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 5663 | "S.ljust(width[, fillchar]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5664 | \n\ |
| 5665 | 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] | 5666 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5667 | |
| 5668 | static PyObject * |
| 5669 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 5670 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5671 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5672 | Py_UNICODE fillchar = ' '; |
| 5673 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5674 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5675 | return NULL; |
| 5676 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5677 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5678 | Py_INCREF(self); |
| 5679 | return (PyObject*) self; |
| 5680 | } |
| 5681 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5682 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5683 | } |
| 5684 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5685 | PyDoc_STRVAR(lower__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5686 | "S.lower() -> unicode\n\ |
| 5687 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5688 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5689 | |
| 5690 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5691 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5692 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5693 | return fixup(self, fixlower); |
| 5694 | } |
| 5695 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5696 | #define LEFTSTRIP 0 |
| 5697 | #define RIGHTSTRIP 1 |
| 5698 | #define BOTHSTRIP 2 |
| 5699 | |
| 5700 | /* Arrays indexed by above */ |
| 5701 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 5702 | |
| 5703 | #define STRIPNAME(i) (stripformat[i]+3) |
| 5704 | |
| 5705 | static const Py_UNICODE * |
| 5706 | unicode_memchr(const Py_UNICODE *s, Py_UNICODE c, size_t n) |
| 5707 | { |
Tim Peters | 030a5ce | 2002-04-22 19:00:10 +0000 | [diff] [blame] | 5708 | size_t i; |
| 5709 | for (i = 0; i < n; ++i) |
| 5710 | if (s[i] == c) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5711 | return s+i; |
| 5712 | return NULL; |
| 5713 | } |
| 5714 | |
| 5715 | /* externally visible for str.strip(unicode) */ |
| 5716 | PyObject * |
| 5717 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 5718 | { |
| 5719 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5720 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5721 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5722 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 5723 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5724 | |
| 5725 | i = 0; |
| 5726 | if (striptype != RIGHTSTRIP) { |
| 5727 | while (i < len && unicode_memchr(sep, s[i], seplen)) { |
| 5728 | i++; |
| 5729 | } |
| 5730 | } |
| 5731 | |
| 5732 | j = len; |
| 5733 | if (striptype != LEFTSTRIP) { |
| 5734 | do { |
| 5735 | j--; |
| 5736 | } while (j >= i && unicode_memchr(sep, s[j], seplen)); |
| 5737 | j++; |
| 5738 | } |
| 5739 | |
| 5740 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 5741 | Py_INCREF(self); |
| 5742 | return (PyObject*)self; |
| 5743 | } |
| 5744 | else |
| 5745 | return PyUnicode_FromUnicode(s+i, j-i); |
| 5746 | } |
| 5747 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5748 | |
| 5749 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5750 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5751 | { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5752 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5753 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5754 | |
| 5755 | i = 0; |
| 5756 | if (striptype != RIGHTSTRIP) { |
| 5757 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 5758 | i++; |
| 5759 | } |
| 5760 | } |
| 5761 | |
| 5762 | j = len; |
| 5763 | if (striptype != LEFTSTRIP) { |
| 5764 | do { |
| 5765 | j--; |
| 5766 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 5767 | j++; |
| 5768 | } |
| 5769 | |
| 5770 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 5771 | Py_INCREF(self); |
| 5772 | return (PyObject*)self; |
| 5773 | } |
| 5774 | else |
| 5775 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5776 | } |
| 5777 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5778 | |
| 5779 | static PyObject * |
| 5780 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 5781 | { |
| 5782 | PyObject *sep = NULL; |
| 5783 | |
| 5784 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 5785 | return NULL; |
| 5786 | |
| 5787 | if (sep != NULL && sep != Py_None) { |
| 5788 | if (PyUnicode_Check(sep)) |
| 5789 | return _PyUnicode_XStrip(self, striptype, sep); |
| 5790 | else if (PyString_Check(sep)) { |
| 5791 | PyObject *res; |
| 5792 | sep = PyUnicode_FromObject(sep); |
| 5793 | if (sep==NULL) |
| 5794 | return NULL; |
| 5795 | res = _PyUnicode_XStrip(self, striptype, sep); |
| 5796 | Py_DECREF(sep); |
| 5797 | return res; |
| 5798 | } |
| 5799 | else { |
| 5800 | PyErr_Format(PyExc_TypeError, |
| 5801 | "%s arg must be None, unicode or str", |
| 5802 | STRIPNAME(striptype)); |
| 5803 | return NULL; |
| 5804 | } |
| 5805 | } |
| 5806 | |
| 5807 | return do_strip(self, striptype); |
| 5808 | } |
| 5809 | |
| 5810 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5811 | PyDoc_STRVAR(strip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5812 | "S.strip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5813 | \n\ |
| 5814 | Return a copy of the string S with leading and trailing\n\ |
| 5815 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5816 | If chars is given and not None, remove characters in chars instead.\n\ |
| 5817 | 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] | 5818 | |
| 5819 | static PyObject * |
| 5820 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 5821 | { |
| 5822 | if (PyTuple_GET_SIZE(args) == 0) |
| 5823 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 5824 | else |
| 5825 | return do_argstrip(self, BOTHSTRIP, args); |
| 5826 | } |
| 5827 | |
| 5828 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5829 | PyDoc_STRVAR(lstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5830 | "S.lstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5831 | \n\ |
| 5832 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5833 | If chars is given and not None, remove characters in chars instead.\n\ |
| 5834 | 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] | 5835 | |
| 5836 | static PyObject * |
| 5837 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 5838 | { |
| 5839 | if (PyTuple_GET_SIZE(args) == 0) |
| 5840 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 5841 | else |
| 5842 | return do_argstrip(self, LEFTSTRIP, args); |
| 5843 | } |
| 5844 | |
| 5845 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5846 | PyDoc_STRVAR(rstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5847 | "S.rstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5848 | \n\ |
| 5849 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5850 | If chars is given and not None, remove characters in chars instead.\n\ |
| 5851 | 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] | 5852 | |
| 5853 | static PyObject * |
| 5854 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 5855 | { |
| 5856 | if (PyTuple_GET_SIZE(args) == 0) |
| 5857 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 5858 | else |
| 5859 | return do_argstrip(self, RIGHTSTRIP, args); |
| 5860 | } |
| 5861 | |
| 5862 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5863 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5864 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5865 | { |
| 5866 | PyUnicodeObject *u; |
| 5867 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5868 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 5869 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5870 | |
| 5871 | if (len < 0) |
| 5872 | len = 0; |
| 5873 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5874 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5875 | /* no repeat, return original string */ |
| 5876 | Py_INCREF(str); |
| 5877 | return (PyObject*) str; |
| 5878 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 5879 | |
| 5880 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 5881 | * needed doesn't overflow size_t |
| 5882 | */ |
| 5883 | nchars = len * str->length; |
| 5884 | if (len && nchars / len != str->length) { |
| 5885 | PyErr_SetString(PyExc_OverflowError, |
| 5886 | "repeated string is too long"); |
| 5887 | return NULL; |
| 5888 | } |
| 5889 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 5890 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 5891 | PyErr_SetString(PyExc_OverflowError, |
| 5892 | "repeated string is too long"); |
| 5893 | return NULL; |
| 5894 | } |
| 5895 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5896 | if (!u) |
| 5897 | return NULL; |
| 5898 | |
| 5899 | p = u->str; |
| 5900 | |
| 5901 | while (len-- > 0) { |
| 5902 | Py_UNICODE_COPY(p, str->str, str->length); |
| 5903 | p += str->length; |
| 5904 | } |
| 5905 | |
| 5906 | return (PyObject*) u; |
| 5907 | } |
| 5908 | |
| 5909 | PyObject *PyUnicode_Replace(PyObject *obj, |
| 5910 | PyObject *subobj, |
| 5911 | PyObject *replobj, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5912 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5913 | { |
| 5914 | PyObject *self; |
| 5915 | PyObject *str1; |
| 5916 | PyObject *str2; |
| 5917 | PyObject *result; |
| 5918 | |
| 5919 | self = PyUnicode_FromObject(obj); |
| 5920 | if (self == NULL) |
| 5921 | return NULL; |
| 5922 | str1 = PyUnicode_FromObject(subobj); |
| 5923 | if (str1 == NULL) { |
| 5924 | Py_DECREF(self); |
| 5925 | return NULL; |
| 5926 | } |
| 5927 | str2 = PyUnicode_FromObject(replobj); |
| 5928 | if (str2 == NULL) { |
| 5929 | Py_DECREF(self); |
| 5930 | Py_DECREF(str1); |
| 5931 | return NULL; |
| 5932 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5933 | result = replace((PyUnicodeObject *)self, |
| 5934 | (PyUnicodeObject *)str1, |
| 5935 | (PyUnicodeObject *)str2, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5936 | maxcount); |
| 5937 | Py_DECREF(self); |
| 5938 | Py_DECREF(str1); |
| 5939 | Py_DECREF(str2); |
| 5940 | return result; |
| 5941 | } |
| 5942 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5943 | PyDoc_STRVAR(replace__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5944 | "S.replace (old, new[, maxsplit]) -> unicode\n\ |
| 5945 | \n\ |
| 5946 | Return a copy of S with all occurrences of substring\n\ |
| 5947 | 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] | 5948 | given, only the first maxsplit occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5949 | |
| 5950 | static PyObject* |
| 5951 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 5952 | { |
| 5953 | PyUnicodeObject *str1; |
| 5954 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5955 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5956 | PyObject *result; |
| 5957 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5958 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5959 | return NULL; |
| 5960 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 5961 | if (str1 == NULL) |
| 5962 | return NULL; |
| 5963 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 5964 | if (str2 == NULL) { |
| 5965 | Py_DECREF(str1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5966 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 5967 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5968 | |
| 5969 | result = replace(self, str1, str2, maxcount); |
| 5970 | |
| 5971 | Py_DECREF(str1); |
| 5972 | Py_DECREF(str2); |
| 5973 | return result; |
| 5974 | } |
| 5975 | |
| 5976 | static |
| 5977 | PyObject *unicode_repr(PyObject *unicode) |
| 5978 | { |
| 5979 | return unicodeescape_string(PyUnicode_AS_UNICODE(unicode), |
| 5980 | PyUnicode_GET_SIZE(unicode), |
| 5981 | 1); |
| 5982 | } |
| 5983 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5984 | PyDoc_STRVAR(rfind__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5985 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 5986 | \n\ |
| 5987 | Return the highest index in S where substring sub is found,\n\ |
| 5988 | such that sub is contained within s[start,end]. Optional\n\ |
| 5989 | arguments start and end are interpreted as in slice notation.\n\ |
| 5990 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5991 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5992 | |
| 5993 | static PyObject * |
| 5994 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 5995 | { |
| 5996 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5997 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5998 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5999 | PyObject *result; |
| 6000 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6001 | if (!PyArg_ParseTuple(args, "O|O&O&:rfind", &substring, |
| 6002 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6003 | return NULL; |
| 6004 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6005 | (PyObject *)substring); |
| 6006 | if (substring == NULL) |
| 6007 | return NULL; |
| 6008 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6009 | result = PyInt_FromSsize_t(findstring(self, substring, start, end, -1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6010 | |
| 6011 | Py_DECREF(substring); |
| 6012 | return result; |
| 6013 | } |
| 6014 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6015 | PyDoc_STRVAR(rindex__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6016 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 6017 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6018 | 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] | 6019 | |
| 6020 | static PyObject * |
| 6021 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 6022 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6023 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6024 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6025 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6026 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6027 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6028 | if (!PyArg_ParseTuple(args, "O|O&O&:rindex", &substring, |
| 6029 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6030 | return NULL; |
| 6031 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6032 | (PyObject *)substring); |
| 6033 | if (substring == NULL) |
| 6034 | return NULL; |
| 6035 | |
| 6036 | result = findstring(self, substring, start, end, -1); |
| 6037 | |
| 6038 | Py_DECREF(substring); |
| 6039 | if (result < 0) { |
| 6040 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 6041 | return NULL; |
| 6042 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6043 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6044 | } |
| 6045 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6046 | PyDoc_STRVAR(rjust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6047 | "S.rjust(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6048 | \n\ |
| 6049 | 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] | 6050 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6051 | |
| 6052 | static PyObject * |
| 6053 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 6054 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6055 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6056 | Py_UNICODE fillchar = ' '; |
| 6057 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6058 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6059 | return NULL; |
| 6060 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6061 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6062 | Py_INCREF(self); |
| 6063 | return (PyObject*) self; |
| 6064 | } |
| 6065 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6066 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6067 | } |
| 6068 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6069 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6070 | unicode_slice(PyUnicodeObject *self, Py_ssize_t start, Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6071 | { |
| 6072 | /* standard clamping */ |
| 6073 | if (start < 0) |
| 6074 | start = 0; |
| 6075 | if (end < 0) |
| 6076 | end = 0; |
| 6077 | if (end > self->length) |
| 6078 | end = self->length; |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6079 | if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6080 | /* full slice, return original string */ |
| 6081 | Py_INCREF(self); |
| 6082 | return (PyObject*) self; |
| 6083 | } |
| 6084 | if (start > end) |
| 6085 | start = end; |
| 6086 | /* copy slice */ |
| 6087 | return (PyObject*) PyUnicode_FromUnicode(self->str + start, |
| 6088 | end - start); |
| 6089 | } |
| 6090 | |
| 6091 | PyObject *PyUnicode_Split(PyObject *s, |
| 6092 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6093 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6094 | { |
| 6095 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6096 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6097 | s = PyUnicode_FromObject(s); |
| 6098 | if (s == NULL) |
| 6099 | return NULL; |
| 6100 | if (sep != NULL) { |
| 6101 | sep = PyUnicode_FromObject(sep); |
| 6102 | if (sep == NULL) { |
| 6103 | Py_DECREF(s); |
| 6104 | return NULL; |
| 6105 | } |
| 6106 | } |
| 6107 | |
| 6108 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 6109 | |
| 6110 | Py_DECREF(s); |
| 6111 | Py_XDECREF(sep); |
| 6112 | return result; |
| 6113 | } |
| 6114 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6115 | PyDoc_STRVAR(split__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6116 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 6117 | \n\ |
| 6118 | Return a list of the words in S, using sep as the\n\ |
| 6119 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Thomas Heller | ca0d2cb | 2004-09-15 11:41:32 +0000 | [diff] [blame] | 6120 | splits are done. If sep is not specified or is None,\n\ |
Walter Dörwald | 782afc5 | 2004-09-14 09:40:45 +0000 | [diff] [blame] | 6121 | any whitespace string is a separator."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6122 | |
| 6123 | static PyObject* |
| 6124 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 6125 | { |
| 6126 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6127 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6128 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6129 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6130 | return NULL; |
| 6131 | |
| 6132 | if (substring == Py_None) |
| 6133 | return split(self, NULL, maxcount); |
| 6134 | else if (PyUnicode_Check(substring)) |
| 6135 | return split(self, (PyUnicodeObject *)substring, maxcount); |
| 6136 | else |
| 6137 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
| 6138 | } |
| 6139 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6140 | PyObject *PyUnicode_RSplit(PyObject *s, |
| 6141 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6142 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6143 | { |
| 6144 | PyObject *result; |
| 6145 | |
| 6146 | s = PyUnicode_FromObject(s); |
| 6147 | if (s == NULL) |
| 6148 | return NULL; |
| 6149 | if (sep != NULL) { |
| 6150 | sep = PyUnicode_FromObject(sep); |
| 6151 | if (sep == NULL) { |
| 6152 | Py_DECREF(s); |
| 6153 | return NULL; |
| 6154 | } |
| 6155 | } |
| 6156 | |
| 6157 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 6158 | |
| 6159 | Py_DECREF(s); |
| 6160 | Py_XDECREF(sep); |
| 6161 | return result; |
| 6162 | } |
| 6163 | |
| 6164 | PyDoc_STRVAR(rsplit__doc__, |
| 6165 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
| 6166 | \n\ |
| 6167 | Return a list of the words in S, using sep as the\n\ |
| 6168 | delimiter string, starting at the end of the string and\n\ |
| 6169 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 6170 | splits are done. If sep is not specified, any whitespace string\n\ |
| 6171 | is a separator."); |
| 6172 | |
| 6173 | static PyObject* |
| 6174 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 6175 | { |
| 6176 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6177 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6178 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6179 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6180 | return NULL; |
| 6181 | |
| 6182 | if (substring == Py_None) |
| 6183 | return rsplit(self, NULL, maxcount); |
| 6184 | else if (PyUnicode_Check(substring)) |
| 6185 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
| 6186 | else |
| 6187 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
| 6188 | } |
| 6189 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6190 | PyDoc_STRVAR(splitlines__doc__, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6191 | "S.splitlines([keepends]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6192 | \n\ |
| 6193 | 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] | 6194 | 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] | 6195 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6196 | |
| 6197 | static PyObject* |
| 6198 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 6199 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6200 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6201 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6202 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6203 | return NULL; |
| 6204 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6205 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6206 | } |
| 6207 | |
| 6208 | static |
| 6209 | PyObject *unicode_str(PyUnicodeObject *self) |
| 6210 | { |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6211 | return PyUnicode_AsEncodedString((PyObject *)self, NULL, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6212 | } |
| 6213 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6214 | PyDoc_STRVAR(swapcase__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6215 | "S.swapcase() -> unicode\n\ |
| 6216 | \n\ |
| 6217 | 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] | 6218 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6219 | |
| 6220 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6221 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6222 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6223 | return fixup(self, fixswapcase); |
| 6224 | } |
| 6225 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6226 | PyDoc_STRVAR(translate__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6227 | "S.translate(table) -> unicode\n\ |
| 6228 | \n\ |
| 6229 | Return a copy of the string S, where all characters have been mapped\n\ |
| 6230 | 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] | 6231 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ |
| 6232 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 6233 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6234 | |
| 6235 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6236 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6237 | { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6238 | return PyUnicode_TranslateCharmap(self->str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6239 | self->length, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6240 | table, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6241 | "ignore"); |
| 6242 | } |
| 6243 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6244 | PyDoc_STRVAR(upper__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6245 | "S.upper() -> unicode\n\ |
| 6246 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6247 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6248 | |
| 6249 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6250 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6251 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6252 | return fixup(self, fixupper); |
| 6253 | } |
| 6254 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6255 | PyDoc_STRVAR(zfill__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6256 | "S.zfill(width) -> unicode\n\ |
| 6257 | \n\ |
| 6258 | 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] | 6259 | of the specified width. The string x is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6260 | |
| 6261 | static PyObject * |
| 6262 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 6263 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6264 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6265 | PyUnicodeObject *u; |
| 6266 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6267 | Py_ssize_t width; |
| 6268 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6269 | return NULL; |
| 6270 | |
| 6271 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 6272 | if (PyUnicode_CheckExact(self)) { |
| 6273 | Py_INCREF(self); |
| 6274 | return (PyObject*) self; |
| 6275 | } |
| 6276 | else |
| 6277 | return PyUnicode_FromUnicode( |
| 6278 | PyUnicode_AS_UNICODE(self), |
| 6279 | PyUnicode_GET_SIZE(self) |
| 6280 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6281 | } |
| 6282 | |
| 6283 | fill = width - self->length; |
| 6284 | |
| 6285 | u = pad(self, fill, 0, '0'); |
| 6286 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 6287 | if (u == NULL) |
| 6288 | return NULL; |
| 6289 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6290 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 6291 | /* move sign to beginning of string */ |
| 6292 | u->str[0] = u->str[fill]; |
| 6293 | u->str[fill] = '0'; |
| 6294 | } |
| 6295 | |
| 6296 | return (PyObject*) u; |
| 6297 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6298 | |
| 6299 | #if 0 |
| 6300 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6301 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6302 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6303 | return PyInt_FromLong(unicode_freelist_size); |
| 6304 | } |
| 6305 | #endif |
| 6306 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6307 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6308 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6309 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 6310 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 6311 | With optional start, test S beginning at that position.\n\ |
| 6312 | With optional end, stop comparing S at that position."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6313 | |
| 6314 | static PyObject * |
| 6315 | unicode_startswith(PyUnicodeObject *self, |
| 6316 | PyObject *args) |
| 6317 | { |
| 6318 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6319 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6320 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6321 | PyObject *result; |
| 6322 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6323 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &substring, |
| 6324 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6325 | return NULL; |
| 6326 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6327 | (PyObject *)substring); |
| 6328 | if (substring == NULL) |
| 6329 | return NULL; |
| 6330 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6331 | result = PyBool_FromLong(tailmatch(self, substring, start, end, -1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6332 | |
| 6333 | Py_DECREF(substring); |
| 6334 | return result; |
| 6335 | } |
| 6336 | |
| 6337 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6338 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6339 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6340 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 6341 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 6342 | With optional start, test S beginning at that position.\n\ |
| 6343 | With optional end, stop comparing S at that position."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6344 | |
| 6345 | static PyObject * |
| 6346 | unicode_endswith(PyUnicodeObject *self, |
| 6347 | PyObject *args) |
| 6348 | { |
| 6349 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6350 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6351 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6352 | PyObject *result; |
| 6353 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6354 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &substring, |
| 6355 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6356 | return NULL; |
| 6357 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6358 | (PyObject *)substring); |
| 6359 | if (substring == NULL) |
| 6360 | return NULL; |
| 6361 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6362 | result = PyBool_FromLong(tailmatch(self, substring, start, end, +1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6363 | |
| 6364 | Py_DECREF(substring); |
| 6365 | return result; |
| 6366 | } |
| 6367 | |
| 6368 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 6369 | |
| 6370 | static PyObject * |
| 6371 | unicode_getnewargs(PyUnicodeObject *v) |
| 6372 | { |
| 6373 | return Py_BuildValue("(u#)", v->str, v->length); |
| 6374 | } |
| 6375 | |
| 6376 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6377 | static PyMethodDef unicode_methods[] = { |
| 6378 | |
| 6379 | /* Order is according to common usage: often used methods should |
| 6380 | appear first, since lookup is done sequentially. */ |
| 6381 | |
Georg Brandl | ecdc0a9 | 2006-03-30 12:19:07 +0000 | [diff] [blame] | 6382 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6383 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 6384 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6385 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6386 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 6387 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 6388 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 6389 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 6390 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 6391 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 6392 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
| 6393 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 6394 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 6395 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6396 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6397 | {"decode", (PyCFunction) unicode_decode, METH_VARARGS, decode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6398 | /* {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */ |
| 6399 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 6400 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 6401 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6402 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6403 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6404 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6405 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 6406 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 6407 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 6408 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 6409 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 6410 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 6411 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 6412 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 6413 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 6414 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 6415 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 6416 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 6417 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 6418 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6419 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 6420 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6421 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6422 | #endif |
| 6423 | |
| 6424 | #if 0 |
| 6425 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6426 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6427 | #endif |
| 6428 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 6429 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6430 | {NULL, NULL} |
| 6431 | }; |
| 6432 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 6433 | static PyObject * |
| 6434 | unicode_mod(PyObject *v, PyObject *w) |
| 6435 | { |
| 6436 | if (!PyUnicode_Check(v)) { |
| 6437 | Py_INCREF(Py_NotImplemented); |
| 6438 | return Py_NotImplemented; |
| 6439 | } |
| 6440 | return PyUnicode_Format(v, w); |
| 6441 | } |
| 6442 | |
| 6443 | static PyNumberMethods unicode_as_number = { |
| 6444 | 0, /*nb_add*/ |
| 6445 | 0, /*nb_subtract*/ |
| 6446 | 0, /*nb_multiply*/ |
| 6447 | 0, /*nb_divide*/ |
| 6448 | unicode_mod, /*nb_remainder*/ |
| 6449 | }; |
| 6450 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6451 | static PySequenceMethods unicode_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6452 | (lenfunc) unicode_length, /* sq_length */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 6453 | PyUnicode_Concat, /* sq_concat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6454 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 6455 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 6456 | (ssizessizeargfunc) unicode_slice, /* sq_slice */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6457 | 0, /* sq_ass_item */ |
| 6458 | 0, /* sq_ass_slice */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 6459 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6460 | }; |
| 6461 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 6462 | #define HASINDEX(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_HAVE_INDEX) |
| 6463 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6464 | static PyObject* |
| 6465 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 6466 | { |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 6467 | PyNumberMethods *nb = item->ob_type->tp_as_number; |
| 6468 | if (nb != NULL && HASINDEX(item) && nb->nb_index != NULL) { |
| 6469 | Py_ssize_t i = nb->nb_index(item); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6470 | if (i == -1 && PyErr_Occurred()) |
| 6471 | return NULL; |
| 6472 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6473 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6474 | return unicode_getitem(self, i); |
| 6475 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6476 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6477 | Py_UNICODE* source_buf; |
| 6478 | Py_UNICODE* result_buf; |
| 6479 | PyObject* result; |
| 6480 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6481 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6482 | &start, &stop, &step, &slicelength) < 0) { |
| 6483 | return NULL; |
| 6484 | } |
| 6485 | |
| 6486 | if (slicelength <= 0) { |
| 6487 | return PyUnicode_FromUnicode(NULL, 0); |
| 6488 | } else { |
| 6489 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 6490 | result_buf = (Py_UNICODE *)PyMem_MALLOC(slicelength* |
| 6491 | sizeof(Py_UNICODE)); |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6492 | |
| 6493 | if (result_buf == NULL) |
| 6494 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6495 | |
| 6496 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 6497 | result_buf[i] = source_buf[cur]; |
| 6498 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6499 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6500 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
| 6501 | PyMem_FREE(result_buf); |
| 6502 | return result; |
| 6503 | } |
| 6504 | } else { |
| 6505 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 6506 | return NULL; |
| 6507 | } |
| 6508 | } |
| 6509 | |
| 6510 | static PyMappingMethods unicode_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6511 | (lenfunc)unicode_length, /* mp_length */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6512 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 6513 | (objobjargproc)0, /* mp_ass_subscript */ |
| 6514 | }; |
| 6515 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6516 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6517 | unicode_buffer_getreadbuf(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6518 | Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6519 | const void **ptr) |
| 6520 | { |
| 6521 | if (index != 0) { |
| 6522 | PyErr_SetString(PyExc_SystemError, |
| 6523 | "accessing non-existent unicode segment"); |
| 6524 | return -1; |
| 6525 | } |
| 6526 | *ptr = (void *) self->str; |
| 6527 | return PyUnicode_GET_DATA_SIZE(self); |
| 6528 | } |
| 6529 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6530 | static Py_ssize_t |
| 6531 | unicode_buffer_getwritebuf(PyUnicodeObject *self, Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6532 | const void **ptr) |
| 6533 | { |
| 6534 | PyErr_SetString(PyExc_TypeError, |
Neal Norwitz | 20e7213 | 2002-06-13 21:25:17 +0000 | [diff] [blame] | 6535 | "cannot use unicode as modifiable buffer"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6536 | return -1; |
| 6537 | } |
| 6538 | |
| 6539 | static int |
| 6540 | unicode_buffer_getsegcount(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6541 | Py_ssize_t *lenp) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6542 | { |
| 6543 | if (lenp) |
| 6544 | *lenp = PyUnicode_GET_DATA_SIZE(self); |
| 6545 | return 1; |
| 6546 | } |
| 6547 | |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 6548 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6549 | unicode_buffer_getcharbuf(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6550 | Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6551 | const void **ptr) |
| 6552 | { |
| 6553 | PyObject *str; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6554 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6555 | if (index != 0) { |
| 6556 | PyErr_SetString(PyExc_SystemError, |
| 6557 | "accessing non-existent unicode segment"); |
| 6558 | return -1; |
| 6559 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 6560 | str = _PyUnicode_AsDefaultEncodedString((PyObject *)self, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6561 | if (str == NULL) |
| 6562 | return -1; |
| 6563 | *ptr = (void *) PyString_AS_STRING(str); |
| 6564 | return PyString_GET_SIZE(str); |
| 6565 | } |
| 6566 | |
| 6567 | /* Helpers for PyUnicode_Format() */ |
| 6568 | |
| 6569 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6570 | getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6571 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6572 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6573 | if (argidx < arglen) { |
| 6574 | (*p_argidx)++; |
| 6575 | if (arglen < 0) |
| 6576 | return args; |
| 6577 | else |
| 6578 | return PyTuple_GetItem(args, argidx); |
| 6579 | } |
| 6580 | PyErr_SetString(PyExc_TypeError, |
| 6581 | "not enough arguments for format string"); |
| 6582 | return NULL; |
| 6583 | } |
| 6584 | |
| 6585 | #define F_LJUST (1<<0) |
| 6586 | #define F_SIGN (1<<1) |
| 6587 | #define F_BLANK (1<<2) |
| 6588 | #define F_ALT (1<<3) |
| 6589 | #define F_ZERO (1<<4) |
| 6590 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6591 | static Py_ssize_t |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6592 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6593 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6594 | register Py_ssize_t i; |
| 6595 | Py_ssize_t len = strlen(charbuffer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6596 | for (i = len - 1; i >= 0; i--) |
| 6597 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
| 6598 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6599 | return len; |
| 6600 | } |
| 6601 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6602 | static int |
| 6603 | doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) |
| 6604 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 6605 | Py_ssize_t result; |
| 6606 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6607 | PyOS_ascii_formatd((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 6608 | result = strtounicode(buffer, (char *)buffer); |
| 6609 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6610 | } |
| 6611 | |
| 6612 | static int |
| 6613 | longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) |
| 6614 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 6615 | Py_ssize_t result; |
| 6616 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6617 | PyOS_snprintf((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 6618 | result = strtounicode(buffer, (char *)buffer); |
| 6619 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6620 | } |
| 6621 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 6622 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 6623 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 6624 | formatting is done. */ |
| 6625 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6626 | static int |
| 6627 | formatfloat(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6628 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6629 | int flags, |
| 6630 | int prec, |
| 6631 | int type, |
| 6632 | PyObject *v) |
| 6633 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6634 | /* fmt = '%#.' + `prec` + `type` |
| 6635 | 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] | 6636 | char fmt[20]; |
| 6637 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6638 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6639 | x = PyFloat_AsDouble(v); |
| 6640 | if (x == -1.0 && PyErr_Occurred()) |
| 6641 | return -1; |
| 6642 | if (prec < 0) |
| 6643 | prec = 6; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6644 | if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
| 6645 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6646 | /* Worst case length calc to ensure no buffer overrun: |
| 6647 | |
| 6648 | 'g' formats: |
| 6649 | fmt = %#.<prec>g |
| 6650 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 6651 | for any double rep.) |
| 6652 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 6653 | |
| 6654 | 'f' formats: |
| 6655 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 6656 | len = 1 + 50 + 1 + prec = 52 + prec |
| 6657 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6658 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6659 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6660 | |
| 6661 | */ |
| 6662 | if ((type == 'g' && buflen <= (size_t)10 + (size_t)prec) || |
| 6663 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6664 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6665 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6666 | return -1; |
| 6667 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6668 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 6669 | (flags&F_ALT) ? "#" : "", |
| 6670 | prec, type); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6671 | return doubletounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6672 | } |
| 6673 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6674 | static PyObject* |
| 6675 | formatlong(PyObject *val, int flags, int prec, int type) |
| 6676 | { |
| 6677 | char *buf; |
| 6678 | int i, len; |
| 6679 | PyObject *str; /* temporary string object. */ |
| 6680 | PyUnicodeObject *result; |
| 6681 | |
| 6682 | str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
| 6683 | if (!str) |
| 6684 | return NULL; |
| 6685 | result = _PyUnicode_New(len); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 6686 | if (!result) { |
| 6687 | Py_DECREF(str); |
| 6688 | return NULL; |
| 6689 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6690 | for (i = 0; i < len; i++) |
| 6691 | result->str[i] = buf[i]; |
| 6692 | result->str[len] = 0; |
| 6693 | Py_DECREF(str); |
| 6694 | return (PyObject*)result; |
| 6695 | } |
| 6696 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6697 | static int |
| 6698 | formatint(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6699 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6700 | int flags, |
| 6701 | int prec, |
| 6702 | int type, |
| 6703 | PyObject *v) |
| 6704 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6705 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6706 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 6707 | * + 1 + 1 |
| 6708 | * = 24 |
| 6709 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6710 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6711 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6712 | long x; |
| 6713 | |
| 6714 | x = PyInt_AsLong(v); |
| 6715 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6716 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6717 | if (x < 0 && type == 'u') { |
| 6718 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 6719 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6720 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 6721 | sign = "-"; |
| 6722 | else |
| 6723 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6724 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6725 | prec = 1; |
| 6726 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6727 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 6728 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6729 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6730 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6731 | PyErr_SetString(PyExc_OverflowError, |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6732 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6733 | return -1; |
| 6734 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6735 | |
| 6736 | if ((flags & F_ALT) && |
| 6737 | (type == 'x' || type == 'X')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6738 | /* When converting under %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6739 | * of issues that cause pain: |
| 6740 | * - when 0 is being converted, the C standard leaves off |
| 6741 | * the '0x' or '0X', which is inconsistent with other |
| 6742 | * %#x/%#X conversions and inconsistent with Python's |
| 6743 | * hex() function |
| 6744 | * - there are platforms that violate the standard and |
| 6745 | * convert 0 with the '0x' or '0X' |
| 6746 | * (Metrowerks, Compaq Tru64) |
| 6747 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6748 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6749 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6750 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6751 | * We can achieve the desired consistency by inserting our |
| 6752 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 6753 | * of %#x/%#X. |
| 6754 | * |
| 6755 | * Note that this is the same approach as used in |
| 6756 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 6757 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6758 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 6759 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 6760 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6761 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6762 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 6763 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6764 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 6765 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6766 | if (sign[0]) |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6767 | return longtounicode(buf, buflen, fmt, -x); |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6768 | else |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6769 | return longtounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6770 | } |
| 6771 | |
| 6772 | static int |
| 6773 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6774 | size_t buflen, |
| 6775 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6776 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6777 | /* presume that the buffer is at least 2 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6778 | if (PyUnicode_Check(v)) { |
| 6779 | if (PyUnicode_GET_SIZE(v) != 1) |
| 6780 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6781 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6782 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6783 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6784 | else if (PyString_Check(v)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6785 | if (PyString_GET_SIZE(v) != 1) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6786 | goto onError; |
| 6787 | buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0]; |
| 6788 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6789 | |
| 6790 | else { |
| 6791 | /* Integer input truncated to a character */ |
| 6792 | long x; |
| 6793 | x = PyInt_AsLong(v); |
| 6794 | if (x == -1 && PyErr_Occurred()) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6795 | goto onError; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 6796 | #ifdef Py_UNICODE_WIDE |
| 6797 | if (x < 0 || x > 0x10ffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 6798 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 6799 | "%c arg not in range(0x110000) " |
| 6800 | "(wide Python build)"); |
| 6801 | return -1; |
| 6802 | } |
| 6803 | #else |
| 6804 | if (x < 0 || x > 0xffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 6805 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 6806 | "%c arg not in range(0x10000) " |
| 6807 | "(narrow Python build)"); |
| 6808 | return -1; |
| 6809 | } |
| 6810 | #endif |
| 6811 | buf[0] = (Py_UNICODE) x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6812 | } |
| 6813 | buf[1] = '\0'; |
| 6814 | return 1; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6815 | |
| 6816 | onError: |
| 6817 | PyErr_SetString(PyExc_TypeError, |
| 6818 | "%c requires int or char"); |
| 6819 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6820 | } |
| 6821 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6822 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 6823 | |
| 6824 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 6825 | chars are formatted. XXX This is a magic number. Each formatting |
| 6826 | routine does bounds checking to ensure no overflow, but a better |
| 6827 | solution may be to malloc a buffer of appropriate size for each |
| 6828 | format. For now, the current solution is sufficient. |
| 6829 | */ |
| 6830 | #define FORMATBUFLEN (size_t)120 |
| 6831 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6832 | PyObject *PyUnicode_Format(PyObject *format, |
| 6833 | PyObject *args) |
| 6834 | { |
| 6835 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6836 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6837 | int args_owned = 0; |
| 6838 | PyUnicodeObject *result = NULL; |
| 6839 | PyObject *dict = NULL; |
| 6840 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6841 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6842 | if (format == NULL || args == NULL) { |
| 6843 | PyErr_BadInternalCall(); |
| 6844 | return NULL; |
| 6845 | } |
| 6846 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6847 | if (uformat == NULL) |
| 6848 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6849 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 6850 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 6851 | |
| 6852 | reslen = rescnt = fmtcnt + 100; |
| 6853 | result = _PyUnicode_New(reslen); |
| 6854 | if (result == NULL) |
| 6855 | goto onError; |
| 6856 | res = PyUnicode_AS_UNICODE(result); |
| 6857 | |
| 6858 | if (PyTuple_Check(args)) { |
| 6859 | arglen = PyTuple_Size(args); |
| 6860 | argidx = 0; |
| 6861 | } |
| 6862 | else { |
| 6863 | arglen = -1; |
| 6864 | argidx = -2; |
| 6865 | } |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 6866 | if (args->ob_type->tp_as_mapping && !PyTuple_Check(args) && |
| 6867 | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6868 | dict = args; |
| 6869 | |
| 6870 | while (--fmtcnt >= 0) { |
| 6871 | if (*fmt != '%') { |
| 6872 | if (--rescnt < 0) { |
| 6873 | rescnt = fmtcnt + 100; |
| 6874 | reslen += rescnt; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6875 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 6876 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6877 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 6878 | --rescnt; |
| 6879 | } |
| 6880 | *res++ = *fmt++; |
| 6881 | } |
| 6882 | else { |
| 6883 | /* Got a format specifier */ |
| 6884 | int flags = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6885 | Py_ssize_t width = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6886 | int prec = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6887 | Py_UNICODE c = '\0'; |
| 6888 | Py_UNICODE fill; |
| 6889 | PyObject *v = NULL; |
| 6890 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6891 | Py_UNICODE *pbuf; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6892 | Py_UNICODE sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6893 | Py_ssize_t len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6894 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6895 | |
| 6896 | fmt++; |
| 6897 | if (*fmt == '(') { |
| 6898 | Py_UNICODE *keystart; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6899 | Py_ssize_t keylen; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6900 | PyObject *key; |
| 6901 | int pcount = 1; |
| 6902 | |
| 6903 | if (dict == NULL) { |
| 6904 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6905 | "format requires a mapping"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6906 | goto onError; |
| 6907 | } |
| 6908 | ++fmt; |
| 6909 | --fmtcnt; |
| 6910 | keystart = fmt; |
| 6911 | /* Skip over balanced parentheses */ |
| 6912 | while (pcount > 0 && --fmtcnt >= 0) { |
| 6913 | if (*fmt == ')') |
| 6914 | --pcount; |
| 6915 | else if (*fmt == '(') |
| 6916 | ++pcount; |
| 6917 | fmt++; |
| 6918 | } |
| 6919 | keylen = fmt - keystart - 1; |
| 6920 | if (fmtcnt < 0 || pcount > 0) { |
| 6921 | PyErr_SetString(PyExc_ValueError, |
| 6922 | "incomplete format key"); |
| 6923 | goto onError; |
| 6924 | } |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 6925 | #if 0 |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6926 | /* keys are converted to strings using UTF-8 and |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6927 | then looked up since Python uses strings to hold |
| 6928 | variables names etc. in its namespaces and we |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6929 | wouldn't want to break common idioms. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6930 | key = PyUnicode_EncodeUTF8(keystart, |
| 6931 | keylen, |
| 6932 | NULL); |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 6933 | #else |
| 6934 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 6935 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6936 | if (key == NULL) |
| 6937 | goto onError; |
| 6938 | if (args_owned) { |
| 6939 | Py_DECREF(args); |
| 6940 | args_owned = 0; |
| 6941 | } |
| 6942 | args = PyObject_GetItem(dict, key); |
| 6943 | Py_DECREF(key); |
| 6944 | if (args == NULL) { |
| 6945 | goto onError; |
| 6946 | } |
| 6947 | args_owned = 1; |
| 6948 | arglen = -1; |
| 6949 | argidx = -2; |
| 6950 | } |
| 6951 | while (--fmtcnt >= 0) { |
| 6952 | switch (c = *fmt++) { |
| 6953 | case '-': flags |= F_LJUST; continue; |
| 6954 | case '+': flags |= F_SIGN; continue; |
| 6955 | case ' ': flags |= F_BLANK; continue; |
| 6956 | case '#': flags |= F_ALT; continue; |
| 6957 | case '0': flags |= F_ZERO; continue; |
| 6958 | } |
| 6959 | break; |
| 6960 | } |
| 6961 | if (c == '*') { |
| 6962 | v = getnextarg(args, arglen, &argidx); |
| 6963 | if (v == NULL) |
| 6964 | goto onError; |
| 6965 | if (!PyInt_Check(v)) { |
| 6966 | PyErr_SetString(PyExc_TypeError, |
| 6967 | "* wants int"); |
| 6968 | goto onError; |
| 6969 | } |
| 6970 | width = PyInt_AsLong(v); |
| 6971 | if (width < 0) { |
| 6972 | flags |= F_LJUST; |
| 6973 | width = -width; |
| 6974 | } |
| 6975 | if (--fmtcnt >= 0) |
| 6976 | c = *fmt++; |
| 6977 | } |
| 6978 | else if (c >= '0' && c <= '9') { |
| 6979 | width = c - '0'; |
| 6980 | while (--fmtcnt >= 0) { |
| 6981 | c = *fmt++; |
| 6982 | if (c < '0' || c > '9') |
| 6983 | break; |
| 6984 | if ((width*10) / 10 != width) { |
| 6985 | PyErr_SetString(PyExc_ValueError, |
| 6986 | "width too big"); |
| 6987 | goto onError; |
| 6988 | } |
| 6989 | width = width*10 + (c - '0'); |
| 6990 | } |
| 6991 | } |
| 6992 | if (c == '.') { |
| 6993 | prec = 0; |
| 6994 | if (--fmtcnt >= 0) |
| 6995 | c = *fmt++; |
| 6996 | if (c == '*') { |
| 6997 | v = getnextarg(args, arglen, &argidx); |
| 6998 | if (v == NULL) |
| 6999 | goto onError; |
| 7000 | if (!PyInt_Check(v)) { |
| 7001 | PyErr_SetString(PyExc_TypeError, |
| 7002 | "* wants int"); |
| 7003 | goto onError; |
| 7004 | } |
| 7005 | prec = PyInt_AsLong(v); |
| 7006 | if (prec < 0) |
| 7007 | prec = 0; |
| 7008 | if (--fmtcnt >= 0) |
| 7009 | c = *fmt++; |
| 7010 | } |
| 7011 | else if (c >= '0' && c <= '9') { |
| 7012 | prec = c - '0'; |
| 7013 | while (--fmtcnt >= 0) { |
| 7014 | c = Py_CHARMASK(*fmt++); |
| 7015 | if (c < '0' || c > '9') |
| 7016 | break; |
| 7017 | if ((prec*10) / 10 != prec) { |
| 7018 | PyErr_SetString(PyExc_ValueError, |
| 7019 | "prec too big"); |
| 7020 | goto onError; |
| 7021 | } |
| 7022 | prec = prec*10 + (c - '0'); |
| 7023 | } |
| 7024 | } |
| 7025 | } /* prec */ |
| 7026 | if (fmtcnt >= 0) { |
| 7027 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7028 | if (--fmtcnt >= 0) |
| 7029 | c = *fmt++; |
| 7030 | } |
| 7031 | } |
| 7032 | if (fmtcnt < 0) { |
| 7033 | PyErr_SetString(PyExc_ValueError, |
| 7034 | "incomplete format"); |
| 7035 | goto onError; |
| 7036 | } |
| 7037 | if (c != '%') { |
| 7038 | v = getnextarg(args, arglen, &argidx); |
| 7039 | if (v == NULL) |
| 7040 | goto onError; |
| 7041 | } |
| 7042 | sign = 0; |
| 7043 | fill = ' '; |
| 7044 | switch (c) { |
| 7045 | |
| 7046 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7047 | pbuf = formatbuf; |
| 7048 | /* presume that buffer length is at least 1 */ |
| 7049 | pbuf[0] = '%'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7050 | len = 1; |
| 7051 | break; |
| 7052 | |
| 7053 | case 's': |
| 7054 | case 'r': |
| 7055 | if (PyUnicode_Check(v) && c == 's') { |
| 7056 | temp = v; |
| 7057 | Py_INCREF(temp); |
| 7058 | } |
| 7059 | else { |
| 7060 | PyObject *unicode; |
| 7061 | if (c == 's') |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 7062 | temp = PyObject_Unicode(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7063 | else |
| 7064 | temp = PyObject_Repr(v); |
| 7065 | if (temp == NULL) |
| 7066 | goto onError; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 7067 | if (PyUnicode_Check(temp)) |
| 7068 | /* nothing to do */; |
| 7069 | else if (PyString_Check(temp)) { |
| 7070 | /* convert to string to Unicode */ |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7071 | unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7072 | PyString_GET_SIZE(temp), |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7073 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7074 | "strict"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7075 | Py_DECREF(temp); |
| 7076 | temp = unicode; |
| 7077 | if (temp == NULL) |
| 7078 | goto onError; |
| 7079 | } |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 7080 | else { |
| 7081 | Py_DECREF(temp); |
| 7082 | PyErr_SetString(PyExc_TypeError, |
| 7083 | "%s argument has non-string str()"); |
| 7084 | goto onError; |
| 7085 | } |
| 7086 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7087 | pbuf = PyUnicode_AS_UNICODE(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7088 | len = PyUnicode_GET_SIZE(temp); |
| 7089 | if (prec >= 0 && len > prec) |
| 7090 | len = prec; |
| 7091 | break; |
| 7092 | |
| 7093 | case 'i': |
| 7094 | case 'd': |
| 7095 | case 'u': |
| 7096 | case 'o': |
| 7097 | case 'x': |
| 7098 | case 'X': |
| 7099 | if (c == 'i') |
| 7100 | c = 'd'; |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 7101 | if (PyLong_Check(v)) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7102 | temp = formatlong(v, flags, prec, c); |
| 7103 | if (!temp) |
| 7104 | goto onError; |
| 7105 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 7106 | len = PyUnicode_GET_SIZE(temp); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7107 | sign = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7108 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7109 | else { |
| 7110 | pbuf = formatbuf; |
| 7111 | len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 7112 | flags, prec, c, v); |
| 7113 | if (len < 0) |
| 7114 | goto onError; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7115 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7116 | } |
| 7117 | if (flags & F_ZERO) |
| 7118 | fill = '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7119 | break; |
| 7120 | |
| 7121 | case 'e': |
| 7122 | case 'E': |
| 7123 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 7124 | case 'F': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7125 | case 'g': |
| 7126 | case 'G': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 7127 | if (c == 'F') |
| 7128 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7129 | pbuf = formatbuf; |
| 7130 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 7131 | flags, prec, c, v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7132 | if (len < 0) |
| 7133 | goto onError; |
| 7134 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7135 | if (flags & F_ZERO) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7136 | fill = '0'; |
| 7137 | break; |
| 7138 | |
| 7139 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7140 | pbuf = formatbuf; |
| 7141 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7142 | if (len < 0) |
| 7143 | goto onError; |
| 7144 | break; |
| 7145 | |
| 7146 | default: |
| 7147 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 7148 | "unsupported format character '%c' (0x%x) " |
| 7149 | "at index %i", |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7150 | (31<=c && c<=126) ? (char)c : '?', |
Marc-André Lemburg | 24e53b6 | 2002-09-24 09:32:14 +0000 | [diff] [blame] | 7151 | (int)c, |
Guido van Rossum | efc1188 | 2002-09-12 14:43:41 +0000 | [diff] [blame] | 7152 | (int)(fmt -1 - PyUnicode_AS_UNICODE(uformat))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7153 | goto onError; |
| 7154 | } |
| 7155 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7156 | if (*pbuf == '-' || *pbuf == '+') { |
| 7157 | sign = *pbuf++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7158 | len--; |
| 7159 | } |
| 7160 | else if (flags & F_SIGN) |
| 7161 | sign = '+'; |
| 7162 | else if (flags & F_BLANK) |
| 7163 | sign = ' '; |
| 7164 | else |
| 7165 | sign = 0; |
| 7166 | } |
| 7167 | if (width < len) |
| 7168 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 7169 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7170 | reslen -= rescnt; |
| 7171 | rescnt = width + fmtcnt + 100; |
| 7172 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 7173 | if (reslen < 0) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 7174 | Py_XDECREF(temp); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7175 | PyErr_NoMemory(); |
| 7176 | goto onError; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 7177 | } |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7178 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 7179 | Py_XDECREF(temp); |
| 7180 | goto onError; |
| 7181 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7182 | res = PyUnicode_AS_UNICODE(result) |
| 7183 | + reslen - rescnt; |
| 7184 | } |
| 7185 | if (sign) { |
| 7186 | if (fill != ' ') |
| 7187 | *res++ = sign; |
| 7188 | rescnt--; |
| 7189 | if (width > len) |
| 7190 | width--; |
| 7191 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7192 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 7193 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7194 | assert(pbuf[1] == c); |
| 7195 | if (fill != ' ') { |
| 7196 | *res++ = *pbuf++; |
| 7197 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7198 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7199 | rescnt -= 2; |
| 7200 | width -= 2; |
| 7201 | if (width < 0) |
| 7202 | width = 0; |
| 7203 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7204 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7205 | if (width > len && !(flags & F_LJUST)) { |
| 7206 | do { |
| 7207 | --rescnt; |
| 7208 | *res++ = fill; |
| 7209 | } while (--width > len); |
| 7210 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7211 | if (fill == ' ') { |
| 7212 | if (sign) |
| 7213 | *res++ = sign; |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7214 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7215 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7216 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7217 | *res++ = *pbuf++; |
| 7218 | *res++ = *pbuf++; |
| 7219 | } |
| 7220 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7221 | Py_UNICODE_COPY(res, pbuf, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7222 | res += len; |
| 7223 | rescnt -= len; |
| 7224 | while (--width >= len) { |
| 7225 | --rescnt; |
| 7226 | *res++ = ' '; |
| 7227 | } |
| 7228 | if (dict && (argidx < arglen) && c != '%') { |
| 7229 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 7230 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7231 | Py_XDECREF(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7232 | goto onError; |
| 7233 | } |
| 7234 | Py_XDECREF(temp); |
| 7235 | } /* '%' */ |
| 7236 | } /* until end */ |
| 7237 | if (argidx < arglen && !dict) { |
| 7238 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 7239 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7240 | goto onError; |
| 7241 | } |
| 7242 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7243 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
| 7244 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7245 | if (args_owned) { |
| 7246 | Py_DECREF(args); |
| 7247 | } |
| 7248 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7249 | return (PyObject *)result; |
| 7250 | |
| 7251 | onError: |
| 7252 | Py_XDECREF(result); |
| 7253 | Py_DECREF(uformat); |
| 7254 | if (args_owned) { |
| 7255 | Py_DECREF(args); |
| 7256 | } |
| 7257 | return NULL; |
| 7258 | } |
| 7259 | |
| 7260 | static PyBufferProcs unicode_as_buffer = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7261 | (readbufferproc) unicode_buffer_getreadbuf, |
| 7262 | (writebufferproc) unicode_buffer_getwritebuf, |
| 7263 | (segcountproc) unicode_buffer_getsegcount, |
| 7264 | (charbufferproc) unicode_buffer_getcharbuf, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7265 | }; |
| 7266 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 7267 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7268 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 7269 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7270 | static PyObject * |
| 7271 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 7272 | { |
| 7273 | PyObject *x = NULL; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 7274 | static char *kwlist[] = {"string", "encoding", "errors", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7275 | char *encoding = NULL; |
| 7276 | char *errors = NULL; |
| 7277 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7278 | if (type != &PyUnicode_Type) |
| 7279 | return unicode_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7280 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
| 7281 | kwlist, &x, &encoding, &errors)) |
| 7282 | return NULL; |
| 7283 | if (x == NULL) |
| 7284 | return (PyObject *)_PyUnicode_New(0); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 7285 | if (encoding == NULL && errors == NULL) |
| 7286 | return PyObject_Unicode(x); |
| 7287 | else |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7288 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
| 7289 | } |
| 7290 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7291 | static PyObject * |
| 7292 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 7293 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7294 | PyUnicodeObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7295 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7296 | |
| 7297 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 7298 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 7299 | if (tmp == NULL) |
| 7300 | return NULL; |
| 7301 | assert(PyUnicode_Check(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7302 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7303 | if (pnew == NULL) { |
| 7304 | Py_DECREF(tmp); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7305 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7306 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7307 | pnew->str = PyMem_NEW(Py_UNICODE, n+1); |
| 7308 | if (pnew->str == NULL) { |
| 7309 | _Py_ForgetReference((PyObject *)pnew); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 7310 | PyObject_Del(pnew); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7311 | Py_DECREF(tmp); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 7312 | return PyErr_NoMemory(); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7313 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7314 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 7315 | pnew->length = n; |
| 7316 | pnew->hash = tmp->hash; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7317 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7318 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7319 | } |
| 7320 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7321 | PyDoc_STRVAR(unicode_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7322 | "unicode(string [, encoding[, errors]]) -> object\n\ |
| 7323 | \n\ |
| 7324 | Create a new Unicode object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 7325 | encoding defaults to the current default string encoding.\n\ |
| 7326 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7327 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7328 | PyTypeObject PyUnicode_Type = { |
| 7329 | PyObject_HEAD_INIT(&PyType_Type) |
| 7330 | 0, /* ob_size */ |
| 7331 | "unicode", /* tp_name */ |
| 7332 | sizeof(PyUnicodeObject), /* tp_size */ |
| 7333 | 0, /* tp_itemsize */ |
| 7334 | /* Slots */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 7335 | (destructor)unicode_dealloc, /* tp_dealloc */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7336 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7337 | 0, /* tp_getattr */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7338 | 0, /* tp_setattr */ |
| 7339 | (cmpfunc) unicode_compare, /* tp_compare */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 7340 | unicode_repr, /* tp_repr */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7341 | &unicode_as_number, /* tp_as_number */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7342 | &unicode_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7343 | &unicode_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7344 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 7345 | 0, /* tp_call*/ |
| 7346 | (reprfunc) unicode_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7347 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 7348 | 0, /* tp_setattro */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7349 | &unicode_as_buffer, /* tp_as_buffer */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7350 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
| 7351 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7352 | unicode_doc, /* tp_doc */ |
| 7353 | 0, /* tp_traverse */ |
| 7354 | 0, /* tp_clear */ |
| 7355 | 0, /* tp_richcompare */ |
| 7356 | 0, /* tp_weaklistoffset */ |
| 7357 | 0, /* tp_iter */ |
| 7358 | 0, /* tp_iternext */ |
| 7359 | unicode_methods, /* tp_methods */ |
| 7360 | 0, /* tp_members */ |
| 7361 | 0, /* tp_getset */ |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 7362 | &PyBaseString_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7363 | 0, /* tp_dict */ |
| 7364 | 0, /* tp_descr_get */ |
| 7365 | 0, /* tp_descr_set */ |
| 7366 | 0, /* tp_dictoffset */ |
| 7367 | 0, /* tp_init */ |
| 7368 | 0, /* tp_alloc */ |
| 7369 | unicode_new, /* tp_new */ |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 7370 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7371 | }; |
| 7372 | |
| 7373 | /* Initialize the Unicode implementation */ |
| 7374 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 7375 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7376 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7377 | int i; |
| 7378 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7379 | /* Init the implementation */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7380 | unicode_freelist = NULL; |
| 7381 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7382 | unicode_empty = _PyUnicode_New(0); |
Marc-André Lemburg | 90e8147 | 2000-06-07 09:13:21 +0000 | [diff] [blame] | 7383 | strcpy(unicode_default_encoding, "ascii"); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7384 | for (i = 0; i < 256; i++) |
| 7385 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 7386 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 7387 | Py_FatalError("Can't initialize 'unicode'"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7388 | } |
| 7389 | |
| 7390 | /* Finalize the Unicode implementation */ |
| 7391 | |
| 7392 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 7393 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7394 | { |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 7395 | PyUnicodeObject *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7396 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7397 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 7398 | Py_XDECREF(unicode_empty); |
| 7399 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 7400 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7401 | for (i = 0; i < 256; i++) { |
| 7402 | if (unicode_latin1[i]) { |
| 7403 | Py_DECREF(unicode_latin1[i]); |
| 7404 | unicode_latin1[i] = NULL; |
| 7405 | } |
| 7406 | } |
| 7407 | |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 7408 | for (u = unicode_freelist; u != NULL;) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7409 | PyUnicodeObject *v = u; |
| 7410 | u = *(PyUnicodeObject **)u; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 7411 | if (v->str) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 7412 | PyMem_DEL(v->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 7413 | Py_XDECREF(v->defenc); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 7414 | PyObject_Del(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7415 | } |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7416 | unicode_freelist = NULL; |
| 7417 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7418 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 7419 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 7420 | #ifdef __cplusplus |
| 7421 | } |
| 7422 | #endif |
| 7423 | |
| 7424 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 7425 | /* |
| 7426 | Local variables: |
| 7427 | c-basic-offset: 4 |
| 7428 | indent-tabs-mode: nil |
| 7429 | End: |
| 7430 | */ |