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 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7 | Major speed upgrades to the method implementations at the Reykjavik |
| 8 | NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke. |
| 9 | |
Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 10 | Copyright (c) Corporation for National Research Initiatives. |
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 | -------------------------------------------------------------------- |
| 13 | The original string type implementation is: |
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 | Copyright (c) 1999 by Secret Labs AB |
| 16 | Copyright (c) 1999 by Fredrik Lundh |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 17 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 18 | By obtaining, using, and/or copying this software and/or its |
| 19 | associated documentation, you agree that you have read, understood, |
| 20 | and will comply with the following terms and conditions: |
| 21 | |
| 22 | Permission to use, copy, modify, and distribute this software and its |
| 23 | associated documentation for any purpose and without fee is hereby |
| 24 | granted, provided that the above copyright notice appears in all |
| 25 | copies, and that both that copyright notice and this permission notice |
| 26 | appear in supporting documentation, and that the name of Secret Labs |
| 27 | AB or the author not be used in advertising or publicity pertaining to |
| 28 | distribution of the software without specific, written prior |
| 29 | permission. |
| 30 | |
| 31 | SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 32 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 33 | FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR |
| 34 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 35 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 36 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 37 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 38 | -------------------------------------------------------------------- |
| 39 | |
| 40 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 41 | |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 42 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 43 | #include "Python.h" |
| 44 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 45 | #include "unicodeobject.h" |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 46 | #include "ucnhash.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 47 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 48 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 49 | #include <windows.h> |
| 50 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 52 | /* Limit for the Unicode object free list */ |
| 53 | |
| 54 | #define MAX_UNICODE_FREELIST_SIZE 1024 |
| 55 | |
| 56 | /* Limit for the Unicode object free list stay alive optimization. |
| 57 | |
| 58 | The implementation will keep allocated Unicode memory intact for |
| 59 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 60 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 61 | |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 62 | At worst this will result in MAX_UNICODE_FREELIST_SIZE * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 63 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 64 | malloc()-overhead) bytes of unused garbage. |
| 65 | |
| 66 | Setting the limit to 0 effectively turns the feature off. |
| 67 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 68 | Note: This is an experimental feature ! If you get core dumps when |
| 69 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 70 | |
| 71 | */ |
| 72 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 73 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 74 | |
| 75 | /* Endianness switches; defaults to little endian */ |
| 76 | |
| 77 | #ifdef WORDS_BIGENDIAN |
| 78 | # define BYTEORDER_IS_BIG_ENDIAN |
| 79 | #else |
| 80 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 81 | #endif |
| 82 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 83 | /* --- Globals ------------------------------------------------------------ |
| 84 | |
| 85 | The globals are initialized by the _PyUnicode_Init() API and should |
| 86 | not be used before calling that API. |
| 87 | |
| 88 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 89 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 90 | |
| 91 | #ifdef __cplusplus |
| 92 | extern "C" { |
| 93 | #endif |
| 94 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 95 | /* Free list for Unicode objects */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 96 | static PyUnicodeObject *unicode_freelist; |
| 97 | static int unicode_freelist_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 98 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 99 | /* The empty Unicode object is shared to improve performance. */ |
| 100 | static PyUnicodeObject *unicode_empty; |
| 101 | |
| 102 | /* Single character Unicode strings in the Latin-1 range are being |
| 103 | shared as well. */ |
| 104 | static PyUnicodeObject *unicode_latin1[256]; |
| 105 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 106 | /* Default encoding to use and assume when NULL is passed as encoding |
| 107 | parameter; it is initialized by _PyUnicode_Init(). |
| 108 | |
| 109 | Always use the PyUnicode_SetDefaultEncoding() and |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 110 | PyUnicode_GetDefaultEncoding() APIs to access this global. |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 111 | |
| 112 | */ |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 113 | static char unicode_default_encoding[100]; |
| 114 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 115 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 116 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 117 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 118 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 119 | return 0x10FFFF; |
| 120 | #else |
| 121 | /* This is actually an illegal character, so it should |
| 122 | not be passed to unichr. */ |
| 123 | return 0xFFFF; |
| 124 | #endif |
| 125 | } |
| 126 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 127 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 128 | |
| 129 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 130 | to keep things simple, we use a single bitmask, using the least 5 |
| 131 | bits from each unicode characters as the bit index. */ |
| 132 | |
| 133 | /* the linebreak mask is set up by Unicode_Init below */ |
| 134 | |
| 135 | #define BLOOM_MASK unsigned long |
| 136 | |
| 137 | static BLOOM_MASK bloom_linebreak; |
| 138 | |
| 139 | #define BLOOM(mask, ch) ((mask & (1 << ((ch) & 0x1F)))) |
| 140 | |
| 141 | #define BLOOM_LINEBREAK(ch)\ |
| 142 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK((ch))) |
| 143 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 144 | Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len) |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 145 | { |
| 146 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 147 | |
| 148 | long mask; |
| 149 | Py_ssize_t i; |
| 150 | |
| 151 | mask = 0; |
| 152 | for (i = 0; i < len; i++) |
| 153 | mask |= (1 << (ptr[i] & 0x1F)); |
| 154 | |
| 155 | return mask; |
| 156 | } |
| 157 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 158 | Py_LOCAL_INLINE(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen) |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 159 | { |
| 160 | Py_ssize_t i; |
| 161 | |
| 162 | for (i = 0; i < setlen; i++) |
| 163 | if (set[i] == chr) |
| 164 | return 1; |
| 165 | |
Fredrik Lundh | 7763351 | 2006-05-23 19:47:35 +0000 | [diff] [blame] | 166 | return 0; |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | #define BLOOM_MEMBER(mask, chr, set, setlen)\ |
| 170 | BLOOM(mask, chr) && unicode_member(chr, set, setlen) |
| 171 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 172 | /* --- Unicode Object ----------------------------------------------------- */ |
| 173 | |
| 174 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 175 | int unicode_resize(register PyUnicodeObject *unicode, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 176 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 177 | { |
| 178 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 179 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 180 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 181 | if (unicode->length == length) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 182 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 183 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 184 | /* Resizing shared object (unicode_empty or single character |
| 185 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 186 | instead ! */ |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 187 | |
Martin v. Löwis | 80d2e59 | 2006-04-13 06:06:08 +0000 | [diff] [blame] | 188 | if (unicode == unicode_empty || |
| 189 | (unicode->length == 1 && |
| 190 | unicode->str[0] < 256U && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 191 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 192 | PyErr_SetString(PyExc_SystemError, |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 193 | "can't resize shared unicode objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 194 | return -1; |
| 195 | } |
| 196 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 197 | /* We allocate one more byte to make sure the string is Ux0000 terminated. |
| 198 | The overallocation is also used by fastsearch, which assumes that it's |
Andrew M. Kuchling | 07bbfc6 | 2006-05-26 19:51:10 +0000 | [diff] [blame] | 199 | safe to look at str[length] (without making any assumptions about what |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 200 | it contains). */ |
| 201 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 202 | oldstr = unicode->str; |
| 203 | PyMem_RESIZE(unicode->str, Py_UNICODE, length + 1); |
| 204 | if (!unicode->str) { |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 205 | unicode->str = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 206 | PyErr_NoMemory(); |
| 207 | return -1; |
| 208 | } |
| 209 | unicode->str[length] = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 210 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 211 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 212 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 213 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 214 | if (unicode->defenc) { |
| 215 | Py_DECREF(unicode->defenc); |
| 216 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 217 | } |
| 218 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 219 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | /* We allocate one more byte to make sure the string is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 224 | Ux0000 terminated -- XXX is this needed ? |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 225 | |
| 226 | XXX This allocator could further be enhanced by assuring that the |
| 227 | free list never reduces its size below 1. |
| 228 | |
| 229 | */ |
| 230 | |
| 231 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 232 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 233 | { |
| 234 | register PyUnicodeObject *unicode; |
| 235 | |
Andrew Dalke | e0df762 | 2006-05-27 11:04:36 +0000 | [diff] [blame] | 236 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 237 | if (length == 0 && unicode_empty != NULL) { |
| 238 | Py_INCREF(unicode_empty); |
| 239 | return unicode_empty; |
| 240 | } |
| 241 | |
| 242 | /* Unicode freelist & memory allocation */ |
| 243 | if (unicode_freelist) { |
| 244 | unicode = unicode_freelist; |
Marc-André Lemburg | bea47e7 | 2000-06-17 20:31:17 +0000 | [diff] [blame] | 245 | unicode_freelist = *(PyUnicodeObject **)unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 246 | unicode_freelist_size--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 247 | if (unicode->str) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 248 | /* Keep-Alive optimization: we only upsize the buffer, |
| 249 | never downsize it. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 250 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 251 | unicode_resize(unicode, length) < 0) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 252 | PyMem_DEL(unicode->str); |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 253 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 254 | } |
| 255 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 256 | else { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 257 | unicode->str = PyMem_NEW(Py_UNICODE, length + 1); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 258 | } |
| 259 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 260 | } |
| 261 | else { |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 262 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 263 | if (unicode == NULL) |
| 264 | return NULL; |
| 265 | unicode->str = PyMem_NEW(Py_UNICODE, length + 1); |
| 266 | } |
| 267 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 268 | if (!unicode->str) { |
| 269 | PyErr_NoMemory(); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 270 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 271 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 272 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 273 | * the caller fails before initializing str -- unicode_resize() |
| 274 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 275 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 276 | * We don't want unicode_resize to read uninitialized memory in |
| 277 | * that case. |
| 278 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 279 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 280 | unicode->str[length] = 0; |
Martin v. Löwis | f15da69 | 2006-04-13 07:24:50 +0000 | [diff] [blame] | 281 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 282 | unicode->hash = -1; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 283 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 284 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 285 | |
| 286 | onError: |
| 287 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 288 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 289 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 293 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 294 | { |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 295 | if (PyUnicode_CheckExact(unicode) && |
| 296 | unicode_freelist_size < MAX_UNICODE_FREELIST_SIZE) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 297 | /* Keep-Alive optimization */ |
| 298 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 299 | PyMem_DEL(unicode->str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 300 | unicode->str = NULL; |
| 301 | unicode->length = 0; |
| 302 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 303 | if (unicode->defenc) { |
| 304 | Py_DECREF(unicode->defenc); |
| 305 | unicode->defenc = NULL; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 306 | } |
| 307 | /* Add to free list */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 308 | *(PyUnicodeObject **)unicode = unicode_freelist; |
| 309 | unicode_freelist = unicode; |
| 310 | unicode_freelist_size++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 311 | } |
| 312 | else { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 313 | PyMem_DEL(unicode->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 314 | Py_XDECREF(unicode->defenc); |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 315 | Py_Type(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 319 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 320 | { |
| 321 | register PyUnicodeObject *v; |
| 322 | |
| 323 | /* Argument checks */ |
| 324 | if (unicode == NULL) { |
| 325 | PyErr_BadInternalCall(); |
| 326 | return -1; |
| 327 | } |
| 328 | v = (PyUnicodeObject *)*unicode; |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 329 | if (v == NULL || !PyUnicode_Check(v) || Py_Refcnt(v) != 1 || length < 0) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 330 | PyErr_BadInternalCall(); |
| 331 | return -1; |
| 332 | } |
| 333 | |
| 334 | /* Resizing unicode_empty and single character objects is not |
| 335 | possible since these are being shared. We simply return a fresh |
| 336 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 337 | if (v->length != length && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 338 | (v == unicode_empty || v->length == 1)) { |
| 339 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 340 | if (w == NULL) |
| 341 | return -1; |
| 342 | Py_UNICODE_COPY(w->str, v->str, |
| 343 | length < v->length ? length : v->length); |
Raymond Hettinger | c8df578 | 2003-03-09 07:30:43 +0000 | [diff] [blame] | 344 | Py_DECREF(*unicode); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 345 | *unicode = (PyObject *)w; |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 350 | objects, since we can modify them in-place. */ |
| 351 | return unicode_resize(v, length); |
| 352 | } |
| 353 | |
| 354 | /* Internal API for use in unicodeobject.c only ! */ |
| 355 | #define _PyUnicode_Resize(unicodevar, length) \ |
| 356 | PyUnicode_Resize(((PyObject **)(unicodevar)), length) |
| 357 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 358 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 359 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 360 | { |
| 361 | PyUnicodeObject *unicode; |
| 362 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 363 | /* If the Unicode data is known at construction time, we can apply |
| 364 | some optimizations which share commonly used objects. */ |
| 365 | if (u != NULL) { |
| 366 | |
| 367 | /* Optimization for empty strings */ |
| 368 | if (size == 0 && unicode_empty != NULL) { |
| 369 | Py_INCREF(unicode_empty); |
| 370 | return (PyObject *)unicode_empty; |
| 371 | } |
| 372 | |
| 373 | /* Single character Unicode objects in the Latin-1 range are |
| 374 | shared when using this constructor */ |
| 375 | if (size == 1 && *u < 256) { |
| 376 | unicode = unicode_latin1[*u]; |
| 377 | if (!unicode) { |
| 378 | unicode = _PyUnicode_New(1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 379 | if (!unicode) |
| 380 | return NULL; |
Marc-André Lemburg | 8879a33 | 2001-06-07 12:26:56 +0000 | [diff] [blame] | 381 | unicode->str[0] = *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 382 | unicode_latin1[*u] = unicode; |
| 383 | } |
| 384 | Py_INCREF(unicode); |
| 385 | return (PyObject *)unicode; |
| 386 | } |
| 387 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 388 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 389 | unicode = _PyUnicode_New(size); |
| 390 | if (!unicode) |
| 391 | return NULL; |
| 392 | |
| 393 | /* Copy the Unicode data into the new object */ |
| 394 | if (u != NULL) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 395 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 396 | |
| 397 | return (PyObject *)unicode; |
| 398 | } |
| 399 | |
| 400 | #ifdef HAVE_WCHAR_H |
| 401 | |
| 402 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 403 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 404 | { |
| 405 | PyUnicodeObject *unicode; |
| 406 | |
| 407 | if (w == NULL) { |
| 408 | PyErr_BadInternalCall(); |
| 409 | return NULL; |
| 410 | } |
| 411 | |
| 412 | unicode = _PyUnicode_New(size); |
| 413 | if (!unicode) |
| 414 | return NULL; |
| 415 | |
| 416 | /* Copy the wchar_t data into the new object */ |
| 417 | #ifdef HAVE_USABLE_WCHAR_T |
| 418 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 419 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 420 | { |
| 421 | register Py_UNICODE *u; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 422 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 423 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 424 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 425 | *u++ = *w++; |
| 426 | } |
| 427 | #endif |
| 428 | |
| 429 | return (PyObject *)unicode; |
| 430 | } |
| 431 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 432 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 433 | wchar_t *w, |
| 434 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 435 | { |
| 436 | if (unicode == NULL) { |
| 437 | PyErr_BadInternalCall(); |
| 438 | return -1; |
| 439 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 440 | |
| 441 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 442 | if (size > PyUnicode_GET_SIZE(unicode)) |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 443 | size = PyUnicode_GET_SIZE(unicode) + 1; |
| 444 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 445 | #ifdef HAVE_USABLE_WCHAR_T |
| 446 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 447 | #else |
| 448 | { |
| 449 | register Py_UNICODE *u; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 450 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 451 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 452 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 453 | *w++ = *u++; |
| 454 | } |
| 455 | #endif |
| 456 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 457 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 458 | return PyUnicode_GET_SIZE(unicode); |
| 459 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 460 | return size; |
| 461 | } |
| 462 | |
| 463 | #endif |
| 464 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 465 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 466 | { |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 467 | Py_UNICODE s[1]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 468 | |
| 469 | #ifdef Py_UNICODE_WIDE |
| 470 | if (ordinal < 0 || ordinal > 0x10ffff) { |
| 471 | PyErr_SetString(PyExc_ValueError, |
| 472 | "unichr() arg not in range(0x110000) " |
| 473 | "(wide Python build)"); |
| 474 | return NULL; |
| 475 | } |
| 476 | #else |
| 477 | if (ordinal < 0 || ordinal > 0xffff) { |
| 478 | PyErr_SetString(PyExc_ValueError, |
| 479 | "unichr() arg not in range(0x10000) " |
| 480 | "(narrow Python build)"); |
| 481 | return NULL; |
| 482 | } |
| 483 | #endif |
| 484 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 485 | s[0] = (Py_UNICODE)ordinal; |
| 486 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 489 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 490 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 491 | /* XXX Perhaps we should make this API an alias of |
| 492 | PyObject_Unicode() instead ?! */ |
| 493 | if (PyUnicode_CheckExact(obj)) { |
| 494 | Py_INCREF(obj); |
| 495 | return obj; |
| 496 | } |
| 497 | if (PyUnicode_Check(obj)) { |
| 498 | /* For a Unicode subtype that's not a Unicode object, |
| 499 | return a true Unicode object with the same data. */ |
| 500 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 501 | PyUnicode_GET_SIZE(obj)); |
| 502 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 503 | return PyUnicode_FromEncodedObject(obj, NULL, "strict"); |
| 504 | } |
| 505 | |
| 506 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
| 507 | const char *encoding, |
| 508 | const char *errors) |
| 509 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 510 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 511 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 512 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 513 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 514 | if (obj == NULL) { |
| 515 | PyErr_BadInternalCall(); |
| 516 | return NULL; |
| 517 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 518 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 519 | #if 0 |
| 520 | /* For b/w compatibility we also accept Unicode objects provided |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 521 | that no encodings is given and then redirect to |
| 522 | PyObject_Unicode() which then applies the additional logic for |
| 523 | Unicode subclasses. |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 524 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 525 | NOTE: This API should really only be used for object which |
| 526 | represent *encoded* Unicode ! |
| 527 | |
| 528 | */ |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 529 | if (PyUnicode_Check(obj)) { |
| 530 | if (encoding) { |
| 531 | PyErr_SetString(PyExc_TypeError, |
| 532 | "decoding Unicode is not supported"); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 533 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 534 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 535 | return PyObject_Unicode(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 536 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 537 | #else |
| 538 | if (PyUnicode_Check(obj)) { |
| 539 | PyErr_SetString(PyExc_TypeError, |
| 540 | "decoding Unicode is not supported"); |
| 541 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 542 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 543 | #endif |
| 544 | |
| 545 | /* Coerce object */ |
| 546 | if (PyString_Check(obj)) { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 547 | s = PyString_AS_STRING(obj); |
| 548 | len = PyString_GET_SIZE(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 549 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 550 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
| 551 | /* Overwrite the error message with something more useful in |
| 552 | case of a TypeError. */ |
| 553 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 554 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 555 | "coercing to Unicode: need string or buffer, " |
| 556 | "%.80s found", |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 557 | Py_Type(obj)->tp_name); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 558 | goto onError; |
| 559 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 560 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 561 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 562 | if (len == 0) { |
| 563 | Py_INCREF(unicode_empty); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 564 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 565 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 566 | else |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 567 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 568 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 569 | return v; |
| 570 | |
| 571 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 572 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | PyObject *PyUnicode_Decode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 576 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 577 | const char *encoding, |
| 578 | const char *errors) |
| 579 | { |
| 580 | PyObject *buffer = NULL, *unicode; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 581 | |
| 582 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 583 | encoding = PyUnicode_GetDefaultEncoding(); |
| 584 | |
| 585 | /* Shortcuts for common default encodings */ |
| 586 | if (strcmp(encoding, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 587 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 588 | else if (strcmp(encoding, "latin-1") == 0) |
| 589 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 590 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 591 | else if (strcmp(encoding, "mbcs") == 0) |
| 592 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 593 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 594 | else if (strcmp(encoding, "ascii") == 0) |
| 595 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 596 | |
| 597 | /* Decode via the codec registry */ |
| 598 | buffer = PyBuffer_FromMemory((void *)s, size); |
| 599 | if (buffer == NULL) |
| 600 | goto onError; |
| 601 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 602 | if (unicode == NULL) |
| 603 | goto onError; |
| 604 | if (!PyUnicode_Check(unicode)) { |
| 605 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 606 | "decoder did not return an unicode object (type=%.400s)", |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 607 | Py_Type(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 608 | Py_DECREF(unicode); |
| 609 | goto onError; |
| 610 | } |
| 611 | Py_DECREF(buffer); |
| 612 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 613 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 614 | onError: |
| 615 | Py_XDECREF(buffer); |
| 616 | return NULL; |
| 617 | } |
| 618 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 619 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 620 | const char *encoding, |
| 621 | const char *errors) |
| 622 | { |
| 623 | PyObject *v; |
| 624 | |
| 625 | if (!PyUnicode_Check(unicode)) { |
| 626 | PyErr_BadArgument(); |
| 627 | goto onError; |
| 628 | } |
| 629 | |
| 630 | if (encoding == NULL) |
| 631 | encoding = PyUnicode_GetDefaultEncoding(); |
| 632 | |
| 633 | /* Decode via the codec registry */ |
| 634 | v = PyCodec_Decode(unicode, encoding, errors); |
| 635 | if (v == NULL) |
| 636 | goto onError; |
| 637 | return v; |
| 638 | |
| 639 | onError: |
| 640 | return NULL; |
| 641 | } |
| 642 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 643 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 644 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 645 | const char *encoding, |
| 646 | const char *errors) |
| 647 | { |
| 648 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 649 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 650 | unicode = PyUnicode_FromUnicode(s, size); |
| 651 | if (unicode == NULL) |
| 652 | return NULL; |
| 653 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 654 | Py_DECREF(unicode); |
| 655 | return v; |
| 656 | } |
| 657 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 658 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 659 | const char *encoding, |
| 660 | const char *errors) |
| 661 | { |
| 662 | PyObject *v; |
| 663 | |
| 664 | if (!PyUnicode_Check(unicode)) { |
| 665 | PyErr_BadArgument(); |
| 666 | goto onError; |
| 667 | } |
| 668 | |
| 669 | if (encoding == NULL) |
| 670 | encoding = PyUnicode_GetDefaultEncoding(); |
| 671 | |
| 672 | /* Encode via the codec registry */ |
| 673 | v = PyCodec_Encode(unicode, encoding, errors); |
| 674 | if (v == NULL) |
| 675 | goto onError; |
| 676 | return v; |
| 677 | |
| 678 | onError: |
| 679 | return NULL; |
| 680 | } |
| 681 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 682 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 683 | const char *encoding, |
| 684 | const char *errors) |
| 685 | { |
| 686 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 687 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 688 | if (!PyUnicode_Check(unicode)) { |
| 689 | PyErr_BadArgument(); |
| 690 | goto onError; |
| 691 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 692 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 693 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 694 | encoding = PyUnicode_GetDefaultEncoding(); |
| 695 | |
| 696 | /* Shortcuts for common default encodings */ |
| 697 | if (errors == NULL) { |
| 698 | if (strcmp(encoding, "utf-8") == 0) |
Jeremy Hylton | 9cea41c | 2001-05-29 17:13:15 +0000 | [diff] [blame] | 699 | return PyUnicode_AsUTF8String(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 700 | else if (strcmp(encoding, "latin-1") == 0) |
| 701 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 702 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 703 | else if (strcmp(encoding, "mbcs") == 0) |
| 704 | return PyUnicode_AsMBCSString(unicode); |
| 705 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 706 | else if (strcmp(encoding, "ascii") == 0) |
| 707 | return PyUnicode_AsASCIIString(unicode); |
| 708 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 709 | |
| 710 | /* Encode via the codec registry */ |
| 711 | v = PyCodec_Encode(unicode, encoding, errors); |
| 712 | if (v == NULL) |
| 713 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 714 | if (!PyString_Check(v)) { |
| 715 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 716 | "encoder did not return a string object (type=%.400s)", |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 717 | Py_Type(v)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 718 | Py_DECREF(v); |
| 719 | goto onError; |
| 720 | } |
| 721 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 722 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 723 | onError: |
| 724 | return NULL; |
| 725 | } |
| 726 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 727 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 728 | const char *errors) |
| 729 | { |
| 730 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
| 731 | |
| 732 | if (v) |
| 733 | return v; |
| 734 | v = PyUnicode_AsEncodedString(unicode, NULL, errors); |
| 735 | if (v && errors == NULL) |
| 736 | ((PyUnicodeObject *)unicode)->defenc = v; |
| 737 | return v; |
| 738 | } |
| 739 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 740 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 741 | { |
| 742 | if (!PyUnicode_Check(unicode)) { |
| 743 | PyErr_BadArgument(); |
| 744 | goto onError; |
| 745 | } |
| 746 | return PyUnicode_AS_UNICODE(unicode); |
| 747 | |
| 748 | onError: |
| 749 | return NULL; |
| 750 | } |
| 751 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 752 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 753 | { |
| 754 | if (!PyUnicode_Check(unicode)) { |
| 755 | PyErr_BadArgument(); |
| 756 | goto onError; |
| 757 | } |
| 758 | return PyUnicode_GET_SIZE(unicode); |
| 759 | |
| 760 | onError: |
| 761 | return -1; |
| 762 | } |
| 763 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 764 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 765 | { |
| 766 | return unicode_default_encoding; |
| 767 | } |
| 768 | |
| 769 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 770 | { |
| 771 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 772 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 773 | /* Make sure the encoding is valid. As side effect, this also |
| 774 | loads the encoding into the codec registry cache. */ |
| 775 | v = _PyCodec_Lookup(encoding); |
| 776 | if (v == NULL) |
| 777 | goto onError; |
| 778 | Py_DECREF(v); |
| 779 | strncpy(unicode_default_encoding, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 780 | encoding, |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 781 | sizeof(unicode_default_encoding)); |
| 782 | return 0; |
| 783 | |
| 784 | onError: |
| 785 | return -1; |
| 786 | } |
| 787 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 788 | /* error handling callback helper: |
| 789 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 790 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 791 | and adjust various state variables. |
| 792 | return 0 on success, -1 on error |
| 793 | */ |
| 794 | |
| 795 | static |
| 796 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 797 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 798 | const char *input, Py_ssize_t insize, Py_ssize_t *startinpos, Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 799 | PyObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 800 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 801 | 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] | 802 | |
| 803 | PyObject *restuple = NULL; |
| 804 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 805 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
| 806 | Py_ssize_t requiredsize; |
| 807 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 808 | Py_UNICODE *repptr; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 809 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 810 | int res = -1; |
| 811 | |
| 812 | if (*errorHandler == NULL) { |
| 813 | *errorHandler = PyCodec_LookupError(errors); |
| 814 | if (*errorHandler == NULL) |
| 815 | goto onError; |
| 816 | } |
| 817 | |
| 818 | if (*exceptionObject == NULL) { |
| 819 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 820 | encoding, input, insize, *startinpos, *endinpos, reason); |
| 821 | if (*exceptionObject == NULL) |
| 822 | goto onError; |
| 823 | } |
| 824 | else { |
| 825 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 826 | goto onError; |
| 827 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 828 | goto onError; |
| 829 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 830 | goto onError; |
| 831 | } |
| 832 | |
| 833 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 834 | if (restuple == NULL) |
| 835 | goto onError; |
| 836 | if (!PyTuple_Check(restuple)) { |
| 837 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 838 | goto onError; |
| 839 | } |
| 840 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 841 | goto onError; |
| 842 | if (newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 843 | newpos = insize+newpos; |
| 844 | if (newpos<0 || newpos>insize) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 845 | 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] | 846 | goto onError; |
| 847 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 848 | |
| 849 | /* need more space? (at least enough for what we |
| 850 | have+the replacement+the rest of the string (starting |
| 851 | at the new input position), so we won't have to check space |
| 852 | when there are no errors in the rest of the string) */ |
| 853 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 854 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 855 | requiredsize = *outpos + repsize + insize-newpos; |
| 856 | if (requiredsize > outsize) { |
| 857 | if (requiredsize<2*outsize) |
| 858 | requiredsize = 2*outsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 859 | if (PyUnicode_Resize(output, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 860 | goto onError; |
| 861 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
| 862 | } |
| 863 | *endinpos = newpos; |
| 864 | *inptr = input + newpos; |
| 865 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 866 | *outptr += repsize; |
| 867 | *outpos += repsize; |
| 868 | /* we made it! */ |
| 869 | res = 0; |
| 870 | |
| 871 | onError: |
| 872 | Py_XDECREF(restuple); |
| 873 | return res; |
| 874 | } |
| 875 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 876 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 877 | |
| 878 | /* see RFC2152 for details */ |
| 879 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 880 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 881 | char utf7_special[128] = { |
| 882 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 883 | encoded: |
| 884 | 0 - not special |
| 885 | 1 - special |
| 886 | 2 - whitespace (optional) |
| 887 | 3 - RFC2152 Set O (optional) */ |
| 888 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 889 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 890 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 891 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 892 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 893 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 894 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 895 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 896 | |
| 897 | }; |
| 898 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 899 | /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
| 900 | warnings about the comparison always being false; since |
| 901 | utf7_special[0] is 1, we can safely make that one comparison |
| 902 | true */ |
| 903 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 904 | #define SPECIAL(c, encodeO, encodeWS) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 905 | ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 906 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 907 | (encodeO && (utf7_special[(c)] == 3))) |
| 908 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 909 | #define B64(n) \ |
| 910 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 911 | #define B64CHAR(c) \ |
| 912 | (isalnum(c) || (c) == '+' || (c) == '/') |
| 913 | #define UB64(c) \ |
| 914 | ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
| 915 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 916 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 917 | #define ENCODE(out, ch, bits) \ |
| 918 | while (bits >= 6) { \ |
| 919 | *out++ = B64(ch >> (bits-6)); \ |
| 920 | bits -= 6; \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 921 | } |
| 922 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 923 | #define DECODE(out, ch, bits, surrogate) \ |
| 924 | while (bits >= 16) { \ |
| 925 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 926 | bits -= 16; \ |
| 927 | if (surrogate) { \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 928 | /* We have already generated an error for the high surrogate \ |
| 929 | 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] | 930 | surrogate = 0; \ |
| 931 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 932 | /* This is a surrogate pair. Unfortunately we can't represent \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 933 | it in a 16-bit character */ \ |
| 934 | surrogate = 1; \ |
| 935 | errmsg = "code pairs are not supported"; \ |
| 936 | goto utf7Error; \ |
| 937 | } else { \ |
| 938 | *out++ = outCh; \ |
| 939 | } \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 940 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 941 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 942 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 943 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 944 | const char *errors) |
| 945 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 946 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 947 | Py_ssize_t startinpos; |
| 948 | Py_ssize_t endinpos; |
| 949 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 950 | const char *e; |
| 951 | PyUnicodeObject *unicode; |
| 952 | Py_UNICODE *p; |
| 953 | const char *errmsg = ""; |
| 954 | int inShift = 0; |
| 955 | unsigned int bitsleft = 0; |
| 956 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 957 | int surrogate = 0; |
| 958 | PyObject *errorHandler = NULL; |
| 959 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 960 | |
| 961 | unicode = _PyUnicode_New(size); |
| 962 | if (!unicode) |
| 963 | return NULL; |
| 964 | if (size == 0) |
| 965 | return (PyObject *)unicode; |
| 966 | |
| 967 | p = unicode->str; |
| 968 | e = s + size; |
| 969 | |
| 970 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 971 | Py_UNICODE ch; |
| 972 | restart: |
| 973 | ch = *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 974 | |
| 975 | if (inShift) { |
| 976 | if ((ch == '-') || !B64CHAR(ch)) { |
| 977 | inShift = 0; |
| 978 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 979 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 980 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 981 | if (bitsleft >= 6) { |
| 982 | /* The shift sequence has a partial character in it. If |
| 983 | bitsleft < 6 then we could just classify it as padding |
| 984 | but that is not the case here */ |
| 985 | |
| 986 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 987 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 988 | } |
| 989 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 990 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 991 | here so indicate the potential of a misencoded character. */ |
| 992 | |
| 993 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 994 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 995 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 996 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | if (ch == '-') { |
| 1000 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1001 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1002 | inShift = 1; |
| 1003 | } |
| 1004 | } else if (SPECIAL(ch,0,0)) { |
| 1005 | errmsg = "unexpected special character"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1006 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1007 | } else { |
| 1008 | *p++ = ch; |
| 1009 | } |
| 1010 | } else { |
| 1011 | charsleft = (charsleft << 6) | UB64(ch); |
| 1012 | bitsleft += 6; |
| 1013 | s++; |
| 1014 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1015 | } |
| 1016 | } |
| 1017 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1018 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1019 | s++; |
| 1020 | if (s < e && *s == '-') { |
| 1021 | s++; |
| 1022 | *p++ = '+'; |
| 1023 | } else |
| 1024 | { |
| 1025 | inShift = 1; |
| 1026 | bitsleft = 0; |
| 1027 | } |
| 1028 | } |
| 1029 | else if (SPECIAL(ch,0,0)) { |
| 1030 | errmsg = "unexpected special character"; |
| 1031 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1032 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1033 | } |
| 1034 | else { |
| 1035 | *p++ = ch; |
| 1036 | s++; |
| 1037 | } |
| 1038 | continue; |
| 1039 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1040 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1041 | endinpos = s-starts; |
| 1042 | if (unicode_decode_call_errorhandler( |
| 1043 | errors, &errorHandler, |
| 1044 | "utf7", errmsg, |
| 1045 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1046 | (PyObject **)&unicode, &outpos, &p)) |
| 1047 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | if (inShift) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1051 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1052 | endinpos = size; |
| 1053 | if (unicode_decode_call_errorhandler( |
| 1054 | errors, &errorHandler, |
| 1055 | "utf7", "unterminated shift sequence", |
| 1056 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1057 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1058 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1059 | if (s < e) |
| 1060 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1063 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1064 | goto onError; |
| 1065 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1066 | Py_XDECREF(errorHandler); |
| 1067 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1068 | return (PyObject *)unicode; |
| 1069 | |
| 1070 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1071 | Py_XDECREF(errorHandler); |
| 1072 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1073 | Py_DECREF(unicode); |
| 1074 | return NULL; |
| 1075 | } |
| 1076 | |
| 1077 | |
| 1078 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1079 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1080 | int encodeSetO, |
| 1081 | int encodeWhiteSpace, |
| 1082 | const char *errors) |
| 1083 | { |
| 1084 | PyObject *v; |
| 1085 | /* It might be possible to tighten this worst case */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1086 | Py_ssize_t cbAllocated = 5 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1087 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1088 | Py_ssize_t i = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1089 | unsigned int bitsleft = 0; |
| 1090 | unsigned long charsleft = 0; |
| 1091 | char * out; |
| 1092 | char * start; |
| 1093 | |
| 1094 | if (size == 0) |
| 1095 | return PyString_FromStringAndSize(NULL, 0); |
| 1096 | |
| 1097 | v = PyString_FromStringAndSize(NULL, cbAllocated); |
| 1098 | if (v == NULL) |
| 1099 | return NULL; |
| 1100 | |
| 1101 | start = out = PyString_AS_STRING(v); |
| 1102 | for (;i < size; ++i) { |
| 1103 | Py_UNICODE ch = s[i]; |
| 1104 | |
| 1105 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1106 | if (ch == '+') { |
| 1107 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1108 | *out++ = '-'; |
| 1109 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1110 | charsleft = ch; |
| 1111 | bitsleft = 16; |
| 1112 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1113 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1114 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1115 | } else { |
| 1116 | *out++ = (char) ch; |
| 1117 | } |
| 1118 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1119 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1120 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1121 | charsleft = 0; |
| 1122 | bitsleft = 0; |
| 1123 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1124 | so no '-' is required, except if the character is itself a '-' */ |
| 1125 | if (B64CHAR(ch) || ch == '-') { |
| 1126 | *out++ = '-'; |
| 1127 | } |
| 1128 | inShift = 0; |
| 1129 | *out++ = (char) ch; |
| 1130 | } else { |
| 1131 | bitsleft += 16; |
| 1132 | charsleft = (charsleft << 16) | ch; |
| 1133 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1134 | |
| 1135 | /* If the next character is special then we dont' need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1136 | 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] | 1137 | or '-' then the shift sequence will be terminated implicitly and we |
| 1138 | don't have to insert a '-'. */ |
| 1139 | |
| 1140 | if (bitsleft == 0) { |
| 1141 | if (i + 1 < size) { |
| 1142 | Py_UNICODE ch2 = s[i+1]; |
| 1143 | |
| 1144 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1145 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1146 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1147 | *out++ = '-'; |
| 1148 | inShift = 0; |
| 1149 | } else { |
| 1150 | inShift = 0; |
| 1151 | } |
| 1152 | |
| 1153 | } |
| 1154 | else { |
| 1155 | *out++ = '-'; |
| 1156 | inShift = 0; |
| 1157 | } |
| 1158 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1159 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1160 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1161 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1162 | if (bitsleft) { |
| 1163 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1164 | *out++ = '-'; |
| 1165 | } |
| 1166 | |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 1167 | _PyString_Resize(&v, out - start); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1168 | return v; |
| 1169 | } |
| 1170 | |
| 1171 | #undef SPECIAL |
| 1172 | #undef B64 |
| 1173 | #undef B64CHAR |
| 1174 | #undef UB64 |
| 1175 | #undef ENCODE |
| 1176 | #undef DECODE |
| 1177 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1178 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1179 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1180 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1181 | char utf8_code_length[256] = { |
| 1182 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1183 | illegal prefix. see RFC 2279 for details */ |
| 1184 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1185 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1186 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1187 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1188 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1189 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1190 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1191 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1192 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1193 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1194 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1195 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1196 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1197 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1198 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1199 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1200 | }; |
| 1201 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1202 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1203 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1204 | const char *errors) |
| 1205 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1206 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 1207 | } |
| 1208 | |
| 1209 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1210 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1211 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1212 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1213 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1214 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1215 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1216 | Py_ssize_t startinpos; |
| 1217 | Py_ssize_t endinpos; |
| 1218 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1219 | const char *e; |
| 1220 | PyUnicodeObject *unicode; |
| 1221 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1222 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1223 | PyObject *errorHandler = NULL; |
| 1224 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1225 | |
| 1226 | /* Note: size will always be longer than the resulting Unicode |
| 1227 | character count */ |
| 1228 | unicode = _PyUnicode_New(size); |
| 1229 | if (!unicode) |
| 1230 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1231 | if (size == 0) { |
| 1232 | if (consumed) |
| 1233 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1234 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1235 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1236 | |
| 1237 | /* Unpack UTF-8 encoded data */ |
| 1238 | p = unicode->str; |
| 1239 | e = s + size; |
| 1240 | |
| 1241 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1242 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1243 | |
| 1244 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1245 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1246 | s++; |
| 1247 | continue; |
| 1248 | } |
| 1249 | |
| 1250 | n = utf8_code_length[ch]; |
| 1251 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1252 | if (s + n > e) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1253 | if (consumed) |
| 1254 | break; |
| 1255 | else { |
| 1256 | errmsg = "unexpected end of data"; |
| 1257 | startinpos = s-starts; |
| 1258 | endinpos = size; |
| 1259 | goto utf8Error; |
| 1260 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1261 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1262 | |
| 1263 | switch (n) { |
| 1264 | |
| 1265 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1266 | errmsg = "unexpected code byte"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1267 | startinpos = s-starts; |
| 1268 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1269 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1270 | |
| 1271 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1272 | errmsg = "internal error"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1273 | startinpos = s-starts; |
| 1274 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1275 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1276 | |
| 1277 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1278 | if ((s[1] & 0xc0) != 0x80) { |
| 1279 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1280 | startinpos = s-starts; |
| 1281 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1282 | goto utf8Error; |
| 1283 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1284 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1285 | if (ch < 0x80) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1286 | startinpos = s-starts; |
| 1287 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1288 | errmsg = "illegal encoding"; |
| 1289 | goto utf8Error; |
| 1290 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1291 | else |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1292 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1293 | break; |
| 1294 | |
| 1295 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1296 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1297 | (s[2] & 0xc0) != 0x80) { |
| 1298 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1299 | startinpos = s-starts; |
| 1300 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1301 | goto utf8Error; |
| 1302 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1303 | 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] | 1304 | if (ch < 0x0800) { |
| 1305 | /* Note: UTF-8 encodings of surrogates are considered |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1306 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1307 | |
| 1308 | XXX For wide builds (UCS-4) we should probably try |
| 1309 | to recombine the surrogates into a single code |
| 1310 | unit. |
| 1311 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1312 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1313 | startinpos = s-starts; |
| 1314 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1315 | goto utf8Error; |
| 1316 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1317 | else |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1318 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1319 | break; |
| 1320 | |
| 1321 | case 4: |
| 1322 | if ((s[1] & 0xc0) != 0x80 || |
| 1323 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1324 | (s[3] & 0xc0) != 0x80) { |
| 1325 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1326 | startinpos = s-starts; |
| 1327 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1328 | goto utf8Error; |
| 1329 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1330 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 1331 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 1332 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1333 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1334 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1335 | || (ch > 0x10ffff)) /* maximum value allowed for |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1336 | UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1337 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1338 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1339 | startinpos = s-starts; |
| 1340 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1341 | goto utf8Error; |
| 1342 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1343 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1344 | *p++ = (Py_UNICODE)ch; |
| 1345 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1346 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1347 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1348 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 1349 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1350 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1351 | /* high surrogate = top 10 bits added to D800 */ |
| 1352 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1353 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1354 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1355 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1356 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1357 | break; |
| 1358 | |
| 1359 | default: |
| 1360 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1361 | errmsg = "unsupported Unicode code range"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1362 | startinpos = s-starts; |
| 1363 | endinpos = startinpos+n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1364 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1365 | } |
| 1366 | s += n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1367 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1368 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1369 | utf8Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1370 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1371 | if (unicode_decode_call_errorhandler( |
| 1372 | errors, &errorHandler, |
| 1373 | "utf8", errmsg, |
| 1374 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1375 | (PyObject **)&unicode, &outpos, &p)) |
| 1376 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1377 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1378 | if (consumed) |
| 1379 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1380 | |
| 1381 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1382 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1383 | goto onError; |
| 1384 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1385 | Py_XDECREF(errorHandler); |
| 1386 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1387 | return (PyObject *)unicode; |
| 1388 | |
| 1389 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1390 | Py_XDECREF(errorHandler); |
| 1391 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1392 | Py_DECREF(unicode); |
| 1393 | return NULL; |
| 1394 | } |
| 1395 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1396 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 1397 | and allocate exactly as much space needed at the end. Else allocate the |
| 1398 | maximum possible needed (4 result bytes per Unicode character), and return |
| 1399 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1400 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1401 | PyObject * |
| 1402 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1403 | Py_ssize_t size, |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1404 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1405 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1406 | #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] | 1407 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1408 | Py_ssize_t i; /* index into s of next input byte */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1409 | PyObject *v; /* result string object */ |
| 1410 | char *p; /* next free byte in output buffer */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1411 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1412 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1413 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1414 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1415 | assert(s != NULL); |
| 1416 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1417 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1418 | if (size <= MAX_SHORT_UNICHARS) { |
| 1419 | /* Write into the stack buffer; nallocated can't overflow. |
| 1420 | * At the end, we'll allocate exactly as much heap space as it |
| 1421 | * turns out we need. |
| 1422 | */ |
| 1423 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
| 1424 | v = NULL; /* will allocate after we're done */ |
| 1425 | p = stackbuf; |
| 1426 | } |
| 1427 | else { |
| 1428 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 1429 | nallocated = size * 4; |
| 1430 | if (nallocated / 4 != size) /* overflow! */ |
| 1431 | return PyErr_NoMemory(); |
| 1432 | v = PyString_FromStringAndSize(NULL, nallocated); |
| 1433 | if (v == NULL) |
| 1434 | return NULL; |
| 1435 | p = PyString_AS_STRING(v); |
| 1436 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1437 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1438 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1439 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1440 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1441 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1442 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1443 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1444 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1445 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1446 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 1447 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 1448 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1449 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1450 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1451 | /* Encode UCS2 Unicode ordinals */ |
| 1452 | if (ch < 0x10000) { |
| 1453 | /* Special case: check for high surrogate */ |
| 1454 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 1455 | Py_UCS4 ch2 = s[i]; |
| 1456 | /* Check for low surrogate and combine the two to |
| 1457 | form a UCS4 value */ |
| 1458 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1459 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1460 | i++; |
| 1461 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1462 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1463 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1464 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1465 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1466 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1467 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1468 | continue; |
| 1469 | } |
| 1470 | encodeUCS4: |
| 1471 | /* Encode UCS4 Unicode ordinals */ |
| 1472 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 1473 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 1474 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1475 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1476 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1477 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 1478 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1479 | if (v == NULL) { |
| 1480 | /* This was stack allocated. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1481 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1482 | assert(nneeded <= nallocated); |
| 1483 | v = PyString_FromStringAndSize(stackbuf, nneeded); |
| 1484 | } |
| 1485 | else { |
| 1486 | /* Cut back to size actually needed. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1487 | nneeded = p - PyString_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1488 | assert(nneeded <= nallocated); |
| 1489 | _PyString_Resize(&v, nneeded); |
| 1490 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1491 | return v; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1492 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1493 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1496 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 1497 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1498 | if (!PyUnicode_Check(unicode)) { |
| 1499 | PyErr_BadArgument(); |
| 1500 | return NULL; |
| 1501 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 1502 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1503 | PyUnicode_GET_SIZE(unicode), |
| 1504 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 1507 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 1508 | |
| 1509 | PyObject * |
| 1510 | PyUnicode_DecodeUTF32(const char *s, |
| 1511 | Py_ssize_t size, |
| 1512 | const char *errors, |
| 1513 | int *byteorder) |
| 1514 | { |
| 1515 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 1516 | } |
| 1517 | |
| 1518 | PyObject * |
| 1519 | PyUnicode_DecodeUTF32Stateful(const char *s, |
| 1520 | Py_ssize_t size, |
| 1521 | const char *errors, |
| 1522 | int *byteorder, |
| 1523 | Py_ssize_t *consumed) |
| 1524 | { |
| 1525 | const char *starts = s; |
| 1526 | Py_ssize_t startinpos; |
| 1527 | Py_ssize_t endinpos; |
| 1528 | Py_ssize_t outpos; |
| 1529 | PyUnicodeObject *unicode; |
| 1530 | Py_UNICODE *p; |
| 1531 | #ifndef Py_UNICODE_WIDE |
| 1532 | int i, pairs; |
| 1533 | #else |
| 1534 | const int pairs = 0; |
| 1535 | #endif |
| 1536 | const unsigned char *q, *e; |
| 1537 | int bo = 0; /* assume native ordering by default */ |
| 1538 | const char *errmsg = ""; |
Walter Dörwald | 20b40d3 | 2007-08-17 16:52:50 +0000 | [diff] [blame^] | 1539 | /* Offsets from q for retrieving bytes in the right order. */ |
| 1540 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1541 | int iorder[] = {0, 1, 2, 3}; |
| 1542 | #else |
| 1543 | int iorder[] = {3, 2, 1, 0}; |
| 1544 | #endif |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 1545 | /* On narrow builds we split characters outside the BMP into two |
| 1546 | codepoints => count how much extra space we need. */ |
| 1547 | #ifndef Py_UNICODE_WIDE |
| 1548 | for (i = pairs = 0; i < size/4; i++) |
| 1549 | if (((Py_UCS4 *)s)[i] >= 0x10000) |
| 1550 | pairs++; |
| 1551 | #endif |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 1552 | PyObject *errorHandler = NULL; |
| 1553 | PyObject *exc = NULL; |
| 1554 | |
| 1555 | /* This might be one to much, because of a BOM */ |
| 1556 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 1557 | if (!unicode) |
| 1558 | return NULL; |
| 1559 | if (size == 0) |
| 1560 | return (PyObject *)unicode; |
| 1561 | |
| 1562 | /* Unpack UTF-32 encoded data */ |
| 1563 | p = unicode->str; |
| 1564 | q = (unsigned char *)s; |
| 1565 | e = q + size; |
| 1566 | |
| 1567 | if (byteorder) |
| 1568 | bo = *byteorder; |
| 1569 | |
| 1570 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 1571 | byte order setting accordingly. In native mode, the leading BOM |
| 1572 | mark is skipped, in all other modes, it is copied to the output |
| 1573 | stream as-is (giving a ZWNBSP character). */ |
| 1574 | if (bo == 0) { |
| 1575 | if (size >= 4) { |
| 1576 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 1577 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 1578 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1579 | if (bom == 0x0000FEFF) { |
| 1580 | q += 4; |
| 1581 | bo = -1; |
| 1582 | } |
| 1583 | else if (bom == 0xFFFE0000) { |
| 1584 | q += 4; |
| 1585 | bo = 1; |
| 1586 | } |
| 1587 | #else |
| 1588 | if (bom == 0x0000FEFF) { |
| 1589 | q += 4; |
| 1590 | bo = 1; |
| 1591 | } |
| 1592 | else if (bom == 0xFFFE0000) { |
| 1593 | q += 4; |
| 1594 | bo = -1; |
| 1595 | } |
| 1596 | #endif |
| 1597 | } |
| 1598 | } |
| 1599 | |
| 1600 | if (bo == -1) { |
| 1601 | /* force LE */ |
| 1602 | iorder[0] = 0; |
| 1603 | iorder[1] = 1; |
| 1604 | iorder[2] = 2; |
| 1605 | iorder[3] = 3; |
| 1606 | } |
| 1607 | else if (bo == 1) { |
| 1608 | /* force BE */ |
| 1609 | iorder[0] = 3; |
| 1610 | iorder[1] = 2; |
| 1611 | iorder[2] = 1; |
| 1612 | iorder[3] = 0; |
| 1613 | } |
| 1614 | |
| 1615 | while (q < e) { |
| 1616 | Py_UCS4 ch; |
| 1617 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 1618 | if (e-q<4) { |
| 1619 | if (consumed) |
| 1620 | break; |
| 1621 | errmsg = "truncated data"; |
| 1622 | startinpos = ((const char *)q)-starts; |
| 1623 | endinpos = ((const char *)e)-starts; |
| 1624 | goto utf32Error; |
| 1625 | /* The remaining input chars are ignored if the callback |
| 1626 | chooses to skip the input */ |
| 1627 | } |
| 1628 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 1629 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 1630 | |
| 1631 | if (ch >= 0x110000) |
| 1632 | { |
| 1633 | errmsg = "codepoint not in range(0x110000)"; |
| 1634 | startinpos = ((const char *)q)-starts; |
| 1635 | endinpos = startinpos+4; |
| 1636 | goto utf32Error; |
| 1637 | } |
| 1638 | #ifndef Py_UNICODE_WIDE |
| 1639 | if (ch >= 0x10000) |
| 1640 | { |
| 1641 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 1642 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 1643 | } |
| 1644 | else |
| 1645 | #endif |
| 1646 | *p++ = ch; |
| 1647 | q += 4; |
| 1648 | continue; |
| 1649 | utf32Error: |
| 1650 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1651 | if (unicode_decode_call_errorhandler( |
| 1652 | errors, &errorHandler, |
| 1653 | "utf32", errmsg, |
| 1654 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1655 | (PyObject **)&unicode, &outpos, &p)) |
| 1656 | goto onError; |
| 1657 | } |
| 1658 | |
| 1659 | if (byteorder) |
| 1660 | *byteorder = bo; |
| 1661 | |
| 1662 | if (consumed) |
| 1663 | *consumed = (const char *)q-starts; |
| 1664 | |
| 1665 | /* Adjust length */ |
| 1666 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 1667 | goto onError; |
| 1668 | |
| 1669 | Py_XDECREF(errorHandler); |
| 1670 | Py_XDECREF(exc); |
| 1671 | return (PyObject *)unicode; |
| 1672 | |
| 1673 | onError: |
| 1674 | Py_DECREF(unicode); |
| 1675 | Py_XDECREF(errorHandler); |
| 1676 | Py_XDECREF(exc); |
| 1677 | return NULL; |
| 1678 | } |
| 1679 | |
| 1680 | PyObject * |
| 1681 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 1682 | Py_ssize_t size, |
| 1683 | const char *errors, |
| 1684 | int byteorder) |
| 1685 | { |
| 1686 | PyObject *v; |
| 1687 | unsigned char *p; |
| 1688 | #ifndef Py_UNICODE_WIDE |
| 1689 | int i, pairs; |
| 1690 | #else |
| 1691 | const int pairs = 0; |
| 1692 | #endif |
| 1693 | /* Offsets from p for storing byte pairs in the right order. */ |
| 1694 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1695 | int iorder[] = {0, 1, 2, 3}; |
| 1696 | #else |
| 1697 | int iorder[] = {3, 2, 1, 0}; |
| 1698 | #endif |
| 1699 | |
| 1700 | #define STORECHAR(CH) \ |
| 1701 | do { \ |
| 1702 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 1703 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 1704 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 1705 | p[iorder[0]] = (CH) & 0xff; \ |
| 1706 | p += 4; \ |
| 1707 | } while(0) |
| 1708 | |
| 1709 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 1710 | so we need less space. */ |
| 1711 | #ifndef Py_UNICODE_WIDE |
| 1712 | for (i = pairs = 0; i < size-1; i++) |
| 1713 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 1714 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 1715 | pairs++; |
| 1716 | #endif |
| 1717 | v = PyString_FromStringAndSize(NULL, |
| 1718 | 4 * (size - pairs + (byteorder == 0))); |
| 1719 | if (v == NULL) |
| 1720 | return NULL; |
| 1721 | |
| 1722 | p = (unsigned char *)PyString_AS_STRING(v); |
| 1723 | if (byteorder == 0) |
| 1724 | STORECHAR(0xFEFF); |
| 1725 | if (size == 0) |
| 1726 | return v; |
| 1727 | |
| 1728 | if (byteorder == -1) { |
| 1729 | /* force LE */ |
| 1730 | iorder[0] = 0; |
| 1731 | iorder[1] = 1; |
| 1732 | iorder[2] = 2; |
| 1733 | iorder[3] = 3; |
| 1734 | } |
| 1735 | else if (byteorder == 1) { |
| 1736 | /* force BE */ |
| 1737 | iorder[0] = 3; |
| 1738 | iorder[1] = 2; |
| 1739 | iorder[2] = 1; |
| 1740 | iorder[3] = 0; |
| 1741 | } |
| 1742 | |
| 1743 | while (size-- > 0) { |
| 1744 | Py_UCS4 ch = *s++; |
| 1745 | #ifndef Py_UNICODE_WIDE |
| 1746 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 1747 | Py_UCS4 ch2 = *s; |
| 1748 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 1749 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 1750 | s++; |
| 1751 | size--; |
| 1752 | } |
| 1753 | } |
| 1754 | #endif |
| 1755 | STORECHAR(ch); |
| 1756 | } |
| 1757 | return v; |
| 1758 | #undef STORECHAR |
| 1759 | } |
| 1760 | |
| 1761 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 1762 | { |
| 1763 | if (!PyUnicode_Check(unicode)) { |
| 1764 | PyErr_BadArgument(); |
| 1765 | return NULL; |
| 1766 | } |
| 1767 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
| 1768 | PyUnicode_GET_SIZE(unicode), |
| 1769 | NULL, |
| 1770 | 0); |
| 1771 | } |
| 1772 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1773 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 1774 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1775 | PyObject * |
| 1776 | PyUnicode_DecodeUTF16(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1777 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1778 | const char *errors, |
| 1779 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1780 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1781 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 1782 | } |
| 1783 | |
| 1784 | PyObject * |
| 1785 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1786 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1787 | const char *errors, |
| 1788 | int *byteorder, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1789 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1790 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1791 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1792 | Py_ssize_t startinpos; |
| 1793 | Py_ssize_t endinpos; |
| 1794 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1795 | PyUnicodeObject *unicode; |
| 1796 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1797 | const unsigned char *q, *e; |
| 1798 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1799 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1800 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 1801 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1802 | int ihi = 1, ilo = 0; |
| 1803 | #else |
| 1804 | int ihi = 0, ilo = 1; |
| 1805 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1806 | PyObject *errorHandler = NULL; |
| 1807 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1808 | |
| 1809 | /* Note: size will always be longer than the resulting Unicode |
| 1810 | character count */ |
| 1811 | unicode = _PyUnicode_New(size); |
| 1812 | if (!unicode) |
| 1813 | return NULL; |
| 1814 | if (size == 0) |
| 1815 | return (PyObject *)unicode; |
| 1816 | |
| 1817 | /* Unpack UTF-16 encoded data */ |
| 1818 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1819 | q = (unsigned char *)s; |
| 1820 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1821 | |
| 1822 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1823 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1824 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1825 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 1826 | byte order setting accordingly. In native mode, the leading BOM |
| 1827 | mark is skipped, in all other modes, it is copied to the output |
| 1828 | stream as-is (giving a ZWNBSP character). */ |
| 1829 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1830 | if (size >= 2) { |
| 1831 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1832 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1833 | if (bom == 0xFEFF) { |
| 1834 | q += 2; |
| 1835 | bo = -1; |
| 1836 | } |
| 1837 | else if (bom == 0xFFFE) { |
| 1838 | q += 2; |
| 1839 | bo = 1; |
| 1840 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1841 | #else |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1842 | if (bom == 0xFEFF) { |
| 1843 | q += 2; |
| 1844 | bo = 1; |
| 1845 | } |
| 1846 | else if (bom == 0xFFFE) { |
| 1847 | q += 2; |
| 1848 | bo = -1; |
| 1849 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1850 | #endif |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1851 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1852 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1853 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1854 | if (bo == -1) { |
| 1855 | /* force LE */ |
| 1856 | ihi = 1; |
| 1857 | ilo = 0; |
| 1858 | } |
| 1859 | else if (bo == 1) { |
| 1860 | /* force BE */ |
| 1861 | ihi = 0; |
| 1862 | ilo = 1; |
| 1863 | } |
| 1864 | |
| 1865 | while (q < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1866 | Py_UNICODE ch; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1867 | /* remaining bytes at the end? (size should be even) */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1868 | if (e-q<2) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1869 | if (consumed) |
| 1870 | break; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1871 | errmsg = "truncated data"; |
| 1872 | startinpos = ((const char *)q)-starts; |
| 1873 | endinpos = ((const char *)e)-starts; |
| 1874 | goto utf16Error; |
| 1875 | /* The remaining input chars are ignored if the callback |
| 1876 | chooses to skip the input */ |
| 1877 | } |
| 1878 | ch = (q[ihi] << 8) | q[ilo]; |
| 1879 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1880 | q += 2; |
| 1881 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1882 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 1883 | *p++ = ch; |
| 1884 | continue; |
| 1885 | } |
| 1886 | |
| 1887 | /* UTF-16 code pair: */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1888 | if (q >= e) { |
| 1889 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1890 | startinpos = (((const char *)q)-2)-starts; |
| 1891 | endinpos = ((const char *)e)-starts; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1892 | goto utf16Error; |
| 1893 | } |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 1894 | if (0xD800 <= ch && ch <= 0xDBFF) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1895 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 1896 | q += 2; |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 1897 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1898 | #ifndef Py_UNICODE_WIDE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1899 | *p++ = ch; |
| 1900 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1901 | #else |
| 1902 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1903 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1904 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1905 | } |
| 1906 | else { |
| 1907 | errmsg = "illegal UTF-16 surrogate"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1908 | startinpos = (((const char *)q)-4)-starts; |
| 1909 | endinpos = startinpos+2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1910 | goto utf16Error; |
| 1911 | } |
| 1912 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1913 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1914 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1915 | startinpos = (((const char *)q)-2)-starts; |
| 1916 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1917 | /* Fall through to report the error */ |
| 1918 | |
| 1919 | utf16Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1920 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1921 | if (unicode_decode_call_errorhandler( |
| 1922 | errors, &errorHandler, |
| 1923 | "utf16", errmsg, |
| 1924 | starts, size, &startinpos, &endinpos, &exc, (const char **)&q, |
| 1925 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1926 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
| 1929 | if (byteorder) |
| 1930 | *byteorder = bo; |
| 1931 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1932 | if (consumed) |
| 1933 | *consumed = (const char *)q-starts; |
| 1934 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1935 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1936 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1937 | goto onError; |
| 1938 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1939 | Py_XDECREF(errorHandler); |
| 1940 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1941 | return (PyObject *)unicode; |
| 1942 | |
| 1943 | onError: |
| 1944 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1945 | Py_XDECREF(errorHandler); |
| 1946 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1947 | return NULL; |
| 1948 | } |
| 1949 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1950 | PyObject * |
| 1951 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1952 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1953 | const char *errors, |
| 1954 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1955 | { |
| 1956 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1957 | unsigned char *p; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1958 | #ifdef Py_UNICODE_WIDE |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1959 | int i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1960 | #else |
| 1961 | const int pairs = 0; |
| 1962 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1963 | /* Offsets from p for storing byte pairs in the right order. */ |
| 1964 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1965 | int ihi = 1, ilo = 0; |
| 1966 | #else |
| 1967 | int ihi = 0, ilo = 1; |
| 1968 | #endif |
| 1969 | |
| 1970 | #define STORECHAR(CH) \ |
| 1971 | do { \ |
| 1972 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 1973 | p[ilo] = (CH) & 0xff; \ |
| 1974 | p += 2; \ |
| 1975 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1976 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1977 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1978 | for (i = pairs = 0; i < size; i++) |
| 1979 | if (s[i] >= 0x10000) |
| 1980 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1981 | #endif |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1982 | v = PyString_FromStringAndSize(NULL, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1983 | 2 * (size + pairs + (byteorder == 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1984 | if (v == NULL) |
| 1985 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1986 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1987 | p = (unsigned char *)PyString_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1988 | if (byteorder == 0) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1989 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 1990 | if (size == 0) |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 1991 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1992 | |
| 1993 | if (byteorder == -1) { |
| 1994 | /* force LE */ |
| 1995 | ihi = 1; |
| 1996 | ilo = 0; |
| 1997 | } |
| 1998 | else if (byteorder == 1) { |
| 1999 | /* force BE */ |
| 2000 | ihi = 0; |
| 2001 | ilo = 1; |
| 2002 | } |
| 2003 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2004 | while (size-- > 0) { |
| 2005 | Py_UNICODE ch = *s++; |
| 2006 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2007 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2008 | if (ch >= 0x10000) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2009 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2010 | ch = 0xD800 | ((ch-0x10000) >> 10); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2011 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2012 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2013 | STORECHAR(ch); |
| 2014 | if (ch2) |
| 2015 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2016 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2017 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2018 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
| 2021 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 2022 | { |
| 2023 | if (!PyUnicode_Check(unicode)) { |
| 2024 | PyErr_BadArgument(); |
| 2025 | return NULL; |
| 2026 | } |
| 2027 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 2028 | PyUnicode_GET_SIZE(unicode), |
| 2029 | NULL, |
| 2030 | 0); |
| 2031 | } |
| 2032 | |
| 2033 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 2034 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2035 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 2036 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2037 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2038 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2039 | const char *errors) |
| 2040 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2041 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2042 | Py_ssize_t startinpos; |
| 2043 | Py_ssize_t endinpos; |
| 2044 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2045 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2046 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2047 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2048 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2049 | char* message; |
| 2050 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2051 | PyObject *errorHandler = NULL; |
| 2052 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2053 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2054 | /* Escaped strings will always be longer than the resulting |
| 2055 | 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] | 2056 | length after conversion to the true value. |
| 2057 | (but if the error callback returns a long replacement string |
| 2058 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2059 | v = _PyUnicode_New(size); |
| 2060 | if (v == NULL) |
| 2061 | goto onError; |
| 2062 | if (size == 0) |
| 2063 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2064 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2065 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2066 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2067 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2068 | while (s < end) { |
| 2069 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2070 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2071 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2072 | |
| 2073 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2074 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2075 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2076 | continue; |
| 2077 | } |
| 2078 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2079 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2080 | /* \ - Escapes */ |
| 2081 | s++; |
| 2082 | switch (*s++) { |
| 2083 | |
| 2084 | /* \x escapes */ |
| 2085 | case '\n': break; |
| 2086 | case '\\': *p++ = '\\'; break; |
| 2087 | case '\'': *p++ = '\''; break; |
| 2088 | case '\"': *p++ = '\"'; break; |
| 2089 | case 'b': *p++ = '\b'; break; |
| 2090 | case 'f': *p++ = '\014'; break; /* FF */ |
| 2091 | case 't': *p++ = '\t'; break; |
| 2092 | case 'n': *p++ = '\n'; break; |
| 2093 | case 'r': *p++ = '\r'; break; |
| 2094 | case 'v': *p++ = '\013'; break; /* VT */ |
| 2095 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 2096 | |
| 2097 | /* \OOO (octal) escapes */ |
| 2098 | case '0': case '1': case '2': case '3': |
| 2099 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2100 | x = s[-1] - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2101 | if ('0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2102 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2103 | if ('0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2104 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2105 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2106 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2107 | break; |
| 2108 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2109 | /* hex escapes */ |
| 2110 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2111 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2112 | digits = 2; |
| 2113 | message = "truncated \\xXX escape"; |
| 2114 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2115 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2116 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2117 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2118 | digits = 4; |
| 2119 | message = "truncated \\uXXXX escape"; |
| 2120 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2121 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2122 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2123 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2124 | digits = 8; |
| 2125 | message = "truncated \\UXXXXXXXX escape"; |
| 2126 | hexescape: |
| 2127 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2128 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2129 | if (s+digits>end) { |
| 2130 | endinpos = size; |
| 2131 | if (unicode_decode_call_errorhandler( |
| 2132 | errors, &errorHandler, |
| 2133 | "unicodeescape", "end of string in escape sequence", |
| 2134 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2135 | (PyObject **)&v, &outpos, &p)) |
| 2136 | goto onError; |
| 2137 | goto nextByte; |
| 2138 | } |
| 2139 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2140 | c = (unsigned char) s[i]; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2141 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2142 | endinpos = (s+i+1)-starts; |
| 2143 | if (unicode_decode_call_errorhandler( |
| 2144 | errors, &errorHandler, |
| 2145 | "unicodeescape", message, |
| 2146 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2147 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2148 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2149 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2150 | } |
| 2151 | chr = (chr<<4) & ~0xF; |
| 2152 | if (c >= '0' && c <= '9') |
| 2153 | chr += c - '0'; |
| 2154 | else if (c >= 'a' && c <= 'f') |
| 2155 | chr += 10 + c - 'a'; |
| 2156 | else |
| 2157 | chr += 10 + c - 'A'; |
| 2158 | } |
| 2159 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 2160 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2161 | /* _decoding_error will have already written into the |
| 2162 | target buffer. */ |
| 2163 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2164 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2165 | /* when we get here, chr is a 32-bit unicode character */ |
| 2166 | if (chr <= 0xffff) |
| 2167 | /* UCS-2 character */ |
| 2168 | *p++ = (Py_UNICODE) chr; |
| 2169 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2170 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2171 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2172 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2173 | *p++ = chr; |
| 2174 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2175 | chr -= 0x10000L; |
| 2176 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2177 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2178 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2179 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2180 | endinpos = s-starts; |
| 2181 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2182 | if (unicode_decode_call_errorhandler( |
| 2183 | errors, &errorHandler, |
| 2184 | "unicodeescape", "illegal Unicode character", |
| 2185 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2186 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2187 | goto onError; |
| 2188 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2189 | break; |
| 2190 | |
| 2191 | /* \N{name} */ |
| 2192 | case 'N': |
| 2193 | message = "malformed \\N character escape"; |
| 2194 | if (ucnhash_CAPI == NULL) { |
| 2195 | /* load the unicode data module */ |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2196 | PyObject *m, *api; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2197 | m = PyImport_ImportModule("unicodedata"); |
| 2198 | if (m == NULL) |
| 2199 | goto ucnhashError; |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2200 | api = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2201 | Py_DECREF(m); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2202 | if (api == NULL) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2203 | goto ucnhashError; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2204 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2205 | Py_DECREF(api); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2206 | if (ucnhash_CAPI == NULL) |
| 2207 | goto ucnhashError; |
| 2208 | } |
| 2209 | if (*s == '{') { |
| 2210 | const char *start = s+1; |
| 2211 | /* look for the closing brace */ |
| 2212 | while (*s != '}' && s < end) |
| 2213 | s++; |
| 2214 | if (s > start && s < end && *s == '}') { |
| 2215 | /* found a name. look it up in the unicode database */ |
| 2216 | message = "unknown Unicode character name"; |
| 2217 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 2218 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2219 | goto store; |
| 2220 | } |
| 2221 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2222 | endinpos = s-starts; |
| 2223 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2224 | if (unicode_decode_call_errorhandler( |
| 2225 | errors, &errorHandler, |
| 2226 | "unicodeescape", message, |
| 2227 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2228 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2229 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2230 | break; |
| 2231 | |
| 2232 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2233 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2234 | message = "\\ at end of string"; |
| 2235 | s--; |
| 2236 | endinpos = s-starts; |
| 2237 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2238 | if (unicode_decode_call_errorhandler( |
| 2239 | errors, &errorHandler, |
| 2240 | "unicodeescape", message, |
| 2241 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2242 | (PyObject **)&v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2243 | goto onError; |
| 2244 | } |
| 2245 | else { |
| 2246 | *p++ = '\\'; |
| 2247 | *p++ = (unsigned char)s[-1]; |
| 2248 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2249 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2250 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2251 | nextByte: |
| 2252 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2253 | } |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2254 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2255 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 2256 | Py_XDECREF(errorHandler); |
| 2257 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2258 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2259 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2260 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2261 | PyErr_SetString( |
| 2262 | PyExc_UnicodeError, |
| 2263 | "\\N escapes not supported (can't load unicodedata module)" |
| 2264 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2265 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2266 | Py_XDECREF(errorHandler); |
| 2267 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 2268 | return NULL; |
| 2269 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2270 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2271 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2272 | Py_XDECREF(errorHandler); |
| 2273 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2274 | return NULL; |
| 2275 | } |
| 2276 | |
| 2277 | /* Return a Unicode-Escape string version of the Unicode object. |
| 2278 | |
| 2279 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 2280 | appropriate. |
| 2281 | |
| 2282 | */ |
| 2283 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 2284 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
Fredrik Lundh | 95e2a91 | 2006-05-26 11:38:15 +0000 | [diff] [blame] | 2285 | Py_ssize_t size, |
| 2286 | Py_UNICODE ch) |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 2287 | { |
| 2288 | /* like wcschr, but doesn't stop at NULL characters */ |
| 2289 | |
| 2290 | while (size-- > 0) { |
| 2291 | if (*s == ch) |
| 2292 | return s; |
| 2293 | s++; |
| 2294 | } |
| 2295 | |
| 2296 | return NULL; |
| 2297 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 2298 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2299 | static |
| 2300 | PyObject *unicodeescape_string(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2301 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2302 | int quotes) |
| 2303 | { |
| 2304 | PyObject *repr; |
| 2305 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2306 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2307 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2308 | |
Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 2309 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 2310 | better to choose a different scheme. Perhaps scan the |
| 2311 | first N-chars of the string and allocate based on that size. |
| 2312 | */ |
| 2313 | /* Initial allocation is based on the longest-possible unichr |
| 2314 | escape. |
| 2315 | |
| 2316 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 2317 | unichr, so in this case it's the longest unichr escape. In |
| 2318 | narrow (UTF-16) builds this is five chars per source unichr |
| 2319 | since there are two unichrs in the surrogate pair, so in narrow |
| 2320 | (UTF-16) builds it's not the longest unichr escape. |
| 2321 | |
| 2322 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 2323 | so in the narrow (UTF-16) build case it's the longest unichr |
| 2324 | escape. |
| 2325 | */ |
| 2326 | |
| 2327 | repr = PyString_FromStringAndSize(NULL, |
| 2328 | 2 |
| 2329 | #ifdef Py_UNICODE_WIDE |
| 2330 | + 10*size |
| 2331 | #else |
| 2332 | + 6*size |
| 2333 | #endif |
| 2334 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2335 | if (repr == NULL) |
| 2336 | return NULL; |
| 2337 | |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2338 | p = PyString_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2339 | |
| 2340 | if (quotes) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2341 | *p++ = 'u'; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2342 | *p++ = (findchar(s, size, '\'') && |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2343 | !findchar(s, size, '"')) ? '"' : '\''; |
| 2344 | } |
| 2345 | while (size-- > 0) { |
| 2346 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2347 | |
Hye-Shik Chang | 835b243 | 2005-12-17 04:38:31 +0000 | [diff] [blame] | 2348 | /* Escape quotes and backslashes */ |
| 2349 | if ((quotes && |
| 2350 | ch == (Py_UNICODE) PyString_AS_STRING(repr)[1]) || ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2351 | *p++ = '\\'; |
| 2352 | *p++ = (char) ch; |
Guido van Rossum | ad9744a | 2001-09-21 15:38:17 +0000 | [diff] [blame] | 2353 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2354 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2355 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 2356 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2357 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 2358 | else if (ch >= 0x10000) { |
| 2359 | *p++ = '\\'; |
| 2360 | *p++ = 'U'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2361 | *p++ = hexdigit[(ch >> 28) & 0x0000000F]; |
| 2362 | *p++ = hexdigit[(ch >> 24) & 0x0000000F]; |
| 2363 | *p++ = hexdigit[(ch >> 20) & 0x0000000F]; |
| 2364 | *p++ = hexdigit[(ch >> 16) & 0x0000000F]; |
| 2365 | *p++ = hexdigit[(ch >> 12) & 0x0000000F]; |
| 2366 | *p++ = hexdigit[(ch >> 8) & 0x0000000F]; |
| 2367 | *p++ = hexdigit[(ch >> 4) & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2368 | *p++ = hexdigit[ch & 0x0000000F]; |
| 2369 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2370 | } |
Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 2371 | #else |
| 2372 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2373 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 2374 | Py_UNICODE ch2; |
| 2375 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2376 | |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2377 | ch2 = *s++; |
| 2378 | size--; |
| 2379 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 2380 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 2381 | *p++ = '\\'; |
| 2382 | *p++ = 'U'; |
| 2383 | *p++ = hexdigit[(ucs >> 28) & 0x0000000F]; |
| 2384 | *p++ = hexdigit[(ucs >> 24) & 0x0000000F]; |
| 2385 | *p++ = hexdigit[(ucs >> 20) & 0x0000000F]; |
| 2386 | *p++ = hexdigit[(ucs >> 16) & 0x0000000F]; |
| 2387 | *p++ = hexdigit[(ucs >> 12) & 0x0000000F]; |
| 2388 | *p++ = hexdigit[(ucs >> 8) & 0x0000000F]; |
| 2389 | *p++ = hexdigit[(ucs >> 4) & 0x0000000F]; |
| 2390 | *p++ = hexdigit[ucs & 0x0000000F]; |
| 2391 | continue; |
| 2392 | } |
| 2393 | /* Fall through: isolated surrogates are copied as-is */ |
| 2394 | s--; |
| 2395 | size++; |
| 2396 | } |
Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 2397 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2398 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2399 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2400 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2401 | *p++ = '\\'; |
| 2402 | *p++ = 'u'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2403 | *p++ = hexdigit[(ch >> 12) & 0x000F]; |
| 2404 | *p++ = hexdigit[(ch >> 8) & 0x000F]; |
| 2405 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2406 | *p++ = hexdigit[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2407 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2408 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2409 | /* Map special whitespace to '\t', \n', '\r' */ |
| 2410 | else if (ch == '\t') { |
| 2411 | *p++ = '\\'; |
| 2412 | *p++ = 't'; |
| 2413 | } |
| 2414 | else if (ch == '\n') { |
| 2415 | *p++ = '\\'; |
| 2416 | *p++ = 'n'; |
| 2417 | } |
| 2418 | else if (ch == '\r') { |
| 2419 | *p++ = '\\'; |
| 2420 | *p++ = 'r'; |
| 2421 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2422 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2423 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 2424 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2425 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2426 | *p++ = 'x'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2427 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2428 | *p++ = hexdigit[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2429 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2430 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2431 | /* Copy everything else as-is */ |
| 2432 | else |
| 2433 | *p++ = (char) ch; |
| 2434 | } |
| 2435 | if (quotes) |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2436 | *p++ = PyString_AS_STRING(repr)[1]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2437 | |
| 2438 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2439 | _PyString_Resize(&repr, p - PyString_AS_STRING(repr)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2440 | return repr; |
| 2441 | } |
| 2442 | |
| 2443 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2444 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2445 | { |
| 2446 | return unicodeescape_string(s, size, 0); |
| 2447 | } |
| 2448 | |
| 2449 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 2450 | { |
| 2451 | if (!PyUnicode_Check(unicode)) { |
| 2452 | PyErr_BadArgument(); |
| 2453 | return NULL; |
| 2454 | } |
| 2455 | return PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2456 | PyUnicode_GET_SIZE(unicode)); |
| 2457 | } |
| 2458 | |
| 2459 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 2460 | |
| 2461 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2462 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2463 | const char *errors) |
| 2464 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2465 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2466 | Py_ssize_t startinpos; |
| 2467 | Py_ssize_t endinpos; |
| 2468 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2469 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2470 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2471 | const char *end; |
| 2472 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2473 | PyObject *errorHandler = NULL; |
| 2474 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2475 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2476 | /* Escaped strings will always be longer than the resulting |
| 2477 | 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] | 2478 | length after conversion to the true value. (But decoding error |
| 2479 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2480 | v = _PyUnicode_New(size); |
| 2481 | if (v == NULL) |
| 2482 | goto onError; |
| 2483 | if (size == 0) |
| 2484 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2485 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2486 | end = s + size; |
| 2487 | while (s < end) { |
| 2488 | unsigned char c; |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 2489 | Py_UCS4 x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2490 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2491 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2492 | |
| 2493 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2494 | if (*s != '\\') { |
| 2495 | *p++ = (unsigned char)*s++; |
| 2496 | continue; |
| 2497 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2498 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2499 | |
| 2500 | /* \u-escapes are only interpreted iff the number of leading |
| 2501 | backslashes if odd */ |
| 2502 | bs = s; |
| 2503 | for (;s < end;) { |
| 2504 | if (*s != '\\') |
| 2505 | break; |
| 2506 | *p++ = (unsigned char)*s++; |
| 2507 | } |
| 2508 | if (((s - bs) & 1) == 0 || |
| 2509 | s >= end || |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2510 | (*s != 'u' && *s != 'U')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2511 | continue; |
| 2512 | } |
| 2513 | p--; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2514 | count = *s=='u' ? 4 : 8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2515 | s++; |
| 2516 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2517 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2518 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2519 | for (x = 0, i = 0; i < count; ++i, ++s) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2520 | c = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2521 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2522 | endinpos = s-starts; |
| 2523 | if (unicode_decode_call_errorhandler( |
| 2524 | errors, &errorHandler, |
| 2525 | "rawunicodeescape", "truncated \\uXXXX", |
| 2526 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2527 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2528 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2529 | goto nextByte; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2530 | } |
| 2531 | x = (x<<4) & ~0xF; |
| 2532 | if (c >= '0' && c <= '9') |
| 2533 | x += c - '0'; |
| 2534 | else if (c >= 'a' && c <= 'f') |
| 2535 | x += 10 + c - 'a'; |
| 2536 | else |
| 2537 | x += 10 + c - 'A'; |
| 2538 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2539 | #ifndef Py_UNICODE_WIDE |
| 2540 | if (x > 0x10000) { |
| 2541 | if (unicode_decode_call_errorhandler( |
| 2542 | errors, &errorHandler, |
| 2543 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
| 2544 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2545 | (PyObject **)&v, &outpos, &p)) |
| 2546 | goto onError; |
| 2547 | } |
| 2548 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2549 | *p++ = x; |
| 2550 | nextByte: |
| 2551 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2552 | } |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2553 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2554 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2555 | Py_XDECREF(errorHandler); |
| 2556 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2557 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2558 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2559 | onError: |
| 2560 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2561 | Py_XDECREF(errorHandler); |
| 2562 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2563 | return NULL; |
| 2564 | } |
| 2565 | |
| 2566 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2567 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2568 | { |
| 2569 | PyObject *repr; |
| 2570 | char *p; |
| 2571 | char *q; |
| 2572 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2573 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2574 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2575 | #ifdef Py_UNICODE_WIDE |
| 2576 | repr = PyString_FromStringAndSize(NULL, 10 * size); |
| 2577 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2578 | repr = PyString_FromStringAndSize(NULL, 6 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2579 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2580 | if (repr == NULL) |
| 2581 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2582 | if (size == 0) |
| 2583 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2584 | |
| 2585 | p = q = PyString_AS_STRING(repr); |
| 2586 | while (size-- > 0) { |
| 2587 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2588 | #ifdef Py_UNICODE_WIDE |
| 2589 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2590 | if (ch >= 0x10000) { |
| 2591 | *p++ = '\\'; |
| 2592 | *p++ = 'U'; |
| 2593 | *p++ = hexdigit[(ch >> 28) & 0xf]; |
| 2594 | *p++ = hexdigit[(ch >> 24) & 0xf]; |
| 2595 | *p++ = hexdigit[(ch >> 20) & 0xf]; |
| 2596 | *p++ = hexdigit[(ch >> 16) & 0xf]; |
| 2597 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 2598 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 2599 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 2600 | *p++ = hexdigit[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2601 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2602 | else |
| 2603 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2604 | /* Map 16-bit characters to '\uxxxx' */ |
| 2605 | if (ch >= 256) { |
| 2606 | *p++ = '\\'; |
| 2607 | *p++ = 'u'; |
| 2608 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 2609 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 2610 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 2611 | *p++ = hexdigit[ch & 15]; |
| 2612 | } |
| 2613 | /* Copy everything else as-is */ |
| 2614 | else |
| 2615 | *p++ = (char) ch; |
| 2616 | } |
| 2617 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2618 | _PyString_Resize(&repr, p - q); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2619 | return repr; |
| 2620 | } |
| 2621 | |
| 2622 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 2623 | { |
| 2624 | if (!PyUnicode_Check(unicode)) { |
| 2625 | PyErr_BadArgument(); |
| 2626 | return NULL; |
| 2627 | } |
| 2628 | return PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2629 | PyUnicode_GET_SIZE(unicode)); |
| 2630 | } |
| 2631 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2632 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 2633 | |
| 2634 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2635 | Py_ssize_t size, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2636 | const char *errors) |
| 2637 | { |
| 2638 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2639 | Py_ssize_t startinpos; |
| 2640 | Py_ssize_t endinpos; |
| 2641 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2642 | PyUnicodeObject *v; |
| 2643 | Py_UNICODE *p; |
| 2644 | const char *end; |
| 2645 | const char *reason; |
| 2646 | PyObject *errorHandler = NULL; |
| 2647 | PyObject *exc = NULL; |
| 2648 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 2649 | #ifdef Py_UNICODE_WIDE |
| 2650 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 2651 | #endif |
| 2652 | |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 2653 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2654 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 2655 | if (v == NULL) |
| 2656 | goto onError; |
| 2657 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
| 2658 | return (PyObject *)v; |
| 2659 | p = PyUnicode_AS_UNICODE(v); |
| 2660 | end = s + size; |
| 2661 | |
| 2662 | while (s < end) { |
Neal Norwitz | 1004a53 | 2006-05-15 07:17:23 +0000 | [diff] [blame] | 2663 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2664 | /* We have to sanity check the raw data, otherwise doom looms for |
| 2665 | some malformed UCS-4 data. */ |
| 2666 | if ( |
| 2667 | #ifdef Py_UNICODE_WIDE |
| 2668 | *p > unimax || *p < 0 || |
| 2669 | #endif |
| 2670 | end-s < Py_UNICODE_SIZE |
| 2671 | ) |
| 2672 | { |
| 2673 | startinpos = s - starts; |
| 2674 | if (end-s < Py_UNICODE_SIZE) { |
| 2675 | endinpos = end-starts; |
| 2676 | reason = "truncated input"; |
| 2677 | } |
| 2678 | else { |
| 2679 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 2680 | reason = "illegal code point (> 0x10FFFF)"; |
| 2681 | } |
| 2682 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 2683 | if (unicode_decode_call_errorhandler( |
| 2684 | errors, &errorHandler, |
| 2685 | "unicode_internal", reason, |
| 2686 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2687 | (PyObject **)&v, &outpos, &p)) { |
| 2688 | goto onError; |
| 2689 | } |
| 2690 | } |
| 2691 | else { |
| 2692 | p++; |
| 2693 | s += Py_UNICODE_SIZE; |
| 2694 | } |
| 2695 | } |
| 2696 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2697 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2698 | goto onError; |
| 2699 | Py_XDECREF(errorHandler); |
| 2700 | Py_XDECREF(exc); |
| 2701 | return (PyObject *)v; |
| 2702 | |
| 2703 | onError: |
| 2704 | Py_XDECREF(v); |
| 2705 | Py_XDECREF(errorHandler); |
| 2706 | Py_XDECREF(exc); |
| 2707 | return NULL; |
| 2708 | } |
| 2709 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2710 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 2711 | |
| 2712 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2713 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2714 | const char *errors) |
| 2715 | { |
| 2716 | PyUnicodeObject *v; |
| 2717 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2718 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2719 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2720 | if (size == 1) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 2721 | Py_UNICODE r = *(unsigned char*)s; |
| 2722 | return PyUnicode_FromUnicode(&r, 1); |
| 2723 | } |
| 2724 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2725 | v = _PyUnicode_New(size); |
| 2726 | if (v == NULL) |
| 2727 | goto onError; |
| 2728 | if (size == 0) |
| 2729 | return (PyObject *)v; |
| 2730 | p = PyUnicode_AS_UNICODE(v); |
| 2731 | while (size-- > 0) |
| 2732 | *p++ = (unsigned char)*s++; |
| 2733 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2734 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2735 | onError: |
| 2736 | Py_XDECREF(v); |
| 2737 | return NULL; |
| 2738 | } |
| 2739 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2740 | /* create or adjust a UnicodeEncodeError */ |
| 2741 | static void make_encode_exception(PyObject **exceptionObject, |
| 2742 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2743 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 2744 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2745 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2746 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2747 | if (*exceptionObject == NULL) { |
| 2748 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 2749 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2750 | } |
| 2751 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2752 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 2753 | goto onError; |
| 2754 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 2755 | goto onError; |
| 2756 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 2757 | goto onError; |
| 2758 | return; |
| 2759 | onError: |
| 2760 | Py_DECREF(*exceptionObject); |
| 2761 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2762 | } |
| 2763 | } |
| 2764 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2765 | /* raises a UnicodeEncodeError */ |
| 2766 | static void raise_encode_exception(PyObject **exceptionObject, |
| 2767 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2768 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 2769 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2770 | const char *reason) |
| 2771 | { |
| 2772 | make_encode_exception(exceptionObject, |
| 2773 | encoding, unicode, size, startpos, endpos, reason); |
| 2774 | if (*exceptionObject != NULL) |
| 2775 | PyCodec_StrictErrors(*exceptionObject); |
| 2776 | } |
| 2777 | |
| 2778 | /* error handling callback helper: |
| 2779 | build arguments, call the callback and check the arguments, |
| 2780 | put the result into newpos and return the replacement string, which |
| 2781 | has to be freed by the caller */ |
| 2782 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 2783 | PyObject **errorHandler, |
| 2784 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2785 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 2786 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 2787 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2788 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2789 | 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] | 2790 | |
| 2791 | PyObject *restuple; |
| 2792 | PyObject *resunicode; |
| 2793 | |
| 2794 | if (*errorHandler == NULL) { |
| 2795 | *errorHandler = PyCodec_LookupError(errors); |
| 2796 | if (*errorHandler == NULL) |
| 2797 | return NULL; |
| 2798 | } |
| 2799 | |
| 2800 | make_encode_exception(exceptionObject, |
| 2801 | encoding, unicode, size, startpos, endpos, reason); |
| 2802 | if (*exceptionObject == NULL) |
| 2803 | return NULL; |
| 2804 | |
| 2805 | restuple = PyObject_CallFunctionObjArgs( |
| 2806 | *errorHandler, *exceptionObject, NULL); |
| 2807 | if (restuple == NULL) |
| 2808 | return NULL; |
| 2809 | if (!PyTuple_Check(restuple)) { |
| 2810 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 2811 | Py_DECREF(restuple); |
| 2812 | return NULL; |
| 2813 | } |
| 2814 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 2815 | &resunicode, newpos)) { |
| 2816 | Py_DECREF(restuple); |
| 2817 | return NULL; |
| 2818 | } |
| 2819 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2820 | *newpos = size+*newpos; |
| 2821 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 2822 | 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] | 2823 | Py_DECREF(restuple); |
| 2824 | return NULL; |
| 2825 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2826 | Py_INCREF(resunicode); |
| 2827 | Py_DECREF(restuple); |
| 2828 | return resunicode; |
| 2829 | } |
| 2830 | |
| 2831 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2832 | Py_ssize_t size, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2833 | const char *errors, |
| 2834 | int limit) |
| 2835 | { |
| 2836 | /* output object */ |
| 2837 | PyObject *res; |
| 2838 | /* pointers to the beginning and end+1 of input */ |
| 2839 | const Py_UNICODE *startp = p; |
| 2840 | const Py_UNICODE *endp = p + size; |
| 2841 | /* pointer to the beginning of the unencodable characters */ |
| 2842 | /* const Py_UNICODE *badp = NULL; */ |
| 2843 | /* pointer into the output */ |
| 2844 | char *str; |
| 2845 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2846 | Py_ssize_t respos = 0; |
| 2847 | Py_ssize_t ressize; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2848 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 2849 | 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] | 2850 | PyObject *errorHandler = NULL; |
| 2851 | PyObject *exc = NULL; |
| 2852 | /* the following variable is used for caching string comparisons |
| 2853 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 2854 | int known_errorHandler = -1; |
| 2855 | |
| 2856 | /* allocate enough for a simple encoding without |
| 2857 | replacements, if we need more, we'll resize */ |
| 2858 | res = PyString_FromStringAndSize(NULL, size); |
| 2859 | if (res == NULL) |
| 2860 | goto onError; |
| 2861 | if (size == 0) |
| 2862 | return res; |
| 2863 | str = PyString_AS_STRING(res); |
| 2864 | ressize = size; |
| 2865 | |
| 2866 | while (p<endp) { |
| 2867 | Py_UNICODE c = *p; |
| 2868 | |
| 2869 | /* can we encode this? */ |
| 2870 | if (c<limit) { |
| 2871 | /* no overflow check, because we know that the space is enough */ |
| 2872 | *str++ = (char)c; |
| 2873 | ++p; |
| 2874 | } |
| 2875 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2876 | Py_ssize_t unicodepos = p-startp; |
| 2877 | Py_ssize_t requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2878 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2879 | Py_ssize_t repsize; |
| 2880 | Py_ssize_t newpos; |
| 2881 | Py_ssize_t respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2882 | Py_UNICODE *uni2; |
| 2883 | /* startpos for collecting unencodable chars */ |
| 2884 | const Py_UNICODE *collstart = p; |
| 2885 | const Py_UNICODE *collend = p; |
| 2886 | /* find all unecodable characters */ |
| 2887 | while ((collend < endp) && ((*collend)>=limit)) |
| 2888 | ++collend; |
| 2889 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 2890 | if (known_errorHandler==-1) { |
| 2891 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 2892 | known_errorHandler = 1; |
| 2893 | else if (!strcmp(errors, "replace")) |
| 2894 | known_errorHandler = 2; |
| 2895 | else if (!strcmp(errors, "ignore")) |
| 2896 | known_errorHandler = 3; |
| 2897 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 2898 | known_errorHandler = 4; |
| 2899 | else |
| 2900 | known_errorHandler = 0; |
| 2901 | } |
| 2902 | switch (known_errorHandler) { |
| 2903 | case 1: /* strict */ |
| 2904 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 2905 | goto onError; |
| 2906 | case 2: /* replace */ |
| 2907 | while (collstart++<collend) |
| 2908 | *str++ = '?'; /* fall through */ |
| 2909 | case 3: /* ignore */ |
| 2910 | p = collend; |
| 2911 | break; |
| 2912 | case 4: /* xmlcharrefreplace */ |
| 2913 | respos = str-PyString_AS_STRING(res); |
| 2914 | /* determine replacement size (temporarily (mis)uses p) */ |
| 2915 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 2916 | if (*p<10) |
| 2917 | repsize += 2+1+1; |
| 2918 | else if (*p<100) |
| 2919 | repsize += 2+2+1; |
| 2920 | else if (*p<1000) |
| 2921 | repsize += 2+3+1; |
| 2922 | else if (*p<10000) |
| 2923 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 2924 | #ifndef Py_UNICODE_WIDE |
| 2925 | else |
| 2926 | repsize += 2+5+1; |
| 2927 | #else |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2928 | else if (*p<100000) |
| 2929 | repsize += 2+5+1; |
| 2930 | else if (*p<1000000) |
| 2931 | repsize += 2+6+1; |
| 2932 | else |
| 2933 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2934 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2935 | } |
| 2936 | requiredsize = respos+repsize+(endp-collend); |
| 2937 | if (requiredsize > ressize) { |
| 2938 | if (requiredsize<2*ressize) |
| 2939 | requiredsize = 2*ressize; |
| 2940 | if (_PyString_Resize(&res, requiredsize)) |
| 2941 | goto onError; |
| 2942 | str = PyString_AS_STRING(res) + respos; |
| 2943 | ressize = requiredsize; |
| 2944 | } |
| 2945 | /* generate replacement (temporarily (mis)uses p) */ |
| 2946 | for (p = collstart; p < collend; ++p) { |
| 2947 | str += sprintf(str, "&#%d;", (int)*p); |
| 2948 | } |
| 2949 | p = collend; |
| 2950 | break; |
| 2951 | default: |
| 2952 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 2953 | encoding, reason, startp, size, &exc, |
| 2954 | collstart-startp, collend-startp, &newpos); |
| 2955 | if (repunicode == NULL) |
| 2956 | goto onError; |
| 2957 | /* need more space? (at least enough for what we |
| 2958 | have+the replacement+the rest of the string, so |
| 2959 | we won't have to check space for encodable characters) */ |
| 2960 | respos = str-PyString_AS_STRING(res); |
| 2961 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 2962 | requiredsize = respos+repsize+(endp-collend); |
| 2963 | if (requiredsize > ressize) { |
| 2964 | if (requiredsize<2*ressize) |
| 2965 | requiredsize = 2*ressize; |
| 2966 | if (_PyString_Resize(&res, requiredsize)) { |
| 2967 | Py_DECREF(repunicode); |
| 2968 | goto onError; |
| 2969 | } |
| 2970 | str = PyString_AS_STRING(res) + respos; |
| 2971 | ressize = requiredsize; |
| 2972 | } |
| 2973 | /* check if there is anything unencodable in the replacement |
| 2974 | and copy it to the output */ |
| 2975 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 2976 | c = *uni2; |
| 2977 | if (c >= limit) { |
| 2978 | raise_encode_exception(&exc, encoding, startp, size, |
| 2979 | unicodepos, unicodepos+1, reason); |
| 2980 | Py_DECREF(repunicode); |
| 2981 | goto onError; |
| 2982 | } |
| 2983 | *str = (char)c; |
| 2984 | } |
| 2985 | p = startp + newpos; |
| 2986 | Py_DECREF(repunicode); |
| 2987 | } |
| 2988 | } |
| 2989 | } |
| 2990 | /* Resize if we allocated to much */ |
| 2991 | respos = str-PyString_AS_STRING(res); |
| 2992 | if (respos<ressize) |
| 2993 | /* If this falls res will be NULL */ |
| 2994 | _PyString_Resize(&res, respos); |
| 2995 | Py_XDECREF(errorHandler); |
| 2996 | Py_XDECREF(exc); |
| 2997 | return res; |
| 2998 | |
| 2999 | onError: |
| 3000 | Py_XDECREF(res); |
| 3001 | Py_XDECREF(errorHandler); |
| 3002 | Py_XDECREF(exc); |
| 3003 | return NULL; |
| 3004 | } |
| 3005 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3006 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3007 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3008 | const char *errors) |
| 3009 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3010 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3011 | } |
| 3012 | |
| 3013 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 3014 | { |
| 3015 | if (!PyUnicode_Check(unicode)) { |
| 3016 | PyErr_BadArgument(); |
| 3017 | return NULL; |
| 3018 | } |
| 3019 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 3020 | PyUnicode_GET_SIZE(unicode), |
| 3021 | NULL); |
| 3022 | } |
| 3023 | |
| 3024 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 3025 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3026 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3027 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3028 | const char *errors) |
| 3029 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3030 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3031 | PyUnicodeObject *v; |
| 3032 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3033 | Py_ssize_t startinpos; |
| 3034 | Py_ssize_t endinpos; |
| 3035 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3036 | const char *e; |
| 3037 | PyObject *errorHandler = NULL; |
| 3038 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3039 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3040 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3041 | if (size == 1 && *(unsigned char*)s < 128) { |
| 3042 | Py_UNICODE r = *(unsigned char*)s; |
| 3043 | return PyUnicode_FromUnicode(&r, 1); |
| 3044 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3045 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3046 | v = _PyUnicode_New(size); |
| 3047 | if (v == NULL) |
| 3048 | goto onError; |
| 3049 | if (size == 0) |
| 3050 | return (PyObject *)v; |
| 3051 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3052 | e = s + size; |
| 3053 | while (s < e) { |
| 3054 | register unsigned char c = (unsigned char)*s; |
| 3055 | if (c < 128) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3056 | *p++ = c; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3057 | ++s; |
| 3058 | } |
| 3059 | else { |
| 3060 | startinpos = s-starts; |
| 3061 | endinpos = startinpos + 1; |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 3062 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3063 | if (unicode_decode_call_errorhandler( |
| 3064 | errors, &errorHandler, |
| 3065 | "ascii", "ordinal not in range(128)", |
| 3066 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3067 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3068 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3069 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3070 | } |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3071 | if (p - PyUnicode_AS_UNICODE(v) < PyString_GET_SIZE(v)) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3072 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3073 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3074 | Py_XDECREF(errorHandler); |
| 3075 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3076 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3077 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3078 | onError: |
| 3079 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3080 | Py_XDECREF(errorHandler); |
| 3081 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3082 | return NULL; |
| 3083 | } |
| 3084 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3085 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3086 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3087 | const char *errors) |
| 3088 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3089 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3090 | } |
| 3091 | |
| 3092 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 3093 | { |
| 3094 | if (!PyUnicode_Check(unicode)) { |
| 3095 | PyErr_BadArgument(); |
| 3096 | return NULL; |
| 3097 | } |
| 3098 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 3099 | PyUnicode_GET_SIZE(unicode), |
| 3100 | NULL); |
| 3101 | } |
| 3102 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3103 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3104 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3105 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3106 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3107 | #if SIZEOF_INT < SIZEOF_SSIZE_T |
| 3108 | #define NEED_RETRY |
| 3109 | #endif |
| 3110 | |
| 3111 | /* XXX This code is limited to "true" double-byte encodings, as |
| 3112 | a) it assumes an incomplete character consists of a single byte, and |
| 3113 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
| 3114 | encodings, see IsDBCSLeadByteEx documentation. */ |
| 3115 | |
| 3116 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 3117 | { |
| 3118 | const char *curr = s + offset; |
| 3119 | |
| 3120 | if (IsDBCSLeadByte(*curr)) { |
| 3121 | const char *prev = CharPrev(s, curr); |
| 3122 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
| 3123 | } |
| 3124 | return 0; |
| 3125 | } |
| 3126 | |
| 3127 | /* |
| 3128 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 3129 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 3130 | */ |
| 3131 | static int decode_mbcs(PyUnicodeObject **v, |
| 3132 | const char *s, /* MBCS string */ |
| 3133 | int size, /* sizeof MBCS string */ |
| 3134 | int final) |
| 3135 | { |
| 3136 | Py_UNICODE *p; |
| 3137 | Py_ssize_t n = 0; |
| 3138 | int usize = 0; |
| 3139 | |
| 3140 | assert(size >= 0); |
| 3141 | |
| 3142 | /* Skip trailing lead-byte unless 'final' is set */ |
| 3143 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
| 3144 | --size; |
| 3145 | |
| 3146 | /* First get the size of the result */ |
| 3147 | if (size > 0) { |
| 3148 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 3149 | if (usize == 0) { |
| 3150 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3151 | return -1; |
| 3152 | } |
| 3153 | } |
| 3154 | |
| 3155 | if (*v == NULL) { |
| 3156 | /* Create unicode object */ |
| 3157 | *v = _PyUnicode_New(usize); |
| 3158 | if (*v == NULL) |
| 3159 | return -1; |
| 3160 | } |
| 3161 | else { |
| 3162 | /* Extend unicode object */ |
| 3163 | n = PyUnicode_GET_SIZE(*v); |
| 3164 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 3165 | return -1; |
| 3166 | } |
| 3167 | |
| 3168 | /* Do the conversion */ |
| 3169 | if (size > 0) { |
| 3170 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 3171 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 3172 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3173 | return -1; |
| 3174 | } |
| 3175 | } |
| 3176 | |
| 3177 | return size; |
| 3178 | } |
| 3179 | |
| 3180 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
| 3181 | Py_ssize_t size, |
| 3182 | const char *errors, |
| 3183 | Py_ssize_t *consumed) |
| 3184 | { |
| 3185 | PyUnicodeObject *v = NULL; |
| 3186 | int done; |
| 3187 | |
| 3188 | if (consumed) |
| 3189 | *consumed = 0; |
| 3190 | |
| 3191 | #ifdef NEED_RETRY |
| 3192 | retry: |
| 3193 | if (size > INT_MAX) |
| 3194 | done = decode_mbcs(&v, s, INT_MAX, 0); |
| 3195 | else |
| 3196 | #endif |
| 3197 | done = decode_mbcs(&v, s, (int)size, !consumed); |
| 3198 | |
| 3199 | if (done < 0) { |
| 3200 | Py_XDECREF(v); |
| 3201 | return NULL; |
| 3202 | } |
| 3203 | |
| 3204 | if (consumed) |
| 3205 | *consumed += done; |
| 3206 | |
| 3207 | #ifdef NEED_RETRY |
| 3208 | if (size > INT_MAX) { |
| 3209 | s += done; |
| 3210 | size -= done; |
| 3211 | goto retry; |
| 3212 | } |
| 3213 | #endif |
| 3214 | |
| 3215 | return (PyObject *)v; |
| 3216 | } |
| 3217 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3218 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3219 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3220 | const char *errors) |
| 3221 | { |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3222 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 3223 | } |
| 3224 | |
| 3225 | /* |
| 3226 | * Convert unicode into string object (MBCS). |
| 3227 | * Returns 0 if succeed, -1 otherwise. |
| 3228 | */ |
| 3229 | static int encode_mbcs(PyObject **repr, |
| 3230 | const Py_UNICODE *p, /* unicode */ |
| 3231 | int size) /* size of unicode */ |
| 3232 | { |
| 3233 | int mbcssize = 0; |
| 3234 | Py_ssize_t n = 0; |
| 3235 | |
| 3236 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3237 | |
| 3238 | /* First get the size of the result */ |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3239 | if (size > 0) { |
| 3240 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 3241 | if (mbcssize == 0) { |
| 3242 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3243 | return -1; |
| 3244 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3245 | } |
| 3246 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3247 | if (*repr == NULL) { |
| 3248 | /* Create string object */ |
| 3249 | *repr = PyString_FromStringAndSize(NULL, mbcssize); |
| 3250 | if (*repr == NULL) |
| 3251 | return -1; |
| 3252 | } |
| 3253 | else { |
| 3254 | /* Extend string object */ |
| 3255 | n = PyString_Size(*repr); |
| 3256 | if (_PyString_Resize(repr, n + mbcssize) < 0) |
| 3257 | return -1; |
| 3258 | } |
| 3259 | |
| 3260 | /* Do the conversion */ |
| 3261 | if (size > 0) { |
| 3262 | char *s = PyString_AS_STRING(*repr) + n; |
| 3263 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 3264 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3265 | return -1; |
| 3266 | } |
| 3267 | } |
| 3268 | |
| 3269 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3270 | } |
| 3271 | |
| 3272 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3273 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3274 | const char *errors) |
| 3275 | { |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3276 | PyObject *repr = NULL; |
| 3277 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 3278 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3279 | #ifdef NEED_RETRY |
| 3280 | retry: |
| 3281 | if (size > INT_MAX) |
| 3282 | ret = encode_mbcs(&repr, p, INT_MAX); |
| 3283 | else |
| 3284 | #endif |
| 3285 | ret = encode_mbcs(&repr, p, (int)size); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3286 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3287 | if (ret < 0) { |
| 3288 | Py_XDECREF(repr); |
| 3289 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3290 | } |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3291 | |
| 3292 | #ifdef NEED_RETRY |
| 3293 | if (size > INT_MAX) { |
| 3294 | p += INT_MAX; |
| 3295 | size -= INT_MAX; |
| 3296 | goto retry; |
| 3297 | } |
| 3298 | #endif |
| 3299 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3300 | return repr; |
| 3301 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3302 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3303 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 3304 | { |
| 3305 | if (!PyUnicode_Check(unicode)) { |
| 3306 | PyErr_BadArgument(); |
| 3307 | return NULL; |
| 3308 | } |
| 3309 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 3310 | PyUnicode_GET_SIZE(unicode), |
| 3311 | NULL); |
| 3312 | } |
| 3313 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3314 | #undef NEED_RETRY |
| 3315 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3316 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3317 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3318 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 3319 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3320 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3321 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3322 | PyObject *mapping, |
| 3323 | const char *errors) |
| 3324 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3325 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3326 | Py_ssize_t startinpos; |
| 3327 | Py_ssize_t endinpos; |
| 3328 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3329 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3330 | PyUnicodeObject *v; |
| 3331 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3332 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3333 | PyObject *errorHandler = NULL; |
| 3334 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3335 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3336 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3337 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3338 | /* Default to Latin-1 */ |
| 3339 | if (mapping == NULL) |
| 3340 | return PyUnicode_DecodeLatin1(s, size, errors); |
| 3341 | |
| 3342 | v = _PyUnicode_New(size); |
| 3343 | if (v == NULL) |
| 3344 | goto onError; |
| 3345 | if (size == 0) |
| 3346 | return (PyObject *)v; |
| 3347 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3348 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3349 | if (PyUnicode_CheckExact(mapping)) { |
| 3350 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 3351 | maplen = PyUnicode_GET_SIZE(mapping); |
| 3352 | while (s < e) { |
| 3353 | unsigned char ch = *s; |
| 3354 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3355 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3356 | if (ch < maplen) |
| 3357 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3358 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3359 | if (x == 0xfffe) { |
| 3360 | /* undefined mapping */ |
| 3361 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3362 | startinpos = s-starts; |
| 3363 | endinpos = startinpos+1; |
| 3364 | if (unicode_decode_call_errorhandler( |
| 3365 | errors, &errorHandler, |
| 3366 | "charmap", "character maps to <undefined>", |
| 3367 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3368 | (PyObject **)&v, &outpos, &p)) { |
| 3369 | goto onError; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 3370 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3371 | continue; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 3372 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3373 | *p++ = x; |
| 3374 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3375 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3376 | } |
| 3377 | else { |
| 3378 | while (s < e) { |
| 3379 | unsigned char ch = *s; |
| 3380 | PyObject *w, *x; |
| 3381 | |
| 3382 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 3383 | w = PyInt_FromLong((long)ch); |
| 3384 | if (w == NULL) |
| 3385 | goto onError; |
| 3386 | x = PyObject_GetItem(mapping, w); |
| 3387 | Py_DECREF(w); |
| 3388 | if (x == NULL) { |
| 3389 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3390 | /* No mapping found means: mapping is undefined. */ |
| 3391 | PyErr_Clear(); |
| 3392 | x = Py_None; |
| 3393 | Py_INCREF(x); |
| 3394 | } else |
| 3395 | goto onError; |
| 3396 | } |
| 3397 | |
| 3398 | /* Apply mapping */ |
| 3399 | if (PyInt_Check(x)) { |
| 3400 | long value = PyInt_AS_LONG(x); |
| 3401 | if (value < 0 || value > 65535) { |
| 3402 | PyErr_SetString(PyExc_TypeError, |
| 3403 | "character mapping must be in range(65536)"); |
| 3404 | Py_DECREF(x); |
| 3405 | goto onError; |
| 3406 | } |
| 3407 | *p++ = (Py_UNICODE)value; |
| 3408 | } |
| 3409 | else if (x == Py_None) { |
| 3410 | /* undefined mapping */ |
| 3411 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3412 | startinpos = s-starts; |
| 3413 | endinpos = startinpos+1; |
| 3414 | if (unicode_decode_call_errorhandler( |
| 3415 | errors, &errorHandler, |
| 3416 | "charmap", "character maps to <undefined>", |
| 3417 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3418 | (PyObject **)&v, &outpos, &p)) { |
| 3419 | Py_DECREF(x); |
| 3420 | goto onError; |
| 3421 | } |
Walter Dörwald | d4fff17 | 2005-11-28 22:15:56 +0000 | [diff] [blame] | 3422 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3423 | continue; |
| 3424 | } |
| 3425 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3426 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3427 | |
| 3428 | if (targetsize == 1) |
| 3429 | /* 1-1 mapping */ |
| 3430 | *p++ = *PyUnicode_AS_UNICODE(x); |
| 3431 | |
| 3432 | else if (targetsize > 1) { |
| 3433 | /* 1-n mapping */ |
| 3434 | if (targetsize > extrachars) { |
| 3435 | /* resize first */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3436 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 3437 | Py_ssize_t needed = (targetsize - extrachars) + \ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3438 | (targetsize << 2); |
| 3439 | extrachars += needed; |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 3440 | /* XXX overflow detection missing */ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3441 | if (_PyUnicode_Resize(&v, |
| 3442 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 3443 | Py_DECREF(x); |
| 3444 | goto onError; |
| 3445 | } |
| 3446 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 3447 | } |
| 3448 | Py_UNICODE_COPY(p, |
| 3449 | PyUnicode_AS_UNICODE(x), |
| 3450 | targetsize); |
| 3451 | p += targetsize; |
| 3452 | extrachars -= targetsize; |
| 3453 | } |
| 3454 | /* 1-0 mapping: skip the character */ |
| 3455 | } |
| 3456 | else { |
| 3457 | /* wrong return value */ |
| 3458 | PyErr_SetString(PyExc_TypeError, |
| 3459 | "character mapping must return integer, None or unicode"); |
| 3460 | Py_DECREF(x); |
| 3461 | goto onError; |
| 3462 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3463 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3464 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3465 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3466 | } |
| 3467 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3468 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3469 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3470 | Py_XDECREF(errorHandler); |
| 3471 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3472 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3473 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3474 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3475 | Py_XDECREF(errorHandler); |
| 3476 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3477 | Py_XDECREF(v); |
| 3478 | return NULL; |
| 3479 | } |
| 3480 | |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3481 | /* Charmap encoding: the lookup table */ |
| 3482 | |
| 3483 | struct encoding_map{ |
| 3484 | PyObject_HEAD |
| 3485 | unsigned char level1[32]; |
| 3486 | int count2, count3; |
| 3487 | unsigned char level23[1]; |
| 3488 | }; |
| 3489 | |
| 3490 | static PyObject* |
| 3491 | encoding_map_size(PyObject *obj, PyObject* args) |
| 3492 | { |
| 3493 | struct encoding_map *map = (struct encoding_map*)obj; |
| 3494 | return PyInt_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
| 3495 | 128*map->count3); |
| 3496 | } |
| 3497 | |
| 3498 | static PyMethodDef encoding_map_methods[] = { |
| 3499 | {"size", encoding_map_size, METH_NOARGS, |
| 3500 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 3501 | { 0 } |
| 3502 | }; |
| 3503 | |
| 3504 | static void |
| 3505 | encoding_map_dealloc(PyObject* o) |
| 3506 | { |
| 3507 | PyObject_FREE(o); |
| 3508 | } |
| 3509 | |
| 3510 | static PyTypeObject EncodingMapType = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 3511 | PyVarObject_HEAD_INIT(NULL, 0) |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3512 | "EncodingMap", /*tp_name*/ |
| 3513 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 3514 | 0, /*tp_itemsize*/ |
| 3515 | /* methods */ |
| 3516 | encoding_map_dealloc, /*tp_dealloc*/ |
| 3517 | 0, /*tp_print*/ |
| 3518 | 0, /*tp_getattr*/ |
| 3519 | 0, /*tp_setattr*/ |
| 3520 | 0, /*tp_compare*/ |
| 3521 | 0, /*tp_repr*/ |
| 3522 | 0, /*tp_as_number*/ |
| 3523 | 0, /*tp_as_sequence*/ |
| 3524 | 0, /*tp_as_mapping*/ |
| 3525 | 0, /*tp_hash*/ |
| 3526 | 0, /*tp_call*/ |
| 3527 | 0, /*tp_str*/ |
| 3528 | 0, /*tp_getattro*/ |
| 3529 | 0, /*tp_setattro*/ |
| 3530 | 0, /*tp_as_buffer*/ |
| 3531 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 3532 | 0, /*tp_doc*/ |
| 3533 | 0, /*tp_traverse*/ |
| 3534 | 0, /*tp_clear*/ |
| 3535 | 0, /*tp_richcompare*/ |
| 3536 | 0, /*tp_weaklistoffset*/ |
| 3537 | 0, /*tp_iter*/ |
| 3538 | 0, /*tp_iternext*/ |
| 3539 | encoding_map_methods, /*tp_methods*/ |
| 3540 | 0, /*tp_members*/ |
| 3541 | 0, /*tp_getset*/ |
| 3542 | 0, /*tp_base*/ |
| 3543 | 0, /*tp_dict*/ |
| 3544 | 0, /*tp_descr_get*/ |
| 3545 | 0, /*tp_descr_set*/ |
| 3546 | 0, /*tp_dictoffset*/ |
| 3547 | 0, /*tp_init*/ |
| 3548 | 0, /*tp_alloc*/ |
| 3549 | 0, /*tp_new*/ |
| 3550 | 0, /*tp_free*/ |
| 3551 | 0, /*tp_is_gc*/ |
| 3552 | }; |
| 3553 | |
| 3554 | PyObject* |
| 3555 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 3556 | { |
| 3557 | Py_UNICODE *decode; |
| 3558 | PyObject *result; |
| 3559 | struct encoding_map *mresult; |
| 3560 | int i; |
| 3561 | int need_dict = 0; |
| 3562 | unsigned char level1[32]; |
| 3563 | unsigned char level2[512]; |
| 3564 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 3565 | int count2 = 0, count3 = 0; |
| 3566 | |
| 3567 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 3568 | PyErr_BadArgument(); |
| 3569 | return NULL; |
| 3570 | } |
| 3571 | decode = PyUnicode_AS_UNICODE(string); |
| 3572 | memset(level1, 0xFF, sizeof level1); |
| 3573 | memset(level2, 0xFF, sizeof level2); |
| 3574 | |
| 3575 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 3576 | or if there are non-BMP characters, we need to use |
| 3577 | a mapping dictionary. */ |
| 3578 | if (decode[0] != 0) |
| 3579 | need_dict = 1; |
| 3580 | for (i = 1; i < 256; i++) { |
| 3581 | int l1, l2; |
| 3582 | if (decode[i] == 0 |
| 3583 | #ifdef Py_UNICODE_WIDE |
| 3584 | || decode[i] > 0xFFFF |
| 3585 | #endif |
| 3586 | ) { |
| 3587 | need_dict = 1; |
| 3588 | break; |
| 3589 | } |
| 3590 | if (decode[i] == 0xFFFE) |
| 3591 | /* unmapped character */ |
| 3592 | continue; |
| 3593 | l1 = decode[i] >> 11; |
| 3594 | l2 = decode[i] >> 7; |
| 3595 | if (level1[l1] == 0xFF) |
| 3596 | level1[l1] = count2++; |
| 3597 | if (level2[l2] == 0xFF) |
| 3598 | level2[l2] = count3++; |
| 3599 | } |
| 3600 | |
| 3601 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 3602 | need_dict = 1; |
| 3603 | |
| 3604 | if (need_dict) { |
| 3605 | PyObject *result = PyDict_New(); |
| 3606 | PyObject *key, *value; |
| 3607 | if (!result) |
| 3608 | return NULL; |
| 3609 | for (i = 0; i < 256; i++) { |
| 3610 | key = value = NULL; |
| 3611 | key = PyInt_FromLong(decode[i]); |
| 3612 | value = PyInt_FromLong(i); |
| 3613 | if (!key || !value) |
| 3614 | goto failed1; |
| 3615 | if (PyDict_SetItem(result, key, value) == -1) |
| 3616 | goto failed1; |
Georg Brandl | 9f16760 | 2006-06-04 21:46:16 +0000 | [diff] [blame] | 3617 | Py_DECREF(key); |
| 3618 | Py_DECREF(value); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3619 | } |
| 3620 | return result; |
| 3621 | failed1: |
| 3622 | Py_XDECREF(key); |
| 3623 | Py_XDECREF(value); |
| 3624 | Py_DECREF(result); |
| 3625 | return NULL; |
| 3626 | } |
| 3627 | |
| 3628 | /* Create a three-level trie */ |
| 3629 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 3630 | 16*count2 + 128*count3 - 1); |
| 3631 | if (!result) |
| 3632 | return PyErr_NoMemory(); |
| 3633 | PyObject_Init(result, &EncodingMapType); |
| 3634 | mresult = (struct encoding_map*)result; |
| 3635 | mresult->count2 = count2; |
| 3636 | mresult->count3 = count3; |
| 3637 | mlevel1 = mresult->level1; |
| 3638 | mlevel2 = mresult->level23; |
| 3639 | mlevel3 = mresult->level23 + 16*count2; |
| 3640 | memcpy(mlevel1, level1, 32); |
| 3641 | memset(mlevel2, 0xFF, 16*count2); |
| 3642 | memset(mlevel3, 0, 128*count3); |
| 3643 | count3 = 0; |
| 3644 | for (i = 1; i < 256; i++) { |
| 3645 | int o1, o2, o3, i2, i3; |
| 3646 | if (decode[i] == 0xFFFE) |
| 3647 | /* unmapped character */ |
| 3648 | continue; |
| 3649 | o1 = decode[i]>>11; |
| 3650 | o2 = (decode[i]>>7) & 0xF; |
| 3651 | i2 = 16*mlevel1[o1] + o2; |
| 3652 | if (mlevel2[i2] == 0xFF) |
| 3653 | mlevel2[i2] = count3++; |
| 3654 | o3 = decode[i] & 0x7F; |
| 3655 | i3 = 128*mlevel2[i2] + o3; |
| 3656 | mlevel3[i3] = i; |
| 3657 | } |
| 3658 | return result; |
| 3659 | } |
| 3660 | |
| 3661 | static int |
| 3662 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 3663 | { |
| 3664 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 3665 | int l1 = c>>11; |
| 3666 | int l2 = (c>>7) & 0xF; |
| 3667 | int l3 = c & 0x7F; |
| 3668 | int i; |
| 3669 | |
| 3670 | #ifdef Py_UNICODE_WIDE |
| 3671 | if (c > 0xFFFF) { |
| 3672 | return -1; |
| 3673 | } |
| 3674 | #endif |
| 3675 | if (c == 0) |
| 3676 | return 0; |
| 3677 | /* level 1*/ |
| 3678 | i = map->level1[l1]; |
| 3679 | if (i == 0xFF) { |
| 3680 | return -1; |
| 3681 | } |
| 3682 | /* level 2*/ |
| 3683 | i = map->level23[16*i+l2]; |
| 3684 | if (i == 0xFF) { |
| 3685 | return -1; |
| 3686 | } |
| 3687 | /* level 3 */ |
| 3688 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 3689 | if (i == 0) { |
| 3690 | return -1; |
| 3691 | } |
| 3692 | return i; |
| 3693 | } |
| 3694 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3695 | /* Lookup the character ch in the mapping. If the character |
| 3696 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3697 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3698 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3699 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3700 | PyObject *w = PyInt_FromLong((long)c); |
| 3701 | PyObject *x; |
| 3702 | |
| 3703 | if (w == NULL) |
| 3704 | return NULL; |
| 3705 | x = PyObject_GetItem(mapping, w); |
| 3706 | Py_DECREF(w); |
| 3707 | if (x == NULL) { |
| 3708 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3709 | /* No mapping found means: mapping is undefined. */ |
| 3710 | PyErr_Clear(); |
| 3711 | x = Py_None; |
| 3712 | Py_INCREF(x); |
| 3713 | return x; |
| 3714 | } else |
| 3715 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3716 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 3717 | else if (x == Py_None) |
| 3718 | return x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3719 | else if (PyInt_Check(x)) { |
| 3720 | long value = PyInt_AS_LONG(x); |
| 3721 | if (value < 0 || value > 255) { |
| 3722 | PyErr_SetString(PyExc_TypeError, |
| 3723 | "character mapping must be in range(256)"); |
| 3724 | Py_DECREF(x); |
| 3725 | return NULL; |
| 3726 | } |
| 3727 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3728 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3729 | else if (PyString_Check(x)) |
| 3730 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3731 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3732 | /* wrong return value */ |
| 3733 | PyErr_SetString(PyExc_TypeError, |
| 3734 | "character mapping must return integer, None or str"); |
| 3735 | Py_DECREF(x); |
| 3736 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3737 | } |
| 3738 | } |
| 3739 | |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3740 | static int |
| 3741 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
| 3742 | { |
| 3743 | Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
| 3744 | /* exponentially overallocate to minimize reallocations */ |
| 3745 | if (requiredsize < 2*outsize) |
| 3746 | requiredsize = 2*outsize; |
| 3747 | if (_PyString_Resize(outobj, requiredsize)) { |
| 3748 | return 0; |
| 3749 | } |
| 3750 | return 1; |
| 3751 | } |
| 3752 | |
| 3753 | typedef enum charmapencode_result { |
| 3754 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
| 3755 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3756 | /* lookup the character, put the result in the output string and adjust |
| 3757 | various state variables. Reallocate the output string if not enough |
| 3758 | space is available. Return a new reference to the object that |
| 3759 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 3760 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 3761 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3762 | static |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3763 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3764 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3765 | { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3766 | PyObject *rep; |
| 3767 | char *outstart; |
| 3768 | Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3769 | |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 3770 | if (Py_Type(mapping) == &EncodingMapType) { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3771 | int res = encoding_map_lookup(c, mapping); |
| 3772 | Py_ssize_t requiredsize = *outpos+1; |
| 3773 | if (res == -1) |
| 3774 | return enc_FAILED; |
| 3775 | if (outsize<requiredsize) |
| 3776 | if (!charmapencode_resize(outobj, outpos, requiredsize)) |
| 3777 | return enc_EXCEPTION; |
| 3778 | outstart = PyString_AS_STRING(*outobj); |
| 3779 | outstart[(*outpos)++] = (char)res; |
| 3780 | return enc_SUCCESS; |
| 3781 | } |
| 3782 | |
| 3783 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3784 | if (rep==NULL) |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3785 | return enc_EXCEPTION; |
| 3786 | else if (rep==Py_None) { |
| 3787 | Py_DECREF(rep); |
| 3788 | return enc_FAILED; |
| 3789 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3790 | if (PyInt_Check(rep)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3791 | Py_ssize_t requiredsize = *outpos+1; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3792 | if (outsize<requiredsize) |
| 3793 | if (!charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3794 | Py_DECREF(rep); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3795 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3796 | } |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3797 | outstart = PyString_AS_STRING(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3798 | outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); |
| 3799 | } |
| 3800 | else { |
| 3801 | const char *repchars = PyString_AS_STRING(rep); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3802 | Py_ssize_t repsize = PyString_GET_SIZE(rep); |
| 3803 | Py_ssize_t requiredsize = *outpos+repsize; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3804 | if (outsize<requiredsize) |
| 3805 | if (!charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3806 | Py_DECREF(rep); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3807 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3808 | } |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3809 | outstart = PyString_AS_STRING(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3810 | memcpy(outstart + *outpos, repchars, repsize); |
| 3811 | *outpos += repsize; |
| 3812 | } |
| 3813 | } |
Georg Brandl | 9f16760 | 2006-06-04 21:46:16 +0000 | [diff] [blame] | 3814 | Py_DECREF(rep); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3815 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3816 | } |
| 3817 | |
| 3818 | /* handle an error in PyUnicode_EncodeCharmap |
| 3819 | Return 0 on success, -1 on error */ |
| 3820 | static |
| 3821 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3822 | 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] | 3823 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3824 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3825 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3826 | { |
| 3827 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3828 | Py_ssize_t repsize; |
| 3829 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3830 | Py_UNICODE *uni2; |
| 3831 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3832 | Py_ssize_t collstartpos = *inpos; |
| 3833 | Py_ssize_t collendpos = *inpos+1; |
| 3834 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3835 | char *encoding = "charmap"; |
| 3836 | char *reason = "character maps to <undefined>"; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3837 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3838 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3839 | /* find all unencodable characters */ |
| 3840 | while (collendpos < size) { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3841 | PyObject *rep; |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 3842 | if (Py_Type(mapping) == &EncodingMapType) { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3843 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 3844 | if (res != -1) |
| 3845 | break; |
| 3846 | ++collendpos; |
| 3847 | continue; |
| 3848 | } |
| 3849 | |
| 3850 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 3851 | if (rep==NULL) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3852 | return -1; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3853 | else if (rep!=Py_None) { |
| 3854 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3855 | break; |
| 3856 | } |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3857 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3858 | ++collendpos; |
| 3859 | } |
| 3860 | /* cache callback name lookup |
| 3861 | * (if not done yet, i.e. it's the first error) */ |
| 3862 | if (*known_errorHandler==-1) { |
| 3863 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3864 | *known_errorHandler = 1; |
| 3865 | else if (!strcmp(errors, "replace")) |
| 3866 | *known_errorHandler = 2; |
| 3867 | else if (!strcmp(errors, "ignore")) |
| 3868 | *known_errorHandler = 3; |
| 3869 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3870 | *known_errorHandler = 4; |
| 3871 | else |
| 3872 | *known_errorHandler = 0; |
| 3873 | } |
| 3874 | switch (*known_errorHandler) { |
| 3875 | case 1: /* strict */ |
| 3876 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3877 | return -1; |
| 3878 | case 2: /* replace */ |
| 3879 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 3880 | x = charmapencode_output('?', mapping, res, respos); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3881 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3882 | return -1; |
| 3883 | } |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3884 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3885 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3886 | return -1; |
| 3887 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3888 | } |
| 3889 | /* fall through */ |
| 3890 | case 3: /* ignore */ |
| 3891 | *inpos = collendpos; |
| 3892 | break; |
| 3893 | case 4: /* xmlcharrefreplace */ |
| 3894 | /* generate replacement (temporarily (mis)uses p) */ |
| 3895 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 3896 | char buffer[2+29+1+1]; |
| 3897 | char *cp; |
| 3898 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 3899 | for (cp = buffer; *cp; ++cp) { |
| 3900 | x = charmapencode_output(*cp, mapping, res, respos); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3901 | if (x==enc_EXCEPTION) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3902 | return -1; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3903 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3904 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3905 | return -1; |
| 3906 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3907 | } |
| 3908 | } |
| 3909 | *inpos = collendpos; |
| 3910 | break; |
| 3911 | default: |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3912 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3913 | encoding, reason, p, size, exceptionObject, |
| 3914 | collstartpos, collendpos, &newpos); |
| 3915 | if (repunicode == NULL) |
| 3916 | return -1; |
| 3917 | /* generate replacement */ |
| 3918 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3919 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 3920 | x = charmapencode_output(*uni2, mapping, res, respos); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3921 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3922 | return -1; |
| 3923 | } |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3924 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3925 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3926 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3927 | return -1; |
| 3928 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3929 | } |
| 3930 | *inpos = newpos; |
| 3931 | Py_DECREF(repunicode); |
| 3932 | } |
| 3933 | return 0; |
| 3934 | } |
| 3935 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3936 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3937 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3938 | PyObject *mapping, |
| 3939 | const char *errors) |
| 3940 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3941 | /* output object */ |
| 3942 | PyObject *res = NULL; |
| 3943 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3944 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3945 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3946 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3947 | PyObject *errorHandler = NULL; |
| 3948 | PyObject *exc = NULL; |
| 3949 | /* the following variable is used for caching string comparisons |
| 3950 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 3951 | * 3=ignore, 4=xmlcharrefreplace */ |
| 3952 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3953 | |
| 3954 | /* Default to Latin-1 */ |
| 3955 | if (mapping == NULL) |
| 3956 | return PyUnicode_EncodeLatin1(p, size, errors); |
| 3957 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3958 | /* allocate enough for a simple encoding without |
| 3959 | replacements, if we need more, we'll resize */ |
| 3960 | res = PyString_FromStringAndSize(NULL, size); |
| 3961 | if (res == NULL) |
| 3962 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3963 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3964 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3965 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3966 | while (inpos<size) { |
| 3967 | /* try to encode it */ |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3968 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 3969 | if (x==enc_EXCEPTION) /* error */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3970 | goto onError; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3971 | if (x==enc_FAILED) { /* unencodable character */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3972 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 3973 | &exc, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3974 | &known_errorHandler, &errorHandler, errors, |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 3975 | &res, &respos)) { |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3976 | goto onError; |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 3977 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3978 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3979 | else |
| 3980 | /* done with this character => adjust input position */ |
| 3981 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3982 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3983 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3984 | /* Resize if we allocated to much */ |
| 3985 | if (respos<PyString_GET_SIZE(res)) { |
| 3986 | if (_PyString_Resize(&res, respos)) |
| 3987 | goto onError; |
| 3988 | } |
| 3989 | Py_XDECREF(exc); |
| 3990 | Py_XDECREF(errorHandler); |
| 3991 | return res; |
| 3992 | |
| 3993 | onError: |
| 3994 | Py_XDECREF(res); |
| 3995 | Py_XDECREF(exc); |
| 3996 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3997 | return NULL; |
| 3998 | } |
| 3999 | |
| 4000 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 4001 | PyObject *mapping) |
| 4002 | { |
| 4003 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 4004 | PyErr_BadArgument(); |
| 4005 | return NULL; |
| 4006 | } |
| 4007 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 4008 | PyUnicode_GET_SIZE(unicode), |
| 4009 | mapping, |
| 4010 | NULL); |
| 4011 | } |
| 4012 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4013 | /* create or adjust a UnicodeTranslateError */ |
| 4014 | static void make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4015 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4016 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4017 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4018 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4019 | if (*exceptionObject == NULL) { |
| 4020 | *exceptionObject = PyUnicodeTranslateError_Create( |
| 4021 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4022 | } |
| 4023 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4024 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 4025 | goto onError; |
| 4026 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 4027 | goto onError; |
| 4028 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 4029 | goto onError; |
| 4030 | return; |
| 4031 | onError: |
| 4032 | Py_DECREF(*exceptionObject); |
| 4033 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4034 | } |
| 4035 | } |
| 4036 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4037 | /* raises a UnicodeTranslateError */ |
| 4038 | static void raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4039 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4040 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4041 | const char *reason) |
| 4042 | { |
| 4043 | make_translate_exception(exceptionObject, |
| 4044 | unicode, size, startpos, endpos, reason); |
| 4045 | if (*exceptionObject != NULL) |
| 4046 | PyCodec_StrictErrors(*exceptionObject); |
| 4047 | } |
| 4048 | |
| 4049 | /* error handling callback helper: |
| 4050 | build arguments, call the callback and check the arguments, |
| 4051 | put the result into newpos and return the replacement string, which |
| 4052 | has to be freed by the caller */ |
| 4053 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 4054 | PyObject **errorHandler, |
| 4055 | const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4056 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4057 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4058 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4059 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4060 | 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] | 4061 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4062 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4063 | PyObject *restuple; |
| 4064 | PyObject *resunicode; |
| 4065 | |
| 4066 | if (*errorHandler == NULL) { |
| 4067 | *errorHandler = PyCodec_LookupError(errors); |
| 4068 | if (*errorHandler == NULL) |
| 4069 | return NULL; |
| 4070 | } |
| 4071 | |
| 4072 | make_translate_exception(exceptionObject, |
| 4073 | unicode, size, startpos, endpos, reason); |
| 4074 | if (*exceptionObject == NULL) |
| 4075 | return NULL; |
| 4076 | |
| 4077 | restuple = PyObject_CallFunctionObjArgs( |
| 4078 | *errorHandler, *exceptionObject, NULL); |
| 4079 | if (restuple == NULL) |
| 4080 | return NULL; |
| 4081 | if (!PyTuple_Check(restuple)) { |
| 4082 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 4083 | Py_DECREF(restuple); |
| 4084 | return NULL; |
| 4085 | } |
| 4086 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4087 | &resunicode, &i_newpos)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4088 | Py_DECREF(restuple); |
| 4089 | return NULL; |
| 4090 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4091 | if (i_newpos<0) |
| 4092 | *newpos = size+i_newpos; |
| 4093 | else |
| 4094 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4095 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 4096 | 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] | 4097 | Py_DECREF(restuple); |
| 4098 | return NULL; |
| 4099 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4100 | Py_INCREF(resunicode); |
| 4101 | Py_DECREF(restuple); |
| 4102 | return resunicode; |
| 4103 | } |
| 4104 | |
| 4105 | /* Lookup the character ch in the mapping and put the result in result, |
| 4106 | which must be decrefed by the caller. |
| 4107 | Return 0 on success, -1 on error */ |
| 4108 | static |
| 4109 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 4110 | { |
| 4111 | PyObject *w = PyInt_FromLong((long)c); |
| 4112 | PyObject *x; |
| 4113 | |
| 4114 | if (w == NULL) |
| 4115 | return -1; |
| 4116 | x = PyObject_GetItem(mapping, w); |
| 4117 | Py_DECREF(w); |
| 4118 | if (x == NULL) { |
| 4119 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4120 | /* No mapping found means: use 1:1 mapping. */ |
| 4121 | PyErr_Clear(); |
| 4122 | *result = NULL; |
| 4123 | return 0; |
| 4124 | } else |
| 4125 | return -1; |
| 4126 | } |
| 4127 | else if (x == Py_None) { |
| 4128 | *result = x; |
| 4129 | return 0; |
| 4130 | } |
| 4131 | else if (PyInt_Check(x)) { |
| 4132 | long value = PyInt_AS_LONG(x); |
| 4133 | long max = PyUnicode_GetMax(); |
| 4134 | if (value < 0 || value > max) { |
| 4135 | PyErr_Format(PyExc_TypeError, |
| 4136 | "character mapping must be in range(0x%lx)", max+1); |
| 4137 | Py_DECREF(x); |
| 4138 | return -1; |
| 4139 | } |
| 4140 | *result = x; |
| 4141 | return 0; |
| 4142 | } |
| 4143 | else if (PyUnicode_Check(x)) { |
| 4144 | *result = x; |
| 4145 | return 0; |
| 4146 | } |
| 4147 | else { |
| 4148 | /* wrong return value */ |
| 4149 | PyErr_SetString(PyExc_TypeError, |
| 4150 | "character mapping must return integer, None or unicode"); |
Walter Dörwald | 150523e | 2003-08-15 16:52:19 +0000 | [diff] [blame] | 4151 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4152 | return -1; |
| 4153 | } |
| 4154 | } |
| 4155 | /* ensure that *outobj is at least requiredsize characters long, |
| 4156 | if not reallocate and adjust various state variables. |
| 4157 | Return 0 on success, -1 on error */ |
| 4158 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4159 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4160 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4161 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4162 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4163 | if (requiredsize > oldsize) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4164 | /* remember old output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4165 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4166 | /* exponentially overallocate to minimize reallocations */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4167 | if (requiredsize < 2 * oldsize) |
| 4168 | requiredsize = 2 * oldsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4169 | if (_PyUnicode_Resize(outobj, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4170 | return -1; |
| 4171 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4172 | } |
| 4173 | return 0; |
| 4174 | } |
| 4175 | /* lookup the character, put the result in the output string and adjust |
| 4176 | various state variables. Return a new reference to the object that |
| 4177 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 4178 | undefined (in which case no character was written). |
| 4179 | The called must decref result. |
| 4180 | Return 0 on success, -1 on error. */ |
| 4181 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4182 | 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] | 4183 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4184 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4185 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4186 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4187 | return -1; |
| 4188 | if (*res==NULL) { |
| 4189 | /* not found => default to 1:1 mapping */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4190 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4191 | } |
| 4192 | else if (*res==Py_None) |
| 4193 | ; |
| 4194 | else if (PyInt_Check(*res)) { |
| 4195 | /* no overflow check, because we know that the space is enough */ |
| 4196 | *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); |
| 4197 | } |
| 4198 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4199 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4200 | if (repsize==1) { |
| 4201 | /* no overflow check, because we know that the space is enough */ |
| 4202 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 4203 | } |
| 4204 | else if (repsize!=0) { |
| 4205 | /* more than one character */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4206 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
Walter Dörwald | cd736e7 | 2004-02-05 17:36:00 +0000 | [diff] [blame] | 4207 | (insize - (curinp-startinp)) + |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4208 | repsize - 1; |
| 4209 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4210 | return -1; |
| 4211 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 4212 | *outp += repsize; |
| 4213 | } |
| 4214 | } |
| 4215 | else |
| 4216 | return -1; |
| 4217 | return 0; |
| 4218 | } |
| 4219 | |
| 4220 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4221 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4222 | PyObject *mapping, |
| 4223 | const char *errors) |
| 4224 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4225 | /* output object */ |
| 4226 | PyObject *res = NULL; |
| 4227 | /* pointers to the beginning and end+1 of input */ |
| 4228 | const Py_UNICODE *startp = p; |
| 4229 | const Py_UNICODE *endp = p + size; |
| 4230 | /* pointer into the output */ |
| 4231 | Py_UNICODE *str; |
| 4232 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4233 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4234 | char *reason = "character maps to <undefined>"; |
| 4235 | PyObject *errorHandler = NULL; |
| 4236 | PyObject *exc = NULL; |
| 4237 | /* the following variable is used for caching string comparisons |
| 4238 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4239 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4240 | int known_errorHandler = -1; |
| 4241 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4242 | if (mapping == NULL) { |
| 4243 | PyErr_BadArgument(); |
| 4244 | return NULL; |
| 4245 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4246 | |
| 4247 | /* allocate enough for a simple 1:1 translation without |
| 4248 | replacements, if we need more, we'll resize */ |
| 4249 | res = PyUnicode_FromUnicode(NULL, size); |
| 4250 | if (res == NULL) |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4251 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4252 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4253 | return res; |
| 4254 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4255 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4256 | while (p<endp) { |
| 4257 | /* try to encode it */ |
| 4258 | PyObject *x = NULL; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4259 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4260 | Py_XDECREF(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4261 | goto onError; |
| 4262 | } |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 4263 | Py_XDECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4264 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 4265 | ++p; |
| 4266 | else { /* untranslatable character */ |
| 4267 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4268 | Py_ssize_t repsize; |
| 4269 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4270 | Py_UNICODE *uni2; |
| 4271 | /* startpos for collecting untranslatable chars */ |
| 4272 | const Py_UNICODE *collstart = p; |
| 4273 | const Py_UNICODE *collend = p+1; |
| 4274 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4275 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4276 | /* find all untranslatable characters */ |
| 4277 | while (collend < endp) { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4278 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4279 | goto onError; |
| 4280 | Py_XDECREF(x); |
| 4281 | if (x!=Py_None) |
| 4282 | break; |
| 4283 | ++collend; |
| 4284 | } |
| 4285 | /* cache callback name lookup |
| 4286 | * (if not done yet, i.e. it's the first error) */ |
| 4287 | if (known_errorHandler==-1) { |
| 4288 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4289 | known_errorHandler = 1; |
| 4290 | else if (!strcmp(errors, "replace")) |
| 4291 | known_errorHandler = 2; |
| 4292 | else if (!strcmp(errors, "ignore")) |
| 4293 | known_errorHandler = 3; |
| 4294 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4295 | known_errorHandler = 4; |
| 4296 | else |
| 4297 | known_errorHandler = 0; |
| 4298 | } |
| 4299 | switch (known_errorHandler) { |
| 4300 | case 1: /* strict */ |
| 4301 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
| 4302 | goto onError; |
| 4303 | case 2: /* replace */ |
| 4304 | /* No need to check for space, this is a 1:1 replacement */ |
| 4305 | for (coll = collstart; coll<collend; ++coll) |
| 4306 | *str++ = '?'; |
| 4307 | /* fall through */ |
| 4308 | case 3: /* ignore */ |
| 4309 | p = collend; |
| 4310 | break; |
| 4311 | case 4: /* xmlcharrefreplace */ |
| 4312 | /* generate replacement (temporarily (mis)uses p) */ |
| 4313 | for (p = collstart; p < collend; ++p) { |
| 4314 | char buffer[2+29+1+1]; |
| 4315 | char *cp; |
| 4316 | sprintf(buffer, "&#%d;", (int)*p); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4317 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4318 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 4319 | goto onError; |
| 4320 | for (cp = buffer; *cp; ++cp) |
| 4321 | *str++ = *cp; |
| 4322 | } |
| 4323 | p = collend; |
| 4324 | break; |
| 4325 | default: |
| 4326 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 4327 | reason, startp, size, &exc, |
| 4328 | collstart-startp, collend-startp, &newpos); |
| 4329 | if (repunicode == NULL) |
| 4330 | goto onError; |
| 4331 | /* generate replacement */ |
| 4332 | repsize = PyUnicode_GET_SIZE(repunicode); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4333 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4334 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 4335 | Py_DECREF(repunicode); |
| 4336 | goto onError; |
| 4337 | } |
| 4338 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 4339 | *str++ = *uni2; |
| 4340 | p = startp + newpos; |
| 4341 | Py_DECREF(repunicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4342 | } |
| 4343 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4344 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4345 | /* Resize if we allocated to much */ |
| 4346 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4347 | if (respos<PyUnicode_GET_SIZE(res)) { |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4348 | if (_PyUnicode_Resize(&res, respos) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 4349 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4350 | } |
| 4351 | Py_XDECREF(exc); |
| 4352 | Py_XDECREF(errorHandler); |
| 4353 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4354 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4355 | onError: |
| 4356 | Py_XDECREF(res); |
| 4357 | Py_XDECREF(exc); |
| 4358 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4359 | return NULL; |
| 4360 | } |
| 4361 | |
| 4362 | PyObject *PyUnicode_Translate(PyObject *str, |
| 4363 | PyObject *mapping, |
| 4364 | const char *errors) |
| 4365 | { |
| 4366 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4367 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4368 | str = PyUnicode_FromObject(str); |
| 4369 | if (str == NULL) |
| 4370 | goto onError; |
| 4371 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 4372 | PyUnicode_GET_SIZE(str), |
| 4373 | mapping, |
| 4374 | errors); |
| 4375 | Py_DECREF(str); |
| 4376 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4377 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4378 | onError: |
| 4379 | Py_XDECREF(str); |
| 4380 | return NULL; |
| 4381 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4382 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4383 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 4384 | |
| 4385 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4386 | Py_ssize_t length, |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4387 | char *output, |
| 4388 | const char *errors) |
| 4389 | { |
| 4390 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4391 | PyObject *errorHandler = NULL; |
| 4392 | PyObject *exc = NULL; |
| 4393 | const char *encoding = "decimal"; |
| 4394 | const char *reason = "invalid decimal Unicode string"; |
| 4395 | /* the following variable is used for caching string comparisons |
| 4396 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 4397 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4398 | |
| 4399 | if (output == NULL) { |
| 4400 | PyErr_BadArgument(); |
| 4401 | return -1; |
| 4402 | } |
| 4403 | |
| 4404 | p = s; |
| 4405 | end = s + length; |
| 4406 | while (p < end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4407 | register Py_UNICODE ch = *p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4408 | int decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4409 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4410 | Py_ssize_t repsize; |
| 4411 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4412 | Py_UNICODE *uni2; |
| 4413 | Py_UNICODE *collstart; |
| 4414 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4415 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4416 | if (Py_UNICODE_ISSPACE(ch)) { |
| 4417 | *output++ = ' '; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4418 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4419 | continue; |
| 4420 | } |
| 4421 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 4422 | if (decimal >= 0) { |
| 4423 | *output++ = '0' + decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4424 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4425 | continue; |
| 4426 | } |
Guido van Rossum | ba47704 | 2000-04-06 18:18:10 +0000 | [diff] [blame] | 4427 | if (0 < ch && ch < 256) { |
Guido van Rossum | 42c29aa | 2000-05-03 23:58:29 +0000 | [diff] [blame] | 4428 | *output++ = (char)ch; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4429 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4430 | continue; |
| 4431 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4432 | /* All other characters are considered unencodable */ |
| 4433 | collstart = p; |
| 4434 | collend = p+1; |
| 4435 | while (collend < end) { |
| 4436 | if ((0 < *collend && *collend < 256) || |
| 4437 | !Py_UNICODE_ISSPACE(*collend) || |
| 4438 | Py_UNICODE_TODECIMAL(*collend)) |
| 4439 | break; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4440 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4441 | /* cache callback name lookup |
| 4442 | * (if not done yet, i.e. it's the first error) */ |
| 4443 | if (known_errorHandler==-1) { |
| 4444 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4445 | known_errorHandler = 1; |
| 4446 | else if (!strcmp(errors, "replace")) |
| 4447 | known_errorHandler = 2; |
| 4448 | else if (!strcmp(errors, "ignore")) |
| 4449 | known_errorHandler = 3; |
| 4450 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4451 | known_errorHandler = 4; |
| 4452 | else |
| 4453 | known_errorHandler = 0; |
| 4454 | } |
| 4455 | switch (known_errorHandler) { |
| 4456 | case 1: /* strict */ |
| 4457 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 4458 | goto onError; |
| 4459 | case 2: /* replace */ |
| 4460 | for (p = collstart; p < collend; ++p) |
| 4461 | *output++ = '?'; |
| 4462 | /* fall through */ |
| 4463 | case 3: /* ignore */ |
| 4464 | p = collend; |
| 4465 | break; |
| 4466 | case 4: /* xmlcharrefreplace */ |
| 4467 | /* generate replacement (temporarily (mis)uses p) */ |
| 4468 | for (p = collstart; p < collend; ++p) |
| 4469 | output += sprintf(output, "&#%d;", (int)*p); |
| 4470 | p = collend; |
| 4471 | break; |
| 4472 | default: |
| 4473 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 4474 | encoding, reason, s, length, &exc, |
| 4475 | collstart-s, collend-s, &newpos); |
| 4476 | if (repunicode == NULL) |
| 4477 | goto onError; |
| 4478 | /* generate replacement */ |
| 4479 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4480 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 4481 | Py_UNICODE ch = *uni2; |
| 4482 | if (Py_UNICODE_ISSPACE(ch)) |
| 4483 | *output++ = ' '; |
| 4484 | else { |
| 4485 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 4486 | if (decimal >= 0) |
| 4487 | *output++ = '0' + decimal; |
| 4488 | else if (0 < ch && ch < 256) |
| 4489 | *output++ = (char)ch; |
| 4490 | else { |
| 4491 | Py_DECREF(repunicode); |
| 4492 | raise_encode_exception(&exc, encoding, |
| 4493 | s, length, collstart-s, collend-s, reason); |
| 4494 | goto onError; |
| 4495 | } |
| 4496 | } |
| 4497 | } |
| 4498 | p = s + newpos; |
| 4499 | Py_DECREF(repunicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4500 | } |
| 4501 | } |
| 4502 | /* 0-terminate the output string */ |
| 4503 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4504 | Py_XDECREF(exc); |
| 4505 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4506 | return 0; |
| 4507 | |
| 4508 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4509 | Py_XDECREF(exc); |
| 4510 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4511 | return -1; |
| 4512 | } |
| 4513 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4514 | /* --- Helpers ------------------------------------------------------------ */ |
| 4515 | |
Fredrik Lundh | a50d201 | 2006-05-26 17:04:58 +0000 | [diff] [blame] | 4516 | #define STRINGLIB_CHAR Py_UNICODE |
Fredrik Lundh | 6471ee4 | 2006-05-24 14:28:11 +0000 | [diff] [blame] | 4517 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 4518 | #define STRINGLIB_LEN PyUnicode_GET_SIZE |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 4519 | #define STRINGLIB_NEW PyUnicode_FromUnicode |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 4520 | #define STRINGLIB_STR PyUnicode_AS_UNICODE |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 4521 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 4522 | Py_LOCAL_INLINE(int) |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 4523 | STRINGLIB_CMP(const Py_UNICODE* str, const Py_UNICODE* other, Py_ssize_t len) |
| 4524 | { |
Fredrik Lundh | 9c0e9c0 | 2006-05-26 18:24:15 +0000 | [diff] [blame] | 4525 | if (str[0] != other[0]) |
| 4526 | return 1; |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 4527 | return memcmp((void*) str, (void*) other, len * sizeof(Py_UNICODE)); |
| 4528 | } |
| 4529 | |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 4530 | #define STRINGLIB_EMPTY unicode_empty |
| 4531 | |
Fredrik Lundh | a50d201 | 2006-05-26 17:04:58 +0000 | [diff] [blame] | 4532 | #include "stringlib/fastsearch.h" |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 4533 | |
| 4534 | #include "stringlib/count.h" |
| 4535 | #include "stringlib/find.h" |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 4536 | #include "stringlib/partition.h" |
| 4537 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 4538 | /* helper macro to fixup start/end slice values */ |
| 4539 | #define FIX_START_END(obj) \ |
| 4540 | if (start < 0) \ |
| 4541 | start += (obj)->length; \ |
| 4542 | if (start < 0) \ |
| 4543 | start = 0; \ |
| 4544 | if (end > (obj)->length) \ |
| 4545 | end = (obj)->length; \ |
| 4546 | if (end < 0) \ |
| 4547 | end += (obj)->length; \ |
| 4548 | if (end < 0) \ |
| 4549 | end = 0; |
| 4550 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4551 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 4552 | PyObject *substr, |
| 4553 | Py_ssize_t start, |
| 4554 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4555 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4556 | Py_ssize_t result; |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 4557 | PyUnicodeObject* str_obj; |
| 4558 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4559 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 4560 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 4561 | if (!str_obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4562 | return -1; |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 4563 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 4564 | if (!sub_obj) { |
| 4565 | Py_DECREF(str_obj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4566 | return -1; |
| 4567 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4568 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 4569 | FIX_START_END(str_obj); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4570 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 4571 | result = stringlib_count( |
| 4572 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
| 4573 | ); |
| 4574 | |
| 4575 | Py_DECREF(sub_obj); |
| 4576 | Py_DECREF(str_obj); |
| 4577 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4578 | return result; |
| 4579 | } |
| 4580 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4581 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 4582 | PyObject *sub, |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 4583 | Py_ssize_t start, |
| 4584 | Py_ssize_t end, |
| 4585 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4586 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4587 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4588 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 4589 | str = PyUnicode_FromObject(str); |
| 4590 | if (!str) |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 4591 | return -2; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 4592 | sub = PyUnicode_FromObject(sub); |
| 4593 | if (!sub) { |
| 4594 | Py_DECREF(str); |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 4595 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4596 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4597 | |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 4598 | if (direction > 0) |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 4599 | result = stringlib_find_slice( |
| 4600 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 4601 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 4602 | start, end |
| 4603 | ); |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 4604 | else |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 4605 | result = stringlib_rfind_slice( |
| 4606 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 4607 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 4608 | start, end |
| 4609 | ); |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 4610 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 4611 | Py_DECREF(str); |
| 4612 | Py_DECREF(sub); |
| 4613 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4614 | return result; |
| 4615 | } |
| 4616 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4617 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4618 | int tailmatch(PyUnicodeObject *self, |
| 4619 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4620 | Py_ssize_t start, |
| 4621 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4622 | int direction) |
| 4623 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4624 | if (substring->length == 0) |
| 4625 | return 1; |
| 4626 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 4627 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4628 | |
| 4629 | end -= substring->length; |
| 4630 | if (end < start) |
| 4631 | return 0; |
| 4632 | |
| 4633 | if (direction > 0) { |
| 4634 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 4635 | return 1; |
| 4636 | } else { |
| 4637 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 4638 | return 1; |
| 4639 | } |
| 4640 | |
| 4641 | return 0; |
| 4642 | } |
| 4643 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4644 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4645 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4646 | Py_ssize_t start, |
| 4647 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4648 | int direction) |
| 4649 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4650 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4651 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4652 | str = PyUnicode_FromObject(str); |
| 4653 | if (str == NULL) |
| 4654 | return -1; |
| 4655 | substr = PyUnicode_FromObject(substr); |
| 4656 | if (substr == NULL) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 4657 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4658 | return -1; |
| 4659 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4660 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4661 | result = tailmatch((PyUnicodeObject *)str, |
| 4662 | (PyUnicodeObject *)substr, |
| 4663 | start, end, direction); |
| 4664 | Py_DECREF(str); |
| 4665 | Py_DECREF(substr); |
| 4666 | return result; |
| 4667 | } |
| 4668 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4669 | /* Apply fixfct filter to the Unicode object self and return a |
| 4670 | reference to the modified object */ |
| 4671 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4672 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4673 | PyObject *fixup(PyUnicodeObject *self, |
| 4674 | int (*fixfct)(PyUnicodeObject *s)) |
| 4675 | { |
| 4676 | |
| 4677 | PyUnicodeObject *u; |
| 4678 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4679 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4680 | if (u == NULL) |
| 4681 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4682 | |
| 4683 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 4684 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4685 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4686 | /* fixfct should return TRUE if it modified the buffer. If |
| 4687 | FALSE, return a reference to the original buffer instead |
| 4688 | (to save space, not time) */ |
| 4689 | Py_INCREF(self); |
| 4690 | Py_DECREF(u); |
| 4691 | return (PyObject*) self; |
| 4692 | } |
| 4693 | return (PyObject*) u; |
| 4694 | } |
| 4695 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4696 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4697 | int fixupper(PyUnicodeObject *self) |
| 4698 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4699 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4700 | Py_UNICODE *s = self->str; |
| 4701 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4702 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4703 | while (len-- > 0) { |
| 4704 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4705 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4706 | ch = Py_UNICODE_TOUPPER(*s); |
| 4707 | if (ch != *s) { |
| 4708 | status = 1; |
| 4709 | *s = ch; |
| 4710 | } |
| 4711 | s++; |
| 4712 | } |
| 4713 | |
| 4714 | return status; |
| 4715 | } |
| 4716 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4717 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4718 | int fixlower(PyUnicodeObject *self) |
| 4719 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4720 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4721 | Py_UNICODE *s = self->str; |
| 4722 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4723 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4724 | while (len-- > 0) { |
| 4725 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4726 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4727 | ch = Py_UNICODE_TOLOWER(*s); |
| 4728 | if (ch != *s) { |
| 4729 | status = 1; |
| 4730 | *s = ch; |
| 4731 | } |
| 4732 | s++; |
| 4733 | } |
| 4734 | |
| 4735 | return status; |
| 4736 | } |
| 4737 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4738 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4739 | int fixswapcase(PyUnicodeObject *self) |
| 4740 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4741 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4742 | Py_UNICODE *s = self->str; |
| 4743 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4744 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4745 | while (len-- > 0) { |
| 4746 | if (Py_UNICODE_ISUPPER(*s)) { |
| 4747 | *s = Py_UNICODE_TOLOWER(*s); |
| 4748 | status = 1; |
| 4749 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 4750 | *s = Py_UNICODE_TOUPPER(*s); |
| 4751 | status = 1; |
| 4752 | } |
| 4753 | s++; |
| 4754 | } |
| 4755 | |
| 4756 | return status; |
| 4757 | } |
| 4758 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4759 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4760 | int fixcapitalize(PyUnicodeObject *self) |
| 4761 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4762 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4763 | Py_UNICODE *s = self->str; |
| 4764 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4765 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4766 | if (len == 0) |
| 4767 | return 0; |
| 4768 | if (Py_UNICODE_ISLOWER(*s)) { |
| 4769 | *s = Py_UNICODE_TOUPPER(*s); |
| 4770 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4771 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4772 | s++; |
| 4773 | while (--len > 0) { |
| 4774 | if (Py_UNICODE_ISUPPER(*s)) { |
| 4775 | *s = Py_UNICODE_TOLOWER(*s); |
| 4776 | status = 1; |
| 4777 | } |
| 4778 | s++; |
| 4779 | } |
| 4780 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4781 | } |
| 4782 | |
| 4783 | static |
| 4784 | int fixtitle(PyUnicodeObject *self) |
| 4785 | { |
| 4786 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 4787 | register Py_UNICODE *e; |
| 4788 | int previous_is_cased; |
| 4789 | |
| 4790 | /* Shortcut for single character strings */ |
| 4791 | if (PyUnicode_GET_SIZE(self) == 1) { |
| 4792 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 4793 | if (*p != ch) { |
| 4794 | *p = ch; |
| 4795 | return 1; |
| 4796 | } |
| 4797 | else |
| 4798 | return 0; |
| 4799 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4800 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4801 | e = p + PyUnicode_GET_SIZE(self); |
| 4802 | previous_is_cased = 0; |
| 4803 | for (; p < e; p++) { |
| 4804 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4805 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4806 | if (previous_is_cased) |
| 4807 | *p = Py_UNICODE_TOLOWER(ch); |
| 4808 | else |
| 4809 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4810 | |
| 4811 | if (Py_UNICODE_ISLOWER(ch) || |
| 4812 | Py_UNICODE_ISUPPER(ch) || |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4813 | Py_UNICODE_ISTITLE(ch)) |
| 4814 | previous_is_cased = 1; |
| 4815 | else |
| 4816 | previous_is_cased = 0; |
| 4817 | } |
| 4818 | return 1; |
| 4819 | } |
| 4820 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4821 | PyObject * |
| 4822 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4823 | { |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4824 | PyObject *internal_separator = NULL; |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 4825 | const Py_UNICODE blank = ' '; |
| 4826 | const Py_UNICODE *sep = ␣ |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4827 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4828 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4829 | Py_ssize_t res_alloc = 100; /* # allocated bytes for string in res */ |
| 4830 | Py_ssize_t res_used; /* # used bytes */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4831 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 4832 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4833 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4834 | PyObject *item; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4835 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4836 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4837 | fseq = PySequence_Fast(seq, ""); |
| 4838 | if (fseq == NULL) { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4839 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4840 | } |
| 4841 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4842 | /* Grrrr. A codec may be invoked to convert str objects to |
| 4843 | * Unicode, and so it's possible to call back into Python code |
| 4844 | * during PyUnicode_FromObject(), and so it's possible for a sick |
| 4845 | * codec to change the size of fseq (if seq is a list). Therefore |
| 4846 | * we have to keep refetching the size -- can't assume seqlen |
| 4847 | * is invariant. |
| 4848 | */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4849 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 4850 | /* If empty sequence, return u"". */ |
| 4851 | if (seqlen == 0) { |
| 4852 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 4853 | goto Done; |
| 4854 | } |
| 4855 | /* If singleton sequence with an exact Unicode, return that. */ |
| 4856 | if (seqlen == 1) { |
| 4857 | item = PySequence_Fast_GET_ITEM(fseq, 0); |
| 4858 | if (PyUnicode_CheckExact(item)) { |
| 4859 | Py_INCREF(item); |
| 4860 | res = (PyUnicodeObject *)item; |
| 4861 | goto Done; |
| 4862 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4863 | } |
| 4864 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4865 | /* At least two items to join, or one that isn't exact Unicode. */ |
| 4866 | if (seqlen > 1) { |
| 4867 | /* Set up sep and seplen -- they're needed. */ |
| 4868 | if (separator == NULL) { |
| 4869 | sep = ␣ |
| 4870 | seplen = 1; |
| 4871 | } |
| 4872 | else { |
| 4873 | internal_separator = PyUnicode_FromObject(separator); |
| 4874 | if (internal_separator == NULL) |
| 4875 | goto onError; |
| 4876 | sep = PyUnicode_AS_UNICODE(internal_separator); |
| 4877 | seplen = PyUnicode_GET_SIZE(internal_separator); |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4878 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 4879 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4880 | } |
| 4881 | } |
| 4882 | |
| 4883 | /* Get space. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4884 | res = _PyUnicode_New(res_alloc); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4885 | if (res == NULL) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4886 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4887 | res_p = PyUnicode_AS_UNICODE(res); |
| 4888 | res_used = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4889 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4890 | for (i = 0; i < seqlen; ++i) { |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4891 | Py_ssize_t itemlen; |
| 4892 | Py_ssize_t new_res_used; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4893 | |
| 4894 | item = PySequence_Fast_GET_ITEM(fseq, i); |
| 4895 | /* Convert item to Unicode. */ |
| 4896 | if (! PyUnicode_Check(item) && ! PyString_Check(item)) { |
| 4897 | PyErr_Format(PyExc_TypeError, |
Thomas Wouters | 715a4cd | 2006-04-16 22:04:49 +0000 | [diff] [blame] | 4898 | "sequence item %zd: expected string or Unicode," |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4899 | " %.80s found", |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 4900 | i, Py_Type(item)->tp_name); |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 4901 | goto onError; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4902 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4903 | item = PyUnicode_FromObject(item); |
| 4904 | if (item == NULL) |
| 4905 | goto onError; |
| 4906 | /* We own a reference to item from here on. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4907 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4908 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 4909 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 4910 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4911 | /* 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] | 4912 | itemlen = PyUnicode_GET_SIZE(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4913 | new_res_used = res_used + itemlen; |
Georg Brandl | 90e27d3 | 2006-06-10 06:40:50 +0000 | [diff] [blame] | 4914 | if (new_res_used < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4915 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4916 | if (i < seqlen - 1) { |
| 4917 | new_res_used += seplen; |
Georg Brandl | 90e27d3 | 2006-06-10 06:40:50 +0000 | [diff] [blame] | 4918 | if (new_res_used < 0) |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4919 | goto Overflow; |
| 4920 | } |
| 4921 | if (new_res_used > res_alloc) { |
| 4922 | /* double allocated size until it's big enough */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4923 | do { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4924 | res_alloc += res_alloc; |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4925 | if (res_alloc <= 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4926 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4927 | } while (new_res_used > res_alloc); |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4928 | if (_PyUnicode_Resize(&res, res_alloc) < 0) { |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4929 | Py_DECREF(item); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4930 | goto onError; |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4931 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4932 | res_p = PyUnicode_AS_UNICODE(res) + res_used; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4933 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4934 | |
| 4935 | /* Copy item, and maybe the separator. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4936 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4937 | res_p += itemlen; |
| 4938 | if (i < seqlen - 1) { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4939 | Py_UNICODE_COPY(res_p, sep, seplen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4940 | res_p += seplen; |
| 4941 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4942 | Py_DECREF(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4943 | res_used = new_res_used; |
| 4944 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4945 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4946 | /* Shrink res to match the used area; this probably can't fail, |
| 4947 | * but it's cheap to check. |
| 4948 | */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4949 | if (_PyUnicode_Resize(&res, res_used) < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4950 | goto onError; |
| 4951 | |
| 4952 | Done: |
| 4953 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4954 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4955 | return (PyObject *)res; |
| 4956 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4957 | Overflow: |
| 4958 | PyErr_SetString(PyExc_OverflowError, |
Georg Brandl | 90e27d3 | 2006-06-10 06:40:50 +0000 | [diff] [blame] | 4959 | "join() result is too long for a Python string"); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4960 | Py_DECREF(item); |
| 4961 | /* fall through */ |
| 4962 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4963 | onError: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4964 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4965 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4966 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4967 | return NULL; |
| 4968 | } |
| 4969 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4970 | static |
| 4971 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4972 | Py_ssize_t left, |
| 4973 | Py_ssize_t right, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4974 | Py_UNICODE fill) |
| 4975 | { |
| 4976 | PyUnicodeObject *u; |
| 4977 | |
| 4978 | if (left < 0) |
| 4979 | left = 0; |
| 4980 | if (right < 0) |
| 4981 | right = 0; |
| 4982 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4983 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4984 | Py_INCREF(self); |
| 4985 | return self; |
| 4986 | } |
| 4987 | |
| 4988 | u = _PyUnicode_New(left + self->length + right); |
| 4989 | if (u) { |
| 4990 | if (left) |
| 4991 | Py_UNICODE_FILL(u->str, fill, left); |
| 4992 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 4993 | if (right) |
| 4994 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 4995 | } |
| 4996 | |
| 4997 | return u; |
| 4998 | } |
| 4999 | |
| 5000 | #define SPLIT_APPEND(data, left, right) \ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5001 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5002 | if (!str) \ |
| 5003 | goto onError; \ |
| 5004 | if (PyList_Append(list, str)) { \ |
| 5005 | Py_DECREF(str); \ |
| 5006 | goto onError; \ |
| 5007 | } \ |
| 5008 | else \ |
| 5009 | Py_DECREF(str); |
| 5010 | |
| 5011 | static |
| 5012 | PyObject *split_whitespace(PyUnicodeObject *self, |
| 5013 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5014 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5015 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5016 | register Py_ssize_t i; |
| 5017 | register Py_ssize_t j; |
| 5018 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5019 | PyObject *str; |
| 5020 | |
| 5021 | for (i = j = 0; i < len; ) { |
| 5022 | /* find a token */ |
| 5023 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 5024 | i++; |
| 5025 | j = i; |
| 5026 | while (i < len && !Py_UNICODE_ISSPACE(self->str[i])) |
| 5027 | i++; |
| 5028 | if (j < i) { |
| 5029 | if (maxcount-- <= 0) |
| 5030 | break; |
| 5031 | SPLIT_APPEND(self->str, j, i); |
| 5032 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 5033 | i++; |
| 5034 | j = i; |
| 5035 | } |
| 5036 | } |
| 5037 | if (j < len) { |
| 5038 | SPLIT_APPEND(self->str, j, len); |
| 5039 | } |
| 5040 | return list; |
| 5041 | |
| 5042 | onError: |
| 5043 | Py_DECREF(list); |
| 5044 | return NULL; |
| 5045 | } |
| 5046 | |
| 5047 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5048 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5049 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5050 | register Py_ssize_t i; |
| 5051 | register Py_ssize_t j; |
| 5052 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5053 | PyObject *list; |
| 5054 | PyObject *str; |
| 5055 | Py_UNICODE *data; |
| 5056 | |
| 5057 | string = PyUnicode_FromObject(string); |
| 5058 | if (string == NULL) |
| 5059 | return NULL; |
| 5060 | data = PyUnicode_AS_UNICODE(string); |
| 5061 | len = PyUnicode_GET_SIZE(string); |
| 5062 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5063 | list = PyList_New(0); |
| 5064 | if (!list) |
| 5065 | goto onError; |
| 5066 | |
| 5067 | for (i = j = 0; i < len; ) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5068 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5069 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5070 | /* Find a line and append it */ |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5071 | while (i < len && !BLOOM_LINEBREAK(data[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5072 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5073 | |
| 5074 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5075 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5076 | if (i < len) { |
| 5077 | if (data[i] == '\r' && i + 1 < len && |
| 5078 | data[i+1] == '\n') |
| 5079 | i += 2; |
| 5080 | else |
| 5081 | i++; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5082 | if (keepends) |
| 5083 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5084 | } |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5085 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5086 | j = i; |
| 5087 | } |
| 5088 | if (j < len) { |
| 5089 | SPLIT_APPEND(data, j, len); |
| 5090 | } |
| 5091 | |
| 5092 | Py_DECREF(string); |
| 5093 | return list; |
| 5094 | |
| 5095 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5096 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5097 | Py_DECREF(string); |
| 5098 | return NULL; |
| 5099 | } |
| 5100 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5101 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5102 | PyObject *split_char(PyUnicodeObject *self, |
| 5103 | PyObject *list, |
| 5104 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5105 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5106 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5107 | register Py_ssize_t i; |
| 5108 | register Py_ssize_t j; |
| 5109 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5110 | PyObject *str; |
| 5111 | |
| 5112 | for (i = j = 0; i < len; ) { |
| 5113 | if (self->str[i] == ch) { |
| 5114 | if (maxcount-- <= 0) |
| 5115 | break; |
| 5116 | SPLIT_APPEND(self->str, j, i); |
| 5117 | i = j = i + 1; |
| 5118 | } else |
| 5119 | i++; |
| 5120 | } |
| 5121 | if (j <= len) { |
| 5122 | SPLIT_APPEND(self->str, j, len); |
| 5123 | } |
| 5124 | return list; |
| 5125 | |
| 5126 | onError: |
| 5127 | Py_DECREF(list); |
| 5128 | return NULL; |
| 5129 | } |
| 5130 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5131 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5132 | PyObject *split_substring(PyUnicodeObject *self, |
| 5133 | PyObject *list, |
| 5134 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5135 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5136 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5137 | register Py_ssize_t i; |
| 5138 | register Py_ssize_t j; |
| 5139 | Py_ssize_t len = self->length; |
| 5140 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5141 | PyObject *str; |
| 5142 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 5143 | for (i = j = 0; i <= len - sublen; ) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5144 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5145 | if (maxcount-- <= 0) |
| 5146 | break; |
| 5147 | SPLIT_APPEND(self->str, j, i); |
| 5148 | i = j = i + sublen; |
| 5149 | } else |
| 5150 | i++; |
| 5151 | } |
| 5152 | if (j <= len) { |
| 5153 | SPLIT_APPEND(self->str, j, len); |
| 5154 | } |
| 5155 | return list; |
| 5156 | |
| 5157 | onError: |
| 5158 | Py_DECREF(list); |
| 5159 | return NULL; |
| 5160 | } |
| 5161 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5162 | static |
| 5163 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
| 5164 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5165 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5166 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5167 | register Py_ssize_t i; |
| 5168 | register Py_ssize_t j; |
| 5169 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5170 | PyObject *str; |
| 5171 | |
| 5172 | for (i = j = len - 1; i >= 0; ) { |
| 5173 | /* find a token */ |
| 5174 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 5175 | i--; |
| 5176 | j = i; |
| 5177 | while (i >= 0 && !Py_UNICODE_ISSPACE(self->str[i])) |
| 5178 | i--; |
| 5179 | if (j > i) { |
| 5180 | if (maxcount-- <= 0) |
| 5181 | break; |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5182 | SPLIT_APPEND(self->str, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5183 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 5184 | i--; |
| 5185 | j = i; |
| 5186 | } |
| 5187 | } |
| 5188 | if (j >= 0) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5189 | SPLIT_APPEND(self->str, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5190 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5191 | if (PyList_Reverse(list) < 0) |
| 5192 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5193 | return list; |
| 5194 | |
| 5195 | onError: |
| 5196 | Py_DECREF(list); |
| 5197 | return NULL; |
| 5198 | } |
| 5199 | |
| 5200 | static |
| 5201 | PyObject *rsplit_char(PyUnicodeObject *self, |
| 5202 | PyObject *list, |
| 5203 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5204 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5205 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5206 | register Py_ssize_t i; |
| 5207 | register Py_ssize_t j; |
| 5208 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5209 | PyObject *str; |
| 5210 | |
| 5211 | for (i = j = len - 1; i >= 0; ) { |
| 5212 | if (self->str[i] == ch) { |
| 5213 | if (maxcount-- <= 0) |
| 5214 | break; |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5215 | SPLIT_APPEND(self->str, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5216 | j = i = i - 1; |
| 5217 | } else |
| 5218 | i--; |
| 5219 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 5220 | if (j >= -1) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5221 | SPLIT_APPEND(self->str, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5222 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5223 | if (PyList_Reverse(list) < 0) |
| 5224 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5225 | return list; |
| 5226 | |
| 5227 | onError: |
| 5228 | Py_DECREF(list); |
| 5229 | return NULL; |
| 5230 | } |
| 5231 | |
| 5232 | static |
| 5233 | PyObject *rsplit_substring(PyUnicodeObject *self, |
| 5234 | PyObject *list, |
| 5235 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5236 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5237 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5238 | register Py_ssize_t i; |
| 5239 | register Py_ssize_t j; |
| 5240 | Py_ssize_t len = self->length; |
| 5241 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5242 | PyObject *str; |
| 5243 | |
| 5244 | for (i = len - sublen, j = len; i >= 0; ) { |
| 5245 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5246 | if (maxcount-- <= 0) |
| 5247 | break; |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5248 | SPLIT_APPEND(self->str, i + sublen, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5249 | j = i; |
| 5250 | i -= sublen; |
| 5251 | } else |
| 5252 | i--; |
| 5253 | } |
| 5254 | if (j >= 0) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5255 | SPLIT_APPEND(self->str, 0, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5256 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5257 | if (PyList_Reverse(list) < 0) |
| 5258 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5259 | return list; |
| 5260 | |
| 5261 | onError: |
| 5262 | Py_DECREF(list); |
| 5263 | return NULL; |
| 5264 | } |
| 5265 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5266 | #undef SPLIT_APPEND |
| 5267 | |
| 5268 | static |
| 5269 | PyObject *split(PyUnicodeObject *self, |
| 5270 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5271 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5272 | { |
| 5273 | PyObject *list; |
| 5274 | |
| 5275 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5276 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5277 | |
| 5278 | list = PyList_New(0); |
| 5279 | if (!list) |
| 5280 | return NULL; |
| 5281 | |
| 5282 | if (substring == NULL) |
| 5283 | return split_whitespace(self,list,maxcount); |
| 5284 | |
| 5285 | else if (substring->length == 1) |
| 5286 | return split_char(self,list,substring->str[0],maxcount); |
| 5287 | |
| 5288 | else if (substring->length == 0) { |
| 5289 | Py_DECREF(list); |
| 5290 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5291 | return NULL; |
| 5292 | } |
| 5293 | else |
| 5294 | return split_substring(self,list,substring,maxcount); |
| 5295 | } |
| 5296 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5297 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5298 | PyObject *rsplit(PyUnicodeObject *self, |
| 5299 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5300 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5301 | { |
| 5302 | PyObject *list; |
| 5303 | |
| 5304 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5305 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5306 | |
| 5307 | list = PyList_New(0); |
| 5308 | if (!list) |
| 5309 | return NULL; |
| 5310 | |
| 5311 | if (substring == NULL) |
| 5312 | return rsplit_whitespace(self,list,maxcount); |
| 5313 | |
| 5314 | else if (substring->length == 1) |
| 5315 | return rsplit_char(self,list,substring->str[0],maxcount); |
| 5316 | |
| 5317 | else if (substring->length == 0) { |
| 5318 | Py_DECREF(list); |
| 5319 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5320 | return NULL; |
| 5321 | } |
| 5322 | else |
| 5323 | return rsplit_substring(self,list,substring,maxcount); |
| 5324 | } |
| 5325 | |
| 5326 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5327 | PyObject *replace(PyUnicodeObject *self, |
| 5328 | PyUnicodeObject *str1, |
| 5329 | PyUnicodeObject *str2, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5330 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5331 | { |
| 5332 | PyUnicodeObject *u; |
| 5333 | |
| 5334 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5335 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5336 | |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5337 | if (str1->length == str2->length) { |
| 5338 | /* same length */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5339 | Py_ssize_t i; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5340 | if (str1->length == 1) { |
| 5341 | /* replace characters */ |
| 5342 | Py_UNICODE u1, u2; |
| 5343 | if (!findchar(self->str, self->length, str1->str[0])) |
| 5344 | goto nothing; |
| 5345 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5346 | if (!u) |
| 5347 | return NULL; |
| 5348 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5349 | u1 = str1->str[0]; |
| 5350 | u2 = str2->str[0]; |
| 5351 | for (i = 0; i < u->length; i++) |
| 5352 | if (u->str[i] == u1) { |
| 5353 | if (--maxcount < 0) |
| 5354 | break; |
| 5355 | u->str[i] = u2; |
| 5356 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5357 | } else { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5358 | i = fastsearch( |
| 5359 | self->str, self->length, str1->str, str1->length, FAST_SEARCH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5360 | ); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5361 | if (i < 0) |
| 5362 | goto nothing; |
| 5363 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5364 | if (!u) |
| 5365 | return NULL; |
| 5366 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5367 | while (i <= self->length - str1->length) |
| 5368 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 5369 | if (--maxcount < 0) |
| 5370 | break; |
| 5371 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 5372 | i += str1->length; |
| 5373 | } else |
| 5374 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5375 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5376 | } else { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5377 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 5378 | Py_ssize_t n, i, j, e; |
Fredrik Lundh | 0c71f88 | 2006-05-25 16:46:54 +0000 | [diff] [blame] | 5379 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5380 | Py_UNICODE *p; |
| 5381 | |
| 5382 | /* replace strings */ |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5383 | n = stringlib_count(self->str, self->length, str1->str, str1->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5384 | if (n > maxcount) |
| 5385 | n = maxcount; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5386 | if (n == 0) |
| 5387 | goto nothing; |
Fredrik Lundh | 0c71f88 | 2006-05-25 16:46:54 +0000 | [diff] [blame] | 5388 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 5389 | delta = (str2->length - str1->length); |
| 5390 | if (delta == 0) { |
| 5391 | new_size = self->length; |
| 5392 | } else { |
| 5393 | product = n * (str2->length - str1->length); |
| 5394 | if ((product / (str2->length - str1->length)) != n) { |
| 5395 | PyErr_SetString(PyExc_OverflowError, |
| 5396 | "replace string is too long"); |
| 5397 | return NULL; |
| 5398 | } |
| 5399 | new_size = self->length + product; |
| 5400 | if (new_size < 0) { |
| 5401 | PyErr_SetString(PyExc_OverflowError, |
| 5402 | "replace string is too long"); |
| 5403 | return NULL; |
| 5404 | } |
| 5405 | } |
| 5406 | u = _PyUnicode_New(new_size); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5407 | if (!u) |
| 5408 | return NULL; |
| 5409 | i = 0; |
| 5410 | p = u->str; |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 5411 | e = self->length - str1->length; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5412 | if (str1->length > 0) { |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 5413 | while (n-- > 0) { |
| 5414 | /* look for next match */ |
| 5415 | j = i; |
| 5416 | while (j <= e) { |
| 5417 | if (Py_UNICODE_MATCH(self, j, str1)) |
| 5418 | break; |
| 5419 | j++; |
| 5420 | } |
| 5421 | if (j > i) { |
| 5422 | if (j > e) |
| 5423 | break; |
| 5424 | /* copy unchanged part [i:j] */ |
| 5425 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 5426 | p += j - i; |
| 5427 | } |
| 5428 | /* copy substitution string */ |
| 5429 | if (str2->length > 0) { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5430 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 5431 | p += str2->length; |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 5432 | } |
| 5433 | i = j + str1->length; |
| 5434 | } |
| 5435 | if (i < self->length) |
| 5436 | /* copy tail [i:] */ |
| 5437 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5438 | } else { |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 5439 | /* interleave */ |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5440 | while (n > 0) { |
| 5441 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 5442 | p += str2->length; |
| 5443 | if (--n <= 0) |
| 5444 | break; |
| 5445 | *p++ = self->str[i++]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5446 | } |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5447 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5448 | } |
| 5449 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5450 | return (PyObject *) u; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5451 | |
| 5452 | nothing: |
| 5453 | /* nothing to replace; return original string (when possible) */ |
| 5454 | if (PyUnicode_CheckExact(self)) { |
| 5455 | Py_INCREF(self); |
| 5456 | return (PyObject *) self; |
| 5457 | } |
| 5458 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5459 | } |
| 5460 | |
| 5461 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 5462 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5463 | PyDoc_STRVAR(title__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5464 | "S.title() -> unicode\n\ |
| 5465 | \n\ |
| 5466 | 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] | 5467 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5468 | |
| 5469 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5470 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5471 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5472 | return fixup(self, fixtitle); |
| 5473 | } |
| 5474 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5475 | PyDoc_STRVAR(capitalize__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5476 | "S.capitalize() -> unicode\n\ |
| 5477 | \n\ |
| 5478 | 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] | 5479 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5480 | |
| 5481 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5482 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5483 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5484 | return fixup(self, fixcapitalize); |
| 5485 | } |
| 5486 | |
| 5487 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5488 | PyDoc_STRVAR(capwords__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5489 | "S.capwords() -> unicode\n\ |
| 5490 | \n\ |
| 5491 | 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] | 5492 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5493 | |
| 5494 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5495 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5496 | { |
| 5497 | PyObject *list; |
| 5498 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5499 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5500 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5501 | /* Split into words */ |
| 5502 | list = split(self, NULL, -1); |
| 5503 | if (!list) |
| 5504 | return NULL; |
| 5505 | |
| 5506 | /* Capitalize each word */ |
| 5507 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 5508 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 5509 | fixcapitalize); |
| 5510 | if (item == NULL) |
| 5511 | goto onError; |
| 5512 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 5513 | PyList_SET_ITEM(list, i, item); |
| 5514 | } |
| 5515 | |
| 5516 | /* Join the words to form a new string */ |
| 5517 | item = PyUnicode_Join(NULL, list); |
| 5518 | |
| 5519 | onError: |
| 5520 | Py_DECREF(list); |
| 5521 | return (PyObject *)item; |
| 5522 | } |
| 5523 | #endif |
| 5524 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5525 | /* Argument converter. Coerces to a single unicode character */ |
| 5526 | |
| 5527 | static int |
| 5528 | convert_uc(PyObject *obj, void *addr) |
| 5529 | { |
| 5530 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 5531 | PyObject *uniobj; |
| 5532 | Py_UNICODE *unistr; |
| 5533 | |
| 5534 | uniobj = PyUnicode_FromObject(obj); |
| 5535 | if (uniobj == NULL) { |
| 5536 | PyErr_SetString(PyExc_TypeError, |
| 5537 | "The fill character cannot be converted to Unicode"); |
| 5538 | return 0; |
| 5539 | } |
| 5540 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 5541 | PyErr_SetString(PyExc_TypeError, |
| 5542 | "The fill character must be exactly one character long"); |
| 5543 | Py_DECREF(uniobj); |
| 5544 | return 0; |
| 5545 | } |
| 5546 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 5547 | *fillcharloc = unistr[0]; |
| 5548 | Py_DECREF(uniobj); |
| 5549 | return 1; |
| 5550 | } |
| 5551 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5552 | PyDoc_STRVAR(center__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5553 | "S.center(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5554 | \n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5555 | Return S centered in a Unicode string of length width. Padding is\n\ |
| 5556 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5557 | |
| 5558 | static PyObject * |
| 5559 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 5560 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5561 | Py_ssize_t marg, left; |
| 5562 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5563 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5564 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 5565 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5566 | return NULL; |
| 5567 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5568 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5569 | Py_INCREF(self); |
| 5570 | return (PyObject*) self; |
| 5571 | } |
| 5572 | |
| 5573 | marg = width - self->length; |
| 5574 | left = marg / 2 + (marg & width & 1); |
| 5575 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5576 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5577 | } |
| 5578 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5579 | #if 0 |
| 5580 | |
| 5581 | /* This code should go into some future Unicode collation support |
| 5582 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 5583 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5584 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5585 | /* speedy UTF-16 code point order comparison */ |
| 5586 | /* gleaned from: */ |
| 5587 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 5588 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 5589 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5590 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5591 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5592 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 5593 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 5594 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5595 | }; |
| 5596 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5597 | static int |
| 5598 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 5599 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5600 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5601 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5602 | Py_UNICODE *s1 = str1->str; |
| 5603 | Py_UNICODE *s2 = str2->str; |
| 5604 | |
| 5605 | len1 = str1->length; |
| 5606 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5607 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5608 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5609 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5610 | |
| 5611 | c1 = *s1++; |
| 5612 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 5613 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5614 | if (c1 > (1<<11) * 26) |
| 5615 | c1 += utf16Fixup[c1>>11]; |
| 5616 | if (c2 > (1<<11) * 26) |
| 5617 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5618 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 5619 | |
| 5620 | if (c1 != c2) |
| 5621 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5622 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5623 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5624 | } |
| 5625 | |
| 5626 | return (len1 < len2) ? -1 : (len1 != len2); |
| 5627 | } |
| 5628 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5629 | #else |
| 5630 | |
| 5631 | static int |
| 5632 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 5633 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5634 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5635 | |
| 5636 | Py_UNICODE *s1 = str1->str; |
| 5637 | Py_UNICODE *s2 = str2->str; |
| 5638 | |
| 5639 | len1 = str1->length; |
| 5640 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5641 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5642 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5643 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5644 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 5645 | c1 = *s1++; |
| 5646 | c2 = *s2++; |
| 5647 | |
| 5648 | if (c1 != c2) |
| 5649 | return (c1 < c2) ? -1 : 1; |
| 5650 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5651 | len1--; len2--; |
| 5652 | } |
| 5653 | |
| 5654 | return (len1 < len2) ? -1 : (len1 != len2); |
| 5655 | } |
| 5656 | |
| 5657 | #endif |
| 5658 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5659 | int PyUnicode_Compare(PyObject *left, |
| 5660 | PyObject *right) |
| 5661 | { |
| 5662 | PyUnicodeObject *u = NULL, *v = NULL; |
| 5663 | int result; |
| 5664 | |
| 5665 | /* Coerce the two arguments */ |
| 5666 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 5667 | if (u == NULL) |
| 5668 | goto onError; |
| 5669 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 5670 | if (v == NULL) |
| 5671 | goto onError; |
| 5672 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5673 | /* Shortcut for empty or interned objects */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5674 | if (v == u) { |
| 5675 | Py_DECREF(u); |
| 5676 | Py_DECREF(v); |
| 5677 | return 0; |
| 5678 | } |
| 5679 | |
| 5680 | result = unicode_compare(u, v); |
| 5681 | |
| 5682 | Py_DECREF(u); |
| 5683 | Py_DECREF(v); |
| 5684 | return result; |
| 5685 | |
| 5686 | onError: |
| 5687 | Py_XDECREF(u); |
| 5688 | Py_XDECREF(v); |
| 5689 | return -1; |
| 5690 | } |
| 5691 | |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 5692 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 5693 | PyObject *right, |
| 5694 | int op) |
| 5695 | { |
| 5696 | int result; |
| 5697 | |
| 5698 | result = PyUnicode_Compare(left, right); |
| 5699 | if (result == -1 && PyErr_Occurred()) |
| 5700 | goto onError; |
| 5701 | |
| 5702 | /* Convert the return value to a Boolean */ |
| 5703 | switch (op) { |
| 5704 | case Py_EQ: |
| 5705 | result = (result == 0); |
| 5706 | break; |
| 5707 | case Py_NE: |
| 5708 | result = (result != 0); |
| 5709 | break; |
| 5710 | case Py_LE: |
| 5711 | result = (result <= 0); |
| 5712 | break; |
| 5713 | case Py_GE: |
| 5714 | result = (result >= 0); |
| 5715 | break; |
| 5716 | case Py_LT: |
| 5717 | result = (result == -1); |
| 5718 | break; |
| 5719 | case Py_GT: |
| 5720 | result = (result == 1); |
| 5721 | break; |
| 5722 | } |
| 5723 | return PyBool_FromLong(result); |
| 5724 | |
| 5725 | onError: |
| 5726 | |
| 5727 | /* Standard case |
| 5728 | |
| 5729 | Type errors mean that PyUnicode_FromObject() could not convert |
| 5730 | one of the arguments (usually the right hand side) to Unicode, |
| 5731 | ie. we can't handle the comparison request. However, it is |
| 5732 | possible that the other object knows a comparison method, which |
| 5733 | is why we return Py_NotImplemented to give the other object a |
| 5734 | chance. |
| 5735 | |
| 5736 | */ |
| 5737 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 5738 | PyErr_Clear(); |
| 5739 | Py_INCREF(Py_NotImplemented); |
| 5740 | return Py_NotImplemented; |
| 5741 | } |
| 5742 | if (op != Py_EQ && op != Py_NE) |
| 5743 | return NULL; |
| 5744 | |
| 5745 | /* Equality comparison. |
| 5746 | |
| 5747 | This is a special case: we silence any PyExc_UnicodeDecodeError |
| 5748 | and instead turn it into a PyErr_UnicodeWarning. |
| 5749 | |
| 5750 | */ |
| 5751 | if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) |
| 5752 | return NULL; |
| 5753 | PyErr_Clear(); |
| 5754 | if (PyErr_Warn(PyExc_UnicodeWarning, |
| 5755 | (op == Py_EQ) ? |
| 5756 | "Unicode equal comparison " |
| 5757 | "failed to convert both arguments to Unicode - " |
| 5758 | "interpreting them as being unequal" : |
| 5759 | "Unicode unequal comparison " |
| 5760 | "failed to convert both arguments to Unicode - " |
| 5761 | "interpreting them as being unequal" |
| 5762 | ) < 0) |
| 5763 | return NULL; |
| 5764 | result = (op == Py_NE); |
| 5765 | return PyBool_FromLong(result); |
| 5766 | } |
| 5767 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5768 | int PyUnicode_Contains(PyObject *container, |
| 5769 | PyObject *element) |
| 5770 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5771 | PyObject *str, *sub; |
| 5772 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5773 | |
| 5774 | /* Coerce the two arguments */ |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5775 | sub = PyUnicode_FromObject(element); |
| 5776 | if (!sub) { |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 5777 | PyErr_SetString(PyExc_TypeError, |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 5778 | "'in <string>' requires string as left operand"); |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5779 | return -1; |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 5780 | } |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5781 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5782 | str = PyUnicode_FromObject(container); |
| 5783 | if (!str) { |
| 5784 | Py_DECREF(sub); |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5785 | return -1; |
| 5786 | } |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5787 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5788 | result = stringlib_contains_obj(str, sub); |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 5789 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5790 | Py_DECREF(str); |
| 5791 | Py_DECREF(sub); |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5792 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5793 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5794 | } |
| 5795 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5796 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 5797 | |
| 5798 | PyObject *PyUnicode_Concat(PyObject *left, |
| 5799 | PyObject *right) |
| 5800 | { |
| 5801 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 5802 | |
| 5803 | /* Coerce the two arguments */ |
| 5804 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 5805 | if (u == NULL) |
| 5806 | goto onError; |
| 5807 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 5808 | if (v == NULL) |
| 5809 | goto onError; |
| 5810 | |
| 5811 | /* Shortcuts */ |
| 5812 | if (v == unicode_empty) { |
| 5813 | Py_DECREF(v); |
| 5814 | return (PyObject *)u; |
| 5815 | } |
| 5816 | if (u == unicode_empty) { |
| 5817 | Py_DECREF(u); |
| 5818 | return (PyObject *)v; |
| 5819 | } |
| 5820 | |
| 5821 | /* Concat the two Unicode strings */ |
| 5822 | w = _PyUnicode_New(u->length + v->length); |
| 5823 | if (w == NULL) |
| 5824 | goto onError; |
| 5825 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 5826 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 5827 | |
| 5828 | Py_DECREF(u); |
| 5829 | Py_DECREF(v); |
| 5830 | return (PyObject *)w; |
| 5831 | |
| 5832 | onError: |
| 5833 | Py_XDECREF(u); |
| 5834 | Py_XDECREF(v); |
| 5835 | return NULL; |
| 5836 | } |
| 5837 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5838 | PyDoc_STRVAR(count__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5839 | "S.count(sub[, start[, end]]) -> int\n\ |
| 5840 | \n\ |
Fredrik Lundh | 763b50f | 2006-05-22 15:35:12 +0000 | [diff] [blame] | 5841 | Return the number of non-overlapping occurrences of substring sub in\n\ |
| 5842 | Unicode string S[start:end]. Optional arguments start and end are\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5843 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5844 | |
| 5845 | static PyObject * |
| 5846 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 5847 | { |
| 5848 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5849 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5850 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5851 | PyObject *result; |
| 5852 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5853 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 5854 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5855 | return NULL; |
| 5856 | |
| 5857 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5858 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5859 | if (substring == NULL) |
| 5860 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5861 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 5862 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5863 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5864 | result = PyInt_FromSsize_t( |
| 5865 | stringlib_count(self->str + start, end - start, |
| 5866 | substring->str, substring->length) |
| 5867 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5868 | |
| 5869 | Py_DECREF(substring); |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5870 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5871 | return result; |
| 5872 | } |
| 5873 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5874 | PyDoc_STRVAR(encode__doc__, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5875 | "S.encode([encoding[,errors]]) -> string or unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5876 | \n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5877 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 5878 | 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] | 5879 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5880 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 5881 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 5882 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5883 | |
| 5884 | static PyObject * |
| 5885 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 5886 | { |
| 5887 | char *encoding = NULL; |
| 5888 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5889 | PyObject *v; |
| 5890 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5891 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 5892 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5893 | v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5894 | if (v == NULL) |
| 5895 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5896 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 5897 | PyErr_Format(PyExc_TypeError, |
| 5898 | "encoder did not return a string/unicode object " |
| 5899 | "(type=%.400s)", |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 5900 | Py_Type(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5901 | Py_DECREF(v); |
| 5902 | return NULL; |
| 5903 | } |
| 5904 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5905 | |
| 5906 | onError: |
| 5907 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5908 | } |
| 5909 | |
| 5910 | PyDoc_STRVAR(decode__doc__, |
| 5911 | "S.decode([encoding[,errors]]) -> string or unicode\n\ |
| 5912 | \n\ |
| 5913 | Decodes S using the codec registered for encoding. encoding defaults\n\ |
| 5914 | to the default encoding. errors may be given to set a different error\n\ |
| 5915 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 5916 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 5917 | as well as any other name registerd with codecs.register_error that is\n\ |
| 5918 | able to handle UnicodeDecodeErrors."); |
| 5919 | |
| 5920 | static PyObject * |
Marc-André Lemburg | 126b44c | 2004-07-10 12:04:20 +0000 | [diff] [blame] | 5921 | unicode_decode(PyUnicodeObject *self, PyObject *args) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5922 | { |
| 5923 | char *encoding = NULL; |
| 5924 | char *errors = NULL; |
| 5925 | PyObject *v; |
| 5926 | |
| 5927 | if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
| 5928 | return NULL; |
| 5929 | v = PyUnicode_AsDecodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5930 | if (v == NULL) |
| 5931 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5932 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 5933 | PyErr_Format(PyExc_TypeError, |
| 5934 | "decoder did not return a string/unicode object " |
| 5935 | "(type=%.400s)", |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 5936 | Py_Type(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5937 | Py_DECREF(v); |
| 5938 | return NULL; |
| 5939 | } |
| 5940 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5941 | |
| 5942 | onError: |
| 5943 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5944 | } |
| 5945 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5946 | PyDoc_STRVAR(expandtabs__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5947 | "S.expandtabs([tabsize]) -> unicode\n\ |
| 5948 | \n\ |
| 5949 | 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] | 5950 | 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] | 5951 | |
| 5952 | static PyObject* |
| 5953 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 5954 | { |
| 5955 | Py_UNICODE *e; |
| 5956 | Py_UNICODE *p; |
| 5957 | Py_UNICODE *q; |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 5958 | Py_ssize_t i, j, old_j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5959 | PyUnicodeObject *u; |
| 5960 | int tabsize = 8; |
| 5961 | |
| 5962 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 5963 | return NULL; |
| 5964 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5965 | /* First pass: determine size of output string */ |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 5966 | i = j = old_j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5967 | e = self->str + self->length; |
| 5968 | for (p = self->str; p < e; p++) |
| 5969 | if (*p == '\t') { |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 5970 | if (tabsize > 0) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5971 | j += tabsize - (j % tabsize); |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 5972 | if (old_j > j) { |
Neal Norwitz | 5c9a81a | 2007-06-11 02:16:10 +0000 | [diff] [blame] | 5973 | PyErr_SetString(PyExc_OverflowError, |
| 5974 | "new string is too long"); |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 5975 | return NULL; |
| 5976 | } |
| 5977 | old_j = j; |
| 5978 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5979 | } |
| 5980 | else { |
| 5981 | j++; |
| 5982 | if (*p == '\n' || *p == '\r') { |
| 5983 | i += j; |
Neal Norwitz | 5c9a81a | 2007-06-11 02:16:10 +0000 | [diff] [blame] | 5984 | old_j = j = 0; |
| 5985 | if (i < 0) { |
| 5986 | PyErr_SetString(PyExc_OverflowError, |
| 5987 | "new string is too long"); |
| 5988 | return NULL; |
| 5989 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5990 | } |
| 5991 | } |
| 5992 | |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 5993 | if ((i + j) < 0) { |
| 5994 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 5995 | return NULL; |
| 5996 | } |
| 5997 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5998 | /* Second pass: create output string and fill it */ |
| 5999 | u = _PyUnicode_New(i + j); |
| 6000 | if (!u) |
| 6001 | return NULL; |
| 6002 | |
| 6003 | j = 0; |
| 6004 | q = u->str; |
| 6005 | |
| 6006 | for (p = self->str; p < e; p++) |
| 6007 | if (*p == '\t') { |
| 6008 | if (tabsize > 0) { |
| 6009 | i = tabsize - (j % tabsize); |
| 6010 | j += i; |
| 6011 | while (i--) |
| 6012 | *q++ = ' '; |
| 6013 | } |
| 6014 | } |
| 6015 | else { |
| 6016 | j++; |
| 6017 | *q++ = *p; |
| 6018 | if (*p == '\n' || *p == '\r') |
| 6019 | j = 0; |
| 6020 | } |
| 6021 | |
| 6022 | return (PyObject*) u; |
| 6023 | } |
| 6024 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6025 | PyDoc_STRVAR(find__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6026 | "S.find(sub [,start [,end]]) -> int\n\ |
| 6027 | \n\ |
| 6028 | Return the lowest index in S where substring sub is found,\n\ |
Georg Brandl | 9efd9b6 | 2007-07-29 17:38:35 +0000 | [diff] [blame] | 6029 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6030 | arguments start and end are interpreted as in slice notation.\n\ |
| 6031 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6032 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6033 | |
| 6034 | static PyObject * |
| 6035 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 6036 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6037 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6038 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6039 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6040 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6041 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6042 | if (!PyArg_ParseTuple(args, "O|O&O&:find", &substring, |
| 6043 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6044 | return NULL; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6045 | substring = PyUnicode_FromObject(substring); |
| 6046 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6047 | return NULL; |
| 6048 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 6049 | result = stringlib_find_slice( |
| 6050 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6051 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6052 | start, end |
| 6053 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6054 | |
| 6055 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6056 | |
| 6057 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6058 | } |
| 6059 | |
| 6060 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6061 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6062 | { |
| 6063 | if (index < 0 || index >= self->length) { |
| 6064 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 6065 | return NULL; |
| 6066 | } |
| 6067 | |
| 6068 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 6069 | } |
| 6070 | |
| 6071 | static long |
| 6072 | unicode_hash(PyUnicodeObject *self) |
| 6073 | { |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6074 | /* Since Unicode objects compare equal to their ASCII string |
| 6075 | counterparts, they should use the individual character values |
| 6076 | as basis for their hash value. This is needed to assure that |
| 6077 | strings and Unicode objects behave in the same way as |
| 6078 | dictionary keys. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6079 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6080 | register Py_ssize_t len; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6081 | register Py_UNICODE *p; |
| 6082 | register long x; |
| 6083 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6084 | if (self->hash != -1) |
| 6085 | return self->hash; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6086 | len = PyUnicode_GET_SIZE(self); |
| 6087 | p = PyUnicode_AS_UNICODE(self); |
| 6088 | x = *p << 7; |
| 6089 | while (--len >= 0) |
| 6090 | x = (1000003*x) ^ *p++; |
| 6091 | x ^= PyUnicode_GET_SIZE(self); |
| 6092 | if (x == -1) |
| 6093 | x = -2; |
| 6094 | self->hash = x; |
| 6095 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6096 | } |
| 6097 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6098 | PyDoc_STRVAR(index__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6099 | "S.index(sub [,start [,end]]) -> int\n\ |
| 6100 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6101 | 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] | 6102 | |
| 6103 | static PyObject * |
| 6104 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 6105 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6106 | Py_ssize_t result; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6107 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6108 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6109 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6110 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6111 | if (!PyArg_ParseTuple(args, "O|O&O&:index", &substring, |
| 6112 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6113 | return NULL; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6114 | substring = PyUnicode_FromObject(substring); |
| 6115 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6116 | return NULL; |
| 6117 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 6118 | result = stringlib_find_slice( |
| 6119 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6120 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6121 | start, end |
| 6122 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6123 | |
| 6124 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6125 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6126 | if (result < 0) { |
| 6127 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 6128 | return NULL; |
| 6129 | } |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6130 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6131 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6132 | } |
| 6133 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6134 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6135 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6136 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6137 | 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] | 6138 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6139 | |
| 6140 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6141 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6142 | { |
| 6143 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6144 | register const Py_UNICODE *e; |
| 6145 | int cased; |
| 6146 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6147 | /* Shortcut for single character strings */ |
| 6148 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6149 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6150 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6151 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6152 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6153 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6154 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6155 | e = p + PyUnicode_GET_SIZE(self); |
| 6156 | cased = 0; |
| 6157 | for (; p < e; p++) { |
| 6158 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6159 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6160 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6161 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6162 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 6163 | cased = 1; |
| 6164 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6165 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6166 | } |
| 6167 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6168 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6169 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6170 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6171 | 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] | 6172 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6173 | |
| 6174 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6175 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6176 | { |
| 6177 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6178 | register const Py_UNICODE *e; |
| 6179 | int cased; |
| 6180 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6181 | /* Shortcut for single character strings */ |
| 6182 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6183 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6184 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6185 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6186 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6187 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6188 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6189 | e = p + PyUnicode_GET_SIZE(self); |
| 6190 | cased = 0; |
| 6191 | for (; p < e; p++) { |
| 6192 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6193 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6194 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6195 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6196 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 6197 | cased = 1; |
| 6198 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6199 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6200 | } |
| 6201 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6202 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6203 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6204 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6205 | Return True if S is a titlecased string and there is at least one\n\ |
| 6206 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 6207 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 6208 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6209 | |
| 6210 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6211 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6212 | { |
| 6213 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6214 | register const Py_UNICODE *e; |
| 6215 | int cased, previous_is_cased; |
| 6216 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6217 | /* Shortcut for single character strings */ |
| 6218 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6219 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 6220 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6221 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6222 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6223 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6224 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6225 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6226 | e = p + PyUnicode_GET_SIZE(self); |
| 6227 | cased = 0; |
| 6228 | previous_is_cased = 0; |
| 6229 | for (; p < e; p++) { |
| 6230 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6231 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6232 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 6233 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6234 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6235 | previous_is_cased = 1; |
| 6236 | cased = 1; |
| 6237 | } |
| 6238 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 6239 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6240 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6241 | previous_is_cased = 1; |
| 6242 | cased = 1; |
| 6243 | } |
| 6244 | else |
| 6245 | previous_is_cased = 0; |
| 6246 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6247 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6248 | } |
| 6249 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6250 | PyDoc_STRVAR(isspace__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6251 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6252 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6253 | Return True if all characters in S are whitespace\n\ |
| 6254 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6255 | |
| 6256 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6257 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6258 | { |
| 6259 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6260 | register const Py_UNICODE *e; |
| 6261 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6262 | /* Shortcut for single character strings */ |
| 6263 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6264 | Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6265 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6266 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6267 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6268 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6269 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6270 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6271 | e = p + PyUnicode_GET_SIZE(self); |
| 6272 | for (; p < e; p++) { |
| 6273 | if (!Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6274 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6275 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6276 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6277 | } |
| 6278 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6279 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6280 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6281 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6282 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6283 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6284 | |
| 6285 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6286 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6287 | { |
| 6288 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6289 | register const Py_UNICODE *e; |
| 6290 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6291 | /* Shortcut for single character strings */ |
| 6292 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6293 | Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6294 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6295 | |
| 6296 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6297 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6298 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6299 | |
| 6300 | e = p + PyUnicode_GET_SIZE(self); |
| 6301 | for (; p < e; p++) { |
| 6302 | if (!Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6303 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6304 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6305 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6306 | } |
| 6307 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6308 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6309 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6310 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6311 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6312 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6313 | |
| 6314 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6315 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6316 | { |
| 6317 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6318 | register const Py_UNICODE *e; |
| 6319 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6320 | /* Shortcut for single character strings */ |
| 6321 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6322 | Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6323 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6324 | |
| 6325 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6326 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6327 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6328 | |
| 6329 | e = p + PyUnicode_GET_SIZE(self); |
| 6330 | for (; p < e; p++) { |
| 6331 | if (!Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6332 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6333 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6334 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6335 | } |
| 6336 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6337 | PyDoc_STRVAR(isdecimal__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6338 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6339 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6340 | 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] | 6341 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6342 | |
| 6343 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6344 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6345 | { |
| 6346 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6347 | register const Py_UNICODE *e; |
| 6348 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6349 | /* Shortcut for single character strings */ |
| 6350 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6351 | Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6352 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6353 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6354 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6355 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6356 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6357 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6358 | e = p + PyUnicode_GET_SIZE(self); |
| 6359 | for (; p < e; p++) { |
| 6360 | if (!Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6361 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6362 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6363 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6364 | } |
| 6365 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6366 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6367 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6368 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6369 | Return True if all characters in S are digits\n\ |
| 6370 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6371 | |
| 6372 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6373 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6374 | { |
| 6375 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6376 | register const Py_UNICODE *e; |
| 6377 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6378 | /* Shortcut for single character strings */ |
| 6379 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6380 | Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6381 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6382 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6383 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6384 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6385 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6386 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6387 | e = p + PyUnicode_GET_SIZE(self); |
| 6388 | for (; p < e; p++) { |
| 6389 | if (!Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6390 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6391 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6392 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6393 | } |
| 6394 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6395 | PyDoc_STRVAR(isnumeric__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6396 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6397 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6398 | 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] | 6399 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6400 | |
| 6401 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6402 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6403 | { |
| 6404 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6405 | register const Py_UNICODE *e; |
| 6406 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6407 | /* Shortcut for single character strings */ |
| 6408 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6409 | Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6410 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6411 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6412 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6413 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6414 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6415 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6416 | e = p + PyUnicode_GET_SIZE(self); |
| 6417 | for (; p < e; p++) { |
| 6418 | if (!Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6419 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6420 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6421 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6422 | } |
| 6423 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6424 | PyDoc_STRVAR(join__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6425 | "S.join(sequence) -> unicode\n\ |
| 6426 | \n\ |
| 6427 | 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] | 6428 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6429 | |
| 6430 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6431 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6432 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6433 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6434 | } |
| 6435 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6436 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6437 | unicode_length(PyUnicodeObject *self) |
| 6438 | { |
| 6439 | return self->length; |
| 6440 | } |
| 6441 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6442 | PyDoc_STRVAR(ljust__doc__, |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 6443 | "S.ljust(width[, fillchar]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6444 | \n\ |
| 6445 | 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] | 6446 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6447 | |
| 6448 | static PyObject * |
| 6449 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 6450 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6451 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6452 | Py_UNICODE fillchar = ' '; |
| 6453 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6454 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6455 | return NULL; |
| 6456 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6457 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6458 | Py_INCREF(self); |
| 6459 | return (PyObject*) self; |
| 6460 | } |
| 6461 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6462 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6463 | } |
| 6464 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6465 | PyDoc_STRVAR(lower__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6466 | "S.lower() -> unicode\n\ |
| 6467 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6468 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6469 | |
| 6470 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6471 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6472 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6473 | return fixup(self, fixlower); |
| 6474 | } |
| 6475 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6476 | #define LEFTSTRIP 0 |
| 6477 | #define RIGHTSTRIP 1 |
| 6478 | #define BOTHSTRIP 2 |
| 6479 | |
| 6480 | /* Arrays indexed by above */ |
| 6481 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 6482 | |
| 6483 | #define STRIPNAME(i) (stripformat[i]+3) |
| 6484 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6485 | /* externally visible for str.strip(unicode) */ |
| 6486 | PyObject * |
| 6487 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 6488 | { |
| 6489 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6490 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6491 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6492 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 6493 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6494 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 6495 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
| 6496 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6497 | i = 0; |
| 6498 | if (striptype != RIGHTSTRIP) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 6499 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 6500 | i++; |
| 6501 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6502 | } |
| 6503 | |
| 6504 | j = len; |
| 6505 | if (striptype != LEFTSTRIP) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 6506 | do { |
| 6507 | j--; |
| 6508 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 6509 | j++; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6510 | } |
| 6511 | |
| 6512 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 6513 | Py_INCREF(self); |
| 6514 | return (PyObject*)self; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6515 | } |
| 6516 | else |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 6517 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6518 | } |
| 6519 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6520 | |
| 6521 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6522 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6523 | { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6524 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6525 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6526 | |
| 6527 | i = 0; |
| 6528 | if (striptype != RIGHTSTRIP) { |
| 6529 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 6530 | i++; |
| 6531 | } |
| 6532 | } |
| 6533 | |
| 6534 | j = len; |
| 6535 | if (striptype != LEFTSTRIP) { |
| 6536 | do { |
| 6537 | j--; |
| 6538 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 6539 | j++; |
| 6540 | } |
| 6541 | |
| 6542 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 6543 | Py_INCREF(self); |
| 6544 | return (PyObject*)self; |
| 6545 | } |
| 6546 | else |
| 6547 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6548 | } |
| 6549 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6550 | |
| 6551 | static PyObject * |
| 6552 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 6553 | { |
| 6554 | PyObject *sep = NULL; |
| 6555 | |
| 6556 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 6557 | return NULL; |
| 6558 | |
| 6559 | if (sep != NULL && sep != Py_None) { |
| 6560 | if (PyUnicode_Check(sep)) |
| 6561 | return _PyUnicode_XStrip(self, striptype, sep); |
| 6562 | else if (PyString_Check(sep)) { |
| 6563 | PyObject *res; |
| 6564 | sep = PyUnicode_FromObject(sep); |
| 6565 | if (sep==NULL) |
| 6566 | return NULL; |
| 6567 | res = _PyUnicode_XStrip(self, striptype, sep); |
| 6568 | Py_DECREF(sep); |
| 6569 | return res; |
| 6570 | } |
| 6571 | else { |
| 6572 | PyErr_Format(PyExc_TypeError, |
| 6573 | "%s arg must be None, unicode or str", |
| 6574 | STRIPNAME(striptype)); |
| 6575 | return NULL; |
| 6576 | } |
| 6577 | } |
| 6578 | |
| 6579 | return do_strip(self, striptype); |
| 6580 | } |
| 6581 | |
| 6582 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6583 | PyDoc_STRVAR(strip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6584 | "S.strip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6585 | \n\ |
| 6586 | Return a copy of the string S with leading and trailing\n\ |
| 6587 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6588 | If chars is given and not None, remove characters in chars instead.\n\ |
| 6589 | 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] | 6590 | |
| 6591 | static PyObject * |
| 6592 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 6593 | { |
| 6594 | if (PyTuple_GET_SIZE(args) == 0) |
| 6595 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 6596 | else |
| 6597 | return do_argstrip(self, BOTHSTRIP, args); |
| 6598 | } |
| 6599 | |
| 6600 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6601 | PyDoc_STRVAR(lstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6602 | "S.lstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6603 | \n\ |
| 6604 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6605 | If chars is given and not None, remove characters in chars instead.\n\ |
| 6606 | 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] | 6607 | |
| 6608 | static PyObject * |
| 6609 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 6610 | { |
| 6611 | if (PyTuple_GET_SIZE(args) == 0) |
| 6612 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 6613 | else |
| 6614 | return do_argstrip(self, LEFTSTRIP, args); |
| 6615 | } |
| 6616 | |
| 6617 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6618 | PyDoc_STRVAR(rstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6619 | "S.rstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6620 | \n\ |
| 6621 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6622 | If chars is given and not None, remove characters in chars instead.\n\ |
| 6623 | 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] | 6624 | |
| 6625 | static PyObject * |
| 6626 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 6627 | { |
| 6628 | if (PyTuple_GET_SIZE(args) == 0) |
| 6629 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 6630 | else |
| 6631 | return do_argstrip(self, RIGHTSTRIP, args); |
| 6632 | } |
| 6633 | |
| 6634 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6635 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6636 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6637 | { |
| 6638 | PyUnicodeObject *u; |
| 6639 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6640 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 6641 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6642 | |
| 6643 | if (len < 0) |
| 6644 | len = 0; |
| 6645 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6646 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6647 | /* no repeat, return original string */ |
| 6648 | Py_INCREF(str); |
| 6649 | return (PyObject*) str; |
| 6650 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 6651 | |
| 6652 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 6653 | * needed doesn't overflow size_t |
| 6654 | */ |
| 6655 | nchars = len * str->length; |
| 6656 | if (len && nchars / len != str->length) { |
| 6657 | PyErr_SetString(PyExc_OverflowError, |
| 6658 | "repeated string is too long"); |
| 6659 | return NULL; |
| 6660 | } |
| 6661 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 6662 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 6663 | PyErr_SetString(PyExc_OverflowError, |
| 6664 | "repeated string is too long"); |
| 6665 | return NULL; |
| 6666 | } |
| 6667 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6668 | if (!u) |
| 6669 | return NULL; |
| 6670 | |
| 6671 | p = u->str; |
| 6672 | |
Fredrik Lundh | f1d60a5 | 2006-05-22 16:29:30 +0000 | [diff] [blame] | 6673 | if (str->length == 1 && len > 0) { |
| 6674 | Py_UNICODE_FILL(p, str->str[0], len); |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 6675 | } else { |
Tim Peters | 1bacc64 | 2006-05-23 05:47:16 +0000 | [diff] [blame] | 6676 | Py_ssize_t done = 0; /* number of characters copied this far */ |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 6677 | if (done < nchars) { |
Fredrik Lundh | f1d60a5 | 2006-05-22 16:29:30 +0000 | [diff] [blame] | 6678 | Py_UNICODE_COPY(p, str->str, str->length); |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 6679 | done = str->length; |
| 6680 | } |
| 6681 | while (done < nchars) { |
| 6682 | int n = (done <= nchars-done) ? done : nchars-done; |
| 6683 | Py_UNICODE_COPY(p+done, p, n); |
| 6684 | done += n; |
| 6685 | } |
| 6686 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6687 | |
| 6688 | return (PyObject*) u; |
| 6689 | } |
| 6690 | |
| 6691 | PyObject *PyUnicode_Replace(PyObject *obj, |
| 6692 | PyObject *subobj, |
| 6693 | PyObject *replobj, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6694 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6695 | { |
| 6696 | PyObject *self; |
| 6697 | PyObject *str1; |
| 6698 | PyObject *str2; |
| 6699 | PyObject *result; |
| 6700 | |
| 6701 | self = PyUnicode_FromObject(obj); |
| 6702 | if (self == NULL) |
| 6703 | return NULL; |
| 6704 | str1 = PyUnicode_FromObject(subobj); |
| 6705 | if (str1 == NULL) { |
| 6706 | Py_DECREF(self); |
| 6707 | return NULL; |
| 6708 | } |
| 6709 | str2 = PyUnicode_FromObject(replobj); |
| 6710 | if (str2 == NULL) { |
| 6711 | Py_DECREF(self); |
| 6712 | Py_DECREF(str1); |
| 6713 | return NULL; |
| 6714 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6715 | result = replace((PyUnicodeObject *)self, |
| 6716 | (PyUnicodeObject *)str1, |
| 6717 | (PyUnicodeObject *)str2, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6718 | maxcount); |
| 6719 | Py_DECREF(self); |
| 6720 | Py_DECREF(str1); |
| 6721 | Py_DECREF(str2); |
| 6722 | return result; |
| 6723 | } |
| 6724 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6725 | PyDoc_STRVAR(replace__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6726 | "S.replace (old, new[, maxsplit]) -> unicode\n\ |
| 6727 | \n\ |
| 6728 | Return a copy of S with all occurrences of substring\n\ |
| 6729 | 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] | 6730 | given, only the first maxsplit occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6731 | |
| 6732 | static PyObject* |
| 6733 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 6734 | { |
| 6735 | PyUnicodeObject *str1; |
| 6736 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6737 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6738 | PyObject *result; |
| 6739 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6740 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6741 | return NULL; |
| 6742 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 6743 | if (str1 == NULL) |
| 6744 | return NULL; |
| 6745 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 6746 | if (str2 == NULL) { |
| 6747 | Py_DECREF(str1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6748 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 6749 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6750 | |
| 6751 | result = replace(self, str1, str2, maxcount); |
| 6752 | |
| 6753 | Py_DECREF(str1); |
| 6754 | Py_DECREF(str2); |
| 6755 | return result; |
| 6756 | } |
| 6757 | |
| 6758 | static |
| 6759 | PyObject *unicode_repr(PyObject *unicode) |
| 6760 | { |
| 6761 | return unicodeescape_string(PyUnicode_AS_UNICODE(unicode), |
| 6762 | PyUnicode_GET_SIZE(unicode), |
| 6763 | 1); |
| 6764 | } |
| 6765 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6766 | PyDoc_STRVAR(rfind__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6767 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 6768 | \n\ |
| 6769 | Return the highest index in S where substring sub is found,\n\ |
Georg Brandl | 9efd9b6 | 2007-07-29 17:38:35 +0000 | [diff] [blame] | 6770 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6771 | arguments start and end are interpreted as in slice notation.\n\ |
| 6772 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6773 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6774 | |
| 6775 | static PyObject * |
| 6776 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 6777 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6778 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6779 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6780 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6781 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6782 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6783 | if (!PyArg_ParseTuple(args, "O|O&O&:rfind", &substring, |
| 6784 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6785 | return NULL; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6786 | substring = PyUnicode_FromObject(substring); |
| 6787 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6788 | return NULL; |
| 6789 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 6790 | result = stringlib_rfind_slice( |
| 6791 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6792 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6793 | start, end |
| 6794 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6795 | |
| 6796 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6797 | |
| 6798 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6799 | } |
| 6800 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6801 | PyDoc_STRVAR(rindex__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6802 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 6803 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6804 | 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] | 6805 | |
| 6806 | static PyObject * |
| 6807 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 6808 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6809 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6810 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6811 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6812 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6813 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6814 | if (!PyArg_ParseTuple(args, "O|O&O&:rindex", &substring, |
| 6815 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6816 | return NULL; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6817 | substring = PyUnicode_FromObject(substring); |
| 6818 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6819 | return NULL; |
| 6820 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 6821 | result = stringlib_rfind_slice( |
| 6822 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6823 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6824 | start, end |
| 6825 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6826 | |
| 6827 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6828 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6829 | if (result < 0) { |
| 6830 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 6831 | return NULL; |
| 6832 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6833 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6834 | } |
| 6835 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6836 | PyDoc_STRVAR(rjust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6837 | "S.rjust(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6838 | \n\ |
| 6839 | 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] | 6840 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6841 | |
| 6842 | static PyObject * |
| 6843 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 6844 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6845 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6846 | Py_UNICODE fillchar = ' '; |
| 6847 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6848 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6849 | return NULL; |
| 6850 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6851 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6852 | Py_INCREF(self); |
| 6853 | return (PyObject*) self; |
| 6854 | } |
| 6855 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6856 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6857 | } |
| 6858 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6859 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6860 | 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] | 6861 | { |
| 6862 | /* standard clamping */ |
| 6863 | if (start < 0) |
| 6864 | start = 0; |
| 6865 | if (end < 0) |
| 6866 | end = 0; |
| 6867 | if (end > self->length) |
| 6868 | end = self->length; |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6869 | if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6870 | /* full slice, return original string */ |
| 6871 | Py_INCREF(self); |
| 6872 | return (PyObject*) self; |
| 6873 | } |
| 6874 | if (start > end) |
| 6875 | start = end; |
| 6876 | /* copy slice */ |
| 6877 | return (PyObject*) PyUnicode_FromUnicode(self->str + start, |
| 6878 | end - start); |
| 6879 | } |
| 6880 | |
| 6881 | PyObject *PyUnicode_Split(PyObject *s, |
| 6882 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6883 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6884 | { |
| 6885 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6886 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6887 | s = PyUnicode_FromObject(s); |
| 6888 | if (s == NULL) |
| 6889 | return NULL; |
| 6890 | if (sep != NULL) { |
| 6891 | sep = PyUnicode_FromObject(sep); |
| 6892 | if (sep == NULL) { |
| 6893 | Py_DECREF(s); |
| 6894 | return NULL; |
| 6895 | } |
| 6896 | } |
| 6897 | |
| 6898 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 6899 | |
| 6900 | Py_DECREF(s); |
| 6901 | Py_XDECREF(sep); |
| 6902 | return result; |
| 6903 | } |
| 6904 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6905 | PyDoc_STRVAR(split__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6906 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 6907 | \n\ |
| 6908 | Return a list of the words in S, using sep as the\n\ |
| 6909 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Thomas Heller | ca0d2cb | 2004-09-15 11:41:32 +0000 | [diff] [blame] | 6910 | 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] | 6911 | any whitespace string is a separator."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6912 | |
| 6913 | static PyObject* |
| 6914 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 6915 | { |
| 6916 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6917 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6918 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6919 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6920 | return NULL; |
| 6921 | |
| 6922 | if (substring == Py_None) |
| 6923 | return split(self, NULL, maxcount); |
| 6924 | else if (PyUnicode_Check(substring)) |
| 6925 | return split(self, (PyUnicodeObject *)substring, maxcount); |
| 6926 | else |
| 6927 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
| 6928 | } |
| 6929 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6930 | PyObject * |
| 6931 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 6932 | { |
| 6933 | PyObject* str_obj; |
| 6934 | PyObject* sep_obj; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6935 | PyObject* out; |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 6936 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6937 | str_obj = PyUnicode_FromObject(str_in); |
| 6938 | if (!str_obj) |
| 6939 | return NULL; |
| 6940 | sep_obj = PyUnicode_FromObject(sep_in); |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 6941 | if (!sep_obj) { |
| 6942 | Py_DECREF(str_obj); |
| 6943 | return NULL; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6944 | } |
| 6945 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6946 | out = stringlib_partition( |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 6947 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 6948 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 6949 | ); |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6950 | |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 6951 | Py_DECREF(sep_obj); |
| 6952 | Py_DECREF(str_obj); |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6953 | |
| 6954 | return out; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6955 | } |
| 6956 | |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 6957 | |
| 6958 | PyObject * |
| 6959 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 6960 | { |
| 6961 | PyObject* str_obj; |
| 6962 | PyObject* sep_obj; |
| 6963 | PyObject* out; |
| 6964 | |
| 6965 | str_obj = PyUnicode_FromObject(str_in); |
| 6966 | if (!str_obj) |
| 6967 | return NULL; |
| 6968 | sep_obj = PyUnicode_FromObject(sep_in); |
| 6969 | if (!sep_obj) { |
| 6970 | Py_DECREF(str_obj); |
| 6971 | return NULL; |
| 6972 | } |
| 6973 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6974 | out = stringlib_rpartition( |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 6975 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 6976 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 6977 | ); |
| 6978 | |
| 6979 | Py_DECREF(sep_obj); |
| 6980 | Py_DECREF(str_obj); |
| 6981 | |
| 6982 | return out; |
| 6983 | } |
| 6984 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6985 | PyDoc_STRVAR(partition__doc__, |
| 6986 | "S.partition(sep) -> (head, sep, tail)\n\ |
| 6987 | \n\ |
| 6988 | Searches for the separator sep in S, and returns the part before it,\n\ |
| 6989 | the separator itself, and the part after it. If the separator is not\n\ |
| 6990 | found, returns S and two empty strings."); |
| 6991 | |
| 6992 | static PyObject* |
Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 6993 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6994 | { |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6995 | return PyUnicode_Partition((PyObject *)self, separator); |
| 6996 | } |
| 6997 | |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 6998 | PyDoc_STRVAR(rpartition__doc__, |
Raymond Hettinger | a0c95fa | 2006-09-04 15:32:48 +0000 | [diff] [blame] | 6999 | "S.rpartition(sep) -> (tail, sep, head)\n\ |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7000 | \n\ |
| 7001 | Searches for the separator sep in S, starting at the end of S, and returns\n\ |
| 7002 | the part before it, the separator itself, and the part after it. If the\n\ |
Raymond Hettinger | a0c95fa | 2006-09-04 15:32:48 +0000 | [diff] [blame] | 7003 | separator is not found, returns two empty strings and S."); |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7004 | |
| 7005 | static PyObject* |
| 7006 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 7007 | { |
| 7008 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 7009 | } |
| 7010 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7011 | PyObject *PyUnicode_RSplit(PyObject *s, |
| 7012 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7013 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7014 | { |
| 7015 | PyObject *result; |
| 7016 | |
| 7017 | s = PyUnicode_FromObject(s); |
| 7018 | if (s == NULL) |
| 7019 | return NULL; |
| 7020 | if (sep != NULL) { |
| 7021 | sep = PyUnicode_FromObject(sep); |
| 7022 | if (sep == NULL) { |
| 7023 | Py_DECREF(s); |
| 7024 | return NULL; |
| 7025 | } |
| 7026 | } |
| 7027 | |
| 7028 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7029 | |
| 7030 | Py_DECREF(s); |
| 7031 | Py_XDECREF(sep); |
| 7032 | return result; |
| 7033 | } |
| 7034 | |
| 7035 | PyDoc_STRVAR(rsplit__doc__, |
| 7036 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
| 7037 | \n\ |
| 7038 | Return a list of the words in S, using sep as the\n\ |
| 7039 | delimiter string, starting at the end of the string and\n\ |
| 7040 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 7041 | splits are done. If sep is not specified, any whitespace string\n\ |
| 7042 | is a separator."); |
| 7043 | |
| 7044 | static PyObject* |
| 7045 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 7046 | { |
| 7047 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7048 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7049 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7050 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7051 | return NULL; |
| 7052 | |
| 7053 | if (substring == Py_None) |
| 7054 | return rsplit(self, NULL, maxcount); |
| 7055 | else if (PyUnicode_Check(substring)) |
| 7056 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
| 7057 | else |
| 7058 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
| 7059 | } |
| 7060 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7061 | PyDoc_STRVAR(splitlines__doc__, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7062 | "S.splitlines([keepends]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7063 | \n\ |
| 7064 | 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] | 7065 | 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] | 7066 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7067 | |
| 7068 | static PyObject* |
| 7069 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 7070 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7071 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7072 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7073 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7074 | return NULL; |
| 7075 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7076 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7077 | } |
| 7078 | |
| 7079 | static |
| 7080 | PyObject *unicode_str(PyUnicodeObject *self) |
| 7081 | { |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7082 | return PyUnicode_AsEncodedString((PyObject *)self, NULL, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7083 | } |
| 7084 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7085 | PyDoc_STRVAR(swapcase__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7086 | "S.swapcase() -> unicode\n\ |
| 7087 | \n\ |
| 7088 | 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] | 7089 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7090 | |
| 7091 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7092 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7093 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7094 | return fixup(self, fixswapcase); |
| 7095 | } |
| 7096 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7097 | PyDoc_STRVAR(translate__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7098 | "S.translate(table) -> unicode\n\ |
| 7099 | \n\ |
| 7100 | Return a copy of the string S, where all characters have been mapped\n\ |
| 7101 | 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] | 7102 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ |
| 7103 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 7104 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7105 | |
| 7106 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7107 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7108 | { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7109 | return PyUnicode_TranslateCharmap(self->str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7110 | self->length, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7111 | table, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7112 | "ignore"); |
| 7113 | } |
| 7114 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7115 | PyDoc_STRVAR(upper__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7116 | "S.upper() -> unicode\n\ |
| 7117 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7118 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7119 | |
| 7120 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7121 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7122 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7123 | return fixup(self, fixupper); |
| 7124 | } |
| 7125 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7126 | PyDoc_STRVAR(zfill__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7127 | "S.zfill(width) -> unicode\n\ |
| 7128 | \n\ |
| 7129 | 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] | 7130 | of the specified width. The string x is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7131 | |
| 7132 | static PyObject * |
| 7133 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 7134 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7135 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7136 | PyUnicodeObject *u; |
| 7137 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7138 | Py_ssize_t width; |
| 7139 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7140 | return NULL; |
| 7141 | |
| 7142 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 7143 | if (PyUnicode_CheckExact(self)) { |
| 7144 | Py_INCREF(self); |
| 7145 | return (PyObject*) self; |
| 7146 | } |
| 7147 | else |
| 7148 | return PyUnicode_FromUnicode( |
| 7149 | PyUnicode_AS_UNICODE(self), |
| 7150 | PyUnicode_GET_SIZE(self) |
| 7151 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7152 | } |
| 7153 | |
| 7154 | fill = width - self->length; |
| 7155 | |
| 7156 | u = pad(self, fill, 0, '0'); |
| 7157 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 7158 | if (u == NULL) |
| 7159 | return NULL; |
| 7160 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7161 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 7162 | /* move sign to beginning of string */ |
| 7163 | u->str[0] = u->str[fill]; |
| 7164 | u->str[fill] = '0'; |
| 7165 | } |
| 7166 | |
| 7167 | return (PyObject*) u; |
| 7168 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7169 | |
| 7170 | #if 0 |
| 7171 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7172 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7173 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7174 | return PyInt_FromLong(unicode_freelist_size); |
| 7175 | } |
| 7176 | #endif |
| 7177 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7178 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7179 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7180 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7181 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 7182 | With optional start, test S beginning at that position.\n\ |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7183 | With optional end, stop comparing S at that position.\n\ |
| 7184 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7185 | |
| 7186 | static PyObject * |
| 7187 | unicode_startswith(PyUnicodeObject *self, |
| 7188 | PyObject *args) |
| 7189 | { |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7190 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7191 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7192 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7193 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7194 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7195 | |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7196 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7197 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7198 | return NULL; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7199 | if (PyTuple_Check(subobj)) { |
| 7200 | Py_ssize_t i; |
| 7201 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 7202 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 7203 | PyTuple_GET_ITEM(subobj, i)); |
| 7204 | if (substring == NULL) |
| 7205 | return NULL; |
| 7206 | result = tailmatch(self, substring, start, end, -1); |
| 7207 | Py_DECREF(substring); |
| 7208 | if (result) { |
| 7209 | Py_RETURN_TRUE; |
| 7210 | } |
| 7211 | } |
| 7212 | /* nothing matched */ |
| 7213 | Py_RETURN_FALSE; |
| 7214 | } |
| 7215 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7216 | if (substring == NULL) |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7217 | return NULL; |
| 7218 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7219 | Py_DECREF(substring); |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7220 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7221 | } |
| 7222 | |
| 7223 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7224 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7225 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7226 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7227 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 7228 | With optional start, test S beginning at that position.\n\ |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7229 | With optional end, stop comparing S at that position.\n\ |
| 7230 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7231 | |
| 7232 | static PyObject * |
| 7233 | unicode_endswith(PyUnicodeObject *self, |
| 7234 | PyObject *args) |
| 7235 | { |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7236 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7237 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7238 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7239 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7240 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7241 | |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7242 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 7243 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7244 | return NULL; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7245 | if (PyTuple_Check(subobj)) { |
| 7246 | Py_ssize_t i; |
| 7247 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 7248 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 7249 | PyTuple_GET_ITEM(subobj, i)); |
| 7250 | if (substring == NULL) |
| 7251 | return NULL; |
| 7252 | result = tailmatch(self, substring, start, end, +1); |
| 7253 | Py_DECREF(substring); |
| 7254 | if (result) { |
| 7255 | Py_RETURN_TRUE; |
| 7256 | } |
| 7257 | } |
| 7258 | Py_RETURN_FALSE; |
| 7259 | } |
| 7260 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7261 | if (substring == NULL) |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7262 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7263 | |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7264 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7265 | Py_DECREF(substring); |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7266 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7267 | } |
| 7268 | |
| 7269 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 7270 | |
| 7271 | static PyObject * |
| 7272 | unicode_getnewargs(PyUnicodeObject *v) |
| 7273 | { |
| 7274 | return Py_BuildValue("(u#)", v->str, v->length); |
| 7275 | } |
| 7276 | |
| 7277 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7278 | static PyMethodDef unicode_methods[] = { |
| 7279 | |
| 7280 | /* Order is according to common usage: often used methods should |
| 7281 | appear first, since lookup is done sequentially. */ |
| 7282 | |
Georg Brandl | ecdc0a9 | 2006-03-30 12:19:07 +0000 | [diff] [blame] | 7283 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7284 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 7285 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7286 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7287 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 7288 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 7289 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 7290 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 7291 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 7292 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 7293 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 7294 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7295 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 7296 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 7297 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7298 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7299 | {"decode", (PyCFunction) unicode_decode, METH_VARARGS, decode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7300 | /* {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */ |
| 7301 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 7302 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 7303 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7304 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7305 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7306 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7307 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7308 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 7309 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 7310 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 7311 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 7312 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 7313 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 7314 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 7315 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 7316 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 7317 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 7318 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 7319 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 7320 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 7321 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7322 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 7323 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7324 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7325 | #endif |
| 7326 | |
| 7327 | #if 0 |
| 7328 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7329 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7330 | #endif |
| 7331 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 7332 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7333 | {NULL, NULL} |
| 7334 | }; |
| 7335 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7336 | static PyObject * |
| 7337 | unicode_mod(PyObject *v, PyObject *w) |
| 7338 | { |
| 7339 | if (!PyUnicode_Check(v)) { |
| 7340 | Py_INCREF(Py_NotImplemented); |
| 7341 | return Py_NotImplemented; |
| 7342 | } |
| 7343 | return PyUnicode_Format(v, w); |
| 7344 | } |
| 7345 | |
| 7346 | static PyNumberMethods unicode_as_number = { |
| 7347 | 0, /*nb_add*/ |
| 7348 | 0, /*nb_subtract*/ |
| 7349 | 0, /*nb_multiply*/ |
| 7350 | 0, /*nb_divide*/ |
| 7351 | unicode_mod, /*nb_remainder*/ |
| 7352 | }; |
| 7353 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7354 | static PySequenceMethods unicode_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7355 | (lenfunc) unicode_length, /* sq_length */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 7356 | PyUnicode_Concat, /* sq_concat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7357 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 7358 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 7359 | (ssizessizeargfunc) unicode_slice, /* sq_slice */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7360 | 0, /* sq_ass_item */ |
| 7361 | 0, /* sq_ass_slice */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 7362 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7363 | }; |
| 7364 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7365 | static PyObject* |
| 7366 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 7367 | { |
Marc-André Lemburg | 3a45779 | 2006-08-14 12:57:27 +0000 | [diff] [blame] | 7368 | if (PyIndex_Check(item)) { |
| 7369 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7370 | if (i == -1 && PyErr_Occurred()) |
| 7371 | return NULL; |
| 7372 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7373 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7374 | return unicode_getitem(self, i); |
| 7375 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7376 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7377 | Py_UNICODE* source_buf; |
| 7378 | Py_UNICODE* result_buf; |
| 7379 | PyObject* result; |
| 7380 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7381 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7382 | &start, &stop, &step, &slicelength) < 0) { |
| 7383 | return NULL; |
| 7384 | } |
| 7385 | |
| 7386 | if (slicelength <= 0) { |
| 7387 | return PyUnicode_FromUnicode(NULL, 0); |
| 7388 | } else { |
| 7389 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 7390 | result_buf = (Py_UNICODE *)PyMem_MALLOC(slicelength* |
| 7391 | sizeof(Py_UNICODE)); |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7392 | |
| 7393 | if (result_buf == NULL) |
| 7394 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7395 | |
| 7396 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 7397 | result_buf[i] = source_buf[cur]; |
| 7398 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7399 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7400 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
| 7401 | PyMem_FREE(result_buf); |
| 7402 | return result; |
| 7403 | } |
| 7404 | } else { |
| 7405 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 7406 | return NULL; |
| 7407 | } |
| 7408 | } |
| 7409 | |
| 7410 | static PyMappingMethods unicode_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7411 | (lenfunc)unicode_length, /* mp_length */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7412 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 7413 | (objobjargproc)0, /* mp_ass_subscript */ |
| 7414 | }; |
| 7415 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7416 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7417 | unicode_buffer_getreadbuf(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7418 | Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7419 | const void **ptr) |
| 7420 | { |
| 7421 | if (index != 0) { |
| 7422 | PyErr_SetString(PyExc_SystemError, |
| 7423 | "accessing non-existent unicode segment"); |
| 7424 | return -1; |
| 7425 | } |
| 7426 | *ptr = (void *) self->str; |
| 7427 | return PyUnicode_GET_DATA_SIZE(self); |
| 7428 | } |
| 7429 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7430 | static Py_ssize_t |
| 7431 | unicode_buffer_getwritebuf(PyUnicodeObject *self, Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7432 | const void **ptr) |
| 7433 | { |
| 7434 | PyErr_SetString(PyExc_TypeError, |
Neal Norwitz | 20e7213 | 2002-06-13 21:25:17 +0000 | [diff] [blame] | 7435 | "cannot use unicode as modifiable buffer"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7436 | return -1; |
| 7437 | } |
| 7438 | |
| 7439 | static int |
| 7440 | unicode_buffer_getsegcount(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7441 | Py_ssize_t *lenp) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7442 | { |
| 7443 | if (lenp) |
| 7444 | *lenp = PyUnicode_GET_DATA_SIZE(self); |
| 7445 | return 1; |
| 7446 | } |
| 7447 | |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 7448 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7449 | unicode_buffer_getcharbuf(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7450 | Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7451 | const void **ptr) |
| 7452 | { |
| 7453 | PyObject *str; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7454 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7455 | if (index != 0) { |
| 7456 | PyErr_SetString(PyExc_SystemError, |
| 7457 | "accessing non-existent unicode segment"); |
| 7458 | return -1; |
| 7459 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 7460 | str = _PyUnicode_AsDefaultEncodedString((PyObject *)self, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7461 | if (str == NULL) |
| 7462 | return -1; |
| 7463 | *ptr = (void *) PyString_AS_STRING(str); |
| 7464 | return PyString_GET_SIZE(str); |
| 7465 | } |
| 7466 | |
| 7467 | /* Helpers for PyUnicode_Format() */ |
| 7468 | |
| 7469 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7470 | 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] | 7471 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7472 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7473 | if (argidx < arglen) { |
| 7474 | (*p_argidx)++; |
| 7475 | if (arglen < 0) |
| 7476 | return args; |
| 7477 | else |
| 7478 | return PyTuple_GetItem(args, argidx); |
| 7479 | } |
| 7480 | PyErr_SetString(PyExc_TypeError, |
| 7481 | "not enough arguments for format string"); |
| 7482 | return NULL; |
| 7483 | } |
| 7484 | |
| 7485 | #define F_LJUST (1<<0) |
| 7486 | #define F_SIGN (1<<1) |
| 7487 | #define F_BLANK (1<<2) |
| 7488 | #define F_ALT (1<<3) |
| 7489 | #define F_ZERO (1<<4) |
| 7490 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7491 | static Py_ssize_t |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7492 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7493 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7494 | register Py_ssize_t i; |
| 7495 | Py_ssize_t len = strlen(charbuffer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7496 | for (i = len - 1; i >= 0; i--) |
| 7497 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
| 7498 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7499 | return len; |
| 7500 | } |
| 7501 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7502 | static int |
| 7503 | doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) |
| 7504 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 7505 | Py_ssize_t result; |
| 7506 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7507 | PyOS_ascii_formatd((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 7508 | result = strtounicode(buffer, (char *)buffer); |
| 7509 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7510 | } |
| 7511 | |
| 7512 | static int |
| 7513 | longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) |
| 7514 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 7515 | Py_ssize_t result; |
| 7516 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7517 | PyOS_snprintf((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 7518 | result = strtounicode(buffer, (char *)buffer); |
| 7519 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7520 | } |
| 7521 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 7522 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 7523 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 7524 | formatting is done. */ |
| 7525 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7526 | static int |
| 7527 | formatfloat(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7528 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7529 | int flags, |
| 7530 | int prec, |
| 7531 | int type, |
| 7532 | PyObject *v) |
| 7533 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7534 | /* fmt = '%#.' + `prec` + `type` |
| 7535 | 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] | 7536 | char fmt[20]; |
| 7537 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7538 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7539 | x = PyFloat_AsDouble(v); |
| 7540 | if (x == -1.0 && PyErr_Occurred()) |
| 7541 | return -1; |
| 7542 | if (prec < 0) |
| 7543 | prec = 6; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7544 | if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
| 7545 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7546 | /* Worst case length calc to ensure no buffer overrun: |
| 7547 | |
| 7548 | 'g' formats: |
| 7549 | fmt = %#.<prec>g |
| 7550 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 7551 | for any double rep.) |
| 7552 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 7553 | |
| 7554 | 'f' formats: |
| 7555 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 7556 | len = 1 + 50 + 1 + prec = 52 + prec |
| 7557 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7558 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7559 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7560 | |
| 7561 | */ |
Georg Brandl | 7c3b50d | 2007-07-12 08:38:00 +0000 | [diff] [blame] | 7562 | if (((type == 'g' || type == 'G') && |
| 7563 | buflen <= (size_t)10 + (size_t)prec) || |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7564 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7565 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7566 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7567 | return -1; |
| 7568 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7569 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 7570 | (flags&F_ALT) ? "#" : "", |
| 7571 | prec, type); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7572 | return doubletounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7573 | } |
| 7574 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7575 | static PyObject* |
| 7576 | formatlong(PyObject *val, int flags, int prec, int type) |
| 7577 | { |
| 7578 | char *buf; |
| 7579 | int i, len; |
| 7580 | PyObject *str; /* temporary string object. */ |
| 7581 | PyUnicodeObject *result; |
| 7582 | |
| 7583 | str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
| 7584 | if (!str) |
| 7585 | return NULL; |
| 7586 | result = _PyUnicode_New(len); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 7587 | if (!result) { |
| 7588 | Py_DECREF(str); |
| 7589 | return NULL; |
| 7590 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7591 | for (i = 0; i < len; i++) |
| 7592 | result->str[i] = buf[i]; |
| 7593 | result->str[len] = 0; |
| 7594 | Py_DECREF(str); |
| 7595 | return (PyObject*)result; |
| 7596 | } |
| 7597 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7598 | static int |
| 7599 | formatint(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7600 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7601 | int flags, |
| 7602 | int prec, |
| 7603 | int type, |
| 7604 | PyObject *v) |
| 7605 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7606 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7607 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 7608 | * + 1 + 1 |
| 7609 | * = 24 |
| 7610 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7611 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7612 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7613 | long x; |
| 7614 | |
| 7615 | x = PyInt_AsLong(v); |
| 7616 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7617 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7618 | if (x < 0 && type == 'u') { |
| 7619 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 7620 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7621 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 7622 | sign = "-"; |
| 7623 | else |
| 7624 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7625 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7626 | prec = 1; |
| 7627 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7628 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 7629 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7630 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7631 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7632 | PyErr_SetString(PyExc_OverflowError, |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7633 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7634 | return -1; |
| 7635 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7636 | |
| 7637 | if ((flags & F_ALT) && |
| 7638 | (type == 'x' || type == 'X')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7639 | /* When converting under %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7640 | * of issues that cause pain: |
| 7641 | * - when 0 is being converted, the C standard leaves off |
| 7642 | * the '0x' or '0X', which is inconsistent with other |
| 7643 | * %#x/%#X conversions and inconsistent with Python's |
| 7644 | * hex() function |
| 7645 | * - there are platforms that violate the standard and |
| 7646 | * convert 0 with the '0x' or '0X' |
| 7647 | * (Metrowerks, Compaq Tru64) |
| 7648 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7649 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7650 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7651 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7652 | * We can achieve the desired consistency by inserting our |
| 7653 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 7654 | * of %#x/%#X. |
| 7655 | * |
| 7656 | * Note that this is the same approach as used in |
| 7657 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 7658 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7659 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 7660 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 7661 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7662 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7663 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 7664 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7665 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 7666 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7667 | if (sign[0]) |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7668 | return longtounicode(buf, buflen, fmt, -x); |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7669 | else |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7670 | return longtounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7671 | } |
| 7672 | |
| 7673 | static int |
| 7674 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7675 | size_t buflen, |
| 7676 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7677 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7678 | /* presume that the buffer is at least 2 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7679 | if (PyUnicode_Check(v)) { |
| 7680 | if (PyUnicode_GET_SIZE(v) != 1) |
| 7681 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7682 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7683 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7684 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7685 | else if (PyString_Check(v)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7686 | if (PyString_GET_SIZE(v) != 1) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7687 | goto onError; |
| 7688 | buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0]; |
| 7689 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7690 | |
| 7691 | else { |
| 7692 | /* Integer input truncated to a character */ |
| 7693 | long x; |
| 7694 | x = PyInt_AsLong(v); |
| 7695 | if (x == -1 && PyErr_Occurred()) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7696 | goto onError; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 7697 | #ifdef Py_UNICODE_WIDE |
| 7698 | if (x < 0 || x > 0x10ffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 7699 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 7700 | "%c arg not in range(0x110000) " |
| 7701 | "(wide Python build)"); |
| 7702 | return -1; |
| 7703 | } |
| 7704 | #else |
| 7705 | if (x < 0 || x > 0xffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 7706 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 7707 | "%c arg not in range(0x10000) " |
| 7708 | "(narrow Python build)"); |
| 7709 | return -1; |
| 7710 | } |
| 7711 | #endif |
| 7712 | buf[0] = (Py_UNICODE) x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7713 | } |
| 7714 | buf[1] = '\0'; |
| 7715 | return 1; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7716 | |
| 7717 | onError: |
| 7718 | PyErr_SetString(PyExc_TypeError, |
| 7719 | "%c requires int or char"); |
| 7720 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7721 | } |
| 7722 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7723 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 7724 | |
| 7725 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 7726 | chars are formatted. XXX This is a magic number. Each formatting |
| 7727 | routine does bounds checking to ensure no overflow, but a better |
| 7728 | solution may be to malloc a buffer of appropriate size for each |
| 7729 | format. For now, the current solution is sufficient. |
| 7730 | */ |
| 7731 | #define FORMATBUFLEN (size_t)120 |
| 7732 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7733 | PyObject *PyUnicode_Format(PyObject *format, |
| 7734 | PyObject *args) |
| 7735 | { |
| 7736 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7737 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7738 | int args_owned = 0; |
| 7739 | PyUnicodeObject *result = NULL; |
| 7740 | PyObject *dict = NULL; |
| 7741 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7742 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7743 | if (format == NULL || args == NULL) { |
| 7744 | PyErr_BadInternalCall(); |
| 7745 | return NULL; |
| 7746 | } |
| 7747 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7748 | if (uformat == NULL) |
| 7749 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7750 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 7751 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 7752 | |
| 7753 | reslen = rescnt = fmtcnt + 100; |
| 7754 | result = _PyUnicode_New(reslen); |
| 7755 | if (result == NULL) |
| 7756 | goto onError; |
| 7757 | res = PyUnicode_AS_UNICODE(result); |
| 7758 | |
| 7759 | if (PyTuple_Check(args)) { |
| 7760 | arglen = PyTuple_Size(args); |
| 7761 | argidx = 0; |
| 7762 | } |
| 7763 | else { |
| 7764 | arglen = -1; |
| 7765 | argidx = -2; |
| 7766 | } |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 7767 | if (Py_Type(args)->tp_as_mapping && !PyTuple_Check(args) && |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 7768 | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7769 | dict = args; |
| 7770 | |
| 7771 | while (--fmtcnt >= 0) { |
| 7772 | if (*fmt != '%') { |
| 7773 | if (--rescnt < 0) { |
| 7774 | rescnt = fmtcnt + 100; |
| 7775 | reslen += rescnt; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7776 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 7777 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7778 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 7779 | --rescnt; |
| 7780 | } |
| 7781 | *res++ = *fmt++; |
| 7782 | } |
| 7783 | else { |
| 7784 | /* Got a format specifier */ |
| 7785 | int flags = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7786 | Py_ssize_t width = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7787 | int prec = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7788 | Py_UNICODE c = '\0'; |
| 7789 | Py_UNICODE fill; |
| 7790 | PyObject *v = NULL; |
| 7791 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7792 | Py_UNICODE *pbuf; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7793 | Py_UNICODE sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7794 | Py_ssize_t len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7795 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7796 | |
| 7797 | fmt++; |
| 7798 | if (*fmt == '(') { |
| 7799 | Py_UNICODE *keystart; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7800 | Py_ssize_t keylen; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7801 | PyObject *key; |
| 7802 | int pcount = 1; |
| 7803 | |
| 7804 | if (dict == NULL) { |
| 7805 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7806 | "format requires a mapping"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7807 | goto onError; |
| 7808 | } |
| 7809 | ++fmt; |
| 7810 | --fmtcnt; |
| 7811 | keystart = fmt; |
| 7812 | /* Skip over balanced parentheses */ |
| 7813 | while (pcount > 0 && --fmtcnt >= 0) { |
| 7814 | if (*fmt == ')') |
| 7815 | --pcount; |
| 7816 | else if (*fmt == '(') |
| 7817 | ++pcount; |
| 7818 | fmt++; |
| 7819 | } |
| 7820 | keylen = fmt - keystart - 1; |
| 7821 | if (fmtcnt < 0 || pcount > 0) { |
| 7822 | PyErr_SetString(PyExc_ValueError, |
| 7823 | "incomplete format key"); |
| 7824 | goto onError; |
| 7825 | } |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 7826 | #if 0 |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7827 | /* keys are converted to strings using UTF-8 and |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7828 | then looked up since Python uses strings to hold |
| 7829 | variables names etc. in its namespaces and we |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7830 | wouldn't want to break common idioms. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7831 | key = PyUnicode_EncodeUTF8(keystart, |
| 7832 | keylen, |
| 7833 | NULL); |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 7834 | #else |
| 7835 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 7836 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7837 | if (key == NULL) |
| 7838 | goto onError; |
| 7839 | if (args_owned) { |
| 7840 | Py_DECREF(args); |
| 7841 | args_owned = 0; |
| 7842 | } |
| 7843 | args = PyObject_GetItem(dict, key); |
| 7844 | Py_DECREF(key); |
| 7845 | if (args == NULL) { |
| 7846 | goto onError; |
| 7847 | } |
| 7848 | args_owned = 1; |
| 7849 | arglen = -1; |
| 7850 | argidx = -2; |
| 7851 | } |
| 7852 | while (--fmtcnt >= 0) { |
| 7853 | switch (c = *fmt++) { |
| 7854 | case '-': flags |= F_LJUST; continue; |
| 7855 | case '+': flags |= F_SIGN; continue; |
| 7856 | case ' ': flags |= F_BLANK; continue; |
| 7857 | case '#': flags |= F_ALT; continue; |
| 7858 | case '0': flags |= F_ZERO; continue; |
| 7859 | } |
| 7860 | break; |
| 7861 | } |
| 7862 | if (c == '*') { |
| 7863 | v = getnextarg(args, arglen, &argidx); |
| 7864 | if (v == NULL) |
| 7865 | goto onError; |
| 7866 | if (!PyInt_Check(v)) { |
| 7867 | PyErr_SetString(PyExc_TypeError, |
| 7868 | "* wants int"); |
| 7869 | goto onError; |
| 7870 | } |
| 7871 | width = PyInt_AsLong(v); |
| 7872 | if (width < 0) { |
| 7873 | flags |= F_LJUST; |
| 7874 | width = -width; |
| 7875 | } |
| 7876 | if (--fmtcnt >= 0) |
| 7877 | c = *fmt++; |
| 7878 | } |
| 7879 | else if (c >= '0' && c <= '9') { |
| 7880 | width = c - '0'; |
| 7881 | while (--fmtcnt >= 0) { |
| 7882 | c = *fmt++; |
| 7883 | if (c < '0' || c > '9') |
| 7884 | break; |
| 7885 | if ((width*10) / 10 != width) { |
| 7886 | PyErr_SetString(PyExc_ValueError, |
| 7887 | "width too big"); |
| 7888 | goto onError; |
| 7889 | } |
| 7890 | width = width*10 + (c - '0'); |
| 7891 | } |
| 7892 | } |
| 7893 | if (c == '.') { |
| 7894 | prec = 0; |
| 7895 | if (--fmtcnt >= 0) |
| 7896 | c = *fmt++; |
| 7897 | if (c == '*') { |
| 7898 | v = getnextarg(args, arglen, &argidx); |
| 7899 | if (v == NULL) |
| 7900 | goto onError; |
| 7901 | if (!PyInt_Check(v)) { |
| 7902 | PyErr_SetString(PyExc_TypeError, |
| 7903 | "* wants int"); |
| 7904 | goto onError; |
| 7905 | } |
| 7906 | prec = PyInt_AsLong(v); |
| 7907 | if (prec < 0) |
| 7908 | prec = 0; |
| 7909 | if (--fmtcnt >= 0) |
| 7910 | c = *fmt++; |
| 7911 | } |
| 7912 | else if (c >= '0' && c <= '9') { |
| 7913 | prec = c - '0'; |
| 7914 | while (--fmtcnt >= 0) { |
| 7915 | c = Py_CHARMASK(*fmt++); |
| 7916 | if (c < '0' || c > '9') |
| 7917 | break; |
| 7918 | if ((prec*10) / 10 != prec) { |
| 7919 | PyErr_SetString(PyExc_ValueError, |
| 7920 | "prec too big"); |
| 7921 | goto onError; |
| 7922 | } |
| 7923 | prec = prec*10 + (c - '0'); |
| 7924 | } |
| 7925 | } |
| 7926 | } /* prec */ |
| 7927 | if (fmtcnt >= 0) { |
| 7928 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7929 | if (--fmtcnt >= 0) |
| 7930 | c = *fmt++; |
| 7931 | } |
| 7932 | } |
| 7933 | if (fmtcnt < 0) { |
| 7934 | PyErr_SetString(PyExc_ValueError, |
| 7935 | "incomplete format"); |
| 7936 | goto onError; |
| 7937 | } |
| 7938 | if (c != '%') { |
| 7939 | v = getnextarg(args, arglen, &argidx); |
| 7940 | if (v == NULL) |
| 7941 | goto onError; |
| 7942 | } |
| 7943 | sign = 0; |
| 7944 | fill = ' '; |
| 7945 | switch (c) { |
| 7946 | |
| 7947 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7948 | pbuf = formatbuf; |
| 7949 | /* presume that buffer length is at least 1 */ |
| 7950 | pbuf[0] = '%'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7951 | len = 1; |
| 7952 | break; |
| 7953 | |
| 7954 | case 's': |
| 7955 | case 'r': |
| 7956 | if (PyUnicode_Check(v) && c == 's') { |
| 7957 | temp = v; |
| 7958 | Py_INCREF(temp); |
| 7959 | } |
| 7960 | else { |
| 7961 | PyObject *unicode; |
| 7962 | if (c == 's') |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 7963 | temp = PyObject_Unicode(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7964 | else |
| 7965 | temp = PyObject_Repr(v); |
| 7966 | if (temp == NULL) |
| 7967 | goto onError; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 7968 | if (PyUnicode_Check(temp)) |
| 7969 | /* nothing to do */; |
| 7970 | else if (PyString_Check(temp)) { |
| 7971 | /* convert to string to Unicode */ |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7972 | unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7973 | PyString_GET_SIZE(temp), |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7974 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7975 | "strict"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7976 | Py_DECREF(temp); |
| 7977 | temp = unicode; |
| 7978 | if (temp == NULL) |
| 7979 | goto onError; |
| 7980 | } |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 7981 | else { |
| 7982 | Py_DECREF(temp); |
| 7983 | PyErr_SetString(PyExc_TypeError, |
| 7984 | "%s argument has non-string str()"); |
| 7985 | goto onError; |
| 7986 | } |
| 7987 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7988 | pbuf = PyUnicode_AS_UNICODE(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7989 | len = PyUnicode_GET_SIZE(temp); |
| 7990 | if (prec >= 0 && len > prec) |
| 7991 | len = prec; |
| 7992 | break; |
| 7993 | |
| 7994 | case 'i': |
| 7995 | case 'd': |
| 7996 | case 'u': |
| 7997 | case 'o': |
| 7998 | case 'x': |
| 7999 | case 'X': |
| 8000 | if (c == 'i') |
| 8001 | c = 'd'; |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 8002 | if (PyLong_Check(v)) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8003 | temp = formatlong(v, flags, prec, c); |
| 8004 | if (!temp) |
| 8005 | goto onError; |
| 8006 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 8007 | len = PyUnicode_GET_SIZE(temp); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8008 | sign = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8009 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8010 | else { |
| 8011 | pbuf = formatbuf; |
| 8012 | len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8013 | flags, prec, c, v); |
| 8014 | if (len < 0) |
| 8015 | goto onError; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8016 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8017 | } |
| 8018 | if (flags & F_ZERO) |
| 8019 | fill = '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8020 | break; |
| 8021 | |
| 8022 | case 'e': |
| 8023 | case 'E': |
| 8024 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 8025 | case 'F': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8026 | case 'g': |
| 8027 | case 'G': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 8028 | if (c == 'F') |
| 8029 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8030 | pbuf = formatbuf; |
| 8031 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8032 | flags, prec, c, v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8033 | if (len < 0) |
| 8034 | goto onError; |
| 8035 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8036 | if (flags & F_ZERO) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8037 | fill = '0'; |
| 8038 | break; |
| 8039 | |
| 8040 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8041 | pbuf = formatbuf; |
| 8042 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8043 | if (len < 0) |
| 8044 | goto onError; |
| 8045 | break; |
| 8046 | |
| 8047 | default: |
| 8048 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 8049 | "unsupported format character '%c' (0x%x) " |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 8050 | "at index %zd", |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8051 | (31<=c && c<=126) ? (char)c : '?', |
Marc-André Lemburg | 24e53b6 | 2002-09-24 09:32:14 +0000 | [diff] [blame] | 8052 | (int)c, |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 8053 | (Py_ssize_t)(fmt - 1 - |
| 8054 | PyUnicode_AS_UNICODE(uformat))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8055 | goto onError; |
| 8056 | } |
| 8057 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8058 | if (*pbuf == '-' || *pbuf == '+') { |
| 8059 | sign = *pbuf++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8060 | len--; |
| 8061 | } |
| 8062 | else if (flags & F_SIGN) |
| 8063 | sign = '+'; |
| 8064 | else if (flags & F_BLANK) |
| 8065 | sign = ' '; |
| 8066 | else |
| 8067 | sign = 0; |
| 8068 | } |
| 8069 | if (width < len) |
| 8070 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8071 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8072 | reslen -= rescnt; |
| 8073 | rescnt = width + fmtcnt + 100; |
| 8074 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8075 | if (reslen < 0) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8076 | Py_XDECREF(temp); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8077 | PyErr_NoMemory(); |
| 8078 | goto onError; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8079 | } |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8080 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 8081 | Py_XDECREF(temp); |
| 8082 | goto onError; |
| 8083 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8084 | res = PyUnicode_AS_UNICODE(result) |
| 8085 | + reslen - rescnt; |
| 8086 | } |
| 8087 | if (sign) { |
| 8088 | if (fill != ' ') |
| 8089 | *res++ = sign; |
| 8090 | rescnt--; |
| 8091 | if (width > len) |
| 8092 | width--; |
| 8093 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8094 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 8095 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8096 | assert(pbuf[1] == c); |
| 8097 | if (fill != ' ') { |
| 8098 | *res++ = *pbuf++; |
| 8099 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8100 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8101 | rescnt -= 2; |
| 8102 | width -= 2; |
| 8103 | if (width < 0) |
| 8104 | width = 0; |
| 8105 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8106 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8107 | if (width > len && !(flags & F_LJUST)) { |
| 8108 | do { |
| 8109 | --rescnt; |
| 8110 | *res++ = fill; |
| 8111 | } while (--width > len); |
| 8112 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8113 | if (fill == ' ') { |
| 8114 | if (sign) |
| 8115 | *res++ = sign; |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8116 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8117 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8118 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8119 | *res++ = *pbuf++; |
| 8120 | *res++ = *pbuf++; |
| 8121 | } |
| 8122 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8123 | Py_UNICODE_COPY(res, pbuf, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8124 | res += len; |
| 8125 | rescnt -= len; |
| 8126 | while (--width >= len) { |
| 8127 | --rescnt; |
| 8128 | *res++ = ' '; |
| 8129 | } |
| 8130 | if (dict && (argidx < arglen) && c != '%') { |
| 8131 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 8132 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8133 | Py_XDECREF(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8134 | goto onError; |
| 8135 | } |
| 8136 | Py_XDECREF(temp); |
| 8137 | } /* '%' */ |
| 8138 | } /* until end */ |
| 8139 | if (argidx < arglen && !dict) { |
| 8140 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 8141 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8142 | goto onError; |
| 8143 | } |
| 8144 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8145 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
| 8146 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8147 | if (args_owned) { |
| 8148 | Py_DECREF(args); |
| 8149 | } |
| 8150 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8151 | return (PyObject *)result; |
| 8152 | |
| 8153 | onError: |
| 8154 | Py_XDECREF(result); |
| 8155 | Py_DECREF(uformat); |
| 8156 | if (args_owned) { |
| 8157 | Py_DECREF(args); |
| 8158 | } |
| 8159 | return NULL; |
| 8160 | } |
| 8161 | |
| 8162 | static PyBufferProcs unicode_as_buffer = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8163 | (readbufferproc) unicode_buffer_getreadbuf, |
| 8164 | (writebufferproc) unicode_buffer_getwritebuf, |
| 8165 | (segcountproc) unicode_buffer_getsegcount, |
| 8166 | (charbufferproc) unicode_buffer_getcharbuf, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8167 | }; |
| 8168 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 8169 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8170 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 8171 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8172 | static PyObject * |
| 8173 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8174 | { |
| 8175 | PyObject *x = NULL; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 8176 | static char *kwlist[] = {"string", "encoding", "errors", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8177 | char *encoding = NULL; |
| 8178 | char *errors = NULL; |
| 8179 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8180 | if (type != &PyUnicode_Type) |
| 8181 | return unicode_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8182 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
| 8183 | kwlist, &x, &encoding, &errors)) |
| 8184 | return NULL; |
| 8185 | if (x == NULL) |
| 8186 | return (PyObject *)_PyUnicode_New(0); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 8187 | if (encoding == NULL && errors == NULL) |
| 8188 | return PyObject_Unicode(x); |
| 8189 | else |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8190 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
| 8191 | } |
| 8192 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8193 | static PyObject * |
| 8194 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8195 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8196 | PyUnicodeObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8197 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8198 | |
| 8199 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 8200 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 8201 | if (tmp == NULL) |
| 8202 | return NULL; |
| 8203 | assert(PyUnicode_Check(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8204 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8205 | if (pnew == NULL) { |
| 8206 | Py_DECREF(tmp); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8207 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8208 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8209 | pnew->str = PyMem_NEW(Py_UNICODE, n+1); |
| 8210 | if (pnew->str == NULL) { |
| 8211 | _Py_ForgetReference((PyObject *)pnew); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8212 | PyObject_Del(pnew); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8213 | Py_DECREF(tmp); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 8214 | return PyErr_NoMemory(); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8215 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8216 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 8217 | pnew->length = n; |
| 8218 | pnew->hash = tmp->hash; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8219 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8220 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8221 | } |
| 8222 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8223 | PyDoc_STRVAR(unicode_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8224 | "unicode(string [, encoding[, errors]]) -> object\n\ |
| 8225 | \n\ |
| 8226 | Create a new Unicode object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 8227 | encoding defaults to the current default string encoding.\n\ |
| 8228 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8229 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8230 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 8231 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8232 | "unicode", /* tp_name */ |
| 8233 | sizeof(PyUnicodeObject), /* tp_size */ |
| 8234 | 0, /* tp_itemsize */ |
| 8235 | /* Slots */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 8236 | (destructor)unicode_dealloc, /* tp_dealloc */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8237 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8238 | 0, /* tp_getattr */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8239 | 0, /* tp_setattr */ |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 8240 | 0, /* tp_compare */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 8241 | unicode_repr, /* tp_repr */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8242 | &unicode_as_number, /* tp_as_number */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8243 | &unicode_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8244 | &unicode_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8245 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 8246 | 0, /* tp_call*/ |
| 8247 | (reprfunc) unicode_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8248 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 8249 | 0, /* tp_setattro */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8250 | &unicode_as_buffer, /* tp_as_buffer */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8251 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
Neal Norwitz | ee3a1b5 | 2007-02-25 19:44:48 +0000 | [diff] [blame] | 8252 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8253 | unicode_doc, /* tp_doc */ |
| 8254 | 0, /* tp_traverse */ |
| 8255 | 0, /* tp_clear */ |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 8256 | PyUnicode_RichCompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8257 | 0, /* tp_weaklistoffset */ |
| 8258 | 0, /* tp_iter */ |
| 8259 | 0, /* tp_iternext */ |
| 8260 | unicode_methods, /* tp_methods */ |
| 8261 | 0, /* tp_members */ |
| 8262 | 0, /* tp_getset */ |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 8263 | &PyBaseString_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8264 | 0, /* tp_dict */ |
| 8265 | 0, /* tp_descr_get */ |
| 8266 | 0, /* tp_descr_set */ |
| 8267 | 0, /* tp_dictoffset */ |
| 8268 | 0, /* tp_init */ |
| 8269 | 0, /* tp_alloc */ |
| 8270 | unicode_new, /* tp_new */ |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8271 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8272 | }; |
| 8273 | |
| 8274 | /* Initialize the Unicode implementation */ |
| 8275 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 8276 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8277 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8278 | int i; |
| 8279 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 8280 | /* XXX - move this array to unicodectype.c ? */ |
| 8281 | Py_UNICODE linebreak[] = { |
| 8282 | 0x000A, /* LINE FEED */ |
| 8283 | 0x000D, /* CARRIAGE RETURN */ |
| 8284 | 0x001C, /* FILE SEPARATOR */ |
| 8285 | 0x001D, /* GROUP SEPARATOR */ |
| 8286 | 0x001E, /* RECORD SEPARATOR */ |
| 8287 | 0x0085, /* NEXT LINE */ |
| 8288 | 0x2028, /* LINE SEPARATOR */ |
| 8289 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 8290 | }; |
| 8291 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8292 | /* Init the implementation */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8293 | unicode_freelist = NULL; |
| 8294 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8295 | unicode_empty = _PyUnicode_New(0); |
Neal Norwitz | e1fdb32 | 2006-07-21 05:32:28 +0000 | [diff] [blame] | 8296 | if (!unicode_empty) |
| 8297 | return; |
| 8298 | |
Marc-André Lemburg | 90e8147 | 2000-06-07 09:13:21 +0000 | [diff] [blame] | 8299 | strcpy(unicode_default_encoding, "ascii"); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8300 | for (i = 0; i < 256; i++) |
| 8301 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 8302 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 8303 | Py_FatalError("Can't initialize 'unicode'"); |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 8304 | |
| 8305 | /* initialize the linebreak bloom filter */ |
| 8306 | bloom_linebreak = make_bloom_mask( |
| 8307 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 8308 | ); |
Neal Norwitz | de4c78a | 2006-06-13 08:28:19 +0000 | [diff] [blame] | 8309 | |
| 8310 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8311 | } |
| 8312 | |
| 8313 | /* Finalize the Unicode implementation */ |
| 8314 | |
| 8315 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 8316 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8317 | { |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8318 | PyUnicodeObject *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8319 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8320 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 8321 | Py_XDECREF(unicode_empty); |
| 8322 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8323 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8324 | for (i = 0; i < 256; i++) { |
| 8325 | if (unicode_latin1[i]) { |
| 8326 | Py_DECREF(unicode_latin1[i]); |
| 8327 | unicode_latin1[i] = NULL; |
| 8328 | } |
| 8329 | } |
| 8330 | |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8331 | for (u = unicode_freelist; u != NULL;) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8332 | PyUnicodeObject *v = u; |
| 8333 | u = *(PyUnicodeObject **)u; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 8334 | if (v->str) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 8335 | PyMem_DEL(v->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 8336 | Py_XDECREF(v->defenc); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8337 | PyObject_Del(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8338 | } |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8339 | unicode_freelist = NULL; |
| 8340 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8341 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 8342 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 8343 | #ifdef __cplusplus |
| 8344 | } |
| 8345 | #endif |
| 8346 | |
| 8347 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 8348 | /* |
| 8349 | Local variables: |
| 8350 | c-basic-offset: 4 |
| 8351 | indent-tabs-mode: nil |
| 8352 | End: |
| 8353 | */ |